blob: b0034511d76512f4cfa17d5d9b2d92aeb365d006 [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>
Ana Krulecfb772822018-11-30 10:44:07 +010031#include <cutils/properties.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070032#include <gui/ISurfaceComposer.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"
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
Ana Krulece588e312018-09-18 12:32:24 -070059Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function)
Sundong Ahnd5e08f62018-12-12 20:27:28 +090060 : mHasSyncFramework(running_without_sync_framework(true)),
61 mDispSyncPresentTimeOffset(present_time_offset_from_vsync_ns(0)),
Ana Krulece588e312018-09-18 12:32:24 -070062 mPrimaryHWVsyncEnabled(false),
63 mHWVsyncAvailable(false) {
64 // Note: We create a local temporary with the real DispSync implementation
65 // type temporarily so we can initialize it with the configured values,
66 // before storing it for more generic use using the interface type.
67 auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync");
68 primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset);
69 mPrimaryDispSync = std::move(primaryDispSync);
70 mEventControlThread = std::make_unique<impl::EventControlThread>(function);
Ana Krulecfb772822018-11-30 10:44:07 +010071
72 char value[PROPERTY_VALUE_MAX];
73 property_get("debug.sf.set_idle_timer_ms", value, "0");
74 mSetIdleTimerMs = atoi(value);
75
76 if (mSetIdleTimerMs > 0) {
77 mIdleTimer =
78 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetIdleTimerMs),
79 [this] { expiredTimerCallback(); });
80 mIdleTimer->start();
81 }
Ana Krulece588e312018-09-18 12:32:24 -070082}
83
Ana Krulec0c8cd522018-08-31 12:27:28 -070084Scheduler::~Scheduler() = default;
85
Ana Krulec98b5b242018-08-10 15:03:23 -070086sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Dominik Laskowskibd52c842019-01-28 18:11:23 -080087 const char* connectionName, int64_t phaseOffsetNs, ResyncCallback resyncCallback,
Ana Krulec98b5b242018-08-10 15:03:23 -070088 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
89 const int64_t id = sNextId++;
90 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
91
Ana Krulec98b5b242018-08-10 15:03:23 -070092 std::unique_ptr<EventThread> eventThread =
Dominik Laskowskif654d572018-12-20 11:03:06 -080093 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
Dominik Laskowskibd52c842019-01-28 18:11:23 -080094 std::move(interceptCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -070095 auto connection = std::make_unique<Connection>(new ConnectionHandle(id),
Dominik Laskowskif654d572018-12-20 11:03:06 -080096 eventThread->createEventConnection(
97 std::move(resyncCallback)),
Ana Krulec98b5b242018-08-10 15:03:23 -070098 std::move(eventThread));
Dominik Laskowskif654d572018-12-20 11:03:06 -080099
Ana Krulec98b5b242018-08-10 15:03:23 -0700100 mConnections.insert(std::make_pair(id, std::move(connection)));
101 return mConnections[id]->handle;
102}
103
Ana Krulec0c8cd522018-08-31 12:27:28 -0700104std::unique_ptr<EventThread> Scheduler::makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800105 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700106 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
107 std::unique_ptr<VSyncSource> eventThreadSource =
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800108 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName);
109 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
110 std::move(interceptCallback),
111 [this] { resetIdleTimer(); }, connectionName);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700112}
113
Ana Krulec98b5b242018-08-10 15:03:23 -0700114sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Dominik Laskowskif654d572018-12-20 11:03:06 -0800115 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700116 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800117 return mConnections[handle->id]->thread->createEventConnection(std::move(resyncCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -0700118}
119
120EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700121 RETURN_VALUE_IF_INVALID(nullptr);
122 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700123}
124
Ana Krulec85c39af2018-12-26 17:29:57 -0800125sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700126 RETURN_VALUE_IF_INVALID(nullptr);
127 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700128}
129
130void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
131 EventThread::DisplayType displayType, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700132 RETURN_IF_INVALID();
133 mConnections[handle->id]->thread->onHotplugReceived(displayType, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700134}
135
136void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700137 RETURN_IF_INVALID();
138 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700139}
140
141void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700142 RETURN_IF_INVALID();
143 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700144}
145
Yiwei Zhang5434a782018-12-05 18:06:32 -0800146void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700147 RETURN_IF_INVALID();
148 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700149}
150
151void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700152 RETURN_IF_INVALID();
153 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700154}
Ana Krulece588e312018-09-18 12:32:24 -0700155
156void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
157 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
158 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
159}
160
161void Scheduler::enableHardwareVsync() {
162 std::lock_guard<std::mutex> lock(mHWVsyncLock);
163 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
164 mPrimaryDispSync->beginResync();
165 mEventControlThread->setVsyncEnabled(true);
166 mPrimaryHWVsyncEnabled = true;
167 }
168}
169
170void Scheduler::disableHardwareVsync(bool makeUnavailable) {
171 std::lock_guard<std::mutex> lock(mHWVsyncLock);
172 if (mPrimaryHWVsyncEnabled) {
173 mEventControlThread->setVsyncEnabled(false);
174 mPrimaryDispSync->endResync();
175 mPrimaryHWVsyncEnabled = false;
176 }
177 if (makeUnavailable) {
178 mHWVsyncAvailable = false;
179 }
180}
181
182void Scheduler::setVsyncPeriod(const nsecs_t period) {
183 mPrimaryDispSync->reset();
184 mPrimaryDispSync->setPeriod(period);
185 enableHardwareVsync();
186}
187
188void Scheduler::addResyncSample(const nsecs_t timestamp) {
189 bool needsHwVsync = false;
190 { // Scope for the lock
191 std::lock_guard<std::mutex> lock(mHWVsyncLock);
192 if (mPrimaryHWVsyncEnabled) {
193 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp);
194 }
195 }
196
197 if (needsHwVsync) {
198 enableHardwareVsync();
199 } else {
200 disableHardwareVsync(false);
201 }
202}
203
204void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
205 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
206 enableHardwareVsync();
207 } else {
208 disableHardwareVsync(false);
209 }
210}
211
212void Scheduler::setIgnorePresentFences(bool ignore) {
213 mPrimaryDispSync->setIgnorePresentFences(ignore);
214}
215
Ana Krulec7ab56032018-11-02 20:51:06 +0100216void Scheduler::makeHWSyncAvailable(bool makeAvailable) {
217 std::lock_guard<std::mutex> lock(mHWVsyncLock);
218 mHWVsyncAvailable = makeAvailable;
219}
220
Ana Krulec3084c052018-11-21 20:27:17 +0100221void Scheduler::addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
222 const std::string layerName) {
223 // This is V1 logic. It calculates the average FPS based on the timestamp frequency
224 // regardless of which layer the timestamp came from.
225 // For now, the averages and FPS are recorded in the systrace.
226 determineTimestampAverage(isAutoTimestamp, framePresentTime);
227
228 // This is V2 logic. It calculates the average and median timestamp difference based on the
229 // individual layer history. The results are recorded in the systrace.
230 determineLayerTimestampStats(layerName, framePresentTime);
231}
232
233void Scheduler::incrementFrameCounter() {
234 mLayerHistory.incrementCounter();
235}
236
Ana Krulec7d1d6832018-12-27 11:10:09 -0800237void Scheduler::setExpiredIdleTimerCallback(const ExpiredIdleTimerCallback& expiredTimerCallback) {
238 std::lock_guard<std::mutex> lock(mCallbackLock);
239 mExpiredTimerCallback = expiredTimerCallback;
240}
241
242void Scheduler::setResetIdleTimerCallback(const ResetIdleTimerCallback& resetTimerCallback) {
243 std::lock_guard<std::mutex> lock(mCallbackLock);
244 mResetTimerCallback = resetTimerCallback;
245}
246
Ana Krulec3084c052018-11-21 20:27:17 +0100247void Scheduler::updateFrameSkipping(const int64_t skipCount) {
248 ATRACE_INT("FrameSkipCount", skipCount);
249 if (mSkipCount != skipCount) {
250 // Only update DispSync if it hasn't been updated yet.
251 mPrimaryDispSync->setRefreshSkipCount(skipCount);
252 mSkipCount = skipCount;
253 }
254}
255
256void Scheduler::determineLayerTimestampStats(const std::string layerName,
257 const nsecs_t framePresentTime) {
258 mLayerHistory.insert(layerName, framePresentTime);
259 std::vector<int64_t> differencesMs;
260
261 // Traverse through the layer history, and determine the differences in present times.
262 nsecs_t newestPresentTime = framePresentTime;
Ana Krulec434c22d2018-11-28 13:48:36 +0100263 std::string differencesText = "";
Ana Krulec3084c052018-11-21 20:27:17 +0100264 for (int i = 1; i < mLayerHistory.getSize(); i++) {
265 std::unordered_map<std::string, nsecs_t> layers = mLayerHistory.get(i);
266 for (auto layer : layers) {
267 if (layer.first != layerName) {
268 continue;
269 }
270 int64_t differenceMs = (newestPresentTime - layer.second) / 1000000;
Ana Krulec3084c052018-11-21 20:27:17 +0100271 // Dismiss noise.
272 if (differenceMs > 10 && differenceMs < 60) {
273 differencesMs.push_back(differenceMs);
274 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100275 IF_ALOGV() { differencesText += (std::to_string(differenceMs) + " "); }
Ana Krulec3084c052018-11-21 20:27:17 +0100276 newestPresentTime = layer.second;
277 }
278 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100279 ALOGV("Layer %s timestamp intervals: %s", layerName.c_str(), differencesText.c_str());
Ana Krulec3084c052018-11-21 20:27:17 +0100280
Ana Krulec434c22d2018-11-28 13:48:36 +0100281 if (!differencesMs.empty()) {
282 // Mean/Average is a good indicator for when 24fps videos are playing, because the frames
283 // come in 33, and 49 ms intervals with occasional 41ms.
284 const int64_t meanMs = scheduler::calculate_mean(differencesMs);
285 const auto tagMean = "TimestampMean_" + layerName;
286 ATRACE_INT(tagMean.c_str(), meanMs);
287
288 // Mode and median are good indicators for 30 and 60 fps videos, because the majority of
289 // frames come in 16, or 33 ms intervals.
Ana Krulec3084c052018-11-21 20:27:17 +0100290 const auto tagMedian = "TimestampMedian_" + layerName;
Ana Krulec434c22d2018-11-28 13:48:36 +0100291 ATRACE_INT(tagMedian.c_str(), scheduler::calculate_median(&differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100292
Ana Krulec434c22d2018-11-28 13:48:36 +0100293 const auto tagMode = "TimestampMode_" + layerName;
294 ATRACE_INT(tagMode.c_str(), scheduler::calculate_mode(differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100295 }
Ana Krulec3084c052018-11-21 20:27:17 +0100296}
297
298void Scheduler::determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100299 ATRACE_INT("AutoTimestamp", isAutoTimestamp);
Ana Krulec3084c052018-11-21 20:27:17 +0100300
Ana Krulec7ab56032018-11-02 20:51:06 +0100301 // Video does not have timestamp automatically set, so we discard timestamps that are
302 // coming in from other sources for now.
303 if (isAutoTimestamp) {
304 return;
305 }
Ana Krulec3084c052018-11-21 20:27:17 +0100306 int64_t differenceMs = (framePresentTime - mPreviousFrameTimestamp) / 1000000;
307 mPreviousFrameTimestamp = framePresentTime;
Ana Krulec7ab56032018-11-02 20:51:06 +0100308
309 if (differenceMs < 10 || differenceMs > 100) {
310 // Dismiss noise.
311 return;
312 }
313 ATRACE_INT("TimestampDiff", differenceMs);
314
Ana Krulec434c22d2018-11-28 13:48:36 +0100315 mTimeDifferences[mCounter % scheduler::ARRAY_SIZE] = differenceMs;
Ana Krulec7ab56032018-11-02 20:51:06 +0100316 mCounter++;
Ana Krulec434c22d2018-11-28 13:48:36 +0100317 int64_t mean = scheduler::calculate_mean(mTimeDifferences);
318 ATRACE_INT("AutoTimestampMean", mean);
Ana Krulec7ab56032018-11-02 20:51:06 +0100319
320 // TODO(b/113612090): This are current numbers from trial and error while running videos
321 // from YouTube at 24, 30, and 60 fps.
Ana Krulec434c22d2018-11-28 13:48:36 +0100322 if (mean > 14 && mean < 18) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800323 ATRACE_INT("MediaFPS", 60);
Ana Krulec434c22d2018-11-28 13:48:36 +0100324 } else if (mean > 31 && mean < 34) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800325 ATRACE_INT("MediaFPS", 30);
Ana Krulec7ab56032018-11-02 20:51:06 +0100326 return;
Ana Krulec434c22d2018-11-28 13:48:36 +0100327 } else if (mean > 39 && mean < 42) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800328 ATRACE_INT("MediaFPS", 24);
Ana Krulec7ab56032018-11-02 20:51:06 +0100329 }
Ana Krulec7ab56032018-11-02 20:51:06 +0100330}
331
Ana Krulecfb772822018-11-30 10:44:07 +0100332void Scheduler::resetIdleTimer() {
333 if (mIdleTimer) {
334 mIdleTimer->reset();
335 ATRACE_INT("ExpiredIdleTimer", 0);
336 }
Ana Krulec7d1d6832018-12-27 11:10:09 -0800337
338 std::lock_guard<std::mutex> lock(mCallbackLock);
339 if (mResetTimerCallback) {
340 mResetTimerCallback();
341 }
Ana Krulecfb772822018-11-30 10:44:07 +0100342}
343
344void Scheduler::expiredTimerCallback() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800345 std::lock_guard<std::mutex> lock(mCallbackLock);
346 if (mExpiredTimerCallback) {
347 mExpiredTimerCallback();
348 ATRACE_INT("ExpiredIdleTimer", 1);
349 }
Ana Krulecfb772822018-11-30 10:44:07 +0100350}
351
Ana Krulecb43429d2019-01-09 14:28:51 -0800352std::string Scheduler::doDump() {
353 std::ostringstream stream;
354 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
355 return stream.str();
356}
357
Ana Krulec98b5b242018-08-10 15:03:23 -0700358} // namespace android