blob: 9b2a6fc096fe57c95f61ab16bb04bd45943a9ee8 [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;
Tim Murray4a4e4a22016-04-19 16:29:23 +000081 ALOGV("[%s] updateModel: mPeriod = %" PRId64 ", mPhase = %" PRId64
Lloyd Pique78ce4182018-01-31 16:39:51 -080082 " mReferenceTime = %" PRId64,
83 mName, ns2us(mPeriod), ns2us(mPhase), ns2us(mReferenceTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070084 mCond.signal();
85 }
86
87 void stop() {
Ana Krulec064a82f2018-09-11 16:03:03 -070088 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070089 Mutex::Autolock lock(mMutex);
90 mStop = true;
91 mCond.signal();
92 }
93
94 virtual bool threadLoop() {
95 status_t err;
96 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070097
98 while (true) {
Ana Krulec9a52b192018-07-12 18:18:47 -040099 std::vector<CallbackInvocation> callbackInvocations;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700100
101 nsecs_t targetTime = 0;
102
103 { // Scope for lock
104 Mutex::Autolock lock(mMutex);
105
Ana Krulec064a82f2018-09-11 16:03:03 -0700106 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000107 ATRACE_INT64("DispSync:Frame", mFrameNumber);
108 }
109 ALOGV("[%s] Frame %" PRId64, mName, mFrameNumber);
110 ++mFrameNumber;
111
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700112 if (mStop) {
113 return false;
114 }
115
116 if (mPeriod == 0) {
117 err = mCond.wait(mMutex);
118 if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800119 ALOGE("error waiting for new events: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700120 return false;
121 }
122 continue;
123 }
124
Dan Stoza8f8374d2016-04-19 10:03:46 -0700125 targetTime = computeNextEventTimeLocked(now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700126
127 bool isWakeup = false;
128
129 if (now < targetTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700130 if (mTraceDetailedInfo) ATRACE_NAME("DispSync waiting");
Dan Stoza8f8374d2016-04-19 10:03:46 -0700131
132 if (targetTime == INT64_MAX) {
133 ALOGV("[%s] Waiting forever", mName);
134 err = mCond.wait(mMutex);
135 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800136 ALOGV("[%s] Waiting until %" PRId64, mName, ns2us(targetTime));
Dan Stoza8f8374d2016-04-19 10:03:46 -0700137 err = mCond.waitRelative(mMutex, targetTime - now);
138 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700139
140 if (err == TIMED_OUT) {
141 isWakeup = true;
142 } else if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800143 ALOGE("error waiting for next event: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700144 return false;
145 }
146 }
147
148 now = systemTime(SYSTEM_TIME_MONOTONIC);
149
Tim Murray4a4e4a22016-04-19 16:29:23 +0000150 // Don't correct by more than 1.5 ms
151 static const nsecs_t kMaxWakeupLatency = us2ns(1500);
152
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700153 if (isWakeup) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800154 mWakeupLatency = ((mWakeupLatency * 63) + (now - targetTime)) / 64;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000155 mWakeupLatency = min(mWakeupLatency, kMaxWakeupLatency);
Ana Krulec064a82f2018-09-11 16:03:03 -0700156 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000157 ATRACE_INT64("DispSync:WakeupLat", now - targetTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700158 ATRACE_INT64("DispSync:AvgWakeupLat", mWakeupLatency);
159 }
160 }
161
162 callbackInvocations = gatherCallbackInvocationsLocked(now);
163 }
164
165 if (callbackInvocations.size() > 0) {
Andy McFadden645b1f72014-06-10 14:43:32 -0700166 fireCallbackInvocations(callbackInvocations);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700167 }
168 }
169
170 return false;
171 }
172
Lloyd Piquee83f9312018-02-01 12:53:17 -0800173 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700174 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700175 Mutex::Autolock lock(mMutex);
176
177 for (size_t i = 0; i < mEventListeners.size(); i++) {
178 if (mEventListeners[i].mCallback == callback) {
179 return BAD_VALUE;
180 }
181 }
182
183 EventListener listener;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000184 listener.mName = name;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700185 listener.mPhase = phase;
186 listener.mCallback = callback;
Jamie Gennis629b9872013-10-29 13:36:12 -0700187
188 // We want to allow the firstmost future event to fire without
Tim Murray4a4e4a22016-04-19 16:29:23 +0000189 // allowing any past events to fire
Lloyd Pique78ce4182018-01-31 16:39:51 -0800190 listener.mLastEventTime = systemTime() - mPeriod / 2 + mPhase - mWakeupLatency;
Jamie Gennis629b9872013-10-29 13:36:12 -0700191
Ana Krulec9a52b192018-07-12 18:18:47 -0400192 mEventListeners.push_back(listener);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700193
194 mCond.signal();
195
196 return NO_ERROR;
197 }
198
Lloyd Piquee83f9312018-02-01 12:53:17 -0800199 status_t removeEventListener(DispSync::Callback* callback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700200 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700201 Mutex::Autolock lock(mMutex);
202
Ana Krulec9a52b192018-07-12 18:18:47 -0400203 for (std::vector<EventListener>::iterator it = mEventListeners.begin();
204 it != mEventListeners.end(); ++it) {
205 if (it->mCallback == callback) {
206 mEventListeners.erase(it);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700207 mCond.signal();
208 return NO_ERROR;
209 }
210 }
211
212 return BAD_VALUE;
213 }
214
Dan Stoza84d619e2018-03-28 17:07:36 -0700215 status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700216 if (mTraceDetailedInfo) ATRACE_CALL();
Dan Stoza84d619e2018-03-28 17:07:36 -0700217 Mutex::Autolock lock(mMutex);
218
Ana Krulec9a52b192018-07-12 18:18:47 -0400219 for (auto& eventListener : mEventListeners) {
220 if (eventListener.mCallback == callback) {
221 const nsecs_t oldPhase = eventListener.mPhase;
222 eventListener.mPhase = phase;
Dan Stoza84d619e2018-03-28 17:07:36 -0700223
224 // Pretend that the last time this event was handled at the same frame but with the
225 // new offset to allow for a seamless offset change without double-firing or
226 // skipping.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200227 nsecs_t diff = oldPhase - phase;
228 if (diff > mPeriod / 2) {
229 diff -= mPeriod;
230 } else if (diff < -mPeriod / 2) {
231 diff += mPeriod;
232 }
Ana Krulec9a52b192018-07-12 18:18:47 -0400233 eventListener.mLastEventTime -= diff;
Dan Stoza84d619e2018-03-28 17:07:36 -0700234 mCond.signal();
235 return NO_ERROR;
236 }
237 }
238
239 return BAD_VALUE;
240 }
241
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700242private:
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700243 struct EventListener {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000244 const char* mName;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700245 nsecs_t mPhase;
246 nsecs_t mLastEventTime;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800247 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700248 };
249
250 struct CallbackInvocation {
Lloyd Piquee83f9312018-02-01 12:53:17 -0800251 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700252 nsecs_t mEventTime;
253 };
254
255 nsecs_t computeNextEventTimeLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700256 if (mTraceDetailedInfo) ATRACE_CALL();
Tim Murray4a4e4a22016-04-19 16:29:23 +0000257 ALOGV("[%s] computeNextEventTimeLocked", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700258 nsecs_t nextEventTime = INT64_MAX;
259 for (size_t i = 0; i < mEventListeners.size(); i++) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800260 nsecs_t t = computeListenerNextEventTimeLocked(mEventListeners[i], now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700261
262 if (t < nextEventTime) {
263 nextEventTime = t;
264 }
265 }
266
Tim Murray4a4e4a22016-04-19 16:29:23 +0000267 ALOGV("[%s] nextEventTime = %" PRId64, mName, ns2us(nextEventTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700268 return nextEventTime;
269 }
270
Ana Krulec9a52b192018-07-12 18:18:47 -0400271 std::vector<CallbackInvocation> gatherCallbackInvocationsLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700272 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800273 ALOGV("[%s] gatherCallbackInvocationsLocked @ %" PRId64, mName, ns2us(now));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000274
Ana Krulec9a52b192018-07-12 18:18:47 -0400275 std::vector<CallbackInvocation> callbackInvocations;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000276 nsecs_t onePeriodAgo = now - mPeriod;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700277
Ana Krulec9a52b192018-07-12 18:18:47 -0400278 for (auto& eventListener : mEventListeners) {
279 nsecs_t t = computeListenerNextEventTimeLocked(eventListener, onePeriodAgo);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700280
Jamie Gennis0d5c60e2013-10-09 17:49:37 -0700281 if (t < now) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700282 CallbackInvocation ci;
Ana Krulec9a52b192018-07-12 18:18:47 -0400283 ci.mCallback = eventListener.mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700284 ci.mEventTime = t;
Ana Krulec9a52b192018-07-12 18:18:47 -0400285 ALOGV("[%s] [%s] Preparing to fire", mName, eventListener.mName);
286 callbackInvocations.push_back(ci);
287 eventListener.mLastEventTime = t;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700288 }
289 }
290
291 return callbackInvocations;
292 }
293
Lloyd Pique78ce4182018-01-31 16:39:51 -0800294 nsecs_t computeListenerNextEventTimeLocked(const EventListener& listener, nsecs_t baseTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700295 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800296 ALOGV("[%s] [%s] computeListenerNextEventTimeLocked(%" PRId64 ")", mName, listener.mName,
297 ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700298
Tim Murray4a4e4a22016-04-19 16:29:23 +0000299 nsecs_t lastEventTime = listener.mLastEventTime + mWakeupLatency;
300 ALOGV("[%s] lastEventTime: %" PRId64, mName, ns2us(lastEventTime));
301 if (baseTime < lastEventTime) {
302 baseTime = lastEventTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800303 ALOGV("[%s] Clamping baseTime to lastEventTime -> %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700304 }
305
Tim Murray4a4e4a22016-04-19 16:29:23 +0000306 baseTime -= mReferenceTime;
307 ALOGV("[%s] Relative baseTime = %" PRId64, mName, ns2us(baseTime));
308 nsecs_t phase = mPhase + listener.mPhase;
309 ALOGV("[%s] Phase = %" PRId64, mName, ns2us(phase));
310 baseTime -= phase;
311 ALOGV("[%s] baseTime - phase = %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700312
Tim Murray4a4e4a22016-04-19 16:29:23 +0000313 // If our previous time is before the reference (because the reference
314 // has since been updated), the division by mPeriod will truncate
315 // towards zero instead of computing the floor. Since in all cases
316 // before the reference we want the next time to be effectively now, we
317 // set baseTime to -mPeriod so that numPeriods will be -1.
318 // When we add 1 and the phase, we will be at the correct event time for
319 // this period.
320 if (baseTime < 0) {
321 ALOGV("[%s] Correcting negative baseTime", mName);
322 baseTime = -mPeriod;
323 }
324
325 nsecs_t numPeriods = baseTime / mPeriod;
326 ALOGV("[%s] numPeriods = %" PRId64, mName, numPeriods);
327 nsecs_t t = (numPeriods + 1) * mPeriod + phase;
328 ALOGV("[%s] t = %" PRId64, mName, ns2us(t));
329 t += mReferenceTime;
330 ALOGV("[%s] Absolute t = %" PRId64, mName, ns2us(t));
331
332 // Check that it's been slightly more than half a period since the last
333 // event so that we don't accidentally fall into double-rate vsyncs
334 if (t - listener.mLastEventTime < (3 * mPeriod / 5)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700335 t += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000336 ALOGV("[%s] Modifying t -> %" PRId64, mName, ns2us(t));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700337 }
338
Tim Murray4a4e4a22016-04-19 16:29:23 +0000339 t -= mWakeupLatency;
340 ALOGV("[%s] Corrected for wakeup latency -> %" PRId64, mName, ns2us(t));
341
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700342 return t;
343 }
344
Ana Krulec9a52b192018-07-12 18:18:47 -0400345 void fireCallbackInvocations(const std::vector<CallbackInvocation>& callbacks) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700346 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700347 for (size_t i = 0; i < callbacks.size(); i++) {
348 callbacks[i].mCallback->onDispSyncEvent(callbacks[i].mEventTime);
349 }
350 }
351
Tim Murray4a4e4a22016-04-19 16:29:23 +0000352 const char* const mName;
353
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700354 bool mStop;
355
356 nsecs_t mPeriod;
357 nsecs_t mPhase;
Haixia Shi676b1f62015-10-28 16:19:01 -0700358 nsecs_t mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700359 nsecs_t mWakeupLatency;
360
Tim Murray4a4e4a22016-04-19 16:29:23 +0000361 int64_t mFrameNumber;
362
Ana Krulec9a52b192018-07-12 18:18:47 -0400363 std::vector<EventListener> mEventListeners;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700364
365 Mutex mMutex;
366 Condition mCond;
Ana Krulec064a82f2018-09-11 16:03:03 -0700367
368 // Flag to turn on logging in systrace.
369 const bool mTraceDetailedInfo;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700370};
371
Tim Murray4a4e4a22016-04-19 16:29:23 +0000372#undef LOG_TAG
373#define LOG_TAG "DispSync"
374
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700375class ZeroPhaseTracer : public DispSync::Callback {
376public:
377 ZeroPhaseTracer() : mParity(false) {}
378
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700379 virtual void onDispSyncEvent(nsecs_t /*when*/) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700380 mParity = !mParity;
381 ATRACE_INT("ZERO_PHASE_VSYNC", mParity ? 1 : 0);
382 }
383
384private:
385 bool mParity;
386};
387
Ana Krulec064a82f2018-09-11 16:03:03 -0700388DispSync::DispSync(const char* name) : mName(name), mRefreshSkipCount(0) {
389 // This flag offers the ability to turn on systrace logging from the shell.
390 char value[PROPERTY_VALUE_MAX];
391 property_get("debug.sf.dispsync_trace_detailed_info", value, "0");
392 mTraceDetailedInfo = atoi(value);
393 mThread = new DispSyncThread(name, mTraceDetailedInfo);
394}
Andy McFadden645b1f72014-06-10 14:43:32 -0700395
Saurabh Shahf4174532017-07-13 10:45:07 -0700396DispSync::~DispSync() {}
397
398void DispSync::init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset) {
399 mIgnorePresentFences = !hasSyncFramework;
400 mPresentTimeOffset = dispSyncPresentTimeOffset;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700401 mThread->run("DispSync", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
Saurabh Shahf4174532017-07-13 10:45:07 -0700402
Tim Murrayacff43d2016-07-29 13:57:24 -0700403 // set DispSync to SCHED_FIFO to minimize jitter
404 struct sched_param param = {0};
Tim Murray35520632016-09-07 12:18:17 -0700405 param.sched_priority = 2;
Tim Murrayacff43d2016-07-29 13:57:24 -0700406 if (sched_setscheduler(mThread->getTid(), SCHED_FIFO, &param) != 0) {
407 ALOGE("Couldn't set SCHED_FIFO for DispSyncThread");
408 }
409
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700410 reset();
411 beginResync();
412
Ana Krulec064a82f2018-09-11 16:03:03 -0700413 if (mTraceDetailedInfo && kEnableZeroPhaseTracer) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700414 mZeroPhaseTracer = std::make_unique<ZeroPhaseTracer>();
415 addEventListener("ZeroPhaseTracer", 0, mZeroPhaseTracer.get());
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700416 }
417}
418
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700419void DispSync::reset() {
420 Mutex::Autolock lock(mMutex);
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700421 resetLocked();
422}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700423
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700424void DispSync::resetLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700425 mPhase = 0;
426 mReferenceTime = 0;
427 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700428 mNumResyncSamples = 0;
429 mFirstResyncSample = 0;
430 mNumResyncSamplesSincePresent = 0;
431 resetErrorLocked();
432}
433
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700434bool DispSync::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700435 Mutex::Autolock lock(mMutex);
436
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700437 if (mIgnorePresentFences) {
438 return true;
439 }
440
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700441 mPresentFences[mPresentSampleOffset] = fenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700442 mPresentSampleOffset = (mPresentSampleOffset + 1) % NUM_PRESENT_SAMPLES;
443 mNumResyncSamplesSincePresent = 0;
444
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700445 updateErrorLocked();
446
Haixia Shi676b1f62015-10-28 16:19:01 -0700447 return !mModelUpdated || mError > kErrorThreshold;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700448}
449
450void DispSync::beginResync() {
451 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000452 ALOGV("[%s] beginResync", mName);
Haixia Shi676b1f62015-10-28 16:19:01 -0700453 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700454 mNumResyncSamples = 0;
455}
456
457bool DispSync::addResyncSample(nsecs_t timestamp) {
458 Mutex::Autolock lock(mMutex);
459
Tim Murray4a4e4a22016-04-19 16:29:23 +0000460 ALOGV("[%s] addResyncSample(%" PRId64 ")", mName, ns2us(timestamp));
461
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700462 size_t idx = (mFirstResyncSample + mNumResyncSamples) % MAX_RESYNC_SAMPLES;
463 mResyncSamples[idx] = timestamp;
Haixia Shi664339a2015-10-28 13:22:22 -0700464 if (mNumResyncSamples == 0) {
Haixia Shi676b1f62015-10-28 16:19:01 -0700465 mPhase = 0;
466 mReferenceTime = timestamp;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000467 ALOGV("[%s] First resync sample: mPeriod = %" PRId64 ", mPhase = 0, "
Lloyd Pique78ce4182018-01-31 16:39:51 -0800468 "mReferenceTime = %" PRId64,
469 mName, ns2us(mPeriod), ns2us(mReferenceTime));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000470 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Haixia Shi664339a2015-10-28 13:22:22 -0700471 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700472
473 if (mNumResyncSamples < MAX_RESYNC_SAMPLES) {
474 mNumResyncSamples++;
475 } else {
476 mFirstResyncSample = (mFirstResyncSample + 1) % MAX_RESYNC_SAMPLES;
477 }
478
479 updateModelLocked();
480
481 if (mNumResyncSamplesSincePresent++ > MAX_RESYNC_SAMPLES_WITHOUT_PRESENT) {
482 resetErrorLocked();
483 }
484
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800485 if (mIgnorePresentFences) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700486 // If we're ignoring the present fences we have no way to know whether
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700487 // or not we're synchronized with the HW vsyncs, so we just request
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700488 // that the HW vsync events be turned on.
489 return true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700490 }
491
Tim Murray4a4e4a22016-04-19 16:29:23 +0000492 // Check against kErrorThreshold / 2 to add some hysteresis before having to
493 // resync again
494 bool modelLocked = mModelUpdated && mError < (kErrorThreshold / 2);
Lloyd Pique78ce4182018-01-31 16:39:51 -0800495 ALOGV("[%s] addResyncSample returning %s", mName, modelLocked ? "locked" : "unlocked");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000496 return !modelLocked;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700497}
498
Lloyd Pique78ce4182018-01-31 16:39:51 -0800499void DispSync::endResync() {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700500
Lloyd Piquee83f9312018-02-01 12:53:17 -0800501status_t DispSync::addEventListener(const char* name, nsecs_t phase, Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700502 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000503 return mThread->addEventListener(name, phase, callback);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700504}
505
Andy McFadden645b1f72014-06-10 14:43:32 -0700506void DispSync::setRefreshSkipCount(int count) {
507 Mutex::Autolock lock(mMutex);
508 ALOGD("setRefreshSkipCount(%d)", count);
509 mRefreshSkipCount = count;
510 updateModelLocked();
Ruchi Kandoif52b3c82014-04-24 16:42:35 -0700511}
512
Lloyd Piquee83f9312018-02-01 12:53:17 -0800513status_t DispSync::removeEventListener(Callback* callback) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700514 Mutex::Autolock lock(mMutex);
515 return mThread->removeEventListener(callback);
516}
517
Dan Stoza84d619e2018-03-28 17:07:36 -0700518status_t DispSync::changePhaseOffset(Callback* callback, nsecs_t phase) {
519 Mutex::Autolock lock(mMutex);
520 return mThread->changePhaseOffset(callback, phase);
521}
522
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700523void DispSync::setPeriod(nsecs_t period) {
524 Mutex::Autolock lock(mMutex);
David Sodman1afa8b02018-10-15 11:20:48 -0700525 mPeriod = period;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700526 mPhase = 0;
Haixia Shi676b1f62015-10-28 16:19:01 -0700527 mReferenceTime = 0;
528 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700529}
530
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700531nsecs_t DispSync::getPeriod() {
532 // lock mutex as mPeriod changes multiple times in updateModelLocked
533 Mutex::Autolock lock(mMutex);
534 return mPeriod;
535}
536
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700537void DispSync::updateModelLocked() {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000538 ALOGV("[%s] updateModelLocked %zu", mName, mNumResyncSamples);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700539 if (mNumResyncSamples >= MIN_RESYNC_SAMPLES_FOR_UPDATE) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000540 ALOGV("[%s] Computing...", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700541 nsecs_t durationSum = 0;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000542 nsecs_t minDuration = INT64_MAX;
543 nsecs_t maxDuration = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700544 for (size_t i = 1; i < mNumResyncSamples; i++) {
545 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
546 size_t prev = (idx + MAX_RESYNC_SAMPLES - 1) % MAX_RESYNC_SAMPLES;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000547 nsecs_t duration = mResyncSamples[idx] - mResyncSamples[prev];
548 durationSum += duration;
549 minDuration = min(minDuration, duration);
550 maxDuration = max(maxDuration, duration);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700551 }
552
Tim Murray4a4e4a22016-04-19 16:29:23 +0000553 // Exclude the min and max from the average
554 durationSum -= minDuration + maxDuration;
David Sodman1afa8b02018-10-15 11:20:48 -0700555 mPeriod = durationSum / (mNumResyncSamples - 3);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000556
557 ALOGV("[%s] mPeriod = %" PRId64, mName, ns2us(mPeriod));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700558
559 double sampleAvgX = 0;
560 double sampleAvgY = 0;
561 double scale = 2.0 * M_PI / double(mPeriod);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000562 // Intentionally skip the first sample
563 for (size_t i = 1; i < mNumResyncSamples; i++) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700564 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
Haixia Shi676b1f62015-10-28 16:19:01 -0700565 nsecs_t sample = mResyncSamples[idx] - mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700566 double samplePhase = double(sample % mPeriod) * scale;
567 sampleAvgX += cos(samplePhase);
568 sampleAvgY += sin(samplePhase);
569 }
570
Tim Murray4a4e4a22016-04-19 16:29:23 +0000571 sampleAvgX /= double(mNumResyncSamples - 1);
572 sampleAvgY /= double(mNumResyncSamples - 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700573
574 mPhase = nsecs_t(atan2(sampleAvgY, sampleAvgX) / scale);
575
Tim Murray4a4e4a22016-04-19 16:29:23 +0000576 ALOGV("[%s] mPhase = %" PRId64, mName, ns2us(mPhase));
577
578 if (mPhase < -(mPeriod / 2)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700579 mPhase += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000580 ALOGV("[%s] Adjusting mPhase -> %" PRId64, mName, ns2us(mPhase));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700581 }
582
Ana Krulec064a82f2018-09-11 16:03:03 -0700583 if (mTraceDetailedInfo) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700584 ATRACE_INT64("DispSync:Period", mPeriod);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000585 ATRACE_INT64("DispSync:Phase", mPhase + mPeriod / 2);
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).
760 const nsecs_t nextRefresh = computeNextRefresh(hwcLatency);
761
762 // The DispSync time is already adjusted for the difference between
763 // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so
764 // we don't need to factor that in here. Pad a little to avoid
765 // weird effects if apps might be requesting times right on the edge.
766 nsecs_t extraPadding = 0;
767 if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) {
768 extraPadding = 1000000; // 1ms (6% of 60Hz)
769 }
770
771 return nextRefresh + extraPadding;
772}
773
Lloyd Pique41be5d22018-06-21 13:11:48 -0700774} // namespace impl
775
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700776} // namespace android