blob: 5b08aab69a20113f5810a0eeed70c49c0aaeda69 [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
Lloyd Piquee83f9312018-02-01 12:53:17 -0800188 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700189 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700190 Mutex::Autolock lock(mMutex);
191
192 for (size_t i = 0; i < mEventListeners.size(); i++) {
193 if (mEventListeners[i].mCallback == callback) {
194 return BAD_VALUE;
195 }
196 }
197
198 EventListener listener;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000199 listener.mName = name;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700200 listener.mPhase = phase;
201 listener.mCallback = callback;
Jamie Gennis629b9872013-10-29 13:36:12 -0700202
203 // We want to allow the firstmost future event to fire without
Alec Mouri81835982019-02-27 13:40:44 -0800204 // allowing any past events to fire. To do this extrapolate from
205 // mReferenceTime the most recent hardware vsync, and pin the
206 // last event time there.
207 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
208 if (mPeriod != 0) {
209 const nsecs_t baseTime = now - mReferenceTime;
210 const nsecs_t numPeriodsSinceReference = baseTime / mPeriod;
211 const nsecs_t predictedReference = mReferenceTime + numPeriodsSinceReference * mPeriod;
212 const nsecs_t phaseCorrection = mPhase + listener.mPhase;
213 const nsecs_t predictedLastEventTime = predictedReference + phaseCorrection;
214 if (predictedLastEventTime > now) {
215 // Make sure that the last event time does not exceed the current time.
216 // If it would, then back the last event time by a period.
217 listener.mLastEventTime = predictedLastEventTime - mPeriod;
218 } else {
219 listener.mLastEventTime = predictedLastEventTime;
220 }
221 } else {
222 listener.mLastEventTime = now + mPhase - mWakeupLatency;
223 }
Jamie Gennis629b9872013-10-29 13:36:12 -0700224
Ana Krulec9a52b192018-07-12 18:18:47 -0400225 mEventListeners.push_back(listener);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700226
227 mCond.signal();
228
229 return NO_ERROR;
230 }
231
Lloyd Piquee83f9312018-02-01 12:53:17 -0800232 status_t removeEventListener(DispSync::Callback* callback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700233 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700234 Mutex::Autolock lock(mMutex);
235
Ana Krulec9a52b192018-07-12 18:18:47 -0400236 for (std::vector<EventListener>::iterator it = mEventListeners.begin();
237 it != mEventListeners.end(); ++it) {
238 if (it->mCallback == callback) {
239 mEventListeners.erase(it);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700240 mCond.signal();
241 return NO_ERROR;
242 }
243 }
244
245 return BAD_VALUE;
246 }
247
Dan Stoza84d619e2018-03-28 17:07:36 -0700248 status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700249 if (mTraceDetailedInfo) ATRACE_CALL();
Dan Stoza84d619e2018-03-28 17:07:36 -0700250 Mutex::Autolock lock(mMutex);
251
Ana Krulec9a52b192018-07-12 18:18:47 -0400252 for (auto& eventListener : mEventListeners) {
253 if (eventListener.mCallback == callback) {
254 const nsecs_t oldPhase = eventListener.mPhase;
255 eventListener.mPhase = phase;
Dan Stoza84d619e2018-03-28 17:07:36 -0700256
257 // Pretend that the last time this event was handled at the same frame but with the
258 // new offset to allow for a seamless offset change without double-firing or
259 // skipping.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200260 nsecs_t diff = oldPhase - phase;
261 if (diff > mPeriod / 2) {
262 diff -= mPeriod;
263 } else if (diff < -mPeriod / 2) {
264 diff += mPeriod;
265 }
Ana Krulec9a52b192018-07-12 18:18:47 -0400266 eventListener.mLastEventTime -= diff;
Dan Stoza84d619e2018-03-28 17:07:36 -0700267 mCond.signal();
268 return NO_ERROR;
269 }
270 }
271
272 return BAD_VALUE;
273 }
274
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700275private:
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700276 struct EventListener {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000277 const char* mName;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700278 nsecs_t mPhase;
279 nsecs_t mLastEventTime;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800280 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700281 };
282
283 struct CallbackInvocation {
Lloyd Piquee83f9312018-02-01 12:53:17 -0800284 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700285 nsecs_t mEventTime;
286 };
287
288 nsecs_t computeNextEventTimeLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700289 if (mTraceDetailedInfo) ATRACE_CALL();
Tim Murray4a4e4a22016-04-19 16:29:23 +0000290 ALOGV("[%s] computeNextEventTimeLocked", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700291 nsecs_t nextEventTime = INT64_MAX;
292 for (size_t i = 0; i < mEventListeners.size(); i++) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800293 nsecs_t t = computeListenerNextEventTimeLocked(mEventListeners[i], now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700294
295 if (t < nextEventTime) {
296 nextEventTime = t;
297 }
298 }
299
Tim Murray4a4e4a22016-04-19 16:29:23 +0000300 ALOGV("[%s] nextEventTime = %" PRId64, mName, ns2us(nextEventTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700301 return nextEventTime;
302 }
303
Ana Krulec9a52b192018-07-12 18:18:47 -0400304 std::vector<CallbackInvocation> gatherCallbackInvocationsLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700305 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800306 ALOGV("[%s] gatherCallbackInvocationsLocked @ %" PRId64, mName, ns2us(now));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000307
Ana Krulec9a52b192018-07-12 18:18:47 -0400308 std::vector<CallbackInvocation> callbackInvocations;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000309 nsecs_t onePeriodAgo = now - mPeriod;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700310
Ana Krulec9a52b192018-07-12 18:18:47 -0400311 for (auto& eventListener : mEventListeners) {
312 nsecs_t t = computeListenerNextEventTimeLocked(eventListener, onePeriodAgo);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700313
Jamie Gennis0d5c60e2013-10-09 17:49:37 -0700314 if (t < now) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700315 CallbackInvocation ci;
Ana Krulec9a52b192018-07-12 18:18:47 -0400316 ci.mCallback = eventListener.mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700317 ci.mEventTime = t;
Ana Krulec9a52b192018-07-12 18:18:47 -0400318 ALOGV("[%s] [%s] Preparing to fire", mName, eventListener.mName);
319 callbackInvocations.push_back(ci);
320 eventListener.mLastEventTime = t;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700321 }
322 }
323
324 return callbackInvocations;
325 }
326
Lloyd Pique78ce4182018-01-31 16:39:51 -0800327 nsecs_t computeListenerNextEventTimeLocked(const EventListener& listener, nsecs_t baseTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700328 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800329 ALOGV("[%s] [%s] computeListenerNextEventTimeLocked(%" PRId64 ")", mName, listener.mName,
330 ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700331
Tim Murray4a4e4a22016-04-19 16:29:23 +0000332 nsecs_t lastEventTime = listener.mLastEventTime + mWakeupLatency;
333 ALOGV("[%s] lastEventTime: %" PRId64, mName, ns2us(lastEventTime));
334 if (baseTime < lastEventTime) {
335 baseTime = lastEventTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800336 ALOGV("[%s] Clamping baseTime to lastEventTime -> %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700337 }
338
Tim Murray4a4e4a22016-04-19 16:29:23 +0000339 baseTime -= mReferenceTime;
340 ALOGV("[%s] Relative baseTime = %" PRId64, mName, ns2us(baseTime));
341 nsecs_t phase = mPhase + listener.mPhase;
342 ALOGV("[%s] Phase = %" PRId64, mName, ns2us(phase));
343 baseTime -= phase;
344 ALOGV("[%s] baseTime - phase = %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700345
Tim Murray4a4e4a22016-04-19 16:29:23 +0000346 // If our previous time is before the reference (because the reference
347 // has since been updated), the division by mPeriod will truncate
348 // towards zero instead of computing the floor. Since in all cases
349 // before the reference we want the next time to be effectively now, we
350 // set baseTime to -mPeriod so that numPeriods will be -1.
351 // When we add 1 and the phase, we will be at the correct event time for
352 // this period.
353 if (baseTime < 0) {
354 ALOGV("[%s] Correcting negative baseTime", mName);
355 baseTime = -mPeriod;
356 }
357
358 nsecs_t numPeriods = baseTime / mPeriod;
359 ALOGV("[%s] numPeriods = %" PRId64, mName, numPeriods);
360 nsecs_t t = (numPeriods + 1) * mPeriod + phase;
361 ALOGV("[%s] t = %" PRId64, mName, ns2us(t));
362 t += mReferenceTime;
363 ALOGV("[%s] Absolute t = %" PRId64, mName, ns2us(t));
364
365 // Check that it's been slightly more than half a period since the last
366 // event so that we don't accidentally fall into double-rate vsyncs
367 if (t - listener.mLastEventTime < (3 * mPeriod / 5)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700368 t += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000369 ALOGV("[%s] Modifying t -> %" PRId64, mName, ns2us(t));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700370 }
371
Tim Murray4a4e4a22016-04-19 16:29:23 +0000372 t -= mWakeupLatency;
373 ALOGV("[%s] Corrected for wakeup latency -> %" PRId64, mName, ns2us(t));
374
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700375 return t;
376 }
377
Ana Krulec9a52b192018-07-12 18:18:47 -0400378 void fireCallbackInvocations(const std::vector<CallbackInvocation>& callbacks) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700379 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700380 for (size_t i = 0; i < callbacks.size(); i++) {
381 callbacks[i].mCallback->onDispSyncEvent(callbacks[i].mEventTime);
382 }
383 }
384
Tim Murray4a4e4a22016-04-19 16:29:23 +0000385 const char* const mName;
386
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700387 bool mStop;
388
389 nsecs_t mPeriod;
390 nsecs_t mPhase;
Haixia Shi676b1f62015-10-28 16:19:01 -0700391 nsecs_t mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700392 nsecs_t mWakeupLatency;
393
Tim Murray4a4e4a22016-04-19 16:29:23 +0000394 int64_t mFrameNumber;
395
Ana Krulec9a52b192018-07-12 18:18:47 -0400396 std::vector<EventListener> mEventListeners;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700397
398 Mutex mMutex;
399 Condition mCond;
Ana Krulec064a82f2018-09-11 16:03:03 -0700400
401 // Flag to turn on logging in systrace.
402 const bool mTraceDetailedInfo;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700403};
404
Tim Murray4a4e4a22016-04-19 16:29:23 +0000405#undef LOG_TAG
406#define LOG_TAG "DispSync"
407
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700408class ZeroPhaseTracer : public DispSync::Callback {
409public:
410 ZeroPhaseTracer() : mParity(false) {}
411
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700412 virtual void onDispSyncEvent(nsecs_t /*when*/) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700413 mParity = !mParity;
414 ATRACE_INT("ZERO_PHASE_VSYNC", mParity ? 1 : 0);
415 }
416
417private:
418 bool mParity;
419};
420
Ana Krulec064a82f2018-09-11 16:03:03 -0700421DispSync::DispSync(const char* name) : mName(name), mRefreshSkipCount(0) {
422 // This flag offers the ability to turn on systrace logging from the shell.
423 char value[PROPERTY_VALUE_MAX];
424 property_get("debug.sf.dispsync_trace_detailed_info", value, "0");
425 mTraceDetailedInfo = atoi(value);
426 mThread = new DispSyncThread(name, mTraceDetailedInfo);
427}
Andy McFadden645b1f72014-06-10 14:43:32 -0700428
Saurabh Shahf4174532017-07-13 10:45:07 -0700429DispSync::~DispSync() {}
430
431void DispSync::init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset) {
432 mIgnorePresentFences = !hasSyncFramework;
433 mPresentTimeOffset = dispSyncPresentTimeOffset;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700434 mThread->run("DispSync", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
Saurabh Shahf4174532017-07-13 10:45:07 -0700435
Tim Murrayacff43d2016-07-29 13:57:24 -0700436 // set DispSync to SCHED_FIFO to minimize jitter
437 struct sched_param param = {0};
Tim Murray35520632016-09-07 12:18:17 -0700438 param.sched_priority = 2;
Tim Murrayacff43d2016-07-29 13:57:24 -0700439 if (sched_setscheduler(mThread->getTid(), SCHED_FIFO, &param) != 0) {
440 ALOGE("Couldn't set SCHED_FIFO for DispSyncThread");
441 }
442
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700443 reset();
444 beginResync();
445
Ana Krulec064a82f2018-09-11 16:03:03 -0700446 if (mTraceDetailedInfo && kEnableZeroPhaseTracer) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700447 mZeroPhaseTracer = std::make_unique<ZeroPhaseTracer>();
448 addEventListener("ZeroPhaseTracer", 0, mZeroPhaseTracer.get());
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700449 }
450}
451
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700452void DispSync::reset() {
453 Mutex::Autolock lock(mMutex);
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700454 resetLocked();
455}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700456
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700457void DispSync::resetLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700458 mPhase = 0;
Alec Mouri75b0b622019-03-12 18:56:00 +0000459 const size_t lastSampleIdx = (mFirstResyncSample + mNumResyncSamples - 1) % MAX_RESYNC_SAMPLES;
460 // Keep the most recent sample, when we resync to hardware we'll overwrite this
461 // with a more accurate signal
462 if (mResyncSamples[lastSampleIdx] != 0) {
463 mReferenceTime = mResyncSamples[lastSampleIdx];
464 }
Haixia Shi676b1f62015-10-28 16:19:01 -0700465 mModelUpdated = false;
Alec Mouri75b0b622019-03-12 18:56:00 +0000466 for (size_t i = 0; i < MAX_RESYNC_SAMPLES; i++) {
467 mResyncSamples[i] = 0;
468 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700469 mNumResyncSamples = 0;
470 mFirstResyncSample = 0;
471 mNumResyncSamplesSincePresent = 0;
472 resetErrorLocked();
473}
474
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700475bool DispSync::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700476 Mutex::Autolock lock(mMutex);
477
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700478 if (mIgnorePresentFences) {
479 return true;
480 }
481
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700482 mPresentFences[mPresentSampleOffset] = fenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700483 mPresentSampleOffset = (mPresentSampleOffset + 1) % NUM_PRESENT_SAMPLES;
484 mNumResyncSamplesSincePresent = 0;
485
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700486 updateErrorLocked();
487
Haixia Shi676b1f62015-10-28 16:19:01 -0700488 return !mModelUpdated || mError > kErrorThreshold;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700489}
490
491void DispSync::beginResync() {
492 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000493 ALOGV("[%s] beginResync", mName);
Haixia Shi676b1f62015-10-28 16:19:01 -0700494 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700495 mNumResyncSamples = 0;
496}
497
498bool DispSync::addResyncSample(nsecs_t timestamp) {
499 Mutex::Autolock lock(mMutex);
500
Tim Murray4a4e4a22016-04-19 16:29:23 +0000501 ALOGV("[%s] addResyncSample(%" PRId64 ")", mName, ns2us(timestamp));
502
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700503 size_t idx = (mFirstResyncSample + mNumResyncSamples) % MAX_RESYNC_SAMPLES;
504 mResyncSamples[idx] = timestamp;
Haixia Shi664339a2015-10-28 13:22:22 -0700505 if (mNumResyncSamples == 0) {
Haixia Shi676b1f62015-10-28 16:19:01 -0700506 mPhase = 0;
507 mReferenceTime = timestamp;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000508 ALOGV("[%s] First resync sample: mPeriod = %" PRId64 ", mPhase = 0, "
Lloyd Pique78ce4182018-01-31 16:39:51 -0800509 "mReferenceTime = %" PRId64,
510 mName, ns2us(mPeriod), ns2us(mReferenceTime));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000511 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Haixia Shi664339a2015-10-28 13:22:22 -0700512 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700513
514 if (mNumResyncSamples < MAX_RESYNC_SAMPLES) {
515 mNumResyncSamples++;
516 } else {
517 mFirstResyncSample = (mFirstResyncSample + 1) % MAX_RESYNC_SAMPLES;
518 }
519
520 updateModelLocked();
521
522 if (mNumResyncSamplesSincePresent++ > MAX_RESYNC_SAMPLES_WITHOUT_PRESENT) {
523 resetErrorLocked();
524 }
525
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800526 if (mIgnorePresentFences) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700527 // If we're ignoring the present fences we have no way to know whether
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700528 // or not we're synchronized with the HW vsyncs, so we just request
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700529 // that the HW vsync events be turned on.
530 return true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700531 }
532
Tim Murray4a4e4a22016-04-19 16:29:23 +0000533 // Check against kErrorThreshold / 2 to add some hysteresis before having to
534 // resync again
535 bool modelLocked = mModelUpdated && mError < (kErrorThreshold / 2);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800536 ALOGV("[%s] addResyncSample returning %s", mName, modelLocked ? "locked" : "unlocked");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000537 return !modelLocked;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700538}
539
Lloyd Pique78ce4182018-01-31 16:39:51 -0800540void DispSync::endResync() {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700541
Lloyd Piquee83f9312018-02-01 12:53:17 -0800542status_t DispSync::addEventListener(const char* name, nsecs_t phase, Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700543 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000544 return mThread->addEventListener(name, phase, callback);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700545}
546
Andy McFadden645b1f72014-06-10 14:43:32 -0700547void DispSync::setRefreshSkipCount(int count) {
548 Mutex::Autolock lock(mMutex);
549 ALOGD("setRefreshSkipCount(%d)", count);
550 mRefreshSkipCount = count;
551 updateModelLocked();
Ruchi Kandoif52b3c82014-04-24 16:42:35 -0700552}
553
Lloyd Piquee83f9312018-02-01 12:53:17 -0800554status_t DispSync::removeEventListener(Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700555 Mutex::Autolock lock(mMutex);
556 return mThread->removeEventListener(callback);
557}
558
Dan Stoza84d619e2018-03-28 17:07:36 -0700559status_t DispSync::changePhaseOffset(Callback* callback, nsecs_t phase) {
560 Mutex::Autolock lock(mMutex);
561 return mThread->changePhaseOffset(callback, phase);
562}
563
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700564void DispSync::setPeriod(nsecs_t period) {
565 Mutex::Autolock lock(mMutex);
David Sodman1afa8b02018-10-15 11:20:48 -0700566 mPeriod = period;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700567 mPhase = 0;
Haixia Shi676b1f62015-10-28 16:19:01 -0700568 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700569}
570
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700571nsecs_t DispSync::getPeriod() {
572 // lock mutex as mPeriod changes multiple times in updateModelLocked
573 Mutex::Autolock lock(mMutex);
574 return mPeriod;
575}
576
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700577void DispSync::updateModelLocked() {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000578 ALOGV("[%s] updateModelLocked %zu", mName, mNumResyncSamples);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700579 if (mNumResyncSamples >= MIN_RESYNC_SAMPLES_FOR_UPDATE) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000580 ALOGV("[%s] Computing...", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700581 nsecs_t durationSum = 0;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000582 nsecs_t minDuration = INT64_MAX;
583 nsecs_t maxDuration = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700584 for (size_t i = 1; i < mNumResyncSamples; i++) {
585 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
586 size_t prev = (idx + MAX_RESYNC_SAMPLES - 1) % MAX_RESYNC_SAMPLES;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000587 nsecs_t duration = mResyncSamples[idx] - mResyncSamples[prev];
588 durationSum += duration;
589 minDuration = min(minDuration, duration);
590 maxDuration = max(maxDuration, duration);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700591 }
592
Tim Murray4a4e4a22016-04-19 16:29:23 +0000593 // Exclude the min and max from the average
594 durationSum -= minDuration + maxDuration;
David Sodman1afa8b02018-10-15 11:20:48 -0700595 mPeriod = durationSum / (mNumResyncSamples - 3);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000596
597 ALOGV("[%s] mPeriod = %" PRId64, mName, ns2us(mPeriod));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700598
599 double sampleAvgX = 0;
600 double sampleAvgY = 0;
601 double scale = 2.0 * M_PI / double(mPeriod);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000602 // Intentionally skip the first sample
603 for (size_t i = 1; i < mNumResyncSamples; i++) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700604 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
Haixia Shi676b1f62015-10-28 16:19:01 -0700605 nsecs_t sample = mResyncSamples[idx] - mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700606 double samplePhase = double(sample % mPeriod) * scale;
607 sampleAvgX += cos(samplePhase);
608 sampleAvgY += sin(samplePhase);
609 }
610
Tim Murray4a4e4a22016-04-19 16:29:23 +0000611 sampleAvgX /= double(mNumResyncSamples - 1);
612 sampleAvgY /= double(mNumResyncSamples - 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700613
614 mPhase = nsecs_t(atan2(sampleAvgY, sampleAvgX) / scale);
615
Tim Murray4a4e4a22016-04-19 16:29:23 +0000616 ALOGV("[%s] mPhase = %" PRId64, mName, ns2us(mPhase));
617
618 if (mPhase < -(mPeriod / 2)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700619 mPhase += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000620 ALOGV("[%s] Adjusting mPhase -> %" PRId64, mName, ns2us(mPhase));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700621 }
622
Andy McFadden645b1f72014-06-10 14:43:32 -0700623 // Artificially inflate the period if requested.
624 mPeriod += mPeriod * mRefreshSkipCount;
625
Haixia Shi676b1f62015-10-28 16:19:01 -0700626 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
627 mModelUpdated = true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700628 }
629}
630
631void DispSync::updateErrorLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700632 if (!mModelUpdated) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700633 return;
634 }
635
Andy McFadden645b1f72014-06-10 14:43:32 -0700636 // Need to compare present fences against the un-adjusted refresh period,
637 // since they might arrive between two events.
638 nsecs_t period = mPeriod / (1 + mRefreshSkipCount);
639
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700640 int numErrSamples = 0;
641 nsecs_t sqErrSum = 0;
642
643 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700644 // Only check for the cached value of signal time to avoid unecessary
645 // syscalls. It is the responsibility of the DispSync owner to
646 // call getSignalTime() periodically so the cache is updated when the
647 // fence signals.
648 nsecs_t time = mPresentFences[i]->getCachedSignalTime();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800649 if (time == Fence::SIGNAL_TIME_PENDING || time == Fence::SIGNAL_TIME_INVALID) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700650 continue;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700651 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700652
653 nsecs_t sample = time - mReferenceTime;
654 if (sample <= mPhase) {
655 continue;
656 }
657
658 nsecs_t sampleErr = (sample - mPhase) % period;
659 if (sampleErr > period / 2) {
660 sampleErr -= period;
661 }
662 sqErrSum += sampleErr * sampleErr;
663 numErrSamples++;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700664 }
665
666 if (numErrSamples > 0) {
667 mError = sqErrSum / numErrSamples;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700668 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700669 } else {
670 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700671 // Use mod ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT to avoid log spam.
672 mZeroErrSamplesCount++;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800673 ALOGE_IF((mZeroErrSamplesCount % ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT) == 0,
674 "No present times for model error.");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700675 }
676
Ana Krulec064a82f2018-09-11 16:03:03 -0700677 if (mTraceDetailedInfo) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700678 ATRACE_INT64("DispSync:Error", mError);
679 }
680}
681
682void DispSync::resetErrorLocked() {
683 mPresentSampleOffset = 0;
684 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700685 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700686 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700687 mPresentFences[i] = FenceTime::NO_FENCE;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700688 }
689}
690
Andy McFadden41d67d72014-04-25 16:58:34 -0700691nsecs_t DispSync::computeNextRefresh(int periodOffset) const {
Andy McFadden150ecd82014-05-08 14:56:50 -0700692 Mutex::Autolock lock(mMutex);
Andy McFadden41d67d72014-04-25 16:58:34 -0700693 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Haixia Shi676b1f62015-10-28 16:19:01 -0700694 nsecs_t phase = mReferenceTime + mPhase;
Marissa Wall17b4e452018-12-26 16:32:34 -0800695 if (mPeriod == 0) {
696 return 0;
697 }
Haixia Shi676b1f62015-10-28 16:19:01 -0700698 return (((now - phase) / mPeriod) + periodOffset + 1) * mPeriod + phase;
Andy McFadden41d67d72014-04-25 16:58:34 -0700699}
700
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700701void DispSync::setIgnorePresentFences(bool ignore) {
702 Mutex::Autolock lock(mMutex);
703 if (mIgnorePresentFences != ignore) {
704 mIgnorePresentFences = ignore;
705 resetLocked();
706 }
707}
708
Yiwei Zhang5434a782018-12-05 18:06:32 -0800709void DispSync::dump(std::string& result) const {
Andy McFaddenc751e922014-05-08 14:53:26 -0700710 Mutex::Autolock lock(mMutex);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800711 StringAppendF(&result, "present fences are %s\n", mIgnorePresentFences ? "ignored" : "used");
712 StringAppendF(&result, "mPeriod: %" PRId64 " ns (%.3f fps; skipCount=%d)\n", mPeriod,
713 1000000000.0 / mPeriod, mRefreshSkipCount);
714 StringAppendF(&result, "mPhase: %" PRId64 " ns\n", mPhase);
715 StringAppendF(&result, "mError: %" PRId64 " ns (sqrt=%.1f)\n", mError, sqrt(mError));
716 StringAppendF(&result, "mNumResyncSamplesSincePresent: %d (limit %d)\n",
717 mNumResyncSamplesSincePresent, MAX_RESYNC_SAMPLES_WITHOUT_PRESENT);
718 StringAppendF(&result, "mNumResyncSamples: %zd (max %d)\n", mNumResyncSamples,
719 MAX_RESYNC_SAMPLES);
Andy McFaddenc751e922014-05-08 14:53:26 -0700720
Yiwei Zhang5434a782018-12-05 18:06:32 -0800721 result.append("mResyncSamples:\n");
Andy McFaddenc751e922014-05-08 14:53:26 -0700722 nsecs_t previous = -1;
723 for (size_t i = 0; i < mNumResyncSamples; i++) {
724 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
725 nsecs_t sampleTime = mResyncSamples[idx];
726 if (i == 0) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800727 StringAppendF(&result, " %" PRId64 "\n", sampleTime);
Andy McFaddenc751e922014-05-08 14:53:26 -0700728 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800729 StringAppendF(&result, " %" PRId64 " (+%" PRId64 ")\n", sampleTime,
730 sampleTime - previous);
Andy McFaddenc751e922014-05-08 14:53:26 -0700731 }
732 previous = sampleTime;
733 }
734
Yiwei Zhang5434a782018-12-05 18:06:32 -0800735 StringAppendF(&result, "mPresentFences [%d]:\n", NUM_PRESENT_SAMPLES);
Andy McFadden5167ec62014-05-22 13:08:43 -0700736 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700737 previous = Fence::SIGNAL_TIME_INVALID;
Andy McFaddenc751e922014-05-08 14:53:26 -0700738 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
739 size_t idx = (i + mPresentSampleOffset) % NUM_PRESENT_SAMPLES;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700740 nsecs_t presentTime = mPresentFences[idx]->getSignalTime();
741 if (presentTime == Fence::SIGNAL_TIME_PENDING) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800742 StringAppendF(&result, " [unsignaled fence]\n");
Lloyd Pique78ce4182018-01-31 16:39:51 -0800743 } else if (presentTime == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800744 StringAppendF(&result, " [invalid fence]\n");
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700745 } else if (previous == Fence::SIGNAL_TIME_PENDING ||
Lloyd Pique78ce4182018-01-31 16:39:51 -0800746 previous == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800747 StringAppendF(&result, " %" PRId64 " (%.3f ms ago)\n", presentTime,
748 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700749 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800750 StringAppendF(&result, " %" PRId64 " (+%" PRId64 " / %.3f) (%.3f ms ago)\n",
751 presentTime, presentTime - previous,
752 (presentTime - previous) / (double)mPeriod,
753 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700754 }
755 previous = presentTime;
756 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700757
Yiwei Zhang5434a782018-12-05 18:06:32 -0800758 StringAppendF(&result, "current monotonic time: %" PRId64 "\n", now);
Andy McFaddenc751e922014-05-08 14:53:26 -0700759}
760
Ana Krulec010d2192018-10-08 06:29:54 -0700761// TODO(b/113612090): Figure out how much of this is still relevant.
762// We need to determine the time when a buffer acquired now will be
763// displayed. This can be calculated:
764// time when previous buffer's actual-present fence was signaled
765// + current display refresh rate * HWC latency
766// + a little extra padding
767//
768// Buffer producers are expected to set their desired presentation time
769// based on choreographer time stamps, which (coming from vsync events)
770// will be slightly later then the actual-present timing. If we get a
771// desired-present time that is unintentionally a hair after the next
772// vsync, we'll hold the frame when we really want to display it. We
773// need to take the offset between actual-present and reported-vsync
774// into account.
775//
776// If the system is configured without a DispSync phase offset for the app,
777// we also want to throw in a bit of padding to avoid edge cases where we
778// just barely miss. We want to do it here, not in every app. A major
779// source of trouble is the app's use of the display's ideal refresh time
780// (via Display.getRefreshRate()), which could be off of the actual refresh
781// by a few percent, with the error multiplied by the number of frames
782// between now and when the buffer should be displayed.
783//
784// If the refresh reported to the app has a phase offset, we shouldn't need
785// to tweak anything here.
786nsecs_t DispSync::expectedPresentTime() {
787 // The HWC doesn't currently have a way to report additional latency.
788 // Assume that whatever we submit now will appear right after the flip.
789 // For a smart panel this might be 1. This is expressed in frames,
790 // rather than time, because we expect to have a constant frame delay
791 // regardless of the refresh rate.
792 const uint32_t hwcLatency = 0;
793
794 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
Ana Krulec757f63a2019-01-25 10:46:18 -0800795 return computeNextRefresh(hwcLatency);
Ana Krulec010d2192018-10-08 06:29:54 -0700796}
797
Lloyd Pique41be5d22018-06-21 13:11:48 -0700798} // namespace impl
799
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700800} // namespace android