blob: e2a348e146f6950395949cf6bd060fa775e408f0 [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 }
407}
408
Ady Abrahama1a49af2019-02-07 14:36:55 -0800409void Scheduler::resetTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800410 timerChangeRefreshRate(IdleTimerState::RESET);
411 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100412}
413
Alec Mouridc28b372019-04-18 21:17:13 -0700414void Scheduler::resetKernelTimerCallback() {
415 ATRACE_INT("ExpiredKernelIdleTimer", 0);
416 std::lock_guard<std::mutex> lock(mCallbackLock);
417 if (mGetVsyncPeriod) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700418 resyncToHardwareVsync(true, mGetVsyncPeriod());
Alec Mouridc28b372019-04-18 21:17:13 -0700419 }
420}
421
Ana Krulecfb772822018-11-30 10:44:07 +0100422void Scheduler::expiredTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800423 timerChangeRefreshRate(IdleTimerState::EXPIRED);
424 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100425}
426
Ady Abraham8532d012019-05-08 14:50:56 -0700427void Scheduler::resetTouchTimerCallback() {
428 // We do not notify the applications about config changes when idle timer is reset.
429 touchChangeRefreshRate(TouchState::ACTIVE);
430 ATRACE_INT("TouchState", 1);
431}
432
433void Scheduler::expiredTouchTimerCallback() {
434 // We do not notify the applications about config changes when idle timer expires.
435 touchChangeRefreshRate(TouchState::INACTIVE);
436 ATRACE_INT("TouchState", 0);
437}
438
Alec Mouridc28b372019-04-18 21:17:13 -0700439void Scheduler::expiredKernelTimerCallback() {
440 ATRACE_INT("ExpiredKernelIdleTimer", 1);
441 // Disable HW Vsync if the timer expired, as we don't need it
442 // enabled if we're not pushing frames.
443 disableHardwareVsync(false);
444}
445
Ana Krulecb43429d2019-01-09 14:28:51 -0800446std::string Scheduler::doDump() {
447 std::ostringstream stream;
448 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
Ady Abraham8532d012019-05-08 14:50:56 -0700449 stream << "+ Touch timer interval: " << mSetTouchTimerMs << " ms" << std::endl;
Ana Krulecb43429d2019-01-09 14:28:51 -0800450 return stream.str();
451}
452
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800453void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700454 RefreshRateType newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800455 {
456 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700457 if (mCurrentIdleTimerState == idleTimerState) {
458 return;
459 }
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800460 mCurrentIdleTimerState = idleTimerState;
Ady Abraham09bd3922019-04-08 10:44:56 -0700461 newRefreshRateType = calculateRefreshRateType();
Ady Abraham6398a0a2019-04-18 19:30:44 -0700462 if (mRefreshRateType == newRefreshRateType) {
463 return;
464 }
465 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800466 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700467 changeRefreshRate(newRefreshRateType, ConfigEvent::None);
468}
469
Ady Abraham8532d012019-05-08 14:50:56 -0700470void Scheduler::touchChangeRefreshRate(TouchState touchState) {
471 ConfigEvent event = ConfigEvent::None;
472 RefreshRateType newRefreshRateType;
473 {
474 std::lock_guard<std::mutex> lock(mFeatureStateLock);
475 if (mCurrentTouchState == touchState) {
476 return;
477 }
478 mCurrentTouchState = touchState;
479 newRefreshRateType = calculateRefreshRateType();
480 if (mRefreshRateType == newRefreshRateType) {
481 return;
482 }
483 mRefreshRateType = newRefreshRateType;
484 // Send an event in case that content detection is on as touch has a higher priority
485 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_ON) {
486 event = ConfigEvent::Changed;
487 }
488 }
489 changeRefreshRate(newRefreshRateType, event);
490}
491
Ady Abraham09bd3922019-04-08 10:44:56 -0700492Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
Ady Abraham8532d012019-05-08 14:50:56 -0700493 // HDR content is not supported on PERFORMANCE mode
494 if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700495 return RefreshRateType::DEFAULT;
496 }
497
Ady Abraham8532d012019-05-08 14:50:56 -0700498 // As long as touch is active we want to be in performance mode
499 if (mCurrentTouchState == TouchState::ACTIVE) {
500 return RefreshRateType::PERFORMANCE;
501 }
502
503 // If timer has expired as it means there is no new content on the screen
504 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
Ady Abrahama315ce72019-04-24 14:35:20 -0700505 return RefreshRateType::DEFAULT;
506 }
507
Ady Abraham09bd3922019-04-08 10:44:56 -0700508 // If content detection is off we choose performance as we don't know the content fps
509 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
510 return RefreshRateType::PERFORMANCE;
511 }
512
513 // Content detection is on, find the appropriate refresh rate
Ady Abraham85b3f012019-04-08 11:04:14 -0700514 // Start with the smallest refresh rate which is within a margin of the content
515 RefreshRateType currRefreshRateType = RefreshRateType::PERFORMANCE;
516 constexpr float MARGIN = 0.05f;
Ady Abraham09bd3922019-04-08 10:44:56 -0700517 auto iter = mRefreshRateConfigs.getRefreshRates().cbegin();
Ady Abraham09bd3922019-04-08 10:44:56 -0700518 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
Ady Abraham85b3f012019-04-08 11:04:14 -0700519 if (iter->second->fps >= mContentRefreshRate * (1 - MARGIN)) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700520 currRefreshRateType = iter->first;
521 break;
522 }
523 ++iter;
524 }
525
Ady Abraham85b3f012019-04-08 11:04:14 -0700526 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
527 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
528 // align well with both
529 float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps /
530 float(mContentRefreshRate);
531 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
532 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
533 ratio = iter->second->fps / float(mContentRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -0700534
Ady Abraham85b3f012019-04-08 11:04:14 -0700535 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
536 currRefreshRateType = iter->first;
537 break;
538 }
539 ++iter;
540 }
541 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700542
543 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800544}
545
546void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
547 std::lock_guard<std::mutex> lock(mCallbackLock);
548 if (mChangeRefreshRateCallback) {
549 mChangeRefreshRateCallback(refreshRateType, configEvent);
550 }
551}
552
Ana Krulec98b5b242018-08-10 15:03:23 -0700553} // namespace android