blob: 0d587dd59895cc840a7b7b4335481bcb5ca2fd32 [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(
Dominik Laskowskif654d572018-12-20 11:03:06 -080089 const std::string& connectionName, int64_t phaseOffsetNs, ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -070090 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
91 const int64_t id = sNextId++;
92 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
93
Ana Krulec98b5b242018-08-10 15:03:23 -070094 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -080095 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -070096 interceptCallback);
Ana Krulec98b5b242018-08-10 15:03:23 -070097 auto connection = std::make_unique<Connection>(new ConnectionHandle(id),
Dominik Laskowskif654d572018-12-20 11:03:06 -080098 eventThread->createEventConnection(
99 std::move(resyncCallback)),
Ana Krulec98b5b242018-08-10 15:03:23 -0700100 std::move(eventThread));
Dominik Laskowskif654d572018-12-20 11:03:06 -0800101
Ana Krulec98b5b242018-08-10 15:03:23 -0700102 mConnections.insert(std::make_pair(id, std::move(connection)));
103 return mConnections[id]->handle;
104}
105
Ana Krulec0c8cd522018-08-31 12:27:28 -0700106std::unique_ptr<EventThread> Scheduler::makeEventThread(
Ana Krulec1f027912018-09-10 21:36:25 +0000107 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700108 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";
Dominik Laskowskif654d572018-12-20 11:03:06 -0800113 return std::make_unique<impl::EventThread>(std::move(eventThreadSource), interceptCallback,
114 [this] { resetIdleTimer(); }, threadName.c_str());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700115}
116
Ana Krulec98b5b242018-08-10 15:03:23 -0700117sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Dominik Laskowskif654d572018-12-20 11:03:06 -0800118 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700119 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800120 return mConnections[handle->id]->thread->createEventConnection(std::move(resyncCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -0700121}
122
123EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700124 RETURN_VALUE_IF_INVALID(nullptr);
125 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700126}
127
Ana Krulec85c39af2018-12-26 17:29:57 -0800128sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700129 RETURN_VALUE_IF_INVALID(nullptr);
130 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700131}
132
133void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
134 EventThread::DisplayType displayType, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700135 RETURN_IF_INVALID();
136 mConnections[handle->id]->thread->onHotplugReceived(displayType, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700137}
138
139void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700140 RETURN_IF_INVALID();
141 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700142}
143
144void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700145 RETURN_IF_INVALID();
146 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700147}
148
Yiwei Zhang5434a782018-12-05 18:06:32 -0800149void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700150 RETURN_IF_INVALID();
151 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700152}
153
154void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700155 RETURN_IF_INVALID();
156 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700157}
Ana Krulece588e312018-09-18 12:32:24 -0700158
159void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
160 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
161 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
162}
163
164void Scheduler::enableHardwareVsync() {
165 std::lock_guard<std::mutex> lock(mHWVsyncLock);
166 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
167 mPrimaryDispSync->beginResync();
168 mEventControlThread->setVsyncEnabled(true);
169 mPrimaryHWVsyncEnabled = true;
170 }
171}
172
173void Scheduler::disableHardwareVsync(bool makeUnavailable) {
174 std::lock_guard<std::mutex> lock(mHWVsyncLock);
175 if (mPrimaryHWVsyncEnabled) {
176 mEventControlThread->setVsyncEnabled(false);
177 mPrimaryDispSync->endResync();
178 mPrimaryHWVsyncEnabled = false;
179 }
180 if (makeUnavailable) {
181 mHWVsyncAvailable = false;
182 }
183}
184
185void Scheduler::setVsyncPeriod(const nsecs_t period) {
186 mPrimaryDispSync->reset();
187 mPrimaryDispSync->setPeriod(period);
188 enableHardwareVsync();
189}
190
191void Scheduler::addResyncSample(const nsecs_t timestamp) {
192 bool needsHwVsync = false;
193 { // Scope for the lock
194 std::lock_guard<std::mutex> lock(mHWVsyncLock);
195 if (mPrimaryHWVsyncEnabled) {
196 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp);
197 }
198 }
199
200 if (needsHwVsync) {
201 enableHardwareVsync();
202 } else {
203 disableHardwareVsync(false);
204 }
205}
206
207void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
208 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
209 enableHardwareVsync();
210 } else {
211 disableHardwareVsync(false);
212 }
213}
214
215void Scheduler::setIgnorePresentFences(bool ignore) {
216 mPrimaryDispSync->setIgnorePresentFences(ignore);
217}
218
Ana Krulec7ab56032018-11-02 20:51:06 +0100219void Scheduler::makeHWSyncAvailable(bool makeAvailable) {
220 std::lock_guard<std::mutex> lock(mHWVsyncLock);
221 mHWVsyncAvailable = makeAvailable;
222}
223
Ana Krulec3084c052018-11-21 20:27:17 +0100224void Scheduler::addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
225 const std::string layerName) {
226 // This is V1 logic. It calculates the average FPS based on the timestamp frequency
227 // regardless of which layer the timestamp came from.
228 // For now, the averages and FPS are recorded in the systrace.
229 determineTimestampAverage(isAutoTimestamp, framePresentTime);
230
231 // This is V2 logic. It calculates the average and median timestamp difference based on the
232 // individual layer history. The results are recorded in the systrace.
233 determineLayerTimestampStats(layerName, framePresentTime);
234}
235
236void Scheduler::incrementFrameCounter() {
237 mLayerHistory.incrementCounter();
238}
239
Ana Krulec7d1d6832018-12-27 11:10:09 -0800240void Scheduler::setExpiredIdleTimerCallback(const ExpiredIdleTimerCallback& expiredTimerCallback) {
241 std::lock_guard<std::mutex> lock(mCallbackLock);
242 mExpiredTimerCallback = expiredTimerCallback;
243}
244
245void Scheduler::setResetIdleTimerCallback(const ResetIdleTimerCallback& resetTimerCallback) {
246 std::lock_guard<std::mutex> lock(mCallbackLock);
247 mResetTimerCallback = resetTimerCallback;
248}
249
Ana Krulec3084c052018-11-21 20:27:17 +0100250void Scheduler::updateFrameSkipping(const int64_t skipCount) {
251 ATRACE_INT("FrameSkipCount", skipCount);
252 if (mSkipCount != skipCount) {
253 // Only update DispSync if it hasn't been updated yet.
254 mPrimaryDispSync->setRefreshSkipCount(skipCount);
255 mSkipCount = skipCount;
256 }
257}
258
259void Scheduler::determineLayerTimestampStats(const std::string layerName,
260 const nsecs_t framePresentTime) {
261 mLayerHistory.insert(layerName, framePresentTime);
262 std::vector<int64_t> differencesMs;
263
264 // Traverse through the layer history, and determine the differences in present times.
265 nsecs_t newestPresentTime = framePresentTime;
Ana Krulec434c22d2018-11-28 13:48:36 +0100266 std::string differencesText = "";
Ana Krulec3084c052018-11-21 20:27:17 +0100267 for (int i = 1; i < mLayerHistory.getSize(); i++) {
268 std::unordered_map<std::string, nsecs_t> layers = mLayerHistory.get(i);
269 for (auto layer : layers) {
270 if (layer.first != layerName) {
271 continue;
272 }
273 int64_t differenceMs = (newestPresentTime - layer.second) / 1000000;
Ana Krulec3084c052018-11-21 20:27:17 +0100274 // Dismiss noise.
275 if (differenceMs > 10 && differenceMs < 60) {
276 differencesMs.push_back(differenceMs);
277 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100278 IF_ALOGV() { differencesText += (std::to_string(differenceMs) + " "); }
Ana Krulec3084c052018-11-21 20:27:17 +0100279 newestPresentTime = layer.second;
280 }
281 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100282 ALOGV("Layer %s timestamp intervals: %s", layerName.c_str(), differencesText.c_str());
Ana Krulec3084c052018-11-21 20:27:17 +0100283
Ana Krulec434c22d2018-11-28 13:48:36 +0100284 if (!differencesMs.empty()) {
285 // Mean/Average is a good indicator for when 24fps videos are playing, because the frames
286 // come in 33, and 49 ms intervals with occasional 41ms.
287 const int64_t meanMs = scheduler::calculate_mean(differencesMs);
288 const auto tagMean = "TimestampMean_" + layerName;
289 ATRACE_INT(tagMean.c_str(), meanMs);
290
291 // Mode and median are good indicators for 30 and 60 fps videos, because the majority of
292 // frames come in 16, or 33 ms intervals.
Ana Krulec3084c052018-11-21 20:27:17 +0100293 const auto tagMedian = "TimestampMedian_" + layerName;
Ana Krulec434c22d2018-11-28 13:48:36 +0100294 ATRACE_INT(tagMedian.c_str(), scheduler::calculate_median(&differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100295
Ana Krulec434c22d2018-11-28 13:48:36 +0100296 const auto tagMode = "TimestampMode_" + layerName;
297 ATRACE_INT(tagMode.c_str(), scheduler::calculate_mode(differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100298 }
Ana Krulec3084c052018-11-21 20:27:17 +0100299}
300
301void Scheduler::determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100302 ATRACE_INT("AutoTimestamp", isAutoTimestamp);
Ana Krulec3084c052018-11-21 20:27:17 +0100303
Ana Krulec7ab56032018-11-02 20:51:06 +0100304 // Video does not have timestamp automatically set, so we discard timestamps that are
305 // coming in from other sources for now.
306 if (isAutoTimestamp) {
307 return;
308 }
Ana Krulec3084c052018-11-21 20:27:17 +0100309 int64_t differenceMs = (framePresentTime - mPreviousFrameTimestamp) / 1000000;
310 mPreviousFrameTimestamp = framePresentTime;
Ana Krulec7ab56032018-11-02 20:51:06 +0100311
312 if (differenceMs < 10 || differenceMs > 100) {
313 // Dismiss noise.
314 return;
315 }
316 ATRACE_INT("TimestampDiff", differenceMs);
317
Ana Krulec434c22d2018-11-28 13:48:36 +0100318 mTimeDifferences[mCounter % scheduler::ARRAY_SIZE] = differenceMs;
Ana Krulec7ab56032018-11-02 20:51:06 +0100319 mCounter++;
Ana Krulec434c22d2018-11-28 13:48:36 +0100320 int64_t mean = scheduler::calculate_mean(mTimeDifferences);
321 ATRACE_INT("AutoTimestampMean", mean);
Ana Krulec7ab56032018-11-02 20:51:06 +0100322
323 // TODO(b/113612090): This are current numbers from trial and error while running videos
324 // from YouTube at 24, 30, and 60 fps.
Ana Krulec434c22d2018-11-28 13:48:36 +0100325 if (mean > 14 && mean < 18) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800326 ATRACE_INT("MediaFPS", 60);
Ana Krulec434c22d2018-11-28 13:48:36 +0100327 } else if (mean > 31 && mean < 34) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800328 ATRACE_INT("MediaFPS", 30);
Ana Krulec7ab56032018-11-02 20:51:06 +0100329 return;
Ana Krulec434c22d2018-11-28 13:48:36 +0100330 } else if (mean > 39 && mean < 42) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800331 ATRACE_INT("MediaFPS", 24);
Ana Krulec7ab56032018-11-02 20:51:06 +0100332 }
Ana Krulec7ab56032018-11-02 20:51:06 +0100333}
334
Ana Krulecfb772822018-11-30 10:44:07 +0100335void Scheduler::resetIdleTimer() {
336 if (mIdleTimer) {
337 mIdleTimer->reset();
338 ATRACE_INT("ExpiredIdleTimer", 0);
339 }
Ana Krulec7d1d6832018-12-27 11:10:09 -0800340
341 std::lock_guard<std::mutex> lock(mCallbackLock);
342 if (mResetTimerCallback) {
343 mResetTimerCallback();
344 }
Ana Krulecfb772822018-11-30 10:44:07 +0100345}
346
347void Scheduler::expiredTimerCallback() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800348 std::lock_guard<std::mutex> lock(mCallbackLock);
349 if (mExpiredTimerCallback) {
350 mExpiredTimerCallback();
351 ATRACE_INT("ExpiredIdleTimer", 1);
352 }
Ana Krulecfb772822018-11-30 10:44:07 +0100353}
354
Ana Krulec98b5b242018-08-10 15:03:23 -0700355} // namespace android