| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 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 | #ifndef ANDROID_DISPSYNC_H | 
 | 18 | #define ANDROID_DISPSYNC_H | 
 | 19 |  | 
 | 20 | #include <stddef.h> | 
 | 21 |  | 
 | 22 | #include <utils/Mutex.h> | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 23 | #include <utils/RefBase.h> | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 24 | #include <utils/Timers.h> | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 25 |  | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 26 | #include <ui/FenceTime.h> | 
| David Sodman | 44b5de0 | 2018-08-21 16:28:53 -0700 | [diff] [blame] | 27 | #include <DisplayHardware/HWC2.h> | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 28 |  | 
 | 29 | #include <memory> | 
 | 30 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 31 | namespace android { | 
 | 32 |  | 
 | 33 | class String8; | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 34 | class FenceTime; | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 35 |  | 
 | 36 | class DispSync { | 
 | 37 | public: | 
 | 38 |     class Callback { | 
 | 39 |     public: | 
 | 40 |         virtual ~Callback() = default; | 
 | 41 |         virtual void onDispSyncEvent(nsecs_t when) = 0; | 
 | 42 |     }; | 
 | 43 |  | 
 | 44 |     virtual ~DispSync(); | 
 | 45 |  | 
 | 46 |     virtual void reset() = 0; | 
 | 47 |     virtual bool addPresentFence(const std::shared_ptr<FenceTime>&) = 0; | 
 | 48 |     virtual void beginResync() = 0; | 
 | 49 |     virtual bool addResyncSample(nsecs_t timestamp) = 0; | 
 | 50 |     virtual void endResync() = 0; | 
 | 51 |     virtual void setPeriod(nsecs_t period) = 0; | 
| David Sodman | 44b5de0 | 2018-08-21 16:28:53 -0700 | [diff] [blame] | 52 |     virtual void scalePeriod(HWC2::Device::FrequencyScaler) = 0; | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 53 |     virtual nsecs_t getPeriod() = 0; | 
 | 54 |     virtual void setRefreshSkipCount(int count) = 0; | 
 | 55 |     virtual status_t addEventListener(const char* name, nsecs_t phase, Callback* callback) = 0; | 
 | 56 |     virtual status_t removeEventListener(Callback* callback) = 0; | 
 | 57 |     virtual status_t changePhaseOffset(Callback* callback, nsecs_t phase) = 0; | 
 | 58 |     virtual nsecs_t computeNextRefresh(int periodOffset) const = 0; | 
 | 59 |     virtual void setIgnorePresentFences(bool ignore) = 0; | 
 | 60 |  | 
 | 61 |     virtual void dump(String8& result) const = 0; | 
 | 62 | }; | 
 | 63 |  | 
 | 64 | namespace impl { | 
 | 65 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 66 | class DispSyncThread; | 
 | 67 |  | 
 | 68 | // DispSync maintains a model of the periodic hardware-based vsync events of a | 
 | 69 | // display and uses that model to execute period callbacks at specific phase | 
 | 70 | // offsets from the hardware vsync events.  The model is constructed by | 
 | 71 | // feeding consecutive hardware event timestamps to the DispSync object via | 
 | 72 | // the addResyncSample method. | 
 | 73 | // | 
 | 74 | // The model is validated using timestamps from Fence objects that are passed | 
 | 75 | // to the DispSync object via the addPresentFence method.  These fence | 
 | 76 | // timestamps should correspond to a hardware vsync event, but they need not | 
 | 77 | // be consecutive hardware vsync times.  If this method determines that the | 
 | 78 | // current model accurately represents the hardware event times it will return | 
 | 79 | // false to indicate that a resynchronization (via addResyncSample) is not | 
 | 80 | // needed. | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 81 | class DispSync : public android::DispSync { | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 82 | public: | 
| Chih-Hung Hsieh | 342b760 | 2016-09-01 11:34:16 -0700 | [diff] [blame] | 83 |     explicit DispSync(const char* name); | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 84 |     ~DispSync() override; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 85 |  | 
| Saurabh Shah | f417453 | 2017-07-13 10:45:07 -0700 | [diff] [blame] | 86 |     void init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset); | 
 | 87 |  | 
| Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 88 |     // reset clears the resync samples and error value. | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 89 |     void reset() override; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 90 |  | 
 | 91 |     // addPresentFence adds a fence for use in validating the current vsync | 
 | 92 |     // event model.  The fence need not be signaled at the time | 
 | 93 |     // addPresentFence is called.  When the fence does signal, its timestamp | 
 | 94 |     // should correspond to a hardware vsync event.  Unlike the | 
 | 95 |     // addResyncSample method, the timestamps of consecutive fences need not | 
 | 96 |     // correspond to consecutive hardware vsync events. | 
 | 97 |     // | 
 | 98 |     // This method should be called with the retire fence from each HWComposer | 
 | 99 |     // set call that affects the display. | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 100 |     bool addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) override; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 101 |  | 
 | 102 |     // The beginResync, addResyncSample, and endResync methods are used to re- | 
 | 103 |     // synchronize the DispSync's model to the hardware vsync events.  The re- | 
 | 104 |     // synchronization process involves first calling beginResync, then | 
 | 105 |     // calling addResyncSample with a sequence of consecutive hardware vsync | 
 | 106 |     // event timestamps, and finally calling endResync when addResyncSample | 
 | 107 |     // indicates that no more samples are needed by returning false. | 
 | 108 |     // | 
 | 109 |     // This resynchronization process should be performed whenever the display | 
 | 110 |     // is turned on (i.e. once immediately after it's turned on) and whenever | 
 | 111 |     // addPresentFence returns true indicating that the model has drifted away | 
 | 112 |     // from the hardware vsync events. | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 113 |     void beginResync() override; | 
 | 114 |     bool addResyncSample(nsecs_t timestamp) override; | 
 | 115 |     void endResync() override; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 116 |  | 
| Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 117 |     // The setPeriod method sets the vsync event model's period to a specific | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 118 |     // value.  This should be used to prime the model when a display is first | 
 | 119 |     // turned on.  It should NOT be used after that. | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 120 |     void setPeriod(nsecs_t period) override; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 121 |  | 
| David Sodman | 6d46c1e | 2018-07-13 12:59:48 -0700 | [diff] [blame] | 122 |     // The scalePeriod method applies the multiplier and divisor to | 
 | 123 |     // scale the vsync event model's period.   The function is added | 
 | 124 |     // for an experimental test mode and should not be used outside | 
 | 125 |     // of that purpose. | 
| David Sodman | 44b5de0 | 2018-08-21 16:28:53 -0700 | [diff] [blame] | 126 |     void scalePeriod(HWC2::Device::FrequencyScaler frequencyScaler); | 
| David Sodman | 6d46c1e | 2018-07-13 12:59:48 -0700 | [diff] [blame] | 127 |  | 
| Lajos Molnar | 67d8bd6 | 2014-09-11 14:58:45 -0700 | [diff] [blame] | 128 |     // The getPeriod method returns the current vsync period. | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 129 |     nsecs_t getPeriod() override; | 
| Lajos Molnar | 67d8bd6 | 2014-09-11 14:58:45 -0700 | [diff] [blame] | 130 |  | 
| Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 131 |     // setRefreshSkipCount specifies an additional number of refresh | 
 | 132 |     // cycles to skip.  For example, on a 60Hz display, a skip count of 1 | 
 | 133 |     // will result in events happening at 30Hz.  Default is zero.  The idea | 
 | 134 |     // is to sacrifice smoothness for battery life. | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 135 |     void setRefreshSkipCount(int count) override; | 
| Ruchi Kandoi | f52b3c8 | 2014-04-24 16:42:35 -0700 | [diff] [blame] | 136 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 137 |     // addEventListener registers a callback to be called repeatedly at the | 
 | 138 |     // given phase offset from the hardware vsync events.  The callback is | 
 | 139 |     // called from a separate thread and it should return reasonably quickly | 
 | 140 |     // (i.e. within a few hundred microseconds). | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 141 |     status_t addEventListener(const char* name, nsecs_t phase, Callback* callback) override; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 142 |  | 
 | 143 |     // removeEventListener removes an already-registered event callback.  Once | 
 | 144 |     // this method returns that callback will no longer be called by the | 
 | 145 |     // DispSync object. | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 146 |     status_t removeEventListener(Callback* callback) override; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 147 |  | 
| Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 148 |     // changePhaseOffset changes the phase offset of an already-registered event callback. The | 
 | 149 |     // method will make sure that there is no skipping or double-firing on the listener per frame, | 
 | 150 |     // even when changing the offsets multiple times. | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 151 |     status_t changePhaseOffset(Callback* callback, nsecs_t phase) override; | 
| Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 152 |  | 
| Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 153 |     // computeNextRefresh computes when the next refresh is expected to begin. | 
 | 154 |     // The periodOffset value can be used to move forward or backward; an | 
 | 155 |     // offset of zero is the next refresh, -1 is the previous refresh, 1 is | 
 | 156 |     // the refresh after next. etc. | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 157 |     nsecs_t computeNextRefresh(int periodOffset) const override; | 
| Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 158 |  | 
| Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 159 |     // In certain situations the present fences aren't a good indicator of vsync | 
 | 160 |     // time, e.g. when vr flinger is active, or simply aren't available, | 
 | 161 |     // e.g. when the sync framework isn't present. Use this method to toggle | 
 | 162 |     // whether or not DispSync ignores present fences. If present fences are | 
 | 163 |     // ignored, DispSync will always ask for hardware vsync events by returning | 
 | 164 |     // true from addPresentFence() and addResyncSample(). | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 165 |     void setIgnorePresentFences(bool ignore) override; | 
| Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 166 |  | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 167 |     // dump appends human-readable debug info to the result string. | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 168 |     void dump(String8& result) const override; | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 169 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 170 | private: | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 171 |     void updateModelLocked(); | 
 | 172 |     void updateErrorLocked(); | 
| Steven Thomas | dfde8fa | 2018-04-19 16:00:58 -0700 | [diff] [blame] | 173 |     void resetLocked(); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 174 |     void resetErrorLocked(); | 
 | 175 |  | 
 | 176 |     enum { MAX_RESYNC_SAMPLES = 32 }; | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 177 |     enum { MIN_RESYNC_SAMPLES_FOR_UPDATE = 6 }; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 178 |     enum { NUM_PRESENT_SAMPLES = 8 }; | 
| Dan Stoza | ef78916 | 2015-05-29 13:00:23 -0700 | [diff] [blame] | 179 |     enum { MAX_RESYNC_SAMPLES_WITHOUT_PRESENT = 4 }; | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 180 |     enum { ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT = 64 }; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 181 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 182 |     const char* const mName; | 
 | 183 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 184 |     // mPeriod is the computed period of the modeled vsync events in | 
 | 185 |     // nanoseconds. | 
 | 186 |     nsecs_t mPeriod; | 
| David Sodman | 6d46c1e | 2018-07-13 12:59:48 -0700 | [diff] [blame] | 187 |     nsecs_t mPeriodBase; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 188 |  | 
 | 189 |     // mPhase is the phase offset of the modeled vsync events.  It is the | 
 | 190 |     // number of nanoseconds from time 0 to the first vsync event. | 
 | 191 |     nsecs_t mPhase; | 
 | 192 |  | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 193 |     // mReferenceTime is the reference time of the modeled vsync events. | 
 | 194 |     // It is the nanosecond timestamp of the first vsync event after a resync. | 
 | 195 |     nsecs_t mReferenceTime; | 
 | 196 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 197 |     // mError is the computed model error.  It is based on the difference | 
 | 198 |     // between the estimated vsync event times and those observed in the | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 199 |     // mPresentFences array. | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 200 |     nsecs_t mError; | 
 | 201 |  | 
| Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 202 |     // mZeroErrSamplesCount keeps track of how many times in a row there were | 
 | 203 |     // zero timestamps available in the mPresentFences array. | 
 | 204 |     // Used to sanity check that we are able to calculate the model error. | 
 | 205 |     size_t mZeroErrSamplesCount; | 
 | 206 |  | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 207 |     // Whether we have updated the vsync event model since the last resync. | 
 | 208 |     bool mModelUpdated; | 
 | 209 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 210 |     // These member variables are the state used during the resynchronization | 
 | 211 |     // process to store information about the hardware vsync event times used | 
 | 212 |     // to compute the model. | 
 | 213 |     nsecs_t mResyncSamples[MAX_RESYNC_SAMPLES]; | 
 | 214 |     size_t mFirstResyncSample; | 
 | 215 |     size_t mNumResyncSamples; | 
 | 216 |     int mNumResyncSamplesSincePresent; | 
 | 217 |  | 
 | 218 |     // These member variables store information about the present fences used | 
 | 219 |     // to validate the currently computed model. | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 220 |     std::shared_ptr<FenceTime> mPresentFences[NUM_PRESENT_SAMPLES]{FenceTime::NO_FENCE}; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 221 |     size_t mPresentSampleOffset; | 
 | 222 |  | 
| Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 223 |     int mRefreshSkipCount; | 
 | 224 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 225 |     // mThread is the thread from which all the callbacks are called. | 
 | 226 |     sp<DispSyncThread> mThread; | 
 | 227 |  | 
 | 228 |     // mMutex is used to protect access to all member variables. | 
 | 229 |     mutable Mutex mMutex; | 
| Fabien Sanglard | c45a7d9 | 2017-03-14 13:24:22 -0700 | [diff] [blame] | 230 |  | 
 | 231 |     // This is the offset from the present fence timestamps to the corresponding | 
 | 232 |     // vsync event. | 
 | 233 |     int64_t mPresentTimeOffset; | 
| Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 234 |  | 
 | 235 |     // Ignore present (retire) fences if the device doesn't have support for the | 
 | 236 |     // sync framework | 
 | 237 |     bool mIgnorePresentFences; | 
| Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 238 |  | 
 | 239 |     std::unique_ptr<Callback> mZeroPhaseTracer; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 240 | }; | 
 | 241 |  | 
| Lloyd Pique | 41be5d2 | 2018-06-21 13:11:48 -0700 | [diff] [blame] | 242 | } // namespace impl | 
 | 243 |  | 
| Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 244 | } // namespace android | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 245 |  | 
 | 246 | #endif // ANDROID_DISPSYNC_H |