blob: ef53933d2d8b41c024caf46fae2dcf0047c600e1 [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(
Ady Abraham45e4e362019-06-07 18:20:51 -0700122 const char* connectionName, nsecs_t phaseOffsetNs, nsecs_t offsetThresholdForNextVsync,
123 ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -0700124 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
125 const int64_t id = sNextId++;
126 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
127
Ana Krulec98b5b242018-08-10 15:03:23 -0700128 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -0800129 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Ady Abraham45e4e362019-06-07 18:20:51 -0700130 offsetThresholdForNextVsync, std::move(interceptCallback));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800131
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800132 auto eventThreadConnection =
Ady Abrahama1a49af2019-02-07 14:36:55 -0800133 createConnectionInternal(eventThread.get(), std::move(resyncCallback));
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800134 mConnections.emplace(id,
135 std::make_unique<Connection>(new ConnectionHandle(id),
136 eventThreadConnection,
137 std::move(eventThread)));
Ana Krulec98b5b242018-08-10 15:03:23 -0700138 return mConnections[id]->handle;
139}
140
Ana Krulec0c8cd522018-08-31 12:27:28 -0700141std::unique_ptr<EventThread> Scheduler::makeEventThread(
Ady Abraham45e4e362019-06-07 18:20:51 -0700142 const char* connectionName, DispSync* dispSync, nsecs_t phaseOffsetNs,
143 nsecs_t offsetThresholdForNextVsync,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700144 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
145 std::unique_ptr<VSyncSource> eventThreadSource =
Ady Abraham45e4e362019-06-07 18:20:51 -0700146 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, offsetThresholdForNextVsync,
147 true, connectionName);
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800148 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800149 std::move(interceptCallback), connectionName);
150}
151
Ady Abrahama1a49af2019-02-07 14:36:55 -0800152sp<EventThreadConnection> Scheduler::createConnectionInternal(EventThread* eventThread,
153 ResyncCallback&& resyncCallback) {
Ady Abraham8532d012019-05-08 14:50:56 -0700154 return eventThread->createEventConnection(std::move(resyncCallback));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700155}
156
Ana Krulec98b5b242018-08-10 15:03:23 -0700157sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Ady Abrahama1a49af2019-02-07 14:36:55 -0800158 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700159 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800160 return createConnectionInternal(mConnections[handle->id]->thread.get(),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800161 std::move(resyncCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -0700162}
163
164EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700165 RETURN_VALUE_IF_INVALID(nullptr);
166 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700167}
168
Ana Krulec85c39af2018-12-26 17:29:57 -0800169sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700170 RETURN_VALUE_IF_INVALID(nullptr);
171 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700172}
173
174void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800175 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700176 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800177 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700178}
179
180void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700181 RETURN_IF_INVALID();
182 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700183}
184
185void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700186 RETURN_IF_INVALID();
187 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700188}
189
Ady Abraham447052e2019-02-13 16:07:27 -0800190void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
191 int32_t configId) {
192 RETURN_IF_INVALID();
193 mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
194}
195
Yiwei Zhang5434a782018-12-05 18:06:32 -0800196void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700197 RETURN_IF_INVALID();
198 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700199}
200
201void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700202 RETURN_IF_INVALID();
203 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700204}
Ana Krulece588e312018-09-18 12:32:24 -0700205
206void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
207 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
208 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
209}
210
211void Scheduler::enableHardwareVsync() {
212 std::lock_guard<std::mutex> lock(mHWVsyncLock);
213 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
214 mPrimaryDispSync->beginResync();
215 mEventControlThread->setVsyncEnabled(true);
216 mPrimaryHWVsyncEnabled = true;
217 }
218}
219
220void Scheduler::disableHardwareVsync(bool makeUnavailable) {
221 std::lock_guard<std::mutex> lock(mHWVsyncLock);
222 if (mPrimaryHWVsyncEnabled) {
223 mEventControlThread->setVsyncEnabled(false);
224 mPrimaryDispSync->endResync();
225 mPrimaryHWVsyncEnabled = false;
226 }
227 if (makeUnavailable) {
228 mHWVsyncAvailable = false;
229 }
230}
231
Ana Krulecc2870422019-01-29 19:00:58 -0800232void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
233 {
234 std::lock_guard<std::mutex> lock(mHWVsyncLock);
235 if (makeAvailable) {
236 mHWVsyncAvailable = makeAvailable;
237 } else if (!mHWVsyncAvailable) {
238 // Hardware vsync is not currently available, so abort the resync
239 // attempt for now
240 return;
241 }
242 }
243
244 if (period <= 0) {
245 return;
246 }
247
248 setVsyncPeriod(period);
249}
250
251ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
252 std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
253 return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
254 if (const auto vsync = ptr.lock()) {
255 vsync->resync(getVsyncPeriod);
256 }
257 };
258}
259
260void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
261 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
262
263 const nsecs_t now = systemTime();
264 const nsecs_t last = lastResyncTime.exchange(now);
265
266 if (now - last > kIgnoreDelay) {
267 scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
268 }
269}
270
271void Scheduler::setRefreshSkipCount(int count) {
272 mPrimaryDispSync->setRefreshSkipCount(count);
273}
274
Ana Krulece588e312018-09-18 12:32:24 -0700275void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800276 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700277 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800278
279 if (!mPrimaryHWVsyncEnabled) {
280 mPrimaryDispSync->beginResync();
281 mEventControlThread->setVsyncEnabled(true);
282 mPrimaryHWVsyncEnabled = true;
283 }
Ana Krulece588e312018-09-18 12:32:24 -0700284}
285
Alec Mourif8e689c2019-05-20 18:32:22 -0700286void Scheduler::addResyncSample(const nsecs_t timestamp, bool* periodFlushed) {
Ana Krulece588e312018-09-18 12:32:24 -0700287 bool needsHwVsync = false;
Alec Mourif8e689c2019-05-20 18:32:22 -0700288 *periodFlushed = false;
Ana Krulece588e312018-09-18 12:32:24 -0700289 { // Scope for the lock
290 std::lock_guard<std::mutex> lock(mHWVsyncLock);
291 if (mPrimaryHWVsyncEnabled) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700292 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700293 }
294 }
295
296 if (needsHwVsync) {
297 enableHardwareVsync();
298 } else {
299 disableHardwareVsync(false);
300 }
301}
302
303void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
304 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
305 enableHardwareVsync();
306 } else {
307 disableHardwareVsync(false);
308 }
309}
310
311void Scheduler::setIgnorePresentFences(bool ignore) {
312 mPrimaryDispSync->setIgnorePresentFences(ignore);
313}
314
Ady Abrahamc3e21312019-02-07 14:30:23 -0800315nsecs_t Scheduler::expectedPresentTime() {
316 return mPrimaryDispSync->expectedPresentTime();
317}
318
Ady Abraham3aff9172019-02-07 19:10:26 -0800319void Scheduler::dumpPrimaryDispSync(std::string& result) const {
320 mPrimaryDispSync->dump(result);
321}
322
Ady Abraham09bd3922019-04-08 10:44:56 -0700323std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer(
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700324 std::string const& name, int windowType) {
325 RefreshRateType refreshRateType = (windowType == InputWindowInfo::TYPE_WALLPAPER)
326 ? RefreshRateType::DEFAULT
327 : RefreshRateType::PERFORMANCE;
Ady Abraham09bd3922019-04-08 10:44:56 -0700328
329 const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType);
330 const uint32_t fps = (refreshRate) ? refreshRate->fps : 0;
331 return mLayerHistory.createLayer(name, fps);
332}
333
Ady Abrahama315ce72019-04-24 14:35:20 -0700334void Scheduler::addLayerPresentTimeAndHDR(
Ady Abraham09bd3922019-04-08 10:44:56 -0700335 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
Ady Abrahama315ce72019-04-24 14:35:20 -0700336 nsecs_t presentTime, bool isHDR) {
337 mLayerHistory.insert(layerHandle, presentTime, isHDR);
338}
339
340void Scheduler::setLayerVisibility(
341 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, bool visible) {
342 mLayerHistory.setVisibility(layerHandle, visible);
Ana Krulec3084c052018-11-21 20:27:17 +0100343}
344
Kevin DuBois413287f2019-02-25 08:46:47 -0800345void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
346 fn(*mPrimaryDispSync);
347}
348
Ady Abraham09bd3922019-04-08 10:44:56 -0700349void Scheduler::updateFpsBasedOnContent() {
Ady Abrahama315ce72019-04-24 14:35:20 -0700350 auto [refreshRate, isHDR] = mLayerHistory.getDesiredRefreshRateAndHDR();
351 const uint32_t refreshRateRound = std::round(refreshRate);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700352 RefreshRateType newRefreshRateType;
353 {
354 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abrahama315ce72019-04-24 14:35:20 -0700355 if (mContentRefreshRate == refreshRateRound && mIsHDRContent == isHDR) {
Ady Abraham6398a0a2019-04-18 19:30:44 -0700356 return;
357 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700358 mContentRefreshRate = refreshRateRound;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700359 ATRACE_INT("ContentFPS", mContentRefreshRate);
360
Ady Abrahama315ce72019-04-24 14:35:20 -0700361 mIsHDRContent = isHDR;
362 ATRACE_INT("ContentHDR", mIsHDRContent);
363
364 mCurrentContentFeatureState = refreshRateRound > 0
365 ? ContentFeatureState::CONTENT_DETECTION_ON
366 : ContentFeatureState::CONTENT_DETECTION_OFF;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700367 newRefreshRateType = calculateRefreshRateType();
368 if (mRefreshRateType == newRefreshRateType) {
369 return;
370 }
371 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800372 }
Ady Abraham6398a0a2019-04-18 19:30:44 -0700373 changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
Ana Krulec3084c052018-11-21 20:27:17 +0100374}
375
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800376void Scheduler::setChangeRefreshRateCallback(
377 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800378 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800379 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800380}
381
Alec Mouridc28b372019-04-18 21:17:13 -0700382void Scheduler::setGetVsyncPeriodCallback(const GetVsyncPeriod&& getVsyncPeriod) {
383 std::lock_guard<std::mutex> lock(mCallbackLock);
384 mGetVsyncPeriod = getVsyncPeriod;
385}
386
Ana Krulec3084c052018-11-21 20:27:17 +0100387void Scheduler::updateFrameSkipping(const int64_t skipCount) {
388 ATRACE_INT("FrameSkipCount", skipCount);
389 if (mSkipCount != skipCount) {
390 // Only update DispSync if it hasn't been updated yet.
391 mPrimaryDispSync->setRefreshSkipCount(skipCount);
392 mSkipCount = skipCount;
393 }
394}
395
Ana Krulecfb772822018-11-30 10:44:07 +0100396void Scheduler::resetIdleTimer() {
397 if (mIdleTimer) {
398 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800399 }
400}
401
Ady Abraham8532d012019-05-08 14:50:56 -0700402void Scheduler::notifyTouchEvent() {
403 if (mTouchTimer) {
404 mTouchTimer->reset();
405 }
406
407 if (mSupportKernelTimer) {
408 resetIdleTimer();
409 }
410}
411
Ady Abrahama1a49af2019-02-07 14:36:55 -0800412void Scheduler::resetTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800413 timerChangeRefreshRate(IdleTimerState::RESET);
414 ATRACE_INT("ExpiredIdleTimer", 0);
Ana Krulecfb772822018-11-30 10:44:07 +0100415}
416
Alec Mouridc28b372019-04-18 21:17:13 -0700417void Scheduler::resetKernelTimerCallback() {
418 ATRACE_INT("ExpiredKernelIdleTimer", 0);
419 std::lock_guard<std::mutex> lock(mCallbackLock);
420 if (mGetVsyncPeriod) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700421 resyncToHardwareVsync(true, mGetVsyncPeriod());
Alec Mouridc28b372019-04-18 21:17:13 -0700422 }
423}
424
Ana Krulecfb772822018-11-30 10:44:07 +0100425void Scheduler::expiredTimerCallback() {
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800426 timerChangeRefreshRate(IdleTimerState::EXPIRED);
427 ATRACE_INT("ExpiredIdleTimer", 1);
Ana Krulecfb772822018-11-30 10:44:07 +0100428}
429
Ady Abraham8532d012019-05-08 14:50:56 -0700430void Scheduler::resetTouchTimerCallback() {
431 // We do not notify the applications about config changes when idle timer is reset.
432 touchChangeRefreshRate(TouchState::ACTIVE);
433 ATRACE_INT("TouchState", 1);
434}
435
436void Scheduler::expiredTouchTimerCallback() {
437 // We do not notify the applications about config changes when idle timer expires.
438 touchChangeRefreshRate(TouchState::INACTIVE);
439 ATRACE_INT("TouchState", 0);
440}
441
Alec Mouridc28b372019-04-18 21:17:13 -0700442void Scheduler::expiredKernelTimerCallback() {
443 ATRACE_INT("ExpiredKernelIdleTimer", 1);
444 // Disable HW Vsync if the timer expired, as we don't need it
445 // enabled if we're not pushing frames.
446 disableHardwareVsync(false);
447}
448
Ana Krulecb43429d2019-01-09 14:28:51 -0800449std::string Scheduler::doDump() {
450 std::ostringstream stream;
451 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
Ady Abraham8532d012019-05-08 14:50:56 -0700452 stream << "+ Touch timer interval: " << mSetTouchTimerMs << " ms" << std::endl;
Ana Krulecb43429d2019-01-09 14:28:51 -0800453 return stream.str();
454}
455
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800456void Scheduler::timerChangeRefreshRate(IdleTimerState idleTimerState) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700457 RefreshRateType newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800458 {
459 std::lock_guard<std::mutex> lock(mFeatureStateLock);
Ady Abraham6398a0a2019-04-18 19:30:44 -0700460 if (mCurrentIdleTimerState == idleTimerState) {
461 return;
462 }
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800463 mCurrentIdleTimerState = idleTimerState;
Ady Abraham09bd3922019-04-08 10:44:56 -0700464 newRefreshRateType = calculateRefreshRateType();
Ady Abraham6398a0a2019-04-18 19:30:44 -0700465 if (mRefreshRateType == newRefreshRateType) {
466 return;
467 }
468 mRefreshRateType = newRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800469 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700470 changeRefreshRate(newRefreshRateType, ConfigEvent::None);
471}
472
Ady Abraham8532d012019-05-08 14:50:56 -0700473void Scheduler::touchChangeRefreshRate(TouchState touchState) {
474 ConfigEvent event = ConfigEvent::None;
475 RefreshRateType newRefreshRateType;
476 {
477 std::lock_guard<std::mutex> lock(mFeatureStateLock);
478 if (mCurrentTouchState == touchState) {
479 return;
480 }
481 mCurrentTouchState = touchState;
482 newRefreshRateType = calculateRefreshRateType();
483 if (mRefreshRateType == newRefreshRateType) {
484 return;
485 }
486 mRefreshRateType = newRefreshRateType;
487 // Send an event in case that content detection is on as touch has a higher priority
488 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_ON) {
489 event = ConfigEvent::Changed;
490 }
491 }
492 changeRefreshRate(newRefreshRateType, event);
493}
494
Ady Abraham09bd3922019-04-08 10:44:56 -0700495Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
Ady Abraham8532d012019-05-08 14:50:56 -0700496 // HDR content is not supported on PERFORMANCE mode
497 if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700498 return RefreshRateType::DEFAULT;
499 }
500
Ady Abraham8532d012019-05-08 14:50:56 -0700501 // As long as touch is active we want to be in performance mode
502 if (mCurrentTouchState == TouchState::ACTIVE) {
503 return RefreshRateType::PERFORMANCE;
504 }
505
506 // If timer has expired as it means there is no new content on the screen
507 if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
Ady Abrahama315ce72019-04-24 14:35:20 -0700508 return RefreshRateType::DEFAULT;
509 }
510
Ady Abraham09bd3922019-04-08 10:44:56 -0700511 // If content detection is off we choose performance as we don't know the content fps
512 if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
513 return RefreshRateType::PERFORMANCE;
514 }
515
516 // Content detection is on, find the appropriate refresh rate
Ady Abraham85b3f012019-04-08 11:04:14 -0700517 // Start with the smallest refresh rate which is within a margin of the content
518 RefreshRateType currRefreshRateType = RefreshRateType::PERFORMANCE;
519 constexpr float MARGIN = 0.05f;
Ady Abraham09bd3922019-04-08 10:44:56 -0700520 auto iter = mRefreshRateConfigs.getRefreshRates().cbegin();
Ady Abraham09bd3922019-04-08 10:44:56 -0700521 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
Ady Abraham85b3f012019-04-08 11:04:14 -0700522 if (iter->second->fps >= mContentRefreshRate * (1 - MARGIN)) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700523 currRefreshRateType = iter->first;
524 break;
525 }
526 ++iter;
527 }
528
Ady Abraham85b3f012019-04-08 11:04:14 -0700529 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
530 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
531 // align well with both
532 float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps /
533 float(mContentRefreshRate);
534 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
535 while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
536 ratio = iter->second->fps / float(mContentRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -0700537
Ady Abraham85b3f012019-04-08 11:04:14 -0700538 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
539 currRefreshRateType = iter->first;
540 break;
541 }
542 ++iter;
543 }
544 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700545
546 return currRefreshRateType;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800547}
548
549void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
550 std::lock_guard<std::mutex> lock(mCallbackLock);
551 if (mChangeRefreshRateCallback) {
552 mChangeRefreshRateCallback(refreshRateType, configEvent);
553 }
554}
555
Ana Krulec98b5b242018-08-10 15:03:23 -0700556} // namespace android