blob: 63a385561e7f93cce0f50cdd6f4761940cd6c275 [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,
136 ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -0700137 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
138 const int64_t id = sNextId++;
139 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
140
Ana Krulec98b5b242018-08-10 15:03:23 -0700141 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -0800142 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Ady Abraham45e4e362019-06-07 18:20:51 -0700143 offsetThresholdForNextVsync, std::move(interceptCallback));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800144
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800145 auto eventThreadConnection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700146 createConnectionInternal(eventThread.get(), std::move(resyncCallback),
147 ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800148 mConnections.emplace(id,
149 std::make_unique<Connection>(new ConnectionHandle(id),
150 eventThreadConnection,
151 std::move(eventThread)));
Ana Krulec98b5b242018-08-10 15:03:23 -0700152 return mConnections[id]->handle;
153}
154
Ana Krulec0c8cd522018-08-31 12:27:28 -0700155std::unique_ptr<EventThread> Scheduler::makeEventThread(
Ady Abraham45e4e362019-06-07 18:20:51 -0700156 const char* connectionName, DispSync* dispSync, nsecs_t phaseOffsetNs,
157 nsecs_t offsetThresholdForNextVsync,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700158 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
159 std::unique_ptr<VSyncSource> eventThreadSource =
Ady Abraham45e4e362019-06-07 18:20:51 -0700160 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, offsetThresholdForNextVsync,
161 true, connectionName);
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800162 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800163 std::move(interceptCallback), connectionName);
164}
165
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700166sp<EventThreadConnection> Scheduler::createConnectionInternal(
167 EventThread* eventThread, ResyncCallback&& resyncCallback,
168 ISurfaceComposer::ConfigChanged configChanged) {
169 return eventThread->createEventConnection(std::move(resyncCallback), configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700170}
171
Ana Krulec98b5b242018-08-10 15:03:23 -0700172sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700173 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback,
174 ISurfaceComposer::ConfigChanged configChanged) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700175 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800176 return createConnectionInternal(mConnections[handle->id]->thread.get(),
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700177 std::move(resyncCallback), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700178}
179
180EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700181 RETURN_VALUE_IF_INVALID(nullptr);
182 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700183}
184
Ana Krulec85c39af2018-12-26 17:29:57 -0800185sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700186 RETURN_VALUE_IF_INVALID(nullptr);
187 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700188}
189
190void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800191 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700192 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800193 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700194}
195
196void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700197 RETURN_IF_INVALID();
198 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700199}
200
201void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700202 RETURN_IF_INVALID();
203 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700204}
205
Ady Abraham447052e2019-02-13 16:07:27 -0800206void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
207 int32_t configId) {
208 RETURN_IF_INVALID();
209 mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
210}
211
Yiwei Zhang5434a782018-12-05 18:06:32 -0800212void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700213 RETURN_IF_INVALID();
214 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700215}
216
217void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700218 RETURN_IF_INVALID();
219 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700220}
Ana Krulece588e312018-09-18 12:32:24 -0700221
222void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
223 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
224 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
225}
226
227void Scheduler::enableHardwareVsync() {
228 std::lock_guard<std::mutex> lock(mHWVsyncLock);
229 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
230 mPrimaryDispSync->beginResync();
231 mEventControlThread->setVsyncEnabled(true);
232 mPrimaryHWVsyncEnabled = true;
233 }
234}
235
236void Scheduler::disableHardwareVsync(bool makeUnavailable) {
237 std::lock_guard<std::mutex> lock(mHWVsyncLock);
238 if (mPrimaryHWVsyncEnabled) {
239 mEventControlThread->setVsyncEnabled(false);
240 mPrimaryDispSync->endResync();
241 mPrimaryHWVsyncEnabled = false;
242 }
243 if (makeUnavailable) {
244 mHWVsyncAvailable = false;
245 }
246}
247
Ana Krulecc2870422019-01-29 19:00:58 -0800248void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
249 {
250 std::lock_guard<std::mutex> lock(mHWVsyncLock);
251 if (makeAvailable) {
252 mHWVsyncAvailable = makeAvailable;
253 } else if (!mHWVsyncAvailable) {
254 // Hardware vsync is not currently available, so abort the resync
255 // attempt for now
256 return;
257 }
258 }
259
260 if (period <= 0) {
261 return;
262 }
263
264 setVsyncPeriod(period);
265}
266
267ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
268 std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
269 return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
270 if (const auto vsync = ptr.lock()) {
271 vsync->resync(getVsyncPeriod);
272 }
273 };
274}
275
276void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
Long Ling457bef92019-09-11 14:43:11 -0700277 static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
Ana Krulecc2870422019-01-29 19:00:58 -0800278
279 const nsecs_t now = systemTime();
280 const nsecs_t last = lastResyncTime.exchange(now);
281
282 if (now - last > kIgnoreDelay) {
283 scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
284 }
285}
286
287void Scheduler::setRefreshSkipCount(int count) {
288 mPrimaryDispSync->setRefreshSkipCount(count);
289}
290
Ana Krulece588e312018-09-18 12:32:24 -0700291void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800292 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700293 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800294
295 if (!mPrimaryHWVsyncEnabled) {
296 mPrimaryDispSync->beginResync();
297 mEventControlThread->setVsyncEnabled(true);
298 mPrimaryHWVsyncEnabled = true;
299 }
Ana Krulece588e312018-09-18 12:32:24 -0700300}
301
Alec Mourif8e689c2019-05-20 18:32:22 -0700302void Scheduler::addResyncSample(const nsecs_t timestamp, bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700303 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700304 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700305 { // Scope for the lock
306 std::lock_guard<std::mutex> lock(mHWVsyncLock);
307 if (mPrimaryHWVsyncEnabled) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700308 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700309 }
310 }
311
312 if (needsHwVsync) {
313 enableHardwareVsync();
314 } else {
315 disableHardwareVsync(false);
316 }
317}
318
319void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
320 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
321 enableHardwareVsync();
322 } else {
323 disableHardwareVsync(false);
324 }
325}
326
327void Scheduler::setIgnorePresentFences(bool ignore) {
328 mPrimaryDispSync->setIgnorePresentFences(ignore);
329}
330
Ady Abraham8fe11022019-06-12 17:11:12 -0700331nsecs_t Scheduler::getDispSyncExpectedPresentTime() {
Ady Abrahamc3e21312019-02-07 14:30:23 -0800332 return mPrimaryDispSync->expectedPresentTime();
333}
334
Ady Abraham3aff9172019-02-07 19:10:26 -0800335void Scheduler::dumpPrimaryDispSync(std::string& result) const {
336 mPrimaryDispSync->dump(result);
337}
338
Ady Abraham09bd3922019-04-08 10:44:56 -0700339std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer(
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700340 std::string const& name, int windowType) {
341 RefreshRateType refreshRateType = (windowType == InputWindowInfo::TYPE_WALLPAPER)
342 ? RefreshRateType::DEFAULT
343 : RefreshRateType::PERFORMANCE;
Ady Abraham09bd3922019-04-08 10:44:56 -0700344
345 const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType);
Ana Krulecad083c42019-06-26 16:28:08 -0700346 const uint32_t performanceFps = (refreshRate) ? refreshRate->fps : 0;
347
348 const auto defaultRefreshRate = mRefreshRateConfigs.getRefreshRate(RefreshRateType::DEFAULT);
349 const uint32_t defaultFps = (defaultRefreshRate) ? defaultRefreshRate->fps : 0;
350 return mLayerHistory.createLayer(name, defaultFps, performanceFps);
Ady Abraham09bd3922019-04-08 10:44:56 -0700351}
352
Ady Abrahama315ce72019-04-24 14:35:20 -0700353void Scheduler::addLayerPresentTimeAndHDR(
Ady Abraham09bd3922019-04-08 10:44:56 -0700354 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
Ady Abrahama315ce72019-04-24 14:35:20 -0700355 nsecs_t presentTime, bool isHDR) {
356 mLayerHistory.insert(layerHandle, presentTime, isHDR);
357}
358
359void Scheduler::setLayerVisibility(
360 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, bool visible) {
361 mLayerHistory.setVisibility(layerHandle, visible);
Ana Krulec3084c052018-11-21 20:27:17 +0100362}
363
Kevin DuBois413287f2019-02-25 08:46:47 -0800364void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
365 fn(*mPrimaryDispSync);
366}
367
Ady Abraham09bd3922019-04-08 10:44:56 -0700368void Scheduler::updateFpsBasedOnContent() {
Ady Abrahama315ce72019-04-24 14:35:20 -0700369 auto [refreshRate, isHDR] = mLayerHistory.getDesiredRefreshRateAndHDR();
370 const uint32_t refreshRateRound = std::round(refreshRate);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700371 RefreshRateType newRefreshRateType;
372 {
373 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abrahama315ce72019-04-24 14:35:20 -0700374 if (mContentRefreshRate == refreshRateRound && mIsHDRContent == isHDR) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700375 return;
376 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700377 mContentRefreshRate = refreshRateRound;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700378 ATRACE_INT("ContentFPS", mContentRefreshRate);
379
Ady Abrahama315ce72019-04-24 14:35:20 -0700380 mIsHDRContent = isHDR;
381 ATRACE_INT("ContentHDR", mIsHDRContent);
382
383 mCurrentContentFeatureState = refreshRateRound > 0
384 ? ContentFeatureState::CONTENT_DETECTION_ON
385 : ContentFeatureState::CONTENT_DETECTION_OFF;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700386 newRefreshRateType = calculateRefreshRateType();
387 if (mRefreshRateType == newRefreshRateType) {
388 return;
389 }
390 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800391 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700392 changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
Ana Krulec3084c052018-11-21 20:27:17 +0100393}
394
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800395void Scheduler::setChangeRefreshRateCallback(
Alec Mouri7f015182019-07-11 13:56:22 -0700396 const ChangeRefreshRateCallback&& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800397 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800398 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800399}
400
Alec Mouri7f015182019-07-11 13:56:22 -0700401void Scheduler::setGetCurrentRefreshRateTypeCallback(
402 const GetCurrentRefreshRateTypeCallback&& getCurrentRefreshRateTypeCallback) {
403 std::lock_guard<std::mutex> lock(mCallbackLock);
404 mGetCurrentRefreshRateTypeCallback = getCurrentRefreshRateTypeCallback;
405}
406
Alec Mouridc28b372019-04-18 21:17:13 -0700407void Scheduler::setGetVsyncPeriodCallback(const GetVsyncPeriod&& getVsyncPeriod) {
408 std::lock_guard<std::mutex> lock(mCallbackLock);
409 mGetVsyncPeriod = getVsyncPeriod;
410}
411
Ana Krulec3084c052018-11-21 20:27:17 +0100412void Scheduler::updateFrameSkipping(const int64_t skipCount) {
413 ATRACE_INT("FrameSkipCount", skipCount);
414 if (mSkipCount != skipCount) {
415 // Only update DispSync if it hasn't been updated yet.
416 mPrimaryDispSync->setRefreshSkipCount(skipCount);
417 mSkipCount = skipCount;
418 }
419}
420
Ana Krulecfb772822018-11-30 10:44:07 +0100421void Scheduler::resetIdleTimer() {
422 if (mIdleTimer) {
423 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800424 }
425}
426
Ady Abraham8532d012019-05-08 14:50:56 -0700427void Scheduler::notifyTouchEvent() {
428 if (mTouchTimer) {
429 mTouchTimer->reset();
430 }
431
432 if (mSupportKernelTimer) {
433 resetIdleTimer();
434 }
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700435
436 // Touch event will boost the refresh rate to performance.
437 // Clear Layer History to get fresh FPS detection
438 mLayerHistory.clearHistory();
Ady Abraham8532d012019-05-08 14:50:56 -0700439}
440
Ady Abraham24363172019-07-12 12:37:57 -0700441void Scheduler::setDisplayPowerState(bool normal) {
442 {
443 std::lock_guard<std::mutex> lock(mFeatureStateLock);
444 mIsDisplayPowerStateNormal = normal;
445 }
446
447 if (mDisplayPowerTimer) {
448 mDisplayPowerTimer->reset();
449 }
450
451 // Display Power event will boost the refresh rate to performance.
452 // Clear Layer History to get fresh FPS detection
453 mLayerHistory.clearHistory();
454}
455
Ady Abrahama1a49af2019-02-07 14:36:55 -0800456void Scheduler::resetTimerCallback() {
Ady Abraham24363172019-07-12 12:37:57 -0700457 handleTimerStateChanged(&mCurrentIdleTimerState, IdleTimerState::RESET, false);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800458 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100459}
460
Alec Mouridc28b372019-04-18 21:17:13 -0700461void Scheduler::resetKernelTimerCallback() {
462 ATRACE_INT("ExpiredKernelIdleTimer", 0);
463 std::lock_guard<std::mutex> lock(mCallbackLock);
Alec Mouri7f015182019-07-11 13:56:22 -0700464 if (mGetVsyncPeriod && mGetCurrentRefreshRateTypeCallback) {
465 // If we're not in performance mode then the kernel timer shouldn't do
466 // anything, as the refresh rate during DPU power collapse will be the
467 // same.
468 if (mGetCurrentRefreshRateTypeCallback() == Scheduler::RefreshRateType::PERFORMANCE) {
469 resyncToHardwareVsync(true, mGetVsyncPeriod());
470 }
Alec Mouridc28b372019-04-18 21:17:13 -0700471 }
472}
473
Ana Krulecfb772822018-11-30 10:44:07 +0100474void Scheduler::expiredTimerCallback() {
Ady Abraham24363172019-07-12 12:37:57 -0700475 handleTimerStateChanged(&mCurrentIdleTimerState, IdleTimerState::EXPIRED, false);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800476 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100477}
478
Ady Abraham8532d012019-05-08 14:50:56 -0700479void Scheduler::resetTouchTimerCallback() {
Ady Abraham24363172019-07-12 12:37:57 -0700480 handleTimerStateChanged(&mCurrentTouchState, TouchState::ACTIVE, true);
Ady Abraham8532d012019-05-08 14:50:56 -0700481 ATRACE_INT("TouchState", 1);
482}
483
484void Scheduler::expiredTouchTimerCallback() {
Ady Abraham24363172019-07-12 12:37:57 -0700485 handleTimerStateChanged(&mCurrentTouchState, TouchState::INACTIVE, true);
Ady Abraham8532d012019-05-08 14:50:56 -0700486 ATRACE_INT("TouchState", 0);
487}
488
Ady Abraham24363172019-07-12 12:37:57 -0700489void Scheduler::resetDisplayPowerTimerCallback() {
490 handleTimerStateChanged(&mDisplayPowerTimerState, DisplayPowerTimerState::RESET, true);
491 ATRACE_INT("ExpiredDisplayPowerTimer", 0);
492}
493
494void Scheduler::expiredDisplayPowerTimerCallback() {
495 handleTimerStateChanged(&mDisplayPowerTimerState, DisplayPowerTimerState::EXPIRED, true);
496 ATRACE_INT("ExpiredDisplayPowerTimer", 1);
497}
498
Alec Mouridc28b372019-04-18 21:17:13 -0700499void Scheduler::expiredKernelTimerCallback() {
Alec Mouri7f015182019-07-11 13:56:22 -0700500 std::lock_guard<std::mutex> lock(mCallbackLock);
Alec Mouridc28b372019-04-18 21:17:13 -0700501 ATRACE_INT("ExpiredKernelIdleTimer", 1);
Alec Mouri7f015182019-07-11 13:56:22 -0700502 if (mGetCurrentRefreshRateTypeCallback) {
503 if (mGetCurrentRefreshRateTypeCallback() != Scheduler::RefreshRateType::PERFORMANCE) {
504 // Disable HW Vsync if the timer expired, as we don't need it
505 // enabled if we're not pushing frames, and if we're in PERFORMANCE
506 // mode then we'll need to re-update the DispSync model anyways.
507 disableHardwareVsync(false);
508 }
509 }
Alec Mouridc28b372019-04-18 21:17:13 -0700510}
511
Ana Krulecb43429d2019-01-09 14:28:51 -0800512std::string Scheduler::doDump() {
513 std::ostringstream stream;
514 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
Ady Abraham8532d012019-05-08 14:50:56 -0700515 stream << "+ Touch timer interval: " << mSetTouchTimerMs << " ms" << std::endl;
Ana Krulecb43429d2019-01-09 14:28:51 -0800516 return stream.str();
517}
518
Ady Abraham24363172019-07-12 12:37:57 -0700519template <class T>
520void Scheduler::handleTimerStateChanged(T* currentState, T newState, bool eventOnContentDetection) {
Ady Abraham8532d012019-05-08 14:50:56 -0700521 ConfigEvent event = ConfigEvent::None;
522 RefreshRateType newRefreshRateType;
523 {
524 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham24363172019-07-12 12:37:57 -0700525 if (*currentState == newState) {
Ady Abraham8532d012019-05-08 14:50:56 -0700526 return;
527 }
Ady Abraham24363172019-07-12 12:37:57 -0700528 *currentState = newState;
Ady Abraham8532d012019-05-08 14:50:56 -0700529 newRefreshRateType = calculateRefreshRateType();
530 if (mRefreshRateType == newRefreshRateType) {
531 return;
532 }
533 mRefreshRateType = newRefreshRateType;
Ady Abraham24363172019-07-12 12:37:57 -0700534 if (eventOnContentDetection &&
535 mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_ON) {
Ady Abraham8532d012019-05-08 14:50:56 -0700536 event = ConfigEvent::Changed;
537 }
538 }
539 changeRefreshRate(newRefreshRateType, event);
540}
541
Ady Abraham09bd3922019-04-08 10:44:56 -0700542Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
Ady Abraham8532d012019-05-08 14:50:56 -0700543 // HDR content is not supported on PERFORMANCE mode
544 if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700545 return RefreshRateType::DEFAULT;
546 }
547
Ady Abraham24363172019-07-12 12:37:57 -0700548 // If Display Power is not in normal operation we want to be in performance mode.
549 // When coming back to normal mode, a grace period is given with DisplayPowerTimer
550 if (!mIsDisplayPowerStateNormal || mDisplayPowerTimerState == DisplayPowerTimerState::RESET) {
551 return RefreshRateType::PERFORMANCE;
552 }
553
Ady Abraham8532d012019-05-08 14:50:56 -0700554 // As long as touch is active we want to be in performance mode
555 if (mCurrentTouchState == TouchState::ACTIVE) {
556 return RefreshRateType::PERFORMANCE;
557 }
558
559 // If timer has expired as it means there is no new content on the screen
560 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
Ady Abrahama315ce72019-04-24 14:35:20 -0700561 return RefreshRateType::DEFAULT;
562 }
563
Ady Abraham09bd3922019-04-08 10:44:56 -0700564 // If content detection is off we choose performance as we don't know the content fps
565 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
566 return RefreshRateType::PERFORMANCE;
567 }
568
Wei Wang09be73f2019-07-02 14:29:18 -0700569 // Content detection is on, find the appropriate refresh rate with minimal error
570 auto iter = min_element(mRefreshRateConfigs.getRefreshRates().cbegin(),
571 mRefreshRateConfigs.getRefreshRates().cend(),
572 [rate = mContentRefreshRate](const auto& l, const auto& r) -> bool {
573 return std::abs(l.second->fps - static_cast<float>(rate)) <
574 std::abs(r.second->fps - static_cast<float>(rate));
575 });
576 RefreshRateType currRefreshRateType = iter->first;
Ady Abraham09bd3922019-04-08 10:44:56 -0700577
Ady Abraham85b3f012019-04-08 11:04:14 -0700578 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
579 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
580 // align well with both
Wei Wang09be73f2019-07-02 14:29:18 -0700581 constexpr float MARGIN = 0.05f;
Ady Abraham85b3f012019-04-08 11:04:14 -0700582 float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps /
583 float(mContentRefreshRate);
584 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
585 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
586 ratio = iter->second->fps / float(mContentRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -0700587
Ady Abraham85b3f012019-04-08 11:04:14 -0700588 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
589 currRefreshRateType = iter->first;
590 break;
591 }
592 ++iter;
593 }
594 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700595
596 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800597}
598
599void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
600 std::lock_guard<std::mutex> lock(mCallbackLock);
601 if (mChangeRefreshRateCallback) {
602 mChangeRefreshRateCallback(refreshRateType, configEvent);
603 }
604}
605
Ana Krulec98b5b242018-08-10 15:03:23 -0700606} // namespace android