blob: 71ebfc39e06ee4a33eb5da91e865e381543b52fa [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,
Ady Abraham3a77a7b2019-12-02 18:46:59 -080062 const scheduler::RefreshRateConfigs& refreshRateConfig,
63 ISchedulerCallback& schedulerCallback)
Dominik Laskowski98041832019-08-01 18:35:59 -070064 : mPrimaryDispSync(new impl::DispSync("SchedulerDispSync",
65 sysprop::running_without_sync_framework(true))),
66 mEventControlThread(new impl::EventControlThread(std::move(function))),
67 mSupportKernelTimer(sysprop::support_kernel_idle_timer(false)),
Ady Abraham3a77a7b2019-12-02 18:46:59 -080068 mSchedulerCallback(schedulerCallback),
Ady Abraham09bd3922019-04-08 10:44:56 -070069 mRefreshRateConfigs(refreshRateConfig) {
Dominik Laskowski98041832019-08-01 18:35:59 -070070 using namespace sysprop;
Ady Abraham8532d012019-05-08 14:50:56 -070071
Dominik Laskowski49cea512019-11-12 14:13:23 -080072 if (property_get_bool("debug.sf.use_smart_90_for_video", 0) || use_smart_90_for_video(false)) {
73 mLayerHistory.emplace();
74 }
75
76 const int setIdleTimerMs = property_get_int32("debug.sf.set_idle_timer_ms", 0);
Ana Krulecfb772822018-11-30 10:44:07 +010077
Dominik Laskowski98041832019-08-01 18:35:59 -070078 if (const auto millis = setIdleTimerMs ? setIdleTimerMs : set_idle_timer_ms(0); millis > 0) {
79 const auto callback = mSupportKernelTimer ? &Scheduler::kernelIdleTimerCallback
80 : &Scheduler::idleTimerCallback;
81
82 mIdleTimer.emplace(
83 std::chrono::milliseconds(millis),
84 [this, callback] { std::invoke(callback, this, TimerState::Reset); },
85 [this, callback] { std::invoke(callback, this, TimerState::Expired); });
Ana Krulecfb772822018-11-30 10:44:07 +010086 mIdleTimer->start();
87 }
Ady Abraham8532d012019-05-08 14:50:56 -070088
Dominik Laskowski98041832019-08-01 18:35:59 -070089 if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
Ady Abraham8532d012019-05-08 14:50:56 -070090 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Dominik Laskowski98041832019-08-01 18:35:59 -070091 mTouchTimer.emplace(
92 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -070093 [this] { touchTimerCallback(TimerState::Reset); },
94 [this] { touchTimerCallback(TimerState::Expired); });
Ady Abraham8532d012019-05-08 14:50:56 -070095 mTouchTimer->start();
96 }
Ady Abraham6fe2c172019-07-12 12:37:57 -070097
Dominik Laskowski98041832019-08-01 18:35:59 -070098 if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
99 mDisplayPowerTimer.emplace(
100 std::chrono::milliseconds(millis),
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700101 [this] { displayPowerTimerCallback(TimerState::Reset); },
102 [this] { displayPowerTimerCallback(TimerState::Expired); });
Ady Abraham6fe2c172019-07-12 12:37:57 -0700103 mDisplayPowerTimer->start();
104 }
Ana Krulece588e312018-09-18 12:32:24 -0700105}
106
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700107Scheduler::Scheduler(std::unique_ptr<DispSync> primaryDispSync,
108 std::unique_ptr<EventControlThread> eventControlThread,
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800109 const scheduler::RefreshRateConfigs& configs,
110 ISchedulerCallback& schedulerCallback)
Dominik Laskowski98041832019-08-01 18:35:59 -0700111 : mPrimaryDispSync(std::move(primaryDispSync)),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700112 mEventControlThread(std::move(eventControlThread)),
Dominik Laskowski98041832019-08-01 18:35:59 -0700113 mSupportKernelTimer(false),
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800114 mSchedulerCallback(schedulerCallback),
Dominik Laskowski7c9dbf92019-08-01 17:57:31 -0700115 mRefreshRateConfigs(configs) {}
116
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800117Scheduler::~Scheduler() {
Ana Krulecf2c006d2019-06-21 15:37:07 -0700118 // Ensure the OneShotTimer threads are joined before we start destroying state.
Ady Abraham6fe2c172019-07-12 12:37:57 -0700119 mDisplayPowerTimer.reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700120 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800121 mIdleTimer.reset();
122}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700123
Dominik Laskowski98041832019-08-01 18:35:59 -0700124DispSync& Scheduler::getPrimaryDispSync() {
125 return *mPrimaryDispSync;
126}
127
Ady Abraham9e16a482019-12-03 17:19:41 -0800128std::unique_ptr<VSyncSource> Scheduler::makePrimaryDispSyncSource(const char* name,
129 nsecs_t phaseOffsetNs) {
Dominik Laskowski6505f792019-09-18 11:10:05 -0700130 return std::make_unique<DispSyncSource>(mPrimaryDispSync.get(), phaseOffsetNs,
Ady Abraham9e16a482019-12-03 17:19:41 -0800131 true /* traceVsync */, name);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700132}
133
Dominik Laskowski98041832019-08-01 18:35:59 -0700134Scheduler::ConnectionHandle Scheduler::createConnection(
Ady Abraham9e16a482019-12-03 17:19:41 -0800135 const char* connectionName, nsecs_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -0700136 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ady Abraham9e16a482019-12-03 17:19:41 -0800137 auto vsyncSource = makePrimaryDispSyncSource(connectionName, phaseOffsetNs);
Dominik Laskowski6505f792019-09-18 11:10:05 -0700138 auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource),
139 std::move(interceptCallback));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700140 return createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700141}
Ana Krulec98b5b242018-08-10 15:03:23 -0700142
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700143Scheduler::ConnectionHandle Scheduler::createConnection(std::unique_ptr<EventThread> eventThread) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700144 const ConnectionHandle handle = ConnectionHandle{mNextConnectionHandleId++};
145 ALOGV("Creating a connection handle with ID %" PRIuPTR, handle.id);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800146
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700147 auto connection =
148 createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowski98041832019-08-01 18:35:59 -0700149
150 mConnections.emplace(handle, Connection{connection, std::move(eventThread)});
151 return handle;
Ana Krulec98b5b242018-08-10 15:03:23 -0700152}
153
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700154sp<EventThreadConnection> Scheduler::createConnectionInternal(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700155 EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) {
156 return eventThread->createEventConnection([&] { resync(); }, configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700157}
158
Ana Krulec98b5b242018-08-10 15:03:23 -0700159sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700160 ConnectionHandle handle, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700161 RETURN_IF_INVALID_HANDLE(handle, nullptr);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700162 return createConnectionInternal(mConnections[handle].thread.get(), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700163}
164
Dominik Laskowski98041832019-08-01 18:35:59 -0700165sp<EventThreadConnection> Scheduler::getEventConnection(ConnectionHandle handle) {
166 RETURN_IF_INVALID_HANDLE(handle, nullptr);
167 return mConnections[handle].connection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700168}
169
Dominik Laskowski98041832019-08-01 18:35:59 -0700170void Scheduler::onHotplugReceived(ConnectionHandle handle, PhysicalDisplayId displayId,
171 bool connected) {
172 RETURN_IF_INVALID_HANDLE(handle);
173 mConnections[handle].thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700174}
175
Dominik Laskowski98041832019-08-01 18:35:59 -0700176void Scheduler::onScreenAcquired(ConnectionHandle handle) {
177 RETURN_IF_INVALID_HANDLE(handle);
178 mConnections[handle].thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700179}
180
Dominik Laskowski98041832019-08-01 18:35:59 -0700181void Scheduler::onScreenReleased(ConnectionHandle handle) {
182 RETURN_IF_INVALID_HANDLE(handle);
183 mConnections[handle].thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700184}
185
Dominik Laskowski98041832019-08-01 18:35:59 -0700186void Scheduler::onConfigChanged(ConnectionHandle handle, PhysicalDisplayId displayId,
Ady Abraham2139f732019-11-13 18:56:40 -0800187 HwcConfigIndexType configId) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700188 RETURN_IF_INVALID_HANDLE(handle);
189 mConnections[handle].thread->onConfigChanged(displayId, configId);
Ady Abraham447052e2019-02-13 16:07:27 -0800190}
191
Dominik Laskowski98041832019-08-01 18:35:59 -0700192void Scheduler::dump(ConnectionHandle handle, std::string& result) const {
193 RETURN_IF_INVALID_HANDLE(handle);
194 mConnections.at(handle).thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700195}
196
Dominik Laskowski98041832019-08-01 18:35:59 -0700197void Scheduler::setPhaseOffset(ConnectionHandle handle, nsecs_t phaseOffset) {
198 RETURN_IF_INVALID_HANDLE(handle);
199 mConnections[handle].thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700200}
Ana Krulece588e312018-09-18 12:32:24 -0700201
202void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
203 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
204 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
205}
206
Dominik Laskowski6505f792019-09-18 11:10:05 -0700207Scheduler::ConnectionHandle Scheduler::enableVSyncInjection(bool enable) {
208 if (mInjectVSyncs == enable) {
209 return {};
210 }
211
212 ALOGV("%s VSYNC injection", enable ? "Enabling" : "Disabling");
213
214 if (!mInjectorConnectionHandle) {
215 auto vsyncSource = std::make_unique<InjectVSyncSource>();
216 mVSyncInjector = vsyncSource.get();
217
218 auto eventThread =
219 std::make_unique<impl::EventThread>(std::move(vsyncSource),
220 impl::EventThread::InterceptVSyncsCallback());
221
222 mInjectorConnectionHandle = createConnection(std::move(eventThread));
223 }
224
225 mInjectVSyncs = enable;
226 return mInjectorConnectionHandle;
227}
228
229bool Scheduler::injectVSync(nsecs_t when) {
230 if (!mInjectVSyncs || !mVSyncInjector) {
231 return false;
232 }
233
234 mVSyncInjector->onInjectSyncEvent(when);
235 return true;
236}
237
Ana Krulece588e312018-09-18 12:32:24 -0700238void Scheduler::enableHardwareVsync() {
239 std::lock_guard<std::mutex> lock(mHWVsyncLock);
240 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
241 mPrimaryDispSync->beginResync();
242 mEventControlThread->setVsyncEnabled(true);
243 mPrimaryHWVsyncEnabled = true;
244 }
245}
246
247void Scheduler::disableHardwareVsync(bool makeUnavailable) {
248 std::lock_guard<std::mutex> lock(mHWVsyncLock);
249 if (mPrimaryHWVsyncEnabled) {
250 mEventControlThread->setVsyncEnabled(false);
251 mPrimaryDispSync->endResync();
252 mPrimaryHWVsyncEnabled = false;
253 }
254 if (makeUnavailable) {
255 mHWVsyncAvailable = false;
256 }
257}
258
Ana Krulecc2870422019-01-29 19:00:58 -0800259void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
260 {
261 std::lock_guard<std::mutex> lock(mHWVsyncLock);
262 if (makeAvailable) {
263 mHWVsyncAvailable = makeAvailable;
264 } else if (!mHWVsyncAvailable) {
265 // Hardware vsync is not currently available, so abort the resync
266 // attempt for now
267 return;
268 }
269 }
270
271 if (period <= 0) {
272 return;
273 }
274
275 setVsyncPeriod(period);
276}
277
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700278void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700279 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800280
281 const nsecs_t now = systemTime();
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700282 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800283
284 if (now - last > kIgnoreDelay) {
Ady Abraham2139f732019-11-13 18:56:40 -0800285 resyncToHardwareVsync(false, mRefreshRateConfigs.getCurrentRefreshRate().vsyncPeriod);
Ana Krulecc2870422019-01-29 19:00:58 -0800286 }
287}
288
Dominik Laskowski98041832019-08-01 18:35:59 -0700289void Scheduler::setVsyncPeriod(nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800290 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700291 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800292
293 if (!mPrimaryHWVsyncEnabled) {
294 mPrimaryDispSync->beginResync();
295 mEventControlThread->setVsyncEnabled(true);
296 mPrimaryHWVsyncEnabled = true;
297 }
Ana Krulece588e312018-09-18 12:32:24 -0700298}
299
Dominik Laskowski98041832019-08-01 18:35:59 -0700300void Scheduler::addResyncSample(nsecs_t timestamp, bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700301 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700302 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700303 { // Scope for the lock
304 std::lock_guard<std::mutex> lock(mHWVsyncLock);
305 if (mPrimaryHWVsyncEnabled) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700306 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700307 }
308 }
309
310 if (needsHwVsync) {
311 enableHardwareVsync();
312 } else {
313 disableHardwareVsync(false);
314 }
315}
316
317void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
318 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
319 enableHardwareVsync();
320 } else {
321 disableHardwareVsync(false);
322 }
323}
324
325void Scheduler::setIgnorePresentFences(bool ignore) {
326 mPrimaryDispSync->setIgnorePresentFences(ignore);
327}
328
Ady Abraham8fe11022019-06-12 17:11:12 -0700329nsecs_t Scheduler::getDispSyncExpectedPresentTime() {
Ady Abrahamc3e21312019-02-07 14:30:23 -0800330 return mPrimaryDispSync->expectedPresentTime();
331}
332
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700333void Scheduler::registerLayer(Layer* layer) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800334 if (!mLayerHistory) return;
335
Ady Abraham2139f732019-11-13 18:56:40 -0800336 const auto lowFps = mRefreshRateConfigs.getMinRefreshRate().fps;
337 const auto highFps = layer->getWindowType() == InputWindowInfo::TYPE_WALLPAPER
338 ? lowFps
339 : mRefreshRateConfigs.getMaxRefreshRate().fps;
Dominik Laskowski49cea512019-11-12 14:13:23 -0800340
341 mLayerHistory->registerLayer(layer, lowFps, highFps);
Ady Abraham09bd3922019-04-08 10:44:56 -0700342}
343
Ady Abraham2139f732019-11-13 18:56:40 -0800344void Scheduler::recordLayerHistory(Layer* layer, nsecs_t presentTime) {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800345 if (mLayerHistory) {
Ady Abraham2139f732019-11-13 18:56:40 -0800346 mLayerHistory->record(layer, presentTime, systemTime());
Dominik Laskowski49cea512019-11-12 14:13:23 -0800347 }
Ana Krulec3084c052018-11-21 20:27:17 +0100348}
349
Dominik Laskowski49cea512019-11-12 14:13:23 -0800350void Scheduler::chooseRefreshRateForContent() {
351 if (!mLayerHistory) return;
352
Ady Abraham2139f732019-11-13 18:56:40 -0800353 auto [refreshRate] = mLayerHistory->summarize(systemTime());
Ady Abrahama315ce72019-04-24 14:35:20 -0700354 const uint32_t refreshRateRound = std::round(refreshRate);
Ady Abraham2139f732019-11-13 18:56:40 -0800355 HwcConfigIndexType newConfigId;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700356 {
357 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800358 if (mFeatures.contentRefreshRate == refreshRateRound) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700359 return;
360 }
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700361 mFeatures.contentRefreshRate = refreshRateRound;
362 ATRACE_INT("ContentFPS", refreshRateRound);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700363
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700364 mFeatures.contentDetection =
365 refreshRateRound > 0 ? ContentDetectionState::On : ContentDetectionState::Off;
Ady Abraham2139f732019-11-13 18:56:40 -0800366 newConfigId = calculateRefreshRateType();
367 if (mFeatures.configId == newConfigId) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700368 return;
369 }
Ady Abraham2139f732019-11-13 18:56:40 -0800370 mFeatures.configId = newConfigId;
371 };
372 auto newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800373 mSchedulerCallback.changeRefreshRate(newRefreshRate, ConfigEvent::Changed);
Ady Abrahama1a49af2019-02-07 14:36:55 -0800374}
375
Ana Krulecfb772822018-11-30 10:44:07 +0100376void Scheduler::resetIdleTimer() {
377 if (mIdleTimer) {
378 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800379 }
380}
381
Ady Abraham8532d012019-05-08 14:50:56 -0700382void Scheduler::notifyTouchEvent() {
383 if (mTouchTimer) {
384 mTouchTimer->reset();
385 }
386
Dominik Laskowski98041832019-08-01 18:35:59 -0700387 if (mSupportKernelTimer && mIdleTimer) {
388 mIdleTimer->reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700389 }
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700390
391 // Touch event will boost the refresh rate to performance.
392 // Clear Layer History to get fresh FPS detection
Dominik Laskowski49cea512019-11-12 14:13:23 -0800393 if (mLayerHistory) {
394 mLayerHistory->clear();
395 }
Ady Abraham8532d012019-05-08 14:50:56 -0700396}
397
Ady Abraham6fe2c172019-07-12 12:37:57 -0700398void Scheduler::setDisplayPowerState(bool normal) {
399 {
400 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700401 mFeatures.isDisplayPowerStateNormal = normal;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700402 }
403
404 if (mDisplayPowerTimer) {
405 mDisplayPowerTimer->reset();
406 }
407
408 // Display Power event will boost the refresh rate to performance.
409 // Clear Layer History to get fresh FPS detection
Dominik Laskowski49cea512019-11-12 14:13:23 -0800410 if (mLayerHistory) {
411 mLayerHistory->clear();
412 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700413}
414
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700415void Scheduler::kernelIdleTimerCallback(TimerState state) {
416 ATRACE_INT("ExpiredKernelIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100417
Ady Abraham2139f732019-11-13 18:56:40 -0800418 // TODO(145561154): cleanup the kernel idle timer implementation and the refresh rate
419 // magic number
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700420 const auto refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
Ady Abraham2139f732019-11-13 18:56:40 -0800421 constexpr float FPS_THRESHOLD_FOR_KERNEL_TIMER = 65.0f;
422 if (state == TimerState::Reset && refreshRate.fps > FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Alec Mouri7f015182019-07-11 13:56:22 -0700423 // If we're not in performance mode then the kernel timer shouldn't do
424 // anything, as the refresh rate during DPU power collapse will be the
425 // same.
Ady Abraham2139f732019-11-13 18:56:40 -0800426 resyncToHardwareVsync(true /* makeAvailable */, refreshRate.vsyncPeriod);
427 } else if (state == TimerState::Expired && refreshRate.fps <= FPS_THRESHOLD_FOR_KERNEL_TIMER) {
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700428 // Disable HW VSYNC if the timer expired, as we don't need it enabled if
429 // we're not pushing frames, and if we're in PERFORMANCE mode then we'll
430 // need to update the DispSync model anyway.
431 disableHardwareVsync(false /* makeUnavailable */);
Alec Mouridc28b372019-04-18 21:17:13 -0700432 }
433}
434
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700435void Scheduler::idleTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700436 handleTimerStateChanged(&mFeatures.idleTimer, state, false /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700437 ATRACE_INT("ExpiredIdleTimer", static_cast<int>(state));
Ana Krulecfb772822018-11-30 10:44:07 +0100438}
439
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700440void Scheduler::touchTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700441 const TouchState touch = state == TimerState::Reset ? TouchState::Active : TouchState::Inactive;
442 handleTimerStateChanged(&mFeatures.touch, touch, true /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700443 ATRACE_INT("TouchState", static_cast<int>(touch));
Ady Abraham8532d012019-05-08 14:50:56 -0700444}
445
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700446void Scheduler::displayPowerTimerCallback(TimerState state) {
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700447 handleTimerStateChanged(&mFeatures.displayPowerTimer, state,
448 true /* eventOnContentDetection */);
Dominik Laskowski3a80a382019-07-25 11:16:07 -0700449 ATRACE_INT("ExpiredDisplayPowerTimer", static_cast<int>(state));
Alec Mouridc28b372019-04-18 21:17:13 -0700450}
451
Dominik Laskowski98041832019-08-01 18:35:59 -0700452void Scheduler::dump(std::string& result) const {
Dominik Laskowski49cea512019-11-12 14:13:23 -0800453 using base::StringAppendF;
454 const char* const states[] = {"off", "on"};
Dominik Laskowski98041832019-08-01 18:35:59 -0700455
Dominik Laskowski49cea512019-11-12 14:13:23 -0800456 const bool supported = mRefreshRateConfigs.refreshRateSwitchingSupported();
457 StringAppendF(&result, "+ Refresh rate switching: %s\n", states[supported]);
458 StringAppendF(&result, "+ Content detection: %s\n", states[mLayerHistory.has_value()]);
459
460 StringAppendF(&result, "+ Idle timer: %s\n",
461 mIdleTimer ? mIdleTimer->dump().c_str() : states[0]);
462 StringAppendF(&result, "+ Touch timer: %s\n\n",
463 mTouchTimer ? mTouchTimer->dump().c_str() : states[0]);
Ana Krulecb43429d2019-01-09 14:28:51 -0800464}
465
Ady Abraham6fe2c172019-07-12 12:37:57 -0700466template <class T>
467void Scheduler::handleTimerStateChanged(T* currentState, T newState, bool eventOnContentDetection) {
Ady Abraham8532d012019-05-08 14:50:56 -0700468 ConfigEvent event = ConfigEvent::None;
Ady Abraham2139f732019-11-13 18:56:40 -0800469 HwcConfigIndexType newConfigId;
Ady Abraham8532d012019-05-08 14:50:56 -0700470 {
471 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700472 if (*currentState == newState) {
Ady Abraham8532d012019-05-08 14:50:56 -0700473 return;
474 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700475 *currentState = newState;
Ady Abraham2139f732019-11-13 18:56:40 -0800476 newConfigId = calculateRefreshRateType();
477 if (mFeatures.configId == newConfigId) {
Ady Abraham8532d012019-05-08 14:50:56 -0700478 return;
479 }
Ady Abraham2139f732019-11-13 18:56:40 -0800480 mFeatures.configId = newConfigId;
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700481 if (eventOnContentDetection && mFeatures.contentDetection == ContentDetectionState::On) {
Ady Abraham8532d012019-05-08 14:50:56 -0700482 event = ConfigEvent::Changed;
483 }
484 }
Ady Abraham2139f732019-11-13 18:56:40 -0800485 const RefreshRate& newRefreshRate = mRefreshRateConfigs.getRefreshRateFromConfigId(newConfigId);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800486 mSchedulerCallback.changeRefreshRate(newRefreshRate, event);
Ady Abraham8532d012019-05-08 14:50:56 -0700487}
488
Ady Abraham2139f732019-11-13 18:56:40 -0800489HwcConfigIndexType Scheduler::calculateRefreshRateType() {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700490 if (!mRefreshRateConfigs.refreshRateSwitchingSupported()) {
Ady Abraham2139f732019-11-13 18:56:40 -0800491 return mRefreshRateConfigs.getCurrentRefreshRate().configId;
Ady Abraham09bd3922019-04-08 10:44:56 -0700492 }
493
Ady Abraham6fe2c172019-07-12 12:37:57 -0700494 // If Display Power is not in normal operation we want to be in performance mode.
495 // When coming back to normal mode, a grace period is given with DisplayPowerTimer
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700496 if (!mFeatures.isDisplayPowerStateNormal || mFeatures.displayPowerTimer == TimerState::Reset) {
Ady Abraham2139f732019-11-13 18:56:40 -0800497 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700498 }
499
Ady Abraham8532d012019-05-08 14:50:56 -0700500 // As long as touch is active we want to be in performance mode
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700501 if (mFeatures.touch == TouchState::Active) {
Ady Abraham2139f732019-11-13 18:56:40 -0800502 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
Ady Abraham8532d012019-05-08 14:50:56 -0700503 }
504
505 // If timer has expired as it means there is no new content on the screen
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700506 if (mFeatures.idleTimer == TimerState::Expired) {
Ady Abraham2139f732019-11-13 18:56:40 -0800507 return mRefreshRateConfigs.getMinRefreshRateByPolicy().configId;
Ady Abrahama315ce72019-04-24 14:35:20 -0700508 }
509
Ady Abraham09bd3922019-04-08 10:44:56 -0700510 // If content detection is off we choose performance as we don't know the content fps
Dominik Laskowskidd252cd2019-07-26 09:10:16 -0700511 if (mFeatures.contentDetection == ContentDetectionState::Off) {
Ady Abraham2139f732019-11-13 18:56:40 -0800512 return mRefreshRateConfigs.getMaxRefreshRateByPolicy().configId;
Ady Abraham09bd3922019-04-08 10:44:56 -0700513 }
514
Wei Wang09be73f2019-07-02 14:29:18 -0700515 // Content detection is on, find the appropriate refresh rate with minimal error
Ady Abraham2139f732019-11-13 18:56:40 -0800516 return mRefreshRateConfigs
517 .getRefreshRateForContent(static_cast<float>(mFeatures.contentRefreshRate))
518 .configId;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800519}
520
Ady Abraham2139f732019-11-13 18:56:40 -0800521std::optional<HwcConfigIndexType> Scheduler::getPreferredConfigId() {
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700522 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800523 return mFeatures.configId;
Daniel Solomon0f0ddc12019-08-19 19:31:09 -0700524}
525
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800526void Scheduler::onNewVsyncPeriodChangeTimeline(const HWC2::VsyncPeriodChangeTimeline& timeline) {
527 if (timeline.refreshRequired) {
528 mSchedulerCallback.repaintEverythingForHWC();
529 }
530
531 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
532 mLastVsyncPeriodChangeTimeline = std::make_optional(timeline);
533
534 const auto maxAppliedTime = systemTime() + MAX_VSYNC_APPLIED_TIME.count();
535 if (timeline.newVsyncAppliedTimeNanos > maxAppliedTime) {
536 mLastVsyncPeriodChangeTimeline->newVsyncAppliedTimeNanos = maxAppliedTime;
537 }
538}
539
540void Scheduler::onDisplayRefreshed(nsecs_t timestamp) {
541 bool callRepaint = false;
542 {
543 std::lock_guard<std::mutex> lock(mVsyncTimelineLock);
544 if (mLastVsyncPeriodChangeTimeline && mLastVsyncPeriodChangeTimeline->refreshRequired) {
545 if (mLastVsyncPeriodChangeTimeline->refreshTimeNanos < timestamp) {
546 mLastVsyncPeriodChangeTimeline->refreshRequired = false;
547 } else {
548 // We need to send another refresh as refreshTimeNanos is still in the future
549 callRepaint = true;
550 }
551 }
552 }
553
554 if (callRepaint) {
555 mSchedulerCallback.repaintEverythingForHWC();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800556 }
557}
558
Ana Krulec98b5b242018-08-10 15:03:23 -0700559} // namespace android