blob: 871f556988ec64c94b125aab4c0ffb90d4a8a98e [file] [log] [blame]
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Tim Murray4a4e4a22016-04-19 16:29:23 +000018//#define LOG_NDEBUG 0
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070019
20// This is needed for stdint.h to define INT64_MAX in C++
21#define __STDC_LIMIT_MACROS
22
23#include <math.h>
24
Mark Salyzyna5e161b2016-09-29 08:08:05 -070025#include <algorithm>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070026
Yiwei Zhang5434a782018-12-05 18:06:32 -080027#include <android-base/stringprintf.h>
David Sodman1afa8b02018-10-15 11:20:48 -070028#include <cutils/properties.h>
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <log/log.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070030#include <utils/Thread.h>
31#include <utils/Trace.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070032
Brian Andersonfbc80ae2017-05-26 16:23:54 -070033#include <ui/FenceTime.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070034
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070035#include "DispSync.h"
36#include "EventLog/EventLog.h"
Lloyd Pique78ce4182018-01-31 16:39:51 -080037#include "SurfaceFlinger.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070038
Yiwei Zhang5434a782018-12-05 18:06:32 -080039using android::base::StringAppendF;
Tim Murray4a4e4a22016-04-19 16:29:23 +000040using std::max;
41using std::min;
42
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070043namespace android {
44
Lloyd Pique41be5d22018-06-21 13:11:48 -070045DispSync::~DispSync() = default;
Kevin DuBois7b743be2019-02-27 10:05:48 -080046DispSync::Callback::~Callback() = default;
Lloyd Pique41be5d22018-06-21 13:11:48 -070047
48namespace impl {
49
Tim Murray4a4e4a22016-04-19 16:29:23 +000050// Setting this to true adds a zero-phase tracer for correlating with hardware
51// vsync events
52static const bool kEnableZeroPhaseTracer = false;
53
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070054// This is the threshold used to determine when hardware vsync events are
55// needed to re-synchronize the software vsync model with the hardware. The
56// error metric used is the mean of the squared difference between each
57// present time and the nearest software-predicted vsync.
Lloyd Pique78ce4182018-01-31 16:39:51 -080058static const nsecs_t kErrorThreshold = 160000000000; // 400 usec squared
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070059
Tim Murray4a4e4a22016-04-19 16:29:23 +000060#undef LOG_TAG
61#define LOG_TAG "DispSyncThread"
Lloyd Pique78ce4182018-01-31 16:39:51 -080062class DispSyncThread : public Thread {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070063public:
Ana Krulec064a82f2018-09-11 16:03:03 -070064 DispSyncThread(const char* name, bool showTraceDetailedInfo)
Lloyd Pique78ce4182018-01-31 16:39:51 -080065 : mName(name),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070066 mStop(false),
67 mPeriod(0),
68 mPhase(0),
Haixia Shi676b1f62015-10-28 16:19:01 -070069 mReferenceTime(0),
Tim Murray4a4e4a22016-04-19 16:29:23 +000070 mWakeupLatency(0),
Ana Krulec064a82f2018-09-11 16:03:03 -070071 mFrameNumber(0),
72 mTraceDetailedInfo(showTraceDetailedInfo) {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070073
74 virtual ~DispSyncThread() {}
75
Haixia Shi676b1f62015-10-28 16:19:01 -070076 void updateModel(nsecs_t period, nsecs_t phase, nsecs_t referenceTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -070077 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070078 Mutex::Autolock lock(mMutex);
Alec Mouri75b0b622019-03-12 18:56:00 +000079
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070080 mPhase = phase;
Haixia Shi676b1f62015-10-28 16:19:01 -070081 mReferenceTime = referenceTime;
Alec Mouri75b0b622019-03-12 18:56:00 +000082 if (mPeriod != 0 && mPeriod != period && mReferenceTime != 0) {
83 // Inflate the reference time to be the most recent predicted
84 // vsync before the current time.
85 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
86 const nsecs_t baseTime = now - mReferenceTime;
87 const nsecs_t numOldPeriods = baseTime / mPeriod;
88 mReferenceTime = mReferenceTime + (numOldPeriods)*mPeriod;
89 }
90 mPeriod = period;
Alec Mourif5fe85c2019-02-22 13:40:36 -080091 if (mTraceDetailedInfo) {
92 ATRACE_INT64("DispSync:Period", mPeriod);
93 ATRACE_INT64("DispSync:Phase", mPhase + mPeriod / 2);
94 ATRACE_INT64("DispSync:Reference Time", mReferenceTime);
95 }
Tim Murray4a4e4a22016-04-19 16:29:23 +000096 ALOGV("[%s] updateModel: mPeriod = %" PRId64 ", mPhase = %" PRId64
Lloyd Pique78ce4182018-01-31 16:39:51 -080097 " mReferenceTime = %" PRId64,
98 mName, ns2us(mPeriod), ns2us(mPhase), ns2us(mReferenceTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070099 mCond.signal();
100 }
101
102 void stop() {
Ana Krulec064a82f2018-09-11 16:03:03 -0700103 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700104 Mutex::Autolock lock(mMutex);
105 mStop = true;
106 mCond.signal();
107 }
108
109 virtual bool threadLoop() {
110 status_t err;
111 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700112
113 while (true) {
Ana Krulec9a52b192018-07-12 18:18:47 -0400114 std::vector<CallbackInvocation> callbackInvocations;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700115
116 nsecs_t targetTime = 0;
117
118 { // Scope for lock
119 Mutex::Autolock lock(mMutex);
120
Ana Krulec064a82f2018-09-11 16:03:03 -0700121 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000122 ATRACE_INT64("DispSync:Frame", mFrameNumber);
123 }
124 ALOGV("[%s] Frame %" PRId64, mName, mFrameNumber);
125 ++mFrameNumber;
126
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700127 if (mStop) {
128 return false;
129 }
130
131 if (mPeriod == 0) {
132 err = mCond.wait(mMutex);
133 if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800134 ALOGE("error waiting for new events: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700135 return false;
136 }
137 continue;
138 }
139
Dan Stoza8f8374d2016-04-19 10:03:46 -0700140 targetTime = computeNextEventTimeLocked(now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700141
142 bool isWakeup = false;
143
144 if (now < targetTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700145 if (mTraceDetailedInfo) ATRACE_NAME("DispSync waiting");
Dan Stoza8f8374d2016-04-19 10:03:46 -0700146
147 if (targetTime == INT64_MAX) {
148 ALOGV("[%s] Waiting forever", mName);
149 err = mCond.wait(mMutex);
150 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800151 ALOGV("[%s] Waiting until %" PRId64, mName, ns2us(targetTime));
Dan Stoza8f8374d2016-04-19 10:03:46 -0700152 err = mCond.waitRelative(mMutex, targetTime - now);
153 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700154
155 if (err == TIMED_OUT) {
156 isWakeup = true;
157 } else if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800158 ALOGE("error waiting for next event: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700159 return false;
160 }
161 }
162
163 now = systemTime(SYSTEM_TIME_MONOTONIC);
164
Tim Murray4a4e4a22016-04-19 16:29:23 +0000165 // Don't correct by more than 1.5 ms
166 static const nsecs_t kMaxWakeupLatency = us2ns(1500);
167
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700168 if (isWakeup) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800169 mWakeupLatency = ((mWakeupLatency * 63) + (now - targetTime)) / 64;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000170 mWakeupLatency = min(mWakeupLatency, kMaxWakeupLatency);
Ana Krulec064a82f2018-09-11 16:03:03 -0700171 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000172 ATRACE_INT64("DispSync:WakeupLat", now - targetTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700173 ATRACE_INT64("DispSync:AvgWakeupLat", mWakeupLatency);
174 }
175 }
176
177 callbackInvocations = gatherCallbackInvocationsLocked(now);
178 }
179
180 if (callbackInvocations.size() > 0) {
Andy McFadden645b1f72014-06-10 14:43:32 -0700181 fireCallbackInvocations(callbackInvocations);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700182 }
183 }
184
185 return false;
186 }
187
Alec Mouri7355eb22019-03-05 14:19:10 -0800188 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback,
189 nsecs_t lastCallbackTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700190 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700191 Mutex::Autolock lock(mMutex);
192
193 for (size_t i = 0; i < mEventListeners.size(); i++) {
194 if (mEventListeners[i].mCallback == callback) {
195 return BAD_VALUE;
196 }
197 }
198
199 EventListener listener;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000200 listener.mName = name;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700201 listener.mPhase = phase;
202 listener.mCallback = callback;
Jamie Gennis629b9872013-10-29 13:36:12 -0700203
204 // We want to allow the firstmost future event to fire without
Alec Mouri81835982019-02-27 13:40:44 -0800205 // allowing any past events to fire. To do this extrapolate from
206 // mReferenceTime the most recent hardware vsync, and pin the
207 // last event time there.
208 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
209 if (mPeriod != 0) {
210 const nsecs_t baseTime = now - mReferenceTime;
211 const nsecs_t numPeriodsSinceReference = baseTime / mPeriod;
212 const nsecs_t predictedReference = mReferenceTime + numPeriodsSinceReference * mPeriod;
Alec Mouri754c98a2019-03-18 18:53:42 -0700213 listener.mLastEventTime = predictedReference + mPhase + listener.mPhase;
Alec Mouri6db6d622019-03-19 18:42:03 -0700214 // If we're very close in time to the predicted last event time,
Alec Mouri7e3aa762019-04-10 11:27:54 -0700215 // and we're not very close to the next predicted last event time
Alec Mouri6db6d622019-03-19 18:42:03 -0700216 // then we need to back up the last event time so that we can
217 // attempt to fire an event immediately.
218 //
Alec Mouri7e3aa762019-04-10 11:27:54 -0700219 // Otherwise, keep the last event time that we predicted so that
220 // we don't wake up early.
221 if (isShorterThanPeriod(now - listener.mLastEventTime) &&
222 !isShorterThanPeriod(listener.mLastEventTime + mPeriod - now)) {
Alec Mouri6db6d622019-03-19 18:42:03 -0700223 listener.mLastEventTime -= mPeriod;
Alec Mouri81835982019-02-27 13:40:44 -0800224 }
225 } else {
226 listener.mLastEventTime = now + mPhase - mWakeupLatency;
227 }
Jamie Gennis629b9872013-10-29 13:36:12 -0700228
Alec Mouri7355eb22019-03-05 14:19:10 -0800229 if (lastCallbackTime <= 0) {
230 // If there is no prior callback time, try to infer one based on the
231 // logical last event time.
232 listener.mLastCallbackTime = listener.mLastEventTime + mWakeupLatency;
233 } else {
234 listener.mLastCallbackTime = lastCallbackTime;
235 }
236
Ana Krulec9a52b192018-07-12 18:18:47 -0400237 mEventListeners.push_back(listener);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700238
239 mCond.signal();
240
241 return NO_ERROR;
242 }
243
Alec Mouri7355eb22019-03-05 14:19:10 -0800244 status_t removeEventListener(DispSync::Callback* callback, nsecs_t* outLastCallback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700245 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700246 Mutex::Autolock lock(mMutex);
247
Ana Krulec9a52b192018-07-12 18:18:47 -0400248 for (std::vector<EventListener>::iterator it = mEventListeners.begin();
249 it != mEventListeners.end(); ++it) {
250 if (it->mCallback == callback) {
Alec Mouri7355eb22019-03-05 14:19:10 -0800251 *outLastCallback = it->mLastCallbackTime;
Ana Krulec9a52b192018-07-12 18:18:47 -0400252 mEventListeners.erase(it);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700253 mCond.signal();
254 return NO_ERROR;
255 }
256 }
257
258 return BAD_VALUE;
259 }
260
Dan Stoza84d619e2018-03-28 17:07:36 -0700261 status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700262 if (mTraceDetailedInfo) ATRACE_CALL();
Dan Stoza84d619e2018-03-28 17:07:36 -0700263 Mutex::Autolock lock(mMutex);
264
Ana Krulec9a52b192018-07-12 18:18:47 -0400265 for (auto& eventListener : mEventListeners) {
266 if (eventListener.mCallback == callback) {
267 const nsecs_t oldPhase = eventListener.mPhase;
268 eventListener.mPhase = phase;
Dan Stoza84d619e2018-03-28 17:07:36 -0700269
270 // Pretend that the last time this event was handled at the same frame but with the
271 // new offset to allow for a seamless offset change without double-firing or
272 // skipping.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200273 nsecs_t diff = oldPhase - phase;
274 if (diff > mPeriod / 2) {
275 diff -= mPeriod;
276 } else if (diff < -mPeriod / 2) {
277 diff += mPeriod;
278 }
Ana Krulec9a52b192018-07-12 18:18:47 -0400279 eventListener.mLastEventTime -= diff;
Dan Stoza84d619e2018-03-28 17:07:36 -0700280 mCond.signal();
281 return NO_ERROR;
282 }
283 }
Dan Stoza84d619e2018-03-28 17:07:36 -0700284 return BAD_VALUE;
285 }
286
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700287private:
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700288 struct EventListener {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000289 const char* mName;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700290 nsecs_t mPhase;
291 nsecs_t mLastEventTime;
Alec Mouri7355eb22019-03-05 14:19:10 -0800292 nsecs_t mLastCallbackTime;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800293 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700294 };
295
296 struct CallbackInvocation {
Lloyd Piquee83f9312018-02-01 12:53:17 -0800297 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700298 nsecs_t mEventTime;
299 };
300
301 nsecs_t computeNextEventTimeLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700302 if (mTraceDetailedInfo) ATRACE_CALL();
Tim Murray4a4e4a22016-04-19 16:29:23 +0000303 ALOGV("[%s] computeNextEventTimeLocked", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700304 nsecs_t nextEventTime = INT64_MAX;
305 for (size_t i = 0; i < mEventListeners.size(); i++) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800306 nsecs_t t = computeListenerNextEventTimeLocked(mEventListeners[i], now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700307
308 if (t < nextEventTime) {
309 nextEventTime = t;
310 }
311 }
312
Tim Murray4a4e4a22016-04-19 16:29:23 +0000313 ALOGV("[%s] nextEventTime = %" PRId64, mName, ns2us(nextEventTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700314 return nextEventTime;
315 }
316
Alec Mouri7355eb22019-03-05 14:19:10 -0800317 // Sanity check that the duration is close enough in length to a period without
318 // falling into double-rate vsyncs.
Alec Mouri6db6d622019-03-19 18:42:03 -0700319 bool isShorterThanPeriod(nsecs_t duration) {
Alec Mouri7355eb22019-03-05 14:19:10 -0800320 // Ratio of 3/5 is arbitrary, but it must be greater than 1/2.
321 return duration < (3 * mPeriod) / 5;
322 }
323
Ana Krulec9a52b192018-07-12 18:18:47 -0400324 std::vector<CallbackInvocation> gatherCallbackInvocationsLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700325 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800326 ALOGV("[%s] gatherCallbackInvocationsLocked @ %" PRId64, mName, ns2us(now));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000327
Ana Krulec9a52b192018-07-12 18:18:47 -0400328 std::vector<CallbackInvocation> callbackInvocations;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000329 nsecs_t onePeriodAgo = now - mPeriod;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700330
Ana Krulec9a52b192018-07-12 18:18:47 -0400331 for (auto& eventListener : mEventListeners) {
332 nsecs_t t = computeListenerNextEventTimeLocked(eventListener, onePeriodAgo);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700333
Jamie Gennis0d5c60e2013-10-09 17:49:37 -0700334 if (t < now) {
Alec Mouri6db6d622019-03-19 18:42:03 -0700335 if (isShorterThanPeriod(now - eventListener.mLastCallbackTime)) {
Alec Mouri7355eb22019-03-05 14:19:10 -0800336 eventListener.mLastEventTime = t;
Alec Mouri7355eb22019-03-05 14:19:10 -0800337 ALOGV("[%s] [%s] Skipping event due to model error", mName,
338 eventListener.mName);
339 continue;
340 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700341 CallbackInvocation ci;
Ana Krulec9a52b192018-07-12 18:18:47 -0400342 ci.mCallback = eventListener.mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700343 ci.mEventTime = t;
Alec Mouri7355eb22019-03-05 14:19:10 -0800344 ALOGV("[%s] [%s] Preparing to fire, latency: %" PRId64, mName, eventListener.mName,
345 t - eventListener.mLastEventTime);
Ana Krulec9a52b192018-07-12 18:18:47 -0400346 callbackInvocations.push_back(ci);
347 eventListener.mLastEventTime = t;
Alec Mouri7355eb22019-03-05 14:19:10 -0800348 eventListener.mLastCallbackTime = now;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700349 }
350 }
351
352 return callbackInvocations;
353 }
354
Lloyd Pique78ce4182018-01-31 16:39:51 -0800355 nsecs_t computeListenerNextEventTimeLocked(const EventListener& listener, nsecs_t baseTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700356 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800357 ALOGV("[%s] [%s] computeListenerNextEventTimeLocked(%" PRId64 ")", mName, listener.mName,
358 ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700359
Tim Murray4a4e4a22016-04-19 16:29:23 +0000360 nsecs_t lastEventTime = listener.mLastEventTime + mWakeupLatency;
361 ALOGV("[%s] lastEventTime: %" PRId64, mName, ns2us(lastEventTime));
362 if (baseTime < lastEventTime) {
363 baseTime = lastEventTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800364 ALOGV("[%s] Clamping baseTime to lastEventTime -> %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700365 }
366
Tim Murray4a4e4a22016-04-19 16:29:23 +0000367 baseTime -= mReferenceTime;
368 ALOGV("[%s] Relative baseTime = %" PRId64, mName, ns2us(baseTime));
369 nsecs_t phase = mPhase + listener.mPhase;
370 ALOGV("[%s] Phase = %" PRId64, mName, ns2us(phase));
371 baseTime -= phase;
372 ALOGV("[%s] baseTime - phase = %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700373
Tim Murray4a4e4a22016-04-19 16:29:23 +0000374 // If our previous time is before the reference (because the reference
375 // has since been updated), the division by mPeriod will truncate
376 // towards zero instead of computing the floor. Since in all cases
377 // before the reference we want the next time to be effectively now, we
378 // set baseTime to -mPeriod so that numPeriods will be -1.
379 // When we add 1 and the phase, we will be at the correct event time for
380 // this period.
381 if (baseTime < 0) {
382 ALOGV("[%s] Correcting negative baseTime", mName);
383 baseTime = -mPeriod;
384 }
385
386 nsecs_t numPeriods = baseTime / mPeriod;
387 ALOGV("[%s] numPeriods = %" PRId64, mName, numPeriods);
388 nsecs_t t = (numPeriods + 1) * mPeriod + phase;
389 ALOGV("[%s] t = %" PRId64, mName, ns2us(t));
390 t += mReferenceTime;
391 ALOGV("[%s] Absolute t = %" PRId64, mName, ns2us(t));
392
393 // Check that it's been slightly more than half a period since the last
394 // event so that we don't accidentally fall into double-rate vsyncs
Alec Mouri6db6d622019-03-19 18:42:03 -0700395 if (isShorterThanPeriod(t - listener.mLastEventTime)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700396 t += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000397 ALOGV("[%s] Modifying t -> %" PRId64, mName, ns2us(t));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700398 }
399
Tim Murray4a4e4a22016-04-19 16:29:23 +0000400 t -= mWakeupLatency;
401 ALOGV("[%s] Corrected for wakeup latency -> %" PRId64, mName, ns2us(t));
402
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700403 return t;
404 }
405
Ana Krulec9a52b192018-07-12 18:18:47 -0400406 void fireCallbackInvocations(const std::vector<CallbackInvocation>& callbacks) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700407 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700408 for (size_t i = 0; i < callbacks.size(); i++) {
409 callbacks[i].mCallback->onDispSyncEvent(callbacks[i].mEventTime);
410 }
411 }
412
Tim Murray4a4e4a22016-04-19 16:29:23 +0000413 const char* const mName;
414
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700415 bool mStop;
416
417 nsecs_t mPeriod;
418 nsecs_t mPhase;
Haixia Shi676b1f62015-10-28 16:19:01 -0700419 nsecs_t mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700420 nsecs_t mWakeupLatency;
421
Tim Murray4a4e4a22016-04-19 16:29:23 +0000422 int64_t mFrameNumber;
423
Ana Krulec9a52b192018-07-12 18:18:47 -0400424 std::vector<EventListener> mEventListeners;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700425
426 Mutex mMutex;
427 Condition mCond;
Ana Krulec064a82f2018-09-11 16:03:03 -0700428
429 // Flag to turn on logging in systrace.
430 const bool mTraceDetailedInfo;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700431};
432
Tim Murray4a4e4a22016-04-19 16:29:23 +0000433#undef LOG_TAG
434#define LOG_TAG "DispSync"
435
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700436class ZeroPhaseTracer : public DispSync::Callback {
437public:
438 ZeroPhaseTracer() : mParity(false) {}
439
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700440 virtual void onDispSyncEvent(nsecs_t /*when*/) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700441 mParity = !mParity;
442 ATRACE_INT("ZERO_PHASE_VSYNC", mParity ? 1 : 0);
443 }
444
445private:
446 bool mParity;
447};
448
Ana Krulec064a82f2018-09-11 16:03:03 -0700449DispSync::DispSync(const char* name) : mName(name), mRefreshSkipCount(0) {
450 // This flag offers the ability to turn on systrace logging from the shell.
451 char value[PROPERTY_VALUE_MAX];
452 property_get("debug.sf.dispsync_trace_detailed_info", value, "0");
453 mTraceDetailedInfo = atoi(value);
454 mThread = new DispSyncThread(name, mTraceDetailedInfo);
455}
Andy McFadden645b1f72014-06-10 14:43:32 -0700456
Ady Abraham50c202a2019-03-14 11:44:38 -0700457DispSync::~DispSync() {
458 mThread->stop();
459 mThread->requestExitAndWait();
460}
Saurabh Shahf4174532017-07-13 10:45:07 -0700461
462void DispSync::init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset) {
463 mIgnorePresentFences = !hasSyncFramework;
464 mPresentTimeOffset = dispSyncPresentTimeOffset;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700465 mThread->run("DispSync", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
Saurabh Shahf4174532017-07-13 10:45:07 -0700466
Tim Murrayacff43d2016-07-29 13:57:24 -0700467 // set DispSync to SCHED_FIFO to minimize jitter
468 struct sched_param param = {0};
Tim Murray35520632016-09-07 12:18:17 -0700469 param.sched_priority = 2;
Tim Murrayacff43d2016-07-29 13:57:24 -0700470 if (sched_setscheduler(mThread->getTid(), SCHED_FIFO, &param) != 0) {
471 ALOGE("Couldn't set SCHED_FIFO for DispSyncThread");
472 }
473
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700474 reset();
475 beginResync();
476
Ana Krulec064a82f2018-09-11 16:03:03 -0700477 if (mTraceDetailedInfo && kEnableZeroPhaseTracer) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700478 mZeroPhaseTracer = std::make_unique<ZeroPhaseTracer>();
Alec Mouri7355eb22019-03-05 14:19:10 -0800479 addEventListener("ZeroPhaseTracer", 0, mZeroPhaseTracer.get(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700480 }
481}
482
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700483void DispSync::reset() {
484 Mutex::Autolock lock(mMutex);
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700485 resetLocked();
486}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700487
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700488void DispSync::resetLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700489 mPhase = 0;
Alec Mouri75b0b622019-03-12 18:56:00 +0000490 const size_t lastSampleIdx = (mFirstResyncSample + mNumResyncSamples - 1) % MAX_RESYNC_SAMPLES;
491 // Keep the most recent sample, when we resync to hardware we'll overwrite this
492 // with a more accurate signal
493 if (mResyncSamples[lastSampleIdx] != 0) {
494 mReferenceTime = mResyncSamples[lastSampleIdx];
495 }
Haixia Shi676b1f62015-10-28 16:19:01 -0700496 mModelUpdated = false;
Alec Mouri75b0b622019-03-12 18:56:00 +0000497 for (size_t i = 0; i < MAX_RESYNC_SAMPLES; i++) {
498 mResyncSamples[i] = 0;
499 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700500 mNumResyncSamples = 0;
501 mFirstResyncSample = 0;
502 mNumResyncSamplesSincePresent = 0;
503 resetErrorLocked();
504}
505
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700506bool DispSync::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700507 Mutex::Autolock lock(mMutex);
508
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700509 if (mIgnorePresentFences) {
510 return true;
511 }
512
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700513 mPresentFences[mPresentSampleOffset] = fenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700514 mPresentSampleOffset = (mPresentSampleOffset + 1) % NUM_PRESENT_SAMPLES;
515 mNumResyncSamplesSincePresent = 0;
516
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700517 updateErrorLocked();
518
Haixia Shi676b1f62015-10-28 16:19:01 -0700519 return !mModelUpdated || mError > kErrorThreshold;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700520}
521
522void DispSync::beginResync() {
523 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000524 ALOGV("[%s] beginResync", mName);
Haixia Shi676b1f62015-10-28 16:19:01 -0700525 mModelUpdated = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700526 mNumResyncSamples = 0;
527}
528
Alec Mouri754c98a2019-03-18 18:53:42 -0700529bool DispSync::addResyncSample(nsecs_t timestamp, bool* periodChanged) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700530 Mutex::Autolock lock(mMutex);
531
Tim Murray4a4e4a22016-04-19 16:29:23 +0000532 ALOGV("[%s] addResyncSample(%" PRId64 ")", mName, ns2us(timestamp));
533
Alec Mouri754c98a2019-03-18 18:53:42 -0700534 *periodChanged = false;
535 const size_t idx = (mFirstResyncSample + mNumResyncSamples) % MAX_RESYNC_SAMPLES;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700536 mResyncSamples[idx] = timestamp;
Haixia Shi664339a2015-10-28 13:22:22 -0700537 if (mNumResyncSamples == 0) {
Haixia Shi676b1f62015-10-28 16:19:01 -0700538 mPhase = 0;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000539 ALOGV("[%s] First resync sample: mPeriod = %" PRId64 ", mPhase = 0, "
Lloyd Pique78ce4182018-01-31 16:39:51 -0800540 "mReferenceTime = %" PRId64,
Alec Mouri754c98a2019-03-18 18:53:42 -0700541 mName, ns2us(mPeriod), ns2us(timestamp));
542 } else if (mPendingPeriod > 0) {
543 // mNumResyncSamples > 0, so priorIdx won't overflow
544 const size_t priorIdx = (mFirstResyncSample + mNumResyncSamples - 1) % MAX_RESYNC_SAMPLES;
545 const nsecs_t lastTimestamp = mResyncSamples[priorIdx];
546
547 const nsecs_t observedVsync = std::abs(timestamp - lastTimestamp);
548 if (std::abs(observedVsync - mPendingPeriod) < std::abs(observedVsync - mPeriod)) {
549 // Observed vsync is closer to the pending period, so reset the
550 // model and flush the pending period.
551 resetLocked();
552 mPeriod = mPendingPeriod;
553 mPendingPeriod = 0;
554 if (mTraceDetailedInfo) {
555 ATRACE_INT("DispSync:PendingPeriod", mPendingPeriod);
556 }
557 *periodChanged = true;
558 }
Haixia Shi664339a2015-10-28 13:22:22 -0700559 }
Alec Mouri754c98a2019-03-18 18:53:42 -0700560 // Always update the reference time with the most recent timestamp.
561 mReferenceTime = timestamp;
562 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700563
564 if (mNumResyncSamples < MAX_RESYNC_SAMPLES) {
565 mNumResyncSamples++;
566 } else {
567 mFirstResyncSample = (mFirstResyncSample + 1) % MAX_RESYNC_SAMPLES;
568 }
569
570 updateModelLocked();
571
572 if (mNumResyncSamplesSincePresent++ > MAX_RESYNC_SAMPLES_WITHOUT_PRESENT) {
573 resetErrorLocked();
574 }
575
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800576 if (mIgnorePresentFences) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700577 // If we're ignoring the present fences we have no way to know whether
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700578 // or not we're synchronized with the HW vsyncs, so we just request
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700579 // that the HW vsync events be turned on.
580 return true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700581 }
582
Tim Murray4a4e4a22016-04-19 16:29:23 +0000583 // Check against kErrorThreshold / 2 to add some hysteresis before having to
584 // resync again
Alec Mouri754c98a2019-03-18 18:53:42 -0700585 bool modelLocked = mModelUpdated && mError < (kErrorThreshold / 2) && mPendingPeriod == 0;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800586 ALOGV("[%s] addResyncSample returning %s", mName, modelLocked ? "locked" : "unlocked");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000587 return !modelLocked;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700588}
589
Lloyd Pique78ce4182018-01-31 16:39:51 -0800590void DispSync::endResync() {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700591
Alec Mouri7355eb22019-03-05 14:19:10 -0800592status_t DispSync::addEventListener(const char* name, nsecs_t phase, Callback* callback,
593 nsecs_t lastCallbackTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700594 Mutex::Autolock lock(mMutex);
Alec Mouri7355eb22019-03-05 14:19:10 -0800595 return mThread->addEventListener(name, phase, callback, lastCallbackTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700596}
597
Andy McFadden645b1f72014-06-10 14:43:32 -0700598void DispSync::setRefreshSkipCount(int count) {
599 Mutex::Autolock lock(mMutex);
600 ALOGD("setRefreshSkipCount(%d)", count);
601 mRefreshSkipCount = count;
602 updateModelLocked();
Ruchi Kandoif52b3c82014-04-24 16:42:35 -0700603}
604
Alec Mouri7355eb22019-03-05 14:19:10 -0800605status_t DispSync::removeEventListener(Callback* callback, nsecs_t* outLastCallbackTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700606 Mutex::Autolock lock(mMutex);
Alec Mouri7355eb22019-03-05 14:19:10 -0800607 return mThread->removeEventListener(callback, outLastCallbackTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700608}
609
Dan Stoza84d619e2018-03-28 17:07:36 -0700610status_t DispSync::changePhaseOffset(Callback* callback, nsecs_t phase) {
611 Mutex::Autolock lock(mMutex);
612 return mThread->changePhaseOffset(callback, phase);
613}
614
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700615void DispSync::setPeriod(nsecs_t period) {
616 Mutex::Autolock lock(mMutex);
Alec Mouri754c98a2019-03-18 18:53:42 -0700617 if (mTraceDetailedInfo) {
618 ATRACE_INT("DispSync:PendingPeriod", period);
619 }
620 mPendingPeriod = period;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700621}
622
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700623nsecs_t DispSync::getPeriod() {
624 // lock mutex as mPeriod changes multiple times in updateModelLocked
625 Mutex::Autolock lock(mMutex);
626 return mPeriod;
627}
628
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700629void DispSync::updateModelLocked() {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000630 ALOGV("[%s] updateModelLocked %zu", mName, mNumResyncSamples);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700631 if (mNumResyncSamples >= MIN_RESYNC_SAMPLES_FOR_UPDATE) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000632 ALOGV("[%s] Computing...", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700633 nsecs_t durationSum = 0;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000634 nsecs_t minDuration = INT64_MAX;
635 nsecs_t maxDuration = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700636 for (size_t i = 1; i < mNumResyncSamples; i++) {
637 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
638 size_t prev = (idx + MAX_RESYNC_SAMPLES - 1) % MAX_RESYNC_SAMPLES;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000639 nsecs_t duration = mResyncSamples[idx] - mResyncSamples[prev];
640 durationSum += duration;
641 minDuration = min(minDuration, duration);
642 maxDuration = max(maxDuration, duration);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700643 }
644
Tim Murray4a4e4a22016-04-19 16:29:23 +0000645 // Exclude the min and max from the average
646 durationSum -= minDuration + maxDuration;
David Sodman1afa8b02018-10-15 11:20:48 -0700647 mPeriod = durationSum / (mNumResyncSamples - 3);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000648
649 ALOGV("[%s] mPeriod = %" PRId64, mName, ns2us(mPeriod));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700650
651 double sampleAvgX = 0;
652 double sampleAvgY = 0;
653 double scale = 2.0 * M_PI / double(mPeriod);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000654 // Intentionally skip the first sample
655 for (size_t i = 1; i < mNumResyncSamples; i++) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700656 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
Haixia Shi676b1f62015-10-28 16:19:01 -0700657 nsecs_t sample = mResyncSamples[idx] - mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700658 double samplePhase = double(sample % mPeriod) * scale;
659 sampleAvgX += cos(samplePhase);
660 sampleAvgY += sin(samplePhase);
661 }
662
Tim Murray4a4e4a22016-04-19 16:29:23 +0000663 sampleAvgX /= double(mNumResyncSamples - 1);
664 sampleAvgY /= double(mNumResyncSamples - 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700665
666 mPhase = nsecs_t(atan2(sampleAvgY, sampleAvgX) / scale);
667
Tim Murray4a4e4a22016-04-19 16:29:23 +0000668 ALOGV("[%s] mPhase = %" PRId64, mName, ns2us(mPhase));
669
670 if (mPhase < -(mPeriod / 2)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700671 mPhase += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000672 ALOGV("[%s] Adjusting mPhase -> %" PRId64, mName, ns2us(mPhase));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700673 }
674
Andy McFadden645b1f72014-06-10 14:43:32 -0700675 // Artificially inflate the period if requested.
676 mPeriod += mPeriod * mRefreshSkipCount;
677
Haixia Shi676b1f62015-10-28 16:19:01 -0700678 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
679 mModelUpdated = true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700680 }
681}
682
683void DispSync::updateErrorLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700684 if (!mModelUpdated) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700685 return;
686 }
687
Andy McFadden645b1f72014-06-10 14:43:32 -0700688 // Need to compare present fences against the un-adjusted refresh period,
689 // since they might arrive between two events.
690 nsecs_t period = mPeriod / (1 + mRefreshSkipCount);
691
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700692 int numErrSamples = 0;
693 nsecs_t sqErrSum = 0;
694
695 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700696 // Only check for the cached value of signal time to avoid unecessary
697 // syscalls. It is the responsibility of the DispSync owner to
698 // call getSignalTime() periodically so the cache is updated when the
699 // fence signals.
700 nsecs_t time = mPresentFences[i]->getCachedSignalTime();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800701 if (time == Fence::SIGNAL_TIME_PENDING || time == Fence::SIGNAL_TIME_INVALID) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700702 continue;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700703 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700704
705 nsecs_t sample = time - mReferenceTime;
706 if (sample <= mPhase) {
707 continue;
708 }
709
710 nsecs_t sampleErr = (sample - mPhase) % period;
711 if (sampleErr > period / 2) {
712 sampleErr -= period;
713 }
714 sqErrSum += sampleErr * sampleErr;
715 numErrSamples++;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700716 }
717
718 if (numErrSamples > 0) {
719 mError = sqErrSum / numErrSamples;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700720 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700721 } else {
722 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700723 // Use mod ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT to avoid log spam.
724 mZeroErrSamplesCount++;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800725 ALOGE_IF((mZeroErrSamplesCount % ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT) == 0,
726 "No present times for model error.");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700727 }
728
Ana Krulec064a82f2018-09-11 16:03:03 -0700729 if (mTraceDetailedInfo) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700730 ATRACE_INT64("DispSync:Error", mError);
731 }
732}
733
734void DispSync::resetErrorLocked() {
735 mPresentSampleOffset = 0;
736 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700737 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700738 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700739 mPresentFences[i] = FenceTime::NO_FENCE;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700740 }
741}
742
Andy McFadden41d67d72014-04-25 16:58:34 -0700743nsecs_t DispSync::computeNextRefresh(int periodOffset) const {
Andy McFadden150ecd82014-05-08 14:56:50 -0700744 Mutex::Autolock lock(mMutex);
Andy McFadden41d67d72014-04-25 16:58:34 -0700745 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Haixia Shi676b1f62015-10-28 16:19:01 -0700746 nsecs_t phase = mReferenceTime + mPhase;
Marissa Wall17b4e452018-12-26 16:32:34 -0800747 if (mPeriod == 0) {
748 return 0;
749 }
Haixia Shi676b1f62015-10-28 16:19:01 -0700750 return (((now - phase) / mPeriod) + periodOffset + 1) * mPeriod + phase;
Andy McFadden41d67d72014-04-25 16:58:34 -0700751}
752
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700753void DispSync::setIgnorePresentFences(bool ignore) {
754 Mutex::Autolock lock(mMutex);
755 if (mIgnorePresentFences != ignore) {
756 mIgnorePresentFences = ignore;
757 resetLocked();
758 }
759}
760
Yiwei Zhang5434a782018-12-05 18:06:32 -0800761void DispSync::dump(std::string& result) const {
Andy McFaddenc751e922014-05-08 14:53:26 -0700762 Mutex::Autolock lock(mMutex);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800763 StringAppendF(&result, "present fences are %s\n", mIgnorePresentFences ? "ignored" : "used");
764 StringAppendF(&result, "mPeriod: %" PRId64 " ns (%.3f fps; skipCount=%d)\n", mPeriod,
765 1000000000.0 / mPeriod, mRefreshSkipCount);
766 StringAppendF(&result, "mPhase: %" PRId64 " ns\n", mPhase);
767 StringAppendF(&result, "mError: %" PRId64 " ns (sqrt=%.1f)\n", mError, sqrt(mError));
768 StringAppendF(&result, "mNumResyncSamplesSincePresent: %d (limit %d)\n",
769 mNumResyncSamplesSincePresent, MAX_RESYNC_SAMPLES_WITHOUT_PRESENT);
770 StringAppendF(&result, "mNumResyncSamples: %zd (max %d)\n", mNumResyncSamples,
771 MAX_RESYNC_SAMPLES);
Andy McFaddenc751e922014-05-08 14:53:26 -0700772
Yiwei Zhang5434a782018-12-05 18:06:32 -0800773 result.append("mResyncSamples:\n");
Andy McFaddenc751e922014-05-08 14:53:26 -0700774 nsecs_t previous = -1;
775 for (size_t i = 0; i < mNumResyncSamples; i++) {
776 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
777 nsecs_t sampleTime = mResyncSamples[idx];
778 if (i == 0) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800779 StringAppendF(&result, " %" PRId64 "\n", sampleTime);
Andy McFaddenc751e922014-05-08 14:53:26 -0700780 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800781 StringAppendF(&result, " %" PRId64 " (+%" PRId64 ")\n", sampleTime,
782 sampleTime - previous);
Andy McFaddenc751e922014-05-08 14:53:26 -0700783 }
784 previous = sampleTime;
785 }
786
Yiwei Zhang5434a782018-12-05 18:06:32 -0800787 StringAppendF(&result, "mPresentFences [%d]:\n", NUM_PRESENT_SAMPLES);
Andy McFadden5167ec62014-05-22 13:08:43 -0700788 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700789 previous = Fence::SIGNAL_TIME_INVALID;
Andy McFaddenc751e922014-05-08 14:53:26 -0700790 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
791 size_t idx = (i + mPresentSampleOffset) % NUM_PRESENT_SAMPLES;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700792 nsecs_t presentTime = mPresentFences[idx]->getSignalTime();
793 if (presentTime == Fence::SIGNAL_TIME_PENDING) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800794 StringAppendF(&result, " [unsignaled fence]\n");
Lloyd Pique78ce4182018-01-31 16:39:51 -0800795 } else if (presentTime == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800796 StringAppendF(&result, " [invalid fence]\n");
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700797 } else if (previous == Fence::SIGNAL_TIME_PENDING ||
Lloyd Pique78ce4182018-01-31 16:39:51 -0800798 previous == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800799 StringAppendF(&result, " %" PRId64 " (%.3f ms ago)\n", presentTime,
800 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700801 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800802 StringAppendF(&result, " %" PRId64 " (+%" PRId64 " / %.3f) (%.3f ms ago)\n",
803 presentTime, presentTime - previous,
804 (presentTime - previous) / (double)mPeriod,
805 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700806 }
807 previous = presentTime;
808 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700809
Yiwei Zhang5434a782018-12-05 18:06:32 -0800810 StringAppendF(&result, "current monotonic time: %" PRId64 "\n", now);
Andy McFaddenc751e922014-05-08 14:53:26 -0700811}
812
Ana Krulec010d2192018-10-08 06:29:54 -0700813nsecs_t DispSync::expectedPresentTime() {
814 // The HWC doesn't currently have a way to report additional latency.
815 // Assume that whatever we submit now will appear right after the flip.
816 // For a smart panel this might be 1. This is expressed in frames,
817 // rather than time, because we expect to have a constant frame delay
818 // regardless of the refresh rate.
819 const uint32_t hwcLatency = 0;
820
821 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
Ana Krulec757f63a2019-01-25 10:46:18 -0800822 return computeNextRefresh(hwcLatency);
Ana Krulec010d2192018-10-08 06:29:54 -0700823}
824
Lloyd Pique41be5d22018-06-21 13:11:48 -0700825} // namespace impl
826
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700827} // namespace android