blob: 52045dc15081f515e5ab1d63ba1563f87772037d [file] [log] [blame]
Ana Krulec98b5b242018-08-10 15:03:23 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski98041832019-08-01 18:35:59 -070017#undef LOG_TAG
18#define LOG_TAG "Scheduler"
Ana Krulec7ab56032018-11-02 20:51:06 +010019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
Ana Krulec98b5b242018-08-10 15:03:23 -070021#include "Scheduler.h"
22
Dominik Laskowski49cea512019-11-12 14:13:23 -080023#include <android-base/stringprintf.h>
Ana Krulece588e312018-09-18 12:32:24 -070024#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
25#include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
Ana Krulece588e312018-09-18 12:32:24 -070026#include <configstore/Utils.h>
Ana Krulecfb772822018-11-30 10:44:07 +010027#include <cutils/properties.h>
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070028#include <input/InputWindow.h>
Ana Krulecfefd6ae2019-02-13 17:53:08 -080029#include <system/window.h>
Ana Krulece588e312018-09-18 12:32:24 -070030#include <ui/DisplayStatInfo.h>
Ana Krulec3084c052018-11-21 20:27:17 +010031#include <utils/Timers.h>
Ana Krulec7ab56032018-11-02 20:51:06 +010032#include <utils/Trace.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070033
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070034#include <algorithm>
35#include <cinttypes>
36#include <cstdint>
37#include <functional>
38#include <memory>
39#include <numeric>
40
41#include "../Layer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070042#include "DispSync.h"
43#include "DispSyncSource.h"
Ana Krulece588e312018-09-18 12:32:24 -070044#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070045#include "EventThread.h"
Dominik Laskowski6505f792019-09-18 11:10:05 -070046#include "InjectVSyncSource.h"
Ana Krulecf2c006d2019-06-21 15:37:07 -070047#include "OneShotTimer.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010048#include "SchedulerUtils.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090049#include "SurfaceFlingerProperties.h"
Kevin DuBois00287382019-11-19 15:11:55 -080050#include "Timer.h"
51#include "VSyncDispatchTimerQueue.h"
52#include "VSyncPredictor.h"
53#include "VSyncReactor.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070054
Dominik Laskowski98041832019-08-01 18:35:59 -070055#define RETURN_IF_INVALID_HANDLE(handle, ...) \
56 do { \
57 if (mConnections.count(handle) == 0) { \
58 ALOGE("Invalid connection handle %" PRIuPTR, handle.id); \
59 return __VA_ARGS__; \
60 } \
61 } while (false)
62
Ana Krulec98b5b242018-08-10 15:03:23 -070063namespace android {
64
Kevin DuBois00287382019-11-19 15:11:55 -080065std::unique_ptr<DispSync> createDispSync() {
66 // TODO (140302863) remove this and use the vsync_reactor system.
Kevin DuBoisc57f2c32019-12-20 16:32:29 -080067 if (property_get_bool("debug.sf.vsync_reactor", true)) {
Kevin DuBois00287382019-11-19 15:11:55 -080068 // TODO (144707443) tune Predictor tunables.
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 auto tracker = 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 static constexpr auto vsyncMoveThreshold =
83 std::chrono::duration_cast<std::chrono::nanoseconds>(3ms);
84 static constexpr auto timerSlack =
85 std::chrono::duration_cast<std::chrono::nanoseconds>(500us);
86 auto dispatch = std::make_unique<
87 scheduler::VSyncDispatchTimerQueue>(std::make_unique<scheduler::Timer>(), *tracker,
88 timerSlack.count(), vsyncMoveThreshold.count());
89
90 static constexpr size_t pendingFenceLimit = 20;
91 return std::make_unique<scheduler::VSyncReactor>(std::make_unique<scheduler::SystemClock>(),
92 std::move(dispatch), std::move(tracker),
93 pendingFenceLimit);
94 } else {
95 return std::make_unique<impl::DispSync>("SchedulerDispSync",
96 sysprop::running_without_sync_framework(true));
97 }
98}
99
Ady Abraham09bd3922019-04-08 10:44:56 -0700100Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function,
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800101 const scheduler::RefreshRateConfigs& refreshRateConfig,
Ana Krulec3d367c82020-02-25 15:02:01 -0800102 ISchedulerCallback& schedulerCallback, bool useContentDetectionV2,
103 bool useContentDetection)
Kevin DuBois00287382019-11-19 15:11:55 -0800104 : mPrimaryDispSync(createDispSync()),
Dominik Laskowski98041832019-08-01 18:35:59 -0700105 mEventControlThread(new impl::EventControlThread(std::move(function))),
106 mSupportKernelTimer(sysprop::support_kernel_idle_timer(false)),
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800107 mSchedulerCallback(schedulerCallback),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800108 mRefreshRateConfigs(refreshRateConfig),
Ana Krulec3d367c82020-02-25 15:02:01 -0800109 mUseContentDetection(useContentDetection),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800110 mUseContentDetectionV2(useContentDetectionV2) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700111 using namespace sysprop;
Ady Abraham8532d012019-05-08 14:50:56 -0700112
Ana Krulec3803b8d2020-02-03 16:35:46 -0800113 if (mUseContentDetectionV2) {
114 mLayerHistory = std::make_unique<scheduler::impl::LayerHistoryV2>();
115 } else {
116 mLayerHistory = std::make_unique<scheduler::impl::LayerHistory>();
Dominik Laskowski49cea512019-11-12 14:13:23 -0800117 }
118
119 const int setIdleTimerMs = property_get_int32("debug.sf.set_idle_timer_ms", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100120
Dominik Laskowski98041832019-08-01 18:35:59 -0700121 if (const auto millis = setIdleTimerMs ? setIdleTimerMs : set_idle_timer_ms(0); millis > 0) {
122 const auto callback = mSupportKernelTimer ? &Scheduler::kernelIdleTimerCallback
123 : &Scheduler::idleTimerCallback;
Dominik Laskowski98041832019-08-01 18:35:59 -0700124 mIdleTimer.emplace(
125 std::chrono::milliseconds(millis),
126 [this, callback] { std::invoke(callback, this, TimerState::Reset); },
127 [this, callback] { std::invoke(callback, this, TimerState::Expired); });
Ana Krulecfb772822018-11-30 10:44:07 +0100128 mIdleTimer->start();
129 }
Ady Abraham8532d012019-05-08 14:50:56 -0700130
Dominik Laskowski98041832019-08-01 18:35:59 -0700131 if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -0700132 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -0700133 mTouchTimer.emplace(
134 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700135 [this] { touchTimerCallback(TimerState::Reset); },
136 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -0700137 mTouchTimer->start();
138 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700139
Dominik Laskowski98041832019-08-01 18:35:59 -0700140 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
141 mDisplayPowerTimer.emplace(
142 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700143 [this] { displayPowerTimerCallback(TimerState::Reset); },
144 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -0700145 mDisplayPowerTimer->start();
146 }
Ana Krulece588e312018-09-18 12:32:24 -0700147}
148
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700149Scheduler::Scheduler(std::unique_ptr<DispSync> primaryDispSync,
150 std::unique_ptr<EventControlThread> eventControlThread,
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800151 const scheduler::RefreshRateConfigs& configs,
Ana Krulec3d367c82020-02-25 15:02:01 -0800152 ISchedulerCallback& schedulerCallback, bool useContentDetectionV2,
153 bool useContentDetection)
Dominik Laskowski98041832019-08-01 18:35:59 -0700154 : mPrimaryDispSync(std::move(primaryDispSync)),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700155 mEventControlThread(std::move(eventControlThread)),
Dominik Laskowski98041832019-08-01 18:35:59 -0700156 mSupportKernelTimer(false),
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800157 mSchedulerCallback(schedulerCallback),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800158 mRefreshRateConfigs(configs),
Ana Krulec3d367c82020-02-25 15:02:01 -0800159 mUseContentDetection(useContentDetection),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800160 mUseContentDetectionV2(useContentDetectionV2) {}
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700161
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800162Scheduler::~Scheduler() {
Ana Krulecf2c006d2019-06-21 15:37:07 -0700163 // Ensure the OneShotTimer threads are joined before we start destroying state.
Ady Abraham6fe2c172019-07-12 12:37:57 -0700164 mDisplayPowerTimer.reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700165 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800166 mIdleTimer.reset();
167}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700168
Dominik Laskowski98041832019-08-01 18:35:59 -0700169DispSync& Scheduler::getPrimaryDispSync() {
170 return *mPrimaryDispSync;
171}
172
Ady Abraham9e16a482019-12-03 17:19:41 -0800173std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(const char* name,
174 nsecs_t phaseOffsetNs) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700175 return std::make_unique<DispSyncSource>(mPrimaryDispSync.get(), phaseOffsetNs,
Ady Abraham9e16a482019-12-03 17:19:41 -0800176 true /* traceVsync */, name);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700177}
178
Dominik Laskowski98041832019-08-01 18:35:59 -0700179Scheduler::ConnectionHandle Scheduler::createConnection(
Ady Abraham9e16a482019-12-03 17:19:41 -0800180 const char* connectionName, nsecs_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -0700181 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ady Abraham9e16a482019-12-03 17:19:41 -0800182 auto vsyncSource = makePrimaryDispSyncSource(connectionName, phaseOffsetNs);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700183 auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource),
184 std::move(interceptCallback));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700185 return createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700186}
Ana Krulec98b5b242018-08-10 15:03:23 -0700187
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700188Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700189 const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
190 ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800191
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700192 auto connection =
193 createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowski98041832019-08-01 18:35:59 -0700194
195 mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
196 return handle;
Ana Krulec98b5b242018-08-10 15:03:23 -0700197}
198
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700199sp<EventThreadConnection> Scheduler::createConnectionInternal(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700200 EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) {
201 return eventThread->createEventConnection([&] { resync(); }, configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700202}
203
Ana Krulec98b5b242018-08-10 15:03:23 -0700204sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700205 ConnectionHandle handle, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700206 RETURN_IF_INVALID_HANDLE(handle, nullptr);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700207 return createConnectionInternal(mConnections[handle].thread.get(), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700208}
209
Dominik Laskowski98041832019-08-01 18:35:59 -0700210sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
211 RETURN_IF_INVALID_HANDLE(handle, nullptr);
212 return mConnections[handle].connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700213}
214
Dominik Laskowski98041832019-08-01 18:35:59 -0700215void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
216 bool connected) {
217 RETURN_IF_INVALID_HANDLE(handle);
218 mConnections[handle].thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700219}
220
Dominik Laskowski98041832019-08-01 18:35:59 -0700221void Scheduler::onScreenAcquired(ConnectionHandle handle) {
222 RETURN_IF_INVALID_HANDLE(handle);
223 mConnections[handle].thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700224}
225
Dominik Laskowski98041832019-08-01 18:35:59 -0700226void Scheduler::onScreenReleased(ConnectionHandle handle) {
227 RETURN_IF_INVALID_HANDLE(handle);
228 mConnections[handle].thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700229}
230
Dominik Laskowski98041832019-08-01 18:35:59 -0700231void Scheduler::onConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700232 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700233 RETURN_IF_INVALID_HANDLE(handle);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700234 mConnections[handle].thread->onConfigChanged(displayId, configId, vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800235}
236
Alec Mouri717bcb62020-02-10 17:07:19 -0800237size_t Scheduler::getEventThreadConnectionCount(ConnectionHandle handle) {
238 RETURN_IF_INVALID_HANDLE(handle, 0);
239 return mConnections[handle].thread->getEventThreadConnectionCount();
240}
241
Dominik Laskowski98041832019-08-01 18:35:59 -0700242void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
243 RETURN_IF_INVALID_HANDLE(handle);
244 mConnections.at(handle).thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700245}
246
Dominik Laskowski98041832019-08-01 18:35:59 -0700247void Scheduler::setPhaseOffset(ConnectionHandle handle, nsecs_t phaseOffset) {
248 RETURN_IF_INVALID_HANDLE(handle);
249 mConnections[handle].thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700250}
Ana Krulece588e312018-09-18 12:32:24 -0700251
252void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
253 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
254 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
255}
256
Dominik Laskowski6505f792019-09-18 11:10:05 -0700257Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
258 if (mInjectVSyncs == enable) {
259 return {};
260 }
261
262 ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling");
263
264 if (!mInjectorConnectionHandle) {
265 auto vsyncSource = std::make_unique<InjectVSyncSource>();
266 mVSyncInjector = vsyncSource.get();
267
268 auto eventThread =
269 std::make_unique<impl::EventThread>(std::move(vsyncSource),
270 impl::EventThread::InterceptVSyncsCallback());
271
272 mInjectorConnectionHandle = createConnection(std::move(eventThread));
273 }
274
275 mInjectVSyncs = enable;
276 return mInjectorConnectionHandle;
277}
278
279bool Scheduler::injectVSync(nsecs_t when) {
280 if (!mInjectVSyncs || !mVSyncInjector) {
281 return false;
282 }
283
284 mVSyncInjector->onInjectSyncEvent(when);
285 return true;
286}
287
Ana Krulece588e312018-09-18 12:32:24 -0700288void Scheduler::enableHardwareVsync() {
289 std::lock_guard<std::mutex> lock(mHWVsyncLock);
290 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
291 mPrimaryDispSync->beginResync();
292 mEventControlThread->setVsyncEnabled(true);
293 mPrimaryHWVsyncEnabled = true;
294 }
295}
296
297void Scheduler::disableHardwareVsync(bool makeUnavailable) {
298 std::lock_guard<std::mutex> lock(mHWVsyncLock);
299 if (mPrimaryHWVsyncEnabled) {
300 mEventControlThread->setVsyncEnabled(false);
301 mPrimaryDispSync->endResync();
302 mPrimaryHWVsyncEnabled = false;
303 }
304 if (makeUnavailable) {
305 mHWVsyncAvailable = false;
306 }
307}
308
Ana Krulecc2870422019-01-29 19:00:58 -0800309void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
310 {
311 std::lock_guard<std::mutex> lock(mHWVsyncLock);
312 if (makeAvailable) {
313 mHWVsyncAvailable = makeAvailable;
314 } else if (!mHWVsyncAvailable) {
315 // Hardware vsync is not currently available, so abort the resync
316 // attempt for now
317 return;
318 }
319 }
320
321 if (period <= 0) {
322 return;
323 }
324
325 setVsyncPeriod(period);
326}
327
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700328void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700329 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800330
331 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700332 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800333
334 if (now - last > kIgnoreDelay) {
Ady Abraham2139f732019-11-13 18:56:40 -0800335 resyncToHardwareVsync(false, mRefreshRateConfigs.getCurrentRefreshRate().vsyncPeriod);
Ana Krulecc2870422019-01-29 19:00:58 -0800336 }
337}
338
Dominik Laskowski98041832019-08-01 18:35:59 -0700339void Scheduler::setVsyncPeriod(nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800340 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700341 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800342
343 if (!mPrimaryHWVsyncEnabled) {
344 mPrimaryDispSync->beginResync();
345 mEventControlThread->setVsyncEnabled(true);
346 mPrimaryHWVsyncEnabled = true;
347 }
Ana Krulece588e312018-09-18 12:32:24 -0700348}
349
Ady Abraham5dee2f12020-02-05 17:49:47 -0800350void Scheduler::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
351 bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700352 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700353 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700354 { // Scope for the lock
355 std::lock_guard<std::mutex> lock(mHWVsyncLock);
356 if (mPrimaryHWVsyncEnabled) {
Ady Abraham5dee2f12020-02-05 17:49:47 -0800357 needsHwVsync =
358 mPrimaryDispSync->addResyncSample(timestamp, hwcVsyncPeriod, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700359 }
360 }
361
362 if (needsHwVsync) {
363 enableHardwareVsync();
364 } else {
365 disableHardwareVsync(false);
366 }
367}
368
369void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
370 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
371 enableHardwareVsync();
372 } else {
373 disableHardwareVsync(false);
374 }
375}
376
377void Scheduler::setIgnorePresentFences(bool ignore) {
378 mPrimaryDispSync->setIgnorePresentFences(ignore);
379}
380
Ady Abraham8fe11022019-06-12 17:11:12 -0700381nsecs_t Scheduler::getDispSyncExpectedPresentTime() {
Ady Abrahamc3e21312019-02-07 14:30:23 -0800382 return mPrimaryDispSync->expectedPresentTime();
383}
384
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700385void Scheduler::registerLayer(Layer* layer) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800386 if (!mLayerHistory) return;
387
Ana Krulec3d367c82020-02-25 15:02:01 -0800388 // If the content detection feature is off, all layers are registered at NoVote. We still
389 // keep the layer history, since we use it for other features (like Frame Rate API), so layers
390 // still need to be registered.
391 if (!mUseContentDetection) {
392 mLayerHistory->registerLayer(layer, mRefreshRateConfigs.getMinRefreshRate().fps,
393 mRefreshRateConfigs.getMaxRefreshRate().fps,
394 scheduler::LayerHistory::LayerVoteType::NoVote);
395 return;
396 }
397
398 // In V1 of content detection, all layers are registered as Heuristic (unless it's wallpaper).
Ady Abraham8a82ba62020-01-17 12:43:17 -0800399 if (!mUseContentDetectionV2) {
400 const auto lowFps = mRefreshRateConfigs.getMinRefreshRate().fps;
401 const auto highFps = layer->getWindowType() == InputWindowInfo::TYPE_WALLPAPER
402 ? lowFps
403 : mRefreshRateConfigs.getMaxRefreshRate().fps;
Dominik Laskowski49cea512019-11-12 14:13:23 -0800404
Ady Abraham8a82ba62020-01-17 12:43:17 -0800405 mLayerHistory->registerLayer(layer, lowFps, highFps,
406 scheduler::LayerHistory::LayerVoteType::Heuristic);
407 } else {
408 if (layer->getWindowType() == InputWindowInfo::TYPE_WALLPAPER) {
Ana Krulec3d367c82020-02-25 15:02:01 -0800409 // Running Wallpaper at Min is considered as part of content detection.
Ady Abraham8a82ba62020-01-17 12:43:17 -0800410 mLayerHistory->registerLayer(layer, mRefreshRateConfigs.getMinRefreshRate().fps,
411 mRefreshRateConfigs.getMaxRefreshRate().fps,
412 scheduler::LayerHistory::LayerVoteType::Min);
413 } else if (layer->getWindowType() == InputWindowInfo::TYPE_STATUS_BAR) {
414 mLayerHistory->registerLayer(layer, mRefreshRateConfigs.getMinRefreshRate().fps,
415 mRefreshRateConfigs.getMaxRefreshRate().fps,
416 scheduler::LayerHistory::LayerVoteType::NoVote);
417 } else {
418 mLayerHistory->registerLayer(layer, mRefreshRateConfigs.getMinRefreshRate().fps,
419 mRefreshRateConfigs.getMaxRefreshRate().fps,
420 scheduler::LayerHistory::LayerVoteType::Heuristic);
421 }
422
423 // TODO(146935143): Simulate youtube app vote. This should be removed once youtube calls the
424 // API to set desired rate
425 {
426 const auto vote = property_get_int32("experimental.sf.force_youtube_vote", 0);
427 if (vote != 0 &&
428 layer->getName() ==
429 "SurfaceView - "
430 "com.google.android.youtube/"
431 "com.google.android.apps.youtube.app.WatchWhileActivity#0") {
Ady Abraham71c437d2020-01-31 15:56:57 -0800432 layer->setFrameRate(
433 Layer::FrameRate(vote, Layer::FrameRateCompatibility::ExactOrMultiple));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800434 }
435 }
436 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700437}
438
Ady Abraham2139f732019-11-13 18:56:40 -0800439void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800440 if (mLayerHistory) {
Ady Abraham2139f732019-11-13 18:56:40 -0800441 mLayerHistory->record(layer, presentTime, systemTime());
Dominik Laskowski49cea512019-11-12 14:13:23 -0800442 }
Ana Krulec3084c052018-11-21 20:27:17 +0100443}
444
Dominik Laskowski49cea512019-11-12 14:13:23 -0800445void Scheduler::chooseRefreshRateForContent() {
446 if (!mLayerHistory) return;
447
Ady Abraham8a82ba62020-01-17 12:43:17 -0800448 ATRACE_CALL();
449
450 scheduler::LayerHistory::Summary summary = mLayerHistory->summarize(systemTime());
Ady Abraham2139f732019-11-13 18:56:40 -0800451 HwcConfigIndexType newConfigId;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700452 {
453 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800454 if (mFeatures.contentRequirements == summary) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700455 return;
456 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800457 mFeatures.contentRequirements = summary;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800458 mFeatures.contentDetectionV1 =
Ady Abraham8a82ba62020-01-17 12:43:17 -0800459 !summary.empty() ? ContentDetectionState::On : ContentDetectionState::Off;
460
Ana Krulec3803b8d2020-02-03 16:35:46 -0800461 newConfigId = calculateRefreshRateConfigIndexType();
Ady Abraham2139f732019-11-13 18:56:40 -0800462 if (mFeatures.configId == newConfigId) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700463 return;
464 }
Ady Abraham2139f732019-11-13 18:56:40 -0800465 mFeatures.configId = newConfigId;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800466 auto newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
467 mSchedulerCallback.changeRefreshRate(newRefreshRate, ConfigEvent::Changed);
468 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800469}
470
Ana Krulecfb772822018-11-30 10:44:07 +0100471void Scheduler::resetIdleTimer() {
472 if (mIdleTimer) {
473 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800474 }
475}
476
Ady Abraham8532d012019-05-08 14:50:56 -0700477void Scheduler::notifyTouchEvent() {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800478 if (!mTouchTimer) return;
479
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700480 // Touch event will boost the refresh rate to performance.
Steven Thomas540730a2020-01-08 20:12:42 -0800481 // Clear Layer History to get fresh FPS detection.
482 // NOTE: Instead of checking all the layers, we should be checking the layer
483 // that is currently on top. b/142507166 will give us this capability.
Ady Abraham8a82ba62020-01-17 12:43:17 -0800484 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800485 if (mLayerHistory) {
Ady Abraham6fb599b2020-03-05 13:48:22 -0800486 // Layer History will be cleared based on RefreshRateConfigs::getRefreshRateForContentV2
Steven Thomas540730a2020-01-08 20:12:42 -0800487
Ady Abraham8a82ba62020-01-17 12:43:17 -0800488 mTouchTimer->reset();
Steven Thomas540730a2020-01-08 20:12:42 -0800489
490 if (mSupportKernelTimer && mIdleTimer) {
491 mIdleTimer->reset();
492 }
Dominik Laskowski49cea512019-11-12 14:13:23 -0800493 }
Ady Abraham8532d012019-05-08 14:50:56 -0700494}
495
Ady Abraham6fe2c172019-07-12 12:37:57 -0700496void Scheduler::setDisplayPowerState(bool normal) {
497 {
498 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700499 mFeatures.isDisplayPowerStateNormal = normal;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700500 }
501
502 if (mDisplayPowerTimer) {
503 mDisplayPowerTimer->reset();
504 }
505
506 // Display Power event will boost the refresh rate to performance.
507 // Clear Layer History to get fresh FPS detection
Dominik Laskowski49cea512019-11-12 14:13:23 -0800508 if (mLayerHistory) {
509 mLayerHistory->clear();
510 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700511}
512
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700513void Scheduler::kernelIdleTimerCallback(TimerState state) {
514 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100515
Ady Abraham2139f732019-11-13 18:56:40 -0800516 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
517 // magic number
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700518 const auto refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
Ady Abraham2139f732019-11-13 18:56:40 -0800519 constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f;
520 if (state == TimerState::Reset && refreshRate.fps > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700521 // If we're not in performance mode then the kernel timer shouldn't do
522 // anything, as the refresh rate during DPU power collapse will be the
523 // same.
Ady Abraham2139f732019-11-13 18:56:40 -0800524 resyncToHardwareVsync(true /* makeAvailable */, refreshRate.vsyncPeriod);
525 } else if (state == TimerState::Expired && refreshRate.fps <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700526 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
527 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
528 // need to update the DispSync model anyway.
529 disableHardwareVsync(false /* makeUnavailable */);
Alec Mouridc28b372019-04-18 21:17:13 -0700530 }
Ady Abrahama09852a2020-02-20 14:23:42 -0800531
532 mSchedulerCallback.kernelTimerChanged(state == TimerState::Expired);
Alec Mouridc28b372019-04-18 21:17:13 -0700533}
534
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700535void Scheduler::idleTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700536 handleTimerStateChanged(&mFeatures.idleTimer, state, false /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700537 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100538}
539
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700540void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700541 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
542 handleTimerStateChanged(&mFeatures.touch, touch, true /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700543 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700544}
545
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700546void Scheduler::displayPowerTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700547 handleTimerStateChanged(&mFeatures.displayPowerTimer, state,
548 true /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700549 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700550}
551
Dominik Laskowski98041832019-08-01 18:35:59 -0700552void Scheduler::dump(std::string& result) const {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800553 using base::StringAppendF;
554 const char* const states[] = {"off", "on"};
Dominik Laskowski98041832019-08-01 18:35:59 -0700555
Dominik Laskowski49cea512019-11-12 14:13:23 -0800556 StringAppendF(&result, "+ Idle timer: %s\n",
557 mIdleTimer ? mIdleTimer->dump().c_str() : states[0]);
Ana Krulec3d367c82020-02-25 15:02:01 -0800558 StringAppendF(&result, "+ Touch timer: %s\n",
Dominik Laskowski49cea512019-11-12 14:13:23 -0800559 mTouchTimer ? mTouchTimer->dump().c_str() : states[0]);
Ana Krulec3d367c82020-02-25 15:02:01 -0800560 StringAppendF(&result, "+ Use content detection: %s\n\n",
561 sysprop::use_content_detection_for_refresh_rate(false) ? "on" : "off");
Ana Krulecb43429d2019-01-09 14:28:51 -0800562}
563
Ady Abraham6fe2c172019-07-12 12:37:57 -0700564template <class T>
565void Scheduler::handleTimerStateChanged(T* currentState, T newState, bool eventOnContentDetection) {
Ady Abraham8532d012019-05-08 14:50:56 -0700566 ConfigEvent event = ConfigEvent::None;
Ady Abraham2139f732019-11-13 18:56:40 -0800567 HwcConfigIndexType newConfigId;
Ady Abraham8532d012019-05-08 14:50:56 -0700568 {
569 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700570 if (*currentState == newState) {
Ady Abraham8532d012019-05-08 14:50:56 -0700571 return;
572 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700573 *currentState = newState;
Ana Krulec3803b8d2020-02-03 16:35:46 -0800574 newConfigId = calculateRefreshRateConfigIndexType();
Ady Abraham2139f732019-11-13 18:56:40 -0800575 if (mFeatures.configId == newConfigId) {
Ady Abraham8532d012019-05-08 14:50:56 -0700576 return;
577 }
Ady Abraham2139f732019-11-13 18:56:40 -0800578 mFeatures.configId = newConfigId;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800579 if (eventOnContentDetection && !mFeatures.contentRequirements.empty()) {
Ady Abraham8532d012019-05-08 14:50:56 -0700580 event = ConfigEvent::Changed;
581 }
582 }
Ady Abraham2139f732019-11-13 18:56:40 -0800583 const RefreshRate& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800584 mSchedulerCallback.changeRefreshRate(newRefreshRate, event);
Ady Abraham8532d012019-05-08 14:50:56 -0700585}
586
Ana Krulec3803b8d2020-02-03 16:35:46 -0800587HwcConfigIndexType Scheduler::calculateRefreshRateConfigIndexType() {
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800588 ATRACE_CALL();
Ady Abraham09bd3922019-04-08 10:44:56 -0700589
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800590 // NOTE: If we remove the kernel idle timer, and use our internal idle timer, this
Ana Krulec3803b8d2020-02-03 16:35:46 -0800591 // code will have to be refactored. If Display Power is not in normal operation we want to be in
592 // performance mode. When coming back to normal mode, a grace period is given with
593 // DisplayPowerTimer.
Ana Krulec3f6a2062020-01-23 15:48:01 -0800594 if (mDisplayPowerTimer &&
595 (!mFeatures.isDisplayPowerStateNormal ||
596 mFeatures.displayPowerTimer == TimerState::Reset)) {
597 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
598 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700599
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800600 if (!mUseContentDetectionV2) {
601 // As long as touch is active we want to be in performance mode.
602 if (mTouchTimer && mFeatures.touch == TouchState::Active) {
603 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
604 }
Ana Krulec3f6a2062020-01-23 15:48:01 -0800605 }
Ady Abraham8532d012019-05-08 14:50:56 -0700606
Ana Krulec3803b8d2020-02-03 16:35:46 -0800607 // If timer has expired as it means there is no new content on the screen.
Ana Krulec3f6a2062020-01-23 15:48:01 -0800608 if (mIdleTimer && mFeatures.idleTimer == TimerState::Expired) {
609 return mRefreshRateConfigs.getMinRefreshRateByPolicy().configId;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800610 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700611
Ady Abraham8a82ba62020-01-17 12:43:17 -0800612 if (!mUseContentDetectionV2) {
Ana Krulec3f6a2062020-01-23 15:48:01 -0800613 // If content detection is off we choose performance as we don't know the content fps.
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800614 if (mFeatures.contentDetectionV1 == ContentDetectionState::Off) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800615 // NOTE: V1 always calls this, but this is not a default behavior for V2.
Steven Thomas540730a2020-01-08 20:12:42 -0800616 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
617 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800618
619 // Content detection is on, find the appropriate refresh rate with minimal error
620 return mRefreshRateConfigs.getRefreshRateForContent(mFeatures.contentRequirements).configId;
Ady Abraham09bd3922019-04-08 10:44:56 -0700621 }
622
Ady Abraham6fb599b2020-03-05 13:48:22 -0800623 bool touchConsidered;
624 const auto& ret =
625 mRefreshRateConfigs
626 .getRefreshRateForContentV2(mFeatures.contentRequirements,
627 mTouchTimer &&
628 mFeatures.touch == TouchState::Active,
629 &touchConsidered)
630 .configId;
631 if (touchConsidered) {
632 // Clear layer history if refresh rate was selected based on touch to allow
633 // the hueristic to pick up with the new rate.
634 mLayerHistory->clear();
635 }
636
637 return ret;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800638}
639
Ady Abraham2139f732019-11-13 18:56:40 -0800640std::optional<HwcConfigIndexType> Scheduler::getPreferredConfigId() {
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700641 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800642 // Make sure that the default config ID is first updated, before returned.
643 if (mFeatures.configId.has_value()) {
Ana Krulec3803b8d2020-02-03 16:35:46 -0800644 mFeatures.configId = calculateRefreshRateConfigIndexType();
Ana Krulec3f6a2062020-01-23 15:48:01 -0800645 }
Ady Abraham2139f732019-11-13 18:56:40 -0800646 return mFeatures.configId;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700647}
648
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800649void Scheduler::onNewVsyncPeriodChangeTimeline(const HWC2::VsyncPeriodChangeTimeline& timeline) {
650 if (timeline.refreshRequired) {
651 mSchedulerCallback.repaintEverythingForHWC();
652 }
653
654 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
655 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
656
657 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
658 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
659 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
660 }
661}
662
663void Scheduler::onDisplayRefreshed(nsecs_t timestamp) {
664 bool callRepaint = false;
665 {
666 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
667 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
668 if (mLastVsyncPeriodChangeTimeline->refreshTimeNanos < timestamp) {
669 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
670 } else {
671 // We need to send another refresh as refreshTimeNanos is still in the future
672 callRepaint = true;
673 }
674 }
675 }
676
677 if (callRepaint) {
678 mSchedulerCallback.repaintEverythingForHWC();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800679 }
680}
681
Ady Abraham8a82ba62020-01-17 12:43:17 -0800682void Scheduler::onPrimaryDisplayAreaChanged(uint32_t displayArea) {
683 if (mLayerHistory) {
684 mLayerHistory->setDisplayArea(displayArea);
685 }
686}
687
Ana Krulec98b5b242018-08-10 15:03:23 -0700688} // namespace android