blob: eb271cdccf8c3b16ea72d9ec7d1033c953a4170d [file] [log] [blame]
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001/*
2 * Copyright (C) 2013 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
17#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Tim Murray4a4e4a22016-04-19 16:29:23 +000018//#define LOG_NDEBUG 0
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070019
20// This is needed for stdint.h to define INT64_MAX in C++
21#define __STDC_LIMIT_MACROS
22
23#include <math.h>
24
Mark Salyzyna5e161b2016-09-29 08:08:05 -070025#include <algorithm>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070026
Mark Salyzyn7823e122016-09-29 08:08:05 -070027#include <log/log.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070028#include <utils/String8.h>
29#include <utils/Thread.h>
30#include <utils/Trace.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070031
Brian Andersonfbc80ae2017-05-26 16:23:54 -070032#include <ui/FenceTime.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070034#include "DispSync.h"
35#include "EventLog/EventLog.h"
Lloyd Pique78ce4182018-01-31 16:39:51 -080036#include "SurfaceFlinger.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070037
Tim Murray4a4e4a22016-04-19 16:29:23 +000038using std::max;
39using std::min;
40
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070041namespace android {
42
43// Setting this to true enables verbose tracing that can be used to debug
44// vsync event model or phase issues.
Andy McFadden5167ec62014-05-22 13:08:43 -070045static const bool kTraceDetailedInfo = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070046
Tim Murray4a4e4a22016-04-19 16:29:23 +000047// Setting this to true adds a zero-phase tracer for correlating with hardware
48// vsync events
49static const bool kEnableZeroPhaseTracer = false;
50
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070051// This is the threshold used to determine when hardware vsync events are
52// needed to re-synchronize the software vsync model with the hardware. The
53// error metric used is the mean of the squared difference between each
54// present time and the nearest software-predicted vsync.
Lloyd Pique78ce4182018-01-31 16:39:51 -080055static const nsecs_t kErrorThreshold = 160000000000; // 400 usec squared
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070056
Tim Murray4a4e4a22016-04-19 16:29:23 +000057#undef LOG_TAG
58#define LOG_TAG "DispSyncThread"
Lloyd Pique78ce4182018-01-31 16:39:51 -080059class DispSyncThread : public Thread {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070060public:
Lloyd Pique78ce4182018-01-31 16:39:51 -080061 explicit DispSyncThread(const char* name)
62 : mName(name),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070063 mStop(false),
64 mPeriod(0),
65 mPhase(0),
Haixia Shi676b1f62015-10-28 16:19:01 -070066 mReferenceTime(0),
Tim Murray4a4e4a22016-04-19 16:29:23 +000067 mWakeupLatency(0),
68 mFrameNumber(0) {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070069
70 virtual ~DispSyncThread() {}
71
Haixia Shi676b1f62015-10-28 16:19:01 -070072 void updateModel(nsecs_t period, nsecs_t phase, nsecs_t referenceTime) {
Tim Murray4a4e4a22016-04-19 16:29:23 +000073 if (kTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070074 Mutex::Autolock lock(mMutex);
75 mPeriod = period;
76 mPhase = phase;
Haixia Shi676b1f62015-10-28 16:19:01 -070077 mReferenceTime = referenceTime;
Tim Murray4a4e4a22016-04-19 16:29:23 +000078 ALOGV("[%s] updateModel: mPeriod = %" PRId64 ", mPhase = %" PRId64
Lloyd Pique78ce4182018-01-31 16:39:51 -080079 " mReferenceTime = %" PRId64,
80 mName, ns2us(mPeriod), ns2us(mPhase), ns2us(mReferenceTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070081 mCond.signal();
82 }
83
84 void stop() {
Tim Murray4a4e4a22016-04-19 16:29:23 +000085 if (kTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070086 Mutex::Autolock lock(mMutex);
87 mStop = true;
88 mCond.signal();
89 }
90
91 virtual bool threadLoop() {
92 status_t err;
93 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070094
95 while (true) {
Ana Krulec9a52b192018-07-12 18:18:47 -040096 std::vector<CallbackInvocation> callbackInvocations;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070097
98 nsecs_t targetTime = 0;
99
100 { // Scope for lock
101 Mutex::Autolock lock(mMutex);
102
Tim Murray4a4e4a22016-04-19 16:29:23 +0000103 if (kTraceDetailedInfo) {
104 ATRACE_INT64("DispSync:Frame", mFrameNumber);
105 }
106 ALOGV("[%s] Frame %" PRId64, mName, mFrameNumber);
107 ++mFrameNumber;
108
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700109 if (mStop) {
110 return false;
111 }
112
113 if (mPeriod == 0) {
114 err = mCond.wait(mMutex);
115 if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800116 ALOGE("error waiting for new events: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700117 return false;
118 }
119 continue;
120 }
121
Dan Stoza8f8374d2016-04-19 10:03:46 -0700122 targetTime = computeNextEventTimeLocked(now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700123
124 bool isWakeup = false;
125
126 if (now < targetTime) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000127 if (kTraceDetailedInfo) ATRACE_NAME("DispSync waiting");
Dan Stoza8f8374d2016-04-19 10:03:46 -0700128
129 if (targetTime == INT64_MAX) {
130 ALOGV("[%s] Waiting forever", mName);
131 err = mCond.wait(mMutex);
132 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800133 ALOGV("[%s] Waiting until %" PRId64, mName, ns2us(targetTime));
Dan Stoza8f8374d2016-04-19 10:03:46 -0700134 err = mCond.waitRelative(mMutex, targetTime - now);
135 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700136
137 if (err == TIMED_OUT) {
138 isWakeup = true;
139 } else if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800140 ALOGE("error waiting for next event: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700141 return false;
142 }
143 }
144
145 now = systemTime(SYSTEM_TIME_MONOTONIC);
146
Tim Murray4a4e4a22016-04-19 16:29:23 +0000147 // Don't correct by more than 1.5 ms
148 static const nsecs_t kMaxWakeupLatency = us2ns(1500);
149
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700150 if (isWakeup) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800151 mWakeupLatency = ((mWakeupLatency * 63) + (now - targetTime)) / 64;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000152 mWakeupLatency = min(mWakeupLatency, kMaxWakeupLatency);
Andy McFadden5167ec62014-05-22 13:08:43 -0700153 if (kTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000154 ATRACE_INT64("DispSync:WakeupLat", now - targetTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700155 ATRACE_INT64("DispSync:AvgWakeupLat", mWakeupLatency);
156 }
157 }
158
159 callbackInvocations = gatherCallbackInvocationsLocked(now);
160 }
161
162 if (callbackInvocations.size() > 0) {
Andy McFadden645b1f72014-06-10 14:43:32 -0700163 fireCallbackInvocations(callbackInvocations);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700164 }
165 }
166
167 return false;
168 }
169
Lloyd Piquee83f9312018-02-01 12:53:17 -0800170 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000171 if (kTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700172 Mutex::Autolock lock(mMutex);
173
174 for (size_t i = 0; i < mEventListeners.size(); i++) {
175 if (mEventListeners[i].mCallback == callback) {
176 return BAD_VALUE;
177 }
178 }
179
180 EventListener listener;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000181 listener.mName = name;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700182 listener.mPhase = phase;
183 listener.mCallback = callback;
Jamie Gennis629b9872013-10-29 13:36:12 -0700184
185 // We want to allow the firstmost future event to fire without
Tim Murray4a4e4a22016-04-19 16:29:23 +0000186 // allowing any past events to fire
Lloyd Pique78ce4182018-01-31 16:39:51 -0800187 listener.mLastEventTime = systemTime() - mPeriod / 2 + mPhase - mWakeupLatency;
Jamie Gennis629b9872013-10-29 13:36:12 -0700188
Ana Krulec9a52b192018-07-12 18:18:47 -0400189 mEventListeners.push_back(listener);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700190
191 mCond.signal();
192
193 return NO_ERROR;
194 }
195
Lloyd Piquee83f9312018-02-01 12:53:17 -0800196 status_t removeEventListener(DispSync::Callback* callback) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000197 if (kTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700198 Mutex::Autolock lock(mMutex);
199
Ana Krulec9a52b192018-07-12 18:18:47 -0400200 for (std::vector<EventListener>::iterator it = mEventListeners.begin();
201 it != mEventListeners.end(); ++it) {
202 if (it->mCallback == callback) {
203 mEventListeners.erase(it);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700204 mCond.signal();
205 return NO_ERROR;
206 }
207 }
208
209 return BAD_VALUE;
210 }
211
Dan Stoza84d619e2018-03-28 17:07:36 -0700212 status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
213 if (kTraceDetailedInfo) ATRACE_CALL();
214 Mutex::Autolock lock(mMutex);
215
Ana Krulec9a52b192018-07-12 18:18:47 -0400216 for (auto& eventListener : mEventListeners) {
217 if (eventListener.mCallback == callback) {
218 const nsecs_t oldPhase = eventListener.mPhase;
219 eventListener.mPhase = phase;
Dan Stoza84d619e2018-03-28 17:07:36 -0700220
221 // Pretend that the last time this event was handled at the same frame but with the
222 // new offset to allow for a seamless offset change without double-firing or
223 // skipping.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200224 nsecs_t diff = oldPhase - phase;
225 if (diff > mPeriod / 2) {
226 diff -= mPeriod;
227 } else if (diff < -mPeriod / 2) {
228 diff += mPeriod;
229 }
Ana Krulec9a52b192018-07-12 18:18:47 -0400230 eventListener.mLastEventTime -= diff;
Dan Stoza84d619e2018-03-28 17:07:36 -0700231 mCond.signal();
232 return NO_ERROR;
233 }
234 }
235
236 return BAD_VALUE;
237 }
238
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800239 // This method is only here to handle the !SurfaceFlinger::hasSyncFramework
240 // case.
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700241 bool hasAnyEventListeners() {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000242 if (kTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700243 Mutex::Autolock lock(mMutex);
244 return !mEventListeners.empty();
245 }
246
247private:
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700248 struct EventListener {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000249 const char* mName;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700250 nsecs_t mPhase;
251 nsecs_t mLastEventTime;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800252 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700253 };
254
255 struct CallbackInvocation {
Lloyd Piquee83f9312018-02-01 12:53:17 -0800256 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700257 nsecs_t mEventTime;
258 };
259
260 nsecs_t computeNextEventTimeLocked(nsecs_t now) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000261 if (kTraceDetailedInfo) ATRACE_CALL();
262 ALOGV("[%s] computeNextEventTimeLocked", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700263 nsecs_t nextEventTime = INT64_MAX;
264 for (size_t i = 0; i < mEventListeners.size(); i++) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800265 nsecs_t t = computeListenerNextEventTimeLocked(mEventListeners[i], now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700266
267 if (t < nextEventTime) {
268 nextEventTime = t;
269 }
270 }
271
Tim Murray4a4e4a22016-04-19 16:29:23 +0000272 ALOGV("[%s] nextEventTime = %" PRId64, mName, ns2us(nextEventTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700273 return nextEventTime;
274 }
275
Ana Krulec9a52b192018-07-12 18:18:47 -0400276 std::vector<CallbackInvocation> gatherCallbackInvocationsLocked(nsecs_t now) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000277 if (kTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800278 ALOGV("[%s] gatherCallbackInvocationsLocked @ %" PRId64, mName, ns2us(now));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000279
Ana Krulec9a52b192018-07-12 18:18:47 -0400280 std::vector<CallbackInvocation> callbackInvocations;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000281 nsecs_t onePeriodAgo = now - mPeriod;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700282
Ana Krulec9a52b192018-07-12 18:18:47 -0400283 for (auto& eventListener : mEventListeners) {
284 nsecs_t t = computeListenerNextEventTimeLocked(eventListener, onePeriodAgo);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700285
Jamie Gennis0d5c60e2013-10-09 17:49:37 -0700286 if (t < now) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700287 CallbackInvocation ci;
Ana Krulec9a52b192018-07-12 18:18:47 -0400288 ci.mCallback = eventListener.mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700289 ci.mEventTime = t;
Ana Krulec9a52b192018-07-12 18:18:47 -0400290 ALOGV("[%s] [%s] Preparing to fire", mName, eventListener.mName);
291 callbackInvocations.push_back(ci);
292 eventListener.mLastEventTime = t;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700293 }
294 }
295
296 return callbackInvocations;
297 }
298
Lloyd Pique78ce4182018-01-31 16:39:51 -0800299 nsecs_t computeListenerNextEventTimeLocked(const EventListener& listener, nsecs_t baseTime) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000300 if (kTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800301 ALOGV("[%s] [%s] computeListenerNextEventTimeLocked(%" PRId64 ")", mName, listener.mName,
302 ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700303
Tim Murray4a4e4a22016-04-19 16:29:23 +0000304 nsecs_t lastEventTime = listener.mLastEventTime + mWakeupLatency;
305 ALOGV("[%s] lastEventTime: %" PRId64, mName, ns2us(lastEventTime));
306 if (baseTime < lastEventTime) {
307 baseTime = lastEventTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800308 ALOGV("[%s] Clamping baseTime to lastEventTime -> %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700309 }
310
Tim Murray4a4e4a22016-04-19 16:29:23 +0000311 baseTime -= mReferenceTime;
312 ALOGV("[%s] Relative baseTime = %" PRId64, mName, ns2us(baseTime));
313 nsecs_t phase = mPhase + listener.mPhase;
314 ALOGV("[%s] Phase = %" PRId64, mName, ns2us(phase));
315 baseTime -= phase;
316 ALOGV("[%s] baseTime - phase = %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700317
Tim Murray4a4e4a22016-04-19 16:29:23 +0000318 // If our previous time is before the reference (because the reference
319 // has since been updated), the division by mPeriod will truncate
320 // towards zero instead of computing the floor. Since in all cases
321 // before the reference we want the next time to be effectively now, we
322 // set baseTime to -mPeriod so that numPeriods will be -1.
323 // When we add 1 and the phase, we will be at the correct event time for
324 // this period.
325 if (baseTime < 0) {
326 ALOGV("[%s] Correcting negative baseTime", mName);
327 baseTime = -mPeriod;
328 }
329
330 nsecs_t numPeriods = baseTime / mPeriod;
331 ALOGV("[%s] numPeriods = %" PRId64, mName, numPeriods);
332 nsecs_t t = (numPeriods + 1) * mPeriod + phase;
333 ALOGV("[%s] t = %" PRId64, mName, ns2us(t));
334 t += mReferenceTime;
335 ALOGV("[%s] Absolute t = %" PRId64, mName, ns2us(t));
336
337 // Check that it's been slightly more than half a period since the last
338 // event so that we don't accidentally fall into double-rate vsyncs
339 if (t - listener.mLastEventTime < (3 * mPeriod / 5)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700340 t += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000341 ALOGV("[%s] Modifying t -> %" PRId64, mName, ns2us(t));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700342 }
343
Tim Murray4a4e4a22016-04-19 16:29:23 +0000344 t -= mWakeupLatency;
345 ALOGV("[%s] Corrected for wakeup latency -> %" PRId64, mName, ns2us(t));
346
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700347 return t;
348 }
349
Ana Krulec9a52b192018-07-12 18:18:47 -0400350 void fireCallbackInvocations(const std::vector<CallbackInvocation>& callbacks) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000351 if (kTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700352 for (size_t i = 0; i < callbacks.size(); i++) {
353 callbacks[i].mCallback->onDispSyncEvent(callbacks[i].mEventTime);
354 }
355 }
356
Tim Murray4a4e4a22016-04-19 16:29:23 +0000357 const char* const mName;
358
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700359 bool mStop;
360
361 nsecs_t mPeriod;
362 nsecs_t mPhase;
Haixia Shi676b1f62015-10-28 16:19:01 -0700363 nsecs_t mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700364 nsecs_t mWakeupLatency;
365
Tim Murray4a4e4a22016-04-19 16:29:23 +0000366 int64_t mFrameNumber;
367
Ana Krulec9a52b192018-07-12 18:18:47 -0400368 std::vector<EventListener> mEventListeners;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700369
370 Mutex mMutex;
371 Condition mCond;
372};
373
Tim Murray4a4e4a22016-04-19 16:29:23 +0000374#undef LOG_TAG
375#define LOG_TAG "DispSync"
376
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700377class ZeroPhaseTracer : public DispSync::Callback {
378public:
379 ZeroPhaseTracer() : mParity(false) {}
380
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700381 virtual void onDispSyncEvent(nsecs_t /*when*/) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700382 mParity = !mParity;
383 ATRACE_INT("ZERO_PHASE_VSYNC", mParity ? 1 : 0);
384 }
385
386private:
387 bool mParity;
388};
389
Lloyd Pique78ce4182018-01-31 16:39:51 -0800390DispSync::DispSync(const char* name)
391 : mName(name), mRefreshSkipCount(0), mThread(new DispSyncThread(name)) {}
Andy McFadden645b1f72014-06-10 14:43:32 -0700392
Saurabh Shahf4174532017-07-13 10:45:07 -0700393DispSync::~DispSync() {}
394
395void DispSync::init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset) {
396 mIgnorePresentFences = !hasSyncFramework;
397 mPresentTimeOffset = dispSyncPresentTimeOffset;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700398 mThread->run("DispSync", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
Saurabh Shahf4174532017-07-13 10:45:07 -0700399
Tim Murrayacff43d2016-07-29 13:57:24 -0700400 // set DispSync to SCHED_FIFO to minimize jitter
401 struct sched_param param = {0};
Tim Murray35520632016-09-07 12:18:17 -0700402 param.sched_priority = 2;
Tim Murrayacff43d2016-07-29 13:57:24 -0700403 if (sched_setscheduler(mThread->getTid(), SCHED_FIFO, &param) != 0) {
404 ALOGE("Couldn't set SCHED_FIFO for DispSyncThread");
405 }
406
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700407 reset();
408 beginResync();
409
Andy McFadden5167ec62014-05-22 13:08:43 -0700410 if (kTraceDetailedInfo) {
411 // If we're not getting present fences then the ZeroPhaseTracer
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700412 // would prevent HW vsync event from ever being turned off.
Andy McFadden5167ec62014-05-22 13:08:43 -0700413 // Even if we're just ignoring the fences, the zero-phase tracing is
414 // not needed because any time there is an event registered we will
415 // turn on the HW vsync events.
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800416 if (!mIgnorePresentFences && kEnableZeroPhaseTracer) {
Lloyd Piquee83f9312018-02-01 12:53:17 -0800417 mZeroPhaseTracer = std::make_unique<ZeroPhaseTracer>();
418 addEventListener("ZeroPhaseTracer", 0, mZeroPhaseTracer.get());
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700419 }
420 }
421}
422
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700423void DispSync::reset() {
424 Mutex::Autolock lock(mMutex);
425
Haixia Shi676b1f62015-10-28 16:19:01 -0700426 mPhase = 0;
427 mReferenceTime = 0;
428 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700429 mNumResyncSamples = 0;
430 mFirstResyncSample = 0;
431 mNumResyncSamplesSincePresent = 0;
432 resetErrorLocked();
433}
434
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700435bool DispSync::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700436 Mutex::Autolock lock(mMutex);
437
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700438 mPresentFences[mPresentSampleOffset] = fenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700439 mPresentSampleOffset = (mPresentSampleOffset + 1) % NUM_PRESENT_SAMPLES;
440 mNumResyncSamplesSincePresent = 0;
441
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700442 updateErrorLocked();
443
Haixia Shi676b1f62015-10-28 16:19:01 -0700444 return !mModelUpdated || mError > kErrorThreshold;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700445}
446
447void DispSync::beginResync() {
448 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000449 ALOGV("[%s] beginResync", mName);
Haixia Shi676b1f62015-10-28 16:19:01 -0700450 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700451 mNumResyncSamples = 0;
452}
453
454bool DispSync::addResyncSample(nsecs_t timestamp) {
455 Mutex::Autolock lock(mMutex);
456
Tim Murray4a4e4a22016-04-19 16:29:23 +0000457 ALOGV("[%s] addResyncSample(%" PRId64 ")", mName, ns2us(timestamp));
458
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700459 size_t idx = (mFirstResyncSample + mNumResyncSamples) % MAX_RESYNC_SAMPLES;
460 mResyncSamples[idx] = timestamp;
Haixia Shi664339a2015-10-28 13:22:22 -0700461 if (mNumResyncSamples == 0) {
Haixia Shi676b1f62015-10-28 16:19:01 -0700462 mPhase = 0;
463 mReferenceTime = timestamp;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000464 ALOGV("[%s] First resync sample: mPeriod = %" PRId64 ", mPhase = 0, "
Lloyd Pique78ce4182018-01-31 16:39:51 -0800465 "mReferenceTime = %" PRId64,
466 mName, ns2us(mPeriod), ns2us(mReferenceTime));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000467 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Haixia Shi664339a2015-10-28 13:22:22 -0700468 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700469
470 if (mNumResyncSamples < MAX_RESYNC_SAMPLES) {
471 mNumResyncSamples++;
472 } else {
473 mFirstResyncSample = (mFirstResyncSample + 1) % MAX_RESYNC_SAMPLES;
474 }
475
476 updateModelLocked();
477
478 if (mNumResyncSamplesSincePresent++ > MAX_RESYNC_SAMPLES_WITHOUT_PRESENT) {
479 resetErrorLocked();
480 }
481
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800482 if (mIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700483 // If we don't have the sync framework we will never have
484 // addPresentFence called. This means we have no way to know whether
485 // or not we're synchronized with the HW vsyncs, so we just request
486 // that the HW vsync events be turned on whenever we need to generate
487 // SW vsync events.
488 return mThread->hasAnyEventListeners();
489 }
490
Tim Murray4a4e4a22016-04-19 16:29:23 +0000491 // Check against kErrorThreshold / 2 to add some hysteresis before having to
492 // resync again
493 bool modelLocked = mModelUpdated && mError < (kErrorThreshold / 2);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800494 ALOGV("[%s] addResyncSample returning %s", mName, modelLocked ? "locked" : "unlocked");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000495 return !modelLocked;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700496}
497
Lloyd Pique78ce4182018-01-31 16:39:51 -0800498void DispSync::endResync() {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700499
Lloyd Piquee83f9312018-02-01 12:53:17 -0800500status_t DispSync::addEventListener(const char* name, nsecs_t phase, Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700501 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000502 return mThread->addEventListener(name, phase, callback);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700503}
504
Andy McFadden645b1f72014-06-10 14:43:32 -0700505void DispSync::setRefreshSkipCount(int count) {
506 Mutex::Autolock lock(mMutex);
507 ALOGD("setRefreshSkipCount(%d)", count);
508 mRefreshSkipCount = count;
509 updateModelLocked();
Ruchi Kandoif52b3c82014-04-24 16:42:35 -0700510}
511
Lloyd Piquee83f9312018-02-01 12:53:17 -0800512status_t DispSync::removeEventListener(Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700513 Mutex::Autolock lock(mMutex);
514 return mThread->removeEventListener(callback);
515}
516
Dan Stoza84d619e2018-03-28 17:07:36 -0700517status_t DispSync::changePhaseOffset(Callback* callback, nsecs_t phase) {
518 Mutex::Autolock lock(mMutex);
519 return mThread->changePhaseOffset(callback, phase);
520}
521
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700522void DispSync::setPeriod(nsecs_t period) {
523 Mutex::Autolock lock(mMutex);
524 mPeriod = period;
525 mPhase = 0;
Haixia Shi676b1f62015-10-28 16:19:01 -0700526 mReferenceTime = 0;
527 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700528}
529
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700530nsecs_t DispSync::getPeriod() {
531 // lock mutex as mPeriod changes multiple times in updateModelLocked
532 Mutex::Autolock lock(mMutex);
533 return mPeriod;
534}
535
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700536void DispSync::updateModelLocked() {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000537 ALOGV("[%s] updateModelLocked %zu", mName, mNumResyncSamples);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700538 if (mNumResyncSamples >= MIN_RESYNC_SAMPLES_FOR_UPDATE) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000539 ALOGV("[%s] Computing...", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700540 nsecs_t durationSum = 0;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000541 nsecs_t minDuration = INT64_MAX;
542 nsecs_t maxDuration = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700543 for (size_t i = 1; i < mNumResyncSamples; i++) {
544 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
545 size_t prev = (idx + MAX_RESYNC_SAMPLES - 1) % MAX_RESYNC_SAMPLES;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000546 nsecs_t duration = mResyncSamples[idx] - mResyncSamples[prev];
547 durationSum += duration;
548 minDuration = min(minDuration, duration);
549 maxDuration = max(maxDuration, duration);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700550 }
551
Tim Murray4a4e4a22016-04-19 16:29:23 +0000552 // Exclude the min and max from the average
553 durationSum -= minDuration + maxDuration;
554 mPeriod = durationSum / (mNumResyncSamples - 3);
555
556 ALOGV("[%s] mPeriod = %" PRId64, mName, ns2us(mPeriod));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700557
558 double sampleAvgX = 0;
559 double sampleAvgY = 0;
560 double scale = 2.0 * M_PI / double(mPeriod);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000561 // Intentionally skip the first sample
562 for (size_t i = 1; i < mNumResyncSamples; i++) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700563 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
Haixia Shi676b1f62015-10-28 16:19:01 -0700564 nsecs_t sample = mResyncSamples[idx] - mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700565 double samplePhase = double(sample % mPeriod) * scale;
566 sampleAvgX += cos(samplePhase);
567 sampleAvgY += sin(samplePhase);
568 }
569
Tim Murray4a4e4a22016-04-19 16:29:23 +0000570 sampleAvgX /= double(mNumResyncSamples - 1);
571 sampleAvgY /= double(mNumResyncSamples - 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700572
573 mPhase = nsecs_t(atan2(sampleAvgY, sampleAvgX) / scale);
574
Tim Murray4a4e4a22016-04-19 16:29:23 +0000575 ALOGV("[%s] mPhase = %" PRId64, mName, ns2us(mPhase));
576
577 if (mPhase < -(mPeriod / 2)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700578 mPhase += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000579 ALOGV("[%s] Adjusting mPhase -> %" PRId64, mName, ns2us(mPhase));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700580 }
581
Andy McFadden5167ec62014-05-22 13:08:43 -0700582 if (kTraceDetailedInfo) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700583 ATRACE_INT64("DispSync:Period", mPeriod);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000584 ATRACE_INT64("DispSync:Phase", mPhase + mPeriod / 2);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700585 }
586
Andy McFadden645b1f72014-06-10 14:43:32 -0700587 // Artificially inflate the period if requested.
588 mPeriod += mPeriod * mRefreshSkipCount;
589
Haixia Shi676b1f62015-10-28 16:19:01 -0700590 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
591 mModelUpdated = true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700592 }
593}
594
595void DispSync::updateErrorLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700596 if (!mModelUpdated) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700597 return;
598 }
599
Andy McFadden645b1f72014-06-10 14:43:32 -0700600 // Need to compare present fences against the un-adjusted refresh period,
601 // since they might arrive between two events.
602 nsecs_t period = mPeriod / (1 + mRefreshSkipCount);
603
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700604 int numErrSamples = 0;
605 nsecs_t sqErrSum = 0;
606
607 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700608 // Only check for the cached value of signal time to avoid unecessary
609 // syscalls. It is the responsibility of the DispSync owner to
610 // call getSignalTime() periodically so the cache is updated when the
611 // fence signals.
612 nsecs_t time = mPresentFences[i]->getCachedSignalTime();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800613 if (time == Fence::SIGNAL_TIME_PENDING || time == Fence::SIGNAL_TIME_INVALID) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700614 continue;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700615 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700616
617 nsecs_t sample = time - mReferenceTime;
618 if (sample <= mPhase) {
619 continue;
620 }
621
622 nsecs_t sampleErr = (sample - mPhase) % period;
623 if (sampleErr > period / 2) {
624 sampleErr -= period;
625 }
626 sqErrSum += sampleErr * sampleErr;
627 numErrSamples++;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700628 }
629
630 if (numErrSamples > 0) {
631 mError = sqErrSum / numErrSamples;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700632 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700633 } else {
634 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700635 // Use mod ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT to avoid log spam.
636 mZeroErrSamplesCount++;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800637 ALOGE_IF((mZeroErrSamplesCount % ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT) == 0,
638 "No present times for model error.");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700639 }
640
Andy McFadden5167ec62014-05-22 13:08:43 -0700641 if (kTraceDetailedInfo) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700642 ATRACE_INT64("DispSync:Error", mError);
643 }
644}
645
646void DispSync::resetErrorLocked() {
647 mPresentSampleOffset = 0;
648 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700649 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700650 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700651 mPresentFences[i] = FenceTime::NO_FENCE;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700652 }
653}
654
Andy McFadden41d67d72014-04-25 16:58:34 -0700655nsecs_t DispSync::computeNextRefresh(int periodOffset) const {
Andy McFadden150ecd82014-05-08 14:56:50 -0700656 Mutex::Autolock lock(mMutex);
Andy McFadden41d67d72014-04-25 16:58:34 -0700657 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Haixia Shi676b1f62015-10-28 16:19:01 -0700658 nsecs_t phase = mReferenceTime + mPhase;
659 return (((now - phase) / mPeriod) + periodOffset + 1) * mPeriod + phase;
Andy McFadden41d67d72014-04-25 16:58:34 -0700660}
661
Andy McFaddenc751e922014-05-08 14:53:26 -0700662void DispSync::dump(String8& result) const {
663 Mutex::Autolock lock(mMutex);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800664 result.appendFormat("present fences are %s\n", mIgnorePresentFences ? "ignored" : "used");
665 result.appendFormat("mPeriod: %" PRId64 " ns (%.3f fps; skipCount=%d)\n", mPeriod,
666 1000000000.0 / mPeriod, mRefreshSkipCount);
Andy McFadden5167ec62014-05-22 13:08:43 -0700667 result.appendFormat("mPhase: %" PRId64 " ns\n", mPhase);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800668 result.appendFormat("mError: %" PRId64 " ns (sqrt=%.1f)\n", mError, sqrt(mError));
Andy McFadden5167ec62014-05-22 13:08:43 -0700669 result.appendFormat("mNumResyncSamplesSincePresent: %d (limit %d)\n",
Lloyd Pique78ce4182018-01-31 16:39:51 -0800670 mNumResyncSamplesSincePresent, MAX_RESYNC_SAMPLES_WITHOUT_PRESENT);
671 result.appendFormat("mNumResyncSamples: %zd (max %d)\n", mNumResyncSamples, MAX_RESYNC_SAMPLES);
Andy McFaddenc751e922014-05-08 14:53:26 -0700672
673 result.appendFormat("mResyncSamples:\n");
674 nsecs_t previous = -1;
675 for (size_t i = 0; i < mNumResyncSamples; i++) {
676 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
677 nsecs_t sampleTime = mResyncSamples[idx];
678 if (i == 0) {
Andy McFadden5167ec62014-05-22 13:08:43 -0700679 result.appendFormat(" %" PRId64 "\n", sampleTime);
Andy McFaddenc751e922014-05-08 14:53:26 -0700680 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800681 result.appendFormat(" %" PRId64 " (+%" PRId64 ")\n", sampleTime,
682 sampleTime - previous);
Andy McFaddenc751e922014-05-08 14:53:26 -0700683 }
684 previous = sampleTime;
685 }
686
Lloyd Pique78ce4182018-01-31 16:39:51 -0800687 result.appendFormat("mPresentFences [%d]:\n", NUM_PRESENT_SAMPLES);
Andy McFadden5167ec62014-05-22 13:08:43 -0700688 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700689 previous = Fence::SIGNAL_TIME_INVALID;
Andy McFaddenc751e922014-05-08 14:53:26 -0700690 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
691 size_t idx = (i + mPresentSampleOffset) % NUM_PRESENT_SAMPLES;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700692 nsecs_t presentTime = mPresentFences[idx]->getSignalTime();
693 if (presentTime == Fence::SIGNAL_TIME_PENDING) {
Andy McFaddenc751e922014-05-08 14:53:26 -0700694 result.appendFormat(" [unsignaled fence]\n");
Lloyd Pique78ce4182018-01-31 16:39:51 -0800695 } else if (presentTime == Fence::SIGNAL_TIME_INVALID) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700696 result.appendFormat(" [invalid fence]\n");
697 } else if (previous == Fence::SIGNAL_TIME_PENDING ||
Lloyd Pique78ce4182018-01-31 16:39:51 -0800698 previous == Fence::SIGNAL_TIME_INVALID) {
Andy McFadden5167ec62014-05-22 13:08:43 -0700699 result.appendFormat(" %" PRId64 " (%.3f ms ago)\n", presentTime,
Lloyd Pique78ce4182018-01-31 16:39:51 -0800700 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700701 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800702 result.appendFormat(" %" PRId64 " (+%" PRId64 " / %.3f) (%.3f ms ago)\n", presentTime,
703 presentTime - previous, (presentTime - previous) / (double)mPeriod,
704 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700705 }
706 previous = presentTime;
707 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700708
709 result.appendFormat("current monotonic time: %" PRId64 "\n", now);
Andy McFaddenc751e922014-05-08 14:53:26 -0700710}
711
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700712} // namespace android