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 * 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 |
19 * 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. |
20 */ | 20 */ |
21 @SuppressLint("NewApi") | 21 @SuppressLint("NewApi") |
22 public class VSyncMonitor { | 22 public class VSyncMonitor { |
23 private static final long NANOSECONDS_PER_SECOND = 1000000000; | 23 private static final long NANOSECONDS_PER_SECOND = 1000000000; |
24 private static final long NANOSECONDS_PER_MILLISECOND = 1000000; | 24 private static final long NANOSECONDS_PER_MILLISECOND = 1000000; |
25 private static final long NANOSECONDS_PER_MICROSECOND = 1000; | 25 private static final long NANOSECONDS_PER_MICROSECOND = 1000; |
26 | 26 |
27 private static final long MAX_VALID_VSYNC_PERIOD_NANOSECONDS = NANOSECONDS_P ER_SECOND / 30; | |
28 | |
27 private boolean mInsideVSync = false; | 29 private boolean mInsideVSync = false; |
30 private boolean mConsecutiveVSync = false; | |
28 | 31 |
29 /** | 32 /** |
30 * VSync listener class | 33 * VSync listener class |
31 */ | 34 */ |
32 public interface Listener { | 35 public interface Listener { |
33 /** | 36 /** |
34 * Called very soon after the start of the display's vertical sync perio d. | 37 * Called very soon after the start of the display's vertical sync perio d. |
35 * @param monitor The VSyncMonitor that triggered the signal. | 38 * @param monitor The VSyncMonitor that triggered the signal. |
36 * @param vsyncTimeMicros Absolute frame time in microseconds. | 39 * @param vsyncTimeMicros Absolute frame time in microseconds. |
37 */ | 40 */ |
38 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros); | 41 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros); |
39 } | 42 } |
40 | 43 |
41 private Listener mListener; | 44 private Listener mListener; |
42 | 45 |
43 // Display refresh rate as reported by the system. | 46 // Display refresh rate as reported by the system. |
44 private final long mRefreshPeriodNano; | 47 private long mRefreshPeriodNano; |
45 | 48 |
46 private boolean mHaveRequestInFlight; | 49 private boolean mHaveRequestInFlight; |
47 | 50 |
48 // Choreographer is used to detect vsync on >= JB. | 51 // Choreographer is used to detect vsync on >= JB. |
49 private final Choreographer mChoreographer; | 52 private final Choreographer mChoreographer; |
50 private final Choreographer.FrameCallback mVSyncFrameCallback; | 53 private final Choreographer.FrameCallback mVSyncFrameCallback; |
51 | 54 |
52 // On ICS we just post a task through the handler (http://crbug.com/156397) | 55 // On ICS we just post a task through the handler (http://crbug.com/156397) |
53 private final Runnable mVSyncRunnableCallback; | 56 private final Runnable mVSyncRunnableCallback; |
54 private long mGoodStartingPointNano; | 57 private long mGoodStartingPointNano; |
(...skipping 23 matching lines...) Expand all Loading... | |
78 public VSyncMonitor(Context context, VSyncMonitor.Listener listener, boolean enableJBVSync) { | 81 public VSyncMonitor(Context context, VSyncMonitor.Listener listener, boolean enableJBVSync) { |
79 mListener = listener; | 82 mListener = listener; |
80 float refreshRate = ((WindowManager) context.getSystemService(Context.WI NDOW_SERVICE)) | 83 float refreshRate = ((WindowManager) context.getSystemService(Context.WI NDOW_SERVICE)) |
81 .getDefaultDisplay().getRefreshRate(); | 84 .getDefaultDisplay().getRefreshRate(); |
82 if (refreshRate <= 0) refreshRate = 60; | 85 if (refreshRate <= 0) refreshRate = 60; |
83 mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate); | 86 mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate); |
84 | 87 |
85 if (enableJBVSync && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_ BEAN) { | 88 if (enableJBVSync && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_ BEAN) { |
86 // Use Choreographer on JB+ to get notified of vsync. | 89 // Use Choreographer on JB+ to get notified of vsync. |
87 mChoreographer = Choreographer.getInstance(); | 90 mChoreographer = Choreographer.getInstance(); |
91 final boolean useEstimatedRefreshPeriod = | |
92 mRefreshPeriodNano > MAX_VALID_VSYNC_PERIOD_NANOSECONDS; | |
Sami
2014/10/02 15:52:31
Indent by +4 spaces.
| |
88 mVSyncFrameCallback = new Choreographer.FrameCallback() { | 93 mVSyncFrameCallback = new Choreographer.FrameCallback() { |
89 @Override | 94 @Override |
90 public void doFrame(long frameTimeNanos) { | 95 public void doFrame(long frameTimeNanos) { |
91 TraceEvent.begin("VSync"); | 96 TraceEvent.begin("VSync"); |
97 if (useEstimatedRefreshPeriod && mConsecutiveVSync) { | |
98 // Display.getRefreshRate() is unreliable on some platfo rms. | |
99 // Adjust refresh period- initial value is based on Disp lay.getRefreshRate() | |
100 // after that it asymptotically approaches the real valu e. | |
101 long lastRefreshDurationNano = frameTimeNanos - mGoodSta rtingPointNano; | |
102 float lastRefreshDurationWeight = 0.75; | |
103 mRefreshPeriodNano = (long) ( | |
Sami
2014/10/02 15:52:31
nit: This would be slightly more efficient as:
| |
104 mRefreshPeriodNano * (1.0f - lastRefreshDurationWeig ht) + | |
105 lastRefreshDurationNano * lastRefreshDurationWeight) ; | |
106 } | |
92 mGoodStartingPointNano = frameTimeNanos; | 107 mGoodStartingPointNano = frameTimeNanos; |
93 onVSyncCallback(frameTimeNanos, getCurrentNanoTime()); | 108 onVSyncCallback(frameTimeNanos, getCurrentNanoTime()); |
94 TraceEvent.end("VSync"); | 109 TraceEvent.end("VSync"); |
95 } | 110 } |
96 }; | 111 }; |
97 mVSyncRunnableCallback = null; | 112 mVSyncRunnableCallback = null; |
98 } else { | 113 } else { |
99 // On ICS we just hope that running tasks is relatively predictable. | 114 // On ICS we just hope that running tasks is relatively predictable. |
100 mChoreographer = null; | 115 mChoreographer = null; |
101 mVSyncFrameCallback = null; | 116 mVSyncFrameCallback = null; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 } finally { | 194 } finally { |
180 mInsideVSync = false; | 195 mInsideVSync = false; |
181 } | 196 } |
182 } | 197 } |
183 | 198 |
184 private void postCallback() { | 199 private void postCallback() { |
185 if (mHaveRequestInFlight) return; | 200 if (mHaveRequestInFlight) return; |
186 mHaveRequestInFlight = true; | 201 mHaveRequestInFlight = true; |
187 if (postSyntheticVSync()) return; | 202 if (postSyntheticVSync()) return; |
188 if (isVSyncSignalAvailable()) { | 203 if (isVSyncSignalAvailable()) { |
204 mConsecutiveVSync = mInsideVSync; | |
Sami
2014/10/02 15:52:31
This can have some false negatives if the next fra
| |
189 mChoreographer.postFrameCallback(mVSyncFrameCallback); | 205 mChoreographer.postFrameCallback(mVSyncFrameCallback); |
190 } else { | 206 } else { |
191 postRunnableCallback(); | 207 postRunnableCallback(); |
192 } | 208 } |
193 } | 209 } |
194 | 210 |
195 private boolean postSyntheticVSync() { | 211 private boolean postSyntheticVSync() { |
196 final long currentTime = getCurrentNanoTime(); | 212 final long currentTime = getCurrentNanoTime(); |
197 // Only trigger a synthetic vsync if we've been idle for long enough and the upcoming real | 213 // Only trigger a synthetic vsync if we've been idle for long enough and the upcoming real |
198 // vsync is more than half a frame away. | 214 // vsync is more than half a frame away. |
(...skipping 18 matching lines...) Expand all Loading... | |
217 | 233 |
218 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { | 234 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { |
219 delay += mRefreshPeriodNano; | 235 delay += mRefreshPeriodNano; |
220 } | 236 } |
221 | 237 |
222 mLastPostedNano = currentTime + delay; | 238 mLastPostedNano = currentTime + delay; |
223 if (delay == 0) mHandler.post(mVSyncRunnableCallback); | 239 if (delay == 0) mHandler.post(mVSyncRunnableCallback); |
224 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE R_MILLISECOND); | 240 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE R_MILLISECOND); |
225 } | 241 } |
226 } | 242 } |
OLD | NEW |