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