| 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. |
| 18 * On ICS, VSyncMonitor relies on setVSyncPointForICS() being called to set a re
asonable | 21 * 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. | 22 * approximation of a vertical sync starting point; see also http://crbug.com/15
6397. |
| 20 */ | 23 */ |
| 21 @SuppressLint("NewApi") | 24 @SuppressLint("NewApi") |
| 22 public class VSyncMonitor { | 25 public class VSyncMonitor { |
| 23 private static final long NANOSECONDS_PER_SECOND = 1000000000; | 26 private static final long NANOSECONDS_PER_SECOND = 1000000000; |
| 24 private static final long NANOSECONDS_PER_MILLISECOND = 1000000; | 27 private static final long NANOSECONDS_PER_MILLISECOND = 1000000; |
| 25 private static final long NANOSECONDS_PER_MICROSECOND = 1000; | 28 private static final long NANOSECONDS_PER_MICROSECOND = 1000; |
| 29 public static final int MAX_AUTO_ONVSYNC_COUNT = 5; |
| 26 | 30 |
| 27 /** | 31 /** |
| 28 * VSync listener class | 32 * VSync listener class |
| 29 */ | 33 */ |
| 30 public interface Listener { | 34 public interface Listener { |
| 31 /** | 35 /** |
| 32 * 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. |
| 33 * @param monitor The VSyncMonitor that triggered the signal. | 37 * @param monitor The VSyncMonitor that triggered the signal. |
| 34 * @param vsyncTimeMicros Absolute frame time in microseconds. | 38 * @param vsyncTimeMicros Absolute frame time in microseconds. |
| 35 */ | 39 */ |
| 36 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros); | 40 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros); |
| 37 } | 41 } |
| 38 | 42 |
| 39 private Listener mListener; | 43 private Listener mListener; |
| 40 | 44 |
| 41 // Display refresh rate as reported by the system. | 45 // Display refresh rate as reported by the system. |
| 42 private final long mRefreshPeriodNano; | 46 private final long mRefreshPeriodNano; |
| 43 | 47 |
| 44 private boolean mHaveRequestInFlight; | 48 private boolean mHaveRequestInFlight; |
| 49 private int mTriggerNextVSyncCount; |
| 45 | 50 |
| 46 // Choreographer is used to detect vsync on >= JB. | 51 // Choreographer is used to detect vsync on >= JB. |
| 47 private final Choreographer mChoreographer; | 52 private final Choreographer mChoreographer; |
| 48 private final Choreographer.FrameCallback mVSyncFrameCallback; | 53 private final Choreographer.FrameCallback mVSyncFrameCallback; |
| 49 | 54 |
| 50 // 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) |
| 51 private final Runnable mVSyncRunnableCallback; | 56 private final Runnable mVSyncRunnableCallback; |
| 52 private long mGoodStartingPointNano; | 57 private long mGoodStartingPointNano; |
| 53 private long mLastPostedNano; | 58 private long mLastPostedNano; |
| 54 | 59 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 72 * @param context The application context. | 77 * @param context The application context. |
| 73 * @param listener The listener receiving VSync notifications. | 78 * @param listener The listener receiving VSync notifications. |
| 74 * @param enableJBVsync Whether to allow Choreographer-based notifications o
n JB and up. | 79 * @param enableJBVsync Whether to allow Choreographer-based notifications o
n JB and up. |
| 75 */ | 80 */ |
| 76 public VSyncMonitor(Context context, VSyncMonitor.Listener listener, boolean
enableJBVSync) { | 81 public VSyncMonitor(Context context, VSyncMonitor.Listener listener, boolean
enableJBVSync) { |
| 77 mListener = listener; | 82 mListener = listener; |
| 78 float refreshRate = ((WindowManager) context.getSystemService(Context.WI
NDOW_SERVICE)) | 83 float refreshRate = ((WindowManager) context.getSystemService(Context.WI
NDOW_SERVICE)) |
| 79 .getDefaultDisplay().getRefreshRate(); | 84 .getDefaultDisplay().getRefreshRate(); |
| 80 if (refreshRate <= 0) refreshRate = 60; | 85 if (refreshRate <= 0) refreshRate = 60; |
| 81 mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate); | 86 mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate); |
| 87 mTriggerNextVSyncCount = 0; |
| 82 | 88 |
| 83 if (enableJBVSync && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_
BEAN) { | 89 if (enableJBVSync && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_
BEAN) { |
| 84 // Use Choreographer on JB+ to get notified of vsync. | 90 // Use Choreographer on JB+ to get notified of vsync. |
| 85 mChoreographer = Choreographer.getInstance(); | 91 mChoreographer = Choreographer.getInstance(); |
| 86 mVSyncFrameCallback = new Choreographer.FrameCallback() { | 92 mVSyncFrameCallback = new Choreographer.FrameCallback() { |
| 87 @Override | 93 @Override |
| 88 public void doFrame(long frameTimeNanos) { | 94 public void doFrame(long frameTimeNanos) { |
| 89 TraceEvent.begin("VSync"); | 95 TraceEvent.begin("VSync"); |
| 90 mGoodStartingPointNano = frameTimeNanos; | 96 mGoodStartingPointNano = frameTimeNanos; |
| 91 onVSyncCallback(frameTimeNanos, getCurrentNanoTime()); | 97 onVSyncCallback(frameTimeNanos, getCurrentNanoTime()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 */ | 138 */ |
| 133 private boolean isVSyncSignalAvailable() { | 139 private boolean isVSyncSignalAvailable() { |
| 134 return mChoreographer != null; | 140 return mChoreographer != null; |
| 135 } | 141 } |
| 136 | 142 |
| 137 /** | 143 /** |
| 138 * Stop reporting vsync events. Note that at most one pending vsync event ca
n still be delivered | 144 * Stop reporting vsync events. Note that at most one pending vsync event ca
n still be delivered |
| 139 * after this function is called. | 145 * after this function is called. |
| 140 */ | 146 */ |
| 141 public void stop() { | 147 public void stop() { |
| 148 mTriggerNextVSyncCount = 0; |
| 142 } | 149 } |
| 143 | 150 |
| 144 /** | 151 /** |
| 145 * Request to be notified of the closest display vsync events. | 152 * Request to be notified of the closest display vsync events. |
| 146 * Listener.onVSync() will be called soon after the upcoming vsync pulses. | 153 * 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. |
| 147 */ | 155 */ |
| 148 public void requestUpdate() { | 156 public void requestUpdate() { |
| 157 mTriggerNextVSyncCount = MAX_AUTO_ONVSYNC_COUNT; |
| 149 postCallback(); | 158 postCallback(); |
| 150 } | 159 } |
| 151 | 160 |
| 152 /** | 161 /** |
| 153 * Set the best guess of the point in the past when the vsync has happened. | 162 * Set the best guess of the point in the past when the vsync has happened. |
| 154 * @param goodStartingPointNano Known vsync point in the past. | 163 * @param goodStartingPointNano Known vsync point in the past. |
| 155 */ | 164 */ |
| 156 public void setVSyncPointForICS(long goodStartingPointNano) { | 165 public void setVSyncPointForICS(long goodStartingPointNano) { |
| 157 mGoodStartingPointNano = goodStartingPointNano; | 166 mGoodStartingPointNano = goodStartingPointNano; |
| 158 } | 167 } |
| 159 | 168 |
| 160 private long getCurrentNanoTime() { | 169 private long getCurrentNanoTime() { |
| 161 return System.nanoTime(); | 170 return System.nanoTime(); |
| 162 } | 171 } |
| 163 | 172 |
| 164 private void onVSyncCallback(long frameTimeNanos, long currentTimeNanos) { | 173 private void onVSyncCallback(long frameTimeNanos, long currentTimeNanos) { |
| 165 assert mHaveRequestInFlight; | 174 assert mHaveRequestInFlight; |
| 166 mHaveRequestInFlight = false; | 175 mHaveRequestInFlight = false; |
| 167 mLastVSyncCpuTimeNano = currentTimeNanos; | 176 mLastVSyncCpuTimeNano = currentTimeNanos; |
| 177 if (mTriggerNextVSyncCount >= 0) { |
| 178 mTriggerNextVSyncCount--; |
| 179 postCallback(); |
| 180 } |
| 168 if (mListener != null) { | 181 if (mListener != null) { |
| 169 mListener.onVSync(this, frameTimeNanos / NANOSECONDS_PER_MICROSECOND
); | 182 mListener.onVSync(this, frameTimeNanos / NANOSECONDS_PER_MICROSECOND
); |
| 170 } | 183 } |
| 171 } | 184 } |
| 172 | 185 |
| 173 private void postCallback() { | 186 private void postCallback() { |
| 174 if (mHaveRequestInFlight) return; | 187 if (mHaveRequestInFlight) return; |
| 175 mHaveRequestInFlight = true; | 188 mHaveRequestInFlight = true; |
| 176 if (postSyntheticVSync()) return; | 189 if (postSyntheticVSync()) return; |
| 177 if (isVSyncSignalAvailable()) { | 190 if (isVSyncSignalAvailable()) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 206 | 219 |
| 207 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { | 220 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { |
| 208 delay += mRefreshPeriodNano; | 221 delay += mRefreshPeriodNano; |
| 209 } | 222 } |
| 210 | 223 |
| 211 mLastPostedNano = currentTime + delay; | 224 mLastPostedNano = currentTime + delay; |
| 212 if (delay == 0) mHandler.post(mVSyncRunnableCallback); | 225 if (delay == 0) mHandler.post(mVSyncRunnableCallback); |
| 213 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE
R_MILLISECOND); | 226 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE
R_MILLISECOND); |
| 214 } | 227 } |
| 215 } | 228 } |
| OLD | NEW |