blob: b0b5e2eff135fbf929d4d40e3f5076b113127c2a [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
Ady Abraham8735eac2020-08-12 16:35:04 -070098class PredictedVsyncTracer {
99public:
100 PredictedVsyncTracer(scheduler::VSyncDispatch& dispatch)
101 : mRegistration(dispatch, std::bind(&PredictedVsyncTracer::callback, this),
102 "PredictedVsyncTracer") {
103 scheduleRegistration();
104 }
105
106private:
107 TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
108 scheduler::VSyncCallbackRegistration mRegistration;
109
110 void scheduleRegistration() { mRegistration.schedule({0, 0, 0}); }
111
112 void callback() {
113 mParity = !mParity;
114 scheduleRegistration();
115 }
116};
117
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700118Scheduler::Scheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback)
119 : Scheduler(configs, callback,
120 {.supportKernelTimer = sysprop::support_kernel_idle_timer(false),
121 .useContentDetection = sysprop::use_content_detection_for_refresh_rate(false),
122 .useContentDetectionV2 =
Ady Abraham49cb7d52020-07-22 18:37:07 -0700123 base::GetBoolProperty("debug.sf.use_content_detection_v2"s, true)}) {}
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700124
125Scheduler::Scheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCallback& callback,
126 Options options)
127 : Scheduler(createVsyncSchedule(options), configs, callback,
128 createLayerHistory(configs, options.useContentDetectionV2), options) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700129 using namespace sysprop;
Ady Abraham8532d012019-05-08 14:50:56 -0700130
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700131 const int setIdleTimerMs = base::GetIntProperty("debug.sf.set_idle_timer_ms"s, 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100132
Dominik Laskowski98041832019-08-01 18:35:59 -0700133 if (const auto millis = setIdleTimerMs ? setIdleTimerMs : set_idle_timer_ms(0); millis > 0) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700134 const auto callback = mOptions.supportKernelTimer ? &Scheduler::kernelIdleTimerCallback
135 : &Scheduler::idleTimerCallback;
Dominik Laskowski98041832019-08-01 18:35:59 -0700136 mIdleTimer.emplace(
137 std::chrono::milliseconds(millis),
138 [this, callback] { std::invoke(callback, this, TimerState::Reset); },
139 [this, callback] { std::invoke(callback, this, TimerState::Expired); });
Ana Krulecfb772822018-11-30 10:44:07 +0100140 mIdleTimer->start();
141 }
Ady Abraham8532d012019-05-08 14:50:56 -0700142
Dominik Laskowski98041832019-08-01 18:35:59 -0700143 if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -0700144 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -0700145 mTouchTimer.emplace(
146 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700147 [this] { touchTimerCallback(TimerState::Reset); },
148 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -0700149 mTouchTimer->start();
150 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700151
Dominik Laskowski98041832019-08-01 18:35:59 -0700152 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
153 mDisplayPowerTimer.emplace(
154 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700155 [this] { displayPowerTimerCallback(TimerState::Reset); },
156 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -0700157 mDisplayPowerTimer->start();
158 }
Ana Krulece588e312018-09-18 12:32:24 -0700159}
160
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700161Scheduler::Scheduler(VsyncSchedule schedule, const scheduler::RefreshRateConfigs& configs,
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700162 ISchedulerCallback& schedulerCallback,
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700163 std::unique_ptr<LayerHistory> layerHistory, Options options)
164 : mOptions(options),
165 mVsyncSchedule(std::move(schedule)),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700166 mLayerHistory(std::move(layerHistory)),
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800167 mSchedulerCallback(schedulerCallback),
Ady Abraham8735eac2020-08-12 16:35:04 -0700168 mRefreshRateConfigs(configs),
169 mPredictedVsyncTracer(
170 base::GetBoolProperty("debug.sf.show_predicted_vsync", false)
171 ? std::make_unique<PredictedVsyncTracer>(*mVsyncSchedule.dispatch)
172 : nullptr) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700173 mSchedulerCallback.setVsyncEnabled(false);
174}
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700175
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800176Scheduler::~Scheduler() {
Ana Krulecf2c006d2019-06-21 15:37:07 -0700177 // Ensure the OneShotTimer threads are joined before we start destroying state.
Ady Abraham6fe2c172019-07-12 12:37:57 -0700178 mDisplayPowerTimer.reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700179 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800180 mIdleTimer.reset();
181}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700182
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700183Scheduler::VsyncSchedule Scheduler::createVsyncSchedule(Options options) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700184 auto clock = std::make_unique<scheduler::SystemClock>();
185 auto tracker = createVSyncTracker();
186 auto dispatch = createVSyncDispatch(*tracker);
187
188 // TODO(b/144707443): Tune constants.
189 constexpr size_t pendingFenceLimit = 20;
Ady Abraham8735eac2020-08-12 16:35:04 -0700190 auto sync =
191 std::make_unique<scheduler::VSyncReactor>(std::move(clock), *tracker, pendingFenceLimit,
192 options.supportKernelTimer);
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700193 return {std::move(sync), std::move(tracker), std::move(dispatch)};
194}
195
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700196std::unique_ptr<LayerHistory> Scheduler::createLayerHistory(
197 const scheduler::RefreshRateConfigs& configs, bool useContentDetectionV2) {
198 if (!configs.canSwitch()) return nullptr;
199
200 if (useContentDetectionV2) {
201 return std::make_unique<scheduler::impl::LayerHistoryV2>(configs);
202 }
203
204 return std::make_unique<scheduler::impl::LayerHistory>();
205}
206
Dominik Laskowski98041832019-08-01 18:35:59 -0700207DispSync& Scheduler::getPrimaryDispSync() {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700208 return *mVsyncSchedule.sync;
Dominik Laskowski98041832019-08-01 18:35:59 -0700209}
210
Ady Abraham9c53ee72020-07-22 21:16:18 -0700211std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(
212 const char* name, std::chrono::nanoseconds workDuration,
213 std::chrono::nanoseconds readyDuration, bool traceVsync) {
214 return std::make_unique<scheduler::DispSyncSource>(*mVsyncSchedule.dispatch, workDuration,
215 readyDuration, traceVsync, name);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700216}
217
Dominik Laskowski98041832019-08-01 18:35:59 -0700218Scheduler::ConnectionHandle Scheduler::createConnection(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700219 const char* connectionName, std::chrono::nanoseconds workDuration,
220 std::chrono::nanoseconds readyDuration,
Ana Krulec98b5b242018-08-10 15:03:23 -0700221 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700222 auto vsyncSource = makePrimaryDispSyncSource(connectionName, workDuration, readyDuration);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700223 auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource),
224 std::move(interceptCallback));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700225 return createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700226}
Ana Krulec98b5b242018-08-10 15:03:23 -0700227
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700228Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700229 const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
230 ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800231
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700232 auto connection =
233 createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowski98041832019-08-01 18:35:59 -0700234
235 mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
236 return handle;
Ana Krulec98b5b242018-08-10 15:03:23 -0700237}
238
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700239sp<EventThreadConnection> Scheduler::createConnectionInternal(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700240 EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) {
241 return eventThread->createEventConnection([&] { resync(); }, configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700242}
243
Ana Krulec98b5b242018-08-10 15:03:23 -0700244sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700245 ConnectionHandle handle, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700246 RETURN_IF_INVALID_HANDLE(handle, nullptr);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700247 return createConnectionInternal(mConnections[handle].thread.get(), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700248}
249
Dominik Laskowski98041832019-08-01 18:35:59 -0700250sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
251 RETURN_IF_INVALID_HANDLE(handle, nullptr);
252 return mConnections[handle].connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700253}
254
Dominik Laskowski98041832019-08-01 18:35:59 -0700255void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
256 bool connected) {
257 RETURN_IF_INVALID_HANDLE(handle);
258 mConnections[handle].thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700259}
260
Dominik Laskowski98041832019-08-01 18:35:59 -0700261void Scheduler::onScreenAcquired(ConnectionHandle handle) {
262 RETURN_IF_INVALID_HANDLE(handle);
263 mConnections[handle].thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700264}
265
Dominik Laskowski98041832019-08-01 18:35:59 -0700266void Scheduler::onScreenReleased(ConnectionHandle handle) {
267 RETURN_IF_INVALID_HANDLE(handle);
268 mConnections[handle].thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700269}
270
Ady Abrahamdfd62162020-06-10 16:11:56 -0700271void Scheduler::onPrimaryDisplayConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId,
272 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
273 std::lock_guard<std::mutex> lock(mFeatureStateLock);
274 // Cache the last reported config for primary display.
275 mFeatures.cachedConfigChangedParams = {handle, displayId, configId, vsyncPeriod};
276 onNonPrimaryDisplayConfigChanged(handle, displayId, configId, vsyncPeriod);
277}
278
279void Scheduler::dispatchCachedReportedConfig() {
280 const auto configId = *mFeatures.configId;
281 const auto vsyncPeriod =
282 mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getVsyncPeriod();
283
284 // If there is no change from cached config, there is no need to dispatch an event
285 if (configId == mFeatures.cachedConfigChangedParams->configId &&
286 vsyncPeriod == mFeatures.cachedConfigChangedParams->vsyncPeriod) {
287 return;
288 }
289
290 mFeatures.cachedConfigChangedParams->configId = configId;
291 mFeatures.cachedConfigChangedParams->vsyncPeriod = vsyncPeriod;
292 onNonPrimaryDisplayConfigChanged(mFeatures.cachedConfigChangedParams->handle,
293 mFeatures.cachedConfigChangedParams->displayId,
294 mFeatures.cachedConfigChangedParams->configId,
295 mFeatures.cachedConfigChangedParams->vsyncPeriod);
296}
297
298void Scheduler::onNonPrimaryDisplayConfigChanged(ConnectionHandle handle,
299 PhysicalDisplayId displayId,
300 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700301 RETURN_IF_INVALID_HANDLE(handle);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700302 mConnections[handle].thread->onConfigChanged(displayId, configId, vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800303}
304
Alec Mouri717bcb62020-02-10 17:07:19 -0800305size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) {
306 RETURN_IF_INVALID_HANDLE(handle, 0);
307 return mConnections[handle].thread->getEventThreadConnectionCount();
308}
309
Dominik Laskowski98041832019-08-01 18:35:59 -0700310void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
311 RETURN_IF_INVALID_HANDLE(handle);
312 mConnections.at(handle).thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700313}
314
Ady Abraham9c53ee72020-07-22 21:16:18 -0700315void Scheduler::setDuration(ConnectionHandle handle, std::chrono::nanoseconds workDuration,
316 std::chrono::nanoseconds readyDuration) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700317 RETURN_IF_INVALID_HANDLE(handle);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700318 mConnections[handle].thread->setDuration(workDuration, readyDuration);
Ana Krulec98b5b242018-08-10 15:03:23 -0700319}
Ana Krulece588e312018-09-18 12:32:24 -0700320
321void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700322 stats->vsyncTime = getPrimaryDispSync().computeNextRefresh(0, systemTime());
323 stats->vsyncPeriod = getPrimaryDispSync().getPeriod();
Ana Krulece588e312018-09-18 12:32:24 -0700324}
325
Dominik Laskowski6505f792019-09-18 11:10:05 -0700326Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
327 if (mInjectVSyncs == enable) {
328 return {};
329 }
330
331 ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling");
332
333 if (!mInjectorConnectionHandle) {
334 auto vsyncSource = std::make_unique<InjectVSyncSource>();
335 mVSyncInjector = vsyncSource.get();
336
337 auto eventThread =
338 std::make_unique<impl::EventThread>(std::move(vsyncSource),
339 impl::EventThread::InterceptVSyncsCallback());
340
341 mInjectorConnectionHandle = createConnection(std::move(eventThread));
342 }
343
344 mInjectVSyncs = enable;
345 return mInjectorConnectionHandle;
346}
347
Ady Abraham9c53ee72020-07-22 21:16:18 -0700348bool Scheduler::injectVSync(nsecs_t when, nsecs_t expectedVSyncTime, nsecs_t deadlineTimestamp) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700349 if (!mInjectVSyncs || !mVSyncInjector) {
350 return false;
351 }
352
Ady Abraham9c53ee72020-07-22 21:16:18 -0700353 mVSyncInjector->onInjectSyncEvent(when, expectedVSyncTime, deadlineTimestamp);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700354 return true;
355}
356
Ana Krulece588e312018-09-18 12:32:24 -0700357void Scheduler::enableHardwareVsync() {
358 std::lock_guard<std::mutex> lock(mHWVsyncLock);
359 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700360 getPrimaryDispSync().beginResync();
361 mSchedulerCallback.setVsyncEnabled(true);
Ana Krulece588e312018-09-18 12:32:24 -0700362 mPrimaryHWVsyncEnabled = true;
363 }
364}
365
366void Scheduler::disableHardwareVsync(bool makeUnavailable) {
367 std::lock_guard<std::mutex> lock(mHWVsyncLock);
368 if (mPrimaryHWVsyncEnabled) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700369 mSchedulerCallback.setVsyncEnabled(false);
Ana Krulece588e312018-09-18 12:32:24 -0700370 mPrimaryHWVsyncEnabled = false;
371 }
372 if (makeUnavailable) {
373 mHWVsyncAvailable = false;
374 }
375}
376
Ana Krulecc2870422019-01-29 19:00:58 -0800377void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
378 {
379 std::lock_guard<std::mutex> lock(mHWVsyncLock);
380 if (makeAvailable) {
381 mHWVsyncAvailable = makeAvailable;
382 } else if (!mHWVsyncAvailable) {
383 // Hardware vsync is not currently available, so abort the resync
384 // attempt for now
385 return;
386 }
387 }
388
389 if (period <= 0) {
390 return;
391 }
392
393 setVsyncPeriod(period);
394}
395
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700396void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700397 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800398
399 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700400 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800401
402 if (now - last > kIgnoreDelay) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700403 resyncToHardwareVsync(false, mRefreshRateConfigs.getCurrentRefreshRate().getVsyncPeriod());
Ana Krulecc2870422019-01-29 19:00:58 -0800404 }
405}
406
Dominik Laskowski98041832019-08-01 18:35:59 -0700407void Scheduler::setVsyncPeriod(nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800408 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700409 getPrimaryDispSync().setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800410
411 if (!mPrimaryHWVsyncEnabled) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700412 getPrimaryDispSync().beginResync();
413 mSchedulerCallback.setVsyncEnabled(true);
Ady Abraham3aff9172019-02-07 19:10:26 -0800414 mPrimaryHWVsyncEnabled = true;
415 }
Ana Krulece588e312018-09-18 12:32:24 -0700416}
417
Ady Abraham5dee2f12020-02-05 17:49:47 -0800418void Scheduler::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
419 bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700420 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700421 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700422 { // Scope for the lock
423 std::lock_guard<std::mutex> lock(mHWVsyncLock);
424 if (mPrimaryHWVsyncEnabled) {
Ady Abraham5dee2f12020-02-05 17:49:47 -0800425 needsHwVsync =
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700426 getPrimaryDispSync().addResyncSample(timestamp, hwcVsyncPeriod, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700427 }
428 }
429
430 if (needsHwVsync) {
431 enableHardwareVsync();
432 } else {
433 disableHardwareVsync(false);
434 }
435}
436
437void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700438 if (getPrimaryDispSync().addPresentFence(fenceTime)) {
Ana Krulece588e312018-09-18 12:32:24 -0700439 enableHardwareVsync();
440 } else {
441 disableHardwareVsync(false);
442 }
443}
444
445void Scheduler::setIgnorePresentFences(bool ignore) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700446 getPrimaryDispSync().setIgnorePresentFences(ignore);
Ana Krulece588e312018-09-18 12:32:24 -0700447}
448
Ady Abraham0ed31c92020-04-16 11:48:45 -0700449nsecs_t Scheduler::getDispSyncExpectedPresentTime(nsecs_t now) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700450 return getPrimaryDispSync().expectedPresentTime(now);
Ady Abrahamc3e21312019-02-07 14:30:23 -0800451}
452
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700453void Scheduler::registerLayer(Layer* layer) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800454 if (!mLayerHistory) return;
455
Steven Thomasdebafed2020-05-18 17:30:35 -0700456 const auto minFps = mRefreshRateConfigs.getMinRefreshRate().getFps();
457 const auto maxFps = mRefreshRateConfigs.getMaxRefreshRate().getFps();
458
Michael Wright44753b12020-07-08 13:48:11 +0100459 if (layer->getWindowType() == InputWindowInfo::Type::STATUS_BAR) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700460 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ana Krulec3d367c82020-02-25 15:02:01 -0800461 scheduler::LayerHistory::LayerVoteType::NoVote);
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700462 } else if (!mOptions.useContentDetection) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700463 // If the content detection feature is off, all layers are registered at Max. We still keep
464 // the layer history, since we use it for other features (like Frame Rate API), so layers
465 // still need to be registered.
466 mLayerHistory->registerLayer(layer, minFps, maxFps,
467 scheduler::LayerHistory::LayerVoteType::Max);
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700468 } else if (!mOptions.useContentDetectionV2) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700469 // In V1 of content detection, all layers are registered as Heuristic (unless it's
470 // wallpaper).
471 const auto highFps =
Michael Wright44753b12020-07-08 13:48:11 +0100472 layer->getWindowType() == InputWindowInfo::Type::WALLPAPER ? minFps : maxFps;
Ana Krulec3d367c82020-02-25 15:02:01 -0800473
Steven Thomasdebafed2020-05-18 17:30:35 -0700474 mLayerHistory->registerLayer(layer, minFps, highFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800475 scheduler::LayerHistory::LayerVoteType::Heuristic);
476 } else {
Michael Wright44753b12020-07-08 13:48:11 +0100477 if (layer->getWindowType() == InputWindowInfo::Type::WALLPAPER) {
Ana Krulec3d367c82020-02-25 15:02:01 -0800478 // Running Wallpaper at Min is considered as part of content detection.
Steven Thomasdebafed2020-05-18 17:30:35 -0700479 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800480 scheduler::LayerHistory::LayerVoteType::Min);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800481 } else {
Steven Thomasdebafed2020-05-18 17:30:35 -0700482 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800483 scheduler::LayerHistory::LayerVoteType::Heuristic);
484 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800485 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700486}
487
Ady Abraham5def7332020-05-29 16:13:47 -0700488void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime,
489 LayerHistory::LayerUpdateType updateType) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800490 if (mLayerHistory) {
Ady Abraham5def7332020-05-29 16:13:47 -0700491 mLayerHistory->record(layer, presentTime, systemTime(), updateType);
Dominik Laskowski49cea512019-11-12 14:13:23 -0800492 }
Ana Krulec3084c052018-11-21 20:27:17 +0100493}
494
Ady Abraham32efd542020-05-19 17:49:26 -0700495void Scheduler::setConfigChangePending(bool pending) {
496 if (mLayerHistory) {
497 mLayerHistory->setConfigChangePending(pending);
498 }
499}
500
Dominik Laskowski49cea512019-11-12 14:13:23 -0800501void Scheduler::chooseRefreshRateForContent() {
502 if (!mLayerHistory) return;
503
Ady Abraham8a82ba62020-01-17 12:43:17 -0800504 ATRACE_CALL();
505
506 scheduler::LayerHistory::Summary summary = mLayerHistory->summarize(systemTime());
Ady Abraham2139f732019-11-13 18:56:40 -0800507 HwcConfigIndexType newConfigId;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700508 {
509 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800510 if (mFeatures.contentRequirements == summary) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700511 return;
512 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800513 mFeatures.contentRequirements = summary;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800514 mFeatures.contentDetectionV1 =
Ady Abraham8a82ba62020-01-17 12:43:17 -0800515 !summary.empty() ? ContentDetectionState::On : ContentDetectionState::Off;
516
Ady Abrahamdfd62162020-06-10 16:11:56 -0700517 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
518 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800519 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700520 // We don't need to change the config, but we might need to send an event
521 // about a config change, since it was suppressed due to a previous idleConsidered
522 if (!consideredSignals.idle) {
523 dispatchCachedReportedConfig();
524 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700525 return;
526 }
Ady Abraham2139f732019-11-13 18:56:40 -0800527 mFeatures.configId = newConfigId;
Ady Abraham2e1dd892020-03-05 13:48:36 -0800528 auto& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700529 mSchedulerCallback.changeRefreshRate(newRefreshRate,
530 consideredSignals.idle ? ConfigEvent::None
531 : ConfigEvent::Changed);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800532 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800533}
534
Ana Krulecfb772822018-11-30 10:44:07 +0100535void Scheduler::resetIdleTimer() {
536 if (mIdleTimer) {
537 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800538 }
539}
540
Ady Abraham8532d012019-05-08 14:50:56 -0700541void Scheduler::notifyTouchEvent() {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700542 if (mTouchTimer) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800543 mTouchTimer->reset();
Steven Thomas540730a2020-01-08 20:12:42 -0800544
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700545 if (mOptions.supportKernelTimer && mIdleTimer) {
Steven Thomas540730a2020-01-08 20:12:42 -0800546 mIdleTimer->reset();
547 }
Dominik Laskowski49cea512019-11-12 14:13:23 -0800548 }
Ady Abraham8532d012019-05-08 14:50:56 -0700549}
550
Ady Abraham6fe2c172019-07-12 12:37:57 -0700551void Scheduler::setDisplayPowerState(bool normal) {
552 {
553 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700554 mFeatures.isDisplayPowerStateNormal = normal;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700555 }
556
557 if (mDisplayPowerTimer) {
558 mDisplayPowerTimer->reset();
559 }
560
561 // Display Power event will boost the refresh rate to performance.
562 // Clear Layer History to get fresh FPS detection
Dominik Laskowski49cea512019-11-12 14:13:23 -0800563 if (mLayerHistory) {
564 mLayerHistory->clear();
565 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700566}
567
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700568void Scheduler::kernelIdleTimerCallback(TimerState state) {
569 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100570
Ady Abraham2139f732019-11-13 18:56:40 -0800571 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
572 // magic number
Ady Abraham2e1dd892020-03-05 13:48:36 -0800573 const auto& refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
Ady Abraham2139f732019-11-13 18:56:40 -0800574 constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f;
Ady Abrahamabc27602020-04-08 17:20:29 -0700575 if (state == TimerState::Reset && refreshRate.getFps() > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700576 // If we're not in performance mode then the kernel timer shouldn't do
577 // anything, as the refresh rate during DPU power collapse will be the
578 // same.
Ady Abrahamabc27602020-04-08 17:20:29 -0700579 resyncToHardwareVsync(true /* makeAvailable */, refreshRate.getVsyncPeriod());
580 } else if (state == TimerState::Expired &&
581 refreshRate.getFps() <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700582 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
583 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
584 // need to update the DispSync model anyway.
585 disableHardwareVsync(false /* makeUnavailable */);
Alec Mouridc28b372019-04-18 21:17:13 -0700586 }
Ady Abrahama09852a2020-02-20 14:23:42 -0800587
588 mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
Alec Mouridc28b372019-04-18 21:17:13 -0700589}
590
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700591void Scheduler::idleTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700592 handleTimerStateChanged(&mFeatures.idleTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700593 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100594}
595
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700596void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700597 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700598 // Touch event will boost the refresh rate to performance.
599 // Clear layer history to get fresh FPS detection.
600 // NOTE: Instead of checking all the layers, we should be checking the layer
601 // that is currently on top. b/142507166 will give us this capability.
Ady Abrahamdfd62162020-06-10 16:11:56 -0700602 if (handleTimerStateChanged(&mFeatures.touch, touch)) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700603 if (mLayerHistory) {
604 mLayerHistory->clear();
605 }
Ady Abraham1adbb722020-05-15 11:51:48 -0700606 }
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700607 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700608}
609
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700610void Scheduler::displayPowerTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700611 handleTimerStateChanged(&mFeatures.displayPowerTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700612 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700613}
614
Dominik Laskowski98041832019-08-01 18:35:59 -0700615void Scheduler::dump(std::string& result) const {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800616 using base::StringAppendF;
Dominik Laskowski98041832019-08-01 18:35:59 -0700617
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700618 StringAppendF(&result, "+ Idle timer: %s\n", mIdleTimer ? mIdleTimer->dump().c_str() : "off");
Ana Krulec3d367c82020-02-25 15:02:01 -0800619 StringAppendF(&result, "+ Touch timer: %s\n",
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700620 mTouchTimer ? mTouchTimer->dump().c_str() : "off");
621 StringAppendF(&result, "+ Content detection: %s %s\n\n",
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700622 toContentDetectionString(mOptions.useContentDetection,
623 mOptions.useContentDetectionV2),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700624 mLayerHistory ? mLayerHistory->dump().c_str() : "(no layer history)");
Ana Krulecb43429d2019-01-09 14:28:51 -0800625}
626
Ady Abraham8735eac2020-08-12 16:35:04 -0700627void Scheduler::dumpVSync(std::string& s) const {
628 using base::StringAppendF;
629
630 StringAppendF(&s, "VSyncReactor:\n");
631 mVsyncSchedule.sync->dump(s);
632 StringAppendF(&s, "VSyncDispatch:\n");
633 mVsyncSchedule.dispatch->dump(s);
634}
635
Ady Abraham6fe2c172019-07-12 12:37:57 -0700636template <class T>
Ady Abrahamdfd62162020-06-10 16:11:56 -0700637bool Scheduler::handleTimerStateChanged(T* currentState, T newState) {
Ady Abraham2139f732019-11-13 18:56:40 -0800638 HwcConfigIndexType newConfigId;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700639 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
Ady Abraham8532d012019-05-08 14:50:56 -0700640 {
641 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700642 if (*currentState == newState) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700643 return false;
Ady Abraham8532d012019-05-08 14:50:56 -0700644 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700645 *currentState = newState;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700646 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800647 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700648 // We don't need to change the config, but we might need to send an event
649 // about a config change, since it was suppressed due to a previous idleConsidered
650 if (!consideredSignals.idle) {
651 dispatchCachedReportedConfig();
652 }
653 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700654 }
Ady Abraham2139f732019-11-13 18:56:40 -0800655 mFeatures.configId = newConfigId;
Ady Abraham8532d012019-05-08 14:50:56 -0700656 }
Ady Abraham2139f732019-11-13 18:56:40 -0800657 const RefreshRate& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700658 mSchedulerCallback.changeRefreshRate(newRefreshRate,
659 consideredSignals.idle ? ConfigEvent::None
660 : ConfigEvent::Changed);
661 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700662}
663
Ady Abrahamdfd62162020-06-10 16:11:56 -0700664HwcConfigIndexType Scheduler::calculateRefreshRateConfigIndexType(
665 scheduler::RefreshRateConfigs::GlobalSignals* consideredSignals) {
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800666 ATRACE_CALL();
Ady Abrahamdfd62162020-06-10 16:11:56 -0700667 if (consideredSignals) *consideredSignals = {};
Ady Abraham09bd3922019-04-08 10:44:56 -0700668
Steven Thomasf734df42020-04-13 21:09:28 -0700669 // If Display Power is not in normal operation we want to be in performance mode. When coming
670 // back to normal mode, a grace period is given with DisplayPowerTimer.
Ana Krulec3f6a2062020-01-23 15:48:01 -0800671 if (mDisplayPowerTimer &&
672 (!mFeatures.isDisplayPowerStateNormal ||
673 mFeatures.displayPowerTimer == TimerState::Reset)) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700674 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800675 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700676
Steven Thomasbb374322020-04-28 22:47:16 -0700677 const bool touchActive = mTouchTimer && mFeatures.touch == TouchState::Active;
678 const bool idle = mIdleTimer && mFeatures.idleTimer == TimerState::Expired;
679
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700680 if (!mOptions.useContentDetectionV2) {
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800681 // As long as touch is active we want to be in performance mode.
Steven Thomasbb374322020-04-28 22:47:16 -0700682 if (touchActive) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700683 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800684 }
Ady Abraham8532d012019-05-08 14:50:56 -0700685
Steven Thomasbb374322020-04-28 22:47:16 -0700686 // If timer has expired as it means there is no new content on the screen.
687 if (idle) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700688 if (consideredSignals) consideredSignals->idle = true;
Steven Thomasbb374322020-04-28 22:47:16 -0700689 return mRefreshRateConfigs.getMinRefreshRateByPolicy().getConfigId();
690 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700691
Ana Krulec3f6a2062020-01-23 15:48:01 -0800692 // If content detection is off we choose performance as we don't know the content fps.
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800693 if (mFeatures.contentDetectionV1 == ContentDetectionState::Off) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800694 // NOTE: V1 always calls this, but this is not a default behavior for V2.
Ady Abrahamabc27602020-04-08 17:20:29 -0700695 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Steven Thomas540730a2020-01-08 20:12:42 -0800696 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800697
698 // Content detection is on, find the appropriate refresh rate with minimal error
Ady Abrahamabc27602020-04-08 17:20:29 -0700699 return mRefreshRateConfigs.getRefreshRateForContent(mFeatures.contentRequirements)
700 .getConfigId();
Ady Abraham09bd3922019-04-08 10:44:56 -0700701 }
702
Ady Abraham1adbb722020-05-15 11:51:48 -0700703 return mRefreshRateConfigs
Ady Abrahamdfd62162020-06-10 16:11:56 -0700704 .getBestRefreshRate(mFeatures.contentRequirements, {.touch = touchActive, .idle = idle},
705 consideredSignals)
Ady Abraham1adbb722020-05-15 11:51:48 -0700706 .getConfigId();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800707}
708
Ady Abraham2139f732019-11-13 18:56:40 -0800709std::optional<HwcConfigIndexType> Scheduler::getPreferredConfigId() {
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700710 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800711 // Make sure that the default config ID is first updated, before returned.
712 if (mFeatures.configId.has_value()) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800713 mFeatures.configId = calculateRefreshRateConfigIndexType();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800714 }
Ady Abraham2139f732019-11-13 18:56:40 -0800715 return mFeatures.configId;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700716}
717
Peiyong Line9d809e2020-04-14 13:10:48 -0700718void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800719 if (timeline.refreshRequired) {
720 mSchedulerCallback.repaintEverythingForHWC();
721 }
722
723 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
724 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
725
726 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
727 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
728 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
729 }
730}
731
732void Scheduler::onDisplayRefreshed(nsecs_t timestamp) {
733 bool callRepaint = false;
734 {
735 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
736 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
737 if (mLastVsyncPeriodChangeTimeline->refreshTimeNanos < timestamp) {
738 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
739 } else {
740 // We need to send another refresh as refreshTimeNanos is still in the future
741 callRepaint = true;
742 }
743 }
744 }
745
746 if (callRepaint) {
747 mSchedulerCallback.repaintEverythingForHWC();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800748 }
749}
750
Ady Abraham8a82ba62020-01-17 12:43:17 -0800751void Scheduler::onPrimaryDisplayAreaChanged(uint32_t displayArea) {
752 if (mLayerHistory) {
753 mLayerHistory->setDisplayArea(displayArea);
754 }
755}
756
Ana Krulec98b5b242018-08-10 15:03:23 -0700757} // namespace android