blob: 832f08e07bd7c045975dfe2f1111be4e2b0649e7 [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
Kevin DuBois7b743be2019-02-27 10:05:48 -080017#pragma once
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070018
19#include <stddef.h>
20
21#include <utils/Mutex.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070022#include <utils/RefBase.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080023#include <utils/Timers.h>
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070024
Brian Andersonfbc80ae2017-05-26 16:23:54 -070025#include <ui/FenceTime.h>
26
27#include <memory>
28
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070029namespace android {
30
Brian Andersonfbc80ae2017-05-26 16:23:54 -070031class FenceTime;
Lloyd Pique41be5d22018-06-21 13:11:48 -070032
33class DispSync {
34public:
35 class Callback {
36 public:
Kevin DuBois7b743be2019-02-27 10:05:48 -080037 Callback() = default;
38 virtual ~Callback();
Ady Abraham5facfb12020-04-22 15:18:31 -070039 virtual void onDispSyncEvent(nsecs_t when, nsecs_t expectedVSyncTimestamp) = 0;
Kevin DuBois7b743be2019-02-27 10:05:48 -080040
41 protected:
42 Callback(Callback const&) = delete;
43 Callback& operator=(Callback const&) = delete;
Lloyd Pique41be5d22018-06-21 13:11:48 -070044 };
45
Kevin DuBois7b743be2019-02-27 10:05:48 -080046 DispSync() = default;
Lloyd Pique41be5d22018-06-21 13:11:48 -070047 virtual ~DispSync();
48
49 virtual void reset() = 0;
50 virtual bool addPresentFence(const std::shared_ptr<FenceTime>&) = 0;
51 virtual void beginResync() = 0;
Ady Abraham5dee2f12020-02-05 17:49:47 -080052 virtual bool addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
53 bool* periodFlushed) = 0;
Lloyd Pique41be5d22018-06-21 13:11:48 -070054 virtual void endResync() = 0;
55 virtual void setPeriod(nsecs_t period) = 0;
56 virtual nsecs_t getPeriod() = 0;
Alec Mouri7355eb22019-03-05 14:19:10 -080057 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;
Lloyd Pique41be5d22018-06-21 13:11:48 -070060 virtual status_t changePhaseOffset(Callback* callback, nsecs_t phase) = 0;
Ady Abraham0ed31c92020-04-16 11:48:45 -070061 virtual nsecs_t computeNextRefresh(int periodOffset, nsecs_t now) const = 0;
Lloyd Pique41be5d22018-06-21 13:11:48 -070062 virtual void setIgnorePresentFences(bool ignore) = 0;
Ady Abraham0ed31c92020-04-16 11:48:45 -070063 virtual nsecs_t expectedPresentTime(nsecs_t now) = 0;
Lloyd Pique41be5d22018-06-21 13:11:48 -070064
Yiwei Zhang5434a782018-12-05 18:06:32 -080065 virtual void dump(std::string& result) const = 0;
Kevin DuBois7b743be2019-02-27 10:05:48 -080066
67protected:
68 DispSync(DispSync const&) = delete;
69 DispSync& operator=(DispSync const&) = delete;
Lloyd Pique41be5d22018-06-21 13:11:48 -070070};
71
72namespace impl {
73
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070074class 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.
Lloyd Pique41be5d22018-06-21 13:11:48 -070089class DispSync : public android::DispSync {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070090public:
Dominik Laskowski98041832019-08-01 18:35:59 -070091 // hasSyncFramework specifies whether the platform supports present fences.
92 DispSync(const char* name, bool hasSyncFramework);
Lloyd Pique41be5d22018-06-21 13:11:48 -070093 ~DispSync() override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070094
Andy McFadden645b1f72014-06-10 14:43:32 -070095 // reset clears the resync samples and error value.
Lloyd Pique41be5d22018-06-21 13:11:48 -070096 void reset() override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070097
98 // addPresentFence adds a fence for use in validating the current vsync
99 // event model. The fence need not be signaled at the time
100 // addPresentFence is called. When the fence does signal, its timestamp
101 // should correspond to a hardware vsync event. Unlike the
102 // addResyncSample method, the timestamps of consecutive fences need not
103 // correspond to consecutive hardware vsync events.
104 //
105 // This method should be called with the retire fence from each HWComposer
106 // set call that affects the display.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700107 bool addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700108
109 // The beginResync, addResyncSample, and endResync methods are used to re-
110 // synchronize the DispSync's model to the hardware vsync events. The re-
111 // synchronization process involves first calling beginResync, then
112 // calling addResyncSample with a sequence of consecutive hardware vsync
113 // event timestamps, and finally calling endResync when addResyncSample
114 // indicates that no more samples are needed by returning false.
115 //
116 // This resynchronization process should be performed whenever the display
117 // is turned on (i.e. once immediately after it's turned on) and whenever
118 // addPresentFence returns true indicating that the model has drifted away
119 // from the hardware vsync events.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700120 void beginResync() override;
Alec Mouri754c98a2019-03-18 18:53:42 -0700121 // Adds a vsync sample to the dispsync model. The timestamp is the time
Alec Mourif8e689c2019-05-20 18:32:22 -0700122 // of the vsync event that fired. periodFlushed will return true if the
Alec Mouri754c98a2019-03-18 18:53:42 -0700123 // vsync period was detected to have changed to mPendingPeriod.
124 //
125 // This method will return true if more vsync samples are needed to lock
126 // down the DispSync model, and false otherwise.
Alec Mourif8e689c2019-05-20 18:32:22 -0700127 // periodFlushed will be set to true if mPendingPeriod is flushed to
128 // mIntendedPeriod, and false otherwise.
Ady Abraham5dee2f12020-02-05 17:49:47 -0800129 bool addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
130 bool* periodFlushed) override;
Lloyd Pique41be5d22018-06-21 13:11:48 -0700131 void endResync() override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700132
Andy McFadden41d67d72014-04-25 16:58:34 -0700133 // The setPeriod method sets the vsync event model's period to a specific
Alec Mourif8e689c2019-05-20 18:32:22 -0700134 // 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.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700136 void setPeriod(nsecs_t period) override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700137
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700138 // The getPeriod method returns the current vsync period.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700139 nsecs_t getPeriod() override;
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700140
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700141 // addEventListener registers a callback to be called repeatedly at the
142 // given phase offset from the hardware vsync events. The callback is
143 // called from a separate thread and it should return reasonably quickly
144 // (i.e. within a few hundred microseconds).
Alec Mouri7355eb22019-03-05 14:19:10 -0800145 // If the callback was previously registered, and the last clock time the
146 // callback was invoked was known to the caller (e.g. via removeEventListener),
147 // then the caller may pass that through to lastCallbackTime, so that
148 // callbacks do not accidentally double-fire if they are unregistered and
149 // reregistered in rapid succession.
150 status_t addEventListener(const char* name, nsecs_t phase, Callback* callback,
151 nsecs_t lastCallbackTime) override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700152
153 // removeEventListener removes an already-registered event callback. Once
154 // this method returns that callback will no longer be called by the
155 // DispSync object.
Alec Mouri7355eb22019-03-05 14:19:10 -0800156 // outLastCallbackTime will contain the last time that the callback was invoked.
157 // If the caller wishes to reregister the same callback, they should pass the
158 // callback time back into lastCallbackTime (see addEventListener).
159 status_t removeEventListener(Callback* callback, nsecs_t* outLastCallbackTime) override;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700160
Dan Stoza84d619e2018-03-28 17:07:36 -0700161 // changePhaseOffset changes the phase offset of an already-registered event callback. The
162 // method will make sure that there is no skipping or double-firing on the listener per frame,
163 // even when changing the offsets multiple times.
Lloyd Pique41be5d22018-06-21 13:11:48 -0700164 status_t changePhaseOffset(Callback* callback, nsecs_t phase) override;
Dan Stoza84d619e2018-03-28 17:07:36 -0700165
Andy McFadden41d67d72014-04-25 16:58:34 -0700166 // computeNextRefresh computes when the next refresh is expected to begin.
167 // The periodOffset value can be used to move forward or backward; an
168 // offset of zero is the next refresh, -1 is the previous refresh, 1 is
169 // the refresh after next. etc.
Ady Abraham0ed31c92020-04-16 11:48:45 -0700170 nsecs_t computeNextRefresh(int periodOffset, nsecs_t now) const override;
Andy McFadden41d67d72014-04-25 16:58:34 -0700171
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700172 // In certain situations the present fences aren't a good indicator of vsync
173 // time, e.g. when vr flinger is active, or simply aren't available,
174 // e.g. when the sync framework isn't present. Use this method to toggle
175 // whether or not DispSync ignores present fences. If present fences are
176 // ignored, DispSync will always ask for hardware vsync events by returning
177 // true from addPresentFence() and addResyncSample().
Lloyd Pique41be5d22018-06-21 13:11:48 -0700178 void setIgnorePresentFences(bool ignore) override;
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700179
Ana Krulec010d2192018-10-08 06:29:54 -0700180 // Determine the expected present time when a buffer acquired now will be displayed.
Ady Abraham0ed31c92020-04-16 11:48:45 -0700181 nsecs_t expectedPresentTime(nsecs_t now);
Ana Krulec010d2192018-10-08 06:29:54 -0700182
Andy McFaddenc751e922014-05-08 14:53:26 -0700183 // dump appends human-readable debug info to the result string.
Yiwei Zhang5434a782018-12-05 18:06:32 -0800184 void dump(std::string& result) const override;
Andy McFaddenc751e922014-05-08 14:53:26 -0700185
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700186private:
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700187 void updateModelLocked();
188 void updateErrorLocked();
Steven Thomasdfde8fa2018-04-19 16:00:58 -0700189 void resetLocked();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700190 void resetErrorLocked();
191
192 enum { MAX_RESYNC_SAMPLES = 32 };
Tim Murray4a4e4a22016-04-19 16:29:23 +0000193 enum { MIN_RESYNC_SAMPLES_FOR_UPDATE = 6 };
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700194 enum { NUM_PRESENT_SAMPLES = 8 };
Dan Stozaef789162015-05-29 13:00:23 -0700195 enum { MAX_RESYNC_SAMPLES_WITHOUT_PRESENT = 4 };
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700196 enum { ACCEPTABLE_ZERO_ERR_SAMPLES_COUNT = 64 };
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700197
Tim Murray4a4e4a22016-04-19 16:29:23 +0000198 const char* const mName;
199
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700200 // mPeriod is the computed period of the modeled vsync events in
201 // nanoseconds.
202 nsecs_t mPeriod;
203
Alec Mourif8e689c2019-05-20 18:32:22 -0700204 // mIntendedPeriod is the intended period of the modeled vsync events in
205 // nanoseconds. Under ideal conditions this should be similar if not the
206 // same as mPeriod, plus or minus an observed error.
207 nsecs_t mIntendedPeriod = 0;
208
Alec Mouri754c98a2019-03-18 18:53:42 -0700209 // mPendingPeriod is the proposed period change in nanoseconds.
210 // If mPendingPeriod differs from mPeriod and is nonzero, it will
211 // be flushed to mPeriod when we detect that the hardware switched
212 // vsync frequency.
213 nsecs_t mPendingPeriod = 0;
214
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700215 // mPhase is the phase offset of the modeled vsync events. It is the
216 // number of nanoseconds from time 0 to the first vsync event.
217 nsecs_t mPhase;
218
Haixia Shi676b1f62015-10-28 16:19:01 -0700219 // mReferenceTime is the reference time of the modeled vsync events.
220 // It is the nanosecond timestamp of the first vsync event after a resync.
221 nsecs_t mReferenceTime;
222
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700223 // mError is the computed model error. It is based on the difference
224 // between the estimated vsync event times and those observed in the
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700225 // mPresentFences array.
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700226 nsecs_t mError;
227
Brian Andersonfbc80ae2017-05-26 16:23:54 -0700228 // mZeroErrSamplesCount keeps track of how many times in a row there were
229 // zero timestamps available in the mPresentFences array.
230 // Used to sanity check that we are able to calculate the model error.
231 size_t mZeroErrSamplesCount;
232
Haixia Shi676b1f62015-10-28 16:19:01 -0700233 // Whether we have updated the vsync event model since the last resync.
234 bool mModelUpdated;
235
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700236 // These member variables are the state used during the resynchronization
237 // process to store information about the hardware vsync event times used
238 // to compute the model.
Alec Mouri75b0b622019-03-12 18:56:00 +0000239 nsecs_t mResyncSamples[MAX_RESYNC_SAMPLES] = {0};
Alec Mourif8e689c2019-05-20 18:32:22 -0700240 size_t mFirstResyncSample = 0;
241 size_t mNumResyncSamples = 0;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700242 int mNumResyncSamplesSincePresent;
243
244 // These member variables store information about the present fences used
245 // to validate the currently computed model.
Lloyd Pique78ce4182018-01-31 16:39:51 -0800246 std::shared_ptr<FenceTime> mPresentFences[NUM_PRESENT_SAMPLES]{FenceTime::NO_FENCE};
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700247 size_t mPresentSampleOffset;
248
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700249 // mThread is the thread from which all the callbacks are called.
250 sp<DispSyncThread> mThread;
251
252 // mMutex is used to protect access to all member variables.
253 mutable Mutex mMutex;
Fabien Sanglardc45a7d92017-03-14 13:24:22 -0700254
Fabien Sanglardcbf153b2017-03-10 17:57:12 -0800255 // Ignore present (retire) fences if the device doesn't have support for the
256 // sync framework
257 bool mIgnorePresentFences;
Lloyd Piquee83f9312018-02-01 12:53:17 -0800258
259 std::unique_ptr<Callback> mZeroPhaseTracer;
Ana Krulec064a82f2018-09-11 16:03:03 -0700260
261 // Flag to turn on logging in systrace.
262 bool mTraceDetailedInfo = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700263};
264
Lloyd Pique41be5d22018-06-21 13:11:48 -0700265} // namespace impl
266
Lloyd Pique78ce4182018-01-31 16:39:51 -0800267} // namespace android