blob: 2ed2866ddfe9270336e3783491ee2af37a7abb09 [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 Laskowskif654d572018-12-20 11:03:06 -080087 const std::string& 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,
Ana Krulec0c8cd522018-08-31 12:27:28 -070094 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(
Ana Krulec1f027912018-09-10 21:36:25 +0000105 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700106 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ana Krulec1f027912018-09-10 21:36:25 +0000107 const std::string sourceName = connectionName + "Source";
Ana Krulec0c8cd522018-08-31 12:27:28 -0700108 std::unique_ptr<VSyncSource> eventThreadSource =
Ana Krulec1f027912018-09-10 21:36:25 +0000109 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, sourceName.c_str());
110 const std::string threadName = connectionName + "Thread";
Dominik Laskowskif654d572018-12-20 11:03:06 -0800111 return std::make_unique<impl::EventThread>(std::move(eventThreadSource), interceptCallback,
112 [this] { resetIdleTimer(); }, threadName.c_str());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700113}
114
Ana Krulec98b5b242018-08-10 15:03:23 -0700115sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Dominik Laskowskif654d572018-12-20 11:03:06 -0800116 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700117 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800118 return mConnections[handle->id]->thread->createEventConnection(std::move(resyncCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -0700119}
120
121EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700122 RETURN_VALUE_IF_INVALID(nullptr);
123 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700124}
125
Ana Krulec85c39af2018-12-26 17:29:57 -0800126sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700127 RETURN_VALUE_IF_INVALID(nullptr);
128 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700129}
130
131void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
132 EventThread::DisplayType displayType, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700133 RETURN_IF_INVALID();
134 mConnections[handle->id]->thread->onHotplugReceived(displayType, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700135}
136
137void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700138 RETURN_IF_INVALID();
139 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700140}
141
142void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700143 RETURN_IF_INVALID();
144 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700145}
146
Yiwei Zhang5434a782018-12-05 18:06:32 -0800147void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700148 RETURN_IF_INVALID();
149 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700150}
151
152void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700153 RETURN_IF_INVALID();
154 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700155}
Ana Krulece588e312018-09-18 12:32:24 -0700156
157void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
158 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
159 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
160}
161
162void Scheduler::enableHardwareVsync() {
163 std::lock_guard<std::mutex> lock(mHWVsyncLock);
164 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
165 mPrimaryDispSync->beginResync();
166 mEventControlThread->setVsyncEnabled(true);
167 mPrimaryHWVsyncEnabled = true;
168 }
169}
170
171void Scheduler::disableHardwareVsync(bool makeUnavailable) {
172 std::lock_guard<std::mutex> lock(mHWVsyncLock);
173 if (mPrimaryHWVsyncEnabled) {
174 mEventControlThread->setVsyncEnabled(false);
175 mPrimaryDispSync->endResync();
176 mPrimaryHWVsyncEnabled = false;
177 }
178 if (makeUnavailable) {
179 mHWVsyncAvailable = false;
180 }
181}
182
183void Scheduler::setVsyncPeriod(const nsecs_t period) {
184 mPrimaryDispSync->reset();
185 mPrimaryDispSync->setPeriod(period);
186 enableHardwareVsync();
187}
188
189void Scheduler::addResyncSample(const nsecs_t timestamp) {
190 bool needsHwVsync = false;
191 { // Scope for the lock
192 std::lock_guard<std::mutex> lock(mHWVsyncLock);
193 if (mPrimaryHWVsyncEnabled) {
194 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp);
195 }
196 }
197
198 if (needsHwVsync) {
199 enableHardwareVsync();
200 } else {
201 disableHardwareVsync(false);
202 }
203}
204
205void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
206 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
207 enableHardwareVsync();
208 } else {
209 disableHardwareVsync(false);
210 }
211}
212
213void Scheduler::setIgnorePresentFences(bool ignore) {
214 mPrimaryDispSync->setIgnorePresentFences(ignore);
215}
216
Ana Krulec7ab56032018-11-02 20:51:06 +0100217void Scheduler::makeHWSyncAvailable(bool makeAvailable) {
218 std::lock_guard<std::mutex> lock(mHWVsyncLock);
219 mHWVsyncAvailable = makeAvailable;
220}
221
Ana Krulec3084c052018-11-21 20:27:17 +0100222void Scheduler::addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
223 const std::string layerName) {
224 // This is V1 logic. It calculates the average FPS based on the timestamp frequency
225 // regardless of which layer the timestamp came from.
226 // For now, the averages and FPS are recorded in the systrace.
227 determineTimestampAverage(isAutoTimestamp, framePresentTime);
228
229 // This is V2 logic. It calculates the average and median timestamp difference based on the
230 // individual layer history. The results are recorded in the systrace.
231 determineLayerTimestampStats(layerName, framePresentTime);
232}
233
234void Scheduler::incrementFrameCounter() {
235 mLayerHistory.incrementCounter();
236}
237
Ana Krulec7d1d6832018-12-27 11:10:09 -0800238void Scheduler::setExpiredIdleTimerCallback(const ExpiredIdleTimerCallback& expiredTimerCallback) {
239 std::lock_guard<std::mutex> lock(mCallbackLock);
240 mExpiredTimerCallback = expiredTimerCallback;
241}
242
243void Scheduler::setResetIdleTimerCallback(const ResetIdleTimerCallback& resetTimerCallback) {
244 std::lock_guard<std::mutex> lock(mCallbackLock);
245 mResetTimerCallback = resetTimerCallback;
246}
247
Ana Krulec3084c052018-11-21 20:27:17 +0100248void Scheduler::updateFrameSkipping(const int64_t skipCount) {
249 ATRACE_INT("FrameSkipCount", skipCount);
250 if (mSkipCount != skipCount) {
251 // Only update DispSync if it hasn't been updated yet.
252 mPrimaryDispSync->setRefreshSkipCount(skipCount);
253 mSkipCount = skipCount;
254 }
255}
256
257void Scheduler::determineLayerTimestampStats(const std::string layerName,
258 const nsecs_t framePresentTime) {
259 mLayerHistory.insert(layerName, framePresentTime);
260 std::vector<int64_t> differencesMs;
261
262 // Traverse through the layer history, and determine the differences in present times.
263 nsecs_t newestPresentTime = framePresentTime;
Ana Krulec434c22d2018-11-28 13:48:36 +0100264 std::string differencesText = "";
Ana Krulec3084c052018-11-21 20:27:17 +0100265 for (int i = 1; i < mLayerHistory.getSize(); i++) {
266 std::unordered_map<std::string, nsecs_t> layers = mLayerHistory.get(i);
267 for (auto layer : layers) {
268 if (layer.first != layerName) {
269 continue;
270 }
271 int64_t differenceMs = (newestPresentTime - layer.second) / 1000000;
Ana Krulec3084c052018-11-21 20:27:17 +0100272 // Dismiss noise.
273 if (differenceMs > 10 && differenceMs < 60) {
274 differencesMs.push_back(differenceMs);
275 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100276 IF_ALOGV() { differencesText += (std::to_string(differenceMs) + " "); }
Ana Krulec3084c052018-11-21 20:27:17 +0100277 newestPresentTime = layer.second;
278 }
279 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100280 ALOGV("Layer %s timestamp intervals: %s", layerName.c_str(), differencesText.c_str());
Ana Krulec3084c052018-11-21 20:27:17 +0100281
Ana Krulec434c22d2018-11-28 13:48:36 +0100282 if (!differencesMs.empty()) {
283 // Mean/Average is a good indicator for when 24fps videos are playing, because the frames
284 // come in 33, and 49 ms intervals with occasional 41ms.
285 const int64_t meanMs = scheduler::calculate_mean(differencesMs);
286 const auto tagMean = "TimestampMean_" + layerName;
287 ATRACE_INT(tagMean.c_str(), meanMs);
288
289 // Mode and median are good indicators for 30 and 60 fps videos, because the majority of
290 // frames come in 16, or 33 ms intervals.
Ana Krulec3084c052018-11-21 20:27:17 +0100291 const auto tagMedian = "TimestampMedian_" + layerName;
Ana Krulec434c22d2018-11-28 13:48:36 +0100292 ATRACE_INT(tagMedian.c_str(), scheduler::calculate_median(&differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100293
Ana Krulec434c22d2018-11-28 13:48:36 +0100294 const auto tagMode = "TimestampMode_" + layerName;
295 ATRACE_INT(tagMode.c_str(), scheduler::calculate_mode(differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100296 }
Ana Krulec3084c052018-11-21 20:27:17 +0100297}
298
299void Scheduler::determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100300 ATRACE_INT("AutoTimestamp", isAutoTimestamp);
Ana Krulec3084c052018-11-21 20:27:17 +0100301
Ana Krulec7ab56032018-11-02 20:51:06 +0100302 // Video does not have timestamp automatically set, so we discard timestamps that are
303 // coming in from other sources for now.
304 if (isAutoTimestamp) {
305 return;
306 }
Ana Krulec3084c052018-11-21 20:27:17 +0100307 int64_t differenceMs = (framePresentTime - mPreviousFrameTimestamp) / 1000000;
308 mPreviousFrameTimestamp = framePresentTime;
Ana Krulec7ab56032018-11-02 20:51:06 +0100309
310 if (differenceMs < 10 || differenceMs > 100) {
311 // Dismiss noise.
312 return;
313 }
314 ATRACE_INT("TimestampDiff", differenceMs);
315
Ana Krulec434c22d2018-11-28 13:48:36 +0100316 mTimeDifferences[mCounter % scheduler::ARRAY_SIZE] = differenceMs;
Ana Krulec7ab56032018-11-02 20:51:06 +0100317 mCounter++;
Ana Krulec434c22d2018-11-28 13:48:36 +0100318 int64_t mean = scheduler::calculate_mean(mTimeDifferences);
319 ATRACE_INT("AutoTimestampMean", mean);
Ana Krulec7ab56032018-11-02 20:51:06 +0100320
321 // TODO(b/113612090): This are current numbers from trial and error while running videos
322 // from YouTube at 24, 30, and 60 fps.
Ana Krulec434c22d2018-11-28 13:48:36 +0100323 if (mean > 14 && mean < 18) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800324 ATRACE_INT("MediaFPS", 60);
Ana Krulec434c22d2018-11-28 13:48:36 +0100325 } else if (mean > 31 && mean < 34) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800326 ATRACE_INT("MediaFPS", 30);
Ana Krulec7ab56032018-11-02 20:51:06 +0100327 return;
Ana Krulec434c22d2018-11-28 13:48:36 +0100328 } else if (mean > 39 && mean < 42) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800329 ATRACE_INT("MediaFPS", 24);
Ana Krulec7ab56032018-11-02 20:51:06 +0100330 }
Ana Krulec7ab56032018-11-02 20:51:06 +0100331}
332
Ana Krulecfb772822018-11-30 10:44:07 +0100333void Scheduler::resetIdleTimer() {
334 if (mIdleTimer) {
335 mIdleTimer->reset();
336 ATRACE_INT("ExpiredIdleTimer", 0);
337 }
Ana Krulec7d1d6832018-12-27 11:10:09 -0800338
339 std::lock_guard<std::mutex> lock(mCallbackLock);
340 if (mResetTimerCallback) {
341 mResetTimerCallback();
342 }
Ana Krulecfb772822018-11-30 10:44:07 +0100343}
344
345void Scheduler::expiredTimerCallback() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800346 std::lock_guard<std::mutex> lock(mCallbackLock);
347 if (mExpiredTimerCallback) {
348 mExpiredTimerCallback();
349 ATRACE_INT("ExpiredIdleTimer", 1);
350 }
Ana Krulecfb772822018-11-30 10:44:07 +0100351}
352
Ana Krulecb43429d2019-01-09 14:28:51 -0800353std::string Scheduler::doDump() {
354 std::ostringstream stream;
355 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
356 return stream.str();
357}
358
Ana Krulec98b5b242018-08-10 15:03:23 -0700359} // namespace android