blob: 172c4184f95be5fe9ef04543cca1e3bdcbb6c326 [file] [log] [blame]
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Tim Murray4a4e4a22016-04-19 16:29:23 +000018//#define LOG_NDEBUG 0
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070019
20// This is needed for stdint.h to define INT64_MAX in C++
21#define __STDC_LIMIT_MACROS
22
23#include <math.h>
24
Mark Salyzyna5e161b2016-09-29 08:08:05 -070025#include <algorithm>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070026
Mark Salyzyn7823e122016-09-29 08:08:05 -070027#include <log/log.h>
David Sodman1afa8b02018-10-15 11:20:48 -070028#include <cutils/properties.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070029#include <utils/String8.h>
30#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
Tim Murray4a4e4a22016-04-19 16:29:23 +000039using std::max;
40using std::min;
41
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070042namespace android {
43
Lloyd Pique41be5d22018-06-21 13:11:48 -070044DispSync::~DispSync() = default;
45
46namespace impl {
47
Tim Murray4a4e4a22016-04-19 16:29:23 +000048// Setting this to true adds a zero-phase tracer for correlating with hardware
49// vsync events
50static const bool kEnableZeroPhaseTracer = false;
51
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070052// This is the threshold used to determine when hardware vsync events are
53// needed to re-synchronize the software vsync model with the hardware. The
54// error metric used is the mean of the squared difference between each
55// present time and the nearest software-predicted vsync.
Lloyd Pique78ce4182018-01-31 16:39:51 -080056static const nsecs_t kErrorThreshold = 160000000000; // 400 usec squared
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070057
Tim Murray4a4e4a22016-04-19 16:29:23 +000058#undef LOG_TAG
59#define LOG_TAG "DispSyncThread"
Lloyd Pique78ce4182018-01-31 16:39:51 -080060class DispSyncThread : public Thread {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070061public:
Ana Krulec064a82f2018-09-11 16:03:03 -070062 DispSyncThread(const char* name, bool showTraceDetailedInfo)
Lloyd Pique78ce4182018-01-31 16:39:51 -080063 : mName(name),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070064 mStop(false),
65 mPeriod(0),
66 mPhase(0),
Haixia Shi676b1f62015-10-28 16:19:01 -070067 mReferenceTime(0),
Tim Murray4a4e4a22016-04-19 16:29:23 +000068 mWakeupLatency(0),
Ana Krulec064a82f2018-09-11 16:03:03 -070069 mFrameNumber(0),
70 mTraceDetailedInfo(showTraceDetailedInfo) {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070071
72 virtual ~DispSyncThread() {}
73
Haixia Shi676b1f62015-10-28 16:19:01 -070074 void updateModel(nsecs_t period, nsecs_t phase, nsecs_t referenceTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -070075 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070076 Mutex::Autolock lock(mMutex);
77 mPeriod = period;
78 mPhase = phase;
Haixia Shi676b1f62015-10-28 16:19:01 -070079 mReferenceTime = referenceTime;
Tim Murray4a4e4a22016-04-19 16:29:23 +000080 ALOGV("[%s] updateModel: mPeriod = %" PRId64 ", mPhase = %" PRId64
Lloyd Pique78ce4182018-01-31 16:39:51 -080081 " mReferenceTime = %" PRId64,
82 mName, ns2us(mPeriod), ns2us(mPhase), ns2us(mReferenceTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070083 mCond.signal();
84 }
85
86 void stop() {
Ana Krulec064a82f2018-09-11 16:03:03 -070087 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070088 Mutex::Autolock lock(mMutex);
89 mStop = true;
90 mCond.signal();
91 }
92
93 virtual bool threadLoop() {
94 status_t err;
95 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070096
97 while (true) {
Ana Krulec9a52b192018-07-12 18:18:47 -040098 std::vector<CallbackInvocation> callbackInvocations;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070099
100 nsecs_t targetTime = 0;
101
102 { // Scope for lock
103 Mutex::Autolock lock(mMutex);
104
Ana Krulec064a82f2018-09-11 16:03:03 -0700105 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000106 ATRACE_INT64("DispSync:Frame", mFrameNumber);
107 }
108 ALOGV("[%s] Frame %" PRId64, mName, mFrameNumber);
109 ++mFrameNumber;
110
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700111 if (mStop) {
112 return false;
113 }
114
115 if (mPeriod == 0) {
116 err = mCond.wait(mMutex);
117 if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800118 ALOGE("error waiting for new events: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700119 return false;
120 }
121 continue;
122 }
123
Dan Stoza8f8374d2016-04-19 10:03:46 -0700124 targetTime = computeNextEventTimeLocked(now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700125
126 bool isWakeup = false;
127
128 if (now < targetTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700129 if (mTraceDetailedInfo) ATRACE_NAME("DispSync waiting");
Dan Stoza8f8374d2016-04-19 10:03:46 -0700130
131 if (targetTime == INT64_MAX) {
132 ALOGV("[%s] Waiting forever", mName);
133 err = mCond.wait(mMutex);
134 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800135 ALOGV("[%s] Waiting until %" PRId64, mName, ns2us(targetTime));
Dan Stoza8f8374d2016-04-19 10:03:46 -0700136 err = mCond.waitRelative(mMutex, targetTime - now);
137 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700138
139 if (err == TIMED_OUT) {
140 isWakeup = true;
141 } else if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800142 ALOGE("error waiting for next event: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700143 return false;
144 }
145 }
146
147 now = systemTime(SYSTEM_TIME_MONOTONIC);
148
Tim Murray4a4e4a22016-04-19 16:29:23 +0000149 // Don't correct by more than 1.5 ms
150 static const nsecs_t kMaxWakeupLatency = us2ns(1500);
151
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700152 if (isWakeup) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800153 mWakeupLatency = ((mWakeupLatency * 63) + (now - targetTime)) / 64;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000154 mWakeupLatency = min(mWakeupLatency, kMaxWakeupLatency);
Ana Krulec064a82f2018-09-11 16:03:03 -0700155 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000156 ATRACE_INT64("DispSync:WakeupLat", now - targetTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700157 ATRACE_INT64("DispSync:AvgWakeupLat", mWakeupLatency);
158 }
159 }
160
161 callbackInvocations = gatherCallbackInvocationsLocked(now);
162 }
163
164 if (callbackInvocations.size() > 0) {
Andy McFadden645b1f72014-06-10 14:43:32 -0700165 fireCallbackInvocations(callbackInvocations);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700166 }
167 }
168
169 return false;
170 }
171
Lloyd Piquee83f9312018-02-01 12:53:17 -0800172 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700173 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700174 Mutex::Autolock lock(mMutex);
175
176 for (size_t i = 0; i < mEventListeners.size(); i++) {
177 if (mEventListeners[i].mCallback == callback) {
178 return BAD_VALUE;
179 }
180 }
181
182 EventListener listener;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000183 listener.mName = name;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700184 listener.mPhase = phase;
185 listener.mCallback = callback;
Jamie Gennis629b9872013-10-29 13:36:12 -0700186
187 // We want to allow the firstmost future event to fire without
Tim Murray4a4e4a22016-04-19 16:29:23 +0000188 // allowing any past events to fire
Lloyd Pique78ce4182018-01-31 16:39:51 -0800189 listener.mLastEventTime = systemTime() - mPeriod / 2 + mPhase - mWakeupLatency;
Jamie Gennis629b9872013-10-29 13:36:12 -0700190
Ana Krulec9a52b192018-07-12 18:18:47 -0400191 mEventListeners.push_back(listener);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700192
193 mCond.signal();
194
195 return NO_ERROR;
196 }
197
Lloyd Piquee83f9312018-02-01 12:53:17 -0800198 status_t removeEventListener(DispSync::Callback* callback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700199 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700200 Mutex::Autolock lock(mMutex);
201
Ana Krulec9a52b192018-07-12 18:18:47 -0400202 for (std::vector<EventListener>::iterator it = mEventListeners.begin();
203 it != mEventListeners.end(); ++it) {
204 if (it->mCallback == callback) {
205 mEventListeners.erase(it);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700206 mCond.signal();
207 return NO_ERROR;
208 }
209 }
210
211 return BAD_VALUE;
212 }
213
Dan Stoza84d619e2018-03-28 17:07:36 -0700214 status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700215 if (mTraceDetailedInfo) ATRACE_CALL();
Dan Stoza84d619e2018-03-28 17:07:36 -0700216 Mutex::Autolock lock(mMutex);
217
Ana Krulec9a52b192018-07-12 18:18:47 -0400218 for (auto& eventListener : mEventListeners) {
219 if (eventListener.mCallback == callback) {
220 const nsecs_t oldPhase = eventListener.mPhase;
221 eventListener.mPhase = phase;
Dan Stoza84d619e2018-03-28 17:07:36 -0700222
223 // Pretend that the last time this event was handled at the same frame but with the
224 // new offset to allow for a seamless offset change without double-firing or
225 // skipping.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200226 nsecs_t diff = oldPhase - phase;
227 if (diff > mPeriod / 2) {
228 diff -= mPeriod;
229 } else if (diff < -mPeriod / 2) {
230 diff += mPeriod;
231 }
Ana Krulec9a52b192018-07-12 18:18:47 -0400232 eventListener.mLastEventTime -= diff;
Dan Stoza84d619e2018-03-28 17:07:36 -0700233 mCond.signal();
234 return NO_ERROR;
235 }
236 }
237
238 return BAD_VALUE;
239 }
240
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700241private:
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700242 struct EventListener {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000243 const char* mName;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700244 nsecs_t mPhase;
245 nsecs_t mLastEventTime;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800246 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700247 };
248
249 struct CallbackInvocation {
Lloyd Piquee83f9312018-02-01 12:53:17 -0800250 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700251 nsecs_t mEventTime;
252 };
253
254 nsecs_t computeNextEventTimeLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700255 if (mTraceDetailedInfo) ATRACE_CALL();
Tim Murray4a4e4a22016-04-19 16:29:23 +0000256 ALOGV("[%s] computeNextEventTimeLocked", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700257 nsecs_t nextEventTime = INT64_MAX;
258 for (size_t i = 0; i < mEventListeners.size(); i++) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800259 nsecs_t t = computeListenerNextEventTimeLocked(mEventListeners[i], now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700260
261 if (t < nextEventTime) {
262 nextEventTime = t;
263 }
264 }
265
Tim Murray4a4e4a22016-04-19 16:29:23 +0000266 ALOGV("[%s] nextEventTime = %" PRId64, mName, ns2us(nextEventTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700267 return nextEventTime;
268 }
269
Ana Krulec9a52b192018-07-12 18:18:47 -0400270 std::vector<CallbackInvocation> gatherCallbackInvocationsLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700271 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800272 ALOGV("[%s] gatherCallbackInvocationsLocked @ %" PRId64, mName, ns2us(now));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000273
Ana Krulec9a52b192018-07-12 18:18:47 -0400274 std::vector<CallbackInvocation> callbackInvocations;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000275 nsecs_t onePeriodAgo = now - mPeriod;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700276
Ana Krulec9a52b192018-07-12 18:18:47 -0400277 for (auto& eventListener : mEventListeners) {
278 nsecs_t t = computeListenerNextEventTimeLocked(eventListener, onePeriodAgo);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700279
Jamie Gennis0d5c60e2013-10-09 17:49:37 -0700280 if (t < now) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700281 CallbackInvocation ci;
Ana Krulec9a52b192018-07-12 18:18:47 -0400282 ci.mCallback = eventListener.mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700283 ci.mEventTime = t;
Ana Krulec9a52b192018-07-12 18:18:47 -0400284 ALOGV("[%s] [%s] Preparing to fire", mName, eventListener.mName);
285 callbackInvocations.push_back(ci);
286 eventListener.mLastEventTime = t;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700287 }
288 }
289
290 return callbackInvocations;
291 }
292
Lloyd Pique78ce4182018-01-31 16:39:51 -0800293 nsecs_t computeListenerNextEventTimeLocked(const EventListener& listener, nsecs_t baseTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700294 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800295 ALOGV("[%s] [%s] computeListenerNextEventTimeLocked(%" PRId64 ")", mName, listener.mName,
296 ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700297
Tim Murray4a4e4a22016-04-19 16:29:23 +0000298 nsecs_t lastEventTime = listener.mLastEventTime + mWakeupLatency;
299 ALOGV("[%s] lastEventTime: %" PRId64, mName, ns2us(lastEventTime));
300 if (baseTime < lastEventTime) {
301 baseTime = lastEventTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800302 ALOGV("[%s] Clamping baseTime to lastEventTime -> %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700303 }
304
Tim Murray4a4e4a22016-04-19 16:29:23 +0000305 baseTime -= mReferenceTime;
306 ALOGV("[%s] Relative baseTime = %" PRId64, mName, ns2us(baseTime));
307 nsecs_t phase = mPhase + listener.mPhase;
308 ALOGV("[%s] Phase = %" PRId64, mName, ns2us(phase));
309 baseTime -= phase;
310 ALOGV("[%s] baseTime - phase = %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700311
Tim Murray4a4e4a22016-04-19 16:29:23 +0000312 // If our previous time is before the reference (because the reference
313 // has since been updated), the division by mPeriod will truncate
314 // towards zero instead of computing the floor. Since in all cases
315 // before the reference we want the next time to be effectively now, we
316 // set baseTime to -mPeriod so that numPeriods will be -1.
317 // When we add 1 and the phase, we will be at the correct event time for
318 // this period.
319 if (baseTime < 0) {
320 ALOGV("[%s] Correcting negative baseTime", mName);
321 baseTime = -mPeriod;
322 }
323
324 nsecs_t numPeriods = baseTime / mPeriod;
325 ALOGV("[%s] numPeriods = %" PRId64, mName, numPeriods);
326 nsecs_t t = (numPeriods + 1) * mPeriod + phase;
327 ALOGV("[%s] t = %" PRId64, mName, ns2us(t));
328 t += mReferenceTime;
329 ALOGV("[%s] Absolute t = %" PRId64, mName, ns2us(t));
330
331 // Check that it's been slightly more than half a period since the last
332 // event so that we don't accidentally fall into double-rate vsyncs
333 if (t - listener.mLastEventTime < (3 * mPeriod / 5)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700334 t += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000335 ALOGV("[%s] Modifying t -> %" PRId64, mName, ns2us(t));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700336 }
337
Tim Murray4a4e4a22016-04-19 16:29:23 +0000338 t -= mWakeupLatency;
339 ALOGV("[%s] Corrected for wakeup latency -> %" PRId64, mName, ns2us(t));
340
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700341 return t;
342 }
343
Ana Krulec9a52b192018-07-12 18:18:47 -0400344 void fireCallbackInvocations(const std::vector<CallbackInvocation>& callbacks) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700345 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700346 for (size_t i = 0; i < callbacks.size(); i++) {
347 callbacks[i].mCallback->onDispSyncEvent(callbacks[i].mEventTime);
348 }
349 }
350
Tim Murray4a4e4a22016-04-19 16:29:23 +0000351 const char* const mName;
352
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700353 bool mStop;
354
355 nsecs_t mPeriod;
356 nsecs_t mPhase;
Haixia Shi676b1f62015-10-28 16:19:01 -0700357 nsecs_t mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700358 nsecs_t mWakeupLatency;
359
Tim Murray4a4e4a22016-04-19 16:29:23 +0000360 int64_t mFrameNumber;
361
Ana Krulec9a52b192018-07-12 18:18:47 -0400362 std::vector<EventListener> mEventListeners;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700363
364 Mutex mMutex;
365 Condition mCond;
Ana Krulec064a82f2018-09-11 16:03:03 -0700366
367 // Flag to turn on logging in systrace.
368 const bool mTraceDetailedInfo;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700369};
370
Tim Murray4a4e4a22016-04-19 16:29:23 +0000371#undef LOG_TAG
372#define LOG_TAG "DispSync"
373
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700374class ZeroPhaseTracer : public DispSync::Callback {
375public:
376 ZeroPhaseTracer() : mParity(false) {}
377
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700378 virtual void onDispSyncEvent(nsecs_t /*when*/) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700379 mParity = !mParity;
380 ATRACE_INT("ZERO_PHASE_VSYNC", mParity ? 1 : 0);
381 }
382
383private:
384 bool mParity;
385};
386
Ana Krulec064a82f2018-09-11 16:03:03 -0700387DispSync::DispSync(const char* name) : mName(name), mRefreshSkipCount(0) {
388 // This flag offers the ability to turn on systrace logging from the shell.
389 char value[PROPERTY_VALUE_MAX];
390 property_get("debug.sf.dispsync_trace_detailed_info", value, "0");
391 mTraceDetailedInfo = atoi(value);
392 mThread = new DispSyncThread(name, mTraceDetailedInfo);
393}
Andy McFadden645b1f72014-06-10 14:43:32 -0700394
Saurabh Shahf4174532017-07-13 10:45:07 -0700395DispSync::~DispSync() {}
396
397void DispSync::init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset) {
398 mIgnorePresentFences = !hasSyncFramework;
399 mPresentTimeOffset = dispSyncPresentTimeOffset;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700400 mThread->run("DispSync", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
Saurabh Shahf4174532017-07-13 10:45:07 -0700401
Tim Murrayacff43d2016-07-29 13:57:24 -0700402 // set DispSync to SCHED_FIFO to minimize jitter
403 struct sched_param param = {0};
Tim Murray35520632016-09-07 12:18:17 -0700404 param.sched_priority = 2;
Tim Murrayacff43d2016-07-29 13:57:24 -0700405 if (sched_setscheduler(mThread->getTid(), SCHED_FIFO, &param) != 0) {
406 ALOGE("Couldn't set SCHED_FIFO for DispSyncThread");
407 }
408
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700409 reset();
410 beginResync();
411
Ana Krulec064a82f2018-09-11 16:03:03 -0700412 if (mTraceDetailedInfo && kEnableZeroPhaseTracer) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700413 mZeroPhaseTracer = std::make_unique<ZeroPhaseTracer>();
414 addEventListener("ZeroPhaseTracer", 0, mZeroPhaseTracer.get());
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700415 }
416}
417
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700418void DispSync::reset() {
419 Mutex::Autolock lock(mMutex);
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700420 resetLocked();
421}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700422
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700423void DispSync::resetLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700424 mPhase = 0;
425 mReferenceTime = 0;
426 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700427 mNumResyncSamples = 0;
428 mFirstResyncSample = 0;
429 mNumResyncSamplesSincePresent = 0;
430 resetErrorLocked();
431}
432
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700433bool DispSync::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700434 Mutex::Autolock lock(mMutex);
435
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700436 if (mIgnorePresentFences) {
437 return true;
438 }
439
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700440 mPresentFences[mPresentSampleOffset] = fenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700441 mPresentSampleOffset = (mPresentSampleOffset + 1) % NUM_PRESENT_SAMPLES;
442 mNumResyncSamplesSincePresent = 0;
443
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700444 updateErrorLocked();
445
Haixia Shi676b1f62015-10-28 16:19:01 -0700446 return !mModelUpdated || mError > kErrorThreshold;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700447}
448
449void DispSync::beginResync() {
450 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000451 ALOGV("[%s] beginResync", mName);
Haixia Shi676b1f62015-10-28 16:19:01 -0700452 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700453 mNumResyncSamples = 0;
454}
455
456bool DispSync::addResyncSample(nsecs_t timestamp) {
457 Mutex::Autolock lock(mMutex);
458
Tim Murray4a4e4a22016-04-19 16:29:23 +0000459 ALOGV("[%s] addResyncSample(%" PRId64 ")", mName, ns2us(timestamp));
460
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700461 size_t idx = (mFirstResyncSample + mNumResyncSamples) % MAX_RESYNC_SAMPLES;
462 mResyncSamples[idx] = timestamp;
Haixia Shi664339a2015-10-28 13:22:22 -0700463 if (mNumResyncSamples == 0) {
Haixia Shi676b1f62015-10-28 16:19:01 -0700464 mPhase = 0;
465 mReferenceTime = timestamp;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000466 ALOGV("[%s] First resync sample: mPeriod = %" PRId64 ", mPhase = 0, "
Lloyd Pique78ce4182018-01-31 16:39:51 -0800467 "mReferenceTime = %" PRId64,
468 mName, ns2us(mPeriod), ns2us(mReferenceTime));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000469 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Haixia Shi664339a2015-10-28 13:22:22 -0700470 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700471
472 if (mNumResyncSamples < MAX_RESYNC_SAMPLES) {
473 mNumResyncSamples++;
474 } else {
475 mFirstResyncSample = (mFirstResyncSample + 1) % MAX_RESYNC_SAMPLES;
476 }
477
478 updateModelLocked();
479
480 if (mNumResyncSamplesSincePresent++ > MAX_RESYNC_SAMPLES_WITHOUT_PRESENT) {
481 resetErrorLocked();
482 }
483
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800484 if (mIgnorePresentFences) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700485 // If we're ignoring the present fences we have no way to know whether
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700486 // or not we're synchronized with the HW vsyncs, so we just request
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700487 // that the HW vsync events be turned on.
488 return true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700489 }
490
Tim Murray4a4e4a22016-04-19 16:29:23 +0000491 // Check against kErrorThreshold / 2 to add some hysteresis before having to
492 // resync again
493 bool modelLocked = mModelUpdated && mError < (kErrorThreshold / 2);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800494 ALOGV("[%s] addResyncSample returning %s", mName, modelLocked ? "locked" : "unlocked");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000495 return !modelLocked;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700496}
497
Lloyd Pique78ce4182018-01-31 16:39:51 -0800498void DispSync::endResync() {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700499
Lloyd Piquee83f9312018-02-01 12:53:17 -0800500status_t DispSync::addEventListener(const char* name, nsecs_t phase, Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700501 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000502 return mThread->addEventListener(name, phase, callback);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700503}
504
Andy McFadden645b1f72014-06-10 14:43:32 -0700505void DispSync::setRefreshSkipCount(int count) {
506 Mutex::Autolock lock(mMutex);
507 ALOGD("setRefreshSkipCount(%d)", count);
508 mRefreshSkipCount = count;
509 updateModelLocked();
Ruchi Kandoif52b3c82014-04-24 16:42:35 -0700510}
511
Lloyd Piquee83f9312018-02-01 12:53:17 -0800512status_t DispSync::removeEventListener(Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700513 Mutex::Autolock lock(mMutex);
514 return mThread->removeEventListener(callback);
515}
516
Dan Stoza84d619e2018-03-28 17:07:36 -0700517status_t DispSync::changePhaseOffset(Callback* callback, nsecs_t phase) {
518 Mutex::Autolock lock(mMutex);
519 return mThread->changePhaseOffset(callback, phase);
520}
521
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700522void DispSync::setPeriod(nsecs_t period) {
523 Mutex::Autolock lock(mMutex);
David Sodman1afa8b02018-10-15 11:20:48 -0700524 mPeriod = period;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700525 mPhase = 0;
Haixia Shi676b1f62015-10-28 16:19:01 -0700526 mReferenceTime = 0;
527 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700528}
529
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700530nsecs_t DispSync::getPeriod() {
531 // lock mutex as mPeriod changes multiple times in updateModelLocked
532 Mutex::Autolock lock(mMutex);
533 return mPeriod;
534}
535
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700536void DispSync::updateModelLocked() {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000537 ALOGV("[%s] updateModelLocked %zu", mName, mNumResyncSamples);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700538 if (mNumResyncSamples >= MIN_RESYNC_SAMPLES_FOR_UPDATE) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000539 ALOGV("[%s] Computing...", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700540 nsecs_t durationSum = 0;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000541 nsecs_t minDuration = INT64_MAX;
542 nsecs_t maxDuration = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700543 for (size_t i = 1; i < mNumResyncSamples; i++) {
544 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
545 size_t prev = (idx + MAX_RESYNC_SAMPLES - 1) % MAX_RESYNC_SAMPLES;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000546 nsecs_t duration = mResyncSamples[idx] - mResyncSamples[prev];
547 durationSum += duration;
548 minDuration = min(minDuration, duration);
549 maxDuration = max(maxDuration, duration);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700550 }
551
Tim Murray4a4e4a22016-04-19 16:29:23 +0000552 // Exclude the min and max from the average
553 durationSum -= minDuration + maxDuration;
David Sodman1afa8b02018-10-15 11:20:48 -0700554 mPeriod = durationSum / (mNumResyncSamples - 3);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000555
556 ALOGV("[%s] mPeriod = %" PRId64, mName, ns2us(mPeriod));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700557
558 double sampleAvgX = 0;
559 double sampleAvgY = 0;
560 double scale = 2.0 * M_PI / double(mPeriod);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000561 // Intentionally skip the first sample
562 for (size_t i = 1; i < mNumResyncSamples; i++) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700563 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
Haixia Shi676b1f62015-10-28 16:19:01 -0700564 nsecs_t sample = mResyncSamples[idx] - mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700565 double samplePhase = double(sample % mPeriod) * scale;
566 sampleAvgX += cos(samplePhase);
567 sampleAvgY += sin(samplePhase);
568 }
569
Tim Murray4a4e4a22016-04-19 16:29:23 +0000570 sampleAvgX /= double(mNumResyncSamples - 1);
571 sampleAvgY /= double(mNumResyncSamples - 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700572
573 mPhase = nsecs_t(atan2(sampleAvgY, sampleAvgX) / scale);
574
Tim Murray4a4e4a22016-04-19 16:29:23 +0000575 ALOGV("[%s] mPhase = %" PRId64, mName, ns2us(mPhase));
576
577 if (mPhase < -(mPeriod / 2)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700578 mPhase += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000579 ALOGV("[%s] Adjusting mPhase -> %" PRId64, mName, ns2us(mPhase));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700580 }
581
Ana Krulec064a82f2018-09-11 16:03:03 -0700582 if (mTraceDetailedInfo) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700583 ATRACE_INT64("DispSync:Period", mPeriod);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000584 ATRACE_INT64("DispSync:Phase", mPhase + mPeriod / 2);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700585 }
586
Andy McFadden645b1f72014-06-10 14:43:32 -0700587 // Artificially inflate the period if requested.
588 mPeriod += mPeriod * mRefreshSkipCount;
589
Haixia Shi676b1f62015-10-28 16:19:01 -0700590 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
591 mModelUpdated = true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700592 }
593}
594
595void DispSync::updateErrorLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700596 if (!mModelUpdated) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700597 return;
598 }
599
Andy McFadden645b1f72014-06-10 14:43:32 -0700600 // Need to compare present fences against the un-adjusted refresh period,
601 // since they might arrive between two events.
602 nsecs_t period = mPeriod / (1 + mRefreshSkipCount);
603
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700604 int numErrSamples = 0;
605 nsecs_t sqErrSum = 0;
606
607 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700608 // Only check for the cached value of signal time to avoid unecessary
609 // syscalls. It is the responsibility of the DispSync owner to
610 // call getSignalTime() periodically so the cache is updated when the
611 // fence signals.
612 nsecs_t time = mPresentFences[i]->getCachedSignalTime();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800613 if (time == Fence::SIGNAL_TIME_PENDING || time == Fence::SIGNAL_TIME_INVALID) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700614 continue;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700615 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700616
617 nsecs_t sample = time - mReferenceTime;
618 if (sample <= mPhase) {
619 continue;
620 }
621
622 nsecs_t sampleErr = (sample - mPhase) % period;
623 if (sampleErr > period / 2) {
624 sampleErr -= period;
625 }
626 sqErrSum += sampleErr * sampleErr;
627 numErrSamples++;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700628 }
629
630 if (numErrSamples > 0) {
631 mError = sqErrSum / numErrSamples;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700632 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700633 } else {
634 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700635 // Use mod ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT to avoid log spam.
636 mZeroErrSamplesCount++;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800637 ALOGE_IF((mZeroErrSamplesCount % ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT) == 0,
638 "No present times for model error.");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700639 }
640
Ana Krulec064a82f2018-09-11 16:03:03 -0700641 if (mTraceDetailedInfo) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700642 ATRACE_INT64("DispSync:Error", mError);
643 }
644}
645
646void DispSync::resetErrorLocked() {
647 mPresentSampleOffset = 0;
648 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700649 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700650 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700651 mPresentFences[i] = FenceTime::NO_FENCE;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700652 }
653}
654
Andy McFadden41d67d72014-04-25 16:58:34 -0700655nsecs_t DispSync::computeNextRefresh(int periodOffset) const {
Andy McFadden150ecd82014-05-08 14:56:50 -0700656 Mutex::Autolock lock(mMutex);
Andy McFadden41d67d72014-04-25 16:58:34 -0700657 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Haixia Shi676b1f62015-10-28 16:19:01 -0700658 nsecs_t phase = mReferenceTime + mPhase;
659 return (((now - phase) / mPeriod) + periodOffset + 1) * mPeriod + phase;
Andy McFadden41d67d72014-04-25 16:58:34 -0700660}
661
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700662void DispSync::setIgnorePresentFences(bool ignore) {
663 Mutex::Autolock lock(mMutex);
664 if (mIgnorePresentFences != ignore) {
665 mIgnorePresentFences = ignore;
666 resetLocked();
667 }
668}
669
Andy McFaddenc751e922014-05-08 14:53:26 -0700670void DispSync::dump(String8& result) const {
671 Mutex::Autolock lock(mMutex);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800672 result.appendFormat("present fences are %s\n", mIgnorePresentFences ? "ignored" : "used");
673 result.appendFormat("mPeriod: %" PRId64 " ns (%.3f fps; skipCount=%d)\n", mPeriod,
674 1000000000.0 / mPeriod, mRefreshSkipCount);
Andy McFadden5167ec62014-05-22 13:08:43 -0700675 result.appendFormat("mPhase: %" PRId64 " ns\n", mPhase);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800676 result.appendFormat("mError: %" PRId64 " ns (sqrt=%.1f)\n", mError, sqrt(mError));
Andy McFadden5167ec62014-05-22 13:08:43 -0700677 result.appendFormat("mNumResyncSamplesSincePresent: %d (limit %d)\n",
Lloyd Pique78ce4182018-01-31 16:39:51 -0800678 mNumResyncSamplesSincePresent, MAX_RESYNC_SAMPLES_WITHOUT_PRESENT);
679 result.appendFormat("mNumResyncSamples: %zd (max %d)\n", mNumResyncSamples, MAX_RESYNC_SAMPLES);
Andy McFaddenc751e922014-05-08 14:53:26 -0700680
681 result.appendFormat("mResyncSamples:\n");
682 nsecs_t previous = -1;
683 for (size_t i = 0; i < mNumResyncSamples; i++) {
684 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
685 nsecs_t sampleTime = mResyncSamples[idx];
686 if (i == 0) {
Andy McFadden5167ec62014-05-22 13:08:43 -0700687 result.appendFormat(" %" PRId64 "\n", sampleTime);
Andy McFaddenc751e922014-05-08 14:53:26 -0700688 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800689 result.appendFormat(" %" PRId64 " (+%" PRId64 ")\n", sampleTime,
690 sampleTime - previous);
Andy McFaddenc751e922014-05-08 14:53:26 -0700691 }
692 previous = sampleTime;
693 }
694
Lloyd Pique78ce4182018-01-31 16:39:51 -0800695 result.appendFormat("mPresentFences [%d]:\n", NUM_PRESENT_SAMPLES);
Andy McFadden5167ec62014-05-22 13:08:43 -0700696 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700697 previous = Fence::SIGNAL_TIME_INVALID;
Andy McFaddenc751e922014-05-08 14:53:26 -0700698 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
699 size_t idx = (i + mPresentSampleOffset) % NUM_PRESENT_SAMPLES;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700700 nsecs_t presentTime = mPresentFences[idx]->getSignalTime();
701 if (presentTime == Fence::SIGNAL_TIME_PENDING) {
Andy McFaddenc751e922014-05-08 14:53:26 -0700702 result.appendFormat(" [unsignaled fence]\n");
Lloyd Pique78ce4182018-01-31 16:39:51 -0800703 } else if (presentTime == Fence::SIGNAL_TIME_INVALID) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700704 result.appendFormat(" [invalid fence]\n");
705 } else if (previous == Fence::SIGNAL_TIME_PENDING ||
Lloyd Pique78ce4182018-01-31 16:39:51 -0800706 previous == Fence::SIGNAL_TIME_INVALID) {
Andy McFadden5167ec62014-05-22 13:08:43 -0700707 result.appendFormat(" %" PRId64 " (%.3f ms ago)\n", presentTime,
Lloyd Pique78ce4182018-01-31 16:39:51 -0800708 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700709 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800710 result.appendFormat(" %" PRId64 " (+%" PRId64 " / %.3f) (%.3f ms ago)\n", presentTime,
711 presentTime - previous, (presentTime - previous) / (double)mPeriod,
712 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700713 }
714 previous = presentTime;
715 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700716
717 result.appendFormat("current monotonic time: %" PRId64 "\n", now);
Andy McFaddenc751e922014-05-08 14:53:26 -0700718}
719
Ana Krulec010d2192018-10-08 06:29:54 -0700720// TODO(b/113612090): Figure out how much of this is still relevant.
721// We need to determine the time when a buffer acquired now will be
722// displayed. This can be calculated:
723// time when previous buffer's actual-present fence was signaled
724// + current display refresh rate * HWC latency
725// + a little extra padding
726//
727// Buffer producers are expected to set their desired presentation time
728// based on choreographer time stamps, which (coming from vsync events)
729// will be slightly later then the actual-present timing. If we get a
730// desired-present time that is unintentionally a hair after the next
731// vsync, we'll hold the frame when we really want to display it. We
732// need to take the offset between actual-present and reported-vsync
733// into account.
734//
735// If the system is configured without a DispSync phase offset for the app,
736// we also want to throw in a bit of padding to avoid edge cases where we
737// just barely miss. We want to do it here, not in every app. A major
738// source of trouble is the app's use of the display's ideal refresh time
739// (via Display.getRefreshRate()), which could be off of the actual refresh
740// by a few percent, with the error multiplied by the number of frames
741// between now and when the buffer should be displayed.
742//
743// If the refresh reported to the app has a phase offset, we shouldn't need
744// to tweak anything here.
745nsecs_t DispSync::expectedPresentTime() {
746 // The HWC doesn't currently have a way to report additional latency.
747 // Assume that whatever we submit now will appear right after the flip.
748 // For a smart panel this might be 1. This is expressed in frames,
749 // rather than time, because we expect to have a constant frame delay
750 // regardless of the refresh rate.
751 const uint32_t hwcLatency = 0;
752
753 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
754 const nsecs_t nextRefresh = computeNextRefresh(hwcLatency);
755
756 // The DispSync time is already adjusted for the difference between
757 // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so
758 // we don't need to factor that in here. Pad a little to avoid
759 // weird effects if apps might be requesting times right on the edge.
760 nsecs_t extraPadding = 0;
761 if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) {
762 extraPadding = 1000000; // 1ms (6% of 60Hz)
763 }
764
765 return nextRefresh + extraPadding;
766}
767
Lloyd Pique41be5d22018-06-21 13:11:48 -0700768} // namespace impl
769
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700770} // namespace android