Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "Scheduler" |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 21 | #include "Scheduler.h" |
| 22 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 23 | #include <android-base/properties.h> |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 25 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
| 26 | #include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h> |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 27 | #include <configstore/Utils.h> |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 28 | #include <ftl/enum.h> |
ramindani | a556d07 | 2022-06-14 23:25:11 +0000 | [diff] [blame] | 29 | #include <ftl/fake_guard.h> |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 30 | #include <ftl/small_map.h> |
Ady Abraham | 9243bba | 2023-02-10 15:31:14 -0800 | [diff] [blame] | 31 | #include <gui/TraceUtils.h> |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 32 | #include <gui/WindowInfo.h> |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 33 | #include <system/window.h> |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 34 | #include <utils/Timers.h> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 35 | |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 36 | #include <FrameTimeline/FrameTimeline.h> |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 37 | #include <scheduler/interface/ICompositor.h> |
| 38 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 39 | #include <algorithm> |
| 40 | #include <cinttypes> |
| 41 | #include <cstdint> |
| 42 | #include <functional> |
| 43 | #include <memory> |
| 44 | #include <numeric> |
| 45 | |
| 46 | #include "../Layer.h" |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 47 | #include "Display/DisplayMap.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 48 | #include "EventThread.h" |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 49 | #include "FrameRateOverrideMappings.h" |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 50 | #include "FrontEnd/LayerHandle.h" |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 51 | #include "OneShotTimer.h" |
Sundong Ahn | d5e08f6 | 2018-12-12 20:27:28 +0900 | [diff] [blame] | 52 | #include "SurfaceFlingerProperties.h" |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 53 | #include "VSyncPredictor.h" |
| 54 | #include "VSyncReactor.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 55 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 56 | #define RETURN_IF_INVALID_HANDLE(handle, ...) \ |
| 57 | do { \ |
| 58 | if (mConnections.count(handle) == 0) { \ |
| 59 | ALOGE("Invalid connection handle %" PRIuPTR, handle.id); \ |
| 60 | return __VA_ARGS__; \ |
| 61 | } \ |
| 62 | } while (false) |
| 63 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 64 | namespace android::scheduler { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 65 | |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 66 | Scheduler::Scheduler(ICompositor& compositor, ISchedulerCallback& callback, FeatureFlags features, |
| 67 | sp<VsyncModulator> modulatorPtr) |
| 68 | : impl::MessageQueue(compositor), |
| 69 | mFeatures(features), |
| 70 | mVsyncModulator(std::move(modulatorPtr)), |
| 71 | mSchedulerCallback(callback) {} |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 72 | |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 73 | Scheduler::~Scheduler() { |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 74 | // MessageQueue depends on VsyncSchedule, so first destroy it. |
| 75 | // Otherwise, MessageQueue will get destroyed after Scheduler's dtor, |
| 76 | // which will cause a use-after-free issue. |
| 77 | Impl::destroyVsync(); |
| 78 | |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 79 | // Stop timers and wait for their threads to exit. |
| 80 | mDisplayPowerTimer.reset(); |
| 81 | mTouchTimer.reset(); |
| 82 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 83 | // Stop idle timer and clear callbacks, as the RefreshRateSelector may outlive the Scheduler. |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 84 | demoteLeaderDisplay(); |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 87 | void Scheduler::startTimers() { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 88 | using namespace sysprop; |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 89 | using namespace std::string_literals; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 90 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 91 | if (const int64_t millis = set_touch_timer_ms(0); millis > 0) { |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 92 | // Touch events are coming to SF every 100ms, so the timer needs to be higher than that |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 93 | mTouchTimer.emplace( |
Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 94 | "TouchTimer", std::chrono::milliseconds(millis), |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 95 | [this] { touchTimerCallback(TimerState::Reset); }, |
| 96 | [this] { touchTimerCallback(TimerState::Expired); }); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 97 | mTouchTimer->start(); |
| 98 | } |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 99 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 100 | if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) { |
| 101 | mDisplayPowerTimer.emplace( |
Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 102 | "DisplayPowerTimer", std::chrono::milliseconds(millis), |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 103 | [this] { displayPowerTimerCallback(TimerState::Reset); }, |
| 104 | [this] { displayPowerTimerCallback(TimerState::Expired); }); |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 105 | mDisplayPowerTimer->start(); |
| 106 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 109 | void Scheduler::setLeaderDisplay(std::optional<PhysicalDisplayId> leaderIdOpt) { |
| 110 | demoteLeaderDisplay(); |
Dominik Laskowski | 59db956 | 2022-10-27 16:18:53 -0400 | [diff] [blame] | 111 | |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 112 | std::scoped_lock lock(mDisplayLock); |
| 113 | promoteLeaderDisplay(leaderIdOpt); |
Lloyd Pique | 1f9f1a4 | 2019-01-31 13:04:00 -0800 | [diff] [blame] | 114 | } |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 115 | |
Dominik Laskowski | b5a094b | 2022-10-27 12:00:12 -0400 | [diff] [blame] | 116 | void Scheduler::registerDisplay(PhysicalDisplayId displayId, RefreshRateSelectorPtr selectorPtr) { |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 117 | demoteLeaderDisplay(); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 118 | |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 119 | std::scoped_lock lock(mDisplayLock); |
Dominik Laskowski | b5a094b | 2022-10-27 12:00:12 -0400 | [diff] [blame] | 120 | mRefreshRateSelectors.emplace_or_replace(displayId, std::move(selectorPtr)); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 121 | |
| 122 | promoteLeaderDisplay(); |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void Scheduler::unregisterDisplay(PhysicalDisplayId displayId) { |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 126 | demoteLeaderDisplay(); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 127 | |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 128 | std::scoped_lock lock(mDisplayLock); |
Dominik Laskowski | b5a094b | 2022-10-27 12:00:12 -0400 | [diff] [blame] | 129 | mRefreshRateSelectors.erase(displayId); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 130 | |
Leon Scroggins III | da21f42 | 2023-01-30 20:17:56 -0500 | [diff] [blame] | 131 | // Do not allow removing the final display. Code in the scheduler expects |
| 132 | // there to be at least one display. (This may be relaxed in the future with |
| 133 | // headless virtual display.) |
| 134 | LOG_ALWAYS_FATAL_IF(mRefreshRateSelectors.empty(), "Cannot unregister all displays!"); |
| 135 | |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 136 | promoteLeaderDisplay(); |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 139 | void Scheduler::run() { |
| 140 | while (true) { |
| 141 | waitMessage(); |
| 142 | } |
| 143 | } |
| 144 | |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 145 | void Scheduler::onFrameSignal(ICompositor& compositor, VsyncId vsyncId, |
| 146 | TimePoint expectedVsyncTime) { |
| 147 | const TimePoint frameTime = SchedulerClock::now(); |
| 148 | |
| 149 | if (!compositor.commit(frameTime, vsyncId, expectedVsyncTime)) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | compositor.composite(frameTime, vsyncId); |
| 154 | compositor.sample(); |
| 155 | } |
| 156 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 157 | void Scheduler::createVsyncSchedule(FeatureFlags features) { |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 158 | mVsyncSchedule = std::make_unique<VsyncSchedule>(features); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 161 | std::optional<Fps> Scheduler::getFrameRateOverride(uid_t uid) const { |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 162 | const bool supportsFrameRateOverrideByContent = |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 163 | leaderSelectorPtr()->supportsAppFrameRateOverrideByContent(); |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 164 | return mFrameRateOverrideMappings |
| 165 | .getFrameRateOverrideForUid(uid, supportsFrameRateOverrideByContent); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 168 | bool Scheduler::isVsyncValid(TimePoint expectedVsyncTimestamp, uid_t uid) const { |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 169 | const auto frameRate = getFrameRateOverride(uid); |
| 170 | if (!frameRate.has_value()) { |
| 171 | return true; |
| 172 | } |
| 173 | |
Ady Abraham | 9243bba | 2023-02-10 15:31:14 -0800 | [diff] [blame] | 174 | ATRACE_FORMAT("%s uid: %d frameRate: %s", __func__, uid, to_string(*frameRate).c_str()); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 175 | return mVsyncSchedule->getTracker().isVSyncInPhase(expectedVsyncTimestamp.ns(), *frameRate); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 178 | bool Scheduler::isVsyncInPhase(TimePoint timePoint, const Fps frameRate) const { |
| 179 | return mVsyncSchedule->getTracker().isVSyncInPhase(timePoint.ns(), frameRate); |
Huihong Luo | 1768cb0 | 2022-10-11 11:10:34 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 182 | impl::EventThread::ThrottleVsyncCallback Scheduler::makeThrottleVsyncCallback() const { |
| 183 | return [this](nsecs_t expectedVsyncTimestamp, uid_t uid) { |
| 184 | return !isVsyncValid(TimePoint::fromNs(expectedVsyncTimestamp), uid); |
| 185 | }; |
| 186 | } |
Ady Abraham | 64c2fc0 | 2020-12-29 12:07:50 -0800 | [diff] [blame] | 187 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 188 | impl::EventThread::GetVsyncPeriodFunction Scheduler::makeGetVsyncPeriodFunction() const { |
| 189 | return [this](uid_t uid) { |
| 190 | const Fps refreshRate = leaderSelectorPtr()->getActiveMode().fps; |
| 191 | const nsecs_t currentPeriod = mVsyncSchedule->period().ns() ?: refreshRate.getPeriodNsecs(); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 192 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 193 | const auto frameRate = getFrameRateOverride(uid); |
| 194 | if (!frameRate.has_value()) { |
| 195 | return currentPeriod; |
| 196 | } |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 197 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 198 | const auto divisor = RefreshRateSelector::getFrameRateDivisor(refreshRate, *frameRate); |
| 199 | if (divisor <= 1) { |
| 200 | return currentPeriod; |
| 201 | } |
| 202 | return currentPeriod * divisor; |
| 203 | }; |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 206 | ConnectionHandle Scheduler::createEventThread(Cycle cycle, |
| 207 | frametimeline::TokenManager* tokenManager, |
| 208 | std::chrono::nanoseconds workDuration, |
| 209 | std::chrono::nanoseconds readyDuration) { |
| 210 | auto eventThread = std::make_unique<impl::EventThread>(cycle == Cycle::Render ? "app" : "appSf", |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 211 | *mVsyncSchedule, tokenManager, |
| 212 | makeThrottleVsyncCallback(), |
| 213 | makeGetVsyncPeriodFunction(), |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 214 | workDuration, readyDuration); |
| 215 | |
| 216 | auto& handle = cycle == Cycle::Render ? mAppConnectionHandle : mSfConnectionHandle; |
| 217 | handle = createConnection(std::move(eventThread)); |
| 218 | return handle; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 219 | } |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 220 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 221 | ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 222 | const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++}; |
| 223 | ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id); |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 224 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 225 | auto connection = createConnectionInternal(eventThread.get()); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 226 | |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 227 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 228 | mConnections.emplace(handle, Connection{connection, std::move(eventThread)}); |
| 229 | return handle; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 232 | sp<EventThreadConnection> Scheduler::createConnectionInternal( |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 233 | EventThread* eventThread, EventRegistrationFlags eventRegistration, |
| 234 | const sp<IBinder>& layerHandle) { |
| 235 | int32_t layerId = static_cast<int32_t>(LayerHandle::getLayerId(layerHandle)); |
| 236 | auto connection = eventThread->createEventConnection([&] { resync(); }, eventRegistration); |
| 237 | mLayerHistory.attachChoreographer(layerId, connection); |
| 238 | return connection; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 241 | sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection( |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 242 | ConnectionHandle handle, EventRegistrationFlags eventRegistration, |
| 243 | const sp<IBinder>& layerHandle) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 244 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 245 | RETURN_IF_INVALID_HANDLE(handle, nullptr); |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 246 | return createConnectionInternal(mConnections[handle].thread.get(), eventRegistration, |
| 247 | layerHandle); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 250 | sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 251 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 252 | RETURN_IF_INVALID_HANDLE(handle, nullptr); |
| 253 | return mConnections[handle].connection; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 256 | void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId, |
| 257 | bool connected) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 258 | android::EventThread* thread; |
| 259 | { |
| 260 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 261 | RETURN_IF_INVALID_HANDLE(handle); |
| 262 | thread = mConnections[handle].thread.get(); |
| 263 | } |
| 264 | |
| 265 | thread->onHotplugReceived(displayId, connected); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame^] | 268 | void Scheduler::enableSyntheticVsync(bool enable) { |
| 269 | // TODO(b/241285945): Remove connection handles. |
| 270 | const ConnectionHandle handle = mAppConnectionHandle; |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 271 | android::EventThread* thread; |
| 272 | { |
| 273 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 274 | RETURN_IF_INVALID_HANDLE(handle); |
| 275 | thread = mConnections[handle].thread.get(); |
| 276 | } |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame^] | 277 | thread->enableSyntheticVsync(enable); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 280 | void Scheduler::onFrameRateOverridesChanged(ConnectionHandle handle, PhysicalDisplayId displayId) { |
Andy Yu | d6a3620 | 2022-01-26 04:08:22 -0800 | [diff] [blame] | 281 | const bool supportsFrameRateOverrideByContent = |
Ady Abraham | 6863606 | 2022-11-16 17:07:25 -0800 | [diff] [blame] | 282 | leaderSelectorPtr()->supportsAppFrameRateOverrideByContent(); |
Andy Yu | d6a3620 | 2022-01-26 04:08:22 -0800 | [diff] [blame] | 283 | |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 284 | std::vector<FrameRateOverride> overrides = |
Andy Yu | d6a3620 | 2022-01-26 04:08:22 -0800 | [diff] [blame] | 285 | mFrameRateOverrideMappings.getAllFrameRateOverrides(supportsFrameRateOverrideByContent); |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 286 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 287 | android::EventThread* thread; |
| 288 | { |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 289 | std::lock_guard lock(mConnectionsLock); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 290 | RETURN_IF_INVALID_HANDLE(handle); |
| 291 | thread = mConnections[handle].thread.get(); |
| 292 | } |
| 293 | thread->onFrameRateOverridesChanged(displayId, std::move(overrides)); |
| 294 | } |
| 295 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 296 | void Scheduler::onPrimaryDisplayModeChanged(ConnectionHandle handle, const FrameRateMode& mode) { |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 297 | { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 298 | std::lock_guard<std::mutex> lock(mPolicyLock); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 299 | // Cache the last reported modes for primary display. |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 300 | mPolicy.cachedModeChangedParams = {handle, mode}; |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 301 | |
| 302 | // Invalidate content based refresh rate selection so it could be calculated |
| 303 | // again for the new refresh rate. |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 304 | mPolicy.contentRequirements.clear(); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 305 | } |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 306 | onNonPrimaryDisplayModeChanged(handle, mode); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 309 | void Scheduler::dispatchCachedReportedMode() { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 310 | // Check optional fields first. |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 311 | if (!mPolicy.modeOpt) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 312 | ALOGW("No mode ID found, not dispatching cached mode."); |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 313 | return; |
| 314 | } |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 315 | if (!mPolicy.cachedModeChangedParams) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 316 | ALOGW("No mode changed params found, not dispatching cached mode."); |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 317 | return; |
| 318 | } |
| 319 | |
Ady Abraham | d159170 | 2021-07-27 16:27:56 -0700 | [diff] [blame] | 320 | // If the mode is not the current mode, this means that a |
| 321 | // mode change is in progress. In that case we shouldn't dispatch an event |
| 322 | // as it will be dispatched when the current mode changes. |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 323 | if (leaderSelectorPtr()->getActiveMode() != mPolicy.modeOpt) { |
Ady Abraham | d159170 | 2021-07-27 16:27:56 -0700 | [diff] [blame] | 324 | return; |
| 325 | } |
| 326 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 327 | // If there is no change from cached mode, there is no need to dispatch an event |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 328 | if (*mPolicy.modeOpt == mPolicy.cachedModeChangedParams->mode) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 332 | mPolicy.cachedModeChangedParams->mode = *mPolicy.modeOpt; |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 333 | onNonPrimaryDisplayModeChanged(mPolicy.cachedModeChangedParams->handle, |
| 334 | mPolicy.cachedModeChangedParams->mode); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 337 | void Scheduler::onNonPrimaryDisplayModeChanged(ConnectionHandle handle, const FrameRateMode& mode) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 338 | android::EventThread* thread; |
| 339 | { |
| 340 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 341 | RETURN_IF_INVALID_HANDLE(handle); |
| 342 | thread = mConnections[handle].thread.get(); |
| 343 | } |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 344 | thread->onModeChanged(mode); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 347 | size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 348 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 349 | RETURN_IF_INVALID_HANDLE(handle, 0); |
| 350 | return mConnections[handle].thread->getEventThreadConnectionCount(); |
| 351 | } |
| 352 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 353 | void Scheduler::dump(ConnectionHandle handle, std::string& result) const { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 354 | android::EventThread* thread; |
| 355 | { |
| 356 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 357 | RETURN_IF_INVALID_HANDLE(handle); |
| 358 | thread = mConnections.at(handle).thread.get(); |
| 359 | } |
| 360 | thread->dump(result); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 363 | void Scheduler::setDuration(ConnectionHandle handle, std::chrono::nanoseconds workDuration, |
| 364 | std::chrono::nanoseconds readyDuration) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 365 | android::EventThread* thread; |
| 366 | { |
| 367 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 368 | RETURN_IF_INVALID_HANDLE(handle); |
| 369 | thread = mConnections[handle].thread.get(); |
| 370 | } |
| 371 | thread->setDuration(workDuration, readyDuration); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 372 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 373 | |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 374 | void Scheduler::setVsyncConfigSet(const VsyncConfigSet& configs, Period vsyncPeriod) { |
| 375 | setVsyncConfig(mVsyncModulator->setVsyncConfigSet(configs), vsyncPeriod); |
| 376 | } |
| 377 | |
| 378 | void Scheduler::setVsyncConfig(const VsyncConfig& config, Period vsyncPeriod) { |
| 379 | setDuration(mAppConnectionHandle, |
| 380 | /* workDuration */ config.appWorkDuration, |
| 381 | /* readyDuration */ config.sfWorkDuration); |
| 382 | setDuration(mSfConnectionHandle, |
| 383 | /* workDuration */ vsyncPeriod, |
| 384 | /* readyDuration */ config.sfWorkDuration); |
| 385 | setDuration(config.sfWorkDuration); |
| 386 | } |
| 387 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 388 | void Scheduler::enableHardwareVsync() { |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 389 | mVsyncSchedule->enableHardwareVsync(mSchedulerCallback); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 392 | void Scheduler::disableHardwareVsync(bool disallow) { |
| 393 | mVsyncSchedule->disableHardwareVsync(mSchedulerCallback, disallow); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 396 | void Scheduler::resyncToHardwareVsync(bool allowToEnable, Fps refreshRate) { |
| 397 | if (mVsyncSchedule->isHardwareVsyncAllowed(allowToEnable) && refreshRate.isValid()) { |
| 398 | mVsyncSchedule->startPeriodTransition(mSchedulerCallback, refreshRate.getPeriod()); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 399 | } |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | void Scheduler::setRenderRate(Fps renderFrameRate) { |
| 403 | const auto mode = leaderSelectorPtr()->getActiveMode(); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 404 | |
| 405 | using fps_approx_ops::operator!=; |
| 406 | LOG_ALWAYS_FATAL_IF(renderFrameRate != mode.fps, |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 407 | "Mismatch in render frame rates. Selector: %s, Scheduler: %s", |
| 408 | to_string(mode.fps).c_str(), to_string(renderFrameRate).c_str()); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 409 | |
| 410 | ALOGV("%s %s (%s)", __func__, to_string(mode.fps).c_str(), |
| 411 | to_string(mode.modePtr->getFps()).c_str()); |
| 412 | |
Ady Abraham | fdc049c | 2023-02-17 14:52:05 +0000 | [diff] [blame] | 413 | mVsyncSchedule->getTracker().setRenderRate(renderFrameRate); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 416 | void Scheduler::resync() { |
Long Ling | 457bef9 | 2019-09-11 14:43:11 -0700 | [diff] [blame] | 417 | static constexpr nsecs_t kIgnoreDelay = ms2ns(750); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 418 | |
| 419 | const nsecs_t now = systemTime(); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 420 | const nsecs_t last = mLastResyncTime.exchange(now); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 421 | |
| 422 | if (now - last > kIgnoreDelay) { |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 423 | const auto refreshRate = leaderSelectorPtr()->getActiveMode().modePtr->getFps(); |
| 424 | resyncToHardwareVsync(false, refreshRate); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 428 | bool Scheduler::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriodIn) { |
| 429 | const auto hwcVsyncPeriod = ftl::Optional(hwcVsyncPeriodIn).transform([](nsecs_t nanos) { |
| 430 | return Period::fromNs(nanos); |
| 431 | }); |
| 432 | return mVsyncSchedule->addResyncSample(mSchedulerCallback, TimePoint::fromNs(timestamp), |
| 433 | hwcVsyncPeriod); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 436 | void Scheduler::addPresentFence(std::shared_ptr<FenceTime> fence) { |
| 437 | if (mVsyncSchedule->getController().addPresentFence(std::move(fence))) { |
| 438 | enableHardwareVsync(); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 439 | } else { |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 440 | disableHardwareVsync(false); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 444 | void Scheduler::registerLayer(Layer* layer) { |
Marin Shalamanov | 4be385e | 2021-04-23 13:25:30 +0200 | [diff] [blame] | 445 | // If the content detection feature is off, we still keep the layer history, |
| 446 | // since we use it for other features (like Frame Rate API), so layers |
| 447 | // still need to be registered. |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 448 | mLayerHistory.registerLayer(layer, mFeatures.test(Feature::kContentDetection)); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 451 | void Scheduler::deregisterLayer(Layer* layer) { |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 452 | mLayerHistory.deregisterLayer(layer); |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 455 | void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime, |
| 456 | LayerHistory::LayerUpdateType updateType) { |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 457 | if (leaderSelectorPtr()->canSwitch()) { |
| 458 | mLayerHistory.record(layer, presentTime, systemTime(), updateType); |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 459 | } |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 460 | } |
| 461 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 462 | void Scheduler::setModeChangePending(bool pending) { |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 463 | mLayerHistory.setModeChangePending(pending); |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 466 | void Scheduler::setDefaultFrameRateCompatibility(Layer* layer) { |
| 467 | mLayerHistory.setDefaultFrameRateCompatibility(layer, |
| 468 | mFeatures.test(Feature::kContentDetection)); |
| 469 | } |
| 470 | |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 471 | void Scheduler::chooseRefreshRateForContent() { |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 472 | const auto selectorPtr = leaderSelectorPtr(); |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 473 | if (!selectorPtr->canSwitch()) return; |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 474 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 475 | ATRACE_CALL(); |
| 476 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 477 | LayerHistory::Summary summary = mLayerHistory.summarize(*selectorPtr, systemTime()); |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 478 | applyPolicy(&Policy::contentRequirements, std::move(summary)); |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 481 | void Scheduler::resetIdleTimer() { |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 482 | leaderSelectorPtr()->resetIdleTimer(); |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 485 | void Scheduler::onTouchHint() { |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 486 | if (mTouchTimer) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 487 | mTouchTimer->reset(); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 488 | leaderSelectorPtr()->resetKernelIdleTimer(); |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 489 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 492 | void Scheduler::setDisplayPowerMode(hal::PowerMode powerMode) { |
| 493 | { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 494 | std::lock_guard<std::mutex> lock(mPolicyLock); |
Rachel Lee | 6a9731d | 2022-06-06 17:08:14 -0700 | [diff] [blame] | 495 | mPolicy.displayPowerMode = powerMode; |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 496 | } |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 497 | mVsyncSchedule->getController().setDisplayPowerMode(powerMode); |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 498 | |
| 499 | if (mDisplayPowerTimer) { |
| 500 | mDisplayPowerTimer->reset(); |
| 501 | } |
| 502 | |
| 503 | // Display Power event will boost the refresh rate to performance. |
| 504 | // Clear Layer History to get fresh FPS detection |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 505 | mLayerHistory.clear(); |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 508 | void Scheduler::kernelIdleTimerCallback(TimerState state) { |
| 509 | ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state)); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 510 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 511 | // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate |
| 512 | // magic number |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 513 | const Fps refreshRate = leaderSelectorPtr()->getActiveMode().modePtr->getFps(); |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 514 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 515 | constexpr Fps FPS_THRESHOLD_FOR_KERNEL_TIMER = 65_Hz; |
| 516 | using namespace fps_approx_ops; |
| 517 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 518 | if (state == TimerState::Reset && refreshRate > FPS_THRESHOLD_FOR_KERNEL_TIMER) { |
Alec Mouri | 7f01518 | 2019-07-11 13:56:22 -0700 | [diff] [blame] | 519 | // If we're not in performance mode then the kernel timer shouldn't do |
| 520 | // anything, as the refresh rate during DPU power collapse will be the |
| 521 | // same. |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 522 | resyncToHardwareVsync(true /* makeAvailable */, refreshRate); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 523 | } else if (state == TimerState::Expired && refreshRate <= FPS_THRESHOLD_FOR_KERNEL_TIMER) { |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 524 | // Disable HW VSYNC if the timer expired, as we don't need it enabled if |
| 525 | // we're not pushing frames, and if we're in PERFORMANCE mode then we'll |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 526 | // need to update the VsyncController model anyway. |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 527 | disableHardwareVsync(false /* makeUnavailable */); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 528 | } |
Ady Abraham | a09852a | 2020-02-20 14:23:42 -0800 | [diff] [blame] | 529 | |
| 530 | mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 533 | void Scheduler::idleTimerCallback(TimerState state) { |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 534 | applyPolicy(&Policy::idleTimer, state); |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 535 | ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state)); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 538 | void Scheduler::touchTimerCallback(TimerState state) { |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 539 | const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive; |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 540 | // Touch event will boost the refresh rate to performance. |
| 541 | // Clear layer history to get fresh FPS detection. |
| 542 | // NOTE: Instead of checking all the layers, we should be checking the layer |
| 543 | // that is currently on top. b/142507166 will give us this capability. |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 544 | if (applyPolicy(&Policy::touch, touch).touch) { |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 545 | mLayerHistory.clear(); |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 546 | } |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 547 | ATRACE_INT("TouchState", static_cast<int>(touch)); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 550 | void Scheduler::displayPowerTimerCallback(TimerState state) { |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 551 | applyPolicy(&Policy::displayPowerTimer, state); |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 552 | ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state)); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 555 | void Scheduler::dump(utils::Dumper& dumper) const { |
| 556 | using namespace std::string_view_literals; |
Ady Abraham | 4f960d1 | 2021-10-13 16:59:49 -0700 | [diff] [blame] | 557 | |
| 558 | { |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 559 | utils::Dumper::Section section(dumper, "Features"sv); |
| 560 | |
| 561 | for (Feature feature : ftl::enum_range<Feature>()) { |
| 562 | if (const auto flagOpt = ftl::flag_name(feature)) { |
| 563 | dumper.dump(flagOpt->substr(1), mFeatures.test(feature)); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | { |
| 568 | utils::Dumper::Section section(dumper, "Policy"sv); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 569 | { |
| 570 | std::scoped_lock lock(mDisplayLock); |
| 571 | ftl::FakeGuard guard(kMainThreadContext); |
| 572 | dumper.dump("leaderDisplayId"sv, mLeaderDisplayId); |
| 573 | } |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 574 | dumper.dump("layerHistory"sv, mLayerHistory.dump()); |
| 575 | dumper.dump("touchTimer"sv, mTouchTimer.transform(&OneShotTimer::interval)); |
| 576 | dumper.dump("displayPowerTimer"sv, mDisplayPowerTimer.transform(&OneShotTimer::interval)); |
| 577 | } |
| 578 | |
| 579 | mFrameRateOverrideMappings.dump(dumper); |
| 580 | dumper.eol(); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 583 | void Scheduler::dumpVsync(std::string& out) const { |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 584 | mVsyncSchedule->dump(out); |
Ady Abraham | 8735eac | 2020-08-12 16:35:04 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 587 | bool Scheduler::updateFrameRateOverrides(GlobalSignals consideredSignals, Fps displayRefreshRate) { |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 588 | if (consideredSignals.idle) return false; |
| 589 | |
| 590 | const auto frameRateOverrides = |
| 591 | leaderSelectorPtr()->getFrameRateOverrides(mPolicy.contentRequirements, |
| 592 | displayRefreshRate, consideredSignals); |
| 593 | |
| 594 | // Note that RefreshRateSelector::supportsFrameRateOverrideByContent is checked when querying |
| 595 | // the FrameRateOverrideMappings rather than here. |
| 596 | return mFrameRateOverrideMappings.updateFrameRateOverridesByContent(frameRateOverrides); |
| 597 | } |
| 598 | |
| 599 | void Scheduler::promoteLeaderDisplay(std::optional<PhysicalDisplayId> leaderIdOpt) { |
| 600 | // TODO(b/241286431): Choose the leader display. |
| 601 | mLeaderDisplayId = leaderIdOpt.value_or(mRefreshRateSelectors.begin()->first); |
| 602 | ALOGI("Display %s is the leader", to_string(*mLeaderDisplayId).c_str()); |
| 603 | |
| 604 | if (const auto leaderPtr = leaderSelectorPtrLocked()) { |
| 605 | leaderPtr->setIdleTimerCallbacks( |
| 606 | {.platform = {.onReset = [this] { idleTimerCallback(TimerState::Reset); }, |
| 607 | .onExpired = [this] { idleTimerCallback(TimerState::Expired); }}, |
| 608 | .kernel = {.onReset = [this] { kernelIdleTimerCallback(TimerState::Reset); }, |
| 609 | .onExpired = |
| 610 | [this] { kernelIdleTimerCallback(TimerState::Expired); }}}); |
| 611 | |
| 612 | leaderPtr->startIdleTimer(); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 613 | } |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | void Scheduler::demoteLeaderDisplay() { |
| 617 | // No need to lock for reads on kMainThreadContext. |
| 618 | if (const auto leaderPtr = FTL_FAKE_GUARD(mDisplayLock, leaderSelectorPtrLocked())) { |
| 619 | leaderPtr->stopIdleTimer(); |
| 620 | leaderPtr->clearIdleTimerCallbacks(); |
| 621 | } |
| 622 | |
| 623 | // Clear state that depends on the leader's RefreshRateSelector. |
| 624 | std::scoped_lock lock(mPolicyLock); |
| 625 | mPolicy = {}; |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 628 | template <typename S, typename T> |
| 629 | auto Scheduler::applyPolicy(S Policy::*statePtr, T&& newState) -> GlobalSignals { |
Ady Abraham | 73c3df5 | 2023-01-12 18:09:31 -0800 | [diff] [blame] | 630 | ATRACE_CALL(); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 631 | std::vector<display::DisplayModeRequest> modeRequests; |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 632 | GlobalSignals consideredSignals; |
| 633 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 634 | bool refreshRateChanged = false; |
| 635 | bool frameRateOverridesChanged; |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 636 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 637 | { |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 638 | std::scoped_lock lock(mPolicyLock); |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 639 | |
| 640 | auto& currentState = mPolicy.*statePtr; |
| 641 | if (currentState == newState) return {}; |
| 642 | currentState = std::forward<T>(newState); |
| 643 | |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 644 | DisplayModeChoiceMap modeChoices; |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 645 | ftl::Optional<FrameRateMode> modeOpt; |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 646 | { |
| 647 | std::scoped_lock lock(mDisplayLock); |
| 648 | ftl::FakeGuard guard(kMainThreadContext); |
| 649 | |
| 650 | modeChoices = chooseDisplayModes(); |
| 651 | |
| 652 | // TODO(b/240743786): The leader display's mode must change for any DisplayModeRequest |
| 653 | // to go through. Fix this by tracking per-display Scheduler::Policy and timers. |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 654 | std::tie(modeOpt, consideredSignals) = |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 655 | modeChoices.get(*mLeaderDisplayId) |
| 656 | .transform([](const DisplayModeChoice& choice) { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 657 | return std::make_pair(choice.mode, choice.consideredSignals); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 658 | }) |
| 659 | .value(); |
| 660 | } |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 661 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 662 | modeRequests.reserve(modeChoices.size()); |
| 663 | for (auto& [id, choice] : modeChoices) { |
| 664 | modeRequests.emplace_back( |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 665 | display::DisplayModeRequest{.mode = std::move(choice.mode), |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 666 | .emitEvent = !choice.consideredSignals.idle}); |
| 667 | } |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 668 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 669 | frameRateOverridesChanged = updateFrameRateOverrides(consideredSignals, modeOpt->fps); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 670 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 671 | if (mPolicy.modeOpt != modeOpt) { |
| 672 | mPolicy.modeOpt = modeOpt; |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 673 | refreshRateChanged = true; |
| 674 | } else { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 675 | // We don't need to change the display mode, but we might need to send an event |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 676 | // about a mode change, since it was suppressed if previously considered idle. |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 677 | if (!consideredSignals.idle) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 678 | dispatchCachedReportedMode(); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 679 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 680 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 681 | } |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 682 | if (refreshRateChanged) { |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 683 | mSchedulerCallback.requestDisplayModes(std::move(modeRequests)); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 684 | } |
| 685 | if (frameRateOverridesChanged) { |
| 686 | mSchedulerCallback.triggerOnFrameRateOverridesChanged(); |
| 687 | } |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 688 | return consideredSignals; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 691 | auto Scheduler::chooseDisplayModes() const -> DisplayModeChoiceMap { |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 692 | ATRACE_CALL(); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 693 | |
Ady Abraham | 6863606 | 2022-11-16 17:07:25 -0800 | [diff] [blame] | 694 | using RankedRefreshRates = RefreshRateSelector::RankedFrameRates; |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 695 | display::PhysicalDisplayVector<RankedRefreshRates> perDisplayRanking; |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 696 | |
| 697 | // Tallies the score of a refresh rate across `displayCount` displays. |
| 698 | struct RefreshRateTally { |
| 699 | explicit RefreshRateTally(float score) : score(score) {} |
| 700 | |
| 701 | float score; |
| 702 | size_t displayCount = 1; |
| 703 | }; |
| 704 | |
| 705 | // Chosen to exceed a typical number of refresh rates across displays. |
| 706 | constexpr size_t kStaticCapacity = 8; |
| 707 | ftl::SmallMap<Fps, RefreshRateTally, kStaticCapacity, FpsApproxEqual> refreshRateTallies; |
| 708 | |
| 709 | const auto globalSignals = makeGlobalSignals(); |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 710 | |
Dominik Laskowski | b5a094b | 2022-10-27 12:00:12 -0400 | [diff] [blame] | 711 | for (const auto& [id, selectorPtr] : mRefreshRateSelectors) { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 712 | auto rankedFrameRates = |
Ady Abraham | 6863606 | 2022-11-16 17:07:25 -0800 | [diff] [blame] | 713 | selectorPtr->getRankedFrameRates(mPolicy.contentRequirements, globalSignals); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 714 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 715 | for (const auto& [frameRateMode, score] : rankedFrameRates.ranking) { |
| 716 | const auto [it, inserted] = refreshRateTallies.try_emplace(frameRateMode.fps, score); |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 717 | |
| 718 | if (!inserted) { |
| 719 | auto& tally = it->second; |
| 720 | tally.score += score; |
| 721 | tally.displayCount++; |
| 722 | } |
| 723 | } |
| 724 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 725 | perDisplayRanking.push_back(std::move(rankedFrameRates)); |
Dominik Laskowski | 95df6a1 | 2022-10-07 18:11:07 -0400 | [diff] [blame] | 726 | } |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 727 | |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 728 | auto maxScoreIt = refreshRateTallies.cbegin(); |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 729 | |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 730 | // Find the first refresh rate common to all displays. |
| 731 | while (maxScoreIt != refreshRateTallies.cend() && |
Dominik Laskowski | b5a094b | 2022-10-27 12:00:12 -0400 | [diff] [blame] | 732 | maxScoreIt->second.displayCount != mRefreshRateSelectors.size()) { |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 733 | ++maxScoreIt; |
| 734 | } |
| 735 | |
| 736 | if (maxScoreIt != refreshRateTallies.cend()) { |
| 737 | // Choose the highest refresh rate common to all displays, if any. |
| 738 | for (auto it = maxScoreIt + 1; it != refreshRateTallies.cend(); ++it) { |
| 739 | const auto [fps, tally] = *it; |
| 740 | |
Dominik Laskowski | b5a094b | 2022-10-27 12:00:12 -0400 | [diff] [blame] | 741 | if (tally.displayCount == mRefreshRateSelectors.size() && |
| 742 | tally.score > maxScoreIt->second.score) { |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 743 | maxScoreIt = it; |
| 744 | } |
ramindani | 7c48728 | 2022-10-10 16:17:51 -0700 | [diff] [blame] | 745 | } |
| 746 | } |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 747 | |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 748 | const std::optional<Fps> chosenFps = maxScoreIt != refreshRateTallies.cend() |
| 749 | ? std::make_optional(maxScoreIt->first) |
| 750 | : std::nullopt; |
| 751 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 752 | DisplayModeChoiceMap modeChoices; |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 753 | |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 754 | using fps_approx_ops::operator==; |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 755 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 756 | for (auto& [ranking, signals] : perDisplayRanking) { |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 757 | if (!chosenFps) { |
Ady Abraham | 6863606 | 2022-11-16 17:07:25 -0800 | [diff] [blame] | 758 | const auto& [frameRateMode, _] = ranking.front(); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 759 | modeChoices.try_emplace(frameRateMode.modePtr->getPhysicalDisplayId(), |
| 760 | DisplayModeChoice{frameRateMode, signals}); |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 761 | continue; |
| 762 | } |
| 763 | |
Ady Abraham | 6863606 | 2022-11-16 17:07:25 -0800 | [diff] [blame] | 764 | for (auto& [frameRateMode, _] : ranking) { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 765 | if (frameRateMode.fps == *chosenFps) { |
| 766 | modeChoices.try_emplace(frameRateMode.modePtr->getPhysicalDisplayId(), |
| 767 | DisplayModeChoice{frameRateMode, signals}); |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 768 | break; |
| 769 | } |
| 770 | } |
| 771 | } |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 772 | return modeChoices; |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Dominik Laskowski | 95df6a1 | 2022-10-07 18:11:07 -0400 | [diff] [blame] | 775 | GlobalSignals Scheduler::makeGlobalSignals() const { |
ramindani | 38c8498 | 2022-08-29 18:02:57 +0000 | [diff] [blame] | 776 | const bool powerOnImminent = mDisplayPowerTimer && |
| 777 | (mPolicy.displayPowerMode != hal::PowerMode::ON || |
| 778 | mPolicy.displayPowerTimer == TimerState::Reset); |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 779 | |
Dominik Laskowski | 95df6a1 | 2022-10-07 18:11:07 -0400 | [diff] [blame] | 780 | return {.touch = mTouchTimer && mPolicy.touch == TouchState::Active, |
| 781 | .idle = mPolicy.idleTimer == TimerState::Expired, |
| 782 | .powerOnImminent = powerOnImminent}; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 785 | FrameRateMode Scheduler::getPreferredDisplayMode() { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 786 | std::lock_guard<std::mutex> lock(mPolicyLock); |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 787 | const auto frameRateMode = |
| 788 | leaderSelectorPtr() |
| 789 | ->getRankedFrameRates(mPolicy.contentRequirements, makeGlobalSignals()) |
| 790 | .ranking.front() |
| 791 | .frameRateMode; |
Dominik Laskowski | 95df6a1 | 2022-10-07 18:11:07 -0400 | [diff] [blame] | 792 | |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 793 | // Make sure the stored mode is up to date. |
| 794 | mPolicy.modeOpt = frameRateMode; |
| 795 | |
| 796 | return frameRateMode; |
Daniel Solomon | 0f0ddc1 | 2019-08-19 19:31:09 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 799 | void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) { |
Ady Abraham | 3a77a7b | 2019-12-02 18:46:59 -0800 | [diff] [blame] | 800 | std::lock_guard<std::mutex> lock(mVsyncTimelineLock); |
| 801 | mLastVsyncPeriodChangeTimeline = std::make_optional(timeline); |
| 802 | |
| 803 | const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count(); |
| 804 | if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) { |
| 805 | mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime; |
| 806 | } |
| 807 | } |
| 808 | |
Dominik Laskowski | dd5827a | 2022-03-17 12:44:23 -0700 | [diff] [blame] | 809 | bool Scheduler::onPostComposition(nsecs_t presentTime) { |
| 810 | std::lock_guard<std::mutex> lock(mVsyncTimelineLock); |
| 811 | if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) { |
| 812 | if (presentTime < mLastVsyncPeriodChangeTimeline->refreshTimeNanos) { |
| 813 | // We need to composite again as refreshTimeNanos is still in the future. |
| 814 | return true; |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 815 | } |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 816 | |
Dominik Laskowski | dd5827a | 2022-03-17 12:44:23 -0700 | [diff] [blame] | 817 | mLastVsyncPeriodChangeTimeline->refreshRequired = false; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 818 | } |
Dominik Laskowski | dd5827a | 2022-03-17 12:44:23 -0700 | [diff] [blame] | 819 | return false; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Ady Abraham | 7825c68 | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 822 | void Scheduler::onActiveDisplayAreaChanged(uint32_t displayArea) { |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 823 | mLayerHistory.setDisplayArea(displayArea); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 824 | } |
| 825 | |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 826 | void Scheduler::setGameModeRefreshRateForUid(FrameRateOverride frameRateOverride) { |
| 827 | if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) { |
| 828 | return; |
| 829 | } |
| 830 | |
| 831 | mFrameRateOverrideMappings.setGameModeRefreshRateForUid(frameRateOverride); |
| 832 | } |
| 833 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 834 | void Scheduler::setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride) { |
| 835 | if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) { |
| 836 | return; |
| 837 | } |
| 838 | |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 839 | mFrameRateOverrideMappings.setPreferredRefreshRateForUid(frameRateOverride); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 840 | } |
| 841 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 842 | } // namespace android::scheduler |