OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.ui; | 5 package org.chromium.ui; |
6 | 6 |
7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.os.Build; | 9 import android.os.Build; |
10 import android.os.Handler; | 10 import android.os.Handler; |
11 import android.view.Choreographer; | 11 import android.view.Choreographer; |
12 import android.view.WindowManager; | 12 import android.view.WindowManager; |
13 | 13 |
14 import org.chromium.base.TraceEvent; | 14 import org.chromium.base.TraceEvent; |
15 | 15 |
16 /** | 16 /** |
17 * Notifies clients of the default displays's vertical sync pulses. | 17 * Notifies clients of the default displays's vertical sync pulses. |
18 * This class works in "burst" mode: once the update is requested, the listener
will be | |
19 * called MAX_VSYNC_COUNT times on the vertical sync pulses (on JB) or on every
refresh | |
20 * period (on ICS, see below), unless stop() is called. | |
21 * On ICS, VSyncMonitor relies on setVSyncPointForICS() being called to set a re
asonable | 18 * On ICS, VSyncMonitor relies on setVSyncPointForICS() being called to set a re
asonable |
22 * approximation of a vertical sync starting point; see also http://crbug.com/15
6397. | 19 * approximation of a vertical sync starting point; see also http://crbug.com/15
6397. |
23 */ | 20 */ |
24 @SuppressLint("NewApi") | 21 @SuppressLint("NewApi") |
25 public class VSyncMonitor { | 22 public class VSyncMonitor { |
26 private static final long NANOSECONDS_PER_SECOND = 1000000000; | 23 private static final long NANOSECONDS_PER_SECOND = 1000000000; |
27 private static final long NANOSECONDS_PER_MILLISECOND = 1000000; | 24 private static final long NANOSECONDS_PER_MILLISECOND = 1000000; |
28 private static final long NANOSECONDS_PER_MICROSECOND = 1000; | 25 private static final long NANOSECONDS_PER_MICROSECOND = 1000; |
29 public static final int MAX_AUTO_ONVSYNC_COUNT = 5; | |
30 | 26 |
31 /** | 27 /** |
32 * VSync listener class | 28 * VSync listener class |
33 */ | 29 */ |
34 public interface Listener { | 30 public interface Listener { |
35 /** | 31 /** |
36 * Called very soon after the start of the display's vertical sync perio
d. | 32 * Called very soon after the start of the display's vertical sync perio
d. |
37 * @param monitor The VSyncMonitor that triggered the signal. | 33 * @param monitor The VSyncMonitor that triggered the signal. |
38 * @param vsyncTimeMicros Absolute frame time in microseconds. | 34 * @param vsyncTimeMicros Absolute frame time in microseconds. |
39 */ | 35 */ |
40 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros); | 36 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros); |
41 } | 37 } |
42 | 38 |
43 private Listener mListener; | 39 private Listener mListener; |
44 | 40 |
45 // Display refresh rate as reported by the system. | 41 // Display refresh rate as reported by the system. |
46 private final long mRefreshPeriodNano; | 42 private final long mRefreshPeriodNano; |
47 | 43 |
48 private boolean mHaveRequestInFlight; | 44 private boolean mHaveRequestInFlight; |
49 private int mTriggerNextVSyncCount; | |
50 | 45 |
51 // Choreographer is used to detect vsync on >= JB. | 46 // Choreographer is used to detect vsync on >= JB. |
52 private final Choreographer mChoreographer; | 47 private final Choreographer mChoreographer; |
53 private final Choreographer.FrameCallback mVSyncFrameCallback; | 48 private final Choreographer.FrameCallback mVSyncFrameCallback; |
54 | 49 |
55 // On ICS we just post a task through the handler (http://crbug.com/156397) | 50 // On ICS we just post a task through the handler (http://crbug.com/156397) |
56 private final Runnable mVSyncRunnableCallback; | 51 private final Runnable mVSyncRunnableCallback; |
57 private long mGoodStartingPointNano; | 52 private long mGoodStartingPointNano; |
58 private long mLastPostedNano; | 53 private long mLastPostedNano; |
59 | 54 |
(...skipping 17 matching lines...) Expand all Loading... |
77 * @param context The application context. | 72 * @param context The application context. |
78 * @param listener The listener receiving VSync notifications. | 73 * @param listener The listener receiving VSync notifications. |
79 * @param enableJBVsync Whether to allow Choreographer-based notifications o
n JB and up. | 74 * @param enableJBVsync Whether to allow Choreographer-based notifications o
n JB and up. |
80 */ | 75 */ |
81 public VSyncMonitor(Context context, VSyncMonitor.Listener listener, boolean
enableJBVSync) { | 76 public VSyncMonitor(Context context, VSyncMonitor.Listener listener, boolean
enableJBVSync) { |
82 mListener = listener; | 77 mListener = listener; |
83 float refreshRate = ((WindowManager) context.getSystemService(Context.WI
NDOW_SERVICE)) | 78 float refreshRate = ((WindowManager) context.getSystemService(Context.WI
NDOW_SERVICE)) |
84 .getDefaultDisplay().getRefreshRate(); | 79 .getDefaultDisplay().getRefreshRate(); |
85 if (refreshRate <= 0) refreshRate = 60; | 80 if (refreshRate <= 0) refreshRate = 60; |
86 mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate); | 81 mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate); |
87 mTriggerNextVSyncCount = 0; | |
88 | 82 |
89 if (enableJBVSync && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_
BEAN) { | 83 if (enableJBVSync && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_
BEAN) { |
90 // Use Choreographer on JB+ to get notified of vsync. | 84 // Use Choreographer on JB+ to get notified of vsync. |
91 mChoreographer = Choreographer.getInstance(); | 85 mChoreographer = Choreographer.getInstance(); |
92 mVSyncFrameCallback = new Choreographer.FrameCallback() { | 86 mVSyncFrameCallback = new Choreographer.FrameCallback() { |
93 @Override | 87 @Override |
94 public void doFrame(long frameTimeNanos) { | 88 public void doFrame(long frameTimeNanos) { |
95 TraceEvent.begin("VSync"); | 89 TraceEvent.begin("VSync"); |
96 mGoodStartingPointNano = frameTimeNanos; | 90 mGoodStartingPointNano = frameTimeNanos; |
97 onVSyncCallback(frameTimeNanos, getCurrentNanoTime()); | 91 onVSyncCallback(frameTimeNanos, getCurrentNanoTime()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 } | 128 } |
135 | 129 |
136 /** | 130 /** |
137 * Determine whether a true vsync signal is available on this platform. | 131 * Determine whether a true vsync signal is available on this platform. |
138 */ | 132 */ |
139 private boolean isVSyncSignalAvailable() { | 133 private boolean isVSyncSignalAvailable() { |
140 return mChoreographer != null; | 134 return mChoreographer != null; |
141 } | 135 } |
142 | 136 |
143 /** | 137 /** |
144 * Stop reporting vsync events. Note that at most one pending vsync event ca
n still be delivered | |
145 * after this function is called. | |
146 */ | |
147 public void stop() { | |
148 mTriggerNextVSyncCount = 0; | |
149 } | |
150 | |
151 /** | |
152 * Request to be notified of the closest display vsync events. | 138 * Request to be notified of the closest display vsync events. |
153 * Listener.onVSync() will be called soon after the upcoming vsync pulses. | 139 * Listener.onVSync() will be called soon after the upcoming vsync pulses. |
154 * It will be called at most MAX_AUTO_ONVSYNC_COUNT times unless requestUpda
te() is called. | |
155 */ | 140 */ |
156 public void requestUpdate() { | 141 public void requestUpdate() { |
157 mTriggerNextVSyncCount = MAX_AUTO_ONVSYNC_COUNT; | |
158 postCallback(); | 142 postCallback(); |
159 } | 143 } |
160 | 144 |
161 /** | 145 /** |
162 * Set the best guess of the point in the past when the vsync has happened. | 146 * Set the best guess of the point in the past when the vsync has happened. |
163 * @param goodStartingPointNano Known vsync point in the past. | 147 * @param goodStartingPointNano Known vsync point in the past. |
164 */ | 148 */ |
165 public void setVSyncPointForICS(long goodStartingPointNano) { | 149 public void setVSyncPointForICS(long goodStartingPointNano) { |
166 mGoodStartingPointNano = goodStartingPointNano; | 150 mGoodStartingPointNano = goodStartingPointNano; |
167 } | 151 } |
168 | 152 |
169 private long getCurrentNanoTime() { | 153 private long getCurrentNanoTime() { |
170 return System.nanoTime(); | 154 return System.nanoTime(); |
171 } | 155 } |
172 | 156 |
173 private void onVSyncCallback(long frameTimeNanos, long currentTimeNanos) { | 157 private void onVSyncCallback(long frameTimeNanos, long currentTimeNanos) { |
174 assert mHaveRequestInFlight; | 158 assert mHaveRequestInFlight; |
175 mHaveRequestInFlight = false; | 159 mHaveRequestInFlight = false; |
176 mLastVSyncCpuTimeNano = currentTimeNanos; | 160 mLastVSyncCpuTimeNano = currentTimeNanos; |
177 if (mTriggerNextVSyncCount >= 0) { | |
178 mTriggerNextVSyncCount--; | |
179 postCallback(); | |
180 } | |
181 if (mListener != null) { | 161 if (mListener != null) { |
182 mListener.onVSync(this, frameTimeNanos / NANOSECONDS_PER_MICROSECOND
); | 162 mListener.onVSync(this, frameTimeNanos / NANOSECONDS_PER_MICROSECOND
); |
183 } | 163 } |
184 } | 164 } |
185 | 165 |
186 private void postCallback() { | 166 private void postCallback() { |
187 if (mHaveRequestInFlight) return; | 167 if (mHaveRequestInFlight) return; |
188 mHaveRequestInFlight = true; | 168 mHaveRequestInFlight = true; |
189 if (postSyntheticVSync()) return; | 169 if (postSyntheticVSync()) return; |
190 if (isVSyncSignalAvailable()) { | 170 if (isVSyncSignalAvailable()) { |
(...skipping 28 matching lines...) Expand all Loading... |
219 | 199 |
220 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { | 200 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { |
221 delay += mRefreshPeriodNano; | 201 delay += mRefreshPeriodNano; |
222 } | 202 } |
223 | 203 |
224 mLastPostedNano = currentTime + delay; | 204 mLastPostedNano = currentTime + delay; |
225 if (delay == 0) mHandler.post(mVSyncRunnableCallback); | 205 if (delay == 0) mHandler.post(mVSyncRunnableCallback); |
226 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE
R_MILLISECOND); | 206 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE
R_MILLISECOND); |
227 } | 207 } |
228 } | 208 } |
OLD | NEW |