blob: 2815cb7209e304716f94b348daacecb397c13341 [file] [log] [blame]
Ana Krulec98b5b242018-08-10 15:03:23 -07001/*
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 Laskowski98041832019-08-01 18:35:59 -070017#undef LOG_TAG
18#define LOG_TAG "Scheduler"
Ana Krulec7ab56032018-11-02 20:51:06 +010019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
Ana Krulec98b5b242018-08-10 15:03:23 -070021#include "Scheduler.h"
22
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070023#include <android-base/properties.h>
Dominik Laskowski49cea512019-11-12 14:13:23 -080024#include <android-base/stringprintf.h>
Ana Krulece588e312018-09-18 12:32:24 -070025#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
26#include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000027#include <common/trace.h>
Ana Krulece588e312018-09-18 12:32:24 -070028#include <configstore/Utils.h>
Dominik Laskowskiec0eac22023-01-28 16:16:19 -050029#include <ftl/concat.h>
Dominik Laskowski03cfce82022-11-02 12:13:29 -040030#include <ftl/enum.h>
ramindania556d072022-06-14 23:25:11 +000031#include <ftl/fake_guard.h>
Dominik Laskowski01602522022-10-07 19:02:28 -040032#include <ftl/small_map.h>
chaviw3277faf2021-05-19 16:45:23 -050033#include <gui/WindowInfo.h>
Ana Krulecfefd6ae2019-02-13 17:53:08 -080034#include <system/window.h>
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050035#include <ui/DisplayMap.h>
Ana Krulec3084c052018-11-21 20:27:17 +010036#include <utils/Timers.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070037
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070038#include <FrameTimeline/FrameTimeline.h>
Dominik Laskowski63f12792023-01-21 16:58:22 -050039#include <scheduler/interface/ICompositor.h>
40
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070041#include <cinttypes>
42#include <cstdint>
43#include <functional>
44#include <memory>
45#include <numeric>
46
Alec Mouri9b133ca2023-11-14 19:00:01 +000047#include <common/FlagManager.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070048#include "EventThread.h"
Andy Yu2ae6b6b2021-11-18 14:51:06 -080049#include "FrameRateOverrideMappings.h"
Rachel Lee2248f522023-01-27 16:45:23 -080050#include "FrontEnd/LayerHandle.h"
Lloyd Pique6f240172024-09-16 15:51:45 -070051#include "Layer.h"
Ana Krulecf2c006d2019-06-21 15:37:07 -070052#include "OneShotTimer.h"
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050053#include "RefreshRateStats.h"
54#include "SurfaceFlingerFactory.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090055#include "SurfaceFlingerProperties.h"
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050056#include "TimeStats/TimeStats.h"
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050057#include "VsyncConfiguration.h"
Dominik Laskowskic404cb42023-03-03 19:57:53 -050058#include "VsyncController.h"
59#include "VsyncSchedule.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070060
Dominik Laskowski068173d2021-08-11 17:22:59 -070061namespace android::scheduler {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070062
Dominik Laskowski1c99a002023-01-20 17:10:36 -050063Scheduler::Scheduler(ICompositor& compositor, ISchedulerCallback& callback, FeatureFlags features,
ramindaniae645822024-01-11 10:57:29 -080064 surfaceflinger::Factory& factory, Fps activeRefreshRate, TimeStats& timeStats)
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050065 : android::impl::MessageQueue(compositor),
Dominik Laskowski1c99a002023-01-20 17:10:36 -050066 mFeatures(features),
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050067 mVsyncConfiguration(factory.createVsyncConfiguration(activeRefreshRate)),
68 mVsyncModulator(sp<VsyncModulator>::make(mVsyncConfiguration->getCurrentConfigs())),
Leon Scroggins IIIde8d9a12024-01-23 12:05:49 -050069 mRefreshRateStats(std::make_unique<RefreshRateStats>(timeStats, activeRefreshRate)),
ramindaniae645822024-01-11 10:57:29 -080070 mSchedulerCallback(callback) {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070071
Dominik Laskowski83bd7712022-01-07 14:30:53 -080072Scheduler::~Scheduler() {
Ady Abraham011f8ba2022-11-22 15:09:07 -080073 // MessageQueue depends on VsyncSchedule, so first destroy it.
74 // Otherwise, MessageQueue will get destroyed after Scheduler's dtor,
75 // which will cause a use-after-free issue.
76 Impl::destroyVsync();
77
Dominik Laskowski83bd7712022-01-07 14:30:53 -080078 // Stop timers and wait for their threads to exit.
79 mDisplayPowerTimer.reset();
80 mTouchTimer.reset();
81
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040082 // Stop idle timer and clear callbacks, as the RefreshRateSelector may outlive the Scheduler.
Dominik Laskowskib9724192024-07-12 15:55:28 -040083 demotePacesetterDisplay({.toggleIdleTimer = true});
Dominik Laskowski83bd7712022-01-07 14:30:53 -080084}
85
Leon Scroggins IIIa7be94e2024-01-23 12:24:30 -050086void Scheduler::initVsync(frametimeline::TokenManager& tokenManager,
87 std::chrono::nanoseconds workDuration) {
88 Impl::initVsyncInternal(getVsyncSchedule()->getDispatch(), tokenManager, workDuration);
89}
90
Dominik Laskowski9c93d602021-10-07 19:38:26 -070091void Scheduler::startTimers() {
Dominik Laskowski98041832019-08-01 18:35:59 -070092 using namespace sysprop;
Dominik Laskowski068173d2021-08-11 17:22:59 -070093 using namespace std::string_literals;
Ady Abraham8532d012019-05-08 14:50:56 -070094
Ady Abrahamc496b432023-12-01 21:35:05 +000095 const int32_t defaultTouchTimerValue =
Ady Abraham3f84c502023-11-30 18:18:06 -080096 FlagManager::getInstance().enable_fro_dependent_features() &&
97 sysprop::enable_frame_rate_override(true)
98 ? 200
99 : 0;
Ady Abrahamc496b432023-12-01 21:35:05 +0000100 if (const int32_t millis = set_touch_timer_ms(defaultTouchTimerValue); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -0700101 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -0700102 mTouchTimer.emplace(
Ady Abrahamdb3dfee2020-11-17 17:07:12 -0800103 "TouchTimer", std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700104 [this] { touchTimerCallback(TimerState::Reset); },
105 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -0700106 mTouchTimer->start();
107 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700108
Dominik Laskowski98041832019-08-01 18:35:59 -0700109 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
110 mDisplayPowerTimer.emplace(
Ady Abrahamdb3dfee2020-11-17 17:07:12 -0800111 "DisplayPowerTimer", std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700112 [this] { displayPowerTimerCallback(TimerState::Reset); },
113 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -0700114 mDisplayPowerTimer->start();
115 }
Ana Krulece588e312018-09-18 12:32:24 -0700116}
117
Dominik Laskowski16d1b3c2024-07-12 14:35:00 -0400118void Scheduler::setPacesetterDisplay(PhysicalDisplayId pacesetterId) {
Dominik Laskowskib9724192024-07-12 15:55:28 -0400119 constexpr PromotionParams kPromotionParams = {.toggleIdleTimer = true};
Dominik Laskowski59db9562022-10-27 16:18:53 -0400120
Dominik Laskowskib9724192024-07-12 15:55:28 -0400121 demotePacesetterDisplay(kPromotionParams);
122 promotePacesetterDisplay(pacesetterId, kPromotionParams);
Dominik Laskowskie0bc94b2024-08-04 15:39:25 -0400123
124 // Cancel the pending refresh rate change, if any, before updating the phase configuration.
125 mVsyncModulator->cancelRefreshRateChange();
126
127 mVsyncConfiguration->reset();
Dominik Laskowskie37c04e2024-08-04 15:48:00 -0400128 updatePhaseConfiguration(pacesetterId, pacesetterSelectorPtr()->getActiveMode().fps);
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800129}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700130
Dominik Laskowski3ff44c72024-06-17 11:03:30 -0400131void Scheduler::registerDisplay(PhysicalDisplayId displayId, RefreshRateSelectorPtr selectorPtr,
132 PhysicalDisplayId activeDisplayId) {
ramindaniae645822024-01-11 10:57:29 -0800133 auto schedulePtr =
134 std::make_shared<VsyncSchedule>(selectorPtr->getActiveMode().modePtr, mFeatures,
135 [this](PhysicalDisplayId id, bool enable) {
136 onHardwareVsyncRequest(id, enable);
137 });
Dominik Laskowski66295432023-03-14 12:25:36 -0400138
Dominik Laskowski3ff44c72024-06-17 11:03:30 -0400139 registerDisplayInternal(displayId, std::move(selectorPtr), std::move(schedulePtr),
140 activeDisplayId);
Leon Scroggins III67388622023-02-06 20:36:20 -0500141}
142
143void Scheduler::registerDisplayInternal(PhysicalDisplayId displayId,
144 RefreshRateSelectorPtr selectorPtr,
Dominik Laskowski3ff44c72024-06-17 11:03:30 -0400145 VsyncSchedulePtr schedulePtr,
146 PhysicalDisplayId activeDisplayId) {
Dominik Laskowskib9724192024-07-12 15:55:28 -0400147 const bool isPrimary = (ftl::FakeGuard(mDisplayLock), !mPacesetterDisplayId);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400148
Dominik Laskowskib9724192024-07-12 15:55:28 -0400149 // Start the idle timer for the first registered (i.e. primary) display.
150 const PromotionParams promotionParams = {.toggleIdleTimer = isPrimary};
151
152 demotePacesetterDisplay(promotionParams);
153
154 auto [pacesetterVsyncSchedule, isNew] = [&]() REQUIRES(kMainThreadContext) {
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400155 std::scoped_lock lock(mDisplayLock);
Dominik Laskowski008bec02023-03-14 12:04:58 -0400156 const bool isNew = mDisplays
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500157 .emplace_or_replace(displayId, displayId, std::move(selectorPtr),
158 std::move(schedulePtr), mFeatures)
Dominik Laskowski008bec02023-03-14 12:04:58 -0400159 .second;
Dominik Laskowski596a2562022-10-28 11:26:12 -0400160
Dominik Laskowskib9724192024-07-12 15:55:28 -0400161 return std::make_pair(promotePacesetterDisplayLocked(activeDisplayId, promotionParams),
162 isNew);
Dominik Laskowski008bec02023-03-14 12:04:58 -0400163 }();
164
Leon Scroggins39d25342023-04-19 17:11:01 +0000165 applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
Dominik Laskowski008bec02023-03-14 12:04:58 -0400166
167 // Disable hardware VSYNC if the registration is new, as opposed to a renewal.
168 if (isNew) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400169 onHardwareVsyncRequest(displayId, false);
Dominik Laskowski008bec02023-03-14 12:04:58 -0400170 }
Dominik Laskowski091129a2024-02-21 14:26:03 -0500171
172 dispatchHotplug(displayId, Hotplug::Connected);
Dominik Laskowski01602522022-10-07 19:02:28 -0400173}
174
Dominik Laskowski3ff44c72024-06-17 11:03:30 -0400175void Scheduler::unregisterDisplay(PhysicalDisplayId displayId, PhysicalDisplayId activeDisplayId) {
176 LOG_ALWAYS_FATAL_IF(displayId == activeDisplayId, "Cannot unregister the active display!");
177
Dominik Laskowski091129a2024-02-21 14:26:03 -0500178 dispatchHotplug(displayId, Hotplug::Disconnected);
179
Dominik Laskowskib9724192024-07-12 15:55:28 -0400180 constexpr PromotionParams kPromotionParams = {.toggleIdleTimer = false};
181 demotePacesetterDisplay(kPromotionParams);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400182
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400183 std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule;
184 {
185 std::scoped_lock lock(mDisplayLock);
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500186 mDisplays.erase(displayId);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400187
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400188 // Do not allow removing the final display. Code in the scheduler expects
189 // there to be at least one display. (This may be relaxed in the future with
190 // headless virtual display.)
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500191 LOG_ALWAYS_FATAL_IF(mDisplays.empty(), "Cannot unregister all displays!");
Leon Scroggins IIIda21f422023-01-30 20:17:56 -0500192
Dominik Laskowskib9724192024-07-12 15:55:28 -0400193 pacesetterVsyncSchedule = promotePacesetterDisplayLocked(activeDisplayId, kPromotionParams);
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400194 }
Leon Scroggins39d25342023-04-19 17:11:01 +0000195 applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
Dominik Laskowski01602522022-10-07 19:02:28 -0400196}
197
Dominik Laskowski756b7892021-08-04 12:53:59 -0700198void Scheduler::run() {
199 while (true) {
200 waitMessage();
201 }
202}
203
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700204void Scheduler::onFrameSignal(ICompositor& compositor, VsyncId vsyncId,
205 TimePoint expectedVsyncTime) {
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500206 const FrameTargeter::BeginFrameArgs beginFrameArgs =
207 {.frameBeginTime = SchedulerClock::now(),
208 .vsyncId = vsyncId,
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500209 .expectedVsyncTime = expectedVsyncTime,
Leon Scroggins III0bd0d4c2022-12-08 13:20:45 -0500210 .sfWorkDuration = mVsyncModulator->getVsyncConfig().sfWorkDuration,
211 .hwcMinWorkDuration = mVsyncConfiguration->getCurrentConfigs().hwcMinWorkDuration};
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700212
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500213 ftl::NonNull<const Display*> pacesetterPtr = pacesetterPtrLocked();
214 pacesetterPtr->targeterPtr->beginFrame(beginFrameArgs, *pacesetterPtr->schedulePtr);
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500215
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500216 {
217 FrameTargets targets;
218 targets.try_emplace(pacesetterPtr->displayId, &pacesetterPtr->targeterPtr->target());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500219
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500220 // TODO (b/256196556): Followers should use the next VSYNC after the frontrunner, not the
221 // pacesetter.
222 // Update expectedVsyncTime, which may have been adjusted by beginFrame.
223 expectedVsyncTime = pacesetterPtr->targeterPtr->target().expectedPresentTime();
224
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500225 for (const auto& [id, display] : mDisplays) {
226 if (id == pacesetterPtr->displayId) continue;
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400227
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500228 auto followerBeginFrameArgs = beginFrameArgs;
229 followerBeginFrameArgs.expectedVsyncTime =
230 display.schedulePtr->vsyncDeadlineAfter(expectedVsyncTime);
231
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500232 FrameTargeter& targeter = *display.targeterPtr;
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500233 targeter.beginFrame(followerBeginFrameArgs, *display.schedulePtr);
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500234 targets.try_emplace(id, &targeter.target());
235 }
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400236
ramindaniae645822024-01-11 10:57:29 -0800237 if (!compositor.commit(pacesetterPtr->displayId, targets)) {
238 if (FlagManager::getInstance().vrr_config()) {
239 compositor.sendNotifyExpectedPresentHint(pacesetterPtr->displayId);
240 }
Ady Abraham14beed72024-05-15 17:16:45 -0700241 mSchedulerCallback.onCommitNotComposited(pacesetterPtr->displayId);
ramindaniae645822024-01-11 10:57:29 -0800242 return;
243 }
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400244 }
245
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500246 // The pacesetter may have changed or been registered anew during commit.
247 pacesetterPtr = pacesetterPtrLocked();
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500248
249 // TODO(b/256196556): Choose the frontrunner display.
250 FrameTargeters targeters;
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500251 targeters.try_emplace(pacesetterPtr->displayId, pacesetterPtr->targeterPtr.get());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500252
253 for (auto& [id, display] : mDisplays) {
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500254 if (id == pacesetterPtr->displayId) continue;
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500255
256 FrameTargeter& targeter = *display.targeterPtr;
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500257 targeters.try_emplace(id, &targeter);
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700258 }
259
Ady Abrahamd6d80162023-10-23 12:57:41 -0700260 if (FlagManager::getInstance().vrr_config() &&
Alec Mouri1c7938e2023-09-22 04:17:23 +0000261 CC_UNLIKELY(mPacesetterFrameDurationFractionToSkip > 0.f)) {
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500262 const auto period = pacesetterPtr->targeterPtr->target().expectedFrameDuration();
Alec Mouri1c7938e2023-09-22 04:17:23 +0000263 const auto skipDuration = Duration::fromNs(
264 static_cast<nsecs_t>(period.ns() * mPacesetterFrameDurationFractionToSkip));
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000265 SFTRACE_FORMAT("Injecting jank for %f%% of the frame (%" PRId64 " ns)",
266 mPacesetterFrameDurationFractionToSkip * 100, skipDuration.ns());
Alec Mouri1c7938e2023-09-22 04:17:23 +0000267 std::this_thread::sleep_for(skipDuration);
268 mPacesetterFrameDurationFractionToSkip = 0.f;
269 }
270
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500271 const auto resultsPerDisplay = compositor.composite(pacesetterPtr->displayId, targeters);
ramindaniae645822024-01-11 10:57:29 -0800272 if (FlagManager::getInstance().vrr_config()) {
273 compositor.sendNotifyExpectedPresentHint(pacesetterPtr->displayId);
274 }
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700275 compositor.sample();
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400276
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500277 for (const auto& [id, targeter] : targeters) {
278 const auto resultOpt = resultsPerDisplay.get(id);
279 LOG_ALWAYS_FATAL_IF(!resultOpt);
280 targeter->endFrame(*resultOpt);
281 }
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700282}
283
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500284std::optional<Fps> Scheduler::getFrameRateOverride(uid_t uid) const {
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800285 const bool supportsFrameRateOverrideByContent =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500286 pacesetterSelectorPtr()->supportsAppFrameRateOverrideByContent();
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800287 return mFrameRateOverrideMappings
288 .getFrameRateOverrideForUid(uid, supportsFrameRateOverrideByContent);
Ady Abraham62a0be22020-12-08 16:54:10 -0800289}
290
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400291bool Scheduler::isVsyncValid(TimePoint expectedVsyncTime, uid_t uid) const {
Ady Abraham62a0be22020-12-08 16:54:10 -0800292 const auto frameRate = getFrameRateOverride(uid);
293 if (!frameRate.has_value()) {
294 return true;
295 }
296
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000297 SFTRACE_FORMAT("%s uid: %d frameRate: %s", __func__, uid, to_string(*frameRate).c_str());
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400298 return getVsyncSchedule()->getTracker().isVSyncInPhase(expectedVsyncTime.ns(), *frameRate);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700299}
300
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400301bool Scheduler::isVsyncInPhase(TimePoint expectedVsyncTime, Fps frameRate) const {
302 return getVsyncSchedule()->getTracker().isVSyncInPhase(expectedVsyncTime.ns(), frameRate);
Huihong Luo1768cb02022-10-11 11:10:34 -0700303}
304
Ady Abrahamf2851612023-09-25 17:19:00 -0700305bool Scheduler::throttleVsync(android::TimePoint expectedPresentTime, uid_t uid) {
306 return !isVsyncValid(expectedPresentTime, uid);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500307}
Ady Abraham64c2fc02020-12-29 12:07:50 -0800308
Ady Abrahamf2851612023-09-25 17:19:00 -0700309Period Scheduler::getVsyncPeriod(uid_t uid) {
310 const auto [refreshRate, period] = [this] {
311 std::scoped_lock lock(mDisplayLock);
312 const auto pacesetterOpt = pacesetterDisplayLocked();
313 LOG_ALWAYS_FATAL_IF(!pacesetterOpt);
314 const Display& pacesetter = *pacesetterOpt;
Ying Weiaf854ad2024-03-16 05:24:39 +0000315 const FrameRateMode& frameRateMode = pacesetter.selectorPtr->getActiveMode();
316 const auto refreshRate = frameRateMode.fps;
317 const auto displayVsync = frameRateMode.modePtr->getVsyncRate();
318 const auto numPeriod = RefreshRateSelector::getFrameRateDivisor(displayVsync, refreshRate);
319 return std::make_pair(refreshRate, numPeriod * pacesetter.schedulePtr->period());
Ady Abrahamf2851612023-09-25 17:19:00 -0700320 }();
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500321
Ady Abrahamf2851612023-09-25 17:19:00 -0700322 const Period currentPeriod = period != Period::zero() ? period : refreshRate.getPeriod();
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800323
Ady Abrahamf2851612023-09-25 17:19:00 -0700324 const auto frameRate = getFrameRateOverride(uid);
325 if (!frameRate.has_value()) {
326 return currentPeriod;
327 }
Jorim Jaggic0086af2021-02-12 18:18:11 +0100328
Ady Abrahamf2851612023-09-25 17:19:00 -0700329 const auto divisor = RefreshRateSelector::getFrameRateDivisor(refreshRate, *frameRate);
330 if (divisor <= 1) {
331 return currentPeriod;
332 }
333
334 // TODO(b/299378819): the casting is not needed, but we need a flag as it might change
335 // behaviour.
336 return Period::fromNs(currentPeriod.ns() * divisor);
Jorim Jaggic0086af2021-02-12 18:18:11 +0100337}
ramindaniae645822024-01-11 10:57:29 -0800338void Scheduler::onExpectedPresentTimePosted(TimePoint expectedPresentTime) {
339 const auto frameRateMode = [this] {
340 std::scoped_lock lock(mDisplayLock);
341 const auto pacesetterOpt = pacesetterDisplayLocked();
342 const Display& pacesetter = *pacesetterOpt;
343 return pacesetter.selectorPtr->getActiveMode();
344 }();
345
346 if (frameRateMode.modePtr->getVrrConfig()) {
347 mSchedulerCallback.onExpectedPresentTimePosted(expectedPresentTime, frameRateMode.modePtr,
348 frameRateMode.fps);
349 }
350}
Jorim Jaggic0086af2021-02-12 18:18:11 +0100351
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500352void Scheduler::createEventThread(Cycle cycle, frametimeline::TokenManager* tokenManager,
353 std::chrono::nanoseconds workDuration,
354 std::chrono::nanoseconds readyDuration) {
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500355 auto eventThread =
356 std::make_unique<android::impl::EventThread>(cycle == Cycle::Render ? "app" : "appSf",
357 getVsyncSchedule(), tokenManager, *this,
358 workDuration, readyDuration);
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500359
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500360 if (cycle == Cycle::Render) {
361 mRenderEventThread = std::move(eventThread);
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500362 } else {
363 mLastCompositeEventThread = std::move(eventThread);
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500364 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700365}
366
367sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500368 Cycle cycle, EventRegistrationFlags eventRegistration, const sp<IBinder>& layerHandle) {
369 const auto connection = eventThreadFor(cycle).createEventConnection(eventRegistration);
Ady Abraham822ecbd2023-07-07 16:16:09 -0700370 const auto layerId = static_cast<int32_t>(LayerHandle::getLayerId(layerHandle));
371
372 if (layerId != static_cast<int32_t>(UNASSIGNED_LAYER_ID)) {
373 // TODO(b/290409668): Moving the choreographer attachment to be a transaction that will be
374 // processed on the main thread.
375 mSchedulerCallback.onChoreographerAttached();
376
377 std::scoped_lock lock(mChoreographerLock);
378 const auto [iter, emplaced] =
379 mAttachedChoreographers.emplace(layerId,
380 AttachedChoreographers{Fps(), {connection}});
381 if (!emplaced) {
382 iter->second.connections.emplace(connection);
383 connection->frameRate = iter->second.frameRate;
384 }
385 }
386 return connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700387}
388
Dominik Laskowski091129a2024-02-21 14:26:03 -0500389void Scheduler::dispatchHotplug(PhysicalDisplayId displayId, Hotplug hotplug) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500390 if (hasEventThreads()) {
Dominik Laskowski091129a2024-02-21 14:26:03 -0500391 const bool connected = hotplug == Hotplug::Connected;
392 eventThreadFor(Cycle::Render).onHotplugReceived(displayId, connected);
393 eventThreadFor(Cycle::LastComposite).onHotplugReceived(displayId, connected);
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500394 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700395}
396
Dominik Laskowski091129a2024-02-21 14:26:03 -0500397void Scheduler::dispatchHotplugError(int32_t errorCode) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500398 if (hasEventThreads()) {
Dominik Laskowski091129a2024-02-21 14:26:03 -0500399 eventThreadFor(Cycle::Render).onHotplugConnectionError(errorCode);
400 eventThreadFor(Cycle::LastComposite).onHotplugConnectionError(errorCode);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700401 }
Brian Johnson5dcd75d2023-08-15 09:36:37 -0700402}
403
Dominik Laskowskie99b98c2023-02-02 12:37:23 -0500404void Scheduler::enableSyntheticVsync(bool enable) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500405 eventThreadFor(Cycle::Render).enableSyntheticVsync(enable);
Ana Krulec98b5b242018-08-10 15:03:23 -0700406}
407
Dominik Laskowski1d7be062024-07-24 13:59:07 -0400408void Scheduler::onFrameRateOverridesChanged() {
409 const auto [pacesetterId, supportsFrameRateOverrideByContent] = [this] {
410 std::scoped_lock lock(mDisplayLock);
411 const auto pacesetterOpt = pacesetterDisplayLocked();
412 LOG_ALWAYS_FATAL_IF(!pacesetterOpt);
413 const Display& pacesetter = *pacesetterOpt;
414 return std::make_pair(FTL_FAKE_GUARD(kMainThreadContext, *mPacesetterDisplayId),
415 pacesetter.selectorPtr->supportsAppFrameRateOverrideByContent());
416 }();
Andy Yud6a36202022-01-26 04:08:22 -0800417
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800418 std::vector<FrameRateOverride> overrides =
Andy Yud6a36202022-01-26 04:08:22 -0800419 mFrameRateOverrideMappings.getAllFrameRateOverrides(supportsFrameRateOverrideByContent);
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800420
Dominik Laskowski1d7be062024-07-24 13:59:07 -0400421 eventThreadFor(Cycle::Render).onFrameRateOverridesChanged(pacesetterId, std::move(overrides));
Ady Abraham62f216c2020-10-13 19:07:23 -0700422}
423
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500424void Scheduler::onHdcpLevelsChanged(Cycle cycle, PhysicalDisplayId displayId,
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700425 int32_t connectedLevel, int32_t maxLevel) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500426 eventThreadFor(cycle).onHdcpLevelsChanged(displayId, connectedLevel, maxLevel);
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700427}
428
Aditya Kumar91b33f12024-10-09 20:21:46 +0000429#pragma clang diagnostic push
430#pragma clang diagnostic ignored "-Wunused-value" // b/369277774
Dominik Laskowskibda52362024-08-04 00:41:46 -0400431bool Scheduler::onDisplayModeChanged(PhysicalDisplayId displayId, const FrameRateMode& mode) {
432 const bool isPacesetter =
433 FTL_FAKE_GUARD(kMainThreadContext,
434 (std::scoped_lock(mDisplayLock), displayId == mPacesetterDisplayId));
435
436 if (isPacesetter) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700437 std::lock_guard<std::mutex> lock(mPolicyLock);
Dominik Laskowskifc94b412024-08-03 15:02:23 -0400438 mPolicy.emittedModeOpt = mode;
Ady Abraham5cc2e262021-03-25 13:09:17 -0700439
440 // Invalidate content based refresh rate selection so it could be calculated
441 // again for the new refresh rate.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700442 mPolicy.contentRequirements.clear();
Ady Abraham62a0be22020-12-08 16:54:10 -0800443 }
Dominik Laskowskibda52362024-08-04 00:41:46 -0400444
445 if (hasEventThreads()) {
446 eventThreadFor(Cycle::Render).onModeChanged(mode);
447 }
448
449 return isPacesetter;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700450}
Aditya Kumar91b33f12024-10-09 20:21:46 +0000451#pragma clang diagnostic pop
Ady Abrahamdfd62162020-06-10 16:11:56 -0700452
Dominik Laskowskifc94b412024-08-03 15:02:23 -0400453void Scheduler::emitModeChangeIfNeeded() {
454 if (!mPolicy.modeOpt || !mPolicy.emittedModeOpt) {
455 ALOGW("No mode change to emit");
Ana Krulec6ddd2612020-09-24 13:06:33 -0700456 return;
457 }
458
Dominik Laskowskifc94b412024-08-03 15:02:23 -0400459 const auto& mode = *mPolicy.modeOpt;
460
461 if (mode != pacesetterSelectorPtr()->getActiveMode()) {
462 // A mode change is pending. The event will be emitted when the mode becomes active.
Ady Abrahamd1591702021-07-27 16:27:56 -0700463 return;
464 }
465
Dominik Laskowskifc94b412024-08-03 15:02:23 -0400466 if (mode == *mPolicy.emittedModeOpt) {
467 // The event was already emitted.
Ady Abrahamdfd62162020-06-10 16:11:56 -0700468 return;
469 }
470
Dominik Laskowskifc94b412024-08-03 15:02:23 -0400471 mPolicy.emittedModeOpt = mode;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700472
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500473 if (hasEventThreads()) {
Dominik Laskowskifc94b412024-08-03 15:02:23 -0400474 eventThreadFor(Cycle::Render).onModeChanged(mode);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700475 }
Ady Abraham447052e2019-02-13 16:07:27 -0800476}
477
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500478void Scheduler::dump(Cycle cycle, std::string& result) const {
479 eventThreadFor(cycle).dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700480}
481
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500482void Scheduler::setDuration(Cycle cycle, std::chrono::nanoseconds workDuration,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700483 std::chrono::nanoseconds readyDuration) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500484 if (hasEventThreads()) {
485 eventThreadFor(cycle).setDuration(workDuration, readyDuration);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700486 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700487}
Ana Krulece588e312018-09-18 12:32:24 -0700488
Dominik Laskowskie37c04e2024-08-04 15:48:00 -0400489void Scheduler::updatePhaseConfiguration(PhysicalDisplayId displayId, Fps refreshRate) {
490 const bool isPacesetter =
491 FTL_FAKE_GUARD(kMainThreadContext,
492 (std::scoped_lock(mDisplayLock), displayId == mPacesetterDisplayId));
493 if (!isPacesetter) return;
494
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500495 mRefreshRateStats->setRefreshRate(refreshRate);
496 mVsyncConfiguration->setRefreshRateFps(refreshRate);
497 setVsyncConfig(mVsyncModulator->setVsyncConfigSet(mVsyncConfiguration->getCurrentConfigs()),
498 refreshRate.getPeriod());
499}
500
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500501void Scheduler::setActiveDisplayPowerModeForRefreshRateStats(hal::PowerMode powerMode) {
502 mRefreshRateStats->setPowerMode(powerMode);
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500503}
504
505void Scheduler::setVsyncConfig(const VsyncConfig& config, Period vsyncPeriod) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500506 setDuration(Cycle::Render,
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500507 /* workDuration */ config.appWorkDuration,
508 /* readyDuration */ config.sfWorkDuration);
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500509 setDuration(Cycle::LastComposite,
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500510 /* workDuration */ vsyncPeriod,
511 /* readyDuration */ config.sfWorkDuration);
512 setDuration(config.sfWorkDuration);
513}
514
Leon Scroggins III67388622023-02-06 20:36:20 -0500515void Scheduler::enableHardwareVsync(PhysicalDisplayId id) {
516 auto schedule = getVsyncSchedule(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400517 LOG_ALWAYS_FATAL_IF(!schedule);
Dominik Laskowski66295432023-03-14 12:25:36 -0400518 schedule->enableHardwareVsync();
Ana Krulece588e312018-09-18 12:32:24 -0700519}
520
Leon Scroggins III67388622023-02-06 20:36:20 -0500521void Scheduler::disableHardwareVsync(PhysicalDisplayId id, bool disallow) {
522 auto schedule = getVsyncSchedule(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400523 LOG_ALWAYS_FATAL_IF(!schedule);
Dominik Laskowski66295432023-03-14 12:25:36 -0400524 schedule->disableHardwareVsync(disallow);
Ana Krulece588e312018-09-18 12:32:24 -0700525}
526
Leon Scroggins III67388622023-02-06 20:36:20 -0500527void Scheduler::resyncAllToHardwareVsync(bool allowToEnable) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000528 SFTRACE_CALL();
Leon Scroggins III67388622023-02-06 20:36:20 -0500529 std::scoped_lock lock(mDisplayLock);
530 ftl::FakeGuard guard(kMainThreadContext);
531
Leon Scroggins III792ea802023-11-27 17:32:51 -0500532 for (const auto& [id, display] : mDisplays) {
533 if (display.powerMode != hal::PowerMode::OFF ||
534 !FlagManager::getInstance().multithreaded_present()) {
535 resyncToHardwareVsyncLocked(id, allowToEnable);
536 }
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500537 }
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500538}
539
Leon Scroggins III67388622023-02-06 20:36:20 -0500540void Scheduler::resyncToHardwareVsyncLocked(PhysicalDisplayId id, bool allowToEnable,
Ady Abrahamc585dba2023-11-15 18:41:35 -0800541 DisplayModePtr modePtr) {
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500542 const auto displayOpt = mDisplays.get(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400543 if (!displayOpt) {
544 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
545 return;
546 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500547 const Display& display = *displayOpt;
548
549 if (display.schedulePtr->isHardwareVsyncAllowed(allowToEnable)) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800550 if (!modePtr) {
551 modePtr = display.selectorPtr->getActiveMode().modePtr.get();
Leon Scroggins III67388622023-02-06 20:36:20 -0500552 }
Ady Abrahamc585dba2023-11-15 18:41:35 -0800553 if (modePtr->getVsyncRate().isValid()) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400554 constexpr bool kForce = false;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800555 display.schedulePtr->onDisplayModeChanged(ftl::as_non_null(modePtr), kForce);
Leon Scroggins III67388622023-02-06 20:36:20 -0500556 }
557 }
558}
559
Dominik Laskowski66295432023-03-14 12:25:36 -0400560void Scheduler::onHardwareVsyncRequest(PhysicalDisplayId id, bool enabled) {
561 static const auto& whence = __func__;
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000562 SFTRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str());
Dominik Laskowski66295432023-03-14 12:25:36 -0400563
564 // On main thread to serialize reads/writes of pending hardware VSYNC state.
565 static_cast<void>(
Leon Scroggins III53ca9562023-12-27 16:32:12 -0500566 schedule([=, this]() FTL_FAKE_GUARD(mDisplayLock) FTL_FAKE_GUARD(kMainThreadContext) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000567 SFTRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str());
Dominik Laskowski66295432023-03-14 12:25:36 -0400568
569 if (const auto displayOpt = mDisplays.get(id)) {
570 auto& display = displayOpt->get();
571 display.schedulePtr->setPendingHardwareVsyncState(enabled);
572
573 if (display.powerMode != hal::PowerMode::OFF) {
574 mSchedulerCallback.requestHardwareVsync(id, enabled);
575 }
576 }
577 }));
578}
579
Ady Abrahamee6365b2024-03-06 14:31:45 -0800580void Scheduler::setRenderRate(PhysicalDisplayId id, Fps renderFrameRate, bool applyImmediately) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500581 std::scoped_lock lock(mDisplayLock);
582 ftl::FakeGuard guard(kMainThreadContext);
583
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500584 const auto displayOpt = mDisplays.get(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400585 if (!displayOpt) {
586 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
587 return;
588 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500589 const Display& display = *displayOpt;
590 const auto mode = display.selectorPtr->getActiveMode();
Ady Abrahamace3d052022-11-17 16:25:05 -0800591
592 using fps_approx_ops::operator!=;
593 LOG_ALWAYS_FATAL_IF(renderFrameRate != mode.fps,
Leon Scroggins III67388622023-02-06 20:36:20 -0500594 "Mismatch in render frame rates. Selector: %s, Scheduler: %s, Display: "
595 "%" PRIu64,
596 to_string(mode.fps).c_str(), to_string(renderFrameRate).c_str(), id.value);
Ady Abrahamace3d052022-11-17 16:25:05 -0800597
598 ALOGV("%s %s (%s)", __func__, to_string(mode.fps).c_str(),
ramindania04b8a52023-08-07 18:49:47 -0700599 to_string(mode.modePtr->getVsyncRate()).c_str());
Ady Abrahamace3d052022-11-17 16:25:05 -0800600
Ady Abrahamee6365b2024-03-06 14:31:45 -0800601 display.schedulePtr->getTracker().setRenderRate(renderFrameRate, applyImmediately);
Ady Abrahamace3d052022-11-17 16:25:05 -0800602}
603
ramindani0491e642023-11-16 17:42:14 -0800604Fps Scheduler::getNextFrameInterval(PhysicalDisplayId id,
605 TimePoint currentExpectedPresentTime) const {
606 std::scoped_lock lock(mDisplayLock);
607 ftl::FakeGuard guard(kMainThreadContext);
608
609 const auto displayOpt = mDisplays.get(id);
610 if (!displayOpt) {
611 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
612 return Fps{};
613 }
614 const Display& display = *displayOpt;
Leon Scroggins IIIa0785012024-01-23 16:05:59 -0500615 const Duration threshold =
616 display.selectorPtr->getActiveMode().modePtr->getVsyncRate().getPeriod() / 2;
617 const TimePoint nextVsyncTime =
618 display.schedulePtr->vsyncDeadlineAfter(currentExpectedPresentTime + threshold,
619 currentExpectedPresentTime);
620 const Duration frameInterval = nextVsyncTime - currentExpectedPresentTime;
621 return Fps::fromPeriodNsecs(frameInterval.ns());
ramindani0491e642023-11-16 17:42:14 -0800622}
623
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700624void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700625 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800626
627 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700628 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800629
630 if (now - last > kIgnoreDelay) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500631 resyncAllToHardwareVsync(false /* allowToEnable */);
Ana Krulecc2870422019-01-29 19:00:58 -0800632 }
633}
634
Leon Scroggins III67388622023-02-06 20:36:20 -0500635bool Scheduler::addResyncSample(PhysicalDisplayId id, nsecs_t timestamp,
636 std::optional<nsecs_t> hwcVsyncPeriodIn) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500637 const auto hwcVsyncPeriod = ftl::Optional(hwcVsyncPeriodIn).transform([](nsecs_t nanos) {
638 return Period::fromNs(nanos);
639 });
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400640 auto schedule = getVsyncSchedule(id);
641 if (!schedule) {
642 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
643 return false;
644 }
Dominik Laskowski66295432023-03-14 12:25:36 -0400645 return schedule->addResyncSample(TimePoint::fromNs(timestamp), hwcVsyncPeriod);
Ana Krulece588e312018-09-18 12:32:24 -0700646}
647
Leon Scroggins III67388622023-02-06 20:36:20 -0500648void Scheduler::addPresentFence(PhysicalDisplayId id, std::shared_ptr<FenceTime> fence) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000649 SFTRACE_NAME(ftl::Concat(__func__, ' ', id.value).c_str());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500650 const auto scheduleOpt =
651 (ftl::FakeGuard(mDisplayLock), mDisplays.get(id)).and_then([](const Display& display) {
652 return display.powerMode == hal::PowerMode::OFF
653 ? std::nullopt
654 : std::make_optional(display.schedulePtr);
655 });
656
657 if (!scheduleOpt) return;
658 const auto& schedule = scheduleOpt->get();
659
Yi Kong08d7c812023-12-12 16:40:22 +0900660 const bool needMoreSignals = schedule->getController().addPresentFence(std::move(fence));
661 if (needMoreSignals) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400662 schedule->enableHardwareVsync();
Ana Krulece588e312018-09-18 12:32:24 -0700663 } else {
Dominik Laskowski66295432023-03-14 12:25:36 -0400664 constexpr bool kDisallow = false;
665 schedule->disableHardwareVsync(kDisallow);
Ana Krulece588e312018-09-18 12:32:24 -0700666 }
667}
668
Vishnu Nair5c61a012024-08-05 21:14:39 -0700669void Scheduler::registerLayer(Layer* layer, FrameRateCompatibility frameRateCompatibility) {
Marin Shalamanov4be385e2021-04-23 13:25:30 +0200670 // If the content detection feature is off, we still keep the layer history,
671 // since we use it for other features (like Frame Rate API), so layers
672 // still need to be registered.
Vishnu Nair5c61a012024-08-05 21:14:39 -0700673 mLayerHistory.registerLayer(layer, mFeatures.test(Feature::kContentDetection),
674 frameRateCompatibility);
Ady Abraham09bd3922019-04-08 10:44:56 -0700675}
676
Ady Abrahambdda8f02021-04-01 16:06:11 -0700677void Scheduler::deregisterLayer(Layer* layer) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700678 mLayerHistory.deregisterLayer(layer);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700679}
680
Ady Abraham822ecbd2023-07-07 16:16:09 -0700681void Scheduler::onLayerDestroyed(Layer* layer) {
682 std::scoped_lock lock(mChoreographerLock);
683 mAttachedChoreographers.erase(layer->getSequence());
684}
685
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000686void Scheduler::recordLayerHistory(int32_t id, const LayerProps& layerProps, nsecs_t presentTime,
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700687 nsecs_t now, LayerHistory::LayerUpdateType updateType) {
Ady Abrahamf8fdc452024-04-05 16:22:55 +0000688 if (pacesetterSelectorPtr()->canSwitch()) {
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700689 mLayerHistory.record(id, layerProps, presentTime, now, updateType);
Dominik Laskowski49cea512019-11-12 14:13:23 -0800690 }
Ana Krulec3084c052018-11-21 20:27:17 +0100691}
692
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100693void Scheduler::setModeChangePending(bool pending) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700694 mLayerHistory.setModeChangePending(pending);
Ady Abraham32efd542020-05-19 17:49:26 -0700695}
696
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700697void Scheduler::setDefaultFrameRateCompatibility(
698 int32_t id, scheduler::FrameRateCompatibility frameRateCompatibility) {
699 mLayerHistory.setDefaultFrameRateCompatibility(id, frameRateCompatibility,
Andy Labrada096227e2022-06-15 16:58:11 +0000700 mFeatures.test(Feature::kContentDetection));
701}
702
Vishnu Nair41376b62023-11-08 05:08:58 -0800703void Scheduler::setLayerProperties(int32_t id, const android::scheduler::LayerProps& properties) {
704 mLayerHistory.setLayerProperties(id, properties);
705}
706
Ady Abraham822ecbd2023-07-07 16:16:09 -0700707void Scheduler::chooseRefreshRateForContent(
708 const surfaceflinger::frontend::LayerHierarchy* hierarchy,
709 bool updateAttachedChoreographer) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500710 const auto selectorPtr = pacesetterSelectorPtr();
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400711 if (!selectorPtr->canSwitch()) return;
Dominik Laskowski49cea512019-11-12 14:13:23 -0800712
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000713 SFTRACE_CALL();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800714
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400715 LayerHistory::Summary summary = mLayerHistory.summarize(*selectorPtr, systemTime());
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800716 applyPolicy(&Policy::contentRequirements, std::move(summary));
Ady Abraham822ecbd2023-07-07 16:16:09 -0700717
718 if (updateAttachedChoreographer) {
719 LOG_ALWAYS_FATAL_IF(!hierarchy);
720
721 // update the attached choreographers after we selected the render rate.
722 const ftl::Optional<FrameRateMode> modeOpt = [&] {
723 std::scoped_lock lock(mPolicyLock);
724 return mPolicy.modeOpt;
725 }();
726
727 if (modeOpt) {
728 updateAttachedChoreographers(*hierarchy, modeOpt->fps);
729 }
730 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800731}
732
Ana Krulecfb772822018-11-30 10:44:07 +0100733void Scheduler::resetIdleTimer() {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500734 pacesetterSelectorPtr()->resetIdleTimer();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800735}
736
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700737void Scheduler::onTouchHint() {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700738 if (mTouchTimer) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800739 mTouchTimer->reset();
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500740 pacesetterSelectorPtr()->resetKernelIdleTimer();
Dominik Laskowski49cea512019-11-12 14:13:23 -0800741 }
Ady Abraham8532d012019-05-08 14:50:56 -0700742}
743
Leon Scroggins III67388622023-02-06 20:36:20 -0500744void Scheduler::setDisplayPowerMode(PhysicalDisplayId id, hal::PowerMode powerMode) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500745 const bool isPacesetter = [this, id]() REQUIRES(kMainThreadContext) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500746 ftl::FakeGuard guard(mDisplayLock);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500747 return id == mPacesetterDisplayId;
Leon Scroggins III67388622023-02-06 20:36:20 -0500748 }();
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500749 if (isPacesetter) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500750 // TODO (b/255657128): This needs to be handled per display.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700751 std::lock_guard<std::mutex> lock(mPolicyLock);
Rachel Lee6a9731d2022-06-06 17:08:14 -0700752 mPolicy.displayPowerMode = powerMode;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700753 }
Leon Scroggins III67388622023-02-06 20:36:20 -0500754 {
755 std::scoped_lock lock(mDisplayLock);
Dominik Laskowski66295432023-03-14 12:25:36 -0400756
757 const auto displayOpt = mDisplays.get(id);
758 LOG_ALWAYS_FATAL_IF(!displayOpt);
759 auto& display = displayOpt->get();
760
761 display.powerMode = powerMode;
762 display.schedulePtr->getController().setDisplayPowerMode(powerMode);
Leon Scroggins III67388622023-02-06 20:36:20 -0500763 }
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500764 if (!isPacesetter) return;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700765
766 if (mDisplayPowerTimer) {
767 mDisplayPowerTimer->reset();
768 }
769
770 // Display Power event will boost the refresh rate to performance.
771 // Clear Layer History to get fresh FPS detection
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700772 mLayerHistory.clear();
Ady Abraham6fe2c172019-07-12 12:37:57 -0700773}
774
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500775auto Scheduler::getVsyncSchedule(std::optional<PhysicalDisplayId> idOpt) const
776 -> ConstVsyncSchedulePtr {
Leon Scroggins III67388622023-02-06 20:36:20 -0500777 std::scoped_lock lock(mDisplayLock);
778 return getVsyncScheduleLocked(idOpt);
779}
780
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500781auto Scheduler::getVsyncScheduleLocked(std::optional<PhysicalDisplayId> idOpt) const
782 -> ConstVsyncSchedulePtr {
Leon Scroggins III67388622023-02-06 20:36:20 -0500783 ftl::FakeGuard guard(kMainThreadContext);
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500784
Leon Scroggins III67388622023-02-06 20:36:20 -0500785 if (!idOpt) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500786 LOG_ALWAYS_FATAL_IF(!mPacesetterDisplayId, "Missing a pacesetter!");
787 idOpt = mPacesetterDisplayId;
Leon Scroggins III67388622023-02-06 20:36:20 -0500788 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500789
790 const auto displayOpt = mDisplays.get(*idOpt);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400791 if (!displayOpt) {
792 return nullptr;
793 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500794 return displayOpt->get().schedulePtr;
Leon Scroggins III67388622023-02-06 20:36:20 -0500795}
796
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700797void Scheduler::kernelIdleTimerCallback(TimerState state) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000798 SFTRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100799
Ady Abraham2139f732019-11-13 18:56:40 -0800800 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
801 // magic number
ramindania04b8a52023-08-07 18:49:47 -0700802 const Fps refreshRate = pacesetterSelectorPtr()->getActiveMode().modePtr->getPeakFps();
Ady Abraham3efa3942021-06-24 19:01:25 -0700803
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700804 constexpr Fps FPS_THRESHOLD_FOR_KERNEL_TIMER = 65_Hz;
805 using namespace fps_approx_ops;
806
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800807 if (state == TimerState::Reset && refreshRate > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700808 // If we're not in performance mode then the kernel timer shouldn't do
809 // anything, as the refresh rate during DPU power collapse will be the
810 // same.
Leon Scroggins III67388622023-02-06 20:36:20 -0500811 resyncAllToHardwareVsync(true /* allowToEnable */);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800812 } else if (state == TimerState::Expired && refreshRate <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700813 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
814 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
Ady Abraham8cb21882020-08-26 18:22:05 -0700815 // need to update the VsyncController model anyway.
Leon Scroggins III67388622023-02-06 20:36:20 -0500816 std::scoped_lock lock(mDisplayLock);
817 ftl::FakeGuard guard(kMainThreadContext);
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500818 for (const auto& [_, display] : mDisplays) {
819 constexpr bool kDisallow = false;
Dominik Laskowski66295432023-03-14 12:25:36 -0400820 display.schedulePtr->disableHardwareVsync(kDisallow);
Leon Scroggins III67388622023-02-06 20:36:20 -0500821 }
Alec Mouridc28b372019-04-18 21:17:13 -0700822 }
Ady Abrahama09852a2020-02-20 14:23:42 -0800823
824 mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
Alec Mouridc28b372019-04-18 21:17:13 -0700825}
826
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700827void Scheduler::idleTimerCallback(TimerState state) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800828 applyPolicy(&Policy::idleTimer, state);
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000829 SFTRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100830}
831
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700832void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700833 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700834 // Touch event will boost the refresh rate to performance.
835 // Clear layer history to get fresh FPS detection.
836 // NOTE: Instead of checking all the layers, we should be checking the layer
837 // that is currently on top. b/142507166 will give us this capability.
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800838 if (applyPolicy(&Policy::touch, touch).touch) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700839 mLayerHistory.clear();
Ady Abraham1adbb722020-05-15 11:51:48 -0700840 }
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000841 SFTRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700842}
843
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700844void Scheduler::displayPowerTimerCallback(TimerState state) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800845 applyPolicy(&Policy::displayPowerTimer, state);
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000846 SFTRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700847}
848
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400849void Scheduler::dump(utils::Dumper& dumper) const {
850 using namespace std::string_view_literals;
Ady Abraham4f960d12021-10-13 16:59:49 -0700851
852 {
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400853 utils::Dumper::Section section(dumper, "Features"sv);
854
855 for (Feature feature : ftl::enum_range<Feature>()) {
856 if (const auto flagOpt = ftl::flag_name(feature)) {
857 dumper.dump(flagOpt->substr(1), mFeatures.test(feature));
858 }
859 }
860 }
861 {
862 utils::Dumper::Section section(dumper, "Policy"sv);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400863 {
864 std::scoped_lock lock(mDisplayLock);
865 ftl::FakeGuard guard(kMainThreadContext);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500866 dumper.dump("pacesetterDisplayId"sv, mPacesetterDisplayId);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400867 }
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400868 dumper.dump("layerHistory"sv, mLayerHistory.dump());
869 dumper.dump("touchTimer"sv, mTouchTimer.transform(&OneShotTimer::interval));
870 dumper.dump("displayPowerTimer"sv, mDisplayPowerTimer.transform(&OneShotTimer::interval));
871 }
872
873 mFrameRateOverrideMappings.dump(dumper);
874 dumper.eol();
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400875
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500876 mVsyncConfiguration->dump(dumper.out());
877 dumper.eol();
878
879 mRefreshRateStats->dump(dumper.out());
880 dumper.eol();
881
Dominik Laskowskife749dc2024-07-26 10:09:31 -0400882 std::scoped_lock lock(mDisplayLock);
883 ftl::FakeGuard guard(kMainThreadContext);
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500884
Dominik Laskowskife749dc2024-07-26 10:09:31 -0400885 for (const auto& [id, display] : mDisplays) {
886 utils::Dumper::Section
887 section(dumper,
888 id == mPacesetterDisplayId
889 ? ftl::Concat("Pacesetter Display ", id.value).c_str()
890 : ftl::Concat("Follower Display ", id.value).c_str());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500891
Dominik Laskowskife749dc2024-07-26 10:09:31 -0400892 display.selectorPtr->dump(dumper);
893 display.targeterPtr->dump(dumper);
894 dumper.eol();
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500895 }
Ana Krulecb43429d2019-01-09 14:28:51 -0800896}
897
Dominik Laskowski068173d2021-08-11 17:22:59 -0700898void Scheduler::dumpVsync(std::string& out) const {
Leon Scroggins III67388622023-02-06 20:36:20 -0500899 std::scoped_lock lock(mDisplayLock);
900 ftl::FakeGuard guard(kMainThreadContext);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500901 if (mPacesetterDisplayId) {
902 base::StringAppendF(&out, "VsyncSchedule for pacesetter %s:\n",
903 to_string(*mPacesetterDisplayId).c_str());
Leon Scroggins III67388622023-02-06 20:36:20 -0500904 getVsyncScheduleLocked()->dump(out);
905 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500906 for (auto& [id, display] : mDisplays) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500907 if (id == mPacesetterDisplayId) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500908 continue;
909 }
910 base::StringAppendF(&out, "VsyncSchedule for follower %s:\n", to_string(id).c_str());
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500911 display.schedulePtr->dump(out);
Leon Scroggins III67388622023-02-06 20:36:20 -0500912 }
Ady Abraham8735eac2020-08-12 16:35:04 -0700913}
914
Dominik Laskowski1d7be062024-07-24 13:59:07 -0400915void Scheduler::updateFrameRateOverrides(GlobalSignals consideredSignals, Fps displayRefreshRate) {
916 const bool changed = (std::scoped_lock(mPolicyLock),
917 updateFrameRateOverridesLocked(consideredSignals, displayRefreshRate));
918
919 if (changed) {
920 onFrameRateOverridesChanged();
921 }
Ady Abraham33a386b2023-07-18 15:37:11 -0700922}
923
924bool Scheduler::updateFrameRateOverridesLocked(GlobalSignals consideredSignals,
925 Fps displayRefreshRate) {
Dominik Laskowski596a2562022-10-28 11:26:12 -0400926 if (consideredSignals.idle) return false;
927
928 const auto frameRateOverrides =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500929 pacesetterSelectorPtr()->getFrameRateOverrides(mPolicy.contentRequirements,
930 displayRefreshRate, consideredSignals);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400931
932 // Note that RefreshRateSelector::supportsFrameRateOverrideByContent is checked when querying
933 // the FrameRateOverrideMappings rather than here.
934 return mFrameRateOverrideMappings.updateFrameRateOverridesByContent(frameRateOverrides);
935}
936
Dominik Laskowskib9724192024-07-12 15:55:28 -0400937void Scheduler::promotePacesetterDisplay(PhysicalDisplayId pacesetterId, PromotionParams params) {
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400938 std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule;
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400939 {
940 std::scoped_lock lock(mDisplayLock);
Dominik Laskowskib9724192024-07-12 15:55:28 -0400941 pacesetterVsyncSchedule = promotePacesetterDisplayLocked(pacesetterId, params);
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400942 }
943
Leon Scroggins39d25342023-04-19 17:11:01 +0000944 applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400945}
946
947std::shared_ptr<VsyncSchedule> Scheduler::promotePacesetterDisplayLocked(
Dominik Laskowskib9724192024-07-12 15:55:28 -0400948 PhysicalDisplayId pacesetterId, PromotionParams params) {
Dominik Laskowski16d1b3c2024-07-12 14:35:00 -0400949 // TODO: b/241286431 - Choose the pacesetter among mDisplays.
950 mPacesetterDisplayId = pacesetterId;
951 ALOGI("Display %s is the pacesetter", to_string(pacesetterId).c_str());
Dominik Laskowski596a2562022-10-28 11:26:12 -0400952
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500953 std::shared_ptr<VsyncSchedule> newVsyncSchedulePtr;
954 if (const auto pacesetterOpt = pacesetterDisplayLocked()) {
955 const Display& pacesetter = *pacesetterOpt;
956
Dominik Laskowskib9724192024-07-12 15:55:28 -0400957 if (!FlagManager::getInstance().connected_display() || params.toggleIdleTimer) {
958 pacesetter.selectorPtr->setIdleTimerCallbacks(
959 {.platform = {.onReset = [this] { idleTimerCallback(TimerState::Reset); },
960 .onExpired = [this] { idleTimerCallback(TimerState::Expired); }},
961 .kernel = {.onReset = [this] { kernelIdleTimerCallback(TimerState::Reset); },
962 .onExpired =
963 [this] { kernelIdleTimerCallback(TimerState::Expired); }},
964 .vrr = {.onReset = [this] { mSchedulerCallback.vrrDisplayIdle(false); },
965 .onExpired = [this] { mSchedulerCallback.vrrDisplayIdle(true); }}});
Dominik Laskowski596a2562022-10-28 11:26:12 -0400966
Dominik Laskowskib9724192024-07-12 15:55:28 -0400967 pacesetter.selectorPtr->startIdleTimer();
968 }
Leon Scroggins III67388622023-02-06 20:36:20 -0500969
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500970 newVsyncSchedulePtr = pacesetter.schedulePtr;
971
Dominik Laskowski66295432023-03-14 12:25:36 -0400972 constexpr bool kForce = true;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800973 newVsyncSchedulePtr->onDisplayModeChanged(pacesetter.selectorPtr->getActiveMode().modePtr,
974 kForce);
Leon Scroggins III67388622023-02-06 20:36:20 -0500975 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500976 return newVsyncSchedulePtr;
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400977}
Leon Scroggins III67388622023-02-06 20:36:20 -0500978
Leon Scroggins39d25342023-04-19 17:11:01 +0000979void Scheduler::applyNewVsyncSchedule(std::shared_ptr<VsyncSchedule> vsyncSchedule) {
980 onNewVsyncSchedule(vsyncSchedule->getDispatch());
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500981
982 if (hasEventThreads()) {
983 eventThreadFor(Cycle::Render).onNewVsyncSchedule(vsyncSchedule);
984 eventThreadFor(Cycle::LastComposite).onNewVsyncSchedule(vsyncSchedule);
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400985 }
Dominik Laskowski596a2562022-10-28 11:26:12 -0400986}
987
Dominik Laskowskib9724192024-07-12 15:55:28 -0400988void Scheduler::demotePacesetterDisplay(PromotionParams params) {
989 if (!FlagManager::getInstance().connected_display() || params.toggleIdleTimer) {
990 // No need to lock for reads on kMainThreadContext.
991 if (const auto pacesetterPtr =
992 FTL_FAKE_GUARD(mDisplayLock, pacesetterSelectorPtrLocked())) {
993 pacesetterPtr->stopIdleTimer();
994 pacesetterPtr->clearIdleTimerCallbacks();
995 }
Dominik Laskowski596a2562022-10-28 11:26:12 -0400996 }
997
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500998 // Clear state that depends on the pacesetter's RefreshRateSelector.
Dominik Laskowski596a2562022-10-28 11:26:12 -0400999 std::scoped_lock lock(mPolicyLock);
1000 mPolicy = {};
Ady Abraham62a0be22020-12-08 16:54:10 -08001001}
1002
Ady Abraham822ecbd2023-07-07 16:16:09 -07001003void Scheduler::updateAttachedChoreographersFrameRate(
1004 const surfaceflinger::frontend::RequestedLayerState& layer, Fps fps) {
1005 std::scoped_lock lock(mChoreographerLock);
1006
1007 const auto layerId = static_cast<int32_t>(layer.id);
1008 const auto choreographers = mAttachedChoreographers.find(layerId);
1009 if (choreographers == mAttachedChoreographers.end()) {
1010 return;
1011 }
1012
1013 auto& layerChoreographers = choreographers->second;
1014
1015 layerChoreographers.frameRate = fps;
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001016 SFTRACE_FORMAT_INSTANT("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str());
Ady Abraham822ecbd2023-07-07 16:16:09 -07001017 ALOGV("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str());
1018
1019 auto it = layerChoreographers.connections.begin();
1020 while (it != layerChoreographers.connections.end()) {
1021 sp<EventThreadConnection> choreographerConnection = it->promote();
1022 if (choreographerConnection) {
1023 choreographerConnection->frameRate = fps;
1024 it++;
1025 } else {
1026 it = choreographers->second.connections.erase(it);
1027 }
1028 }
1029
1030 if (layerChoreographers.connections.empty()) {
1031 mAttachedChoreographers.erase(choreographers);
1032 }
1033}
1034
1035int Scheduler::updateAttachedChoreographersInternal(
1036 const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate,
1037 int parentDivisor) {
1038 const char* name = layerHierarchy.getLayer() ? layerHierarchy.getLayer()->name.c_str() : "Root";
1039
1040 int divisor = 0;
1041 if (layerHierarchy.getLayer()) {
1042 const auto frameRateCompatibility = layerHierarchy.getLayer()->frameRateCompatibility;
1043 const auto frameRate = Fps::fromValue(layerHierarchy.getLayer()->frameRate);
1044 ALOGV("%s: %s frameRate %s parentDivisor=%d", __func__, name, to_string(frameRate).c_str(),
1045 parentDivisor);
1046
1047 if (frameRate.isValid()) {
1048 if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE ||
1049 frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_EXACT) {
1050 // Since this layer wants an exact match, we would only set a frame rate if the
1051 // desired rate is a divisor of the display refresh rate.
1052 divisor = RefreshRateSelector::getFrameRateDivisor(displayRefreshRate, frameRate);
1053 } else if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT) {
1054 // find the closest frame rate divisor for the desired frame rate.
1055 divisor = static_cast<int>(
1056 std::round(displayRefreshRate.getValue() / frameRate.getValue()));
1057 }
1058 }
1059 }
1060
1061 // We start by traversing the children, updating their choreographers, and getting back the
1062 // aggregated frame rate.
1063 int childrenDivisor = 0;
1064 for (const auto& [child, _] : layerHierarchy.mChildren) {
1065 LOG_ALWAYS_FATAL_IF(child == nullptr || child->getLayer() == nullptr);
1066
1067 ALOGV("%s: %s traversing child %s", __func__, name, child->getLayer()->name.c_str());
1068
1069 const int childDivisor =
1070 updateAttachedChoreographersInternal(*child, displayRefreshRate, divisor);
1071 childrenDivisor = childrenDivisor > 0 ? childrenDivisor : childDivisor;
1072 if (childDivisor > 0) {
1073 childrenDivisor = std::gcd(childrenDivisor, childDivisor);
1074 }
1075 ALOGV("%s: %s childrenDivisor=%d", __func__, name, childrenDivisor);
1076 }
1077
1078 ALOGV("%s: %s divisor=%d", __func__, name, divisor);
1079
1080 // If there is no explicit vote for this layer. Use the children's vote if exists
1081 divisor = (divisor == 0) ? childrenDivisor : divisor;
1082 ALOGV("%s: %s divisor=%d with children", __func__, name, divisor);
1083
1084 // If there is no explicit vote for this layer or its children, Use the parent vote if exists
1085 divisor = (divisor == 0) ? parentDivisor : divisor;
1086 ALOGV("%s: %s divisor=%d with parent", __func__, name, divisor);
1087
1088 if (layerHierarchy.getLayer()) {
1089 Fps fps = divisor > 1 ? displayRefreshRate / (unsigned int)divisor : Fps();
1090 updateAttachedChoreographersFrameRate(*layerHierarchy.getLayer(), fps);
1091 }
1092
1093 return divisor;
1094}
1095
1096void Scheduler::updateAttachedChoreographers(
1097 const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001098 SFTRACE_CALL();
Ady Abraham822ecbd2023-07-07 16:16:09 -07001099 updateAttachedChoreographersInternal(layerHierarchy, displayRefreshRate, 0);
1100}
1101
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001102template <typename S, typename T>
1103auto Scheduler::applyPolicy(S Policy::*statePtr, T&& newState) -> GlobalSignals {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001104 SFTRACE_CALL();
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001105 std::vector<display::DisplayModeRequest> modeRequests;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001106 GlobalSignals consideredSignals;
1107
Ady Abraham62a0be22020-12-08 16:54:10 -08001108 bool refreshRateChanged = false;
1109 bool frameRateOverridesChanged;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001110
Ady Abraham8532d012019-05-08 14:50:56 -07001111 {
Dominik Laskowski596a2562022-10-28 11:26:12 -04001112 std::scoped_lock lock(mPolicyLock);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001113
1114 auto& currentState = mPolicy.*statePtr;
1115 if (currentState == newState) return {};
1116 currentState = std::forward<T>(newState);
1117
Dominik Laskowski596a2562022-10-28 11:26:12 -04001118 DisplayModeChoiceMap modeChoices;
Ady Abrahamace3d052022-11-17 16:25:05 -08001119 ftl::Optional<FrameRateMode> modeOpt;
Dominik Laskowski596a2562022-10-28 11:26:12 -04001120 {
1121 std::scoped_lock lock(mDisplayLock);
1122 ftl::FakeGuard guard(kMainThreadContext);
1123
1124 modeChoices = chooseDisplayModes();
1125
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001126 // TODO(b/240743786): The pacesetter display's mode must change for any
1127 // DisplayModeRequest to go through. Fix this by tracking per-display Scheduler::Policy
1128 // and timers.
Ady Abrahamace3d052022-11-17 16:25:05 -08001129 std::tie(modeOpt, consideredSignals) =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001130 modeChoices.get(*mPacesetterDisplayId)
Dominik Laskowski596a2562022-10-28 11:26:12 -04001131 .transform([](const DisplayModeChoice& choice) {
Ady Abrahamace3d052022-11-17 16:25:05 -08001132 return std::make_pair(choice.mode, choice.consideredSignals);
Dominik Laskowski596a2562022-10-28 11:26:12 -04001133 })
1134 .value();
1135 }
ramindani69b58e82022-09-26 16:48:36 -07001136
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001137 modeRequests.reserve(modeChoices.size());
1138 for (auto& [id, choice] : modeChoices) {
1139 modeRequests.emplace_back(
Ady Abrahamace3d052022-11-17 16:25:05 -08001140 display::DisplayModeRequest{.mode = std::move(choice.mode),
Dominik Laskowskifc94b412024-08-03 15:02:23 -04001141 .emitEvent = choice.consideredSignals
1142 .shouldEmitEvent()});
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001143 }
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001144
ramindanif2f40f42024-06-18 16:48:28 -07001145 if (!FlagManager::getInstance().vrr_bugfix_dropped_frame()) {
1146 frameRateOverridesChanged =
1147 updateFrameRateOverridesLocked(consideredSignals, modeOpt->fps);
1148 }
Ady Abrahamace3d052022-11-17 16:25:05 -08001149 if (mPolicy.modeOpt != modeOpt) {
1150 mPolicy.modeOpt = modeOpt;
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001151 refreshRateChanged = true;
Dominik Laskowskifc94b412024-08-03 15:02:23 -04001152 } else if (consideredSignals.shouldEmitEvent()) {
1153 // The mode did not change, but we may need to emit if DisplayModeRequest::emitEvent was
1154 // previously false.
1155 emitModeChangeIfNeeded();
Ady Abraham8532d012019-05-08 14:50:56 -07001156 }
Ady Abraham8532d012019-05-08 14:50:56 -07001157 }
Ady Abraham62a0be22020-12-08 16:54:10 -08001158 if (refreshRateChanged) {
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001159 mSchedulerCallback.requestDisplayModes(std::move(modeRequests));
Ady Abraham62a0be22020-12-08 16:54:10 -08001160 }
ramindanif2f40f42024-06-18 16:48:28 -07001161
1162 if (FlagManager::getInstance().vrr_bugfix_dropped_frame()) {
1163 std::scoped_lock lock(mPolicyLock);
1164 frameRateOverridesChanged =
1165 updateFrameRateOverridesLocked(consideredSignals, mPolicy.modeOpt->fps);
1166 }
Ady Abraham62a0be22020-12-08 16:54:10 -08001167 if (frameRateOverridesChanged) {
Dominik Laskowski1d7be062024-07-24 13:59:07 -04001168 onFrameRateOverridesChanged();
Ady Abraham62a0be22020-12-08 16:54:10 -08001169 }
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001170 return consideredSignals;
Ady Abraham8532d012019-05-08 14:50:56 -07001171}
1172
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001173auto Scheduler::chooseDisplayModes() const -> DisplayModeChoiceMap {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001174 SFTRACE_CALL();
Ady Abraham09bd3922019-04-08 10:44:56 -07001175
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001176 DisplayModeChoiceMap modeChoices;
Dominik Laskowski01602522022-10-07 19:02:28 -04001177 const auto globalSignals = makeGlobalSignals();
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001178
1179 const Fps pacesetterFps = [&]() REQUIRES(mPolicyLock, mDisplayLock, kMainThreadContext) {
1180 auto rankedFrameRates =
1181 pacesetterSelectorPtrLocked()->getRankedFrameRates(mPolicy.contentRequirements,
1182 globalSignals);
1183
1184 const Fps pacesetterFps = rankedFrameRates.ranking.front().frameRateMode.fps;
1185
1186 modeChoices.try_emplace(*mPacesetterDisplayId,
1187 DisplayModeChoice::from(std::move(rankedFrameRates)));
1188 return pacesetterFps;
1189 }();
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001190
Dominik Laskowski086507b2024-05-16 15:33:16 -04001191 // Choose a mode for powered-on follower displays.
Dominik Laskowskic404cb42023-03-03 19:57:53 -05001192 for (const auto& [id, display] : mDisplays) {
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001193 if (id == *mPacesetterDisplayId) continue;
Dominik Laskowski086507b2024-05-16 15:33:16 -04001194 if (display.powerMode != hal::PowerMode::ON) continue;
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001195
Ady Abrahamace3d052022-11-17 16:25:05 -08001196 auto rankedFrameRates =
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001197 display.selectorPtr->getRankedFrameRates(mPolicy.contentRequirements, globalSignals,
1198 pacesetterFps);
1199
1200 modeChoices.try_emplace(id, DisplayModeChoice::from(std::move(rankedFrameRates)));
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001201 }
ramindani69b58e82022-09-26 16:48:36 -07001202
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001203 return modeChoices;
ramindani69b58e82022-09-26 16:48:36 -07001204}
1205
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001206GlobalSignals Scheduler::makeGlobalSignals() const {
ramindani38c84982022-08-29 18:02:57 +00001207 const bool powerOnImminent = mDisplayPowerTimer &&
1208 (mPolicy.displayPowerMode != hal::PowerMode::ON ||
1209 mPolicy.displayPowerTimer == TimerState::Reset);
Ady Abraham6fe2c172019-07-12 12:37:57 -07001210
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001211 return {.touch = mTouchTimer && mPolicy.touch == TouchState::Active,
1212 .idle = mPolicy.idleTimer == TimerState::Expired,
1213 .powerOnImminent = powerOnImminent};
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001214}
1215
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001216FrameRateMode Scheduler::getPreferredDisplayMode() {
Dominik Laskowski068173d2021-08-11 17:22:59 -07001217 std::lock_guard<std::mutex> lock(mPolicyLock);
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001218 const auto frameRateMode =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001219 pacesetterSelectorPtr()
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001220 ->getRankedFrameRates(mPolicy.contentRequirements, makeGlobalSignals())
1221 .ranking.front()
1222 .frameRateMode;
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001223
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001224 // Make sure the stored mode is up to date.
1225 mPolicy.modeOpt = frameRateMode;
1226
1227 return frameRateMode;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -07001228}
1229
Peiyong Line9d809e2020-04-14 13:10:48 -07001230void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
Ady Abraham3a77a7b2019-12-02 18:46:59 -08001231 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
1232 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
1233
1234 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
1235 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
1236 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
1237 }
1238}
1239
Leon Scroggins III5b581492023-10-31 14:29:41 -04001240bool Scheduler::onCompositionPresented(nsecs_t presentTime) {
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001241 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
1242 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
1243 if (presentTime < mLastVsyncPeriodChangeTimeline->refreshTimeNanos) {
1244 // We need to composite again as refreshTimeNanos is still in the future.
1245 return true;
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001246 }
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001247
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001248 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001249 }
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001250 return false;
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001251}
1252
Ady Abraham7825c682021-05-17 15:12:14 -07001253void Scheduler::onActiveDisplayAreaChanged(uint32_t displayArea) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -07001254 mLayerHistory.setDisplayArea(displayArea);
Ady Abraham8a82ba62020-01-17 12:43:17 -08001255}
1256
Andy Yu8c2703d2023-11-03 11:22:46 -07001257void Scheduler::setGameModeFrameRateForUid(FrameRateOverride frameRateOverride) {
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001258 if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) {
1259 return;
1260 }
1261
Andy Yu8c2703d2023-11-03 11:22:46 -07001262 if (FlagManager::getInstance().game_default_frame_rate()) {
1263 // update the frame rate override mapping in LayerHistory
1264 mLayerHistory.updateGameModeFrameRateOverride(frameRateOverride);
1265 } else {
1266 mFrameRateOverrideMappings.setGameModeRefreshRateForUid(frameRateOverride);
1267 }
Dominik Laskowski1d7be062024-07-24 13:59:07 -04001268
1269 onFrameRateOverridesChanged();
Andy Yu8c2703d2023-11-03 11:22:46 -07001270}
1271
1272void Scheduler::setGameDefaultFrameRateForUid(FrameRateOverride frameRateOverride) {
1273 if (!FlagManager::getInstance().game_default_frame_rate() ||
1274 (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f)) {
1275 return;
1276 }
1277
1278 // update the frame rate override mapping in LayerHistory
1279 mLayerHistory.updateGameDefaultFrameRateOverride(frameRateOverride);
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001280}
1281
Ady Abraham62a0be22020-12-08 16:54:10 -08001282void Scheduler::setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride) {
1283 if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) {
1284 return;
1285 }
1286
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001287 mFrameRateOverrideMappings.setPreferredRefreshRateForUid(frameRateOverride);
Dominik Laskowski1d7be062024-07-24 13:59:07 -04001288 onFrameRateOverridesChanged();
Ady Abraham62a0be22020-12-08 16:54:10 -08001289}
1290
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001291void Scheduler::updateSmallAreaDetection(
Tony Huangf3621102023-09-04 17:14:22 +08001292 std::vector<std::pair<int32_t, float>>& uidThresholdMappings) {
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001293 mSmallAreaDetectionAllowMappings.update(uidThresholdMappings);
1294}
1295
Tony Huangf3621102023-09-04 17:14:22 +08001296void Scheduler::setSmallAreaDetectionThreshold(int32_t appId, float threshold) {
Jerry Chang36678002023-11-29 16:56:17 +00001297 mSmallAreaDetectionAllowMappings.setThresholdForAppId(appId, threshold);
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001298}
1299
Tony Huangf3621102023-09-04 17:14:22 +08001300bool Scheduler::isSmallDirtyArea(int32_t appId, uint32_t dirtyArea) {
1301 std::optional<float> oThreshold = mSmallAreaDetectionAllowMappings.getThresholdForAppId(appId);
1302 if (oThreshold) {
1303 return mLayerHistory.isSmallDirtyArea(dirtyArea, oThreshold.value());
1304 }
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001305 return false;
1306}
1307
Dominik Laskowski068173d2021-08-11 17:22:59 -07001308} // namespace android::scheduler