blob: 1d50fe1dd40e7ec0c247e4a16c1970550be04bfe [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"
Ana Krulec98b5b242018-08-10 15:03:23 -070050
Dominik Laskowski98041832019-08-01 18:35:59 -070051#define RETURN_IF_INVALID_HANDLE(handle, ...) \
52 do { \
53 if (mConnections.count(handle) == 0) { \
54 ALOGE("Invalid connection handle %" PRIuPTR, handle.id); \
55 return __VA_ARGS__; \
56 } \
57 } while (false)
58
Ana Krulec98b5b242018-08-10 15:03:23 -070059namespace android {
60
Ady Abraham09bd3922019-04-08 10:44:56 -070061Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function,
62 const scheduler::RefreshRateConfigs& refreshRateConfig)
Dominik Laskowski98041832019-08-01 18:35:59 -070063 : mPrimaryDispSync(new impl::DispSync("SchedulerDispSync",
64 sysprop::running_without_sync_framework(true))),
65 mEventControlThread(new impl::EventControlThread(std::move(function))),
66 mSupportKernelTimer(sysprop::support_kernel_idle_timer(false)),
Ady Abraham09bd3922019-04-08 10:44:56 -070067 mRefreshRateConfigs(refreshRateConfig) {
Dominik Laskowski98041832019-08-01 18:35:59 -070068 using namespace sysprop;
Ady Abraham8532d012019-05-08 14:50:56 -070069
Dominik Laskowski49cea512019-11-12 14:13:23 -080070 if (property_get_bool("debug.sf.use_smart_90_for_video", 0) || use_smart_90_for_video(false)) {
71 mLayerHistory.emplace();
72 }
73
74 const int setIdleTimerMs = property_get_int32("debug.sf.set_idle_timer_ms", 0);
Ana Krulecfb772822018-11-30 10:44:07 +010075
Dominik Laskowski98041832019-08-01 18:35:59 -070076 if (const auto millis = setIdleTimerMs ? setIdleTimerMs : set_idle_timer_ms(0); millis > 0) {
77 const auto callback = mSupportKernelTimer ? &Scheduler::kernelIdleTimerCallback
78 : &Scheduler::idleTimerCallback;
79
80 mIdleTimer.emplace(
81 std::chrono::milliseconds(millis),
82 [this, callback] { std::invoke(callback, this, TimerState::Reset); },
83 [this, callback] { std::invoke(callback, this, TimerState::Expired); });
Ana Krulecfb772822018-11-30 10:44:07 +010084 mIdleTimer->start();
85 }
Ady Abraham8532d012019-05-08 14:50:56 -070086
Dominik Laskowski98041832019-08-01 18:35:59 -070087 if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -070088 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -070089 mTouchTimer.emplace(
90 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -070091 [this] { touchTimerCallback(TimerState::Reset); },
92 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -070093 mTouchTimer->start();
94 }
Ady Abraham6fe2c172019-07-12 12:37:57 -070095
Dominik Laskowski98041832019-08-01 18:35:59 -070096 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
97 mDisplayPowerTimer.emplace(
98 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -070099 [this] { displayPowerTimerCallback(TimerState::Reset); },
100 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -0700101 mDisplayPowerTimer->start();
102 }
Ana Krulece588e312018-09-18 12:32:24 -0700103}
104
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700105Scheduler::Scheduler(std::unique_ptr<DispSync> primaryDispSync,
106 std::unique_ptr<EventControlThread> eventControlThread,
107 const scheduler::RefreshRateConfigs& configs)
Dominik Laskowski98041832019-08-01 18:35:59 -0700108 : mPrimaryDispSync(std::move(primaryDispSync)),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700109 mEventControlThread(std::move(eventControlThread)),
Dominik Laskowski98041832019-08-01 18:35:59 -0700110 mSupportKernelTimer(false),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700111 mRefreshRateConfigs(configs) {}
112
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800113Scheduler::~Scheduler() {
Ana Krulecf2c006d2019-06-21 15:37:07 -0700114 // Ensure the OneShotTimer threads are joined before we start destroying state.
Ady Abraham6fe2c172019-07-12 12:37:57 -0700115 mDisplayPowerTimer.reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700116 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800117 mIdleTimer.reset();
118}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700119
Dominik Laskowski98041832019-08-01 18:35:59 -0700120DispSync& Scheduler::getPrimaryDispSync() {
121 return *mPrimaryDispSync;
122}
123
Dominik Laskowski6505f792019-09-18 11:10:05 -0700124std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(
125 const char* name, nsecs_t phaseOffsetNs, nsecs_t offsetThresholdForNextVsync) {
126 return std::make_unique<DispSyncSource>(mPrimaryDispSync.get(), phaseOffsetNs,
127 offsetThresholdForNextVsync, true /* traceVsync */,
128 name);
129}
130
Dominik Laskowski98041832019-08-01 18:35:59 -0700131Scheduler::ConnectionHandle Scheduler::createConnection(
Ady Abraham45e4e362019-06-07 18:20:51 -0700132 const char* connectionName, nsecs_t phaseOffsetNs, nsecs_t offsetThresholdForNextVsync,
Ana Krulec98b5b242018-08-10 15:03:23 -0700133 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700134 auto vsyncSource =
135 makePrimaryDispSyncSource(connectionName, phaseOffsetNs, offsetThresholdForNextVsync);
136 auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource),
137 std::move(interceptCallback));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700138 return createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700139}
Ana Krulec98b5b242018-08-10 15:03:23 -0700140
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700141Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700142 const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
143 ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800144
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700145 auto connection =
146 createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowski98041832019-08-01 18:35:59 -0700147
148 mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
149 return handle;
Ana Krulec98b5b242018-08-10 15:03:23 -0700150}
151
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700152sp<EventThreadConnection> Scheduler::createConnectionInternal(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700153 EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) {
154 return eventThread->createEventConnection([&] { resync(); }, configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700155}
156
Ana Krulec98b5b242018-08-10 15:03:23 -0700157sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700158 ConnectionHandle handle, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700159 RETURN_IF_INVALID_HANDLE(handle, nullptr);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700160 return createConnectionInternal(mConnections[handle].thread.get(), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700161}
162
Dominik Laskowski98041832019-08-01 18:35:59 -0700163sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
164 RETURN_IF_INVALID_HANDLE(handle, nullptr);
165 return mConnections[handle].connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700166}
167
Dominik Laskowski98041832019-08-01 18:35:59 -0700168void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
169 bool connected) {
170 RETURN_IF_INVALID_HANDLE(handle);
171 mConnections[handle].thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700172}
173
Dominik Laskowski98041832019-08-01 18:35:59 -0700174void Scheduler::onScreenAcquired(ConnectionHandle handle) {
175 RETURN_IF_INVALID_HANDLE(handle);
176 mConnections[handle].thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700177}
178
Dominik Laskowski98041832019-08-01 18:35:59 -0700179void Scheduler::onScreenReleased(ConnectionHandle handle) {
180 RETURN_IF_INVALID_HANDLE(handle);
181 mConnections[handle].thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700182}
183
Dominik Laskowski98041832019-08-01 18:35:59 -0700184void Scheduler::onConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId,
Ady Abraham2139f732019-11-13 18:56:40 -0800185 HwcConfigIndexType configId) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700186 RETURN_IF_INVALID_HANDLE(handle);
187 mConnections[handle].thread->onConfigChanged(displayId, configId);
Ady Abraham447052e2019-02-13 16:07:27 -0800188}
189
Dominik Laskowski98041832019-08-01 18:35:59 -0700190void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
191 RETURN_IF_INVALID_HANDLE(handle);
192 mConnections.at(handle).thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700193}
194
Dominik Laskowski98041832019-08-01 18:35:59 -0700195void Scheduler::setPhaseOffset(ConnectionHandle handle, nsecs_t phaseOffset) {
196 RETURN_IF_INVALID_HANDLE(handle);
197 mConnections[handle].thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700198}
Ana Krulece588e312018-09-18 12:32:24 -0700199
200void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
201 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
202 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
203}
204
Dominik Laskowski6505f792019-09-18 11:10:05 -0700205Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
206 if (mInjectVSyncs == enable) {
207 return {};
208 }
209
210 ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling");
211
212 if (!mInjectorConnectionHandle) {
213 auto vsyncSource = std::make_unique<InjectVSyncSource>();
214 mVSyncInjector = vsyncSource.get();
215
216 auto eventThread =
217 std::make_unique<impl::EventThread>(std::move(vsyncSource),
218 impl::EventThread::InterceptVSyncsCallback());
219
220 mInjectorConnectionHandle = createConnection(std::move(eventThread));
221 }
222
223 mInjectVSyncs = enable;
224 return mInjectorConnectionHandle;
225}
226
227bool Scheduler::injectVSync(nsecs_t when) {
228 if (!mInjectVSyncs || !mVSyncInjector) {
229 return false;
230 }
231
232 mVSyncInjector->onInjectSyncEvent(when);
233 return true;
234}
235
Ana Krulece588e312018-09-18 12:32:24 -0700236void Scheduler::enableHardwareVsync() {
237 std::lock_guard<std::mutex> lock(mHWVsyncLock);
238 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
239 mPrimaryDispSync->beginResync();
240 mEventControlThread->setVsyncEnabled(true);
241 mPrimaryHWVsyncEnabled = true;
242 }
243}
244
245void Scheduler::disableHardwareVsync(bool makeUnavailable) {
246 std::lock_guard<std::mutex> lock(mHWVsyncLock);
247 if (mPrimaryHWVsyncEnabled) {
248 mEventControlThread->setVsyncEnabled(false);
249 mPrimaryDispSync->endResync();
250 mPrimaryHWVsyncEnabled = false;
251 }
252 if (makeUnavailable) {
253 mHWVsyncAvailable = false;
254 }
255}
256
Ana Krulecc2870422019-01-29 19:00:58 -0800257void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
258 {
259 std::lock_guard<std::mutex> lock(mHWVsyncLock);
260 if (makeAvailable) {
261 mHWVsyncAvailable = makeAvailable;
262 } else if (!mHWVsyncAvailable) {
263 // Hardware vsync is not currently available, so abort the resync
264 // attempt for now
265 return;
266 }
267 }
268
269 if (period <= 0) {
270 return;
271 }
272
273 setVsyncPeriod(period);
274}
275
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700276void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700277 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800278
279 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700280 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800281
282 if (now - last > kIgnoreDelay) {
Ady Abraham2139f732019-11-13 18:56:40 -0800283 resyncToHardwareVsync(false, mRefreshRateConfigs.getCurrentRefreshRate().vsyncPeriod);
Ana Krulecc2870422019-01-29 19:00:58 -0800284 }
285}
286
Dominik Laskowski98041832019-08-01 18:35:59 -0700287void Scheduler::setVsyncPeriod(nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800288 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700289 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800290
291 if (!mPrimaryHWVsyncEnabled) {
292 mPrimaryDispSync->beginResync();
293 mEventControlThread->setVsyncEnabled(true);
294 mPrimaryHWVsyncEnabled = true;
295 }
Ana Krulece588e312018-09-18 12:32:24 -0700296}
297
Dominik Laskowski98041832019-08-01 18:35:59 -0700298void Scheduler::addResyncSample(nsecs_t timestamp, bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700299 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700300 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700301 { // Scope for the lock
302 std::lock_guard<std::mutex> lock(mHWVsyncLock);
303 if (mPrimaryHWVsyncEnabled) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700304 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700305 }
306 }
307
308 if (needsHwVsync) {
309 enableHardwareVsync();
310 } else {
311 disableHardwareVsync(false);
312 }
313}
314
315void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
316 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
317 enableHardwareVsync();
318 } else {
319 disableHardwareVsync(false);
320 }
321}
322
323void Scheduler::setIgnorePresentFences(bool ignore) {
324 mPrimaryDispSync->setIgnorePresentFences(ignore);
325}
326
Ady Abraham8fe11022019-06-12 17:11:12 -0700327nsecs_t Scheduler::getDispSyncExpectedPresentTime() {
Ady Abrahamc3e21312019-02-07 14:30:23 -0800328 return mPrimaryDispSync->expectedPresentTime();
329}
330
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700331void Scheduler::registerLayer(Layer* layer) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800332 if (!mLayerHistory) return;
333
Ady Abraham2139f732019-11-13 18:56:40 -0800334 const auto lowFps = mRefreshRateConfigs.getMinRefreshRate().fps;
335 const auto highFps = layer->getWindowType() == InputWindowInfo::TYPE_WALLPAPER
336 ? lowFps
337 : mRefreshRateConfigs.getMaxRefreshRate().fps;
Dominik Laskowski49cea512019-11-12 14:13:23 -0800338
339 mLayerHistory->registerLayer(layer, lowFps, highFps);
Ady Abraham09bd3922019-04-08 10:44:56 -0700340}
341
Ady Abraham2139f732019-11-13 18:56:40 -0800342void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800343 if (mLayerHistory) {
Ady Abraham2139f732019-11-13 18:56:40 -0800344 mLayerHistory->record(layer, presentTime, systemTime());
Dominik Laskowski49cea512019-11-12 14:13:23 -0800345 }
Ana Krulec3084c052018-11-21 20:27:17 +0100346}
347
Dominik Laskowski49cea512019-11-12 14:13:23 -0800348void Scheduler::chooseRefreshRateForContent() {
349 if (!mLayerHistory) return;
350
Ady Abraham2139f732019-11-13 18:56:40 -0800351 auto [refreshRate] = mLayerHistory->summarize(systemTime());
Ady Abrahama315ce72019-04-24 14:35:20 -0700352 const uint32_t refreshRateRound = std::round(refreshRate);
Ady Abraham2139f732019-11-13 18:56:40 -0800353 HwcConfigIndexType newConfigId;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700354 {
355 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800356 if (mFeatures.contentRefreshRate == refreshRateRound) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700357 return;
358 }
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700359 mFeatures.contentRefreshRate = refreshRateRound;
360 ATRACE_INT("ContentFPS", refreshRateRound);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700361
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700362 mFeatures.contentDetection =
363 refreshRateRound > 0 ? ContentDetectionState::On : ContentDetectionState::Off;
Ady Abraham2139f732019-11-13 18:56:40 -0800364 newConfigId = calculateRefreshRateType();
365 if (mFeatures.configId == newConfigId) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700366 return;
367 }
Ady Abraham2139f732019-11-13 18:56:40 -0800368 mFeatures.configId = newConfigId;
369 };
370 auto newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
371 changeRefreshRate(newRefreshRate, ConfigEvent::Changed);
Ana Krulec3084c052018-11-21 20:27:17 +0100372}
373
Ady Abraham2139f732019-11-13 18:56:40 -0800374void Scheduler::setSchedulerCallback(android::Scheduler::ISchedulerCallback* callback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800375 std::lock_guard<std::mutex> lock(mCallbackLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800376 mSchedulerCallback = callback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800377}
378
Ana Krulecfb772822018-11-30 10:44:07 +0100379void Scheduler::resetIdleTimer() {
380 if (mIdleTimer) {
381 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800382 }
383}
384
Ady Abraham8532d012019-05-08 14:50:56 -0700385void Scheduler::notifyTouchEvent() {
386 if (mTouchTimer) {
387 mTouchTimer->reset();
388 }
389
Dominik Laskowski98041832019-08-01 18:35:59 -0700390 if (mSupportKernelTimer && mIdleTimer) {
391 mIdleTimer->reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700392 }
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700393
394 // Touch event will boost the refresh rate to performance.
395 // Clear Layer History to get fresh FPS detection
Dominik Laskowski49cea512019-11-12 14:13:23 -0800396 if (mLayerHistory) {
397 mLayerHistory->clear();
398 }
Ady Abraham8532d012019-05-08 14:50:56 -0700399}
400
Ady Abraham6fe2c172019-07-12 12:37:57 -0700401void Scheduler::setDisplayPowerState(bool normal) {
402 {
403 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700404 mFeatures.isDisplayPowerStateNormal = normal;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700405 }
406
407 if (mDisplayPowerTimer) {
408 mDisplayPowerTimer->reset();
409 }
410
411 // Display Power event will boost the refresh rate to performance.
412 // Clear Layer History to get fresh FPS detection
Dominik Laskowski49cea512019-11-12 14:13:23 -0800413 if (mLayerHistory) {
414 mLayerHistory->clear();
415 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700416}
417
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700418void Scheduler::kernelIdleTimerCallback(TimerState state) {
419 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100420
Ady Abraham2139f732019-11-13 18:56:40 -0800421 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
422 // magic number
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700423 const auto refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
Ady Abraham2139f732019-11-13 18:56:40 -0800424 constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f;
425 if (state == TimerState::Reset && refreshRate.fps > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700426 // If we're not in performance mode then the kernel timer shouldn't do
427 // anything, as the refresh rate during DPU power collapse will be the
428 // same.
Ady Abraham2139f732019-11-13 18:56:40 -0800429 resyncToHardwareVsync(true /* makeAvailable */, refreshRate.vsyncPeriod);
430 } else if (state == TimerState::Expired && refreshRate.fps <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700431 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
432 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
433 // need to update the DispSync model anyway.
434 disableHardwareVsync(false /* makeUnavailable */);
Alec Mouridc28b372019-04-18 21:17:13 -0700435 }
436}
437
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700438void Scheduler::idleTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700439 handleTimerStateChanged(&mFeatures.idleTimer, state, false /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700440 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100441}
442
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700443void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700444 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
445 handleTimerStateChanged(&mFeatures.touch, touch, true /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700446 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700447}
448
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700449void Scheduler::displayPowerTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700450 handleTimerStateChanged(&mFeatures.displayPowerTimer, state,
451 true /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700452 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700453}
454
Dominik Laskowski98041832019-08-01 18:35:59 -0700455void Scheduler::dump(std::string& result) const {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800456 using base::StringAppendF;
457 const char* const states[] = {"off", "on"};
Dominik Laskowski98041832019-08-01 18:35:59 -0700458
Dominik Laskowski49cea512019-11-12 14:13:23 -0800459 const bool supported = mRefreshRateConfigs.refreshRateSwitchingSupported();
460 StringAppendF(&result, "+ Refresh rate switching: %s\n", states[supported]);
461 StringAppendF(&result, "+ Content detection: %s\n", states[mLayerHistory.has_value()]);
462
463 StringAppendF(&result, "+ Idle timer: %s\n",
464 mIdleTimer ? mIdleTimer->dump().c_str() : states[0]);
465 StringAppendF(&result, "+ Touch timer: %s\n\n",
466 mTouchTimer ? mTouchTimer->dump().c_str() : states[0]);
Ana Krulecb43429d2019-01-09 14:28:51 -0800467}
468
Ady Abraham6fe2c172019-07-12 12:37:57 -0700469template <class T>
470void Scheduler::handleTimerStateChanged(T* currentState, T newState, bool eventOnContentDetection) {
Ady Abraham8532d012019-05-08 14:50:56 -0700471 ConfigEvent event = ConfigEvent::None;
Ady Abraham2139f732019-11-13 18:56:40 -0800472 HwcConfigIndexType newConfigId;
Ady Abraham8532d012019-05-08 14:50:56 -0700473 {
474 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700475 if (*currentState == newState) {
Ady Abraham8532d012019-05-08 14:50:56 -0700476 return;
477 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700478 *currentState = newState;
Ady Abraham2139f732019-11-13 18:56:40 -0800479 newConfigId = calculateRefreshRateType();
480 if (mFeatures.configId == newConfigId) {
Ady Abraham8532d012019-05-08 14:50:56 -0700481 return;
482 }
Ady Abraham2139f732019-11-13 18:56:40 -0800483 mFeatures.configId = newConfigId;
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700484 if (eventOnContentDetection && mFeatures.contentDetection == ContentDetectionState::On) {
Ady Abraham8532d012019-05-08 14:50:56 -0700485 event = ConfigEvent::Changed;
486 }
487 }
Ady Abraham2139f732019-11-13 18:56:40 -0800488 const RefreshRate& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
489 changeRefreshRate(newRefreshRate, event);
Ady Abraham8532d012019-05-08 14:50:56 -0700490}
491
Ady Abraham2139f732019-11-13 18:56:40 -0800492HwcConfigIndexType Scheduler::calculateRefreshRateType() {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700493 if (!mRefreshRateConfigs.refreshRateSwitchingSupported()) {
Ady Abraham2139f732019-11-13 18:56:40 -0800494 return mRefreshRateConfigs.getCurrentRefreshRate().configId;
Ady Abraham09bd3922019-04-08 10:44:56 -0700495 }
496
Ady Abraham6fe2c172019-07-12 12:37:57 -0700497 // If Display Power is not in normal operation we want to be in performance mode.
498 // When coming back to normal mode, a grace period is given with DisplayPowerTimer
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700499 if (!mFeatures.isDisplayPowerStateNormal || mFeatures.displayPowerTimer == TimerState::Reset) {
Ady Abraham2139f732019-11-13 18:56:40 -0800500 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700501 }
502
Ady Abraham8532d012019-05-08 14:50:56 -0700503 // As long as touch is active we want to be in performance mode
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700504 if (mFeatures.touch == TouchState::Active) {
Ady Abraham2139f732019-11-13 18:56:40 -0800505 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
Ady Abraham8532d012019-05-08 14:50:56 -0700506 }
507
508 // If timer has expired as it means there is no new content on the screen
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700509 if (mFeatures.idleTimer == TimerState::Expired) {
Ady Abraham2139f732019-11-13 18:56:40 -0800510 return mRefreshRateConfigs.getMinRefreshRateByPolicy().configId;
Ady Abrahama315ce72019-04-24 14:35:20 -0700511 }
512
Ady Abraham09bd3922019-04-08 10:44:56 -0700513 // If content detection is off we choose performance as we don't know the content fps
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700514 if (mFeatures.contentDetection == ContentDetectionState::Off) {
Ady Abraham2139f732019-11-13 18:56:40 -0800515 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
Ady Abraham09bd3922019-04-08 10:44:56 -0700516 }
517
Wei Wang09be73f2019-07-02 14:29:18 -0700518 // Content detection is on, find the appropriate refresh rate with minimal error
Ady Abraham2139f732019-11-13 18:56:40 -0800519 return mRefreshRateConfigs
520 .getRefreshRateForContent(static_cast<float>(mFeatures.contentRefreshRate))
521 .configId;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800522}
523
Ady Abraham2139f732019-11-13 18:56:40 -0800524std::optional<HwcConfigIndexType> Scheduler::getPreferredConfigId() {
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700525 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800526 return mFeatures.configId;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700527}
528
Ady Abraham2139f732019-11-13 18:56:40 -0800529void Scheduler::changeRefreshRate(const RefreshRate& refreshRate, ConfigEvent configEvent) {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800530 std::lock_guard<std::mutex> lock(mCallbackLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800531 if (mSchedulerCallback) {
532 mSchedulerCallback->changeRefreshRate(refreshRate, configEvent);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800533 }
534}
535
Ana Krulec98b5b242018-08-10 15:03:23 -0700536} // namespace android