blob: b1c39ef906bb549d43adad30079aaa9474cb6eb6 [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);
76
Ana Krulecfb772822018-11-30 10:44:07 +010077 char value[PROPERTY_VALUE_MAX];
Ana Kruleca5bdd9d2019-01-29 19:00:58 -080078 property_get("debug.sf.set_idle_timer_ms", value, "0");
Ady Abrahambe59c0d2019-03-05 13:01:13 -080079 int int_value = atoi(value);
80 if (int_value) {
81 mSetIdleTimerMs = atoi(value);
82 }
Ana Krulecfb772822018-11-30 10:44:07 +010083
84 if (mSetIdleTimerMs > 0) {
85 mIdleTimer =
86 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetIdleTimerMs),
Ady Abrahama1a49af2019-02-07 14:36:55 -080087 [this] { resetTimerCallback(); },
Ana Krulecfb772822018-11-30 10:44:07 +010088 [this] { expiredTimerCallback(); });
89 mIdleTimer->start();
90 }
Ana Krulece588e312018-09-18 12:32:24 -070091}
92
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080093Scheduler::~Scheduler() {
94 // Ensure the IdleTimer thread is joined before we start destroying state.
95 mIdleTimer.reset();
96}
Ana Krulec0c8cd522018-08-31 12:27:28 -070097
Ana Krulec98b5b242018-08-10 15:03:23 -070098sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Dominik Laskowskibd52c842019-01-28 18:11:23 -080099 const char* connectionName, int64_t phaseOffsetNs, ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -0700100 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
101 const int64_t id = sNextId++;
102 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
103
Ana Krulec98b5b242018-08-10 15:03:23 -0700104 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -0800105 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800106 std::move(interceptCallback));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800107
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800108 auto eventThreadConnection =
Ady Abrahama1a49af2019-02-07 14:36:55 -0800109 createConnectionInternal(eventThread.get(), std::move(resyncCallback));
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800110 mConnections.emplace(id,
111 std::make_unique<Connection>(new ConnectionHandle(id),
112 eventThreadConnection,
113 std::move(eventThread)));
Ana Krulec98b5b242018-08-10 15:03:23 -0700114 return mConnections[id]->handle;
115}
116
Ana Krulec0c8cd522018-08-31 12:27:28 -0700117std::unique_ptr<EventThread> Scheduler::makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800118 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700119 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
120 std::unique_ptr<VSyncSource> eventThreadSource =
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800121 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName);
122 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800123 std::move(interceptCallback), connectionName);
124}
125
Ady Abrahama1a49af2019-02-07 14:36:55 -0800126sp<EventThreadConnection> Scheduler::createConnectionInternal(EventThread* eventThread,
127 ResyncCallback&& resyncCallback) {
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800128 return eventThread->createEventConnection(std::move(resyncCallback),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800129 [this] { resetIdleTimer(); });
Ana Krulec0c8cd522018-08-31 12:27:28 -0700130}
131
Ana Krulec98b5b242018-08-10 15:03:23 -0700132sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Ady Abrahama1a49af2019-02-07 14:36:55 -0800133 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700134 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800135 return createConnectionInternal(mConnections[handle->id]->thread.get(),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800136 std::move(resyncCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -0700137}
138
139EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700140 RETURN_VALUE_IF_INVALID(nullptr);
141 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700142}
143
Ana Krulec85c39af2018-12-26 17:29:57 -0800144sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700145 RETURN_VALUE_IF_INVALID(nullptr);
146 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700147}
148
149void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800150 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700151 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800152 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700153}
154
155void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700156 RETURN_IF_INVALID();
157 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700158}
159
160void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700161 RETURN_IF_INVALID();
162 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700163}
164
Ady Abraham447052e2019-02-13 16:07:27 -0800165void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
166 int32_t configId) {
167 RETURN_IF_INVALID();
168 mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
169}
170
Yiwei Zhang5434a782018-12-05 18:06:32 -0800171void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700172 RETURN_IF_INVALID();
173 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700174}
175
176void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700177 RETURN_IF_INVALID();
178 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700179}
Ana Krulece588e312018-09-18 12:32:24 -0700180
Ady Abrahamb838aed2019-02-12 15:30:16 -0800181void Scheduler::pauseVsyncCallback(const android::sp<android::Scheduler::ConnectionHandle>& handle,
182 bool pause) {
183 RETURN_IF_INVALID();
184 mConnections[handle->id]->thread->pauseVsyncCallback(pause);
185}
186
Ana Krulece588e312018-09-18 12:32:24 -0700187void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
188 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
189 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
190}
191
192void Scheduler::enableHardwareVsync() {
193 std::lock_guard<std::mutex> lock(mHWVsyncLock);
194 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
195 mPrimaryDispSync->beginResync();
196 mEventControlThread->setVsyncEnabled(true);
197 mPrimaryHWVsyncEnabled = true;
198 }
199}
200
201void Scheduler::disableHardwareVsync(bool makeUnavailable) {
202 std::lock_guard<std::mutex> lock(mHWVsyncLock);
203 if (mPrimaryHWVsyncEnabled) {
204 mEventControlThread->setVsyncEnabled(false);
205 mPrimaryDispSync->endResync();
206 mPrimaryHWVsyncEnabled = false;
207 }
208 if (makeUnavailable) {
209 mHWVsyncAvailable = false;
210 }
211}
212
Ana Krulecc2870422019-01-29 19:00:58 -0800213void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
214 {
215 std::lock_guard<std::mutex> lock(mHWVsyncLock);
216 if (makeAvailable) {
217 mHWVsyncAvailable = makeAvailable;
218 } else if (!mHWVsyncAvailable) {
219 // Hardware vsync is not currently available, so abort the resync
220 // attempt for now
221 return;
222 }
223 }
224
225 if (period <= 0) {
226 return;
227 }
228
229 setVsyncPeriod(period);
230}
231
232ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
233 std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
234 return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
235 if (const auto vsync = ptr.lock()) {
236 vsync->resync(getVsyncPeriod);
237 }
238 };
239}
240
241void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
242 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
243
244 const nsecs_t now = systemTime();
245 const nsecs_t last = lastResyncTime.exchange(now);
246
247 if (now - last > kIgnoreDelay) {
248 scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
249 }
250}
251
252void Scheduler::setRefreshSkipCount(int count) {
253 mPrimaryDispSync->setRefreshSkipCount(count);
254}
255
Ana Krulece588e312018-09-18 12:32:24 -0700256void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800257 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700258 mPrimaryDispSync->reset();
259 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800260
261 if (!mPrimaryHWVsyncEnabled) {
262 mPrimaryDispSync->beginResync();
263 mEventControlThread->setVsyncEnabled(true);
264 mPrimaryHWVsyncEnabled = true;
265 }
Ana Krulece588e312018-09-18 12:32:24 -0700266}
267
268void Scheduler::addResyncSample(const nsecs_t timestamp) {
269 bool needsHwVsync = false;
270 { // Scope for the lock
271 std::lock_guard<std::mutex> lock(mHWVsyncLock);
272 if (mPrimaryHWVsyncEnabled) {
273 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp);
274 }
275 }
276
277 if (needsHwVsync) {
278 enableHardwareVsync();
279 } else {
280 disableHardwareVsync(false);
281 }
282}
283
284void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
285 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
286 enableHardwareVsync();
287 } else {
288 disableHardwareVsync(false);
289 }
290}
291
292void Scheduler::setIgnorePresentFences(bool ignore) {
293 mPrimaryDispSync->setIgnorePresentFences(ignore);
294}
295
Ady Abrahamc3e21312019-02-07 14:30:23 -0800296nsecs_t Scheduler::expectedPresentTime() {
297 return mPrimaryDispSync->expectedPresentTime();
298}
299
Ady Abraham3aff9172019-02-07 19:10:26 -0800300void Scheduler::dumpPrimaryDispSync(std::string& result) const {
301 mPrimaryDispSync->dump(result);
302}
303
Ady Abraham09bd3922019-04-08 10:44:56 -0700304std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer(
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700305 std::string const& name, int windowType) {
306 RefreshRateType refreshRateType = (windowType == InputWindowInfo::TYPE_WALLPAPER)
307 ? RefreshRateType::DEFAULT
308 : RefreshRateType::PERFORMANCE;
Ady Abraham09bd3922019-04-08 10:44:56 -0700309
310 const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType);
311 const uint32_t fps = (refreshRate) ? refreshRate->fps : 0;
312 return mLayerHistory.createLayer(name, fps);
313}
314
315void Scheduler::addLayerPresentTime(
316 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
317 nsecs_t presentTime) {
318 mLayerHistory.insert(layerHandle, presentTime);
Ana Krulec3084c052018-11-21 20:27:17 +0100319}
320
Kevin DuBois413287f2019-02-25 08:46:47 -0800321void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
322 fn(*mPrimaryDispSync);
323}
324
Ady Abraham09bd3922019-04-08 10:44:56 -0700325void Scheduler::updateFpsBasedOnContent() {
326 uint32_t refreshRate = std::round(mLayerHistory.getDesiredRefreshRate());
327 ATRACE_INT("ContentFPS", refreshRate);
328 if (refreshRate > 0) {
329 contentChangeRefreshRate(ContentFeatureState::CONTENT_DETECTION_ON, refreshRate);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800330 } else {
Ady Abraham09bd3922019-04-08 10:44:56 -0700331 contentChangeRefreshRate(ContentFeatureState::CONTENT_DETECTION_OFF, 0);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800332 }
Ana Krulec3084c052018-11-21 20:27:17 +0100333}
334
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800335void Scheduler::setChangeRefreshRateCallback(
336 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800337 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800338 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800339}
340
Ana Krulec3084c052018-11-21 20:27:17 +0100341void Scheduler::updateFrameSkipping(const int64_t skipCount) {
342 ATRACE_INT("FrameSkipCount", skipCount);
343 if (mSkipCount != skipCount) {
344 // Only update DispSync if it hasn't been updated yet.
345 mPrimaryDispSync->setRefreshSkipCount(skipCount);
346 mSkipCount = skipCount;
347 }
348}
349
Ana Krulecfb772822018-11-30 10:44:07 +0100350void Scheduler::resetIdleTimer() {
351 if (mIdleTimer) {
352 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800353 }
354}
355
356void Scheduler::resetTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800357 // We do not notify the applications about config changes when idle timer is reset.
358 timerChangeRefreshRate(IdleTimerState::RESET);
359 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100360}
361
362void Scheduler::expiredTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800363 // We do not notify the applications about config changes when idle timer expires.
364 timerChangeRefreshRate(IdleTimerState::EXPIRED);
365 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100366}
367
Ana Krulecb43429d2019-01-09 14:28:51 -0800368std::string Scheduler::doDump() {
369 std::ostringstream stream;
370 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
371 return stream.str();
372}
373
Ady Abraham09bd3922019-04-08 10:44:56 -0700374void Scheduler::contentChangeRefreshRate(ContentFeatureState contentFeatureState,
375 uint32_t refreshRate) {
376 RefreshRateType newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800377 {
378 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham09bd3922019-04-08 10:44:56 -0700379 mCurrentContentFeatureState = contentFeatureState;
380 mContentRefreshRate = refreshRate;
381 newRefreshRateType = calculateRefreshRateType();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800382 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700383 changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800384}
385
386void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700387 RefreshRateType newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800388 {
389 std::lock_guard<std::mutex> lock(mFeatureStateLock);
390 mCurrentIdleTimerState = idleTimerState;
Ady Abraham09bd3922019-04-08 10:44:56 -0700391 newRefreshRateType = calculateRefreshRateType();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800392 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700393 changeRefreshRate(newRefreshRateType, ConfigEvent::None);
394}
395
396Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
397 // First check if timer has expired as it means there is no new content on the screen
398 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
399 return RefreshRateType::DEFAULT;
400 }
401
402 // If content detection is off we choose performance as we don't know the content fps
403 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
404 return RefreshRateType::PERFORMANCE;
405 }
406
407 // Content detection is on, find the appropriate refresh rate
Ady Abraham85b3f012019-04-08 11:04:14 -0700408 // Start with the smallest refresh rate which is within a margin of the content
409 RefreshRateType currRefreshRateType = RefreshRateType::PERFORMANCE;
410 constexpr float MARGIN = 0.05f;
Ady Abraham09bd3922019-04-08 10:44:56 -0700411 auto iter = mRefreshRateConfigs.getRefreshRates().cbegin();
Ady Abraham09bd3922019-04-08 10:44:56 -0700412 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
Ady Abraham85b3f012019-04-08 11:04:14 -0700413 if (iter->second->fps >= mContentRefreshRate * (1 - MARGIN)) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700414 currRefreshRateType = iter->first;
415 break;
416 }
417 ++iter;
418 }
419
Ady Abraham85b3f012019-04-08 11:04:14 -0700420 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
421 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
422 // align well with both
423 float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps /
424 float(mContentRefreshRate);
425 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
426 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
427 ratio = iter->second->fps / float(mContentRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -0700428
Ady Abraham85b3f012019-04-08 11:04:14 -0700429 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
430 currRefreshRateType = iter->first;
431 break;
432 }
433 ++iter;
434 }
435 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700436
437 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800438}
439
440void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
441 std::lock_guard<std::mutex> lock(mCallbackLock);
442 if (mChangeRefreshRateCallback) {
443 mChangeRefreshRateCallback(refreshRateType, configEvent);
444 }
445}
446
Ana Krulec98b5b242018-08-10 15:03:23 -0700447} // namespace android