blob: b984e92f20c3df82ae45bc2c60a6f94925165b5c [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
Aditya Kumar860ef882024-10-10 13:47:44 -0700489#pragma clang diagnostic push
490#pragma clang diagnostic ignored "-Wunused-value" // b/369277774
Dominik Laskowskie37c04e2024-08-04 15:48:00 -0400491void Scheduler::updatePhaseConfiguration(PhysicalDisplayId displayId, Fps refreshRate) {
492 const bool isPacesetter =
493 FTL_FAKE_GUARD(kMainThreadContext,
494 (std::scoped_lock(mDisplayLock), displayId == mPacesetterDisplayId));
495 if (!isPacesetter) return;
496
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500497 mRefreshRateStats->setRefreshRate(refreshRate);
498 mVsyncConfiguration->setRefreshRateFps(refreshRate);
499 setVsyncConfig(mVsyncModulator->setVsyncConfigSet(mVsyncConfiguration->getCurrentConfigs()),
500 refreshRate.getPeriod());
501}
Aditya Kumar860ef882024-10-10 13:47:44 -0700502#pragma clang diagnostic pop
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500503
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500504void Scheduler::setActiveDisplayPowerModeForRefreshRateStats(hal::PowerMode powerMode) {
505 mRefreshRateStats->setPowerMode(powerMode);
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500506}
507
508void Scheduler::setVsyncConfig(const VsyncConfig& config, Period vsyncPeriod) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500509 setDuration(Cycle::Render,
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500510 /* workDuration */ config.appWorkDuration,
511 /* readyDuration */ config.sfWorkDuration);
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500512 setDuration(Cycle::LastComposite,
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500513 /* workDuration */ vsyncPeriod,
514 /* readyDuration */ config.sfWorkDuration);
515 setDuration(config.sfWorkDuration);
516}
517
Leon Scroggins III67388622023-02-06 20:36:20 -0500518void Scheduler::enableHardwareVsync(PhysicalDisplayId id) {
519 auto schedule = getVsyncSchedule(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400520 LOG_ALWAYS_FATAL_IF(!schedule);
Dominik Laskowski66295432023-03-14 12:25:36 -0400521 schedule->enableHardwareVsync();
Ana Krulece588e312018-09-18 12:32:24 -0700522}
523
Leon Scroggins III67388622023-02-06 20:36:20 -0500524void Scheduler::disableHardwareVsync(PhysicalDisplayId id, bool disallow) {
525 auto schedule = getVsyncSchedule(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400526 LOG_ALWAYS_FATAL_IF(!schedule);
Dominik Laskowski66295432023-03-14 12:25:36 -0400527 schedule->disableHardwareVsync(disallow);
Ana Krulece588e312018-09-18 12:32:24 -0700528}
529
Leon Scroggins III67388622023-02-06 20:36:20 -0500530void Scheduler::resyncAllToHardwareVsync(bool allowToEnable) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000531 SFTRACE_CALL();
Leon Scroggins III67388622023-02-06 20:36:20 -0500532 std::scoped_lock lock(mDisplayLock);
533 ftl::FakeGuard guard(kMainThreadContext);
534
Leon Scroggins III792ea802023-11-27 17:32:51 -0500535 for (const auto& [id, display] : mDisplays) {
536 if (display.powerMode != hal::PowerMode::OFF ||
537 !FlagManager::getInstance().multithreaded_present()) {
538 resyncToHardwareVsyncLocked(id, allowToEnable);
539 }
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500540 }
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500541}
542
Leon Scroggins III67388622023-02-06 20:36:20 -0500543void Scheduler::resyncToHardwareVsyncLocked(PhysicalDisplayId id, bool allowToEnable,
Ady Abrahamc585dba2023-11-15 18:41:35 -0800544 DisplayModePtr modePtr) {
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500545 const auto displayOpt = mDisplays.get(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400546 if (!displayOpt) {
547 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
548 return;
549 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500550 const Display& display = *displayOpt;
551
552 if (display.schedulePtr->isHardwareVsyncAllowed(allowToEnable)) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800553 if (!modePtr) {
554 modePtr = display.selectorPtr->getActiveMode().modePtr.get();
Leon Scroggins III67388622023-02-06 20:36:20 -0500555 }
Ady Abrahamc585dba2023-11-15 18:41:35 -0800556 if (modePtr->getVsyncRate().isValid()) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400557 constexpr bool kForce = false;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800558 display.schedulePtr->onDisplayModeChanged(ftl::as_non_null(modePtr), kForce);
Leon Scroggins III67388622023-02-06 20:36:20 -0500559 }
560 }
561}
562
Dominik Laskowski66295432023-03-14 12:25:36 -0400563void Scheduler::onHardwareVsyncRequest(PhysicalDisplayId id, bool enabled) {
564 static const auto& whence = __func__;
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000565 SFTRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str());
Dominik Laskowski66295432023-03-14 12:25:36 -0400566
567 // On main thread to serialize reads/writes of pending hardware VSYNC state.
568 static_cast<void>(
Leon Scroggins III53ca9562023-12-27 16:32:12 -0500569 schedule([=, this]() FTL_FAKE_GUARD(mDisplayLock) FTL_FAKE_GUARD(kMainThreadContext) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000570 SFTRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str());
Dominik Laskowski66295432023-03-14 12:25:36 -0400571
572 if (const auto displayOpt = mDisplays.get(id)) {
573 auto& display = displayOpt->get();
574 display.schedulePtr->setPendingHardwareVsyncState(enabled);
575
576 if (display.powerMode != hal::PowerMode::OFF) {
577 mSchedulerCallback.requestHardwareVsync(id, enabled);
578 }
579 }
580 }));
581}
582
Ady Abrahamee6365b2024-03-06 14:31:45 -0800583void Scheduler::setRenderRate(PhysicalDisplayId id, Fps renderFrameRate, bool applyImmediately) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500584 std::scoped_lock lock(mDisplayLock);
585 ftl::FakeGuard guard(kMainThreadContext);
586
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500587 const auto displayOpt = mDisplays.get(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400588 if (!displayOpt) {
589 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
590 return;
591 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500592 const Display& display = *displayOpt;
593 const auto mode = display.selectorPtr->getActiveMode();
Ady Abrahamace3d052022-11-17 16:25:05 -0800594
595 using fps_approx_ops::operator!=;
596 LOG_ALWAYS_FATAL_IF(renderFrameRate != mode.fps,
Leon Scroggins III67388622023-02-06 20:36:20 -0500597 "Mismatch in render frame rates. Selector: %s, Scheduler: %s, Display: "
598 "%" PRIu64,
599 to_string(mode.fps).c_str(), to_string(renderFrameRate).c_str(), id.value);
Ady Abrahamace3d052022-11-17 16:25:05 -0800600
601 ALOGV("%s %s (%s)", __func__, to_string(mode.fps).c_str(),
ramindania04b8a52023-08-07 18:49:47 -0700602 to_string(mode.modePtr->getVsyncRate()).c_str());
Ady Abrahamace3d052022-11-17 16:25:05 -0800603
Ady Abrahamee6365b2024-03-06 14:31:45 -0800604 display.schedulePtr->getTracker().setRenderRate(renderFrameRate, applyImmediately);
Ady Abrahamace3d052022-11-17 16:25:05 -0800605}
606
ramindani0491e642023-11-16 17:42:14 -0800607Fps Scheduler::getNextFrameInterval(PhysicalDisplayId id,
608 TimePoint currentExpectedPresentTime) const {
609 std::scoped_lock lock(mDisplayLock);
610 ftl::FakeGuard guard(kMainThreadContext);
611
612 const auto displayOpt = mDisplays.get(id);
613 if (!displayOpt) {
614 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
615 return Fps{};
616 }
617 const Display& display = *displayOpt;
Leon Scroggins IIIa0785012024-01-23 16:05:59 -0500618 const Duration threshold =
619 display.selectorPtr->getActiveMode().modePtr->getVsyncRate().getPeriod() / 2;
620 const TimePoint nextVsyncTime =
621 display.schedulePtr->vsyncDeadlineAfter(currentExpectedPresentTime + threshold,
622 currentExpectedPresentTime);
623 const Duration frameInterval = nextVsyncTime - currentExpectedPresentTime;
624 return Fps::fromPeriodNsecs(frameInterval.ns());
ramindani0491e642023-11-16 17:42:14 -0800625}
626
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700627void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700628 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800629
630 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700631 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800632
633 if (now - last > kIgnoreDelay) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500634 resyncAllToHardwareVsync(false /* allowToEnable */);
Ana Krulecc2870422019-01-29 19:00:58 -0800635 }
636}
637
Leon Scroggins III67388622023-02-06 20:36:20 -0500638bool Scheduler::addResyncSample(PhysicalDisplayId id, nsecs_t timestamp,
639 std::optional<nsecs_t> hwcVsyncPeriodIn) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500640 const auto hwcVsyncPeriod = ftl::Optional(hwcVsyncPeriodIn).transform([](nsecs_t nanos) {
641 return Period::fromNs(nanos);
642 });
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400643 auto schedule = getVsyncSchedule(id);
644 if (!schedule) {
645 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
646 return false;
647 }
Dominik Laskowski66295432023-03-14 12:25:36 -0400648 return schedule->addResyncSample(TimePoint::fromNs(timestamp), hwcVsyncPeriod);
Ana Krulece588e312018-09-18 12:32:24 -0700649}
650
Leon Scroggins III67388622023-02-06 20:36:20 -0500651void Scheduler::addPresentFence(PhysicalDisplayId id, std::shared_ptr<FenceTime> fence) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000652 SFTRACE_NAME(ftl::Concat(__func__, ' ', id.value).c_str());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500653 const auto scheduleOpt =
654 (ftl::FakeGuard(mDisplayLock), mDisplays.get(id)).and_then([](const Display& display) {
655 return display.powerMode == hal::PowerMode::OFF
656 ? std::nullopt
657 : std::make_optional(display.schedulePtr);
658 });
659
660 if (!scheduleOpt) return;
661 const auto& schedule = scheduleOpt->get();
662
Yi Kong08d7c812023-12-12 16:40:22 +0900663 const bool needMoreSignals = schedule->getController().addPresentFence(std::move(fence));
664 if (needMoreSignals) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400665 schedule->enableHardwareVsync();
Ana Krulece588e312018-09-18 12:32:24 -0700666 } else {
Dominik Laskowski66295432023-03-14 12:25:36 -0400667 constexpr bool kDisallow = false;
668 schedule->disableHardwareVsync(kDisallow);
Ana Krulece588e312018-09-18 12:32:24 -0700669 }
670}
671
Vishnu Nair5c61a012024-08-05 21:14:39 -0700672void Scheduler::registerLayer(Layer* layer, FrameRateCompatibility frameRateCompatibility) {
Marin Shalamanov4be385e2021-04-23 13:25:30 +0200673 // If the content detection feature is off, we still keep the layer history,
674 // since we use it for other features (like Frame Rate API), so layers
675 // still need to be registered.
Vishnu Nair5c61a012024-08-05 21:14:39 -0700676 mLayerHistory.registerLayer(layer, mFeatures.test(Feature::kContentDetection),
677 frameRateCompatibility);
Ady Abraham09bd3922019-04-08 10:44:56 -0700678}
679
Ady Abrahambdda8f02021-04-01 16:06:11 -0700680void Scheduler::deregisterLayer(Layer* layer) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700681 mLayerHistory.deregisterLayer(layer);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700682}
683
Ady Abraham822ecbd2023-07-07 16:16:09 -0700684void Scheduler::onLayerDestroyed(Layer* layer) {
685 std::scoped_lock lock(mChoreographerLock);
686 mAttachedChoreographers.erase(layer->getSequence());
687}
688
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000689void Scheduler::recordLayerHistory(int32_t id, const LayerProps& layerProps, nsecs_t presentTime,
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700690 nsecs_t now, LayerHistory::LayerUpdateType updateType) {
Ady Abrahamf8fdc452024-04-05 16:22:55 +0000691 if (pacesetterSelectorPtr()->canSwitch()) {
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700692 mLayerHistory.record(id, layerProps, presentTime, now, updateType);
Dominik Laskowski49cea512019-11-12 14:13:23 -0800693 }
Ana Krulec3084c052018-11-21 20:27:17 +0100694}
695
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100696void Scheduler::setModeChangePending(bool pending) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700697 mLayerHistory.setModeChangePending(pending);
Ady Abraham32efd542020-05-19 17:49:26 -0700698}
699
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700700void Scheduler::setDefaultFrameRateCompatibility(
701 int32_t id, scheduler::FrameRateCompatibility frameRateCompatibility) {
702 mLayerHistory.setDefaultFrameRateCompatibility(id, frameRateCompatibility,
Andy Labrada096227e2022-06-15 16:58:11 +0000703 mFeatures.test(Feature::kContentDetection));
704}
705
Vishnu Nair41376b62023-11-08 05:08:58 -0800706void Scheduler::setLayerProperties(int32_t id, const android::scheduler::LayerProps& properties) {
707 mLayerHistory.setLayerProperties(id, properties);
708}
709
Ady Abraham822ecbd2023-07-07 16:16:09 -0700710void Scheduler::chooseRefreshRateForContent(
711 const surfaceflinger::frontend::LayerHierarchy* hierarchy,
712 bool updateAttachedChoreographer) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500713 const auto selectorPtr = pacesetterSelectorPtr();
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400714 if (!selectorPtr->canSwitch()) return;
Dominik Laskowski49cea512019-11-12 14:13:23 -0800715
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000716 SFTRACE_CALL();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800717
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400718 LayerHistory::Summary summary = mLayerHistory.summarize(*selectorPtr, systemTime());
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800719 applyPolicy(&Policy::contentRequirements, std::move(summary));
Ady Abraham822ecbd2023-07-07 16:16:09 -0700720
721 if (updateAttachedChoreographer) {
722 LOG_ALWAYS_FATAL_IF(!hierarchy);
723
724 // update the attached choreographers after we selected the render rate.
725 const ftl::Optional<FrameRateMode> modeOpt = [&] {
726 std::scoped_lock lock(mPolicyLock);
727 return mPolicy.modeOpt;
728 }();
729
730 if (modeOpt) {
731 updateAttachedChoreographers(*hierarchy, modeOpt->fps);
732 }
733 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800734}
735
Ana Krulecfb772822018-11-30 10:44:07 +0100736void Scheduler::resetIdleTimer() {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500737 pacesetterSelectorPtr()->resetIdleTimer();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800738}
739
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700740void Scheduler::onTouchHint() {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700741 if (mTouchTimer) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800742 mTouchTimer->reset();
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500743 pacesetterSelectorPtr()->resetKernelIdleTimer();
Dominik Laskowski49cea512019-11-12 14:13:23 -0800744 }
Ady Abraham8532d012019-05-08 14:50:56 -0700745}
746
Leon Scroggins III67388622023-02-06 20:36:20 -0500747void Scheduler::setDisplayPowerMode(PhysicalDisplayId id, hal::PowerMode powerMode) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500748 const bool isPacesetter = [this, id]() REQUIRES(kMainThreadContext) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500749 ftl::FakeGuard guard(mDisplayLock);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500750 return id == mPacesetterDisplayId;
Leon Scroggins III67388622023-02-06 20:36:20 -0500751 }();
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500752 if (isPacesetter) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500753 // TODO (b/255657128): This needs to be handled per display.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700754 std::lock_guard<std::mutex> lock(mPolicyLock);
Rachel Lee6a9731d2022-06-06 17:08:14 -0700755 mPolicy.displayPowerMode = powerMode;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700756 }
Leon Scroggins III67388622023-02-06 20:36:20 -0500757 {
758 std::scoped_lock lock(mDisplayLock);
Dominik Laskowski66295432023-03-14 12:25:36 -0400759
760 const auto displayOpt = mDisplays.get(id);
761 LOG_ALWAYS_FATAL_IF(!displayOpt);
762 auto& display = displayOpt->get();
763
764 display.powerMode = powerMode;
765 display.schedulePtr->getController().setDisplayPowerMode(powerMode);
Leon Scroggins III67388622023-02-06 20:36:20 -0500766 }
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500767 if (!isPacesetter) return;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700768
769 if (mDisplayPowerTimer) {
770 mDisplayPowerTimer->reset();
771 }
772
773 // Display Power event will boost the refresh rate to performance.
774 // Clear Layer History to get fresh FPS detection
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700775 mLayerHistory.clear();
Ady Abraham6fe2c172019-07-12 12:37:57 -0700776}
777
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500778auto Scheduler::getVsyncSchedule(std::optional<PhysicalDisplayId> idOpt) const
779 -> ConstVsyncSchedulePtr {
Leon Scroggins III67388622023-02-06 20:36:20 -0500780 std::scoped_lock lock(mDisplayLock);
781 return getVsyncScheduleLocked(idOpt);
782}
783
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500784auto Scheduler::getVsyncScheduleLocked(std::optional<PhysicalDisplayId> idOpt) const
785 -> ConstVsyncSchedulePtr {
Leon Scroggins III67388622023-02-06 20:36:20 -0500786 ftl::FakeGuard guard(kMainThreadContext);
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500787
Leon Scroggins III67388622023-02-06 20:36:20 -0500788 if (!idOpt) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500789 LOG_ALWAYS_FATAL_IF(!mPacesetterDisplayId, "Missing a pacesetter!");
790 idOpt = mPacesetterDisplayId;
Leon Scroggins III67388622023-02-06 20:36:20 -0500791 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500792
793 const auto displayOpt = mDisplays.get(*idOpt);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400794 if (!displayOpt) {
795 return nullptr;
796 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500797 return displayOpt->get().schedulePtr;
Leon Scroggins III67388622023-02-06 20:36:20 -0500798}
799
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700800void Scheduler::kernelIdleTimerCallback(TimerState state) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000801 SFTRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100802
Ady Abraham2139f732019-11-13 18:56:40 -0800803 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
804 // magic number
ramindania04b8a52023-08-07 18:49:47 -0700805 const Fps refreshRate = pacesetterSelectorPtr()->getActiveMode().modePtr->getPeakFps();
Ady Abraham3efa3942021-06-24 19:01:25 -0700806
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700807 constexpr Fps FPS_THRESHOLD_FOR_KERNEL_TIMER = 65_Hz;
808 using namespace fps_approx_ops;
809
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800810 if (state == TimerState::Reset && refreshRate > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700811 // If we're not in performance mode then the kernel timer shouldn't do
812 // anything, as the refresh rate during DPU power collapse will be the
813 // same.
Leon Scroggins III67388622023-02-06 20:36:20 -0500814 resyncAllToHardwareVsync(true /* allowToEnable */);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800815 } else if (state == TimerState::Expired && refreshRate <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700816 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
817 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
Ady Abraham8cb21882020-08-26 18:22:05 -0700818 // need to update the VsyncController model anyway.
Leon Scroggins III67388622023-02-06 20:36:20 -0500819 std::scoped_lock lock(mDisplayLock);
820 ftl::FakeGuard guard(kMainThreadContext);
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500821 for (const auto& [_, display] : mDisplays) {
822 constexpr bool kDisallow = false;
Dominik Laskowski66295432023-03-14 12:25:36 -0400823 display.schedulePtr->disableHardwareVsync(kDisallow);
Leon Scroggins III67388622023-02-06 20:36:20 -0500824 }
Alec Mouridc28b372019-04-18 21:17:13 -0700825 }
Ady Abrahama09852a2020-02-20 14:23:42 -0800826
827 mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
Alec Mouridc28b372019-04-18 21:17:13 -0700828}
829
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700830void Scheduler::idleTimerCallback(TimerState state) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800831 applyPolicy(&Policy::idleTimer, state);
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000832 SFTRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100833}
834
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700835void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700836 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700837 // Touch event will boost the refresh rate to performance.
838 // Clear layer history to get fresh FPS detection.
839 // NOTE: Instead of checking all the layers, we should be checking the layer
840 // that is currently on top. b/142507166 will give us this capability.
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800841 if (applyPolicy(&Policy::touch, touch).touch) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700842 mLayerHistory.clear();
Ady Abraham1adbb722020-05-15 11:51:48 -0700843 }
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000844 SFTRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700845}
846
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700847void Scheduler::displayPowerTimerCallback(TimerState state) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800848 applyPolicy(&Policy::displayPowerTimer, state);
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000849 SFTRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700850}
851
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400852void Scheduler::dump(utils::Dumper& dumper) const {
853 using namespace std::string_view_literals;
Ady Abraham4f960d12021-10-13 16:59:49 -0700854
855 {
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400856 utils::Dumper::Section section(dumper, "Features"sv);
857
858 for (Feature feature : ftl::enum_range<Feature>()) {
859 if (const auto flagOpt = ftl::flag_name(feature)) {
860 dumper.dump(flagOpt->substr(1), mFeatures.test(feature));
861 }
862 }
863 }
864 {
865 utils::Dumper::Section section(dumper, "Policy"sv);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400866 {
867 std::scoped_lock lock(mDisplayLock);
868 ftl::FakeGuard guard(kMainThreadContext);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500869 dumper.dump("pacesetterDisplayId"sv, mPacesetterDisplayId);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400870 }
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400871 dumper.dump("layerHistory"sv, mLayerHistory.dump());
872 dumper.dump("touchTimer"sv, mTouchTimer.transform(&OneShotTimer::interval));
873 dumper.dump("displayPowerTimer"sv, mDisplayPowerTimer.transform(&OneShotTimer::interval));
874 }
875
876 mFrameRateOverrideMappings.dump(dumper);
877 dumper.eol();
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400878
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500879 mVsyncConfiguration->dump(dumper.out());
880 dumper.eol();
881
882 mRefreshRateStats->dump(dumper.out());
883 dumper.eol();
884
Dominik Laskowskife749dc2024-07-26 10:09:31 -0400885 std::scoped_lock lock(mDisplayLock);
886 ftl::FakeGuard guard(kMainThreadContext);
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500887
Dominik Laskowskife749dc2024-07-26 10:09:31 -0400888 for (const auto& [id, display] : mDisplays) {
889 utils::Dumper::Section
890 section(dumper,
891 id == mPacesetterDisplayId
892 ? ftl::Concat("Pacesetter Display ", id.value).c_str()
893 : ftl::Concat("Follower Display ", id.value).c_str());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500894
Dominik Laskowskife749dc2024-07-26 10:09:31 -0400895 display.selectorPtr->dump(dumper);
896 display.targeterPtr->dump(dumper);
897 dumper.eol();
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500898 }
Ana Krulecb43429d2019-01-09 14:28:51 -0800899}
900
Dominik Laskowski068173d2021-08-11 17:22:59 -0700901void Scheduler::dumpVsync(std::string& out) const {
Leon Scroggins III67388622023-02-06 20:36:20 -0500902 std::scoped_lock lock(mDisplayLock);
903 ftl::FakeGuard guard(kMainThreadContext);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500904 if (mPacesetterDisplayId) {
905 base::StringAppendF(&out, "VsyncSchedule for pacesetter %s:\n",
906 to_string(*mPacesetterDisplayId).c_str());
Leon Scroggins III67388622023-02-06 20:36:20 -0500907 getVsyncScheduleLocked()->dump(out);
908 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500909 for (auto& [id, display] : mDisplays) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500910 if (id == mPacesetterDisplayId) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500911 continue;
912 }
913 base::StringAppendF(&out, "VsyncSchedule for follower %s:\n", to_string(id).c_str());
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500914 display.schedulePtr->dump(out);
Leon Scroggins III67388622023-02-06 20:36:20 -0500915 }
Ady Abraham8735eac2020-08-12 16:35:04 -0700916}
917
Aditya Kumar860ef882024-10-10 13:47:44 -0700918#pragma clang diagnostic push
919#pragma clang diagnostic ignored "-Wunused-value" // b/369277774
Dominik Laskowski1d7be062024-07-24 13:59:07 -0400920void Scheduler::updateFrameRateOverrides(GlobalSignals consideredSignals, Fps displayRefreshRate) {
921 const bool changed = (std::scoped_lock(mPolicyLock),
922 updateFrameRateOverridesLocked(consideredSignals, displayRefreshRate));
923
924 if (changed) {
925 onFrameRateOverridesChanged();
926 }
Ady Abraham33a386b2023-07-18 15:37:11 -0700927}
Aditya Kumar860ef882024-10-10 13:47:44 -0700928#pragma clang diagnostic pop
Ady Abraham33a386b2023-07-18 15:37:11 -0700929
930bool Scheduler::updateFrameRateOverridesLocked(GlobalSignals consideredSignals,
931 Fps displayRefreshRate) {
Dominik Laskowski596a2562022-10-28 11:26:12 -0400932 if (consideredSignals.idle) return false;
933
934 const auto frameRateOverrides =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500935 pacesetterSelectorPtr()->getFrameRateOverrides(mPolicy.contentRequirements,
936 displayRefreshRate, consideredSignals);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400937
938 // Note that RefreshRateSelector::supportsFrameRateOverrideByContent is checked when querying
939 // the FrameRateOverrideMappings rather than here.
940 return mFrameRateOverrideMappings.updateFrameRateOverridesByContent(frameRateOverrides);
941}
942
Melody Hsue524dd92024-08-27 22:27:29 +0000943void Scheduler::addBufferStuffedUids(BufferStuffingMap bufferStuffedUids) {
944 if (!mRenderEventThread) return;
945 mRenderEventThread->addBufferStuffedUids(std::move(bufferStuffedUids));
946}
947
Dominik Laskowskib9724192024-07-12 15:55:28 -0400948void Scheduler::promotePacesetterDisplay(PhysicalDisplayId pacesetterId, PromotionParams params) {
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400949 std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule;
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400950 {
951 std::scoped_lock lock(mDisplayLock);
Dominik Laskowskib9724192024-07-12 15:55:28 -0400952 pacesetterVsyncSchedule = promotePacesetterDisplayLocked(pacesetterId, params);
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400953 }
954
Leon Scroggins39d25342023-04-19 17:11:01 +0000955 applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400956}
957
958std::shared_ptr<VsyncSchedule> Scheduler::promotePacesetterDisplayLocked(
Dominik Laskowskib9724192024-07-12 15:55:28 -0400959 PhysicalDisplayId pacesetterId, PromotionParams params) {
Dominik Laskowski16d1b3c2024-07-12 14:35:00 -0400960 // TODO: b/241286431 - Choose the pacesetter among mDisplays.
961 mPacesetterDisplayId = pacesetterId;
962 ALOGI("Display %s is the pacesetter", to_string(pacesetterId).c_str());
Dominik Laskowski596a2562022-10-28 11:26:12 -0400963
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500964 std::shared_ptr<VsyncSchedule> newVsyncSchedulePtr;
965 if (const auto pacesetterOpt = pacesetterDisplayLocked()) {
966 const Display& pacesetter = *pacesetterOpt;
967
Dominik Laskowskib9724192024-07-12 15:55:28 -0400968 if (!FlagManager::getInstance().connected_display() || params.toggleIdleTimer) {
969 pacesetter.selectorPtr->setIdleTimerCallbacks(
970 {.platform = {.onReset = [this] { idleTimerCallback(TimerState::Reset); },
971 .onExpired = [this] { idleTimerCallback(TimerState::Expired); }},
972 .kernel = {.onReset = [this] { kernelIdleTimerCallback(TimerState::Reset); },
973 .onExpired =
974 [this] { kernelIdleTimerCallback(TimerState::Expired); }},
975 .vrr = {.onReset = [this] { mSchedulerCallback.vrrDisplayIdle(false); },
976 .onExpired = [this] { mSchedulerCallback.vrrDisplayIdle(true); }}});
Dominik Laskowski596a2562022-10-28 11:26:12 -0400977
Dominik Laskowskib9724192024-07-12 15:55:28 -0400978 pacesetter.selectorPtr->startIdleTimer();
979 }
Leon Scroggins III67388622023-02-06 20:36:20 -0500980
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500981 newVsyncSchedulePtr = pacesetter.schedulePtr;
982
Dominik Laskowski66295432023-03-14 12:25:36 -0400983 constexpr bool kForce = true;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800984 newVsyncSchedulePtr->onDisplayModeChanged(pacesetter.selectorPtr->getActiveMode().modePtr,
985 kForce);
Leon Scroggins III67388622023-02-06 20:36:20 -0500986 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500987 return newVsyncSchedulePtr;
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400988}
Leon Scroggins III67388622023-02-06 20:36:20 -0500989
Leon Scroggins39d25342023-04-19 17:11:01 +0000990void Scheduler::applyNewVsyncSchedule(std::shared_ptr<VsyncSchedule> vsyncSchedule) {
991 onNewVsyncSchedule(vsyncSchedule->getDispatch());
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500992
993 if (hasEventThreads()) {
994 eventThreadFor(Cycle::Render).onNewVsyncSchedule(vsyncSchedule);
995 eventThreadFor(Cycle::LastComposite).onNewVsyncSchedule(vsyncSchedule);
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400996 }
Dominik Laskowski596a2562022-10-28 11:26:12 -0400997}
998
Dominik Laskowskib9724192024-07-12 15:55:28 -0400999void Scheduler::demotePacesetterDisplay(PromotionParams params) {
1000 if (!FlagManager::getInstance().connected_display() || params.toggleIdleTimer) {
1001 // No need to lock for reads on kMainThreadContext.
1002 if (const auto pacesetterPtr =
1003 FTL_FAKE_GUARD(mDisplayLock, pacesetterSelectorPtrLocked())) {
1004 pacesetterPtr->stopIdleTimer();
1005 pacesetterPtr->clearIdleTimerCallbacks();
1006 }
Dominik Laskowski596a2562022-10-28 11:26:12 -04001007 }
1008
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001009 // Clear state that depends on the pacesetter's RefreshRateSelector.
Dominik Laskowski596a2562022-10-28 11:26:12 -04001010 std::scoped_lock lock(mPolicyLock);
1011 mPolicy = {};
Ady Abraham62a0be22020-12-08 16:54:10 -08001012}
1013
Ady Abraham822ecbd2023-07-07 16:16:09 -07001014void Scheduler::updateAttachedChoreographersFrameRate(
1015 const surfaceflinger::frontend::RequestedLayerState& layer, Fps fps) {
1016 std::scoped_lock lock(mChoreographerLock);
1017
1018 const auto layerId = static_cast<int32_t>(layer.id);
1019 const auto choreographers = mAttachedChoreographers.find(layerId);
1020 if (choreographers == mAttachedChoreographers.end()) {
1021 return;
1022 }
1023
1024 auto& layerChoreographers = choreographers->second;
1025
1026 layerChoreographers.frameRate = fps;
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001027 SFTRACE_FORMAT_INSTANT("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str());
Ady Abraham822ecbd2023-07-07 16:16:09 -07001028 ALOGV("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str());
1029
1030 auto it = layerChoreographers.connections.begin();
1031 while (it != layerChoreographers.connections.end()) {
1032 sp<EventThreadConnection> choreographerConnection = it->promote();
1033 if (choreographerConnection) {
1034 choreographerConnection->frameRate = fps;
1035 it++;
1036 } else {
1037 it = choreographers->second.connections.erase(it);
1038 }
1039 }
1040
1041 if (layerChoreographers.connections.empty()) {
1042 mAttachedChoreographers.erase(choreographers);
1043 }
1044}
1045
1046int Scheduler::updateAttachedChoreographersInternal(
1047 const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate,
1048 int parentDivisor) {
1049 const char* name = layerHierarchy.getLayer() ? layerHierarchy.getLayer()->name.c_str() : "Root";
1050
1051 int divisor = 0;
1052 if (layerHierarchy.getLayer()) {
1053 const auto frameRateCompatibility = layerHierarchy.getLayer()->frameRateCompatibility;
1054 const auto frameRate = Fps::fromValue(layerHierarchy.getLayer()->frameRate);
1055 ALOGV("%s: %s frameRate %s parentDivisor=%d", __func__, name, to_string(frameRate).c_str(),
1056 parentDivisor);
1057
1058 if (frameRate.isValid()) {
1059 if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE ||
1060 frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_EXACT) {
1061 // Since this layer wants an exact match, we would only set a frame rate if the
1062 // desired rate is a divisor of the display refresh rate.
1063 divisor = RefreshRateSelector::getFrameRateDivisor(displayRefreshRate, frameRate);
1064 } else if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT) {
1065 // find the closest frame rate divisor for the desired frame rate.
1066 divisor = static_cast<int>(
1067 std::round(displayRefreshRate.getValue() / frameRate.getValue()));
1068 }
1069 }
1070 }
1071
1072 // We start by traversing the children, updating their choreographers, and getting back the
1073 // aggregated frame rate.
1074 int childrenDivisor = 0;
1075 for (const auto& [child, _] : layerHierarchy.mChildren) {
1076 LOG_ALWAYS_FATAL_IF(child == nullptr || child->getLayer() == nullptr);
1077
1078 ALOGV("%s: %s traversing child %s", __func__, name, child->getLayer()->name.c_str());
1079
1080 const int childDivisor =
1081 updateAttachedChoreographersInternal(*child, displayRefreshRate, divisor);
1082 childrenDivisor = childrenDivisor > 0 ? childrenDivisor : childDivisor;
1083 if (childDivisor > 0) {
1084 childrenDivisor = std::gcd(childrenDivisor, childDivisor);
1085 }
1086 ALOGV("%s: %s childrenDivisor=%d", __func__, name, childrenDivisor);
1087 }
1088
1089 ALOGV("%s: %s divisor=%d", __func__, name, divisor);
1090
1091 // If there is no explicit vote for this layer. Use the children's vote if exists
1092 divisor = (divisor == 0) ? childrenDivisor : divisor;
1093 ALOGV("%s: %s divisor=%d with children", __func__, name, divisor);
1094
1095 // If there is no explicit vote for this layer or its children, Use the parent vote if exists
1096 divisor = (divisor == 0) ? parentDivisor : divisor;
1097 ALOGV("%s: %s divisor=%d with parent", __func__, name, divisor);
1098
1099 if (layerHierarchy.getLayer()) {
1100 Fps fps = divisor > 1 ? displayRefreshRate / (unsigned int)divisor : Fps();
1101 updateAttachedChoreographersFrameRate(*layerHierarchy.getLayer(), fps);
1102 }
1103
1104 return divisor;
1105}
1106
1107void Scheduler::updateAttachedChoreographers(
1108 const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001109 SFTRACE_CALL();
Ady Abraham822ecbd2023-07-07 16:16:09 -07001110 updateAttachedChoreographersInternal(layerHierarchy, displayRefreshRate, 0);
1111}
1112
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001113template <typename S, typename T>
1114auto Scheduler::applyPolicy(S Policy::*statePtr, T&& newState) -> GlobalSignals {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001115 SFTRACE_CALL();
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001116 std::vector<display::DisplayModeRequest> modeRequests;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001117 GlobalSignals consideredSignals;
1118
Ady Abraham62a0be22020-12-08 16:54:10 -08001119 bool refreshRateChanged = false;
1120 bool frameRateOverridesChanged;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001121
Ady Abraham8532d012019-05-08 14:50:56 -07001122 {
Dominik Laskowski596a2562022-10-28 11:26:12 -04001123 std::scoped_lock lock(mPolicyLock);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001124
1125 auto& currentState = mPolicy.*statePtr;
1126 if (currentState == newState) return {};
1127 currentState = std::forward<T>(newState);
1128
Dominik Laskowski596a2562022-10-28 11:26:12 -04001129 DisplayModeChoiceMap modeChoices;
Ady Abrahamace3d052022-11-17 16:25:05 -08001130 ftl::Optional<FrameRateMode> modeOpt;
Dominik Laskowski596a2562022-10-28 11:26:12 -04001131 {
1132 std::scoped_lock lock(mDisplayLock);
1133 ftl::FakeGuard guard(kMainThreadContext);
1134
1135 modeChoices = chooseDisplayModes();
1136
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001137 // TODO(b/240743786): The pacesetter display's mode must change for any
1138 // DisplayModeRequest to go through. Fix this by tracking per-display Scheduler::Policy
1139 // and timers.
Ady Abrahamace3d052022-11-17 16:25:05 -08001140 std::tie(modeOpt, consideredSignals) =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001141 modeChoices.get(*mPacesetterDisplayId)
Dominik Laskowski596a2562022-10-28 11:26:12 -04001142 .transform([](const DisplayModeChoice& choice) {
Ady Abrahamace3d052022-11-17 16:25:05 -08001143 return std::make_pair(choice.mode, choice.consideredSignals);
Dominik Laskowski596a2562022-10-28 11:26:12 -04001144 })
1145 .value();
1146 }
ramindani69b58e82022-09-26 16:48:36 -07001147
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001148 modeRequests.reserve(modeChoices.size());
1149 for (auto& [id, choice] : modeChoices) {
1150 modeRequests.emplace_back(
Ady Abrahamace3d052022-11-17 16:25:05 -08001151 display::DisplayModeRequest{.mode = std::move(choice.mode),
Dominik Laskowskifc94b412024-08-03 15:02:23 -04001152 .emitEvent = choice.consideredSignals
1153 .shouldEmitEvent()});
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001154 }
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001155
ramindanif2f40f42024-06-18 16:48:28 -07001156 if (!FlagManager::getInstance().vrr_bugfix_dropped_frame()) {
1157 frameRateOverridesChanged =
1158 updateFrameRateOverridesLocked(consideredSignals, modeOpt->fps);
1159 }
Ady Abrahamace3d052022-11-17 16:25:05 -08001160 if (mPolicy.modeOpt != modeOpt) {
1161 mPolicy.modeOpt = modeOpt;
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001162 refreshRateChanged = true;
Dominik Laskowskifc94b412024-08-03 15:02:23 -04001163 } else if (consideredSignals.shouldEmitEvent()) {
1164 // The mode did not change, but we may need to emit if DisplayModeRequest::emitEvent was
1165 // previously false.
1166 emitModeChangeIfNeeded();
Ady Abraham8532d012019-05-08 14:50:56 -07001167 }
Ady Abraham8532d012019-05-08 14:50:56 -07001168 }
Ady Abraham62a0be22020-12-08 16:54:10 -08001169 if (refreshRateChanged) {
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001170 mSchedulerCallback.requestDisplayModes(std::move(modeRequests));
Ady Abraham62a0be22020-12-08 16:54:10 -08001171 }
ramindanif2f40f42024-06-18 16:48:28 -07001172
1173 if (FlagManager::getInstance().vrr_bugfix_dropped_frame()) {
1174 std::scoped_lock lock(mPolicyLock);
1175 frameRateOverridesChanged =
1176 updateFrameRateOverridesLocked(consideredSignals, mPolicy.modeOpt->fps);
1177 }
Ady Abraham62a0be22020-12-08 16:54:10 -08001178 if (frameRateOverridesChanged) {
Dominik Laskowski1d7be062024-07-24 13:59:07 -04001179 onFrameRateOverridesChanged();
Ady Abraham62a0be22020-12-08 16:54:10 -08001180 }
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001181 return consideredSignals;
Ady Abraham8532d012019-05-08 14:50:56 -07001182}
1183
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001184auto Scheduler::chooseDisplayModes() const -> DisplayModeChoiceMap {
Vishnu Nairbe0ad902024-06-27 23:38:43 +00001185 SFTRACE_CALL();
Ady Abraham09bd3922019-04-08 10:44:56 -07001186
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001187 DisplayModeChoiceMap modeChoices;
Dominik Laskowski01602522022-10-07 19:02:28 -04001188 const auto globalSignals = makeGlobalSignals();
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001189
1190 const Fps pacesetterFps = [&]() REQUIRES(mPolicyLock, mDisplayLock, kMainThreadContext) {
1191 auto rankedFrameRates =
1192 pacesetterSelectorPtrLocked()->getRankedFrameRates(mPolicy.contentRequirements,
1193 globalSignals);
1194
1195 const Fps pacesetterFps = rankedFrameRates.ranking.front().frameRateMode.fps;
1196
1197 modeChoices.try_emplace(*mPacesetterDisplayId,
1198 DisplayModeChoice::from(std::move(rankedFrameRates)));
1199 return pacesetterFps;
1200 }();
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001201
Dominik Laskowski086507b2024-05-16 15:33:16 -04001202 // Choose a mode for powered-on follower displays.
Dominik Laskowskic404cb42023-03-03 19:57:53 -05001203 for (const auto& [id, display] : mDisplays) {
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001204 if (id == *mPacesetterDisplayId) continue;
Dominik Laskowski086507b2024-05-16 15:33:16 -04001205 if (display.powerMode != hal::PowerMode::ON) continue;
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001206
Ady Abrahamace3d052022-11-17 16:25:05 -08001207 auto rankedFrameRates =
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001208 display.selectorPtr->getRankedFrameRates(mPolicy.contentRequirements, globalSignals,
1209 pacesetterFps);
1210
1211 modeChoices.try_emplace(id, DisplayModeChoice::from(std::move(rankedFrameRates)));
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001212 }
ramindani69b58e82022-09-26 16:48:36 -07001213
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001214 return modeChoices;
ramindani69b58e82022-09-26 16:48:36 -07001215}
1216
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001217GlobalSignals Scheduler::makeGlobalSignals() const {
ramindani38c84982022-08-29 18:02:57 +00001218 const bool powerOnImminent = mDisplayPowerTimer &&
1219 (mPolicy.displayPowerMode != hal::PowerMode::ON ||
1220 mPolicy.displayPowerTimer == TimerState::Reset);
Ady Abraham6fe2c172019-07-12 12:37:57 -07001221
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001222 return {.touch = mTouchTimer && mPolicy.touch == TouchState::Active,
1223 .idle = mPolicy.idleTimer == TimerState::Expired,
1224 .powerOnImminent = powerOnImminent};
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001225}
1226
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001227FrameRateMode Scheduler::getPreferredDisplayMode() {
Dominik Laskowski068173d2021-08-11 17:22:59 -07001228 std::lock_guard<std::mutex> lock(mPolicyLock);
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001229 const auto frameRateMode =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001230 pacesetterSelectorPtr()
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001231 ->getRankedFrameRates(mPolicy.contentRequirements, makeGlobalSignals())
1232 .ranking.front()
1233 .frameRateMode;
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001234
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001235 // Make sure the stored mode is up to date.
1236 mPolicy.modeOpt = frameRateMode;
1237
1238 return frameRateMode;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -07001239}
1240
Peiyong Line9d809e2020-04-14 13:10:48 -07001241void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
Ady Abraham3a77a7b2019-12-02 18:46:59 -08001242 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
1243 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
1244
1245 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
1246 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
1247 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
1248 }
1249}
1250
Leon Scroggins III5b581492023-10-31 14:29:41 -04001251bool Scheduler::onCompositionPresented(nsecs_t presentTime) {
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001252 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
1253 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
1254 if (presentTime < mLastVsyncPeriodChangeTimeline->refreshTimeNanos) {
1255 // We need to composite again as refreshTimeNanos is still in the future.
1256 return true;
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001257 }
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001258
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001259 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001260 }
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001261 return false;
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001262}
1263
Ady Abraham7825c682021-05-17 15:12:14 -07001264void Scheduler::onActiveDisplayAreaChanged(uint32_t displayArea) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -07001265 mLayerHistory.setDisplayArea(displayArea);
Ady Abraham8a82ba62020-01-17 12:43:17 -08001266}
1267
Andy Yu8c2703d2023-11-03 11:22:46 -07001268void Scheduler::setGameModeFrameRateForUid(FrameRateOverride frameRateOverride) {
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001269 if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) {
1270 return;
1271 }
1272
Andy Yu8c2703d2023-11-03 11:22:46 -07001273 if (FlagManager::getInstance().game_default_frame_rate()) {
1274 // update the frame rate override mapping in LayerHistory
1275 mLayerHistory.updateGameModeFrameRateOverride(frameRateOverride);
1276 } else {
1277 mFrameRateOverrideMappings.setGameModeRefreshRateForUid(frameRateOverride);
1278 }
Dominik Laskowski1d7be062024-07-24 13:59:07 -04001279
1280 onFrameRateOverridesChanged();
Andy Yu8c2703d2023-11-03 11:22:46 -07001281}
1282
1283void Scheduler::setGameDefaultFrameRateForUid(FrameRateOverride frameRateOverride) {
1284 if (!FlagManager::getInstance().game_default_frame_rate() ||
1285 (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f)) {
1286 return;
1287 }
1288
1289 // update the frame rate override mapping in LayerHistory
1290 mLayerHistory.updateGameDefaultFrameRateOverride(frameRateOverride);
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001291}
1292
Ady Abraham62a0be22020-12-08 16:54:10 -08001293void Scheduler::setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride) {
1294 if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) {
1295 return;
1296 }
1297
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001298 mFrameRateOverrideMappings.setPreferredRefreshRateForUid(frameRateOverride);
Dominik Laskowski1d7be062024-07-24 13:59:07 -04001299 onFrameRateOverridesChanged();
Ady Abraham62a0be22020-12-08 16:54:10 -08001300}
1301
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001302void Scheduler::updateSmallAreaDetection(
Tony Huangf3621102023-09-04 17:14:22 +08001303 std::vector<std::pair<int32_t, float>>& uidThresholdMappings) {
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001304 mSmallAreaDetectionAllowMappings.update(uidThresholdMappings);
1305}
1306
Tony Huangf3621102023-09-04 17:14:22 +08001307void Scheduler::setSmallAreaDetectionThreshold(int32_t appId, float threshold) {
Jerry Chang36678002023-11-29 16:56:17 +00001308 mSmallAreaDetectionAllowMappings.setThresholdForAppId(appId, threshold);
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001309}
1310
Tony Huangf3621102023-09-04 17:14:22 +08001311bool Scheduler::isSmallDirtyArea(int32_t appId, uint32_t dirtyArea) {
1312 std::optional<float> oThreshold = mSmallAreaDetectionAllowMappings.getThresholdForAppId(appId);
1313 if (oThreshold) {
1314 return mLayerHistory.isSmallDirtyArea(dirtyArea, oThreshold.value());
1315 }
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001316 return false;
1317}
1318
Dominik Laskowski068173d2021-08-11 17:22:59 -07001319} // namespace android::scheduler