blob: 9ea3f350576151e82cef57b1d504140e23f5e1e3 [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>
Ana Krulece588e312018-09-18 12:32:24 -070027#include <configstore/Utils.h>
Dominik Laskowskiec0eac22023-01-28 16:16:19 -050028#include <ftl/concat.h>
Dominik Laskowski03cfce82022-11-02 12:13:29 -040029#include <ftl/enum.h>
ramindania556d072022-06-14 23:25:11 +000030#include <ftl/fake_guard.h>
Dominik Laskowski01602522022-10-07 19:02:28 -040031#include <ftl/small_map.h>
Ady Abraham9243bba2023-02-10 15:31:14 -080032#include <gui/TraceUtils.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 <algorithm>
42#include <cinttypes>
43#include <cstdint>
44#include <functional>
45#include <memory>
46#include <numeric>
47
Alec Mouri9b133ca2023-11-14 19:00:01 +000048#include <common/FlagManager.h>
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070049#include "../Layer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070050#include "EventThread.h"
Andy Yu2ae6b6b2021-11-18 14:51:06 -080051#include "FrameRateOverrideMappings.h"
Rachel Lee2248f522023-01-27 16:45:23 -080052#include "FrontEnd/LayerHandle.h"
Ana Krulecf2c006d2019-06-21 15:37:07 -070053#include "OneShotTimer.h"
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050054#include "RefreshRateStats.h"
55#include "SurfaceFlingerFactory.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090056#include "SurfaceFlingerProperties.h"
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050057#include "TimeStats/TimeStats.h"
Dominik Laskowskic404cb42023-03-03 19:57:53 -050058#include "VSyncTracker.h"
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050059#include "VsyncConfiguration.h"
Dominik Laskowskic404cb42023-03-03 19:57:53 -050060#include "VsyncController.h"
61#include "VsyncSchedule.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070062
Dominik Laskowski068173d2021-08-11 17:22:59 -070063namespace android::scheduler {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070064
Dominik Laskowski1c99a002023-01-20 17:10:36 -050065Scheduler::Scheduler(ICompositor& compositor, ISchedulerCallback& callback, FeatureFlags features,
ramindaniae645822024-01-11 10:57:29 -080066 surfaceflinger::Factory& factory, Fps activeRefreshRate, TimeStats& timeStats)
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050067 : android::impl::MessageQueue(compositor),
Dominik Laskowski1c99a002023-01-20 17:10:36 -050068 mFeatures(features),
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050069 mVsyncConfiguration(factory.createVsyncConfiguration(activeRefreshRate)),
70 mVsyncModulator(sp<VsyncModulator>::make(mVsyncConfiguration->getCurrentConfigs())),
Leon Scroggins IIIde8d9a12024-01-23 12:05:49 -050071 mRefreshRateStats(std::make_unique<RefreshRateStats>(timeStats, activeRefreshRate)),
ramindaniae645822024-01-11 10:57:29 -080072 mSchedulerCallback(callback) {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070073
Dominik Laskowski83bd7712022-01-07 14:30:53 -080074Scheduler::~Scheduler() {
Ady Abraham011f8ba2022-11-22 15:09:07 -080075 // MessageQueue depends on VsyncSchedule, so first destroy it.
76 // Otherwise, MessageQueue will get destroyed after Scheduler's dtor,
77 // which will cause a use-after-free issue.
78 Impl::destroyVsync();
79
Dominik Laskowski83bd7712022-01-07 14:30:53 -080080 // Stop timers and wait for their threads to exit.
81 mDisplayPowerTimer.reset();
82 mTouchTimer.reset();
83
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040084 // Stop idle timer and clear callbacks, as the RefreshRateSelector may outlive the Scheduler.
Leon Scroggins III1af0fb62023-03-02 14:21:44 -050085 demotePacesetterDisplay();
Dominik Laskowski83bd7712022-01-07 14:30:53 -080086}
87
Leon Scroggins IIIa7be94e2024-01-23 12:24:30 -050088void Scheduler::initVsync(frametimeline::TokenManager& tokenManager,
89 std::chrono::nanoseconds workDuration) {
90 Impl::initVsyncInternal(getVsyncSchedule()->getDispatch(), tokenManager, workDuration);
91}
92
Dominik Laskowski9c93d602021-10-07 19:38:26 -070093void Scheduler::startTimers() {
Dominik Laskowski98041832019-08-01 18:35:59 -070094 using namespace sysprop;
Dominik Laskowski068173d2021-08-11 17:22:59 -070095 using namespace std::string_literals;
Ady Abraham8532d012019-05-08 14:50:56 -070096
Ady Abrahamc496b432023-12-01 21:35:05 +000097 const int32_t defaultTouchTimerValue =
Ady Abraham3f84c502023-11-30 18:18:06 -080098 FlagManager::getInstance().enable_fro_dependent_features() &&
99 sysprop::enable_frame_rate_override(true)
100 ? 200
101 : 0;
Ady Abrahamc496b432023-12-01 21:35:05 +0000102 if (const int32_t millis = set_touch_timer_ms(defaultTouchTimerValue); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -0700103 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -0700104 mTouchTimer.emplace(
Ady Abrahamdb3dfee2020-11-17 17:07:12 -0800105 "TouchTimer", std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700106 [this] { touchTimerCallback(TimerState::Reset); },
107 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -0700108 mTouchTimer->start();
109 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700110
Dominik Laskowski98041832019-08-01 18:35:59 -0700111 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
112 mDisplayPowerTimer.emplace(
Ady Abrahamdb3dfee2020-11-17 17:07:12 -0800113 "DisplayPowerTimer", std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700114 [this] { displayPowerTimerCallback(TimerState::Reset); },
115 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -0700116 mDisplayPowerTimer->start();
117 }
Ana Krulece588e312018-09-18 12:32:24 -0700118}
119
Dominik Laskowskic2d89d62024-07-12 14:35:00 -0400120void Scheduler::setPacesetterDisplay(PhysicalDisplayId pacesetterId) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500121 demotePacesetterDisplay();
Dominik Laskowski59db9562022-10-27 16:18:53 -0400122
Dominik Laskowskic2d89d62024-07-12 14:35:00 -0400123 promotePacesetterDisplay(pacesetterId);
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800124}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700125
Dominik Laskowski575aa722024-06-17 11:03:30 -0400126void Scheduler::registerDisplay(PhysicalDisplayId displayId, RefreshRateSelectorPtr selectorPtr,
127 PhysicalDisplayId activeDisplayId) {
ramindaniae645822024-01-11 10:57:29 -0800128 auto schedulePtr =
129 std::make_shared<VsyncSchedule>(selectorPtr->getActiveMode().modePtr, mFeatures,
130 [this](PhysicalDisplayId id, bool enable) {
131 onHardwareVsyncRequest(id, enable);
132 });
Dominik Laskowski66295432023-03-14 12:25:36 -0400133
Dominik Laskowski575aa722024-06-17 11:03:30 -0400134 registerDisplayInternal(displayId, std::move(selectorPtr), std::move(schedulePtr),
135 activeDisplayId);
Leon Scroggins III67388622023-02-06 20:36:20 -0500136}
137
138void Scheduler::registerDisplayInternal(PhysicalDisplayId displayId,
139 RefreshRateSelectorPtr selectorPtr,
Dominik Laskowski575aa722024-06-17 11:03:30 -0400140 VsyncSchedulePtr schedulePtr,
141 PhysicalDisplayId activeDisplayId) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500142 demotePacesetterDisplay();
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400143
Dominik Laskowski008bec02023-03-14 12:04:58 -0400144 auto [pacesetterVsyncSchedule, isNew] = [&]() FTL_FAKE_GUARD(kMainThreadContext) {
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400145 std::scoped_lock lock(mDisplayLock);
Dominik Laskowski008bec02023-03-14 12:04:58 -0400146 const bool isNew = mDisplays
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500147 .emplace_or_replace(displayId, displayId, std::move(selectorPtr),
148 std::move(schedulePtr), mFeatures)
Dominik Laskowski008bec02023-03-14 12:04:58 -0400149 .second;
Dominik Laskowski596a2562022-10-28 11:26:12 -0400150
Dominik Laskowski575aa722024-06-17 11:03:30 -0400151 return std::make_pair(promotePacesetterDisplayLocked(activeDisplayId), isNew);
Dominik Laskowski008bec02023-03-14 12:04:58 -0400152 }();
153
Leon Scroggins39d25342023-04-19 17:11:01 +0000154 applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
Dominik Laskowski008bec02023-03-14 12:04:58 -0400155
156 // Disable hardware VSYNC if the registration is new, as opposed to a renewal.
157 if (isNew) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400158 onHardwareVsyncRequest(displayId, false);
Dominik Laskowski008bec02023-03-14 12:04:58 -0400159 }
Dominik Laskowski091129a2024-02-21 14:26:03 -0500160
161 dispatchHotplug(displayId, Hotplug::Connected);
Dominik Laskowski01602522022-10-07 19:02:28 -0400162}
163
Dominik Laskowski575aa722024-06-17 11:03:30 -0400164void Scheduler::unregisterDisplay(PhysicalDisplayId displayId, PhysicalDisplayId activeDisplayId) {
165 LOG_ALWAYS_FATAL_IF(displayId == activeDisplayId, "Cannot unregister the active display!");
166
Dominik Laskowski091129a2024-02-21 14:26:03 -0500167 dispatchHotplug(displayId, Hotplug::Disconnected);
168
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500169 demotePacesetterDisplay();
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400170
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400171 std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule;
172 {
173 std::scoped_lock lock(mDisplayLock);
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500174 mDisplays.erase(displayId);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400175
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400176 // Do not allow removing the final display. Code in the scheduler expects
177 // there to be at least one display. (This may be relaxed in the future with
178 // headless virtual display.)
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500179 LOG_ALWAYS_FATAL_IF(mDisplays.empty(), "Cannot unregister all displays!");
Leon Scroggins IIIda21f422023-01-30 20:17:56 -0500180
Dominik Laskowski575aa722024-06-17 11:03:30 -0400181 pacesetterVsyncSchedule = promotePacesetterDisplayLocked(activeDisplayId);
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400182 }
Leon Scroggins39d25342023-04-19 17:11:01 +0000183 applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
Dominik Laskowski01602522022-10-07 19:02:28 -0400184}
185
Dominik Laskowski756b7892021-08-04 12:53:59 -0700186void Scheduler::run() {
187 while (true) {
188 waitMessage();
189 }
190}
191
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700192void Scheduler::onFrameSignal(ICompositor& compositor, VsyncId vsyncId,
193 TimePoint expectedVsyncTime) {
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500194 const FrameTargeter::BeginFrameArgs beginFrameArgs =
195 {.frameBeginTime = SchedulerClock::now(),
196 .vsyncId = vsyncId,
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500197 .expectedVsyncTime = expectedVsyncTime,
Leon Scroggins III0bd0d4c2022-12-08 13:20:45 -0500198 .sfWorkDuration = mVsyncModulator->getVsyncConfig().sfWorkDuration,
199 .hwcMinWorkDuration = mVsyncConfiguration->getCurrentConfigs().hwcMinWorkDuration};
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700200
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500201 ftl::NonNull<const Display*> pacesetterPtr = pacesetterPtrLocked();
202 pacesetterPtr->targeterPtr->beginFrame(beginFrameArgs, *pacesetterPtr->schedulePtr);
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500203
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500204 {
205 FrameTargets targets;
206 targets.try_emplace(pacesetterPtr->displayId, &pacesetterPtr->targeterPtr->target());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500207
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500208 // TODO (b/256196556): Followers should use the next VSYNC after the frontrunner, not the
209 // pacesetter.
210 // Update expectedVsyncTime, which may have been adjusted by beginFrame.
211 expectedVsyncTime = pacesetterPtr->targeterPtr->target().expectedPresentTime();
212
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500213 for (const auto& [id, display] : mDisplays) {
214 if (id == pacesetterPtr->displayId) continue;
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400215
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500216 auto followerBeginFrameArgs = beginFrameArgs;
217 followerBeginFrameArgs.expectedVsyncTime =
218 display.schedulePtr->vsyncDeadlineAfter(expectedVsyncTime);
219
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500220 FrameTargeter& targeter = *display.targeterPtr;
Leon Scroggins III370b8b52022-12-08 13:20:45 -0500221 targeter.beginFrame(followerBeginFrameArgs, *display.schedulePtr);
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500222 targets.try_emplace(id, &targeter.target());
223 }
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400224
ramindaniae645822024-01-11 10:57:29 -0800225 if (!compositor.commit(pacesetterPtr->displayId, targets)) {
226 if (FlagManager::getInstance().vrr_config()) {
227 compositor.sendNotifyExpectedPresentHint(pacesetterPtr->displayId);
228 }
Ady Abraham14beed72024-05-15 17:16:45 -0700229 mSchedulerCallback.onCommitNotComposited(pacesetterPtr->displayId);
ramindaniae645822024-01-11 10:57:29 -0800230 return;
231 }
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400232 }
233
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500234 // The pacesetter may have changed or been registered anew during commit.
235 pacesetterPtr = pacesetterPtrLocked();
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500236
237 // TODO(b/256196556): Choose the frontrunner display.
238 FrameTargeters targeters;
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500239 targeters.try_emplace(pacesetterPtr->displayId, pacesetterPtr->targeterPtr.get());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500240
241 for (auto& [id, display] : mDisplays) {
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500242 if (id == pacesetterPtr->displayId) continue;
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500243
244 FrameTargeter& targeter = *display.targeterPtr;
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500245 targeters.try_emplace(id, &targeter);
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700246 }
247
Ady Abrahamd6d80162023-10-23 12:57:41 -0700248 if (FlagManager::getInstance().vrr_config() &&
Alec Mouri1c7938e2023-09-22 04:17:23 +0000249 CC_UNLIKELY(mPacesetterFrameDurationFractionToSkip > 0.f)) {
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500250 const auto period = pacesetterPtr->targeterPtr->target().expectedFrameDuration();
Alec Mouri1c7938e2023-09-22 04:17:23 +0000251 const auto skipDuration = Duration::fromNs(
252 static_cast<nsecs_t>(period.ns() * mPacesetterFrameDurationFractionToSkip));
253 ATRACE_FORMAT("Injecting jank for %f%% of the frame (%" PRId64 " ns)",
254 mPacesetterFrameDurationFractionToSkip * 100, skipDuration.ns());
255 std::this_thread::sleep_for(skipDuration);
256 mPacesetterFrameDurationFractionToSkip = 0.f;
257 }
258
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500259 const auto resultsPerDisplay = compositor.composite(pacesetterPtr->displayId, targeters);
ramindaniae645822024-01-11 10:57:29 -0800260 if (FlagManager::getInstance().vrr_config()) {
261 compositor.sendNotifyExpectedPresentHint(pacesetterPtr->displayId);
262 }
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700263 compositor.sample();
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400264
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500265 for (const auto& [id, targeter] : targeters) {
266 const auto resultOpt = resultsPerDisplay.get(id);
267 LOG_ALWAYS_FATAL_IF(!resultOpt);
268 targeter->endFrame(*resultOpt);
269 }
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700270}
271
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500272std::optional<Fps> Scheduler::getFrameRateOverride(uid_t uid) const {
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800273 const bool supportsFrameRateOverrideByContent =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500274 pacesetterSelectorPtr()->supportsAppFrameRateOverrideByContent();
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800275 return mFrameRateOverrideMappings
276 .getFrameRateOverrideForUid(uid, supportsFrameRateOverrideByContent);
Ady Abraham62a0be22020-12-08 16:54:10 -0800277}
278
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400279bool Scheduler::isVsyncValid(TimePoint expectedVsyncTime, uid_t uid) const {
Ady Abraham62a0be22020-12-08 16:54:10 -0800280 const auto frameRate = getFrameRateOverride(uid);
281 if (!frameRate.has_value()) {
282 return true;
283 }
284
Ady Abraham9243bba2023-02-10 15:31:14 -0800285 ATRACE_FORMAT("%s uid: %d frameRate: %s", __func__, uid, to_string(*frameRate).c_str());
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400286 return getVsyncSchedule()->getTracker().isVSyncInPhase(expectedVsyncTime.ns(), *frameRate);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700287}
288
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400289bool Scheduler::isVsyncInPhase(TimePoint expectedVsyncTime, Fps frameRate) const {
290 return getVsyncSchedule()->getTracker().isVSyncInPhase(expectedVsyncTime.ns(), frameRate);
Huihong Luo1768cb02022-10-11 11:10:34 -0700291}
292
Ady Abrahamf2851612023-09-25 17:19:00 -0700293bool Scheduler::throttleVsync(android::TimePoint expectedPresentTime, uid_t uid) {
294 return !isVsyncValid(expectedPresentTime, uid);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500295}
Ady Abraham64c2fc02020-12-29 12:07:50 -0800296
Ady Abrahamf2851612023-09-25 17:19:00 -0700297Period Scheduler::getVsyncPeriod(uid_t uid) {
298 const auto [refreshRate, period] = [this] {
299 std::scoped_lock lock(mDisplayLock);
300 const auto pacesetterOpt = pacesetterDisplayLocked();
301 LOG_ALWAYS_FATAL_IF(!pacesetterOpt);
302 const Display& pacesetter = *pacesetterOpt;
Ying Weiaf854ad2024-03-16 05:24:39 +0000303 const FrameRateMode& frameRateMode = pacesetter.selectorPtr->getActiveMode();
304 const auto refreshRate = frameRateMode.fps;
305 const auto displayVsync = frameRateMode.modePtr->getVsyncRate();
306 const auto numPeriod = RefreshRateSelector::getFrameRateDivisor(displayVsync, refreshRate);
307 return std::make_pair(refreshRate, numPeriod * pacesetter.schedulePtr->period());
Ady Abrahamf2851612023-09-25 17:19:00 -0700308 }();
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500309
Ady Abrahamf2851612023-09-25 17:19:00 -0700310 const Period currentPeriod = period != Period::zero() ? period : refreshRate.getPeriod();
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800311
Ady Abrahamf2851612023-09-25 17:19:00 -0700312 const auto frameRate = getFrameRateOverride(uid);
313 if (!frameRate.has_value()) {
314 return currentPeriod;
315 }
Jorim Jaggic0086af2021-02-12 18:18:11 +0100316
Ady Abrahamf2851612023-09-25 17:19:00 -0700317 const auto divisor = RefreshRateSelector::getFrameRateDivisor(refreshRate, *frameRate);
318 if (divisor <= 1) {
319 return currentPeriod;
320 }
321
322 // TODO(b/299378819): the casting is not needed, but we need a flag as it might change
323 // behaviour.
324 return Period::fromNs(currentPeriod.ns() * divisor);
Jorim Jaggic0086af2021-02-12 18:18:11 +0100325}
ramindaniae645822024-01-11 10:57:29 -0800326void Scheduler::onExpectedPresentTimePosted(TimePoint expectedPresentTime) {
327 const auto frameRateMode = [this] {
328 std::scoped_lock lock(mDisplayLock);
329 const auto pacesetterOpt = pacesetterDisplayLocked();
330 const Display& pacesetter = *pacesetterOpt;
331 return pacesetter.selectorPtr->getActiveMode();
332 }();
333
334 if (frameRateMode.modePtr->getVrrConfig()) {
335 mSchedulerCallback.onExpectedPresentTimePosted(expectedPresentTime, frameRateMode.modePtr,
336 frameRateMode.fps);
337 }
338}
Jorim Jaggic0086af2021-02-12 18:18:11 +0100339
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500340void Scheduler::createEventThread(Cycle cycle, frametimeline::TokenManager* tokenManager,
341 std::chrono::nanoseconds workDuration,
342 std::chrono::nanoseconds readyDuration) {
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500343 auto eventThread =
344 std::make_unique<android::impl::EventThread>(cycle == Cycle::Render ? "app" : "appSf",
345 getVsyncSchedule(), tokenManager, *this,
346 workDuration, readyDuration);
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500347
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500348 if (cycle == Cycle::Render) {
349 mRenderEventThread = std::move(eventThread);
350 mRenderEventConnection = mRenderEventThread->createEventConnection();
351 } else {
352 mLastCompositeEventThread = std::move(eventThread);
353 mLastCompositeEventConnection = mLastCompositeEventThread->createEventConnection();
354 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700355}
356
357sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500358 Cycle cycle, EventRegistrationFlags eventRegistration, const sp<IBinder>& layerHandle) {
359 const auto connection = eventThreadFor(cycle).createEventConnection(eventRegistration);
Ady Abraham822ecbd2023-07-07 16:16:09 -0700360 const auto layerId = static_cast<int32_t>(LayerHandle::getLayerId(layerHandle));
361
362 if (layerId != static_cast<int32_t>(UNASSIGNED_LAYER_ID)) {
363 // TODO(b/290409668): Moving the choreographer attachment to be a transaction that will be
364 // processed on the main thread.
365 mSchedulerCallback.onChoreographerAttached();
366
367 std::scoped_lock lock(mChoreographerLock);
368 const auto [iter, emplaced] =
369 mAttachedChoreographers.emplace(layerId,
370 AttachedChoreographers{Fps(), {connection}});
371 if (!emplaced) {
372 iter->second.connections.emplace(connection);
373 connection->frameRate = iter->second.frameRate;
374 }
375 }
376 return connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700377}
378
Dominik Laskowski091129a2024-02-21 14:26:03 -0500379void Scheduler::dispatchHotplug(PhysicalDisplayId displayId, Hotplug hotplug) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500380 if (hasEventThreads()) {
Dominik Laskowski091129a2024-02-21 14:26:03 -0500381 const bool connected = hotplug == Hotplug::Connected;
382 eventThreadFor(Cycle::Render).onHotplugReceived(displayId, connected);
383 eventThreadFor(Cycle::LastComposite).onHotplugReceived(displayId, connected);
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500384 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700385}
386
Dominik Laskowski091129a2024-02-21 14:26:03 -0500387void Scheduler::dispatchHotplugError(int32_t errorCode) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500388 if (hasEventThreads()) {
Dominik Laskowski091129a2024-02-21 14:26:03 -0500389 eventThreadFor(Cycle::Render).onHotplugConnectionError(errorCode);
390 eventThreadFor(Cycle::LastComposite).onHotplugConnectionError(errorCode);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700391 }
Brian Johnson5dcd75d2023-08-15 09:36:37 -0700392}
393
Dominik Laskowskie99b98c2023-02-02 12:37:23 -0500394void Scheduler::enableSyntheticVsync(bool enable) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500395 eventThreadFor(Cycle::Render).enableSyntheticVsync(enable);
Ana Krulec98b5b242018-08-10 15:03:23 -0700396}
397
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500398void Scheduler::onFrameRateOverridesChanged(Cycle cycle, PhysicalDisplayId displayId) {
Andy Yud6a36202022-01-26 04:08:22 -0800399 const bool supportsFrameRateOverrideByContent =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500400 pacesetterSelectorPtr()->supportsAppFrameRateOverrideByContent();
Andy Yud6a36202022-01-26 04:08:22 -0800401
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800402 std::vector<FrameRateOverride> overrides =
Andy Yud6a36202022-01-26 04:08:22 -0800403 mFrameRateOverrideMappings.getAllFrameRateOverrides(supportsFrameRateOverrideByContent);
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800404
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500405 eventThreadFor(cycle).onFrameRateOverridesChanged(displayId, std::move(overrides));
Ady Abraham62f216c2020-10-13 19:07:23 -0700406}
407
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500408void Scheduler::onHdcpLevelsChanged(Cycle cycle, PhysicalDisplayId displayId,
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700409 int32_t connectedLevel, int32_t maxLevel) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500410 eventThreadFor(cycle).onHdcpLevelsChanged(displayId, connectedLevel, maxLevel);
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700411}
412
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500413void Scheduler::onPrimaryDisplayModeChanged(Cycle cycle, const FrameRateMode& mode) {
Ady Abraham62a0be22020-12-08 16:54:10 -0800414 {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700415 std::lock_guard<std::mutex> lock(mPolicyLock);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100416 // Cache the last reported modes for primary display.
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500417 mPolicy.cachedModeChangedParams = {cycle, mode};
Ady Abraham5cc2e262021-03-25 13:09:17 -0700418
419 // Invalidate content based refresh rate selection so it could be calculated
420 // again for the new refresh rate.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700421 mPolicy.contentRequirements.clear();
Ady Abraham62a0be22020-12-08 16:54:10 -0800422 }
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500423 onNonPrimaryDisplayModeChanged(cycle, mode);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700424}
425
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100426void Scheduler::dispatchCachedReportedMode() {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700427 // Check optional fields first.
Ady Abrahamace3d052022-11-17 16:25:05 -0800428 if (!mPolicy.modeOpt) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100429 ALOGW("No mode ID found, not dispatching cached mode.");
Ana Krulec6ddd2612020-09-24 13:06:33 -0700430 return;
431 }
Dominik Laskowski068173d2021-08-11 17:22:59 -0700432 if (!mPolicy.cachedModeChangedParams) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100433 ALOGW("No mode changed params found, not dispatching cached mode.");
Ana Krulec6ddd2612020-09-24 13:06:33 -0700434 return;
435 }
436
Ady Abrahamd1591702021-07-27 16:27:56 -0700437 // If the mode is not the current mode, this means that a
438 // mode change is in progress. In that case we shouldn't dispatch an event
439 // as it will be dispatched when the current mode changes.
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500440 if (pacesetterSelectorPtr()->getActiveMode() != mPolicy.modeOpt) {
Ady Abrahamd1591702021-07-27 16:27:56 -0700441 return;
442 }
443
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100444 // If there is no change from cached mode, there is no need to dispatch an event
Ady Abrahamace3d052022-11-17 16:25:05 -0800445 if (*mPolicy.modeOpt == mPolicy.cachedModeChangedParams->mode) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700446 return;
447 }
448
Ady Abrahamace3d052022-11-17 16:25:05 -0800449 mPolicy.cachedModeChangedParams->mode = *mPolicy.modeOpt;
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500450 onNonPrimaryDisplayModeChanged(mPolicy.cachedModeChangedParams->cycle,
Dominik Laskowski068173d2021-08-11 17:22:59 -0700451 mPolicy.cachedModeChangedParams->mode);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700452}
453
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500454void Scheduler::onNonPrimaryDisplayModeChanged(Cycle cycle, const FrameRateMode& mode) {
455 if (hasEventThreads()) {
456 eventThreadFor(cycle).onModeChanged(mode);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700457 }
Ady Abraham447052e2019-02-13 16:07:27 -0800458}
459
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500460void Scheduler::dump(Cycle cycle, std::string& result) const {
461 eventThreadFor(cycle).dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700462}
463
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500464void Scheduler::setDuration(Cycle cycle, std::chrono::nanoseconds workDuration,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700465 std::chrono::nanoseconds readyDuration) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500466 if (hasEventThreads()) {
467 eventThreadFor(cycle).setDuration(workDuration, readyDuration);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700468 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700469}
Ana Krulece588e312018-09-18 12:32:24 -0700470
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500471void Scheduler::updatePhaseConfiguration(Fps refreshRate) {
472 mRefreshRateStats->setRefreshRate(refreshRate);
473 mVsyncConfiguration->setRefreshRateFps(refreshRate);
474 setVsyncConfig(mVsyncModulator->setVsyncConfigSet(mVsyncConfiguration->getCurrentConfigs()),
475 refreshRate.getPeriod());
476}
477
478void Scheduler::resetPhaseConfiguration(Fps refreshRate) {
479 // Cancel the pending refresh rate change, if any, before updating the phase configuration.
480 mVsyncModulator->cancelRefreshRateChange();
481
482 mVsyncConfiguration->reset();
483 updatePhaseConfiguration(refreshRate);
484}
485
486void Scheduler::setActiveDisplayPowerModeForRefreshRateStats(hal::PowerMode powerMode) {
487 mRefreshRateStats->setPowerMode(powerMode);
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500488}
489
490void Scheduler::setVsyncConfig(const VsyncConfig& config, Period vsyncPeriod) {
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500491 setDuration(Cycle::Render,
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500492 /* workDuration */ config.appWorkDuration,
493 /* readyDuration */ config.sfWorkDuration);
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500494 setDuration(Cycle::LastComposite,
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500495 /* workDuration */ vsyncPeriod,
496 /* readyDuration */ config.sfWorkDuration);
497 setDuration(config.sfWorkDuration);
498}
499
Leon Scroggins III67388622023-02-06 20:36:20 -0500500void Scheduler::enableHardwareVsync(PhysicalDisplayId id) {
501 auto schedule = getVsyncSchedule(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400502 LOG_ALWAYS_FATAL_IF(!schedule);
Dominik Laskowski66295432023-03-14 12:25:36 -0400503 schedule->enableHardwareVsync();
Ana Krulece588e312018-09-18 12:32:24 -0700504}
505
Leon Scroggins III67388622023-02-06 20:36:20 -0500506void Scheduler::disableHardwareVsync(PhysicalDisplayId id, bool disallow) {
507 auto schedule = getVsyncSchedule(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400508 LOG_ALWAYS_FATAL_IF(!schedule);
Dominik Laskowski66295432023-03-14 12:25:36 -0400509 schedule->disableHardwareVsync(disallow);
Ana Krulece588e312018-09-18 12:32:24 -0700510}
511
Leon Scroggins III67388622023-02-06 20:36:20 -0500512void Scheduler::resyncAllToHardwareVsync(bool allowToEnable) {
Rachel Leea5be3282023-03-08 20:15:54 -0800513 ATRACE_CALL();
Leon Scroggins III67388622023-02-06 20:36:20 -0500514 std::scoped_lock lock(mDisplayLock);
515 ftl::FakeGuard guard(kMainThreadContext);
516
Leon Scroggins III792ea802023-11-27 17:32:51 -0500517 for (const auto& [id, display] : mDisplays) {
518 if (display.powerMode != hal::PowerMode::OFF ||
519 !FlagManager::getInstance().multithreaded_present()) {
520 resyncToHardwareVsyncLocked(id, allowToEnable);
521 }
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500522 }
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500523}
524
Leon Scroggins III67388622023-02-06 20:36:20 -0500525void Scheduler::resyncToHardwareVsyncLocked(PhysicalDisplayId id, bool allowToEnable,
Ady Abrahamc585dba2023-11-15 18:41:35 -0800526 DisplayModePtr modePtr) {
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500527 const auto displayOpt = mDisplays.get(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400528 if (!displayOpt) {
529 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
530 return;
531 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500532 const Display& display = *displayOpt;
533
534 if (display.schedulePtr->isHardwareVsyncAllowed(allowToEnable)) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800535 if (!modePtr) {
536 modePtr = display.selectorPtr->getActiveMode().modePtr.get();
Leon Scroggins III67388622023-02-06 20:36:20 -0500537 }
Ady Abrahamc585dba2023-11-15 18:41:35 -0800538 if (modePtr->getVsyncRate().isValid()) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400539 constexpr bool kForce = false;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800540 display.schedulePtr->onDisplayModeChanged(ftl::as_non_null(modePtr), kForce);
Leon Scroggins III67388622023-02-06 20:36:20 -0500541 }
542 }
543}
544
Dominik Laskowski66295432023-03-14 12:25:36 -0400545void Scheduler::onHardwareVsyncRequest(PhysicalDisplayId id, bool enabled) {
546 static const auto& whence = __func__;
547 ATRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str());
548
549 // On main thread to serialize reads/writes of pending hardware VSYNC state.
550 static_cast<void>(
Leon Scroggins III53ca9562023-12-27 16:32:12 -0500551 schedule([=, this]() FTL_FAKE_GUARD(mDisplayLock) FTL_FAKE_GUARD(kMainThreadContext) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400552 ATRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str());
553
554 if (const auto displayOpt = mDisplays.get(id)) {
555 auto& display = displayOpt->get();
556 display.schedulePtr->setPendingHardwareVsyncState(enabled);
557
558 if (display.powerMode != hal::PowerMode::OFF) {
559 mSchedulerCallback.requestHardwareVsync(id, enabled);
560 }
561 }
562 }));
563}
564
Ady Abrahamee6365b2024-03-06 14:31:45 -0800565void Scheduler::setRenderRate(PhysicalDisplayId id, Fps renderFrameRate, bool applyImmediately) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500566 std::scoped_lock lock(mDisplayLock);
567 ftl::FakeGuard guard(kMainThreadContext);
568
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500569 const auto displayOpt = mDisplays.get(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400570 if (!displayOpt) {
571 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
572 return;
573 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500574 const Display& display = *displayOpt;
575 const auto mode = display.selectorPtr->getActiveMode();
Ady Abrahamace3d052022-11-17 16:25:05 -0800576
577 using fps_approx_ops::operator!=;
578 LOG_ALWAYS_FATAL_IF(renderFrameRate != mode.fps,
Leon Scroggins III67388622023-02-06 20:36:20 -0500579 "Mismatch in render frame rates. Selector: %s, Scheduler: %s, Display: "
580 "%" PRIu64,
581 to_string(mode.fps).c_str(), to_string(renderFrameRate).c_str(), id.value);
Ady Abrahamace3d052022-11-17 16:25:05 -0800582
583 ALOGV("%s %s (%s)", __func__, to_string(mode.fps).c_str(),
ramindania04b8a52023-08-07 18:49:47 -0700584 to_string(mode.modePtr->getVsyncRate()).c_str());
Ady Abrahamace3d052022-11-17 16:25:05 -0800585
Ady Abrahamee6365b2024-03-06 14:31:45 -0800586 display.schedulePtr->getTracker().setRenderRate(renderFrameRate, applyImmediately);
Ady Abrahamace3d052022-11-17 16:25:05 -0800587}
588
ramindani0491e642023-11-16 17:42:14 -0800589Fps Scheduler::getNextFrameInterval(PhysicalDisplayId id,
590 TimePoint currentExpectedPresentTime) const {
591 std::scoped_lock lock(mDisplayLock);
592 ftl::FakeGuard guard(kMainThreadContext);
593
594 const auto displayOpt = mDisplays.get(id);
595 if (!displayOpt) {
596 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
597 return Fps{};
598 }
599 const Display& display = *displayOpt;
Leon Scroggins IIIa0785012024-01-23 16:05:59 -0500600 const Duration threshold =
601 display.selectorPtr->getActiveMode().modePtr->getVsyncRate().getPeriod() / 2;
602 const TimePoint nextVsyncTime =
603 display.schedulePtr->vsyncDeadlineAfter(currentExpectedPresentTime + threshold,
604 currentExpectedPresentTime);
605 const Duration frameInterval = nextVsyncTime - currentExpectedPresentTime;
606 return Fps::fromPeriodNsecs(frameInterval.ns());
ramindani0491e642023-11-16 17:42:14 -0800607}
608
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700609void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700610 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800611
612 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700613 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800614
615 if (now - last > kIgnoreDelay) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500616 resyncAllToHardwareVsync(false /* allowToEnable */);
Ana Krulecc2870422019-01-29 19:00:58 -0800617 }
618}
619
Leon Scroggins III67388622023-02-06 20:36:20 -0500620bool Scheduler::addResyncSample(PhysicalDisplayId id, nsecs_t timestamp,
621 std::optional<nsecs_t> hwcVsyncPeriodIn) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500622 const auto hwcVsyncPeriod = ftl::Optional(hwcVsyncPeriodIn).transform([](nsecs_t nanos) {
623 return Period::fromNs(nanos);
624 });
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400625 auto schedule = getVsyncSchedule(id);
626 if (!schedule) {
627 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
628 return false;
629 }
Dominik Laskowski66295432023-03-14 12:25:36 -0400630 return schedule->addResyncSample(TimePoint::fromNs(timestamp), hwcVsyncPeriod);
Ana Krulece588e312018-09-18 12:32:24 -0700631}
632
Leon Scroggins III67388622023-02-06 20:36:20 -0500633void Scheduler::addPresentFence(PhysicalDisplayId id, std::shared_ptr<FenceTime> fence) {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000634 ATRACE_NAME(ftl::Concat(__func__, ' ', id.value).c_str());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500635 const auto scheduleOpt =
636 (ftl::FakeGuard(mDisplayLock), mDisplays.get(id)).and_then([](const Display& display) {
637 return display.powerMode == hal::PowerMode::OFF
638 ? std::nullopt
639 : std::make_optional(display.schedulePtr);
640 });
641
642 if (!scheduleOpt) return;
643 const auto& schedule = scheduleOpt->get();
644
Yi Kong08d7c812023-12-12 16:40:22 +0900645 const bool needMoreSignals = schedule->getController().addPresentFence(std::move(fence));
646 if (needMoreSignals) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400647 schedule->enableHardwareVsync();
Ana Krulece588e312018-09-18 12:32:24 -0700648 } else {
Dominik Laskowski66295432023-03-14 12:25:36 -0400649 constexpr bool kDisallow = false;
650 schedule->disableHardwareVsync(kDisallow);
Ana Krulece588e312018-09-18 12:32:24 -0700651 }
652}
653
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700654void Scheduler::registerLayer(Layer* layer) {
Marin Shalamanov4be385e2021-04-23 13:25:30 +0200655 // If the content detection feature is off, we still keep the layer history,
656 // since we use it for other features (like Frame Rate API), so layers
657 // still need to be registered.
Andy Labrada096227e2022-06-15 16:58:11 +0000658 mLayerHistory.registerLayer(layer, mFeatures.test(Feature::kContentDetection));
Ady Abraham09bd3922019-04-08 10:44:56 -0700659}
660
Ady Abrahambdda8f02021-04-01 16:06:11 -0700661void Scheduler::deregisterLayer(Layer* layer) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700662 mLayerHistory.deregisterLayer(layer);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700663}
664
Ady Abraham822ecbd2023-07-07 16:16:09 -0700665void Scheduler::onLayerDestroyed(Layer* layer) {
666 std::scoped_lock lock(mChoreographerLock);
667 mAttachedChoreographers.erase(layer->getSequence());
668}
669
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000670void Scheduler::recordLayerHistory(int32_t id, const LayerProps& layerProps, nsecs_t presentTime,
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700671 nsecs_t now, LayerHistory::LayerUpdateType updateType) {
Ady Abrahamf8fdc452024-04-05 16:22:55 +0000672 if (pacesetterSelectorPtr()->canSwitch()) {
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700673 mLayerHistory.record(id, layerProps, presentTime, now, updateType);
Dominik Laskowski49cea512019-11-12 14:13:23 -0800674 }
Ana Krulec3084c052018-11-21 20:27:17 +0100675}
676
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100677void Scheduler::setModeChangePending(bool pending) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700678 mLayerHistory.setModeChangePending(pending);
Ady Abraham32efd542020-05-19 17:49:26 -0700679}
680
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700681void Scheduler::setDefaultFrameRateCompatibility(
682 int32_t id, scheduler::FrameRateCompatibility frameRateCompatibility) {
683 mLayerHistory.setDefaultFrameRateCompatibility(id, frameRateCompatibility,
Andy Labrada096227e2022-06-15 16:58:11 +0000684 mFeatures.test(Feature::kContentDetection));
685}
686
Vishnu Nair41376b62023-11-08 05:08:58 -0800687void Scheduler::setLayerProperties(int32_t id, const android::scheduler::LayerProps& properties) {
688 mLayerHistory.setLayerProperties(id, properties);
689}
690
Ady Abraham822ecbd2023-07-07 16:16:09 -0700691void Scheduler::chooseRefreshRateForContent(
692 const surfaceflinger::frontend::LayerHierarchy* hierarchy,
693 bool updateAttachedChoreographer) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500694 const auto selectorPtr = pacesetterSelectorPtr();
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400695 if (!selectorPtr->canSwitch()) return;
Dominik Laskowski49cea512019-11-12 14:13:23 -0800696
Ady Abraham8a82ba62020-01-17 12:43:17 -0800697 ATRACE_CALL();
698
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400699 LayerHistory::Summary summary = mLayerHistory.summarize(*selectorPtr, systemTime());
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800700 applyPolicy(&Policy::contentRequirements, std::move(summary));
Ady Abraham822ecbd2023-07-07 16:16:09 -0700701
702 if (updateAttachedChoreographer) {
703 LOG_ALWAYS_FATAL_IF(!hierarchy);
704
705 // update the attached choreographers after we selected the render rate.
706 const ftl::Optional<FrameRateMode> modeOpt = [&] {
707 std::scoped_lock lock(mPolicyLock);
708 return mPolicy.modeOpt;
709 }();
710
711 if (modeOpt) {
712 updateAttachedChoreographers(*hierarchy, modeOpt->fps);
713 }
714 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800715}
716
Ana Krulecfb772822018-11-30 10:44:07 +0100717void Scheduler::resetIdleTimer() {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500718 pacesetterSelectorPtr()->resetIdleTimer();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800719}
720
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700721void Scheduler::onTouchHint() {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700722 if (mTouchTimer) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800723 mTouchTimer->reset();
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500724 pacesetterSelectorPtr()->resetKernelIdleTimer();
Dominik Laskowski49cea512019-11-12 14:13:23 -0800725 }
Ady Abraham8532d012019-05-08 14:50:56 -0700726}
727
Leon Scroggins III67388622023-02-06 20:36:20 -0500728void Scheduler::setDisplayPowerMode(PhysicalDisplayId id, hal::PowerMode powerMode) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500729 const bool isPacesetter = [this, id]() REQUIRES(kMainThreadContext) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500730 ftl::FakeGuard guard(mDisplayLock);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500731 return id == mPacesetterDisplayId;
Leon Scroggins III67388622023-02-06 20:36:20 -0500732 }();
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500733 if (isPacesetter) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500734 // TODO (b/255657128): This needs to be handled per display.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700735 std::lock_guard<std::mutex> lock(mPolicyLock);
Rachel Lee6a9731d2022-06-06 17:08:14 -0700736 mPolicy.displayPowerMode = powerMode;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700737 }
Leon Scroggins III67388622023-02-06 20:36:20 -0500738 {
739 std::scoped_lock lock(mDisplayLock);
Dominik Laskowski66295432023-03-14 12:25:36 -0400740
741 const auto displayOpt = mDisplays.get(id);
742 LOG_ALWAYS_FATAL_IF(!displayOpt);
743 auto& display = displayOpt->get();
744
745 display.powerMode = powerMode;
746 display.schedulePtr->getController().setDisplayPowerMode(powerMode);
Leon Scroggins III67388622023-02-06 20:36:20 -0500747 }
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500748 if (!isPacesetter) return;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700749
750 if (mDisplayPowerTimer) {
751 mDisplayPowerTimer->reset();
752 }
753
754 // Display Power event will boost the refresh rate to performance.
755 // Clear Layer History to get fresh FPS detection
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700756 mLayerHistory.clear();
Ady Abraham6fe2c172019-07-12 12:37:57 -0700757}
758
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500759auto Scheduler::getVsyncSchedule(std::optional<PhysicalDisplayId> idOpt) const
760 -> ConstVsyncSchedulePtr {
Leon Scroggins III67388622023-02-06 20:36:20 -0500761 std::scoped_lock lock(mDisplayLock);
762 return getVsyncScheduleLocked(idOpt);
763}
764
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500765auto Scheduler::getVsyncScheduleLocked(std::optional<PhysicalDisplayId> idOpt) const
766 -> ConstVsyncSchedulePtr {
Leon Scroggins III67388622023-02-06 20:36:20 -0500767 ftl::FakeGuard guard(kMainThreadContext);
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500768
Leon Scroggins III67388622023-02-06 20:36:20 -0500769 if (!idOpt) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500770 LOG_ALWAYS_FATAL_IF(!mPacesetterDisplayId, "Missing a pacesetter!");
771 idOpt = mPacesetterDisplayId;
Leon Scroggins III67388622023-02-06 20:36:20 -0500772 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500773
774 const auto displayOpt = mDisplays.get(*idOpt);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400775 if (!displayOpt) {
776 return nullptr;
777 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500778 return displayOpt->get().schedulePtr;
Leon Scroggins III67388622023-02-06 20:36:20 -0500779}
780
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700781void Scheduler::kernelIdleTimerCallback(TimerState state) {
782 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100783
Ady Abraham2139f732019-11-13 18:56:40 -0800784 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
785 // magic number
ramindania04b8a52023-08-07 18:49:47 -0700786 const Fps refreshRate = pacesetterSelectorPtr()->getActiveMode().modePtr->getPeakFps();
Ady Abraham3efa3942021-06-24 19:01:25 -0700787
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700788 constexpr Fps FPS_THRESHOLD_FOR_KERNEL_TIMER = 65_Hz;
789 using namespace fps_approx_ops;
790
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800791 if (state == TimerState::Reset && refreshRate > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700792 // If we're not in performance mode then the kernel timer shouldn't do
793 // anything, as the refresh rate during DPU power collapse will be the
794 // same.
Leon Scroggins III67388622023-02-06 20:36:20 -0500795 resyncAllToHardwareVsync(true /* allowToEnable */);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800796 } else if (state == TimerState::Expired && refreshRate <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700797 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
798 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
Ady Abraham8cb21882020-08-26 18:22:05 -0700799 // need to update the VsyncController model anyway.
Leon Scroggins III67388622023-02-06 20:36:20 -0500800 std::scoped_lock lock(mDisplayLock);
801 ftl::FakeGuard guard(kMainThreadContext);
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500802 for (const auto& [_, display] : mDisplays) {
803 constexpr bool kDisallow = false;
Dominik Laskowski66295432023-03-14 12:25:36 -0400804 display.schedulePtr->disableHardwareVsync(kDisallow);
Leon Scroggins III67388622023-02-06 20:36:20 -0500805 }
Alec Mouridc28b372019-04-18 21:17:13 -0700806 }
Ady Abrahama09852a2020-02-20 14:23:42 -0800807
808 mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
Alec Mouridc28b372019-04-18 21:17:13 -0700809}
810
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700811void Scheduler::idleTimerCallback(TimerState state) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800812 applyPolicy(&Policy::idleTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700813 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100814}
815
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700816void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700817 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700818 // Touch event will boost the refresh rate to performance.
819 // Clear layer history to get fresh FPS detection.
820 // NOTE: Instead of checking all the layers, we should be checking the layer
821 // that is currently on top. b/142507166 will give us this capability.
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800822 if (applyPolicy(&Policy::touch, touch).touch) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700823 mLayerHistory.clear();
Ady Abraham1adbb722020-05-15 11:51:48 -0700824 }
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700825 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700826}
827
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700828void Scheduler::displayPowerTimerCallback(TimerState state) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800829 applyPolicy(&Policy::displayPowerTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700830 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700831}
832
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400833void Scheduler::dump(utils::Dumper& dumper) const {
834 using namespace std::string_view_literals;
Ady Abraham4f960d12021-10-13 16:59:49 -0700835
836 {
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400837 utils::Dumper::Section section(dumper, "Features"sv);
838
839 for (Feature feature : ftl::enum_range<Feature>()) {
840 if (const auto flagOpt = ftl::flag_name(feature)) {
841 dumper.dump(flagOpt->substr(1), mFeatures.test(feature));
842 }
843 }
844 }
845 {
846 utils::Dumper::Section section(dumper, "Policy"sv);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400847 {
848 std::scoped_lock lock(mDisplayLock);
849 ftl::FakeGuard guard(kMainThreadContext);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500850 dumper.dump("pacesetterDisplayId"sv, mPacesetterDisplayId);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400851 }
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400852 dumper.dump("layerHistory"sv, mLayerHistory.dump());
853 dumper.dump("touchTimer"sv, mTouchTimer.transform(&OneShotTimer::interval));
854 dumper.dump("displayPowerTimer"sv, mDisplayPowerTimer.transform(&OneShotTimer::interval));
855 }
856
857 mFrameRateOverrideMappings.dump(dumper);
858 dumper.eol();
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400859
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500860 mVsyncConfiguration->dump(dumper.out());
861 dumper.eol();
862
863 mRefreshRateStats->dump(dumper.out());
864 dumper.eol();
865
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500866 {
867 utils::Dumper::Section section(dumper, "Frame Targeting"sv);
868
869 std::scoped_lock lock(mDisplayLock);
870 ftl::FakeGuard guard(kMainThreadContext);
871
872 for (const auto& [id, display] : mDisplays) {
873 utils::Dumper::Section
874 section(dumper,
875 id == mPacesetterDisplayId
876 ? ftl::Concat("Pacesetter Display ", id.value).c_str()
877 : ftl::Concat("Follower Display ", id.value).c_str());
878
879 display.targeterPtr->dump(dumper);
880 dumper.eol();
881 }
882 }
Ana Krulecb43429d2019-01-09 14:28:51 -0800883}
884
Dominik Laskowski068173d2021-08-11 17:22:59 -0700885void Scheduler::dumpVsync(std::string& out) const {
Leon Scroggins III67388622023-02-06 20:36:20 -0500886 std::scoped_lock lock(mDisplayLock);
887 ftl::FakeGuard guard(kMainThreadContext);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500888 if (mPacesetterDisplayId) {
889 base::StringAppendF(&out, "VsyncSchedule for pacesetter %s:\n",
890 to_string(*mPacesetterDisplayId).c_str());
Leon Scroggins III67388622023-02-06 20:36:20 -0500891 getVsyncScheduleLocked()->dump(out);
892 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500893 for (auto& [id, display] : mDisplays) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500894 if (id == mPacesetterDisplayId) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500895 continue;
896 }
897 base::StringAppendF(&out, "VsyncSchedule for follower %s:\n", to_string(id).c_str());
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500898 display.schedulePtr->dump(out);
Leon Scroggins III67388622023-02-06 20:36:20 -0500899 }
Ady Abraham8735eac2020-08-12 16:35:04 -0700900}
901
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800902bool Scheduler::updateFrameRateOverrides(GlobalSignals consideredSignals, Fps displayRefreshRate) {
Ady Abraham33a386b2023-07-18 15:37:11 -0700903 std::scoped_lock lock(mPolicyLock);
904 return updateFrameRateOverridesLocked(consideredSignals, displayRefreshRate);
905}
906
907bool Scheduler::updateFrameRateOverridesLocked(GlobalSignals consideredSignals,
908 Fps displayRefreshRate) {
Dominik Laskowski596a2562022-10-28 11:26:12 -0400909 if (consideredSignals.idle) return false;
910
911 const auto frameRateOverrides =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500912 pacesetterSelectorPtr()->getFrameRateOverrides(mPolicy.contentRequirements,
913 displayRefreshRate, consideredSignals);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400914
915 // Note that RefreshRateSelector::supportsFrameRateOverrideByContent is checked when querying
916 // the FrameRateOverrideMappings rather than here.
917 return mFrameRateOverrideMappings.updateFrameRateOverridesByContent(frameRateOverrides);
918}
919
Dominik Laskowskic2d89d62024-07-12 14:35:00 -0400920void Scheduler::promotePacesetterDisplay(PhysicalDisplayId pacesetterId) {
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400921 std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule;
922
923 {
924 std::scoped_lock lock(mDisplayLock);
Dominik Laskowskic2d89d62024-07-12 14:35:00 -0400925 pacesetterVsyncSchedule = promotePacesetterDisplayLocked(pacesetterId);
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400926 }
927
Leon Scroggins39d25342023-04-19 17:11:01 +0000928 applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400929}
930
931std::shared_ptr<VsyncSchedule> Scheduler::promotePacesetterDisplayLocked(
Dominik Laskowskic2d89d62024-07-12 14:35:00 -0400932 PhysicalDisplayId pacesetterId) {
933 // TODO: b/241286431 - Choose the pacesetter among mDisplays.
934 mPacesetterDisplayId = pacesetterId;
935 ALOGI("Display %s is the pacesetter", to_string(pacesetterId).c_str());
Dominik Laskowski596a2562022-10-28 11:26:12 -0400936
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500937 std::shared_ptr<VsyncSchedule> newVsyncSchedulePtr;
938 if (const auto pacesetterOpt = pacesetterDisplayLocked()) {
939 const Display& pacesetter = *pacesetterOpt;
940
941 pacesetter.selectorPtr->setIdleTimerCallbacks(
Dominik Laskowski596a2562022-10-28 11:26:12 -0400942 {.platform = {.onReset = [this] { idleTimerCallback(TimerState::Reset); },
943 .onExpired = [this] { idleTimerCallback(TimerState::Expired); }},
944 .kernel = {.onReset = [this] { kernelIdleTimerCallback(TimerState::Reset); },
Ady Abrahame2946322024-07-10 17:45:29 -0700945 .onExpired = [this] { kernelIdleTimerCallback(TimerState::Expired); }},
946 .vrr = {.onReset = [this] { mSchedulerCallback.vrrDisplayIdle(false); },
947 .onExpired = [this] { mSchedulerCallback.vrrDisplayIdle(true); }}});
Dominik Laskowski596a2562022-10-28 11:26:12 -0400948
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500949 pacesetter.selectorPtr->startIdleTimer();
Leon Scroggins III67388622023-02-06 20:36:20 -0500950
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500951 newVsyncSchedulePtr = pacesetter.schedulePtr;
952
Dominik Laskowski66295432023-03-14 12:25:36 -0400953 constexpr bool kForce = true;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800954 newVsyncSchedulePtr->onDisplayModeChanged(pacesetter.selectorPtr->getActiveMode().modePtr,
955 kForce);
Leon Scroggins III67388622023-02-06 20:36:20 -0500956 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500957 return newVsyncSchedulePtr;
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400958}
Leon Scroggins III67388622023-02-06 20:36:20 -0500959
Leon Scroggins39d25342023-04-19 17:11:01 +0000960void Scheduler::applyNewVsyncSchedule(std::shared_ptr<VsyncSchedule> vsyncSchedule) {
961 onNewVsyncSchedule(vsyncSchedule->getDispatch());
Dominik Laskowski4babfc42024-02-16 12:28:40 -0500962
963 if (hasEventThreads()) {
964 eventThreadFor(Cycle::Render).onNewVsyncSchedule(vsyncSchedule);
965 eventThreadFor(Cycle::LastComposite).onNewVsyncSchedule(vsyncSchedule);
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400966 }
Dominik Laskowski596a2562022-10-28 11:26:12 -0400967}
968
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500969void Scheduler::demotePacesetterDisplay() {
Dominik Laskowski596a2562022-10-28 11:26:12 -0400970 // No need to lock for reads on kMainThreadContext.
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500971 if (const auto pacesetterPtr = FTL_FAKE_GUARD(mDisplayLock, pacesetterSelectorPtrLocked())) {
972 pacesetterPtr->stopIdleTimer();
973 pacesetterPtr->clearIdleTimerCallbacks();
Dominik Laskowski596a2562022-10-28 11:26:12 -0400974 }
975
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500976 // Clear state that depends on the pacesetter's RefreshRateSelector.
Dominik Laskowski596a2562022-10-28 11:26:12 -0400977 std::scoped_lock lock(mPolicyLock);
978 mPolicy = {};
Ady Abraham62a0be22020-12-08 16:54:10 -0800979}
980
Ady Abraham822ecbd2023-07-07 16:16:09 -0700981void Scheduler::updateAttachedChoreographersFrameRate(
982 const surfaceflinger::frontend::RequestedLayerState& layer, Fps fps) {
983 std::scoped_lock lock(mChoreographerLock);
984
985 const auto layerId = static_cast<int32_t>(layer.id);
986 const auto choreographers = mAttachedChoreographers.find(layerId);
987 if (choreographers == mAttachedChoreographers.end()) {
988 return;
989 }
990
991 auto& layerChoreographers = choreographers->second;
992
993 layerChoreographers.frameRate = fps;
994 ATRACE_FORMAT_INSTANT("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str());
995 ALOGV("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str());
996
997 auto it = layerChoreographers.connections.begin();
998 while (it != layerChoreographers.connections.end()) {
999 sp<EventThreadConnection> choreographerConnection = it->promote();
1000 if (choreographerConnection) {
1001 choreographerConnection->frameRate = fps;
1002 it++;
1003 } else {
1004 it = choreographers->second.connections.erase(it);
1005 }
1006 }
1007
1008 if (layerChoreographers.connections.empty()) {
1009 mAttachedChoreographers.erase(choreographers);
1010 }
1011}
1012
1013int Scheduler::updateAttachedChoreographersInternal(
1014 const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate,
1015 int parentDivisor) {
1016 const char* name = layerHierarchy.getLayer() ? layerHierarchy.getLayer()->name.c_str() : "Root";
1017
1018 int divisor = 0;
1019 if (layerHierarchy.getLayer()) {
1020 const auto frameRateCompatibility = layerHierarchy.getLayer()->frameRateCompatibility;
1021 const auto frameRate = Fps::fromValue(layerHierarchy.getLayer()->frameRate);
1022 ALOGV("%s: %s frameRate %s parentDivisor=%d", __func__, name, to_string(frameRate).c_str(),
1023 parentDivisor);
1024
1025 if (frameRate.isValid()) {
1026 if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE ||
1027 frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_EXACT) {
1028 // Since this layer wants an exact match, we would only set a frame rate if the
1029 // desired rate is a divisor of the display refresh rate.
1030 divisor = RefreshRateSelector::getFrameRateDivisor(displayRefreshRate, frameRate);
1031 } else if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT) {
1032 // find the closest frame rate divisor for the desired frame rate.
1033 divisor = static_cast<int>(
1034 std::round(displayRefreshRate.getValue() / frameRate.getValue()));
1035 }
1036 }
1037 }
1038
1039 // We start by traversing the children, updating their choreographers, and getting back the
1040 // aggregated frame rate.
1041 int childrenDivisor = 0;
1042 for (const auto& [child, _] : layerHierarchy.mChildren) {
1043 LOG_ALWAYS_FATAL_IF(child == nullptr || child->getLayer() == nullptr);
1044
1045 ALOGV("%s: %s traversing child %s", __func__, name, child->getLayer()->name.c_str());
1046
1047 const int childDivisor =
1048 updateAttachedChoreographersInternal(*child, displayRefreshRate, divisor);
1049 childrenDivisor = childrenDivisor > 0 ? childrenDivisor : childDivisor;
1050 if (childDivisor > 0) {
1051 childrenDivisor = std::gcd(childrenDivisor, childDivisor);
1052 }
1053 ALOGV("%s: %s childrenDivisor=%d", __func__, name, childrenDivisor);
1054 }
1055
1056 ALOGV("%s: %s divisor=%d", __func__, name, divisor);
1057
1058 // If there is no explicit vote for this layer. Use the children's vote if exists
1059 divisor = (divisor == 0) ? childrenDivisor : divisor;
1060 ALOGV("%s: %s divisor=%d with children", __func__, name, divisor);
1061
1062 // If there is no explicit vote for this layer or its children, Use the parent vote if exists
1063 divisor = (divisor == 0) ? parentDivisor : divisor;
1064 ALOGV("%s: %s divisor=%d with parent", __func__, name, divisor);
1065
1066 if (layerHierarchy.getLayer()) {
1067 Fps fps = divisor > 1 ? displayRefreshRate / (unsigned int)divisor : Fps();
1068 updateAttachedChoreographersFrameRate(*layerHierarchy.getLayer(), fps);
1069 }
1070
1071 return divisor;
1072}
1073
1074void Scheduler::updateAttachedChoreographers(
1075 const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate) {
1076 ATRACE_CALL();
1077 updateAttachedChoreographersInternal(layerHierarchy, displayRefreshRate, 0);
1078}
1079
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001080template <typename S, typename T>
1081auto Scheduler::applyPolicy(S Policy::*statePtr, T&& newState) -> GlobalSignals {
Ady Abraham73c3df52023-01-12 18:09:31 -08001082 ATRACE_CALL();
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001083 std::vector<display::DisplayModeRequest> modeRequests;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001084 GlobalSignals consideredSignals;
1085
Ady Abraham62a0be22020-12-08 16:54:10 -08001086 bool refreshRateChanged = false;
1087 bool frameRateOverridesChanged;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001088
Ady Abraham8532d012019-05-08 14:50:56 -07001089 {
Dominik Laskowski596a2562022-10-28 11:26:12 -04001090 std::scoped_lock lock(mPolicyLock);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001091
1092 auto& currentState = mPolicy.*statePtr;
1093 if (currentState == newState) return {};
1094 currentState = std::forward<T>(newState);
1095
Dominik Laskowski596a2562022-10-28 11:26:12 -04001096 DisplayModeChoiceMap modeChoices;
Ady Abrahamace3d052022-11-17 16:25:05 -08001097 ftl::Optional<FrameRateMode> modeOpt;
Dominik Laskowski596a2562022-10-28 11:26:12 -04001098 {
1099 std::scoped_lock lock(mDisplayLock);
1100 ftl::FakeGuard guard(kMainThreadContext);
1101
1102 modeChoices = chooseDisplayModes();
1103
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001104 // TODO(b/240743786): The pacesetter display's mode must change for any
1105 // DisplayModeRequest to go through. Fix this by tracking per-display Scheduler::Policy
1106 // and timers.
Ady Abrahamace3d052022-11-17 16:25:05 -08001107 std::tie(modeOpt, consideredSignals) =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001108 modeChoices.get(*mPacesetterDisplayId)
Dominik Laskowski596a2562022-10-28 11:26:12 -04001109 .transform([](const DisplayModeChoice& choice) {
Ady Abrahamace3d052022-11-17 16:25:05 -08001110 return std::make_pair(choice.mode, choice.consideredSignals);
Dominik Laskowski596a2562022-10-28 11:26:12 -04001111 })
1112 .value();
1113 }
ramindani69b58e82022-09-26 16:48:36 -07001114
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001115 modeRequests.reserve(modeChoices.size());
1116 for (auto& [id, choice] : modeChoices) {
1117 modeRequests.emplace_back(
Ady Abrahamace3d052022-11-17 16:25:05 -08001118 display::DisplayModeRequest{.mode = std::move(choice.mode),
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001119 .emitEvent = !choice.consideredSignals.idle});
1120 }
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001121
Ady Abraham33a386b2023-07-18 15:37:11 -07001122 frameRateOverridesChanged = updateFrameRateOverridesLocked(consideredSignals, modeOpt->fps);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001123
Ady Abrahamace3d052022-11-17 16:25:05 -08001124 if (mPolicy.modeOpt != modeOpt) {
1125 mPolicy.modeOpt = modeOpt;
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001126 refreshRateChanged = true;
1127 } else {
Marin Shalamanova7fe3042021-01-29 21:02:08 +01001128 // We don't need to change the display mode, but we might need to send an event
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001129 // about a mode change, since it was suppressed if previously considered idle.
Ady Abrahamdfd62162020-06-10 16:11:56 -07001130 if (!consideredSignals.idle) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +01001131 dispatchCachedReportedMode();
Ady Abrahamdfd62162020-06-10 16:11:56 -07001132 }
Ady Abraham8532d012019-05-08 14:50:56 -07001133 }
Ady Abraham8532d012019-05-08 14:50:56 -07001134 }
Ady Abraham62a0be22020-12-08 16:54:10 -08001135 if (refreshRateChanged) {
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001136 mSchedulerCallback.requestDisplayModes(std::move(modeRequests));
Ady Abraham62a0be22020-12-08 16:54:10 -08001137 }
1138 if (frameRateOverridesChanged) {
1139 mSchedulerCallback.triggerOnFrameRateOverridesChanged();
1140 }
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001141 return consideredSignals;
Ady Abraham8532d012019-05-08 14:50:56 -07001142}
1143
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001144auto Scheduler::chooseDisplayModes() const -> DisplayModeChoiceMap {
Ady Abraham4ccdcb42020-02-11 17:34:34 -08001145 ATRACE_CALL();
Ady Abraham09bd3922019-04-08 10:44:56 -07001146
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001147 DisplayModeChoiceMap modeChoices;
Dominik Laskowski01602522022-10-07 19:02:28 -04001148 const auto globalSignals = makeGlobalSignals();
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001149
1150 const Fps pacesetterFps = [&]() REQUIRES(mPolicyLock, mDisplayLock, kMainThreadContext) {
1151 auto rankedFrameRates =
1152 pacesetterSelectorPtrLocked()->getRankedFrameRates(mPolicy.contentRequirements,
1153 globalSignals);
1154
1155 const Fps pacesetterFps = rankedFrameRates.ranking.front().frameRateMode.fps;
1156
1157 modeChoices.try_emplace(*mPacesetterDisplayId,
1158 DisplayModeChoice::from(std::move(rankedFrameRates)));
1159 return pacesetterFps;
1160 }();
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001161
Dominik Laskowski086507b2024-05-16 15:33:16 -04001162 // Choose a mode for powered-on follower displays.
Dominik Laskowskic404cb42023-03-03 19:57:53 -05001163 for (const auto& [id, display] : mDisplays) {
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001164 if (id == *mPacesetterDisplayId) continue;
Dominik Laskowski086507b2024-05-16 15:33:16 -04001165 if (display.powerMode != hal::PowerMode::ON) continue;
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001166
Ady Abrahamace3d052022-11-17 16:25:05 -08001167 auto rankedFrameRates =
Dominik Laskowski9e88d622024-03-06 17:42:39 -05001168 display.selectorPtr->getRankedFrameRates(mPolicy.contentRequirements, globalSignals,
1169 pacesetterFps);
1170
1171 modeChoices.try_emplace(id, DisplayModeChoice::from(std::move(rankedFrameRates)));
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001172 }
ramindani69b58e82022-09-26 16:48:36 -07001173
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001174 return modeChoices;
ramindani69b58e82022-09-26 16:48:36 -07001175}
1176
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001177GlobalSignals Scheduler::makeGlobalSignals() const {
ramindani38c84982022-08-29 18:02:57 +00001178 const bool powerOnImminent = mDisplayPowerTimer &&
1179 (mPolicy.displayPowerMode != hal::PowerMode::ON ||
1180 mPolicy.displayPowerTimer == TimerState::Reset);
Ady Abraham6fe2c172019-07-12 12:37:57 -07001181
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001182 return {.touch = mTouchTimer && mPolicy.touch == TouchState::Active,
1183 .idle = mPolicy.idleTimer == TimerState::Expired,
1184 .powerOnImminent = powerOnImminent};
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001185}
1186
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001187FrameRateMode Scheduler::getPreferredDisplayMode() {
Dominik Laskowski068173d2021-08-11 17:22:59 -07001188 std::lock_guard<std::mutex> lock(mPolicyLock);
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001189 const auto frameRateMode =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001190 pacesetterSelectorPtr()
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001191 ->getRankedFrameRates(mPolicy.contentRequirements, makeGlobalSignals())
1192 .ranking.front()
1193 .frameRateMode;
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001194
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001195 // Make sure the stored mode is up to date.
1196 mPolicy.modeOpt = frameRateMode;
1197
1198 return frameRateMode;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -07001199}
1200
Peiyong Line9d809e2020-04-14 13:10:48 -07001201void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
Ady Abraham3a77a7b2019-12-02 18:46:59 -08001202 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
1203 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
1204
1205 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
1206 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
1207 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
1208 }
1209}
1210
Leon Scroggins III5b581492023-10-31 14:29:41 -04001211bool Scheduler::onCompositionPresented(nsecs_t presentTime) {
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001212 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
1213 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
1214 if (presentTime < mLastVsyncPeriodChangeTimeline->refreshTimeNanos) {
1215 // We need to composite again as refreshTimeNanos is still in the future.
1216 return true;
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001217 }
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001218
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001219 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001220 }
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001221 return false;
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001222}
1223
Ady Abraham7825c682021-05-17 15:12:14 -07001224void Scheduler::onActiveDisplayAreaChanged(uint32_t displayArea) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -07001225 mLayerHistory.setDisplayArea(displayArea);
Ady Abraham8a82ba62020-01-17 12:43:17 -08001226}
1227
Andy Yu8c2703d2023-11-03 11:22:46 -07001228void Scheduler::setGameModeFrameRateForUid(FrameRateOverride frameRateOverride) {
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001229 if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) {
1230 return;
1231 }
1232
Andy Yu8c2703d2023-11-03 11:22:46 -07001233 if (FlagManager::getInstance().game_default_frame_rate()) {
1234 // update the frame rate override mapping in LayerHistory
1235 mLayerHistory.updateGameModeFrameRateOverride(frameRateOverride);
1236 } else {
1237 mFrameRateOverrideMappings.setGameModeRefreshRateForUid(frameRateOverride);
1238 }
1239}
1240
1241void Scheduler::setGameDefaultFrameRateForUid(FrameRateOverride frameRateOverride) {
1242 if (!FlagManager::getInstance().game_default_frame_rate() ||
1243 (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f)) {
1244 return;
1245 }
1246
1247 // update the frame rate override mapping in LayerHistory
1248 mLayerHistory.updateGameDefaultFrameRateOverride(frameRateOverride);
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001249}
1250
Ady Abraham62a0be22020-12-08 16:54:10 -08001251void Scheduler::setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride) {
1252 if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) {
1253 return;
1254 }
1255
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001256 mFrameRateOverrideMappings.setPreferredRefreshRateForUid(frameRateOverride);
Ady Abraham62a0be22020-12-08 16:54:10 -08001257}
1258
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001259void Scheduler::updateSmallAreaDetection(
Tony Huangf3621102023-09-04 17:14:22 +08001260 std::vector<std::pair<int32_t, float>>& uidThresholdMappings) {
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001261 mSmallAreaDetectionAllowMappings.update(uidThresholdMappings);
1262}
1263
Tony Huangf3621102023-09-04 17:14:22 +08001264void Scheduler::setSmallAreaDetectionThreshold(int32_t appId, float threshold) {
Jerry Chang36678002023-11-29 16:56:17 +00001265 mSmallAreaDetectionAllowMappings.setThresholdForAppId(appId, threshold);
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001266}
1267
Tony Huangf3621102023-09-04 17:14:22 +08001268bool Scheduler::isSmallDirtyArea(int32_t appId, uint32_t dirtyArea) {
1269 std::optional<float> oThreshold = mSmallAreaDetectionAllowMappings.getThresholdForAppId(appId);
1270 if (oThreshold) {
1271 return mLayerHistory.isSmallDirtyArea(dirtyArea, oThreshold.value());
1272 }
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001273 return false;
1274}
1275
Dominik Laskowski068173d2021-08-11 17:22:59 -07001276} // namespace android::scheduler