blob: 183966feba963603991f2fd9240f326085c6d4be [file] [log] [blame]
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001/*
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 Gennisfaf77cc2013-07-30 15:10:32 -070023#include <utils/RefBase.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080024#include <utils/Timers.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070025
Brian Andersonfbc80ae2017-05-26 16:23:54 -070026#include <ui/FenceTime.h>
27
28#include <memory>
29
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070030namespace android {
31
32class String8;
Brian Andersonfbc80ae2017-05-26 16:23:54 -070033class FenceTime;
Lloyd Pique41be5d22018-06-21 13:11:48 -070034
35class DispSync {
36public:
37 class Callback {
38 public:
39 virtual ~Callback() = default;
40 virtual void onDispSyncEvent(nsecs_t when) = 0;
41 };
42
43 virtual ~DispSync();
44
45 virtual void reset() = 0;
46 virtual bool addPresentFence(const std::shared_ptr<FenceTime>&) = 0;
47 virtual void beginResync() = 0;
48 virtual bool addResyncSample(nsecs_t timestamp) = 0;
49 virtual void endResync() = 0;
50 virtual void setPeriod(nsecs_t period) = 0;
David Sodman6d46c1e2018-07-13 12:59:48 -070051 virtual void scalePeriod(const uint32_t multiplier, uint32_t divisor) = 0;
Lloyd Pique41be5d22018-06-21 13:11:48 -070052 virtual nsecs_t getPeriod() = 0;
53 virtual void setRefreshSkipCount(int count) = 0;
54 virtual status_t addEventListener(const char* name, nsecs_t phase, Callback* callback) = 0;
55 virtual status_t removeEventListener(Callback* callback) = 0;
56 virtual status_t changePhaseOffset(Callback* callback, nsecs_t phase) = 0;
57 virtual nsecs_t computeNextRefresh(int periodOffset) const = 0;
58 virtual void setIgnorePresentFences(bool ignore) = 0;
59
60 virtual void dump(String8& result) const = 0;
61};
62
63namespace impl {
64
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070065class DispSyncThread;
66
67// DispSync maintains a model of the periodic hardware-based vsync events of a
68// display and uses that model to execute period callbacks at specific phase
69// offsets from the hardware vsync events. The model is constructed by
70// feeding consecutive hardware event timestamps to the DispSync object via
71// the addResyncSample method.
72//
73// The model is validated using timestamps from Fence objects that are passed
74// to the DispSync object via the addPresentFence method. These fence
75// timestamps should correspond to a hardware vsync event, but they need not
76// be consecutive hardware vsync times. If this method determines that the
77// current model accurately represents the hardware event times it will return
78// false to indicate that a resynchronization (via addResyncSample) is not
79// needed.
Lloyd Pique41be5d22018-06-21 13:11:48 -070080class DispSync : public android::DispSync {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070081public:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -070082 explicit DispSync(const char* name);
Lloyd Pique41be5d22018-06-21 13:11:48 -070083 ~DispSync() override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070084
Saurabh Shahf4174532017-07-13 10:45:07 -070085 void init(bool hasSyncFramework, int64_t dispSyncPresentTimeOffset);
86
Andy McFadden645b1f72014-06-10 14:43:32 -070087 // reset clears the resync samples and error value.
Lloyd Pique41be5d22018-06-21 13:11:48 -070088 void reset() override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070089
90 // addPresentFence adds a fence for use in validating the current vsync
91 // event model. The fence need not be signaled at the time
92 // addPresentFence is called. When the fence does signal, its timestamp
93 // should correspond to a hardware vsync event. Unlike the
94 // addResyncSample method, the timestamps of consecutive fences need not
95 // correspond to consecutive hardware vsync events.
96 //
97 // This method should be called with the retire fence from each HWComposer
98 // set call that affects the display.
Lloyd Pique41be5d22018-06-21 13:11:48 -070099 bool addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700100
101 // The beginResync, addResyncSample, and endResync methods are used to re-
102 // synchronize the DispSync's model to the hardware vsync events. The re-
103 // synchronization process involves first calling beginResync, then
104 // calling addResyncSample with a sequence of consecutive hardware vsync
105 // event timestamps, and finally calling endResync when addResyncSample
106 // indicates that no more samples are needed by returning false.
107 //
108 // This resynchronization process should be performed whenever the display
109 // is turned on (i.e. once immediately after it's turned on) and whenever
110 // addPresentFence returns true indicating that the model has drifted away
111 // from the hardware vsync events.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700112 void beginResync() override;
113 bool addResyncSample(nsecs_t timestamp) override;
114 void endResync() override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700115
Andy McFadden41d67d72014-04-25 16:58:34 -0700116 // The setPeriod method sets the vsync event model's period to a specific
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700117 // value. This should be used to prime the model when a display is first
118 // turned on. It should NOT be used after that.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700119 void setPeriod(nsecs_t period) override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700120
David Sodman6d46c1e2018-07-13 12:59:48 -0700121 // The scalePeriod method applies the multiplier and divisor to
122 // scale the vsync event model's period. The function is added
123 // for an experimental test mode and should not be used outside
124 // of that purpose.
125 void scalePeriod(const uint32_t multiplier, uint32_t divisor);
126
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700127 // The getPeriod method returns the current vsync period.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700128 nsecs_t getPeriod() override;
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700129
Andy McFadden645b1f72014-06-10 14:43:32 -0700130 // setRefreshSkipCount specifies an additional number of refresh
131 // cycles to skip. For example, on a 60Hz display, a skip count of 1
132 // will result in events happening at 30Hz. Default is zero. The idea
133 // is to sacrifice smoothness for battery life.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700134 void setRefreshSkipCount(int count) override;
Ruchi Kandoif52b3c82014-04-24 16:42:35 -0700135
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700136 // addEventListener registers a callback to be called repeatedly at the
137 // given phase offset from the hardware vsync events. The callback is
138 // called from a separate thread and it should return reasonably quickly
139 // (i.e. within a few hundred microseconds).
Lloyd Pique41be5d22018-06-21 13:11:48 -0700140 status_t addEventListener(const char* name, nsecs_t phase, Callback* callback) override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700141
142 // removeEventListener removes an already-registered event callback. Once
143 // this method returns that callback will no longer be called by the
144 // DispSync object.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700145 status_t removeEventListener(Callback* callback) override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700146
Dan Stoza84d619e2018-03-28 17:07:36 -0700147 // changePhaseOffset changes the phase offset of an already-registered event callback. The
148 // method will make sure that there is no skipping or double-firing on the listener per frame,
149 // even when changing the offsets multiple times.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700150 status_t changePhaseOffset(Callback* callback, nsecs_t phase) override;
Dan Stoza84d619e2018-03-28 17:07:36 -0700151
Andy McFadden41d67d72014-04-25 16:58:34 -0700152 // computeNextRefresh computes when the next refresh is expected to begin.
153 // The periodOffset value can be used to move forward or backward; an
154 // offset of zero is the next refresh, -1 is the previous refresh, 1 is
155 // the refresh after next. etc.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700156 nsecs_t computeNextRefresh(int periodOffset) const override;
Andy McFadden41d67d72014-04-25 16:58:34 -0700157
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700158 // In certain situations the present fences aren't a good indicator of vsync
159 // time, e.g. when vr flinger is active, or simply aren't available,
160 // e.g. when the sync framework isn't present. Use this method to toggle
161 // whether or not DispSync ignores present fences. If present fences are
162 // ignored, DispSync will always ask for hardware vsync events by returning
163 // true from addPresentFence() and addResyncSample().
Lloyd Pique41be5d22018-06-21 13:11:48 -0700164 void setIgnorePresentFences(bool ignore) override;
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700165
Andy McFaddenc751e922014-05-08 14:53:26 -0700166 // dump appends human-readable debug info to the result string.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700167 void dump(String8& result) const override;
Andy McFaddenc751e922014-05-08 14:53:26 -0700168
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700169private:
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700170 void updateModelLocked();
171 void updateErrorLocked();
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700172 void resetLocked();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700173 void resetErrorLocked();
174
175 enum { MAX_RESYNC_SAMPLES = 32 };
Tim Murray4a4e4a22016-04-19 16:29:23 +0000176 enum { MIN_RESYNC_SAMPLES_FOR_UPDATE = 6 };
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700177 enum { NUM_PRESENT_SAMPLES = 8 };
Dan Stozaef789162015-05-29 13:00:23 -0700178 enum { MAX_RESYNC_SAMPLES_WITHOUT_PRESENT = 4 };
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700179 enum { ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT = 64 };
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700180
Tim Murray4a4e4a22016-04-19 16:29:23 +0000181 const char* const mName;
182
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700183 // mPeriod is the computed period of the modeled vsync events in
184 // nanoseconds.
185 nsecs_t mPeriod;
David Sodman6d46c1e2018-07-13 12:59:48 -0700186 nsecs_t mPeriodBase;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700187
188 // mPhase is the phase offset of the modeled vsync events. It is the
189 // number of nanoseconds from time 0 to the first vsync event.
190 nsecs_t mPhase;
191
Haixia Shi676b1f62015-10-28 16:19:01 -0700192 // mReferenceTime is the reference time of the modeled vsync events.
193 // It is the nanosecond timestamp of the first vsync event after a resync.
194 nsecs_t mReferenceTime;
195
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700196 // mError is the computed model error. It is based on the difference
197 // between the estimated vsync event times and those observed in the
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700198 // mPresentFences array.
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700199 nsecs_t mError;
200
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700201 // mZeroErrSamplesCount keeps track of how many times in a row there were
202 // zero timestamps available in the mPresentFences array.
203 // Used to sanity check that we are able to calculate the model error.
204 size_t mZeroErrSamplesCount;
205
Haixia Shi676b1f62015-10-28 16:19:01 -0700206 // Whether we have updated the vsync event model since the last resync.
207 bool mModelUpdated;
208
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700209 // These member variables are the state used during the resynchronization
210 // process to store information about the hardware vsync event times used
211 // to compute the model.
212 nsecs_t mResyncSamples[MAX_RESYNC_SAMPLES];
213 size_t mFirstResyncSample;
214 size_t mNumResyncSamples;
215 int mNumResyncSamplesSincePresent;
216
217 // These member variables store information about the present fences used
218 // to validate the currently computed model.
Lloyd Pique78ce4182018-01-31 16:39:51 -0800219 std::shared_ptr<FenceTime> mPresentFences[NUM_PRESENT_SAMPLES]{FenceTime::NO_FENCE};
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700220 size_t mPresentSampleOffset;
221
Andy McFadden645b1f72014-06-10 14:43:32 -0700222 int mRefreshSkipCount;
223
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700224 // mThread is the thread from which all the callbacks are called.
225 sp<DispSyncThread> mThread;
226
227 // mMutex is used to protect access to all member variables.
228 mutable Mutex mMutex;
Fabien Sanglardc45a7d92017-03-14 13:24:22 -0700229
230 // This is the offset from the present fence timestamps to the corresponding
231 // vsync event.
232 int64_t mPresentTimeOffset;
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800233
234 // Ignore present (retire) fences if the device doesn't have support for the
235 // sync framework
236 bool mIgnorePresentFences;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800237
238 std::unique_ptr<Callback> mZeroPhaseTracer;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700239};
240
Lloyd Pique41be5d22018-06-21 13:11:48 -0700241} // namespace impl
242
Lloyd Pique78ce4182018-01-31 16:39:51 -0800243} // namespace android
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700244
245#endif // ANDROID_DISPSYNC_H