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 <gui/WindowInfo.h>
29 #include <system/window.h>
30 #include <ui/DisplayStatInfo.h>
31 #include <utils/Timers.h>
32 #include <utils/Trace.h>
33 
34 #include <FrameTimeline/FrameTimeline.h>
35 #include <algorithm>
36 #include <cinttypes>
37 #include <cstdint>
38 #include <functional>
39 #include <memory>
40 #include <numeric>
41 
42 #include "../Layer.h"
43 #include "DispSyncSource.h"
44 #include "EventThread.h"
45 #include "InjectVSyncSource.h"
46 #include "OneShotTimer.h"
47 #include "SchedulerUtils.h"
48 #include "SurfaceFlingerProperties.h"
49 #include "Timer.h"
50 #include "VSyncDispatchTimerQueue.h"
51 #include "VSyncPredictor.h"
52 #include "VSyncReactor.h"
53 #include "VsyncController.h"
54 
55 #define RETURN_IF_INVALID_HANDLE(handle, ...)                        \
56     do {                                                             \
57         if (mConnections.count(handle) == 0) {                       \
58             ALOGE("Invalid connection handle %" PRIuPTR, handle.id); \
59             return __VA_ARGS__;                                      \
60         }                                                            \
61     } while (false)
62 
63 using namespace std::string_literals;
64 
65 namespace android {
66 
67 using gui::WindowInfo;
68 
69 namespace {
70 
createVSyncTracker()71 std::unique_ptr<scheduler::VSyncTracker> createVSyncTracker() {
72     // TODO(b/144707443): Tune constants.
73     constexpr int kDefaultRate = 60;
74     constexpr auto initialPeriod = std::chrono::duration<nsecs_t, std::ratio<1, kDefaultRate>>(1);
75     constexpr nsecs_t idealPeriod =
76             std::chrono::duration_cast<std::chrono::nanoseconds>(initialPeriod).count();
77     constexpr size_t vsyncTimestampHistorySize = 20;
78     constexpr size_t minimumSamplesForPrediction = 6;
79     constexpr uint32_t discardOutlierPercent = 20;
80     return std::make_unique<scheduler::VSyncPredictor>(idealPeriod, vsyncTimestampHistorySize,
81                                                        minimumSamplesForPrediction,
82                                                        discardOutlierPercent);
83 }
84 
createVSyncDispatch(scheduler::VSyncTracker & tracker)85 std::unique_ptr<scheduler::VSyncDispatch> createVSyncDispatch(scheduler::VSyncTracker& tracker) {
86     // TODO(b/144707443): Tune constants.
87     constexpr std::chrono::nanoseconds vsyncMoveThreshold = 3ms;
88     constexpr std::chrono::nanoseconds timerSlack = 500us;
89     return std::make_unique<
90             scheduler::VSyncDispatchTimerQueue>(std::make_unique<scheduler::Timer>(), tracker,
91                                                 timerSlack.count(), vsyncMoveThreshold.count());
92 }
93 
toContentDetectionString(bool useContentDetection)94 const char* toContentDetectionString(bool useContentDetection) {
95     return useContentDetection ? "on" : "off";
96 }
97 
98 } // namespace
99 
100 class PredictedVsyncTracer {
101 public:
PredictedVsyncTracer(scheduler::VSyncDispatch & dispatch)102     PredictedVsyncTracer(scheduler::VSyncDispatch& dispatch)
103           : mRegistration(dispatch, std::bind(&PredictedVsyncTracer::callback, this),
104                           "PredictedVsyncTracer") {
105         scheduleRegistration();
106     }
107 
108 private:
109     TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
110     scheduler::VSyncCallbackRegistration mRegistration;
111 
scheduleRegistration()112     void scheduleRegistration() { mRegistration.schedule({0, 0, 0}); }
113 
callback()114     void callback() {
115         mParity = !mParity;
116         scheduleRegistration();
117     }
118 };
119 
Scheduler(const std::shared_ptr<scheduler::RefreshRateConfigs> & configs,ISchedulerCallback & callback)120 Scheduler::Scheduler(const std::shared_ptr<scheduler::RefreshRateConfigs>& configs,
121                      ISchedulerCallback& callback)
122       : Scheduler(configs, callback,
123                   {.useContentDetection = sysprop::use_content_detection_for_refresh_rate(false)}) {
124 }
125 
Scheduler(const std::shared_ptr<scheduler::RefreshRateConfigs> & configs,ISchedulerCallback & callback,Options options)126 Scheduler::Scheduler(const std::shared_ptr<scheduler::RefreshRateConfigs>& configs,
127                      ISchedulerCallback& callback, Options options)
128       : Scheduler(createVsyncSchedule(configs->supportsKernelIdleTimer()), configs, callback,
129                   createLayerHistory(), options) {
130     using namespace sysprop;
131 
132     if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
133         // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
134         mTouchTimer.emplace(
135                 "TouchTimer", std::chrono::milliseconds(millis),
136                 [this] { touchTimerCallback(TimerState::Reset); },
137                 [this] { touchTimerCallback(TimerState::Expired); });
138         mTouchTimer->start();
139     }
140 
141     if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
142         mDisplayPowerTimer.emplace(
143                 "DisplayPowerTimer", std::chrono::milliseconds(millis),
144                 [this] { displayPowerTimerCallback(TimerState::Reset); },
145                 [this] { displayPowerTimerCallback(TimerState::Expired); });
146         mDisplayPowerTimer->start();
147     }
148 }
149 
Scheduler(VsyncSchedule schedule,const std::shared_ptr<scheduler::RefreshRateConfigs> & configs,ISchedulerCallback & schedulerCallback,std::unique_ptr<LayerHistory> layerHistory,Options options)150 Scheduler::Scheduler(VsyncSchedule schedule,
151                      const std::shared_ptr<scheduler::RefreshRateConfigs>& configs,
152                      ISchedulerCallback& schedulerCallback,
153                      std::unique_ptr<LayerHistory> layerHistory, Options options)
154       : mOptions(options),
155         mVsyncSchedule(std::move(schedule)),
156         mLayerHistory(std::move(layerHistory)),
157         mSchedulerCallback(schedulerCallback),
158         mPredictedVsyncTracer(
159                 base::GetBoolProperty("debug.sf.show_predicted_vsync", false)
160                         ? std::make_unique<PredictedVsyncTracer>(*mVsyncSchedule.dispatch)
161                         : nullptr) {
162     setRefreshRateConfigs(configs);
163     mSchedulerCallback.setVsyncEnabled(false);
164 }
165 
~Scheduler()166 Scheduler::~Scheduler() {
167     // Ensure the OneShotTimer threads are joined before we start destroying state.
168     mDisplayPowerTimer.reset();
169     mTouchTimer.reset();
170     mRefreshRateConfigs.reset();
171 }
172 
createVsyncSchedule(bool supportKernelTimer)173 Scheduler::VsyncSchedule Scheduler::createVsyncSchedule(bool supportKernelTimer) {
174     auto clock = std::make_unique<scheduler::SystemClock>();
175     auto tracker = createVSyncTracker();
176     auto dispatch = createVSyncDispatch(*tracker);
177 
178     // TODO(b/144707443): Tune constants.
179     constexpr size_t pendingFenceLimit = 20;
180     auto controller =
181             std::make_unique<scheduler::VSyncReactor>(std::move(clock), *tracker, pendingFenceLimit,
182                                                       supportKernelTimer);
183     return {std::move(controller), std::move(tracker), std::move(dispatch)};
184 }
185 
createLayerHistory()186 std::unique_ptr<LayerHistory> Scheduler::createLayerHistory() {
187     return std::make_unique<scheduler::LayerHistory>();
188 }
189 
makePrimaryDispSyncSource(const char * name,std::chrono::nanoseconds workDuration,std::chrono::nanoseconds readyDuration,bool traceVsync)190 std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(
191         const char* name, std::chrono::nanoseconds workDuration,
192         std::chrono::nanoseconds readyDuration, bool traceVsync) {
193     return std::make_unique<scheduler::DispSyncSource>(*mVsyncSchedule.dispatch, workDuration,
194                                                        readyDuration, traceVsync, name);
195 }
196 
getFrameRateOverride(uid_t uid) const197 std::optional<Fps> Scheduler::getFrameRateOverride(uid_t uid) const {
198     {
199         std::scoped_lock lock(mRefreshRateConfigsLock);
200         if (!mRefreshRateConfigs->supportsFrameRateOverride()) {
201             return std::nullopt;
202         }
203     }
204 
205     std::lock_guard lock(mFrameRateOverridesLock);
206     {
207         const auto iter = mFrameRateOverridesFromBackdoor.find(uid);
208         if (iter != mFrameRateOverridesFromBackdoor.end()) {
209             return std::make_optional<Fps>(iter->second);
210         }
211     }
212 
213     {
214         const auto iter = mFrameRateOverridesByContent.find(uid);
215         if (iter != mFrameRateOverridesByContent.end()) {
216             return std::make_optional<Fps>(iter->second);
217         }
218     }
219 
220     return std::nullopt;
221 }
222 
isVsyncValid(nsecs_t expectedVsyncTimestamp,uid_t uid) const223 bool Scheduler::isVsyncValid(nsecs_t expectedVsyncTimestamp, uid_t uid) const {
224     const auto frameRate = getFrameRateOverride(uid);
225     if (!frameRate.has_value()) {
226         return true;
227     }
228 
229     return mVsyncSchedule.tracker->isVSyncInPhase(expectedVsyncTimestamp, *frameRate);
230 }
231 
makeThrottleVsyncCallback() const232 impl::EventThread::ThrottleVsyncCallback Scheduler::makeThrottleVsyncCallback() const {
233     std::scoped_lock lock(mRefreshRateConfigsLock);
234     if (!mRefreshRateConfigs->supportsFrameRateOverride()) {
235         return {};
236     }
237 
238     return [this](nsecs_t expectedVsyncTimestamp, uid_t uid) {
239         return !isVsyncValid(expectedVsyncTimestamp, uid);
240     };
241 }
242 
makeGetVsyncPeriodFunction() const243 impl::EventThread::GetVsyncPeriodFunction Scheduler::makeGetVsyncPeriodFunction() const {
244     return [this](uid_t uid) {
245         const auto refreshRateConfigs = holdRefreshRateConfigs();
246         nsecs_t basePeriod = refreshRateConfigs->getCurrentRefreshRate().getVsyncPeriod();
247         const auto frameRate = getFrameRateOverride(uid);
248         if (!frameRate.has_value()) {
249             return basePeriod;
250         }
251 
252         const auto divider =
253                 scheduler::RefreshRateConfigs::getFrameRateDivider(refreshRateConfigs
254                                                                            ->getCurrentRefreshRate()
255                                                                            .getFps(),
256                                                                    *frameRate);
257         if (divider <= 1) {
258             return basePeriod;
259         }
260         return basePeriod * divider;
261     };
262 }
263 
createConnection(const char * connectionName,frametimeline::TokenManager * tokenManager,std::chrono::nanoseconds workDuration,std::chrono::nanoseconds readyDuration,impl::EventThread::InterceptVSyncsCallback interceptCallback)264 Scheduler::ConnectionHandle Scheduler::createConnection(
265         const char* connectionName, frametimeline::TokenManager* tokenManager,
266         std::chrono::nanoseconds workDuration, std::chrono::nanoseconds readyDuration,
267         impl::EventThread::InterceptVSyncsCallback interceptCallback) {
268     auto vsyncSource = makePrimaryDispSyncSource(connectionName, workDuration, readyDuration);
269     auto throttleVsync = makeThrottleVsyncCallback();
270     auto getVsyncPeriod = makeGetVsyncPeriodFunction();
271     auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource), tokenManager,
272                                                            std::move(interceptCallback),
273                                                            std::move(throttleVsync),
274                                                            std::move(getVsyncPeriod));
275     return createConnection(std::move(eventThread));
276 }
277 
createConnection(std::unique_ptr<EventThread> eventThread)278 Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
279     const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
280     ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
281 
282     auto connection = createConnectionInternal(eventThread.get());
283 
284     std::lock_guard<std::mutex> lock(mConnectionsLock);
285     mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
286     return handle;
287 }
288 
createConnectionInternal(EventThread * eventThread,ISurfaceComposer::EventRegistrationFlags eventRegistration)289 sp<EventThreadConnection> Scheduler::createConnectionInternal(
290         EventThread* eventThread, ISurfaceComposer::EventRegistrationFlags eventRegistration) {
291     return eventThread->createEventConnection([&] { resync(); }, eventRegistration);
292 }
293 
createDisplayEventConnection(ConnectionHandle handle,ISurfaceComposer::EventRegistrationFlags eventRegistration)294 sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
295         ConnectionHandle handle, ISurfaceComposer::EventRegistrationFlags eventRegistration) {
296     std::lock_guard<std::mutex> lock(mConnectionsLock);
297     RETURN_IF_INVALID_HANDLE(handle, nullptr);
298     return createConnectionInternal(mConnections[handle].thread.get(), eventRegistration);
299 }
300 
getEventConnection(ConnectionHandle handle)301 sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
302     std::lock_guard<std::mutex> lock(mConnectionsLock);
303     RETURN_IF_INVALID_HANDLE(handle, nullptr);
304     return mConnections[handle].connection;
305 }
306 
onHotplugReceived(ConnectionHandle handle,PhysicalDisplayId displayId,bool connected)307 void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
308                                   bool connected) {
309     android::EventThread* thread;
310     {
311         std::lock_guard<std::mutex> lock(mConnectionsLock);
312         RETURN_IF_INVALID_HANDLE(handle);
313         thread = mConnections[handle].thread.get();
314     }
315 
316     thread->onHotplugReceived(displayId, connected);
317 }
318 
onScreenAcquired(ConnectionHandle handle)319 void Scheduler::onScreenAcquired(ConnectionHandle handle) {
320     android::EventThread* thread;
321     {
322         std::lock_guard<std::mutex> lock(mConnectionsLock);
323         RETURN_IF_INVALID_HANDLE(handle);
324         thread = mConnections[handle].thread.get();
325     }
326     thread->onScreenAcquired();
327     mScreenAcquired = true;
328 }
329 
onScreenReleased(ConnectionHandle handle)330 void Scheduler::onScreenReleased(ConnectionHandle handle) {
331     android::EventThread* thread;
332     {
333         std::lock_guard<std::mutex> lock(mConnectionsLock);
334         RETURN_IF_INVALID_HANDLE(handle);
335         thread = mConnections[handle].thread.get();
336     }
337     thread->onScreenReleased();
338     mScreenAcquired = false;
339 }
340 
onFrameRateOverridesChanged(ConnectionHandle handle,PhysicalDisplayId displayId)341 void Scheduler::onFrameRateOverridesChanged(ConnectionHandle handle, PhysicalDisplayId displayId) {
342     std::vector<FrameRateOverride> overrides;
343     {
344         std::lock_guard lock(mFrameRateOverridesLock);
345         for (const auto& [uid, frameRate] : mFrameRateOverridesFromBackdoor) {
346             overrides.emplace_back(FrameRateOverride{uid, frameRate.getValue()});
347         }
348         for (const auto& [uid, frameRate] : mFrameRateOverridesByContent) {
349             if (mFrameRateOverridesFromBackdoor.count(uid) == 0) {
350                 overrides.emplace_back(FrameRateOverride{uid, frameRate.getValue()});
351             }
352         }
353     }
354     android::EventThread* thread;
355     {
356         std::lock_guard lock(mConnectionsLock);
357         RETURN_IF_INVALID_HANDLE(handle);
358         thread = mConnections[handle].thread.get();
359     }
360     thread->onFrameRateOverridesChanged(displayId, std::move(overrides));
361 }
362 
onPrimaryDisplayModeChanged(ConnectionHandle handle,DisplayModePtr mode)363 void Scheduler::onPrimaryDisplayModeChanged(ConnectionHandle handle, DisplayModePtr mode) {
364     {
365         std::lock_guard<std::mutex> lock(mFeatureStateLock);
366         // Cache the last reported modes for primary display.
367         mFeatures.cachedModeChangedParams = {handle, mode};
368 
369         // Invalidate content based refresh rate selection so it could be calculated
370         // again for the new refresh rate.
371         mFeatures.contentRequirements.clear();
372     }
373     onNonPrimaryDisplayModeChanged(handle, mode);
374 }
375 
dispatchCachedReportedMode()376 void Scheduler::dispatchCachedReportedMode() {
377     // Check optional fields first.
378     if (!mFeatures.mode) {
379         ALOGW("No mode ID found, not dispatching cached mode.");
380         return;
381     }
382     if (!mFeatures.cachedModeChangedParams.has_value()) {
383         ALOGW("No mode changed params found, not dispatching cached mode.");
384         return;
385     }
386 
387     // If the mode is not the current mode, this means that a
388     // mode change is in progress. In that case we shouldn't dispatch an event
389     // as it will be dispatched when the current mode changes.
390     if (std::scoped_lock lock(mRefreshRateConfigsLock);
391         mRefreshRateConfigs->getCurrentRefreshRate().getMode() != mFeatures.mode) {
392         return;
393     }
394 
395     // If there is no change from cached mode, there is no need to dispatch an event
396     if (mFeatures.mode == mFeatures.cachedModeChangedParams->mode) {
397         return;
398     }
399 
400     mFeatures.cachedModeChangedParams->mode = mFeatures.mode;
401     onNonPrimaryDisplayModeChanged(mFeatures.cachedModeChangedParams->handle,
402                                    mFeatures.cachedModeChangedParams->mode);
403 }
404 
onNonPrimaryDisplayModeChanged(ConnectionHandle handle,DisplayModePtr mode)405 void Scheduler::onNonPrimaryDisplayModeChanged(ConnectionHandle handle, DisplayModePtr mode) {
406     android::EventThread* thread;
407     {
408         std::lock_guard<std::mutex> lock(mConnectionsLock);
409         RETURN_IF_INVALID_HANDLE(handle);
410         thread = mConnections[handle].thread.get();
411     }
412     thread->onModeChanged(mode);
413 }
414 
getEventThreadConnectionCount(ConnectionHandle handle)415 size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) {
416     std::lock_guard<std::mutex> lock(mConnectionsLock);
417     RETURN_IF_INVALID_HANDLE(handle, 0);
418     return mConnections[handle].thread->getEventThreadConnectionCount();
419 }
420 
dump(ConnectionHandle handle,std::string & result) const421 void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
422     android::EventThread* thread;
423     {
424         std::lock_guard<std::mutex> lock(mConnectionsLock);
425         RETURN_IF_INVALID_HANDLE(handle);
426         thread = mConnections.at(handle).thread.get();
427     }
428     thread->dump(result);
429 }
430 
setDuration(ConnectionHandle handle,std::chrono::nanoseconds workDuration,std::chrono::nanoseconds readyDuration)431 void Scheduler::setDuration(ConnectionHandle handle, std::chrono::nanoseconds workDuration,
432                             std::chrono::nanoseconds readyDuration) {
433     android::EventThread* thread;
434     {
435         std::lock_guard<std::mutex> lock(mConnectionsLock);
436         RETURN_IF_INVALID_HANDLE(handle);
437         thread = mConnections[handle].thread.get();
438     }
439     thread->setDuration(workDuration, readyDuration);
440 }
441 
getDisplayStatInfo(nsecs_t now)442 DisplayStatInfo Scheduler::getDisplayStatInfo(nsecs_t now) {
443     const auto vsyncTime = mVsyncSchedule.tracker->nextAnticipatedVSyncTimeFrom(now);
444     const auto vsyncPeriod = mVsyncSchedule.tracker->currentPeriod();
445     return DisplayStatInfo{.vsyncTime = vsyncTime, .vsyncPeriod = vsyncPeriod};
446 }
447 
enableVSyncInjection(bool enable)448 Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
449     if (mInjectVSyncs == enable) {
450         return {};
451     }
452 
453     ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling");
454 
455     if (!mInjectorConnectionHandle) {
456         auto vsyncSource = std::make_unique<InjectVSyncSource>();
457         mVSyncInjector = vsyncSource.get();
458 
459         auto eventThread =
460                 std::make_unique<impl::EventThread>(std::move(vsyncSource),
461                                                     /*tokenManager=*/nullptr,
462                                                     impl::EventThread::InterceptVSyncsCallback(),
463                                                     impl::EventThread::ThrottleVsyncCallback(),
464                                                     impl::EventThread::GetVsyncPeriodFunction());
465 
466         // EventThread does not dispatch VSYNC unless the display is connected and powered on.
467         eventThread->onHotplugReceived(PhysicalDisplayId::fromPort(0), true);
468         eventThread->onScreenAcquired();
469 
470         mInjectorConnectionHandle = createConnection(std::move(eventThread));
471     }
472 
473     mInjectVSyncs = enable;
474     return mInjectorConnectionHandle;
475 }
476 
injectVSync(nsecs_t when,nsecs_t expectedVSyncTime,nsecs_t deadlineTimestamp)477 bool Scheduler::injectVSync(nsecs_t when, nsecs_t expectedVSyncTime, nsecs_t deadlineTimestamp) {
478     if (!mInjectVSyncs || !mVSyncInjector) {
479         return false;
480     }
481 
482     mVSyncInjector->onInjectSyncEvent(when, expectedVSyncTime, deadlineTimestamp);
483     return true;
484 }
485 
enableHardwareVsync()486 void Scheduler::enableHardwareVsync() {
487     std::lock_guard<std::mutex> lock(mHWVsyncLock);
488     if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
489         mVsyncSchedule.tracker->resetModel();
490         mSchedulerCallback.setVsyncEnabled(true);
491         mPrimaryHWVsyncEnabled = true;
492     }
493 }
494 
disableHardwareVsync(bool makeUnavailable)495 void Scheduler::disableHardwareVsync(bool makeUnavailable) {
496     std::lock_guard<std::mutex> lock(mHWVsyncLock);
497     if (mPrimaryHWVsyncEnabled) {
498         mSchedulerCallback.setVsyncEnabled(false);
499         mPrimaryHWVsyncEnabled = false;
500     }
501     if (makeUnavailable) {
502         mHWVsyncAvailable = false;
503     }
504 }
505 
resyncToHardwareVsync(bool makeAvailable,nsecs_t period)506 void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
507     {
508         std::lock_guard<std::mutex> lock(mHWVsyncLock);
509         if (makeAvailable) {
510             mHWVsyncAvailable = makeAvailable;
511         } else if (!mHWVsyncAvailable) {
512             // Hardware vsync is not currently available, so abort the resync
513             // attempt for now
514             return;
515         }
516     }
517 
518     if (period <= 0) {
519         return;
520     }
521 
522     setVsyncPeriod(period);
523 }
524 
resync()525 void Scheduler::resync() {
526     static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
527 
528     const nsecs_t now = systemTime();
529     const nsecs_t last = mLastResyncTime.exchange(now);
530 
531     if (now - last > kIgnoreDelay) {
532         const auto vsyncPeriod = [&] {
533             std::scoped_lock lock(mRefreshRateConfigsLock);
534             return mRefreshRateConfigs->getCurrentRefreshRate().getVsyncPeriod();
535         }();
536         resyncToHardwareVsync(false, vsyncPeriod);
537     }
538 }
539 
setVsyncPeriod(nsecs_t period)540 void Scheduler::setVsyncPeriod(nsecs_t period) {
541     std::lock_guard<std::mutex> lock(mHWVsyncLock);
542     mVsyncSchedule.controller->startPeriodTransition(period);
543 
544     if (!mPrimaryHWVsyncEnabled) {
545         mVsyncSchedule.tracker->resetModel();
546         mSchedulerCallback.setVsyncEnabled(true);
547         mPrimaryHWVsyncEnabled = true;
548     }
549 }
550 
addResyncSample(nsecs_t timestamp,std::optional<nsecs_t> hwcVsyncPeriod,bool * periodFlushed)551 void Scheduler::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
552                                 bool* periodFlushed) {
553     bool needsHwVsync = false;
554     *periodFlushed = false;
555     { // Scope for the lock
556         std::lock_guard<std::mutex> lock(mHWVsyncLock);
557         if (mPrimaryHWVsyncEnabled) {
558             needsHwVsync = mVsyncSchedule.controller->addHwVsyncTimestamp(timestamp, hwcVsyncPeriod,
559                                                                           periodFlushed);
560         }
561     }
562 
563     if (needsHwVsync) {
564         enableHardwareVsync();
565     } else {
566         disableHardwareVsync(false);
567     }
568 }
569 
addPresentFence(const std::shared_ptr<FenceTime> & fenceTime)570 void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
571     if (mVsyncSchedule.controller->addPresentFence(fenceTime)) {
572         enableHardwareVsync();
573     } else {
574         disableHardwareVsync(false);
575     }
576 }
577 
setIgnorePresentFences(bool ignore)578 void Scheduler::setIgnorePresentFences(bool ignore) {
579     mVsyncSchedule.controller->setIgnorePresentFences(ignore);
580 }
581 
registerLayer(Layer * layer)582 void Scheduler::registerLayer(Layer* layer) {
583     scheduler::LayerHistory::LayerVoteType voteType;
584 
585     if (!mOptions.useContentDetection || layer->getWindowType() == WindowInfo::Type::STATUS_BAR) {
586         voteType = scheduler::LayerHistory::LayerVoteType::NoVote;
587     } else if (layer->getWindowType() == WindowInfo::Type::WALLPAPER) {
588         // Running Wallpaper at Min is considered as part of content detection.
589         voteType = scheduler::LayerHistory::LayerVoteType::Min;
590     } else {
591         voteType = scheduler::LayerHistory::LayerVoteType::Heuristic;
592     }
593 
594     // If the content detection feature is off, we still keep the layer history,
595     // since we use it for other features (like Frame Rate API), so layers
596     // still need to be registered.
597     mLayerHistory->registerLayer(layer, voteType);
598 }
599 
deregisterLayer(Layer * layer)600 void Scheduler::deregisterLayer(Layer* layer) {
601     mLayerHistory->deregisterLayer(layer);
602 }
603 
recordLayerHistory(Layer * layer,nsecs_t presentTime,LayerHistory::LayerUpdateType updateType)604 void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime,
605                                    LayerHistory::LayerUpdateType updateType) {
606     {
607         std::scoped_lock lock(mRefreshRateConfigsLock);
608         if (!mRefreshRateConfigs->canSwitch()) return;
609     }
610 
611     mLayerHistory->record(layer, presentTime, systemTime(), updateType);
612 }
613 
setModeChangePending(bool pending)614 void Scheduler::setModeChangePending(bool pending) {
615     mLayerHistory->setModeChangePending(pending);
616 }
617 
chooseRefreshRateForContent()618 void Scheduler::chooseRefreshRateForContent() {
619     {
620         std::scoped_lock lock(mRefreshRateConfigsLock);
621         if (!mRefreshRateConfigs->canSwitch()) return;
622     }
623 
624     ATRACE_CALL();
625 
626     const auto refreshRateConfigs = holdRefreshRateConfigs();
627     scheduler::LayerHistory::Summary summary =
628             mLayerHistory->summarize(*refreshRateConfigs, systemTime());
629     scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
630     DisplayModePtr newMode;
631     bool frameRateChanged;
632     bool frameRateOverridesChanged;
633     {
634         std::lock_guard<std::mutex> lock(mFeatureStateLock);
635         mFeatures.contentRequirements = summary;
636 
637         newMode = calculateRefreshRateModeId(&consideredSignals);
638         frameRateOverridesChanged = updateFrameRateOverrides(consideredSignals, newMode->getFps());
639 
640         if (mFeatures.mode == newMode) {
641             // We don't need to change the display mode, but we might need to send an event
642             // about a mode change, since it was suppressed due to a previous idleConsidered
643             if (!consideredSignals.idle) {
644                 dispatchCachedReportedMode();
645             }
646             frameRateChanged = false;
647         } else {
648             mFeatures.mode = newMode;
649             frameRateChanged = true;
650         }
651     }
652     if (frameRateChanged) {
653         auto newRefreshRate = refreshRateConfigs->getRefreshRateFromModeId(newMode->getId());
654         mSchedulerCallback.changeRefreshRate(newRefreshRate,
655                                              consideredSignals.idle ? ModeEvent::None
656                                                                     : ModeEvent::Changed);
657     }
658     if (frameRateOverridesChanged) {
659         mSchedulerCallback.triggerOnFrameRateOverridesChanged();
660     }
661 }
662 
resetIdleTimer()663 void Scheduler::resetIdleTimer() {
664     std::scoped_lock lock(mRefreshRateConfigsLock);
665     mRefreshRateConfigs->resetIdleTimer(/*kernelOnly*/ false);
666 }
667 
notifyTouchEvent()668 void Scheduler::notifyTouchEvent() {
669     if (mTouchTimer) {
670         mTouchTimer->reset();
671 
672         std::scoped_lock lock(mRefreshRateConfigsLock);
673         mRefreshRateConfigs->resetIdleTimer(/*kernelOnly*/ true);
674     }
675 }
676 
setDisplayPowerState(bool normal)677 void Scheduler::setDisplayPowerState(bool normal) {
678     {
679         std::lock_guard<std::mutex> lock(mFeatureStateLock);
680         mFeatures.isDisplayPowerStateNormal = normal;
681     }
682 
683     if (mDisplayPowerTimer) {
684         mDisplayPowerTimer->reset();
685     }
686 
687     // Display Power event will boost the refresh rate to performance.
688     // Clear Layer History to get fresh FPS detection
689     mLayerHistory->clear();
690 }
691 
kernelIdleTimerCallback(TimerState state)692 void Scheduler::kernelIdleTimerCallback(TimerState state) {
693     ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
694 
695     // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
696     // magic number
697     const auto refreshRate = [&] {
698         std::scoped_lock lock(mRefreshRateConfigsLock);
699         return mRefreshRateConfigs->getCurrentRefreshRate();
700     }();
701 
702     constexpr Fps FPS_THRESHOLD_FOR_KERNEL_TIMER{65.0f};
703     if (state == TimerState::Reset &&
704         refreshRate.getFps().greaterThanWithMargin(FPS_THRESHOLD_FOR_KERNEL_TIMER)) {
705         // If we're not in performance mode then the kernel timer shouldn't do
706         // anything, as the refresh rate during DPU power collapse will be the
707         // same.
708         resyncToHardwareVsync(true /* makeAvailable */, refreshRate.getVsyncPeriod());
709     } else if (state == TimerState::Expired &&
710                refreshRate.getFps().lessThanOrEqualWithMargin(FPS_THRESHOLD_FOR_KERNEL_TIMER)) {
711         // Disable HW VSYNC if the timer expired, as we don't need it enabled if
712         // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
713         // need to update the VsyncController model anyway.
714         disableHardwareVsync(false /* makeUnavailable */);
715     }
716 
717     mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
718 }
719 
idleTimerCallback(TimerState state)720 void Scheduler::idleTimerCallback(TimerState state) {
721     handleTimerStateChanged(&mFeatures.idleTimer, state);
722     ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
723 }
724 
touchTimerCallback(TimerState state)725 void Scheduler::touchTimerCallback(TimerState state) {
726     const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
727     // Touch event will boost the refresh rate to performance.
728     // Clear layer history to get fresh FPS detection.
729     // NOTE: Instead of checking all the layers, we should be checking the layer
730     // that is currently on top. b/142507166 will give us this capability.
731     if (handleTimerStateChanged(&mFeatures.touch, touch)) {
732         mLayerHistory->clear();
733     }
734     ATRACE_INT("TouchState", static_cast<int>(touch));
735 }
736 
displayPowerTimerCallback(TimerState state)737 void Scheduler::displayPowerTimerCallback(TimerState state) {
738     handleTimerStateChanged(&mFeatures.displayPowerTimer, state);
739     ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
740 }
741 
dump(std::string & result) const742 void Scheduler::dump(std::string& result) const {
743     using base::StringAppendF;
744 
745     StringAppendF(&result, "+  Touch timer: %s\n",
746                   mTouchTimer ? mTouchTimer->dump().c_str() : "off");
747     StringAppendF(&result, "+  Content detection: %s %s\n\n",
748                   toContentDetectionString(mOptions.useContentDetection),
749                   mLayerHistory ? mLayerHistory->dump().c_str() : "(no layer history)");
750 
751     {
752         std::lock_guard lock(mFrameRateOverridesLock);
753         StringAppendF(&result, "Frame Rate Overrides (backdoor): {");
754         for (const auto& [uid, frameRate] : mFrameRateOverridesFromBackdoor) {
755             StringAppendF(&result, "[uid: %d frameRate: %s], ", uid, to_string(frameRate).c_str());
756         }
757         StringAppendF(&result, "}\n");
758 
759         StringAppendF(&result, "Frame Rate Overrides (setFrameRate): {");
760         for (const auto& [uid, frameRate] : mFrameRateOverridesByContent) {
761             StringAppendF(&result, "[uid: %d frameRate: %s], ", uid, to_string(frameRate).c_str());
762         }
763         StringAppendF(&result, "}\n");
764     }
765 
766     {
767         std::lock_guard lock(mHWVsyncLock);
768         StringAppendF(&result,
769                       "mScreenAcquired=%d mPrimaryHWVsyncEnabled=%d mHWVsyncAvailable=%d\n",
770                       mScreenAcquired.load(), mPrimaryHWVsyncEnabled, mHWVsyncAvailable);
771     }
772 }
773 
dumpVsync(std::string & s) const774 void Scheduler::dumpVsync(std::string& s) const {
775     using base::StringAppendF;
776 
777     StringAppendF(&s, "VSyncReactor:\n");
778     mVsyncSchedule.controller->dump(s);
779     StringAppendF(&s, "VSyncDispatch:\n");
780     mVsyncSchedule.dispatch->dump(s);
781 }
782 
updateFrameRateOverrides(scheduler::RefreshRateConfigs::GlobalSignals consideredSignals,Fps displayRefreshRate)783 bool Scheduler::updateFrameRateOverrides(
784         scheduler::RefreshRateConfigs::GlobalSignals consideredSignals, Fps displayRefreshRate) {
785     const auto refreshRateConfigs = holdRefreshRateConfigs();
786     if (!refreshRateConfigs->supportsFrameRateOverride()) {
787         return false;
788     }
789 
790     if (!consideredSignals.idle) {
791         const auto frameRateOverrides =
792                 refreshRateConfigs->getFrameRateOverrides(mFeatures.contentRequirements,
793                                                           displayRefreshRate,
794                                                           consideredSignals.touch);
795         std::lock_guard lock(mFrameRateOverridesLock);
796         if (!std::equal(mFrameRateOverridesByContent.begin(), mFrameRateOverridesByContent.end(),
797                         frameRateOverrides.begin(), frameRateOverrides.end(),
798                         [](const std::pair<uid_t, Fps>& a, const std::pair<uid_t, Fps>& b) {
799                             return a.first == b.first && a.second.equalsWithMargin(b.second);
800                         })) {
801             mFrameRateOverridesByContent = frameRateOverrides;
802             return true;
803         }
804     }
805     return false;
806 }
807 
808 template <class T>
handleTimerStateChanged(T * currentState,T newState)809 bool Scheduler::handleTimerStateChanged(T* currentState, T newState) {
810     DisplayModePtr newMode;
811     bool refreshRateChanged = false;
812     bool frameRateOverridesChanged;
813     scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
814     const auto refreshRateConfigs = holdRefreshRateConfigs();
815     {
816         std::lock_guard<std::mutex> lock(mFeatureStateLock);
817         if (*currentState == newState) {
818             return false;
819         }
820         *currentState = newState;
821         newMode = calculateRefreshRateModeId(&consideredSignals);
822         frameRateOverridesChanged = updateFrameRateOverrides(consideredSignals, newMode->getFps());
823         if (mFeatures.mode == newMode) {
824             // We don't need to change the display mode, but we might need to send an event
825             // about a mode change, since it was suppressed due to a previous idleConsidered
826             if (!consideredSignals.idle) {
827                 dispatchCachedReportedMode();
828             }
829         } else {
830             mFeatures.mode = newMode;
831             refreshRateChanged = true;
832         }
833     }
834     if (refreshRateChanged) {
835         const RefreshRate& newRefreshRate =
836                 refreshRateConfigs->getRefreshRateFromModeId(newMode->getId());
837 
838         mSchedulerCallback.changeRefreshRate(newRefreshRate,
839                                              consideredSignals.idle ? ModeEvent::None
840                                                                     : ModeEvent::Changed);
841     }
842     if (frameRateOverridesChanged) {
843         mSchedulerCallback.triggerOnFrameRateOverridesChanged();
844     }
845     return consideredSignals.touch;
846 }
847 
calculateRefreshRateModeId(scheduler::RefreshRateConfigs::GlobalSignals * consideredSignals)848 DisplayModePtr Scheduler::calculateRefreshRateModeId(
849         scheduler::RefreshRateConfigs::GlobalSignals* consideredSignals) {
850     ATRACE_CALL();
851     if (consideredSignals) *consideredSignals = {};
852 
853     const auto refreshRateConfigs = holdRefreshRateConfigs();
854     // If Display Power is not in normal operation we want to be in performance mode. When coming
855     // back to normal mode, a grace period is given with DisplayPowerTimer.
856     if (mDisplayPowerTimer &&
857         (!mFeatures.isDisplayPowerStateNormal ||
858          mFeatures.displayPowerTimer == TimerState::Reset)) {
859         return refreshRateConfigs->getMaxRefreshRateByPolicy().getMode();
860     }
861 
862     const bool touchActive = mTouchTimer && mFeatures.touch == TouchState::Active;
863     const bool idle = mFeatures.idleTimer == TimerState::Expired;
864 
865     return refreshRateConfigs
866             ->getBestRefreshRate(mFeatures.contentRequirements,
867                                  {.touch = touchActive, .idle = idle}, consideredSignals)
868             .getMode();
869 }
870 
getPreferredDisplayMode()871 DisplayModePtr Scheduler::getPreferredDisplayMode() {
872     std::lock_guard<std::mutex> lock(mFeatureStateLock);
873     // Make sure that the default mode ID is first updated, before returned.
874     if (mFeatures.mode) {
875         mFeatures.mode = calculateRefreshRateModeId();
876     }
877     return mFeatures.mode;
878 }
879 
onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline & timeline)880 void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
881     if (timeline.refreshRequired) {
882         mSchedulerCallback.repaintEverythingForHWC();
883     }
884 
885     std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
886     mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
887 
888     const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
889     if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
890         mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
891     }
892 }
893 
onDisplayRefreshed(nsecs_t timestamp)894 void Scheduler::onDisplayRefreshed(nsecs_t timestamp) {
895     bool callRepaint = false;
896     {
897         std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
898         if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
899             if (mLastVsyncPeriodChangeTimeline->refreshTimeNanos < timestamp) {
900                 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
901             } else {
902                 // We need to send another refresh as refreshTimeNanos is still in the future
903                 callRepaint = true;
904             }
905         }
906     }
907 
908     if (callRepaint) {
909         mSchedulerCallback.repaintEverythingForHWC();
910     }
911 }
912 
onActiveDisplayAreaChanged(uint32_t displayArea)913 void Scheduler::onActiveDisplayAreaChanged(uint32_t displayArea) {
914     mLayerHistory->setDisplayArea(displayArea);
915 }
916 
setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride)917 void Scheduler::setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride) {
918     if (frameRateOverride.frameRateHz > 0.f && frameRateOverride.frameRateHz < 1.f) {
919         return;
920     }
921 
922     std::lock_guard lock(mFrameRateOverridesLock);
923     if (frameRateOverride.frameRateHz != 0.f) {
924         mFrameRateOverridesFromBackdoor[frameRateOverride.uid] = Fps(frameRateOverride.frameRateHz);
925     } else {
926         mFrameRateOverridesFromBackdoor.erase(frameRateOverride.uid);
927     }
928 }
929 
getPreviousVsyncFrom(nsecs_t expectedPresentTime) const930 std::chrono::steady_clock::time_point Scheduler::getPreviousVsyncFrom(
931         nsecs_t expectedPresentTime) const {
932     const auto presentTime = std::chrono::nanoseconds(expectedPresentTime);
933     const auto vsyncPeriod = std::chrono::nanoseconds(mVsyncSchedule.tracker->currentPeriod());
934     return std::chrono::steady_clock::time_point(presentTime - vsyncPeriod);
935 }
936 
937 } // namespace android
938