blob: b53e2dc57a335f3615dc4e167d9a3e8043ec4842 [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 {
Ady Abraham5a858552020-03-31 17:54:56 -070064
Dominik Laskowski983f2b52020-06-25 16:54:06 -070065namespace {
Ana Krulec98b5b242018-08-10 15:03:23 -070066
Ady Abraham5a858552020-03-31 17:54:56 -070067std::unique_ptr<scheduler::VSyncTracker> createVSyncTracker() {
68 // TODO (144707443) tune Predictor tunables.
69 static constexpr int default_rate = 60;
70 static constexpr auto initial_period =
71 std::chrono::duration<nsecs_t, std::ratio<1, default_rate>>(1);
72 static constexpr size_t vsyncTimestampHistorySize = 20;
73 static constexpr size_t minimumSamplesForPrediction = 6;
74 static constexpr uint32_t discardOutlierPercent = 20;
75 return std::make_unique<
76 scheduler::VSyncPredictor>(std::chrono::duration_cast<std::chrono::nanoseconds>(
77 initial_period)
78 .count(),
79 vsyncTimestampHistorySize, minimumSamplesForPrediction,
80 discardOutlierPercent);
81}
82
83std::unique_ptr<scheduler::VSyncDispatch> createVSyncDispatch(
84 const std::unique_ptr<scheduler::VSyncTracker>& vSyncTracker) {
85 if (!vSyncTracker) return {};
86
87 // TODO (144707443) tune Predictor tunables.
88 static constexpr auto vsyncMoveThreshold =
89 std::chrono::duration_cast<std::chrono::nanoseconds>(3ms);
90 static constexpr auto timerSlack = std::chrono::duration_cast<std::chrono::nanoseconds>(500us);
91 return std::make_unique<
92 scheduler::VSyncDispatchTimerQueue>(std::make_unique<scheduler::Timer>(), *vSyncTracker,
93 timerSlack.count(), vsyncMoveThreshold.count());
94}
95
96std::unique_ptr<DispSync> createDispSync(
97 const std::unique_ptr<scheduler::VSyncTracker>& vSyncTracker,
98 const std::unique_ptr<scheduler::VSyncDispatch>& vSyncDispatch, bool supportKernelTimer) {
99 if (vSyncTracker && vSyncDispatch) {
Kevin DuBois00287382019-11-19 15:11:55 -0800100 // TODO (144707443) tune Predictor tunables.
Kevin DuBois00287382019-11-19 15:11:55 -0800101 static constexpr size_t pendingFenceLimit = 20;
102 return std::make_unique<scheduler::VSyncReactor>(std::make_unique<scheduler::SystemClock>(),
Ady Abraham5a858552020-03-31 17:54:56 -0700103 *vSyncDispatch, *vSyncTracker,
Dan Stoza027d3652020-05-26 17:26:34 -0700104 pendingFenceLimit, supportKernelTimer);
Kevin DuBois00287382019-11-19 15:11:55 -0800105 } else {
106 return std::make_unique<impl::DispSync>("SchedulerDispSync",
107 sysprop::running_without_sync_framework(true));
108 }
109}
110
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700111const char* toContentDetectionString(bool useContentDetection, bool useContentDetectionV2) {
112 if (!useContentDetection) return "off";
113 return useContentDetectionV2 ? "V2" : "V1";
114}
115
116} // namespace
117
Ady Abraham09bd3922019-04-08 10:44:56 -0700118Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function,
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700119 const scheduler::RefreshRateConfigs& refreshRateConfigs,
Ana Krulec3d367c82020-02-25 15:02:01 -0800120 ISchedulerCallback& schedulerCallback, bool useContentDetectionV2,
121 bool useContentDetection)
Dan Stoza027d3652020-05-26 17:26:34 -0700122 : mSupportKernelTimer(sysprop::support_kernel_idle_timer(false)),
Ady Abraham5a858552020-03-31 17:54:56 -0700123 // TODO (140302863) remove this and use the vsync_reactor system.
124 mUseVsyncPredictor(property_get_bool("debug.sf.vsync_reactor", true)),
125 mVSyncTracker(mUseVsyncPredictor ? createVSyncTracker() : nullptr),
126 mVSyncDispatch(createVSyncDispatch(mVSyncTracker)),
127 mPrimaryDispSync(createDispSync(mVSyncTracker, mVSyncDispatch, mSupportKernelTimer)),
Dominik Laskowski98041832019-08-01 18:35:59 -0700128 mEventControlThread(new impl::EventControlThread(std::move(function))),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700129 mLayerHistory(createLayerHistory(refreshRateConfigs, useContentDetectionV2)),
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800130 mSchedulerCallback(schedulerCallback),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700131 mRefreshRateConfigs(refreshRateConfigs),
Ana Krulec3d367c82020-02-25 15:02:01 -0800132 mUseContentDetection(useContentDetection),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800133 mUseContentDetectionV2(useContentDetectionV2) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700134 using namespace sysprop;
Ady Abraham8532d012019-05-08 14:50:56 -0700135
Dominik Laskowski49cea512019-11-12 14:13:23 -0800136 const int setIdleTimerMs = property_get_int32("debug.sf.set_idle_timer_ms", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100137
Dominik Laskowski98041832019-08-01 18:35:59 -0700138 if (const auto millis = setIdleTimerMs ? setIdleTimerMs : set_idle_timer_ms(0); millis > 0) {
139 const auto callback = mSupportKernelTimer ? &Scheduler::kernelIdleTimerCallback
140 : &Scheduler::idleTimerCallback;
Dominik Laskowski98041832019-08-01 18:35:59 -0700141 mIdleTimer.emplace(
142 std::chrono::milliseconds(millis),
143 [this, callback] { std::invoke(callback, this, TimerState::Reset); },
144 [this, callback] { std::invoke(callback, this, TimerState::Expired); });
Ana Krulecfb772822018-11-30 10:44:07 +0100145 mIdleTimer->start();
146 }
Ady Abraham8532d012019-05-08 14:50:56 -0700147
Dominik Laskowski98041832019-08-01 18:35:59 -0700148 if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -0700149 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -0700150 mTouchTimer.emplace(
151 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700152 [this] { touchTimerCallback(TimerState::Reset); },
153 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -0700154 mTouchTimer->start();
155 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700156
Dominik Laskowski98041832019-08-01 18:35:59 -0700157 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
158 mDisplayPowerTimer.emplace(
159 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700160 [this] { displayPowerTimerCallback(TimerState::Reset); },
161 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -0700162 mDisplayPowerTimer->start();
163 }
Ana Krulece588e312018-09-18 12:32:24 -0700164}
165
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700166Scheduler::Scheduler(std::unique_ptr<DispSync> primaryDispSync,
167 std::unique_ptr<EventControlThread> eventControlThread,
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800168 const scheduler::RefreshRateConfigs& configs,
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700169 ISchedulerCallback& schedulerCallback,
170 std::unique_ptr<LayerHistory> layerHistory, bool useContentDetectionV2,
Ana Krulec3d367c82020-02-25 15:02:01 -0800171 bool useContentDetection)
Dan Stoza027d3652020-05-26 17:26:34 -0700172 : mSupportKernelTimer(false),
Ady Abraham5a858552020-03-31 17:54:56 -0700173 mUseVsyncPredictor(true),
Dan Stoza027d3652020-05-26 17:26:34 -0700174 mPrimaryDispSync(std::move(primaryDispSync)),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700175 mEventControlThread(std::move(eventControlThread)),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700176 mLayerHistory(std::move(layerHistory)),
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800177 mSchedulerCallback(schedulerCallback),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800178 mRefreshRateConfigs(configs),
Ana Krulec3d367c82020-02-25 15:02:01 -0800179 mUseContentDetection(useContentDetection),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800180 mUseContentDetectionV2(useContentDetectionV2) {}
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700181
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800182Scheduler::~Scheduler() {
Ana Krulecf2c006d2019-06-21 15:37:07 -0700183 // Ensure the OneShotTimer threads are joined before we start destroying state.
Ady Abraham6fe2c172019-07-12 12:37:57 -0700184 mDisplayPowerTimer.reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700185 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800186 mIdleTimer.reset();
187}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700188
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700189std::unique_ptr<LayerHistory> Scheduler::createLayerHistory(
190 const scheduler::RefreshRateConfigs& configs, bool useContentDetectionV2) {
191 if (!configs.canSwitch()) return nullptr;
192
193 if (useContentDetectionV2) {
194 return std::make_unique<scheduler::impl::LayerHistoryV2>(configs);
195 }
196
197 return std::make_unique<scheduler::impl::LayerHistory>();
198}
199
Dominik Laskowski98041832019-08-01 18:35:59 -0700200DispSync& Scheduler::getPrimaryDispSync() {
201 return *mPrimaryDispSync;
202}
203
Ady Abraham9e16a482019-12-03 17:19:41 -0800204std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(const char* name,
205 nsecs_t phaseOffsetNs) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700206 return std::make_unique<DispSyncSource>(mPrimaryDispSync.get(), phaseOffsetNs,
Ady Abraham9e16a482019-12-03 17:19:41 -0800207 true /* traceVsync */, name);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700208}
209
Dominik Laskowski98041832019-08-01 18:35:59 -0700210Scheduler::ConnectionHandle Scheduler::createConnection(
Ady Abraham9e16a482019-12-03 17:19:41 -0800211 const char* connectionName, nsecs_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -0700212 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ady Abraham9e16a482019-12-03 17:19:41 -0800213 auto vsyncSource = makePrimaryDispSyncSource(connectionName, phaseOffsetNs);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700214 auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource),
215 std::move(interceptCallback));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700216 return createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700217}
Ana Krulec98b5b242018-08-10 15:03:23 -0700218
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700219Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700220 const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
221 ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800222
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700223 auto connection =
224 createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowski98041832019-08-01 18:35:59 -0700225
226 mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
227 return handle;
Ana Krulec98b5b242018-08-10 15:03:23 -0700228}
229
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700230sp<EventThreadConnection> Scheduler::createConnectionInternal(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700231 EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) {
232 return eventThread->createEventConnection([&] { resync(); }, configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700233}
234
Ana Krulec98b5b242018-08-10 15:03:23 -0700235sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700236 ConnectionHandle handle, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700237 RETURN_IF_INVALID_HANDLE(handle, nullptr);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700238 return createConnectionInternal(mConnections[handle].thread.get(), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700239}
240
Dominik Laskowski98041832019-08-01 18:35:59 -0700241sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
242 RETURN_IF_INVALID_HANDLE(handle, nullptr);
243 return mConnections[handle].connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700244}
245
Dominik Laskowski98041832019-08-01 18:35:59 -0700246void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
247 bool connected) {
248 RETURN_IF_INVALID_HANDLE(handle);
249 mConnections[handle].thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700250}
251
Dominik Laskowski98041832019-08-01 18:35:59 -0700252void Scheduler::onScreenAcquired(ConnectionHandle handle) {
253 RETURN_IF_INVALID_HANDLE(handle);
254 mConnections[handle].thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700255}
256
Dominik Laskowski98041832019-08-01 18:35:59 -0700257void Scheduler::onScreenReleased(ConnectionHandle handle) {
258 RETURN_IF_INVALID_HANDLE(handle);
259 mConnections[handle].thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700260}
261
Ady Abrahamdfd62162020-06-10 16:11:56 -0700262void Scheduler::onPrimaryDisplayConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId,
263 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
264 std::lock_guard<std::mutex> lock(mFeatureStateLock);
265 // Cache the last reported config for primary display.
266 mFeatures.cachedConfigChangedParams = {handle, displayId, configId, vsyncPeriod};
267 onNonPrimaryDisplayConfigChanged(handle, displayId, configId, vsyncPeriod);
268}
269
270void Scheduler::dispatchCachedReportedConfig() {
271 const auto configId = *mFeatures.configId;
272 const auto vsyncPeriod =
273 mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getVsyncPeriod();
274
275 // If there is no change from cached config, there is no need to dispatch an event
276 if (configId == mFeatures.cachedConfigChangedParams->configId &&
277 vsyncPeriod == mFeatures.cachedConfigChangedParams->vsyncPeriod) {
278 return;
279 }
280
281 mFeatures.cachedConfigChangedParams->configId = configId;
282 mFeatures.cachedConfigChangedParams->vsyncPeriod = vsyncPeriod;
283 onNonPrimaryDisplayConfigChanged(mFeatures.cachedConfigChangedParams->handle,
284 mFeatures.cachedConfigChangedParams->displayId,
285 mFeatures.cachedConfigChangedParams->configId,
286 mFeatures.cachedConfigChangedParams->vsyncPeriod);
287}
288
289void Scheduler::onNonPrimaryDisplayConfigChanged(ConnectionHandle handle,
290 PhysicalDisplayId displayId,
291 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700292 RETURN_IF_INVALID_HANDLE(handle);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700293 mConnections[handle].thread->onConfigChanged(displayId, configId, vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800294}
295
Alec Mouri717bcb62020-02-10 17:07:19 -0800296size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) {
297 RETURN_IF_INVALID_HANDLE(handle, 0);
298 return mConnections[handle].thread->getEventThreadConnectionCount();
299}
300
Dominik Laskowski98041832019-08-01 18:35:59 -0700301void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
302 RETURN_IF_INVALID_HANDLE(handle);
303 mConnections.at(handle).thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700304}
305
Dominik Laskowski98041832019-08-01 18:35:59 -0700306void Scheduler::setPhaseOffset(ConnectionHandle handle, nsecs_t phaseOffset) {
307 RETURN_IF_INVALID_HANDLE(handle);
308 mConnections[handle].thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700309}
Ana Krulece588e312018-09-18 12:32:24 -0700310
311void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
Ady Abraham0ed31c92020-04-16 11:48:45 -0700312 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0, systemTime());
Ana Krulece588e312018-09-18 12:32:24 -0700313 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
314}
315
Dominik Laskowski6505f792019-09-18 11:10:05 -0700316Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
317 if (mInjectVSyncs == enable) {
318 return {};
319 }
320
321 ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling");
322
323 if (!mInjectorConnectionHandle) {
324 auto vsyncSource = std::make_unique<InjectVSyncSource>();
325 mVSyncInjector = vsyncSource.get();
326
327 auto eventThread =
328 std::make_unique<impl::EventThread>(std::move(vsyncSource),
329 impl::EventThread::InterceptVSyncsCallback());
330
331 mInjectorConnectionHandle = createConnection(std::move(eventThread));
332 }
333
334 mInjectVSyncs = enable;
335 return mInjectorConnectionHandle;
336}
337
Ady Abraham5facfb12020-04-22 15:18:31 -0700338bool Scheduler::injectVSync(nsecs_t when, nsecs_t expectedVSyncTime) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700339 if (!mInjectVSyncs || !mVSyncInjector) {
340 return false;
341 }
342
Ady Abraham5facfb12020-04-22 15:18:31 -0700343 mVSyncInjector->onInjectSyncEvent(when, expectedVSyncTime);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700344 return true;
345}
346
Ana Krulece588e312018-09-18 12:32:24 -0700347void Scheduler::enableHardwareVsync() {
348 std::lock_guard<std::mutex> lock(mHWVsyncLock);
349 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
350 mPrimaryDispSync->beginResync();
351 mEventControlThread->setVsyncEnabled(true);
352 mPrimaryHWVsyncEnabled = true;
353 }
354}
355
356void Scheduler::disableHardwareVsync(bool makeUnavailable) {
357 std::lock_guard<std::mutex> lock(mHWVsyncLock);
358 if (mPrimaryHWVsyncEnabled) {
359 mEventControlThread->setVsyncEnabled(false);
360 mPrimaryDispSync->endResync();
361 mPrimaryHWVsyncEnabled = false;
362 }
363 if (makeUnavailable) {
364 mHWVsyncAvailable = false;
365 }
366}
367
Ana Krulecc2870422019-01-29 19:00:58 -0800368void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
369 {
370 std::lock_guard<std::mutex> lock(mHWVsyncLock);
371 if (makeAvailable) {
372 mHWVsyncAvailable = makeAvailable;
373 } else if (!mHWVsyncAvailable) {
374 // Hardware vsync is not currently available, so abort the resync
375 // attempt for now
376 return;
377 }
378 }
379
380 if (period <= 0) {
381 return;
382 }
383
384 setVsyncPeriod(period);
385}
386
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700387void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700388 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800389
390 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700391 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800392
393 if (now - last > kIgnoreDelay) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700394 resyncToHardwareVsync(false, mRefreshRateConfigs.getCurrentRefreshRate().getVsyncPeriod());
Ana Krulecc2870422019-01-29 19:00:58 -0800395 }
396}
397
Dominik Laskowski98041832019-08-01 18:35:59 -0700398void Scheduler::setVsyncPeriod(nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800399 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700400 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800401
402 if (!mPrimaryHWVsyncEnabled) {
403 mPrimaryDispSync->beginResync();
404 mEventControlThread->setVsyncEnabled(true);
405 mPrimaryHWVsyncEnabled = true;
406 }
Ana Krulece588e312018-09-18 12:32:24 -0700407}
408
Ady Abraham5dee2f12020-02-05 17:49:47 -0800409void Scheduler::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
410 bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700411 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700412 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700413 { // Scope for the lock
414 std::lock_guard<std::mutex> lock(mHWVsyncLock);
415 if (mPrimaryHWVsyncEnabled) {
Ady Abraham5dee2f12020-02-05 17:49:47 -0800416 needsHwVsync =
417 mPrimaryDispSync->addResyncSample(timestamp, hwcVsyncPeriod, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700418 }
419 }
420
421 if (needsHwVsync) {
422 enableHardwareVsync();
423 } else {
424 disableHardwareVsync(false);
425 }
426}
427
428void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
429 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
430 enableHardwareVsync();
431 } else {
432 disableHardwareVsync(false);
433 }
434}
435
436void Scheduler::setIgnorePresentFences(bool ignore) {
437 mPrimaryDispSync->setIgnorePresentFences(ignore);
438}
439
Ady Abraham0ed31c92020-04-16 11:48:45 -0700440nsecs_t Scheduler::getDispSyncExpectedPresentTime(nsecs_t now) {
441 return mPrimaryDispSync->expectedPresentTime(now);
Ady Abrahamc3e21312019-02-07 14:30:23 -0800442}
443
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700444void Scheduler::registerLayer(Layer* layer) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800445 if (!mLayerHistory) return;
446
Steven Thomasdebafed2020-05-18 17:30:35 -0700447 const auto minFps = mRefreshRateConfigs.getMinRefreshRate().getFps();
448 const auto maxFps = mRefreshRateConfigs.getMaxRefreshRate().getFps();
449
Michael Wright44753b12020-07-08 13:48:11 +0100450 if (layer->getWindowType() == InputWindowInfo::Type::STATUS_BAR) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700451 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ana Krulec3d367c82020-02-25 15:02:01 -0800452 scheduler::LayerHistory::LayerVoteType::NoVote);
Steven Thomasdebafed2020-05-18 17:30:35 -0700453 } else if (!mUseContentDetection) {
454 // If the content detection feature is off, all layers are registered at Max. We still keep
455 // the layer history, since we use it for other features (like Frame Rate API), so layers
456 // still need to be registered.
457 mLayerHistory->registerLayer(layer, minFps, maxFps,
458 scheduler::LayerHistory::LayerVoteType::Max);
459 } else if (!mUseContentDetectionV2) {
460 // In V1 of content detection, all layers are registered as Heuristic (unless it's
461 // wallpaper).
462 const auto highFps =
Michael Wright44753b12020-07-08 13:48:11 +0100463 layer->getWindowType() == InputWindowInfo::Type::WALLPAPER ? minFps : maxFps;
Ana Krulec3d367c82020-02-25 15:02:01 -0800464
Steven Thomasdebafed2020-05-18 17:30:35 -0700465 mLayerHistory->registerLayer(layer, minFps, highFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800466 scheduler::LayerHistory::LayerVoteType::Heuristic);
467 } else {
Michael Wright44753b12020-07-08 13:48:11 +0100468 if (layer->getWindowType() == InputWindowInfo::Type::WALLPAPER) {
Ana Krulec3d367c82020-02-25 15:02:01 -0800469 // Running Wallpaper at Min is considered as part of content detection.
Steven Thomasdebafed2020-05-18 17:30:35 -0700470 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800471 scheduler::LayerHistory::LayerVoteType::Min);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800472 } else {
Steven Thomasdebafed2020-05-18 17:30:35 -0700473 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800474 scheduler::LayerHistory::LayerVoteType::Heuristic);
475 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800476 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700477}
478
Ady Abraham5def7332020-05-29 16:13:47 -0700479void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime,
480 LayerHistory::LayerUpdateType updateType) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800481 if (mLayerHistory) {
Ady Abraham5def7332020-05-29 16:13:47 -0700482 mLayerHistory->record(layer, presentTime, systemTime(), updateType);
Dominik Laskowski49cea512019-11-12 14:13:23 -0800483 }
Ana Krulec3084c052018-11-21 20:27:17 +0100484}
485
Ady Abraham32efd542020-05-19 17:49:26 -0700486void Scheduler::setConfigChangePending(bool pending) {
487 if (mLayerHistory) {
488 mLayerHistory->setConfigChangePending(pending);
489 }
490}
491
Dominik Laskowski49cea512019-11-12 14:13:23 -0800492void Scheduler::chooseRefreshRateForContent() {
493 if (!mLayerHistory) return;
494
Ady Abraham8a82ba62020-01-17 12:43:17 -0800495 ATRACE_CALL();
496
497 scheduler::LayerHistory::Summary summary = mLayerHistory->summarize(systemTime());
Ady Abraham2139f732019-11-13 18:56:40 -0800498 HwcConfigIndexType newConfigId;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700499 {
500 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800501 if (mFeatures.contentRequirements == summary) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700502 return;
503 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800504 mFeatures.contentRequirements = summary;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800505 mFeatures.contentDetectionV1 =
Ady Abraham8a82ba62020-01-17 12:43:17 -0800506 !summary.empty() ? ContentDetectionState::On : ContentDetectionState::Off;
507
Ady Abrahamdfd62162020-06-10 16:11:56 -0700508 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
509 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800510 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700511 // We don't need to change the config, but we might need to send an event
512 // about a config change, since it was suppressed due to a previous idleConsidered
513 if (!consideredSignals.idle) {
514 dispatchCachedReportedConfig();
515 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700516 return;
517 }
Ady Abraham2139f732019-11-13 18:56:40 -0800518 mFeatures.configId = newConfigId;
Ady Abraham2e1dd892020-03-05 13:48:36 -0800519 auto& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700520 mSchedulerCallback.changeRefreshRate(newRefreshRate,
521 consideredSignals.idle ? ConfigEvent::None
522 : ConfigEvent::Changed);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800523 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800524}
525
Ana Krulecfb772822018-11-30 10:44:07 +0100526void Scheduler::resetIdleTimer() {
527 if (mIdleTimer) {
528 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800529 }
530}
531
Ady Abraham8532d012019-05-08 14:50:56 -0700532void Scheduler::notifyTouchEvent() {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700533 if (mTouchTimer) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800534 mTouchTimer->reset();
Steven Thomas540730a2020-01-08 20:12:42 -0800535
536 if (mSupportKernelTimer && mIdleTimer) {
537 mIdleTimer->reset();
538 }
Dominik Laskowski49cea512019-11-12 14:13:23 -0800539 }
Ady Abraham8532d012019-05-08 14:50:56 -0700540}
541
Ady Abraham6fe2c172019-07-12 12:37:57 -0700542void Scheduler::setDisplayPowerState(bool normal) {
543 {
544 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700545 mFeatures.isDisplayPowerStateNormal = normal;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700546 }
547
548 if (mDisplayPowerTimer) {
549 mDisplayPowerTimer->reset();
550 }
551
552 // Display Power event will boost the refresh rate to performance.
553 // Clear Layer History to get fresh FPS detection
Dominik Laskowski49cea512019-11-12 14:13:23 -0800554 if (mLayerHistory) {
555 mLayerHistory->clear();
556 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700557}
558
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700559void Scheduler::kernelIdleTimerCallback(TimerState state) {
560 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100561
Ady Abraham2139f732019-11-13 18:56:40 -0800562 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
563 // magic number
Ady Abraham2e1dd892020-03-05 13:48:36 -0800564 const auto& refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
Ady Abraham2139f732019-11-13 18:56:40 -0800565 constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f;
Ady Abrahamabc27602020-04-08 17:20:29 -0700566 if (state == TimerState::Reset && refreshRate.getFps() > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700567 // If we're not in performance mode then the kernel timer shouldn't do
568 // anything, as the refresh rate during DPU power collapse will be the
569 // same.
Ady Abrahamabc27602020-04-08 17:20:29 -0700570 resyncToHardwareVsync(true /* makeAvailable */, refreshRate.getVsyncPeriod());
571 } else if (state == TimerState::Expired &&
572 refreshRate.getFps() <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700573 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
574 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
575 // need to update the DispSync model anyway.
576 disableHardwareVsync(false /* makeUnavailable */);
Alec Mouridc28b372019-04-18 21:17:13 -0700577 }
Ady Abrahama09852a2020-02-20 14:23:42 -0800578
579 mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
Alec Mouridc28b372019-04-18 21:17:13 -0700580}
581
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700582void Scheduler::idleTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700583 handleTimerStateChanged(&mFeatures.idleTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700584 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100585}
586
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700587void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700588 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700589 // Touch event will boost the refresh rate to performance.
590 // Clear layer history to get fresh FPS detection.
591 // NOTE: Instead of checking all the layers, we should be checking the layer
592 // that is currently on top. b/142507166 will give us this capability.
Ady Abrahamdfd62162020-06-10 16:11:56 -0700593 if (handleTimerStateChanged(&mFeatures.touch, touch)) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700594 if (mLayerHistory) {
595 mLayerHistory->clear();
596 }
Ady Abraham1adbb722020-05-15 11:51:48 -0700597 }
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700598 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700599}
600
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700601void Scheduler::displayPowerTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700602 handleTimerStateChanged(&mFeatures.displayPowerTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700603 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700604}
605
Dominik Laskowski98041832019-08-01 18:35:59 -0700606void Scheduler::dump(std::string& result) const {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800607 using base::StringAppendF;
Dominik Laskowski98041832019-08-01 18:35:59 -0700608
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700609 StringAppendF(&result, "+ Idle timer: %s\n", mIdleTimer ? mIdleTimer->dump().c_str() : "off");
Ana Krulec3d367c82020-02-25 15:02:01 -0800610 StringAppendF(&result, "+ Touch timer: %s\n",
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700611 mTouchTimer ? mTouchTimer->dump().c_str() : "off");
612 StringAppendF(&result, "+ Content detection: %s %s\n\n",
613 toContentDetectionString(mUseContentDetection, mUseContentDetectionV2),
614 mLayerHistory ? mLayerHistory->dump().c_str() : "(no layer history)");
Ana Krulecb43429d2019-01-09 14:28:51 -0800615}
616
Ady Abraham6fe2c172019-07-12 12:37:57 -0700617template <class T>
Ady Abrahamdfd62162020-06-10 16:11:56 -0700618bool Scheduler::handleTimerStateChanged(T* currentState, T newState) {
Ady Abraham2139f732019-11-13 18:56:40 -0800619 HwcConfigIndexType newConfigId;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700620 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
Ady Abraham8532d012019-05-08 14:50:56 -0700621 {
622 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700623 if (*currentState == newState) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700624 return false;
Ady Abraham8532d012019-05-08 14:50:56 -0700625 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700626 *currentState = newState;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700627 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800628 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700629 // We don't need to change the config, but we might need to send an event
630 // about a config change, since it was suppressed due to a previous idleConsidered
631 if (!consideredSignals.idle) {
632 dispatchCachedReportedConfig();
633 }
634 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700635 }
Ady Abraham2139f732019-11-13 18:56:40 -0800636 mFeatures.configId = newConfigId;
Ady Abraham8532d012019-05-08 14:50:56 -0700637 }
Ady Abraham2139f732019-11-13 18:56:40 -0800638 const RefreshRate& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700639 mSchedulerCallback.changeRefreshRate(newRefreshRate,
640 consideredSignals.idle ? ConfigEvent::None
641 : ConfigEvent::Changed);
642 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700643}
644
Ady Abrahamdfd62162020-06-10 16:11:56 -0700645HwcConfigIndexType Scheduler::calculateRefreshRateConfigIndexType(
646 scheduler::RefreshRateConfigs::GlobalSignals* consideredSignals) {
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800647 ATRACE_CALL();
Ady Abrahamdfd62162020-06-10 16:11:56 -0700648 if (consideredSignals) *consideredSignals = {};
Ady Abraham09bd3922019-04-08 10:44:56 -0700649
Steven Thomasf734df42020-04-13 21:09:28 -0700650 // If Display Power is not in normal operation we want to be in performance mode. When coming
651 // back to normal mode, a grace period is given with DisplayPowerTimer.
Ana Krulec3f6a2062020-01-23 15:48:01 -0800652 if (mDisplayPowerTimer &&
653 (!mFeatures.isDisplayPowerStateNormal ||
654 mFeatures.displayPowerTimer == TimerState::Reset)) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700655 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800656 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700657
Steven Thomasbb374322020-04-28 22:47:16 -0700658 const bool touchActive = mTouchTimer && mFeatures.touch == TouchState::Active;
659 const bool idle = mIdleTimer && mFeatures.idleTimer == TimerState::Expired;
660
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800661 if (!mUseContentDetectionV2) {
662 // As long as touch is active we want to be in performance mode.
Steven Thomasbb374322020-04-28 22:47:16 -0700663 if (touchActive) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700664 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800665 }
Ady Abraham8532d012019-05-08 14:50:56 -0700666
Steven Thomasbb374322020-04-28 22:47:16 -0700667 // If timer has expired as it means there is no new content on the screen.
668 if (idle) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700669 if (consideredSignals) consideredSignals->idle = true;
Steven Thomasbb374322020-04-28 22:47:16 -0700670 return mRefreshRateConfigs.getMinRefreshRateByPolicy().getConfigId();
671 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700672
Ana Krulec3f6a2062020-01-23 15:48:01 -0800673 // If content detection is off we choose performance as we don't know the content fps.
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800674 if (mFeatures.contentDetectionV1 == ContentDetectionState::Off) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800675 // NOTE: V1 always calls this, but this is not a default behavior for V2.
Ady Abrahamabc27602020-04-08 17:20:29 -0700676 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Steven Thomas540730a2020-01-08 20:12:42 -0800677 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800678
679 // Content detection is on, find the appropriate refresh rate with minimal error
Ady Abrahamabc27602020-04-08 17:20:29 -0700680 return mRefreshRateConfigs.getRefreshRateForContent(mFeatures.contentRequirements)
681 .getConfigId();
Ady Abraham09bd3922019-04-08 10:44:56 -0700682 }
683
Ady Abraham1adbb722020-05-15 11:51:48 -0700684 return mRefreshRateConfigs
Ady Abrahamdfd62162020-06-10 16:11:56 -0700685 .getBestRefreshRate(mFeatures.contentRequirements, {.touch = touchActive, .idle = idle},
686 consideredSignals)
Ady Abraham1adbb722020-05-15 11:51:48 -0700687 .getConfigId();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800688}
689
Ady Abraham2139f732019-11-13 18:56:40 -0800690std::optional<HwcConfigIndexType> Scheduler::getPreferredConfigId() {
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700691 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800692 // Make sure that the default config ID is first updated, before returned.
693 if (mFeatures.configId.has_value()) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800694 mFeatures.configId = calculateRefreshRateConfigIndexType();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800695 }
Ady Abraham2139f732019-11-13 18:56:40 -0800696 return mFeatures.configId;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700697}
698
Peiyong Line9d809e2020-04-14 13:10:48 -0700699void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800700 if (timeline.refreshRequired) {
701 mSchedulerCallback.repaintEverythingForHWC();
702 }
703
704 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
705 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
706
707 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
708 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
709 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
710 }
711}
712
713void Scheduler::onDisplayRefreshed(nsecs_t timestamp) {
714 bool callRepaint = false;
715 {
716 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
717 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
718 if (mLastVsyncPeriodChangeTimeline->refreshTimeNanos < timestamp) {
719 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
720 } else {
721 // We need to send another refresh as refreshTimeNanos is still in the future
722 callRepaint = true;
723 }
724 }
725 }
726
727 if (callRepaint) {
728 mSchedulerCallback.repaintEverythingForHWC();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800729 }
730}
731
Ady Abraham8a82ba62020-01-17 12:43:17 -0800732void Scheduler::onPrimaryDisplayAreaChanged(uint32_t displayArea) {
733 if (mLayerHistory) {
734 mLayerHistory->setDisplayArea(displayArea);
735 }
736}
737
Ana Krulec98b5b242018-08-10 15:03:23 -0700738} // namespace android