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