blob: 1eccf9a73bf2ec0b07213fb5137126684f9a2071 [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);
Weng Sud6598312019-03-11 04:10:18 +000079 mPeriod = period;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070080 mPhase = phase;
Haixia Shi676b1f62015-10-28 16:19:01 -070081 mReferenceTime = referenceTime;
Alec Mourif5fe85c2019-02-22 13:40:36 -080082 if (mTraceDetailedInfo) {
83 ATRACE_INT64("DispSync:Period", mPeriod);
84 ATRACE_INT64("DispSync:Phase", mPhase + mPeriod / 2);
85 ATRACE_INT64("DispSync:Reference Time", mReferenceTime);
86 }
Tim Murray4a4e4a22016-04-19 16:29:23 +000087 ALOGV("[%s] updateModel: mPeriod = %" PRId64 ", mPhase = %" PRId64
Lloyd Pique78ce4182018-01-31 16:39:51 -080088 " mReferenceTime = %" PRId64,
89 mName, ns2us(mPeriod), ns2us(mPhase), ns2us(mReferenceTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070090 mCond.signal();
91 }
92
93 void stop() {
Ana Krulec064a82f2018-09-11 16:03:03 -070094 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070095 Mutex::Autolock lock(mMutex);
96 mStop = true;
97 mCond.signal();
98 }
99
100 virtual bool threadLoop() {
101 status_t err;
102 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700103
104 while (true) {
Ana Krulec9a52b192018-07-12 18:18:47 -0400105 std::vector<CallbackInvocation> callbackInvocations;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700106
107 nsecs_t targetTime = 0;
108
109 { // Scope for lock
110 Mutex::Autolock lock(mMutex);
111
Ana Krulec064a82f2018-09-11 16:03:03 -0700112 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000113 ATRACE_INT64("DispSync:Frame", mFrameNumber);
114 }
115 ALOGV("[%s] Frame %" PRId64, mName, mFrameNumber);
116 ++mFrameNumber;
117
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700118 if (mStop) {
119 return false;
120 }
121
122 if (mPeriod == 0) {
123 err = mCond.wait(mMutex);
124 if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800125 ALOGE("error waiting for new events: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700126 return false;
127 }
128 continue;
129 }
130
Dan Stoza8f8374d2016-04-19 10:03:46 -0700131 targetTime = computeNextEventTimeLocked(now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700132
133 bool isWakeup = false;
134
135 if (now < targetTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700136 if (mTraceDetailedInfo) ATRACE_NAME("DispSync waiting");
Dan Stoza8f8374d2016-04-19 10:03:46 -0700137
138 if (targetTime == INT64_MAX) {
139 ALOGV("[%s] Waiting forever", mName);
140 err = mCond.wait(mMutex);
141 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800142 ALOGV("[%s] Waiting until %" PRId64, mName, ns2us(targetTime));
Dan Stoza8f8374d2016-04-19 10:03:46 -0700143 err = mCond.waitRelative(mMutex, targetTime - now);
144 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700145
146 if (err == TIMED_OUT) {
147 isWakeup = true;
148 } else if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800149 ALOGE("error waiting for next event: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700150 return false;
151 }
152 }
153
154 now = systemTime(SYSTEM_TIME_MONOTONIC);
155
Tim Murray4a4e4a22016-04-19 16:29:23 +0000156 // Don't correct by more than 1.5 ms
157 static const nsecs_t kMaxWakeupLatency = us2ns(1500);
158
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700159 if (isWakeup) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800160 mWakeupLatency = ((mWakeupLatency * 63) + (now - targetTime)) / 64;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000161 mWakeupLatency = min(mWakeupLatency, kMaxWakeupLatency);
Ana Krulec064a82f2018-09-11 16:03:03 -0700162 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000163 ATRACE_INT64("DispSync:WakeupLat", now - targetTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700164 ATRACE_INT64("DispSync:AvgWakeupLat", mWakeupLatency);
165 }
166 }
167
168 callbackInvocations = gatherCallbackInvocationsLocked(now);
169 }
170
171 if (callbackInvocations.size() > 0) {
Andy McFadden645b1f72014-06-10 14:43:32 -0700172 fireCallbackInvocations(callbackInvocations);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700173 }
174 }
175
176 return false;
177 }
178
Lloyd Piquee83f9312018-02-01 12:53:17 -0800179 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700180 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700181 Mutex::Autolock lock(mMutex);
182
183 for (size_t i = 0; i < mEventListeners.size(); i++) {
184 if (mEventListeners[i].mCallback == callback) {
185 return BAD_VALUE;
186 }
187 }
188
189 EventListener listener;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000190 listener.mName = name;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700191 listener.mPhase = phase;
192 listener.mCallback = callback;
Jamie Gennis629b9872013-10-29 13:36:12 -0700193
194 // We want to allow the firstmost future event to fire without
Tim Murray4a4e4a22016-04-19 16:29:23 +0000195 // allowing any past events to fire
Lloyd Pique78ce4182018-01-31 16:39:51 -0800196 listener.mLastEventTime = systemTime() - mPeriod / 2 + mPhase - mWakeupLatency;
Jamie Gennis629b9872013-10-29 13:36:12 -0700197
Ana Krulec9a52b192018-07-12 18:18:47 -0400198 mEventListeners.push_back(listener);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700199
200 mCond.signal();
201
202 return NO_ERROR;
203 }
204
Lloyd Piquee83f9312018-02-01 12:53:17 -0800205 status_t removeEventListener(DispSync::Callback* callback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700206 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700207 Mutex::Autolock lock(mMutex);
208
Ana Krulec9a52b192018-07-12 18:18:47 -0400209 for (std::vector<EventListener>::iterator it = mEventListeners.begin();
210 it != mEventListeners.end(); ++it) {
211 if (it->mCallback == callback) {
212 mEventListeners.erase(it);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700213 mCond.signal();
214 return NO_ERROR;
215 }
216 }
217
218 return BAD_VALUE;
219 }
220
Dan Stoza84d619e2018-03-28 17:07:36 -0700221 status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700222 if (mTraceDetailedInfo) ATRACE_CALL();
Dan Stoza84d619e2018-03-28 17:07:36 -0700223 Mutex::Autolock lock(mMutex);
224
Ana Krulec9a52b192018-07-12 18:18:47 -0400225 for (auto& eventListener : mEventListeners) {
226 if (eventListener.mCallback == callback) {
227 const nsecs_t oldPhase = eventListener.mPhase;
228 eventListener.mPhase = phase;
Dan Stoza84d619e2018-03-28 17:07:36 -0700229
230 // Pretend that the last time this event was handled at the same frame but with the
231 // new offset to allow for a seamless offset change without double-firing or
232 // skipping.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200233 nsecs_t diff = oldPhase - phase;
234 if (diff > mPeriod / 2) {
235 diff -= mPeriod;
236 } else if (diff < -mPeriod / 2) {
237 diff += mPeriod;
238 }
Ana Krulec9a52b192018-07-12 18:18:47 -0400239 eventListener.mLastEventTime -= diff;
Dan Stoza84d619e2018-03-28 17:07:36 -0700240 mCond.signal();
241 return NO_ERROR;
242 }
243 }
244
245 return BAD_VALUE;
246 }
247
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700248private:
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700249 struct EventListener {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000250 const char* mName;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700251 nsecs_t mPhase;
252 nsecs_t mLastEventTime;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800253 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700254 };
255
256 struct CallbackInvocation {
Lloyd Piquee83f9312018-02-01 12:53:17 -0800257 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700258 nsecs_t mEventTime;
259 };
260
261 nsecs_t computeNextEventTimeLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700262 if (mTraceDetailedInfo) ATRACE_CALL();
Tim Murray4a4e4a22016-04-19 16:29:23 +0000263 ALOGV("[%s] computeNextEventTimeLocked", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700264 nsecs_t nextEventTime = INT64_MAX;
265 for (size_t i = 0; i < mEventListeners.size(); i++) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800266 nsecs_t t = computeListenerNextEventTimeLocked(mEventListeners[i], now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700267
268 if (t < nextEventTime) {
269 nextEventTime = t;
270 }
271 }
272
Tim Murray4a4e4a22016-04-19 16:29:23 +0000273 ALOGV("[%s] nextEventTime = %" PRId64, mName, ns2us(nextEventTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700274 return nextEventTime;
275 }
276
Ana Krulec9a52b192018-07-12 18:18:47 -0400277 std::vector<CallbackInvocation> gatherCallbackInvocationsLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700278 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800279 ALOGV("[%s] gatherCallbackInvocationsLocked @ %" PRId64, mName, ns2us(now));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000280
Ana Krulec9a52b192018-07-12 18:18:47 -0400281 std::vector<CallbackInvocation> callbackInvocations;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000282 nsecs_t onePeriodAgo = now - mPeriod;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700283
Ana Krulec9a52b192018-07-12 18:18:47 -0400284 for (auto& eventListener : mEventListeners) {
285 nsecs_t t = computeListenerNextEventTimeLocked(eventListener, onePeriodAgo);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700286
Jamie Gennis0d5c60e2013-10-09 17:49:37 -0700287 if (t < now) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700288 CallbackInvocation ci;
Ana Krulec9a52b192018-07-12 18:18:47 -0400289 ci.mCallback = eventListener.mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700290 ci.mEventTime = t;
Ana Krulec9a52b192018-07-12 18:18:47 -0400291 ALOGV("[%s] [%s] Preparing to fire", mName, eventListener.mName);
292 callbackInvocations.push_back(ci);
293 eventListener.mLastEventTime = t;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700294 }
295 }
296
297 return callbackInvocations;
298 }
299
Lloyd Pique78ce4182018-01-31 16:39:51 -0800300 nsecs_t computeListenerNextEventTimeLocked(const EventListener& listener, nsecs_t baseTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700301 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800302 ALOGV("[%s] [%s] computeListenerNextEventTimeLocked(%" PRId64 ")", mName, listener.mName,
303 ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700304
Tim Murray4a4e4a22016-04-19 16:29:23 +0000305 nsecs_t lastEventTime = listener.mLastEventTime + mWakeupLatency;
306 ALOGV("[%s] lastEventTime: %" PRId64, mName, ns2us(lastEventTime));
307 if (baseTime < lastEventTime) {
308 baseTime = lastEventTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800309 ALOGV("[%s] Clamping baseTime to lastEventTime -> %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700310 }
311
Tim Murray4a4e4a22016-04-19 16:29:23 +0000312 baseTime -= mReferenceTime;
313 ALOGV("[%s] Relative baseTime = %" PRId64, mName, ns2us(baseTime));
314 nsecs_t phase = mPhase + listener.mPhase;
315 ALOGV("[%s] Phase = %" PRId64, mName, ns2us(phase));
316 baseTime -= phase;
317 ALOGV("[%s] baseTime - phase = %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700318
Tim Murray4a4e4a22016-04-19 16:29:23 +0000319 // If our previous time is before the reference (because the reference
320 // has since been updated), the division by mPeriod will truncate
321 // towards zero instead of computing the floor. Since in all cases
322 // before the reference we want the next time to be effectively now, we
323 // set baseTime to -mPeriod so that numPeriods will be -1.
324 // When we add 1 and the phase, we will be at the correct event time for
325 // this period.
326 if (baseTime < 0) {
327 ALOGV("[%s] Correcting negative baseTime", mName);
328 baseTime = -mPeriod;
329 }
330
331 nsecs_t numPeriods = baseTime / mPeriod;
332 ALOGV("[%s] numPeriods = %" PRId64, mName, numPeriods);
333 nsecs_t t = (numPeriods + 1) * mPeriod + phase;
334 ALOGV("[%s] t = %" PRId64, mName, ns2us(t));
335 t += mReferenceTime;
336 ALOGV("[%s] Absolute t = %" PRId64, mName, ns2us(t));
337
338 // Check that it's been slightly more than half a period since the last
339 // event so that we don't accidentally fall into double-rate vsyncs
340 if (t - listener.mLastEventTime < (3 * mPeriod / 5)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700341 t += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000342 ALOGV("[%s] Modifying t -> %" PRId64, mName, ns2us(t));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700343 }
344
Tim Murray4a4e4a22016-04-19 16:29:23 +0000345 t -= mWakeupLatency;
346 ALOGV("[%s] Corrected for wakeup latency -> %" PRId64, mName, ns2us(t));
347
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700348 return t;
349 }
350
Ana Krulec9a52b192018-07-12 18:18:47 -0400351 void fireCallbackInvocations(const std::vector<CallbackInvocation>& callbacks) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700352 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700353 for (size_t i = 0; i < callbacks.size(); i++) {
354 callbacks[i].mCallback->onDispSyncEvent(callbacks[i].mEventTime);
355 }
356 }
357
Tim Murray4a4e4a22016-04-19 16:29:23 +0000358 const char* const mName;
359
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700360 bool mStop;
361
362 nsecs_t mPeriod;
363 nsecs_t mPhase;
Haixia Shi676b1f62015-10-28 16:19:01 -0700364 nsecs_t mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700365 nsecs_t mWakeupLatency;
366
Tim Murray4a4e4a22016-04-19 16:29:23 +0000367 int64_t mFrameNumber;
368
Ana Krulec9a52b192018-07-12 18:18:47 -0400369 std::vector<EventListener> mEventListeners;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700370
371 Mutex mMutex;
372 Condition mCond;
Ana Krulec064a82f2018-09-11 16:03:03 -0700373
374 // Flag to turn on logging in systrace.
375 const bool mTraceDetailedInfo;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700376};
377
Tim Murray4a4e4a22016-04-19 16:29:23 +0000378#undef LOG_TAG
379#define LOG_TAG "DispSync"
380
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700381class ZeroPhaseTracer : public DispSync::Callback {
382public:
383 ZeroPhaseTracer() : mParity(false) {}
384
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700385 virtual void onDispSyncEvent(nsecs_t /*when*/) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700386 mParity = !mParity;
387 ATRACE_INT("ZERO_PHASE_VSYNC", mParity ? 1 : 0);
388 }
389
390private:
391 bool mParity;
392};
393
Ana Krulec064a82f2018-09-11 16:03:03 -0700394DispSync::DispSync(const char* name) : mName(name), mRefreshSkipCount(0) {
395 // This flag offers the ability to turn on systrace logging from the shell.
396 char value[PROPERTY_VALUE_MAX];
397 property_get("debug.sf.dispsync_trace_detailed_info", value, "0");
398 mTraceDetailedInfo = atoi(value);
399 mThread = new DispSyncThread(name, mTraceDetailedInfo);
400}
Andy McFadden645b1f72014-06-10 14:43:32 -0700401
Saurabh Shahf4174532017-07-13 10:45:07 -0700402DispSync::~DispSync() {}
403
404void DispSync::init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset) {
405 mIgnorePresentFences = !hasSyncFramework;
406 mPresentTimeOffset = dispSyncPresentTimeOffset;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700407 mThread->run("DispSync", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
Saurabh Shahf4174532017-07-13 10:45:07 -0700408
Tim Murrayacff43d2016-07-29 13:57:24 -0700409 // set DispSync to SCHED_FIFO to minimize jitter
410 struct sched_param param = {0};
Tim Murray35520632016-09-07 12:18:17 -0700411 param.sched_priority = 2;
Tim Murrayacff43d2016-07-29 13:57:24 -0700412 if (sched_setscheduler(mThread->getTid(), SCHED_FIFO, &param) != 0) {
413 ALOGE("Couldn't set SCHED_FIFO for DispSyncThread");
414 }
415
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700416 reset();
417 beginResync();
418
Ana Krulec064a82f2018-09-11 16:03:03 -0700419 if (mTraceDetailedInfo && kEnableZeroPhaseTracer) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700420 mZeroPhaseTracer = std::make_unique<ZeroPhaseTracer>();
421 addEventListener("ZeroPhaseTracer", 0, mZeroPhaseTracer.get());
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700422 }
423}
424
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700425void DispSync::reset() {
426 Mutex::Autolock lock(mMutex);
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700427 resetLocked();
428}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700429
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700430void DispSync::resetLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700431 mPhase = 0;
Weng Sud6598312019-03-11 04:10:18 +0000432 mReferenceTime = 0;
Haixia Shi676b1f62015-10-28 16:19:01 -0700433 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700434 mNumResyncSamples = 0;
435 mFirstResyncSample = 0;
436 mNumResyncSamplesSincePresent = 0;
437 resetErrorLocked();
438}
439
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700440bool DispSync::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700441 Mutex::Autolock lock(mMutex);
442
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700443 if (mIgnorePresentFences) {
444 return true;
445 }
446
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700447 mPresentFences[mPresentSampleOffset] = fenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700448 mPresentSampleOffset = (mPresentSampleOffset + 1) % NUM_PRESENT_SAMPLES;
449 mNumResyncSamplesSincePresent = 0;
450
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700451 updateErrorLocked();
452
Haixia Shi676b1f62015-10-28 16:19:01 -0700453 return !mModelUpdated || mError > kErrorThreshold;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700454}
455
456void DispSync::beginResync() {
457 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000458 ALOGV("[%s] beginResync", mName);
Haixia Shi676b1f62015-10-28 16:19:01 -0700459 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700460 mNumResyncSamples = 0;
461}
462
463bool DispSync::addResyncSample(nsecs_t timestamp) {
464 Mutex::Autolock lock(mMutex);
465
Tim Murray4a4e4a22016-04-19 16:29:23 +0000466 ALOGV("[%s] addResyncSample(%" PRId64 ")", mName, ns2us(timestamp));
467
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700468 size_t idx = (mFirstResyncSample + mNumResyncSamples) % MAX_RESYNC_SAMPLES;
469 mResyncSamples[idx] = timestamp;
Haixia Shi664339a2015-10-28 13:22:22 -0700470 if (mNumResyncSamples == 0) {
Haixia Shi676b1f62015-10-28 16:19:01 -0700471 mPhase = 0;
472 mReferenceTime = timestamp;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000473 ALOGV("[%s] First resync sample: mPeriod = %" PRId64 ", mPhase = 0, "
Lloyd Pique78ce4182018-01-31 16:39:51 -0800474 "mReferenceTime = %" PRId64,
475 mName, ns2us(mPeriod), ns2us(mReferenceTime));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000476 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Haixia Shi664339a2015-10-28 13:22:22 -0700477 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700478
479 if (mNumResyncSamples < MAX_RESYNC_SAMPLES) {
480 mNumResyncSamples++;
481 } else {
482 mFirstResyncSample = (mFirstResyncSample + 1) % MAX_RESYNC_SAMPLES;
483 }
484
485 updateModelLocked();
486
487 if (mNumResyncSamplesSincePresent++ > MAX_RESYNC_SAMPLES_WITHOUT_PRESENT) {
488 resetErrorLocked();
489 }
490
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800491 if (mIgnorePresentFences) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700492 // If we're ignoring the present fences we have no way to know whether
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700493 // or not we're synchronized with the HW vsyncs, so we just request
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700494 // that the HW vsync events be turned on.
495 return true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700496 }
497
Tim Murray4a4e4a22016-04-19 16:29:23 +0000498 // Check against kErrorThreshold / 2 to add some hysteresis before having to
499 // resync again
500 bool modelLocked = mModelUpdated && mError < (kErrorThreshold / 2);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800501 ALOGV("[%s] addResyncSample returning %s", mName, modelLocked ? "locked" : "unlocked");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000502 return !modelLocked;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700503}
504
Lloyd Pique78ce4182018-01-31 16:39:51 -0800505void DispSync::endResync() {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700506
Lloyd Piquee83f9312018-02-01 12:53:17 -0800507status_t DispSync::addEventListener(const char* name, nsecs_t phase, Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700508 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000509 return mThread->addEventListener(name, phase, callback);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700510}
511
Andy McFadden645b1f72014-06-10 14:43:32 -0700512void DispSync::setRefreshSkipCount(int count) {
513 Mutex::Autolock lock(mMutex);
514 ALOGD("setRefreshSkipCount(%d)", count);
515 mRefreshSkipCount = count;
516 updateModelLocked();
Ruchi Kandoif52b3c82014-04-24 16:42:35 -0700517}
518
Lloyd Piquee83f9312018-02-01 12:53:17 -0800519status_t DispSync::removeEventListener(Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700520 Mutex::Autolock lock(mMutex);
521 return mThread->removeEventListener(callback);
522}
523
Dan Stoza84d619e2018-03-28 17:07:36 -0700524status_t DispSync::changePhaseOffset(Callback* callback, nsecs_t phase) {
525 Mutex::Autolock lock(mMutex);
526 return mThread->changePhaseOffset(callback, phase);
527}
528
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700529void DispSync::setPeriod(nsecs_t period) {
530 Mutex::Autolock lock(mMutex);
David Sodman1afa8b02018-10-15 11:20:48 -0700531 mPeriod = period;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700532 mPhase = 0;
Weng Sud6598312019-03-11 04:10:18 +0000533 mReferenceTime = 0;
Haixia Shi676b1f62015-10-28 16:19:01 -0700534 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700535}
536
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700537nsecs_t DispSync::getPeriod() {
538 // lock mutex as mPeriod changes multiple times in updateModelLocked
539 Mutex::Autolock lock(mMutex);
540 return mPeriod;
541}
542
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700543void DispSync::updateModelLocked() {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000544 ALOGV("[%s] updateModelLocked %zu", mName, mNumResyncSamples);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700545 if (mNumResyncSamples >= MIN_RESYNC_SAMPLES_FOR_UPDATE) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000546 ALOGV("[%s] Computing...", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700547 nsecs_t durationSum = 0;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000548 nsecs_t minDuration = INT64_MAX;
549 nsecs_t maxDuration = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700550 for (size_t i = 1; i < mNumResyncSamples; i++) {
551 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
552 size_t prev = (idx + MAX_RESYNC_SAMPLES - 1) % MAX_RESYNC_SAMPLES;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000553 nsecs_t duration = mResyncSamples[idx] - mResyncSamples[prev];
554 durationSum += duration;
555 minDuration = min(minDuration, duration);
556 maxDuration = max(maxDuration, duration);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700557 }
558
Tim Murray4a4e4a22016-04-19 16:29:23 +0000559 // Exclude the min and max from the average
560 durationSum -= minDuration + maxDuration;
David Sodman1afa8b02018-10-15 11:20:48 -0700561 mPeriod = durationSum / (mNumResyncSamples - 3);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000562
563 ALOGV("[%s] mPeriod = %" PRId64, mName, ns2us(mPeriod));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700564
565 double sampleAvgX = 0;
566 double sampleAvgY = 0;
567 double scale = 2.0 * M_PI / double(mPeriod);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000568 // Intentionally skip the first sample
569 for (size_t i = 1; i < mNumResyncSamples; i++) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700570 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
Haixia Shi676b1f62015-10-28 16:19:01 -0700571 nsecs_t sample = mResyncSamples[idx] - mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700572 double samplePhase = double(sample % mPeriod) * scale;
573 sampleAvgX += cos(samplePhase);
574 sampleAvgY += sin(samplePhase);
575 }
576
Tim Murray4a4e4a22016-04-19 16:29:23 +0000577 sampleAvgX /= double(mNumResyncSamples - 1);
578 sampleAvgY /= double(mNumResyncSamples - 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700579
580 mPhase = nsecs_t(atan2(sampleAvgY, sampleAvgX) / scale);
581
Tim Murray4a4e4a22016-04-19 16:29:23 +0000582 ALOGV("[%s] mPhase = %" PRId64, mName, ns2us(mPhase));
583
584 if (mPhase < -(mPeriod / 2)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700585 mPhase += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000586 ALOGV("[%s] Adjusting mPhase -> %" PRId64, mName, ns2us(mPhase));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700587 }
588
Andy McFadden645b1f72014-06-10 14:43:32 -0700589 // Artificially inflate the period if requested.
590 mPeriod += mPeriod * mRefreshSkipCount;
591
Haixia Shi676b1f62015-10-28 16:19:01 -0700592 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
593 mModelUpdated = true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700594 }
595}
596
597void DispSync::updateErrorLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700598 if (!mModelUpdated) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700599 return;
600 }
601
Andy McFadden645b1f72014-06-10 14:43:32 -0700602 // Need to compare present fences against the un-adjusted refresh period,
603 // since they might arrive between two events.
604 nsecs_t period = mPeriod / (1 + mRefreshSkipCount);
605
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700606 int numErrSamples = 0;
607 nsecs_t sqErrSum = 0;
608
609 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700610 // Only check for the cached value of signal time to avoid unecessary
611 // syscalls. It is the responsibility of the DispSync owner to
612 // call getSignalTime() periodically so the cache is updated when the
613 // fence signals.
614 nsecs_t time = mPresentFences[i]->getCachedSignalTime();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800615 if (time == Fence::SIGNAL_TIME_PENDING || time == Fence::SIGNAL_TIME_INVALID) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700616 continue;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700617 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700618
619 nsecs_t sample = time - mReferenceTime;
620 if (sample <= mPhase) {
621 continue;
622 }
623
624 nsecs_t sampleErr = (sample - mPhase) % period;
625 if (sampleErr > period / 2) {
626 sampleErr -= period;
627 }
628 sqErrSum += sampleErr * sampleErr;
629 numErrSamples++;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700630 }
631
632 if (numErrSamples > 0) {
633 mError = sqErrSum / numErrSamples;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700634 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700635 } else {
636 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700637 // Use mod ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT to avoid log spam.
638 mZeroErrSamplesCount++;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800639 ALOGE_IF((mZeroErrSamplesCount % ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT) == 0,
640 "No present times for model error.");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700641 }
642
Ana Krulec064a82f2018-09-11 16:03:03 -0700643 if (mTraceDetailedInfo) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700644 ATRACE_INT64("DispSync:Error", mError);
645 }
646}
647
648void DispSync::resetErrorLocked() {
649 mPresentSampleOffset = 0;
650 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700651 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700652 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700653 mPresentFences[i] = FenceTime::NO_FENCE;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700654 }
655}
656
Andy McFadden41d67d72014-04-25 16:58:34 -0700657nsecs_t DispSync::computeNextRefresh(int periodOffset) const {
Andy McFadden150ecd82014-05-08 14:56:50 -0700658 Mutex::Autolock lock(mMutex);
Andy McFadden41d67d72014-04-25 16:58:34 -0700659 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Haixia Shi676b1f62015-10-28 16:19:01 -0700660 nsecs_t phase = mReferenceTime + mPhase;
Marissa Wall17b4e452018-12-26 16:32:34 -0800661 if (mPeriod == 0) {
662 return 0;
663 }
Haixia Shi676b1f62015-10-28 16:19:01 -0700664 return (((now - phase) / mPeriod) + periodOffset + 1) * mPeriod + phase;
Andy McFadden41d67d72014-04-25 16:58:34 -0700665}
666
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700667void DispSync::setIgnorePresentFences(bool ignore) {
668 Mutex::Autolock lock(mMutex);
669 if (mIgnorePresentFences != ignore) {
670 mIgnorePresentFences = ignore;
671 resetLocked();
672 }
673}
674
Yiwei Zhang5434a782018-12-05 18:06:32 -0800675void DispSync::dump(std::string& result) const {
Andy McFaddenc751e922014-05-08 14:53:26 -0700676 Mutex::Autolock lock(mMutex);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800677 StringAppendF(&result, "present fences are %s\n", mIgnorePresentFences ? "ignored" : "used");
678 StringAppendF(&result, "mPeriod: %" PRId64 " ns (%.3f fps; skipCount=%d)\n", mPeriod,
679 1000000000.0 / mPeriod, mRefreshSkipCount);
680 StringAppendF(&result, "mPhase: %" PRId64 " ns\n", mPhase);
681 StringAppendF(&result, "mError: %" PRId64 " ns (sqrt=%.1f)\n", mError, sqrt(mError));
682 StringAppendF(&result, "mNumResyncSamplesSincePresent: %d (limit %d)\n",
683 mNumResyncSamplesSincePresent, MAX_RESYNC_SAMPLES_WITHOUT_PRESENT);
684 StringAppendF(&result, "mNumResyncSamples: %zd (max %d)\n", mNumResyncSamples,
685 MAX_RESYNC_SAMPLES);
Andy McFaddenc751e922014-05-08 14:53:26 -0700686
Yiwei Zhang5434a782018-12-05 18:06:32 -0800687 result.append("mResyncSamples:\n");
Andy McFaddenc751e922014-05-08 14:53:26 -0700688 nsecs_t previous = -1;
689 for (size_t i = 0; i < mNumResyncSamples; i++) {
690 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
691 nsecs_t sampleTime = mResyncSamples[idx];
692 if (i == 0) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800693 StringAppendF(&result, " %" PRId64 "\n", sampleTime);
Andy McFaddenc751e922014-05-08 14:53:26 -0700694 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800695 StringAppendF(&result, " %" PRId64 " (+%" PRId64 ")\n", sampleTime,
696 sampleTime - previous);
Andy McFaddenc751e922014-05-08 14:53:26 -0700697 }
698 previous = sampleTime;
699 }
700
Yiwei Zhang5434a782018-12-05 18:06:32 -0800701 StringAppendF(&result, "mPresentFences [%d]:\n", NUM_PRESENT_SAMPLES);
Andy McFadden5167ec62014-05-22 13:08:43 -0700702 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700703 previous = Fence::SIGNAL_TIME_INVALID;
Andy McFaddenc751e922014-05-08 14:53:26 -0700704 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
705 size_t idx = (i + mPresentSampleOffset) % NUM_PRESENT_SAMPLES;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700706 nsecs_t presentTime = mPresentFences[idx]->getSignalTime();
707 if (presentTime == Fence::SIGNAL_TIME_PENDING) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800708 StringAppendF(&result, " [unsignaled fence]\n");
Lloyd Pique78ce4182018-01-31 16:39:51 -0800709 } else if (presentTime == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800710 StringAppendF(&result, " [invalid fence]\n");
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700711 } else if (previous == Fence::SIGNAL_TIME_PENDING ||
Lloyd Pique78ce4182018-01-31 16:39:51 -0800712 previous == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800713 StringAppendF(&result, " %" PRId64 " (%.3f ms ago)\n", presentTime,
714 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700715 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800716 StringAppendF(&result, " %" PRId64 " (+%" PRId64 " / %.3f) (%.3f ms ago)\n",
717 presentTime, presentTime - previous,
718 (presentTime - previous) / (double)mPeriod,
719 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700720 }
721 previous = presentTime;
722 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700723
Yiwei Zhang5434a782018-12-05 18:06:32 -0800724 StringAppendF(&result, "current monotonic time: %" PRId64 "\n", now);
Andy McFaddenc751e922014-05-08 14:53:26 -0700725}
726
Ana Krulec010d2192018-10-08 06:29:54 -0700727// TODO(b/113612090): Figure out how much of this is still relevant.
728// We need to determine the time when a buffer acquired now will be
729// displayed. This can be calculated:
730// time when previous buffer's actual-present fence was signaled
731// + current display refresh rate * HWC latency
732// + a little extra padding
733//
734// Buffer producers are expected to set their desired presentation time
735// based on choreographer time stamps, which (coming from vsync events)
736// will be slightly later then the actual-present timing. If we get a
737// desired-present time that is unintentionally a hair after the next
738// vsync, we'll hold the frame when we really want to display it. We
739// need to take the offset between actual-present and reported-vsync
740// into account.
741//
742// If the system is configured without a DispSync phase offset for the app,
743// we also want to throw in a bit of padding to avoid edge cases where we
744// just barely miss. We want to do it here, not in every app. A major
745// source of trouble is the app's use of the display's ideal refresh time
746// (via Display.getRefreshRate()), which could be off of the actual refresh
747// by a few percent, with the error multiplied by the number of frames
748// between now and when the buffer should be displayed.
749//
750// If the refresh reported to the app has a phase offset, we shouldn't need
751// to tweak anything here.
752nsecs_t DispSync::expectedPresentTime() {
753 // The HWC doesn't currently have a way to report additional latency.
754 // Assume that whatever we submit now will appear right after the flip.
755 // For a smart panel this might be 1. This is expressed in frames,
756 // rather than time, because we expect to have a constant frame delay
757 // regardless of the refresh rate.
758 const uint32_t hwcLatency = 0;
759
760 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
Ana Krulec757f63a2019-01-25 10:46:18 -0800761 return computeNextRefresh(hwcLatency);
Ana Krulec010d2192018-10-08 06:29:54 -0700762}
763
Lloyd Pique41be5d22018-06-21 13:11:48 -0700764} // namespace impl
765
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700766} // namespace android