blob: 3f9a88df75a8f6681fa1c9c6655f8a5fc5e17ad7 [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
Ana Krulecfb772822018-11-30 10:44:07 +010078 char value[PROPERTY_VALUE_MAX];
Ana Kruleca5bdd9d2019-01-29 19:00:58 -080079 property_get("debug.sf.set_idle_timer_ms", value, "0");
Ady Abrahambe59c0d2019-03-05 13:01:13 -080080 int int_value = atoi(value);
81 if (int_value) {
82 mSetIdleTimerMs = atoi(value);
83 }
Ana Krulecfb772822018-11-30 10:44:07 +010084
85 if (mSetIdleTimerMs > 0) {
Alec Mouridc28b372019-04-18 21:17:13 -070086 if (mSupportKernelTimer) {
87 mIdleTimer =
88 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(
89 mSetIdleTimerMs),
90 [this] { resetKernelTimerCallback(); },
91 [this] {
92 expiredKernelTimerCallback();
93 });
94 } else {
95 mIdleTimer = std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(
96 mSetIdleTimerMs),
97 [this] { resetTimerCallback(); },
98 [this] { expiredTimerCallback(); });
99 }
Ana Krulecfb772822018-11-30 10:44:07 +0100100 mIdleTimer->start();
101 }
Ana Krulece588e312018-09-18 12:32:24 -0700102}
103
Lloyd Pique1f9f1a42019-01-31 13:04:00 -0800104Scheduler::~Scheduler() {
105 // Ensure the IdleTimer thread is joined before we start destroying state.
106 mIdleTimer.reset();
107}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700108
Ana Krulec98b5b242018-08-10 15:03:23 -0700109sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800110 const char* connectionName, int64_t phaseOffsetNs, ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -0700111 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
112 const int64_t id = sNextId++;
113 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
114
Ana Krulec98b5b242018-08-10 15:03:23 -0700115 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -0800116 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800117 std::move(interceptCallback));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800118
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800119 auto eventThreadConnection =
Ady Abrahama1a49af2019-02-07 14:36:55 -0800120 createConnectionInternal(eventThread.get(), std::move(resyncCallback));
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800121 mConnections.emplace(id,
122 std::make_unique<Connection>(new ConnectionHandle(id),
123 eventThreadConnection,
124 std::move(eventThread)));
Ana Krulec98b5b242018-08-10 15:03:23 -0700125 return mConnections[id]->handle;
126}
127
Ana Krulec0c8cd522018-08-31 12:27:28 -0700128std::unique_ptr<EventThread> Scheduler::makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800129 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700130 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
131 std::unique_ptr<VSyncSource> eventThreadSource =
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800132 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName);
133 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800134 std::move(interceptCallback), connectionName);
135}
136
Ady Abrahama1a49af2019-02-07 14:36:55 -0800137sp<EventThreadConnection> Scheduler::createConnectionInternal(EventThread* eventThread,
138 ResyncCallback&& resyncCallback) {
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800139 return eventThread->createEventConnection(std::move(resyncCallback),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800140 [this] { resetIdleTimer(); });
Ana Krulec0c8cd522018-08-31 12:27:28 -0700141}
142
Ana Krulec98b5b242018-08-10 15:03:23 -0700143sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Ady Abrahama1a49af2019-02-07 14:36:55 -0800144 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700145 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800146 return createConnectionInternal(mConnections[handle->id]->thread.get(),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800147 std::move(resyncCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -0700148}
149
150EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700151 RETURN_VALUE_IF_INVALID(nullptr);
152 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700153}
154
Ana Krulec85c39af2018-12-26 17:29:57 -0800155sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700156 RETURN_VALUE_IF_INVALID(nullptr);
157 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700158}
159
160void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800161 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700162 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800163 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700164}
165
166void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700167 RETURN_IF_INVALID();
168 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700169}
170
171void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700172 RETURN_IF_INVALID();
173 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700174}
175
Ady Abraham447052e2019-02-13 16:07:27 -0800176void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
177 int32_t configId) {
178 RETURN_IF_INVALID();
179 mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
180}
181
Yiwei Zhang5434a782018-12-05 18:06:32 -0800182void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700183 RETURN_IF_INVALID();
184 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700185}
186
187void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700188 RETURN_IF_INVALID();
189 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700190}
Ana Krulece588e312018-09-18 12:32:24 -0700191
192void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
193 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
194 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
195}
196
197void Scheduler::enableHardwareVsync() {
198 std::lock_guard<std::mutex> lock(mHWVsyncLock);
199 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
200 mPrimaryDispSync->beginResync();
201 mEventControlThread->setVsyncEnabled(true);
202 mPrimaryHWVsyncEnabled = true;
203 }
204}
205
206void Scheduler::disableHardwareVsync(bool makeUnavailable) {
207 std::lock_guard<std::mutex> lock(mHWVsyncLock);
208 if (mPrimaryHWVsyncEnabled) {
209 mEventControlThread->setVsyncEnabled(false);
210 mPrimaryDispSync->endResync();
211 mPrimaryHWVsyncEnabled = false;
212 }
213 if (makeUnavailable) {
214 mHWVsyncAvailable = false;
215 }
216}
217
Ana Krulecc2870422019-01-29 19:00:58 -0800218void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
219 {
220 std::lock_guard<std::mutex> lock(mHWVsyncLock);
221 if (makeAvailable) {
222 mHWVsyncAvailable = makeAvailable;
223 } else if (!mHWVsyncAvailable) {
224 // Hardware vsync is not currently available, so abort the resync
225 // attempt for now
226 return;
227 }
228 }
229
230 if (period <= 0) {
231 return;
232 }
233
234 setVsyncPeriod(period);
235}
236
237ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
238 std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
239 return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
240 if (const auto vsync = ptr.lock()) {
241 vsync->resync(getVsyncPeriod);
242 }
243 };
244}
245
246void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
247 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
248
249 const nsecs_t now = systemTime();
250 const nsecs_t last = lastResyncTime.exchange(now);
251
252 if (now - last > kIgnoreDelay) {
253 scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
254 }
255}
256
257void Scheduler::setRefreshSkipCount(int count) {
258 mPrimaryDispSync->setRefreshSkipCount(count);
259}
260
Ana Krulece588e312018-09-18 12:32:24 -0700261void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800262 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700263 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800264
265 if (!mPrimaryHWVsyncEnabled) {
266 mPrimaryDispSync->beginResync();
267 mEventControlThread->setVsyncEnabled(true);
268 mPrimaryHWVsyncEnabled = true;
269 }
Ana Krulece588e312018-09-18 12:32:24 -0700270}
271
Alec Mouri754c98a2019-03-18 18:53:42 -0700272void Scheduler::addResyncSample(const nsecs_t timestamp, bool* periodChanged) {
Ana Krulece588e312018-09-18 12:32:24 -0700273 bool needsHwVsync = false;
Alec Mouri754c98a2019-03-18 18:53:42 -0700274 *periodChanged = false;
Ana Krulece588e312018-09-18 12:32:24 -0700275 { // Scope for the lock
276 std::lock_guard<std::mutex> lock(mHWVsyncLock);
277 if (mPrimaryHWVsyncEnabled) {
Alec Mouri754c98a2019-03-18 18:53:42 -0700278 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodChanged);
Ana Krulece588e312018-09-18 12:32:24 -0700279 }
280 }
281
282 if (needsHwVsync) {
283 enableHardwareVsync();
284 } else {
285 disableHardwareVsync(false);
286 }
287}
288
289void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
290 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
291 enableHardwareVsync();
292 } else {
293 disableHardwareVsync(false);
294 }
295}
296
297void Scheduler::setIgnorePresentFences(bool ignore) {
298 mPrimaryDispSync->setIgnorePresentFences(ignore);
299}
300
Ady Abrahamc3e21312019-02-07 14:30:23 -0800301nsecs_t Scheduler::expectedPresentTime() {
302 return mPrimaryDispSync->expectedPresentTime();
303}
304
Ady Abraham3aff9172019-02-07 19:10:26 -0800305void Scheduler::dumpPrimaryDispSync(std::string& result) const {
306 mPrimaryDispSync->dump(result);
307}
308
Ady Abraham09bd3922019-04-08 10:44:56 -0700309std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer(
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700310 std::string const& name, int windowType) {
311 RefreshRateType refreshRateType = (windowType == InputWindowInfo::TYPE_WALLPAPER)
312 ? RefreshRateType::DEFAULT
313 : RefreshRateType::PERFORMANCE;
Ady Abraham09bd3922019-04-08 10:44:56 -0700314
315 const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType);
316 const uint32_t fps = (refreshRate) ? refreshRate->fps : 0;
317 return mLayerHistory.createLayer(name, fps);
318}
319
Ady Abrahama315ce72019-04-24 14:35:20 -0700320void Scheduler::addLayerPresentTimeAndHDR(
Ady Abraham09bd3922019-04-08 10:44:56 -0700321 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
Ady Abrahama315ce72019-04-24 14:35:20 -0700322 nsecs_t presentTime, bool isHDR) {
323 mLayerHistory.insert(layerHandle, presentTime, isHDR);
324}
325
326void Scheduler::setLayerVisibility(
327 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, bool visible) {
328 mLayerHistory.setVisibility(layerHandle, visible);
Ana Krulec3084c052018-11-21 20:27:17 +0100329}
330
Kevin DuBois413287f2019-02-25 08:46:47 -0800331void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
332 fn(*mPrimaryDispSync);
333}
334
Ady Abraham09bd3922019-04-08 10:44:56 -0700335void Scheduler::updateFpsBasedOnContent() {
Ady Abrahama315ce72019-04-24 14:35:20 -0700336 auto [refreshRate, isHDR] = mLayerHistory.getDesiredRefreshRateAndHDR();
337 const uint32_t refreshRateRound = std::round(refreshRate);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700338 RefreshRateType newRefreshRateType;
339 {
340 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abrahama315ce72019-04-24 14:35:20 -0700341 if (mContentRefreshRate == refreshRateRound && mIsHDRContent == isHDR) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700342 return;
343 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700344 mContentRefreshRate = refreshRateRound;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700345 ATRACE_INT("ContentFPS", mContentRefreshRate);
346
Ady Abrahama315ce72019-04-24 14:35:20 -0700347 mIsHDRContent = isHDR;
348 ATRACE_INT("ContentHDR", mIsHDRContent);
349
350 mCurrentContentFeatureState = refreshRateRound > 0
351 ? ContentFeatureState::CONTENT_DETECTION_ON
352 : ContentFeatureState::CONTENT_DETECTION_OFF;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700353 newRefreshRateType = calculateRefreshRateType();
354 if (mRefreshRateType == newRefreshRateType) {
355 return;
356 }
357 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800358 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700359 changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
Ana Krulec3084c052018-11-21 20:27:17 +0100360}
361
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800362void Scheduler::setChangeRefreshRateCallback(
363 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800364 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800365 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800366}
367
Alec Mouridc28b372019-04-18 21:17:13 -0700368void Scheduler::setGetVsyncPeriodCallback(const GetVsyncPeriod&& getVsyncPeriod) {
369 std::lock_guard<std::mutex> lock(mCallbackLock);
370 mGetVsyncPeriod = getVsyncPeriod;
371}
372
Ana Krulec3084c052018-11-21 20:27:17 +0100373void Scheduler::updateFrameSkipping(const int64_t skipCount) {
374 ATRACE_INT("FrameSkipCount", skipCount);
375 if (mSkipCount != skipCount) {
376 // Only update DispSync if it hasn't been updated yet.
377 mPrimaryDispSync->setRefreshSkipCount(skipCount);
378 mSkipCount = skipCount;
379 }
380}
381
Ana Krulecfb772822018-11-30 10:44:07 +0100382void Scheduler::resetIdleTimer() {
383 if (mIdleTimer) {
384 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800385 }
386}
387
388void Scheduler::resetTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800389 timerChangeRefreshRate(IdleTimerState::RESET);
390 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100391}
392
Alec Mouridc28b372019-04-18 21:17:13 -0700393void Scheduler::resetKernelTimerCallback() {
394 ATRACE_INT("ExpiredKernelIdleTimer", 0);
395 std::lock_guard<std::mutex> lock(mCallbackLock);
396 if (mGetVsyncPeriod) {
397 resyncToHardwareVsync(false, mGetVsyncPeriod());
398 }
399}
400
Ana Krulecfb772822018-11-30 10:44:07 +0100401void Scheduler::expiredTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800402 timerChangeRefreshRate(IdleTimerState::EXPIRED);
403 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100404}
405
Alec Mouridc28b372019-04-18 21:17:13 -0700406void Scheduler::expiredKernelTimerCallback() {
407 ATRACE_INT("ExpiredKernelIdleTimer", 1);
408 // Disable HW Vsync if the timer expired, as we don't need it
409 // enabled if we're not pushing frames.
410 disableHardwareVsync(false);
411}
412
Ana Krulecb43429d2019-01-09 14:28:51 -0800413std::string Scheduler::doDump() {
414 std::ostringstream stream;
415 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
416 return stream.str();
417}
418
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800419void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700420 RefreshRateType newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800421 {
422 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700423 if (mCurrentIdleTimerState == idleTimerState) {
424 return;
425 }
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800426 mCurrentIdleTimerState = idleTimerState;
Ady Abraham09bd3922019-04-08 10:44:56 -0700427 newRefreshRateType = calculateRefreshRateType();
Ady Abraham6398a0a2019-04-18 19:30:44 -0700428 if (mRefreshRateType == newRefreshRateType) {
429 return;
430 }
431 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800432 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700433 changeRefreshRate(newRefreshRateType, ConfigEvent::None);
434}
435
436Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
437 // First check if timer has expired as it means there is no new content on the screen
438 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
439 return RefreshRateType::DEFAULT;
440 }
441
Ady Abrahama315ce72019-04-24 14:35:20 -0700442 // HDR content is not supported on PERFORMANCE mode
443 if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) {
444 return RefreshRateType::DEFAULT;
445 }
446
Ady Abraham09bd3922019-04-08 10:44:56 -0700447 // If content detection is off we choose performance as we don't know the content fps
448 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
449 return RefreshRateType::PERFORMANCE;
450 }
451
452 // Content detection is on, find the appropriate refresh rate
Ady Abraham85b3f012019-04-08 11:04:14 -0700453 // Start with the smallest refresh rate which is within a margin of the content
454 RefreshRateType currRefreshRateType = RefreshRateType::PERFORMANCE;
455 constexpr float MARGIN = 0.05f;
Ady Abraham09bd3922019-04-08 10:44:56 -0700456 auto iter = mRefreshRateConfigs.getRefreshRates().cbegin();
Ady Abraham09bd3922019-04-08 10:44:56 -0700457 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
Ady Abraham85b3f012019-04-08 11:04:14 -0700458 if (iter->second->fps >= mContentRefreshRate * (1 - MARGIN)) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700459 currRefreshRateType = iter->first;
460 break;
461 }
462 ++iter;
463 }
464
Ady Abraham85b3f012019-04-08 11:04:14 -0700465 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
466 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
467 // align well with both
468 float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps /
469 float(mContentRefreshRate);
470 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
471 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
472 ratio = iter->second->fps / float(mContentRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -0700473
Ady Abraham85b3f012019-04-08 11:04:14 -0700474 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
475 currRefreshRateType = iter->first;
476 break;
477 }
478 ++iter;
479 }
480 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700481
482 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800483}
484
485void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
486 std::lock_guard<std::mutex> lock(mCallbackLock);
487 if (mChangeRefreshRateCallback) {
488 mChangeRefreshRateCallback(refreshRateType, configEvent);
489 }
490}
491
Ana Krulec98b5b242018-08-10 15:03:23 -0700492} // namespace android