blob: 5c0ba01cd3979ef3133729833314a733633490ba [file] [log] [blame]
Ana Krulec98b5b242018-08-10 15:03:23 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski98041832019-08-01 18:35:59 -070017#undef LOG_TAG
18#define LOG_TAG "Scheduler"
Ana Krulec7ab56032018-11-02 20:51:06 +010019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
Ana Krulec98b5b242018-08-10 15:03:23 -070021#include "Scheduler.h"
22
Dominik Laskowski49cea512019-11-12 14:13:23 -080023#include <android-base/stringprintf.h>
Ana Krulece588e312018-09-18 12:32:24 -070024#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
25#include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
Ana Krulece588e312018-09-18 12:32:24 -070026#include <configstore/Utils.h>
Ana Krulecfb772822018-11-30 10:44:07 +010027#include <cutils/properties.h>
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070028#include <input/InputWindow.h>
Ana Krulecfefd6ae2019-02-13 17:53:08 -080029#include <system/window.h>
Ana Krulece588e312018-09-18 12:32:24 -070030#include <ui/DisplayStatInfo.h>
Ana Krulec3084c052018-11-21 20:27:17 +010031#include <utils/Timers.h>
Ana Krulec7ab56032018-11-02 20:51:06 +010032#include <utils/Trace.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070033
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070034#include <algorithm>
35#include <cinttypes>
36#include <cstdint>
37#include <functional>
38#include <memory>
39#include <numeric>
40
41#include "../Layer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070042#include "DispSync.h"
43#include "DispSyncSource.h"
Ana Krulece588e312018-09-18 12:32:24 -070044#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070045#include "EventThread.h"
Dominik Laskowski6505f792019-09-18 11:10:05 -070046#include "InjectVSyncSource.h"
Ana Krulecf2c006d2019-06-21 15:37:07 -070047#include "OneShotTimer.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010048#include "SchedulerUtils.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090049#include "SurfaceFlingerProperties.h"
Kevin DuBois00287382019-11-19 15:11:55 -080050#include "Timer.h"
51#include "VSyncDispatchTimerQueue.h"
52#include "VSyncPredictor.h"
53#include "VSyncReactor.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070054
Dominik Laskowski98041832019-08-01 18:35:59 -070055#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
Ana Krulec98b5b242018-08-10 15:03:23 -070063namespace android {
64
Dan Stoza027d3652020-05-26 17:26:34 -070065std::unique_ptr<DispSync> createDispSync(bool supportKernelTimer) {
Kevin DuBois00287382019-11-19 15:11:55 -080066 // TODO (140302863) remove this and use the vsync_reactor system.
Kevin DuBoisc57f2c32019-12-20 16:32:29 -080067 if (property_get_bool("debug.sf.vsync_reactor", true)) {
Kevin DuBois00287382019-11-19 15:11:55 -080068 // TODO (144707443) tune Predictor tunables.
Marin Shalamanovcfeebd42020-05-15 15:23:49 +020069 static constexpr int defaultRate = 60;
70 static constexpr auto initialPeriod =
71 std::chrono::duration<nsecs_t, std::ratio<1, defaultRate>>(1);
Kevin DuBois00287382019-11-19 15:11:55 -080072 static constexpr size_t vsyncTimestampHistorySize = 20;
73 static constexpr size_t minimumSamplesForPrediction = 6;
74 static constexpr uint32_t discardOutlierPercent = 20;
75 auto tracker = std::make_unique<
76 scheduler::VSyncPredictor>(std::chrono::duration_cast<std::chrono::nanoseconds>(
Marin Shalamanovcfeebd42020-05-15 15:23:49 +020077 initialPeriod)
Kevin DuBois00287382019-11-19 15:11:55 -080078 .count(),
79 vsyncTimestampHistorySize, minimumSamplesForPrediction,
80 discardOutlierPercent);
81
82 static constexpr auto vsyncMoveThreshold =
83 std::chrono::duration_cast<std::chrono::nanoseconds>(3ms);
84 static constexpr auto timerSlack =
85 std::chrono::duration_cast<std::chrono::nanoseconds>(500us);
86 auto dispatch = std::make_unique<
87 scheduler::VSyncDispatchTimerQueue>(std::make_unique<scheduler::Timer>(), *tracker,
88 timerSlack.count(), vsyncMoveThreshold.count());
89
90 static constexpr size_t pendingFenceLimit = 20;
91 return std::make_unique<scheduler::VSyncReactor>(std::make_unique<scheduler::SystemClock>(),
92 std::move(dispatch), std::move(tracker),
Dan Stoza027d3652020-05-26 17:26:34 -070093 pendingFenceLimit, supportKernelTimer);
Kevin DuBois00287382019-11-19 15:11:55 -080094 } else {
95 return std::make_unique<impl::DispSync>("SchedulerDispSync",
96 sysprop::running_without_sync_framework(true));
97 }
98}
99
Ady Abraham09bd3922019-04-08 10:44:56 -0700100Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function,
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800101 const scheduler::RefreshRateConfigs& refreshRateConfig,
Ana Krulec3d367c82020-02-25 15:02:01 -0800102 ISchedulerCallback& schedulerCallback, bool useContentDetectionV2,
103 bool useContentDetection)
Dan Stoza027d3652020-05-26 17:26:34 -0700104 : mSupportKernelTimer(sysprop::support_kernel_idle_timer(false)),
105 mPrimaryDispSync(createDispSync(mSupportKernelTimer)),
Dominik Laskowski98041832019-08-01 18:35:59 -0700106 mEventControlThread(new impl::EventControlThread(std::move(function))),
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800107 mSchedulerCallback(schedulerCallback),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800108 mRefreshRateConfigs(refreshRateConfig),
Ana Krulec3d367c82020-02-25 15:02:01 -0800109 mUseContentDetection(useContentDetection),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800110 mUseContentDetectionV2(useContentDetectionV2) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700111 using namespace sysprop;
Ady Abraham8532d012019-05-08 14:50:56 -0700112
Ana Krulec3803b8d2020-02-03 16:35:46 -0800113 if (mUseContentDetectionV2) {
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700114 mLayerHistory = std::make_unique<scheduler::impl::LayerHistoryV2>(refreshRateConfig);
Ana Krulec3803b8d2020-02-03 16:35:46 -0800115 } else {
116 mLayerHistory = std::make_unique<scheduler::impl::LayerHistory>();
Dominik Laskowski49cea512019-11-12 14:13:23 -0800117 }
118
119 const int setIdleTimerMs = property_get_int32("debug.sf.set_idle_timer_ms", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100120
Dominik Laskowski98041832019-08-01 18:35:59 -0700121 if (const auto millis = setIdleTimerMs ? setIdleTimerMs : set_idle_timer_ms(0); millis > 0) {
122 const auto callback = mSupportKernelTimer ? &Scheduler::kernelIdleTimerCallback
123 : &Scheduler::idleTimerCallback;
Dominik Laskowski98041832019-08-01 18:35:59 -0700124 mIdleTimer.emplace(
125 std::chrono::milliseconds(millis),
126 [this, callback] { std::invoke(callback, this, TimerState::Reset); },
127 [this, callback] { std::invoke(callback, this, TimerState::Expired); });
Ana Krulecfb772822018-11-30 10:44:07 +0100128 mIdleTimer->start();
129 }
Ady Abraham8532d012019-05-08 14:50:56 -0700130
Dominik Laskowski98041832019-08-01 18:35:59 -0700131 if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -0700132 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -0700133 mTouchTimer.emplace(
134 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700135 [this] { touchTimerCallback(TimerState::Reset); },
136 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -0700137 mTouchTimer->start();
138 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700139
Dominik Laskowski98041832019-08-01 18:35:59 -0700140 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
141 mDisplayPowerTimer.emplace(
142 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700143 [this] { displayPowerTimerCallback(TimerState::Reset); },
144 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -0700145 mDisplayPowerTimer->start();
146 }
Ana Krulece588e312018-09-18 12:32:24 -0700147}
148
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700149Scheduler::Scheduler(std::unique_ptr<DispSync> primaryDispSync,
150 std::unique_ptr<EventControlThread> eventControlThread,
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800151 const scheduler::RefreshRateConfigs& configs,
Ana Krulec3d367c82020-02-25 15:02:01 -0800152 ISchedulerCallback& schedulerCallback, bool useContentDetectionV2,
153 bool useContentDetection)
Dan Stoza027d3652020-05-26 17:26:34 -0700154 : mSupportKernelTimer(false),
155 mPrimaryDispSync(std::move(primaryDispSync)),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700156 mEventControlThread(std::move(eventControlThread)),
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800157 mSchedulerCallback(schedulerCallback),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800158 mRefreshRateConfigs(configs),
Ana Krulec3d367c82020-02-25 15:02:01 -0800159 mUseContentDetection(useContentDetection),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800160 mUseContentDetectionV2(useContentDetectionV2) {}
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700161
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800162Scheduler::~Scheduler() {
Ana Krulecf2c006d2019-06-21 15:37:07 -0700163 // Ensure the OneShotTimer threads are joined before we start destroying state.
Ady Abraham6fe2c172019-07-12 12:37:57 -0700164 mDisplayPowerTimer.reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700165 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800166 mIdleTimer.reset();
167}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700168
Dominik Laskowski98041832019-08-01 18:35:59 -0700169DispSync& Scheduler::getPrimaryDispSync() {
170 return *mPrimaryDispSync;
171}
172
Ady Abraham9e16a482019-12-03 17:19:41 -0800173std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(const char* name,
174 nsecs_t phaseOffsetNs) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700175 return std::make_unique<DispSyncSource>(mPrimaryDispSync.get(), phaseOffsetNs,
Ady Abraham9e16a482019-12-03 17:19:41 -0800176 true /* traceVsync */, name);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700177}
178
Dominik Laskowski98041832019-08-01 18:35:59 -0700179Scheduler::ConnectionHandle Scheduler::createConnection(
Ady Abraham9e16a482019-12-03 17:19:41 -0800180 const char* connectionName, nsecs_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -0700181 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ady Abraham9e16a482019-12-03 17:19:41 -0800182 auto vsyncSource = makePrimaryDispSyncSource(connectionName, phaseOffsetNs);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700183 auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource),
184 std::move(interceptCallback));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700185 return createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700186}
Ana Krulec98b5b242018-08-10 15:03:23 -0700187
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700188Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700189 const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
190 ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800191
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700192 auto connection =
193 createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowski98041832019-08-01 18:35:59 -0700194
195 mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
196 return handle;
Ana Krulec98b5b242018-08-10 15:03:23 -0700197}
198
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700199sp<EventThreadConnection> Scheduler::createConnectionInternal(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700200 EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) {
201 return eventThread->createEventConnection([&] { resync(); }, configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700202}
203
Ana Krulec98b5b242018-08-10 15:03:23 -0700204sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700205 ConnectionHandle handle, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700206 RETURN_IF_INVALID_HANDLE(handle, nullptr);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700207 return createConnectionInternal(mConnections[handle].thread.get(), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700208}
209
Dominik Laskowski98041832019-08-01 18:35:59 -0700210sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
211 RETURN_IF_INVALID_HANDLE(handle, nullptr);
212 return mConnections[handle].connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700213}
214
Dominik Laskowski98041832019-08-01 18:35:59 -0700215void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
216 bool connected) {
217 RETURN_IF_INVALID_HANDLE(handle);
218 mConnections[handle].thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700219}
220
Dominik Laskowski98041832019-08-01 18:35:59 -0700221void Scheduler::onScreenAcquired(ConnectionHandle handle) {
222 RETURN_IF_INVALID_HANDLE(handle);
223 mConnections[handle].thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700224}
225
Dominik Laskowski98041832019-08-01 18:35:59 -0700226void Scheduler::onScreenReleased(ConnectionHandle handle) {
227 RETURN_IF_INVALID_HANDLE(handle);
228 mConnections[handle].thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700229}
230
Ady Abrahamdfd62162020-06-10 16:11:56 -0700231void Scheduler::onPrimaryDisplayConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId,
232 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
233 std::lock_guard<std::mutex> lock(mFeatureStateLock);
234 // Cache the last reported config for primary display.
235 mFeatures.cachedConfigChangedParams = {handle, displayId, configId, vsyncPeriod};
236 onNonPrimaryDisplayConfigChanged(handle, displayId, configId, vsyncPeriod);
237}
238
239void Scheduler::dispatchCachedReportedConfig() {
240 const auto configId = *mFeatures.configId;
241 const auto vsyncPeriod =
242 mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getVsyncPeriod();
243
244 // If there is no change from cached config, there is no need to dispatch an event
245 if (configId == mFeatures.cachedConfigChangedParams->configId &&
246 vsyncPeriod == mFeatures.cachedConfigChangedParams->vsyncPeriod) {
247 return;
248 }
249
250 mFeatures.cachedConfigChangedParams->configId = configId;
251 mFeatures.cachedConfigChangedParams->vsyncPeriod = vsyncPeriod;
252 onNonPrimaryDisplayConfigChanged(mFeatures.cachedConfigChangedParams->handle,
253 mFeatures.cachedConfigChangedParams->displayId,
254 mFeatures.cachedConfigChangedParams->configId,
255 mFeatures.cachedConfigChangedParams->vsyncPeriod);
256}
257
258void Scheduler::onNonPrimaryDisplayConfigChanged(ConnectionHandle handle,
259 PhysicalDisplayId displayId,
260 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700261 RETURN_IF_INVALID_HANDLE(handle);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700262 mConnections[handle].thread->onConfigChanged(displayId, configId, vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800263}
264
Alec Mouri717bcb62020-02-10 17:07:19 -0800265size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) {
266 RETURN_IF_INVALID_HANDLE(handle, 0);
267 return mConnections[handle].thread->getEventThreadConnectionCount();
268}
269
Dominik Laskowski98041832019-08-01 18:35:59 -0700270void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
271 RETURN_IF_INVALID_HANDLE(handle);
272 mConnections.at(handle).thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700273}
274
Dominik Laskowski98041832019-08-01 18:35:59 -0700275void Scheduler::setPhaseOffset(ConnectionHandle handle, nsecs_t phaseOffset) {
276 RETURN_IF_INVALID_HANDLE(handle);
277 mConnections[handle].thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700278}
Ana Krulece588e312018-09-18 12:32:24 -0700279
280void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
Ady Abraham0ed31c92020-04-16 11:48:45 -0700281 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0, systemTime());
Ana Krulece588e312018-09-18 12:32:24 -0700282 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
283}
284
Dominik Laskowski6505f792019-09-18 11:10:05 -0700285Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
286 if (mInjectVSyncs == enable) {
287 return {};
288 }
289
290 ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling");
291
292 if (!mInjectorConnectionHandle) {
293 auto vsyncSource = std::make_unique<InjectVSyncSource>();
294 mVSyncInjector = vsyncSource.get();
295
296 auto eventThread =
297 std::make_unique<impl::EventThread>(std::move(vsyncSource),
298 impl::EventThread::InterceptVSyncsCallback());
299
300 mInjectorConnectionHandle = createConnection(std::move(eventThread));
301 }
302
303 mInjectVSyncs = enable;
304 return mInjectorConnectionHandle;
305}
306
Ady Abraham5facfb12020-04-22 15:18:31 -0700307bool Scheduler::injectVSync(nsecs_t when, nsecs_t expectedVSyncTime) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700308 if (!mInjectVSyncs || !mVSyncInjector) {
309 return false;
310 }
311
Ady Abraham5facfb12020-04-22 15:18:31 -0700312 mVSyncInjector->onInjectSyncEvent(when, expectedVSyncTime);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700313 return true;
314}
315
Ana Krulece588e312018-09-18 12:32:24 -0700316void Scheduler::enableHardwareVsync() {
317 std::lock_guard<std::mutex> lock(mHWVsyncLock);
318 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
319 mPrimaryDispSync->beginResync();
320 mEventControlThread->setVsyncEnabled(true);
321 mPrimaryHWVsyncEnabled = true;
322 }
323}
324
325void Scheduler::disableHardwareVsync(bool makeUnavailable) {
326 std::lock_guard<std::mutex> lock(mHWVsyncLock);
327 if (mPrimaryHWVsyncEnabled) {
328 mEventControlThread->setVsyncEnabled(false);
329 mPrimaryDispSync->endResync();
330 mPrimaryHWVsyncEnabled = false;
331 }
332 if (makeUnavailable) {
333 mHWVsyncAvailable = false;
334 }
335}
336
Ana Krulecc2870422019-01-29 19:00:58 -0800337void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
338 {
339 std::lock_guard<std::mutex> lock(mHWVsyncLock);
340 if (makeAvailable) {
341 mHWVsyncAvailable = makeAvailable;
342 } else if (!mHWVsyncAvailable) {
343 // Hardware vsync is not currently available, so abort the resync
344 // attempt for now
345 return;
346 }
347 }
348
349 if (period <= 0) {
350 return;
351 }
352
353 setVsyncPeriod(period);
354}
355
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700356void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700357 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800358
359 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700360 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800361
362 if (now - last > kIgnoreDelay) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700363 resyncToHardwareVsync(false, mRefreshRateConfigs.getCurrentRefreshRate().getVsyncPeriod());
Ana Krulecc2870422019-01-29 19:00:58 -0800364 }
365}
366
Dominik Laskowski98041832019-08-01 18:35:59 -0700367void Scheduler::setVsyncPeriod(nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800368 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700369 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800370
371 if (!mPrimaryHWVsyncEnabled) {
372 mPrimaryDispSync->beginResync();
373 mEventControlThread->setVsyncEnabled(true);
374 mPrimaryHWVsyncEnabled = true;
375 }
Ana Krulece588e312018-09-18 12:32:24 -0700376}
377
Ady Abraham5dee2f12020-02-05 17:49:47 -0800378void Scheduler::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
379 bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700380 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700381 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700382 { // Scope for the lock
383 std::lock_guard<std::mutex> lock(mHWVsyncLock);
384 if (mPrimaryHWVsyncEnabled) {
Ady Abraham5dee2f12020-02-05 17:49:47 -0800385 needsHwVsync =
386 mPrimaryDispSync->addResyncSample(timestamp, hwcVsyncPeriod, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700387 }
388 }
389
390 if (needsHwVsync) {
391 enableHardwareVsync();
392 } else {
393 disableHardwareVsync(false);
394 }
395}
396
397void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
398 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
399 enableHardwareVsync();
400 } else {
401 disableHardwareVsync(false);
402 }
403}
404
405void Scheduler::setIgnorePresentFences(bool ignore) {
406 mPrimaryDispSync->setIgnorePresentFences(ignore);
407}
408
Ady Abraham0ed31c92020-04-16 11:48:45 -0700409nsecs_t Scheduler::getDispSyncExpectedPresentTime(nsecs_t now) {
410 return mPrimaryDispSync->expectedPresentTime(now);
Ady Abrahamc3e21312019-02-07 14:30:23 -0800411}
412
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700413void Scheduler::registerLayer(Layer* layer) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800414 if (!mLayerHistory) return;
415
Steven Thomasdebafed2020-05-18 17:30:35 -0700416 const auto minFps = mRefreshRateConfigs.getMinRefreshRate().getFps();
417 const auto maxFps = mRefreshRateConfigs.getMaxRefreshRate().getFps();
418
419 if (layer->getWindowType() == InputWindowInfo::TYPE_STATUS_BAR) {
420 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ana Krulec3d367c82020-02-25 15:02:01 -0800421 scheduler::LayerHistory::LayerVoteType::NoVote);
Steven Thomasdebafed2020-05-18 17:30:35 -0700422 } else if (!mUseContentDetection) {
423 // If the content detection feature is off, all layers are registered at Max. We still keep
424 // the layer history, since we use it for other features (like Frame Rate API), so layers
425 // still need to be registered.
426 mLayerHistory->registerLayer(layer, minFps, maxFps,
427 scheduler::LayerHistory::LayerVoteType::Max);
428 } else if (!mUseContentDetectionV2) {
429 // In V1 of content detection, all layers are registered as Heuristic (unless it's
430 // wallpaper).
431 const auto highFps =
432 layer->getWindowType() == InputWindowInfo::TYPE_WALLPAPER ? minFps : maxFps;
Ana Krulec3d367c82020-02-25 15:02:01 -0800433
Steven Thomasdebafed2020-05-18 17:30:35 -0700434 mLayerHistory->registerLayer(layer, minFps, highFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800435 scheduler::LayerHistory::LayerVoteType::Heuristic);
436 } else {
437 if (layer->getWindowType() == InputWindowInfo::TYPE_WALLPAPER) {
Ana Krulec3d367c82020-02-25 15:02:01 -0800438 // Running Wallpaper at Min is considered as part of content detection.
Steven Thomasdebafed2020-05-18 17:30:35 -0700439 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800440 scheduler::LayerHistory::LayerVoteType::Min);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800441 } else {
Steven Thomasdebafed2020-05-18 17:30:35 -0700442 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800443 scheduler::LayerHistory::LayerVoteType::Heuristic);
444 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800445 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700446}
447
Ady Abraham5def7332020-05-29 16:13:47 -0700448void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime,
449 LayerHistory::LayerUpdateType updateType) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800450 if (mLayerHistory) {
Ady Abraham5def7332020-05-29 16:13:47 -0700451 mLayerHistory->record(layer, presentTime, systemTime(), updateType);
Dominik Laskowski49cea512019-11-12 14:13:23 -0800452 }
Ana Krulec3084c052018-11-21 20:27:17 +0100453}
454
Ady Abraham32efd542020-05-19 17:49:26 -0700455void Scheduler::setConfigChangePending(bool pending) {
456 if (mLayerHistory) {
457 mLayerHistory->setConfigChangePending(pending);
458 }
459}
460
Dominik Laskowski49cea512019-11-12 14:13:23 -0800461void Scheduler::chooseRefreshRateForContent() {
462 if (!mLayerHistory) return;
463
Ady Abraham8a82ba62020-01-17 12:43:17 -0800464 ATRACE_CALL();
465
466 scheduler::LayerHistory::Summary summary = mLayerHistory->summarize(systemTime());
Ady Abraham2139f732019-11-13 18:56:40 -0800467 HwcConfigIndexType newConfigId;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700468 {
469 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800470 if (mFeatures.contentRequirements == summary) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700471 return;
472 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800473 mFeatures.contentRequirements = summary;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800474 mFeatures.contentDetectionV1 =
Ady Abraham8a82ba62020-01-17 12:43:17 -0800475 !summary.empty() ? ContentDetectionState::On : ContentDetectionState::Off;
476
Ady Abrahamdfd62162020-06-10 16:11:56 -0700477 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
478 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800479 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700480 // We don't need to change the config, but we might need to send an event
481 // about a config change, since it was suppressed due to a previous idleConsidered
482 if (!consideredSignals.idle) {
483 dispatchCachedReportedConfig();
484 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700485 return;
486 }
Ady Abraham2139f732019-11-13 18:56:40 -0800487 mFeatures.configId = newConfigId;
Ady Abraham2e1dd892020-03-05 13:48:36 -0800488 auto& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700489 mSchedulerCallback.changeRefreshRate(newRefreshRate,
490 consideredSignals.idle ? ConfigEvent::None
491 : ConfigEvent::Changed);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800492 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800493}
494
Ana Krulecfb772822018-11-30 10:44:07 +0100495void Scheduler::resetIdleTimer() {
496 if (mIdleTimer) {
497 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800498 }
499}
500
Ady Abraham8532d012019-05-08 14:50:56 -0700501void Scheduler::notifyTouchEvent() {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800502 if (!mTouchTimer) return;
503
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700504 // Touch event will boost the refresh rate to performance.
Steven Thomas540730a2020-01-08 20:12:42 -0800505 // Clear Layer History to get fresh FPS detection.
506 // NOTE: Instead of checking all the layers, we should be checking the layer
507 // that is currently on top. b/142507166 will give us this capability.
Ady Abraham8a82ba62020-01-17 12:43:17 -0800508 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800509 if (mLayerHistory) {
Steven Thomasbb374322020-04-28 22:47:16 -0700510 // Layer History will be cleared based on RefreshRateConfigs::getBestRefreshRate
Steven Thomas540730a2020-01-08 20:12:42 -0800511
Ady Abraham8a82ba62020-01-17 12:43:17 -0800512 mTouchTimer->reset();
Steven Thomas540730a2020-01-08 20:12:42 -0800513
514 if (mSupportKernelTimer && mIdleTimer) {
515 mIdleTimer->reset();
516 }
Dominik Laskowski49cea512019-11-12 14:13:23 -0800517 }
Ady Abraham8532d012019-05-08 14:50:56 -0700518}
519
Ady Abraham6fe2c172019-07-12 12:37:57 -0700520void Scheduler::setDisplayPowerState(bool normal) {
521 {
522 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700523 mFeatures.isDisplayPowerStateNormal = normal;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700524 }
525
526 if (mDisplayPowerTimer) {
527 mDisplayPowerTimer->reset();
528 }
529
530 // Display Power event will boost the refresh rate to performance.
531 // Clear Layer History to get fresh FPS detection
Dominik Laskowski49cea512019-11-12 14:13:23 -0800532 if (mLayerHistory) {
533 mLayerHistory->clear();
534 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700535}
536
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700537void Scheduler::kernelIdleTimerCallback(TimerState state) {
538 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100539
Ady Abraham2139f732019-11-13 18:56:40 -0800540 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
541 // magic number
Ady Abraham2e1dd892020-03-05 13:48:36 -0800542 const auto& refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
Ady Abraham2139f732019-11-13 18:56:40 -0800543 constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f;
Ady Abrahamabc27602020-04-08 17:20:29 -0700544 if (state == TimerState::Reset && refreshRate.getFps() > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700545 // If we're not in performance mode then the kernel timer shouldn't do
546 // anything, as the refresh rate during DPU power collapse will be the
547 // same.
Ady Abrahamabc27602020-04-08 17:20:29 -0700548 resyncToHardwareVsync(true /* makeAvailable */, refreshRate.getVsyncPeriod());
549 } else if (state == TimerState::Expired &&
550 refreshRate.getFps() <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700551 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
552 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
553 // need to update the DispSync model anyway.
554 disableHardwareVsync(false /* makeUnavailable */);
Alec Mouridc28b372019-04-18 21:17:13 -0700555 }
Ady Abrahama09852a2020-02-20 14:23:42 -0800556
557 mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
Alec Mouridc28b372019-04-18 21:17:13 -0700558}
559
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700560void Scheduler::idleTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700561 handleTimerStateChanged(&mFeatures.idleTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700562 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100563}
564
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700565void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700566 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700567 if (handleTimerStateChanged(&mFeatures.touch, touch)) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700568 mLayerHistory->clear();
569 }
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700570 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700571}
572
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700573void Scheduler::displayPowerTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700574 handleTimerStateChanged(&mFeatures.displayPowerTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700575 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700576}
577
Dominik Laskowski98041832019-08-01 18:35:59 -0700578void Scheduler::dump(std::string& result) const {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800579 using base::StringAppendF;
580 const char* const states[] = {"off", "on"};
Dominik Laskowski98041832019-08-01 18:35:59 -0700581
Dominik Laskowski49cea512019-11-12 14:13:23 -0800582 StringAppendF(&result, "+ Idle timer: %s\n",
583 mIdleTimer ? mIdleTimer->dump().c_str() : states[0]);
Ana Krulec3d367c82020-02-25 15:02:01 -0800584 StringAppendF(&result, "+ Touch timer: %s\n",
Dominik Laskowski49cea512019-11-12 14:13:23 -0800585 mTouchTimer ? mTouchTimer->dump().c_str() : states[0]);
Ana Krulec3d367c82020-02-25 15:02:01 -0800586 StringAppendF(&result, "+ Use content detection: %s\n\n",
587 sysprop::use_content_detection_for_refresh_rate(false) ? "on" : "off");
Ana Krulecb43429d2019-01-09 14:28:51 -0800588}
589
Ady Abraham6fe2c172019-07-12 12:37:57 -0700590template <class T>
Ady Abrahamdfd62162020-06-10 16:11:56 -0700591bool Scheduler::handleTimerStateChanged(T* currentState, T newState) {
Ady Abraham2139f732019-11-13 18:56:40 -0800592 HwcConfigIndexType newConfigId;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700593 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
Ady Abraham8532d012019-05-08 14:50:56 -0700594 {
595 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700596 if (*currentState == newState) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700597 return false;
Ady Abraham8532d012019-05-08 14:50:56 -0700598 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700599 *currentState = newState;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700600 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800601 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700602 // We don't need to change the config, but we might need to send an event
603 // about a config change, since it was suppressed due to a previous idleConsidered
604 if (!consideredSignals.idle) {
605 dispatchCachedReportedConfig();
606 }
607 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700608 }
Ady Abraham2139f732019-11-13 18:56:40 -0800609 mFeatures.configId = newConfigId;
Ady Abraham8532d012019-05-08 14:50:56 -0700610 }
Ady Abraham2139f732019-11-13 18:56:40 -0800611 const RefreshRate& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700612 mSchedulerCallback.changeRefreshRate(newRefreshRate,
613 consideredSignals.idle ? ConfigEvent::None
614 : ConfigEvent::Changed);
615 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700616}
617
Ady Abrahamdfd62162020-06-10 16:11:56 -0700618HwcConfigIndexType Scheduler::calculateRefreshRateConfigIndexType(
619 scheduler::RefreshRateConfigs::GlobalSignals* consideredSignals) {
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800620 ATRACE_CALL();
Ady Abrahamdfd62162020-06-10 16:11:56 -0700621 if (consideredSignals) *consideredSignals = {};
Ady Abraham09bd3922019-04-08 10:44:56 -0700622
Steven Thomasf734df42020-04-13 21:09:28 -0700623 // If Display Power is not in normal operation we want to be in performance mode. When coming
624 // back to normal mode, a grace period is given with DisplayPowerTimer.
Ana Krulec3f6a2062020-01-23 15:48:01 -0800625 if (mDisplayPowerTimer &&
626 (!mFeatures.isDisplayPowerStateNormal ||
627 mFeatures.displayPowerTimer == TimerState::Reset)) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700628 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800629 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700630
Steven Thomasbb374322020-04-28 22:47:16 -0700631 const bool touchActive = mTouchTimer && mFeatures.touch == TouchState::Active;
632 const bool idle = mIdleTimer && mFeatures.idleTimer == TimerState::Expired;
633
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800634 if (!mUseContentDetectionV2) {
635 // As long as touch is active we want to be in performance mode.
Steven Thomasbb374322020-04-28 22:47:16 -0700636 if (touchActive) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700637 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800638 }
Ady Abraham8532d012019-05-08 14:50:56 -0700639
Steven Thomasbb374322020-04-28 22:47:16 -0700640 // If timer has expired as it means there is no new content on the screen.
641 if (idle) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700642 if (consideredSignals) consideredSignals->idle = true;
Steven Thomasbb374322020-04-28 22:47:16 -0700643 return mRefreshRateConfigs.getMinRefreshRateByPolicy().getConfigId();
644 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700645
Ana Krulec3f6a2062020-01-23 15:48:01 -0800646 // If content detection is off we choose performance as we don't know the content fps.
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800647 if (mFeatures.contentDetectionV1 == ContentDetectionState::Off) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800648 // NOTE: V1 always calls this, but this is not a default behavior for V2.
Ady Abrahamabc27602020-04-08 17:20:29 -0700649 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Steven Thomas540730a2020-01-08 20:12:42 -0800650 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800651
652 // Content detection is on, find the appropriate refresh rate with minimal error
Ady Abrahamabc27602020-04-08 17:20:29 -0700653 return mRefreshRateConfigs.getRefreshRateForContent(mFeatures.contentRequirements)
654 .getConfigId();
Ady Abraham09bd3922019-04-08 10:44:56 -0700655 }
656
Ady Abraham1adbb722020-05-15 11:51:48 -0700657 return mRefreshRateConfigs
Ady Abrahamdfd62162020-06-10 16:11:56 -0700658 .getBestRefreshRate(mFeatures.contentRequirements, {.touch = touchActive, .idle = idle},
659 consideredSignals)
Ady Abraham1adbb722020-05-15 11:51:48 -0700660 .getConfigId();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800661}
662
Ady Abraham2139f732019-11-13 18:56:40 -0800663std::optional<HwcConfigIndexType> Scheduler::getPreferredConfigId() {
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700664 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800665 // Make sure that the default config ID is first updated, before returned.
666 if (mFeatures.configId.has_value()) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800667 mFeatures.configId = calculateRefreshRateConfigIndexType();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800668 }
Ady Abraham2139f732019-11-13 18:56:40 -0800669 return mFeatures.configId;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700670}
671
Peiyong Line9d809e2020-04-14 13:10:48 -0700672void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800673 if (timeline.refreshRequired) {
674 mSchedulerCallback.repaintEverythingForHWC();
675 }
676
677 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
678 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
679
680 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
681 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
682 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
683 }
684}
685
686void Scheduler::onDisplayRefreshed(nsecs_t timestamp) {
687 bool callRepaint = false;
688 {
689 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
690 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
691 if (mLastVsyncPeriodChangeTimeline->refreshTimeNanos < timestamp) {
692 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
693 } else {
694 // We need to send another refresh as refreshTimeNanos is still in the future
695 callRepaint = true;
696 }
697 }
698 }
699
700 if (callRepaint) {
701 mSchedulerCallback.repaintEverythingForHWC();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800702 }
703}
704
Ady Abraham8a82ba62020-01-17 12:43:17 -0800705void Scheduler::onPrimaryDisplayAreaChanged(uint32_t displayArea) {
706 if (mLayerHistory) {
707 mLayerHistory->setDisplayArea(displayArea);
708 }
709}
710
Ana Krulec98b5b242018-08-10 15:03:23 -0700711} // namespace android