blob: 9435193d05f159f7276dc547c87034599451deb5 [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
Ana Krulec7ab56032018-11-02 20:51:06 +010017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Ana Krulec98b5b242018-08-10 15:03:23 -070019#include "Scheduler.h"
20
Ana Krulec434c22d2018-11-28 13:48:36 +010021#include <algorithm>
Ana Krulec98b5b242018-08-10 15:03:23 -070022#include <cinttypes>
23#include <cstdint>
24#include <memory>
Ana Krulec7ab56032018-11-02 20:51:06 +010025#include <numeric>
Ana Krulec98b5b242018-08-10 15:03:23 -070026
Ana Krulece588e312018-09-18 12:32:24 -070027#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
28#include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
Ana Krulece588e312018-09-18 12:32:24 -070029#include <configstore/Utils.h>
Ana Krulecfb772822018-11-30 10:44:07 +010030#include <cutils/properties.h>
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070031#include <input/InputWindow.h>
Ana Krulecfefd6ae2019-02-13 17:53:08 -080032#include <system/window.h>
Ana Krulece588e312018-09-18 12:32:24 -070033#include <ui/DisplayStatInfo.h>
Ana Krulec3084c052018-11-21 20:27:17 +010034#include <utils/Timers.h>
Ana Krulec7ab56032018-11-02 20:51:06 +010035#include <utils/Trace.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070036
37#include "DispSync.h"
38#include "DispSyncSource.h"
Ana Krulece588e312018-09-18 12:32:24 -070039#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070040#include "EventThread.h"
41#include "InjectVSyncSource.h"
Ady Abraham09bd3922019-04-08 10:44:56 -070042#include "LayerInfo.h"
Ana Krulecf2c006d2019-06-21 15:37:07 -070043#include "OneShotTimer.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010044#include "SchedulerUtils.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090045#include "SurfaceFlingerProperties.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070046
47namespace android {
48
Ana Krulece588e312018-09-18 12:32:24 -070049using namespace android::hardware::configstore;
50using namespace android::hardware::configstore::V1_0;
Sundong Ahnd5e08f62018-12-12 20:27:28 +090051using namespace android::sysprop;
Ana Krulece588e312018-09-18 12:32:24 -070052
Ana Krulec0c8cd522018-08-31 12:27:28 -070053#define RETURN_VALUE_IF_INVALID(value) \
54 if (handle == nullptr || mConnections.count(handle->id) == 0) return value
55#define RETURN_IF_INVALID() \
56 if (handle == nullptr || mConnections.count(handle->id) == 0) return
57
Ana Krulec98b5b242018-08-10 15:03:23 -070058std::atomic<int64_t> Scheduler::sNextId = 0;
59
Ady Abraham09bd3922019-04-08 10:44:56 -070060Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function,
61 const scheduler::RefreshRateConfigs& refreshRateConfig)
Sundong Ahnd5e08f62018-12-12 20:27:28 +090062 : mHasSyncFramework(running_without_sync_framework(true)),
63 mDispSyncPresentTimeOffset(present_time_offset_from_vsync_ns(0)),
Ana Krulece588e312018-09-18 12:32:24 -070064 mPrimaryHWVsyncEnabled(false),
Ady Abraham09bd3922019-04-08 10:44:56 -070065 mHWVsyncAvailable(false),
66 mRefreshRateConfigs(refreshRateConfig) {
Ana Krulece588e312018-09-18 12:32:24 -070067 // Note: We create a local temporary with the real DispSync implementation
68 // type temporarily so we can initialize it with the configured values,
69 // before storing it for more generic use using the interface type.
70 auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync");
71 primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset);
72 mPrimaryDispSync = std::move(primaryDispSync);
73 mEventControlThread = std::make_unique<impl::EventControlThread>(function);
Ana Krulecfb772822018-11-30 10:44:07 +010074
Ady Abrahambe59c0d2019-03-05 13:01:13 -080075 mSetIdleTimerMs = set_idle_timer_ms(0);
Alec Mouridc28b372019-04-18 21:17:13 -070076 mSupportKernelTimer = support_kernel_idle_timer(false);
Ady Abrahambe59c0d2019-03-05 13:01:13 -080077
Ady Abraham8532d012019-05-08 14:50:56 -070078 mSetTouchTimerMs = set_touch_timer_ms(0);
Ady Abraham6fe2c172019-07-12 12:37:57 -070079 mSetDisplayPowerTimerMs = set_display_power_timer_ms(0);
Ady Abraham8532d012019-05-08 14:50:56 -070080
Ana Krulecfb772822018-11-30 10:44:07 +010081 char value[PROPERTY_VALUE_MAX];
Ana Kruleca5bdd9d2019-01-29 19:00:58 -080082 property_get("debug.sf.set_idle_timer_ms", value, "0");
Ady Abrahambe59c0d2019-03-05 13:01:13 -080083 int int_value = atoi(value);
84 if (int_value) {
85 mSetIdleTimerMs = atoi(value);
86 }
Ana Krulecfb772822018-11-30 10:44:07 +010087
88 if (mSetIdleTimerMs > 0) {
Alec Mouridc28b372019-04-18 21:17:13 -070089 if (mSupportKernelTimer) {
Ana Krulecf2c006d2019-06-21 15:37:07 -070090 mIdleTimer = std::make_unique<scheduler::OneShotTimer>(
91 std::chrono::milliseconds(mSetIdleTimerMs),
92 [this] { resetKernelTimerCallback(); },
93 [this] { expiredKernelTimerCallback(); });
Alec Mouridc28b372019-04-18 21:17:13 -070094 } else {
Ana Krulecf2c006d2019-06-21 15:37:07 -070095 mIdleTimer = std::make_unique<scheduler::OneShotTimer>(
96 std::chrono::milliseconds(mSetIdleTimerMs), [this] { resetTimerCallback(); },
97 [this] { expiredTimerCallback(); });
Alec Mouridc28b372019-04-18 21:17:13 -070098 }
Ana Krulecfb772822018-11-30 10:44:07 +010099 mIdleTimer->start();
100 }
Ady Abraham8532d012019-05-08 14:50:56 -0700101
102 if (mSetTouchTimerMs > 0) {
103 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
Ana Krulecf2c006d2019-06-21 15:37:07 -0700104 mTouchTimer = std::make_unique<scheduler::OneShotTimer>(
105 std::chrono::milliseconds(mSetTouchTimerMs), [this] { resetTouchTimerCallback(); },
106 [this] { expiredTouchTimerCallback(); });
Ady Abraham8532d012019-05-08 14:50:56 -0700107 mTouchTimer->start();
108 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700109
110 if (mSetDisplayPowerTimerMs > 0) {
111 mDisplayPowerTimer = std::make_unique<scheduler::OneShotTimer>(
112 std::chrono::milliseconds(mSetDisplayPowerTimerMs),
113 [this] { resetDisplayPowerTimerCallback(); },
114 [this] { expiredDisplayPowerTimerCallback(); });
115 mDisplayPowerTimer->start();
116 }
Ana Krulece588e312018-09-18 12:32:24 -0700117}
118
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800119Scheduler::~Scheduler() {
Ana Krulecf2c006d2019-06-21 15:37:07 -0700120 // Ensure the OneShotTimer threads are joined before we start destroying state.
Ady Abraham6fe2c172019-07-12 12:37:57 -0700121 mDisplayPowerTimer.reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700122 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800123 mIdleTimer.reset();
124}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700125
Ana Krulec98b5b242018-08-10 15:03:23 -0700126sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Ady Abraham45e4e362019-06-07 18:20:51 -0700127 const char* connectionName, nsecs_t phaseOffsetNs, nsecs_t offsetThresholdForNextVsync,
128 ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -0700129 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
130 const int64_t id = sNextId++;
131 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
132
Ana Krulec98b5b242018-08-10 15:03:23 -0700133 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -0800134 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Ady Abraham45e4e362019-06-07 18:20:51 -0700135 offsetThresholdForNextVsync, std::move(interceptCallback));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800136
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800137 auto eventThreadConnection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700138 createConnectionInternal(eventThread.get(), std::move(resyncCallback),
139 ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800140 mConnections.emplace(id,
141 std::make_unique<Connection>(new ConnectionHandle(id),
142 eventThreadConnection,
143 std::move(eventThread)));
Ana Krulec98b5b242018-08-10 15:03:23 -0700144 return mConnections[id]->handle;
145}
146
Ana Krulec0c8cd522018-08-31 12:27:28 -0700147std::unique_ptr<EventThread> Scheduler::makeEventThread(
Ady Abraham45e4e362019-06-07 18:20:51 -0700148 const char* connectionName, DispSync* dispSync, nsecs_t phaseOffsetNs,
149 nsecs_t offsetThresholdForNextVsync,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700150 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
151 std::unique_ptr<VSyncSource> eventThreadSource =
Ady Abraham45e4e362019-06-07 18:20:51 -0700152 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, offsetThresholdForNextVsync,
153 true, connectionName);
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800154 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800155 std::move(interceptCallback), connectionName);
156}
157
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700158sp<EventThreadConnection> Scheduler::createConnectionInternal(
159 EventThread* eventThread, ResyncCallback&& resyncCallback,
160 ISurfaceComposer::ConfigChanged configChanged) {
161 return eventThread->createEventConnection(std::move(resyncCallback), configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700162}
163
Ana Krulec98b5b242018-08-10 15:03:23 -0700164sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700165 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback,
166 ISurfaceComposer::ConfigChanged configChanged) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700167 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800168 return createConnectionInternal(mConnections[handle->id]->thread.get(),
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700169 std::move(resyncCallback), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700170}
171
172EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700173 RETURN_VALUE_IF_INVALID(nullptr);
174 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700175}
176
Ana Krulec85c39af2018-12-26 17:29:57 -0800177sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700178 RETURN_VALUE_IF_INVALID(nullptr);
179 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700180}
181
182void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800183 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700184 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800185 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700186}
187
188void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700189 RETURN_IF_INVALID();
190 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700191}
192
193void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700194 RETURN_IF_INVALID();
195 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700196}
197
Ady Abraham447052e2019-02-13 16:07:27 -0800198void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
199 int32_t configId) {
200 RETURN_IF_INVALID();
201 mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
202}
203
Yiwei Zhang5434a782018-12-05 18:06:32 -0800204void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700205 RETURN_IF_INVALID();
206 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700207}
208
209void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700210 RETURN_IF_INVALID();
211 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700212}
Ana Krulece588e312018-09-18 12:32:24 -0700213
214void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
215 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
216 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
217}
218
219void Scheduler::enableHardwareVsync() {
220 std::lock_guard<std::mutex> lock(mHWVsyncLock);
221 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
222 mPrimaryDispSync->beginResync();
223 mEventControlThread->setVsyncEnabled(true);
224 mPrimaryHWVsyncEnabled = true;
225 }
226}
227
228void Scheduler::disableHardwareVsync(bool makeUnavailable) {
229 std::lock_guard<std::mutex> lock(mHWVsyncLock);
230 if (mPrimaryHWVsyncEnabled) {
231 mEventControlThread->setVsyncEnabled(false);
232 mPrimaryDispSync->endResync();
233 mPrimaryHWVsyncEnabled = false;
234 }
235 if (makeUnavailable) {
236 mHWVsyncAvailable = false;
237 }
238}
239
Ana Krulecc2870422019-01-29 19:00:58 -0800240void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
241 {
242 std::lock_guard<std::mutex> lock(mHWVsyncLock);
243 if (makeAvailable) {
244 mHWVsyncAvailable = makeAvailable;
245 } else if (!mHWVsyncAvailable) {
246 // Hardware vsync is not currently available, so abort the resync
247 // attempt for now
248 return;
249 }
250 }
251
252 if (period <= 0) {
253 return;
254 }
255
256 setVsyncPeriod(period);
257}
258
259ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
260 std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
261 return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
262 if (const auto vsync = ptr.lock()) {
263 vsync->resync(getVsyncPeriod);
264 }
265 };
266}
267
268void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
269 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
270
271 const nsecs_t now = systemTime();
272 const nsecs_t last = lastResyncTime.exchange(now);
273
274 if (now - last > kIgnoreDelay) {
275 scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
276 }
277}
278
279void Scheduler::setRefreshSkipCount(int count) {
280 mPrimaryDispSync->setRefreshSkipCount(count);
281}
282
Ana Krulece588e312018-09-18 12:32:24 -0700283void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800284 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700285 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800286
287 if (!mPrimaryHWVsyncEnabled) {
288 mPrimaryDispSync->beginResync();
289 mEventControlThread->setVsyncEnabled(true);
290 mPrimaryHWVsyncEnabled = true;
291 }
Ana Krulece588e312018-09-18 12:32:24 -0700292}
293
Alec Mourif8e689c2019-05-20 18:32:22 -0700294void Scheduler::addResyncSample(const nsecs_t timestamp, bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700295 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700296 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700297 { // Scope for the lock
298 std::lock_guard<std::mutex> lock(mHWVsyncLock);
299 if (mPrimaryHWVsyncEnabled) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700300 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700301 }
302 }
303
304 if (needsHwVsync) {
305 enableHardwareVsync();
306 } else {
307 disableHardwareVsync(false);
308 }
309}
310
311void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
312 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
313 enableHardwareVsync();
314 } else {
315 disableHardwareVsync(false);
316 }
317}
318
319void Scheduler::setIgnorePresentFences(bool ignore) {
320 mPrimaryDispSync->setIgnorePresentFences(ignore);
321}
322
Ady Abraham8fe11022019-06-12 17:11:12 -0700323nsecs_t Scheduler::getDispSyncExpectedPresentTime() {
Ady Abrahamc3e21312019-02-07 14:30:23 -0800324 return mPrimaryDispSync->expectedPresentTime();
325}
326
Ady Abraham3aff9172019-02-07 19:10:26 -0800327void Scheduler::dumpPrimaryDispSync(std::string& result) const {
328 mPrimaryDispSync->dump(result);
329}
330
Ady Abraham09bd3922019-04-08 10:44:56 -0700331std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer(
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700332 std::string const& name, int windowType) {
333 RefreshRateType refreshRateType = (windowType == InputWindowInfo::TYPE_WALLPAPER)
334 ? RefreshRateType::DEFAULT
335 : RefreshRateType::PERFORMANCE;
Ady Abraham09bd3922019-04-08 10:44:56 -0700336
337 const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType);
Ana Krulecad083c42019-06-26 16:28:08 -0700338 const uint32_t performanceFps = (refreshRate) ? refreshRate->fps : 0;
339
340 const auto defaultRefreshRate = mRefreshRateConfigs.getRefreshRate(RefreshRateType::DEFAULT);
341 const uint32_t defaultFps = (defaultRefreshRate) ? defaultRefreshRate->fps : 0;
342 return mLayerHistory.createLayer(name, defaultFps, performanceFps);
Ady Abraham09bd3922019-04-08 10:44:56 -0700343}
344
Ady Abrahama315ce72019-04-24 14:35:20 -0700345void Scheduler::addLayerPresentTimeAndHDR(
Ady Abraham09bd3922019-04-08 10:44:56 -0700346 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
Ady Abrahama315ce72019-04-24 14:35:20 -0700347 nsecs_t presentTime, bool isHDR) {
348 mLayerHistory.insert(layerHandle, presentTime, isHDR);
349}
350
351void Scheduler::setLayerVisibility(
352 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, bool visible) {
353 mLayerHistory.setVisibility(layerHandle, visible);
Ana Krulec3084c052018-11-21 20:27:17 +0100354}
355
Kevin DuBois413287f2019-02-25 08:46:47 -0800356void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
357 fn(*mPrimaryDispSync);
358}
359
Ady Abraham09bd3922019-04-08 10:44:56 -0700360void Scheduler::updateFpsBasedOnContent() {
Ady Abrahama315ce72019-04-24 14:35:20 -0700361 auto [refreshRate, isHDR] = mLayerHistory.getDesiredRefreshRateAndHDR();
362 const uint32_t refreshRateRound = std::round(refreshRate);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700363 RefreshRateType newRefreshRateType;
364 {
365 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abrahama315ce72019-04-24 14:35:20 -0700366 if (mContentRefreshRate == refreshRateRound && mIsHDRContent == isHDR) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700367 return;
368 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700369 mContentRefreshRate = refreshRateRound;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700370 ATRACE_INT("ContentFPS", mContentRefreshRate);
371
Ady Abrahama315ce72019-04-24 14:35:20 -0700372 mIsHDRContent = isHDR;
373 ATRACE_INT("ContentHDR", mIsHDRContent);
374
375 mCurrentContentFeatureState = refreshRateRound > 0
376 ? ContentFeatureState::CONTENT_DETECTION_ON
377 : ContentFeatureState::CONTENT_DETECTION_OFF;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700378 newRefreshRateType = calculateRefreshRateType();
379 if (mRefreshRateType == newRefreshRateType) {
380 return;
381 }
382 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800383 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700384 changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
Ana Krulec3084c052018-11-21 20:27:17 +0100385}
386
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800387void Scheduler::setChangeRefreshRateCallback(
388 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800389 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800390 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800391}
392
Alec Mouridc28b372019-04-18 21:17:13 -0700393void Scheduler::setGetVsyncPeriodCallback(const GetVsyncPeriod&& getVsyncPeriod) {
394 std::lock_guard<std::mutex> lock(mCallbackLock);
395 mGetVsyncPeriod = getVsyncPeriod;
396}
397
Ana Krulec3084c052018-11-21 20:27:17 +0100398void Scheduler::updateFrameSkipping(const int64_t skipCount) {
399 ATRACE_INT("FrameSkipCount", skipCount);
400 if (mSkipCount != skipCount) {
401 // Only update DispSync if it hasn't been updated yet.
402 mPrimaryDispSync->setRefreshSkipCount(skipCount);
403 mSkipCount = skipCount;
404 }
405}
406
Ana Krulecfb772822018-11-30 10:44:07 +0100407void Scheduler::resetIdleTimer() {
408 if (mIdleTimer) {
409 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800410 }
411}
412
Ady Abraham8532d012019-05-08 14:50:56 -0700413void Scheduler::notifyTouchEvent() {
414 if (mTouchTimer) {
415 mTouchTimer->reset();
416 }
417
418 if (mSupportKernelTimer) {
419 resetIdleTimer();
420 }
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700421
422 // Touch event will boost the refresh rate to performance.
423 // Clear Layer History to get fresh FPS detection
424 mLayerHistory.clearHistory();
Ady Abraham8532d012019-05-08 14:50:56 -0700425}
426
Ady Abraham6fe2c172019-07-12 12:37:57 -0700427void Scheduler::setDisplayPowerState(bool normal) {
428 {
429 std::lock_guard<std::mutex> lock(mFeatureStateLock);
430 mIsDisplayPowerStateNormal = normal;
431 }
432
433 if (mDisplayPowerTimer) {
434 mDisplayPowerTimer->reset();
435 }
436
437 // Display Power event will boost the refresh rate to performance.
438 // Clear Layer History to get fresh FPS detection
439 mLayerHistory.clearHistory();
440}
441
Ady Abrahama1a49af2019-02-07 14:36:55 -0800442void Scheduler::resetTimerCallback() {
Ady Abraham6fe2c172019-07-12 12:37:57 -0700443 handleTimerStateChanged(&mCurrentIdleTimerState, IdleTimerState::RESET, false);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800444 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100445}
446
Alec Mouridc28b372019-04-18 21:17:13 -0700447void Scheduler::resetKernelTimerCallback() {
448 ATRACE_INT("ExpiredKernelIdleTimer", 0);
449 std::lock_guard<std::mutex> lock(mCallbackLock);
450 if (mGetVsyncPeriod) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700451 resyncToHardwareVsync(true, mGetVsyncPeriod());
Alec Mouridc28b372019-04-18 21:17:13 -0700452 }
453}
454
Ana Krulecfb772822018-11-30 10:44:07 +0100455void Scheduler::expiredTimerCallback() {
Ady Abraham6fe2c172019-07-12 12:37:57 -0700456 handleTimerStateChanged(&mCurrentIdleTimerState, IdleTimerState::EXPIRED, false);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800457 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100458}
459
Ady Abraham8532d012019-05-08 14:50:56 -0700460void Scheduler::resetTouchTimerCallback() {
Ady Abraham6fe2c172019-07-12 12:37:57 -0700461 handleTimerStateChanged(&mCurrentTouchState, TouchState::ACTIVE, true);
Ady Abraham8532d012019-05-08 14:50:56 -0700462 ATRACE_INT("TouchState", 1);
463}
464
465void Scheduler::expiredTouchTimerCallback() {
Ady Abraham6fe2c172019-07-12 12:37:57 -0700466 handleTimerStateChanged(&mCurrentTouchState, TouchState::INACTIVE, true);
Ady Abraham8532d012019-05-08 14:50:56 -0700467 ATRACE_INT("TouchState", 0);
468}
469
Ady Abraham6fe2c172019-07-12 12:37:57 -0700470void Scheduler::resetDisplayPowerTimerCallback() {
471 handleTimerStateChanged(&mDisplayPowerTimerState, DisplayPowerTimerState::RESET, true);
472 ATRACE_INT("ExpiredDisplayPowerTimer", 0);
473}
474
475void Scheduler::expiredDisplayPowerTimerCallback() {
476 handleTimerStateChanged(&mDisplayPowerTimerState, DisplayPowerTimerState::EXPIRED, true);
477 ATRACE_INT("ExpiredDisplayPowerTimer", 1);
478}
479
Alec Mouridc28b372019-04-18 21:17:13 -0700480void Scheduler::expiredKernelTimerCallback() {
481 ATRACE_INT("ExpiredKernelIdleTimer", 1);
482 // Disable HW Vsync if the timer expired, as we don't need it
483 // enabled if we're not pushing frames.
484 disableHardwareVsync(false);
485}
486
Ana Krulecb43429d2019-01-09 14:28:51 -0800487std::string Scheduler::doDump() {
488 std::ostringstream stream;
489 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
Ady Abraham8532d012019-05-08 14:50:56 -0700490 stream << "+ Touch timer interval: " << mSetTouchTimerMs << " ms" << std::endl;
Ana Krulecb43429d2019-01-09 14:28:51 -0800491 return stream.str();
492}
493
Ady Abraham6fe2c172019-07-12 12:37:57 -0700494template <class T>
495void Scheduler::handleTimerStateChanged(T* currentState, T newState, bool eventOnContentDetection) {
Ady Abraham8532d012019-05-08 14:50:56 -0700496 ConfigEvent event = ConfigEvent::None;
497 RefreshRateType newRefreshRateType;
498 {
499 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700500 if (*currentState == newState) {
Ady Abraham8532d012019-05-08 14:50:56 -0700501 return;
502 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700503 *currentState = newState;
Ady Abraham8532d012019-05-08 14:50:56 -0700504 newRefreshRateType = calculateRefreshRateType();
505 if (mRefreshRateType == newRefreshRateType) {
506 return;
507 }
508 mRefreshRateType = newRefreshRateType;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700509 if (eventOnContentDetection &&
510 mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_ON) {
Ady Abraham8532d012019-05-08 14:50:56 -0700511 event = ConfigEvent::Changed;
512 }
513 }
514 changeRefreshRate(newRefreshRateType, event);
515}
516
Ady Abraham09bd3922019-04-08 10:44:56 -0700517Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
Ady Abraham8532d012019-05-08 14:50:56 -0700518 // HDR content is not supported on PERFORMANCE mode
519 if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700520 return RefreshRateType::DEFAULT;
521 }
522
Ady Abraham6fe2c172019-07-12 12:37:57 -0700523 // If Display Power is not in normal operation we want to be in performance mode.
524 // When coming back to normal mode, a grace period is given with DisplayPowerTimer
525 if (!mIsDisplayPowerStateNormal || mDisplayPowerTimerState == DisplayPowerTimerState::RESET) {
526 return RefreshRateType::PERFORMANCE;
527 }
528
Ady Abraham8532d012019-05-08 14:50:56 -0700529 // As long as touch is active we want to be in performance mode
530 if (mCurrentTouchState == TouchState::ACTIVE) {
531 return RefreshRateType::PERFORMANCE;
532 }
533
534 // If timer has expired as it means there is no new content on the screen
535 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
Ady Abrahama315ce72019-04-24 14:35:20 -0700536 return RefreshRateType::DEFAULT;
537 }
538
Ady Abraham09bd3922019-04-08 10:44:56 -0700539 // If content detection is off we choose performance as we don't know the content fps
540 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
541 return RefreshRateType::PERFORMANCE;
542 }
543
Wei Wang09be73f2019-07-02 14:29:18 -0700544 // Content detection is on, find the appropriate refresh rate with minimal error
545 auto iter = min_element(mRefreshRateConfigs.getRefreshRates().cbegin(),
546 mRefreshRateConfigs.getRefreshRates().cend(),
547 [rate = mContentRefreshRate](const auto& l, const auto& r) -> bool {
548 return std::abs(l.second->fps - static_cast<float>(rate)) <
549 std::abs(r.second->fps - static_cast<float>(rate));
550 });
551 RefreshRateType currRefreshRateType = iter->first;
Ady Abraham09bd3922019-04-08 10:44:56 -0700552
Ady Abraham85b3f012019-04-08 11:04:14 -0700553 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
554 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
555 // align well with both
Wei Wang09be73f2019-07-02 14:29:18 -0700556 constexpr float MARGIN = 0.05f;
Ady Abraham85b3f012019-04-08 11:04:14 -0700557 float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps /
558 float(mContentRefreshRate);
559 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
560 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
561 ratio = iter->second->fps / float(mContentRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -0700562
Ady Abraham85b3f012019-04-08 11:04:14 -0700563 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
564 currRefreshRateType = iter->first;
565 break;
566 }
567 ++iter;
568 }
569 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700570
571 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800572}
573
574void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
575 std::lock_guard<std::mutex> lock(mCallbackLock);
576 if (mChangeRefreshRateCallback) {
577 mChangeRefreshRateCallback(refreshRateType, configEvent);
578 }
579}
580
Ana Krulec98b5b242018-08-10 15:03:23 -0700581} // namespace android