blob: f72aef1bddb6b6db1ff8d06be2079a67e6fe6f0e [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
Yiwei Zhang5434a782018-12-05 18:06:32 -080027#include <android-base/stringprintf.h>
David Sodman1afa8b02018-10-15 11:20:48 -070028#include <cutils/properties.h>
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <log/log.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070030#include <utils/Thread.h>
31#include <utils/Trace.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070032
Brian Andersonfbc80ae2017-05-26 16:23:54 -070033#include <ui/FenceTime.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070034
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070035#include "DispSync.h"
36#include "EventLog/EventLog.h"
Lloyd Pique78ce4182018-01-31 16:39:51 -080037#include "SurfaceFlinger.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070038
Yiwei Zhang5434a782018-12-05 18:06:32 -080039using android::base::StringAppendF;
Tim Murray4a4e4a22016-04-19 16:29:23 +000040using std::max;
41using std::min;
42
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070043namespace android {
44
Lloyd Pique41be5d22018-06-21 13:11:48 -070045DispSync::~DispSync() = default;
Kevin DuBois7b743be2019-02-27 10:05:48 -080046DispSync::Callback::~Callback() = default;
Lloyd Pique41be5d22018-06-21 13:11:48 -070047
48namespace impl {
49
Tim Murray4a4e4a22016-04-19 16:29:23 +000050// Setting this to true adds a zero-phase tracer for correlating with hardware
51// vsync events
52static const bool kEnableZeroPhaseTracer = false;
53
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070054// This is the threshold used to determine when hardware vsync events are
55// needed to re-synchronize the software vsync model with the hardware. The
56// error metric used is the mean of the squared difference between each
57// present time and the nearest software-predicted vsync.
Lloyd Pique78ce4182018-01-31 16:39:51 -080058static const nsecs_t kErrorThreshold = 160000000000; // 400 usec squared
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070059
Tim Murray4a4e4a22016-04-19 16:29:23 +000060#undef LOG_TAG
61#define LOG_TAG "DispSyncThread"
Lloyd Pique78ce4182018-01-31 16:39:51 -080062class DispSyncThread : public Thread {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070063public:
Ana Krulec064a82f2018-09-11 16:03:03 -070064 DispSyncThread(const char* name, bool showTraceDetailedInfo)
Lloyd Pique78ce4182018-01-31 16:39:51 -080065 : mName(name),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070066 mStop(false),
67 mPeriod(0),
68 mPhase(0),
Haixia Shi676b1f62015-10-28 16:19:01 -070069 mReferenceTime(0),
Tim Murray4a4e4a22016-04-19 16:29:23 +000070 mWakeupLatency(0),
Ana Krulec064a82f2018-09-11 16:03:03 -070071 mFrameNumber(0),
72 mTraceDetailedInfo(showTraceDetailedInfo) {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070073
74 virtual ~DispSyncThread() {}
75
Haixia Shi676b1f62015-10-28 16:19:01 -070076 void updateModel(nsecs_t period, nsecs_t phase, nsecs_t referenceTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -070077 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070078 Mutex::Autolock lock(mMutex);
Alec Mouri75b0b622019-03-12 18:56:00 +000079
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070080 mPhase = phase;
Haixia Shi676b1f62015-10-28 16:19:01 -070081 mReferenceTime = referenceTime;
Alec Mouri75b0b622019-03-12 18:56:00 +000082 if (mPeriod != 0 && mPeriod != period && mReferenceTime != 0) {
83 // Inflate the reference time to be the most recent predicted
84 // vsync before the current time.
85 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
86 const nsecs_t baseTime = now - mReferenceTime;
87 const nsecs_t numOldPeriods = baseTime / mPeriod;
88 mReferenceTime = mReferenceTime + (numOldPeriods)*mPeriod;
89 }
90 mPeriod = period;
Alec Mourif5fe85c2019-02-22 13:40:36 -080091 if (mTraceDetailedInfo) {
92 ATRACE_INT64("DispSync:Period", mPeriod);
93 ATRACE_INT64("DispSync:Phase", mPhase + mPeriod / 2);
94 ATRACE_INT64("DispSync:Reference Time", mReferenceTime);
95 }
Tim Murray4a4e4a22016-04-19 16:29:23 +000096 ALOGV("[%s] updateModel: mPeriod = %" PRId64 ", mPhase = %" PRId64
Lloyd Pique78ce4182018-01-31 16:39:51 -080097 " mReferenceTime = %" PRId64,
98 mName, ns2us(mPeriod), ns2us(mPhase), ns2us(mReferenceTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070099 mCond.signal();
100 }
101
102 void stop() {
Ana Krulec064a82f2018-09-11 16:03:03 -0700103 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700104 Mutex::Autolock lock(mMutex);
105 mStop = true;
106 mCond.signal();
107 }
108
109 virtual bool threadLoop() {
110 status_t err;
111 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700112
113 while (true) {
Ana Krulec9a52b192018-07-12 18:18:47 -0400114 std::vector<CallbackInvocation> callbackInvocations;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700115
116 nsecs_t targetTime = 0;
117
118 { // Scope for lock
119 Mutex::Autolock lock(mMutex);
120
Ana Krulec064a82f2018-09-11 16:03:03 -0700121 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000122 ATRACE_INT64("DispSync:Frame", mFrameNumber);
123 }
124 ALOGV("[%s] Frame %" PRId64, mName, mFrameNumber);
125 ++mFrameNumber;
126
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700127 if (mStop) {
128 return false;
129 }
130
131 if (mPeriod == 0) {
132 err = mCond.wait(mMutex);
133 if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800134 ALOGE("error waiting for new events: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700135 return false;
136 }
137 continue;
138 }
139
Dan Stoza8f8374d2016-04-19 10:03:46 -0700140 targetTime = computeNextEventTimeLocked(now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700141
142 bool isWakeup = false;
143
144 if (now < targetTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700145 if (mTraceDetailedInfo) ATRACE_NAME("DispSync waiting");
Dan Stoza8f8374d2016-04-19 10:03:46 -0700146
147 if (targetTime == INT64_MAX) {
148 ALOGV("[%s] Waiting forever", mName);
149 err = mCond.wait(mMutex);
150 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800151 ALOGV("[%s] Waiting until %" PRId64, mName, ns2us(targetTime));
Dan Stoza8f8374d2016-04-19 10:03:46 -0700152 err = mCond.waitRelative(mMutex, targetTime - now);
153 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700154
155 if (err == TIMED_OUT) {
156 isWakeup = true;
157 } else if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800158 ALOGE("error waiting for next event: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700159 return false;
160 }
161 }
162
163 now = systemTime(SYSTEM_TIME_MONOTONIC);
164
Tim Murray4a4e4a22016-04-19 16:29:23 +0000165 // Don't correct by more than 1.5 ms
166 static const nsecs_t kMaxWakeupLatency = us2ns(1500);
167
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700168 if (isWakeup) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800169 mWakeupLatency = ((mWakeupLatency * 63) + (now - targetTime)) / 64;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000170 mWakeupLatency = min(mWakeupLatency, kMaxWakeupLatency);
Ana Krulec064a82f2018-09-11 16:03:03 -0700171 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000172 ATRACE_INT64("DispSync:WakeupLat", now - targetTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700173 ATRACE_INT64("DispSync:AvgWakeupLat", mWakeupLatency);
174 }
175 }
176
177 callbackInvocations = gatherCallbackInvocationsLocked(now);
178 }
179
180 if (callbackInvocations.size() > 0) {
Andy McFadden645b1f72014-06-10 14:43:32 -0700181 fireCallbackInvocations(callbackInvocations);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700182 }
183 }
184
185 return false;
186 }
187
Alec Mouri7355eb22019-03-05 14:19:10 -0800188 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback,
189 nsecs_t lastCallbackTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700190 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700191 Mutex::Autolock lock(mMutex);
192
193 for (size_t i = 0; i < mEventListeners.size(); i++) {
194 if (mEventListeners[i].mCallback == callback) {
195 return BAD_VALUE;
196 }
197 }
198
199 EventListener listener;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000200 listener.mName = name;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700201 listener.mPhase = phase;
202 listener.mCallback = callback;
Jamie Gennis629b9872013-10-29 13:36:12 -0700203
204 // We want to allow the firstmost future event to fire without
Alec Mouri81835982019-02-27 13:40:44 -0800205 // allowing any past events to fire. To do this extrapolate from
206 // mReferenceTime the most recent hardware vsync, and pin the
207 // last event time there.
208 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
209 if (mPeriod != 0) {
210 const nsecs_t baseTime = now - mReferenceTime;
211 const nsecs_t numPeriodsSinceReference = baseTime / mPeriod;
212 const nsecs_t predictedReference = mReferenceTime + numPeriodsSinceReference * mPeriod;
213 const nsecs_t phaseCorrection = mPhase + listener.mPhase;
Alec Mouri6db6d622019-03-19 18:42:03 -0700214 listener.mLastEventTime = predictedReference + phaseCorrection;
215 // If we're very close in time to the predicted last event time,
216 // then we need to back up the last event time so that we can
217 // attempt to fire an event immediately.
218 //
219 // Otherwise, keep the last event time that we predicted.
220 if (isShorterThanPeriod(now - listener.mLastEventTime)) {
221 listener.mLastEventTime -= mPeriod;
Alec Mouri81835982019-02-27 13:40:44 -0800222 }
223 } else {
224 listener.mLastEventTime = now + mPhase - mWakeupLatency;
225 }
Jamie Gennis629b9872013-10-29 13:36:12 -0700226
Alec Mouri7355eb22019-03-05 14:19:10 -0800227 if (lastCallbackTime <= 0) {
228 // If there is no prior callback time, try to infer one based on the
229 // logical last event time.
230 listener.mLastCallbackTime = listener.mLastEventTime + mWakeupLatency;
231 } else {
232 listener.mLastCallbackTime = lastCallbackTime;
233 }
234
Ana Krulec9a52b192018-07-12 18:18:47 -0400235 mEventListeners.push_back(listener);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700236
237 mCond.signal();
238
239 return NO_ERROR;
240 }
241
Alec Mouri7355eb22019-03-05 14:19:10 -0800242 status_t removeEventListener(DispSync::Callback* callback, nsecs_t* outLastCallback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700243 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700244 Mutex::Autolock lock(mMutex);
245
Ana Krulec9a52b192018-07-12 18:18:47 -0400246 for (std::vector<EventListener>::iterator it = mEventListeners.begin();
247 it != mEventListeners.end(); ++it) {
248 if (it->mCallback == callback) {
Alec Mouri7355eb22019-03-05 14:19:10 -0800249 *outLastCallback = it->mLastCallbackTime;
Ana Krulec9a52b192018-07-12 18:18:47 -0400250 mEventListeners.erase(it);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700251 mCond.signal();
252 return NO_ERROR;
253 }
254 }
255
256 return BAD_VALUE;
257 }
258
Dan Stoza84d619e2018-03-28 17:07:36 -0700259 status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700260 if (mTraceDetailedInfo) ATRACE_CALL();
Dan Stoza84d619e2018-03-28 17:07:36 -0700261 Mutex::Autolock lock(mMutex);
262
Ana Krulec9a52b192018-07-12 18:18:47 -0400263 for (auto& eventListener : mEventListeners) {
264 if (eventListener.mCallback == callback) {
265 const nsecs_t oldPhase = eventListener.mPhase;
266 eventListener.mPhase = phase;
Dan Stoza84d619e2018-03-28 17:07:36 -0700267
268 // Pretend that the last time this event was handled at the same frame but with the
269 // new offset to allow for a seamless offset change without double-firing or
270 // skipping.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200271 nsecs_t diff = oldPhase - phase;
272 if (diff > mPeriod / 2) {
273 diff -= mPeriod;
274 } else if (diff < -mPeriod / 2) {
275 diff += mPeriod;
276 }
Ana Krulec9a52b192018-07-12 18:18:47 -0400277 eventListener.mLastEventTime -= diff;
Dan Stoza84d619e2018-03-28 17:07:36 -0700278 mCond.signal();
279 return NO_ERROR;
280 }
281 }
282
283 return BAD_VALUE;
284 }
285
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700286private:
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700287 struct EventListener {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000288 const char* mName;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700289 nsecs_t mPhase;
290 nsecs_t mLastEventTime;
Alec Mouri7355eb22019-03-05 14:19:10 -0800291 nsecs_t mLastCallbackTime;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800292 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700293 };
294
295 struct CallbackInvocation {
Lloyd Piquee83f9312018-02-01 12:53:17 -0800296 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700297 nsecs_t mEventTime;
298 };
299
300 nsecs_t computeNextEventTimeLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700301 if (mTraceDetailedInfo) ATRACE_CALL();
Tim Murray4a4e4a22016-04-19 16:29:23 +0000302 ALOGV("[%s] computeNextEventTimeLocked", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700303 nsecs_t nextEventTime = INT64_MAX;
304 for (size_t i = 0; i < mEventListeners.size(); i++) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800305 nsecs_t t = computeListenerNextEventTimeLocked(mEventListeners[i], now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700306
307 if (t < nextEventTime) {
308 nextEventTime = t;
309 }
310 }
311
Tim Murray4a4e4a22016-04-19 16:29:23 +0000312 ALOGV("[%s] nextEventTime = %" PRId64, mName, ns2us(nextEventTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700313 return nextEventTime;
314 }
315
Alec Mouri7355eb22019-03-05 14:19:10 -0800316 // Sanity check that the duration is close enough in length to a period without
317 // falling into double-rate vsyncs.
Alec Mouri6db6d622019-03-19 18:42:03 -0700318 bool isShorterThanPeriod(nsecs_t duration) {
Alec Mouri7355eb22019-03-05 14:19:10 -0800319 // Ratio of 3/5 is arbitrary, but it must be greater than 1/2.
320 return duration < (3 * mPeriod) / 5;
321 }
322
Ana Krulec9a52b192018-07-12 18:18:47 -0400323 std::vector<CallbackInvocation> gatherCallbackInvocationsLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700324 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800325 ALOGV("[%s] gatherCallbackInvocationsLocked @ %" PRId64, mName, ns2us(now));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000326
Ana Krulec9a52b192018-07-12 18:18:47 -0400327 std::vector<CallbackInvocation> callbackInvocations;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000328 nsecs_t onePeriodAgo = now - mPeriod;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700329
Ana Krulec9a52b192018-07-12 18:18:47 -0400330 for (auto& eventListener : mEventListeners) {
331 nsecs_t t = computeListenerNextEventTimeLocked(eventListener, onePeriodAgo);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700332
Jamie Gennis0d5c60e2013-10-09 17:49:37 -0700333 if (t < now) {
Alec Mouri6db6d622019-03-19 18:42:03 -0700334 if (isShorterThanPeriod(now - eventListener.mLastCallbackTime)) {
Alec Mouri7355eb22019-03-05 14:19:10 -0800335 eventListener.mLastEventTime = t;
Alec Mouri7355eb22019-03-05 14:19:10 -0800336 ALOGV("[%s] [%s] Skipping event due to model error", mName,
337 eventListener.mName);
338 continue;
339 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700340 CallbackInvocation ci;
Ana Krulec9a52b192018-07-12 18:18:47 -0400341 ci.mCallback = eventListener.mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700342 ci.mEventTime = t;
Alec Mouri7355eb22019-03-05 14:19:10 -0800343 ALOGV("[%s] [%s] Preparing to fire, latency: %" PRId64, mName, eventListener.mName,
344 t - eventListener.mLastEventTime);
Ana Krulec9a52b192018-07-12 18:18:47 -0400345 callbackInvocations.push_back(ci);
346 eventListener.mLastEventTime = t;
Alec Mouri7355eb22019-03-05 14:19:10 -0800347 eventListener.mLastCallbackTime = now;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700348 }
349 }
350
351 return callbackInvocations;
352 }
353
Lloyd Pique78ce4182018-01-31 16:39:51 -0800354 nsecs_t computeListenerNextEventTimeLocked(const EventListener& listener, nsecs_t baseTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700355 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800356 ALOGV("[%s] [%s] computeListenerNextEventTimeLocked(%" PRId64 ")", mName, listener.mName,
357 ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700358
Tim Murray4a4e4a22016-04-19 16:29:23 +0000359 nsecs_t lastEventTime = listener.mLastEventTime + mWakeupLatency;
360 ALOGV("[%s] lastEventTime: %" PRId64, mName, ns2us(lastEventTime));
361 if (baseTime < lastEventTime) {
362 baseTime = lastEventTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800363 ALOGV("[%s] Clamping baseTime to lastEventTime -> %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700364 }
365
Tim Murray4a4e4a22016-04-19 16:29:23 +0000366 baseTime -= mReferenceTime;
367 ALOGV("[%s] Relative baseTime = %" PRId64, mName, ns2us(baseTime));
368 nsecs_t phase = mPhase + listener.mPhase;
369 ALOGV("[%s] Phase = %" PRId64, mName, ns2us(phase));
370 baseTime -= phase;
371 ALOGV("[%s] baseTime - phase = %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700372
Tim Murray4a4e4a22016-04-19 16:29:23 +0000373 // If our previous time is before the reference (because the reference
374 // has since been updated), the division by mPeriod will truncate
375 // towards zero instead of computing the floor. Since in all cases
376 // before the reference we want the next time to be effectively now, we
377 // set baseTime to -mPeriod so that numPeriods will be -1.
378 // When we add 1 and the phase, we will be at the correct event time for
379 // this period.
380 if (baseTime < 0) {
381 ALOGV("[%s] Correcting negative baseTime", mName);
382 baseTime = -mPeriod;
383 }
384
385 nsecs_t numPeriods = baseTime / mPeriod;
386 ALOGV("[%s] numPeriods = %" PRId64, mName, numPeriods);
387 nsecs_t t = (numPeriods + 1) * mPeriod + phase;
388 ALOGV("[%s] t = %" PRId64, mName, ns2us(t));
389 t += mReferenceTime;
390 ALOGV("[%s] Absolute t = %" PRId64, mName, ns2us(t));
391
392 // Check that it's been slightly more than half a period since the last
393 // event so that we don't accidentally fall into double-rate vsyncs
Alec Mouri6db6d622019-03-19 18:42:03 -0700394 if (isShorterThanPeriod(t - listener.mLastEventTime)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700395 t += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000396 ALOGV("[%s] Modifying t -> %" PRId64, mName, ns2us(t));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700397 }
398
Tim Murray4a4e4a22016-04-19 16:29:23 +0000399 t -= mWakeupLatency;
400 ALOGV("[%s] Corrected for wakeup latency -> %" PRId64, mName, ns2us(t));
401
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700402 return t;
403 }
404
Ana Krulec9a52b192018-07-12 18:18:47 -0400405 void fireCallbackInvocations(const std::vector<CallbackInvocation>& callbacks) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700406 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700407 for (size_t i = 0; i < callbacks.size(); i++) {
408 callbacks[i].mCallback->onDispSyncEvent(callbacks[i].mEventTime);
409 }
410 }
411
Tim Murray4a4e4a22016-04-19 16:29:23 +0000412 const char* const mName;
413
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700414 bool mStop;
415
416 nsecs_t mPeriod;
417 nsecs_t mPhase;
Haixia Shi676b1f62015-10-28 16:19:01 -0700418 nsecs_t mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700419 nsecs_t mWakeupLatency;
420
Tim Murray4a4e4a22016-04-19 16:29:23 +0000421 int64_t mFrameNumber;
422
Ana Krulec9a52b192018-07-12 18:18:47 -0400423 std::vector<EventListener> mEventListeners;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700424
425 Mutex mMutex;
426 Condition mCond;
Ana Krulec064a82f2018-09-11 16:03:03 -0700427
428 // Flag to turn on logging in systrace.
429 const bool mTraceDetailedInfo;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700430};
431
Tim Murray4a4e4a22016-04-19 16:29:23 +0000432#undef LOG_TAG
433#define LOG_TAG "DispSync"
434
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700435class ZeroPhaseTracer : public DispSync::Callback {
436public:
437 ZeroPhaseTracer() : mParity(false) {}
438
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700439 virtual void onDispSyncEvent(nsecs_t /*when*/) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700440 mParity = !mParity;
441 ATRACE_INT("ZERO_PHASE_VSYNC", mParity ? 1 : 0);
442 }
443
444private:
445 bool mParity;
446};
447
Ana Krulec064a82f2018-09-11 16:03:03 -0700448DispSync::DispSync(const char* name) : mName(name), mRefreshSkipCount(0) {
449 // This flag offers the ability to turn on systrace logging from the shell.
450 char value[PROPERTY_VALUE_MAX];
451 property_get("debug.sf.dispsync_trace_detailed_info", value, "0");
452 mTraceDetailedInfo = atoi(value);
453 mThread = new DispSyncThread(name, mTraceDetailedInfo);
454}
Andy McFadden645b1f72014-06-10 14:43:32 -0700455
Ady Abraham50c202a2019-03-14 11:44:38 -0700456DispSync::~DispSync() {
457 mThread->stop();
458 mThread->requestExitAndWait();
459}
Saurabh Shahf4174532017-07-13 10:45:07 -0700460
461void DispSync::init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset) {
462 mIgnorePresentFences = !hasSyncFramework;
463 mPresentTimeOffset = dispSyncPresentTimeOffset;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700464 mThread->run("DispSync", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
Saurabh Shahf4174532017-07-13 10:45:07 -0700465
Tim Murrayacff43d2016-07-29 13:57:24 -0700466 // set DispSync to SCHED_FIFO to minimize jitter
467 struct sched_param param = {0};
Tim Murray35520632016-09-07 12:18:17 -0700468 param.sched_priority = 2;
Tim Murrayacff43d2016-07-29 13:57:24 -0700469 if (sched_setscheduler(mThread->getTid(), SCHED_FIFO, &param) != 0) {
470 ALOGE("Couldn't set SCHED_FIFO for DispSyncThread");
471 }
472
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700473 reset();
474 beginResync();
475
Ana Krulec064a82f2018-09-11 16:03:03 -0700476 if (mTraceDetailedInfo && kEnableZeroPhaseTracer) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700477 mZeroPhaseTracer = std::make_unique<ZeroPhaseTracer>();
Alec Mouri7355eb22019-03-05 14:19:10 -0800478 addEventListener("ZeroPhaseTracer", 0, mZeroPhaseTracer.get(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700479 }
480}
481
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700482void DispSync::reset() {
483 Mutex::Autolock lock(mMutex);
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700484 resetLocked();
485}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700486
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700487void DispSync::resetLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700488 mPhase = 0;
Alec Mouri75b0b622019-03-12 18:56:00 +0000489 const size_t lastSampleIdx = (mFirstResyncSample + mNumResyncSamples - 1) % MAX_RESYNC_SAMPLES;
490 // Keep the most recent sample, when we resync to hardware we'll overwrite this
491 // with a more accurate signal
492 if (mResyncSamples[lastSampleIdx] != 0) {
493 mReferenceTime = mResyncSamples[lastSampleIdx];
494 }
Haixia Shi676b1f62015-10-28 16:19:01 -0700495 mModelUpdated = false;
Alec Mouri75b0b622019-03-12 18:56:00 +0000496 for (size_t i = 0; i < MAX_RESYNC_SAMPLES; i++) {
497 mResyncSamples[i] = 0;
498 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700499 mNumResyncSamples = 0;
500 mFirstResyncSample = 0;
501 mNumResyncSamplesSincePresent = 0;
502 resetErrorLocked();
503}
504
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700505bool DispSync::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700506 Mutex::Autolock lock(mMutex);
507
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700508 if (mIgnorePresentFences) {
509 return true;
510 }
511
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700512 mPresentFences[mPresentSampleOffset] = fenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700513 mPresentSampleOffset = (mPresentSampleOffset + 1) % NUM_PRESENT_SAMPLES;
514 mNumResyncSamplesSincePresent = 0;
515
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700516 updateErrorLocked();
517
Haixia Shi676b1f62015-10-28 16:19:01 -0700518 return !mModelUpdated || mError > kErrorThreshold;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700519}
520
521void DispSync::beginResync() {
522 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000523 ALOGV("[%s] beginResync", mName);
Haixia Shi676b1f62015-10-28 16:19:01 -0700524 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700525 mNumResyncSamples = 0;
526}
527
528bool DispSync::addResyncSample(nsecs_t timestamp) {
529 Mutex::Autolock lock(mMutex);
530
Tim Murray4a4e4a22016-04-19 16:29:23 +0000531 ALOGV("[%s] addResyncSample(%" PRId64 ")", mName, ns2us(timestamp));
532
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700533 size_t idx = (mFirstResyncSample + mNumResyncSamples) % MAX_RESYNC_SAMPLES;
534 mResyncSamples[idx] = timestamp;
Haixia Shi664339a2015-10-28 13:22:22 -0700535 if (mNumResyncSamples == 0) {
Haixia Shi676b1f62015-10-28 16:19:01 -0700536 mPhase = 0;
537 mReferenceTime = timestamp;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000538 ALOGV("[%s] First resync sample: mPeriod = %" PRId64 ", mPhase = 0, "
Lloyd Pique78ce4182018-01-31 16:39:51 -0800539 "mReferenceTime = %" PRId64,
540 mName, ns2us(mPeriod), ns2us(mReferenceTime));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000541 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Haixia Shi664339a2015-10-28 13:22:22 -0700542 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700543
544 if (mNumResyncSamples < MAX_RESYNC_SAMPLES) {
545 mNumResyncSamples++;
546 } else {
547 mFirstResyncSample = (mFirstResyncSample + 1) % MAX_RESYNC_SAMPLES;
548 }
549
550 updateModelLocked();
551
552 if (mNumResyncSamplesSincePresent++ > MAX_RESYNC_SAMPLES_WITHOUT_PRESENT) {
553 resetErrorLocked();
554 }
555
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800556 if (mIgnorePresentFences) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700557 // If we're ignoring the present fences we have no way to know whether
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700558 // or not we're synchronized with the HW vsyncs, so we just request
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700559 // that the HW vsync events be turned on.
560 return true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700561 }
562
Tim Murray4a4e4a22016-04-19 16:29:23 +0000563 // Check against kErrorThreshold / 2 to add some hysteresis before having to
564 // resync again
565 bool modelLocked = mModelUpdated && mError < (kErrorThreshold / 2);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800566 ALOGV("[%s] addResyncSample returning %s", mName, modelLocked ? "locked" : "unlocked");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000567 return !modelLocked;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700568}
569
Lloyd Pique78ce4182018-01-31 16:39:51 -0800570void DispSync::endResync() {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700571
Alec Mouri7355eb22019-03-05 14:19:10 -0800572status_t DispSync::addEventListener(const char* name, nsecs_t phase, Callback* callback,
573 nsecs_t lastCallbackTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700574 Mutex::Autolock lock(mMutex);
Alec Mouri7355eb22019-03-05 14:19:10 -0800575 return mThread->addEventListener(name, phase, callback, lastCallbackTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700576}
577
Andy McFadden645b1f72014-06-10 14:43:32 -0700578void DispSync::setRefreshSkipCount(int count) {
579 Mutex::Autolock lock(mMutex);
580 ALOGD("setRefreshSkipCount(%d)", count);
581 mRefreshSkipCount = count;
582 updateModelLocked();
Ruchi Kandoif52b3c82014-04-24 16:42:35 -0700583}
584
Alec Mouri7355eb22019-03-05 14:19:10 -0800585status_t DispSync::removeEventListener(Callback* callback, nsecs_t* outLastCallbackTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700586 Mutex::Autolock lock(mMutex);
Alec Mouri7355eb22019-03-05 14:19:10 -0800587 return mThread->removeEventListener(callback, outLastCallbackTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700588}
589
Dan Stoza84d619e2018-03-28 17:07:36 -0700590status_t DispSync::changePhaseOffset(Callback* callback, nsecs_t phase) {
591 Mutex::Autolock lock(mMutex);
592 return mThread->changePhaseOffset(callback, phase);
593}
594
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700595void DispSync::setPeriod(nsecs_t period) {
596 Mutex::Autolock lock(mMutex);
David Sodman1afa8b02018-10-15 11:20:48 -0700597 mPeriod = period;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700598 mPhase = 0;
Haixia Shi676b1f62015-10-28 16:19:01 -0700599 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700600}
601
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700602nsecs_t DispSync::getPeriod() {
603 // lock mutex as mPeriod changes multiple times in updateModelLocked
604 Mutex::Autolock lock(mMutex);
605 return mPeriod;
606}
607
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700608void DispSync::updateModelLocked() {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000609 ALOGV("[%s] updateModelLocked %zu", mName, mNumResyncSamples);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700610 if (mNumResyncSamples >= MIN_RESYNC_SAMPLES_FOR_UPDATE) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000611 ALOGV("[%s] Computing...", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700612 nsecs_t durationSum = 0;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000613 nsecs_t minDuration = INT64_MAX;
614 nsecs_t maxDuration = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700615 for (size_t i = 1; i < mNumResyncSamples; i++) {
616 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
617 size_t prev = (idx + MAX_RESYNC_SAMPLES - 1) % MAX_RESYNC_SAMPLES;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000618 nsecs_t duration = mResyncSamples[idx] - mResyncSamples[prev];
619 durationSum += duration;
620 minDuration = min(minDuration, duration);
621 maxDuration = max(maxDuration, duration);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700622 }
623
Tim Murray4a4e4a22016-04-19 16:29:23 +0000624 // Exclude the min and max from the average
625 durationSum -= minDuration + maxDuration;
David Sodman1afa8b02018-10-15 11:20:48 -0700626 mPeriod = durationSum / (mNumResyncSamples - 3);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000627
628 ALOGV("[%s] mPeriod = %" PRId64, mName, ns2us(mPeriod));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700629
630 double sampleAvgX = 0;
631 double sampleAvgY = 0;
632 double scale = 2.0 * M_PI / double(mPeriod);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000633 // Intentionally skip the first sample
634 for (size_t i = 1; i < mNumResyncSamples; i++) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700635 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
Haixia Shi676b1f62015-10-28 16:19:01 -0700636 nsecs_t sample = mResyncSamples[idx] - mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700637 double samplePhase = double(sample % mPeriod) * scale;
638 sampleAvgX += cos(samplePhase);
639 sampleAvgY += sin(samplePhase);
640 }
641
Tim Murray4a4e4a22016-04-19 16:29:23 +0000642 sampleAvgX /= double(mNumResyncSamples - 1);
643 sampleAvgY /= double(mNumResyncSamples - 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700644
645 mPhase = nsecs_t(atan2(sampleAvgY, sampleAvgX) / scale);
646
Tim Murray4a4e4a22016-04-19 16:29:23 +0000647 ALOGV("[%s] mPhase = %" PRId64, mName, ns2us(mPhase));
648
649 if (mPhase < -(mPeriod / 2)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700650 mPhase += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000651 ALOGV("[%s] Adjusting mPhase -> %" PRId64, mName, ns2us(mPhase));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700652 }
653
Andy McFadden645b1f72014-06-10 14:43:32 -0700654 // Artificially inflate the period if requested.
655 mPeriod += mPeriod * mRefreshSkipCount;
656
Haixia Shi676b1f62015-10-28 16:19:01 -0700657 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
658 mModelUpdated = true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700659 }
660}
661
662void DispSync::updateErrorLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700663 if (!mModelUpdated) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700664 return;
665 }
666
Andy McFadden645b1f72014-06-10 14:43:32 -0700667 // Need to compare present fences against the un-adjusted refresh period,
668 // since they might arrive between two events.
669 nsecs_t period = mPeriod / (1 + mRefreshSkipCount);
670
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700671 int numErrSamples = 0;
672 nsecs_t sqErrSum = 0;
673
674 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700675 // Only check for the cached value of signal time to avoid unecessary
676 // syscalls. It is the responsibility of the DispSync owner to
677 // call getSignalTime() periodically so the cache is updated when the
678 // fence signals.
679 nsecs_t time = mPresentFences[i]->getCachedSignalTime();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800680 if (time == Fence::SIGNAL_TIME_PENDING || time == Fence::SIGNAL_TIME_INVALID) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700681 continue;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700682 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700683
684 nsecs_t sample = time - mReferenceTime;
685 if (sample <= mPhase) {
686 continue;
687 }
688
689 nsecs_t sampleErr = (sample - mPhase) % period;
690 if (sampleErr > period / 2) {
691 sampleErr -= period;
692 }
693 sqErrSum += sampleErr * sampleErr;
694 numErrSamples++;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700695 }
696
697 if (numErrSamples > 0) {
698 mError = sqErrSum / numErrSamples;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700699 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700700 } else {
701 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700702 // Use mod ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT to avoid log spam.
703 mZeroErrSamplesCount++;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800704 ALOGE_IF((mZeroErrSamplesCount % ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT) == 0,
705 "No present times for model error.");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700706 }
707
Ana Krulec064a82f2018-09-11 16:03:03 -0700708 if (mTraceDetailedInfo) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700709 ATRACE_INT64("DispSync:Error", mError);
710 }
711}
712
713void DispSync::resetErrorLocked() {
714 mPresentSampleOffset = 0;
715 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700716 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700717 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700718 mPresentFences[i] = FenceTime::NO_FENCE;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700719 }
720}
721
Andy McFadden41d67d72014-04-25 16:58:34 -0700722nsecs_t DispSync::computeNextRefresh(int periodOffset) const {
Andy McFadden150ecd82014-05-08 14:56:50 -0700723 Mutex::Autolock lock(mMutex);
Andy McFadden41d67d72014-04-25 16:58:34 -0700724 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Haixia Shi676b1f62015-10-28 16:19:01 -0700725 nsecs_t phase = mReferenceTime + mPhase;
Marissa Wall17b4e452018-12-26 16:32:34 -0800726 if (mPeriod == 0) {
727 return 0;
728 }
Haixia Shi676b1f62015-10-28 16:19:01 -0700729 return (((now - phase) / mPeriod) + periodOffset + 1) * mPeriod + phase;
Andy McFadden41d67d72014-04-25 16:58:34 -0700730}
731
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700732void DispSync::setIgnorePresentFences(bool ignore) {
733 Mutex::Autolock lock(mMutex);
734 if (mIgnorePresentFences != ignore) {
735 mIgnorePresentFences = ignore;
736 resetLocked();
737 }
738}
739
Yiwei Zhang5434a782018-12-05 18:06:32 -0800740void DispSync::dump(std::string& result) const {
Andy McFaddenc751e922014-05-08 14:53:26 -0700741 Mutex::Autolock lock(mMutex);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800742 StringAppendF(&result, "present fences are %s\n", mIgnorePresentFences ? "ignored" : "used");
743 StringAppendF(&result, "mPeriod: %" PRId64 " ns (%.3f fps; skipCount=%d)\n", mPeriod,
744 1000000000.0 / mPeriod, mRefreshSkipCount);
745 StringAppendF(&result, "mPhase: %" PRId64 " ns\n", mPhase);
746 StringAppendF(&result, "mError: %" PRId64 " ns (sqrt=%.1f)\n", mError, sqrt(mError));
747 StringAppendF(&result, "mNumResyncSamplesSincePresent: %d (limit %d)\n",
748 mNumResyncSamplesSincePresent, MAX_RESYNC_SAMPLES_WITHOUT_PRESENT);
749 StringAppendF(&result, "mNumResyncSamples: %zd (max %d)\n", mNumResyncSamples,
750 MAX_RESYNC_SAMPLES);
Andy McFaddenc751e922014-05-08 14:53:26 -0700751
Yiwei Zhang5434a782018-12-05 18:06:32 -0800752 result.append("mResyncSamples:\n");
Andy McFaddenc751e922014-05-08 14:53:26 -0700753 nsecs_t previous = -1;
754 for (size_t i = 0; i < mNumResyncSamples; i++) {
755 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
756 nsecs_t sampleTime = mResyncSamples[idx];
757 if (i == 0) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800758 StringAppendF(&result, " %" PRId64 "\n", sampleTime);
Andy McFaddenc751e922014-05-08 14:53:26 -0700759 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800760 StringAppendF(&result, " %" PRId64 " (+%" PRId64 ")\n", sampleTime,
761 sampleTime - previous);
Andy McFaddenc751e922014-05-08 14:53:26 -0700762 }
763 previous = sampleTime;
764 }
765
Yiwei Zhang5434a782018-12-05 18:06:32 -0800766 StringAppendF(&result, "mPresentFences [%d]:\n", NUM_PRESENT_SAMPLES);
Andy McFadden5167ec62014-05-22 13:08:43 -0700767 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700768 previous = Fence::SIGNAL_TIME_INVALID;
Andy McFaddenc751e922014-05-08 14:53:26 -0700769 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
770 size_t idx = (i + mPresentSampleOffset) % NUM_PRESENT_SAMPLES;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700771 nsecs_t presentTime = mPresentFences[idx]->getSignalTime();
772 if (presentTime == Fence::SIGNAL_TIME_PENDING) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800773 StringAppendF(&result, " [unsignaled fence]\n");
Lloyd Pique78ce4182018-01-31 16:39:51 -0800774 } else if (presentTime == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800775 StringAppendF(&result, " [invalid fence]\n");
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700776 } else if (previous == Fence::SIGNAL_TIME_PENDING ||
Lloyd Pique78ce4182018-01-31 16:39:51 -0800777 previous == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800778 StringAppendF(&result, " %" PRId64 " (%.3f ms ago)\n", presentTime,
779 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700780 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800781 StringAppendF(&result, " %" PRId64 " (+%" PRId64 " / %.3f) (%.3f ms ago)\n",
782 presentTime, presentTime - previous,
783 (presentTime - previous) / (double)mPeriod,
784 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700785 }
786 previous = presentTime;
787 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700788
Yiwei Zhang5434a782018-12-05 18:06:32 -0800789 StringAppendF(&result, "current monotonic time: %" PRId64 "\n", now);
Andy McFaddenc751e922014-05-08 14:53:26 -0700790}
791
Ana Krulec010d2192018-10-08 06:29:54 -0700792nsecs_t DispSync::expectedPresentTime() {
793 // The HWC doesn't currently have a way to report additional latency.
794 // Assume that whatever we submit now will appear right after the flip.
795 // For a smart panel this might be 1. This is expressed in frames,
796 // rather than time, because we expect to have a constant frame delay
797 // regardless of the refresh rate.
798 const uint32_t hwcLatency = 0;
799
800 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
Ana Krulec757f63a2019-01-25 10:46:18 -0800801 return computeNextRefresh(hwcLatency);
Ana Krulec010d2192018-10-08 06:29:54 -0700802}
803
Lloyd Pique41be5d22018-06-21 13:11:48 -0700804} // namespace impl
805
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700806} // namespace android