blob: 5b8cc10a4600743c69e46d08985801de137a9f2e [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 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"
41#include "InjectVSyncSource.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010042#include "SchedulerUtils.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;
48
Ana Krulec0c8cd522018-08-31 12:27:28 -070049#define RETURN_VALUE_IF_INVALID(value) \
50 if (handle == nullptr || mConnections.count(handle->id) == 0) return value
51#define RETURN_IF_INVALID() \
52 if (handle == nullptr || mConnections.count(handle->id) == 0) return
53
Ana Krulec98b5b242018-08-10 15:03:23 -070054std::atomic<int64_t> Scheduler::sNextId = 0;
55
Ana Krulece588e312018-09-18 12:32:24 -070056Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function)
57 : mHasSyncFramework(
58 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasSyncFramework>(true)),
59 mDispSyncPresentTimeOffset(
60 getInt64<ISurfaceFlingerConfigs,
61 &ISurfaceFlingerConfigs::presentTimeOffsetFromVSyncNs>(0)),
62 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);
71}
72
Ana Krulec0c8cd522018-08-31 12:27:28 -070073Scheduler::~Scheduler() = default;
74
Ana Krulec98b5b242018-08-10 15:03:23 -070075sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Ana Krulece588e312018-09-18 12:32:24 -070076 const std::string& connectionName, int64_t phaseOffsetNs,
Ana Krulec98b5b242018-08-10 15:03:23 -070077 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
78 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
79 const int64_t id = sNextId++;
80 ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
81
Ana Krulec98b5b242018-08-10 15:03:23 -070082 std::unique_ptr<EventThread> eventThread =
Ana Krulece588e312018-09-18 12:32:24 -070083 makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs, resyncCallback,
Ana Krulec0c8cd522018-08-31 12:27:28 -070084 interceptCallback);
Ana Krulec98b5b242018-08-10 15:03:23 -070085 auto connection = std::make_unique<Connection>(new ConnectionHandle(id),
86 eventThread->createEventConnection(),
87 std::move(eventThread));
88 mConnections.insert(std::make_pair(id, std::move(connection)));
89 return mConnections[id]->handle;
90}
91
Ana Krulec0c8cd522018-08-31 12:27:28 -070092std::unique_ptr<EventThread> Scheduler::makeEventThread(
Ana Krulec1f027912018-09-10 21:36:25 +000093 const std::string& connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -070094 impl::EventThread::ResyncWithRateLimitCallback resyncCallback,
95 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
Ana Krulec1f027912018-09-10 21:36:25 +000096 const std::string sourceName = connectionName + "Source";
Ana Krulec0c8cd522018-08-31 12:27:28 -070097 std::unique_ptr<VSyncSource> eventThreadSource =
Ana Krulec1f027912018-09-10 21:36:25 +000098 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, sourceName.c_str());
99 const std::string threadName = connectionName + "Thread";
Ana Krulec0c8cd522018-08-31 12:27:28 -0700100 return std::make_unique<impl::EventThread>(std::move(eventThreadSource), resyncCallback,
Ana Krulec1f027912018-09-10 21:36:25 +0000101 interceptCallback, threadName.c_str());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700102}
103
Ana Krulec98b5b242018-08-10 15:03:23 -0700104sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
105 const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700106 RETURN_VALUE_IF_INVALID(nullptr);
107 return mConnections[handle->id]->thread->createEventConnection();
Ana Krulec98b5b242018-08-10 15:03:23 -0700108}
109
110EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700111 RETURN_VALUE_IF_INVALID(nullptr);
112 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700113}
114
115sp<BnDisplayEventConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700116 RETURN_VALUE_IF_INVALID(nullptr);
117 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700118}
119
120void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
121 EventThread::DisplayType displayType, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700122 RETURN_IF_INVALID();
123 mConnections[handle->id]->thread->onHotplugReceived(displayType, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700124}
125
126void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700127 RETURN_IF_INVALID();
128 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700129}
130
131void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700132 RETURN_IF_INVALID();
133 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700134}
135
Yiwei Zhang5434a782018-12-05 18:06:32 -0800136void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700137 RETURN_IF_INVALID();
138 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700139}
140
141void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700142 RETURN_IF_INVALID();
143 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700144}
Ana Krulece588e312018-09-18 12:32:24 -0700145
146void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
147 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
148 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
149}
150
151void Scheduler::enableHardwareVsync() {
152 std::lock_guard<std::mutex> lock(mHWVsyncLock);
153 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
154 mPrimaryDispSync->beginResync();
155 mEventControlThread->setVsyncEnabled(true);
156 mPrimaryHWVsyncEnabled = true;
157 }
158}
159
160void Scheduler::disableHardwareVsync(bool makeUnavailable) {
161 std::lock_guard<std::mutex> lock(mHWVsyncLock);
162 if (mPrimaryHWVsyncEnabled) {
163 mEventControlThread->setVsyncEnabled(false);
164 mPrimaryDispSync->endResync();
165 mPrimaryHWVsyncEnabled = false;
166 }
167 if (makeUnavailable) {
168 mHWVsyncAvailable = false;
169 }
170}
171
172void Scheduler::setVsyncPeriod(const nsecs_t period) {
173 mPrimaryDispSync->reset();
174 mPrimaryDispSync->setPeriod(period);
175 enableHardwareVsync();
176}
177
178void Scheduler::addResyncSample(const nsecs_t timestamp) {
179 bool needsHwVsync = false;
180 { // Scope for the lock
181 std::lock_guard<std::mutex> lock(mHWVsyncLock);
182 if (mPrimaryHWVsyncEnabled) {
183 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp);
184 }
185 }
186
187 if (needsHwVsync) {
188 enableHardwareVsync();
189 } else {
190 disableHardwareVsync(false);
191 }
192}
193
194void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
195 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
196 enableHardwareVsync();
197 } else {
198 disableHardwareVsync(false);
199 }
200}
201
202void Scheduler::setIgnorePresentFences(bool ignore) {
203 mPrimaryDispSync->setIgnorePresentFences(ignore);
204}
205
Ana Krulec7ab56032018-11-02 20:51:06 +0100206void Scheduler::makeHWSyncAvailable(bool makeAvailable) {
207 std::lock_guard<std::mutex> lock(mHWVsyncLock);
208 mHWVsyncAvailable = makeAvailable;
209}
210
Ana Krulec3084c052018-11-21 20:27:17 +0100211void Scheduler::addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
212 const std::string layerName) {
213 // This is V1 logic. It calculates the average FPS based on the timestamp frequency
214 // regardless of which layer the timestamp came from.
215 // For now, the averages and FPS are recorded in the systrace.
216 determineTimestampAverage(isAutoTimestamp, framePresentTime);
217
218 // This is V2 logic. It calculates the average and median timestamp difference based on the
219 // individual layer history. The results are recorded in the systrace.
220 determineLayerTimestampStats(layerName, framePresentTime);
221}
222
223void Scheduler::incrementFrameCounter() {
224 mLayerHistory.incrementCounter();
225}
226
Ana Krulec3084c052018-11-21 20:27:17 +0100227void Scheduler::updateFrameSkipping(const int64_t skipCount) {
228 ATRACE_INT("FrameSkipCount", skipCount);
229 if (mSkipCount != skipCount) {
230 // Only update DispSync if it hasn't been updated yet.
231 mPrimaryDispSync->setRefreshSkipCount(skipCount);
232 mSkipCount = skipCount;
233 }
234}
235
236void Scheduler::determineLayerTimestampStats(const std::string layerName,
237 const nsecs_t framePresentTime) {
238 mLayerHistory.insert(layerName, framePresentTime);
239 std::vector<int64_t> differencesMs;
240
241 // Traverse through the layer history, and determine the differences in present times.
242 nsecs_t newestPresentTime = framePresentTime;
Ana Krulec434c22d2018-11-28 13:48:36 +0100243 std::string differencesText = "";
Ana Krulec3084c052018-11-21 20:27:17 +0100244 for (int i = 1; i < mLayerHistory.getSize(); i++) {
245 std::unordered_map<std::string, nsecs_t> layers = mLayerHistory.get(i);
246 for (auto layer : layers) {
247 if (layer.first != layerName) {
248 continue;
249 }
250 int64_t differenceMs = (newestPresentTime - layer.second) / 1000000;
Ana Krulec3084c052018-11-21 20:27:17 +0100251 // Dismiss noise.
252 if (differenceMs > 10 && differenceMs < 60) {
253 differencesMs.push_back(differenceMs);
254 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100255 IF_ALOGV() { differencesText += (std::to_string(differenceMs) + " "); }
Ana Krulec3084c052018-11-21 20:27:17 +0100256 newestPresentTime = layer.second;
257 }
258 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100259 ALOGV("Layer %s timestamp intervals: %s", layerName.c_str(), differencesText.c_str());
Ana Krulec3084c052018-11-21 20:27:17 +0100260
Ana Krulec434c22d2018-11-28 13:48:36 +0100261 if (!differencesMs.empty()) {
262 // Mean/Average is a good indicator for when 24fps videos are playing, because the frames
263 // come in 33, and 49 ms intervals with occasional 41ms.
264 const int64_t meanMs = scheduler::calculate_mean(differencesMs);
265 const auto tagMean = "TimestampMean_" + layerName;
266 ATRACE_INT(tagMean.c_str(), meanMs);
267
268 // Mode and median are good indicators for 30 and 60 fps videos, because the majority of
269 // frames come in 16, or 33 ms intervals.
Ana Krulec3084c052018-11-21 20:27:17 +0100270 const auto tagMedian = "TimestampMedian_" + layerName;
Ana Krulec434c22d2018-11-28 13:48:36 +0100271 ATRACE_INT(tagMedian.c_str(), scheduler::calculate_median(&differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100272
Ana Krulec434c22d2018-11-28 13:48:36 +0100273 const auto tagMode = "TimestampMode_" + layerName;
274 ATRACE_INT(tagMode.c_str(), scheduler::calculate_mode(differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100275 }
Ana Krulec3084c052018-11-21 20:27:17 +0100276}
277
278void Scheduler::determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100279 ATRACE_INT("AutoTimestamp", isAutoTimestamp);
Ana Krulec3084c052018-11-21 20:27:17 +0100280
Ana Krulec7ab56032018-11-02 20:51:06 +0100281 // Video does not have timestamp automatically set, so we discard timestamps that are
282 // coming in from other sources for now.
283 if (isAutoTimestamp) {
284 return;
285 }
Ana Krulec3084c052018-11-21 20:27:17 +0100286 int64_t differenceMs = (framePresentTime - mPreviousFrameTimestamp) / 1000000;
287 mPreviousFrameTimestamp = framePresentTime;
Ana Krulec7ab56032018-11-02 20:51:06 +0100288
289 if (differenceMs < 10 || differenceMs > 100) {
290 // Dismiss noise.
291 return;
292 }
293 ATRACE_INT("TimestampDiff", differenceMs);
294
Ana Krulec434c22d2018-11-28 13:48:36 +0100295 mTimeDifferences[mCounter % scheduler::ARRAY_SIZE] = differenceMs;
Ana Krulec7ab56032018-11-02 20:51:06 +0100296 mCounter++;
Ana Krulec434c22d2018-11-28 13:48:36 +0100297 int64_t mean = scheduler::calculate_mean(mTimeDifferences);
298 ATRACE_INT("AutoTimestampMean", mean);
Ana Krulec7ab56032018-11-02 20:51:06 +0100299
300 // TODO(b/113612090): This are current numbers from trial and error while running videos
301 // from YouTube at 24, 30, and 60 fps.
Ana Krulec434c22d2018-11-28 13:48:36 +0100302 if (mean > 14 && mean < 18) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100303 ATRACE_INT("FPS", 60);
Ana Krulec434c22d2018-11-28 13:48:36 +0100304 } else if (mean > 31 && mean < 34) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100305 ATRACE_INT("FPS", 30);
Ana Krulec7ab56032018-11-02 20:51:06 +0100306 return;
Ana Krulec434c22d2018-11-28 13:48:36 +0100307 } else if (mean > 39 && mean < 42) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100308 ATRACE_INT("FPS", 24);
309 }
Ana Krulec7ab56032018-11-02 20:51:06 +0100310}
311
Ana Krulec98b5b242018-08-10 15:03:23 -0700312} // namespace android