1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #undef LOG_TAG
18 #define LOG_TAG "Scheduler"
19 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
20 
21 #include "Scheduler.h"
22 
23 #include <android-base/properties.h>
24 #include <android-base/stringprintf.h>
25 #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
26 #include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
27 #include <configstore/Utils.h>
28 #include <ftl/concat.h>
29 #include <ftl/enum.h>
30 #include <ftl/fake_guard.h>
31 #include <ftl/small_map.h>
32 #include <gui/TraceUtils.h>
33 #include <gui/WindowInfo.h>
34 #include <system/window.h>
35 #include <ui/DisplayMap.h>
36 #include <utils/Timers.h>
37 
38 #include <FrameTimeline/FrameTimeline.h>
39 #include <scheduler/interface/ICompositor.h>
40 
41 #include <algorithm>
42 #include <cinttypes>
43 #include <cstdint>
44 #include <functional>
45 #include <memory>
46 #include <numeric>
47 
48 #include <common/FlagManager.h>
49 #include "../Layer.h"
50 #include "EventThread.h"
51 #include "FrameRateOverrideMappings.h"
52 #include "FrontEnd/LayerHandle.h"
53 #include "OneShotTimer.h"
54 #include "RefreshRateStats.h"
55 #include "SurfaceFlingerFactory.h"
56 #include "SurfaceFlingerProperties.h"
57 #include "TimeStats/TimeStats.h"
58 #include "VSyncTracker.h"
59 #include "VsyncConfiguration.h"
60 #include "VsyncController.h"
61 #include "VsyncSchedule.h"
62 
63 namespace android::scheduler {
64 
Scheduler(ICompositor & compositor,ISchedulerCallback & callback,FeatureFlags features,surfaceflinger::Factory & factory,Fps activeRefreshRate,TimeStats & timeStats)65 Scheduler::Scheduler(ICompositor& compositor, ISchedulerCallback& callback, FeatureFlags features,
66                      surfaceflinger::Factory& factory, Fps activeRefreshRate, TimeStats& timeStats)
67       : android::impl::MessageQueue(compositor),
68         mFeatures(features),
69         mVsyncConfiguration(factory.createVsyncConfiguration(activeRefreshRate)),
70         mVsyncModulator(sp<VsyncModulator>::make(mVsyncConfiguration->getCurrentConfigs())),
71         mRefreshRateStats(std::make_unique<RefreshRateStats>(timeStats, activeRefreshRate)),
72         mSchedulerCallback(callback) {}
73 
~Scheduler()74 Scheduler::~Scheduler() {
75     // 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 
80     // Stop timers and wait for their threads to exit.
81     mDisplayPowerTimer.reset();
82     mTouchTimer.reset();
83 
84     // Stop idle timer and clear callbacks, as the RefreshRateSelector may outlive the Scheduler.
85     demotePacesetterDisplay({.toggleIdleTimer = true});
86 }
87 
initVsync(frametimeline::TokenManager & tokenManager,std::chrono::nanoseconds workDuration)88 void Scheduler::initVsync(frametimeline::TokenManager& tokenManager,
89                           std::chrono::nanoseconds workDuration) {
90     Impl::initVsyncInternal(getVsyncSchedule()->getDispatch(), tokenManager, workDuration);
91 }
92 
startTimers()93 void Scheduler::startTimers() {
94     using namespace sysprop;
95     using namespace std::string_literals;
96 
97     const int32_t defaultTouchTimerValue =
98             FlagManager::getInstance().enable_fro_dependent_features() &&
99                     sysprop::enable_frame_rate_override(true)
100             ? 200
101             : 0;
102     if (const int32_t millis = set_touch_timer_ms(defaultTouchTimerValue); millis > 0) {
103         // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
104         mTouchTimer.emplace(
105                 "TouchTimer", std::chrono::milliseconds(millis),
106                 [this] { touchTimerCallback(TimerState::Reset); },
107                 [this] { touchTimerCallback(TimerState::Expired); });
108         mTouchTimer->start();
109     }
110 
111     if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
112         mDisplayPowerTimer.emplace(
113                 "DisplayPowerTimer", std::chrono::milliseconds(millis),
114                 [this] { displayPowerTimerCallback(TimerState::Reset); },
115                 [this] { displayPowerTimerCallback(TimerState::Expired); });
116         mDisplayPowerTimer->start();
117     }
118 }
119 
setPacesetterDisplay(PhysicalDisplayId pacesetterId)120 void Scheduler::setPacesetterDisplay(PhysicalDisplayId pacesetterId) {
121     constexpr PromotionParams kPromotionParams = {.toggleIdleTimer = true};
122 
123     demotePacesetterDisplay(kPromotionParams);
124     promotePacesetterDisplay(pacesetterId, kPromotionParams);
125 }
126 
registerDisplay(PhysicalDisplayId displayId,RefreshRateSelectorPtr selectorPtr,PhysicalDisplayId activeDisplayId)127 void Scheduler::registerDisplay(PhysicalDisplayId displayId, RefreshRateSelectorPtr selectorPtr,
128                                 PhysicalDisplayId activeDisplayId) {
129     auto schedulePtr =
130             std::make_shared<VsyncSchedule>(selectorPtr->getActiveMode().modePtr, mFeatures,
131                                             [this](PhysicalDisplayId id, bool enable) {
132                                                 onHardwareVsyncRequest(id, enable);
133                                             });
134 
135     registerDisplayInternal(displayId, std::move(selectorPtr), std::move(schedulePtr),
136                             activeDisplayId);
137 }
138 
registerDisplayInternal(PhysicalDisplayId displayId,RefreshRateSelectorPtr selectorPtr,VsyncSchedulePtr schedulePtr,PhysicalDisplayId activeDisplayId)139 void Scheduler::registerDisplayInternal(PhysicalDisplayId displayId,
140                                         RefreshRateSelectorPtr selectorPtr,
141                                         VsyncSchedulePtr schedulePtr,
142                                         PhysicalDisplayId activeDisplayId) {
143     const bool isPrimary = (ftl::FakeGuard(mDisplayLock), !mPacesetterDisplayId);
144 
145     // Start the idle timer for the first registered (i.e. primary) display.
146     const PromotionParams promotionParams = {.toggleIdleTimer = isPrimary};
147 
148     demotePacesetterDisplay(promotionParams);
149 
150     auto [pacesetterVsyncSchedule, isNew] = [&]() REQUIRES(kMainThreadContext) {
151         std::scoped_lock lock(mDisplayLock);
152         const bool isNew = mDisplays
153                                    .emplace_or_replace(displayId, displayId, std::move(selectorPtr),
154                                                        std::move(schedulePtr), mFeatures)
155                                    .second;
156 
157         return std::make_pair(promotePacesetterDisplayLocked(activeDisplayId, promotionParams),
158                               isNew);
159     }();
160 
161     applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
162 
163     // Disable hardware VSYNC if the registration is new, as opposed to a renewal.
164     if (isNew) {
165         onHardwareVsyncRequest(displayId, false);
166     }
167 
168     dispatchHotplug(displayId, Hotplug::Connected);
169 }
170 
unregisterDisplay(PhysicalDisplayId displayId,PhysicalDisplayId activeDisplayId)171 void Scheduler::unregisterDisplay(PhysicalDisplayId displayId, PhysicalDisplayId activeDisplayId) {
172     LOG_ALWAYS_FATAL_IF(displayId == activeDisplayId, "Cannot unregister the active display!");
173 
174     dispatchHotplug(displayId, Hotplug::Disconnected);
175 
176     constexpr PromotionParams kPromotionParams = {.toggleIdleTimer = false};
177     demotePacesetterDisplay(kPromotionParams);
178 
179     std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule;
180     {
181         std::scoped_lock lock(mDisplayLock);
182         mDisplays.erase(displayId);
183 
184         // Do not allow removing the final display. Code in the scheduler expects
185         // there to be at least one display. (This may be relaxed in the future with
186         // headless virtual display.)
187         LOG_ALWAYS_FATAL_IF(mDisplays.empty(), "Cannot unregister all displays!");
188 
189         pacesetterVsyncSchedule = promotePacesetterDisplayLocked(activeDisplayId, kPromotionParams);
190     }
191     applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
192 }
193 
run()194 void Scheduler::run() {
195     while (true) {
196         waitMessage();
197     }
198 }
199 
onFrameSignal(ICompositor & compositor,VsyncId vsyncId,TimePoint expectedVsyncTime)200 void Scheduler::onFrameSignal(ICompositor& compositor, VsyncId vsyncId,
201                               TimePoint expectedVsyncTime) {
202     const FrameTargeter::BeginFrameArgs beginFrameArgs =
203             {.frameBeginTime = SchedulerClock::now(),
204              .vsyncId = vsyncId,
205              .expectedVsyncTime = expectedVsyncTime,
206              .sfWorkDuration = mVsyncModulator->getVsyncConfig().sfWorkDuration,
207              .hwcMinWorkDuration = mVsyncConfiguration->getCurrentConfigs().hwcMinWorkDuration};
208 
209     ftl::NonNull<const Display*> pacesetterPtr = pacesetterPtrLocked();
210     pacesetterPtr->targeterPtr->beginFrame(beginFrameArgs, *pacesetterPtr->schedulePtr);
211 
212     {
213         FrameTargets targets;
214         targets.try_emplace(pacesetterPtr->displayId, &pacesetterPtr->targeterPtr->target());
215 
216         // TODO (b/256196556): Followers should use the next VSYNC after the frontrunner, not the
217         // pacesetter.
218         // Update expectedVsyncTime, which may have been adjusted by beginFrame.
219         expectedVsyncTime = pacesetterPtr->targeterPtr->target().expectedPresentTime();
220 
221         for (const auto& [id, display] : mDisplays) {
222             if (id == pacesetterPtr->displayId) continue;
223 
224             auto followerBeginFrameArgs = beginFrameArgs;
225             followerBeginFrameArgs.expectedVsyncTime =
226                     display.schedulePtr->vsyncDeadlineAfter(expectedVsyncTime);
227 
228             FrameTargeter& targeter = *display.targeterPtr;
229             targeter.beginFrame(followerBeginFrameArgs, *display.schedulePtr);
230             targets.try_emplace(id, &targeter.target());
231         }
232 
233         if (!compositor.commit(pacesetterPtr->displayId, targets)) {
234             if (FlagManager::getInstance().vrr_config()) {
235                 compositor.sendNotifyExpectedPresentHint(pacesetterPtr->displayId);
236             }
237             mSchedulerCallback.onCommitNotComposited(pacesetterPtr->displayId);
238             return;
239         }
240     }
241 
242     // The pacesetter may have changed or been registered anew during commit.
243     pacesetterPtr = pacesetterPtrLocked();
244 
245     // TODO(b/256196556): Choose the frontrunner display.
246     FrameTargeters targeters;
247     targeters.try_emplace(pacesetterPtr->displayId, pacesetterPtr->targeterPtr.get());
248 
249     for (auto& [id, display] : mDisplays) {
250         if (id == pacesetterPtr->displayId) continue;
251 
252         FrameTargeter& targeter = *display.targeterPtr;
253         targeters.try_emplace(id, &targeter);
254     }
255 
256     if (FlagManager::getInstance().vrr_config() &&
257         CC_UNLIKELY(mPacesetterFrameDurationFractionToSkip > 0.f)) {
258         const auto period = pacesetterPtr->targeterPtr->target().expectedFrameDuration();
259         const auto skipDuration = Duration::fromNs(
260                 static_cast<nsecs_t>(period.ns() * mPacesetterFrameDurationFractionToSkip));
261         ATRACE_FORMAT("Injecting jank for %f%% of the frame (%" PRId64 " ns)",
262                       mPacesetterFrameDurationFractionToSkip * 100, skipDuration.ns());
263         std::this_thread::sleep_for(skipDuration);
264         mPacesetterFrameDurationFractionToSkip = 0.f;
265     }
266 
267     const auto resultsPerDisplay = compositor.composite(pacesetterPtr->displayId, targeters);
268     if (FlagManager::getInstance().vrr_config()) {
269         compositor.sendNotifyExpectedPresentHint(pacesetterPtr->displayId);
270     }
271     compositor.sample();
272 
273     for (const auto& [id, targeter] : targeters) {
274         const auto resultOpt = resultsPerDisplay.get(id);
275         LOG_ALWAYS_FATAL_IF(!resultOpt);
276         targeter->endFrame(*resultOpt);
277     }
278 }
279 
getFrameRateOverride(uid_t uid) const280 std::optional<Fps> Scheduler::getFrameRateOverride(uid_t uid) const {
281     const bool supportsFrameRateOverrideByContent =
282             pacesetterSelectorPtr()->supportsAppFrameRateOverrideByContent();
283     return mFrameRateOverrideMappings
284             .getFrameRateOverrideForUid(uid, supportsFrameRateOverrideByContent);
285 }
286 
isVsyncValid(TimePoint expectedVsyncTime,uid_t uid) const287 bool Scheduler::isVsyncValid(TimePoint expectedVsyncTime, uid_t uid) const {
288     const auto frameRate = getFrameRateOverride(uid);
289     if (!frameRate.has_value()) {
290         return true;
291     }
292 
293     ATRACE_FORMAT("%s uid: %d frameRate: %s", __func__, uid, to_string(*frameRate).c_str());
294     return getVsyncSchedule()->getTracker().isVSyncInPhase(expectedVsyncTime.ns(), *frameRate);
295 }
296 
isVsyncInPhase(TimePoint expectedVsyncTime,Fps frameRate) const297 bool Scheduler::isVsyncInPhase(TimePoint expectedVsyncTime, Fps frameRate) const {
298     return getVsyncSchedule()->getTracker().isVSyncInPhase(expectedVsyncTime.ns(), frameRate);
299 }
300 
throttleVsync(android::TimePoint expectedPresentTime,uid_t uid)301 bool Scheduler::throttleVsync(android::TimePoint expectedPresentTime, uid_t uid) {
302     return !isVsyncValid(expectedPresentTime, uid);
303 }
304 
getVsyncPeriod(uid_t uid)305 Period Scheduler::getVsyncPeriod(uid_t uid) {
306     const auto [refreshRate, period] = [this] {
307         std::scoped_lock lock(mDisplayLock);
308         const auto pacesetterOpt = pacesetterDisplayLocked();
309         LOG_ALWAYS_FATAL_IF(!pacesetterOpt);
310         const Display& pacesetter = *pacesetterOpt;
311         const FrameRateMode& frameRateMode = pacesetter.selectorPtr->getActiveMode();
312         const auto refreshRate = frameRateMode.fps;
313         const auto displayVsync = frameRateMode.modePtr->getVsyncRate();
314         const auto numPeriod = RefreshRateSelector::getFrameRateDivisor(displayVsync, refreshRate);
315         return std::make_pair(refreshRate, numPeriod * pacesetter.schedulePtr->period());
316     }();
317 
318     const Period currentPeriod = period != Period::zero() ? period : refreshRate.getPeriod();
319 
320     const auto frameRate = getFrameRateOverride(uid);
321     if (!frameRate.has_value()) {
322         return currentPeriod;
323     }
324 
325     const auto divisor = RefreshRateSelector::getFrameRateDivisor(refreshRate, *frameRate);
326     if (divisor <= 1) {
327         return currentPeriod;
328     }
329 
330     // TODO(b/299378819): the casting is not needed, but we need a flag as it might change
331     // behaviour.
332     return Period::fromNs(currentPeriod.ns() * divisor);
333 }
onExpectedPresentTimePosted(TimePoint expectedPresentTime)334 void Scheduler::onExpectedPresentTimePosted(TimePoint expectedPresentTime) {
335     const auto frameRateMode = [this] {
336         std::scoped_lock lock(mDisplayLock);
337         const auto pacesetterOpt = pacesetterDisplayLocked();
338         const Display& pacesetter = *pacesetterOpt;
339         return pacesetter.selectorPtr->getActiveMode();
340     }();
341 
342     if (frameRateMode.modePtr->getVrrConfig()) {
343         mSchedulerCallback.onExpectedPresentTimePosted(expectedPresentTime, frameRateMode.modePtr,
344                                                        frameRateMode.fps);
345     }
346 }
347 
createEventThread(Cycle cycle,frametimeline::TokenManager * tokenManager,std::chrono::nanoseconds workDuration,std::chrono::nanoseconds readyDuration)348 void Scheduler::createEventThread(Cycle cycle, frametimeline::TokenManager* tokenManager,
349                                   std::chrono::nanoseconds workDuration,
350                                   std::chrono::nanoseconds readyDuration) {
351     auto eventThread =
352             std::make_unique<android::impl::EventThread>(cycle == Cycle::Render ? "app" : "appSf",
353                                                          getVsyncSchedule(), tokenManager, *this,
354                                                          workDuration, readyDuration);
355 
356     if (cycle == Cycle::Render) {
357         mRenderEventThread = std::move(eventThread);
358         mRenderEventConnection = mRenderEventThread->createEventConnection();
359     } else {
360         mLastCompositeEventThread = std::move(eventThread);
361         mLastCompositeEventConnection = mLastCompositeEventThread->createEventConnection();
362     }
363 }
364 
createDisplayEventConnection(Cycle cycle,EventRegistrationFlags eventRegistration,const sp<IBinder> & layerHandle)365 sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
366         Cycle cycle, EventRegistrationFlags eventRegistration, const sp<IBinder>& layerHandle) {
367     const auto connection = eventThreadFor(cycle).createEventConnection(eventRegistration);
368     const auto layerId = static_cast<int32_t>(LayerHandle::getLayerId(layerHandle));
369 
370     if (layerId != static_cast<int32_t>(UNASSIGNED_LAYER_ID)) {
371         // TODO(b/290409668): Moving the choreographer attachment to be a transaction that will be
372         // processed on the main thread.
373         mSchedulerCallback.onChoreographerAttached();
374 
375         std::scoped_lock lock(mChoreographerLock);
376         const auto [iter, emplaced] =
377                 mAttachedChoreographers.emplace(layerId,
378                                                 AttachedChoreographers{Fps(), {connection}});
379         if (!emplaced) {
380             iter->second.connections.emplace(connection);
381             connection->frameRate = iter->second.frameRate;
382         }
383     }
384     return connection;
385 }
386 
dispatchHotplug(PhysicalDisplayId displayId,Hotplug hotplug)387 void Scheduler::dispatchHotplug(PhysicalDisplayId displayId, Hotplug hotplug) {
388     if (hasEventThreads()) {
389         const bool connected = hotplug == Hotplug::Connected;
390         eventThreadFor(Cycle::Render).onHotplugReceived(displayId, connected);
391         eventThreadFor(Cycle::LastComposite).onHotplugReceived(displayId, connected);
392     }
393 }
394 
dispatchHotplugError(int32_t errorCode)395 void Scheduler::dispatchHotplugError(int32_t errorCode) {
396     if (hasEventThreads()) {
397         eventThreadFor(Cycle::Render).onHotplugConnectionError(errorCode);
398         eventThreadFor(Cycle::LastComposite).onHotplugConnectionError(errorCode);
399     }
400 }
401 
enableSyntheticVsync(bool enable)402 void Scheduler::enableSyntheticVsync(bool enable) {
403     eventThreadFor(Cycle::Render).enableSyntheticVsync(enable);
404 }
405 
onFrameRateOverridesChanged(Cycle cycle,PhysicalDisplayId displayId)406 void Scheduler::onFrameRateOverridesChanged(Cycle cycle, PhysicalDisplayId displayId) {
407     const bool supportsFrameRateOverrideByContent =
408             pacesetterSelectorPtr()->supportsAppFrameRateOverrideByContent();
409 
410     std::vector<FrameRateOverride> overrides =
411             mFrameRateOverrideMappings.getAllFrameRateOverrides(supportsFrameRateOverrideByContent);
412 
413     eventThreadFor(cycle).onFrameRateOverridesChanged(displayId, std::move(overrides));
414 }
415 
onHdcpLevelsChanged(Cycle cycle,PhysicalDisplayId displayId,int32_t connectedLevel,int32_t maxLevel)416 void Scheduler::onHdcpLevelsChanged(Cycle cycle, PhysicalDisplayId displayId,
417                                     int32_t connectedLevel, int32_t maxLevel) {
418     eventThreadFor(cycle).onHdcpLevelsChanged(displayId, connectedLevel, maxLevel);
419 }
420 
onPrimaryDisplayModeChanged(Cycle cycle,const FrameRateMode & mode)421 void Scheduler::onPrimaryDisplayModeChanged(Cycle cycle, const FrameRateMode& mode) {
422     {
423         std::lock_guard<std::mutex> lock(mPolicyLock);
424         // Cache the last reported modes for primary display.
425         mPolicy.cachedModeChangedParams = {cycle, mode};
426 
427         // Invalidate content based refresh rate selection so it could be calculated
428         // again for the new refresh rate.
429         mPolicy.contentRequirements.clear();
430     }
431     onNonPrimaryDisplayModeChanged(cycle, mode);
432 }
433 
dispatchCachedReportedMode()434 void Scheduler::dispatchCachedReportedMode() {
435     // Check optional fields first.
436     if (!mPolicy.modeOpt) {
437         ALOGW("No mode ID found, not dispatching cached mode.");
438         return;
439     }
440     if (!mPolicy.cachedModeChangedParams) {
441         ALOGW("No mode changed params found, not dispatching cached mode.");
442         return;
443     }
444 
445     // If the mode is not the current mode, this means that a
446     // mode change is in progress. In that case we shouldn't dispatch an event
447     // as it will be dispatched when the current mode changes.
448     if (pacesetterSelectorPtr()->getActiveMode() != mPolicy.modeOpt) {
449         return;
450     }
451 
452     // If there is no change from cached mode, there is no need to dispatch an event
453     if (*mPolicy.modeOpt == mPolicy.cachedModeChangedParams->mode) {
454         return;
455     }
456 
457     mPolicy.cachedModeChangedParams->mode = *mPolicy.modeOpt;
458     onNonPrimaryDisplayModeChanged(mPolicy.cachedModeChangedParams->cycle,
459                                    mPolicy.cachedModeChangedParams->mode);
460 }
461 
onNonPrimaryDisplayModeChanged(Cycle cycle,const FrameRateMode & mode)462 void Scheduler::onNonPrimaryDisplayModeChanged(Cycle cycle, const FrameRateMode& mode) {
463     if (hasEventThreads()) {
464         eventThreadFor(cycle).onModeChanged(mode);
465     }
466 }
467 
dump(Cycle cycle,std::string & result) const468 void Scheduler::dump(Cycle cycle, std::string& result) const {
469     eventThreadFor(cycle).dump(result);
470 }
471 
setDuration(Cycle cycle,std::chrono::nanoseconds workDuration,std::chrono::nanoseconds readyDuration)472 void Scheduler::setDuration(Cycle cycle, std::chrono::nanoseconds workDuration,
473                             std::chrono::nanoseconds readyDuration) {
474     if (hasEventThreads()) {
475         eventThreadFor(cycle).setDuration(workDuration, readyDuration);
476     }
477 }
478 
updatePhaseConfiguration(Fps refreshRate)479 void Scheduler::updatePhaseConfiguration(Fps refreshRate) {
480     mRefreshRateStats->setRefreshRate(refreshRate);
481     mVsyncConfiguration->setRefreshRateFps(refreshRate);
482     setVsyncConfig(mVsyncModulator->setVsyncConfigSet(mVsyncConfiguration->getCurrentConfigs()),
483                    refreshRate.getPeriod());
484 }
485 
resetPhaseConfiguration(Fps refreshRate)486 void Scheduler::resetPhaseConfiguration(Fps refreshRate) {
487     // Cancel the pending refresh rate change, if any, before updating the phase configuration.
488     mVsyncModulator->cancelRefreshRateChange();
489 
490     mVsyncConfiguration->reset();
491     updatePhaseConfiguration(refreshRate);
492 }
493 
setActiveDisplayPowerModeForRefreshRateStats(hal::PowerMode powerMode)494 void Scheduler::setActiveDisplayPowerModeForRefreshRateStats(hal::PowerMode powerMode) {
495     mRefreshRateStats->setPowerMode(powerMode);
496 }
497 
setVsyncConfig(const VsyncConfig & config,Period vsyncPeriod)498 void Scheduler::setVsyncConfig(const VsyncConfig& config, Period vsyncPeriod) {
499     setDuration(Cycle::Render,
500                 /* workDuration */ config.appWorkDuration,
501                 /* readyDuration */ config.sfWorkDuration);
502     setDuration(Cycle::LastComposite,
503                 /* workDuration */ vsyncPeriod,
504                 /* readyDuration */ config.sfWorkDuration);
505     setDuration(config.sfWorkDuration);
506 }
507 
enableHardwareVsync(PhysicalDisplayId id)508 void Scheduler::enableHardwareVsync(PhysicalDisplayId id) {
509     auto schedule = getVsyncSchedule(id);
510     LOG_ALWAYS_FATAL_IF(!schedule);
511     schedule->enableHardwareVsync();
512 }
513 
disableHardwareVsync(PhysicalDisplayId id,bool disallow)514 void Scheduler::disableHardwareVsync(PhysicalDisplayId id, bool disallow) {
515     auto schedule = getVsyncSchedule(id);
516     LOG_ALWAYS_FATAL_IF(!schedule);
517     schedule->disableHardwareVsync(disallow);
518 }
519 
resyncAllToHardwareVsync(bool allowToEnable)520 void Scheduler::resyncAllToHardwareVsync(bool allowToEnable) {
521     ATRACE_CALL();
522     std::scoped_lock lock(mDisplayLock);
523     ftl::FakeGuard guard(kMainThreadContext);
524 
525     for (const auto& [id, display] : mDisplays) {
526         if (display.powerMode != hal::PowerMode::OFF ||
527             !FlagManager::getInstance().multithreaded_present()) {
528             resyncToHardwareVsyncLocked(id, allowToEnable);
529         }
530     }
531 }
532 
resyncToHardwareVsyncLocked(PhysicalDisplayId id,bool allowToEnable,DisplayModePtr modePtr)533 void Scheduler::resyncToHardwareVsyncLocked(PhysicalDisplayId id, bool allowToEnable,
534                                             DisplayModePtr modePtr) {
535     const auto displayOpt = mDisplays.get(id);
536     if (!displayOpt) {
537         ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
538         return;
539     }
540     const Display& display = *displayOpt;
541 
542     if (display.schedulePtr->isHardwareVsyncAllowed(allowToEnable)) {
543         if (!modePtr) {
544             modePtr = display.selectorPtr->getActiveMode().modePtr.get();
545         }
546         if (modePtr->getVsyncRate().isValid()) {
547             constexpr bool kForce = false;
548             display.schedulePtr->onDisplayModeChanged(ftl::as_non_null(modePtr), kForce);
549         }
550     }
551 }
552 
onHardwareVsyncRequest(PhysicalDisplayId id,bool enabled)553 void Scheduler::onHardwareVsyncRequest(PhysicalDisplayId id, bool enabled) {
554     static const auto& whence = __func__;
555     ATRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str());
556 
557     // On main thread to serialize reads/writes of pending hardware VSYNC state.
558     static_cast<void>(
559             schedule([=, this]() FTL_FAKE_GUARD(mDisplayLock) FTL_FAKE_GUARD(kMainThreadContext) {
560                 ATRACE_NAME(ftl::Concat(whence, ' ', id.value, ' ', enabled).c_str());
561 
562                 if (const auto displayOpt = mDisplays.get(id)) {
563                     auto& display = displayOpt->get();
564                     display.schedulePtr->setPendingHardwareVsyncState(enabled);
565 
566                     if (display.powerMode != hal::PowerMode::OFF) {
567                         mSchedulerCallback.requestHardwareVsync(id, enabled);
568                     }
569                 }
570             }));
571 }
572 
setRenderRate(PhysicalDisplayId id,Fps renderFrameRate,bool applyImmediately)573 void Scheduler::setRenderRate(PhysicalDisplayId id, Fps renderFrameRate, bool applyImmediately) {
574     std::scoped_lock lock(mDisplayLock);
575     ftl::FakeGuard guard(kMainThreadContext);
576 
577     const auto displayOpt = mDisplays.get(id);
578     if (!displayOpt) {
579         ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
580         return;
581     }
582     const Display& display = *displayOpt;
583     const auto mode = display.selectorPtr->getActiveMode();
584 
585     using fps_approx_ops::operator!=;
586     LOG_ALWAYS_FATAL_IF(renderFrameRate != mode.fps,
587                         "Mismatch in render frame rates. Selector: %s, Scheduler: %s, Display: "
588                         "%" PRIu64,
589                         to_string(mode.fps).c_str(), to_string(renderFrameRate).c_str(), id.value);
590 
591     ALOGV("%s %s (%s)", __func__, to_string(mode.fps).c_str(),
592           to_string(mode.modePtr->getVsyncRate()).c_str());
593 
594     display.schedulePtr->getTracker().setRenderRate(renderFrameRate, applyImmediately);
595 }
596 
getNextFrameInterval(PhysicalDisplayId id,TimePoint currentExpectedPresentTime) const597 Fps Scheduler::getNextFrameInterval(PhysicalDisplayId id,
598                                     TimePoint currentExpectedPresentTime) const {
599     std::scoped_lock lock(mDisplayLock);
600     ftl::FakeGuard guard(kMainThreadContext);
601 
602     const auto displayOpt = mDisplays.get(id);
603     if (!displayOpt) {
604         ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
605         return Fps{};
606     }
607     const Display& display = *displayOpt;
608     const Duration threshold =
609             display.selectorPtr->getActiveMode().modePtr->getVsyncRate().getPeriod() / 2;
610     const TimePoint nextVsyncTime =
611             display.schedulePtr->vsyncDeadlineAfter(currentExpectedPresentTime + threshold,
612                                                     currentExpectedPresentTime);
613     const Duration frameInterval = nextVsyncTime - currentExpectedPresentTime;
614     return Fps::fromPeriodNsecs(frameInterval.ns());
615 }
616 
resync()617 void Scheduler::resync() {
618     static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
619 
620     const nsecs_t now = systemTime();
621     const nsecs_t last = mLastResyncTime.exchange(now);
622 
623     if (now - last > kIgnoreDelay) {
624         resyncAllToHardwareVsync(false /* allowToEnable */);
625     }
626 }
627 
addResyncSample(PhysicalDisplayId id,nsecs_t timestamp,std::optional<nsecs_t> hwcVsyncPeriodIn)628 bool Scheduler::addResyncSample(PhysicalDisplayId id, nsecs_t timestamp,
629                                 std::optional<nsecs_t> hwcVsyncPeriodIn) {
630     const auto hwcVsyncPeriod = ftl::Optional(hwcVsyncPeriodIn).transform([](nsecs_t nanos) {
631         return Period::fromNs(nanos);
632     });
633     auto schedule = getVsyncSchedule(id);
634     if (!schedule) {
635         ALOGW("%s: Invalid display %s!", __func__, to_string(id).c_str());
636         return false;
637     }
638     return schedule->addResyncSample(TimePoint::fromNs(timestamp), hwcVsyncPeriod);
639 }
640 
addPresentFence(PhysicalDisplayId id,std::shared_ptr<FenceTime> fence)641 void Scheduler::addPresentFence(PhysicalDisplayId id, std::shared_ptr<FenceTime> fence) {
642     ATRACE_NAME(ftl::Concat(__func__, ' ', id.value).c_str());
643     const auto scheduleOpt =
644             (ftl::FakeGuard(mDisplayLock), mDisplays.get(id)).and_then([](const Display& display) {
645                 return display.powerMode == hal::PowerMode::OFF
646                         ? std::nullopt
647                         : std::make_optional(display.schedulePtr);
648             });
649 
650     if (!scheduleOpt) return;
651     const auto& schedule = scheduleOpt->get();
652 
653     const bool needMoreSignals = schedule->getController().addPresentFence(std::move(fence));
654     if (needMoreSignals) {
655         schedule->enableHardwareVsync();
656     } else {
657         constexpr bool kDisallow = false;
658         schedule->disableHardwareVsync(kDisallow);
659     }
660 }
661 
registerLayer(Layer * layer)662 void Scheduler::registerLayer(Layer* layer) {
663     // If the content detection feature is off, we still keep the layer history,
664     // since we use it for other features (like Frame Rate API), so layers
665     // still need to be registered.
666     mLayerHistory.registerLayer(layer, mFeatures.test(Feature::kContentDetection));
667 }
668 
deregisterLayer(Layer * layer)669 void Scheduler::deregisterLayer(Layer* layer) {
670     mLayerHistory.deregisterLayer(layer);
671 }
672 
onLayerDestroyed(Layer * layer)673 void Scheduler::onLayerDestroyed(Layer* layer) {
674     std::scoped_lock lock(mChoreographerLock);
675     mAttachedChoreographers.erase(layer->getSequence());
676 }
677 
recordLayerHistory(int32_t id,const LayerProps & layerProps,nsecs_t presentTime,nsecs_t now,LayerHistory::LayerUpdateType updateType)678 void Scheduler::recordLayerHistory(int32_t id, const LayerProps& layerProps, nsecs_t presentTime,
679                                    nsecs_t now, LayerHistory::LayerUpdateType updateType) {
680     if (pacesetterSelectorPtr()->canSwitch()) {
681         mLayerHistory.record(id, layerProps, presentTime, now, updateType);
682     }
683 }
684 
setModeChangePending(bool pending)685 void Scheduler::setModeChangePending(bool pending) {
686     mLayerHistory.setModeChangePending(pending);
687 }
688 
setDefaultFrameRateCompatibility(int32_t id,scheduler::FrameRateCompatibility frameRateCompatibility)689 void Scheduler::setDefaultFrameRateCompatibility(
690         int32_t id, scheduler::FrameRateCompatibility frameRateCompatibility) {
691     mLayerHistory.setDefaultFrameRateCompatibility(id, frameRateCompatibility,
692                                                    mFeatures.test(Feature::kContentDetection));
693 }
694 
setLayerProperties(int32_t id,const android::scheduler::LayerProps & properties)695 void Scheduler::setLayerProperties(int32_t id, const android::scheduler::LayerProps& properties) {
696     mLayerHistory.setLayerProperties(id, properties);
697 }
698 
chooseRefreshRateForContent(const surfaceflinger::frontend::LayerHierarchy * hierarchy,bool updateAttachedChoreographer)699 void Scheduler::chooseRefreshRateForContent(
700         const surfaceflinger::frontend::LayerHierarchy* hierarchy,
701         bool updateAttachedChoreographer) {
702     const auto selectorPtr = pacesetterSelectorPtr();
703     if (!selectorPtr->canSwitch()) return;
704 
705     ATRACE_CALL();
706 
707     LayerHistory::Summary summary = mLayerHistory.summarize(*selectorPtr, systemTime());
708     applyPolicy(&Policy::contentRequirements, std::move(summary));
709 
710     if (updateAttachedChoreographer) {
711         LOG_ALWAYS_FATAL_IF(!hierarchy);
712 
713         // update the attached choreographers after we selected the render rate.
714         const ftl::Optional<FrameRateMode> modeOpt = [&] {
715             std::scoped_lock lock(mPolicyLock);
716             return mPolicy.modeOpt;
717         }();
718 
719         if (modeOpt) {
720             updateAttachedChoreographers(*hierarchy, modeOpt->fps);
721         }
722     }
723 }
724 
resetIdleTimer()725 void Scheduler::resetIdleTimer() {
726     pacesetterSelectorPtr()->resetIdleTimer();
727 }
728 
onTouchHint()729 void Scheduler::onTouchHint() {
730     if (mTouchTimer) {
731         mTouchTimer->reset();
732         pacesetterSelectorPtr()->resetKernelIdleTimer();
733     }
734 }
735 
setDisplayPowerMode(PhysicalDisplayId id,hal::PowerMode powerMode)736 void Scheduler::setDisplayPowerMode(PhysicalDisplayId id, hal::PowerMode powerMode) {
737     const bool isPacesetter = [this, id]() REQUIRES(kMainThreadContext) {
738         ftl::FakeGuard guard(mDisplayLock);
739         return id == mPacesetterDisplayId;
740     }();
741     if (isPacesetter) {
742         // TODO (b/255657128): This needs to be handled per display.
743         std::lock_guard<std::mutex> lock(mPolicyLock);
744         mPolicy.displayPowerMode = powerMode;
745     }
746     {
747         std::scoped_lock lock(mDisplayLock);
748 
749         const auto displayOpt = mDisplays.get(id);
750         LOG_ALWAYS_FATAL_IF(!displayOpt);
751         auto& display = displayOpt->get();
752 
753         display.powerMode = powerMode;
754         display.schedulePtr->getController().setDisplayPowerMode(powerMode);
755     }
756     if (!isPacesetter) return;
757 
758     if (mDisplayPowerTimer) {
759         mDisplayPowerTimer->reset();
760     }
761 
762     // Display Power event will boost the refresh rate to performance.
763     // Clear Layer History to get fresh FPS detection
764     mLayerHistory.clear();
765 }
766 
getVsyncSchedule(std::optional<PhysicalDisplayId> idOpt) const767 auto Scheduler::getVsyncSchedule(std::optional<PhysicalDisplayId> idOpt) const
768         -> ConstVsyncSchedulePtr {
769     std::scoped_lock lock(mDisplayLock);
770     return getVsyncScheduleLocked(idOpt);
771 }
772 
getVsyncScheduleLocked(std::optional<PhysicalDisplayId> idOpt) const773 auto Scheduler::getVsyncScheduleLocked(std::optional<PhysicalDisplayId> idOpt) const
774         -> ConstVsyncSchedulePtr {
775     ftl::FakeGuard guard(kMainThreadContext);
776 
777     if (!idOpt) {
778         LOG_ALWAYS_FATAL_IF(!mPacesetterDisplayId, "Missing a pacesetter!");
779         idOpt = mPacesetterDisplayId;
780     }
781 
782     const auto displayOpt = mDisplays.get(*idOpt);
783     if (!displayOpt) {
784         return nullptr;
785     }
786     return displayOpt->get().schedulePtr;
787 }
788 
kernelIdleTimerCallback(TimerState state)789 void Scheduler::kernelIdleTimerCallback(TimerState state) {
790     ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
791 
792     // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
793     // magic number
794     const Fps refreshRate = pacesetterSelectorPtr()->getActiveMode().modePtr->getPeakFps();
795 
796     constexpr Fps FPS_THRESHOLD_FOR_KERNEL_TIMER = 65_Hz;
797     using namespace fps_approx_ops;
798 
799     if (state == TimerState::Reset && refreshRate > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
800         // If we're not in performance mode then the kernel timer shouldn't do
801         // anything, as the refresh rate during DPU power collapse will be the
802         // same.
803         resyncAllToHardwareVsync(true /* allowToEnable */);
804     } else if (state == TimerState::Expired && refreshRate <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
805         // Disable HW VSYNC if the timer expired, as we don't need it enabled if
806         // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
807         // need to update the VsyncController model anyway.
808         std::scoped_lock lock(mDisplayLock);
809         ftl::FakeGuard guard(kMainThreadContext);
810         for (const auto& [_, display] : mDisplays) {
811             constexpr bool kDisallow = false;
812             display.schedulePtr->disableHardwareVsync(kDisallow);
813         }
814     }
815 
816     mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
817 }
818 
idleTimerCallback(TimerState state)819 void Scheduler::idleTimerCallback(TimerState state) {
820     applyPolicy(&Policy::idleTimer, state);
821     ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
822 }
823 
touchTimerCallback(TimerState state)824 void Scheduler::touchTimerCallback(TimerState state) {
825     const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
826     // Touch event will boost the refresh rate to performance.
827     // Clear layer history to get fresh FPS detection.
828     // NOTE: Instead of checking all the layers, we should be checking the layer
829     // that is currently on top. b/142507166 will give us this capability.
830     if (applyPolicy(&Policy::touch, touch).touch) {
831         mLayerHistory.clear();
832     }
833     ATRACE_INT("TouchState", static_cast<int>(touch));
834 }
835 
displayPowerTimerCallback(TimerState state)836 void Scheduler::displayPowerTimerCallback(TimerState state) {
837     applyPolicy(&Policy::displayPowerTimer, state);
838     ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
839 }
840 
dump(utils::Dumper & dumper) const841 void Scheduler::dump(utils::Dumper& dumper) const {
842     using namespace std::string_view_literals;
843 
844     {
845         utils::Dumper::Section section(dumper, "Features"sv);
846 
847         for (Feature feature : ftl::enum_range<Feature>()) {
848             if (const auto flagOpt = ftl::flag_name(feature)) {
849                 dumper.dump(flagOpt->substr(1), mFeatures.test(feature));
850             }
851         }
852     }
853     {
854         utils::Dumper::Section section(dumper, "Policy"sv);
855         {
856             std::scoped_lock lock(mDisplayLock);
857             ftl::FakeGuard guard(kMainThreadContext);
858             dumper.dump("pacesetterDisplayId"sv, mPacesetterDisplayId);
859         }
860         dumper.dump("layerHistory"sv, mLayerHistory.dump());
861         dumper.dump("touchTimer"sv, mTouchTimer.transform(&OneShotTimer::interval));
862         dumper.dump("displayPowerTimer"sv, mDisplayPowerTimer.transform(&OneShotTimer::interval));
863     }
864 
865     mFrameRateOverrideMappings.dump(dumper);
866     dumper.eol();
867 
868     mVsyncConfiguration->dump(dumper.out());
869     dumper.eol();
870 
871     mRefreshRateStats->dump(dumper.out());
872     dumper.eol();
873 
874     {
875         utils::Dumper::Section section(dumper, "Frame Targeting"sv);
876 
877         std::scoped_lock lock(mDisplayLock);
878         ftl::FakeGuard guard(kMainThreadContext);
879 
880         for (const auto& [id, display] : mDisplays) {
881             utils::Dumper::Section
882                     section(dumper,
883                             id == mPacesetterDisplayId
884                                     ? ftl::Concat("Pacesetter Display ", id.value).c_str()
885                                     : ftl::Concat("Follower Display ", id.value).c_str());
886 
887             display.targeterPtr->dump(dumper);
888             dumper.eol();
889         }
890     }
891 }
892 
dumpVsync(std::string & out) const893 void Scheduler::dumpVsync(std::string& out) const {
894     std::scoped_lock lock(mDisplayLock);
895     ftl::FakeGuard guard(kMainThreadContext);
896     if (mPacesetterDisplayId) {
897         base::StringAppendF(&out, "VsyncSchedule for pacesetter %s:\n",
898                             to_string(*mPacesetterDisplayId).c_str());
899         getVsyncScheduleLocked()->dump(out);
900     }
901     for (auto& [id, display] : mDisplays) {
902         if (id == mPacesetterDisplayId) {
903             continue;
904         }
905         base::StringAppendF(&out, "VsyncSchedule for follower %s:\n", to_string(id).c_str());
906         display.schedulePtr->dump(out);
907     }
908 }
909 
updateFrameRateOverrides(GlobalSignals consideredSignals,Fps displayRefreshRate)910 bool Scheduler::updateFrameRateOverrides(GlobalSignals consideredSignals, Fps displayRefreshRate) {
911     std::scoped_lock lock(mPolicyLock);
912     return updateFrameRateOverridesLocked(consideredSignals, displayRefreshRate);
913 }
914 
updateFrameRateOverridesLocked(GlobalSignals consideredSignals,Fps displayRefreshRate)915 bool Scheduler::updateFrameRateOverridesLocked(GlobalSignals consideredSignals,
916                                                Fps displayRefreshRate) {
917     if (consideredSignals.idle) return false;
918 
919     const auto frameRateOverrides =
920             pacesetterSelectorPtr()->getFrameRateOverrides(mPolicy.contentRequirements,
921                                                            displayRefreshRate, consideredSignals);
922 
923     // Note that RefreshRateSelector::supportsFrameRateOverrideByContent is checked when querying
924     // the FrameRateOverrideMappings rather than here.
925     return mFrameRateOverrideMappings.updateFrameRateOverridesByContent(frameRateOverrides);
926 }
927 
promotePacesetterDisplay(PhysicalDisplayId pacesetterId,PromotionParams params)928 void Scheduler::promotePacesetterDisplay(PhysicalDisplayId pacesetterId, PromotionParams params) {
929     std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule;
930     {
931         std::scoped_lock lock(mDisplayLock);
932         pacesetterVsyncSchedule = promotePacesetterDisplayLocked(pacesetterId, params);
933     }
934 
935     applyNewVsyncSchedule(std::move(pacesetterVsyncSchedule));
936 }
937 
promotePacesetterDisplayLocked(PhysicalDisplayId pacesetterId,PromotionParams params)938 std::shared_ptr<VsyncSchedule> Scheduler::promotePacesetterDisplayLocked(
939         PhysicalDisplayId pacesetterId, PromotionParams params) {
940     // TODO: b/241286431 - Choose the pacesetter among mDisplays.
941     mPacesetterDisplayId = pacesetterId;
942     ALOGI("Display %s is the pacesetter", to_string(pacesetterId).c_str());
943 
944     std::shared_ptr<VsyncSchedule> newVsyncSchedulePtr;
945     if (const auto pacesetterOpt = pacesetterDisplayLocked()) {
946         const Display& pacesetter = *pacesetterOpt;
947 
948         if (!FlagManager::getInstance().connected_display() || params.toggleIdleTimer) {
949             pacesetter.selectorPtr->setIdleTimerCallbacks(
950                     {.platform = {.onReset = [this] { idleTimerCallback(TimerState::Reset); },
951                                   .onExpired = [this] { idleTimerCallback(TimerState::Expired); }},
952                      .kernel = {.onReset = [this] { kernelIdleTimerCallback(TimerState::Reset); },
953                                 .onExpired =
954                                         [this] { kernelIdleTimerCallback(TimerState::Expired); }},
955                      .vrr = {.onReset = [this] { mSchedulerCallback.vrrDisplayIdle(false); },
956                              .onExpired = [this] { mSchedulerCallback.vrrDisplayIdle(true); }}});
957 
958             pacesetter.selectorPtr->startIdleTimer();
959         }
960 
961         newVsyncSchedulePtr = pacesetter.schedulePtr;
962 
963         constexpr bool kForce = true;
964         newVsyncSchedulePtr->onDisplayModeChanged(pacesetter.selectorPtr->getActiveMode().modePtr,
965                                                   kForce);
966     }
967     return newVsyncSchedulePtr;
968 }
969 
applyNewVsyncSchedule(std::shared_ptr<VsyncSchedule> vsyncSchedule)970 void Scheduler::applyNewVsyncSchedule(std::shared_ptr<VsyncSchedule> vsyncSchedule) {
971     onNewVsyncSchedule(vsyncSchedule->getDispatch());
972 
973     if (hasEventThreads()) {
974         eventThreadFor(Cycle::Render).onNewVsyncSchedule(vsyncSchedule);
975         eventThreadFor(Cycle::LastComposite).onNewVsyncSchedule(vsyncSchedule);
976     }
977 }
978 
demotePacesetterDisplay(PromotionParams params)979 void Scheduler::demotePacesetterDisplay(PromotionParams params) {
980     if (!FlagManager::getInstance().connected_display() || params.toggleIdleTimer) {
981         // No need to lock for reads on kMainThreadContext.
982         if (const auto pacesetterPtr =
983                     FTL_FAKE_GUARD(mDisplayLock, pacesetterSelectorPtrLocked())) {
984             pacesetterPtr->stopIdleTimer();
985             pacesetterPtr->clearIdleTimerCallbacks();
986         }
987     }
988 
989     // Clear state that depends on the pacesetter's RefreshRateSelector.
990     std::scoped_lock lock(mPolicyLock);
991     mPolicy = {};
992 }
993 
updateAttachedChoreographersFrameRate(const surfaceflinger::frontend::RequestedLayerState & layer,Fps fps)994 void Scheduler::updateAttachedChoreographersFrameRate(
995         const surfaceflinger::frontend::RequestedLayerState& layer, Fps fps) {
996     std::scoped_lock lock(mChoreographerLock);
997 
998     const auto layerId = static_cast<int32_t>(layer.id);
999     const auto choreographers = mAttachedChoreographers.find(layerId);
1000     if (choreographers == mAttachedChoreographers.end()) {
1001         return;
1002     }
1003 
1004     auto& layerChoreographers = choreographers->second;
1005 
1006     layerChoreographers.frameRate = fps;
1007     ATRACE_FORMAT_INSTANT("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str());
1008     ALOGV("%s: %s for %s", __func__, to_string(fps).c_str(), layer.name.c_str());
1009 
1010     auto it = layerChoreographers.connections.begin();
1011     while (it != layerChoreographers.connections.end()) {
1012         sp<EventThreadConnection> choreographerConnection = it->promote();
1013         if (choreographerConnection) {
1014             choreographerConnection->frameRate = fps;
1015             it++;
1016         } else {
1017             it = choreographers->second.connections.erase(it);
1018         }
1019     }
1020 
1021     if (layerChoreographers.connections.empty()) {
1022         mAttachedChoreographers.erase(choreographers);
1023     }
1024 }
1025 
updateAttachedChoreographersInternal(const surfaceflinger::frontend::LayerHierarchy & layerHierarchy,Fps displayRefreshRate,int parentDivisor)1026 int Scheduler::updateAttachedChoreographersInternal(
1027         const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate,
1028         int parentDivisor) {
1029     const char* name = layerHierarchy.getLayer() ? layerHierarchy.getLayer()->name.c_str() : "Root";
1030 
1031     int divisor = 0;
1032     if (layerHierarchy.getLayer()) {
1033         const auto frameRateCompatibility = layerHierarchy.getLayer()->frameRateCompatibility;
1034         const auto frameRate = Fps::fromValue(layerHierarchy.getLayer()->frameRate);
1035         ALOGV("%s: %s frameRate %s parentDivisor=%d", __func__, name, to_string(frameRate).c_str(),
1036               parentDivisor);
1037 
1038         if (frameRate.isValid()) {
1039             if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE ||
1040                 frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_EXACT) {
1041                 // Since this layer wants an exact match, we would only set a frame rate if the
1042                 // desired rate is a divisor of the display refresh rate.
1043                 divisor = RefreshRateSelector::getFrameRateDivisor(displayRefreshRate, frameRate);
1044             } else if (frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT) {
1045                 // find the closest frame rate divisor for the desired frame rate.
1046                 divisor = static_cast<int>(
1047                         std::round(displayRefreshRate.getValue() / frameRate.getValue()));
1048             }
1049         }
1050     }
1051 
1052     // We start by traversing the children, updating their choreographers, and getting back the
1053     // aggregated frame rate.
1054     int childrenDivisor = 0;
1055     for (const auto& [child, _] : layerHierarchy.mChildren) {
1056         LOG_ALWAYS_FATAL_IF(child == nullptr || child->getLayer() == nullptr);
1057 
1058         ALOGV("%s: %s traversing child %s", __func__, name, child->getLayer()->name.c_str());
1059 
1060         const int childDivisor =
1061                 updateAttachedChoreographersInternal(*child, displayRefreshRate, divisor);
1062         childrenDivisor = childrenDivisor > 0 ? childrenDivisor : childDivisor;
1063         if (childDivisor > 0) {
1064             childrenDivisor = std::gcd(childrenDivisor, childDivisor);
1065         }
1066         ALOGV("%s: %s childrenDivisor=%d", __func__, name, childrenDivisor);
1067     }
1068 
1069     ALOGV("%s: %s divisor=%d", __func__, name, divisor);
1070 
1071     // If there is no explicit vote for this layer. Use the children's vote if exists
1072     divisor = (divisor == 0) ? childrenDivisor : divisor;
1073     ALOGV("%s: %s divisor=%d with children", __func__, name, divisor);
1074 
1075     // If there is no explicit vote for this layer or its children, Use the parent vote if exists
1076     divisor = (divisor == 0) ? parentDivisor : divisor;
1077     ALOGV("%s: %s divisor=%d with parent", __func__, name, divisor);
1078 
1079     if (layerHierarchy.getLayer()) {
1080         Fps fps = divisor > 1 ? displayRefreshRate / (unsigned int)divisor : Fps();
1081         updateAttachedChoreographersFrameRate(*layerHierarchy.getLayer(), fps);
1082     }
1083 
1084     return divisor;
1085 }
1086 
updateAttachedChoreographers(const surfaceflinger::frontend::LayerHierarchy & layerHierarchy,Fps displayRefreshRate)1087 void Scheduler::updateAttachedChoreographers(
1088         const surfaceflinger::frontend::LayerHierarchy& layerHierarchy, Fps displayRefreshRate) {
1089     ATRACE_CALL();
1090     updateAttachedChoreographersInternal(layerHierarchy, displayRefreshRate, 0);
1091 }
1092 
1093 template <typename S, typename T>
applyPolicy(S Policy::* statePtr,T && newState)1094 auto Scheduler::applyPolicy(S Policy::*statePtr, T&& newState) -> GlobalSignals {
1095     ATRACE_CALL();
1096     std::vector<display::DisplayModeRequest> modeRequests;
1097     GlobalSignals consideredSignals;
1098 
1099     bool refreshRateChanged = false;
1100     bool frameRateOverridesChanged;
1101 
1102     {
1103         std::scoped_lock lock(mPolicyLock);
1104 
1105         auto& currentState = mPolicy.*statePtr;
1106         if (currentState == newState) return {};
1107         currentState = std::forward<T>(newState);
1108 
1109         DisplayModeChoiceMap modeChoices;
1110         ftl::Optional<FrameRateMode> modeOpt;
1111         {
1112             std::scoped_lock lock(mDisplayLock);
1113             ftl::FakeGuard guard(kMainThreadContext);
1114 
1115             modeChoices = chooseDisplayModes();
1116 
1117             // TODO(b/240743786): The pacesetter display's mode must change for any
1118             // DisplayModeRequest to go through. Fix this by tracking per-display Scheduler::Policy
1119             // and timers.
1120             std::tie(modeOpt, consideredSignals) =
1121                     modeChoices.get(*mPacesetterDisplayId)
1122                             .transform([](const DisplayModeChoice& choice) {
1123                                 return std::make_pair(choice.mode, choice.consideredSignals);
1124                             })
1125                             .value();
1126         }
1127 
1128         modeRequests.reserve(modeChoices.size());
1129         for (auto& [id, choice] : modeChoices) {
1130             modeRequests.emplace_back(
1131                     display::DisplayModeRequest{.mode = std::move(choice.mode),
1132                                                 .emitEvent = !choice.consideredSignals.idle});
1133         }
1134 
1135         frameRateOverridesChanged = updateFrameRateOverridesLocked(consideredSignals, modeOpt->fps);
1136 
1137         if (mPolicy.modeOpt != modeOpt) {
1138             mPolicy.modeOpt = modeOpt;
1139             refreshRateChanged = true;
1140         } else {
1141             // We don't need to change the display mode, but we might need to send an event
1142             // about a mode change, since it was suppressed if previously considered idle.
1143             if (!consideredSignals.idle) {
1144                 dispatchCachedReportedMode();
1145             }
1146         }
1147     }
1148     if (refreshRateChanged) {
1149         mSchedulerCallback.requestDisplayModes(std::move(modeRequests));
1150     }
1151     if (frameRateOverridesChanged) {
1152         mSchedulerCallback.triggerOnFrameRateOverridesChanged();
1153     }
1154     return consideredSignals;
1155 }
1156 
chooseDisplayModes() const1157 auto Scheduler::chooseDisplayModes() const -> DisplayModeChoiceMap {
1158     ATRACE_CALL();
1159 
1160     DisplayModeChoiceMap modeChoices;
1161     const auto globalSignals = makeGlobalSignals();
1162 
1163     const Fps pacesetterFps = [&]() REQUIRES(mPolicyLock, mDisplayLock, kMainThreadContext) {
1164         auto rankedFrameRates =
1165                 pacesetterSelectorPtrLocked()->getRankedFrameRates(mPolicy.contentRequirements,
1166                                                                    globalSignals);
1167 
1168         const Fps pacesetterFps = rankedFrameRates.ranking.front().frameRateMode.fps;
1169 
1170         modeChoices.try_emplace(*mPacesetterDisplayId,
1171                                 DisplayModeChoice::from(std::move(rankedFrameRates)));
1172         return pacesetterFps;
1173     }();
1174 
1175     // Choose a mode for powered-on follower displays.
1176     for (const auto& [id, display] : mDisplays) {
1177         if (id == *mPacesetterDisplayId) continue;
1178         if (display.powerMode != hal::PowerMode::ON) continue;
1179 
1180         auto rankedFrameRates =
1181                 display.selectorPtr->getRankedFrameRates(mPolicy.contentRequirements, globalSignals,
1182                                                          pacesetterFps);
1183 
1184         modeChoices.try_emplace(id, DisplayModeChoice::from(std::move(rankedFrameRates)));
1185     }
1186 
1187     return modeChoices;
1188 }
1189 
makeGlobalSignals() const1190 GlobalSignals Scheduler::makeGlobalSignals() const {
1191     const bool powerOnImminent = mDisplayPowerTimer &&
1192             (mPolicy.displayPowerMode != hal::PowerMode::ON ||
1193              mPolicy.displayPowerTimer == TimerState::Reset);
1194 
1195     return {.touch = mTouchTimer && mPolicy.touch == TouchState::Active,
1196             .idle = mPolicy.idleTimer == TimerState::Expired,
1197             .powerOnImminent = powerOnImminent};
1198 }
1199 
getPreferredDisplayMode()1200 FrameRateMode Scheduler::getPreferredDisplayMode() {
1201     std::lock_guard<std::mutex> lock(mPolicyLock);
1202     const auto frameRateMode =
1203             pacesetterSelectorPtr()
1204                     ->getRankedFrameRates(mPolicy.contentRequirements, makeGlobalSignals())
1205                     .ranking.front()
1206                     .frameRateMode;
1207 
1208     // Make sure the stored mode is up to date.
1209     mPolicy.modeOpt = frameRateMode;
1210 
1211     return frameRateMode;
1212 }
1213 
onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline & timeline)1214 void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
1215     std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
1216     mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
1217 
1218     const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
1219     if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
1220         mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
1221     }
1222 }
1223 
onCompositionPresented(nsecs_t presentTime)1224 bool Scheduler::onCompositionPresented(nsecs_t presentTime) {
1225     std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
1226     if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
1227         if (presentTime < mLastVsyncPeriodChangeTimeline->refreshTimeNanos) {
1228             // We need to composite again as refreshTimeNanos is still in the future.
1229             return true;
1230         }
1231 
1232         mLastVsyncPeriodChangeTimeline->refreshRequired = false;
1233     }
1234     return false;
1235 }
1236 
onActiveDisplayAreaChanged(uint32_t displayArea)1237 void Scheduler::onActiveDisplayAreaChanged(uint32_t displayArea) {
1238     mLayerHistory.setDisplayArea(displayArea);
1239 }
1240 
setGameModeFrameRateForUid(FrameRateOverride frameRateOverride)1241 void Scheduler::setGameModeFrameRateForUid(FrameRateOverride frameRateOverride) {
1242     if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) {
1243         return;
1244     }
1245 
1246     if (FlagManager::getInstance().game_default_frame_rate()) {
1247         // update the frame rate override mapping in LayerHistory
1248         mLayerHistory.updateGameModeFrameRateOverride(frameRateOverride);
1249     } else {
1250         mFrameRateOverrideMappings.setGameModeRefreshRateForUid(frameRateOverride);
1251     }
1252 }
1253 
setGameDefaultFrameRateForUid(FrameRateOverride frameRateOverride)1254 void Scheduler::setGameDefaultFrameRateForUid(FrameRateOverride frameRateOverride) {
1255     if (!FlagManager::getInstance().game_default_frame_rate() ||
1256         (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f)) {
1257         return;
1258     }
1259 
1260     // update the frame rate override mapping in LayerHistory
1261     mLayerHistory.updateGameDefaultFrameRateOverride(frameRateOverride);
1262 }
1263 
setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride)1264 void Scheduler::setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride) {
1265     if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) {
1266         return;
1267     }
1268 
1269     mFrameRateOverrideMappings.setPreferredRefreshRateForUid(frameRateOverride);
1270 }
1271 
updateSmallAreaDetection(std::vector<std::pair<int32_t,float>> & uidThresholdMappings)1272 void Scheduler::updateSmallAreaDetection(
1273         std::vector<std::pair<int32_t, float>>& uidThresholdMappings) {
1274     mSmallAreaDetectionAllowMappings.update(uidThresholdMappings);
1275 }
1276 
setSmallAreaDetectionThreshold(int32_t appId,float threshold)1277 void Scheduler::setSmallAreaDetectionThreshold(int32_t appId, float threshold) {
1278     mSmallAreaDetectionAllowMappings.setThresholdForAppId(appId, threshold);
1279 }
1280 
isSmallDirtyArea(int32_t appId,uint32_t dirtyArea)1281 bool Scheduler::isSmallDirtyArea(int32_t appId, uint32_t dirtyArea) {
1282     std::optional<float> oThreshold = mSmallAreaDetectionAllowMappings.getThresholdForAppId(appId);
1283     if (oThreshold) {
1284         return mLayerHistory.isSmallDirtyArea(dirtyArea, oThreshold.value());
1285     }
1286     return false;
1287 }
1288 
1289 } // namespace android::scheduler
1290