blob: da9526a0cd97bac07c711a0ad025e5fc2a84cc16 [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(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800122 const char* connectionName, int64_t phaseOffsetNs, ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -0700123 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
124 const int64_t id = sNextId++;
125 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
126
Ana Krulec98b5b242018-08-10 15:03:23 -0700127 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -0800128 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800129 std::move(interceptCallback));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800130
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800131 auto eventThreadConnection =
Ady Abrahama1a49af2019-02-07 14:36:55 -0800132 createConnectionInternal(eventThread.get(), std::move(resyncCallback));
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800133 mConnections.emplace(id,
134 std::make_unique<Connection>(new ConnectionHandle(id),
135 eventThreadConnection,
136 std::move(eventThread)));
Ana Krulec98b5b242018-08-10 15:03:23 -0700137 return mConnections[id]->handle;
138}
139
Ana Krulec0c8cd522018-08-31 12:27:28 -0700140std::unique_ptr<EventThread> Scheduler::makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800141 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700142 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
143 std::unique_ptr<VSyncSource> eventThreadSource =
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800144 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName);
145 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800146 std::move(interceptCallback), connectionName);
147}
148
Ady Abrahama1a49af2019-02-07 14:36:55 -0800149sp<EventThreadConnection> Scheduler::createConnectionInternal(EventThread* eventThread,
150 ResyncCallback&& resyncCallback) {
Ady Abraham8532d012019-05-08 14:50:56 -0700151 return eventThread->createEventConnection(std::move(resyncCallback));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700152}
153
Ana Krulec98b5b242018-08-10 15:03:23 -0700154sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Ady Abrahama1a49af2019-02-07 14:36:55 -0800155 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700156 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800157 return createConnectionInternal(mConnections[handle->id]->thread.get(),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800158 std::move(resyncCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -0700159}
160
161EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700162 RETURN_VALUE_IF_INVALID(nullptr);
163 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700164}
165
Ana Krulec85c39af2018-12-26 17:29:57 -0800166sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700167 RETURN_VALUE_IF_INVALID(nullptr);
168 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700169}
170
171void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800172 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700173 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800174 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700175}
176
177void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700178 RETURN_IF_INVALID();
179 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700180}
181
182void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700183 RETURN_IF_INVALID();
184 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700185}
186
Ady Abraham447052e2019-02-13 16:07:27 -0800187void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
188 int32_t configId) {
189 RETURN_IF_INVALID();
190 mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
191}
192
Yiwei Zhang5434a782018-12-05 18:06:32 -0800193void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700194 RETURN_IF_INVALID();
195 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700196}
197
198void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700199 RETURN_IF_INVALID();
200 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700201}
Ana Krulece588e312018-09-18 12:32:24 -0700202
203void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
204 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
205 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
206}
207
208void Scheduler::enableHardwareVsync() {
209 std::lock_guard<std::mutex> lock(mHWVsyncLock);
210 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
211 mPrimaryDispSync->beginResync();
212 mEventControlThread->setVsyncEnabled(true);
213 mPrimaryHWVsyncEnabled = true;
214 }
215}
216
217void Scheduler::disableHardwareVsync(bool makeUnavailable) {
218 std::lock_guard<std::mutex> lock(mHWVsyncLock);
219 if (mPrimaryHWVsyncEnabled) {
220 mEventControlThread->setVsyncEnabled(false);
221 mPrimaryDispSync->endResync();
222 mPrimaryHWVsyncEnabled = false;
223 }
224 if (makeUnavailable) {
225 mHWVsyncAvailable = false;
226 }
227}
228
Ana Krulecc2870422019-01-29 19:00:58 -0800229void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
230 {
231 std::lock_guard<std::mutex> lock(mHWVsyncLock);
232 if (makeAvailable) {
233 mHWVsyncAvailable = makeAvailable;
234 } else if (!mHWVsyncAvailable) {
235 // Hardware vsync is not currently available, so abort the resync
236 // attempt for now
237 return;
238 }
239 }
240
241 if (period <= 0) {
242 return;
243 }
244
245 setVsyncPeriod(period);
246}
247
248ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
249 std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
250 return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
251 if (const auto vsync = ptr.lock()) {
252 vsync->resync(getVsyncPeriod);
253 }
254 };
255}
256
257void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
258 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
259
260 const nsecs_t now = systemTime();
261 const nsecs_t last = lastResyncTime.exchange(now);
262
263 if (now - last > kIgnoreDelay) {
264 scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
265 }
266}
267
268void Scheduler::setRefreshSkipCount(int count) {
269 mPrimaryDispSync->setRefreshSkipCount(count);
270}
271
Ana Krulece588e312018-09-18 12:32:24 -0700272void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800273 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700274 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800275
276 if (!mPrimaryHWVsyncEnabled) {
277 mPrimaryDispSync->beginResync();
278 mEventControlThread->setVsyncEnabled(true);
279 mPrimaryHWVsyncEnabled = true;
280 }
Ana Krulece588e312018-09-18 12:32:24 -0700281}
282
Alec Mourif8e689c2019-05-20 18:32:22 -0700283void Scheduler::addResyncSample(const nsecs_t timestamp, bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700284 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700285 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700286 { // Scope for the lock
287 std::lock_guard<std::mutex> lock(mHWVsyncLock);
288 if (mPrimaryHWVsyncEnabled) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700289 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700290 }
291 }
292
293 if (needsHwVsync) {
294 enableHardwareVsync();
295 } else {
296 disableHardwareVsync(false);
297 }
298}
299
300void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
301 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
302 enableHardwareVsync();
303 } else {
304 disableHardwareVsync(false);
305 }
306}
307
308void Scheduler::setIgnorePresentFences(bool ignore) {
309 mPrimaryDispSync->setIgnorePresentFences(ignore);
310}
311
Ady Abrahamc3e21312019-02-07 14:30:23 -0800312nsecs_t Scheduler::expectedPresentTime() {
313 return mPrimaryDispSync->expectedPresentTime();
314}
315
Ady Abraham3aff9172019-02-07 19:10:26 -0800316void Scheduler::dumpPrimaryDispSync(std::string& result) const {
317 mPrimaryDispSync->dump(result);
318}
319
Ady Abraham09bd3922019-04-08 10:44:56 -0700320std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer(
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700321 std::string const& name, int windowType) {
322 RefreshRateType refreshRateType = (windowType == InputWindowInfo::TYPE_WALLPAPER)
323 ? RefreshRateType::DEFAULT
324 : RefreshRateType::PERFORMANCE;
Ady Abraham09bd3922019-04-08 10:44:56 -0700325
326 const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType);
327 const uint32_t fps = (refreshRate) ? refreshRate->fps : 0;
328 return mLayerHistory.createLayer(name, fps);
329}
330
Ady Abrahama315ce72019-04-24 14:35:20 -0700331void Scheduler::addLayerPresentTimeAndHDR(
Ady Abraham09bd3922019-04-08 10:44:56 -0700332 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
Ady Abrahama315ce72019-04-24 14:35:20 -0700333 nsecs_t presentTime, bool isHDR) {
334 mLayerHistory.insert(layerHandle, presentTime, isHDR);
335}
336
337void Scheduler::setLayerVisibility(
338 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, bool visible) {
339 mLayerHistory.setVisibility(layerHandle, visible);
Ana Krulec3084c052018-11-21 20:27:17 +0100340}
341
Kevin DuBois413287f2019-02-25 08:46:47 -0800342void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
343 fn(*mPrimaryDispSync);
344}
345
Ady Abraham09bd3922019-04-08 10:44:56 -0700346void Scheduler::updateFpsBasedOnContent() {
Ady Abrahama315ce72019-04-24 14:35:20 -0700347 auto [refreshRate, isHDR] = mLayerHistory.getDesiredRefreshRateAndHDR();
348 const uint32_t refreshRateRound = std::round(refreshRate);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700349 RefreshRateType newRefreshRateType;
350 {
351 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abrahama315ce72019-04-24 14:35:20 -0700352 if (mContentRefreshRate == refreshRateRound && mIsHDRContent == isHDR) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700353 return;
354 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700355 mContentRefreshRate = refreshRateRound;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700356 ATRACE_INT("ContentFPS", mContentRefreshRate);
357
Ady Abrahama315ce72019-04-24 14:35:20 -0700358 mIsHDRContent = isHDR;
359 ATRACE_INT("ContentHDR", mIsHDRContent);
360
361 mCurrentContentFeatureState = refreshRateRound > 0
362 ? ContentFeatureState::CONTENT_DETECTION_ON
363 : ContentFeatureState::CONTENT_DETECTION_OFF;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700364 newRefreshRateType = calculateRefreshRateType();
365 if (mRefreshRateType == newRefreshRateType) {
366 return;
367 }
368 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800369 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700370 changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
Ana Krulec3084c052018-11-21 20:27:17 +0100371}
372
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800373void Scheduler::setChangeRefreshRateCallback(
374 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800375 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800376 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800377}
378
Alec Mouridc28b372019-04-18 21:17:13 -0700379void Scheduler::setGetVsyncPeriodCallback(const GetVsyncPeriod&& getVsyncPeriod) {
380 std::lock_guard<std::mutex> lock(mCallbackLock);
381 mGetVsyncPeriod = getVsyncPeriod;
382}
383
Ana Krulec3084c052018-11-21 20:27:17 +0100384void Scheduler::updateFrameSkipping(const int64_t skipCount) {
385 ATRACE_INT("FrameSkipCount", skipCount);
386 if (mSkipCount != skipCount) {
387 // Only update DispSync if it hasn't been updated yet.
388 mPrimaryDispSync->setRefreshSkipCount(skipCount);
389 mSkipCount = skipCount;
390 }
391}
392
Ana Krulecfb772822018-11-30 10:44:07 +0100393void Scheduler::resetIdleTimer() {
394 if (mIdleTimer) {
395 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800396 }
397}
398
Ady Abraham8532d012019-05-08 14:50:56 -0700399void Scheduler::notifyTouchEvent() {
400 if (mTouchTimer) {
401 mTouchTimer->reset();
402 }
403
404 if (mSupportKernelTimer) {
405 resetIdleTimer();
406 }
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700407
408 // Touch event will boost the refresh rate to performance.
409 // Clear Layer History to get fresh FPS detection
410 mLayerHistory.clearHistory();
Ady Abraham8532d012019-05-08 14:50:56 -0700411}
412
Ady Abrahama1a49af2019-02-07 14:36:55 -0800413void Scheduler::resetTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800414 timerChangeRefreshRate(IdleTimerState::RESET);
415 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100416}
417
Alec Mouridc28b372019-04-18 21:17:13 -0700418void Scheduler::resetKernelTimerCallback() {
419 ATRACE_INT("ExpiredKernelIdleTimer", 0);
420 std::lock_guard<std::mutex> lock(mCallbackLock);
421 if (mGetVsyncPeriod) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700422 resyncToHardwareVsync(true, mGetVsyncPeriod());
Alec Mouridc28b372019-04-18 21:17:13 -0700423 }
424}
425
Ana Krulecfb772822018-11-30 10:44:07 +0100426void Scheduler::expiredTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800427 timerChangeRefreshRate(IdleTimerState::EXPIRED);
428 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100429}
430
Ady Abraham8532d012019-05-08 14:50:56 -0700431void Scheduler::resetTouchTimerCallback() {
432 // We do not notify the applications about config changes when idle timer is reset.
433 touchChangeRefreshRate(TouchState::ACTIVE);
434 ATRACE_INT("TouchState", 1);
435}
436
437void Scheduler::expiredTouchTimerCallback() {
438 // We do not notify the applications about config changes when idle timer expires.
439 touchChangeRefreshRate(TouchState::INACTIVE);
440 ATRACE_INT("TouchState", 0);
441}
442
Alec Mouridc28b372019-04-18 21:17:13 -0700443void Scheduler::expiredKernelTimerCallback() {
444 ATRACE_INT("ExpiredKernelIdleTimer", 1);
445 // Disable HW Vsync if the timer expired, as we don't need it
446 // enabled if we're not pushing frames.
447 disableHardwareVsync(false);
448}
449
Ana Krulecb43429d2019-01-09 14:28:51 -0800450std::string Scheduler::doDump() {
451 std::ostringstream stream;
452 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
Ady Abraham8532d012019-05-08 14:50:56 -0700453 stream << "+ Touch timer interval: " << mSetTouchTimerMs << " ms" << std::endl;
Ana Krulecb43429d2019-01-09 14:28:51 -0800454 return stream.str();
455}
456
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800457void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700458 RefreshRateType newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800459 {
460 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700461 if (mCurrentIdleTimerState == idleTimerState) {
462 return;
463 }
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800464 mCurrentIdleTimerState = idleTimerState;
Ady Abraham09bd3922019-04-08 10:44:56 -0700465 newRefreshRateType = calculateRefreshRateType();
Ady Abraham6398a0a2019-04-18 19:30:44 -0700466 if (mRefreshRateType == newRefreshRateType) {
467 return;
468 }
469 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800470 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700471 changeRefreshRate(newRefreshRateType, ConfigEvent::None);
472}
473
Ady Abraham8532d012019-05-08 14:50:56 -0700474void Scheduler::touchChangeRefreshRate(TouchState touchState) {
475 ConfigEvent event = ConfigEvent::None;
476 RefreshRateType newRefreshRateType;
477 {
478 std::lock_guard<std::mutex> lock(mFeatureStateLock);
479 if (mCurrentTouchState == touchState) {
480 return;
481 }
482 mCurrentTouchState = touchState;
483 newRefreshRateType = calculateRefreshRateType();
484 if (mRefreshRateType == newRefreshRateType) {
485 return;
486 }
487 mRefreshRateType = newRefreshRateType;
488 // Send an event in case that content detection is on as touch has a higher priority
489 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_ON) {
490 event = ConfigEvent::Changed;
491 }
492 }
493 changeRefreshRate(newRefreshRateType, event);
494}
495
Ady Abraham09bd3922019-04-08 10:44:56 -0700496Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
Ady Abraham8532d012019-05-08 14:50:56 -0700497 // HDR content is not supported on PERFORMANCE mode
498 if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700499 return RefreshRateType::DEFAULT;
500 }
501
Ady Abraham8532d012019-05-08 14:50:56 -0700502 // As long as touch is active we want to be in performance mode
503 if (mCurrentTouchState == TouchState::ACTIVE) {
504 return RefreshRateType::PERFORMANCE;
505 }
506
507 // If timer has expired as it means there is no new content on the screen
508 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
Ady Abrahama315ce72019-04-24 14:35:20 -0700509 return RefreshRateType::DEFAULT;
510 }
511
Ady Abraham09bd3922019-04-08 10:44:56 -0700512 // If content detection is off we choose performance as we don't know the content fps
513 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
514 return RefreshRateType::PERFORMANCE;
515 }
516
517 // Content detection is on, find the appropriate refresh rate
Ady Abraham85b3f012019-04-08 11:04:14 -0700518 // Start with the smallest refresh rate which is within a margin of the content
519 RefreshRateType currRefreshRateType = RefreshRateType::PERFORMANCE;
520 constexpr float MARGIN = 0.05f;
Ady Abraham09bd3922019-04-08 10:44:56 -0700521 auto iter = mRefreshRateConfigs.getRefreshRates().cbegin();
Ady Abraham09bd3922019-04-08 10:44:56 -0700522 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
Ady Abraham85b3f012019-04-08 11:04:14 -0700523 if (iter->second->fps >= mContentRefreshRate * (1 - MARGIN)) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700524 currRefreshRateType = iter->first;
525 break;
526 }
527 ++iter;
528 }
529
Ady Abraham85b3f012019-04-08 11:04:14 -0700530 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
531 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
532 // align well with both
533 float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps /
534 float(mContentRefreshRate);
535 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
536 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
537 ratio = iter->second->fps / float(mContentRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -0700538
Ady Abraham85b3f012019-04-08 11:04:14 -0700539 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
540 currRefreshRateType = iter->first;
541 break;
542 }
543 ++iter;
544 }
545 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700546
547 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800548}
549
550void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
551 std::lock_guard<std::mutex> lock(mCallbackLock);
552 if (mChangeRefreshRateCallback) {
553 mChangeRefreshRateCallback(refreshRateType, configEvent);
554 }
555}
556
Ana Krulec98b5b242018-08-10 15:03:23 -0700557} // namespace android