blob: fec53af617ad57b8ea6469022cf3a0314749ff3f [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>
29#include <android/hardware/configstore/1.2/ISurfaceFlingerConfigs.h>
30#include <configstore/Utils.h>
31
Ana Krulecfb772822018-11-30 10:44:07 +010032#include <cutils/properties.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070033#include <gui/ISurfaceComposer.h>
Ana Krulece588e312018-09-18 12:32:24 -070034#include <ui/DisplayStatInfo.h>
Ana Krulec3084c052018-11-21 20:27:17 +010035#include <utils/Timers.h>
Ana Krulec7ab56032018-11-02 20:51:06 +010036#include <utils/Trace.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070037
38#include "DispSync.h"
39#include "DispSyncSource.h"
Ana Krulece588e312018-09-18 12:32:24 -070040#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070041#include "EventThread.h"
Ana Krulecfb772822018-11-30 10:44:07 +010042#include "IdleTimer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070043#include "InjectVSyncSource.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010044#include "SchedulerUtils.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;
50
Ana Krulec0c8cd522018-08-31 12:27:28 -070051#define RETURN_VALUE_IF_INVALID(value) \
52 if (handle == nullptr || mConnections.count(handle->id) == 0) return value
53#define RETURN_IF_INVALID() \
54 if (handle == nullptr || mConnections.count(handle->id) == 0) return
55
Ana Krulec98b5b242018-08-10 15:03:23 -070056std::atomic<int64_t> Scheduler::sNextId = 0;
57
Ana Krulece588e312018-09-18 12:32:24 -070058Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function)
59 : mHasSyncFramework(
60 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasSyncFramework>(true)),
61 mDispSyncPresentTimeOffset(
62 getInt64<ISurfaceFlingerConfigs,
63 &ISurfaceFlingerConfigs::presentTimeOffsetFromVSyncNs>(0)),
64 mPrimaryHWVsyncEnabled(false),
65 mHWVsyncAvailable(false) {
66 // 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
74 char value[PROPERTY_VALUE_MAX];
75 property_get("debug.sf.set_idle_timer_ms", value, "0");
76 mSetIdleTimerMs = atoi(value);
77
78 if (mSetIdleTimerMs > 0) {
79 mIdleTimer =
80 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetIdleTimerMs),
81 [this] { expiredTimerCallback(); });
82 mIdleTimer->start();
83 }
Ana Krulece588e312018-09-18 12:32:24 -070084}
85
Ana Krulec0c8cd522018-08-31 12:27:28 -070086Scheduler::~Scheduler() = default;
87
Ana Krulec98b5b242018-08-10 15:03:23 -070088sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Ana Krulece588e312018-09-18 12:32:24 -070089 const std::string& connectionName, int64_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -070090 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
91 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
92 const int64_t id = sNextId++;
93 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
94
Ana Krulec98b5b242018-08-10 15:03:23 -070095 std::unique_ptr<EventThread> eventThread =
Ana Krulece588e312018-09-18 12:32:24 -070096 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs, resyncCallback,
Ana Krulec0c8cd522018-08-31 12:27:28 -070097 interceptCallback);
Ana Krulec98b5b242018-08-10 15:03:23 -070098 auto connection = std::make_unique<Connection>(new ConnectionHandle(id),
99 eventThread->createEventConnection(),
100 std::move(eventThread));
101 mConnections.insert(std::make_pair(id, std::move(connection)));
102 return mConnections[id]->handle;
103}
104
Ana Krulec0c8cd522018-08-31 12:27:28 -0700105std::unique_ptr<EventThread> Scheduler::makeEventThread(
Ana Krulec1f027912018-09-10 21:36:25 +0000106 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700107 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
108 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ana Krulec1f027912018-09-10 21:36:25 +0000109 const std::string sourceName = connectionName + "Source";
Ana Krulec0c8cd522018-08-31 12:27:28 -0700110 std::unique_ptr<VSyncSource> eventThreadSource =
Ana Krulec1f027912018-09-10 21:36:25 +0000111 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, sourceName.c_str());
112 const std::string threadName = connectionName + "Thread";
Ana Krulec0c8cd522018-08-31 12:27:28 -0700113 return std::make_unique<impl::EventThread>(std::move(eventThreadSource), resyncCallback,
Ana Krulecfb772822018-11-30 10:44:07 +0100114 interceptCallback, [this] { resetIdleTimer(); },
115 threadName.c_str());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700116}
117
Ana Krulec98b5b242018-08-10 15:03:23 -0700118sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
119 const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700120 RETURN_VALUE_IF_INVALID(nullptr);
121 return mConnections[handle->id]->thread->createEventConnection();
Ana Krulec98b5b242018-08-10 15:03:23 -0700122}
123
124EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700125 RETURN_VALUE_IF_INVALID(nullptr);
126 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700127}
128
Ana Krulec85c39af2018-12-26 17:29:57 -0800129sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700130 RETURN_VALUE_IF_INVALID(nullptr);
131 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700132}
133
134void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
135 EventThread::DisplayType displayType, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700136 RETURN_IF_INVALID();
137 mConnections[handle->id]->thread->onHotplugReceived(displayType, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700138}
139
140void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700141 RETURN_IF_INVALID();
142 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700143}
144
145void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700146 RETURN_IF_INVALID();
147 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700148}
149
Yiwei Zhang5434a782018-12-05 18:06:32 -0800150void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700151 RETURN_IF_INVALID();
152 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700153}
154
155void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700156 RETURN_IF_INVALID();
157 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700158}
Ana Krulece588e312018-09-18 12:32:24 -0700159
160void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
161 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
162 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
163}
164
165void Scheduler::enableHardwareVsync() {
166 std::lock_guard<std::mutex> lock(mHWVsyncLock);
167 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
168 mPrimaryDispSync->beginResync();
169 mEventControlThread->setVsyncEnabled(true);
170 mPrimaryHWVsyncEnabled = true;
171 }
172}
173
174void Scheduler::disableHardwareVsync(bool makeUnavailable) {
175 std::lock_guard<std::mutex> lock(mHWVsyncLock);
176 if (mPrimaryHWVsyncEnabled) {
177 mEventControlThread->setVsyncEnabled(false);
178 mPrimaryDispSync->endResync();
179 mPrimaryHWVsyncEnabled = false;
180 }
181 if (makeUnavailable) {
182 mHWVsyncAvailable = false;
183 }
184}
185
186void Scheduler::setVsyncPeriod(const nsecs_t period) {
187 mPrimaryDispSync->reset();
188 mPrimaryDispSync->setPeriod(period);
189 enableHardwareVsync();
190}
191
192void Scheduler::addResyncSample(const nsecs_t timestamp) {
193 bool needsHwVsync = false;
194 { // Scope for the lock
195 std::lock_guard<std::mutex> lock(mHWVsyncLock);
196 if (mPrimaryHWVsyncEnabled) {
197 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp);
198 }
199 }
200
201 if (needsHwVsync) {
202 enableHardwareVsync();
203 } else {
204 disableHardwareVsync(false);
205 }
206}
207
208void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
209 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
210 enableHardwareVsync();
211 } else {
212 disableHardwareVsync(false);
213 }
214}
215
216void Scheduler::setIgnorePresentFences(bool ignore) {
217 mPrimaryDispSync->setIgnorePresentFences(ignore);
218}
219
Ana Krulec7ab56032018-11-02 20:51:06 +0100220void Scheduler::makeHWSyncAvailable(bool makeAvailable) {
221 std::lock_guard<std::mutex> lock(mHWVsyncLock);
222 mHWVsyncAvailable = makeAvailable;
223}
224
Ana Krulec3084c052018-11-21 20:27:17 +0100225void Scheduler::addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
226 const std::string layerName) {
227 // This is V1 logic. It calculates the average FPS based on the timestamp frequency
228 // regardless of which layer the timestamp came from.
229 // For now, the averages and FPS are recorded in the systrace.
230 determineTimestampAverage(isAutoTimestamp, framePresentTime);
231
232 // This is V2 logic. It calculates the average and median timestamp difference based on the
233 // individual layer history. The results are recorded in the systrace.
234 determineLayerTimestampStats(layerName, framePresentTime);
235}
236
237void Scheduler::incrementFrameCounter() {
238 mLayerHistory.incrementCounter();
239}
240
Ana Krulec7d1d6832018-12-27 11:10:09 -0800241void Scheduler::setExpiredIdleTimerCallback(const ExpiredIdleTimerCallback& expiredTimerCallback) {
242 std::lock_guard<std::mutex> lock(mCallbackLock);
243 mExpiredTimerCallback = expiredTimerCallback;
244}
245
246void Scheduler::setResetIdleTimerCallback(const ResetIdleTimerCallback& resetTimerCallback) {
247 std::lock_guard<std::mutex> lock(mCallbackLock);
248 mResetTimerCallback = resetTimerCallback;
249}
250
Ana Krulec3084c052018-11-21 20:27:17 +0100251void Scheduler::updateFrameSkipping(const int64_t skipCount) {
252 ATRACE_INT("FrameSkipCount", skipCount);
253 if (mSkipCount != skipCount) {
254 // Only update DispSync if it hasn't been updated yet.
255 mPrimaryDispSync->setRefreshSkipCount(skipCount);
256 mSkipCount = skipCount;
257 }
258}
259
260void Scheduler::determineLayerTimestampStats(const std::string layerName,
261 const nsecs_t framePresentTime) {
262 mLayerHistory.insert(layerName, framePresentTime);
263 std::vector<int64_t> differencesMs;
264
265 // Traverse through the layer history, and determine the differences in present times.
266 nsecs_t newestPresentTime = framePresentTime;
Ana Krulec434c22d2018-11-28 13:48:36 +0100267 std::string differencesText = "";
Ana Krulec3084c052018-11-21 20:27:17 +0100268 for (int i = 1; i < mLayerHistory.getSize(); i++) {
269 std::unordered_map<std::string, nsecs_t> layers = mLayerHistory.get(i);
270 for (auto layer : layers) {
271 if (layer.first != layerName) {
272 continue;
273 }
274 int64_t differenceMs = (newestPresentTime - layer.second) / 1000000;
Ana Krulec3084c052018-11-21 20:27:17 +0100275 // Dismiss noise.
276 if (differenceMs > 10 && differenceMs < 60) {
277 differencesMs.push_back(differenceMs);
278 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100279 IF_ALOGV() { differencesText += (std::to_string(differenceMs) + " "); }
Ana Krulec3084c052018-11-21 20:27:17 +0100280 newestPresentTime = layer.second;
281 }
282 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100283 ALOGV("Layer %s timestamp intervals: %s", layerName.c_str(), differencesText.c_str());
Ana Krulec3084c052018-11-21 20:27:17 +0100284
Ana Krulec434c22d2018-11-28 13:48:36 +0100285 if (!differencesMs.empty()) {
286 // Mean/Average is a good indicator for when 24fps videos are playing, because the frames
287 // come in 33, and 49 ms intervals with occasional 41ms.
288 const int64_t meanMs = scheduler::calculate_mean(differencesMs);
289 const auto tagMean = "TimestampMean_" + layerName;
290 ATRACE_INT(tagMean.c_str(), meanMs);
291
292 // Mode and median are good indicators for 30 and 60 fps videos, because the majority of
293 // frames come in 16, or 33 ms intervals.
Ana Krulec3084c052018-11-21 20:27:17 +0100294 const auto tagMedian = "TimestampMedian_" + layerName;
Ana Krulec434c22d2018-11-28 13:48:36 +0100295 ATRACE_INT(tagMedian.c_str(), scheduler::calculate_median(&differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100296
Ana Krulec434c22d2018-11-28 13:48:36 +0100297 const auto tagMode = "TimestampMode_" + layerName;
298 ATRACE_INT(tagMode.c_str(), scheduler::calculate_mode(differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100299 }
Ana Krulec3084c052018-11-21 20:27:17 +0100300}
301
302void Scheduler::determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100303 ATRACE_INT("AutoTimestamp", isAutoTimestamp);
Ana Krulec3084c052018-11-21 20:27:17 +0100304
Ana Krulec7ab56032018-11-02 20:51:06 +0100305 // Video does not have timestamp automatically set, so we discard timestamps that are
306 // coming in from other sources for now.
307 if (isAutoTimestamp) {
308 return;
309 }
Ana Krulec3084c052018-11-21 20:27:17 +0100310 int64_t differenceMs = (framePresentTime - mPreviousFrameTimestamp) / 1000000;
311 mPreviousFrameTimestamp = framePresentTime;
Ana Krulec7ab56032018-11-02 20:51:06 +0100312
313 if (differenceMs < 10 || differenceMs > 100) {
314 // Dismiss noise.
315 return;
316 }
317 ATRACE_INT("TimestampDiff", differenceMs);
318
Ana Krulec434c22d2018-11-28 13:48:36 +0100319 mTimeDifferences[mCounter % scheduler::ARRAY_SIZE] = differenceMs;
Ana Krulec7ab56032018-11-02 20:51:06 +0100320 mCounter++;
Ana Krulec434c22d2018-11-28 13:48:36 +0100321 int64_t mean = scheduler::calculate_mean(mTimeDifferences);
322 ATRACE_INT("AutoTimestampMean", mean);
Ana Krulec7ab56032018-11-02 20:51:06 +0100323
324 // TODO(b/113612090): This are current numbers from trial and error while running videos
325 // from YouTube at 24, 30, and 60 fps.
Ana Krulec434c22d2018-11-28 13:48:36 +0100326 if (mean > 14 && mean < 18) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800327 ATRACE_INT("MediaFPS", 60);
Ana Krulec434c22d2018-11-28 13:48:36 +0100328 } else if (mean > 31 && mean < 34) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800329 ATRACE_INT("MediaFPS", 30);
Ana Krulec7ab56032018-11-02 20:51:06 +0100330 return;
Ana Krulec434c22d2018-11-28 13:48:36 +0100331 } else if (mean > 39 && mean < 42) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800332 ATRACE_INT("MediaFPS", 24);
Ana Krulec7ab56032018-11-02 20:51:06 +0100333 }
Ana Krulec7ab56032018-11-02 20:51:06 +0100334}
335
Ana Krulecfb772822018-11-30 10:44:07 +0100336void Scheduler::resetIdleTimer() {
337 if (mIdleTimer) {
338 mIdleTimer->reset();
339 ATRACE_INT("ExpiredIdleTimer", 0);
340 }
Ana Krulec7d1d6832018-12-27 11:10:09 -0800341
342 std::lock_guard<std::mutex> lock(mCallbackLock);
343 if (mResetTimerCallback) {
344 mResetTimerCallback();
345 }
Ana Krulecfb772822018-11-30 10:44:07 +0100346}
347
348void Scheduler::expiredTimerCallback() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800349 std::lock_guard<std::mutex> lock(mCallbackLock);
350 if (mExpiredTimerCallback) {
351 mExpiredTimerCallback();
352 ATRACE_INT("ExpiredIdleTimer", 1);
353 }
Ana Krulecfb772822018-11-30 10:44:07 +0100354}
355
Ana Krulec98b5b242018-08-10 15:03:23 -0700356} // namespace android