blob: b2c069e37d2fa1dc8be883e9ce069dd7b0bef9b4 [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"
Ana Krulecfb772822018-11-30 10:44:07 +010041#include "IdleTimer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070042#include "InjectVSyncSource.h"
Ady Abraham09bd3922019-04-08 10:44:56 -070043#include "LayerInfo.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 Abraham24363172019-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) {
90 mIdleTimer =
91 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(
92 mSetIdleTimerMs),
93 [this] { resetKernelTimerCallback(); },
94 [this] {
95 expiredKernelTimerCallback();
96 });
97 } else {
98 mIdleTimer = std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(
99 mSetIdleTimerMs),
100 [this] { resetTimerCallback(); },
101 [this] { expiredTimerCallback(); });
102 }
Ana Krulecfb772822018-11-30 10:44:07 +0100103 mIdleTimer->start();
104 }
Ady Abraham8532d012019-05-08 14:50:56 -0700105
106 if (mSetTouchTimerMs > 0) {
107 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
108 mTouchTimer =
109 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetTouchTimerMs),
110 [this] { resetTouchTimerCallback(); },
111 [this] { expiredTouchTimerCallback(); });
112 mTouchTimer->start();
113 }
Ady Abraham24363172019-07-12 12:37:57 -0700114
115 if (mSetDisplayPowerTimerMs > 0) {
116 mDisplayPowerTimer =
117 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(
118 mSetDisplayPowerTimerMs),
119 [this] { resetDisplayPowerTimerCallback(); },
120 [this] {
121 expiredDisplayPowerTimerCallback();
122 });
123 mDisplayPowerTimer->start();
124 }
Ana Krulece588e312018-09-18 12:32:24 -0700125}
126
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800127Scheduler::~Scheduler() {
128 // Ensure the IdleTimer thread is joined before we start destroying state.
Ady Abraham24363172019-07-12 12:37:57 -0700129 mDisplayPowerTimer.reset();
Ady Abraham8532d012019-05-08 14:50:56 -0700130 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800131 mIdleTimer.reset();
132}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700133
Ana Krulec98b5b242018-08-10 15:03:23 -0700134sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Ady Abraham45e4e362019-06-07 18:20:51 -0700135 const char* connectionName, nsecs_t phaseOffsetNs, nsecs_t offsetThresholdForNextVsync,
Ana Krulec98b5b242018-08-10 15:03:23 -0700136 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
137 const int64_t id = sNextId++;
138 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
139
Ana Krulec98b5b242018-08-10 15:03:23 -0700140 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -0800141 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Ady Abraham45e4e362019-06-07 18:20:51 -0700142 offsetThresholdForNextVsync, std::move(interceptCallback));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800143
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800144 auto eventThreadConnection =
Steven Thomase9eb1832019-08-28 16:08:35 -0700145 createConnectionInternal(eventThread.get(), ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800146 mConnections.emplace(id,
147 std::make_unique<Connection>(new ConnectionHandle(id),
148 eventThreadConnection,
149 std::move(eventThread)));
Ana Krulec98b5b242018-08-10 15:03:23 -0700150 return mConnections[id]->handle;
151}
152
Ana Krulec0c8cd522018-08-31 12:27:28 -0700153std::unique_ptr<EventThread> Scheduler::makeEventThread(
Ady Abraham45e4e362019-06-07 18:20:51 -0700154 const char* connectionName, DispSync* dispSync, nsecs_t phaseOffsetNs,
155 nsecs_t offsetThresholdForNextVsync,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700156 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
157 std::unique_ptr<VSyncSource> eventThreadSource =
Ady Abraham45e4e362019-06-07 18:20:51 -0700158 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, offsetThresholdForNextVsync,
159 true, connectionName);
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800160 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800161 std::move(interceptCallback), connectionName);
162}
163
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700164sp<EventThreadConnection> Scheduler::createConnectionInternal(
Steven Thomase9eb1832019-08-28 16:08:35 -0700165 EventThread* eventThread, ISurfaceComposer::ConfigChanged configChanged) {
166 return eventThread->createEventConnection([&] { resync(); }, configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700167}
168
Ana Krulec98b5b242018-08-10 15:03:23 -0700169sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Steven Thomase9eb1832019-08-28 16:08:35 -0700170 const sp<Scheduler::ConnectionHandle>& handle,
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700171 ISurfaceComposer::ConfigChanged configChanged) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700172 RETURN_VALUE_IF_INVALID(nullptr);
Steven Thomase9eb1832019-08-28 16:08:35 -0700173 return createConnectionInternal(mConnections[handle->id]->thread.get(), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700174}
175
176EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700177 RETURN_VALUE_IF_INVALID(nullptr);
178 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700179}
180
Ana Krulec85c39af2018-12-26 17:29:57 -0800181sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700182 RETURN_VALUE_IF_INVALID(nullptr);
183 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700184}
185
186void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800187 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700188 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800189 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700190}
191
192void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700193 RETURN_IF_INVALID();
194 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700195}
196
197void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700198 RETURN_IF_INVALID();
199 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700200}
201
Ady Abraham447052e2019-02-13 16:07:27 -0800202void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
203 int32_t configId) {
204 RETURN_IF_INVALID();
205 mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
206}
207
Yiwei Zhang5434a782018-12-05 18:06:32 -0800208void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700209 RETURN_IF_INVALID();
210 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700211}
212
213void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700214 RETURN_IF_INVALID();
215 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700216}
Ana Krulece588e312018-09-18 12:32:24 -0700217
218void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
219 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
220 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
221}
222
223void Scheduler::enableHardwareVsync() {
224 std::lock_guard<std::mutex> lock(mHWVsyncLock);
225 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
226 mPrimaryDispSync->beginResync();
227 mEventControlThread->setVsyncEnabled(true);
228 mPrimaryHWVsyncEnabled = true;
229 }
230}
231
232void Scheduler::disableHardwareVsync(bool makeUnavailable) {
233 std::lock_guard<std::mutex> lock(mHWVsyncLock);
234 if (mPrimaryHWVsyncEnabled) {
235 mEventControlThread->setVsyncEnabled(false);
236 mPrimaryDispSync->endResync();
237 mPrimaryHWVsyncEnabled = false;
238 }
239 if (makeUnavailable) {
240 mHWVsyncAvailable = false;
241 }
242}
243
Ana Krulecc2870422019-01-29 19:00:58 -0800244void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
245 {
246 std::lock_guard<std::mutex> lock(mHWVsyncLock);
247 if (makeAvailable) {
248 mHWVsyncAvailable = makeAvailable;
249 } else if (!mHWVsyncAvailable) {
250 // Hardware vsync is not currently available, so abort the resync
251 // attempt for now
252 return;
253 }
254 }
255
256 if (period <= 0) {
257 return;
258 }
259
260 setVsyncPeriod(period);
261}
262
Steven Thomase9eb1832019-08-28 16:08:35 -0700263void Scheduler::resync() {
Long Ling457bef92019-09-11 14:43:11 -0700264 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800265
266 const nsecs_t now = systemTime();
Steven Thomase9eb1832019-08-28 16:08:35 -0700267 const nsecs_t last = mLastResyncTime.exchange(now);
Ana Krulecc2870422019-01-29 19:00:58 -0800268
269 if (now - last > kIgnoreDelay) {
Steven Thomase9eb1832019-08-28 16:08:35 -0700270 resyncToHardwareVsync(false,
271 mRefreshRateConfigs.getCurrentRefreshRate().second.vsyncPeriod);
Ana Krulecc2870422019-01-29 19:00:58 -0800272 }
273}
274
275void Scheduler::setRefreshSkipCount(int count) {
276 mPrimaryDispSync->setRefreshSkipCount(count);
277}
278
Ana Krulece588e312018-09-18 12:32:24 -0700279void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800280 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700281 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800282
283 if (!mPrimaryHWVsyncEnabled) {
284 mPrimaryDispSync->beginResync();
285 mEventControlThread->setVsyncEnabled(true);
286 mPrimaryHWVsyncEnabled = true;
287 }
Ana Krulece588e312018-09-18 12:32:24 -0700288}
289
Alec Mourif8e689c2019-05-20 18:32:22 -0700290void Scheduler::addResyncSample(const nsecs_t timestamp, bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700291 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700292 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700293 { // Scope for the lock
294 std::lock_guard<std::mutex> lock(mHWVsyncLock);
295 if (mPrimaryHWVsyncEnabled) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700296 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700297 }
298 }
299
300 if (needsHwVsync) {
301 enableHardwareVsync();
302 } else {
303 disableHardwareVsync(false);
304 }
305}
306
307void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
308 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
309 enableHardwareVsync();
310 } else {
311 disableHardwareVsync(false);
312 }
313}
314
315void Scheduler::setIgnorePresentFences(bool ignore) {
316 mPrimaryDispSync->setIgnorePresentFences(ignore);
317}
318
Ady Abraham8fe11022019-06-12 17:11:12 -0700319nsecs_t Scheduler::getDispSyncExpectedPresentTime() {
Ady Abrahamc3e21312019-02-07 14:30:23 -0800320 return mPrimaryDispSync->expectedPresentTime();
321}
322
Ady Abraham3aff9172019-02-07 19:10:26 -0800323void Scheduler::dumpPrimaryDispSync(std::string& result) const {
324 mPrimaryDispSync->dump(result);
325}
326
Ady Abraham09bd3922019-04-08 10:44:56 -0700327std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer(
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700328 std::string const& name, int windowType) {
Steven Thomase9eb1832019-08-28 16:08:35 -0700329 uint32_t defaultFps, performanceFps;
330 if (mRefreshRateConfigs.refreshRateSwitchingSupported()) {
331 defaultFps = mRefreshRateConfigs.getRefreshRateFromType(RefreshRateType::DEFAULT).fps;
332 performanceFps =
333 mRefreshRateConfigs
334 .getRefreshRateFromType((windowType == InputWindowInfo::TYPE_WALLPAPER)
335 ? RefreshRateType::DEFAULT
336 : RefreshRateType::PERFORMANCE)
337 .fps;
338 } else {
339 defaultFps = mRefreshRateConfigs.getCurrentRefreshRate().second.fps;
340 performanceFps = defaultFps;
341 }
Ana Krulecad083c42019-06-26 16:28:08 -0700342 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
Ana Krulec3084c052018-11-21 20:27:17 +0100393void Scheduler::updateFrameSkipping(const int64_t skipCount) {
394 ATRACE_INT("FrameSkipCount", skipCount);
395 if (mSkipCount != skipCount) {
396 // Only update DispSync if it hasn't been updated yet.
397 mPrimaryDispSync->setRefreshSkipCount(skipCount);
398 mSkipCount = skipCount;
399 }
400}
401
Ana Krulecfb772822018-11-30 10:44:07 +0100402void Scheduler::resetIdleTimer() {
403 if (mIdleTimer) {
404 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800405 }
406}
407
Ady Abraham8532d012019-05-08 14:50:56 -0700408void Scheduler::notifyTouchEvent() {
409 if (mTouchTimer) {
410 mTouchTimer->reset();
411 }
412
413 if (mSupportKernelTimer) {
414 resetIdleTimer();
415 }
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700416
417 // Touch event will boost the refresh rate to performance.
418 // Clear Layer History to get fresh FPS detection
419 mLayerHistory.clearHistory();
Ady Abraham8532d012019-05-08 14:50:56 -0700420}
421
Ady Abraham24363172019-07-12 12:37:57 -0700422void Scheduler::setDisplayPowerState(bool normal) {
423 {
424 std::lock_guard<std::mutex> lock(mFeatureStateLock);
425 mIsDisplayPowerStateNormal = normal;
426 }
427
428 if (mDisplayPowerTimer) {
429 mDisplayPowerTimer->reset();
430 }
431
432 // Display Power event will boost the refresh rate to performance.
433 // Clear Layer History to get fresh FPS detection
434 mLayerHistory.clearHistory();
435}
436
Ady Abrahama1a49af2019-02-07 14:36:55 -0800437void Scheduler::resetTimerCallback() {
Ady Abraham24363172019-07-12 12:37:57 -0700438 handleTimerStateChanged(&mCurrentIdleTimerState, IdleTimerState::RESET, false);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800439 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100440}
441
Alec Mouridc28b372019-04-18 21:17:13 -0700442void Scheduler::resetKernelTimerCallback() {
443 ATRACE_INT("ExpiredKernelIdleTimer", 0);
Steven Thomase9eb1832019-08-28 16:08:35 -0700444 const auto refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
445 if (refreshRate.first == RefreshRateType::PERFORMANCE) {
Alec Mouri7f015182019-07-11 13:56:22 -0700446 // If we're not in performance mode then the kernel timer shouldn't do
447 // anything, as the refresh rate during DPU power collapse will be the
448 // same.
Steven Thomase9eb1832019-08-28 16:08:35 -0700449 resyncToHardwareVsync(true, refreshRate.second.vsyncPeriod);
Alec Mouridc28b372019-04-18 21:17:13 -0700450 }
451}
452
Ana Krulecfb772822018-11-30 10:44:07 +0100453void Scheduler::expiredTimerCallback() {
Ady Abraham24363172019-07-12 12:37:57 -0700454 handleTimerStateChanged(&mCurrentIdleTimerState, IdleTimerState::EXPIRED, false);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800455 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100456}
457
Ady Abraham8532d012019-05-08 14:50:56 -0700458void Scheduler::resetTouchTimerCallback() {
Ady Abraham24363172019-07-12 12:37:57 -0700459 handleTimerStateChanged(&mCurrentTouchState, TouchState::ACTIVE, true);
Ady Abraham8532d012019-05-08 14:50:56 -0700460 ATRACE_INT("TouchState", 1);
461}
462
463void Scheduler::expiredTouchTimerCallback() {
Ady Abraham24363172019-07-12 12:37:57 -0700464 handleTimerStateChanged(&mCurrentTouchState, TouchState::INACTIVE, true);
Ady Abraham8532d012019-05-08 14:50:56 -0700465 ATRACE_INT("TouchState", 0);
466}
467
Ady Abraham24363172019-07-12 12:37:57 -0700468void Scheduler::resetDisplayPowerTimerCallback() {
469 handleTimerStateChanged(&mDisplayPowerTimerState, DisplayPowerTimerState::RESET, true);
470 ATRACE_INT("ExpiredDisplayPowerTimer", 0);
471}
472
473void Scheduler::expiredDisplayPowerTimerCallback() {
474 handleTimerStateChanged(&mDisplayPowerTimerState, DisplayPowerTimerState::EXPIRED, true);
475 ATRACE_INT("ExpiredDisplayPowerTimer", 1);
476}
477
Alec Mouridc28b372019-04-18 21:17:13 -0700478void Scheduler::expiredKernelTimerCallback() {
479 ATRACE_INT("ExpiredKernelIdleTimer", 1);
Steven Thomase9eb1832019-08-28 16:08:35 -0700480 const auto refreshRate = mRefreshRateConfigs.getCurrentRefreshRate();
481 if (refreshRate.first != RefreshRateType::PERFORMANCE) {
482 // Disable HW Vsync if the timer expired, as we don't need it
483 // enabled if we're not pushing frames, and if we're in PERFORMANCE
484 // mode then we'll need to re-update the DispSync model anyways.
485 disableHardwareVsync(false);
Alec Mouri7f015182019-07-11 13:56:22 -0700486 }
Alec Mouridc28b372019-04-18 21:17:13 -0700487}
488
Ana Krulecb43429d2019-01-09 14:28:51 -0800489std::string Scheduler::doDump() {
490 std::ostringstream stream;
491 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
Ady Abraham8532d012019-05-08 14:50:56 -0700492 stream << "+ Touch timer interval: " << mSetTouchTimerMs << " ms" << std::endl;
Ana Krulecb43429d2019-01-09 14:28:51 -0800493 return stream.str();
494}
495
Ady Abraham24363172019-07-12 12:37:57 -0700496template <class T>
497void Scheduler::handleTimerStateChanged(T* currentState, T newState, bool eventOnContentDetection) {
Ady Abraham8532d012019-05-08 14:50:56 -0700498 ConfigEvent event = ConfigEvent::None;
499 RefreshRateType newRefreshRateType;
500 {
501 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham24363172019-07-12 12:37:57 -0700502 if (*currentState == newState) {
Ady Abraham8532d012019-05-08 14:50:56 -0700503 return;
504 }
Ady Abraham24363172019-07-12 12:37:57 -0700505 *currentState = newState;
Ady Abraham8532d012019-05-08 14:50:56 -0700506 newRefreshRateType = calculateRefreshRateType();
507 if (mRefreshRateType == newRefreshRateType) {
508 return;
509 }
510 mRefreshRateType = newRefreshRateType;
Ady Abraham24363172019-07-12 12:37:57 -0700511 if (eventOnContentDetection &&
512 mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_ON) {
Ady Abraham8532d012019-05-08 14:50:56 -0700513 event = ConfigEvent::Changed;
514 }
515 }
516 changeRefreshRate(newRefreshRateType, event);
517}
518
Ady Abraham09bd3922019-04-08 10:44:56 -0700519Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
Steven Thomase9eb1832019-08-28 16:08:35 -0700520 if (!mRefreshRateConfigs.refreshRateSwitchingSupported()) {
521 return RefreshRateType::DEFAULT;
522 }
523
Ady Abraham8532d012019-05-08 14:50:56 -0700524 // HDR content is not supported on PERFORMANCE mode
525 if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700526 return RefreshRateType::DEFAULT;
527 }
528
Ady Abraham24363172019-07-12 12:37:57 -0700529 // If Display Power is not in normal operation we want to be in performance mode.
530 // When coming back to normal mode, a grace period is given with DisplayPowerTimer
531 if (!mIsDisplayPowerStateNormal || mDisplayPowerTimerState == DisplayPowerTimerState::RESET) {
532 return RefreshRateType::PERFORMANCE;
533 }
534
Ady Abraham8532d012019-05-08 14:50:56 -0700535 // As long as touch is active we want to be in performance mode
536 if (mCurrentTouchState == TouchState::ACTIVE) {
537 return RefreshRateType::PERFORMANCE;
538 }
539
540 // If timer has expired as it means there is no new content on the screen
541 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
Ady Abrahama315ce72019-04-24 14:35:20 -0700542 return RefreshRateType::DEFAULT;
543 }
544
Ady Abraham09bd3922019-04-08 10:44:56 -0700545 // If content detection is off we choose performance as we don't know the content fps
546 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
547 return RefreshRateType::PERFORMANCE;
548 }
549
Wei Wang09be73f2019-07-02 14:29:18 -0700550 // Content detection is on, find the appropriate refresh rate with minimal error
Steven Thomase9eb1832019-08-28 16:08:35 -0700551 auto begin = mRefreshRateConfigs.getRefreshRateMap().cbegin();
Ady Abraham7e5db1d2019-09-06 13:05:40 -0700552
Steven Thomase9eb1832019-08-28 16:08:35 -0700553 auto iter = min_element(begin, mRefreshRateConfigs.getRefreshRateMap().cend(),
Wei Wang09be73f2019-07-02 14:29:18 -0700554 [rate = mContentRefreshRate](const auto& l, const auto& r) -> bool {
Steven Thomase9eb1832019-08-28 16:08:35 -0700555 return std::abs(l.second.fps - static_cast<float>(rate)) <
556 std::abs(r.second.fps - static_cast<float>(rate));
Wei Wang09be73f2019-07-02 14:29:18 -0700557 });
558 RefreshRateType currRefreshRateType = iter->first;
Ady Abraham09bd3922019-04-08 10:44:56 -0700559
Ady Abraham85b3f012019-04-08 11:04:14 -0700560 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
561 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
562 // align well with both
Wei Wang09be73f2019-07-02 14:29:18 -0700563 constexpr float MARGIN = 0.05f;
Steven Thomase9eb1832019-08-28 16:08:35 -0700564 float ratio = mRefreshRateConfigs.getRefreshRateFromType(currRefreshRateType).fps /
Ady Abraham85b3f012019-04-08 11:04:14 -0700565 float(mContentRefreshRate);
566 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
Steven Thomase9eb1832019-08-28 16:08:35 -0700567 while (iter != mRefreshRateConfigs.getRefreshRateMap().cend()) {
568 ratio = iter->second.fps / float(mContentRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -0700569
Ady Abraham85b3f012019-04-08 11:04:14 -0700570 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
571 currRefreshRateType = iter->first;
572 break;
573 }
574 ++iter;
575 }
576 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700577
578 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800579}
580
581void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
582 std::lock_guard<std::mutex> lock(mCallbackLock);
583 if (mChangeRefreshRateCallback) {
584 mChangeRefreshRateCallback(refreshRateType, configEvent);
585 }
586}
587
Ana Krulec98b5b242018-08-10 15:03:23 -0700588} // namespace android