blob: 46112f5cf45142abf5e72401dabd993abe9abb62 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070021#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Tim Murray4a4e4a22016-04-19 16:29:23 +000022//#define LOG_NDEBUG 0
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070023
24// This is needed for stdint.h to define INT64_MAX in C++
25#define __STDC_LIMIT_MACROS
26
27#include <math.h>
28
Mark Salyzyna5e161b2016-09-29 08:08:05 -070029#include <algorithm>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070030
Yiwei Zhang5434a782018-12-05 18:06:32 -080031#include <android-base/stringprintf.h>
David Sodman1afa8b02018-10-15 11:20:48 -070032#include <cutils/properties.h>
Yiwei Zhang5434a782018-12-05 18:06:32 -080033#include <log/log.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070034#include <utils/Thread.h>
35#include <utils/Trace.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070036
Brian Andersonfbc80ae2017-05-26 16:23:54 -070037#include <ui/FenceTime.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070038
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070039#include "DispSync.h"
40#include "EventLog/EventLog.h"
Lloyd Pique78ce4182018-01-31 16:39:51 -080041#include "SurfaceFlinger.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070042
Yiwei Zhang5434a782018-12-05 18:06:32 -080043using android::base::StringAppendF;
Tim Murray4a4e4a22016-04-19 16:29:23 +000044using std::max;
45using std::min;
46
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070047namespace android {
48
Lloyd Pique41be5d22018-06-21 13:11:48 -070049DispSync::~DispSync() = default;
Kevin DuBois7b743be2019-02-27 10:05:48 -080050DispSync::Callback::~Callback() = default;
Lloyd Pique41be5d22018-06-21 13:11:48 -070051
52namespace impl {
53
Tim Murray4a4e4a22016-04-19 16:29:23 +000054// Setting this to true adds a zero-phase tracer for correlating with hardware
55// vsync events
56static const bool kEnableZeroPhaseTracer = false;
57
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070058// This is the threshold used to determine when hardware vsync events are
59// needed to re-synchronize the software vsync model with the hardware. The
60// error metric used is the mean of the squared difference between each
61// present time and the nearest software-predicted vsync.
Lloyd Pique78ce4182018-01-31 16:39:51 -080062static const nsecs_t kErrorThreshold = 160000000000; // 400 usec squared
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070063
Tim Murray4a4e4a22016-04-19 16:29:23 +000064#undef LOG_TAG
65#define LOG_TAG "DispSyncThread"
Lloyd Pique78ce4182018-01-31 16:39:51 -080066class DispSyncThread : public Thread {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070067public:
Ana Krulec064a82f2018-09-11 16:03:03 -070068 DispSyncThread(const char* name, bool showTraceDetailedInfo)
Lloyd Pique78ce4182018-01-31 16:39:51 -080069 : mName(name),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070070 mStop(false),
Ady Abraham50204dd2019-07-19 15:47:11 -070071 mModelLocked("DispSync:ModelLocked", false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070072 mPeriod(0),
73 mPhase(0),
Haixia Shi676b1f62015-10-28 16:19:01 -070074 mReferenceTime(0),
Tim Murray4a4e4a22016-04-19 16:29:23 +000075 mWakeupLatency(0),
Ana Krulec064a82f2018-09-11 16:03:03 -070076 mFrameNumber(0),
77 mTraceDetailedInfo(showTraceDetailedInfo) {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070078
79 virtual ~DispSyncThread() {}
80
Haixia Shi676b1f62015-10-28 16:19:01 -070081 void updateModel(nsecs_t period, nsecs_t phase, nsecs_t referenceTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -070082 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070083 Mutex::Autolock lock(mMutex);
Alec Mouri75b0b622019-03-12 18:56:00 +000084
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070085 mPhase = phase;
Alec Mouric3a482d2019-05-21 00:51:01 -070086 const bool referenceTimeChanged = mReferenceTime != referenceTime;
Haixia Shi676b1f62015-10-28 16:19:01 -070087 mReferenceTime = referenceTime;
Alec Mouri75b0b622019-03-12 18:56:00 +000088 if (mPeriod != 0 && mPeriod != period && mReferenceTime != 0) {
89 // Inflate the reference time to be the most recent predicted
90 // vsync before the current time.
91 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
92 const nsecs_t baseTime = now - mReferenceTime;
93 const nsecs_t numOldPeriods = baseTime / mPeriod;
94 mReferenceTime = mReferenceTime + (numOldPeriods)*mPeriod;
95 }
96 mPeriod = period;
Alec Mouric3a482d2019-05-21 00:51:01 -070097 if (!mModelLocked && referenceTimeChanged) {
98 for (auto& eventListener : mEventListeners) {
Ady Abraham81ca00f2019-06-19 17:01:57 -070099 eventListener.mLastEventTime = mReferenceTime + mPhase + eventListener.mPhase;
100 // If mLastEventTime is after mReferenceTime (can happen when positive phase offsets
101 // are used) we treat it as like it happened in previous period.
102 if (eventListener.mLastEventTime > mReferenceTime) {
103 eventListener.mLastEventTime -= mPeriod;
104 }
Alec Mouric3a482d2019-05-21 00:51:01 -0700105 }
106 }
Alec Mourif5fe85c2019-02-22 13:40:36 -0800107 if (mTraceDetailedInfo) {
108 ATRACE_INT64("DispSync:Period", mPeriod);
109 ATRACE_INT64("DispSync:Phase", mPhase + mPeriod / 2);
110 ATRACE_INT64("DispSync:Reference Time", mReferenceTime);
111 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000112 ALOGV("[%s] updateModel: mPeriod = %" PRId64 ", mPhase = %" PRId64
Lloyd Pique78ce4182018-01-31 16:39:51 -0800113 " mReferenceTime = %" PRId64,
114 mName, ns2us(mPeriod), ns2us(mPhase), ns2us(mReferenceTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700115 mCond.signal();
116 }
117
118 void stop() {
Ana Krulec064a82f2018-09-11 16:03:03 -0700119 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700120 Mutex::Autolock lock(mMutex);
121 mStop = true;
122 mCond.signal();
123 }
124
Alec Mouri016b8dd2019-04-24 15:16:05 -0700125 void lockModel() {
126 Mutex::Autolock lock(mMutex);
127 mModelLocked = true;
128 }
129
130 void unlockModel() {
131 Mutex::Autolock lock(mMutex);
132 mModelLocked = false;
133 }
134
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700135 virtual bool threadLoop() {
136 status_t err;
137 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700138
139 while (true) {
Ana Krulec9a52b192018-07-12 18:18:47 -0400140 std::vector<CallbackInvocation> callbackInvocations;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700141
142 nsecs_t targetTime = 0;
143
144 { // Scope for lock
145 Mutex::Autolock lock(mMutex);
146
Ana Krulec064a82f2018-09-11 16:03:03 -0700147 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000148 ATRACE_INT64("DispSync:Frame", mFrameNumber);
149 }
150 ALOGV("[%s] Frame %" PRId64, mName, mFrameNumber);
151 ++mFrameNumber;
152
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700153 if (mStop) {
154 return false;
155 }
156
157 if (mPeriod == 0) {
158 err = mCond.wait(mMutex);
159 if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800160 ALOGE("error waiting for new events: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700161 return false;
162 }
163 continue;
164 }
165
Dan Stoza8f8374d2016-04-19 10:03:46 -0700166 targetTime = computeNextEventTimeLocked(now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700167
168 bool isWakeup = false;
169
170 if (now < targetTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700171 if (mTraceDetailedInfo) ATRACE_NAME("DispSync waiting");
Dan Stoza8f8374d2016-04-19 10:03:46 -0700172
173 if (targetTime == INT64_MAX) {
174 ALOGV("[%s] Waiting forever", mName);
175 err = mCond.wait(mMutex);
176 } else {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800177 ALOGV("[%s] Waiting until %" PRId64, mName, ns2us(targetTime));
Dan Stoza8f8374d2016-04-19 10:03:46 -0700178 err = mCond.waitRelative(mMutex, targetTime - now);
179 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700180
181 if (err == TIMED_OUT) {
182 isWakeup = true;
183 } else if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800184 ALOGE("error waiting for next event: %s (%d)", strerror(-err), err);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700185 return false;
186 }
187 }
188
189 now = systemTime(SYSTEM_TIME_MONOTONIC);
190
Tim Murray4a4e4a22016-04-19 16:29:23 +0000191 // Don't correct by more than 1.5 ms
192 static const nsecs_t kMaxWakeupLatency = us2ns(1500);
193
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700194 if (isWakeup) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800195 mWakeupLatency = ((mWakeupLatency * 63) + (now - targetTime)) / 64;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000196 mWakeupLatency = min(mWakeupLatency, kMaxWakeupLatency);
Ana Krulec064a82f2018-09-11 16:03:03 -0700197 if (mTraceDetailedInfo) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000198 ATRACE_INT64("DispSync:WakeupLat", now - targetTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700199 ATRACE_INT64("DispSync:AvgWakeupLat", mWakeupLatency);
200 }
201 }
202
Ady Abraham5facfb12020-04-22 15:18:31 -0700203 callbackInvocations =
204 gatherCallbackInvocationsLocked(now, computeNextRefreshLocked(0, now));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700205 }
206
207 if (callbackInvocations.size() > 0) {
Andy McFadden645b1f72014-06-10 14:43:32 -0700208 fireCallbackInvocations(callbackInvocations);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700209 }
210 }
211
212 return false;
213 }
214
Alec Mouri7355eb22019-03-05 14:19:10 -0800215 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback,
216 nsecs_t lastCallbackTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700217 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700218 Mutex::Autolock lock(mMutex);
219
220 for (size_t i = 0; i < mEventListeners.size(); i++) {
221 if (mEventListeners[i].mCallback == callback) {
222 return BAD_VALUE;
223 }
224 }
225
226 EventListener listener;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000227 listener.mName = name;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700228 listener.mPhase = phase;
229 listener.mCallback = callback;
Jamie Gennis629b9872013-10-29 13:36:12 -0700230
231 // We want to allow the firstmost future event to fire without
Alec Mouri81835982019-02-27 13:40:44 -0800232 // allowing any past events to fire. To do this extrapolate from
233 // mReferenceTime the most recent hardware vsync, and pin the
234 // last event time there.
235 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
236 if (mPeriod != 0) {
237 const nsecs_t baseTime = now - mReferenceTime;
238 const nsecs_t numPeriodsSinceReference = baseTime / mPeriod;
239 const nsecs_t predictedReference = mReferenceTime + numPeriodsSinceReference * mPeriod;
Alec Mouridf3d2e12019-05-02 17:58:46 +0000240 const nsecs_t phaseCorrection = mPhase + listener.mPhase;
241 const nsecs_t predictedLastEventTime = predictedReference + phaseCorrection;
242 if (predictedLastEventTime >= now) {
243 // Make sure that the last event time does not exceed the current time.
244 // If it would, then back the last event time by a period.
245 listener.mLastEventTime = predictedLastEventTime - mPeriod;
246 } else {
247 listener.mLastEventTime = predictedLastEventTime;
Alec Mouri81835982019-02-27 13:40:44 -0800248 }
249 } else {
250 listener.mLastEventTime = now + mPhase - mWakeupLatency;
251 }
Jamie Gennis629b9872013-10-29 13:36:12 -0700252
Alec Mouri7355eb22019-03-05 14:19:10 -0800253 if (lastCallbackTime <= 0) {
254 // If there is no prior callback time, try to infer one based on the
255 // logical last event time.
256 listener.mLastCallbackTime = listener.mLastEventTime + mWakeupLatency;
257 } else {
258 listener.mLastCallbackTime = lastCallbackTime;
259 }
260
Ana Krulec9a52b192018-07-12 18:18:47 -0400261 mEventListeners.push_back(listener);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700262
263 mCond.signal();
264
265 return NO_ERROR;
266 }
267
Alec Mouri7355eb22019-03-05 14:19:10 -0800268 status_t removeEventListener(DispSync::Callback* callback, nsecs_t* outLastCallback) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700269 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700270 Mutex::Autolock lock(mMutex);
271
Ana Krulec9a52b192018-07-12 18:18:47 -0400272 for (std::vector<EventListener>::iterator it = mEventListeners.begin();
273 it != mEventListeners.end(); ++it) {
274 if (it->mCallback == callback) {
Alec Mouri7355eb22019-03-05 14:19:10 -0800275 *outLastCallback = it->mLastCallbackTime;
Ana Krulec9a52b192018-07-12 18:18:47 -0400276 mEventListeners.erase(it);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700277 mCond.signal();
278 return NO_ERROR;
279 }
280 }
281
282 return BAD_VALUE;
283 }
284
Dan Stoza84d619e2018-03-28 17:07:36 -0700285 status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700286 if (mTraceDetailedInfo) ATRACE_CALL();
Dan Stoza84d619e2018-03-28 17:07:36 -0700287 Mutex::Autolock lock(mMutex);
288
Ana Krulec9a52b192018-07-12 18:18:47 -0400289 for (auto& eventListener : mEventListeners) {
290 if (eventListener.mCallback == callback) {
291 const nsecs_t oldPhase = eventListener.mPhase;
292 eventListener.mPhase = phase;
Dan Stoza84d619e2018-03-28 17:07:36 -0700293
294 // Pretend that the last time this event was handled at the same frame but with the
295 // new offset to allow for a seamless offset change without double-firing or
296 // skipping.
Jorim Jaggi22ec38b2018-06-19 15:57:08 +0200297 nsecs_t diff = oldPhase - phase;
Ana Krulec9a52b192018-07-12 18:18:47 -0400298 eventListener.mLastEventTime -= diff;
Ady Abraham45e4e362019-06-07 18:20:51 -0700299 eventListener.mLastCallbackTime -= diff;
Dan Stoza84d619e2018-03-28 17:07:36 -0700300 mCond.signal();
301 return NO_ERROR;
302 }
303 }
Dan Stoza84d619e2018-03-28 17:07:36 -0700304 return BAD_VALUE;
305 }
306
Ady Abraham5facfb12020-04-22 15:18:31 -0700307 nsecs_t computeNextRefresh(int periodOffset, nsecs_t now) const {
308 Mutex::Autolock lock(mMutex);
309 return computeNextRefreshLocked(periodOffset, now);
310 }
311
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700312private:
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700313 struct EventListener {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000314 const char* mName;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700315 nsecs_t mPhase;
316 nsecs_t mLastEventTime;
Alec Mouri7355eb22019-03-05 14:19:10 -0800317 nsecs_t mLastCallbackTime;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800318 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700319 };
320
321 struct CallbackInvocation {
Lloyd Piquee83f9312018-02-01 12:53:17 -0800322 DispSync::Callback* mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700323 nsecs_t mEventTime;
Ady Abraham5facfb12020-04-22 15:18:31 -0700324 nsecs_t mExpectedVSyncTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700325 };
326
327 nsecs_t computeNextEventTimeLocked(nsecs_t now) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700328 if (mTraceDetailedInfo) ATRACE_CALL();
Tim Murray4a4e4a22016-04-19 16:29:23 +0000329 ALOGV("[%s] computeNextEventTimeLocked", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700330 nsecs_t nextEventTime = INT64_MAX;
331 for (size_t i = 0; i < mEventListeners.size(); i++) {
Lloyd Pique78ce4182018-01-31 16:39:51 -0800332 nsecs_t t = computeListenerNextEventTimeLocked(mEventListeners[i], now);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700333
334 if (t < nextEventTime) {
335 nextEventTime = t;
336 }
337 }
338
Tim Murray4a4e4a22016-04-19 16:29:23 +0000339 ALOGV("[%s] nextEventTime = %" PRId64, mName, ns2us(nextEventTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700340 return nextEventTime;
341 }
342
Lihao Liang73a3c962020-07-31 21:47:20 +0000343 // Check that the duration is close enough in length to a period without
Alec Mouri7355eb22019-03-05 14:19:10 -0800344 // falling into double-rate vsyncs.
Alec Mouridf3d2e12019-05-02 17:58:46 +0000345 bool isCloseToPeriod(nsecs_t duration) {
Alec Mouri7355eb22019-03-05 14:19:10 -0800346 // Ratio of 3/5 is arbitrary, but it must be greater than 1/2.
347 return duration < (3 * mPeriod) / 5;
348 }
349
Ady Abraham5facfb12020-04-22 15:18:31 -0700350 std::vector<CallbackInvocation> gatherCallbackInvocationsLocked(nsecs_t now,
351 nsecs_t expectedVSyncTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700352 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800353 ALOGV("[%s] gatherCallbackInvocationsLocked @ %" PRId64, mName, ns2us(now));
Tim Murray4a4e4a22016-04-19 16:29:23 +0000354
Ana Krulec9a52b192018-07-12 18:18:47 -0400355 std::vector<CallbackInvocation> callbackInvocations;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000356 nsecs_t onePeriodAgo = now - mPeriod;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700357
Ana Krulec9a52b192018-07-12 18:18:47 -0400358 for (auto& eventListener : mEventListeners) {
359 nsecs_t t = computeListenerNextEventTimeLocked(eventListener, onePeriodAgo);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700360
Jamie Gennis0d5c60e2013-10-09 17:49:37 -0700361 if (t < now) {
Alec Mouridf3d2e12019-05-02 17:58:46 +0000362 if (isCloseToPeriod(now - eventListener.mLastCallbackTime)) {
Alec Mouri7355eb22019-03-05 14:19:10 -0800363 eventListener.mLastEventTime = t;
Alec Mouri7355eb22019-03-05 14:19:10 -0800364 ALOGV("[%s] [%s] Skipping event due to model error", mName,
365 eventListener.mName);
366 continue;
367 }
Ady Abraham45e4e362019-06-07 18:20:51 -0700368
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700369 CallbackInvocation ci;
Ana Krulec9a52b192018-07-12 18:18:47 -0400370 ci.mCallback = eventListener.mCallback;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700371 ci.mEventTime = t;
Ady Abraham5facfb12020-04-22 15:18:31 -0700372 ci.mExpectedVSyncTime = expectedVSyncTime;
373 if (eventListener.mPhase < 0) {
374 ci.mExpectedVSyncTime += mPeriod;
375 }
Alec Mouri7355eb22019-03-05 14:19:10 -0800376 ALOGV("[%s] [%s] Preparing to fire, latency: %" PRId64, mName, eventListener.mName,
377 t - eventListener.mLastEventTime);
Ana Krulec9a52b192018-07-12 18:18:47 -0400378 callbackInvocations.push_back(ci);
379 eventListener.mLastEventTime = t;
Alec Mouri7355eb22019-03-05 14:19:10 -0800380 eventListener.mLastCallbackTime = now;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700381 }
382 }
383
384 return callbackInvocations;
385 }
386
Lloyd Pique78ce4182018-01-31 16:39:51 -0800387 nsecs_t computeListenerNextEventTimeLocked(const EventListener& listener, nsecs_t baseTime) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700388 if (mTraceDetailedInfo) ATRACE_CALL();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800389 ALOGV("[%s] [%s] computeListenerNextEventTimeLocked(%" PRId64 ")", mName, listener.mName,
390 ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700391
Tim Murray4a4e4a22016-04-19 16:29:23 +0000392 nsecs_t lastEventTime = listener.mLastEventTime + mWakeupLatency;
393 ALOGV("[%s] lastEventTime: %" PRId64, mName, ns2us(lastEventTime));
394 if (baseTime < lastEventTime) {
395 baseTime = lastEventTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800396 ALOGV("[%s] Clamping baseTime to lastEventTime -> %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700397 }
398
Tim Murray4a4e4a22016-04-19 16:29:23 +0000399 baseTime -= mReferenceTime;
400 ALOGV("[%s] Relative baseTime = %" PRId64, mName, ns2us(baseTime));
401 nsecs_t phase = mPhase + listener.mPhase;
402 ALOGV("[%s] Phase = %" PRId64, mName, ns2us(phase));
403 baseTime -= phase;
404 ALOGV("[%s] baseTime - phase = %" PRId64, mName, ns2us(baseTime));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700405
Tim Murray4a4e4a22016-04-19 16:29:23 +0000406 // If our previous time is before the reference (because the reference
407 // has since been updated), the division by mPeriod will truncate
408 // towards zero instead of computing the floor. Since in all cases
409 // before the reference we want the next time to be effectively now, we
410 // set baseTime to -mPeriod so that numPeriods will be -1.
411 // When we add 1 and the phase, we will be at the correct event time for
412 // this period.
413 if (baseTime < 0) {
414 ALOGV("[%s] Correcting negative baseTime", mName);
415 baseTime = -mPeriod;
416 }
417
418 nsecs_t numPeriods = baseTime / mPeriod;
419 ALOGV("[%s] numPeriods = %" PRId64, mName, numPeriods);
420 nsecs_t t = (numPeriods + 1) * mPeriod + phase;
421 ALOGV("[%s] t = %" PRId64, mName, ns2us(t));
422 t += mReferenceTime;
423 ALOGV("[%s] Absolute t = %" PRId64, mName, ns2us(t));
424
425 // Check that it's been slightly more than half a period since the last
426 // event so that we don't accidentally fall into double-rate vsyncs
Alec Mouridf3d2e12019-05-02 17:58:46 +0000427 if (isCloseToPeriod(t - listener.mLastEventTime)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700428 t += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000429 ALOGV("[%s] Modifying t -> %" PRId64, mName, ns2us(t));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700430 }
431
Tim Murray4a4e4a22016-04-19 16:29:23 +0000432 t -= mWakeupLatency;
433 ALOGV("[%s] Corrected for wakeup latency -> %" PRId64, mName, ns2us(t));
434
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700435 return t;
436 }
437
Ana Krulec9a52b192018-07-12 18:18:47 -0400438 void fireCallbackInvocations(const std::vector<CallbackInvocation>& callbacks) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700439 if (mTraceDetailedInfo) ATRACE_CALL();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700440 for (size_t i = 0; i < callbacks.size(); i++) {
Ady Abraham5facfb12020-04-22 15:18:31 -0700441 callbacks[i].mCallback->onDispSyncEvent(callbacks[i].mEventTime,
442 callbacks[i].mExpectedVSyncTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700443 }
444 }
445
Ady Abraham5facfb12020-04-22 15:18:31 -0700446 nsecs_t computeNextRefreshLocked(int periodOffset, nsecs_t now) const {
447 nsecs_t phase = mReferenceTime + mPhase;
448 if (mPeriod == 0) {
449 return 0;
450 }
451 return (((now - phase) / mPeriod) + periodOffset + 1) * mPeriod + phase;
452 }
453
Tim Murray4a4e4a22016-04-19 16:29:23 +0000454 const char* const mName;
455
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700456 bool mStop;
Ady Abraham50204dd2019-07-19 15:47:11 -0700457 TracedOrdinal<bool> mModelLocked;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700458
459 nsecs_t mPeriod;
460 nsecs_t mPhase;
Haixia Shi676b1f62015-10-28 16:19:01 -0700461 nsecs_t mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700462 nsecs_t mWakeupLatency;
463
Tim Murray4a4e4a22016-04-19 16:29:23 +0000464 int64_t mFrameNumber;
465
Ana Krulec9a52b192018-07-12 18:18:47 -0400466 std::vector<EventListener> mEventListeners;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700467
Ady Abraham5facfb12020-04-22 15:18:31 -0700468 mutable Mutex mMutex;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700469 Condition mCond;
Ana Krulec064a82f2018-09-11 16:03:03 -0700470
471 // Flag to turn on logging in systrace.
472 const bool mTraceDetailedInfo;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700473};
474
Tim Murray4a4e4a22016-04-19 16:29:23 +0000475#undef LOG_TAG
476#define LOG_TAG "DispSync"
477
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700478class ZeroPhaseTracer : public DispSync::Callback {
479public:
Ady Abraham50204dd2019-07-19 15:47:11 -0700480 ZeroPhaseTracer() : mParity("ZERO_PHASE_VSYNC", false) {}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700481
Ady Abraham5facfb12020-04-22 15:18:31 -0700482 virtual void onDispSyncEvent(nsecs_t /*when*/, nsecs_t /*expectedVSyncTimestamp*/) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700483 mParity = !mParity;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700484 }
485
486private:
Ady Abraham50204dd2019-07-19 15:47:11 -0700487 TracedOrdinal<bool> mParity;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700488};
489
Dominik Laskowski98041832019-08-01 18:35:59 -0700490DispSync::DispSync(const char* name, bool hasSyncFramework)
491 : mName(name), mIgnorePresentFences(!hasSyncFramework) {
Ana Krulec064a82f2018-09-11 16:03:03 -0700492 // This flag offers the ability to turn on systrace logging from the shell.
493 char value[PROPERTY_VALUE_MAX];
494 property_get("debug.sf.dispsync_trace_detailed_info", value, "0");
495 mTraceDetailedInfo = atoi(value);
Dominik Laskowski98041832019-08-01 18:35:59 -0700496
Ana Krulec064a82f2018-09-11 16:03:03 -0700497 mThread = new DispSyncThread(name, mTraceDetailedInfo);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700498 mThread->run("DispSync", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
Saurabh Shahf4174532017-07-13 10:45:07 -0700499
Tim Murrayacff43d2016-07-29 13:57:24 -0700500 // set DispSync to SCHED_FIFO to minimize jitter
501 struct sched_param param = {0};
Tim Murray35520632016-09-07 12:18:17 -0700502 param.sched_priority = 2;
Tim Murrayacff43d2016-07-29 13:57:24 -0700503 if (sched_setscheduler(mThread->getTid(), SCHED_FIFO, &param) != 0) {
504 ALOGE("Couldn't set SCHED_FIFO for DispSyncThread");
505 }
506
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700507 beginResync();
508
Ana Krulec064a82f2018-09-11 16:03:03 -0700509 if (mTraceDetailedInfo && kEnableZeroPhaseTracer) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700510 mZeroPhaseTracer = std::make_unique<ZeroPhaseTracer>();
Alec Mouri7355eb22019-03-05 14:19:10 -0800511 addEventListener("ZeroPhaseTracer", 0, mZeroPhaseTracer.get(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700512 }
513}
514
Dominik Laskowski98041832019-08-01 18:35:59 -0700515DispSync::~DispSync() {
516 mThread->stop();
517 mThread->requestExitAndWait();
518}
519
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700520void DispSync::reset() {
521 Mutex::Autolock lock(mMutex);
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700522 resetLocked();
523}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700524
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700525void DispSync::resetLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700526 mPhase = 0;
Alec Mouri75b0b622019-03-12 18:56:00 +0000527 const size_t lastSampleIdx = (mFirstResyncSample + mNumResyncSamples - 1) % MAX_RESYNC_SAMPLES;
528 // Keep the most recent sample, when we resync to hardware we'll overwrite this
529 // with a more accurate signal
530 if (mResyncSamples[lastSampleIdx] != 0) {
531 mReferenceTime = mResyncSamples[lastSampleIdx];
532 }
Haixia Shi676b1f62015-10-28 16:19:01 -0700533 mModelUpdated = false;
Alec Mouri75b0b622019-03-12 18:56:00 +0000534 for (size_t i = 0; i < MAX_RESYNC_SAMPLES; i++) {
535 mResyncSamples[i] = 0;
536 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700537 mNumResyncSamples = 0;
538 mFirstResyncSample = 0;
539 mNumResyncSamplesSincePresent = 0;
Alec Mouri016b8dd2019-04-24 15:16:05 -0700540 mThread->unlockModel();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700541 resetErrorLocked();
542}
543
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700544bool DispSync::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700545 Mutex::Autolock lock(mMutex);
546
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700547 if (mIgnorePresentFences) {
548 return true;
549 }
550
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700551 mPresentFences[mPresentSampleOffset] = fenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700552 mPresentSampleOffset = (mPresentSampleOffset + 1) % NUM_PRESENT_SAMPLES;
553 mNumResyncSamplesSincePresent = 0;
554
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700555 updateErrorLocked();
556
Haixia Shi676b1f62015-10-28 16:19:01 -0700557 return !mModelUpdated || mError > kErrorThreshold;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700558}
559
560void DispSync::beginResync() {
561 Mutex::Autolock lock(mMutex);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000562 ALOGV("[%s] beginResync", mName);
Alec Mourif8e689c2019-05-20 18:32:22 -0700563 resetLocked();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700564}
565
Ady Abraham5dee2f12020-02-05 17:49:47 -0800566bool DispSync::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> /*hwcVsyncPeriod*/,
567 bool* periodFlushed) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700568 Mutex::Autolock lock(mMutex);
569
Tim Murray4a4e4a22016-04-19 16:29:23 +0000570 ALOGV("[%s] addResyncSample(%" PRId64 ")", mName, ns2us(timestamp));
571
Alec Mourif8e689c2019-05-20 18:32:22 -0700572 *periodFlushed = false;
Alec Mouri754c98a2019-03-18 18:53:42 -0700573 const size_t idx = (mFirstResyncSample + mNumResyncSamples) % MAX_RESYNC_SAMPLES;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700574 mResyncSamples[idx] = timestamp;
Haixia Shi664339a2015-10-28 13:22:22 -0700575 if (mNumResyncSamples == 0) {
Haixia Shi676b1f62015-10-28 16:19:01 -0700576 mPhase = 0;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000577 ALOGV("[%s] First resync sample: mPeriod = %" PRId64 ", mPhase = 0, "
Lloyd Pique78ce4182018-01-31 16:39:51 -0800578 "mReferenceTime = %" PRId64,
Alec Mouri754c98a2019-03-18 18:53:42 -0700579 mName, ns2us(mPeriod), ns2us(timestamp));
580 } else if (mPendingPeriod > 0) {
581 // mNumResyncSamples > 0, so priorIdx won't overflow
582 const size_t priorIdx = (mFirstResyncSample + mNumResyncSamples - 1) % MAX_RESYNC_SAMPLES;
583 const nsecs_t lastTimestamp = mResyncSamples[priorIdx];
584
585 const nsecs_t observedVsync = std::abs(timestamp - lastTimestamp);
Alec Mourif8e689c2019-05-20 18:32:22 -0700586 if (std::abs(observedVsync - mPendingPeriod) <= std::abs(observedVsync - mIntendedPeriod)) {
587 // Either the observed vsync is closer to the pending period, (and
588 // thus we detected a period change), or the period change will
589 // no-op. In either case, reset the model and flush the pending
590 // period.
Alec Mouri754c98a2019-03-18 18:53:42 -0700591 resetLocked();
Alec Mourif8e689c2019-05-20 18:32:22 -0700592 mIntendedPeriod = mPendingPeriod;
Alec Mouri754c98a2019-03-18 18:53:42 -0700593 mPeriod = mPendingPeriod;
594 mPendingPeriod = 0;
595 if (mTraceDetailedInfo) {
596 ATRACE_INT("DispSync:PendingPeriod", mPendingPeriod);
Alec Mourif8e689c2019-05-20 18:32:22 -0700597 ATRACE_INT("DispSync:IntendedPeriod", mIntendedPeriod);
Alec Mouri754c98a2019-03-18 18:53:42 -0700598 }
Alec Mourif8e689c2019-05-20 18:32:22 -0700599 *periodFlushed = true;
Alec Mouri754c98a2019-03-18 18:53:42 -0700600 }
Haixia Shi664339a2015-10-28 13:22:22 -0700601 }
Alec Mouri754c98a2019-03-18 18:53:42 -0700602 // Always update the reference time with the most recent timestamp.
603 mReferenceTime = timestamp;
604 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700605
606 if (mNumResyncSamples < MAX_RESYNC_SAMPLES) {
607 mNumResyncSamples++;
608 } else {
609 mFirstResyncSample = (mFirstResyncSample + 1) % MAX_RESYNC_SAMPLES;
610 }
611
612 updateModelLocked();
613
614 if (mNumResyncSamplesSincePresent++ > MAX_RESYNC_SAMPLES_WITHOUT_PRESENT) {
615 resetErrorLocked();
616 }
617
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800618 if (mIgnorePresentFences) {
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700619 // If we're ignoring the present fences we have no way to know whether
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700620 // or not we're synchronized with the HW vsyncs, so we just request
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700621 // that the HW vsync events be turned on.
622 return true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700623 }
624
Tim Murray4a4e4a22016-04-19 16:29:23 +0000625 // Check against kErrorThreshold / 2 to add some hysteresis before having to
626 // resync again
Alec Mouri754c98a2019-03-18 18:53:42 -0700627 bool modelLocked = mModelUpdated && mError < (kErrorThreshold / 2) && mPendingPeriod == 0;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800628 ALOGV("[%s] addResyncSample returning %s", mName, modelLocked ? "locked" : "unlocked");
Alec Mouri016b8dd2019-04-24 15:16:05 -0700629 if (modelLocked) {
Alec Mourif8e689c2019-05-20 18:32:22 -0700630 *periodFlushed = true;
Alec Mouri016b8dd2019-04-24 15:16:05 -0700631 mThread->lockModel();
632 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000633 return !modelLocked;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700634}
635
Alec Mouri016b8dd2019-04-24 15:16:05 -0700636void DispSync::endResync() {
637 mThread->lockModel();
638}
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700639
Alec Mouri7355eb22019-03-05 14:19:10 -0800640status_t DispSync::addEventListener(const char* name, nsecs_t phase, Callback* callback,
641 nsecs_t lastCallbackTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700642 Mutex::Autolock lock(mMutex);
Alec Mouri7355eb22019-03-05 14:19:10 -0800643 return mThread->addEventListener(name, phase, callback, lastCallbackTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700644}
645
Alec Mouri7355eb22019-03-05 14:19:10 -0800646status_t DispSync::removeEventListener(Callback* callback, nsecs_t* outLastCallbackTime) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700647 Mutex::Autolock lock(mMutex);
Alec Mouri7355eb22019-03-05 14:19:10 -0800648 return mThread->removeEventListener(callback, outLastCallbackTime);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700649}
650
Dan Stoza84d619e2018-03-28 17:07:36 -0700651status_t DispSync::changePhaseOffset(Callback* callback, nsecs_t phase) {
652 Mutex::Autolock lock(mMutex);
653 return mThread->changePhaseOffset(callback, phase);
654}
655
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700656void DispSync::setPeriod(nsecs_t period) {
657 Mutex::Autolock lock(mMutex);
Alec Mourif8e689c2019-05-20 18:32:22 -0700658
659 const bool pendingPeriodShouldChange =
660 period != mIntendedPeriod || (period == mIntendedPeriod && mPendingPeriod != 0);
661
662 if (pendingPeriodShouldChange) {
663 mPendingPeriod = period;
Alec Mouri754c98a2019-03-18 18:53:42 -0700664 }
Alec Mourif8e689c2019-05-20 18:32:22 -0700665 if (mTraceDetailedInfo) {
666 ATRACE_INT("DispSync:IntendedPeriod", mIntendedPeriod);
667 ATRACE_INT("DispSync:PendingPeriod", mPendingPeriod);
668 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700669}
670
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700671nsecs_t DispSync::getPeriod() {
672 // lock mutex as mPeriod changes multiple times in updateModelLocked
673 Mutex::Autolock lock(mMutex);
674 return mPeriod;
675}
676
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700677void DispSync::updateModelLocked() {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000678 ALOGV("[%s] updateModelLocked %zu", mName, mNumResyncSamples);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700679 if (mNumResyncSamples >= MIN_RESYNC_SAMPLES_FOR_UPDATE) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000680 ALOGV("[%s] Computing...", mName);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700681 nsecs_t durationSum = 0;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000682 nsecs_t minDuration = INT64_MAX;
683 nsecs_t maxDuration = 0;
Alec Mouri94390a32019-07-16 13:20:19 -0700684 // We skip the first 2 samples because the first vsync duration on some
685 // devices may be much more inaccurate than on other devices, e.g. due
686 // to delays in ramping up from a power collapse. By doing so this
687 // actually increases the accuracy of the DispSync model even though
688 // we're effectively relying on fewer sample points.
689 static constexpr size_t numSamplesSkipped = 2;
690 for (size_t i = numSamplesSkipped; i < mNumResyncSamples; i++) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700691 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
692 size_t prev = (idx + MAX_RESYNC_SAMPLES - 1) % MAX_RESYNC_SAMPLES;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000693 nsecs_t duration = mResyncSamples[idx] - mResyncSamples[prev];
694 durationSum += duration;
695 minDuration = min(minDuration, duration);
696 maxDuration = max(maxDuration, duration);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700697 }
698
Tim Murray4a4e4a22016-04-19 16:29:23 +0000699 // Exclude the min and max from the average
700 durationSum -= minDuration + maxDuration;
Alec Mouri94390a32019-07-16 13:20:19 -0700701 mPeriod = durationSum / (mNumResyncSamples - numSamplesSkipped - 2);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000702
703 ALOGV("[%s] mPeriod = %" PRId64, mName, ns2us(mPeriod));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700704
705 double sampleAvgX = 0;
706 double sampleAvgY = 0;
707 double scale = 2.0 * M_PI / double(mPeriod);
Alec Mouri94390a32019-07-16 13:20:19 -0700708 for (size_t i = numSamplesSkipped; i < mNumResyncSamples; i++) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700709 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
Haixia Shi676b1f62015-10-28 16:19:01 -0700710 nsecs_t sample = mResyncSamples[idx] - mReferenceTime;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700711 double samplePhase = double(sample % mPeriod) * scale;
712 sampleAvgX += cos(samplePhase);
713 sampleAvgY += sin(samplePhase);
714 }
715
Alec Mouri94390a32019-07-16 13:20:19 -0700716 sampleAvgX /= double(mNumResyncSamples - numSamplesSkipped);
717 sampleAvgY /= double(mNumResyncSamples - numSamplesSkipped);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700718
719 mPhase = nsecs_t(atan2(sampleAvgY, sampleAvgX) / scale);
720
Tim Murray4a4e4a22016-04-19 16:29:23 +0000721 ALOGV("[%s] mPhase = %" PRId64, mName, ns2us(mPhase));
722
723 if (mPhase < -(mPeriod / 2)) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700724 mPhase += mPeriod;
Tim Murray4a4e4a22016-04-19 16:29:23 +0000725 ALOGV("[%s] Adjusting mPhase -> %" PRId64, mName, ns2us(mPhase));
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700726 }
727
Haixia Shi676b1f62015-10-28 16:19:01 -0700728 mThread->updateModel(mPeriod, mPhase, mReferenceTime);
729 mModelUpdated = true;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700730 }
731}
732
733void DispSync::updateErrorLocked() {
Haixia Shi676b1f62015-10-28 16:19:01 -0700734 if (!mModelUpdated) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700735 return;
736 }
737
738 int numErrSamples = 0;
739 nsecs_t sqErrSum = 0;
740
741 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700742 // Only check for the cached value of signal time to avoid unecessary
743 // syscalls. It is the responsibility of the DispSync owner to
744 // call getSignalTime() periodically so the cache is updated when the
745 // fence signals.
746 nsecs_t time = mPresentFences[i]->getCachedSignalTime();
Lloyd Pique78ce4182018-01-31 16:39:51 -0800747 if (time == Fence::SIGNAL_TIME_PENDING || time == Fence::SIGNAL_TIME_INVALID) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700748 continue;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700749 }
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700750
751 nsecs_t sample = time - mReferenceTime;
752 if (sample <= mPhase) {
753 continue;
754 }
755
Dominik Laskowski8d32ddc2019-08-07 11:25:35 -0700756 nsecs_t sampleErr = (sample - mPhase) % mPeriod;
757 if (sampleErr > mPeriod / 2) {
758 sampleErr -= mPeriod;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700759 }
760 sqErrSum += sampleErr * sampleErr;
761 numErrSamples++;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700762 }
763
764 if (numErrSamples > 0) {
765 mError = sqErrSum / numErrSamples;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700766 mZeroErrSamplesCount = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700767 } else {
768 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700769 // Use mod ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT to avoid log spam.
770 mZeroErrSamplesCount++;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800771 ALOGE_IF((mZeroErrSamplesCount % ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT) == 0,
772 "No present times for model error.");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700773 }
774
Ana Krulec064a82f2018-09-11 16:03:03 -0700775 if (mTraceDetailedInfo) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700776 ATRACE_INT64("DispSync:Error", mError);
777 }
778}
779
780void DispSync::resetErrorLocked() {
781 mPresentSampleOffset = 0;
782 mError = 0;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700783 mZeroErrSamplesCount = 0;
Alec Mourif8e689c2019-05-20 18:32:22 -0700784 if (mTraceDetailedInfo) {
785 ATRACE_INT64("DispSync:Error", mError);
786 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700787 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700788 mPresentFences[i] = FenceTime::NO_FENCE;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700789 }
790}
791
Ady Abraham0ed31c92020-04-16 11:48:45 -0700792nsecs_t DispSync::computeNextRefresh(int periodOffset, nsecs_t now) const {
Andy McFadden150ecd82014-05-08 14:56:50 -0700793 Mutex::Autolock lock(mMutex);
Haixia Shi676b1f62015-10-28 16:19:01 -0700794 nsecs_t phase = mReferenceTime + mPhase;
Marissa Wall17b4e452018-12-26 16:32:34 -0800795 if (mPeriod == 0) {
796 return 0;
797 }
Haixia Shi676b1f62015-10-28 16:19:01 -0700798 return (((now - phase) / mPeriod) + periodOffset + 1) * mPeriod + phase;
Andy McFadden41d67d72014-04-25 16:58:34 -0700799}
800
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700801void DispSync::setIgnorePresentFences(bool ignore) {
802 Mutex::Autolock lock(mMutex);
803 if (mIgnorePresentFences != ignore) {
804 mIgnorePresentFences = ignore;
805 resetLocked();
806 }
807}
808
Yiwei Zhang5434a782018-12-05 18:06:32 -0800809void DispSync::dump(std::string& result) const {
Andy McFaddenc751e922014-05-08 14:53:26 -0700810 Mutex::Autolock lock(mMutex);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800811 StringAppendF(&result, "present fences are %s\n", mIgnorePresentFences ? "ignored" : "used");
Dominik Laskowski8d32ddc2019-08-07 11:25:35 -0700812 StringAppendF(&result, "mPeriod: %" PRId64 " ns (%.3f fps)\n", mPeriod, 1000000000.0 / mPeriod);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800813 StringAppendF(&result, "mPhase: %" PRId64 " ns\n", mPhase);
814 StringAppendF(&result, "mError: %" PRId64 " ns (sqrt=%.1f)\n", mError, sqrt(mError));
815 StringAppendF(&result, "mNumResyncSamplesSincePresent: %d (limit %d)\n",
816 mNumResyncSamplesSincePresent, MAX_RESYNC_SAMPLES_WITHOUT_PRESENT);
817 StringAppendF(&result, "mNumResyncSamples: %zd (max %d)\n", mNumResyncSamples,
818 MAX_RESYNC_SAMPLES);
Andy McFaddenc751e922014-05-08 14:53:26 -0700819
Yiwei Zhang5434a782018-12-05 18:06:32 -0800820 result.append("mResyncSamples:\n");
Andy McFaddenc751e922014-05-08 14:53:26 -0700821 nsecs_t previous = -1;
822 for (size_t i = 0; i < mNumResyncSamples; i++) {
823 size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
824 nsecs_t sampleTime = mResyncSamples[idx];
825 if (i == 0) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800826 StringAppendF(&result, " %" PRId64 "\n", sampleTime);
Andy McFaddenc751e922014-05-08 14:53:26 -0700827 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800828 StringAppendF(&result, " %" PRId64 " (+%" PRId64 ")\n", sampleTime,
829 sampleTime - previous);
Andy McFaddenc751e922014-05-08 14:53:26 -0700830 }
831 previous = sampleTime;
832 }
833
Yiwei Zhang5434a782018-12-05 18:06:32 -0800834 StringAppendF(&result, "mPresentFences [%d]:\n", NUM_PRESENT_SAMPLES);
Andy McFadden5167ec62014-05-22 13:08:43 -0700835 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700836 previous = Fence::SIGNAL_TIME_INVALID;
Andy McFaddenc751e922014-05-08 14:53:26 -0700837 for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) {
838 size_t idx = (i + mPresentSampleOffset) % NUM_PRESENT_SAMPLES;
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700839 nsecs_t presentTime = mPresentFences[idx]->getSignalTime();
840 if (presentTime == Fence::SIGNAL_TIME_PENDING) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800841 StringAppendF(&result, " [unsignaled fence]\n");
Lloyd Pique78ce4182018-01-31 16:39:51 -0800842 } else if (presentTime == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800843 StringAppendF(&result, " [invalid fence]\n");
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700844 } else if (previous == Fence::SIGNAL_TIME_PENDING ||
Lloyd Pique78ce4182018-01-31 16:39:51 -0800845 previous == Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800846 StringAppendF(&result, " %" PRId64 " (%.3f ms ago)\n", presentTime,
847 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700848 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800849 StringAppendF(&result, " %" PRId64 " (+%" PRId64 " / %.3f) (%.3f ms ago)\n",
850 presentTime, presentTime - previous,
851 (presentTime - previous) / (double)mPeriod,
852 (now - presentTime) / 1000000.0);
Andy McFaddenc751e922014-05-08 14:53:26 -0700853 }
854 previous = presentTime;
855 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700856
Yiwei Zhang5434a782018-12-05 18:06:32 -0800857 StringAppendF(&result, "current monotonic time: %" PRId64 "\n", now);
Andy McFaddenc751e922014-05-08 14:53:26 -0700858}
859
Ady Abraham0ed31c92020-04-16 11:48:45 -0700860nsecs_t DispSync::expectedPresentTime(nsecs_t now) {
Ana Krulec010d2192018-10-08 06:29:54 -0700861 // The HWC doesn't currently have a way to report additional latency.
862 // Assume that whatever we submit now will appear right after the flip.
863 // For a smart panel this might be 1. This is expressed in frames,
864 // rather than time, because we expect to have a constant frame delay
865 // regardless of the refresh rate.
866 const uint32_t hwcLatency = 0;
867
868 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
Ady Abraham5facfb12020-04-22 15:18:31 -0700869 return mThread->computeNextRefresh(hwcLatency, now);
Ana Krulec010d2192018-10-08 06:29:54 -0700870}
871
Lloyd Pique41be5d22018-06-21 13:11:48 -0700872} // namespace impl
873
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700874} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800875
876// TODO(b/129481165): remove the #pragma below and fix conversion issues
877#pragma clang diagnostic pop // ignored "-Wconversion"