blob: 88d2638423975f052e7ace69d9d123644022c801 [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
181void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
182 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
183 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
184}
185
186void Scheduler::enableHardwareVsync() {
187 std::lock_guard<std::mutex> lock(mHWVsyncLock);
188 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
189 mPrimaryDispSync->beginResync();
190 mEventControlThread->setVsyncEnabled(true);
191 mPrimaryHWVsyncEnabled = true;
192 }
193}
194
195void Scheduler::disableHardwareVsync(bool makeUnavailable) {
196 std::lock_guard<std::mutex> lock(mHWVsyncLock);
197 if (mPrimaryHWVsyncEnabled) {
198 mEventControlThread->setVsyncEnabled(false);
199 mPrimaryDispSync->endResync();
200 mPrimaryHWVsyncEnabled = false;
201 }
202 if (makeUnavailable) {
203 mHWVsyncAvailable = false;
204 }
205}
206
Ana Krulecc2870422019-01-29 19:00:58 -0800207void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
208 {
209 std::lock_guard<std::mutex> lock(mHWVsyncLock);
210 if (makeAvailable) {
211 mHWVsyncAvailable = makeAvailable;
212 } else if (!mHWVsyncAvailable) {
213 // Hardware vsync is not currently available, so abort the resync
214 // attempt for now
215 return;
216 }
217 }
218
219 if (period <= 0) {
220 return;
221 }
222
223 setVsyncPeriod(period);
224}
225
226ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
227 std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
228 return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
229 if (const auto vsync = ptr.lock()) {
230 vsync->resync(getVsyncPeriod);
231 }
232 };
233}
234
235void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
236 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
237
238 const nsecs_t now = systemTime();
239 const nsecs_t last = lastResyncTime.exchange(now);
240
241 if (now - last > kIgnoreDelay) {
242 scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
243 }
244}
245
246void Scheduler::setRefreshSkipCount(int count) {
247 mPrimaryDispSync->setRefreshSkipCount(count);
248}
249
Ana Krulece588e312018-09-18 12:32:24 -0700250void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800251 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700252 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800253
254 if (!mPrimaryHWVsyncEnabled) {
255 mPrimaryDispSync->beginResync();
256 mEventControlThread->setVsyncEnabled(true);
257 mPrimaryHWVsyncEnabled = true;
258 }
Ana Krulece588e312018-09-18 12:32:24 -0700259}
260
Alec Mouri754c98a2019-03-18 18:53:42 -0700261void Scheduler::addResyncSample(const nsecs_t timestamp, bool* periodChanged) {
Ana Krulece588e312018-09-18 12:32:24 -0700262 bool needsHwVsync = false;
Alec Mouri754c98a2019-03-18 18:53:42 -0700263 *periodChanged = false;
Ana Krulece588e312018-09-18 12:32:24 -0700264 { // Scope for the lock
265 std::lock_guard<std::mutex> lock(mHWVsyncLock);
266 if (mPrimaryHWVsyncEnabled) {
Alec Mouri754c98a2019-03-18 18:53:42 -0700267 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodChanged);
Ana Krulece588e312018-09-18 12:32:24 -0700268 }
269 }
270
271 if (needsHwVsync) {
272 enableHardwareVsync();
273 } else {
274 disableHardwareVsync(false);
275 }
276}
277
278void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
279 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
280 enableHardwareVsync();
281 } else {
282 disableHardwareVsync(false);
283 }
284}
285
286void Scheduler::setIgnorePresentFences(bool ignore) {
287 mPrimaryDispSync->setIgnorePresentFences(ignore);
288}
289
Ady Abrahamc3e21312019-02-07 14:30:23 -0800290nsecs_t Scheduler::expectedPresentTime() {
291 return mPrimaryDispSync->expectedPresentTime();
292}
293
Ady Abraham3aff9172019-02-07 19:10:26 -0800294void Scheduler::dumpPrimaryDispSync(std::string& result) const {
295 mPrimaryDispSync->dump(result);
296}
297
Ady Abraham09bd3922019-04-08 10:44:56 -0700298std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer(
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700299 std::string const& name, int windowType) {
300 RefreshRateType refreshRateType = (windowType == InputWindowInfo::TYPE_WALLPAPER)
301 ? RefreshRateType::DEFAULT
302 : RefreshRateType::PERFORMANCE;
Ady Abraham09bd3922019-04-08 10:44:56 -0700303
304 const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType);
305 const uint32_t fps = (refreshRate) ? refreshRate->fps : 0;
306 return mLayerHistory.createLayer(name, fps);
307}
308
309void Scheduler::addLayerPresentTime(
310 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
311 nsecs_t presentTime) {
312 mLayerHistory.insert(layerHandle, presentTime);
Ana Krulec3084c052018-11-21 20:27:17 +0100313}
314
Kevin DuBois413287f2019-02-25 08:46:47 -0800315void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
316 fn(*mPrimaryDispSync);
317}
318
Ady Abraham09bd3922019-04-08 10:44:56 -0700319void Scheduler::updateFpsBasedOnContent() {
320 uint32_t refreshRate = std::round(mLayerHistory.getDesiredRefreshRate());
Ady Abraham6398a0a2019-04-18 19:30:44 -0700321 RefreshRateType newRefreshRateType;
322 {
323 std::lock_guard<std::mutex> lock(mFeatureStateLock);
324 if (mContentRefreshRate == refreshRate) {
325 return;
326 }
327 mContentRefreshRate = refreshRate;
328 ATRACE_INT("ContentFPS", mContentRefreshRate);
329
330 mCurrentContentFeatureState = refreshRate > 0 ? ContentFeatureState::CONTENT_DETECTION_ON
331 : ContentFeatureState::CONTENT_DETECTION_OFF;
332 newRefreshRateType = calculateRefreshRateType();
333 if (mRefreshRateType == newRefreshRateType) {
334 return;
335 }
336 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800337 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700338 changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
Ana Krulec3084c052018-11-21 20:27:17 +0100339}
340
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800341void Scheduler::setChangeRefreshRateCallback(
342 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800343 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800344 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800345}
346
Ana Krulec3084c052018-11-21 20:27:17 +0100347void Scheduler::updateFrameSkipping(const int64_t skipCount) {
348 ATRACE_INT("FrameSkipCount", skipCount);
349 if (mSkipCount != skipCount) {
350 // Only update DispSync if it hasn't been updated yet.
351 mPrimaryDispSync->setRefreshSkipCount(skipCount);
352 mSkipCount = skipCount;
353 }
354}
355
Ana Krulecfb772822018-11-30 10:44:07 +0100356void Scheduler::resetIdleTimer() {
357 if (mIdleTimer) {
358 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800359 }
360}
361
362void Scheduler::resetTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800363 // We do not notify the applications about config changes when idle timer is reset.
364 timerChangeRefreshRate(IdleTimerState::RESET);
365 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100366}
367
368void Scheduler::expiredTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800369 // We do not notify the applications about config changes when idle timer expires.
370 timerChangeRefreshRate(IdleTimerState::EXPIRED);
371 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100372}
373
Ana Krulecb43429d2019-01-09 14:28:51 -0800374std::string Scheduler::doDump() {
375 std::ostringstream stream;
376 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
377 return stream.str();
378}
379
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800380void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700381 RefreshRateType newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800382 {
383 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700384 if (mCurrentIdleTimerState == idleTimerState) {
385 return;
386 }
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800387 mCurrentIdleTimerState = idleTimerState;
Ady Abraham09bd3922019-04-08 10:44:56 -0700388 newRefreshRateType = calculateRefreshRateType();
Ady Abraham6398a0a2019-04-18 19:30:44 -0700389 if (mRefreshRateType == newRefreshRateType) {
390 return;
391 }
392 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800393 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700394 changeRefreshRate(newRefreshRateType, ConfigEvent::None);
395}
396
397Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
398 // First check if timer has expired as it means there is no new content on the screen
399 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
400 return RefreshRateType::DEFAULT;
401 }
402
403 // If content detection is off we choose performance as we don't know the content fps
404 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
405 return RefreshRateType::PERFORMANCE;
406 }
407
408 // Content detection is on, find the appropriate refresh rate
Ady Abraham85b3f012019-04-08 11:04:14 -0700409 // Start with the smallest refresh rate which is within a margin of the content
410 RefreshRateType currRefreshRateType = RefreshRateType::PERFORMANCE;
411 constexpr float MARGIN = 0.05f;
Ady Abraham09bd3922019-04-08 10:44:56 -0700412 auto iter = mRefreshRateConfigs.getRefreshRates().cbegin();
Ady Abraham09bd3922019-04-08 10:44:56 -0700413 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
Ady Abraham85b3f012019-04-08 11:04:14 -0700414 if (iter->second->fps >= mContentRefreshRate * (1 - MARGIN)) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700415 currRefreshRateType = iter->first;
416 break;
417 }
418 ++iter;
419 }
420
Ady Abraham85b3f012019-04-08 11:04:14 -0700421 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
422 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
423 // align well with both
424 float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps /
425 float(mContentRefreshRate);
426 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
427 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
428 ratio = iter->second->fps / float(mContentRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -0700429
Ady Abraham85b3f012019-04-08 11:04:14 -0700430 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
431 currRefreshRateType = iter->first;
432 break;
433 }
434 ++iter;
435 }
436 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700437
438 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800439}
440
441void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
442 std::lock_guard<std::mutex> lock(mCallbackLock);
443 if (mChangeRefreshRateCallback) {
444 mChangeRefreshRateCallback(refreshRateType, configEvent);
445 }
446}
447
Ana Krulec98b5b242018-08-10 15:03:23 -0700448} // namespace android