blob: 3060f1db5eb79862202d646418fddaf3f15c1c55 [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 Krulece588e312018-09-18 12:32:24 -070032#include <ui/DisplayStatInfo.h>
Ana Krulec3084c052018-11-21 20:27:17 +010033#include <utils/Timers.h>
Ana Krulec7ab56032018-11-02 20:51:06 +010034#include <utils/Trace.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070035
36#include "DispSync.h"
37#include "DispSyncSource.h"
Ana Krulece588e312018-09-18 12:32:24 -070038#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070039#include "EventThread.h"
Ana Krulecfb772822018-11-30 10:44:07 +010040#include "IdleTimer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070041#include "InjectVSyncSource.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010042#include "SchedulerUtils.h"
Sundong Ahnd5e08f62018-12-12 20:27:28 +090043#include "SurfaceFlingerProperties.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070044
45namespace android {
46
Ana Krulece588e312018-09-18 12:32:24 -070047using namespace android::hardware::configstore;
48using namespace android::hardware::configstore::V1_0;
Sundong Ahnd5e08f62018-12-12 20:27:28 +090049using namespace android::sysprop;
Ana Krulece588e312018-09-18 12:32:24 -070050
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)
Sundong Ahnd5e08f62018-12-12 20:27:28 +090059 : mHasSyncFramework(running_without_sync_framework(true)),
60 mDispSyncPresentTimeOffset(present_time_offset_from_vsync_ns(0)),
Ana Krulece588e312018-09-18 12:32:24 -070061 mPrimaryHWVsyncEnabled(false),
62 mHWVsyncAvailable(false) {
63 // Note: We create a local temporary with the real DispSync implementation
64 // type temporarily so we can initialize it with the configured values,
65 // before storing it for more generic use using the interface type.
66 auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync");
67 primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset);
68 mPrimaryDispSync = std::move(primaryDispSync);
69 mEventControlThread = std::make_unique<impl::EventControlThread>(function);
Ana Krulecfb772822018-11-30 10:44:07 +010070
71 char value[PROPERTY_VALUE_MAX];
Ana Krulec757f63a2019-01-25 10:46:18 -080072 property_get("debug.sf.set_idle_timer_ms", value, "30");
Ana Krulecfb772822018-11-30 10:44:07 +010073 mSetIdleTimerMs = atoi(value);
74
75 if (mSetIdleTimerMs > 0) {
76 mIdleTimer =
77 std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetIdleTimerMs),
78 [this] { expiredTimerCallback(); });
79 mIdleTimer->start();
80 }
Ana Krulece588e312018-09-18 12:32:24 -070081}
82
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080083Scheduler::~Scheduler() {
84 // Ensure the IdleTimer thread is joined before we start destroying state.
85 mIdleTimer.reset();
86}
Ana Krulec0c8cd522018-08-31 12:27:28 -070087
Ana Krulec98b5b242018-08-10 15:03:23 -070088sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
Dominik Laskowskibd52c842019-01-28 18:11:23 -080089 const char* 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,
Dominik Laskowskibd52c842019-01-28 18:11:23 -080096 std::move(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(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800107 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700108 impl::EventThread::InterceptVSyncsCallback interceptCallback) {
109 std::unique_ptr<VSyncSource> eventThreadSource =
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800110 std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName);
111 return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
112 std::move(interceptCallback),
113 [this] { resetIdleTimer(); }, connectionName);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700114}
115
Ana Krulec98b5b242018-08-10 15:03:23 -0700116sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
Dominik Laskowskif654d572018-12-20 11:03:06 -0800117 const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700118 RETURN_VALUE_IF_INVALID(nullptr);
Dominik Laskowskif654d572018-12-20 11:03:06 -0800119 return mConnections[handle->id]->thread->createEventConnection(std::move(resyncCallback));
Ana Krulec98b5b242018-08-10 15:03:23 -0700120}
121
122EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700123 RETURN_VALUE_IF_INVALID(nullptr);
124 return mConnections[handle->id]->thread.get();
Ana Krulec98b5b242018-08-10 15:03:23 -0700125}
126
Ana Krulec85c39af2018-12-26 17:29:57 -0800127sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700128 RETURN_VALUE_IF_INVALID(nullptr);
129 return mConnections[handle->id]->eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -0700130}
131
132void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800133 PhysicalDisplayId displayId, bool connected) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700134 RETURN_IF_INVALID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800135 mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
Ana Krulec98b5b242018-08-10 15:03:23 -0700136}
137
138void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700139 RETURN_IF_INVALID();
140 mConnections[handle->id]->thread->onScreenAcquired();
Ana Krulec98b5b242018-08-10 15:03:23 -0700141}
142
143void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700144 RETURN_IF_INVALID();
145 mConnections[handle->id]->thread->onScreenReleased();
Ana Krulec98b5b242018-08-10 15:03:23 -0700146}
147
Yiwei Zhang5434a782018-12-05 18:06:32 -0800148void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700149 RETURN_IF_INVALID();
150 mConnections.at(handle->id)->thread->dump(result);
Ana Krulec98b5b242018-08-10 15:03:23 -0700151}
152
153void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
Ana Krulec0c8cd522018-08-31 12:27:28 -0700154 RETURN_IF_INVALID();
155 mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
Ana Krulec98b5b242018-08-10 15:03:23 -0700156}
Ana Krulece588e312018-09-18 12:32:24 -0700157
158void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
159 stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
160 stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
161}
162
163void Scheduler::enableHardwareVsync() {
164 std::lock_guard<std::mutex> lock(mHWVsyncLock);
165 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
166 mPrimaryDispSync->beginResync();
167 mEventControlThread->setVsyncEnabled(true);
168 mPrimaryHWVsyncEnabled = true;
169 }
170}
171
172void Scheduler::disableHardwareVsync(bool makeUnavailable) {
173 std::lock_guard<std::mutex> lock(mHWVsyncLock);
174 if (mPrimaryHWVsyncEnabled) {
175 mEventControlThread->setVsyncEnabled(false);
176 mPrimaryDispSync->endResync();
177 mPrimaryHWVsyncEnabled = false;
178 }
179 if (makeUnavailable) {
180 mHWVsyncAvailable = false;
181 }
182}
183
184void Scheduler::setVsyncPeriod(const nsecs_t period) {
185 mPrimaryDispSync->reset();
186 mPrimaryDispSync->setPeriod(period);
187 enableHardwareVsync();
188}
189
190void Scheduler::addResyncSample(const nsecs_t timestamp) {
191 bool needsHwVsync = false;
192 { // Scope for the lock
193 std::lock_guard<std::mutex> lock(mHWVsyncLock);
194 if (mPrimaryHWVsyncEnabled) {
195 needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp);
196 }
197 }
198
199 if (needsHwVsync) {
200 enableHardwareVsync();
201 } else {
202 disableHardwareVsync(false);
203 }
204}
205
206void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
207 if (mPrimaryDispSync->addPresentFence(fenceTime)) {
208 enableHardwareVsync();
209 } else {
210 disableHardwareVsync(false);
211 }
212}
213
214void Scheduler::setIgnorePresentFences(bool ignore) {
215 mPrimaryDispSync->setIgnorePresentFences(ignore);
216}
217
Ana Krulec7ab56032018-11-02 20:51:06 +0100218void Scheduler::makeHWSyncAvailable(bool makeAvailable) {
219 std::lock_guard<std::mutex> lock(mHWVsyncLock);
220 mHWVsyncAvailable = makeAvailable;
221}
222
Ana Krulec3084c052018-11-21 20:27:17 +0100223void Scheduler::addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
224 const std::string layerName) {
225 // This is V1 logic. It calculates the average FPS based on the timestamp frequency
226 // regardless of which layer the timestamp came from.
227 // For now, the averages and FPS are recorded in the systrace.
228 determineTimestampAverage(isAutoTimestamp, framePresentTime);
229
230 // This is V2 logic. It calculates the average and median timestamp difference based on the
231 // individual layer history. The results are recorded in the systrace.
232 determineLayerTimestampStats(layerName, framePresentTime);
233}
234
235void Scheduler::incrementFrameCounter() {
236 mLayerHistory.incrementCounter();
237}
238
Ana Krulec7d1d6832018-12-27 11:10:09 -0800239void Scheduler::setExpiredIdleTimerCallback(const ExpiredIdleTimerCallback& expiredTimerCallback) {
240 std::lock_guard<std::mutex> lock(mCallbackLock);
241 mExpiredTimerCallback = expiredTimerCallback;
242}
243
244void Scheduler::setResetIdleTimerCallback(const ResetIdleTimerCallback& resetTimerCallback) {
245 std::lock_guard<std::mutex> lock(mCallbackLock);
246 mResetTimerCallback = resetTimerCallback;
247}
248
Ana Krulec3084c052018-11-21 20:27:17 +0100249void Scheduler::updateFrameSkipping(const int64_t skipCount) {
250 ATRACE_INT("FrameSkipCount", skipCount);
251 if (mSkipCount != skipCount) {
252 // Only update DispSync if it hasn't been updated yet.
253 mPrimaryDispSync->setRefreshSkipCount(skipCount);
254 mSkipCount = skipCount;
255 }
256}
257
258void Scheduler::determineLayerTimestampStats(const std::string layerName,
259 const nsecs_t framePresentTime) {
260 mLayerHistory.insert(layerName, framePresentTime);
261 std::vector<int64_t> differencesMs;
262
263 // Traverse through the layer history, and determine the differences in present times.
264 nsecs_t newestPresentTime = framePresentTime;
Ana Krulec434c22d2018-11-28 13:48:36 +0100265 std::string differencesText = "";
Ana Krulec3084c052018-11-21 20:27:17 +0100266 for (int i = 1; i < mLayerHistory.getSize(); i++) {
267 std::unordered_map<std::string, nsecs_t> layers = mLayerHistory.get(i);
268 for (auto layer : layers) {
269 if (layer.first != layerName) {
270 continue;
271 }
272 int64_t differenceMs = (newestPresentTime - layer.second) / 1000000;
Ana Krulec3084c052018-11-21 20:27:17 +0100273 // Dismiss noise.
274 if (differenceMs > 10 && differenceMs < 60) {
275 differencesMs.push_back(differenceMs);
276 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100277 IF_ALOGV() { differencesText += (std::to_string(differenceMs) + " "); }
Ana Krulec3084c052018-11-21 20:27:17 +0100278 newestPresentTime = layer.second;
279 }
280 }
Ana Krulec434c22d2018-11-28 13:48:36 +0100281 ALOGV("Layer %s timestamp intervals: %s", layerName.c_str(), differencesText.c_str());
Ana Krulec3084c052018-11-21 20:27:17 +0100282
Ana Krulec434c22d2018-11-28 13:48:36 +0100283 if (!differencesMs.empty()) {
284 // Mean/Average is a good indicator for when 24fps videos are playing, because the frames
285 // come in 33, and 49 ms intervals with occasional 41ms.
286 const int64_t meanMs = scheduler::calculate_mean(differencesMs);
287 const auto tagMean = "TimestampMean_" + layerName;
288 ATRACE_INT(tagMean.c_str(), meanMs);
289
290 // Mode and median are good indicators for 30 and 60 fps videos, because the majority of
291 // frames come in 16, or 33 ms intervals.
Ana Krulec3084c052018-11-21 20:27:17 +0100292 const auto tagMedian = "TimestampMedian_" + layerName;
Ana Krulec434c22d2018-11-28 13:48:36 +0100293 ATRACE_INT(tagMedian.c_str(), scheduler::calculate_median(&differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100294
Ana Krulec434c22d2018-11-28 13:48:36 +0100295 const auto tagMode = "TimestampMode_" + layerName;
296 ATRACE_INT(tagMode.c_str(), scheduler::calculate_mode(differencesMs));
Ana Krulec3084c052018-11-21 20:27:17 +0100297 }
Ana Krulec3084c052018-11-21 20:27:17 +0100298}
299
300void Scheduler::determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime) {
Ana Krulec7ab56032018-11-02 20:51:06 +0100301 ATRACE_INT("AutoTimestamp", isAutoTimestamp);
Ana Krulec3084c052018-11-21 20:27:17 +0100302
Ana Krulec7ab56032018-11-02 20:51:06 +0100303 // Video does not have timestamp automatically set, so we discard timestamps that are
304 // coming in from other sources for now.
305 if (isAutoTimestamp) {
306 return;
307 }
Ana Krulec3084c052018-11-21 20:27:17 +0100308 int64_t differenceMs = (framePresentTime - mPreviousFrameTimestamp) / 1000000;
309 mPreviousFrameTimestamp = framePresentTime;
Ana Krulec7ab56032018-11-02 20:51:06 +0100310
311 if (differenceMs < 10 || differenceMs > 100) {
312 // Dismiss noise.
313 return;
314 }
315 ATRACE_INT("TimestampDiff", differenceMs);
316
Ana Krulec434c22d2018-11-28 13:48:36 +0100317 mTimeDifferences[mCounter % scheduler::ARRAY_SIZE] = differenceMs;
Ana Krulec7ab56032018-11-02 20:51:06 +0100318 mCounter++;
Ana Krulec434c22d2018-11-28 13:48:36 +0100319 int64_t mean = scheduler::calculate_mean(mTimeDifferences);
320 ATRACE_INT("AutoTimestampMean", mean);
Ana Krulec7ab56032018-11-02 20:51:06 +0100321
322 // TODO(b/113612090): This are current numbers from trial and error while running videos
323 // from YouTube at 24, 30, and 60 fps.
Ana Krulec434c22d2018-11-28 13:48:36 +0100324 if (mean > 14 && mean < 18) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800325 ATRACE_INT("MediaFPS", 60);
Ana Krulec434c22d2018-11-28 13:48:36 +0100326 } else if (mean > 31 && mean < 34) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800327 ATRACE_INT("MediaFPS", 30);
Ana Krulec7ab56032018-11-02 20:51:06 +0100328 return;
Ana Krulec434c22d2018-11-28 13:48:36 +0100329 } else if (mean > 39 && mean < 42) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800330 ATRACE_INT("MediaFPS", 24);
Ana Krulec7ab56032018-11-02 20:51:06 +0100331 }
Ana Krulec7ab56032018-11-02 20:51:06 +0100332}
333
Ana Krulecfb772822018-11-30 10:44:07 +0100334void Scheduler::resetIdleTimer() {
335 if (mIdleTimer) {
336 mIdleTimer->reset();
337 ATRACE_INT("ExpiredIdleTimer", 0);
338 }
Ana Krulec7d1d6832018-12-27 11:10:09 -0800339
340 std::lock_guard<std::mutex> lock(mCallbackLock);
341 if (mResetTimerCallback) {
342 mResetTimerCallback();
343 }
Ana Krulecfb772822018-11-30 10:44:07 +0100344}
345
346void Scheduler::expiredTimerCallback() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800347 std::lock_guard<std::mutex> lock(mCallbackLock);
348 if (mExpiredTimerCallback) {
349 mExpiredTimerCallback();
350 ATRACE_INT("ExpiredIdleTimer", 1);
351 }
Ana Krulecfb772822018-11-30 10:44:07 +0100352}
353
Ana Krulecb43429d2019-01-09 14:28:51 -0800354std::string Scheduler::doDump() {
355 std::ostringstream stream;
356 stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
357 return stream.str();
358}
359
Ana Krulec98b5b242018-08-10 15:03:23 -0700360} // namespace android