blob: 665179e93e5ada058862e386744f9d389052d605 [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;
46
47namespace impl {
48
Tim Murray4a4e4a22016-04-19 16:29:23 +000049// Setting this to true adds a zero-phase tracer for correlating with hardware
50// vsync events
51static const bool kEnableZeroPhaseTracer = false;
52
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070053// This is the threshold used to determine when hardware vsync events are
54// needed to re-synchronize the software vsync model with the hardware. The
55// error metric used is the mean of the squared difference between each
56// present time and the nearest software-predicted vsync.
Lloyd Pique78ce4182018-01-31 16:39:51 -080057static const nsecs_t kErrorThreshold = 160000000000; // 400 usec squared
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070058
Tim Murray4a4e4a22016-04-19 16:29:23 +000059#undef LOG_TAG
60#define LOG_TAG "DispSyncThread"
Lloyd Pique78ce4182018-01-31 16:39:51 -080061class DispSyncThread : public Thread {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070062public:
Ana Krulec064a82f2018-09-11 16:03:03 -070063 DispSyncThread(const char* name, bool showTraceDetailedInfo)
Lloyd Pique78ce4182018-01-31 16:39:51 -080064 : mName(name),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070065 mStop(false),
66 mPeriod(0),
67 mPhase(0),
Haixia Shi676b1f62015-10-28 16:19:01 -070068 mReferenceTime(0),
Tim Murray4a4e4a22016-04-19 16:29:23 +000069 mWakeupLatency(0),
Ana Krulec064a82f2018-09-11 16:03:03 -070070 mFrameNumber(0),
71 mTraceDetailedInfo(showTraceDetailedInfo) {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070072
73 virtual ~DispSyncThread() {}
74
Haixia Shi676b1f62015-10-28 16:19:01 -070075 void updateModel(nsecs_t period, nsecs_t phase, nsecs_t referenceTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -070076 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070077 Mutex::Autolock lock(mMutex);
78 mPeriod = period;
79 mPhase = phase;
Haixia Shi676b1f62015-10-28 16:19:01 -070080 mReferenceTime = referenceTime;
Alec Mourif5fe85c2019-02-22 13:40:36 -080081 if (mTraceDetailedInfo) {
82 ATRACE_INT64("DispSync:Period", mPeriod);
83 ATRACE_INT64("DispSync:Phase", mPhase + mPeriod / 2);
84 ATRACE_INT64("DispSync:Reference Time", mReferenceTime);
85 }
Tim Murray4a4e4a22016-04-19 16:29:23 +000086 ALOGV("[%s] updateModel: mPeriod = %" PRId64 ", mPhase = %" PRId64
Lloyd Pique78ce4182018-01-31 16:39:51 -080087 " mReferenceTime = %" PRId64,
88 mName, ns2us(mPeriod), ns2us(mPhase), ns2us(mReferenceTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070089 mCond.signal();
90 }
91
92 void stop() {
Ana Krulec064a82f2018-09-11 16:03:03 -070093 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070094 Mutex::Autolock lock(mMutex);
95 mStop = true;
96 mCond.signal();
97 }
98
99 virtual bool threadLoop() {
100 status_t err;
101 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700102
103 while (true) {
Ana Krulec9a52b192018-07-12 18:18:47 -0400104 std::vector<CallbackInvocation> callbackInvocations;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700105
106 nsecs_t targetTime = 0;
107
108 { // Scope for lock
109 Mutex::Autolock lock(mMutex);
110
Ana Krulec064a82f2018-09-11 16:03:03 -0700111 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000112 ATRACE_INT64("DispSync:Frame", mFrameNumber);
113 }
114 ALOGV("[%s] Frame %" PRId64, mName, mFrameNumber);
115 ++mFrameNumber;
116
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700117 if (mStop) {
118 return false;
119 }
120
121 if (mPeriod == 0) {
122 err = mCond.wait(mMutex);
123 if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800124 ALOGE("error waiting for new events: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700125 return false;
126 }
127 continue;
128 }
129
Dan Stoza8f8374d2016-04-19 10:03:46 -0700130 targetTime = computeNextEventTimeLocked(now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700131
132 bool isWakeup = false;
133
134 if (now < targetTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700135 if (mTraceDetailedInfo) ATRACE_NAME("DispSync waiting");
Dan Stoza8f8374d2016-04-19 10:03:46 -0700136
137 if (targetTime == INT64_MAX) {
138 ALOGV("[%s] Waiting forever", mName);
139 err = mCond.wait(mMutex);
140 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800141 ALOGV("[%s] Waiting until %" PRId64, mName, ns2us(targetTime));
Dan Stoza8f8374d2016-04-19 10:03:46 -0700142 err = mCond.waitRelative(mMutex, targetTime - now);
143 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700144
145 if (err == TIMED_OUT) {
146 isWakeup = true;
147 } else if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800148 ALOGE("error waiting for next event: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700149 return false;
150 }
151 }
152
153 now = systemTime(SYSTEM_TIME_MONOTONIC);
154
Tim Murray4a4e4a22016-04-19 16:29:23 +0000155 // Don't correct by more than 1.5 ms
156 static const nsecs_t kMaxWakeupLatency = us2ns(1500);
157
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700158 if (isWakeup) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800159 mWakeupLatency = ((mWakeupLatency * 63) + (now - targetTime)) / 64;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000160 mWakeupLatency = min(mWakeupLatency, kMaxWakeupLatency);
Ana Krulec064a82f2018-09-11 16:03:03 -0700161 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000162 ATRACE_INT64("DispSync:WakeupLat", now - targetTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700163 ATRACE_INT64("DispSync:AvgWakeupLat", mWakeupLatency);
164 }
165 }
166
167 callbackInvocations = gatherCallbackInvocationsLocked(now);
168 }
169
170 if (callbackInvocations.size() > 0) {
Andy McFadden645b1f72014-06-10 14:43:32 -0700171 fireCallbackInvocations(callbackInvocations);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700172 }
173 }
174
175 return false;
176 }
177
Lloyd Piquee83f9312018-02-01 12:53:17 -0800178 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700179 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700180 Mutex::Autolock lock(mMutex);
181
182 for (size_t i = 0; i < mEventListeners.size(); i++) {
183 if (mEventListeners[i].mCallback == callback) {
184 return BAD_VALUE;
185 }
186 }
187
188 EventListener listener;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000189 listener.mName = name;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700190 listener.mPhase = phase;
191 listener.mCallback = callback;
Jamie Gennis629b9872013-10-29 13:36:12 -0700192
193 // We want to allow the firstmost future event to fire without
Tim Murray4a4e4a22016-04-19 16:29:23 +0000194 // allowing any past events to fire
Lloyd Pique78ce4182018-01-31 16:39:51 -0800195 listener.mLastEventTime = systemTime() - mPeriod / 2 + mPhase - mWakeupLatency;
Jamie Gennis629b9872013-10-29 13:36:12 -0700196
Ana Krulec9a52b192018-07-12 18:18:47 -0400197 mEventListeners.push_back(listener);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700198
199 mCond.signal();
200
201 return NO_ERROR;
202 }
203
Lloyd Piquee83f9312018-02-01 12:53:17 -0800204 status_t removeEventListener(DispSync::Callback* callback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700205 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700206 Mutex::Autolock lock(mMutex);
207
Ana Krulec9a52b192018-07-12 18:18:47 -0400208 for (std::vector<EventListener>::iterator it = mEventListeners.begin();
209 it != mEventListeners.end(); ++it) {
210 if (it->mCallback == callback) {
211 mEventListeners.erase(it);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700212 mCond.signal();
213 return NO_ERROR;
214 }
215 }
216
217 return BAD_VALUE;
218 }
219
Dan Stoza84d619e2018-03-28 17:07:36 -0700220 status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700221 if (mTraceDetailedInfo) ATRACE_CALL();
Dan Stoza84d619e2018-03-28 17:07:36 -0700222 Mutex::Autolock lock(mMutex);
223
Ana Krulec9a52b192018-07-12 18:18:47 -0400224 for (auto& eventListener : mEventListeners) {
225 if (eventListener.mCallback == callback) {
226 const nsecs_t oldPhase = eventListener.mPhase;
227 eventListener.mPhase = phase;
Dan Stoza84d619e2018-03-28 17:07:36 -0700228
229 // Pretend that the last time this event was handled at the same frame but with the
230 // new offset to allow for a seamless offset change without double-firing or
231 // skipping.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200232 nsecs_t diff = oldPhase - phase;
233 if (diff > mPeriod / 2) {
234 diff -= mPeriod;
235 } else if (diff < -mPeriod / 2) {
236 diff += mPeriod;
237 }
Ana Krulec9a52b192018-07-12 18:18:47 -0400238 eventListener.mLastEventTime -= diff;
Dan Stoza84d619e2018-03-28 17:07:36 -0700239 mCond.signal();
240 return NO_ERROR;
241 }
242 }
243
244 return BAD_VALUE;
245 }
246
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700247private:
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) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700261 if (mTraceDetailedInfo) ATRACE_CALL();
Tim Murray4a4e4a22016-04-19 16:29:23 +0000262 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) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700277 if (mTraceDetailedInfo) 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) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700300 if (mTraceDetailedInfo) 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) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700351 if (mTraceDetailedInfo) 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;
Ana Krulec064a82f2018-09-11 16:03:03 -0700372
373 // Flag to turn on logging in systrace.
374 const bool mTraceDetailedInfo;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700375};
376
Tim Murray4a4e4a22016-04-19 16:29:23 +0000377#undef LOG_TAG
378#define LOG_TAG "DispSync"
379
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700380class ZeroPhaseTracer : public DispSync::Callback {
381public:
382 ZeroPhaseTracer() : mParity(false) {}
383
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700384 virtual void onDispSyncEvent(nsecs_t /*when*/) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700385 mParity = !mParity;
386 ATRACE_INT("ZERO_PHASE_VSYNC", mParity ? 1 : 0);
387 }
388
389private:
390 bool mParity;
391};
392
Ana Krulec064a82f2018-09-11 16:03:03 -0700393DispSync::DispSync(const char* name) : mName(name), mRefreshSkipCount(0) {
394 // This flag offers the ability to turn on systrace logging from the shell.
395 char value[PROPERTY_VALUE_MAX];
396 property_get("debug.sf.dispsync_trace_detailed_info", value, "0");
397 mTraceDetailedInfo = atoi(value);
398 mThread = new DispSyncThread(name, mTraceDetailedInfo);
399}
Andy McFadden645b1f72014-06-10 14:43:32 -0700400
Saurabh Shahf4174532017-07-13 10:45:07 -0700401DispSync::~DispSync() {}
402
403void DispSync::init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset) {
404 mIgnorePresentFences = !hasSyncFramework;
405 mPresentTimeOffset = dispSyncPresentTimeOffset;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700406 mThread->run("DispSync", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
Saurabh Shahf4174532017-07-13 10:45:07 -0700407
Tim Murrayacff43d2016-07-29 13:57:24 -0700408 // set DispSync to SCHED_FIFO to minimize jitter
409 struct sched_param param = {0};
Tim Murray35520632016-09-07 12:18:17 -0700410 param.sched_priority = 2;
Tim Murrayacff43d2016-07-29 13:57:24 -0700411 if (sched_setscheduler(mThread->getTid(), SCHED_FIFO, &param) != 0) {
412 ALOGE("Couldn't set SCHED_FIFO for DispSyncThread");
413 }
414
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700415 reset();
416 beginResync();
417
Ana Krulec064a82f2018-09-11 16:03:03 -0700418 if (mTraceDetailedInfo && kEnableZeroPhaseTracer) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700419 mZeroPhaseTracer = std::make_unique<ZeroPhaseTracer>();
420 addEventListener("ZeroPhaseTracer", 0, mZeroPhaseTracer.get());
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700421 }
422}
423
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700424void DispSync::reset() {
425 Mutex::Autolock lock(mMutex);
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700426 resetLocked();
427}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700428
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700429void DispSync::resetLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700430 mPhase = 0;
431 mReferenceTime = 0;
432 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700433 mNumResyncSamples = 0;
434 mFirstResyncSample = 0;
435 mNumResyncSamplesSincePresent = 0;
436 resetErrorLocked();
437}
438
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700439bool DispSync::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700440 Mutex::Autolock lock(mMutex);
441
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700442 if (mIgnorePresentFences) {
443 return true;
444 }
445
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700446 mPresentFences[mPresentSampleOffset] = fenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700447 mPresentSampleOffset = (mPresentSampleOffset + 1) % NUM_PRESENT_SAMPLES;
448 mNumResyncSamplesSincePresent = 0;
449
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700450 updateErrorLocked();
451
Haixia Shi676b1f62015-10-28 16:19:01 -0700452 return !mModelUpdated || mError > kErrorThreshold;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700453}
454
455void DispSync::beginResync() {
456 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000457 ALOGV("[%s] beginResync", mName);
Haixia Shi676b1f62015-10-28 16:19:01 -0700458 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700459 mNumResyncSamples = 0;
460}
461
462bool DispSync::addResyncSample(nsecs_t timestamp) {
463 Mutex::Autolock lock(mMutex);
464
Tim Murray4a4e4a22016-04-19 16:29:23 +0000465 ALOGV("[%s] addResyncSample(%" PRId64 ")", mName, ns2us(timestamp));
466
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700467 size_t idx = (mFirstResyncSample + mNumResyncSamples) % MAX_RESYNC_SAMPLES;
468 mResyncSamples[idx] = timestamp;
Haixia Shi664339a2015-10-28 13:22:22 -0700469 if (mNumResyncSamples == 0) {
Haixia Shi676b1f62015-10-28 16:19:01 -0700470 mPhase = 0;
471 mReferenceTime = timestamp;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000472 ALOGV("[%s] First resync sample: mPeriod = %" PRId64 ", mPhase = 0, "
Lloyd Pique78ce4182018-01-31 16:39:51 -0800473 "mReferenceTime = %" PRId64,
474 mName, ns2us(mPeriod), ns2us(mReferenceTime));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000475 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Haixia Shi664339a2015-10-28 13:22:22 -0700476 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700477
478 if (mNumResyncSamples < MAX_RESYNC_SAMPLES) {
479 mNumResyncSamples++;
480 } else {
481 mFirstResyncSample = (mFirstResyncSample + 1) % MAX_RESYNC_SAMPLES;
482 }
483
484 updateModelLocked();
485
486 if (mNumResyncSamplesSincePresent++ > MAX_RESYNC_SAMPLES_WITHOUT_PRESENT) {
487 resetErrorLocked();
488 }
489
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800490 if (mIgnorePresentFences) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700491 // If we're ignoring the present fences we have no way to know whether
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700492 // or not we're synchronized with the HW vsyncs, so we just request
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700493 // that the HW vsync events be turned on.
494 return true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700495 }
496
Tim Murray4a4e4a22016-04-19 16:29:23 +0000497 // Check against kErrorThreshold / 2 to add some hysteresis before having to
498 // resync again
499 bool modelLocked = mModelUpdated && mError < (kErrorThreshold / 2);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800500 ALOGV("[%s] addResyncSample returning %s", mName, modelLocked ? "locked" : "unlocked");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000501 return !modelLocked;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700502}
503
Lloyd Pique78ce4182018-01-31 16:39:51 -0800504void DispSync::endResync() {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700505
Lloyd Piquee83f9312018-02-01 12:53:17 -0800506status_t DispSync::addEventListener(const char* name, nsecs_t phase, Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700507 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000508 return mThread->addEventListener(name, phase, callback);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700509}
510
Andy McFadden645b1f72014-06-10 14:43:32 -0700511void DispSync::setRefreshSkipCount(int count) {
512 Mutex::Autolock lock(mMutex);
513 ALOGD("setRefreshSkipCount(%d)", count);
514 mRefreshSkipCount = count;
515 updateModelLocked();
Ruchi Kandoif52b3c82014-04-24 16:42:35 -0700516}
517
Lloyd Piquee83f9312018-02-01 12:53:17 -0800518status_t DispSync::removeEventListener(Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700519 Mutex::Autolock lock(mMutex);
520 return mThread->removeEventListener(callback);
521}
522
Dan Stoza84d619e2018-03-28 17:07:36 -0700523status_t DispSync::changePhaseOffset(Callback* callback, nsecs_t phase) {
524 Mutex::Autolock lock(mMutex);
525 return mThread->changePhaseOffset(callback, phase);
526}
527
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700528void DispSync::setPeriod(nsecs_t period) {
529 Mutex::Autolock lock(mMutex);
David Sodman1afa8b02018-10-15 11:20:48 -0700530 mPeriod = period;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700531 mPhase = 0;
Haixia Shi676b1f62015-10-28 16:19:01 -0700532 mReferenceTime = 0;
533 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700534}
535
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700536nsecs_t DispSync::getPeriod() {
537 // lock mutex as mPeriod changes multiple times in updateModelLocked
538 Mutex::Autolock lock(mMutex);
539 return mPeriod;
540}
541
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700542void DispSync::updateModelLocked() {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000543 ALOGV("[%s] updateModelLocked %zu", mName, mNumResyncSamples);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700544 if (mNumResyncSamples >= MIN_RESYNC_SAMPLES_FOR_UPDATE) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000545 ALOGV("[%s] Computing...", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700546 nsecs_t durationSum = 0;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000547 nsecs_t minDuration = INT64_MAX;
548 nsecs_t maxDuration = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700549 for (size_t i = 1; i < mNumResyncSamples; i++) {
550 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
551 size_t prev = (idx + MAX_RESYNC_SAMPLES - 1) % MAX_RESYNC_SAMPLES;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000552 nsecs_t duration = mResyncSamples[idx] - mResyncSamples[prev];
553 durationSum += duration;
554 minDuration = min(minDuration, duration);
555 maxDuration = max(maxDuration, duration);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700556 }
557
Tim Murray4a4e4a22016-04-19 16:29:23 +0000558 // Exclude the min and max from the average
559 durationSum -= minDuration + maxDuration;
David Sodman1afa8b02018-10-15 11:20:48 -0700560 mPeriod = durationSum / (mNumResyncSamples - 3);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000561
562 ALOGV("[%s] mPeriod = %" PRId64, mName, ns2us(mPeriod));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700563
564 double sampleAvgX = 0;
565 double sampleAvgY = 0;
566 double scale = 2.0 * M_PI / double(mPeriod);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000567 // Intentionally skip the first sample
568 for (size_t i = 1; i < mNumResyncSamples; i++) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700569 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
Haixia Shi676b1f62015-10-28 16:19:01 -0700570 nsecs_t sample = mResyncSamples[idx] - mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700571 double samplePhase = double(sample % mPeriod) * scale;
572 sampleAvgX += cos(samplePhase);
573 sampleAvgY += sin(samplePhase);
574 }
575
Tim Murray4a4e4a22016-04-19 16:29:23 +0000576 sampleAvgX /= double(mNumResyncSamples - 1);
577 sampleAvgY /= double(mNumResyncSamples - 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700578
579 mPhase = nsecs_t(atan2(sampleAvgY, sampleAvgX) / scale);
580
Tim Murray4a4e4a22016-04-19 16:29:23 +0000581 ALOGV("[%s] mPhase = %" PRId64, mName, ns2us(mPhase));
582
583 if (mPhase < -(mPeriod / 2)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700584 mPhase += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000585 ALOGV("[%s] Adjusting mPhase -> %" PRId64, mName, ns2us(mPhase));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700586 }
587
Andy McFadden645b1f72014-06-10 14:43:32 -0700588 // Artificially inflate the period if requested.
589 mPeriod += mPeriod * mRefreshSkipCount;
590
Haixia Shi676b1f62015-10-28 16:19:01 -0700591 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
592 mModelUpdated = true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700593 }
594}
595
596void DispSync::updateErrorLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700597 if (!mModelUpdated) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700598 return;
599 }
600
Andy McFadden645b1f72014-06-10 14:43:32 -0700601 // Need to compare present fences against the un-adjusted refresh period,
602 // since they might arrive between two events.
603 nsecs_t period = mPeriod / (1 + mRefreshSkipCount);
604
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700605 int numErrSamples = 0;
606 nsecs_t sqErrSum = 0;
607
608 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700609 // Only check for the cached value of signal time to avoid unecessary
610 // syscalls. It is the responsibility of the DispSync owner to
611 // call getSignalTime() periodically so the cache is updated when the
612 // fence signals.
613 nsecs_t time = mPresentFences[i]->getCachedSignalTime();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800614 if (time == Fence::SIGNAL_TIME_PENDING || time == Fence::SIGNAL_TIME_INVALID) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700615 continue;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700616 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700617
618 nsecs_t sample = time - mReferenceTime;
619 if (sample <= mPhase) {
620 continue;
621 }
622
623 nsecs_t sampleErr = (sample - mPhase) % period;
624 if (sampleErr > period / 2) {
625 sampleErr -= period;
626 }
627 sqErrSum += sampleErr * sampleErr;
628 numErrSamples++;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700629 }
630
631 if (numErrSamples > 0) {
632 mError = sqErrSum / numErrSamples;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700633 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700634 } else {
635 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700636 // Use mod ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT to avoid log spam.
637 mZeroErrSamplesCount++;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800638 ALOGE_IF((mZeroErrSamplesCount % ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT) == 0,
639 "No present times for model error.");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700640 }
641
Ana Krulec064a82f2018-09-11 16:03:03 -0700642 if (mTraceDetailedInfo) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700643 ATRACE_INT64("DispSync:Error", mError);
644 }
645}
646
647void DispSync::resetErrorLocked() {
648 mPresentSampleOffset = 0;
649 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700650 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700651 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700652 mPresentFences[i] = FenceTime::NO_FENCE;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700653 }
654}
655
Andy McFadden41d67d72014-04-25 16:58:34 -0700656nsecs_t DispSync::computeNextRefresh(int periodOffset) const {
Andy McFadden150ecd82014-05-08 14:56:50 -0700657 Mutex::Autolock lock(mMutex);
Andy McFadden41d67d72014-04-25 16:58:34 -0700658 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Haixia Shi676b1f62015-10-28 16:19:01 -0700659 nsecs_t phase = mReferenceTime + mPhase;
Marissa Wall17b4e452018-12-26 16:32:34 -0800660 if (mPeriod == 0) {
661 return 0;
662 }
Haixia Shi676b1f62015-10-28 16:19:01 -0700663 return (((now - phase) / mPeriod) + periodOffset + 1) * mPeriod + phase;
Andy McFadden41d67d72014-04-25 16:58:34 -0700664}
665
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700666void DispSync::setIgnorePresentFences(bool ignore) {
667 Mutex::Autolock lock(mMutex);
668 if (mIgnorePresentFences != ignore) {
669 mIgnorePresentFences = ignore;
670 resetLocked();
671 }
672}
673
Yiwei Zhang5434a782018-12-05 18:06:32 -0800674void DispSync::dump(std::string& result) const {
Andy McFaddenc751e922014-05-08 14:53:26 -0700675 Mutex::Autolock lock(mMutex);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800676 StringAppendF(&result, "present fences are %s\n", mIgnorePresentFences ? "ignored" : "used");
677 StringAppendF(&result, "mPeriod: %" PRId64 " ns (%.3f fps; skipCount=%d)\n", mPeriod,
678 1000000000.0 / mPeriod, mRefreshSkipCount);
679 StringAppendF(&result, "mPhase: %" PRId64 " ns\n", mPhase);
680 StringAppendF(&result, "mError: %" PRId64 " ns (sqrt=%.1f)\n", mError, sqrt(mError));
681 StringAppendF(&result, "mNumResyncSamplesSincePresent: %d (limit %d)\n",
682 mNumResyncSamplesSincePresent, MAX_RESYNC_SAMPLES_WITHOUT_PRESENT);
683 StringAppendF(&result, "mNumResyncSamples: %zd (max %d)\n", mNumResyncSamples,
684 MAX_RESYNC_SAMPLES);
Andy McFaddenc751e922014-05-08 14:53:26 -0700685
Yiwei Zhang5434a782018-12-05 18:06:32 -0800686 result.append("mResyncSamples:\n");
Andy McFaddenc751e922014-05-08 14:53:26 -0700687 nsecs_t previous = -1;
688 for (size_t i = 0; i < mNumResyncSamples; i++) {
689 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
690 nsecs_t sampleTime = mResyncSamples[idx];
691 if (i == 0) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800692 StringAppendF(&result, " %" PRId64 "\n", sampleTime);
Andy McFaddenc751e922014-05-08 14:53:26 -0700693 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800694 StringAppendF(&result, " %" PRId64 " (+%" PRId64 ")\n", sampleTime,
695 sampleTime - previous);
Andy McFaddenc751e922014-05-08 14:53:26 -0700696 }
697 previous = sampleTime;
698 }
699
Yiwei Zhang5434a782018-12-05 18:06:32 -0800700 StringAppendF(&result, "mPresentFences [%d]:\n", NUM_PRESENT_SAMPLES);
Andy McFadden5167ec62014-05-22 13:08:43 -0700701 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700702 previous = Fence::SIGNAL_TIME_INVALID;
Andy McFaddenc751e922014-05-08 14:53:26 -0700703 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
704 size_t idx = (i + mPresentSampleOffset) % NUM_PRESENT_SAMPLES;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700705 nsecs_t presentTime = mPresentFences[idx]->getSignalTime();
706 if (presentTime == Fence::SIGNAL_TIME_PENDING) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800707 StringAppendF(&result, " [unsignaled fence]\n");
Lloyd Pique78ce4182018-01-31 16:39:51 -0800708 } else if (presentTime == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800709 StringAppendF(&result, " [invalid fence]\n");
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700710 } else if (previous == Fence::SIGNAL_TIME_PENDING ||
Lloyd Pique78ce4182018-01-31 16:39:51 -0800711 previous == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800712 StringAppendF(&result, " %" PRId64 " (%.3f ms ago)\n", presentTime,
713 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700714 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800715 StringAppendF(&result, " %" PRId64 " (+%" PRId64 " / %.3f) (%.3f ms ago)\n",
716 presentTime, presentTime - previous,
717 (presentTime - previous) / (double)mPeriod,
718 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700719 }
720 previous = presentTime;
721 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700722
Yiwei Zhang5434a782018-12-05 18:06:32 -0800723 StringAppendF(&result, "current monotonic time: %" PRId64 "\n", now);
Andy McFaddenc751e922014-05-08 14:53:26 -0700724}
725
Ana Krulec010d2192018-10-08 06:29:54 -0700726// TODO(b/113612090): Figure out how much of this is still relevant.
727// We need to determine the time when a buffer acquired now will be
728// displayed. This can be calculated:
729// time when previous buffer's actual-present fence was signaled
730// + current display refresh rate * HWC latency
731// + a little extra padding
732//
733// Buffer producers are expected to set their desired presentation time
734// based on choreographer time stamps, which (coming from vsync events)
735// will be slightly later then the actual-present timing. If we get a
736// desired-present time that is unintentionally a hair after the next
737// vsync, we'll hold the frame when we really want to display it. We
738// need to take the offset between actual-present and reported-vsync
739// into account.
740//
741// If the system is configured without a DispSync phase offset for the app,
742// we also want to throw in a bit of padding to avoid edge cases where we
743// just barely miss. We want to do it here, not in every app. A major
744// source of trouble is the app's use of the display's ideal refresh time
745// (via Display.getRefreshRate()), which could be off of the actual refresh
746// by a few percent, with the error multiplied by the number of frames
747// between now and when the buffer should be displayed.
748//
749// If the refresh reported to the app has a phase offset, we shouldn't need
750// to tweak anything here.
751nsecs_t DispSync::expectedPresentTime() {
752 // The HWC doesn't currently have a way to report additional latency.
753 // Assume that whatever we submit now will appear right after the flip.
754 // For a smart panel this might be 1. This is expressed in frames,
755 // rather than time, because we expect to have a constant frame delay
756 // regardless of the refresh rate.
757 const uint32_t hwcLatency = 0;
758
759 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
Ana Krulec757f63a2019-01-25 10:46:18 -0800760 return computeNextRefresh(hwcLatency);
Ana Krulec010d2192018-10-08 06:29:54 -0700761}
762
Lloyd Pique41be5d22018-06-21 13:11:48 -0700763} // namespace impl
764
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700765} // namespace android