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

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: Review follow-up 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 PERIOD_THRESHOLD_NANOSECONDS = NANOSECONDS_PER_SEC OND / 30;
Sami 2014/10/01 11:42:49 nit: Maybe call this something like MAX_VALID_VSYN
28
27 private boolean mInsideVSync = false; 29 private boolean mInsideVSync = false;
28 30
29 /** 31 /**
30 * VSync listener class 32 * VSync listener class
31 */ 33 */
32 public interface Listener { 34 public interface Listener {
33 /** 35 /**
34 * Called very soon after the start of the display's vertical sync perio d. 36 * Called very soon after the start of the display's vertical sync perio d.
35 * @param monitor The VSyncMonitor that triggered the signal. 37 * @param monitor The VSyncMonitor that triggered the signal.
36 * @param vsyncTimeMicros Absolute frame time in microseconds. 38 * @param vsyncTimeMicros Absolute frame time in microseconds.
37 */ 39 */
38 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros); 40 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros);
39 } 41 }
40 42
41 private Listener mListener; 43 private Listener mListener;
42 44
43 // Display refresh rate as reported by the system. 45 // Display refresh rate as reported by the system.
44 private final long mRefreshPeriodNano; 46 private long mRefreshPeriodNano;
45 47
46 private boolean mHaveRequestInFlight; 48 private boolean mHaveRequestInFlight;
47 49
48 // Choreographer is used to detect vsync on >= JB. 50 // Choreographer is used to detect vsync on >= JB.
49 private final Choreographer mChoreographer; 51 private final Choreographer mChoreographer;
50 private final Choreographer.FrameCallback mVSyncFrameCallback; 52 private final Choreographer.FrameCallback mVSyncFrameCallback;
51 53
52 // On ICS we just post a task through the handler (http://crbug.com/156397) 54 // On ICS we just post a task through the handler (http://crbug.com/156397)
53 private final Runnable mVSyncRunnableCallback; 55 private final Runnable mVSyncRunnableCallback;
54 private long mGoodStartingPointNano; 56 private long mGoodStartingPointNano;
(...skipping 23 matching lines...) Expand all
78 public VSyncMonitor(Context context, VSyncMonitor.Listener listener, boolean enableJBVSync) { 80 public VSyncMonitor(Context context, VSyncMonitor.Listener listener, boolean enableJBVSync) {
79 mListener = listener; 81 mListener = listener;
80 float refreshRate = ((WindowManager) context.getSystemService(Context.WI NDOW_SERVICE)) 82 float refreshRate = ((WindowManager) context.getSystemService(Context.WI NDOW_SERVICE))
81 .getDefaultDisplay().getRefreshRate(); 83 .getDefaultDisplay().getRefreshRate();
82 if (refreshRate <= 0) refreshRate = 60; 84 if (refreshRate <= 0) refreshRate = 60;
83 mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate); 85 mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate);
84 86
85 if (enableJBVSync && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_ BEAN) { 87 if (enableJBVSync && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_ BEAN) {
86 // Use Choreographer on JB+ to get notified of vsync. 88 // Use Choreographer on JB+ to get notified of vsync.
87 mChoreographer = Choreographer.getInstance(); 89 mChoreographer = Choreographer.getInstance();
90 final boolean useEstimatedRefreshPeriod =
91 mRefreshPeriodNano > PERIOD_THRESHOLD_NANOSECONDS;
88 mVSyncFrameCallback = new Choreographer.FrameCallback() { 92 mVSyncFrameCallback = new Choreographer.FrameCallback() {
89 @Override 93 @Override
90 public void doFrame(long frameTimeNanos) { 94 public void doFrame(long frameTimeNanos) {
91 TraceEvent.begin("VSync"); 95 TraceEvent.begin("VSync");
96 if (useEstimatedRefreshPeriod) {
97 // Display.getRefreshRate() is unreliable on some platfo rms.
98 // Adjust refresh period- initial value is based on Disp lay.getRefreshRate()
99 // after that it asymptotically approaches the real valu e.
100 long lastRefreshDurationNano = frameTimeNanos - mGoodSta rtingPointNano;
101 if (lastRefreshDurationNano <= PERIOD_THRESHOLD_NANOSECO NDS) {
102 mRefreshPeriodNano = (mRefreshPeriodNano + lastRefre shDurationNano) / 2;
103 }
104 }
92 mGoodStartingPointNano = frameTimeNanos; 105 mGoodStartingPointNano = frameTimeNanos;
93 onVSyncCallback(frameTimeNanos, getCurrentNanoTime()); 106 onVSyncCallback(frameTimeNanos, getCurrentNanoTime());
94 TraceEvent.end("VSync"); 107 TraceEvent.end("VSync");
95 } 108 }
96 }; 109 };
97 mVSyncRunnableCallback = null; 110 mVSyncRunnableCallback = null;
98 } else { 111 } else {
99 // On ICS we just hope that running tasks is relatively predictable. 112 // On ICS we just hope that running tasks is relatively predictable.
100 mChoreographer = null; 113 mChoreographer = null;
101 mVSyncFrameCallback = null; 114 mVSyncFrameCallback = null;
(...skipping 23 matching lines...) Expand all
125 /** 138 /**
126 * Returns the time interval between two consecutive vsync pulses in microse conds. 139 * Returns the time interval between two consecutive vsync pulses in microse conds.
127 */ 140 */
128 public long getVSyncPeriodInMicroseconds() { 141 public long getVSyncPeriodInMicroseconds() {
129 return mRefreshPeriodNano / NANOSECONDS_PER_MICROSECOND; 142 return mRefreshPeriodNano / NANOSECONDS_PER_MICROSECOND;
130 } 143 }
131 144
132 /** 145 /**
133 * Determine whether a true vsync signal is available on this platform. 146 * Determine whether a true vsync signal is available on this platform.
134 */ 147 */
135 private boolean isVSyncSignalAvailable() { 148 public boolean isVSyncSignalAvailable() {
Sami 2014/10/01 11:42:49 Not needed?
136 return mChoreographer != null; 149 return mChoreographer != null;
137 } 150 }
138 151
139 /** 152 /**
140 * Request to be notified of the closest display vsync events. 153 * Request to be notified of the closest display vsync events.
141 * Listener.onVSync() will be called soon after the upcoming vsync pulses. 154 * Listener.onVSync() will be called soon after the upcoming vsync pulses.
142 */ 155 */
143 public void requestUpdate() { 156 public void requestUpdate() {
144 postCallback(); 157 postCallback();
145 } 158 }
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 230
218 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { 231 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) {
219 delay += mRefreshPeriodNano; 232 delay += mRefreshPeriodNano;
220 } 233 }
221 234
222 mLastPostedNano = currentTime + delay; 235 mLastPostedNano = currentTime + delay;
223 if (delay == 0) mHandler.post(mVSyncRunnableCallback); 236 if (delay == 0) mHandler.post(mVSyncRunnableCallback);
224 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE R_MILLISECOND); 237 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE R_MILLISECOND);
225 } 238 }
226 } 239 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698