blob: 5f7b2c280de1630f3c397dfd8346ff1cfd21adda [file] [log] [blame]
Ana Krulec98b5b242018-08-10 15:03:23 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski98041832019-08-01 18:35:59 -070017#undef LOG_TAG
18#define LOG_TAG "Scheduler"
Ana Krulec7ab56032018-11-02 20:51:06 +010019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
Ana Krulec98b5b242018-08-10 15:03:23 -070021#include "Scheduler.h"
22
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070023#include <android-base/properties.h>
Dominik Laskowski49cea512019-11-12 14:13:23 -080024#include <android-base/stringprintf.h>
Ana Krulece588e312018-09-18 12:32:24 -070025#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
26#include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
Ana Krulece588e312018-09-18 12:32:24 -070027#include <configstore/Utils.h>
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"
44#include "EventThread.h"
Dominik Laskowski6505f792019-09-18 11:10:05 -070045#include "InjectVSyncSource.h"
Ana Krulecf2c006d2019-06-21 15:37:07 -070046#include "OneShotTimer.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010047#include "SchedulerUtils.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090048#include "SurfaceFlingerProperties.h"
Kevin DuBois00287382019-11-19 15:11:55 -080049#include "Timer.h"
50#include "VSyncDispatchTimerQueue.h"
51#include "VSyncPredictor.h"
52#include "VSyncReactor.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070053
Dominik Laskowski98041832019-08-01 18:35:59 -070054#define RETURN_IF_INVALID_HANDLE(handle, ...) \
55 do { \
56 if (mConnections.count(handle) == 0) { \
57 ALOGE("Invalid connection handle %" PRIuPTR, handle.id); \
58 return __VA_ARGS__; \
59 } \
60 } while (false)
61
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070062using namespace std::string_literals;
63
Ana Krulec98b5b242018-08-10 15:03:23 -070064namespace android {
Ady Abraham5a858552020-03-31 17:54:56 -070065
Dominik Laskowski983f2b52020-06-25 16:54:06 -070066namespace {
Ana Krulec98b5b242018-08-10 15:03:23 -070067
Ady Abraham5a858552020-03-31 17:54:56 -070068std::unique_ptr<scheduler::VSyncTracker> createVSyncTracker() {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070069 // TODO(b/144707443): Tune constants.
70 constexpr int kDefaultRate = 60;
71 constexpr auto initialPeriod = std::chrono::duration<nsecs_t, std::ratio<1, kDefaultRate>>(1);
72 constexpr nsecs_t idealPeriod =
73 std::chrono::duration_cast<std::chrono::nanoseconds>(initialPeriod).count();
74 constexpr size_t vsyncTimestampHistorySize = 20;
75 constexpr size_t minimumSamplesForPrediction = 6;
76 constexpr uint32_t discardOutlierPercent = 20;
77 return std::make_unique<scheduler::VSyncPredictor>(idealPeriod, vsyncTimestampHistorySize,
78 minimumSamplesForPrediction,
79 discardOutlierPercent);
Ady Abraham5a858552020-03-31 17:54:56 -070080}
81
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070082std::unique_ptr<scheduler::VSyncDispatch> createVSyncDispatch(scheduler::VSyncTracker& tracker) {
83 // TODO(b/144707443): Tune constants.
84 constexpr std::chrono::nanoseconds vsyncMoveThreshold = 3ms;
85 constexpr std::chrono::nanoseconds timerSlack = 500us;
Ady Abraham5a858552020-03-31 17:54:56 -070086 return std::make_unique<
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070087 scheduler::VSyncDispatchTimerQueue>(std::make_unique<scheduler::Timer>(), tracker,
Ady Abraham5a858552020-03-31 17:54:56 -070088 timerSlack.count(), vsyncMoveThreshold.count());
89}
90
Dominik Laskowski983f2b52020-06-25 16:54:06 -070091const char* toContentDetectionString(bool useContentDetection, bool useContentDetectionV2) {
92 if (!useContentDetection) return "off";
93 return useContentDetectionV2 ? "V2" : "V1";
94}
95
96} // namespace
97
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070098Scheduler::Scheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback)
99 : Scheduler(configs, callback,
100 {.supportKernelTimer = sysprop::support_kernel_idle_timer(false),
101 .useContentDetection = sysprop::use_content_detection_for_refresh_rate(false),
102 .useContentDetectionV2 =
Ady Abraham49cb7d52020-07-22 18:37:07 -0700103 base::GetBoolProperty("debug.sf.use_content_detection_v2"s, true)}) {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700104
105Scheduler::Scheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback,
106 Options options)
107 : Scheduler(createVsyncSchedule(options), configs, callback,
108 createLayerHistory(configs, options.useContentDetectionV2), options) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700109 using namespace sysprop;
Ady Abraham8532d012019-05-08 14:50:56 -0700110
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700111 const int setIdleTimerMs = base::GetIntProperty("debug.sf.set_idle_timer_ms"s, 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100112
Dominik Laskowski98041832019-08-01 18:35:59 -0700113 if (const auto millis = setIdleTimerMs ? setIdleTimerMs : set_idle_timer_ms(0); millis > 0) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700114 const auto callback = mOptions.supportKernelTimer ? &Scheduler::kernelIdleTimerCallback
115 : &Scheduler::idleTimerCallback;
Dominik Laskowski98041832019-08-01 18:35:59 -0700116 mIdleTimer.emplace(
117 std::chrono::milliseconds(millis),
118 [this, callback] { std::invoke(callback, this, TimerState::Reset); },
119 [this, callback] { std::invoke(callback, this, TimerState::Expired); });
Ana Krulecfb772822018-11-30 10:44:07 +0100120 mIdleTimer->start();
121 }
Ady Abraham8532d012019-05-08 14:50:56 -0700122
Dominik Laskowski98041832019-08-01 18:35:59 -0700123 if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -0700124 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -0700125 mTouchTimer.emplace(
126 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700127 [this] { touchTimerCallback(TimerState::Reset); },
128 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -0700129 mTouchTimer->start();
130 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700131
Dominik Laskowski98041832019-08-01 18:35:59 -0700132 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
133 mDisplayPowerTimer.emplace(
134 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700135 [this] { displayPowerTimerCallback(TimerState::Reset); },
136 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -0700137 mDisplayPowerTimer->start();
138 }
Ana Krulece588e312018-09-18 12:32:24 -0700139}
140
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700141Scheduler::Scheduler(VsyncSchedule schedule, const scheduler::RefreshRateConfigs& configs,
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700142 ISchedulerCallback& schedulerCallback,
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700143 std::unique_ptr<LayerHistory> layerHistory, Options options)
144 : mOptions(options),
145 mVsyncSchedule(std::move(schedule)),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700146 mLayerHistory(std::move(layerHistory)),
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800147 mSchedulerCallback(schedulerCallback),
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700148 mRefreshRateConfigs(configs) {
149 mSchedulerCallback.setVsyncEnabled(false);
150}
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700151
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800152Scheduler::~Scheduler() {
Ana Krulecf2c006d2019-06-21 15:37:07 -0700153 // Ensure the OneShotTimer threads are joined before we start destroying state.
Ady Abraham6fe2c172019-07-12 12:37:57 -0700154 mDisplayPowerTimer.reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700155 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800156 mIdleTimer.reset();
157}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700158
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700159Scheduler::VsyncSchedule Scheduler::createVsyncSchedule(Options options) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700160 auto clock = std::make_unique<scheduler::SystemClock>();
161 auto tracker = createVSyncTracker();
162 auto dispatch = createVSyncDispatch(*tracker);
163
164 // TODO(b/144707443): Tune constants.
165 constexpr size_t pendingFenceLimit = 20;
166 auto sync = std::make_unique<scheduler::VSyncReactor>(std::move(clock), *dispatch, *tracker,
167 pendingFenceLimit,
168 options.supportKernelTimer);
169 return {std::move(sync), std::move(tracker), std::move(dispatch)};
170}
171
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700172std::unique_ptr<LayerHistory> Scheduler::createLayerHistory(
173 const scheduler::RefreshRateConfigs& configs, bool useContentDetectionV2) {
174 if (!configs.canSwitch()) return nullptr;
175
176 if (useContentDetectionV2) {
177 return std::make_unique<scheduler::impl::LayerHistoryV2>(configs);
178 }
179
180 return std::make_unique<scheduler::impl::LayerHistory>();
181}
182
Dominik Laskowski98041832019-08-01 18:35:59 -0700183DispSync& Scheduler::getPrimaryDispSync() {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700184 return *mVsyncSchedule.sync;
Dominik Laskowski98041832019-08-01 18:35:59 -0700185}
186
Ady Abraham9e16a482019-12-03 17:19:41 -0800187std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(const char* name,
188 nsecs_t phaseOffsetNs) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700189 return std::make_unique<DispSyncSource>(&getPrimaryDispSync(), phaseOffsetNs,
Ady Abraham9e16a482019-12-03 17:19:41 -0800190 true /* traceVsync */, name);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700191}
192
Dominik Laskowski98041832019-08-01 18:35:59 -0700193Scheduler::ConnectionHandle Scheduler::createConnection(
Ady Abraham9e16a482019-12-03 17:19:41 -0800194 const char* connectionName, nsecs_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -0700195 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ady Abraham9e16a482019-12-03 17:19:41 -0800196 auto vsyncSource = makePrimaryDispSyncSource(connectionName, phaseOffsetNs);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700197 auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource),
198 std::move(interceptCallback));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700199 return createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700200}
Ana Krulec98b5b242018-08-10 15:03:23 -0700201
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700202Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700203 const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
204 ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800205
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700206 auto connection =
207 createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowski98041832019-08-01 18:35:59 -0700208
209 mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
210 return handle;
Ana Krulec98b5b242018-08-10 15:03:23 -0700211}
212
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700213sp<EventThreadConnection> Scheduler::createConnectionInternal(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700214 EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) {
215 return eventThread->createEventConnection([&] { resync(); }, configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700216}
217
Ana Krulec98b5b242018-08-10 15:03:23 -0700218sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700219 ConnectionHandle handle, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700220 RETURN_IF_INVALID_HANDLE(handle, nullptr);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700221 return createConnectionInternal(mConnections[handle].thread.get(), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700222}
223
Dominik Laskowski98041832019-08-01 18:35:59 -0700224sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
225 RETURN_IF_INVALID_HANDLE(handle, nullptr);
226 return mConnections[handle].connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700227}
228
Dominik Laskowski98041832019-08-01 18:35:59 -0700229void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
230 bool connected) {
231 RETURN_IF_INVALID_HANDLE(handle);
232 mConnections[handle].thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700233}
234
Dominik Laskowski98041832019-08-01 18:35:59 -0700235void Scheduler::onScreenAcquired(ConnectionHandle handle) {
236 RETURN_IF_INVALID_HANDLE(handle);
237 mConnections[handle].thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700238}
239
Dominik Laskowski98041832019-08-01 18:35:59 -0700240void Scheduler::onScreenReleased(ConnectionHandle handle) {
241 RETURN_IF_INVALID_HANDLE(handle);
242 mConnections[handle].thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700243}
244
Ady Abrahamdfd62162020-06-10 16:11:56 -0700245void Scheduler::onPrimaryDisplayConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId,
246 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
247 std::lock_guard<std::mutex> lock(mFeatureStateLock);
248 // Cache the last reported config for primary display.
249 mFeatures.cachedConfigChangedParams = {handle, displayId, configId, vsyncPeriod};
250 onNonPrimaryDisplayConfigChanged(handle, displayId, configId, vsyncPeriod);
251}
252
253void Scheduler::dispatchCachedReportedConfig() {
254 const auto configId = *mFeatures.configId;
255 const auto vsyncPeriod =
256 mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getVsyncPeriod();
257
258 // If there is no change from cached config, there is no need to dispatch an event
259 if (configId == mFeatures.cachedConfigChangedParams->configId &&
260 vsyncPeriod == mFeatures.cachedConfigChangedParams->vsyncPeriod) {
261 return;
262 }
263
264 mFeatures.cachedConfigChangedParams->configId = configId;
265 mFeatures.cachedConfigChangedParams->vsyncPeriod = vsyncPeriod;
266 onNonPrimaryDisplayConfigChanged(mFeatures.cachedConfigChangedParams->handle,
267 mFeatures.cachedConfigChangedParams->displayId,
268 mFeatures.cachedConfigChangedParams->configId,
269 mFeatures.cachedConfigChangedParams->vsyncPeriod);
270}
271
272void Scheduler::onNonPrimaryDisplayConfigChanged(ConnectionHandle handle,
273 PhysicalDisplayId displayId,
274 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700275 RETURN_IF_INVALID_HANDLE(handle);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700276 mConnections[handle].thread->onConfigChanged(displayId, configId, vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800277}
278
Alec Mouri717bcb62020-02-10 17:07:19 -0800279size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) {
280 RETURN_IF_INVALID_HANDLE(handle, 0);
281 return mConnections[handle].thread->getEventThreadConnectionCount();
282}
283
Dominik Laskowski98041832019-08-01 18:35:59 -0700284void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
285 RETURN_IF_INVALID_HANDLE(handle);
286 mConnections.at(handle).thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700287}
288
Dominik Laskowski98041832019-08-01 18:35:59 -0700289void Scheduler::setPhaseOffset(ConnectionHandle handle, nsecs_t phaseOffset) {
290 RETURN_IF_INVALID_HANDLE(handle);
291 mConnections[handle].thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700292}
Ana Krulece588e312018-09-18 12:32:24 -0700293
294void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700295 stats->vsyncTime = getPrimaryDispSync().computeNextRefresh(0, systemTime());
296 stats->vsyncPeriod = getPrimaryDispSync().getPeriod();
Ana Krulece588e312018-09-18 12:32:24 -0700297}
298
Dominik Laskowski6505f792019-09-18 11:10:05 -0700299Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
300 if (mInjectVSyncs == enable) {
301 return {};
302 }
303
304 ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling");
305
306 if (!mInjectorConnectionHandle) {
307 auto vsyncSource = std::make_unique<InjectVSyncSource>();
308 mVSyncInjector = vsyncSource.get();
309
310 auto eventThread =
311 std::make_unique<impl::EventThread>(std::move(vsyncSource),
312 impl::EventThread::InterceptVSyncsCallback());
313
314 mInjectorConnectionHandle = createConnection(std::move(eventThread));
315 }
316
317 mInjectVSyncs = enable;
318 return mInjectorConnectionHandle;
319}
320
Ady Abraham5facfb12020-04-22 15:18:31 -0700321bool Scheduler::injectVSync(nsecs_t when, nsecs_t expectedVSyncTime) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700322 if (!mInjectVSyncs || !mVSyncInjector) {
323 return false;
324 }
325
Ady Abraham5facfb12020-04-22 15:18:31 -0700326 mVSyncInjector->onInjectSyncEvent(when, expectedVSyncTime);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700327 return true;
328}
329
Ana Krulece588e312018-09-18 12:32:24 -0700330void Scheduler::enableHardwareVsync() {
331 std::lock_guard<std::mutex> lock(mHWVsyncLock);
332 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700333 getPrimaryDispSync().beginResync();
334 mSchedulerCallback.setVsyncEnabled(true);
Ana Krulece588e312018-09-18 12:32:24 -0700335 mPrimaryHWVsyncEnabled = true;
336 }
337}
338
339void Scheduler::disableHardwareVsync(bool makeUnavailable) {
340 std::lock_guard<std::mutex> lock(mHWVsyncLock);
341 if (mPrimaryHWVsyncEnabled) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700342 mSchedulerCallback.setVsyncEnabled(false);
343 getPrimaryDispSync().endResync();
Ana Krulece588e312018-09-18 12:32:24 -0700344 mPrimaryHWVsyncEnabled = false;
345 }
346 if (makeUnavailable) {
347 mHWVsyncAvailable = false;
348 }
349}
350
Ana Krulecc2870422019-01-29 19:00:58 -0800351void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
352 {
353 std::lock_guard<std::mutex> lock(mHWVsyncLock);
354 if (makeAvailable) {
355 mHWVsyncAvailable = makeAvailable;
356 } else if (!mHWVsyncAvailable) {
357 // Hardware vsync is not currently available, so abort the resync
358 // attempt for now
359 return;
360 }
361 }
362
363 if (period <= 0) {
364 return;
365 }
366
367 setVsyncPeriod(period);
368}
369
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700370void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700371 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800372
373 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700374 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800375
376 if (now - last > kIgnoreDelay) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700377 resyncToHardwareVsync(false, mRefreshRateConfigs.getCurrentRefreshRate().getVsyncPeriod());
Ana Krulecc2870422019-01-29 19:00:58 -0800378 }
379}
380
Dominik Laskowski98041832019-08-01 18:35:59 -0700381void Scheduler::setVsyncPeriod(nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800382 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700383 getPrimaryDispSync().setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800384
385 if (!mPrimaryHWVsyncEnabled) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700386 getPrimaryDispSync().beginResync();
387 mSchedulerCallback.setVsyncEnabled(true);
Ady Abraham3aff9172019-02-07 19:10:26 -0800388 mPrimaryHWVsyncEnabled = true;
389 }
Ana Krulece588e312018-09-18 12:32:24 -0700390}
391
Ady Abraham5dee2f12020-02-05 17:49:47 -0800392void Scheduler::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
393 bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700394 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700395 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700396 { // Scope for the lock
397 std::lock_guard<std::mutex> lock(mHWVsyncLock);
398 if (mPrimaryHWVsyncEnabled) {
Ady Abraham5dee2f12020-02-05 17:49:47 -0800399 needsHwVsync =
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700400 getPrimaryDispSync().addResyncSample(timestamp, hwcVsyncPeriod, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700401 }
402 }
403
404 if (needsHwVsync) {
405 enableHardwareVsync();
406 } else {
407 disableHardwareVsync(false);
408 }
409}
410
411void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700412 if (getPrimaryDispSync().addPresentFence(fenceTime)) {
Ana Krulece588e312018-09-18 12:32:24 -0700413 enableHardwareVsync();
414 } else {
415 disableHardwareVsync(false);
416 }
417}
418
419void Scheduler::setIgnorePresentFences(bool ignore) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700420 getPrimaryDispSync().setIgnorePresentFences(ignore);
Ana Krulece588e312018-09-18 12:32:24 -0700421}
422
Ady Abraham0ed31c92020-04-16 11:48:45 -0700423nsecs_t Scheduler::getDispSyncExpectedPresentTime(nsecs_t now) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700424 return getPrimaryDispSync().expectedPresentTime(now);
Ady Abrahamc3e21312019-02-07 14:30:23 -0800425}
426
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700427void Scheduler::registerLayer(Layer* layer) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800428 if (!mLayerHistory) return;
429
Steven Thomasdebafed2020-05-18 17:30:35 -0700430 const auto minFps = mRefreshRateConfigs.getMinRefreshRate().getFps();
431 const auto maxFps = mRefreshRateConfigs.getMaxRefreshRate().getFps();
432
Michael Wright44753b12020-07-08 13:48:11 +0100433 if (layer->getWindowType() == InputWindowInfo::Type::STATUS_BAR) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700434 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ana Krulec3d367c82020-02-25 15:02:01 -0800435 scheduler::LayerHistory::LayerVoteType::NoVote);
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700436 } else if (!mOptions.useContentDetection) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700437 // If the content detection feature is off, all layers are registered at Max. We still keep
438 // the layer history, since we use it for other features (like Frame Rate API), so layers
439 // still need to be registered.
440 mLayerHistory->registerLayer(layer, minFps, maxFps,
441 scheduler::LayerHistory::LayerVoteType::Max);
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700442 } else if (!mOptions.useContentDetectionV2) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700443 // In V1 of content detection, all layers are registered as Heuristic (unless it's
444 // wallpaper).
445 const auto highFps =
Michael Wright44753b12020-07-08 13:48:11 +0100446 layer->getWindowType() == InputWindowInfo::Type::WALLPAPER ? minFps : maxFps;
Ana Krulec3d367c82020-02-25 15:02:01 -0800447
Steven Thomasdebafed2020-05-18 17:30:35 -0700448 mLayerHistory->registerLayer(layer, minFps, highFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800449 scheduler::LayerHistory::LayerVoteType::Heuristic);
450 } else {
Michael Wright44753b12020-07-08 13:48:11 +0100451 if (layer->getWindowType() == InputWindowInfo::Type::WALLPAPER) {
Ana Krulec3d367c82020-02-25 15:02:01 -0800452 // Running Wallpaper at Min is considered as part of content detection.
Steven Thomasdebafed2020-05-18 17:30:35 -0700453 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800454 scheduler::LayerHistory::LayerVoteType::Min);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800455 } else {
Steven Thomasdebafed2020-05-18 17:30:35 -0700456 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800457 scheduler::LayerHistory::LayerVoteType::Heuristic);
458 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800459 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700460}
461
Ady Abraham5def7332020-05-29 16:13:47 -0700462void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime,
463 LayerHistory::LayerUpdateType updateType) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800464 if (mLayerHistory) {
Ady Abraham5def7332020-05-29 16:13:47 -0700465 mLayerHistory->record(layer, presentTime, systemTime(), updateType);
Dominik Laskowski49cea512019-11-12 14:13:23 -0800466 }
Ana Krulec3084c052018-11-21 20:27:17 +0100467}
468
Ady Abraham32efd542020-05-19 17:49:26 -0700469void Scheduler::setConfigChangePending(bool pending) {
470 if (mLayerHistory) {
471 mLayerHistory->setConfigChangePending(pending);
472 }
473}
474
Dominik Laskowski49cea512019-11-12 14:13:23 -0800475void Scheduler::chooseRefreshRateForContent() {
476 if (!mLayerHistory) return;
477
Ady Abraham8a82ba62020-01-17 12:43:17 -0800478 ATRACE_CALL();
479
480 scheduler::LayerHistory::Summary summary = mLayerHistory->summarize(systemTime());
Ady Abraham2139f732019-11-13 18:56:40 -0800481 HwcConfigIndexType newConfigId;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700482 {
483 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800484 if (mFeatures.contentRequirements == summary) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700485 return;
486 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800487 mFeatures.contentRequirements = summary;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800488 mFeatures.contentDetectionV1 =
Ady Abraham8a82ba62020-01-17 12:43:17 -0800489 !summary.empty() ? ContentDetectionState::On : ContentDetectionState::Off;
490
Ady Abrahamdfd62162020-06-10 16:11:56 -0700491 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
492 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800493 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700494 // We don't need to change the config, but we might need to send an event
495 // about a config change, since it was suppressed due to a previous idleConsidered
496 if (!consideredSignals.idle) {
497 dispatchCachedReportedConfig();
498 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700499 return;
500 }
Ady Abraham2139f732019-11-13 18:56:40 -0800501 mFeatures.configId = newConfigId;
Ady Abraham2e1dd892020-03-05 13:48:36 -0800502 auto& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700503 mSchedulerCallback.changeRefreshRate(newRefreshRate,
504 consideredSignals.idle ? ConfigEvent::None
505 : ConfigEvent::Changed);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800506 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800507}
508
Ana Krulecfb772822018-11-30 10:44:07 +0100509void Scheduler::resetIdleTimer() {
510 if (mIdleTimer) {
511 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800512 }
513}
514
Ady Abraham8532d012019-05-08 14:50:56 -0700515void Scheduler::notifyTouchEvent() {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700516 if (mTouchTimer) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800517 mTouchTimer->reset();
Steven Thomas540730a2020-01-08 20:12:42 -0800518
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700519 if (mOptions.supportKernelTimer && mIdleTimer) {
Steven Thomas540730a2020-01-08 20:12:42 -0800520 mIdleTimer->reset();
521 }
Dominik Laskowski49cea512019-11-12 14:13:23 -0800522 }
Ady Abraham8532d012019-05-08 14:50:56 -0700523}
524
Ady Abraham6fe2c172019-07-12 12:37:57 -0700525void Scheduler::setDisplayPowerState(bool normal) {
526 {
527 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700528 mFeatures.isDisplayPowerStateNormal = normal;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700529 }
530
531 if (mDisplayPowerTimer) {
532 mDisplayPowerTimer->reset();
533 }
534
535 // Display Power event will boost the refresh rate to performance.
536 // Clear Layer History to get fresh FPS detection
Dominik Laskowski49cea512019-11-12 14:13:23 -0800537 if (mLayerHistory) {
538 mLayerHistory->clear();
539 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700540}
541
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700542void Scheduler::kernelIdleTimerCallback(TimerState state) {
543 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100544
Ady Abraham2139f732019-11-13 18:56:40 -0800545 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
546 // magic number
Ady Abraham2e1dd892020-03-05 13:48:36 -0800547 const auto& refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
Ady Abraham2139f732019-11-13 18:56:40 -0800548 constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f;
Ady Abrahamabc27602020-04-08 17:20:29 -0700549 if (state == TimerState::Reset && refreshRate.getFps() > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700550 // If we're not in performance mode then the kernel timer shouldn't do
551 // anything, as the refresh rate during DPU power collapse will be the
552 // same.
Ady Abrahamabc27602020-04-08 17:20:29 -0700553 resyncToHardwareVsync(true /* makeAvailable */, refreshRate.getVsyncPeriod());
554 } else if (state == TimerState::Expired &&
555 refreshRate.getFps() <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700556 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
557 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
558 // need to update the DispSync model anyway.
559 disableHardwareVsync(false /* makeUnavailable */);
Alec Mouridc28b372019-04-18 21:17:13 -0700560 }
Ady Abrahama09852a2020-02-20 14:23:42 -0800561
562 mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
Alec Mouridc28b372019-04-18 21:17:13 -0700563}
564
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700565void Scheduler::idleTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700566 handleTimerStateChanged(&mFeatures.idleTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700567 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100568}
569
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700570void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700571 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700572 // Touch event will boost the refresh rate to performance.
573 // Clear layer history to get fresh FPS detection.
574 // NOTE: Instead of checking all the layers, we should be checking the layer
575 // that is currently on top. b/142507166 will give us this capability.
Ady Abrahamdfd62162020-06-10 16:11:56 -0700576 if (handleTimerStateChanged(&mFeatures.touch, touch)) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700577 if (mLayerHistory) {
578 mLayerHistory->clear();
579 }
Ady Abraham1adbb722020-05-15 11:51:48 -0700580 }
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700581 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700582}
583
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700584void Scheduler::displayPowerTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700585 handleTimerStateChanged(&mFeatures.displayPowerTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700586 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700587}
588
Dominik Laskowski98041832019-08-01 18:35:59 -0700589void Scheduler::dump(std::string& result) const {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800590 using base::StringAppendF;
Dominik Laskowski98041832019-08-01 18:35:59 -0700591
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700592 StringAppendF(&result, "+ Idle timer: %s\n", mIdleTimer ? mIdleTimer->dump().c_str() : "off");
Ana Krulec3d367c82020-02-25 15:02:01 -0800593 StringAppendF(&result, "+ Touch timer: %s\n",
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700594 mTouchTimer ? mTouchTimer->dump().c_str() : "off");
595 StringAppendF(&result, "+ Content detection: %s %s\n\n",
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700596 toContentDetectionString(mOptions.useContentDetection,
597 mOptions.useContentDetectionV2),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700598 mLayerHistory ? mLayerHistory->dump().c_str() : "(no layer history)");
Ana Krulecb43429d2019-01-09 14:28:51 -0800599}
600
Ady Abraham6fe2c172019-07-12 12:37:57 -0700601template <class T>
Ady Abrahamdfd62162020-06-10 16:11:56 -0700602bool Scheduler::handleTimerStateChanged(T* currentState, T newState) {
Ady Abraham2139f732019-11-13 18:56:40 -0800603 HwcConfigIndexType newConfigId;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700604 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
Ady Abraham8532d012019-05-08 14:50:56 -0700605 {
606 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700607 if (*currentState == newState) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700608 return false;
Ady Abraham8532d012019-05-08 14:50:56 -0700609 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700610 *currentState = newState;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700611 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800612 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700613 // We don't need to change the config, but we might need to send an event
614 // about a config change, since it was suppressed due to a previous idleConsidered
615 if (!consideredSignals.idle) {
616 dispatchCachedReportedConfig();
617 }
618 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700619 }
Ady Abraham2139f732019-11-13 18:56:40 -0800620 mFeatures.configId = newConfigId;
Ady Abraham8532d012019-05-08 14:50:56 -0700621 }
Ady Abraham2139f732019-11-13 18:56:40 -0800622 const RefreshRate& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700623 mSchedulerCallback.changeRefreshRate(newRefreshRate,
624 consideredSignals.idle ? ConfigEvent::None
625 : ConfigEvent::Changed);
626 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700627}
628
Ady Abrahamdfd62162020-06-10 16:11:56 -0700629HwcConfigIndexType Scheduler::calculateRefreshRateConfigIndexType(
630 scheduler::RefreshRateConfigs::GlobalSignals* consideredSignals) {
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800631 ATRACE_CALL();
Ady Abrahamdfd62162020-06-10 16:11:56 -0700632 if (consideredSignals) *consideredSignals = {};
Ady Abraham09bd3922019-04-08 10:44:56 -0700633
Steven Thomasf734df42020-04-13 21:09:28 -0700634 // If Display Power is not in normal operation we want to be in performance mode. When coming
635 // back to normal mode, a grace period is given with DisplayPowerTimer.
Ana Krulec3f6a2062020-01-23 15:48:01 -0800636 if (mDisplayPowerTimer &&
637 (!mFeatures.isDisplayPowerStateNormal ||
638 mFeatures.displayPowerTimer == TimerState::Reset)) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700639 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800640 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700641
Steven Thomasbb374322020-04-28 22:47:16 -0700642 const bool touchActive = mTouchTimer && mFeatures.touch == TouchState::Active;
643 const bool idle = mIdleTimer && mFeatures.idleTimer == TimerState::Expired;
644
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700645 if (!mOptions.useContentDetectionV2) {
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800646 // As long as touch is active we want to be in performance mode.
Steven Thomasbb374322020-04-28 22:47:16 -0700647 if (touchActive) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700648 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800649 }
Ady Abraham8532d012019-05-08 14:50:56 -0700650
Steven Thomasbb374322020-04-28 22:47:16 -0700651 // If timer has expired as it means there is no new content on the screen.
652 if (idle) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700653 if (consideredSignals) consideredSignals->idle = true;
Steven Thomasbb374322020-04-28 22:47:16 -0700654 return mRefreshRateConfigs.getMinRefreshRateByPolicy().getConfigId();
655 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700656
Ana Krulec3f6a2062020-01-23 15:48:01 -0800657 // If content detection is off we choose performance as we don't know the content fps.
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800658 if (mFeatures.contentDetectionV1 == ContentDetectionState::Off) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800659 // NOTE: V1 always calls this, but this is not a default behavior for V2.
Ady Abrahamabc27602020-04-08 17:20:29 -0700660 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Steven Thomas540730a2020-01-08 20:12:42 -0800661 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800662
663 // Content detection is on, find the appropriate refresh rate with minimal error
Ady Abrahamabc27602020-04-08 17:20:29 -0700664 return mRefreshRateConfigs.getRefreshRateForContent(mFeatures.contentRequirements)
665 .getConfigId();
Ady Abraham09bd3922019-04-08 10:44:56 -0700666 }
667
Ady Abraham1adbb722020-05-15 11:51:48 -0700668 return mRefreshRateConfigs
Ady Abrahamdfd62162020-06-10 16:11:56 -0700669 .getBestRefreshRate(mFeatures.contentRequirements, {.touch = touchActive, .idle = idle},
670 consideredSignals)
Ady Abraham1adbb722020-05-15 11:51:48 -0700671 .getConfigId();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800672}
673
Ady Abraham2139f732019-11-13 18:56:40 -0800674std::optional<HwcConfigIndexType> Scheduler::getPreferredConfigId() {
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700675 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800676 // Make sure that the default config ID is first updated, before returned.
677 if (mFeatures.configId.has_value()) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800678 mFeatures.configId = calculateRefreshRateConfigIndexType();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800679 }
Ady Abraham2139f732019-11-13 18:56:40 -0800680 return mFeatures.configId;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700681}
682
Peiyong Line9d809e2020-04-14 13:10:48 -0700683void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800684 if (timeline.refreshRequired) {
685 mSchedulerCallback.repaintEverythingForHWC();
686 }
687
688 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
689 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
690
691 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
692 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
693 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
694 }
695}
696
697void Scheduler::onDisplayRefreshed(nsecs_t timestamp) {
698 bool callRepaint = false;
699 {
700 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
701 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
702 if (mLastVsyncPeriodChangeTimeline->refreshTimeNanos < timestamp) {
703 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
704 } else {
705 // We need to send another refresh as refreshTimeNanos is still in the future
706 callRepaint = true;
707 }
708 }
709 }
710
711 if (callRepaint) {
712 mSchedulerCallback.repaintEverythingForHWC();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800713 }
714}
715
Ady Abraham8a82ba62020-01-17 12:43:17 -0800716void Scheduler::onPrimaryDisplayAreaChanged(uint32_t displayArea) {
717 if (mLayerHistory) {
718 mLayerHistory->setDisplayArea(displayArea);
719 }
720}
721
Ana Krulec98b5b242018-08-10 15:03:23 -0700722} // namespace android