Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: ui/android/java/src/org/chromium/ui/VSyncMonitor.java

Issue 611313003: Use estimated vsync period on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change lastRefreshDurationWeight. Use estimation also when getRefreshRate() is <= 0. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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;
jdduke (slow) 2014/10/08 03:17:02 I don't think this is used any longer?
28
27 private boolean mInsideVSync = false; 29 private boolean mInsideVSync = false;
28 30
31 // Conservative guess about vsync's consecutivity.
32 // If true, next tick is guaranteed to be consecutive.
33 private boolean mConsecutiveVSync = false;
34
29 /** 35 /**
30 * VSync listener class 36 * VSync listener class
31 */ 37 */
32 public interface Listener { 38 public interface Listener {
33 /** 39 /**
34 * Called very soon after the start of the display's vertical sync perio d. 40 * Called very soon after the start of the display's vertical sync perio d.
35 * @param monitor The VSyncMonitor that triggered the signal. 41 * @param monitor The VSyncMonitor that triggered the signal.
36 * @param vsyncTimeMicros Absolute frame time in microseconds. 42 * @param vsyncTimeMicros Absolute frame time in microseconds.
37 */ 43 */
38 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros); 44 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros);
39 } 45 }
40 46
41 private Listener mListener; 47 private Listener mListener;
42 48
43 // Display refresh rate as reported by the system. 49 // Display refresh rate as reported by the system.
44 private final long mRefreshPeriodNano; 50 private long mRefreshPeriodNano;
45 51
46 private boolean mHaveRequestInFlight; 52 private boolean mHaveRequestInFlight;
47 53
48 // Choreographer is used to detect vsync on >= JB. 54 // Choreographer is used to detect vsync on >= JB.
49 private final Choreographer mChoreographer; 55 private final Choreographer mChoreographer;
50 private final Choreographer.FrameCallback mVSyncFrameCallback; 56 private final Choreographer.FrameCallback mVSyncFrameCallback;
51 57
52 // On ICS we just post a task through the handler (http://crbug.com/156397) 58 // On ICS we just post a task through the handler (http://crbug.com/156397)
53 private final Runnable mVSyncRunnableCallback; 59 private final Runnable mVSyncRunnableCallback;
54 private long mGoodStartingPointNano; 60 private long mGoodStartingPointNano;
(...skipping 17 matching lines...) Expand all
72 /** 78 /**
73 * Constructs a VSyncMonitor 79 * Constructs a VSyncMonitor
74 * @param context The application context. 80 * @param context The application context.
75 * @param listener The listener receiving VSync notifications. 81 * @param listener The listener receiving VSync notifications.
76 * @param enableJBVsync Whether to allow Choreographer-based notifications o n JB and up. 82 * @param enableJBVsync Whether to allow Choreographer-based notifications o n JB and up.
77 */ 83 */
78 public VSyncMonitor(Context context, VSyncMonitor.Listener listener, boolean enableJBVSync) { 84 public VSyncMonitor(Context context, VSyncMonitor.Listener listener, boolean enableJBVSync) {
79 mListener = listener; 85 mListener = listener;
80 float refreshRate = ((WindowManager) context.getSystemService(Context.WI NDOW_SERVICE)) 86 float refreshRate = ((WindowManager) context.getSystemService(Context.WI NDOW_SERVICE))
81 .getDefaultDisplay().getRefreshRate(); 87 .getDefaultDisplay().getRefreshRate();
88 final boolean useEstimatedRefreshPeriod = refreshRate < 30;
jdduke (slow) 2014/10/08 03:17:02 I see 30 both here and above in the MAX_VALID_VSYN
89
82 if (refreshRate <= 0) refreshRate = 60; 90 if (refreshRate <= 0) refreshRate = 60;
83 mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate); 91 mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate);
84 92
85 if (enableJBVSync && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_ BEAN) { 93 if (enableJBVSync && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_ BEAN) {
86 // Use Choreographer on JB+ to get notified of vsync. 94 // Use Choreographer on JB+ to get notified of vsync.
87 mChoreographer = Choreographer.getInstance(); 95 mChoreographer = Choreographer.getInstance();
88 mVSyncFrameCallback = new Choreographer.FrameCallback() { 96 mVSyncFrameCallback = new Choreographer.FrameCallback() {
89 @Override 97 @Override
90 public void doFrame(long frameTimeNanos) { 98 public void doFrame(long frameTimeNanos) {
91 TraceEvent.begin("VSync"); 99 TraceEvent.begin("VSync");
100 if (useEstimatedRefreshPeriod && mConsecutiveVSync) {
101 // Display.getRefreshRate() is unreliable on some platfo rms.
102 // Adjust refresh period- initial value is based on Disp lay.getRefreshRate()
103 // after that it asymptotically approaches the real valu e.
104 long lastRefreshDurationNano = frameTimeNanos - mGoodSta rtingPointNano;
105 float lastRefreshDurationWeight = 0.1f;
106 mRefreshPeriodNano += (long) (lastRefreshDurationWeight *
jdduke (slow) 2014/10/08 03:17:02 Hmm, I could be wrong, but don't you need to rewei
Sami 2014/10/08 10:14:51 Hmm, I think that reduces to mRefreshPeriodNano =
jdduke (slow) 2014/10/08 13:06:00 Of course, sigh, I should actually read the variab
107 (lastRefreshDurationNano - mRefreshPeriodNano));
108 }
92 mGoodStartingPointNano = frameTimeNanos; 109 mGoodStartingPointNano = frameTimeNanos;
93 onVSyncCallback(frameTimeNanos, getCurrentNanoTime()); 110 onVSyncCallback(frameTimeNanos, getCurrentNanoTime());
94 TraceEvent.end("VSync"); 111 TraceEvent.end("VSync");
95 } 112 }
96 }; 113 };
97 mVSyncRunnableCallback = null; 114 mVSyncRunnableCallback = null;
98 } else { 115 } else {
99 // On ICS we just hope that running tasks is relatively predictable. 116 // On ICS we just hope that running tasks is relatively predictable.
100 mChoreographer = null; 117 mChoreographer = null;
101 mVSyncFrameCallback = null; 118 mVSyncFrameCallback = null;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 } finally { 196 } finally {
180 mInsideVSync = false; 197 mInsideVSync = false;
181 } 198 }
182 } 199 }
183 200
184 private void postCallback() { 201 private void postCallback() {
185 if (mHaveRequestInFlight) return; 202 if (mHaveRequestInFlight) return;
186 mHaveRequestInFlight = true; 203 mHaveRequestInFlight = true;
187 if (postSyntheticVSync()) return; 204 if (postSyntheticVSync()) return;
188 if (isVSyncSignalAvailable()) { 205 if (isVSyncSignalAvailable()) {
206 mConsecutiveVSync = mInsideVSync;
189 mChoreographer.postFrameCallback(mVSyncFrameCallback); 207 mChoreographer.postFrameCallback(mVSyncFrameCallback);
190 } else { 208 } else {
191 postRunnableCallback(); 209 postRunnableCallback();
192 } 210 }
193 } 211 }
194 212
195 private boolean postSyntheticVSync() { 213 private boolean postSyntheticVSync() {
196 final long currentTime = getCurrentNanoTime(); 214 final long currentTime = getCurrentNanoTime();
197 // Only trigger a synthetic vsync if we've been idle for long enough and the upcoming real 215 // 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. 216 // vsync is more than half a frame away.
(...skipping 18 matching lines...) Expand all
217 235
218 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { 236 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) {
219 delay += mRefreshPeriodNano; 237 delay += mRefreshPeriodNano;
220 } 238 }
221 239
222 mLastPostedNano = currentTime + delay; 240 mLastPostedNano = currentTime + delay;
223 if (delay == 0) mHandler.post(mVSyncRunnableCallback); 241 if (delay == 0) mHandler.post(mVSyncRunnableCallback);
224 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE R_MILLISECOND); 242 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE R_MILLISECOND);
225 } 243 }
226 } 244 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698