blob: c363ba57f7e9e40bc03f0dd3f213e0c925794f3e [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 Krulec3084c052018-11-21 20:27:17 +0100241void Scheduler::updateFrameSkipping(const int64_t skipCount) {
242 ATRACE_INT("FrameSkipCount", skipCount);
243 if (mSkipCount != skipCount) {
244 // Only update DispSync if it hasn't been updated yet.
245 mPrimaryDispSync->setRefreshSkipCount(skipCount);
246 mSkipCount = skipCount;
247 }
248}
249
250void Scheduler::determineLayerTimestampStats(const std::string layerName,
251 const nsecs_t framePresentTime) {
252 mLayerHistory.insert(layerName, framePresentTime);
253 std::vector<int64_t> differencesMs;
254
255 // Traverse through the layer history, and determine the differences in present times.
256 nsecs_t newestPresentTime = framePresentTime;
Ana Krulec434c22d2018-11-28 13:48:36 +0100257 std::string differencesText = "";
Ana Krulec3084c052018-11-21 20:27:17 +0100258 for (int i = 1; i < mLayerHistory.getSize(); i++) {
259 std::unordered_map<std::string, nsecs_t> layers = mLayerHistory.get(i);
260 for (auto layer : layers) {
261 if (layer.first != layerName) {
262 continue;
263 }
264 int64_t differenceMs = (newestPresentTime - layer.second) / 1000000;
Ana Krulec3084c052018-11-21 20:27:17 +0100265 // Dismiss noise.
266 if (differenceMs > 10 && differenceMs < 60) {
267 differencesMs.push_back(differenceMs);
268 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100269 IF_ALOGV() { differencesText += (std::to_string(differenceMs) + " "); }
Ana Krulec3084c052018-11-21 20:27:17 +0100270 newestPresentTime = layer.second;
271 }
272 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100273 ALOGV("Layer %s timestamp intervals: %s", layerName.c_str(), differencesText.c_str());
Ana Krulec3084c052018-11-21 20:27:17 +0100274
Ana Krulec434c22d2018-11-28 13:48:36 +0100275 if (!differencesMs.empty()) {
276 // Mean/Average is a good indicator for when 24fps videos are playing, because the frames
277 // come in 33, and 49 ms intervals with occasional 41ms.
278 const int64_t meanMs = scheduler::calculate_mean(differencesMs);
279 const auto tagMean = "TimestampMean_" + layerName;
280 ATRACE_INT(tagMean.c_str(), meanMs);
281
282 // Mode and median are good indicators for 30 and 60 fps videos, because the majority of
283 // frames come in 16, or 33 ms intervals.
Ana Krulec3084c052018-11-21 20:27:17 +0100284 const auto tagMedian = "TimestampMedian_" + layerName;
Ana Krulec434c22d2018-11-28 13:48:36 +0100285 ATRACE_INT(tagMedian.c_str(), scheduler::calculate_median(&differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100286
Ana Krulec434c22d2018-11-28 13:48:36 +0100287 const auto tagMode = "TimestampMode_" + layerName;
288 ATRACE_INT(tagMode.c_str(), scheduler::calculate_mode(differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100289 }
Ana Krulec3084c052018-11-21 20:27:17 +0100290}
291
292void Scheduler::determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100293 ATRACE_INT("AutoTimestamp", isAutoTimestamp);
Ana Krulec3084c052018-11-21 20:27:17 +0100294
Ana Krulec7ab56032018-11-02 20:51:06 +0100295 // Video does not have timestamp automatically set, so we discard timestamps that are
296 // coming in from other sources for now.
297 if (isAutoTimestamp) {
298 return;
299 }
Ana Krulec3084c052018-11-21 20:27:17 +0100300 int64_t differenceMs = (framePresentTime - mPreviousFrameTimestamp) / 1000000;
301 mPreviousFrameTimestamp = framePresentTime;
Ana Krulec7ab56032018-11-02 20:51:06 +0100302
303 if (differenceMs < 10 || differenceMs > 100) {
304 // Dismiss noise.
305 return;
306 }
307 ATRACE_INT("TimestampDiff", differenceMs);
308
Ana Krulec434c22d2018-11-28 13:48:36 +0100309 mTimeDifferences[mCounter % scheduler::ARRAY_SIZE] = differenceMs;
Ana Krulec7ab56032018-11-02 20:51:06 +0100310 mCounter++;
Ana Krulec434c22d2018-11-28 13:48:36 +0100311 int64_t mean = scheduler::calculate_mean(mTimeDifferences);
312 ATRACE_INT("AutoTimestampMean", mean);
Ana Krulec7ab56032018-11-02 20:51:06 +0100313
314 // TODO(b/113612090): This are current numbers from trial and error while running videos
315 // from YouTube at 24, 30, and 60 fps.
Ana Krulec434c22d2018-11-28 13:48:36 +0100316 if (mean > 14 && mean < 18) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100317 ATRACE_INT("FPS", 60);
Ana Krulec434c22d2018-11-28 13:48:36 +0100318 } else if (mean > 31 && mean < 34) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100319 ATRACE_INT("FPS", 30);
Ana Krulec7ab56032018-11-02 20:51:06 +0100320 return;
Ana Krulec434c22d2018-11-28 13:48:36 +0100321 } else if (mean > 39 && mean < 42) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100322 ATRACE_INT("FPS", 24);
323 }
Ana Krulec7ab56032018-11-02 20:51:06 +0100324}
325
Ana Krulecfb772822018-11-30 10:44:07 +0100326void Scheduler::resetIdleTimer() {
327 if (mIdleTimer) {
328 mIdleTimer->reset();
329 ATRACE_INT("ExpiredIdleTimer", 0);
330 }
331}
332
333void Scheduler::expiredTimerCallback() {
334 // TODO(b/113612090): Each time a timer expired, we should record the information into
335 // a circular buffer. Once this has happened a given amount (TBD) of times, we can comfortably
336 // say that the device is sitting in idle.
337 ATRACE_INT("ExpiredIdleTimer", 1);
338}
339
Ana Krulec98b5b242018-08-10 15:03:23 -0700340} // namespace android