blob: 7acb4701d9475126bdb241227f4d601b6a4f294f [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(
Alec Mouri7f015182019-07-11 13:56:22 -0700388 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 Mouri7f015182019-07-11 13:56:22 -0700393void Scheduler::setGetCurrentRefreshRateTypeCallback(
394 const GetCurrentRefreshRateTypeCallback&& getCurrentRefreshRateTypeCallback) {
395 std::lock_guard<std::mutex> lock(mCallbackLock);
396 mGetCurrentRefreshRateTypeCallback = getCurrentRefreshRateTypeCallback;
397}
398
Alec Mouridc28b372019-04-18 21:17:13 -0700399void Scheduler::setGetVsyncPeriodCallback(const GetVsyncPeriod&& getVsyncPeriod) {
400 std::lock_guard<std::mutex> lock(mCallbackLock);
401 mGetVsyncPeriod = getVsyncPeriod;
402}
403
Ana Krulec3084c052018-11-21 20:27:17 +0100404void Scheduler::updateFrameSkipping(const int64_t skipCount) {
405 ATRACE_INT("FrameSkipCount", skipCount);
406 if (mSkipCount != skipCount) {
407 // Only update DispSync if it hasn't been updated yet.
408 mPrimaryDispSync->setRefreshSkipCount(skipCount);
409 mSkipCount = skipCount;
410 }
411}
412
Ana Krulecfb772822018-11-30 10:44:07 +0100413void Scheduler::resetIdleTimer() {
414 if (mIdleTimer) {
415 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800416 }
417}
418
Ady Abraham8532d012019-05-08 14:50:56 -0700419void Scheduler::notifyTouchEvent() {
420 if (mTouchTimer) {
421 mTouchTimer->reset();
422 }
423
424 if (mSupportKernelTimer) {
425 resetIdleTimer();
426 }
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700427
428 // Touch event will boost the refresh rate to performance.
429 // Clear Layer History to get fresh FPS detection
430 mLayerHistory.clearHistory();
Ady Abraham8532d012019-05-08 14:50:56 -0700431}
432
Ady Abraham6fe2c172019-07-12 12:37:57 -0700433void Scheduler::setDisplayPowerState(bool normal) {
434 {
435 std::lock_guard<std::mutex> lock(mFeatureStateLock);
436 mIsDisplayPowerStateNormal = normal;
437 }
438
439 if (mDisplayPowerTimer) {
440 mDisplayPowerTimer->reset();
441 }
442
443 // Display Power event will boost the refresh rate to performance.
444 // Clear Layer History to get fresh FPS detection
445 mLayerHistory.clearHistory();
446}
447
Ady Abrahama1a49af2019-02-07 14:36:55 -0800448void Scheduler::resetTimerCallback() {
Ady Abraham6fe2c172019-07-12 12:37:57 -0700449 handleTimerStateChanged(&mCurrentIdleTimerState, IdleTimerState::RESET, false);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800450 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100451}
452
Alec Mouridc28b372019-04-18 21:17:13 -0700453void Scheduler::resetKernelTimerCallback() {
454 ATRACE_INT("ExpiredKernelIdleTimer", 0);
455 std::lock_guard<std::mutex> lock(mCallbackLock);
Alec Mouri7f015182019-07-11 13:56:22 -0700456 if (mGetVsyncPeriod && mGetCurrentRefreshRateTypeCallback) {
457 // If we're not in performance mode then the kernel timer shouldn't do
458 // anything, as the refresh rate during DPU power collapse will be the
459 // same.
460 if (mGetCurrentRefreshRateTypeCallback() == Scheduler::RefreshRateType::PERFORMANCE) {
461 resyncToHardwareVsync(true, mGetVsyncPeriod());
462 }
Alec Mouridc28b372019-04-18 21:17:13 -0700463 }
464}
465
Ana Krulecfb772822018-11-30 10:44:07 +0100466void Scheduler::expiredTimerCallback() {
Ady Abraham6fe2c172019-07-12 12:37:57 -0700467 handleTimerStateChanged(&mCurrentIdleTimerState, IdleTimerState::EXPIRED, false);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800468 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100469}
470
Ady Abraham8532d012019-05-08 14:50:56 -0700471void Scheduler::resetTouchTimerCallback() {
Ady Abraham6fe2c172019-07-12 12:37:57 -0700472 handleTimerStateChanged(&mCurrentTouchState, TouchState::ACTIVE, true);
Ady Abraham8532d012019-05-08 14:50:56 -0700473 ATRACE_INT("TouchState", 1);
474}
475
476void Scheduler::expiredTouchTimerCallback() {
Ady Abraham6fe2c172019-07-12 12:37:57 -0700477 handleTimerStateChanged(&mCurrentTouchState, TouchState::INACTIVE, true);
Ady Abraham8532d012019-05-08 14:50:56 -0700478 ATRACE_INT("TouchState", 0);
479}
480
Ady Abraham6fe2c172019-07-12 12:37:57 -0700481void Scheduler::resetDisplayPowerTimerCallback() {
482 handleTimerStateChanged(&mDisplayPowerTimerState, DisplayPowerTimerState::RESET, true);
483 ATRACE_INT("ExpiredDisplayPowerTimer", 0);
484}
485
486void Scheduler::expiredDisplayPowerTimerCallback() {
487 handleTimerStateChanged(&mDisplayPowerTimerState, DisplayPowerTimerState::EXPIRED, true);
488 ATRACE_INT("ExpiredDisplayPowerTimer", 1);
489}
490
Alec Mouridc28b372019-04-18 21:17:13 -0700491void Scheduler::expiredKernelTimerCallback() {
Alec Mouri7f015182019-07-11 13:56:22 -0700492 std::lock_guard<std::mutex> lock(mCallbackLock);
Alec Mouridc28b372019-04-18 21:17:13 -0700493 ATRACE_INT("ExpiredKernelIdleTimer", 1);
Alec Mouri7f015182019-07-11 13:56:22 -0700494 if (mGetCurrentRefreshRateTypeCallback) {
495 if (mGetCurrentRefreshRateTypeCallback() != Scheduler::RefreshRateType::PERFORMANCE) {
496 // Disable HW Vsync if the timer expired, as we don't need it
497 // enabled if we're not pushing frames, and if we're in PERFORMANCE
498 // mode then we'll need to re-update the DispSync model anyways.
499 disableHardwareVsync(false);
500 }
501 }
Alec Mouridc28b372019-04-18 21:17:13 -0700502}
503
Ana Krulecb43429d2019-01-09 14:28:51 -0800504std::string Scheduler::doDump() {
505 std::ostringstream stream;
506 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
Ady Abraham8532d012019-05-08 14:50:56 -0700507 stream << "+ Touch timer interval: " << mSetTouchTimerMs << " ms" << std::endl;
Ana Krulecb43429d2019-01-09 14:28:51 -0800508 return stream.str();
509}
510
Ady Abraham6fe2c172019-07-12 12:37:57 -0700511template <class T>
512void Scheduler::handleTimerStateChanged(T* currentState, T newState, bool eventOnContentDetection) {
Ady Abraham8532d012019-05-08 14:50:56 -0700513 ConfigEvent event = ConfigEvent::None;
514 RefreshRateType newRefreshRateType;
515 {
516 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6fe2c172019-07-12 12:37:57 -0700517 if (*currentState == newState) {
Ady Abraham8532d012019-05-08 14:50:56 -0700518 return;
519 }
Ady Abraham6fe2c172019-07-12 12:37:57 -0700520 *currentState = newState;
Ady Abraham8532d012019-05-08 14:50:56 -0700521 newRefreshRateType = calculateRefreshRateType();
522 if (mRefreshRateType == newRefreshRateType) {
523 return;
524 }
525 mRefreshRateType = newRefreshRateType;
Ady Abraham6fe2c172019-07-12 12:37:57 -0700526 if (eventOnContentDetection &&
527 mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_ON) {
Ady Abraham8532d012019-05-08 14:50:56 -0700528 event = ConfigEvent::Changed;
529 }
530 }
531 changeRefreshRate(newRefreshRateType, event);
532}
533
Ady Abraham09bd3922019-04-08 10:44:56 -0700534Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
Ady Abraham8532d012019-05-08 14:50:56 -0700535 // HDR content is not supported on PERFORMANCE mode
536 if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700537 return RefreshRateType::DEFAULT;
538 }
539
Ady Abraham6fe2c172019-07-12 12:37:57 -0700540 // If Display Power is not in normal operation we want to be in performance mode.
541 // When coming back to normal mode, a grace period is given with DisplayPowerTimer
542 if (!mIsDisplayPowerStateNormal || mDisplayPowerTimerState == DisplayPowerTimerState::RESET) {
543 return RefreshRateType::PERFORMANCE;
544 }
545
Ady Abraham8532d012019-05-08 14:50:56 -0700546 // As long as touch is active we want to be in performance mode
547 if (mCurrentTouchState == TouchState::ACTIVE) {
548 return RefreshRateType::PERFORMANCE;
549 }
550
551 // If timer has expired as it means there is no new content on the screen
552 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
Ady Abrahama315ce72019-04-24 14:35:20 -0700553 return RefreshRateType::DEFAULT;
554 }
555
Ady Abraham09bd3922019-04-08 10:44:56 -0700556 // If content detection is off we choose performance as we don't know the content fps
557 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
558 return RefreshRateType::PERFORMANCE;
559 }
560
Wei Wang09be73f2019-07-02 14:29:18 -0700561 // Content detection is on, find the appropriate refresh rate with minimal error
562 auto iter = min_element(mRefreshRateConfigs.getRefreshRates().cbegin(),
563 mRefreshRateConfigs.getRefreshRates().cend(),
564 [rate = mContentRefreshRate](const auto& l, const auto& r) -> bool {
565 return std::abs(l.second->fps - static_cast<float>(rate)) <
566 std::abs(r.second->fps - static_cast<float>(rate));
567 });
568 RefreshRateType currRefreshRateType = iter->first;
Ady Abraham09bd3922019-04-08 10:44:56 -0700569
Ady Abraham85b3f012019-04-08 11:04:14 -0700570 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
571 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
572 // align well with both
Wei Wang09be73f2019-07-02 14:29:18 -0700573 constexpr float MARGIN = 0.05f;
Ady Abraham85b3f012019-04-08 11:04:14 -0700574 float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps /
575 float(mContentRefreshRate);
576 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
577 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
578 ratio = iter->second->fps / float(mContentRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -0700579
Ady Abraham85b3f012019-04-08 11:04:14 -0700580 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
581 currRefreshRateType = iter->first;
582 break;
583 }
584 ++iter;
585 }
586 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700587
588 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800589}
590
591void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
592 std::lock_guard<std::mutex> lock(mCallbackLock);
593 if (mChangeRefreshRateCallback) {
594 mChangeRefreshRateCallback(refreshRateType, configEvent);
595 }
596}
597
Ana Krulec98b5b242018-08-10 15:03:23 -0700598} // namespace android