blob: 5106bc89dea8f7736120285add0aa68fda3256dd [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>
23#include <utils/Timers.h>
24#include <utils/RefBase.h>
25
26namespace android {
27
Andy McFadden5167ec62014-05-22 13:08:43 -070028// Ignore present (retire) fences if the device doesn't have support for the
29// sync framework, or if all phase offsets are zero. The latter is useful
30// because it allows us to avoid resync bursts on devices that don't need
31// phase-offset VSYNC events.
32#if defined(RUNNING_WITHOUT_SYNC_FRAMEWORK) || \
33 (VSYNC_EVENT_PHASE_OFFSET_NS == 0 && SF_VSYNC_EVENT_PHASE_OFFSET_NS == 0)
34static const bool kIgnorePresentFences = true;
35#else
36static const bool kIgnorePresentFences = false;
37#endif
38
39
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070040class String8;
41class Fence;
42class DispSyncThread;
43
44// DispSync maintains a model of the periodic hardware-based vsync events of a
45// display and uses that model to execute period callbacks at specific phase
46// offsets from the hardware vsync events. The model is constructed by
47// feeding consecutive hardware event timestamps to the DispSync object via
48// the addResyncSample method.
49//
50// The model is validated using timestamps from Fence objects that are passed
51// to the DispSync object via the addPresentFence method. These fence
52// timestamps should correspond to a hardware vsync event, but they need not
53// be consecutive hardware vsync times. If this method determines that the
54// current model accurately represents the hardware event times it will return
55// false to indicate that a resynchronization (via addResyncSample) is not
56// needed.
57class DispSync {
58
59public:
60
61 class Callback: public virtual RefBase {
62 public:
63 virtual ~Callback() {};
64 virtual void onDispSyncEvent(nsecs_t when) = 0;
65 };
66
67 DispSync();
68 ~DispSync();
69
70 void reset();
71
72 // addPresentFence adds a fence for use in validating the current vsync
73 // event model. The fence need not be signaled at the time
74 // addPresentFence is called. When the fence does signal, its timestamp
75 // should correspond to a hardware vsync event. Unlike the
76 // addResyncSample method, the timestamps of consecutive fences need not
77 // correspond to consecutive hardware vsync events.
78 //
79 // This method should be called with the retire fence from each HWComposer
80 // set call that affects the display.
81 bool addPresentFence(const sp<Fence>& fence);
82
83 // The beginResync, addResyncSample, and endResync methods are used to re-
84 // synchronize the DispSync's model to the hardware vsync events. The re-
85 // synchronization process involves first calling beginResync, then
86 // calling addResyncSample with a sequence of consecutive hardware vsync
87 // event timestamps, and finally calling endResync when addResyncSample
88 // indicates that no more samples are needed by returning false.
89 //
90 // This resynchronization process should be performed whenever the display
91 // is turned on (i.e. once immediately after it's turned on) and whenever
92 // addPresentFence returns true indicating that the model has drifted away
93 // from the hardware vsync events.
94 void beginResync();
95 bool addResyncSample(nsecs_t timestamp);
96 void endResync();
97
Andy McFadden41d67d72014-04-25 16:58:34 -070098 // The setPeriod method sets the vsync event model's period to a specific
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070099 // value. This should be used to prime the model when a display is first
100 // turned on. It should NOT be used after that.
101 void setPeriod(nsecs_t period);
102
Ruchi Kandoif52b3c82014-04-24 16:42:35 -0700103 // Setting the low power mode reduces the frame rate to half of the default
104 void setLowPowerMode(bool enabled);
105
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700106 // addEventListener registers a callback to be called repeatedly at the
107 // given phase offset from the hardware vsync events. The callback is
108 // called from a separate thread and it should return reasonably quickly
109 // (i.e. within a few hundred microseconds).
110 status_t addEventListener(nsecs_t phase, const sp<Callback>& callback);
111
112 // removeEventListener removes an already-registered event callback. Once
113 // this method returns that callback will no longer be called by the
114 // DispSync object.
115 status_t removeEventListener(const sp<Callback>& callback);
116
Andy McFadden41d67d72014-04-25 16:58:34 -0700117 // computeNextRefresh computes when the next refresh is expected to begin.
118 // The periodOffset value can be used to move forward or backward; an
119 // offset of zero is the next refresh, -1 is the previous refresh, 1 is
120 // the refresh after next. etc.
121 nsecs_t computeNextRefresh(int periodOffset) const;
122
Andy McFaddenc751e922014-05-08 14:53:26 -0700123 // dump appends human-readable debug info to the result string.
124 void dump(String8& result) const;
125
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700126private:
127
128 void updateModelLocked();
129 void updateErrorLocked();
130 void resetErrorLocked();
131
132 enum { MAX_RESYNC_SAMPLES = 32 };
133 enum { MIN_RESYNC_SAMPLES_FOR_UPDATE = 3 };
134 enum { NUM_PRESENT_SAMPLES = 8 };
135 enum { MAX_RESYNC_SAMPLES_WITHOUT_PRESENT = 12 };
136
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
145 // mError is the computed model error. It is based on the difference
146 // between the estimated vsync event times and those observed in the
147 // mPresentTimes array.
148 nsecs_t mError;
149
150 // These member variables are the state used during the resynchronization
151 // process to store information about the hardware vsync event times used
152 // to compute the model.
153 nsecs_t mResyncSamples[MAX_RESYNC_SAMPLES];
154 size_t mFirstResyncSample;
155 size_t mNumResyncSamples;
156 int mNumResyncSamplesSincePresent;
157
158 // These member variables store information about the present fences used
159 // to validate the currently computed model.
160 sp<Fence> mPresentFences[NUM_PRESENT_SAMPLES];
161 nsecs_t mPresentTimes[NUM_PRESENT_SAMPLES];
162 size_t mPresentSampleOffset;
163
164 // mThread is the thread from which all the callbacks are called.
165 sp<DispSyncThread> mThread;
166
167 // mMutex is used to protect access to all member variables.
168 mutable Mutex mMutex;
169};
170
171}
172
173#endif // ANDROID_DISPSYNC_H