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> |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 34 | #include <ui/DisplayMap.h> |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 35 | #include <utils/Timers.h> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 36 | |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 37 | #include <FrameTimeline/FrameTimeline.h> |
Dominik Laskowski | 63f1279 | 2023-01-21 16:58:22 -0500 | [diff] [blame] | 38 | #include <scheduler/interface/ICompositor.h> |
| 39 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 40 | #include <algorithm> |
| 41 | #include <cinttypes> |
| 42 | #include <cstdint> |
| 43 | #include <functional> |
| 44 | #include <memory> |
| 45 | #include <numeric> |
| 46 | |
| 47 | #include "../Layer.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" |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 53 | #include "VSyncTracker.h" |
| 54 | #include "VsyncController.h" |
| 55 | #include "VsyncSchedule.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 56 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 57 | #define RETURN_IF_INVALID_HANDLE(handle, ...) \ |
| 58 | do { \ |
| 59 | if (mConnections.count(handle) == 0) { \ |
| 60 | ALOGE("Invalid connection handle %" PRIuPTR, handle.id); \ |
| 61 | return __VA_ARGS__; \ |
| 62 | } \ |
| 63 | } while (false) |
| 64 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 65 | namespace android::scheduler { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 66 | |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 67 | Scheduler::Scheduler(ICompositor& compositor, ISchedulerCallback& callback, FeatureFlags features, |
| 68 | sp<VsyncModulator> modulatorPtr) |
| 69 | : impl::MessageQueue(compositor), |
| 70 | mFeatures(features), |
| 71 | mVsyncModulator(std::move(modulatorPtr)), |
| 72 | mSchedulerCallback(callback) {} |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 73 | |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 74 | Scheduler::~Scheduler() { |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 75 | // MessageQueue depends on VsyncSchedule, so first destroy it. |
| 76 | // Otherwise, MessageQueue will get destroyed after Scheduler's dtor, |
| 77 | // which will cause a use-after-free issue. |
| 78 | Impl::destroyVsync(); |
| 79 | |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 80 | // Stop timers and wait for their threads to exit. |
| 81 | mDisplayPowerTimer.reset(); |
| 82 | mTouchTimer.reset(); |
| 83 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 84 | // 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] | 85 | demotePacesetterDisplay(); |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 88 | void Scheduler::startTimers() { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 89 | using namespace sysprop; |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 90 | using namespace std::string_literals; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 91 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 92 | if (const int64_t millis = set_touch_timer_ms(0); millis > 0) { |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 93 | // 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] | 94 | mTouchTimer.emplace( |
Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 95 | "TouchTimer", std::chrono::milliseconds(millis), |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 96 | [this] { touchTimerCallback(TimerState::Reset); }, |
| 97 | [this] { touchTimerCallback(TimerState::Expired); }); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 98 | mTouchTimer->start(); |
| 99 | } |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 100 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 101 | if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) { |
| 102 | mDisplayPowerTimer.emplace( |
Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 103 | "DisplayPowerTimer", std::chrono::milliseconds(millis), |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 104 | [this] { displayPowerTimerCallback(TimerState::Reset); }, |
| 105 | [this] { displayPowerTimerCallback(TimerState::Expired); }); |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 106 | mDisplayPowerTimer->start(); |
| 107 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 110 | void Scheduler::setPacesetterDisplay(std::optional<PhysicalDisplayId> pacesetterIdOpt) { |
| 111 | demotePacesetterDisplay(); |
Dominik Laskowski | 59db956 | 2022-10-27 16:18:53 -0400 | [diff] [blame] | 112 | |
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) { |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 117 | auto schedulePtr = std::make_shared<VsyncSchedule>(displayId, mFeatures, |
| 118 | [this](PhysicalDisplayId id, bool enable) { |
| 119 | onHardwareVsyncRequest(id, enable); |
| 120 | }); |
| 121 | |
| 122 | registerDisplayInternal(displayId, std::move(selectorPtr), std::move(schedulePtr)); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void Scheduler::registerDisplayInternal(PhysicalDisplayId displayId, |
| 126 | RefreshRateSelectorPtr selectorPtr, |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 127 | VsyncSchedulePtr schedulePtr) { |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 128 | demotePacesetterDisplay(); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 129 | |
Dominik Laskowski | 008bec0 | 2023-03-14 12:04:58 -0400 | [diff] [blame] | 130 | auto [pacesetterVsyncSchedule, isNew] = [&]() FTL_FAKE_GUARD(kMainThreadContext) { |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 131 | std::scoped_lock lock(mDisplayLock); |
Dominik Laskowski | 008bec0 | 2023-03-14 12:04:58 -0400 | [diff] [blame] | 132 | const bool isNew = mDisplays |
| 133 | .emplace_or_replace(displayId, std::move(selectorPtr), |
| 134 | std::move(schedulePtr)) |
| 135 | .second; |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 136 | |
Dominik Laskowski | 008bec0 | 2023-03-14 12:04:58 -0400 | [diff] [blame] | 137 | return std::make_pair(promotePacesetterDisplayLocked(), isNew); |
| 138 | }(); |
| 139 | |
Leon Scroggins | 39d2534 | 2023-04-19 17:11:01 +0000 | [diff] [blame] | 140 | applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule)); |
Dominik Laskowski | 008bec0 | 2023-03-14 12:04:58 -0400 | [diff] [blame] | 141 | |
| 142 | // Disable hardware VSYNC if the registration is new, as opposed to a renewal. |
| 143 | if (isNew) { |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 144 | onHardwareVsyncRequest(displayId, false); |
Dominik Laskowski | 008bec0 | 2023-03-14 12:04:58 -0400 | [diff] [blame] | 145 | } |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void Scheduler::unregisterDisplay(PhysicalDisplayId displayId) { |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 149 | demotePacesetterDisplay(); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 150 | |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 151 | std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule; |
| 152 | { |
| 153 | std::scoped_lock lock(mDisplayLock); |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 154 | mDisplays.erase(displayId); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 155 | |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 156 | // Do not allow removing the final display. Code in the scheduler expects |
| 157 | // there to be at least one display. (This may be relaxed in the future with |
| 158 | // headless virtual display.) |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 159 | LOG_ALWAYS_FATAL_IF(mDisplays.empty(), "Cannot unregister all displays!"); |
Leon Scroggins III | da21f42 | 2023-01-30 20:17:56 -0500 | [diff] [blame] | 160 | |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 161 | pacesetterVsyncSchedule = promotePacesetterDisplayLocked(); |
| 162 | } |
Leon Scroggins | 39d2534 | 2023-04-19 17:11:01 +0000 | [diff] [blame] | 163 | applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule)); |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Dominik Laskowski | 756b789 | 2021-08-04 12:53:59 -0700 | [diff] [blame] | 166 | void Scheduler::run() { |
| 167 | while (true) { |
| 168 | waitMessage(); |
| 169 | } |
| 170 | } |
| 171 | |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 172 | void Scheduler::onFrameSignal(ICompositor& compositor, VsyncId vsyncId, |
| 173 | TimePoint expectedVsyncTime) { |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 174 | mPacesetterFrameTargeter.beginFrame({.frameBeginTime = SchedulerClock::now(), |
| 175 | .vsyncId = vsyncId, |
| 176 | .expectedVsyncTime = expectedVsyncTime, |
| 177 | .sfWorkDuration = |
| 178 | mVsyncModulator->getVsyncConfig().sfWorkDuration}, |
| 179 | *getVsyncSchedule()); |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 180 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 181 | if (!compositor.commit(mPacesetterFrameTargeter.target())) { |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 185 | const auto compositeResult = compositor.composite(mPacesetterFrameTargeter); |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 186 | compositor.sample(); |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 187 | |
| 188 | mPacesetterFrameTargeter.endFrame(compositeResult); |
Dominik Laskowski | 08fbd85 | 2022-07-14 08:53:42 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 191 | std::optional<Fps> Scheduler::getFrameRateOverride(uid_t uid) const { |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 192 | const bool supportsFrameRateOverrideByContent = |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 193 | pacesetterSelectorPtr()->supportsAppFrameRateOverrideByContent(); |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 194 | return mFrameRateOverrideMappings |
| 195 | .getFrameRateOverrideForUid(uid, supportsFrameRateOverrideByContent); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 198 | bool Scheduler::isVsyncValid(TimePoint expectedVsyncTime, uid_t uid) const { |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 199 | const auto frameRate = getFrameRateOverride(uid); |
| 200 | if (!frameRate.has_value()) { |
| 201 | return true; |
| 202 | } |
| 203 | |
Ady Abraham | 9243bba | 2023-02-10 15:31:14 -0800 | [diff] [blame] | 204 | ATRACE_FORMAT("%s uid: %d frameRate: %s", __func__, uid, to_string(*frameRate).c_str()); |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 205 | return getVsyncSchedule()->getTracker().isVSyncInPhase(expectedVsyncTime.ns(), *frameRate); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 208 | bool Scheduler::isVsyncInPhase(TimePoint expectedVsyncTime, Fps frameRate) const { |
| 209 | return getVsyncSchedule()->getTracker().isVSyncInPhase(expectedVsyncTime.ns(), frameRate); |
Huihong Luo | 1768cb0 | 2022-10-11 11:10:34 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 212 | impl::EventThread::ThrottleVsyncCallback Scheduler::makeThrottleVsyncCallback() const { |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 213 | return [this](nsecs_t expectedVsyncTime, uid_t uid) { |
| 214 | return !isVsyncValid(TimePoint::fromNs(expectedVsyncTime), uid); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 215 | }; |
| 216 | } |
Ady Abraham | 64c2fc0 | 2020-12-29 12:07:50 -0800 | [diff] [blame] | 217 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 218 | impl::EventThread::GetVsyncPeriodFunction Scheduler::makeGetVsyncPeriodFunction() const { |
| 219 | return [this](uid_t uid) { |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 220 | const auto [refreshRate, period] = [this] { |
| 221 | std::scoped_lock lock(mDisplayLock); |
| 222 | const auto pacesetterOpt = pacesetterDisplayLocked(); |
| 223 | LOG_ALWAYS_FATAL_IF(!pacesetterOpt); |
| 224 | const Display& pacesetter = *pacesetterOpt; |
| 225 | return std::make_pair(pacesetter.selectorPtr->getActiveMode().fps, |
| 226 | pacesetter.schedulePtr->period()); |
| 227 | }(); |
| 228 | |
| 229 | const Period currentPeriod = period != Period::zero() ? period : refreshRate.getPeriod(); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 230 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 231 | const auto frameRate = getFrameRateOverride(uid); |
| 232 | if (!frameRate.has_value()) { |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 233 | return currentPeriod.ns(); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 234 | } |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 235 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 236 | const auto divisor = RefreshRateSelector::getFrameRateDivisor(refreshRate, *frameRate); |
| 237 | if (divisor <= 1) { |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 238 | return currentPeriod.ns(); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 239 | } |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 240 | return currentPeriod.ns() * divisor; |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 241 | }; |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 244 | ConnectionHandle Scheduler::createEventThread(Cycle cycle, |
| 245 | frametimeline::TokenManager* tokenManager, |
| 246 | std::chrono::nanoseconds workDuration, |
| 247 | std::chrono::nanoseconds readyDuration) { |
| 248 | 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] | 249 | getVsyncSchedule(), tokenManager, |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 250 | makeThrottleVsyncCallback(), |
| 251 | makeGetVsyncPeriodFunction(), |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 252 | workDuration, readyDuration); |
| 253 | |
| 254 | auto& handle = cycle == Cycle::Render ? mAppConnectionHandle : mSfConnectionHandle; |
| 255 | handle = createConnection(std::move(eventThread)); |
| 256 | return handle; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 257 | } |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 258 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 259 | ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 260 | const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++}; |
| 261 | ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id); |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 262 | |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 263 | auto connection = eventThread->createEventConnection([&] { resync(); }); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 264 | |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 265 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 266 | mConnections.emplace(handle, Connection{connection, std::move(eventThread)}); |
| 267 | return handle; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection( |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 271 | ConnectionHandle handle, EventRegistrationFlags eventRegistration, |
| 272 | const sp<IBinder>& layerHandle) { |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 273 | const auto connection = [&]() -> sp<EventThreadConnection> { |
| 274 | std::scoped_lock lock(mConnectionsLock); |
| 275 | RETURN_IF_INVALID_HANDLE(handle, nullptr); |
| 276 | |
| 277 | return mConnections[handle].thread->createEventConnection([&] { resync(); }, |
| 278 | eventRegistration); |
| 279 | }(); |
| 280 | const auto layerId = static_cast<int32_t>(LayerHandle::getLayerId(layerHandle)); |
| 281 | |
| 282 | if (layerId != static_cast<int32_t>(UNASSIGNED_LAYER_ID)) { |
| 283 | // TODO(b/290409668): Moving the choreographer attachment to be a transaction that will be |
| 284 | // processed on the main thread. |
| 285 | mSchedulerCallback.onChoreographerAttached(); |
| 286 | |
| 287 | std::scoped_lock lock(mChoreographerLock); |
| 288 | const auto [iter, emplaced] = |
| 289 | mAttachedChoreographers.emplace(layerId, |
| 290 | AttachedChoreographers{Fps(), {connection}}); |
| 291 | if (!emplaced) { |
| 292 | iter->second.connections.emplace(connection); |
| 293 | connection->frameRate = iter->second.frameRate; |
| 294 | } |
| 295 | } |
| 296 | return connection; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 299 | sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 300 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 301 | RETURN_IF_INVALID_HANDLE(handle, nullptr); |
| 302 | return mConnections[handle].connection; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 305 | void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId, |
| 306 | bool connected) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 307 | android::EventThread* thread; |
| 308 | { |
| 309 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 310 | RETURN_IF_INVALID_HANDLE(handle); |
| 311 | thread = mConnections[handle].thread.get(); |
| 312 | } |
| 313 | |
| 314 | thread->onHotplugReceived(displayId, connected); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 317 | void Scheduler::enableSyntheticVsync(bool enable) { |
| 318 | // TODO(b/241285945): Remove connection handles. |
| 319 | const ConnectionHandle handle = mAppConnectionHandle; |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 320 | android::EventThread* thread; |
| 321 | { |
| 322 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 323 | RETURN_IF_INVALID_HANDLE(handle); |
| 324 | thread = mConnections[handle].thread.get(); |
| 325 | } |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 326 | thread->enableSyntheticVsync(enable); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 329 | void Scheduler::onFrameRateOverridesChanged(ConnectionHandle handle, PhysicalDisplayId displayId) { |
Andy Yu | d6a3620 | 2022-01-26 04:08:22 -0800 | [diff] [blame] | 330 | const bool supportsFrameRateOverrideByContent = |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 331 | pacesetterSelectorPtr()->supportsAppFrameRateOverrideByContent(); |
Andy Yu | d6a3620 | 2022-01-26 04:08:22 -0800 | [diff] [blame] | 332 | |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 333 | std::vector<FrameRateOverride> overrides = |
Andy Yu | d6a3620 | 2022-01-26 04:08:22 -0800 | [diff] [blame] | 334 | mFrameRateOverrideMappings.getAllFrameRateOverrides(supportsFrameRateOverrideByContent); |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 335 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 336 | android::EventThread* thread; |
| 337 | { |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 338 | std::lock_guard lock(mConnectionsLock); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 339 | RETURN_IF_INVALID_HANDLE(handle); |
| 340 | thread = mConnections[handle].thread.get(); |
| 341 | } |
| 342 | thread->onFrameRateOverridesChanged(displayId, std::move(overrides)); |
| 343 | } |
| 344 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 345 | void Scheduler::onPrimaryDisplayModeChanged(ConnectionHandle handle, const FrameRateMode& mode) { |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 346 | { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 347 | std::lock_guard<std::mutex> lock(mPolicyLock); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 348 | // Cache the last reported modes for primary display. |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 349 | mPolicy.cachedModeChangedParams = {handle, mode}; |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 350 | |
| 351 | // Invalidate content based refresh rate selection so it could be calculated |
| 352 | // again for the new refresh rate. |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 353 | mPolicy.contentRequirements.clear(); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 354 | } |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 355 | onNonPrimaryDisplayModeChanged(handle, mode); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 358 | void Scheduler::dispatchCachedReportedMode() { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 359 | // Check optional fields first. |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 360 | if (!mPolicy.modeOpt) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 361 | ALOGW("No mode ID found, not dispatching cached mode."); |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 362 | return; |
| 363 | } |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 364 | if (!mPolicy.cachedModeChangedParams) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 365 | ALOGW("No mode changed params found, not dispatching cached mode."); |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 366 | return; |
| 367 | } |
| 368 | |
Ady Abraham | d159170 | 2021-07-27 16:27:56 -0700 | [diff] [blame] | 369 | // If the mode is not the current mode, this means that a |
| 370 | // mode change is in progress. In that case we shouldn't dispatch an event |
| 371 | // as it will be dispatched when the current mode changes. |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 372 | if (pacesetterSelectorPtr()->getActiveMode() != mPolicy.modeOpt) { |
Ady Abraham | d159170 | 2021-07-27 16:27:56 -0700 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 376 | // 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] | 377 | if (*mPolicy.modeOpt == mPolicy.cachedModeChangedParams->mode) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 378 | return; |
| 379 | } |
| 380 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 381 | mPolicy.cachedModeChangedParams->mode = *mPolicy.modeOpt; |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 382 | onNonPrimaryDisplayModeChanged(mPolicy.cachedModeChangedParams->handle, |
| 383 | mPolicy.cachedModeChangedParams->mode); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 386 | void Scheduler::onNonPrimaryDisplayModeChanged(ConnectionHandle handle, const FrameRateMode& mode) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 387 | android::EventThread* thread; |
| 388 | { |
| 389 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 390 | RETURN_IF_INVALID_HANDLE(handle); |
| 391 | thread = mConnections[handle].thread.get(); |
| 392 | } |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 393 | thread->onModeChanged(mode); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 396 | size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 397 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 398 | RETURN_IF_INVALID_HANDLE(handle, 0); |
| 399 | return mConnections[handle].thread->getEventThreadConnectionCount(); |
| 400 | } |
| 401 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 402 | void Scheduler::dump(ConnectionHandle handle, std::string& result) const { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 403 | android::EventThread* thread; |
| 404 | { |
| 405 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 406 | RETURN_IF_INVALID_HANDLE(handle); |
| 407 | thread = mConnections.at(handle).thread.get(); |
| 408 | } |
| 409 | thread->dump(result); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 412 | void Scheduler::setDuration(ConnectionHandle handle, std::chrono::nanoseconds workDuration, |
| 413 | std::chrono::nanoseconds readyDuration) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 414 | android::EventThread* thread; |
| 415 | { |
| 416 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
| 417 | RETURN_IF_INVALID_HANDLE(handle); |
| 418 | thread = mConnections[handle].thread.get(); |
| 419 | } |
| 420 | thread->setDuration(workDuration, readyDuration); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 421 | } |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 422 | |
Dominik Laskowski | 1c99a00 | 2023-01-20 17:10:36 -0500 | [diff] [blame] | 423 | void Scheduler::setVsyncConfigSet(const VsyncConfigSet& configs, Period vsyncPeriod) { |
| 424 | setVsyncConfig(mVsyncModulator->setVsyncConfigSet(configs), vsyncPeriod); |
| 425 | } |
| 426 | |
| 427 | void Scheduler::setVsyncConfig(const VsyncConfig& config, Period vsyncPeriod) { |
| 428 | setDuration(mAppConnectionHandle, |
| 429 | /* workDuration */ config.appWorkDuration, |
| 430 | /* readyDuration */ config.sfWorkDuration); |
| 431 | setDuration(mSfConnectionHandle, |
| 432 | /* workDuration */ vsyncPeriod, |
| 433 | /* readyDuration */ config.sfWorkDuration); |
| 434 | setDuration(config.sfWorkDuration); |
| 435 | } |
| 436 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 437 | void Scheduler::enableHardwareVsync(PhysicalDisplayId id) { |
| 438 | auto schedule = getVsyncSchedule(id); |
Leon Scroggins III | 4235ea0 | 2023-04-17 15:14:20 -0400 | [diff] [blame] | 439 | LOG_ALWAYS_FATAL_IF(!schedule); |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 440 | schedule->enableHardwareVsync(); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 443 | void Scheduler::disableHardwareVsync(PhysicalDisplayId id, bool disallow) { |
| 444 | auto schedule = getVsyncSchedule(id); |
Leon Scroggins III | 4235ea0 | 2023-04-17 15:14:20 -0400 | [diff] [blame] | 445 | LOG_ALWAYS_FATAL_IF(!schedule); |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 446 | schedule->disableHardwareVsync(disallow); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 449 | void Scheduler::resyncAllToHardwareVsync(bool allowToEnable) { |
Rachel Lee | a5be328 | 2023-03-08 20:15:54 -0800 | [diff] [blame] | 450 | ATRACE_CALL(); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 451 | std::scoped_lock lock(mDisplayLock); |
| 452 | ftl::FakeGuard guard(kMainThreadContext); |
| 453 | |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 454 | for (const auto& [id, _] : mDisplays) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 455 | resyncToHardwareVsyncLocked(id, allowToEnable); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 456 | } |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 457 | } |
| 458 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 459 | void Scheduler::resyncToHardwareVsyncLocked(PhysicalDisplayId id, bool allowToEnable, |
| 460 | std::optional<Fps> refreshRate) { |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 461 | const auto displayOpt = mDisplays.get(id); |
Leon Scroggins III | 4235ea0 | 2023-04-17 15:14:20 -0400 | [diff] [blame] | 462 | if (!displayOpt) { |
| 463 | ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str()); |
| 464 | return; |
| 465 | } |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 466 | const Display& display = *displayOpt; |
| 467 | |
| 468 | if (display.schedulePtr->isHardwareVsyncAllowed(allowToEnable)) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 469 | if (!refreshRate) { |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 470 | refreshRate = display.selectorPtr->getActiveMode().modePtr->getFps(); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 471 | } |
| 472 | if (refreshRate->isValid()) { |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 473 | constexpr bool kForce = false; |
| 474 | display.schedulePtr->startPeriodTransition(refreshRate->getPeriod(), kForce); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 479 | void Scheduler::onHardwareVsyncRequest(PhysicalDisplayId id, bool enabled) { |
| 480 | static const auto& whence = __func__; |
| 481 | ATRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str()); |
| 482 | |
| 483 | // On main thread to serialize reads/writes of pending hardware VSYNC state. |
| 484 | static_cast<void>( |
| 485 | schedule([=]() FTL_FAKE_GUARD(mDisplayLock) FTL_FAKE_GUARD(kMainThreadContext) { |
| 486 | ATRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str()); |
| 487 | |
| 488 | if (const auto displayOpt = mDisplays.get(id)) { |
| 489 | auto& display = displayOpt->get(); |
| 490 | display.schedulePtr->setPendingHardwareVsyncState(enabled); |
| 491 | |
| 492 | if (display.powerMode != hal::PowerMode::OFF) { |
| 493 | mSchedulerCallback.requestHardwareVsync(id, enabled); |
| 494 | } |
| 495 | } |
| 496 | })); |
| 497 | } |
| 498 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 499 | void Scheduler::setRenderRate(PhysicalDisplayId id, Fps renderFrameRate) { |
| 500 | std::scoped_lock lock(mDisplayLock); |
| 501 | ftl::FakeGuard guard(kMainThreadContext); |
| 502 | |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 503 | const auto displayOpt = mDisplays.get(id); |
Leon Scroggins III | 4235ea0 | 2023-04-17 15:14:20 -0400 | [diff] [blame] | 504 | if (!displayOpt) { |
| 505 | ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str()); |
| 506 | return; |
| 507 | } |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 508 | const Display& display = *displayOpt; |
| 509 | const auto mode = display.selectorPtr->getActiveMode(); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 510 | |
| 511 | using fps_approx_ops::operator!=; |
| 512 | LOG_ALWAYS_FATAL_IF(renderFrameRate != mode.fps, |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 513 | "Mismatch in render frame rates. Selector: %s, Scheduler: %s, Display: " |
| 514 | "%" PRIu64, |
| 515 | 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] | 516 | |
| 517 | ALOGV("%s %s (%s)", __func__, to_string(mode.fps).c_str(), |
| 518 | to_string(mode.modePtr->getFps()).c_str()); |
| 519 | |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 520 | display.schedulePtr->getTracker().setRenderRate(renderFrameRate); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 523 | void Scheduler::resync() { |
Long Ling | 457bef9 | 2019-09-11 14:43:11 -0700 | [diff] [blame] | 524 | static constexpr nsecs_t kIgnoreDelay = ms2ns(750); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 525 | |
| 526 | const nsecs_t now = systemTime(); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 527 | const nsecs_t last = mLastResyncTime.exchange(now); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 528 | |
| 529 | if (now - last > kIgnoreDelay) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 530 | resyncAllToHardwareVsync(false /* allowToEnable */); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 534 | bool Scheduler::addResyncSample(PhysicalDisplayId id, nsecs_t timestamp, |
| 535 | std::optional<nsecs_t> hwcVsyncPeriodIn) { |
Leon Scroggins III | c275df4 | 2023-02-07 16:40:21 -0500 | [diff] [blame] | 536 | const auto hwcVsyncPeriod = ftl::Optional(hwcVsyncPeriodIn).transform([](nsecs_t nanos) { |
| 537 | return Period::fromNs(nanos); |
| 538 | }); |
Leon Scroggins III | 4235ea0 | 2023-04-17 15:14:20 -0400 | [diff] [blame] | 539 | auto schedule = getVsyncSchedule(id); |
| 540 | if (!schedule) { |
| 541 | ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str()); |
| 542 | return false; |
| 543 | } |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 544 | return schedule->addResyncSample(TimePoint::fromNs(timestamp), hwcVsyncPeriod); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 547 | void Scheduler::addPresentFence(PhysicalDisplayId id, std::shared_ptr<FenceTime> fence) { |
| 548 | auto schedule = getVsyncSchedule(id); |
Leon Scroggins III | 4235ea0 | 2023-04-17 15:14:20 -0400 | [diff] [blame] | 549 | LOG_ALWAYS_FATAL_IF(!schedule); |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 550 | if (const bool needMoreSignals = schedule->getController().addPresentFence(std::move(fence))) { |
| 551 | schedule->enableHardwareVsync(); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 552 | } else { |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 553 | constexpr bool kDisallow = false; |
| 554 | schedule->disableHardwareVsync(kDisallow); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 558 | void Scheduler::registerLayer(Layer* layer) { |
Marin Shalamanov | 4be385e | 2021-04-23 13:25:30 +0200 | [diff] [blame] | 559 | // If the content detection feature is off, we still keep the layer history, |
| 560 | // since we use it for other features (like Frame Rate API), so layers |
| 561 | // still need to be registered. |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 562 | mLayerHistory.registerLayer(layer, mFeatures.test(Feature::kContentDetection)); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 565 | void Scheduler::deregisterLayer(Layer* layer) { |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 566 | mLayerHistory.deregisterLayer(layer); |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 569 | void Scheduler::onLayerDestroyed(Layer* layer) { |
| 570 | std::scoped_lock lock(mChoreographerLock); |
| 571 | mAttachedChoreographers.erase(layer->getSequence()); |
| 572 | } |
| 573 | |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 574 | void Scheduler::recordLayerHistory(int32_t id, const LayerProps& layerProps, nsecs_t presentTime, |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 575 | LayerHistory::LayerUpdateType updateType) { |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 576 | if (pacesetterSelectorPtr()->canSwitch()) { |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 577 | mLayerHistory.record(id, layerProps, presentTime, systemTime(), updateType); |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 578 | } |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 579 | } |
| 580 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 581 | void Scheduler::setModeChangePending(bool pending) { |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 582 | mLayerHistory.setModeChangePending(pending); |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 585 | void Scheduler::setDefaultFrameRateCompatibility(Layer* layer) { |
| 586 | mLayerHistory.setDefaultFrameRateCompatibility(layer, |
| 587 | mFeatures.test(Feature::kContentDetection)); |
| 588 | } |
| 589 | |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 590 | void Scheduler::chooseRefreshRateForContent( |
| 591 | const surfaceflinger::frontend::LayerHierarchy* hierarchy, |
| 592 | bool updateAttachedChoreographer) { |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 593 | const auto selectorPtr = pacesetterSelectorPtr(); |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 594 | if (!selectorPtr->canSwitch()) return; |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 595 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 596 | ATRACE_CALL(); |
| 597 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 598 | LayerHistory::Summary summary = mLayerHistory.summarize(*selectorPtr, systemTime()); |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 599 | applyPolicy(&Policy::contentRequirements, std::move(summary)); |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 600 | |
| 601 | if (updateAttachedChoreographer) { |
| 602 | LOG_ALWAYS_FATAL_IF(!hierarchy); |
| 603 | |
| 604 | // update the attached choreographers after we selected the render rate. |
| 605 | const ftl::Optional<FrameRateMode> modeOpt = [&] { |
| 606 | std::scoped_lock lock(mPolicyLock); |
| 607 | return mPolicy.modeOpt; |
| 608 | }(); |
| 609 | |
| 610 | if (modeOpt) { |
| 611 | updateAttachedChoreographers(*hierarchy, modeOpt->fps); |
| 612 | } |
| 613 | } |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 616 | void Scheduler::resetIdleTimer() { |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 617 | pacesetterSelectorPtr()->resetIdleTimer(); |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 620 | void Scheduler::onTouchHint() { |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 621 | if (mTouchTimer) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 622 | mTouchTimer->reset(); |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 623 | pacesetterSelectorPtr()->resetKernelIdleTimer(); |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 624 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 627 | void Scheduler::setDisplayPowerMode(PhysicalDisplayId id, hal::PowerMode powerMode) { |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 628 | const bool isPacesetter = [this, id]() REQUIRES(kMainThreadContext) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 629 | ftl::FakeGuard guard(mDisplayLock); |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 630 | return id == mPacesetterDisplayId; |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 631 | }(); |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 632 | if (isPacesetter) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 633 | // TODO (b/255657128): This needs to be handled per display. |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 634 | std::lock_guard<std::mutex> lock(mPolicyLock); |
Rachel Lee | 6a9731d | 2022-06-06 17:08:14 -0700 | [diff] [blame] | 635 | mPolicy.displayPowerMode = powerMode; |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 636 | } |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 637 | { |
| 638 | std::scoped_lock lock(mDisplayLock); |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 639 | |
| 640 | const auto displayOpt = mDisplays.get(id); |
| 641 | LOG_ALWAYS_FATAL_IF(!displayOpt); |
| 642 | auto& display = displayOpt->get(); |
| 643 | |
| 644 | display.powerMode = powerMode; |
| 645 | display.schedulePtr->getController().setDisplayPowerMode(powerMode); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 646 | } |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 647 | if (!isPacesetter) return; |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 648 | |
| 649 | if (mDisplayPowerTimer) { |
| 650 | mDisplayPowerTimer->reset(); |
| 651 | } |
| 652 | |
| 653 | // Display Power event will boost the refresh rate to performance. |
| 654 | // Clear Layer History to get fresh FPS detection |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 655 | mLayerHistory.clear(); |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 658 | auto Scheduler::getVsyncSchedule(std::optional<PhysicalDisplayId> idOpt) const |
| 659 | -> ConstVsyncSchedulePtr { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 660 | std::scoped_lock lock(mDisplayLock); |
| 661 | return getVsyncScheduleLocked(idOpt); |
| 662 | } |
| 663 | |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 664 | auto Scheduler::getVsyncScheduleLocked(std::optional<PhysicalDisplayId> idOpt) const |
| 665 | -> ConstVsyncSchedulePtr { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 666 | ftl::FakeGuard guard(kMainThreadContext); |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 667 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 668 | if (!idOpt) { |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 669 | LOG_ALWAYS_FATAL_IF(!mPacesetterDisplayId, "Missing a pacesetter!"); |
| 670 | idOpt = mPacesetterDisplayId; |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 671 | } |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 672 | |
| 673 | const auto displayOpt = mDisplays.get(*idOpt); |
Leon Scroggins III | 4235ea0 | 2023-04-17 15:14:20 -0400 | [diff] [blame] | 674 | if (!displayOpt) { |
| 675 | return nullptr; |
| 676 | } |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 677 | return displayOpt->get().schedulePtr; |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 678 | } |
| 679 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 680 | void Scheduler::kernelIdleTimerCallback(TimerState state) { |
| 681 | ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state)); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 682 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 683 | // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate |
| 684 | // magic number |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 685 | const Fps refreshRate = pacesetterSelectorPtr()->getActiveMode().modePtr->getFps(); |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 686 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 687 | constexpr Fps FPS_THRESHOLD_FOR_KERNEL_TIMER = 65_Hz; |
| 688 | using namespace fps_approx_ops; |
| 689 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 690 | if (state == TimerState::Reset && refreshRate > FPS_THRESHOLD_FOR_KERNEL_TIMER) { |
Alec Mouri | 7f01518 | 2019-07-11 13:56:22 -0700 | [diff] [blame] | 691 | // If we're not in performance mode then the kernel timer shouldn't do |
| 692 | // anything, as the refresh rate during DPU power collapse will be the |
| 693 | // same. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 694 | resyncAllToHardwareVsync(true /* allowToEnable */); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 695 | } else if (state == TimerState::Expired && refreshRate <= FPS_THRESHOLD_FOR_KERNEL_TIMER) { |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 696 | // Disable HW VSYNC if the timer expired, as we don't need it enabled if |
| 697 | // 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] | 698 | // need to update the VsyncController model anyway. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 699 | std::scoped_lock lock(mDisplayLock); |
| 700 | ftl::FakeGuard guard(kMainThreadContext); |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 701 | for (const auto& [_, display] : mDisplays) { |
| 702 | constexpr bool kDisallow = false; |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 703 | display.schedulePtr->disableHardwareVsync(kDisallow); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 704 | } |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 705 | } |
Ady Abraham | a09852a | 2020-02-20 14:23:42 -0800 | [diff] [blame] | 706 | |
| 707 | mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 710 | void Scheduler::idleTimerCallback(TimerState state) { |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 711 | applyPolicy(&Policy::idleTimer, state); |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 712 | ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state)); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 713 | } |
| 714 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 715 | void Scheduler::touchTimerCallback(TimerState state) { |
Dominik Laskowski | dd252cd | 2019-07-26 09:10:16 -0700 | [diff] [blame] | 716 | const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive; |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 717 | // Touch event will boost the refresh rate to performance. |
| 718 | // Clear layer history to get fresh FPS detection. |
| 719 | // NOTE: Instead of checking all the layers, we should be checking the layer |
| 720 | // that is currently on top. b/142507166 will give us this capability. |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 721 | if (applyPolicy(&Policy::touch, touch).touch) { |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 722 | mLayerHistory.clear(); |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 723 | } |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 724 | ATRACE_INT("TouchState", static_cast<int>(touch)); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 727 | void Scheduler::displayPowerTimerCallback(TimerState state) { |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 728 | applyPolicy(&Policy::displayPowerTimer, state); |
Dominik Laskowski | 3a80a38 | 2019-07-25 11:16:07 -0700 | [diff] [blame] | 729 | ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state)); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 732 | void Scheduler::dump(utils::Dumper& dumper) const { |
| 733 | using namespace std::string_view_literals; |
Ady Abraham | 4f960d1 | 2021-10-13 16:59:49 -0700 | [diff] [blame] | 734 | |
| 735 | { |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 736 | utils::Dumper::Section section(dumper, "Features"sv); |
| 737 | |
| 738 | for (Feature feature : ftl::enum_range<Feature>()) { |
| 739 | if (const auto flagOpt = ftl::flag_name(feature)) { |
| 740 | dumper.dump(flagOpt->substr(1), mFeatures.test(feature)); |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | { |
| 745 | utils::Dumper::Section section(dumper, "Policy"sv); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 746 | { |
| 747 | std::scoped_lock lock(mDisplayLock); |
| 748 | ftl::FakeGuard guard(kMainThreadContext); |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 749 | dumper.dump("pacesetterDisplayId"sv, mPacesetterDisplayId); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 750 | } |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 751 | dumper.dump("layerHistory"sv, mLayerHistory.dump()); |
| 752 | dumper.dump("touchTimer"sv, mTouchTimer.transform(&OneShotTimer::interval)); |
| 753 | dumper.dump("displayPowerTimer"sv, mDisplayPowerTimer.transform(&OneShotTimer::interval)); |
| 754 | } |
| 755 | |
| 756 | mFrameRateOverrideMappings.dump(dumper); |
| 757 | dumper.eol(); |
Dominik Laskowski | b418dd7 | 2023-06-13 17:31:04 -0400 | [diff] [blame] | 758 | |
| 759 | mPacesetterFrameTargeter.dump(dumper); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 760 | } |
| 761 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 762 | void Scheduler::dumpVsync(std::string& out) const { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 763 | std::scoped_lock lock(mDisplayLock); |
| 764 | ftl::FakeGuard guard(kMainThreadContext); |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 765 | if (mPacesetterDisplayId) { |
| 766 | base::StringAppendF(&out, "VsyncSchedule for pacesetter %s:\n", |
| 767 | to_string(*mPacesetterDisplayId).c_str()); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 768 | getVsyncScheduleLocked()->dump(out); |
| 769 | } |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 770 | for (auto& [id, display] : mDisplays) { |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 771 | if (id == mPacesetterDisplayId) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 772 | continue; |
| 773 | } |
| 774 | base::StringAppendF(&out, "VsyncSchedule for follower %s:\n", to_string(id).c_str()); |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 775 | display.schedulePtr->dump(out); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 776 | } |
Ady Abraham | 8735eac | 2020-08-12 16:35:04 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 779 | bool Scheduler::updateFrameRateOverrides(GlobalSignals consideredSignals, Fps displayRefreshRate) { |
Ady Abraham | 33a386b | 2023-07-18 15:37:11 -0700 | [diff] [blame^] | 780 | std::scoped_lock lock(mPolicyLock); |
| 781 | return updateFrameRateOverridesLocked(consideredSignals, displayRefreshRate); |
| 782 | } |
| 783 | |
| 784 | bool Scheduler::updateFrameRateOverridesLocked(GlobalSignals consideredSignals, |
| 785 | Fps displayRefreshRate) { |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 786 | if (consideredSignals.idle) return false; |
| 787 | |
| 788 | const auto frameRateOverrides = |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 789 | pacesetterSelectorPtr()->getFrameRateOverrides(mPolicy.contentRequirements, |
| 790 | displayRefreshRate, consideredSignals); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 791 | |
| 792 | // Note that RefreshRateSelector::supportsFrameRateOverrideByContent is checked when querying |
| 793 | // the FrameRateOverrideMappings rather than here. |
| 794 | return mFrameRateOverrideMappings.updateFrameRateOverridesByContent(frameRateOverrides); |
| 795 | } |
| 796 | |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 797 | void Scheduler::promotePacesetterDisplay(std::optional<PhysicalDisplayId> pacesetterIdOpt) { |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 798 | std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule; |
| 799 | |
| 800 | { |
| 801 | std::scoped_lock lock(mDisplayLock); |
| 802 | pacesetterVsyncSchedule = promotePacesetterDisplayLocked(pacesetterIdOpt); |
| 803 | } |
| 804 | |
Leon Scroggins | 39d2534 | 2023-04-19 17:11:01 +0000 | [diff] [blame] | 805 | applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule)); |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | std::shared_ptr<VsyncSchedule> Scheduler::promotePacesetterDisplayLocked( |
| 809 | std::optional<PhysicalDisplayId> pacesetterIdOpt) { |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 810 | // TODO(b/241286431): Choose the pacesetter display. |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 811 | mPacesetterDisplayId = pacesetterIdOpt.value_or(mDisplays.begin()->first); |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 812 | ALOGI("Display %s is the pacesetter", to_string(*mPacesetterDisplayId).c_str()); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 813 | |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 814 | std::shared_ptr<VsyncSchedule> newVsyncSchedulePtr; |
| 815 | if (const auto pacesetterOpt = pacesetterDisplayLocked()) { |
| 816 | const Display& pacesetter = *pacesetterOpt; |
| 817 | |
| 818 | pacesetter.selectorPtr->setIdleTimerCallbacks( |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 819 | {.platform = {.onReset = [this] { idleTimerCallback(TimerState::Reset); }, |
| 820 | .onExpired = [this] { idleTimerCallback(TimerState::Expired); }}, |
| 821 | .kernel = {.onReset = [this] { kernelIdleTimerCallback(TimerState::Reset); }, |
| 822 | .onExpired = |
| 823 | [this] { kernelIdleTimerCallback(TimerState::Expired); }}}); |
| 824 | |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 825 | pacesetter.selectorPtr->startIdleTimer(); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 826 | |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 827 | newVsyncSchedulePtr = pacesetter.schedulePtr; |
| 828 | |
| 829 | const Fps refreshRate = pacesetter.selectorPtr->getActiveMode().modePtr->getFps(); |
Dominik Laskowski | 6629543 | 2023-03-14 12:25:36 -0400 | [diff] [blame] | 830 | constexpr bool kForce = true; |
| 831 | newVsyncSchedulePtr->startPeriodTransition(refreshRate.getPeriod(), kForce); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 832 | } |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 833 | return newVsyncSchedulePtr; |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 834 | } |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 835 | |
Leon Scroggins | 39d2534 | 2023-04-19 17:11:01 +0000 | [diff] [blame] | 836 | void Scheduler::applyNewVsyncSchedule(std::shared_ptr<VsyncSchedule> vsyncSchedule) { |
| 837 | onNewVsyncSchedule(vsyncSchedule->getDispatch()); |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 838 | std::vector<android::EventThread*> threads; |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 839 | { |
| 840 | std::lock_guard<std::mutex> lock(mConnectionsLock); |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 841 | threads.reserve(mConnections.size()); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 842 | for (auto& [_, connection] : mConnections) { |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 843 | threads.push_back(connection.thread.get()); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 844 | } |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 845 | } |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 846 | for (auto* thread : threads) { |
Leon Scroggins | 39d2534 | 2023-04-19 17:11:01 +0000 | [diff] [blame] | 847 | thread->onNewVsyncSchedule(vsyncSchedule); |
Leon Scroggins III | 6fc4519 | 2023-03-16 12:13:28 -0400 | [diff] [blame] | 848 | } |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 849 | } |
| 850 | |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 851 | void Scheduler::demotePacesetterDisplay() { |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 852 | // No need to lock for reads on kMainThreadContext. |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 853 | if (const auto pacesetterPtr = FTL_FAKE_GUARD(mDisplayLock, pacesetterSelectorPtrLocked())) { |
| 854 | pacesetterPtr->stopIdleTimer(); |
| 855 | pacesetterPtr->clearIdleTimerCallbacks(); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 856 | } |
| 857 | |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 858 | // Clear state that depends on the pacesetter's RefreshRateSelector. |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 859 | std::scoped_lock lock(mPolicyLock); |
| 860 | mPolicy = {}; |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Ady Abraham | 822ecbd | 2023-07-07 16:16:09 -0700 | [diff] [blame] | 863 | void Scheduler::updateAttachedChoreographersFrameRate( |
| 864 | const surfaceflinger::frontend::RequestedLayerState& layer, Fps fps) { |
| 865 | std::scoped_lock lock(mChoreographerLock); |
| 866 | |
| 867 | const auto layerId = static_cast<int32_t>(layer.id); |
| 868 | const auto choreographers = mAttachedChoreographers.find(layerId); |
| 869 | if (choreographers == mAttachedChoreographers.end()) { |
| 870 | return; |
| 871 | } |
| 872 | |
| 873 | auto& layerChoreographers = choreographers->second; |
| 874 | |
| 875 | layerChoreographers.frameRate = fps; |
| 876 | ATRACE_FORMAT_INSTANT("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str()); |
| 877 | ALOGV("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str()); |
| 878 | |
| 879 | auto it = layerChoreographers.connections.begin(); |
| 880 | while (it != layerChoreographers.connections.end()) { |
| 881 | sp<EventThreadConnection> choreographerConnection = it->promote(); |
| 882 | if (choreographerConnection) { |
| 883 | choreographerConnection->frameRate = fps; |
| 884 | it++; |
| 885 | } else { |
| 886 | it = choreographers->second.connections.erase(it); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | if (layerChoreographers.connections.empty()) { |
| 891 | mAttachedChoreographers.erase(choreographers); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | int Scheduler::updateAttachedChoreographersInternal( |
| 896 | const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate, |
| 897 | int parentDivisor) { |
| 898 | const char* name = layerHierarchy.getLayer() ? layerHierarchy.getLayer()->name.c_str() : "Root"; |
| 899 | |
| 900 | int divisor = 0; |
| 901 | if (layerHierarchy.getLayer()) { |
| 902 | const auto frameRateCompatibility = layerHierarchy.getLayer()->frameRateCompatibility; |
| 903 | const auto frameRate = Fps::fromValue(layerHierarchy.getLayer()->frameRate); |
| 904 | ALOGV("%s: %s frameRate %s parentDivisor=%d", __func__, name, to_string(frameRate).c_str(), |
| 905 | parentDivisor); |
| 906 | |
| 907 | if (frameRate.isValid()) { |
| 908 | if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE || |
| 909 | frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_EXACT) { |
| 910 | // Since this layer wants an exact match, we would only set a frame rate if the |
| 911 | // desired rate is a divisor of the display refresh rate. |
| 912 | divisor = RefreshRateSelector::getFrameRateDivisor(displayRefreshRate, frameRate); |
| 913 | } else if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT) { |
| 914 | // find the closest frame rate divisor for the desired frame rate. |
| 915 | divisor = static_cast<int>( |
| 916 | std::round(displayRefreshRate.getValue() / frameRate.getValue())); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | // We start by traversing the children, updating their choreographers, and getting back the |
| 922 | // aggregated frame rate. |
| 923 | int childrenDivisor = 0; |
| 924 | for (const auto& [child, _] : layerHierarchy.mChildren) { |
| 925 | LOG_ALWAYS_FATAL_IF(child == nullptr || child->getLayer() == nullptr); |
| 926 | |
| 927 | ALOGV("%s: %s traversing child %s", __func__, name, child->getLayer()->name.c_str()); |
| 928 | |
| 929 | const int childDivisor = |
| 930 | updateAttachedChoreographersInternal(*child, displayRefreshRate, divisor); |
| 931 | childrenDivisor = childrenDivisor > 0 ? childrenDivisor : childDivisor; |
| 932 | if (childDivisor > 0) { |
| 933 | childrenDivisor = std::gcd(childrenDivisor, childDivisor); |
| 934 | } |
| 935 | ALOGV("%s: %s childrenDivisor=%d", __func__, name, childrenDivisor); |
| 936 | } |
| 937 | |
| 938 | ALOGV("%s: %s divisor=%d", __func__, name, divisor); |
| 939 | |
| 940 | // If there is no explicit vote for this layer. Use the children's vote if exists |
| 941 | divisor = (divisor == 0) ? childrenDivisor : divisor; |
| 942 | ALOGV("%s: %s divisor=%d with children", __func__, name, divisor); |
| 943 | |
| 944 | // If there is no explicit vote for this layer or its children, Use the parent vote if exists |
| 945 | divisor = (divisor == 0) ? parentDivisor : divisor; |
| 946 | ALOGV("%s: %s divisor=%d with parent", __func__, name, divisor); |
| 947 | |
| 948 | if (layerHierarchy.getLayer()) { |
| 949 | Fps fps = divisor > 1 ? displayRefreshRate / (unsigned int)divisor : Fps(); |
| 950 | updateAttachedChoreographersFrameRate(*layerHierarchy.getLayer(), fps); |
| 951 | } |
| 952 | |
| 953 | return divisor; |
| 954 | } |
| 955 | |
| 956 | void Scheduler::updateAttachedChoreographers( |
| 957 | const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate) { |
| 958 | ATRACE_CALL(); |
| 959 | updateAttachedChoreographersInternal(layerHierarchy, displayRefreshRate, 0); |
| 960 | } |
| 961 | |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 962 | template <typename S, typename T> |
| 963 | auto Scheduler::applyPolicy(S Policy::*statePtr, T&& newState) -> GlobalSignals { |
Ady Abraham | 73c3df5 | 2023-01-12 18:09:31 -0800 | [diff] [blame] | 964 | ATRACE_CALL(); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 965 | std::vector<display::DisplayModeRequest> modeRequests; |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 966 | GlobalSignals consideredSignals; |
| 967 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 968 | bool refreshRateChanged = false; |
| 969 | bool frameRateOverridesChanged; |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 970 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 971 | { |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 972 | std::scoped_lock lock(mPolicyLock); |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 973 | |
| 974 | auto& currentState = mPolicy.*statePtr; |
| 975 | if (currentState == newState) return {}; |
| 976 | currentState = std::forward<T>(newState); |
| 977 | |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 978 | DisplayModeChoiceMap modeChoices; |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 979 | ftl::Optional<FrameRateMode> modeOpt; |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 980 | { |
| 981 | std::scoped_lock lock(mDisplayLock); |
| 982 | ftl::FakeGuard guard(kMainThreadContext); |
| 983 | |
| 984 | modeChoices = chooseDisplayModes(); |
| 985 | |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 986 | // TODO(b/240743786): The pacesetter display's mode must change for any |
| 987 | // DisplayModeRequest to go through. Fix this by tracking per-display Scheduler::Policy |
| 988 | // and timers. |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 989 | std::tie(modeOpt, consideredSignals) = |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 990 | modeChoices.get(*mPacesetterDisplayId) |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 991 | .transform([](const DisplayModeChoice& choice) { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 992 | return std::make_pair(choice.mode, choice.consideredSignals); |
Dominik Laskowski | 596a256 | 2022-10-28 11:26:12 -0400 | [diff] [blame] | 993 | }) |
| 994 | .value(); |
| 995 | } |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 996 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 997 | modeRequests.reserve(modeChoices.size()); |
| 998 | for (auto& [id, choice] : modeChoices) { |
| 999 | modeRequests.emplace_back( |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1000 | display::DisplayModeRequest{.mode = std::move(choice.mode), |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 1001 | .emitEvent = !choice.consideredSignals.idle}); |
| 1002 | } |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 1003 | |
Ady Abraham | 33a386b | 2023-07-18 15:37:11 -0700 | [diff] [blame^] | 1004 | frameRateOverridesChanged = updateFrameRateOverridesLocked(consideredSignals, modeOpt->fps); |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 1005 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1006 | if (mPolicy.modeOpt != modeOpt) { |
| 1007 | mPolicy.modeOpt = modeOpt; |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 1008 | refreshRateChanged = true; |
| 1009 | } else { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 1010 | // 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] | 1011 | // about a mode change, since it was suppressed if previously considered idle. |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 1012 | if (!consideredSignals.idle) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 1013 | dispatchCachedReportedMode(); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 1014 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 1015 | } |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 1016 | } |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 1017 | if (refreshRateChanged) { |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 1018 | mSchedulerCallback.requestDisplayModes(std::move(modeRequests)); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 1019 | } |
| 1020 | if (frameRateOverridesChanged) { |
| 1021 | mSchedulerCallback.triggerOnFrameRateOverridesChanged(); |
| 1022 | } |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 1023 | return consideredSignals; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 1026 | auto Scheduler::chooseDisplayModes() const -> DisplayModeChoiceMap { |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 1027 | ATRACE_CALL(); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 1028 | |
Ady Abraham | 6863606 | 2022-11-16 17:07:25 -0800 | [diff] [blame] | 1029 | using RankedRefreshRates = RefreshRateSelector::RankedFrameRates; |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 1030 | ui::PhysicalDisplayVector<RankedRefreshRates> perDisplayRanking; |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 1031 | const auto globalSignals = makeGlobalSignals(); |
ramindani | 22f2ead | 2023-04-21 10:27:11 -0700 | [diff] [blame] | 1032 | Fps pacesetterFps; |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 1033 | |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 1034 | for (const auto& [id, display] : mDisplays) { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1035 | auto rankedFrameRates = |
Dominik Laskowski | c404cb4 | 2023-03-03 19:57:53 -0500 | [diff] [blame] | 1036 | display.selectorPtr->getRankedFrameRates(mPolicy.contentRequirements, |
| 1037 | globalSignals); |
ramindani | 22f2ead | 2023-04-21 10:27:11 -0700 | [diff] [blame] | 1038 | if (id == *mPacesetterDisplayId) { |
| 1039 | pacesetterFps = rankedFrameRates.ranking.front().frameRateMode.fps; |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 1040 | } |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 1041 | perDisplayRanking.push_back(std::move(rankedFrameRates)); |
Dominik Laskowski | 95df6a1 | 2022-10-07 18:11:07 -0400 | [diff] [blame] | 1042 | } |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 1043 | |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 1044 | DisplayModeChoiceMap modeChoices; |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 1045 | using fps_approx_ops::operator==; |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 1046 | |
ramindani | 22f2ead | 2023-04-21 10:27:11 -0700 | [diff] [blame] | 1047 | for (auto& [rankings, signals] : perDisplayRanking) { |
| 1048 | const auto chosenFrameRateMode = |
| 1049 | ftl::find_if(rankings, |
| 1050 | [&](const auto& ranking) { |
| 1051 | return ranking.frameRateMode.fps == pacesetterFps; |
| 1052 | }) |
| 1053 | .transform([](const auto& scoredFrameRate) { |
| 1054 | return scoredFrameRate.get().frameRateMode; |
| 1055 | }) |
| 1056 | .value_or(rankings.front().frameRateMode); |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 1057 | |
ramindani | 22f2ead | 2023-04-21 10:27:11 -0700 | [diff] [blame] | 1058 | modeChoices.try_emplace(chosenFrameRateMode.modePtr->getPhysicalDisplayId(), |
| 1059 | DisplayModeChoice{chosenFrameRateMode, signals}); |
Dominik Laskowski | 0160252 | 2022-10-07 19:02:28 -0400 | [diff] [blame] | 1060 | } |
Dominik Laskowski | 530d6bd | 2022-10-10 16:55:54 -0400 | [diff] [blame] | 1061 | return modeChoices; |
ramindani | 69b58e8 | 2022-09-26 16:48:36 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
Dominik Laskowski | 95df6a1 | 2022-10-07 18:11:07 -0400 | [diff] [blame] | 1064 | GlobalSignals Scheduler::makeGlobalSignals() const { |
ramindani | 38c8498 | 2022-08-29 18:02:57 +0000 | [diff] [blame] | 1065 | const bool powerOnImminent = mDisplayPowerTimer && |
| 1066 | (mPolicy.displayPowerMode != hal::PowerMode::ON || |
| 1067 | mPolicy.displayPowerTimer == TimerState::Reset); |
Ady Abraham | 6fe2c17 | 2019-07-12 12:37:57 -0700 | [diff] [blame] | 1068 | |
Dominik Laskowski | 95df6a1 | 2022-10-07 18:11:07 -0400 | [diff] [blame] | 1069 | return {.touch = mTouchTimer && mPolicy.touch == TouchState::Active, |
| 1070 | .idle = mPolicy.idleTimer == TimerState::Expired, |
| 1071 | .powerOnImminent = powerOnImminent}; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 1074 | FrameRateMode Scheduler::getPreferredDisplayMode() { |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 1075 | std::lock_guard<std::mutex> lock(mPolicyLock); |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 1076 | const auto frameRateMode = |
Leon Scroggins III | 1af0fb6 | 2023-03-02 14:21:44 -0500 | [diff] [blame] | 1077 | pacesetterSelectorPtr() |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 1078 | ->getRankedFrameRates(mPolicy.contentRequirements, makeGlobalSignals()) |
| 1079 | .ranking.front() |
| 1080 | .frameRateMode; |
Dominik Laskowski | 95df6a1 | 2022-10-07 18:11:07 -0400 | [diff] [blame] | 1081 | |
Dominik Laskowski | fc378b0 | 2022-12-02 14:56:05 -0500 | [diff] [blame] | 1082 | // Make sure the stored mode is up to date. |
| 1083 | mPolicy.modeOpt = frameRateMode; |
| 1084 | |
| 1085 | return frameRateMode; |
Daniel Solomon | 0f0ddc1 | 2019-08-19 19:31:09 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 1088 | void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) { |
Ady Abraham | 3a77a7b | 2019-12-02 18:46:59 -0800 | [diff] [blame] | 1089 | std::lock_guard<std::mutex> lock(mVsyncTimelineLock); |
| 1090 | mLastVsyncPeriodChangeTimeline = std::make_optional(timeline); |
| 1091 | |
| 1092 | const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count(); |
| 1093 | if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) { |
| 1094 | mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime; |
| 1095 | } |
| 1096 | } |
| 1097 | |
Dominik Laskowski | dd5827a | 2022-03-17 12:44:23 -0700 | [diff] [blame] | 1098 | bool Scheduler::onPostComposition(nsecs_t presentTime) { |
| 1099 | std::lock_guard<std::mutex> lock(mVsyncTimelineLock); |
| 1100 | if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) { |
| 1101 | if (presentTime < mLastVsyncPeriodChangeTimeline->refreshTimeNanos) { |
| 1102 | // We need to composite again as refreshTimeNanos is still in the future. |
| 1103 | return true; |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 1104 | } |
Dominik Laskowski | 8da6b0e | 2021-05-12 15:34:13 -0700 | [diff] [blame] | 1105 | |
Dominik Laskowski | dd5827a | 2022-03-17 12:44:23 -0700 | [diff] [blame] | 1106 | mLastVsyncPeriodChangeTimeline->refreshRequired = false; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 1107 | } |
Dominik Laskowski | dd5827a | 2022-03-17 12:44:23 -0700 | [diff] [blame] | 1108 | return false; |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
Ady Abraham | 7825c68 | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 1111 | void Scheduler::onActiveDisplayAreaChanged(uint32_t displayArea) { |
Dominik Laskowski | 9c93d60 | 2021-10-07 19:38:26 -0700 | [diff] [blame] | 1112 | mLayerHistory.setDisplayArea(displayArea); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 1115 | void Scheduler::setGameModeRefreshRateForUid(FrameRateOverride frameRateOverride) { |
| 1116 | if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) { |
| 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | mFrameRateOverrideMappings.setGameModeRefreshRateForUid(frameRateOverride); |
| 1121 | } |
| 1122 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 1123 | void Scheduler::setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride) { |
| 1124 | if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) { |
| 1125 | return; |
| 1126 | } |
| 1127 | |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 1128 | mFrameRateOverrideMappings.setPreferredRefreshRateForUid(frameRateOverride); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 1129 | } |
| 1130 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 1131 | } // namespace android::scheduler |