blob: 5271ccc7045d8c418ccf74bcc19ec0b99ddb206e [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 "DispSyncSource.h"
43#include "EventThread.h"
Dominik Laskowski6505f792019-09-18 11:10:05 -070044#include "InjectVSyncSource.h"
Ana Krulecf2c006d2019-06-21 15:37:07 -070045#include "OneShotTimer.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010046#include "SchedulerUtils.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090047#include "SurfaceFlingerProperties.h"
Kevin DuBois00287382019-11-19 15:11:55 -080048#include "Timer.h"
49#include "VSyncDispatchTimerQueue.h"
50#include "VSyncPredictor.h"
51#include "VSyncReactor.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070052#include "VsyncController.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)
Ady Abraham8cb21882020-08-26 18:22:05 -0700127 : Scheduler(createVsyncSchedule(options.supportKernelTimer), configs, callback,
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700128 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
Ady Abraham8cb21882020-08-26 18:22:05 -0700183Scheduler::VsyncSchedule Scheduler::createVsyncSchedule(bool supportKernelTimer) {
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 Abraham8cb21882020-08-26 18:22:05 -0700190 auto controller =
Ady Abraham8735eac2020-08-12 16:35:04 -0700191 std::make_unique<scheduler::VSyncReactor>(std::move(clock), *tracker, pendingFenceLimit,
Ady Abraham8cb21882020-08-26 18:22:05 -0700192 supportKernelTimer);
193 return {std::move(controller), std::move(tracker), std::move(dispatch)};
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700194}
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
Ady Abraham9c53ee72020-07-22 21:16:18 -0700207std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(
208 const char* name, std::chrono::nanoseconds workDuration,
209 std::chrono::nanoseconds readyDuration, bool traceVsync) {
210 return std::make_unique<scheduler::DispSyncSource>(*mVsyncSchedule.dispatch, workDuration,
211 readyDuration, traceVsync, name);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700212}
213
Dominik Laskowski98041832019-08-01 18:35:59 -0700214Scheduler::ConnectionHandle Scheduler::createConnection(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700215 const char* connectionName, std::chrono::nanoseconds workDuration,
216 std::chrono::nanoseconds readyDuration,
Ana Krulec98b5b242018-08-10 15:03:23 -0700217 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700218 auto vsyncSource = makePrimaryDispSyncSource(connectionName, workDuration, readyDuration);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700219 auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource),
220 std::move(interceptCallback));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700221 return createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700222}
Ana Krulec98b5b242018-08-10 15:03:23 -0700223
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700224Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700225 const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
226 ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800227
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700228 auto connection =
229 createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowski98041832019-08-01 18:35:59 -0700230
231 mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
232 return handle;
Ana Krulec98b5b242018-08-10 15:03:23 -0700233}
234
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700235sp<EventThreadConnection> Scheduler::createConnectionInternal(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700236 EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) {
237 return eventThread->createEventConnection([&] { resync(); }, configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700238}
239
Ana Krulec98b5b242018-08-10 15:03:23 -0700240sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700241 ConnectionHandle handle, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700242 RETURN_IF_INVALID_HANDLE(handle, nullptr);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700243 return createConnectionInternal(mConnections[handle].thread.get(), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700244}
245
Dominik Laskowski98041832019-08-01 18:35:59 -0700246sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
247 RETURN_IF_INVALID_HANDLE(handle, nullptr);
248 return mConnections[handle].connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700249}
250
Dominik Laskowski98041832019-08-01 18:35:59 -0700251void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
252 bool connected) {
253 RETURN_IF_INVALID_HANDLE(handle);
254 mConnections[handle].thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700255}
256
Dominik Laskowski98041832019-08-01 18:35:59 -0700257void Scheduler::onScreenAcquired(ConnectionHandle handle) {
258 RETURN_IF_INVALID_HANDLE(handle);
259 mConnections[handle].thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700260}
261
Dominik Laskowski98041832019-08-01 18:35:59 -0700262void Scheduler::onScreenReleased(ConnectionHandle handle) {
263 RETURN_IF_INVALID_HANDLE(handle);
264 mConnections[handle].thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700265}
266
Ady Abrahamdfd62162020-06-10 16:11:56 -0700267void Scheduler::onPrimaryDisplayConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId,
268 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
269 std::lock_guard<std::mutex> lock(mFeatureStateLock);
270 // Cache the last reported config for primary display.
271 mFeatures.cachedConfigChangedParams = {handle, displayId, configId, vsyncPeriod};
272 onNonPrimaryDisplayConfigChanged(handle, displayId, configId, vsyncPeriod);
273}
274
275void Scheduler::dispatchCachedReportedConfig() {
276 const auto configId = *mFeatures.configId;
277 const auto vsyncPeriod =
278 mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getVsyncPeriod();
279
280 // If there is no change from cached config, there is no need to dispatch an event
281 if (configId == mFeatures.cachedConfigChangedParams->configId &&
282 vsyncPeriod == mFeatures.cachedConfigChangedParams->vsyncPeriod) {
283 return;
284 }
285
286 mFeatures.cachedConfigChangedParams->configId = configId;
287 mFeatures.cachedConfigChangedParams->vsyncPeriod = vsyncPeriod;
288 onNonPrimaryDisplayConfigChanged(mFeatures.cachedConfigChangedParams->handle,
289 mFeatures.cachedConfigChangedParams->displayId,
290 mFeatures.cachedConfigChangedParams->configId,
291 mFeatures.cachedConfigChangedParams->vsyncPeriod);
292}
293
294void Scheduler::onNonPrimaryDisplayConfigChanged(ConnectionHandle handle,
295 PhysicalDisplayId displayId,
296 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700297 RETURN_IF_INVALID_HANDLE(handle);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700298 mConnections[handle].thread->onConfigChanged(displayId, configId, vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800299}
300
Alec Mouri717bcb62020-02-10 17:07:19 -0800301size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) {
302 RETURN_IF_INVALID_HANDLE(handle, 0);
303 return mConnections[handle].thread->getEventThreadConnectionCount();
304}
305
Dominik Laskowski98041832019-08-01 18:35:59 -0700306void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
307 RETURN_IF_INVALID_HANDLE(handle);
308 mConnections.at(handle).thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700309}
310
Ady Abraham9c53ee72020-07-22 21:16:18 -0700311void Scheduler::setDuration(ConnectionHandle handle, std::chrono::nanoseconds workDuration,
312 std::chrono::nanoseconds readyDuration) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700313 RETURN_IF_INVALID_HANDLE(handle);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700314 mConnections[handle].thread->setDuration(workDuration, readyDuration);
Ana Krulec98b5b242018-08-10 15:03:23 -0700315}
Ana Krulece588e312018-09-18 12:32:24 -0700316
Ady Abraham8cb21882020-08-26 18:22:05 -0700317void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats, nsecs_t now) {
318 stats->vsyncTime = mVsyncSchedule.tracker->nextAnticipatedVSyncTimeFrom(now);
319 stats->vsyncPeriod = mVsyncSchedule.tracker->currentPeriod();
Ana Krulece588e312018-09-18 12:32:24 -0700320}
321
Dominik Laskowski6505f792019-09-18 11:10:05 -0700322Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
323 if (mInjectVSyncs == enable) {
324 return {};
325 }
326
327 ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling");
328
329 if (!mInjectorConnectionHandle) {
330 auto vsyncSource = std::make_unique<InjectVSyncSource>();
331 mVSyncInjector = vsyncSource.get();
332
333 auto eventThread =
334 std::make_unique<impl::EventThread>(std::move(vsyncSource),
335 impl::EventThread::InterceptVSyncsCallback());
336
337 mInjectorConnectionHandle = createConnection(std::move(eventThread));
338 }
339
340 mInjectVSyncs = enable;
341 return mInjectorConnectionHandle;
342}
343
Ady Abraham9c53ee72020-07-22 21:16:18 -0700344bool Scheduler::injectVSync(nsecs_t when, nsecs_t expectedVSyncTime, nsecs_t deadlineTimestamp) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700345 if (!mInjectVSyncs || !mVSyncInjector) {
346 return false;
347 }
348
Ady Abraham9c53ee72020-07-22 21:16:18 -0700349 mVSyncInjector->onInjectSyncEvent(when, expectedVSyncTime, deadlineTimestamp);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700350 return true;
351}
352
Ana Krulece588e312018-09-18 12:32:24 -0700353void Scheduler::enableHardwareVsync() {
354 std::lock_guard<std::mutex> lock(mHWVsyncLock);
355 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700356 mVsyncSchedule.tracker->resetModel();
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700357 mSchedulerCallback.setVsyncEnabled(true);
Ana Krulece588e312018-09-18 12:32:24 -0700358 mPrimaryHWVsyncEnabled = true;
359 }
360}
361
362void Scheduler::disableHardwareVsync(bool makeUnavailable) {
363 std::lock_guard<std::mutex> lock(mHWVsyncLock);
364 if (mPrimaryHWVsyncEnabled) {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700365 mSchedulerCallback.setVsyncEnabled(false);
Ana Krulece588e312018-09-18 12:32:24 -0700366 mPrimaryHWVsyncEnabled = false;
367 }
368 if (makeUnavailable) {
369 mHWVsyncAvailable = false;
370 }
371}
372
Ana Krulecc2870422019-01-29 19:00:58 -0800373void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
374 {
375 std::lock_guard<std::mutex> lock(mHWVsyncLock);
376 if (makeAvailable) {
377 mHWVsyncAvailable = makeAvailable;
378 } else if (!mHWVsyncAvailable) {
379 // Hardware vsync is not currently available, so abort the resync
380 // attempt for now
381 return;
382 }
383 }
384
385 if (period <= 0) {
386 return;
387 }
388
389 setVsyncPeriod(period);
390}
391
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700392void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700393 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800394
395 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700396 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800397
398 if (now - last > kIgnoreDelay) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700399 resyncToHardwareVsync(false, mRefreshRateConfigs.getCurrentRefreshRate().getVsyncPeriod());
Ana Krulecc2870422019-01-29 19:00:58 -0800400 }
401}
402
Dominik Laskowski98041832019-08-01 18:35:59 -0700403void Scheduler::setVsyncPeriod(nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800404 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ady Abraham8cb21882020-08-26 18:22:05 -0700405 mVsyncSchedule.controller->startPeriodTransition(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800406
407 if (!mPrimaryHWVsyncEnabled) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700408 mVsyncSchedule.tracker->resetModel();
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700409 mSchedulerCallback.setVsyncEnabled(true);
Ady Abraham3aff9172019-02-07 19:10:26 -0800410 mPrimaryHWVsyncEnabled = true;
411 }
Ana Krulece588e312018-09-18 12:32:24 -0700412}
413
Ady Abraham5dee2f12020-02-05 17:49:47 -0800414void Scheduler::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
415 bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700416 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700417 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700418 { // Scope for the lock
419 std::lock_guard<std::mutex> lock(mHWVsyncLock);
420 if (mPrimaryHWVsyncEnabled) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700421 needsHwVsync = mVsyncSchedule.controller->addHwVsyncTimestamp(timestamp, hwcVsyncPeriod,
422 periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700423 }
424 }
425
426 if (needsHwVsync) {
427 enableHardwareVsync();
428 } else {
429 disableHardwareVsync(false);
430 }
431}
432
433void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700434 if (mVsyncSchedule.controller->addPresentFence(fenceTime)) {
Ana Krulece588e312018-09-18 12:32:24 -0700435 enableHardwareVsync();
436 } else {
437 disableHardwareVsync(false);
438 }
439}
440
441void Scheduler::setIgnorePresentFences(bool ignore) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700442 mVsyncSchedule.controller->setIgnorePresentFences(ignore);
Ady Abrahamc3e21312019-02-07 14:30:23 -0800443}
444
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700445void Scheduler::registerLayer(Layer* layer) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800446 if (!mLayerHistory) return;
447
Steven Thomasdebafed2020-05-18 17:30:35 -0700448 const auto minFps = mRefreshRateConfigs.getMinRefreshRate().getFps();
449 const auto maxFps = mRefreshRateConfigs.getMaxRefreshRate().getFps();
450
Michael Wright44753b12020-07-08 13:48:11 +0100451 if (layer->getWindowType() == InputWindowInfo::Type::STATUS_BAR) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700452 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ana Krulec3d367c82020-02-25 15:02:01 -0800453 scheduler::LayerHistory::LayerVoteType::NoVote);
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700454 } else if (!mOptions.useContentDetection) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700455 // If the content detection feature is off, all layers are registered at Max. We still keep
456 // the layer history, since we use it for other features (like Frame Rate API), so layers
457 // still need to be registered.
458 mLayerHistory->registerLayer(layer, minFps, maxFps,
459 scheduler::LayerHistory::LayerVoteType::Max);
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700460 } else if (!mOptions.useContentDetectionV2) {
Steven Thomasdebafed2020-05-18 17:30:35 -0700461 // In V1 of content detection, all layers are registered as Heuristic (unless it's
462 // wallpaper).
463 const auto highFps =
Michael Wright44753b12020-07-08 13:48:11 +0100464 layer->getWindowType() == InputWindowInfo::Type::WALLPAPER ? minFps : maxFps;
Ana Krulec3d367c82020-02-25 15:02:01 -0800465
Steven Thomasdebafed2020-05-18 17:30:35 -0700466 mLayerHistory->registerLayer(layer, minFps, highFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800467 scheduler::LayerHistory::LayerVoteType::Heuristic);
468 } else {
Michael Wright44753b12020-07-08 13:48:11 +0100469 if (layer->getWindowType() == InputWindowInfo::Type::WALLPAPER) {
Ana Krulec3d367c82020-02-25 15:02:01 -0800470 // Running Wallpaper at Min is considered as part of content detection.
Steven Thomasdebafed2020-05-18 17:30:35 -0700471 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800472 scheduler::LayerHistory::LayerVoteType::Min);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800473 } else {
Steven Thomasdebafed2020-05-18 17:30:35 -0700474 mLayerHistory->registerLayer(layer, minFps, maxFps,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800475 scheduler::LayerHistory::LayerVoteType::Heuristic);
476 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800477 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700478}
479
Ady Abraham5def7332020-05-29 16:13:47 -0700480void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime,
481 LayerHistory::LayerUpdateType updateType) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800482 if (mLayerHistory) {
Ady Abraham5def7332020-05-29 16:13:47 -0700483 mLayerHistory->record(layer, presentTime, systemTime(), updateType);
Dominik Laskowski49cea512019-11-12 14:13:23 -0800484 }
Ana Krulec3084c052018-11-21 20:27:17 +0100485}
486
Ady Abraham32efd542020-05-19 17:49:26 -0700487void Scheduler::setConfigChangePending(bool pending) {
488 if (mLayerHistory) {
489 mLayerHistory->setConfigChangePending(pending);
490 }
491}
492
Dominik Laskowski49cea512019-11-12 14:13:23 -0800493void Scheduler::chooseRefreshRateForContent() {
494 if (!mLayerHistory) return;
495
Ady Abraham8a82ba62020-01-17 12:43:17 -0800496 ATRACE_CALL();
497
498 scheduler::LayerHistory::Summary summary = mLayerHistory->summarize(systemTime());
Ady Abraham2139f732019-11-13 18:56:40 -0800499 HwcConfigIndexType newConfigId;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700500 {
501 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800502 if (mFeatures.contentRequirements == summary) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700503 return;
504 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800505 mFeatures.contentRequirements = summary;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800506 mFeatures.contentDetectionV1 =
Ady Abraham8a82ba62020-01-17 12:43:17 -0800507 !summary.empty() ? ContentDetectionState::On : ContentDetectionState::Off;
508
Ady Abrahamdfd62162020-06-10 16:11:56 -0700509 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
510 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800511 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700512 // We don't need to change the config, but we might need to send an event
513 // about a config change, since it was suppressed due to a previous idleConsidered
514 if (!consideredSignals.idle) {
515 dispatchCachedReportedConfig();
516 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700517 return;
518 }
Ady Abraham2139f732019-11-13 18:56:40 -0800519 mFeatures.configId = newConfigId;
Ady Abraham2e1dd892020-03-05 13:48:36 -0800520 auto& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700521 mSchedulerCallback.changeRefreshRate(newRefreshRate,
522 consideredSignals.idle ? ConfigEvent::None
523 : ConfigEvent::Changed);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800524 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800525}
526
Ana Krulecfb772822018-11-30 10:44:07 +0100527void Scheduler::resetIdleTimer() {
528 if (mIdleTimer) {
529 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800530 }
531}
532
Ady Abraham8532d012019-05-08 14:50:56 -0700533void Scheduler::notifyTouchEvent() {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700534 if (mTouchTimer) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800535 mTouchTimer->reset();
Steven Thomas540730a2020-01-08 20:12:42 -0800536
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700537 if (mOptions.supportKernelTimer && mIdleTimer) {
Steven Thomas540730a2020-01-08 20:12:42 -0800538 mIdleTimer->reset();
539 }
Dominik Laskowski49cea512019-11-12 14:13:23 -0800540 }
Ady Abraham8532d012019-05-08 14:50:56 -0700541}
542
Ady Abraham6fe2c172019-07-12 12:37:57 -0700543void Scheduler::setDisplayPowerState(bool normal) {
544 {
545 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700546 mFeatures.isDisplayPowerStateNormal = normal;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700547 }
548
549 if (mDisplayPowerTimer) {
550 mDisplayPowerTimer->reset();
551 }
552
553 // Display Power event will boost the refresh rate to performance.
554 // Clear Layer History to get fresh FPS detection
Dominik Laskowski49cea512019-11-12 14:13:23 -0800555 if (mLayerHistory) {
556 mLayerHistory->clear();
557 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700558}
559
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700560void Scheduler::kernelIdleTimerCallback(TimerState state) {
561 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100562
Ady Abraham2139f732019-11-13 18:56:40 -0800563 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
564 // magic number
Ady Abraham2e1dd892020-03-05 13:48:36 -0800565 const auto& refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
Ady Abraham2139f732019-11-13 18:56:40 -0800566 constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f;
Ady Abrahamabc27602020-04-08 17:20:29 -0700567 if (state == TimerState::Reset && refreshRate.getFps() > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700568 // If we're not in performance mode then the kernel timer shouldn't do
569 // anything, as the refresh rate during DPU power collapse will be the
570 // same.
Ady Abrahamabc27602020-04-08 17:20:29 -0700571 resyncToHardwareVsync(true /* makeAvailable */, refreshRate.getVsyncPeriod());
572 } else if (state == TimerState::Expired &&
573 refreshRate.getFps() <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700574 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
575 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
Ady Abraham8cb21882020-08-26 18:22:05 -0700576 // need to update the VsyncController model anyway.
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700577 disableHardwareVsync(false /* makeUnavailable */);
Alec Mouridc28b372019-04-18 21:17:13 -0700578 }
Ady Abrahama09852a2020-02-20 14:23:42 -0800579
580 mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
Alec Mouridc28b372019-04-18 21:17:13 -0700581}
582
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700583void Scheduler::idleTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700584 handleTimerStateChanged(&mFeatures.idleTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700585 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100586}
587
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700588void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700589 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700590 // Touch event will boost the refresh rate to performance.
591 // Clear layer history to get fresh FPS detection.
592 // NOTE: Instead of checking all the layers, we should be checking the layer
593 // that is currently on top. b/142507166 will give us this capability.
Ady Abrahamdfd62162020-06-10 16:11:56 -0700594 if (handleTimerStateChanged(&mFeatures.touch, touch)) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700595 if (mLayerHistory) {
596 mLayerHistory->clear();
597 }
Ady Abraham1adbb722020-05-15 11:51:48 -0700598 }
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700599 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700600}
601
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700602void Scheduler::displayPowerTimerCallback(TimerState state) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700603 handleTimerStateChanged(&mFeatures.displayPowerTimer, state);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700604 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700605}
606
Dominik Laskowski98041832019-08-01 18:35:59 -0700607void Scheduler::dump(std::string& result) const {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800608 using base::StringAppendF;
Dominik Laskowski98041832019-08-01 18:35:59 -0700609
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700610 StringAppendF(&result, "+ Idle timer: %s\n", mIdleTimer ? mIdleTimer->dump().c_str() : "off");
Ana Krulec3d367c82020-02-25 15:02:01 -0800611 StringAppendF(&result, "+ Touch timer: %s\n",
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700612 mTouchTimer ? mTouchTimer->dump().c_str() : "off");
613 StringAppendF(&result, "+ Content detection: %s %s\n\n",
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700614 toContentDetectionString(mOptions.useContentDetection,
615 mOptions.useContentDetectionV2),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700616 mLayerHistory ? mLayerHistory->dump().c_str() : "(no layer history)");
Ana Krulecb43429d2019-01-09 14:28:51 -0800617}
618
Ady Abraham8cb21882020-08-26 18:22:05 -0700619void Scheduler::dumpVsync(std::string& s) const {
Ady Abraham8735eac2020-08-12 16:35:04 -0700620 using base::StringAppendF;
621
622 StringAppendF(&s, "VSyncReactor:\n");
Ady Abraham8cb21882020-08-26 18:22:05 -0700623 mVsyncSchedule.controller->dump(s);
Ady Abraham8735eac2020-08-12 16:35:04 -0700624 StringAppendF(&s, "VSyncDispatch:\n");
625 mVsyncSchedule.dispatch->dump(s);
626}
627
Ady Abraham6fe2c172019-07-12 12:37:57 -0700628template <class T>
Ady Abrahamdfd62162020-06-10 16:11:56 -0700629bool Scheduler::handleTimerStateChanged(T* currentState, T newState) {
Ady Abraham2139f732019-11-13 18:56:40 -0800630 HwcConfigIndexType newConfigId;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700631 scheduler::RefreshRateConfigs::GlobalSignals consideredSignals;
Ady Abraham8532d012019-05-08 14:50:56 -0700632 {
633 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700634 if (*currentState == newState) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700635 return false;
Ady Abraham8532d012019-05-08 14:50:56 -0700636 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700637 *currentState = newState;
Ady Abrahamdfd62162020-06-10 16:11:56 -0700638 newConfigId = calculateRefreshRateConfigIndexType(&consideredSignals);
Ady Abraham2139f732019-11-13 18:56:40 -0800639 if (mFeatures.configId == newConfigId) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700640 // We don't need to change the config, but we might need to send an event
641 // about a config change, since it was suppressed due to a previous idleConsidered
642 if (!consideredSignals.idle) {
643 dispatchCachedReportedConfig();
644 }
645 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700646 }
Ady Abraham2139f732019-11-13 18:56:40 -0800647 mFeatures.configId = newConfigId;
Ady Abraham8532d012019-05-08 14:50:56 -0700648 }
Ady Abraham2139f732019-11-13 18:56:40 -0800649 const RefreshRate& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abrahamdfd62162020-06-10 16:11:56 -0700650 mSchedulerCallback.changeRefreshRate(newRefreshRate,
651 consideredSignals.idle ? ConfigEvent::None
652 : ConfigEvent::Changed);
653 return consideredSignals.touch;
Ady Abraham8532d012019-05-08 14:50:56 -0700654}
655
Ady Abrahamdfd62162020-06-10 16:11:56 -0700656HwcConfigIndexType Scheduler::calculateRefreshRateConfigIndexType(
657 scheduler::RefreshRateConfigs::GlobalSignals* consideredSignals) {
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800658 ATRACE_CALL();
Ady Abrahamdfd62162020-06-10 16:11:56 -0700659 if (consideredSignals) *consideredSignals = {};
Ady Abraham09bd3922019-04-08 10:44:56 -0700660
Steven Thomasf734df42020-04-13 21:09:28 -0700661 // If Display Power is not in normal operation we want to be in performance mode. When coming
662 // back to normal mode, a grace period is given with DisplayPowerTimer.
Ana Krulec3f6a2062020-01-23 15:48:01 -0800663 if (mDisplayPowerTimer &&
664 (!mFeatures.isDisplayPowerStateNormal ||
665 mFeatures.displayPowerTimer == TimerState::Reset)) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700666 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800667 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700668
Steven Thomasbb374322020-04-28 22:47:16 -0700669 const bool touchActive = mTouchTimer && mFeatures.touch == TouchState::Active;
670 const bool idle = mIdleTimer && mFeatures.idleTimer == TimerState::Expired;
671
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700672 if (!mOptions.useContentDetectionV2) {
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800673 // As long as touch is active we want to be in performance mode.
Steven Thomasbb374322020-04-28 22:47:16 -0700674 if (touchActive) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700675 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800676 }
Ady Abraham8532d012019-05-08 14:50:56 -0700677
Steven Thomasbb374322020-04-28 22:47:16 -0700678 // If timer has expired as it means there is no new content on the screen.
679 if (idle) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700680 if (consideredSignals) consideredSignals->idle = true;
Steven Thomasbb374322020-04-28 22:47:16 -0700681 return mRefreshRateConfigs.getMinRefreshRateByPolicy().getConfigId();
682 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700683
Ana Krulec3f6a2062020-01-23 15:48:01 -0800684 // If content detection is off we choose performance as we don't know the content fps.
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800685 if (mFeatures.contentDetectionV1 == ContentDetectionState::Off) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800686 // NOTE: V1 always calls this, but this is not a default behavior for V2.
Ady Abrahamabc27602020-04-08 17:20:29 -0700687 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().getConfigId();
Steven Thomas540730a2020-01-08 20:12:42 -0800688 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800689
690 // Content detection is on, find the appropriate refresh rate with minimal error
Ady Abrahamabc27602020-04-08 17:20:29 -0700691 return mRefreshRateConfigs.getRefreshRateForContent(mFeatures.contentRequirements)
692 .getConfigId();
Ady Abraham09bd3922019-04-08 10:44:56 -0700693 }
694
Ady Abraham1adbb722020-05-15 11:51:48 -0700695 return mRefreshRateConfigs
Ady Abrahamdfd62162020-06-10 16:11:56 -0700696 .getBestRefreshRate(mFeatures.contentRequirements, {.touch = touchActive, .idle = idle},
697 consideredSignals)
Ady Abraham1adbb722020-05-15 11:51:48 -0700698 .getConfigId();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800699}
700
Ady Abraham2139f732019-11-13 18:56:40 -0800701std::optional<HwcConfigIndexType> Scheduler::getPreferredConfigId() {
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700702 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800703 // Make sure that the default config ID is first updated, before returned.
704 if (mFeatures.configId.has_value()) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800705 mFeatures.configId = calculateRefreshRateConfigIndexType();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800706 }
Ady Abraham2139f732019-11-13 18:56:40 -0800707 return mFeatures.configId;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700708}
709
Peiyong Line9d809e2020-04-14 13:10:48 -0700710void Scheduler::onNewVsyncPeriodChangeTimeline(const hal::VsyncPeriodChangeTimeline& timeline) {
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800711 if (timeline.refreshRequired) {
712 mSchedulerCallback.repaintEverythingForHWC();
713 }
714
715 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
716 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
717
718 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
719 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
720 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
721 }
722}
723
724void Scheduler::onDisplayRefreshed(nsecs_t timestamp) {
725 bool callRepaint = false;
726 {
727 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
728 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
729 if (mLastVsyncPeriodChangeTimeline->refreshTimeNanos < timestamp) {
730 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
731 } else {
732 // We need to send another refresh as refreshTimeNanos is still in the future
733 callRepaint = true;
734 }
735 }
736 }
737
738 if (callRepaint) {
739 mSchedulerCallback.repaintEverythingForHWC();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800740 }
741}
742
Ady Abraham8a82ba62020-01-17 12:43:17 -0800743void Scheduler::onPrimaryDisplayAreaChanged(uint32_t displayArea) {
744 if (mLayerHistory) {
745 mLayerHistory->setDisplayArea(displayArea);
746 }
747}
748
Ana Krulec98b5b242018-08-10 15:03:23 -0700749} // namespace android