blob: bb24f73834aba173ed6e98a46eba9318f2632901 [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);
79
Ana Krulecfb772822018-11-30 10:44:07 +010080 char value[PROPERTY_VALUE_MAX];
Ana Kruleca5bdd9d2019-01-29 19:00:58 -080081 property_get("debug.sf.set_idle_timer_ms", value, "0");
Ady Abrahambe59c0d2019-03-05 13:01:13 -080082 int int_value = atoi(value);
83 if (int_value) {
84 mSetIdleTimerMs = atoi(value);
85 }
Ana Krulecfb772822018-11-30 10:44:07 +010086
87 if (mSetIdleTimerMs > 0) {
Alec Mouridc28b372019-04-18 21:17:13 -070088 if (mSupportKernelTimer) {
89 mIdleTimer =
90 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(
91 mSetIdleTimerMs),
92 [this] { resetKernelTimerCallback(); },
93 [this] {
94 expiredKernelTimerCallback();
95 });
96 } else {
97 mIdleTimer = std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(
98 mSetIdleTimerMs),
99 [this] { resetTimerCallback(); },
100 [this] { expiredTimerCallback(); });
101 }
Ana Krulecfb772822018-11-30 10:44:07 +0100102 mIdleTimer->start();
103 }
Ady Abraham8532d012019-05-08 14:50:56 -0700104
105 if (mSetTouchTimerMs > 0) {
106 // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
107 mTouchTimer =
108 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetTouchTimerMs),
109 [this] { resetTouchTimerCallback(); },
110 [this] { expiredTouchTimerCallback(); });
111 mTouchTimer->start();
112 }
Ana Krulece588e312018-09-18 12:32:24 -0700113}
114
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800115Scheduler::~Scheduler() {
116 // Ensure the IdleTimer thread is joined before we start destroying state.
Ady Abraham8532d012019-05-08 14:50:56 -0700117 mTouchTimer.reset();
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800118 mIdleTimer.reset();
119}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700120
Ana Krulec98b5b242018-08-10 15:03:23 -0700121sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Ady Abraham45e4e362019-06-07 18:20:51 -0700122 const char* connectionName, nsecs_t phaseOffsetNs, nsecs_t offsetThresholdForNextVsync,
123 ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -0700124 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
125 const int64_t id = sNextId++;
126 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
127
Ana Krulec98b5b242018-08-10 15:03:23 -0700128 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -0800129 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Ady Abraham45e4e362019-06-07 18:20:51 -0700130 offsetThresholdForNextVsync, std::move(interceptCallback));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800131
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800132 auto eventThreadConnection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700133 createConnectionInternal(eventThread.get(), std::move(resyncCallback),
134 ISurfaceComposer::eConfigChangedSuppress);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800135 mConnections.emplace(id,
136 std::make_unique<Connection>(new ConnectionHandle(id),
137 eventThreadConnection,
138 std::move(eventThread)));
Ana Krulec98b5b242018-08-10 15:03:23 -0700139 return mConnections[id]->handle;
140}
141
Ana Krulec0c8cd522018-08-31 12:27:28 -0700142std::unique_ptr<EventThread> Scheduler::makeEventThread(
Ady Abraham45e4e362019-06-07 18:20:51 -0700143 const char* connectionName, DispSync* dispSync, nsecs_t phaseOffsetNs,
144 nsecs_t offsetThresholdForNextVsync,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700145 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
146 std::unique_ptr<VSyncSource> eventThreadSource =
Ady Abraham45e4e362019-06-07 18:20:51 -0700147 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, offsetThresholdForNextVsync,
148 true, connectionName);
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800149 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800150 std::move(interceptCallback), connectionName);
151}
152
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700153sp<EventThreadConnection> Scheduler::createConnectionInternal(
154 EventThread* eventThread, ResyncCallback&& resyncCallback,
155 ISurfaceComposer::ConfigChanged configChanged) {
156 return eventThread->createEventConnection(std::move(resyncCallback), configChanged);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700157}
158
Ana Krulec98b5b242018-08-10 15:03:23 -0700159sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700160 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback,
161 ISurfaceComposer::ConfigChanged configChanged) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700162 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800163 return createConnectionInternal(mConnections[handle->id]->thread.get(),
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700164 std::move(resyncCallback), configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -0700165}
166
167EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700168 RETURN_VALUE_IF_INVALID(nullptr);
169 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700170}
171
Ana Krulec85c39af2018-12-26 17:29:57 -0800172sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700173 RETURN_VALUE_IF_INVALID(nullptr);
174 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700175}
176
177void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800178 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700179 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800180 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700181}
182
183void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700184 RETURN_IF_INVALID();
185 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700186}
187
188void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700189 RETURN_IF_INVALID();
190 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700191}
192
Ady Abraham447052e2019-02-13 16:07:27 -0800193void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
194 int32_t configId) {
195 RETURN_IF_INVALID();
196 mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
197}
198
Yiwei Zhang5434a782018-12-05 18:06:32 -0800199void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700200 RETURN_IF_INVALID();
201 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700202}
203
204void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700205 RETURN_IF_INVALID();
206 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700207}
Ana Krulece588e312018-09-18 12:32:24 -0700208
209void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
210 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
211 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
212}
213
214void Scheduler::enableHardwareVsync() {
215 std::lock_guard<std::mutex> lock(mHWVsyncLock);
216 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
217 mPrimaryDispSync->beginResync();
218 mEventControlThread->setVsyncEnabled(true);
219 mPrimaryHWVsyncEnabled = true;
220 }
221}
222
223void Scheduler::disableHardwareVsync(bool makeUnavailable) {
224 std::lock_guard<std::mutex> lock(mHWVsyncLock);
225 if (mPrimaryHWVsyncEnabled) {
226 mEventControlThread->setVsyncEnabled(false);
227 mPrimaryDispSync->endResync();
228 mPrimaryHWVsyncEnabled = false;
229 }
230 if (makeUnavailable) {
231 mHWVsyncAvailable = false;
232 }
233}
234
Ana Krulecc2870422019-01-29 19:00:58 -0800235void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
236 {
237 std::lock_guard<std::mutex> lock(mHWVsyncLock);
238 if (makeAvailable) {
239 mHWVsyncAvailable = makeAvailable;
240 } else if (!mHWVsyncAvailable) {
241 // Hardware vsync is not currently available, so abort the resync
242 // attempt for now
243 return;
244 }
245 }
246
247 if (period <= 0) {
248 return;
249 }
250
251 setVsyncPeriod(period);
252}
253
254ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
255 std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
256 return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
257 if (const auto vsync = ptr.lock()) {
258 vsync->resync(getVsyncPeriod);
259 }
260 };
261}
262
263void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
264 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
265
266 const nsecs_t now = systemTime();
267 const nsecs_t last = lastResyncTime.exchange(now);
268
269 if (now - last > kIgnoreDelay) {
270 scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
271 }
272}
273
274void Scheduler::setRefreshSkipCount(int count) {
275 mPrimaryDispSync->setRefreshSkipCount(count);
276}
277
Ana Krulece588e312018-09-18 12:32:24 -0700278void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800279 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700280 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800281
282 if (!mPrimaryHWVsyncEnabled) {
283 mPrimaryDispSync->beginResync();
284 mEventControlThread->setVsyncEnabled(true);
285 mPrimaryHWVsyncEnabled = true;
286 }
Ana Krulece588e312018-09-18 12:32:24 -0700287}
288
Alec Mourif8e689c2019-05-20 18:32:22 -0700289void Scheduler::addResyncSample(const nsecs_t timestamp, bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700290 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700291 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700292 { // Scope for the lock
293 std::lock_guard<std::mutex> lock(mHWVsyncLock);
294 if (mPrimaryHWVsyncEnabled) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700295 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700296 }
297 }
298
299 if (needsHwVsync) {
300 enableHardwareVsync();
301 } else {
302 disableHardwareVsync(false);
303 }
304}
305
306void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
307 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
308 enableHardwareVsync();
309 } else {
310 disableHardwareVsync(false);
311 }
312}
313
314void Scheduler::setIgnorePresentFences(bool ignore) {
315 mPrimaryDispSync->setIgnorePresentFences(ignore);
316}
317
Ady Abraham8fe11022019-06-12 17:11:12 -0700318nsecs_t Scheduler::getDispSyncExpectedPresentTime() {
Ady Abrahamc3e21312019-02-07 14:30:23 -0800319 return mPrimaryDispSync->expectedPresentTime();
320}
321
Ady Abraham3aff9172019-02-07 19:10:26 -0800322void Scheduler::dumpPrimaryDispSync(std::string& result) const {
323 mPrimaryDispSync->dump(result);
324}
325
Ady Abraham09bd3922019-04-08 10:44:56 -0700326std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer(
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700327 std::string const& name, int windowType) {
328 RefreshRateType refreshRateType = (windowType == InputWindowInfo::TYPE_WALLPAPER)
329 ? RefreshRateType::DEFAULT
330 : RefreshRateType::PERFORMANCE;
Ady Abraham09bd3922019-04-08 10:44:56 -0700331
332 const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType);
333 const uint32_t fps = (refreshRate) ? refreshRate->fps : 0;
334 return mLayerHistory.createLayer(name, fps);
335}
336
Ady Abrahama315ce72019-04-24 14:35:20 -0700337void Scheduler::addLayerPresentTimeAndHDR(
Ady Abraham09bd3922019-04-08 10:44:56 -0700338 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
Ady Abrahama315ce72019-04-24 14:35:20 -0700339 nsecs_t presentTime, bool isHDR) {
340 mLayerHistory.insert(layerHandle, presentTime, isHDR);
341}
342
343void Scheduler::setLayerVisibility(
344 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, bool visible) {
345 mLayerHistory.setVisibility(layerHandle, visible);
Ana Krulec3084c052018-11-21 20:27:17 +0100346}
347
Kevin DuBois413287f2019-02-25 08:46:47 -0800348void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
349 fn(*mPrimaryDispSync);
350}
351
Ady Abraham09bd3922019-04-08 10:44:56 -0700352void Scheduler::updateFpsBasedOnContent() {
Ady Abrahama315ce72019-04-24 14:35:20 -0700353 auto [refreshRate, isHDR] = mLayerHistory.getDesiredRefreshRateAndHDR();
354 const uint32_t refreshRateRound = std::round(refreshRate);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700355 RefreshRateType newRefreshRateType;
356 {
357 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abrahama315ce72019-04-24 14:35:20 -0700358 if (mContentRefreshRate == refreshRateRound && mIsHDRContent == isHDR) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700359 return;
360 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700361 mContentRefreshRate = refreshRateRound;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700362 ATRACE_INT("ContentFPS", mContentRefreshRate);
363
Ady Abrahama315ce72019-04-24 14:35:20 -0700364 mIsHDRContent = isHDR;
365 ATRACE_INT("ContentHDR", mIsHDRContent);
366
367 mCurrentContentFeatureState = refreshRateRound > 0
368 ? ContentFeatureState::CONTENT_DETECTION_ON
369 : ContentFeatureState::CONTENT_DETECTION_OFF;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700370 newRefreshRateType = calculateRefreshRateType();
371 if (mRefreshRateType == newRefreshRateType) {
372 return;
373 }
374 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800375 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700376 changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
Ana Krulec3084c052018-11-21 20:27:17 +0100377}
378
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800379void Scheduler::setChangeRefreshRateCallback(
380 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800381 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800382 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800383}
384
Alec Mouridc28b372019-04-18 21:17:13 -0700385void Scheduler::setGetVsyncPeriodCallback(const GetVsyncPeriod&& getVsyncPeriod) {
386 std::lock_guard<std::mutex> lock(mCallbackLock);
387 mGetVsyncPeriod = getVsyncPeriod;
388}
389
Ana Krulec3084c052018-11-21 20:27:17 +0100390void Scheduler::updateFrameSkipping(const int64_t skipCount) {
391 ATRACE_INT("FrameSkipCount", skipCount);
392 if (mSkipCount != skipCount) {
393 // Only update DispSync if it hasn't been updated yet.
394 mPrimaryDispSync->setRefreshSkipCount(skipCount);
395 mSkipCount = skipCount;
396 }
397}
398
Ana Krulecfb772822018-11-30 10:44:07 +0100399void Scheduler::resetIdleTimer() {
400 if (mIdleTimer) {
401 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800402 }
403}
404
Ady Abraham8532d012019-05-08 14:50:56 -0700405void Scheduler::notifyTouchEvent() {
406 if (mTouchTimer) {
407 mTouchTimer->reset();
408 }
409
410 if (mSupportKernelTimer) {
411 resetIdleTimer();
412 }
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700413
414 // Touch event will boost the refresh rate to performance.
415 // Clear Layer History to get fresh FPS detection
416 mLayerHistory.clearHistory();
Ady Abraham8532d012019-05-08 14:50:56 -0700417}
418
Ady Abrahama1a49af2019-02-07 14:36:55 -0800419void Scheduler::resetTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800420 timerChangeRefreshRate(IdleTimerState::RESET);
421 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100422}
423
Alec Mouridc28b372019-04-18 21:17:13 -0700424void Scheduler::resetKernelTimerCallback() {
425 ATRACE_INT("ExpiredKernelIdleTimer", 0);
426 std::lock_guard<std::mutex> lock(mCallbackLock);
427 if (mGetVsyncPeriod) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700428 resyncToHardwareVsync(true, mGetVsyncPeriod());
Alec Mouridc28b372019-04-18 21:17:13 -0700429 }
430}
431
Ana Krulecfb772822018-11-30 10:44:07 +0100432void Scheduler::expiredTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800433 timerChangeRefreshRate(IdleTimerState::EXPIRED);
434 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100435}
436
Ady Abraham8532d012019-05-08 14:50:56 -0700437void Scheduler::resetTouchTimerCallback() {
438 // We do not notify the applications about config changes when idle timer is reset.
439 touchChangeRefreshRate(TouchState::ACTIVE);
440 ATRACE_INT("TouchState", 1);
441}
442
443void Scheduler::expiredTouchTimerCallback() {
444 // We do not notify the applications about config changes when idle timer expires.
445 touchChangeRefreshRate(TouchState::INACTIVE);
446 ATRACE_INT("TouchState", 0);
447}
448
Alec Mouridc28b372019-04-18 21:17:13 -0700449void Scheduler::expiredKernelTimerCallback() {
450 ATRACE_INT("ExpiredKernelIdleTimer", 1);
451 // Disable HW Vsync if the timer expired, as we don't need it
452 // enabled if we're not pushing frames.
453 disableHardwareVsync(false);
454}
455
Ana Krulecb43429d2019-01-09 14:28:51 -0800456std::string Scheduler::doDump() {
457 std::ostringstream stream;
458 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
Ady Abraham8532d012019-05-08 14:50:56 -0700459 stream << "+ Touch timer interval: " << mSetTouchTimerMs << " ms" << std::endl;
Ana Krulecb43429d2019-01-09 14:28:51 -0800460 return stream.str();
461}
462
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800463void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700464 RefreshRateType newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800465 {
466 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700467 if (mCurrentIdleTimerState == idleTimerState) {
468 return;
469 }
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800470 mCurrentIdleTimerState = idleTimerState;
Ady Abraham09bd3922019-04-08 10:44:56 -0700471 newRefreshRateType = calculateRefreshRateType();
Ady Abraham6398a0a2019-04-18 19:30:44 -0700472 if (mRefreshRateType == newRefreshRateType) {
473 return;
474 }
475 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800476 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700477 changeRefreshRate(newRefreshRateType, ConfigEvent::None);
478}
479
Ady Abraham8532d012019-05-08 14:50:56 -0700480void Scheduler::touchChangeRefreshRate(TouchState touchState) {
481 ConfigEvent event = ConfigEvent::None;
482 RefreshRateType newRefreshRateType;
483 {
484 std::lock_guard<std::mutex> lock(mFeatureStateLock);
485 if (mCurrentTouchState == touchState) {
486 return;
487 }
488 mCurrentTouchState = touchState;
489 newRefreshRateType = calculateRefreshRateType();
490 if (mRefreshRateType == newRefreshRateType) {
491 return;
492 }
493 mRefreshRateType = newRefreshRateType;
494 // Send an event in case that content detection is on as touch has a higher priority
495 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_ON) {
496 event = ConfigEvent::Changed;
497 }
498 }
499 changeRefreshRate(newRefreshRateType, event);
500}
501
Ady Abraham09bd3922019-04-08 10:44:56 -0700502Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
Ady Abraham8532d012019-05-08 14:50:56 -0700503 // HDR content is not supported on PERFORMANCE mode
504 if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700505 return RefreshRateType::DEFAULT;
506 }
507
Ady Abraham8532d012019-05-08 14:50:56 -0700508 // As long as touch is active we want to be in performance mode
509 if (mCurrentTouchState == TouchState::ACTIVE) {
510 return RefreshRateType::PERFORMANCE;
511 }
512
513 // If timer has expired as it means there is no new content on the screen
514 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
Ady Abrahama315ce72019-04-24 14:35:20 -0700515 return RefreshRateType::DEFAULT;
516 }
517
Ady Abraham09bd3922019-04-08 10:44:56 -0700518 // If content detection is off we choose performance as we don't know the content fps
519 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
520 return RefreshRateType::PERFORMANCE;
521 }
522
523 // Content detection is on, find the appropriate refresh rate
Ady Abraham85b3f012019-04-08 11:04:14 -0700524 // Start with the smallest refresh rate which is within a margin of the content
525 RefreshRateType currRefreshRateType = RefreshRateType::PERFORMANCE;
526 constexpr float MARGIN = 0.05f;
Ady Abraham09bd3922019-04-08 10:44:56 -0700527 auto iter = mRefreshRateConfigs.getRefreshRates().cbegin();
Ady Abraham09bd3922019-04-08 10:44:56 -0700528 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
Ady Abraham85b3f012019-04-08 11:04:14 -0700529 if (iter->second->fps >= mContentRefreshRate * (1 - MARGIN)) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700530 currRefreshRateType = iter->first;
531 break;
532 }
533 ++iter;
534 }
535
Ady Abraham85b3f012019-04-08 11:04:14 -0700536 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
537 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
538 // align well with both
539 float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps /
540 float(mContentRefreshRate);
541 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
542 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
543 ratio = iter->second->fps / float(mContentRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -0700544
Ady Abraham85b3f012019-04-08 11:04:14 -0700545 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
546 currRefreshRateType = iter->first;
547 break;
548 }
549 ++iter;
550 }
551 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700552
553 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800554}
555
556void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
557 std::lock_guard<std::mutex> lock(mCallbackLock);
558 if (mChangeRefreshRateCallback) {
559 mChangeRefreshRateCallback(refreshRateType, configEvent);
560 }
561}
562
Ana Krulec98b5b242018-08-10 15:03:23 -0700563} // namespace android