blob: 5f772acc7461962f39446a9371be53c2f08eeee7 [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 Laskowski98041832019-08-01 18:35:59 -070063#define RETURN_IF_INVALID_HANDLE(handle, ...) \
64 do { \
65 if (mConnections.count(handle) == 0) { \
66 ALOGE("Invalid connection handle %" PRIuPTR, handle.id); \
67 return __VA_ARGS__; \
68 } \
69 } while (false)
70
Dominik Laskowski068173d2021-08-11 17:22:59 -070071namespace android::scheduler {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070072
Dominik Laskowski1c99a002023-01-20 17:10:36 -050073Scheduler::Scheduler(ICompositor& compositor, ISchedulerCallback& callback, FeatureFlags features,
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050074 surfaceflinger::Factory& factory, Fps activeRefreshRate, TimeStats& timeStats,
75 IVsyncTrackerCallback& vsyncTrackerCallback)
76 : android::impl::MessageQueue(compositor),
Dominik Laskowski1c99a002023-01-20 17:10:36 -050077 mFeatures(features),
Leon Scroggins III823d4ca2023-12-12 16:57:34 -050078 mVsyncConfiguration(factory.createVsyncConfiguration(activeRefreshRate)),
79 mVsyncModulator(sp<VsyncModulator>::make(mVsyncConfiguration->getCurrentConfigs())),
80 mRefreshRateStats(std::make_unique<RefreshRateStats>(timeStats, activeRefreshRate,
81 hal::PowerMode::OFF)),
ramindanid4354a92023-10-02 15:11:09 -070082 mSchedulerCallback(callback),
83 mVsyncTrackerCallback(vsyncTrackerCallback) {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070084
Dominik Laskowski83bd7712022-01-07 14:30:53 -080085Scheduler::~Scheduler() {
Ady Abraham011f8ba2022-11-22 15:09:07 -080086 // MessageQueue depends on VsyncSchedule, so first destroy it.
87 // Otherwise, MessageQueue will get destroyed after Scheduler's dtor,
88 // which will cause a use-after-free issue.
89 Impl::destroyVsync();
90
Dominik Laskowski83bd7712022-01-07 14:30:53 -080091 // Stop timers and wait for their threads to exit.
92 mDisplayPowerTimer.reset();
93 mTouchTimer.reset();
94
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040095 // Stop idle timer and clear callbacks, as the RefreshRateSelector may outlive the Scheduler.
Leon Scroggins III1af0fb62023-03-02 14:21:44 -050096 demotePacesetterDisplay();
Dominik Laskowski83bd7712022-01-07 14:30:53 -080097}
98
Dominik Laskowski9c93d602021-10-07 19:38:26 -070099void Scheduler::startTimers() {
Dominik Laskowski98041832019-08-01 18:35:59 -0700100 using namespace sysprop;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700101 using namespace std::string_literals;
Ady Abraham8532d012019-05-08 14:50:56 -0700102
Ady Abrahamc496b432023-12-01 21:35:05 +0000103 const int32_t defaultTouchTimerValue =
Ady Abraham3f84c502023-11-30 18:18:06 -0800104 FlagManager::getInstance().enable_fro_dependent_features() &&
105 sysprop::enable_frame_rate_override(true)
106 ? 200
107 : 0;
Ady Abrahamc496b432023-12-01 21:35:05 +0000108 if (const int32_t millis = set_touch_timer_ms(defaultTouchTimerValue); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -0700109 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -0700110 mTouchTimer.emplace(
Ady Abrahamdb3dfee2020-11-17 17:07:12 -0800111 "TouchTimer", std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700112 [this] { touchTimerCallback(TimerState::Reset); },
113 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -0700114 mTouchTimer->start();
115 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700116
Dominik Laskowski98041832019-08-01 18:35:59 -0700117 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
118 mDisplayPowerTimer.emplace(
Ady Abrahamdb3dfee2020-11-17 17:07:12 -0800119 "DisplayPowerTimer", std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700120 [this] { displayPowerTimerCallback(TimerState::Reset); },
121 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -0700122 mDisplayPowerTimer->start();
123 }
Ana Krulece588e312018-09-18 12:32:24 -0700124}
125
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500126void Scheduler::setPacesetterDisplay(std::optional<PhysicalDisplayId> pacesetterIdOpt) {
127 demotePacesetterDisplay();
Dominik Laskowski59db9562022-10-27 16:18:53 -0400128
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500129 promotePacesetterDisplay(pacesetterIdOpt);
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800130}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700131
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400132void Scheduler::registerDisplay(PhysicalDisplayId displayId, RefreshRateSelectorPtr selectorPtr) {
ramindanid4354a92023-10-02 15:11:09 -0700133 auto schedulePtr = std::make_shared<VsyncSchedule>(
Ady Abrahamc585dba2023-11-15 18:41:35 -0800134 selectorPtr->getActiveMode().modePtr, mFeatures,
ramindanid4354a92023-10-02 15:11:09 -0700135 [this](PhysicalDisplayId id, bool enable) { onHardwareVsyncRequest(id, enable); },
136 mVsyncTrackerCallback);
Dominik Laskowski66295432023-03-14 12:25:36 -0400137
138 registerDisplayInternal(displayId, std::move(selectorPtr), std::move(schedulePtr));
Leon Scroggins III67388622023-02-06 20:36:20 -0500139}
140
141void Scheduler::registerDisplayInternal(PhysicalDisplayId displayId,
142 RefreshRateSelectorPtr selectorPtr,
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500143 VsyncSchedulePtr schedulePtr) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500144 demotePacesetterDisplay();
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400145
Dominik Laskowski008bec02023-03-14 12:04:58 -0400146 auto [pacesetterVsyncSchedule, isNew] = [&]() FTL_FAKE_GUARD(kMainThreadContext) {
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400147 std::scoped_lock lock(mDisplayLock);
Dominik Laskowski008bec02023-03-14 12:04:58 -0400148 const bool isNew = mDisplays
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500149 .emplace_or_replace(displayId, displayId, std::move(selectorPtr),
150 std::move(schedulePtr), mFeatures)
Dominik Laskowski008bec02023-03-14 12:04:58 -0400151 .second;
Dominik Laskowski596a2562022-10-28 11:26:12 -0400152
Dominik Laskowski008bec02023-03-14 12:04:58 -0400153 return std::make_pair(promotePacesetterDisplayLocked(), isNew);
154 }();
155
Leon Scroggins39d25342023-04-19 17:11:01 +0000156 applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
Dominik Laskowski008bec02023-03-14 12:04:58 -0400157
158 // Disable hardware VSYNC if the registration is new, as opposed to a renewal.
159 if (isNew) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400160 onHardwareVsyncRequest(displayId, false);
Dominik Laskowski008bec02023-03-14 12:04:58 -0400161 }
Dominik Laskowski01602522022-10-07 19:02:28 -0400162}
163
164void Scheduler::unregisterDisplay(PhysicalDisplayId displayId) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500165 demotePacesetterDisplay();
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400166
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400167 std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule;
168 {
169 std::scoped_lock lock(mDisplayLock);
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500170 mDisplays.erase(displayId);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400171
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400172 // Do not allow removing the final display. Code in the scheduler expects
173 // there to be at least one display. (This may be relaxed in the future with
174 // headless virtual display.)
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500175 LOG_ALWAYS_FATAL_IF(mDisplays.empty(), "Cannot unregister all displays!");
Leon Scroggins IIIda21f422023-01-30 20:17:56 -0500176
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400177 pacesetterVsyncSchedule = promotePacesetterDisplayLocked();
178 }
Leon Scroggins39d25342023-04-19 17:11:01 +0000179 applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
Dominik Laskowski01602522022-10-07 19:02:28 -0400180}
181
Dominik Laskowski756b7892021-08-04 12:53:59 -0700182void Scheduler::run() {
183 while (true) {
184 waitMessage();
185 }
186}
187
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700188void Scheduler::onFrameSignal(ICompositor& compositor, VsyncId vsyncId,
189 TimePoint expectedVsyncTime) {
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500190 const FrameTargeter::BeginFrameArgs beginFrameArgs =
191 {.frameBeginTime = SchedulerClock::now(),
192 .vsyncId = vsyncId,
193 // TODO(b/255601557): Calculate per display.
194 .expectedVsyncTime = expectedVsyncTime,
195 .sfWorkDuration = mVsyncModulator->getVsyncConfig().sfWorkDuration};
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700196
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500197 ftl::NonNull<const Display*> pacesetterPtr = pacesetterPtrLocked();
198 pacesetterPtr->targeterPtr->beginFrame(beginFrameArgs, *pacesetterPtr->schedulePtr);
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500199
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500200 {
201 FrameTargets targets;
202 targets.try_emplace(pacesetterPtr->displayId, &pacesetterPtr->targeterPtr->target());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500203
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500204 for (const auto& [id, display] : mDisplays) {
205 if (id == pacesetterPtr->displayId) continue;
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400206
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500207 FrameTargeter& targeter = *display.targeterPtr;
208 targeter.beginFrame(beginFrameArgs, *display.schedulePtr);
209 targets.try_emplace(id, &targeter.target());
210 }
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400211
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500212 if (!compositor.commit(pacesetterPtr->displayId, targets)) return;
Dominik Laskowskifdac5652023-06-29 12:01:13 -0400213 }
214
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500215 // The pacesetter may have changed or been registered anew during commit.
216 pacesetterPtr = pacesetterPtrLocked();
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500217
218 // TODO(b/256196556): Choose the frontrunner display.
219 FrameTargeters targeters;
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500220 targeters.try_emplace(pacesetterPtr->displayId, pacesetterPtr->targeterPtr.get());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500221
222 for (auto& [id, display] : mDisplays) {
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500223 if (id == pacesetterPtr->displayId) continue;
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500224
225 FrameTargeter& targeter = *display.targeterPtr;
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500226 targeters.try_emplace(id, &targeter);
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700227 }
228
Ady Abrahamd6d80162023-10-23 12:57:41 -0700229 if (FlagManager::getInstance().vrr_config() &&
Alec Mouri1c7938e2023-09-22 04:17:23 +0000230 CC_UNLIKELY(mPacesetterFrameDurationFractionToSkip > 0.f)) {
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500231 const auto period = pacesetterPtr->targeterPtr->target().expectedFrameDuration();
Alec Mouri1c7938e2023-09-22 04:17:23 +0000232 const auto skipDuration = Duration::fromNs(
233 static_cast<nsecs_t>(period.ns() * mPacesetterFrameDurationFractionToSkip));
234 ATRACE_FORMAT("Injecting jank for %f%% of the frame (%" PRId64 " ns)",
235 mPacesetterFrameDurationFractionToSkip * 100, skipDuration.ns());
236 std::this_thread::sleep_for(skipDuration);
237 mPacesetterFrameDurationFractionToSkip = 0.f;
238 }
239
Ady Abrahame9883032023-11-20 17:54:54 -0800240 if (FlagManager::getInstance().vrr_config()) {
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500241 const auto minFramePeriod = pacesetterPtr->schedulePtr->minFramePeriod();
Ady Abrahame9883032023-11-20 17:54:54 -0800242 const auto presentFenceForPastVsync =
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500243 pacesetterPtr->targeterPtr->target().presentFenceForPastVsync(minFramePeriod);
Ady Abrahame9883032023-11-20 17:54:54 -0800244 const auto lastConfirmedPresentTime = presentFenceForPastVsync->getSignalTime();
245 if (lastConfirmedPresentTime != Fence::SIGNAL_TIME_PENDING &&
246 lastConfirmedPresentTime != Fence::SIGNAL_TIME_INVALID) {
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500247 pacesetterPtr->schedulePtr->getTracker()
Ady Abrahame9883032023-11-20 17:54:54 -0800248 .onFrameBegin(expectedVsyncTime, TimePoint::fromNs(lastConfirmedPresentTime));
249 }
250 }
251
Dominik Laskowskifb4b7372023-11-22 09:56:54 -0500252 const auto resultsPerDisplay = compositor.composite(pacesetterPtr->displayId, targeters);
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700253 compositor.sample();
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400254
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500255 for (const auto& [id, targeter] : targeters) {
256 const auto resultOpt = resultsPerDisplay.get(id);
257 LOG_ALWAYS_FATAL_IF(!resultOpt);
258 targeter->endFrame(*resultOpt);
259 }
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700260}
261
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500262std::optional<Fps> Scheduler::getFrameRateOverride(uid_t uid) const {
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800263 const bool supportsFrameRateOverrideByContent =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500264 pacesetterSelectorPtr()->supportsAppFrameRateOverrideByContent();
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800265 return mFrameRateOverrideMappings
266 .getFrameRateOverrideForUid(uid, supportsFrameRateOverrideByContent);
Ady Abraham62a0be22020-12-08 16:54:10 -0800267}
268
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400269bool Scheduler::isVsyncValid(TimePoint expectedVsyncTime, uid_t uid) const {
Ady Abraham62a0be22020-12-08 16:54:10 -0800270 const auto frameRate = getFrameRateOverride(uid);
271 if (!frameRate.has_value()) {
272 return true;
273 }
274
Ady Abraham9243bba2023-02-10 15:31:14 -0800275 ATRACE_FORMAT("%s uid: %d frameRate: %s", __func__, uid, to_string(*frameRate).c_str());
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400276 return getVsyncSchedule()->getTracker().isVSyncInPhase(expectedVsyncTime.ns(), *frameRate);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700277}
278
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400279bool Scheduler::isVsyncInPhase(TimePoint expectedVsyncTime, Fps frameRate) const {
280 return getVsyncSchedule()->getTracker().isVSyncInPhase(expectedVsyncTime.ns(), frameRate);
Huihong Luo1768cb02022-10-11 11:10:34 -0700281}
282
Ady Abrahamf2851612023-09-25 17:19:00 -0700283bool Scheduler::throttleVsync(android::TimePoint expectedPresentTime, uid_t uid) {
284 return !isVsyncValid(expectedPresentTime, uid);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500285}
Ady Abraham64c2fc02020-12-29 12:07:50 -0800286
Ady Abrahamf2851612023-09-25 17:19:00 -0700287Period Scheduler::getVsyncPeriod(uid_t uid) {
288 const auto [refreshRate, period] = [this] {
289 std::scoped_lock lock(mDisplayLock);
290 const auto pacesetterOpt = pacesetterDisplayLocked();
291 LOG_ALWAYS_FATAL_IF(!pacesetterOpt);
292 const Display& pacesetter = *pacesetterOpt;
293 return std::make_pair(pacesetter.selectorPtr->getActiveMode().fps,
294 pacesetter.schedulePtr->period());
295 }();
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500296
Ady Abrahamf2851612023-09-25 17:19:00 -0700297 const Period currentPeriod = period != Period::zero() ? period : refreshRate.getPeriod();
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800298
Ady Abrahamf2851612023-09-25 17:19:00 -0700299 const auto frameRate = getFrameRateOverride(uid);
300 if (!frameRate.has_value()) {
301 return currentPeriod;
302 }
Jorim Jaggic0086af2021-02-12 18:18:11 +0100303
Ady Abrahamf2851612023-09-25 17:19:00 -0700304 const auto divisor = RefreshRateSelector::getFrameRateDivisor(refreshRate, *frameRate);
305 if (divisor <= 1) {
306 return currentPeriod;
307 }
308
309 // TODO(b/299378819): the casting is not needed, but we need a flag as it might change
310 // behaviour.
311 return Period::fromNs(currentPeriod.ns() * divisor);
Jorim Jaggic0086af2021-02-12 18:18:11 +0100312}
313
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500314ConnectionHandle Scheduler::createEventThread(Cycle cycle,
315 frametimeline::TokenManager* tokenManager,
316 std::chrono::nanoseconds workDuration,
317 std::chrono::nanoseconds readyDuration) {
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500318 auto eventThread =
319 std::make_unique<android::impl::EventThread>(cycle == Cycle::Render ? "app" : "appSf",
320 getVsyncSchedule(), tokenManager, *this,
321 workDuration, readyDuration);
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500322
323 auto& handle = cycle == Cycle::Render ? mAppConnectionHandle : mSfConnectionHandle;
324 handle = createConnection(std::move(eventThread));
325 return handle;
Dominik Laskowski98041832019-08-01 18:35:59 -0700326}
Ana Krulec98b5b242018-08-10 15:03:23 -0700327
Dominik Laskowski068173d2021-08-11 17:22:59 -0700328ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700329 const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
330 ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800331
Ady Abrahamf2851612023-09-25 17:19:00 -0700332 auto connection = eventThread->createEventConnection();
Dominik Laskowski98041832019-08-01 18:35:59 -0700333
Ana Krulec6ddd2612020-09-24 13:06:33 -0700334 std::lock_guard<std::mutex> lock(mConnectionsLock);
Dominik Laskowski98041832019-08-01 18:35:59 -0700335 mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
336 return handle;
Ana Krulec98b5b242018-08-10 15:03:23 -0700337}
338
339sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Rachel Lee2248f522023-01-27 16:45:23 -0800340 ConnectionHandle handle, EventRegistrationFlags eventRegistration,
341 const sp<IBinder>& layerHandle) {
Ady Abraham822ecbd2023-07-07 16:16:09 -0700342 const auto connection = [&]() -> sp<EventThreadConnection> {
343 std::scoped_lock lock(mConnectionsLock);
344 RETURN_IF_INVALID_HANDLE(handle, nullptr);
345
Ady Abrahamf2851612023-09-25 17:19:00 -0700346 return mConnections[handle].thread->createEventConnection(eventRegistration);
Ady Abraham822ecbd2023-07-07 16:16:09 -0700347 }();
348 const auto layerId = static_cast<int32_t>(LayerHandle::getLayerId(layerHandle));
349
350 if (layerId != static_cast<int32_t>(UNASSIGNED_LAYER_ID)) {
351 // TODO(b/290409668): Moving the choreographer attachment to be a transaction that will be
352 // processed on the main thread.
353 mSchedulerCallback.onChoreographerAttached();
354
355 std::scoped_lock lock(mChoreographerLock);
356 const auto [iter, emplaced] =
357 mAttachedChoreographers.emplace(layerId,
358 AttachedChoreographers{Fps(), {connection}});
359 if (!emplaced) {
360 iter->second.connections.emplace(connection);
361 connection->frameRate = iter->second.frameRate;
362 }
363 }
364 return connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700365}
366
Dominik Laskowski98041832019-08-01 18:35:59 -0700367sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700368 std::lock_guard<std::mutex> lock(mConnectionsLock);
Dominik Laskowski98041832019-08-01 18:35:59 -0700369 RETURN_IF_INVALID_HANDLE(handle, nullptr);
370 return mConnections[handle].connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700371}
372
Dominik Laskowski98041832019-08-01 18:35:59 -0700373void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
374 bool connected) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700375 android::EventThread* thread;
376 {
377 std::lock_guard<std::mutex> lock(mConnectionsLock);
378 RETURN_IF_INVALID_HANDLE(handle);
379 thread = mConnections[handle].thread.get();
380 }
381
382 thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700383}
384
Brian Johnson5dcd75d2023-08-15 09:36:37 -0700385void Scheduler::onHotplugConnectionError(ConnectionHandle handle, int32_t errorCode) {
386 android::EventThread* thread;
387 {
388 std::lock_guard<std::mutex> lock(mConnectionsLock);
389 RETURN_IF_INVALID_HANDLE(handle);
390 thread = mConnections[handle].thread.get();
391 }
392
393 thread->onHotplugConnectionError(errorCode);
394}
395
Dominik Laskowskie99b98c2023-02-02 12:37:23 -0500396void Scheduler::enableSyntheticVsync(bool enable) {
397 // TODO(b/241285945): Remove connection handles.
398 const ConnectionHandle handle = mAppConnectionHandle;
Ana Krulec6ddd2612020-09-24 13:06:33 -0700399 android::EventThread* thread;
400 {
401 std::lock_guard<std::mutex> lock(mConnectionsLock);
402 RETURN_IF_INVALID_HANDLE(handle);
403 thread = mConnections[handle].thread.get();
404 }
Dominik Laskowskie99b98c2023-02-02 12:37:23 -0500405 thread->enableSyntheticVsync(enable);
Ana Krulec98b5b242018-08-10 15:03:23 -0700406}
407
Ady Abraham62a0be22020-12-08 16:54:10 -0800408void Scheduler::onFrameRateOverridesChanged(ConnectionHandle handle, PhysicalDisplayId displayId) {
Andy Yud6a36202022-01-26 04:08:22 -0800409 const bool supportsFrameRateOverrideByContent =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500410 pacesetterSelectorPtr()->supportsAppFrameRateOverrideByContent();
Andy Yud6a36202022-01-26 04:08:22 -0800411
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800412 std::vector<FrameRateOverride> overrides =
Andy Yud6a36202022-01-26 04:08:22 -0800413 mFrameRateOverrideMappings.getAllFrameRateOverrides(supportsFrameRateOverrideByContent);
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800414
Ady Abraham62f216c2020-10-13 19:07:23 -0700415 android::EventThread* thread;
416 {
Ady Abraham62a0be22020-12-08 16:54:10 -0800417 std::lock_guard lock(mConnectionsLock);
Ady Abraham62f216c2020-10-13 19:07:23 -0700418 RETURN_IF_INVALID_HANDLE(handle);
419 thread = mConnections[handle].thread.get();
420 }
421 thread->onFrameRateOverridesChanged(displayId, std::move(overrides));
422}
423
Huihong Luo9ebb7a72023-06-27 17:01:50 -0700424void Scheduler::onHdcpLevelsChanged(ConnectionHandle handle, PhysicalDisplayId displayId,
425 int32_t connectedLevel, int32_t maxLevel) {
426 android::EventThread* thread;
427 {
428 std::lock_guard<std::mutex> lock(mConnectionsLock);
429 RETURN_IF_INVALID_HANDLE(handle);
430 thread = mConnections[handle].thread.get();
431 }
432 thread->onHdcpLevelsChanged(displayId, connectedLevel, maxLevel);
433}
434
Ady Abrahamace3d052022-11-17 16:25:05 -0800435void Scheduler::onPrimaryDisplayModeChanged(ConnectionHandle handle, const FrameRateMode& mode) {
Ady Abraham62a0be22020-12-08 16:54:10 -0800436 {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700437 std::lock_guard<std::mutex> lock(mPolicyLock);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100438 // Cache the last reported modes for primary display.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700439 mPolicy.cachedModeChangedParams = {handle, mode};
Ady Abraham5cc2e262021-03-25 13:09:17 -0700440
441 // Invalidate content based refresh rate selection so it could be calculated
442 // again for the new refresh rate.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700443 mPolicy.contentRequirements.clear();
Ady Abraham62a0be22020-12-08 16:54:10 -0800444 }
Ady Abraham690f4612021-07-01 23:24:03 -0700445 onNonPrimaryDisplayModeChanged(handle, mode);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700446}
447
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100448void Scheduler::dispatchCachedReportedMode() {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700449 // Check optional fields first.
Ady Abrahamace3d052022-11-17 16:25:05 -0800450 if (!mPolicy.modeOpt) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100451 ALOGW("No mode ID found, not dispatching cached mode.");
Ana Krulec6ddd2612020-09-24 13:06:33 -0700452 return;
453 }
Dominik Laskowski068173d2021-08-11 17:22:59 -0700454 if (!mPolicy.cachedModeChangedParams) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100455 ALOGW("No mode changed params found, not dispatching cached mode.");
Ana Krulec6ddd2612020-09-24 13:06:33 -0700456 return;
457 }
458
Ady Abrahamd1591702021-07-27 16:27:56 -0700459 // If the mode is not the current mode, this means that a
460 // mode change is in progress. In that case we shouldn't dispatch an event
461 // as it will be dispatched when the current mode changes.
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500462 if (pacesetterSelectorPtr()->getActiveMode() != mPolicy.modeOpt) {
Ady Abrahamd1591702021-07-27 16:27:56 -0700463 return;
464 }
465
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100466 // If there is no change from cached mode, there is no need to dispatch an event
Ady Abrahamace3d052022-11-17 16:25:05 -0800467 if (*mPolicy.modeOpt == mPolicy.cachedModeChangedParams->mode) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700468 return;
469 }
470
Ady Abrahamace3d052022-11-17 16:25:05 -0800471 mPolicy.cachedModeChangedParams->mode = *mPolicy.modeOpt;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700472 onNonPrimaryDisplayModeChanged(mPolicy.cachedModeChangedParams->handle,
473 mPolicy.cachedModeChangedParams->mode);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700474}
475
Ady Abrahamace3d052022-11-17 16:25:05 -0800476void Scheduler::onNonPrimaryDisplayModeChanged(ConnectionHandle handle, const FrameRateMode& mode) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700477 android::EventThread* thread;
478 {
479 std::lock_guard<std::mutex> lock(mConnectionsLock);
480 RETURN_IF_INVALID_HANDLE(handle);
481 thread = mConnections[handle].thread.get();
482 }
Ady Abraham67434eb2022-12-01 17:48:12 -0800483 thread->onModeChanged(mode);
Ady Abraham447052e2019-02-13 16:07:27 -0800484}
485
Dominik Laskowski98041832019-08-01 18:35:59 -0700486void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700487 android::EventThread* thread;
488 {
489 std::lock_guard<std::mutex> lock(mConnectionsLock);
490 RETURN_IF_INVALID_HANDLE(handle);
491 thread = mConnections.at(handle).thread.get();
492 }
493 thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700494}
495
Ady Abraham9c53ee72020-07-22 21:16:18 -0700496void Scheduler::setDuration(ConnectionHandle handle, std::chrono::nanoseconds workDuration,
497 std::chrono::nanoseconds readyDuration) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700498 android::EventThread* thread;
499 {
500 std::lock_guard<std::mutex> lock(mConnectionsLock);
501 RETURN_IF_INVALID_HANDLE(handle);
502 thread = mConnections[handle].thread.get();
503 }
504 thread->setDuration(workDuration, readyDuration);
Ana Krulec98b5b242018-08-10 15:03:23 -0700505}
Ana Krulece588e312018-09-18 12:32:24 -0700506
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500507void Scheduler::updatePhaseConfiguration(Fps refreshRate) {
508 mRefreshRateStats->setRefreshRate(refreshRate);
509 mVsyncConfiguration->setRefreshRateFps(refreshRate);
510 setVsyncConfig(mVsyncModulator->setVsyncConfigSet(mVsyncConfiguration->getCurrentConfigs()),
511 refreshRate.getPeriod());
512}
513
514void Scheduler::resetPhaseConfiguration(Fps refreshRate) {
515 // Cancel the pending refresh rate change, if any, before updating the phase configuration.
516 mVsyncModulator->cancelRefreshRateChange();
517
518 mVsyncConfiguration->reset();
519 updatePhaseConfiguration(refreshRate);
520}
521
522void Scheduler::setActiveDisplayPowerModeForRefreshRateStats(hal::PowerMode powerMode) {
523 mRefreshRateStats->setPowerMode(powerMode);
Dominik Laskowski1c99a002023-01-20 17:10:36 -0500524}
525
526void Scheduler::setVsyncConfig(const VsyncConfig& config, Period vsyncPeriod) {
527 setDuration(mAppConnectionHandle,
528 /* workDuration */ config.appWorkDuration,
529 /* readyDuration */ config.sfWorkDuration);
530 setDuration(mSfConnectionHandle,
531 /* workDuration */ vsyncPeriod,
532 /* readyDuration */ config.sfWorkDuration);
533 setDuration(config.sfWorkDuration);
534}
535
Leon Scroggins III67388622023-02-06 20:36:20 -0500536void Scheduler::enableHardwareVsync(PhysicalDisplayId id) {
537 auto schedule = getVsyncSchedule(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400538 LOG_ALWAYS_FATAL_IF(!schedule);
Dominik Laskowski66295432023-03-14 12:25:36 -0400539 schedule->enableHardwareVsync();
Ana Krulece588e312018-09-18 12:32:24 -0700540}
541
Leon Scroggins III67388622023-02-06 20:36:20 -0500542void Scheduler::disableHardwareVsync(PhysicalDisplayId id, bool disallow) {
543 auto schedule = getVsyncSchedule(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400544 LOG_ALWAYS_FATAL_IF(!schedule);
Dominik Laskowski66295432023-03-14 12:25:36 -0400545 schedule->disableHardwareVsync(disallow);
Ana Krulece588e312018-09-18 12:32:24 -0700546}
547
Leon Scroggins III67388622023-02-06 20:36:20 -0500548void Scheduler::resyncAllToHardwareVsync(bool allowToEnable) {
Rachel Leea5be3282023-03-08 20:15:54 -0800549 ATRACE_CALL();
Leon Scroggins III67388622023-02-06 20:36:20 -0500550 std::scoped_lock lock(mDisplayLock);
551 ftl::FakeGuard guard(kMainThreadContext);
552
Leon Scroggins III792ea802023-11-27 17:32:51 -0500553 for (const auto& [id, display] : mDisplays) {
554 if (display.powerMode != hal::PowerMode::OFF ||
555 !FlagManager::getInstance().multithreaded_present()) {
556 resyncToHardwareVsyncLocked(id, allowToEnable);
557 }
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500558 }
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500559}
560
Leon Scroggins III67388622023-02-06 20:36:20 -0500561void Scheduler::resyncToHardwareVsyncLocked(PhysicalDisplayId id, bool allowToEnable,
Ady Abrahamc585dba2023-11-15 18:41:35 -0800562 DisplayModePtr modePtr) {
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500563 const auto displayOpt = mDisplays.get(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400564 if (!displayOpt) {
565 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
566 return;
567 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500568 const Display& display = *displayOpt;
569
570 if (display.schedulePtr->isHardwareVsyncAllowed(allowToEnable)) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800571 if (!modePtr) {
572 modePtr = display.selectorPtr->getActiveMode().modePtr.get();
Leon Scroggins III67388622023-02-06 20:36:20 -0500573 }
Ady Abrahamc585dba2023-11-15 18:41:35 -0800574 if (modePtr->getVsyncRate().isValid()) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400575 constexpr bool kForce = false;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800576 display.schedulePtr->onDisplayModeChanged(ftl::as_non_null(modePtr), kForce);
Leon Scroggins III67388622023-02-06 20:36:20 -0500577 }
578 }
579}
580
Dominik Laskowski66295432023-03-14 12:25:36 -0400581void Scheduler::onHardwareVsyncRequest(PhysicalDisplayId id, bool enabled) {
582 static const auto& whence = __func__;
583 ATRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str());
584
585 // On main thread to serialize reads/writes of pending hardware VSYNC state.
586 static_cast<void>(
587 schedule([=]() FTL_FAKE_GUARD(mDisplayLock) FTL_FAKE_GUARD(kMainThreadContext) {
588 ATRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str());
589
590 if (const auto displayOpt = mDisplays.get(id)) {
591 auto& display = displayOpt->get();
592 display.schedulePtr->setPendingHardwareVsyncState(enabled);
593
594 if (display.powerMode != hal::PowerMode::OFF) {
595 mSchedulerCallback.requestHardwareVsync(id, enabled);
596 }
597 }
598 }));
599}
600
Leon Scroggins III67388622023-02-06 20:36:20 -0500601void Scheduler::setRenderRate(PhysicalDisplayId id, Fps renderFrameRate) {
602 std::scoped_lock lock(mDisplayLock);
603 ftl::FakeGuard guard(kMainThreadContext);
604
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500605 const auto displayOpt = mDisplays.get(id);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400606 if (!displayOpt) {
607 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
608 return;
609 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500610 const Display& display = *displayOpt;
611 const auto mode = display.selectorPtr->getActiveMode();
Ady Abrahamace3d052022-11-17 16:25:05 -0800612
613 using fps_approx_ops::operator!=;
614 LOG_ALWAYS_FATAL_IF(renderFrameRate != mode.fps,
Leon Scroggins III67388622023-02-06 20:36:20 -0500615 "Mismatch in render frame rates. Selector: %s, Scheduler: %s, Display: "
616 "%" PRIu64,
617 to_string(mode.fps).c_str(), to_string(renderFrameRate).c_str(), id.value);
Ady Abrahamace3d052022-11-17 16:25:05 -0800618
619 ALOGV("%s %s (%s)", __func__, to_string(mode.fps).c_str(),
ramindania04b8a52023-08-07 18:49:47 -0700620 to_string(mode.modePtr->getVsyncRate()).c_str());
Ady Abrahamace3d052022-11-17 16:25:05 -0800621
Ady Abrahamc585dba2023-11-15 18:41:35 -0800622 display.schedulePtr->getTracker().setRenderRate(renderFrameRate);
Ady Abrahamace3d052022-11-17 16:25:05 -0800623}
624
ramindani0491e642023-11-16 17:42:14 -0800625Fps Scheduler::getNextFrameInterval(PhysicalDisplayId id,
626 TimePoint currentExpectedPresentTime) const {
627 std::scoped_lock lock(mDisplayLock);
628 ftl::FakeGuard guard(kMainThreadContext);
629
630 const auto displayOpt = mDisplays.get(id);
631 if (!displayOpt) {
632 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
633 return Fps{};
634 }
635 const Display& display = *displayOpt;
636 const nsecs_t threshold =
637 display.selectorPtr->getActiveMode().modePtr->getVsyncRate().getPeriodNsecs() / 2;
Ady Abraham4335afd2023-12-18 19:10:47 -0800638 const nsecs_t nextVsyncTime =
639 display.schedulePtr->getTracker()
640 .nextAnticipatedVSyncTimeFrom(currentExpectedPresentTime.ns() + threshold,
641 currentExpectedPresentTime.ns());
ramindani0491e642023-11-16 17:42:14 -0800642 return Fps::fromPeriodNsecs(nextVsyncTime - currentExpectedPresentTime.ns());
643}
644
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700645void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700646 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800647
648 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700649 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800650
651 if (now - last > kIgnoreDelay) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500652 resyncAllToHardwareVsync(false /* allowToEnable */);
Ana Krulecc2870422019-01-29 19:00:58 -0800653 }
654}
655
Leon Scroggins III67388622023-02-06 20:36:20 -0500656bool Scheduler::addResyncSample(PhysicalDisplayId id, nsecs_t timestamp,
657 std::optional<nsecs_t> hwcVsyncPeriodIn) {
Leon Scroggins IIIc275df42023-02-07 16:40:21 -0500658 const auto hwcVsyncPeriod = ftl::Optional(hwcVsyncPeriodIn).transform([](nsecs_t nanos) {
659 return Period::fromNs(nanos);
660 });
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400661 auto schedule = getVsyncSchedule(id);
662 if (!schedule) {
663 ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
664 return false;
665 }
Dominik Laskowski66295432023-03-14 12:25:36 -0400666 return schedule->addResyncSample(TimePoint::fromNs(timestamp), hwcVsyncPeriod);
Ana Krulece588e312018-09-18 12:32:24 -0700667}
668
Leon Scroggins III67388622023-02-06 20:36:20 -0500669void Scheduler::addPresentFence(PhysicalDisplayId id, std::shared_ptr<FenceTime> fence) {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000670 ATRACE_NAME(ftl::Concat(__func__, ' ', id.value).c_str());
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500671 const auto scheduleOpt =
672 (ftl::FakeGuard(mDisplayLock), mDisplays.get(id)).and_then([](const Display& display) {
673 return display.powerMode == hal::PowerMode::OFF
674 ? std::nullopt
675 : std::make_optional(display.schedulePtr);
676 });
677
678 if (!scheduleOpt) return;
679 const auto& schedule = scheduleOpt->get();
680
Yi Kong08d7c812023-12-12 16:40:22 +0900681 const bool needMoreSignals = schedule->getController().addPresentFence(std::move(fence));
682 if (needMoreSignals) {
Dominik Laskowski66295432023-03-14 12:25:36 -0400683 schedule->enableHardwareVsync();
Ana Krulece588e312018-09-18 12:32:24 -0700684 } else {
Dominik Laskowski66295432023-03-14 12:25:36 -0400685 constexpr bool kDisallow = false;
686 schedule->disableHardwareVsync(kDisallow);
Ana Krulece588e312018-09-18 12:32:24 -0700687 }
688}
689
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700690void Scheduler::registerLayer(Layer* layer) {
Marin Shalamanov4be385e2021-04-23 13:25:30 +0200691 // If the content detection feature is off, we still keep the layer history,
692 // since we use it for other features (like Frame Rate API), so layers
693 // still need to be registered.
Andy Labrada096227e2022-06-15 16:58:11 +0000694 mLayerHistory.registerLayer(layer, mFeatures.test(Feature::kContentDetection));
Ady Abraham09bd3922019-04-08 10:44:56 -0700695}
696
Ady Abrahambdda8f02021-04-01 16:06:11 -0700697void Scheduler::deregisterLayer(Layer* layer) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700698 mLayerHistory.deregisterLayer(layer);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700699}
700
Ady Abraham822ecbd2023-07-07 16:16:09 -0700701void Scheduler::onLayerDestroyed(Layer* layer) {
702 std::scoped_lock lock(mChoreographerLock);
703 mAttachedChoreographers.erase(layer->getSequence());
704}
705
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000706void Scheduler::recordLayerHistory(int32_t id, const LayerProps& layerProps, nsecs_t presentTime,
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700707 nsecs_t now, LayerHistory::LayerUpdateType updateType) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500708 if (pacesetterSelectorPtr()->canSwitch()) {
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700709 mLayerHistory.record(id, layerProps, presentTime, now, updateType);
Dominik Laskowski49cea512019-11-12 14:13:23 -0800710 }
Ana Krulec3084c052018-11-21 20:27:17 +0100711}
712
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100713void Scheduler::setModeChangePending(bool pending) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700714 mLayerHistory.setModeChangePending(pending);
Ady Abraham32efd542020-05-19 17:49:26 -0700715}
716
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700717void Scheduler::setDefaultFrameRateCompatibility(
718 int32_t id, scheduler::FrameRateCompatibility frameRateCompatibility) {
719 mLayerHistory.setDefaultFrameRateCompatibility(id, frameRateCompatibility,
Andy Labrada096227e2022-06-15 16:58:11 +0000720 mFeatures.test(Feature::kContentDetection));
721}
722
Vishnu Nair41376b62023-11-08 05:08:58 -0800723void Scheduler::setLayerProperties(int32_t id, const android::scheduler::LayerProps& properties) {
724 mLayerHistory.setLayerProperties(id, properties);
725}
726
Ady Abraham822ecbd2023-07-07 16:16:09 -0700727void Scheduler::chooseRefreshRateForContent(
728 const surfaceflinger::frontend::LayerHierarchy* hierarchy,
729 bool updateAttachedChoreographer) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500730 const auto selectorPtr = pacesetterSelectorPtr();
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400731 if (!selectorPtr->canSwitch()) return;
Dominik Laskowski49cea512019-11-12 14:13:23 -0800732
Ady Abraham8a82ba62020-01-17 12:43:17 -0800733 ATRACE_CALL();
734
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400735 LayerHistory::Summary summary = mLayerHistory.summarize(*selectorPtr, systemTime());
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800736 applyPolicy(&Policy::contentRequirements, std::move(summary));
Ady Abraham822ecbd2023-07-07 16:16:09 -0700737
738 if (updateAttachedChoreographer) {
739 LOG_ALWAYS_FATAL_IF(!hierarchy);
740
741 // update the attached choreographers after we selected the render rate.
742 const ftl::Optional<FrameRateMode> modeOpt = [&] {
743 std::scoped_lock lock(mPolicyLock);
744 return mPolicy.modeOpt;
745 }();
746
747 if (modeOpt) {
748 updateAttachedChoreographers(*hierarchy, modeOpt->fps);
749 }
750 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800751}
752
Ana Krulecfb772822018-11-30 10:44:07 +0100753void Scheduler::resetIdleTimer() {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500754 pacesetterSelectorPtr()->resetIdleTimer();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800755}
756
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -0700757void Scheduler::onTouchHint() {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700758 if (mTouchTimer) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800759 mTouchTimer->reset();
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500760 pacesetterSelectorPtr()->resetKernelIdleTimer();
Dominik Laskowski49cea512019-11-12 14:13:23 -0800761 }
Ady Abraham8532d012019-05-08 14:50:56 -0700762}
763
Leon Scroggins III67388622023-02-06 20:36:20 -0500764void Scheduler::setDisplayPowerMode(PhysicalDisplayId id, hal::PowerMode powerMode) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500765 const bool isPacesetter = [this, id]() REQUIRES(kMainThreadContext) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500766 ftl::FakeGuard guard(mDisplayLock);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500767 return id == mPacesetterDisplayId;
Leon Scroggins III67388622023-02-06 20:36:20 -0500768 }();
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500769 if (isPacesetter) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500770 // TODO (b/255657128): This needs to be handled per display.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700771 std::lock_guard<std::mutex> lock(mPolicyLock);
Rachel Lee6a9731d2022-06-06 17:08:14 -0700772 mPolicy.displayPowerMode = powerMode;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700773 }
Leon Scroggins III67388622023-02-06 20:36:20 -0500774 {
775 std::scoped_lock lock(mDisplayLock);
Dominik Laskowski66295432023-03-14 12:25:36 -0400776
777 const auto displayOpt = mDisplays.get(id);
778 LOG_ALWAYS_FATAL_IF(!displayOpt);
779 auto& display = displayOpt->get();
780
781 display.powerMode = powerMode;
782 display.schedulePtr->getController().setDisplayPowerMode(powerMode);
Leon Scroggins III67388622023-02-06 20:36:20 -0500783 }
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500784 if (!isPacesetter) return;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700785
786 if (mDisplayPowerTimer) {
787 mDisplayPowerTimer->reset();
788 }
789
790 // Display Power event will boost the refresh rate to performance.
791 // Clear Layer History to get fresh FPS detection
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700792 mLayerHistory.clear();
Ady Abraham6fe2c172019-07-12 12:37:57 -0700793}
794
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500795auto Scheduler::getVsyncSchedule(std::optional<PhysicalDisplayId> idOpt) const
796 -> ConstVsyncSchedulePtr {
Leon Scroggins III67388622023-02-06 20:36:20 -0500797 std::scoped_lock lock(mDisplayLock);
798 return getVsyncScheduleLocked(idOpt);
799}
800
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500801auto Scheduler::getVsyncScheduleLocked(std::optional<PhysicalDisplayId> idOpt) const
802 -> ConstVsyncSchedulePtr {
Leon Scroggins III67388622023-02-06 20:36:20 -0500803 ftl::FakeGuard guard(kMainThreadContext);
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500804
Leon Scroggins III67388622023-02-06 20:36:20 -0500805 if (!idOpt) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500806 LOG_ALWAYS_FATAL_IF(!mPacesetterDisplayId, "Missing a pacesetter!");
807 idOpt = mPacesetterDisplayId;
Leon Scroggins III67388622023-02-06 20:36:20 -0500808 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500809
810 const auto displayOpt = mDisplays.get(*idOpt);
Leon Scroggins III4235ea02023-04-17 15:14:20 -0400811 if (!displayOpt) {
812 return nullptr;
813 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500814 return displayOpt->get().schedulePtr;
Leon Scroggins III67388622023-02-06 20:36:20 -0500815}
816
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700817void Scheduler::kernelIdleTimerCallback(TimerState state) {
818 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100819
Ady Abraham2139f732019-11-13 18:56:40 -0800820 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
821 // magic number
ramindania04b8a52023-08-07 18:49:47 -0700822 const Fps refreshRate = pacesetterSelectorPtr()->getActiveMode().modePtr->getPeakFps();
Ady Abraham3efa3942021-06-24 19:01:25 -0700823
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700824 constexpr Fps FPS_THRESHOLD_FOR_KERNEL_TIMER = 65_Hz;
825 using namespace fps_approx_ops;
826
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800827 if (state == TimerState::Reset && refreshRate > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700828 // If we're not in performance mode then the kernel timer shouldn't do
829 // anything, as the refresh rate during DPU power collapse will be the
830 // same.
Leon Scroggins III67388622023-02-06 20:36:20 -0500831 resyncAllToHardwareVsync(true /* allowToEnable */);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800832 } else if (state == TimerState::Expired && refreshRate <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700833 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
834 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
Ady Abraham8cb21882020-08-26 18:22:05 -0700835 // need to update the VsyncController model anyway.
Leon Scroggins III67388622023-02-06 20:36:20 -0500836 std::scoped_lock lock(mDisplayLock);
837 ftl::FakeGuard guard(kMainThreadContext);
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500838 for (const auto& [_, display] : mDisplays) {
839 constexpr bool kDisallow = false;
Dominik Laskowski66295432023-03-14 12:25:36 -0400840 display.schedulePtr->disableHardwareVsync(kDisallow);
Leon Scroggins III67388622023-02-06 20:36:20 -0500841 }
Alec Mouridc28b372019-04-18 21:17:13 -0700842 }
Ady Abrahama09852a2020-02-20 14:23:42 -0800843
844 mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
Alec Mouridc28b372019-04-18 21:17:13 -0700845}
846
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700847void Scheduler::idleTimerCallback(TimerState state) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800848 applyPolicy(&Policy::idleTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700849 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100850}
851
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700852void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700853 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700854 // Touch event will boost the refresh rate to performance.
855 // Clear layer history to get fresh FPS detection.
856 // NOTE: Instead of checking all the layers, we should be checking the layer
857 // that is currently on top. b/142507166 will give us this capability.
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800858 if (applyPolicy(&Policy::touch, touch).touch) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700859 mLayerHistory.clear();
Ady Abraham1adbb722020-05-15 11:51:48 -0700860 }
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700861 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700862}
863
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700864void Scheduler::displayPowerTimerCallback(TimerState state) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800865 applyPolicy(&Policy::displayPowerTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700866 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700867}
868
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400869void Scheduler::dump(utils::Dumper& dumper) const {
870 using namespace std::string_view_literals;
Ady Abraham4f960d12021-10-13 16:59:49 -0700871
872 {
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400873 utils::Dumper::Section section(dumper, "Features"sv);
874
875 for (Feature feature : ftl::enum_range<Feature>()) {
876 if (const auto flagOpt = ftl::flag_name(feature)) {
877 dumper.dump(flagOpt->substr(1), mFeatures.test(feature));
878 }
879 }
880 }
881 {
882 utils::Dumper::Section section(dumper, "Policy"sv);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400883 {
884 std::scoped_lock lock(mDisplayLock);
885 ftl::FakeGuard guard(kMainThreadContext);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500886 dumper.dump("pacesetterDisplayId"sv, mPacesetterDisplayId);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400887 }
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400888 dumper.dump("layerHistory"sv, mLayerHistory.dump());
889 dumper.dump("touchTimer"sv, mTouchTimer.transform(&OneShotTimer::interval));
890 dumper.dump("displayPowerTimer"sv, mDisplayPowerTimer.transform(&OneShotTimer::interval));
891 }
892
893 mFrameRateOverrideMappings.dump(dumper);
894 dumper.eol();
Dominik Laskowskib418dd72023-06-13 17:31:04 -0400895
Leon Scroggins III823d4ca2023-12-12 16:57:34 -0500896 mVsyncConfiguration->dump(dumper.out());
897 dumper.eol();
898
899 mRefreshRateStats->dump(dumper.out());
900 dumper.eol();
901
Dominik Laskowskiec0eac22023-01-28 16:16:19 -0500902 {
903 utils::Dumper::Section section(dumper, "Frame Targeting"sv);
904
905 std::scoped_lock lock(mDisplayLock);
906 ftl::FakeGuard guard(kMainThreadContext);
907
908 for (const auto& [id, display] : mDisplays) {
909 utils::Dumper::Section
910 section(dumper,
911 id == mPacesetterDisplayId
912 ? ftl::Concat("Pacesetter Display ", id.value).c_str()
913 : ftl::Concat("Follower Display ", id.value).c_str());
914
915 display.targeterPtr->dump(dumper);
916 dumper.eol();
917 }
918 }
Ana Krulecb43429d2019-01-09 14:28:51 -0800919}
920
Dominik Laskowski068173d2021-08-11 17:22:59 -0700921void Scheduler::dumpVsync(std::string& out) const {
Leon Scroggins III67388622023-02-06 20:36:20 -0500922 std::scoped_lock lock(mDisplayLock);
923 ftl::FakeGuard guard(kMainThreadContext);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500924 if (mPacesetterDisplayId) {
925 base::StringAppendF(&out, "VsyncSchedule for pacesetter %s:\n",
926 to_string(*mPacesetterDisplayId).c_str());
Leon Scroggins III67388622023-02-06 20:36:20 -0500927 getVsyncScheduleLocked()->dump(out);
928 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500929 for (auto& [id, display] : mDisplays) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500930 if (id == mPacesetterDisplayId) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500931 continue;
932 }
933 base::StringAppendF(&out, "VsyncSchedule for follower %s:\n", to_string(id).c_str());
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500934 display.schedulePtr->dump(out);
Leon Scroggins III67388622023-02-06 20:36:20 -0500935 }
Ady Abraham8735eac2020-08-12 16:35:04 -0700936}
937
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800938bool Scheduler::updateFrameRateOverrides(GlobalSignals consideredSignals, Fps displayRefreshRate) {
Ady Abraham33a386b2023-07-18 15:37:11 -0700939 std::scoped_lock lock(mPolicyLock);
940 return updateFrameRateOverridesLocked(consideredSignals, displayRefreshRate);
941}
942
943bool Scheduler::updateFrameRateOverridesLocked(GlobalSignals consideredSignals,
944 Fps displayRefreshRate) {
Dominik Laskowski596a2562022-10-28 11:26:12 -0400945 if (consideredSignals.idle) return false;
946
947 const auto frameRateOverrides =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500948 pacesetterSelectorPtr()->getFrameRateOverrides(mPolicy.contentRequirements,
949 displayRefreshRate, consideredSignals);
Dominik Laskowski596a2562022-10-28 11:26:12 -0400950
951 // Note that RefreshRateSelector::supportsFrameRateOverrideByContent is checked when querying
952 // the FrameRateOverrideMappings rather than here.
953 return mFrameRateOverrideMappings.updateFrameRateOverridesByContent(frameRateOverrides);
954}
955
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500956void Scheduler::promotePacesetterDisplay(std::optional<PhysicalDisplayId> pacesetterIdOpt) {
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400957 std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule;
958
959 {
960 std::scoped_lock lock(mDisplayLock);
961 pacesetterVsyncSchedule = promotePacesetterDisplayLocked(pacesetterIdOpt);
962 }
963
Leon Scroggins39d25342023-04-19 17:11:01 +0000964 applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400965}
966
967std::shared_ptr<VsyncSchedule> Scheduler::promotePacesetterDisplayLocked(
968 std::optional<PhysicalDisplayId> pacesetterIdOpt) {
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500969 // TODO(b/241286431): Choose the pacesetter display.
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500970 mPacesetterDisplayId = pacesetterIdOpt.value_or(mDisplays.begin()->first);
Leon Scroggins III1af0fb62023-03-02 14:21:44 -0500971 ALOGI("Display %s is the pacesetter", to_string(*mPacesetterDisplayId).c_str());
Dominik Laskowski596a2562022-10-28 11:26:12 -0400972
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500973 std::shared_ptr<VsyncSchedule> newVsyncSchedulePtr;
974 if (const auto pacesetterOpt = pacesetterDisplayLocked()) {
975 const Display& pacesetter = *pacesetterOpt;
976
977 pacesetter.selectorPtr->setIdleTimerCallbacks(
Dominik Laskowski596a2562022-10-28 11:26:12 -0400978 {.platform = {.onReset = [this] { idleTimerCallback(TimerState::Reset); },
979 .onExpired = [this] { idleTimerCallback(TimerState::Expired); }},
980 .kernel = {.onReset = [this] { kernelIdleTimerCallback(TimerState::Reset); },
981 .onExpired =
982 [this] { kernelIdleTimerCallback(TimerState::Expired); }}});
983
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500984 pacesetter.selectorPtr->startIdleTimer();
Leon Scroggins III67388622023-02-06 20:36:20 -0500985
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500986 newVsyncSchedulePtr = pacesetter.schedulePtr;
987
Dominik Laskowski66295432023-03-14 12:25:36 -0400988 constexpr bool kForce = true;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800989 newVsyncSchedulePtr->onDisplayModeChanged(pacesetter.selectorPtr->getActiveMode().modePtr,
990 kForce);
Leon Scroggins III67388622023-02-06 20:36:20 -0500991 }
Dominik Laskowskic404cb42023-03-03 19:57:53 -0500992 return newVsyncSchedulePtr;
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400993}
Leon Scroggins III67388622023-02-06 20:36:20 -0500994
Leon Scroggins39d25342023-04-19 17:11:01 +0000995void Scheduler::applyNewVsyncSchedule(std::shared_ptr<VsyncSchedule> vsyncSchedule) {
996 onNewVsyncSchedule(vsyncSchedule->getDispatch());
Leon Scroggins III6fc45192023-03-16 12:13:28 -0400997 std::vector<android::EventThread*> threads;
Leon Scroggins III67388622023-02-06 20:36:20 -0500998 {
999 std::lock_guard<std::mutex> lock(mConnectionsLock);
Leon Scroggins III6fc45192023-03-16 12:13:28 -04001000 threads.reserve(mConnections.size());
Leon Scroggins III67388622023-02-06 20:36:20 -05001001 for (auto& [_, connection] : mConnections) {
Leon Scroggins III6fc45192023-03-16 12:13:28 -04001002 threads.push_back(connection.thread.get());
Leon Scroggins III67388622023-02-06 20:36:20 -05001003 }
Ady Abraham62a0be22020-12-08 16:54:10 -08001004 }
Leon Scroggins III6fc45192023-03-16 12:13:28 -04001005 for (auto* thread : threads) {
Leon Scroggins39d25342023-04-19 17:11:01 +00001006 thread->onNewVsyncSchedule(vsyncSchedule);
Leon Scroggins III6fc45192023-03-16 12:13:28 -04001007 }
Dominik Laskowski596a2562022-10-28 11:26:12 -04001008}
1009
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001010void Scheduler::demotePacesetterDisplay() {
Dominik Laskowski596a2562022-10-28 11:26:12 -04001011 // No need to lock for reads on kMainThreadContext.
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001012 if (const auto pacesetterPtr = FTL_FAKE_GUARD(mDisplayLock, pacesetterSelectorPtrLocked())) {
1013 pacesetterPtr->stopIdleTimer();
1014 pacesetterPtr->clearIdleTimerCallbacks();
Dominik Laskowski596a2562022-10-28 11:26:12 -04001015 }
1016
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001017 // Clear state that depends on the pacesetter's RefreshRateSelector.
Dominik Laskowski596a2562022-10-28 11:26:12 -04001018 std::scoped_lock lock(mPolicyLock);
1019 mPolicy = {};
Ady Abraham62a0be22020-12-08 16:54:10 -08001020}
1021
Ady Abraham822ecbd2023-07-07 16:16:09 -07001022void Scheduler::updateAttachedChoreographersFrameRate(
1023 const surfaceflinger::frontend::RequestedLayerState& layer, Fps fps) {
1024 std::scoped_lock lock(mChoreographerLock);
1025
1026 const auto layerId = static_cast<int32_t>(layer.id);
1027 const auto choreographers = mAttachedChoreographers.find(layerId);
1028 if (choreographers == mAttachedChoreographers.end()) {
1029 return;
1030 }
1031
1032 auto& layerChoreographers = choreographers->second;
1033
1034 layerChoreographers.frameRate = fps;
1035 ATRACE_FORMAT_INSTANT("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str());
1036 ALOGV("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str());
1037
1038 auto it = layerChoreographers.connections.begin();
1039 while (it != layerChoreographers.connections.end()) {
1040 sp<EventThreadConnection> choreographerConnection = it->promote();
1041 if (choreographerConnection) {
1042 choreographerConnection->frameRate = fps;
1043 it++;
1044 } else {
1045 it = choreographers->second.connections.erase(it);
1046 }
1047 }
1048
1049 if (layerChoreographers.connections.empty()) {
1050 mAttachedChoreographers.erase(choreographers);
1051 }
1052}
1053
1054int Scheduler::updateAttachedChoreographersInternal(
1055 const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate,
1056 int parentDivisor) {
1057 const char* name = layerHierarchy.getLayer() ? layerHierarchy.getLayer()->name.c_str() : "Root";
1058
1059 int divisor = 0;
1060 if (layerHierarchy.getLayer()) {
1061 const auto frameRateCompatibility = layerHierarchy.getLayer()->frameRateCompatibility;
1062 const auto frameRate = Fps::fromValue(layerHierarchy.getLayer()->frameRate);
1063 ALOGV("%s: %s frameRate %s parentDivisor=%d", __func__, name, to_string(frameRate).c_str(),
1064 parentDivisor);
1065
1066 if (frameRate.isValid()) {
1067 if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE ||
1068 frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_EXACT) {
1069 // Since this layer wants an exact match, we would only set a frame rate if the
1070 // desired rate is a divisor of the display refresh rate.
1071 divisor = RefreshRateSelector::getFrameRateDivisor(displayRefreshRate, frameRate);
1072 } else if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT) {
1073 // find the closest frame rate divisor for the desired frame rate.
1074 divisor = static_cast<int>(
1075 std::round(displayRefreshRate.getValue() / frameRate.getValue()));
1076 }
1077 }
1078 }
1079
1080 // We start by traversing the children, updating their choreographers, and getting back the
1081 // aggregated frame rate.
1082 int childrenDivisor = 0;
1083 for (const auto& [child, _] : layerHierarchy.mChildren) {
1084 LOG_ALWAYS_FATAL_IF(child == nullptr || child->getLayer() == nullptr);
1085
1086 ALOGV("%s: %s traversing child %s", __func__, name, child->getLayer()->name.c_str());
1087
1088 const int childDivisor =
1089 updateAttachedChoreographersInternal(*child, displayRefreshRate, divisor);
1090 childrenDivisor = childrenDivisor > 0 ? childrenDivisor : childDivisor;
1091 if (childDivisor > 0) {
1092 childrenDivisor = std::gcd(childrenDivisor, childDivisor);
1093 }
1094 ALOGV("%s: %s childrenDivisor=%d", __func__, name, childrenDivisor);
1095 }
1096
1097 ALOGV("%s: %s divisor=%d", __func__, name, divisor);
1098
1099 // If there is no explicit vote for this layer. Use the children's vote if exists
1100 divisor = (divisor == 0) ? childrenDivisor : divisor;
1101 ALOGV("%s: %s divisor=%d with children", __func__, name, divisor);
1102
1103 // If there is no explicit vote for this layer or its children, Use the parent vote if exists
1104 divisor = (divisor == 0) ? parentDivisor : divisor;
1105 ALOGV("%s: %s divisor=%d with parent", __func__, name, divisor);
1106
1107 if (layerHierarchy.getLayer()) {
1108 Fps fps = divisor > 1 ? displayRefreshRate / (unsigned int)divisor : Fps();
1109 updateAttachedChoreographersFrameRate(*layerHierarchy.getLayer(), fps);
1110 }
1111
1112 return divisor;
1113}
1114
1115void Scheduler::updateAttachedChoreographers(
1116 const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate) {
1117 ATRACE_CALL();
1118 updateAttachedChoreographersInternal(layerHierarchy, displayRefreshRate, 0);
1119}
1120
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001121template <typename S, typename T>
1122auto Scheduler::applyPolicy(S Policy::*statePtr, T&& newState) -> GlobalSignals {
Ady Abraham73c3df52023-01-12 18:09:31 -08001123 ATRACE_CALL();
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001124 std::vector<display::DisplayModeRequest> modeRequests;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001125 GlobalSignals consideredSignals;
1126
Ady Abraham62a0be22020-12-08 16:54:10 -08001127 bool refreshRateChanged = false;
1128 bool frameRateOverridesChanged;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001129
Ady Abraham8532d012019-05-08 14:50:56 -07001130 {
Dominik Laskowski596a2562022-10-28 11:26:12 -04001131 std::scoped_lock lock(mPolicyLock);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001132
1133 auto& currentState = mPolicy.*statePtr;
1134 if (currentState == newState) return {};
1135 currentState = std::forward<T>(newState);
1136
Dominik Laskowski596a2562022-10-28 11:26:12 -04001137 DisplayModeChoiceMap modeChoices;
Ady Abrahamace3d052022-11-17 16:25:05 -08001138 ftl::Optional<FrameRateMode> modeOpt;
Dominik Laskowski596a2562022-10-28 11:26:12 -04001139 {
1140 std::scoped_lock lock(mDisplayLock);
1141 ftl::FakeGuard guard(kMainThreadContext);
1142
1143 modeChoices = chooseDisplayModes();
1144
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001145 // TODO(b/240743786): The pacesetter display's mode must change for any
1146 // DisplayModeRequest to go through. Fix this by tracking per-display Scheduler::Policy
1147 // and timers.
Ady Abrahamace3d052022-11-17 16:25:05 -08001148 std::tie(modeOpt, consideredSignals) =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001149 modeChoices.get(*mPacesetterDisplayId)
Dominik Laskowski596a2562022-10-28 11:26:12 -04001150 .transform([](const DisplayModeChoice& choice) {
Ady Abrahamace3d052022-11-17 16:25:05 -08001151 return std::make_pair(choice.mode, choice.consideredSignals);
Dominik Laskowski596a2562022-10-28 11:26:12 -04001152 })
1153 .value();
1154 }
ramindani69b58e82022-09-26 16:48:36 -07001155
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001156 modeRequests.reserve(modeChoices.size());
1157 for (auto& [id, choice] : modeChoices) {
1158 modeRequests.emplace_back(
Ady Abrahamace3d052022-11-17 16:25:05 -08001159 display::DisplayModeRequest{.mode = std::move(choice.mode),
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001160 .emitEvent = !choice.consideredSignals.idle});
1161 }
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001162
Ady Abraham33a386b2023-07-18 15:37:11 -07001163 frameRateOverridesChanged = updateFrameRateOverridesLocked(consideredSignals, modeOpt->fps);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001164
Ady Abrahamace3d052022-11-17 16:25:05 -08001165 if (mPolicy.modeOpt != modeOpt) {
1166 mPolicy.modeOpt = modeOpt;
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001167 refreshRateChanged = true;
1168 } else {
Marin Shalamanova7fe3042021-01-29 21:02:08 +01001169 // We don't need to change the display mode, but we might need to send an event
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001170 // about a mode change, since it was suppressed if previously considered idle.
Ady Abrahamdfd62162020-06-10 16:11:56 -07001171 if (!consideredSignals.idle) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +01001172 dispatchCachedReportedMode();
Ady Abrahamdfd62162020-06-10 16:11:56 -07001173 }
Ady Abraham8532d012019-05-08 14:50:56 -07001174 }
Ady Abraham8532d012019-05-08 14:50:56 -07001175 }
Ady Abraham62a0be22020-12-08 16:54:10 -08001176 if (refreshRateChanged) {
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001177 mSchedulerCallback.requestDisplayModes(std::move(modeRequests));
Ady Abraham62a0be22020-12-08 16:54:10 -08001178 }
1179 if (frameRateOverridesChanged) {
1180 mSchedulerCallback.triggerOnFrameRateOverridesChanged();
1181 }
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -08001182 return consideredSignals;
Ady Abraham8532d012019-05-08 14:50:56 -07001183}
1184
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001185auto Scheduler::chooseDisplayModes() const -> DisplayModeChoiceMap {
Ady Abraham4ccdcb42020-02-11 17:34:34 -08001186 ATRACE_CALL();
Ady Abraham09bd3922019-04-08 10:44:56 -07001187
Ady Abraham68636062022-11-16 17:07:25 -08001188 using RankedRefreshRates = RefreshRateSelector::RankedFrameRates;
Dominik Laskowski6b049ff2023-01-29 15:46:45 -05001189 ui::PhysicalDisplayVector<RankedRefreshRates> perDisplayRanking;
Dominik Laskowski01602522022-10-07 19:02:28 -04001190 const auto globalSignals = makeGlobalSignals();
ramindani22f2ead2023-04-21 10:27:11 -07001191 Fps pacesetterFps;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -08001192
Dominik Laskowskic404cb42023-03-03 19:57:53 -05001193 for (const auto& [id, display] : mDisplays) {
Ady Abrahamace3d052022-11-17 16:25:05 -08001194 auto rankedFrameRates =
Dominik Laskowskic404cb42023-03-03 19:57:53 -05001195 display.selectorPtr->getRankedFrameRates(mPolicy.contentRequirements,
1196 globalSignals);
ramindani22f2ead2023-04-21 10:27:11 -07001197 if (id == *mPacesetterDisplayId) {
1198 pacesetterFps = rankedFrameRates.ranking.front().frameRateMode.fps;
Dominik Laskowski01602522022-10-07 19:02:28 -04001199 }
Ady Abrahamace3d052022-11-17 16:25:05 -08001200 perDisplayRanking.push_back(std::move(rankedFrameRates));
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001201 }
ramindani69b58e82022-09-26 16:48:36 -07001202
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001203 DisplayModeChoiceMap modeChoices;
ramindani69b58e82022-09-26 16:48:36 -07001204 using fps_approx_ops::operator==;
Dominik Laskowski01602522022-10-07 19:02:28 -04001205
ramindani22f2ead2023-04-21 10:27:11 -07001206 for (auto& [rankings, signals] : perDisplayRanking) {
1207 const auto chosenFrameRateMode =
1208 ftl::find_if(rankings,
1209 [&](const auto& ranking) {
1210 return ranking.frameRateMode.fps == pacesetterFps;
1211 })
1212 .transform([](const auto& scoredFrameRate) {
1213 return scoredFrameRate.get().frameRateMode;
1214 })
1215 .value_or(rankings.front().frameRateMode);
Dominik Laskowski01602522022-10-07 19:02:28 -04001216
ramindani22f2ead2023-04-21 10:27:11 -07001217 modeChoices.try_emplace(chosenFrameRateMode.modePtr->getPhysicalDisplayId(),
1218 DisplayModeChoice{chosenFrameRateMode, signals});
Dominik Laskowski01602522022-10-07 19:02:28 -04001219 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001220 return modeChoices;
ramindani69b58e82022-09-26 16:48:36 -07001221}
1222
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001223GlobalSignals Scheduler::makeGlobalSignals() const {
ramindani38c84982022-08-29 18:02:57 +00001224 const bool powerOnImminent = mDisplayPowerTimer &&
1225 (mPolicy.displayPowerMode != hal::PowerMode::ON ||
1226 mPolicy.displayPowerTimer == TimerState::Reset);
Ady Abraham6fe2c172019-07-12 12:37:57 -07001227
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001228 return {.touch = mTouchTimer && mPolicy.touch == TouchState::Active,
1229 .idle = mPolicy.idleTimer == TimerState::Expired,
1230 .powerOnImminent = powerOnImminent};
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001231}
1232
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001233FrameRateMode Scheduler::getPreferredDisplayMode() {
Dominik Laskowski068173d2021-08-11 17:22:59 -07001234 std::lock_guard<std::mutex> lock(mPolicyLock);
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001235 const auto frameRateMode =
Leon Scroggins III1af0fb62023-03-02 14:21:44 -05001236 pacesetterSelectorPtr()
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001237 ->getRankedFrameRates(mPolicy.contentRequirements, makeGlobalSignals())
1238 .ranking.front()
1239 .frameRateMode;
Dominik Laskowski95df6a12022-10-07 18:11:07 -04001240
Dominik Laskowskifc378b02022-12-02 14:56:05 -05001241 // Make sure the stored mode is up to date.
1242 mPolicy.modeOpt = frameRateMode;
1243
1244 return frameRateMode;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -07001245}
1246
Peiyong Line9d809e2020-04-14 13:10:48 -07001247void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
Ady Abraham3a77a7b2019-12-02 18:46:59 -08001248 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
1249 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
1250
1251 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
1252 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
1253 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
1254 }
1255}
1256
Leon Scroggins III5b581492023-10-31 14:29:41 -04001257bool Scheduler::onCompositionPresented(nsecs_t presentTime) {
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001258 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
1259 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
1260 if (presentTime < mLastVsyncPeriodChangeTimeline->refreshTimeNanos) {
1261 // We need to composite again as refreshTimeNanos is still in the future.
1262 return true;
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001263 }
Dominik Laskowski8da6b0e2021-05-12 15:34:13 -07001264
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001265 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001266 }
Dominik Laskowskidd5827a2022-03-17 12:44:23 -07001267 return false;
Ana Krulecfefd6ae2019-02-13 17:53:08 -08001268}
1269
Ady Abraham7825c682021-05-17 15:12:14 -07001270void Scheduler::onActiveDisplayAreaChanged(uint32_t displayArea) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -07001271 mLayerHistory.setDisplayArea(displayArea);
Ady Abraham8a82ba62020-01-17 12:43:17 -08001272}
1273
Andy Yu8c2703d2023-11-03 11:22:46 -07001274void Scheduler::setGameModeFrameRateForUid(FrameRateOverride frameRateOverride) {
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001275 if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) {
1276 return;
1277 }
1278
Andy Yu8c2703d2023-11-03 11:22:46 -07001279 if (FlagManager::getInstance().game_default_frame_rate()) {
1280 // update the frame rate override mapping in LayerHistory
1281 mLayerHistory.updateGameModeFrameRateOverride(frameRateOverride);
1282 } else {
1283 mFrameRateOverrideMappings.setGameModeRefreshRateForUid(frameRateOverride);
1284 }
1285}
1286
1287void Scheduler::setGameDefaultFrameRateForUid(FrameRateOverride frameRateOverride) {
1288 if (!FlagManager::getInstance().game_default_frame_rate() ||
1289 (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f)) {
1290 return;
1291 }
1292
1293 // update the frame rate override mapping in LayerHistory
1294 mLayerHistory.updateGameDefaultFrameRateOverride(frameRateOverride);
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001295}
1296
Ady Abraham62a0be22020-12-08 16:54:10 -08001297void Scheduler::setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride) {
1298 if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) {
1299 return;
1300 }
1301
Andy Yu2ae6b6b2021-11-18 14:51:06 -08001302 mFrameRateOverrideMappings.setPreferredRefreshRateForUid(frameRateOverride);
Ady Abraham62a0be22020-12-08 16:54:10 -08001303}
1304
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001305void Scheduler::updateSmallAreaDetection(
Tony Huangf3621102023-09-04 17:14:22 +08001306 std::vector<std::pair<int32_t, float>>& uidThresholdMappings) {
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001307 mSmallAreaDetectionAllowMappings.update(uidThresholdMappings);
1308}
1309
Tony Huangf3621102023-09-04 17:14:22 +08001310void Scheduler::setSmallAreaDetectionThreshold(int32_t appId, float threshold) {
Jerry Chang36678002023-11-29 16:56:17 +00001311 mSmallAreaDetectionAllowMappings.setThresholdForAppId(appId, threshold);
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001312}
1313
Tony Huangf3621102023-09-04 17:14:22 +08001314bool Scheduler::isSmallDirtyArea(int32_t appId, uint32_t dirtyArea) {
1315 std::optional<float> oThreshold = mSmallAreaDetectionAllowMappings.getThresholdForAppId(appId);
1316 if (oThreshold) {
1317 return mLayerHistory.isSmallDirtyArea(dirtyArea, oThreshold.value());
1318 }
Tony Huang9ac5e6e2023-08-24 09:01:44 +00001319 return false;
1320}
1321
Dominik Laskowski068173d2021-08-11 17:22:59 -07001322} // namespace android::scheduler