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