blob: 71b35001b6577f5bfb0017c884961ce51a0a2628 [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
Ana Krulece588e312018-09-18 12:32:24 -070023#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
24#include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
Ana Krulece588e312018-09-18 12:32:24 -070025#include <configstore/Utils.h>
Ana Krulecfb772822018-11-30 10:44:07 +010026#include <cutils/properties.h>
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070027#include <input/InputWindow.h>
Ana Krulecfefd6ae2019-02-13 17:53:08 -080028#include <system/window.h>
Ana Krulece588e312018-09-18 12:32:24 -070029#include <ui/DisplayStatInfo.h>
Ana Krulec3084c052018-11-21 20:27:17 +010030#include <utils/Timers.h>
Ana Krulec7ab56032018-11-02 20:51:06 +010031#include <utils/Trace.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070032
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070033#include <algorithm>
34#include <cinttypes>
35#include <cstdint>
36#include <functional>
37#include <memory>
38#include <numeric>
39
40#include "../Layer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070041#include "DispSync.h"
42#include "DispSyncSource.h"
Ana Krulece588e312018-09-18 12:32:24 -070043#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070044#include "EventThread.h"
Dominik Laskowski6505f792019-09-18 11:10:05 -070045#include "InjectVSyncSource.h"
Ana Krulecf2c006d2019-06-21 15:37:07 -070046#include "OneShotTimer.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010047#include "SchedulerUtils.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090048#include "SurfaceFlingerProperties.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070049
Dominik Laskowski98041832019-08-01 18:35:59 -070050#define RETURN_IF_INVALID_HANDLE(handle, ...) \
51 do { \
52 if (mConnections.count(handle) == 0) { \
53 ALOGE("Invalid connection handle %" PRIuPTR, handle.id); \
54 return __VA_ARGS__; \
55 } \
56 } while (false)
57
Ana Krulec98b5b242018-08-10 15:03:23 -070058namespace android {
59
Ady Abraham09bd3922019-04-08 10:44:56 -070060Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function,
61 const scheduler::RefreshRateConfigs& refreshRateConfig)
Dominik Laskowski98041832019-08-01 18:35:59 -070062 : mPrimaryDispSync(new impl::DispSync("SchedulerDispSync",
63 sysprop::running_without_sync_framework(true))),
64 mEventControlThread(new impl::EventControlThread(std::move(function))),
65 mSupportKernelTimer(sysprop::support_kernel_idle_timer(false)),
Ady Abraham09bd3922019-04-08 10:44:56 -070066 mRefreshRateConfigs(refreshRateConfig) {
Dominik Laskowski98041832019-08-01 18:35:59 -070067 using namespace sysprop;
Ady Abraham8532d012019-05-08 14:50:56 -070068
Ana Krulecfb772822018-11-30 10:44:07 +010069 char value[PROPERTY_VALUE_MAX];
Ana Kruleca5bdd9d2019-01-29 19:00:58 -080070 property_get("debug.sf.set_idle_timer_ms", value, "0");
Dominik Laskowski98041832019-08-01 18:35:59 -070071 const int setIdleTimerMs = atoi(value);
Ana Krulecfb772822018-11-30 10:44:07 +010072
Dominik Laskowski98041832019-08-01 18:35:59 -070073 if (const auto millis = setIdleTimerMs ? setIdleTimerMs : set_idle_timer_ms(0); millis > 0) {
74 const auto callback = mSupportKernelTimer ? &Scheduler::kernelIdleTimerCallback
75 : &Scheduler::idleTimerCallback;
76
77 mIdleTimer.emplace(
78 std::chrono::milliseconds(millis),
79 [this, callback] { std::invoke(callback, this, TimerState::Reset); },
80 [this, callback] { std::invoke(callback, this, TimerState::Expired); });
Ana Krulecfb772822018-11-30 10:44:07 +010081 mIdleTimer->start();
82 }
Ady Abraham8532d012019-05-08 14:50:56 -070083
Dominik Laskowski98041832019-08-01 18:35:59 -070084 if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -070085 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -070086 mTouchTimer.emplace(
87 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -070088 [this] { touchTimerCallback(TimerState::Reset); },
89 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -070090 mTouchTimer->start();
91 }
Ady Abraham6fe2c172019-07-12 12:37:57 -070092
Dominik Laskowski98041832019-08-01 18:35:59 -070093 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
94 mDisplayPowerTimer.emplace(
95 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -070096 [this] { displayPowerTimerCallback(TimerState::Reset); },
97 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -070098 mDisplayPowerTimer->start();
99 }
Ana Krulece588e312018-09-18 12:32:24 -0700100}
101
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700102Scheduler::Scheduler(std::unique_ptr<DispSync> primaryDispSync,
103 std::unique_ptr<EventControlThread> eventControlThread,
104 const scheduler::RefreshRateConfigs& configs)
Dominik Laskowski98041832019-08-01 18:35:59 -0700105 : mPrimaryDispSync(std::move(primaryDispSync)),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700106 mEventControlThread(std::move(eventControlThread)),
Dominik Laskowski98041832019-08-01 18:35:59 -0700107 mSupportKernelTimer(false),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700108 mRefreshRateConfigs(configs) {}
109
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800110Scheduler::~Scheduler() {
Ana Krulecf2c006d2019-06-21 15:37:07 -0700111 // Ensure the OneShotTimer threads are joined before we start destroying state.
Ady Abraham6fe2c172019-07-12 12:37:57 -0700112 mDisplayPowerTimer.reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700113 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800114 mIdleTimer.reset();
115}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700116
Dominik Laskowski98041832019-08-01 18:35:59 -0700117DispSync& Scheduler::getPrimaryDispSync() {
118 return *mPrimaryDispSync;
119}
120
Dominik Laskowski6505f792019-09-18 11:10:05 -0700121std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(
122 const char* name, nsecs_t phaseOffsetNs, nsecs_t offsetThresholdForNextVsync) {
123 return std::make_unique<DispSyncSource>(mPrimaryDispSync.get(), phaseOffsetNs,
124 offsetThresholdForNextVsync, true /* traceVsync */,
125 name);
126}
127
Dominik Laskowski98041832019-08-01 18:35:59 -0700128Scheduler::ConnectionHandle Scheduler::createConnection(
Ady Abraham45e4e362019-06-07 18:20:51 -0700129 const char* connectionName, nsecs_t phaseOffsetNs, nsecs_t offsetThresholdForNextVsync,
Ana Krulec98b5b242018-08-10 15:03:23 -0700130 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700131 auto vsyncSource =
132 makePrimaryDispSyncSource(connectionName, phaseOffsetNs, offsetThresholdForNextVsync);
133 auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource),
134 std::move(interceptCallback));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700135 return createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700136}
Ana Krulec98b5b242018-08-10 15:03:23 -0700137
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700138Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700139 const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
140 ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800141
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700142 auto connection =
143 createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowski98041832019-08-01 18:35:59 -0700144
145 mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
146 return handle;
Ana Krulec98b5b242018-08-10 15:03:23 -0700147}
148
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700149sp<EventThreadConnection> Scheduler::createConnectionInternal(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700150 EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) {
151 return eventThread->createEventConnection([&] { resync(); }, configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700152}
153
Ana Krulec98b5b242018-08-10 15:03:23 -0700154sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700155 ConnectionHandle handle, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700156 RETURN_IF_INVALID_HANDLE(handle, nullptr);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700157 return createConnectionInternal(mConnections[handle].thread.get(), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700158}
159
Dominik Laskowski98041832019-08-01 18:35:59 -0700160sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
161 RETURN_IF_INVALID_HANDLE(handle, nullptr);
162 return mConnections[handle].connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700163}
164
Dominik Laskowski98041832019-08-01 18:35:59 -0700165void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
166 bool connected) {
167 RETURN_IF_INVALID_HANDLE(handle);
168 mConnections[handle].thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700169}
170
Dominik Laskowski98041832019-08-01 18:35:59 -0700171void Scheduler::onScreenAcquired(ConnectionHandle handle) {
172 RETURN_IF_INVALID_HANDLE(handle);
173 mConnections[handle].thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700174}
175
Dominik Laskowski98041832019-08-01 18:35:59 -0700176void Scheduler::onScreenReleased(ConnectionHandle handle) {
177 RETURN_IF_INVALID_HANDLE(handle);
178 mConnections[handle].thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700179}
180
Dominik Laskowski98041832019-08-01 18:35:59 -0700181void Scheduler::onConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId,
Ady Abraham447052e2019-02-13 16:07:27 -0800182 int32_t configId) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700183 RETURN_IF_INVALID_HANDLE(handle);
184 mConnections[handle].thread->onConfigChanged(displayId, configId);
Ady Abraham447052e2019-02-13 16:07:27 -0800185}
186
Dominik Laskowski98041832019-08-01 18:35:59 -0700187void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
188 RETURN_IF_INVALID_HANDLE(handle);
189 mConnections.at(handle).thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700190}
191
Dominik Laskowski98041832019-08-01 18:35:59 -0700192void Scheduler::setPhaseOffset(ConnectionHandle handle, nsecs_t phaseOffset) {
193 RETURN_IF_INVALID_HANDLE(handle);
194 mConnections[handle].thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700195}
Ana Krulece588e312018-09-18 12:32:24 -0700196
197void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
198 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
199 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
200}
201
Dominik Laskowski6505f792019-09-18 11:10:05 -0700202Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
203 if (mInjectVSyncs == enable) {
204 return {};
205 }
206
207 ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling");
208
209 if (!mInjectorConnectionHandle) {
210 auto vsyncSource = std::make_unique<InjectVSyncSource>();
211 mVSyncInjector = vsyncSource.get();
212
213 auto eventThread =
214 std::make_unique<impl::EventThread>(std::move(vsyncSource),
215 impl::EventThread::InterceptVSyncsCallback());
216
217 mInjectorConnectionHandle = createConnection(std::move(eventThread));
218 }
219
220 mInjectVSyncs = enable;
221 return mInjectorConnectionHandle;
222}
223
224bool Scheduler::injectVSync(nsecs_t when) {
225 if (!mInjectVSyncs || !mVSyncInjector) {
226 return false;
227 }
228
229 mVSyncInjector->onInjectSyncEvent(when);
230 return true;
231}
232
Ana Krulece588e312018-09-18 12:32:24 -0700233void Scheduler::enableHardwareVsync() {
234 std::lock_guard<std::mutex> lock(mHWVsyncLock);
235 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
236 mPrimaryDispSync->beginResync();
237 mEventControlThread->setVsyncEnabled(true);
238 mPrimaryHWVsyncEnabled = true;
239 }
240}
241
242void Scheduler::disableHardwareVsync(bool makeUnavailable) {
243 std::lock_guard<std::mutex> lock(mHWVsyncLock);
244 if (mPrimaryHWVsyncEnabled) {
245 mEventControlThread->setVsyncEnabled(false);
246 mPrimaryDispSync->endResync();
247 mPrimaryHWVsyncEnabled = false;
248 }
249 if (makeUnavailable) {
250 mHWVsyncAvailable = false;
251 }
252}
253
Ana Krulecc2870422019-01-29 19:00:58 -0800254void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
255 {
256 std::lock_guard<std::mutex> lock(mHWVsyncLock);
257 if (makeAvailable) {
258 mHWVsyncAvailable = makeAvailable;
259 } else if (!mHWVsyncAvailable) {
260 // Hardware vsync is not currently available, so abort the resync
261 // attempt for now
262 return;
263 }
264 }
265
266 if (period <= 0) {
267 return;
268 }
269
270 setVsyncPeriod(period);
271}
272
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700273void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700274 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800275
276 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700277 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800278
279 if (now - last > kIgnoreDelay) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700280 resyncToHardwareVsync(false,
281 mRefreshRateConfigs.getCurrentRefreshRate().second.vsyncPeriod);
Ana Krulecc2870422019-01-29 19:00:58 -0800282 }
283}
284
Dominik Laskowski98041832019-08-01 18:35:59 -0700285void Scheduler::setVsyncPeriod(nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800286 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700287 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800288
289 if (!mPrimaryHWVsyncEnabled) {
290 mPrimaryDispSync->beginResync();
291 mEventControlThread->setVsyncEnabled(true);
292 mPrimaryHWVsyncEnabled = true;
293 }
Ana Krulece588e312018-09-18 12:32:24 -0700294}
295
Dominik Laskowski98041832019-08-01 18:35:59 -0700296void Scheduler::addResyncSample(nsecs_t timestamp, bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700297 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700298 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700299 { // Scope for the lock
300 std::lock_guard<std::mutex> lock(mHWVsyncLock);
301 if (mPrimaryHWVsyncEnabled) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700302 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700303 }
304 }
305
306 if (needsHwVsync) {
307 enableHardwareVsync();
308 } else {
309 disableHardwareVsync(false);
310 }
311}
312
313void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
314 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
315 enableHardwareVsync();
316 } else {
317 disableHardwareVsync(false);
318 }
319}
320
321void Scheduler::setIgnorePresentFences(bool ignore) {
322 mPrimaryDispSync->setIgnorePresentFences(ignore);
323}
324
Ady Abraham8fe11022019-06-12 17:11:12 -0700325nsecs_t Scheduler::getDispSyncExpectedPresentTime() {
Ady Abrahamc3e21312019-02-07 14:30:23 -0800326 return mPrimaryDispSync->expectedPresentTime();
327}
328
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700329void Scheduler::registerLayer(Layer* layer) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700330 uint32_t defaultFps, performanceFps;
331 if (mRefreshRateConfigs.refreshRateSwitchingSupported()) {
332 defaultFps = mRefreshRateConfigs.getRefreshRateFromType(RefreshRateType::DEFAULT).fps;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700333 const auto type = layer->getWindowType() == InputWindowInfo::TYPE_WALLPAPER
334 ? RefreshRateType::DEFAULT
335 : RefreshRateType::PERFORMANCE;
336 performanceFps = mRefreshRateConfigs.getRefreshRateFromType(type).fps;
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700337 } else {
338 defaultFps = mRefreshRateConfigs.getCurrentRefreshRate().second.fps;
339 performanceFps = defaultFps;
340 }
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700341 mLayerHistory.registerLayer(layer, defaultFps, performanceFps);
Ady Abraham09bd3922019-04-08 10:44:56 -0700342}
343
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700344void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime, bool isHDR) {
345 mLayerHistory.record(layer, presentTime, isHDR, systemTime());
Ana Krulec3084c052018-11-21 20:27:17 +0100346}
347
Ady Abraham09bd3922019-04-08 10:44:56 -0700348void Scheduler::updateFpsBasedOnContent() {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700349 auto [refreshRate, isHDR] = mLayerHistory.summarize(systemTime());
Ady Abrahama315ce72019-04-24 14:35:20 -0700350 const uint32_t refreshRateRound = std::round(refreshRate);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700351 RefreshRateType newRefreshRateType;
352 {
353 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700354 if (mFeatures.contentRefreshRate == refreshRateRound && mFeatures.isHDRContent == isHDR) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700355 return;
356 }
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700357 mFeatures.contentRefreshRate = refreshRateRound;
358 ATRACE_INT("ContentFPS", refreshRateRound);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700359
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700360 mFeatures.isHDRContent = isHDR;
361 ATRACE_INT("ContentHDR", isHDR);
Ady Abrahama315ce72019-04-24 14:35:20 -0700362
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700363 mFeatures.contentDetection =
364 refreshRateRound > 0 ? ContentDetectionState::On : ContentDetectionState::Off;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700365 newRefreshRateType = calculateRefreshRateType();
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700366 if (mFeatures.refreshRateType == newRefreshRateType) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700367 return;
368 }
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700369 mFeatures.refreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800370 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700371 changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
Ana Krulec3084c052018-11-21 20:27:17 +0100372}
373
Dominik Laskowski98041832019-08-01 18:35:59 -0700374void Scheduler::setChangeRefreshRateCallback(ChangeRefreshRateCallback&& callback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800375 std::lock_guard<std::mutex> lock(mCallbackLock);
Dominik Laskowski98041832019-08-01 18:35:59 -0700376 mChangeRefreshRateCallback = std::move(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 Laskowskif7a09ed2019-10-07 13:54:18 -0700396 mLayerHistory.clear();
Ady Abraham8532d012019-05-08 14:50:56 -0700397}
398
Ady Abraham6fe2c172019-07-12 12:37:57 -0700399void Scheduler::setDisplayPowerState(bool normal) {
400 {
401 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700402 mFeatures.isDisplayPowerStateNormal = normal;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700403 }
404
405 if (mDisplayPowerTimer) {
406 mDisplayPowerTimer->reset();
407 }
408
409 // Display Power event will boost the refresh rate to performance.
410 // Clear Layer History to get fresh FPS detection
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700411 mLayerHistory.clear();
Ady Abraham6fe2c172019-07-12 12:37:57 -0700412}
413
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700414void Scheduler::kernelIdleTimerCallback(TimerState state) {
415 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100416
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700417 const auto refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
418 if (state == TimerState::Reset && refreshRate.first == RefreshRateType::PERFORMANCE) {
Alec Mouri7f015182019-07-11 13:56:22 -0700419 // If we're not in performance mode then the kernel timer shouldn't do
420 // anything, as the refresh rate during DPU power collapse will be the
421 // same.
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700422 resyncToHardwareVsync(true /* makeAvailable */, refreshRate.second.vsyncPeriod);
423 } else if (state == TimerState::Expired && refreshRate.first != RefreshRateType::PERFORMANCE) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700424 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
425 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
426 // need to update the DispSync model anyway.
427 disableHardwareVsync(false /* makeUnavailable */);
Alec Mouridc28b372019-04-18 21:17:13 -0700428 }
429}
430
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700431void Scheduler::idleTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700432 handleTimerStateChanged(&mFeatures.idleTimer, state, false /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700433 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100434}
435
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700436void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700437 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
438 handleTimerStateChanged(&mFeatures.touch, touch, true /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700439 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700440}
441
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700442void Scheduler::displayPowerTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700443 handleTimerStateChanged(&mFeatures.displayPowerTimer, state,
444 true /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700445 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700446}
447
Dominik Laskowski98041832019-08-01 18:35:59 -0700448void Scheduler::dump(std::string& result) const {
Ana Krulecb43429d2019-01-09 14:28:51 -0800449 std::ostringstream stream;
Dominik Laskowski98041832019-08-01 18:35:59 -0700450 if (mIdleTimer) {
451 stream << "+ Idle timer interval: " << mIdleTimer->interval().count() << " ms\n";
452 }
453 if (mTouchTimer) {
454 stream << "+ Touch timer interval: " << mTouchTimer->interval().count() << " ms\n";
455 }
456
457 result.append(stream.str());
Ana Krulecb43429d2019-01-09 14:28:51 -0800458}
459
Ady Abraham6fe2c172019-07-12 12:37:57 -0700460template <class T>
461void Scheduler::handleTimerStateChanged(T* currentState, T newState, bool eventOnContentDetection) {
Ady Abraham8532d012019-05-08 14:50:56 -0700462 ConfigEvent event = ConfigEvent::None;
463 RefreshRateType newRefreshRateType;
464 {
465 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700466 if (*currentState == newState) {
Ady Abraham8532d012019-05-08 14:50:56 -0700467 return;
468 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700469 *currentState = newState;
Ady Abraham8532d012019-05-08 14:50:56 -0700470 newRefreshRateType = calculateRefreshRateType();
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700471 if (mFeatures.refreshRateType == newRefreshRateType) {
Ady Abraham8532d012019-05-08 14:50:56 -0700472 return;
473 }
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700474 mFeatures.refreshRateType = newRefreshRateType;
475 if (eventOnContentDetection && mFeatures.contentDetection == ContentDetectionState::On) {
Ady Abraham8532d012019-05-08 14:50:56 -0700476 event = ConfigEvent::Changed;
477 }
478 }
479 changeRefreshRate(newRefreshRateType, event);
480}
481
Ady Abraham09bd3922019-04-08 10:44:56 -0700482Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700483 if (!mRefreshRateConfigs.refreshRateSwitchingSupported()) {
484 return RefreshRateType::DEFAULT;
485 }
486
Ady Abraham8532d012019-05-08 14:50:56 -0700487 // HDR content is not supported on PERFORMANCE mode
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700488 if (mForceHDRContentToDefaultRefreshRate && mFeatures.isHDRContent) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700489 return RefreshRateType::DEFAULT;
490 }
491
Ady Abraham6fe2c172019-07-12 12:37:57 -0700492 // If Display Power is not in normal operation we want to be in performance mode.
493 // When coming back to normal mode, a grace period is given with DisplayPowerTimer
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700494 if (!mFeatures.isDisplayPowerStateNormal || mFeatures.displayPowerTimer == TimerState::Reset) {
Ady Abraham6fe2c172019-07-12 12:37:57 -0700495 return RefreshRateType::PERFORMANCE;
496 }
497
Ady Abraham8532d012019-05-08 14:50:56 -0700498 // As long as touch is active we want to be in performance mode
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700499 if (mFeatures.touch == TouchState::Active) {
Ady Abraham8532d012019-05-08 14:50:56 -0700500 return RefreshRateType::PERFORMANCE;
501 }
502
503 // If timer has expired as it means there is no new content on the screen
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700504 if (mFeatures.idleTimer == TimerState::Expired) {
Ady Abrahama315ce72019-04-24 14:35:20 -0700505 return RefreshRateType::DEFAULT;
506 }
507
Ady Abraham09bd3922019-04-08 10:44:56 -0700508 // If content detection is off we choose performance as we don't know the content fps
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700509 if (mFeatures.contentDetection == ContentDetectionState::Off) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700510 return RefreshRateType::PERFORMANCE;
511 }
512
Wei Wang09be73f2019-07-02 14:29:18 -0700513 // Content detection is on, find the appropriate refresh rate with minimal error
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700514 // TODO(b/139751853): Scan allowed refresh rates only (SurfaceFlinger::mAllowedDisplayConfigs)
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700515 const float rate = static_cast<float>(mFeatures.contentRefreshRate);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700516 auto iter = min_element(mRefreshRateConfigs.getRefreshRateMap().cbegin(),
517 mRefreshRateConfigs.getRefreshRateMap().cend(),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700518 [rate](const auto& lhs, const auto& rhs) -> bool {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700519 return std::abs(lhs.second.fps - rate) <
520 std::abs(rhs.second.fps - rate);
Wei Wang09be73f2019-07-02 14:29:18 -0700521 });
522 RefreshRateType currRefreshRateType = iter->first;
Ady Abraham09bd3922019-04-08 10:44:56 -0700523
Ady Abraham85b3f012019-04-08 11:04:14 -0700524 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
525 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
526 // align well with both
Wei Wang09be73f2019-07-02 14:29:18 -0700527 constexpr float MARGIN = 0.05f;
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700528 float ratio = mRefreshRateConfigs.getRefreshRateFromType(currRefreshRateType).fps / rate;
Ady Abraham85b3f012019-04-08 11:04:14 -0700529 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700530 while (iter != mRefreshRateConfigs.getRefreshRateMap().cend()) {
531 ratio = iter->second.fps / rate;
Ady Abraham09bd3922019-04-08 10:44:56 -0700532
Ady Abraham85b3f012019-04-08 11:04:14 -0700533 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
534 currRefreshRateType = iter->first;
535 break;
536 }
537 ++iter;
538 }
539 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700540
541 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800542}
543
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700544Scheduler::RefreshRateType Scheduler::getPreferredRefreshRateType() {
545 std::lock_guard<std::mutex> lock(mFeatureStateLock);
546 return mFeatures.refreshRateType;
547}
548
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800549void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
550 std::lock_guard<std::mutex> lock(mCallbackLock);
551 if (mChangeRefreshRateCallback) {
552 mChangeRefreshRateCallback(refreshRateType, configEvent);
553 }
554}
555
Ana Krulec98b5b242018-08-10 15:03:23 -0700556} // namespace android