| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 19 |  | 
|  | 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 Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 25 | #include <algorithm> | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 26 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> | 
| David Sodman | 1afa8b0 | 2018-10-15 11:20:48 -0700 | [diff] [blame] | 28 | #include <cutils/properties.h> | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 29 | #include <log/log.h> | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 30 | #include <utils/Thread.h> | 
|  | 31 | #include <utils/Trace.h> | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 32 |  | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 33 | #include <ui/FenceTime.h> | 
| Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 34 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 35 | #include "DispSync.h" | 
|  | 36 | #include "EventLog/EventLog.h" | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 37 | #include "SurfaceFlinger.h" | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 38 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 39 | using android::base::StringAppendF; | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 40 | using std::max; | 
|  | 41 | using std::min; | 
|  | 42 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 43 | namespace android { | 
|  | 44 |  | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 45 | DispSync::~DispSync() = default; | 
| Kevin DuBois | 7b743be | 2019-02-27 10:05:48 -0800 | [diff] [blame] | 46 | DispSync::Callback::~Callback() = default; | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 47 |  | 
|  | 48 | namespace impl { | 
|  | 49 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 50 | // Setting this to true adds a zero-phase tracer for correlating with hardware | 
|  | 51 | // vsync events | 
|  | 52 | static const bool kEnableZeroPhaseTracer = false; | 
|  | 53 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 54 | // 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 Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 58 | static const nsecs_t kErrorThreshold = 160000000000; // 400 usec squared | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 59 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 60 | #undef LOG_TAG | 
|  | 61 | #define LOG_TAG "DispSyncThread" | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 62 | class DispSyncThread : public Thread { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 63 | public: | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 64 | DispSyncThread(const char* name, bool showTraceDetailedInfo) | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 65 | : mName(name), | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 66 | mStop(false), | 
|  | 67 | mPeriod(0), | 
|  | 68 | mPhase(0), | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 69 | mReferenceTime(0), | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 70 | mWakeupLatency(0), | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 71 | mFrameNumber(0), | 
|  | 72 | mTraceDetailedInfo(showTraceDetailedInfo) {} | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 73 |  | 
|  | 74 | virtual ~DispSyncThread() {} | 
|  | 75 |  | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 76 | void updateModel(nsecs_t period, nsecs_t phase, nsecs_t referenceTime) { | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 77 | if (mTraceDetailedInfo) ATRACE_CALL(); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 78 | Mutex::Autolock lock(mMutex); | 
| Alec Mouri | 75b0b62 | 2019-03-12 18:56:00 +0000 | [diff] [blame] | 79 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 80 | mPhase = phase; | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 81 | mReferenceTime = referenceTime; | 
| Alec Mouri | 75b0b62 | 2019-03-12 18:56:00 +0000 | [diff] [blame] | 82 | 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 Mouri | f5fe85c | 2019-02-22 13:40:36 -0800 | [diff] [blame] | 91 | 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 Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 96 | ALOGV("[%s] updateModel: mPeriod = %" PRId64 ", mPhase = %" PRId64 | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 97 | " mReferenceTime = %" PRId64, | 
|  | 98 | mName, ns2us(mPeriod), ns2us(mPhase), ns2us(mReferenceTime)); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 99 | mCond.signal(); | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | void stop() { | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 103 | if (mTraceDetailedInfo) ATRACE_CALL(); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 104 | 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 Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 112 |  | 
|  | 113 | while (true) { | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 114 | std::vector<CallbackInvocation> callbackInvocations; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | nsecs_t targetTime = 0; | 
|  | 117 |  | 
|  | 118 | { // Scope for lock | 
|  | 119 | Mutex::Autolock lock(mMutex); | 
|  | 120 |  | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 121 | if (mTraceDetailedInfo) { | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 122 | ATRACE_INT64("DispSync:Frame", mFrameNumber); | 
|  | 123 | } | 
|  | 124 | ALOGV("[%s] Frame %" PRId64, mName, mFrameNumber); | 
|  | 125 | ++mFrameNumber; | 
|  | 126 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 127 | if (mStop) { | 
|  | 128 | return false; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | if (mPeriod == 0) { | 
|  | 132 | err = mCond.wait(mMutex); | 
|  | 133 | if (err != NO_ERROR) { | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 134 | ALOGE("error waiting for new events: %s (%d)", strerror(-err), err); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 135 | return false; | 
|  | 136 | } | 
|  | 137 | continue; | 
|  | 138 | } | 
|  | 139 |  | 
| Dan Stoza | 8f8374d | 2016-04-19 10:03:46 -0700 | [diff] [blame] | 140 | targetTime = computeNextEventTimeLocked(now); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 141 |  | 
|  | 142 | bool isWakeup = false; | 
|  | 143 |  | 
|  | 144 | if (now < targetTime) { | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 145 | if (mTraceDetailedInfo) ATRACE_NAME("DispSync waiting"); | 
| Dan Stoza | 8f8374d | 2016-04-19 10:03:46 -0700 | [diff] [blame] | 146 |  | 
|  | 147 | if (targetTime == INT64_MAX) { | 
|  | 148 | ALOGV("[%s] Waiting forever", mName); | 
|  | 149 | err = mCond.wait(mMutex); | 
|  | 150 | } else { | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 151 | ALOGV("[%s] Waiting until %" PRId64, mName, ns2us(targetTime)); | 
| Dan Stoza | 8f8374d | 2016-04-19 10:03:46 -0700 | [diff] [blame] | 152 | err = mCond.waitRelative(mMutex, targetTime - now); | 
|  | 153 | } | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 154 |  | 
|  | 155 | if (err == TIMED_OUT) { | 
|  | 156 | isWakeup = true; | 
|  | 157 | } else if (err != NO_ERROR) { | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 158 | ALOGE("error waiting for next event: %s (%d)", strerror(-err), err); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 159 | return false; | 
|  | 160 | } | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | now = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 164 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 165 | // Don't correct by more than 1.5 ms | 
|  | 166 | static const nsecs_t kMaxWakeupLatency = us2ns(1500); | 
|  | 167 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 168 | if (isWakeup) { | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 169 | mWakeupLatency = ((mWakeupLatency * 63) + (now - targetTime)) / 64; | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 170 | mWakeupLatency = min(mWakeupLatency, kMaxWakeupLatency); | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 171 | if (mTraceDetailedInfo) { | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 172 | ATRACE_INT64("DispSync:WakeupLat", now - targetTime); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 173 | ATRACE_INT64("DispSync:AvgWakeupLat", mWakeupLatency); | 
|  | 174 | } | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | callbackInvocations = gatherCallbackInvocationsLocked(now); | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | if (callbackInvocations.size() > 0) { | 
| Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 181 | fireCallbackInvocations(callbackInvocations); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 182 | } | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | return false; | 
|  | 186 | } | 
|  | 187 |  | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 188 | status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback, | 
|  | 189 | nsecs_t lastCallbackTime) { | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 190 | if (mTraceDetailedInfo) ATRACE_CALL(); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 191 | 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 Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 200 | listener.mName = name; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 201 | listener.mPhase = phase; | 
|  | 202 | listener.mCallback = callback; | 
| Jamie Gennis | 629b987 | 2013-10-29 13:36:12 -0700 | [diff] [blame] | 203 |  | 
|  | 204 | // We want to allow the firstmost future event to fire without | 
| Alec Mouri | 8183598 | 2019-02-27 13:40:44 -0800 | [diff] [blame] | 205 | // 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 Mouri | df3d2e1 | 2019-05-02 17:58:46 +0000 | [diff] [blame] | 213 | const nsecs_t phaseCorrection = mPhase + listener.mPhase; | 
|  | 214 | const nsecs_t predictedLastEventTime = predictedReference + phaseCorrection; | 
|  | 215 | if (predictedLastEventTime >= now) { | 
|  | 216 | // Make sure that the last event time does not exceed the current time. | 
|  | 217 | // If it would, then back the last event time by a period. | 
|  | 218 | listener.mLastEventTime = predictedLastEventTime - mPeriod; | 
|  | 219 | } else { | 
|  | 220 | listener.mLastEventTime = predictedLastEventTime; | 
| Alec Mouri | 8183598 | 2019-02-27 13:40:44 -0800 | [diff] [blame] | 221 | } | 
|  | 222 | } else { | 
|  | 223 | listener.mLastEventTime = now + mPhase - mWakeupLatency; | 
|  | 224 | } | 
| Jamie Gennis | 629b987 | 2013-10-29 13:36:12 -0700 | [diff] [blame] | 225 |  | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 226 | if (lastCallbackTime <= 0) { | 
|  | 227 | // If there is no prior callback time, try to infer one based on the | 
|  | 228 | // logical last event time. | 
|  | 229 | listener.mLastCallbackTime = listener.mLastEventTime + mWakeupLatency; | 
|  | 230 | } else { | 
|  | 231 | listener.mLastCallbackTime = lastCallbackTime; | 
|  | 232 | } | 
|  | 233 |  | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 234 | mEventListeners.push_back(listener); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 235 |  | 
|  | 236 | mCond.signal(); | 
|  | 237 |  | 
|  | 238 | return NO_ERROR; | 
|  | 239 | } | 
|  | 240 |  | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 241 | status_t removeEventListener(DispSync::Callback* callback, nsecs_t* outLastCallback) { | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 242 | if (mTraceDetailedInfo) ATRACE_CALL(); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 243 | Mutex::Autolock lock(mMutex); | 
|  | 244 |  | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 245 | for (std::vector<EventListener>::iterator it = mEventListeners.begin(); | 
|  | 246 | it != mEventListeners.end(); ++it) { | 
|  | 247 | if (it->mCallback == callback) { | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 248 | *outLastCallback = it->mLastCallbackTime; | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 249 | mEventListeners.erase(it); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 250 | mCond.signal(); | 
|  | 251 | return NO_ERROR; | 
|  | 252 | } | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | return BAD_VALUE; | 
|  | 256 | } | 
|  | 257 |  | 
| Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 258 | status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) { | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 259 | if (mTraceDetailedInfo) ATRACE_CALL(); | 
| Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 260 | Mutex::Autolock lock(mMutex); | 
|  | 261 |  | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 262 | for (auto& eventListener : mEventListeners) { | 
|  | 263 | if (eventListener.mCallback == callback) { | 
|  | 264 | const nsecs_t oldPhase = eventListener.mPhase; | 
|  | 265 | eventListener.mPhase = phase; | 
| Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 266 |  | 
|  | 267 | // Pretend that the last time this event was handled at the same frame but with the | 
|  | 268 | // new offset to allow for a seamless offset change without double-firing or | 
|  | 269 | // skipping. | 
| Jorim Jaggi | 22ec38b | 2018-06-19 15:57:08 +0200 | [diff] [blame] | 270 | nsecs_t diff = oldPhase - phase; | 
|  | 271 | if (diff > mPeriod / 2) { | 
|  | 272 | diff -= mPeriod; | 
|  | 273 | } else if (diff < -mPeriod / 2) { | 
|  | 274 | diff += mPeriod; | 
|  | 275 | } | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 276 | eventListener.mLastEventTime -= diff; | 
| Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 277 | mCond.signal(); | 
|  | 278 | return NO_ERROR; | 
|  | 279 | } | 
|  | 280 | } | 
| Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 281 | return BAD_VALUE; | 
|  | 282 | } | 
|  | 283 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 284 | private: | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 285 | struct EventListener { | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 286 | const char* mName; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 287 | nsecs_t mPhase; | 
|  | 288 | nsecs_t mLastEventTime; | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 289 | nsecs_t mLastCallbackTime; | 
| Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 290 | DispSync::Callback* mCallback; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 291 | }; | 
|  | 292 |  | 
|  | 293 | struct CallbackInvocation { | 
| Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 294 | DispSync::Callback* mCallback; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 295 | nsecs_t mEventTime; | 
|  | 296 | }; | 
|  | 297 |  | 
|  | 298 | nsecs_t computeNextEventTimeLocked(nsecs_t now) { | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 299 | if (mTraceDetailedInfo) ATRACE_CALL(); | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 300 | ALOGV("[%s] computeNextEventTimeLocked", mName); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 301 | nsecs_t nextEventTime = INT64_MAX; | 
|  | 302 | for (size_t i = 0; i < mEventListeners.size(); i++) { | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 303 | nsecs_t t = computeListenerNextEventTimeLocked(mEventListeners[i], now); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 304 |  | 
|  | 305 | if (t < nextEventTime) { | 
|  | 306 | nextEventTime = t; | 
|  | 307 | } | 
|  | 308 | } | 
|  | 309 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 310 | ALOGV("[%s] nextEventTime = %" PRId64, mName, ns2us(nextEventTime)); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 311 | return nextEventTime; | 
|  | 312 | } | 
|  | 313 |  | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 314 | // Sanity check that the duration is close enough in length to a period without | 
|  | 315 | // falling into double-rate vsyncs. | 
| Alec Mouri | df3d2e1 | 2019-05-02 17:58:46 +0000 | [diff] [blame] | 316 | bool isCloseToPeriod(nsecs_t duration) { | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 317 | // Ratio of 3/5 is arbitrary, but it must be greater than 1/2. | 
|  | 318 | return duration < (3 * mPeriod) / 5; | 
|  | 319 | } | 
|  | 320 |  | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 321 | std::vector<CallbackInvocation> gatherCallbackInvocationsLocked(nsecs_t now) { | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 322 | if (mTraceDetailedInfo) ATRACE_CALL(); | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 323 | ALOGV("[%s] gatherCallbackInvocationsLocked @ %" PRId64, mName, ns2us(now)); | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 324 |  | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 325 | std::vector<CallbackInvocation> callbackInvocations; | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 326 | nsecs_t onePeriodAgo = now - mPeriod; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 327 |  | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 328 | for (auto& eventListener : mEventListeners) { | 
|  | 329 | nsecs_t t = computeListenerNextEventTimeLocked(eventListener, onePeriodAgo); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 330 |  | 
| Jamie Gennis | 0d5c60e | 2013-10-09 17:49:37 -0700 | [diff] [blame] | 331 | if (t < now) { | 
| Alec Mouri | df3d2e1 | 2019-05-02 17:58:46 +0000 | [diff] [blame] | 332 | if (isCloseToPeriod(now - eventListener.mLastCallbackTime)) { | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 333 | eventListener.mLastEventTime = t; | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 334 | ALOGV("[%s] [%s] Skipping event due to model error", mName, | 
|  | 335 | eventListener.mName); | 
|  | 336 | continue; | 
|  | 337 | } | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 338 | CallbackInvocation ci; | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 339 | ci.mCallback = eventListener.mCallback; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 340 | ci.mEventTime = t; | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 341 | ALOGV("[%s] [%s] Preparing to fire, latency: %" PRId64, mName, eventListener.mName, | 
|  | 342 | t - eventListener.mLastEventTime); | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 343 | callbackInvocations.push_back(ci); | 
|  | 344 | eventListener.mLastEventTime = t; | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 345 | eventListener.mLastCallbackTime = now; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 346 | } | 
|  | 347 | } | 
|  | 348 |  | 
|  | 349 | return callbackInvocations; | 
|  | 350 | } | 
|  | 351 |  | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 352 | nsecs_t computeListenerNextEventTimeLocked(const EventListener& listener, nsecs_t baseTime) { | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 353 | if (mTraceDetailedInfo) ATRACE_CALL(); | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 354 | ALOGV("[%s] [%s] computeListenerNextEventTimeLocked(%" PRId64 ")", mName, listener.mName, | 
|  | 355 | ns2us(baseTime)); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 356 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 357 | nsecs_t lastEventTime = listener.mLastEventTime + mWakeupLatency; | 
|  | 358 | ALOGV("[%s] lastEventTime: %" PRId64, mName, ns2us(lastEventTime)); | 
|  | 359 | if (baseTime < lastEventTime) { | 
|  | 360 | baseTime = lastEventTime; | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 361 | ALOGV("[%s] Clamping baseTime to lastEventTime -> %" PRId64, mName, ns2us(baseTime)); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 362 | } | 
|  | 363 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 364 | baseTime -= mReferenceTime; | 
|  | 365 | ALOGV("[%s] Relative baseTime = %" PRId64, mName, ns2us(baseTime)); | 
|  | 366 | nsecs_t phase = mPhase + listener.mPhase; | 
|  | 367 | ALOGV("[%s] Phase = %" PRId64, mName, ns2us(phase)); | 
|  | 368 | baseTime -= phase; | 
|  | 369 | ALOGV("[%s] baseTime - phase = %" PRId64, mName, ns2us(baseTime)); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 370 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 371 | // If our previous time is before the reference (because the reference | 
|  | 372 | // has since been updated), the division by mPeriod will truncate | 
|  | 373 | // towards zero instead of computing the floor. Since in all cases | 
|  | 374 | // before the reference we want the next time to be effectively now, we | 
|  | 375 | // set baseTime to -mPeriod so that numPeriods will be -1. | 
|  | 376 | // When we add 1 and the phase, we will be at the correct event time for | 
|  | 377 | // this period. | 
|  | 378 | if (baseTime < 0) { | 
|  | 379 | ALOGV("[%s] Correcting negative baseTime", mName); | 
|  | 380 | baseTime = -mPeriod; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | nsecs_t numPeriods = baseTime / mPeriod; | 
|  | 384 | ALOGV("[%s] numPeriods = %" PRId64, mName, numPeriods); | 
|  | 385 | nsecs_t t = (numPeriods + 1) * mPeriod + phase; | 
|  | 386 | ALOGV("[%s] t = %" PRId64, mName, ns2us(t)); | 
|  | 387 | t += mReferenceTime; | 
|  | 388 | ALOGV("[%s] Absolute t = %" PRId64, mName, ns2us(t)); | 
|  | 389 |  | 
|  | 390 | // Check that it's been slightly more than half a period since the last | 
|  | 391 | // event so that we don't accidentally fall into double-rate vsyncs | 
| Alec Mouri | df3d2e1 | 2019-05-02 17:58:46 +0000 | [diff] [blame] | 392 | if (isCloseToPeriod(t - listener.mLastEventTime)) { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 393 | t += mPeriod; | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 394 | ALOGV("[%s] Modifying t -> %" PRId64, mName, ns2us(t)); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 395 | } | 
|  | 396 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 397 | t -= mWakeupLatency; | 
|  | 398 | ALOGV("[%s] Corrected for wakeup latency -> %" PRId64, mName, ns2us(t)); | 
|  | 399 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 400 | return t; | 
|  | 401 | } | 
|  | 402 |  | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 403 | void fireCallbackInvocations(const std::vector<CallbackInvocation>& callbacks) { | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 404 | if (mTraceDetailedInfo) ATRACE_CALL(); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 405 | for (size_t i = 0; i < callbacks.size(); i++) { | 
|  | 406 | callbacks[i].mCallback->onDispSyncEvent(callbacks[i].mEventTime); | 
|  | 407 | } | 
|  | 408 | } | 
|  | 409 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 410 | const char* const mName; | 
|  | 411 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 412 | bool mStop; | 
|  | 413 |  | 
|  | 414 | nsecs_t mPeriod; | 
|  | 415 | nsecs_t mPhase; | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 416 | nsecs_t mReferenceTime; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 417 | nsecs_t mWakeupLatency; | 
|  | 418 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 419 | int64_t mFrameNumber; | 
|  | 420 |  | 
| Ana Krulec | 9a52b19 | 2018-07-12 18:18:47 -0400 | [diff] [blame] | 421 | std::vector<EventListener> mEventListeners; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 422 |  | 
|  | 423 | Mutex mMutex; | 
|  | 424 | Condition mCond; | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 425 |  | 
|  | 426 | // Flag to turn on logging in systrace. | 
|  | 427 | const bool mTraceDetailedInfo; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 428 | }; | 
|  | 429 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 430 | #undef LOG_TAG | 
|  | 431 | #define LOG_TAG "DispSync" | 
|  | 432 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 433 | class ZeroPhaseTracer : public DispSync::Callback { | 
|  | 434 | public: | 
|  | 435 | ZeroPhaseTracer() : mParity(false) {} | 
|  | 436 |  | 
| Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 437 | virtual void onDispSyncEvent(nsecs_t /*when*/) { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 438 | mParity = !mParity; | 
|  | 439 | ATRACE_INT("ZERO_PHASE_VSYNC", mParity ? 1 : 0); | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | private: | 
|  | 443 | bool mParity; | 
|  | 444 | }; | 
|  | 445 |  | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 446 | DispSync::DispSync(const char* name) : mName(name), mRefreshSkipCount(0) { | 
|  | 447 | // This flag offers the ability to turn on systrace logging from the shell. | 
|  | 448 | char value[PROPERTY_VALUE_MAX]; | 
|  | 449 | property_get("debug.sf.dispsync_trace_detailed_info", value, "0"); | 
|  | 450 | mTraceDetailedInfo = atoi(value); | 
|  | 451 | mThread = new DispSyncThread(name, mTraceDetailedInfo); | 
|  | 452 | } | 
| Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 453 |  | 
| Ady Abraham | 50c202a | 2019-03-14 11:44:38 -0700 | [diff] [blame] | 454 | DispSync::~DispSync() { | 
|  | 455 | mThread->stop(); | 
|  | 456 | mThread->requestExitAndWait(); | 
|  | 457 | } | 
| Saurabh Shah | f417453 | 2017-07-13 10:45:07 -0700 | [diff] [blame] | 458 |  | 
|  | 459 | void DispSync::init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset) { | 
|  | 460 | mIgnorePresentFences = !hasSyncFramework; | 
|  | 461 | mPresentTimeOffset = dispSyncPresentTimeOffset; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 462 | mThread->run("DispSync", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE); | 
| Saurabh Shah | f417453 | 2017-07-13 10:45:07 -0700 | [diff] [blame] | 463 |  | 
| Tim Murray | acff43d | 2016-07-29 13:57:24 -0700 | [diff] [blame] | 464 | // set DispSync to SCHED_FIFO to minimize jitter | 
|  | 465 | struct sched_param param = {0}; | 
| Tim Murray | 3552063 | 2016-09-07 12:18:17 -0700 | [diff] [blame] | 466 | param.sched_priority = 2; | 
| Tim Murray | acff43d | 2016-07-29 13:57:24 -0700 | [diff] [blame] | 467 | if (sched_setscheduler(mThread->getTid(), SCHED_FIFO, ¶m) != 0) { | 
|  | 468 | ALOGE("Couldn't set SCHED_FIFO for DispSyncThread"); | 
|  | 469 | } | 
|  | 470 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 471 | reset(); | 
|  | 472 | beginResync(); | 
|  | 473 |  | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 474 | if (mTraceDetailedInfo && kEnableZeroPhaseTracer) { | 
| Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 475 | mZeroPhaseTracer = std::make_unique<ZeroPhaseTracer>(); | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 476 | addEventListener("ZeroPhaseTracer", 0, mZeroPhaseTracer.get(), 0); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 477 | } | 
|  | 478 | } | 
|  | 479 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 480 | void DispSync::reset() { | 
|  | 481 | Mutex::Autolock lock(mMutex); | 
| Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 482 | resetLocked(); | 
|  | 483 | } | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 484 |  | 
| Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 485 | void DispSync::resetLocked() { | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 486 | mPhase = 0; | 
| Alec Mouri | 75b0b62 | 2019-03-12 18:56:00 +0000 | [diff] [blame] | 487 | const size_t lastSampleIdx = (mFirstResyncSample + mNumResyncSamples - 1) % MAX_RESYNC_SAMPLES; | 
|  | 488 | // Keep the most recent sample, when we resync to hardware we'll overwrite this | 
|  | 489 | // with a more accurate signal | 
|  | 490 | if (mResyncSamples[lastSampleIdx] != 0) { | 
|  | 491 | mReferenceTime = mResyncSamples[lastSampleIdx]; | 
|  | 492 | } | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 493 | mModelUpdated = false; | 
| Alec Mouri | 75b0b62 | 2019-03-12 18:56:00 +0000 | [diff] [blame] | 494 | for (size_t i = 0; i < MAX_RESYNC_SAMPLES; i++) { | 
|  | 495 | mResyncSamples[i] = 0; | 
|  | 496 | } | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 497 | mNumResyncSamples = 0; | 
|  | 498 | mFirstResyncSample = 0; | 
|  | 499 | mNumResyncSamplesSincePresent = 0; | 
|  | 500 | resetErrorLocked(); | 
|  | 501 | } | 
|  | 502 |  | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 503 | bool DispSync::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 504 | Mutex::Autolock lock(mMutex); | 
|  | 505 |  | 
| Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 506 | if (mIgnorePresentFences) { | 
|  | 507 | return true; | 
|  | 508 | } | 
|  | 509 |  | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 510 | mPresentFences[mPresentSampleOffset] = fenceTime; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 511 | mPresentSampleOffset = (mPresentSampleOffset + 1) % NUM_PRESENT_SAMPLES; | 
|  | 512 | mNumResyncSamplesSincePresent = 0; | 
|  | 513 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 514 | updateErrorLocked(); | 
|  | 515 |  | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 516 | return !mModelUpdated || mError > kErrorThreshold; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 517 | } | 
|  | 518 |  | 
|  | 519 | void DispSync::beginResync() { | 
|  | 520 | Mutex::Autolock lock(mMutex); | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 521 | ALOGV("[%s] beginResync", mName); | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 522 | mModelUpdated = false; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 523 | mNumResyncSamples = 0; | 
|  | 524 | } | 
|  | 525 |  | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 526 | bool DispSync::addResyncSample(nsecs_t timestamp, bool* periodChanged) { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 527 | Mutex::Autolock lock(mMutex); | 
|  | 528 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 529 | ALOGV("[%s] addResyncSample(%" PRId64 ")", mName, ns2us(timestamp)); | 
|  | 530 |  | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 531 | *periodChanged = false; | 
|  | 532 | const size_t idx = (mFirstResyncSample + mNumResyncSamples) % MAX_RESYNC_SAMPLES; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 533 | mResyncSamples[idx] = timestamp; | 
| Haixia Shi | 664339a | 2015-10-28 13:22:22 -0700 | [diff] [blame] | 534 | if (mNumResyncSamples == 0) { | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 535 | mPhase = 0; | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 536 | ALOGV("[%s] First resync sample: mPeriod = %" PRId64 ", mPhase = 0, " | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 537 | "mReferenceTime = %" PRId64, | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 538 | mName, ns2us(mPeriod), ns2us(timestamp)); | 
|  | 539 | } else if (mPendingPeriod > 0) { | 
|  | 540 | // mNumResyncSamples > 0, so priorIdx won't overflow | 
|  | 541 | const size_t priorIdx = (mFirstResyncSample + mNumResyncSamples - 1) % MAX_RESYNC_SAMPLES; | 
|  | 542 | const nsecs_t lastTimestamp = mResyncSamples[priorIdx]; | 
|  | 543 |  | 
|  | 544 | const nsecs_t observedVsync = std::abs(timestamp - lastTimestamp); | 
|  | 545 | if (std::abs(observedVsync - mPendingPeriod) < std::abs(observedVsync - mPeriod)) { | 
|  | 546 | // Observed vsync is closer to the pending period, so reset the | 
|  | 547 | // model and flush the pending period. | 
|  | 548 | resetLocked(); | 
|  | 549 | mPeriod = mPendingPeriod; | 
|  | 550 | mPendingPeriod = 0; | 
|  | 551 | if (mTraceDetailedInfo) { | 
|  | 552 | ATRACE_INT("DispSync:PendingPeriod", mPendingPeriod); | 
|  | 553 | } | 
|  | 554 | *periodChanged = true; | 
|  | 555 | } | 
| Haixia Shi | 664339a | 2015-10-28 13:22:22 -0700 | [diff] [blame] | 556 | } | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 557 | // Always update the reference time with the most recent timestamp. | 
|  | 558 | mReferenceTime = timestamp; | 
|  | 559 | mThread->updateModel(mPeriod, mPhase, mReferenceTime); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 560 |  | 
|  | 561 | if (mNumResyncSamples < MAX_RESYNC_SAMPLES) { | 
|  | 562 | mNumResyncSamples++; | 
|  | 563 | } else { | 
|  | 564 | mFirstResyncSample = (mFirstResyncSample + 1) % MAX_RESYNC_SAMPLES; | 
|  | 565 | } | 
|  | 566 |  | 
|  | 567 | updateModelLocked(); | 
|  | 568 |  | 
|  | 569 | if (mNumResyncSamplesSincePresent++ > MAX_RESYNC_SAMPLES_WITHOUT_PRESENT) { | 
|  | 570 | resetErrorLocked(); | 
|  | 571 | } | 
|  | 572 |  | 
| Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 573 | if (mIgnorePresentFences) { | 
| Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 574 | // If we're ignoring the present fences we have no way to know whether | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 575 | // or not we're synchronized with the HW vsyncs, so we just request | 
| Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 576 | // that the HW vsync events be turned on. | 
|  | 577 | return true; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 578 | } | 
|  | 579 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 580 | // Check against kErrorThreshold / 2 to add some hysteresis before having to | 
|  | 581 | // resync again | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 582 | bool modelLocked = mModelUpdated && mError < (kErrorThreshold / 2) && mPendingPeriod == 0; | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 583 | ALOGV("[%s] addResyncSample returning %s", mName, modelLocked ? "locked" : "unlocked"); | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 584 | return !modelLocked; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 585 | } | 
|  | 586 |  | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 587 | void DispSync::endResync() {} | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 588 |  | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 589 | status_t DispSync::addEventListener(const char* name, nsecs_t phase, Callback* callback, | 
|  | 590 | nsecs_t lastCallbackTime) { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 591 | Mutex::Autolock lock(mMutex); | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 592 | return mThread->addEventListener(name, phase, callback, lastCallbackTime); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 593 | } | 
|  | 594 |  | 
| Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 595 | void DispSync::setRefreshSkipCount(int count) { | 
|  | 596 | Mutex::Autolock lock(mMutex); | 
|  | 597 | ALOGD("setRefreshSkipCount(%d)", count); | 
|  | 598 | mRefreshSkipCount = count; | 
|  | 599 | updateModelLocked(); | 
| Ruchi Kandoi | f52b3c8 | 2014-04-24 16:42:35 -0700 | [diff] [blame] | 600 | } | 
|  | 601 |  | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 602 | status_t DispSync::removeEventListener(Callback* callback, nsecs_t* outLastCallbackTime) { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 603 | Mutex::Autolock lock(mMutex); | 
| Alec Mouri | 7355eb2 | 2019-03-05 14:19:10 -0800 | [diff] [blame] | 604 | return mThread->removeEventListener(callback, outLastCallbackTime); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 605 | } | 
|  | 606 |  | 
| Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 607 | status_t DispSync::changePhaseOffset(Callback* callback, nsecs_t phase) { | 
|  | 608 | Mutex::Autolock lock(mMutex); | 
|  | 609 | return mThread->changePhaseOffset(callback, phase); | 
|  | 610 | } | 
|  | 611 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 612 | void DispSync::setPeriod(nsecs_t period) { | 
|  | 613 | Mutex::Autolock lock(mMutex); | 
| Alec Mouri | 754c98a | 2019-03-18 18:53:42 -0700 | [diff] [blame] | 614 | if (mTraceDetailedInfo) { | 
|  | 615 | ATRACE_INT("DispSync:PendingPeriod", period); | 
|  | 616 | } | 
|  | 617 | mPendingPeriod = period; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 618 | } | 
|  | 619 |  | 
| Lajos Molnar | 67d8bd6 | 2014-09-11 14:58:45 -0700 | [diff] [blame] | 620 | nsecs_t DispSync::getPeriod() { | 
|  | 621 | // lock mutex as mPeriod changes multiple times in updateModelLocked | 
|  | 622 | Mutex::Autolock lock(mMutex); | 
|  | 623 | return mPeriod; | 
|  | 624 | } | 
|  | 625 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 626 | void DispSync::updateModelLocked() { | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 627 | ALOGV("[%s] updateModelLocked %zu", mName, mNumResyncSamples); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 628 | if (mNumResyncSamples >= MIN_RESYNC_SAMPLES_FOR_UPDATE) { | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 629 | ALOGV("[%s] Computing...", mName); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 630 | nsecs_t durationSum = 0; | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 631 | nsecs_t minDuration = INT64_MAX; | 
|  | 632 | nsecs_t maxDuration = 0; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 633 | for (size_t i = 1; i < mNumResyncSamples; i++) { | 
|  | 634 | size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES; | 
|  | 635 | size_t prev = (idx + MAX_RESYNC_SAMPLES - 1) % MAX_RESYNC_SAMPLES; | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 636 | nsecs_t duration = mResyncSamples[idx] - mResyncSamples[prev]; | 
|  | 637 | durationSum += duration; | 
|  | 638 | minDuration = min(minDuration, duration); | 
|  | 639 | maxDuration = max(maxDuration, duration); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 640 | } | 
|  | 641 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 642 | // Exclude the min and max from the average | 
|  | 643 | durationSum -= minDuration + maxDuration; | 
| David Sodman | 1afa8b0 | 2018-10-15 11:20:48 -0700 | [diff] [blame] | 644 | mPeriod = durationSum / (mNumResyncSamples - 3); | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 645 |  | 
|  | 646 | ALOGV("[%s] mPeriod = %" PRId64, mName, ns2us(mPeriod)); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 647 |  | 
|  | 648 | double sampleAvgX = 0; | 
|  | 649 | double sampleAvgY = 0; | 
|  | 650 | double scale = 2.0 * M_PI / double(mPeriod); | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 651 | // Intentionally skip the first sample | 
|  | 652 | for (size_t i = 1; i < mNumResyncSamples; i++) { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 653 | size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES; | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 654 | nsecs_t sample = mResyncSamples[idx] - mReferenceTime; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 655 | double samplePhase = double(sample % mPeriod) * scale; | 
|  | 656 | sampleAvgX += cos(samplePhase); | 
|  | 657 | sampleAvgY += sin(samplePhase); | 
|  | 658 | } | 
|  | 659 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 660 | sampleAvgX /= double(mNumResyncSamples - 1); | 
|  | 661 | sampleAvgY /= double(mNumResyncSamples - 1); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 662 |  | 
|  | 663 | mPhase = nsecs_t(atan2(sampleAvgY, sampleAvgX) / scale); | 
|  | 664 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 665 | ALOGV("[%s] mPhase = %" PRId64, mName, ns2us(mPhase)); | 
|  | 666 |  | 
|  | 667 | if (mPhase < -(mPeriod / 2)) { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 668 | mPhase += mPeriod; | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 669 | ALOGV("[%s] Adjusting mPhase -> %" PRId64, mName, ns2us(mPhase)); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 670 | } | 
|  | 671 |  | 
| Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 672 | // Artificially inflate the period if requested. | 
|  | 673 | mPeriod += mPeriod * mRefreshSkipCount; | 
|  | 674 |  | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 675 | mThread->updateModel(mPeriod, mPhase, mReferenceTime); | 
|  | 676 | mModelUpdated = true; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 677 | } | 
|  | 678 | } | 
|  | 679 |  | 
|  | 680 | void DispSync::updateErrorLocked() { | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 681 | if (!mModelUpdated) { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 682 | return; | 
|  | 683 | } | 
|  | 684 |  | 
| Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 685 | // Need to compare present fences against the un-adjusted refresh period, | 
|  | 686 | // since they might arrive between two events. | 
|  | 687 | nsecs_t period = mPeriod / (1 + mRefreshSkipCount); | 
|  | 688 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 689 | int numErrSamples = 0; | 
|  | 690 | nsecs_t sqErrSum = 0; | 
|  | 691 |  | 
|  | 692 | for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) { | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 693 | // Only check for the cached value of signal time to avoid unecessary | 
|  | 694 | // syscalls. It is the responsibility of the DispSync owner to | 
|  | 695 | // call getSignalTime() periodically so the cache is updated when the | 
|  | 696 | // fence signals. | 
|  | 697 | nsecs_t time = mPresentFences[i]->getCachedSignalTime(); | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 698 | if (time == Fence::SIGNAL_TIME_PENDING || time == Fence::SIGNAL_TIME_INVALID) { | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 699 | continue; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 700 | } | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 701 |  | 
|  | 702 | nsecs_t sample = time - mReferenceTime; | 
|  | 703 | if (sample <= mPhase) { | 
|  | 704 | continue; | 
|  | 705 | } | 
|  | 706 |  | 
|  | 707 | nsecs_t sampleErr = (sample - mPhase) % period; | 
|  | 708 | if (sampleErr > period / 2) { | 
|  | 709 | sampleErr -= period; | 
|  | 710 | } | 
|  | 711 | sqErrSum += sampleErr * sampleErr; | 
|  | 712 | numErrSamples++; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 713 | } | 
|  | 714 |  | 
|  | 715 | if (numErrSamples > 0) { | 
|  | 716 | mError = sqErrSum / numErrSamples; | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 717 | mZeroErrSamplesCount = 0; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 718 | } else { | 
|  | 719 | mError = 0; | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 720 | // Use mod ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT to avoid log spam. | 
|  | 721 | mZeroErrSamplesCount++; | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 722 | ALOGE_IF((mZeroErrSamplesCount % ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT) == 0, | 
|  | 723 | "No present times for model error."); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 724 | } | 
|  | 725 |  | 
| Ana Krulec | 064a82f | 2018-09-11 16:03:03 -0700 | [diff] [blame] | 726 | if (mTraceDetailedInfo) { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 727 | ATRACE_INT64("DispSync:Error", mError); | 
|  | 728 | } | 
|  | 729 | } | 
|  | 730 |  | 
|  | 731 | void DispSync::resetErrorLocked() { | 
|  | 732 | mPresentSampleOffset = 0; | 
|  | 733 | mError = 0; | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 734 | mZeroErrSamplesCount = 0; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 735 | for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) { | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 736 | mPresentFences[i] = FenceTime::NO_FENCE; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 737 | } | 
|  | 738 | } | 
|  | 739 |  | 
| Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 740 | nsecs_t DispSync::computeNextRefresh(int periodOffset) const { | 
| Andy McFadden | 150ecd8 | 2014-05-08 14:56:50 -0700 | [diff] [blame] | 741 | Mutex::Autolock lock(mMutex); | 
| Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 742 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 743 | nsecs_t phase = mReferenceTime + mPhase; | 
| Marissa Wall | 17b4e45 | 2018-12-26 16:32:34 -0800 | [diff] [blame] | 744 | if (mPeriod == 0) { | 
|  | 745 | return 0; | 
|  | 746 | } | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 747 | return (((now - phase) / mPeriod) + periodOffset + 1) * mPeriod + phase; | 
| Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 748 | } | 
|  | 749 |  | 
| Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 750 | void DispSync::setIgnorePresentFences(bool ignore) { | 
|  | 751 | Mutex::Autolock lock(mMutex); | 
|  | 752 | if (mIgnorePresentFences != ignore) { | 
|  | 753 | mIgnorePresentFences = ignore; | 
|  | 754 | resetLocked(); | 
|  | 755 | } | 
|  | 756 | } | 
|  | 757 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 758 | void DispSync::dump(std::string& result) const { | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 759 | Mutex::Autolock lock(mMutex); | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 760 | StringAppendF(&result, "present fences are %s\n", mIgnorePresentFences ? "ignored" : "used"); | 
|  | 761 | StringAppendF(&result, "mPeriod: %" PRId64 " ns (%.3f fps; skipCount=%d)\n", mPeriod, | 
|  | 762 | 1000000000.0 / mPeriod, mRefreshSkipCount); | 
|  | 763 | StringAppendF(&result, "mPhase: %" PRId64 " ns\n", mPhase); | 
|  | 764 | StringAppendF(&result, "mError: %" PRId64 " ns (sqrt=%.1f)\n", mError, sqrt(mError)); | 
|  | 765 | StringAppendF(&result, "mNumResyncSamplesSincePresent: %d (limit %d)\n", | 
|  | 766 | mNumResyncSamplesSincePresent, MAX_RESYNC_SAMPLES_WITHOUT_PRESENT); | 
|  | 767 | StringAppendF(&result, "mNumResyncSamples: %zd (max %d)\n", mNumResyncSamples, | 
|  | 768 | MAX_RESYNC_SAMPLES); | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 769 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 770 | result.append("mResyncSamples:\n"); | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 771 | nsecs_t previous = -1; | 
|  | 772 | for (size_t i = 0; i < mNumResyncSamples; i++) { | 
|  | 773 | size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES; | 
|  | 774 | nsecs_t sampleTime = mResyncSamples[idx]; | 
|  | 775 | if (i == 0) { | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 776 | StringAppendF(&result, "  %" PRId64 "\n", sampleTime); | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 777 | } else { | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 778 | StringAppendF(&result, "  %" PRId64 " (+%" PRId64 ")\n", sampleTime, | 
|  | 779 | sampleTime - previous); | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 780 | } | 
|  | 781 | previous = sampleTime; | 
|  | 782 | } | 
|  | 783 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 784 | StringAppendF(&result, "mPresentFences [%d]:\n", NUM_PRESENT_SAMPLES); | 
| Andy McFadden | 5167ec6 | 2014-05-22 13:08:43 -0700 | [diff] [blame] | 785 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 786 | previous = Fence::SIGNAL_TIME_INVALID; | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 787 | for (size_t i = 0; i < NUM_PRESENT_SAMPLES; i++) { | 
|  | 788 | size_t idx = (i + mPresentSampleOffset) % NUM_PRESENT_SAMPLES; | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 789 | nsecs_t presentTime = mPresentFences[idx]->getSignalTime(); | 
|  | 790 | if (presentTime == Fence::SIGNAL_TIME_PENDING) { | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 791 | StringAppendF(&result, "  [unsignaled fence]\n"); | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 792 | } else if (presentTime == Fence::SIGNAL_TIME_INVALID) { | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 793 | StringAppendF(&result, "  [invalid fence]\n"); | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 794 | } else if (previous == Fence::SIGNAL_TIME_PENDING || | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 795 | previous == Fence::SIGNAL_TIME_INVALID) { | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 796 | StringAppendF(&result, "  %" PRId64 "  (%.3f ms ago)\n", presentTime, | 
|  | 797 | (now - presentTime) / 1000000.0); | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 798 | } else { | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 799 | StringAppendF(&result, "  %" PRId64 " (+%" PRId64 " / %.3f)  (%.3f ms ago)\n", | 
|  | 800 | presentTime, presentTime - previous, | 
|  | 801 | (presentTime - previous) / (double)mPeriod, | 
|  | 802 | (now - presentTime) / 1000000.0); | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 803 | } | 
|  | 804 | previous = presentTime; | 
|  | 805 | } | 
| Andy McFadden | 5167ec6 | 2014-05-22 13:08:43 -0700 | [diff] [blame] | 806 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 807 | StringAppendF(&result, "current monotonic time: %" PRId64 "\n", now); | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 808 | } | 
|  | 809 |  | 
| Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 810 | nsecs_t DispSync::expectedPresentTime() { | 
|  | 811 | // The HWC doesn't currently have a way to report additional latency. | 
|  | 812 | // Assume that whatever we submit now will appear right after the flip. | 
|  | 813 | // For a smart panel this might be 1.  This is expressed in frames, | 
|  | 814 | // rather than time, because we expect to have a constant frame delay | 
|  | 815 | // regardless of the refresh rate. | 
|  | 816 | const uint32_t hwcLatency = 0; | 
|  | 817 |  | 
|  | 818 | // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC). | 
| Ana Krulec | 757f63a | 2019-01-25 10:46:18 -0800 | [diff] [blame] | 819 | return computeNextRefresh(hwcLatency); | 
| Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 820 | } | 
|  | 821 |  | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 822 | } // namespace impl | 
|  | 823 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 824 | } // namespace android |