1 /* 2 * Copyright (C) 2012 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 #pragma once 18 19 #include <stddef.h> 20 21 #include <utils/Mutex.h> 22 #include <utils/RefBase.h> 23 #include <utils/Timers.h> 24 25 #include <ui/FenceTime.h> 26 27 #include <memory> 28 29 namespace android { 30 31 class FenceTime; 32 33 class DispSync { 34 public: 35 class Callback { 36 public: 37 Callback() = default; 38 virtual ~Callback(); 39 virtual void onDispSyncEvent(nsecs_t when) = 0; 40 41 protected: 42 Callback(Callback const&) = delete; 43 Callback& operator=(Callback const&) = delete; 44 }; 45 46 DispSync() = default; 47 virtual ~DispSync(); 48 49 virtual void reset() = 0; 50 virtual bool addPresentFence(const std::shared_ptr<FenceTime>&) = 0; 51 virtual void beginResync() = 0; 52 virtual bool addResyncSample(nsecs_t timestamp, bool* periodFlushed) = 0; 53 virtual void endResync() = 0; 54 virtual void setPeriod(nsecs_t period) = 0; 55 virtual nsecs_t getPeriod() = 0; 56 virtual void setRefreshSkipCount(int count) = 0; 57 virtual status_t addEventListener(const char* name, nsecs_t phase, Callback* callback, 58 nsecs_t lastCallbackTime) = 0; 59 virtual status_t removeEventListener(Callback* callback, nsecs_t* outLastCallback) = 0; 60 virtual status_t changePhaseOffset(Callback* callback, nsecs_t phase) = 0; 61 virtual nsecs_t computeNextRefresh(int periodOffset) const = 0; 62 virtual void setIgnorePresentFences(bool ignore) = 0; 63 virtual nsecs_t expectedPresentTime() = 0; 64 65 virtual void dump(std::string& result) const = 0; 66 67 protected: 68 DispSync(DispSync const&) = delete; 69 DispSync& operator=(DispSync const&) = delete; 70 }; 71 72 namespace impl { 73 74 class DispSyncThread; 75 76 // DispSync maintains a model of the periodic hardware-based vsync events of a 77 // display and uses that model to execute period callbacks at specific phase 78 // offsets from the hardware vsync events. The model is constructed by 79 // feeding consecutive hardware event timestamps to the DispSync object via 80 // the addResyncSample method. 81 // 82 // The model is validated using timestamps from Fence objects that are passed 83 // to the DispSync object via the addPresentFence method. These fence 84 // timestamps should correspond to a hardware vsync event, but they need not 85 // be consecutive hardware vsync times. If this method determines that the 86 // current model accurately represents the hardware event times it will return 87 // false to indicate that a resynchronization (via addResyncSample) is not 88 // needed. 89 class DispSync : public android::DispSync { 90 public: 91 explicit DispSync(const char* name); 92 ~DispSync() override; 93 94 void init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset); 95 96 // reset clears the resync samples and error value. 97 void reset() override; 98 99 // addPresentFence adds a fence for use in validating the current vsync 100 // event model. The fence need not be signaled at the time 101 // addPresentFence is called. When the fence does signal, its timestamp 102 // should correspond to a hardware vsync event. Unlike the 103 // addResyncSample method, the timestamps of consecutive fences need not 104 // correspond to consecutive hardware vsync events. 105 // 106 // This method should be called with the retire fence from each HWComposer 107 // set call that affects the display. 108 bool addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) override; 109 110 // The beginResync, addResyncSample, and endResync methods are used to re- 111 // synchronize the DispSync's model to the hardware vsync events. The re- 112 // synchronization process involves first calling beginResync, then 113 // calling addResyncSample with a sequence of consecutive hardware vsync 114 // event timestamps, and finally calling endResync when addResyncSample 115 // indicates that no more samples are needed by returning false. 116 // 117 // This resynchronization process should be performed whenever the display 118 // is turned on (i.e. once immediately after it's turned on) and whenever 119 // addPresentFence returns true indicating that the model has drifted away 120 // from the hardware vsync events. 121 void beginResync() override; 122 // Adds a vsync sample to the dispsync model. The timestamp is the time 123 // of the vsync event that fired. periodFlushed will return true if the 124 // vsync period was detected to have changed to mPendingPeriod. 125 // 126 // This method will return true if more vsync samples are needed to lock 127 // down the DispSync model, and false otherwise. 128 // periodFlushed will be set to true if mPendingPeriod is flushed to 129 // mIntendedPeriod, and false otherwise. 130 bool addResyncSample(nsecs_t timestamp, bool* periodFlushed) override; 131 void endResync() override; 132 133 // The setPeriod method sets the vsync event model's period to a specific 134 // value. This should be used to prime the model when a display is first 135 // turned on, or when a refresh rate change is requested. 136 void setPeriod(nsecs_t period) override; 137 138 // The getPeriod method returns the current vsync period. 139 nsecs_t getPeriod() override; 140 141 // setRefreshSkipCount specifies an additional number of refresh 142 // cycles to skip. For example, on a 60Hz display, a skip count of 1 143 // will result in events happening at 30Hz. Default is zero. The idea 144 // is to sacrifice smoothness for battery life. 145 void setRefreshSkipCount(int count) override; 146 147 // addEventListener registers a callback to be called repeatedly at the 148 // given phase offset from the hardware vsync events. The callback is 149 // called from a separate thread and it should return reasonably quickly 150 // (i.e. within a few hundred microseconds). 151 // If the callback was previously registered, and the last clock time the 152 // callback was invoked was known to the caller (e.g. via removeEventListener), 153 // then the caller may pass that through to lastCallbackTime, so that 154 // callbacks do not accidentally double-fire if they are unregistered and 155 // reregistered in rapid succession. 156 status_t addEventListener(const char* name, nsecs_t phase, Callback* callback, 157 nsecs_t lastCallbackTime) override; 158 159 // removeEventListener removes an already-registered event callback. Once 160 // this method returns that callback will no longer be called by the 161 // DispSync object. 162 // outLastCallbackTime will contain the last time that the callback was invoked. 163 // If the caller wishes to reregister the same callback, they should pass the 164 // callback time back into lastCallbackTime (see addEventListener). 165 status_t removeEventListener(Callback* callback, nsecs_t* outLastCallbackTime) override; 166 167 // changePhaseOffset changes the phase offset of an already-registered event callback. The 168 // method will make sure that there is no skipping or double-firing on the listener per frame, 169 // even when changing the offsets multiple times. 170 status_t changePhaseOffset(Callback* callback, nsecs_t phase) override; 171 172 // computeNextRefresh computes when the next refresh is expected to begin. 173 // The periodOffset value can be used to move forward or backward; an 174 // offset of zero is the next refresh, -1 is the previous refresh, 1 is 175 // the refresh after next. etc. 176 nsecs_t computeNextRefresh(int periodOffset) const override; 177 178 // In certain situations the present fences aren't a good indicator of vsync 179 // time, e.g. when vr flinger is active, or simply aren't available, 180 // e.g. when the sync framework isn't present. Use this method to toggle 181 // whether or not DispSync ignores present fences. If present fences are 182 // ignored, DispSync will always ask for hardware vsync events by returning 183 // true from addPresentFence() and addResyncSample(). 184 void setIgnorePresentFences(bool ignore) override; 185 186 // Determine the expected present time when a buffer acquired now will be displayed. 187 nsecs_t expectedPresentTime(); 188 189 // dump appends human-readable debug info to the result string. 190 void dump(std::string& result) const override; 191 192 private: 193 void updateModelLocked(); 194 void updateErrorLocked(); 195 void resetLocked(); 196 void resetErrorLocked(); 197 198 enum { MAX_RESYNC_SAMPLES = 32 }; 199 enum { MIN_RESYNC_SAMPLES_FOR_UPDATE = 6 }; 200 enum { NUM_PRESENT_SAMPLES = 8 }; 201 enum { MAX_RESYNC_SAMPLES_WITHOUT_PRESENT = 4 }; 202 enum { ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT = 64 }; 203 204 const char* const mName; 205 206 // mPeriod is the computed period of the modeled vsync events in 207 // nanoseconds. 208 nsecs_t mPeriod; 209 210 // mIntendedPeriod is the intended period of the modeled vsync events in 211 // nanoseconds. Under ideal conditions this should be similar if not the 212 // same as mPeriod, plus or minus an observed error. 213 nsecs_t mIntendedPeriod = 0; 214 215 // mPendingPeriod is the proposed period change in nanoseconds. 216 // If mPendingPeriod differs from mPeriod and is nonzero, it will 217 // be flushed to mPeriod when we detect that the hardware switched 218 // vsync frequency. 219 nsecs_t mPendingPeriod = 0; 220 221 // mPhase is the phase offset of the modeled vsync events. It is the 222 // number of nanoseconds from time 0 to the first vsync event. 223 nsecs_t mPhase; 224 225 // mReferenceTime is the reference time of the modeled vsync events. 226 // It is the nanosecond timestamp of the first vsync event after a resync. 227 nsecs_t mReferenceTime; 228 229 // mError is the computed model error. It is based on the difference 230 // between the estimated vsync event times and those observed in the 231 // mPresentFences array. 232 nsecs_t mError; 233 234 // mZeroErrSamplesCount keeps track of how many times in a row there were 235 // zero timestamps available in the mPresentFences array. 236 // Used to check that we are able to calculate the model error. 237 size_t mZeroErrSamplesCount; 238 239 // Whether we have updated the vsync event model since the last resync. 240 bool mModelUpdated; 241 242 // These member variables are the state used during the resynchronization 243 // process to store information about the hardware vsync event times used 244 // to compute the model. 245 nsecs_t mResyncSamples[MAX_RESYNC_SAMPLES] = {0}; 246 size_t mFirstResyncSample = 0; 247 size_t mNumResyncSamples = 0; 248 int mNumResyncSamplesSincePresent; 249 250 // These member variables store information about the present fences used 251 // to validate the currently computed model. 252 std::shared_ptr<FenceTime> mPresentFences[NUM_PRESENT_SAMPLES]{FenceTime::NO_FENCE}; 253 size_t mPresentSampleOffset; 254 255 int mRefreshSkipCount; 256 257 // mThread is the thread from which all the callbacks are called. 258 sp<DispSyncThread> mThread; 259 260 // mMutex is used to protect access to all member variables. 261 mutable Mutex mMutex; 262 263 // This is the offset from the present fence timestamps to the corresponding 264 // vsync event. 265 int64_t mPresentTimeOffset; 266 267 // Ignore present (retire) fences if the device doesn't have support for the 268 // sync framework 269 bool mIgnorePresentFences; 270 271 std::unique_ptr<Callback> mZeroPhaseTracer; 272 273 // Flag to turn on logging in systrace. 274 bool mTraceDetailedInfo = false; 275 }; 276 277 } // namespace impl 278 279 } // namespace android 280