blob: f3a7f87e2fe4ca8fdcfea5a28a6e8fe403217b7d [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 Krulece588e312018-09-18 12:32:24 -070031#include <ui/DisplayStatInfo.h>
Ana Krulec3084c052018-11-21 20:27:17 +010032#include <utils/Timers.h>
Ana Krulec7ab56032018-11-02 20:51:06 +010033#include <utils/Trace.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070034
35#include "DispSync.h"
36#include "DispSyncSource.h"
Ana Krulece588e312018-09-18 12:32:24 -070037#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070038#include "EventThread.h"
Ana Krulecfb772822018-11-30 10:44:07 +010039#include "IdleTimer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070040#include "InjectVSyncSource.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010041#include "SchedulerUtils.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090042#include "SurfaceFlingerProperties.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070043
44namespace android {
45
Ana Krulece588e312018-09-18 12:32:24 -070046using namespace android::hardware::configstore;
47using namespace android::hardware::configstore::V1_0;
Sundong Ahnd5e08f62018-12-12 20:27:28 +090048using namespace android::sysprop;
Ana Krulece588e312018-09-18 12:32:24 -070049
Ana Krulec0c8cd522018-08-31 12:27:28 -070050#define RETURN_VALUE_IF_INVALID(value) \
51 if (handle == nullptr || mConnections.count(handle->id) == 0) return value
52#define RETURN_IF_INVALID() \
53 if (handle == nullptr || mConnections.count(handle->id) == 0) return
54
Ana Krulec98b5b242018-08-10 15:03:23 -070055std::atomic<int64_t> Scheduler::sNextId = 0;
56
Ana Krulece588e312018-09-18 12:32:24 -070057Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function)
Sundong Ahnd5e08f62018-12-12 20:27:28 +090058 : mHasSyncFramework(running_without_sync_framework(true)),
59 mDispSyncPresentTimeOffset(present_time_offset_from_vsync_ns(0)),
Ana Krulece588e312018-09-18 12:32:24 -070060 mPrimaryHWVsyncEnabled(false),
61 mHWVsyncAvailable(false) {
62 // Note: We create a local temporary with the real DispSync implementation
63 // type temporarily so we can initialize it with the configured values,
64 // before storing it for more generic use using the interface type.
65 auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync");
66 primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset);
67 mPrimaryDispSync = std::move(primaryDispSync);
68 mEventControlThread = std::make_unique<impl::EventControlThread>(function);
Ana Krulecfb772822018-11-30 10:44:07 +010069
Ady Abrahambe59c0d2019-03-05 13:01:13 -080070 mSetIdleTimerMs = set_idle_timer_ms(0);
71
Ana Krulecfb772822018-11-30 10:44:07 +010072 char value[PROPERTY_VALUE_MAX];
Ana Kruleca5bdd9d2019-01-29 19:00:58 -080073 property_get("debug.sf.set_idle_timer_ms", value, "0");
Ady Abrahambe59c0d2019-03-05 13:01:13 -080074 int int_value = atoi(value);
75 if (int_value) {
76 mSetIdleTimerMs = atoi(value);
77 }
Ana Krulecfb772822018-11-30 10:44:07 +010078
79 if (mSetIdleTimerMs > 0) {
80 mIdleTimer =
81 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetIdleTimerMs),
Ady Abrahama1a49af2019-02-07 14:36:55 -080082 [this] { resetTimerCallback(); },
Ana Krulecfb772822018-11-30 10:44:07 +010083 [this] { expiredTimerCallback(); });
84 mIdleTimer->start();
85 }
Ana Krulece588e312018-09-18 12:32:24 -070086}
87
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080088Scheduler::~Scheduler() {
89 // Ensure the IdleTimer thread is joined before we start destroying state.
90 mIdleTimer.reset();
91}
Ana Krulec0c8cd522018-08-31 12:27:28 -070092
Ana Krulec98b5b242018-08-10 15:03:23 -070093sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Dominik Laskowskibd52c842019-01-28 18:11:23 -080094 const char* connectionName, int64_t phaseOffsetNs, ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -070095 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
96 const int64_t id = sNextId++;
97 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
98
Ana Krulec98b5b242018-08-10 15:03:23 -070099 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -0800100 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800101 std::move(interceptCallback));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800102
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800103 auto eventThreadConnection =
Ady Abrahama1a49af2019-02-07 14:36:55 -0800104 createConnectionInternal(eventThread.get(), std::move(resyncCallback));
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800105 mConnections.emplace(id,
106 std::make_unique<Connection>(new ConnectionHandle(id),
107 eventThreadConnection,
108 std::move(eventThread)));
Ana Krulec98b5b242018-08-10 15:03:23 -0700109 return mConnections[id]->handle;
110}
111
Ana Krulec0c8cd522018-08-31 12:27:28 -0700112std::unique_ptr<EventThread> Scheduler::makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800113 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700114 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
115 std::unique_ptr<VSyncSource> eventThreadSource =
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800116 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName);
117 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800118 std::move(interceptCallback), connectionName);
119}
120
Ady Abrahama1a49af2019-02-07 14:36:55 -0800121sp<EventThreadConnection> Scheduler::createConnectionInternal(EventThread* eventThread,
122 ResyncCallback&& resyncCallback) {
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800123 return eventThread->createEventConnection(std::move(resyncCallback),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800124 [this] { resetIdleTimer(); });
Ana Krulec0c8cd522018-08-31 12:27:28 -0700125}
126
Ana Krulec98b5b242018-08-10 15:03:23 -0700127sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Ady Abrahama1a49af2019-02-07 14:36:55 -0800128 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700129 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800130 return createConnectionInternal(mConnections[handle->id]->thread.get(),
Ady Abrahama1a49af2019-02-07 14:36:55 -0800131 std::move(resyncCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -0700132}
133
134EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700135 RETURN_VALUE_IF_INVALID(nullptr);
136 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700137}
138
Ana Krulec85c39af2018-12-26 17:29:57 -0800139sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700140 RETURN_VALUE_IF_INVALID(nullptr);
141 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700142}
143
144void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800145 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700146 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800147 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700148}
149
150void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700151 RETURN_IF_INVALID();
152 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700153}
154
155void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700156 RETURN_IF_INVALID();
157 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700158}
159
Ady Abraham447052e2019-02-13 16:07:27 -0800160void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
161 int32_t configId) {
162 RETURN_IF_INVALID();
163 mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
164}
165
Yiwei Zhang5434a782018-12-05 18:06:32 -0800166void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700167 RETURN_IF_INVALID();
168 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700169}
170
171void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700172 RETURN_IF_INVALID();
173 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700174}
Ana Krulece588e312018-09-18 12:32:24 -0700175
Ady Abrahamb838aed2019-02-12 15:30:16 -0800176void Scheduler::pauseVsyncCallback(const android::sp<android::Scheduler::ConnectionHandle>& handle,
177 bool pause) {
178 RETURN_IF_INVALID();
179 mConnections[handle->id]->thread->pauseVsyncCallback(pause);
180}
181
Ana Krulece588e312018-09-18 12:32:24 -0700182void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
183 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
184 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
185}
186
187void Scheduler::enableHardwareVsync() {
188 std::lock_guard<std::mutex> lock(mHWVsyncLock);
189 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
190 mPrimaryDispSync->beginResync();
191 mEventControlThread->setVsyncEnabled(true);
192 mPrimaryHWVsyncEnabled = true;
193 }
194}
195
196void Scheduler::disableHardwareVsync(bool makeUnavailable) {
197 std::lock_guard<std::mutex> lock(mHWVsyncLock);
198 if (mPrimaryHWVsyncEnabled) {
199 mEventControlThread->setVsyncEnabled(false);
200 mPrimaryDispSync->endResync();
201 mPrimaryHWVsyncEnabled = false;
202 }
203 if (makeUnavailable) {
204 mHWVsyncAvailable = false;
205 }
206}
207
Ana Krulecc2870422019-01-29 19:00:58 -0800208void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
209 {
210 std::lock_guard<std::mutex> lock(mHWVsyncLock);
211 if (makeAvailable) {
212 mHWVsyncAvailable = makeAvailable;
213 } else if (!mHWVsyncAvailable) {
214 // Hardware vsync is not currently available, so abort the resync
215 // attempt for now
216 return;
217 }
218 }
219
220 if (period <= 0) {
221 return;
222 }
223
224 setVsyncPeriod(period);
225}
226
227ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
228 std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
229 return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
230 if (const auto vsync = ptr.lock()) {
231 vsync->resync(getVsyncPeriod);
232 }
233 };
234}
235
236void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
237 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
238
239 const nsecs_t now = systemTime();
240 const nsecs_t last = lastResyncTime.exchange(now);
241
242 if (now - last > kIgnoreDelay) {
243 scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
244 }
245}
246
247void Scheduler::setRefreshSkipCount(int count) {
248 mPrimaryDispSync->setRefreshSkipCount(count);
249}
250
Ana Krulece588e312018-09-18 12:32:24 -0700251void Scheduler::setVsyncPeriod(const nsecs_t period) {
Ady Abraham3aff9172019-02-07 19:10:26 -0800252 std::lock_guard<std::mutex> lock(mHWVsyncLock);
Ana Krulece588e312018-09-18 12:32:24 -0700253 mPrimaryDispSync->reset();
254 mPrimaryDispSync->setPeriod(period);
Ady Abraham3aff9172019-02-07 19:10:26 -0800255
256 if (!mPrimaryHWVsyncEnabled) {
257 mPrimaryDispSync->beginResync();
258 mEventControlThread->setVsyncEnabled(true);
259 mPrimaryHWVsyncEnabled = true;
260 }
Ana Krulece588e312018-09-18 12:32:24 -0700261}
262
263void Scheduler::addResyncSample(const nsecs_t timestamp) {
264 bool needsHwVsync = false;
265 { // Scope for the lock
266 std::lock_guard<std::mutex> lock(mHWVsyncLock);
267 if (mPrimaryHWVsyncEnabled) {
268 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp);
269 }
270 }
271
272 if (needsHwVsync) {
273 enableHardwareVsync();
274 } else {
275 disableHardwareVsync(false);
276 }
277}
278
279void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
280 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
281 enableHardwareVsync();
282 } else {
283 disableHardwareVsync(false);
284 }
285}
286
287void Scheduler::setIgnorePresentFences(bool ignore) {
288 mPrimaryDispSync->setIgnorePresentFences(ignore);
289}
290
Ady Abrahamc3e21312019-02-07 14:30:23 -0800291nsecs_t Scheduler::expectedPresentTime() {
292 return mPrimaryDispSync->expectedPresentTime();
293}
294
Ady Abraham3aff9172019-02-07 19:10:26 -0800295void Scheduler::dumpPrimaryDispSync(std::string& result) const {
296 mPrimaryDispSync->dump(result);
297}
298
Ana Krulec3084c052018-11-21 20:27:17 +0100299void Scheduler::addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
300 const std::string layerName) {
301 // This is V1 logic. It calculates the average FPS based on the timestamp frequency
302 // regardless of which layer the timestamp came from.
303 // For now, the averages and FPS are recorded in the systrace.
304 determineTimestampAverage(isAutoTimestamp, framePresentTime);
305
306 // This is V2 logic. It calculates the average and median timestamp difference based on the
307 // individual layer history. The results are recorded in the systrace.
308 determineLayerTimestampStats(layerName, framePresentTime);
309}
310
311void Scheduler::incrementFrameCounter() {
Ana Krulec6da0e492019-02-19 14:46:01 -0800312 std::lock_guard<std::mutex> lock(mLayerHistoryLock);
Ana Krulec3084c052018-11-21 20:27:17 +0100313 mLayerHistory.incrementCounter();
314}
315
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800316void Scheduler::setChangeRefreshRateCallback(
317 const ChangeRefreshRateCallback& changeRefreshRateCallback) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800318 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800319 mChangeRefreshRateCallback = changeRefreshRateCallback;
Ady Abrahama1a49af2019-02-07 14:36:55 -0800320}
321
Ana Krulec3084c052018-11-21 20:27:17 +0100322void Scheduler::updateFrameSkipping(const int64_t skipCount) {
323 ATRACE_INT("FrameSkipCount", skipCount);
324 if (mSkipCount != skipCount) {
325 // Only update DispSync if it hasn't been updated yet.
326 mPrimaryDispSync->setRefreshSkipCount(skipCount);
327 mSkipCount = skipCount;
328 }
329}
330
331void Scheduler::determineLayerTimestampStats(const std::string layerName,
332 const nsecs_t framePresentTime) {
Ana Krulec3084c052018-11-21 20:27:17 +0100333 std::vector<int64_t> differencesMs;
Ana Krulec434c22d2018-11-28 13:48:36 +0100334 std::string differencesText = "";
Ana Krulec6da0e492019-02-19 14:46:01 -0800335 {
336 std::lock_guard<std::mutex> lock(mLayerHistoryLock);
337 mLayerHistory.insert(layerName, framePresentTime);
338
339 // Traverse through the layer history, and determine the differences in present times.
340 nsecs_t newestPresentTime = framePresentTime;
341 for (int i = 1; i < mLayerHistory.getSize(); i++) {
342 std::unordered_map<std::string, nsecs_t> layers = mLayerHistory.get(i);
343 for (auto layer : layers) {
344 if (layer.first != layerName) {
345 continue;
346 }
347 int64_t differenceMs = (newestPresentTime - layer.second) / 1000000;
348 // Dismiss noise.
349 if (differenceMs > 10 && differenceMs < 60) {
350 differencesMs.push_back(differenceMs);
351 }
352 IF_ALOGV() { differencesText += (std::to_string(differenceMs) + " "); }
353 newestPresentTime = layer.second;
Ana Krulec3084c052018-11-21 20:27:17 +0100354 }
Ana Krulec3084c052018-11-21 20:27:17 +0100355 }
356 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100357 ALOGV("Layer %s timestamp intervals: %s", layerName.c_str(), differencesText.c_str());
Ana Krulec3084c052018-11-21 20:27:17 +0100358
Ana Krulec434c22d2018-11-28 13:48:36 +0100359 if (!differencesMs.empty()) {
360 // Mean/Average is a good indicator for when 24fps videos are playing, because the frames
361 // come in 33, and 49 ms intervals with occasional 41ms.
362 const int64_t meanMs = scheduler::calculate_mean(differencesMs);
363 const auto tagMean = "TimestampMean_" + layerName;
364 ATRACE_INT(tagMean.c_str(), meanMs);
365
366 // Mode and median are good indicators for 30 and 60 fps videos, because the majority of
367 // frames come in 16, or 33 ms intervals.
Ana Krulec3084c052018-11-21 20:27:17 +0100368 const auto tagMedian = "TimestampMedian_" + layerName;
Ana Krulec434c22d2018-11-28 13:48:36 +0100369 ATRACE_INT(tagMedian.c_str(), scheduler::calculate_median(&differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100370
Ana Krulec434c22d2018-11-28 13:48:36 +0100371 const auto tagMode = "TimestampMode_" + layerName;
372 ATRACE_INT(tagMode.c_str(), scheduler::calculate_mode(differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100373 }
Ana Krulec3084c052018-11-21 20:27:17 +0100374}
375
376void Scheduler::determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100377 ATRACE_INT("AutoTimestamp", isAutoTimestamp);
Ana Krulec3084c052018-11-21 20:27:17 +0100378
Ana Krulec7ab56032018-11-02 20:51:06 +0100379 // Video does not have timestamp automatically set, so we discard timestamps that are
380 // coming in from other sources for now.
381 if (isAutoTimestamp) {
382 return;
383 }
Ana Krulec3084c052018-11-21 20:27:17 +0100384 int64_t differenceMs = (framePresentTime - mPreviousFrameTimestamp) / 1000000;
385 mPreviousFrameTimestamp = framePresentTime;
Ana Krulec7ab56032018-11-02 20:51:06 +0100386
387 if (differenceMs < 10 || differenceMs > 100) {
388 // Dismiss noise.
389 return;
390 }
391 ATRACE_INT("TimestampDiff", differenceMs);
392
Ana Krulec434c22d2018-11-28 13:48:36 +0100393 mTimeDifferences[mCounter % scheduler::ARRAY_SIZE] = differenceMs;
Ana Krulec7ab56032018-11-02 20:51:06 +0100394 mCounter++;
Ana Krulec434c22d2018-11-28 13:48:36 +0100395 int64_t mean = scheduler::calculate_mean(mTimeDifferences);
396 ATRACE_INT("AutoTimestampMean", mean);
Ana Krulec7ab56032018-11-02 20:51:06 +0100397
398 // TODO(b/113612090): This are current numbers from trial and error while running videos
399 // from YouTube at 24, 30, and 60 fps.
Ana Krulec434c22d2018-11-28 13:48:36 +0100400 if (mean > 14 && mean < 18) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800401 ATRACE_INT("MediaFPS", 60);
Ana Krulec434c22d2018-11-28 13:48:36 +0100402 } else if (mean > 31 && mean < 34) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800403 ATRACE_INT("MediaFPS", 30);
Ana Krulec7ab56032018-11-02 20:51:06 +0100404 return;
Ana Krulec434c22d2018-11-28 13:48:36 +0100405 } else if (mean > 39 && mean < 42) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800406 ATRACE_INT("MediaFPS", 24);
Ana Krulec7ab56032018-11-02 20:51:06 +0100407 }
Ana Krulec7ab56032018-11-02 20:51:06 +0100408}
409
Ana Krulecfb772822018-11-30 10:44:07 +0100410void Scheduler::resetIdleTimer() {
411 if (mIdleTimer) {
412 mIdleTimer->reset();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800413 }
414}
415
416void Scheduler::resetTimerCallback() {
417 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800418 if (mChangeRefreshRateCallback) {
419 // We do not notify the applications about config changes when idle timer is reset.
420 mChangeRefreshRateCallback(RefreshRateType::PERFORMANCE, ConfigEvent::None);
Ana Krulecfb772822018-11-30 10:44:07 +0100421 ATRACE_INT("ExpiredIdleTimer", 0);
422 }
423}
424
425void Scheduler::expiredTimerCallback() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800426 std::lock_guard<std::mutex> lock(mCallbackLock);
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800427 if (mChangeRefreshRateCallback) {
428 // We do not notify the applications about config changes when idle timer expires.
429 mChangeRefreshRateCallback(RefreshRateType::DEFAULT, ConfigEvent::None);
Ana Krulec7d1d6832018-12-27 11:10:09 -0800430 ATRACE_INT("ExpiredIdleTimer", 1);
431 }
Ana Krulecfb772822018-11-30 10:44:07 +0100432}
433
Ana Krulecb43429d2019-01-09 14:28:51 -0800434std::string Scheduler::doDump() {
435 std::ostringstream stream;
436 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
437 return stream.str();
438}
439
Ana Krulec98b5b242018-08-10 15:03:23 -0700440} // namespace android