| 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> | 
 | 23 | #include <utils/Timers.h> | 
 | 24 | #include <utils/RefBase.h> | 
 | 25 |  | 
 | 26 | namespace android { | 
 | 27 |  | 
 | 28 | class String8; | 
 | 29 | class Fence; | 
 | 30 | class DispSyncThread; | 
 | 31 |  | 
 | 32 | // DispSync maintains a model of the periodic hardware-based vsync events of a | 
 | 33 | // display and uses that model to execute period callbacks at specific phase | 
 | 34 | // offsets from the hardware vsync events.  The model is constructed by | 
 | 35 | // feeding consecutive hardware event timestamps to the DispSync object via | 
 | 36 | // the addResyncSample method. | 
 | 37 | // | 
 | 38 | // The model is validated using timestamps from Fence objects that are passed | 
 | 39 | // to the DispSync object via the addPresentFence method.  These fence | 
 | 40 | // timestamps should correspond to a hardware vsync event, but they need not | 
 | 41 | // be consecutive hardware vsync times.  If this method determines that the | 
 | 42 | // current model accurately represents the hardware event times it will return | 
 | 43 | // false to indicate that a resynchronization (via addResyncSample) is not | 
 | 44 | // needed. | 
 | 45 | class DispSync { | 
 | 46 |  | 
 | 47 | public: | 
 | 48 |  | 
 | 49 |     class Callback: public virtual RefBase { | 
 | 50 |     public: | 
 | 51 |         virtual ~Callback() {}; | 
 | 52 |         virtual void onDispSyncEvent(nsecs_t when) = 0; | 
 | 53 |     }; | 
 | 54 |  | 
| Chih-Hung Hsieh | 342b760 | 2016-09-01 11:34:16 -0700 | [diff] [blame] | 55 |     explicit DispSync(const char* name); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 56 |     ~DispSync(); | 
 | 57 |  | 
| Saurabh Shah | ce398e4 | 2017-07-13 10:45:07 -0700 | [diff] [blame] | 58 |     void init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset); | 
 | 59 |  | 
| Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 60 |     // reset clears the resync samples and error value. | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 61 |     void reset(); | 
 | 62 |  | 
 | 63 |     // addPresentFence adds a fence for use in validating the current vsync | 
 | 64 |     // event model.  The fence need not be signaled at the time | 
 | 65 |     // addPresentFence is called.  When the fence does signal, its timestamp | 
 | 66 |     // should correspond to a hardware vsync event.  Unlike the | 
 | 67 |     // addResyncSample method, the timestamps of consecutive fences need not | 
 | 68 |     // correspond to consecutive hardware vsync events. | 
 | 69 |     // | 
 | 70 |     // This method should be called with the retire fence from each HWComposer | 
 | 71 |     // set call that affects the display. | 
 | 72 |     bool addPresentFence(const sp<Fence>& fence); | 
 | 73 |  | 
 | 74 |     // The beginResync, addResyncSample, and endResync methods are used to re- | 
 | 75 |     // synchronize the DispSync's model to the hardware vsync events.  The re- | 
 | 76 |     // synchronization process involves first calling beginResync, then | 
 | 77 |     // calling addResyncSample with a sequence of consecutive hardware vsync | 
 | 78 |     // event timestamps, and finally calling endResync when addResyncSample | 
 | 79 |     // indicates that no more samples are needed by returning false. | 
 | 80 |     // | 
 | 81 |     // This resynchronization process should be performed whenever the display | 
 | 82 |     // is turned on (i.e. once immediately after it's turned on) and whenever | 
 | 83 |     // addPresentFence returns true indicating that the model has drifted away | 
 | 84 |     // from the hardware vsync events. | 
 | 85 |     void beginResync(); | 
 | 86 |     bool addResyncSample(nsecs_t timestamp); | 
 | 87 |     void endResync(); | 
 | 88 |  | 
| Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 89 |     // 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] | 90 |     // value.  This should be used to prime the model when a display is first | 
 | 91 |     // turned on.  It should NOT be used after that. | 
 | 92 |     void setPeriod(nsecs_t period); | 
 | 93 |  | 
| Lajos Molnar | 67d8bd6 | 2014-09-11 14:58:45 -0700 | [diff] [blame] | 94 |     // The getPeriod method returns the current vsync period. | 
 | 95 |     nsecs_t getPeriod(); | 
 | 96 |  | 
| Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 97 |     // setRefreshSkipCount specifies an additional number of refresh | 
 | 98 |     // cycles to skip.  For example, on a 60Hz display, a skip count of 1 | 
 | 99 |     // will result in events happening at 30Hz.  Default is zero.  The idea | 
 | 100 |     // is to sacrifice smoothness for battery life. | 
 | 101 |     void setRefreshSkipCount(int count); | 
| Ruchi Kandoi | f52b3c8 | 2014-04-24 16:42:35 -0700 | [diff] [blame] | 102 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 103 |     // addEventListener registers a callback to be called repeatedly at the | 
 | 104 |     // given phase offset from the hardware vsync events.  The callback is | 
 | 105 |     // called from a separate thread and it should return reasonably quickly | 
 | 106 |     // (i.e. within a few hundred microseconds). | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 107 |     status_t addEventListener(const char* name, nsecs_t phase, | 
 | 108 |             const sp<Callback>& callback); | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 109 |  | 
 | 110 |     // removeEventListener removes an already-registered event callback.  Once | 
 | 111 |     // this method returns that callback will no longer be called by the | 
 | 112 |     // DispSync object. | 
 | 113 |     status_t removeEventListener(const sp<Callback>& callback); | 
 | 114 |  | 
| Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 115 |     // computeNextRefresh computes when the next refresh is expected to begin. | 
 | 116 |     // The periodOffset value can be used to move forward or backward; an | 
 | 117 |     // offset of zero is the next refresh, -1 is the previous refresh, 1 is | 
 | 118 |     // the refresh after next. etc. | 
 | 119 |     nsecs_t computeNextRefresh(int periodOffset) const; | 
 | 120 |  | 
| Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 121 |     // dump appends human-readable debug info to the result string. | 
 | 122 |     void dump(String8& result) const; | 
 | 123 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 124 | private: | 
 | 125 |  | 
 | 126 |     void updateModelLocked(); | 
 | 127 |     void updateErrorLocked(); | 
 | 128 |     void resetErrorLocked(); | 
 | 129 |  | 
 | 130 |     enum { MAX_RESYNC_SAMPLES = 32 }; | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 131 |     enum { MIN_RESYNC_SAMPLES_FOR_UPDATE = 6 }; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 132 |     enum { NUM_PRESENT_SAMPLES = 8 }; | 
| Dan Stoza | ef78916 | 2015-05-29 13:00:23 -0700 | [diff] [blame] | 133 |     enum { MAX_RESYNC_SAMPLES_WITHOUT_PRESENT = 4 }; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 134 |  | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 135 |     const char* const mName; | 
 | 136 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 137 |     // mPeriod is the computed period of the modeled vsync events in | 
 | 138 |     // nanoseconds. | 
 | 139 |     nsecs_t mPeriod; | 
 | 140 |  | 
 | 141 |     // mPhase is the phase offset of the modeled vsync events.  It is the | 
 | 142 |     // number of nanoseconds from time 0 to the first vsync event. | 
 | 143 |     nsecs_t mPhase; | 
 | 144 |  | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 145 |     // mReferenceTime is the reference time of the modeled vsync events. | 
 | 146 |     // It is the nanosecond timestamp of the first vsync event after a resync. | 
 | 147 |     nsecs_t mReferenceTime; | 
 | 148 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 149 |     // mError is the computed model error.  It is based on the difference | 
 | 150 |     // between the estimated vsync event times and those observed in the | 
 | 151 |     // mPresentTimes array. | 
 | 152 |     nsecs_t mError; | 
 | 153 |  | 
| Haixia Shi | 676b1f6 | 2015-10-28 16:19:01 -0700 | [diff] [blame] | 154 |     // Whether we have updated the vsync event model since the last resync. | 
 | 155 |     bool mModelUpdated; | 
 | 156 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 157 |     // These member variables are the state used during the resynchronization | 
 | 158 |     // process to store information about the hardware vsync event times used | 
 | 159 |     // to compute the model. | 
 | 160 |     nsecs_t mResyncSamples[MAX_RESYNC_SAMPLES]; | 
 | 161 |     size_t mFirstResyncSample; | 
 | 162 |     size_t mNumResyncSamples; | 
 | 163 |     int mNumResyncSamplesSincePresent; | 
 | 164 |  | 
 | 165 |     // These member variables store information about the present fences used | 
 | 166 |     // to validate the currently computed model. | 
 | 167 |     sp<Fence> mPresentFences[NUM_PRESENT_SAMPLES]; | 
 | 168 |     nsecs_t mPresentTimes[NUM_PRESENT_SAMPLES]; | 
 | 169 |     size_t mPresentSampleOffset; | 
 | 170 |  | 
| Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 171 |     int mRefreshSkipCount; | 
 | 172 |  | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 173 |     // mThread is the thread from which all the callbacks are called. | 
 | 174 |     sp<DispSyncThread> mThread; | 
 | 175 |  | 
 | 176 |     // mMutex is used to protect access to all member variables. | 
 | 177 |     mutable Mutex mMutex; | 
| Fabien Sanglard | c45a7d9 | 2017-03-14 13:24:22 -0700 | [diff] [blame] | 178 |  | 
 | 179 |     // This is the offset from the present fence timestamps to the corresponding | 
 | 180 |     // vsync event. | 
 | 181 |     int64_t mPresentTimeOffset; | 
| Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 182 |  | 
 | 183 |     // Ignore present (retire) fences if the device doesn't have support for the | 
 | 184 |     // sync framework | 
 | 185 |     bool mIgnorePresentFences; | 
| Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 186 | }; | 
 | 187 |  | 
 | 188 | } | 
 | 189 |  | 
 | 190 | #endif // ANDROID_DISPSYNC_H |