blob: 9f92d92ecf01a84cbb40f4a5de9b4d8dc6435eb6 [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>
Ana Krulecfefd6ae2019-02-13 17:53:08 -080031#include <system/window.h>
Ana Krulece588e312018-09-18 12:32:24 -070032#include <ui/DisplayStatInfo.h>
Ana Krulec3084c052018-11-21 20:27:17 +010033#include <utils/Timers.h>
Ana Krulec7ab56032018-11-02 20:51:06 +010034#include <utils/Trace.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070035
36#include "DispSync.h"
37#include "DispSyncSource.h"
Ana Krulece588e312018-09-18 12:32:24 -070038#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070039#include "EventThread.h"
Ana Krulecfb772822018-11-30 10:44:07 +010040#include "IdleTimer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070041#include "InjectVSyncSource.h"
Ady Abraham09bd3922019-04-08 10:44:56 -070042#include "LayerInfo.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010043#include "SchedulerUtils.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090044#include "SurfaceFlingerProperties.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070045
46namespace android {
47
Ana Krulece588e312018-09-18 12:32:24 -070048using namespace android::hardware::configstore;
49using namespace android::hardware::configstore::V1_0;
Sundong Ahnd5e08f62018-12-12 20:27:28 +090050using namespace android::sysprop;
Ana Krulece588e312018-09-18 12:32:24 -070051
Ana Krulec0c8cd522018-08-31 12:27:28 -070052#define RETURN_VALUE_IF_INVALID(value) \
53 if (handle == nullptr || mConnections.count(handle->id) == 0) return value
54#define RETURN_IF_INVALID() \
55 if (handle == nullptr || mConnections.count(handle->id) == 0) return
56
Ana Krulec98b5b242018-08-10 15:03:23 -070057std::atomic<int64_t> Scheduler::sNextId = 0;
58
Ady Abraham09bd3922019-04-08 10:44:56 -070059Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function,
60 const scheduler::RefreshRateConfigs& refreshRateConfig)
Sundong Ahnd5e08f62018-12-12 20:27:28 +090061 : mHasSyncFramework(running_without_sync_framework(true)),
62 mDispSyncPresentTimeOffset(present_time_offset_from_vsync_ns(0)),
Ana Krulece588e312018-09-18 12:32:24 -070063 mPrimaryHWVsyncEnabled(false),
Ady Abraham09bd3922019-04-08 10:44:56 -070064 mHWVsyncAvailable(false),
65 mRefreshRateConfigs(refreshRateConfig) {
Ana Krulece588e312018-09-18 12:32:24 -070066 // Note: We create a local temporary with the real DispSync implementation
67 // type temporarily so we can initialize it with the configured values,
68 // before storing it for more generic use using the interface type.
69 auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync");
70 primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset);
71 mPrimaryDispSync = std::move(primaryDispSync);
72 mEventControlThread = std::make_unique<impl::EventControlThread>(function);
Ana Krulecfb772822018-11-30 10:44:07 +010073
Ady Abrahambe59c0d2019-03-05 13:01:13 -080074 mSetIdleTimerMs = set_idle_timer_ms(0);
75
Ana Krulecfb772822018-11-30 10:44:07 +010076 char value[PROPERTY_VALUE_MAX];
Ana Kruleca5bdd9d2019-01-29 19:00:58 -080077 property_get("debug.sf.set_idle_timer_ms", value, "0");
Ady Abrahambe59c0d2019-03-05 13:01:13 -080078 int int_value = atoi(value);
79 if (int_value) {
80 mSetIdleTimerMs = atoi(value);
81 }
Ana Krulecfb772822018-11-30 10:44:07 +010082
83 if (mSetIdleTimerMs > 0) {
84 mIdleTimer =
85 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetIdleTimerMs),
Ady Abrahama1a49af2019-02-07 14:36:55 -080086 [this] { resetTimerCallback(); },
Ana Krulecfb772822018-11-30 10:44:07 +010087 [this] { expiredTimerCallback(); });
88 mIdleTimer->start();
89 }
Ana Krulece588e312018-09-18 12:32:24 -070090}
91
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080092Scheduler::~Scheduler() {
93 // Ensure the IdleTimer thread is joined before we start destroying state.
94 mIdleTimer.reset();
95}
Ana Krulec0c8cd522018-08-31 12:27:28 -070096
Ana Krulec98b5b242018-08-10 15:03:23 -070097sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Dominik Laskowskibd52c842019-01-28 18:11:23 -080098 const char* connectionName, int64_t phaseOffsetNs, ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -070099 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
100 const int64_t id = sNextId++;
101 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
102
Ana Krulec98b5b242018-08-10 15:03:23 -0700103 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -0800104 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800105 std::move(interceptCallback));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800106
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800107 auto eventThreadConnection =
Ady Abrahama1a49af2019-02-07 14:36:55 -0800108 createConnectionInternal(eventThread.get(), std::move(resyncCallback));
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800109 mConnections.emplace(id,
110 std::make_unique<Connection>(new ConnectionHandle(id),
111 eventThreadConnection,
112 std::move(eventThread)));
Ana Krulec98b5b242018-08-10 15:03:23 -0700113 return mConnections[id]->handle;
114}
115
Ana Krulec0c8cd522018-08-31 12:27:28 -0700116std::unique_ptr<EventThread> Scheduler::makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800117 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700118 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
119 std::unique_ptr<VSyncSource> eventThreadSource =
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800120 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName);
121 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800122 std::move(interceptCallback), connectionName);
123}
124
Ady Abrahama1a49af2019-02-07 14:36:55 -0800125sp<EventThreadConnection> Scheduler::createConnectionInternal(EventThread* eventThread,
126 ResyncCallback&& resyncCallback) {
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800127 return eventThread->createEventConnection(std::move(resyncCallback),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800128 [this] { resetIdleTimer(); });
Ana Krulec0c8cd522018-08-31 12:27:28 -0700129}
130
Ana Krulec98b5b242018-08-10 15:03:23 -0700131sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Ady Abrahama1a49af2019-02-07 14:36:55 -0800132 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700133 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800134 return createConnectionInternal(mConnections[handle->id]->thread.get(),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800135 std::move(resyncCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -0700136}
137
138EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700139 RETURN_VALUE_IF_INVALID(nullptr);
140 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700141}
142
Ana Krulec85c39af2018-12-26 17:29:57 -0800143sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700144 RETURN_VALUE_IF_INVALID(nullptr);
145 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700146}
147
148void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800149 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700150 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800151 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700152}
153
154void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700155 RETURN_IF_INVALID();
156 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700157}
158
159void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700160 RETURN_IF_INVALID();
161 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700162}
163
Ady Abraham447052e2019-02-13 16:07:27 -0800164void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
165 int32_t configId) {
166 RETURN_IF_INVALID();
167 mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
168}
169
Yiwei Zhang5434a782018-12-05 18:06:32 -0800170void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700171 RETURN_IF_INVALID();
172 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700173}
174
175void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700176 RETURN_IF_INVALID();
177 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700178}
Ana Krulece588e312018-09-18 12:32:24 -0700179
Ady Abrahamb838aed2019-02-12 15:30:16 -0800180void Scheduler::pauseVsyncCallback(const android::sp<android::Scheduler::ConnectionHandle>& handle,
181 bool pause) {
182 RETURN_IF_INVALID();
183 mConnections[handle->id]->thread->pauseVsyncCallback(pause);
184}
185
Ana Krulece588e312018-09-18 12:32:24 -0700186void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
187 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
188 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
189}
190
191void Scheduler::enableHardwareVsync() {
192 std::lock_guard<std::mutex> lock(mHWVsyncLock);
193 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
194 mPrimaryDispSync->beginResync();
195 mEventControlThread->setVsyncEnabled(true);
196 mPrimaryHWVsyncEnabled = true;
197 }
198}
199
200void Scheduler::disableHardwareVsync(bool makeUnavailable) {
201 std::lock_guard<std::mutex> lock(mHWVsyncLock);
202 if (mPrimaryHWVsyncEnabled) {
203 mEventControlThread->setVsyncEnabled(false);
204 mPrimaryDispSync->endResync();
205 mPrimaryHWVsyncEnabled = false;
206 }
207 if (makeUnavailable) {
208 mHWVsyncAvailable = false;
209 }
210}
211
Ana Krulecc2870422019-01-29 19:00:58 -0800212void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
213 {
214 std::lock_guard<std::mutex> lock(mHWVsyncLock);
215 if (makeAvailable) {
216 mHWVsyncAvailable = makeAvailable;
217 } else if (!mHWVsyncAvailable) {
218 // Hardware vsync is not currently available, so abort the resync
219 // attempt for now
220 return;
221 }
222 }
223
224 if (period <= 0) {
225 return;
226 }
227
228 setVsyncPeriod(period);
229}
230
231ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
232 std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
233 return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
234 if (const auto vsync = ptr.lock()) {
235 vsync->resync(getVsyncPeriod);
236 }
237 };
238}
239
240void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
241 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
242
243 const nsecs_t now = systemTime();
244 const nsecs_t last = lastResyncTime.exchange(now);
245
246 if (now - last > kIgnoreDelay) {
247 scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
248 }
249}
250
251void Scheduler::setRefreshSkipCount(int count) {
252 mPrimaryDispSync->setRefreshSkipCount(count);
253}
254
Ana Krulece588e312018-09-18 12:32:24 -0700255void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800256 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700257 mPrimaryDispSync->reset();
258 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800259
260 if (!mPrimaryHWVsyncEnabled) {
261 mPrimaryDispSync->beginResync();
262 mEventControlThread->setVsyncEnabled(true);
263 mPrimaryHWVsyncEnabled = true;
264 }
Ana Krulece588e312018-09-18 12:32:24 -0700265}
266
267void Scheduler::addResyncSample(const nsecs_t timestamp) {
268 bool needsHwVsync = false;
269 { // Scope for the lock
270 std::lock_guard<std::mutex> lock(mHWVsyncLock);
271 if (mPrimaryHWVsyncEnabled) {
272 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp);
273 }
274 }
275
276 if (needsHwVsync) {
277 enableHardwareVsync();
278 } else {
279 disableHardwareVsync(false);
280 }
281}
282
283void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
284 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
285 enableHardwareVsync();
286 } else {
287 disableHardwareVsync(false);
288 }
289}
290
291void Scheduler::setIgnorePresentFences(bool ignore) {
292 mPrimaryDispSync->setIgnorePresentFences(ignore);
293}
294
Ady Abrahamc3e21312019-02-07 14:30:23 -0800295nsecs_t Scheduler::expectedPresentTime() {
296 return mPrimaryDispSync->expectedPresentTime();
297}
298
Ady Abraham3aff9172019-02-07 19:10:26 -0800299void Scheduler::dumpPrimaryDispSync(std::string& result) const {
300 mPrimaryDispSync->dump(result);
301}
302
Ady Abraham09bd3922019-04-08 10:44:56 -0700303std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer(
304 const std::string name) {
305 RefreshRateType refreshRateType = RefreshRateType::PERFORMANCE;
306
307 const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType);
308 const uint32_t fps = (refreshRate) ? refreshRate->fps : 0;
309 return mLayerHistory.createLayer(name, fps);
310}
311
312void Scheduler::addLayerPresentTime(
313 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
314 nsecs_t presentTime) {
315 mLayerHistory.insert(layerHandle, presentTime);
Ana Krulec3084c052018-11-21 20:27:17 +0100316}
317
Kevin DuBois413287f2019-02-25 08:46:47 -0800318void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
319 fn(*mPrimaryDispSync);
320}
321
Ady Abraham09bd3922019-04-08 10:44:56 -0700322void Scheduler::updateFpsBasedOnContent() {
323 uint32_t refreshRate = std::round(mLayerHistory.getDesiredRefreshRate());
324 ATRACE_INT("ContentFPS", refreshRate);
325 if (refreshRate > 0) {
326 contentChangeRefreshRate(ContentFeatureState::CONTENT_DETECTION_ON, refreshRate);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800327 } else {
Ady Abraham09bd3922019-04-08 10:44:56 -0700328 contentChangeRefreshRate(ContentFeatureState::CONTENT_DETECTION_OFF, 0);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800329 }
Ana Krulec3084c052018-11-21 20:27:17 +0100330}
331
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800332void Scheduler::setChangeRefreshRateCallback(
333 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800334 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800335 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800336}
337
Ana Krulec3084c052018-11-21 20:27:17 +0100338void Scheduler::updateFrameSkipping(const int64_t skipCount) {
339 ATRACE_INT("FrameSkipCount", skipCount);
340 if (mSkipCount != skipCount) {
341 // Only update DispSync if it hasn't been updated yet.
342 mPrimaryDispSync->setRefreshSkipCount(skipCount);
343 mSkipCount = skipCount;
344 }
345}
346
Ana Krulecfb772822018-11-30 10:44:07 +0100347void Scheduler::resetIdleTimer() {
348 if (mIdleTimer) {
349 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800350 }
351}
352
353void Scheduler::resetTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800354 // We do not notify the applications about config changes when idle timer is reset.
355 timerChangeRefreshRate(IdleTimerState::RESET);
356 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100357}
358
359void Scheduler::expiredTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800360 // We do not notify the applications about config changes when idle timer expires.
361 timerChangeRefreshRate(IdleTimerState::EXPIRED);
362 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100363}
364
Ana Krulecb43429d2019-01-09 14:28:51 -0800365std::string Scheduler::doDump() {
366 std::ostringstream stream;
367 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
368 return stream.str();
369}
370
Ady Abraham09bd3922019-04-08 10:44:56 -0700371void Scheduler::contentChangeRefreshRate(ContentFeatureState contentFeatureState,
372 uint32_t refreshRate) {
373 RefreshRateType newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800374 {
375 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham09bd3922019-04-08 10:44:56 -0700376 mCurrentContentFeatureState = contentFeatureState;
377 mContentRefreshRate = refreshRate;
378 newRefreshRateType = calculateRefreshRateType();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800379 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700380 changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800381}
382
383void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700384 RefreshRateType newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800385 {
386 std::lock_guard<std::mutex> lock(mFeatureStateLock);
387 mCurrentIdleTimerState = idleTimerState;
Ady Abraham09bd3922019-04-08 10:44:56 -0700388 newRefreshRateType = calculateRefreshRateType();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800389 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700390 changeRefreshRate(newRefreshRateType, ConfigEvent::None);
391}
392
393Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
394 // First check if timer has expired as it means there is no new content on the screen
395 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
396 return RefreshRateType::DEFAULT;
397 }
398
399 // If content detection is off we choose performance as we don't know the content fps
400 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
401 return RefreshRateType::PERFORMANCE;
402 }
403
404 // Content detection is on, find the appropriate refresh rate
405 // Start with the smallest refresh rate which is greater than the content
406 auto iter = mRefreshRateConfigs.getRefreshRates().cbegin();
407 RefreshRateType currRefreshRateType = iter->first;
408 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
409 if (iter->second->fps >= mContentRefreshRate) {
410 currRefreshRateType = iter->first;
411 break;
412 }
413 ++iter;
414 }
415
416 if (iter == mRefreshRateConfigs.getRefreshRates().cend()) {
417 return RefreshRateType::PERFORMANCE;
418 }
419
420 // TODO(b/129874336): This logic is sub-optimal for content refresh rate that aligns better
421 // with a higher refresh rate. For example for 45fps we should choose 90Hz config.
422
423 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800424}
425
426void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
427 std::lock_guard<std::mutex> lock(mCallbackLock);
428 if (mChangeRefreshRateCallback) {
429 mChangeRefreshRateCallback(refreshRateType, configEvent);
430 }
431}
432
Ana Krulec98b5b242018-08-10 15:03:23 -0700433} // namespace android