blob: cfdbd91e356b8551514946ec744e5429b3362bdf [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);
Ana Krulecad083c42019-06-26 16:28:08 -0700333 const uint32_t performanceFps = (refreshRate) ? refreshRate->fps : 0;
334
335 const auto defaultRefreshRate = mRefreshRateConfigs.getRefreshRate(RefreshRateType::DEFAULT);
336 const uint32_t defaultFps = (defaultRefreshRate) ? defaultRefreshRate->fps : 0;
337 return mLayerHistory.createLayer(name, defaultFps, performanceFps);
Ady Abraham09bd3922019-04-08 10:44:56 -0700338}
339
Ady Abrahama315ce72019-04-24 14:35:20 -0700340void Scheduler::addLayerPresentTimeAndHDR(
Ady Abraham09bd3922019-04-08 10:44:56 -0700341 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
Ady Abrahama315ce72019-04-24 14:35:20 -0700342 nsecs_t presentTime, bool isHDR) {
343 mLayerHistory.insert(layerHandle, presentTime, isHDR);
344}
345
346void Scheduler::setLayerVisibility(
347 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, bool visible) {
348 mLayerHistory.setVisibility(layerHandle, visible);
Ana Krulec3084c052018-11-21 20:27:17 +0100349}
350
Kevin DuBois413287f2019-02-25 08:46:47 -0800351void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
352 fn(*mPrimaryDispSync);
353}
354
Ady Abraham09bd3922019-04-08 10:44:56 -0700355void Scheduler::updateFpsBasedOnContent() {
Ady Abrahama315ce72019-04-24 14:35:20 -0700356 auto [refreshRate, isHDR] = mLayerHistory.getDesiredRefreshRateAndHDR();
357 const uint32_t refreshRateRound = std::round(refreshRate);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700358 RefreshRateType newRefreshRateType;
359 {
360 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abrahama315ce72019-04-24 14:35:20 -0700361 if (mContentRefreshRate == refreshRateRound && mIsHDRContent == isHDR) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700362 return;
363 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700364 mContentRefreshRate = refreshRateRound;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700365 ATRACE_INT("ContentFPS", mContentRefreshRate);
366
Ady Abrahama315ce72019-04-24 14:35:20 -0700367 mIsHDRContent = isHDR;
368 ATRACE_INT("ContentHDR", mIsHDRContent);
369
370 mCurrentContentFeatureState = refreshRateRound > 0
371 ? ContentFeatureState::CONTENT_DETECTION_ON
372 : ContentFeatureState::CONTENT_DETECTION_OFF;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700373 newRefreshRateType = calculateRefreshRateType();
374 if (mRefreshRateType == newRefreshRateType) {
375 return;
376 }
377 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800378 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700379 changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
Ana Krulec3084c052018-11-21 20:27:17 +0100380}
381
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800382void Scheduler::setChangeRefreshRateCallback(
383 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800384 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800385 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800386}
387
Alec Mouridc28b372019-04-18 21:17:13 -0700388void Scheduler::setGetVsyncPeriodCallback(const GetVsyncPeriod&& getVsyncPeriod) {
389 std::lock_guard<std::mutex> lock(mCallbackLock);
390 mGetVsyncPeriod = getVsyncPeriod;
391}
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 Abrahama1a49af2019-02-07 14:36:55 -0800422void Scheduler::resetTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800423 timerChangeRefreshRate(IdleTimerState::RESET);
424 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100425}
426
Alec Mouridc28b372019-04-18 21:17:13 -0700427void Scheduler::resetKernelTimerCallback() {
428 ATRACE_INT("ExpiredKernelIdleTimer", 0);
429 std::lock_guard<std::mutex> lock(mCallbackLock);
430 if (mGetVsyncPeriod) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700431 resyncToHardwareVsync(true, mGetVsyncPeriod());
Alec Mouridc28b372019-04-18 21:17:13 -0700432 }
433}
434
Ana Krulecfb772822018-11-30 10:44:07 +0100435void Scheduler::expiredTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800436 timerChangeRefreshRate(IdleTimerState::EXPIRED);
437 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100438}
439
Ady Abraham8532d012019-05-08 14:50:56 -0700440void Scheduler::resetTouchTimerCallback() {
441 // We do not notify the applications about config changes when idle timer is reset.
442 touchChangeRefreshRate(TouchState::ACTIVE);
443 ATRACE_INT("TouchState", 1);
444}
445
446void Scheduler::expiredTouchTimerCallback() {
447 // We do not notify the applications about config changes when idle timer expires.
448 touchChangeRefreshRate(TouchState::INACTIVE);
449 ATRACE_INT("TouchState", 0);
450}
451
Alec Mouridc28b372019-04-18 21:17:13 -0700452void Scheduler::expiredKernelTimerCallback() {
453 ATRACE_INT("ExpiredKernelIdleTimer", 1);
454 // Disable HW Vsync if the timer expired, as we don't need it
455 // enabled if we're not pushing frames.
456 disableHardwareVsync(false);
457}
458
Ana Krulecb43429d2019-01-09 14:28:51 -0800459std::string Scheduler::doDump() {
460 std::ostringstream stream;
461 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
Ady Abraham8532d012019-05-08 14:50:56 -0700462 stream << "+ Touch timer interval: " << mSetTouchTimerMs << " ms" << std::endl;
Ana Krulecb43429d2019-01-09 14:28:51 -0800463 return stream.str();
464}
465
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800466void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700467 RefreshRateType newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800468 {
469 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700470 if (mCurrentIdleTimerState == idleTimerState) {
471 return;
472 }
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800473 mCurrentIdleTimerState = idleTimerState;
Ady Abraham09bd3922019-04-08 10:44:56 -0700474 newRefreshRateType = calculateRefreshRateType();
Ady Abraham6398a0a2019-04-18 19:30:44 -0700475 if (mRefreshRateType == newRefreshRateType) {
476 return;
477 }
478 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800479 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700480 changeRefreshRate(newRefreshRateType, ConfigEvent::None);
481}
482
Ady Abraham8532d012019-05-08 14:50:56 -0700483void Scheduler::touchChangeRefreshRate(TouchState touchState) {
484 ConfigEvent event = ConfigEvent::None;
485 RefreshRateType newRefreshRateType;
486 {
487 std::lock_guard<std::mutex> lock(mFeatureStateLock);
488 if (mCurrentTouchState == touchState) {
489 return;
490 }
491 mCurrentTouchState = touchState;
492 newRefreshRateType = calculateRefreshRateType();
493 if (mRefreshRateType == newRefreshRateType) {
494 return;
495 }
496 mRefreshRateType = newRefreshRateType;
497 // Send an event in case that content detection is on as touch has a higher priority
498 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_ON) {
499 event = ConfigEvent::Changed;
500 }
501 }
502 changeRefreshRate(newRefreshRateType, event);
503}
504
Ady Abraham09bd3922019-04-08 10:44:56 -0700505Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
Ady Abraham8532d012019-05-08 14:50:56 -0700506 // HDR content is not supported on PERFORMANCE mode
507 if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700508 return RefreshRateType::DEFAULT;
509 }
510
Ady Abraham8532d012019-05-08 14:50:56 -0700511 // As long as touch is active we want to be in performance mode
512 if (mCurrentTouchState == TouchState::ACTIVE) {
513 return RefreshRateType::PERFORMANCE;
514 }
515
516 // If timer has expired as it means there is no new content on the screen
517 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
Ady Abrahama315ce72019-04-24 14:35:20 -0700518 return RefreshRateType::DEFAULT;
519 }
520
Ady Abraham09bd3922019-04-08 10:44:56 -0700521 // If content detection is off we choose performance as we don't know the content fps
522 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
523 return RefreshRateType::PERFORMANCE;
524 }
525
Wei Wang09be73f2019-07-02 14:29:18 -0700526 // Content detection is on, find the appropriate refresh rate with minimal error
527 auto iter = min_element(mRefreshRateConfigs.getRefreshRates().cbegin(),
528 mRefreshRateConfigs.getRefreshRates().cend(),
529 [rate = mContentRefreshRate](const auto& l, const auto& r) -> bool {
530 return std::abs(l.second->fps - static_cast<float>(rate)) <
531 std::abs(r.second->fps - static_cast<float>(rate));
532 });
533 RefreshRateType currRefreshRateType = iter->first;
Ady Abraham09bd3922019-04-08 10:44:56 -0700534
Ady Abraham85b3f012019-04-08 11:04:14 -0700535 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
536 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
537 // align well with both
Wei Wang09be73f2019-07-02 14:29:18 -0700538 constexpr float MARGIN = 0.05f;
Ady Abraham85b3f012019-04-08 11:04:14 -0700539 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