Chromium Code Reviews| 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 | 138 * Stop reporting vsync events. Note that at most one pending vsync event ca n still be delivered |
|
Sami
2014/07/07 14:35:06
Does this comment need updating? Do we need this f
egor.starkov
2014/07/07 15:08:59
I left it here because was afraid to ruin some dep
| |
| 145 * after this function is called. | 139 * after this function is called. |
| 146 */ | 140 */ |
| 147 public void stop() { | 141 public void stop() { |
| 148 mTriggerNextVSyncCount = 0; | |
| 149 } | 142 } |
| 150 | 143 |
| 151 /** | 144 /** |
| 152 * Request to be notified of the closest display vsync events. | 145 * Request to be notified of the closest display vsync events. |
| 153 * Listener.onVSync() will be called soon after the upcoming vsync pulses. | 146 * 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 */ | 147 */ |
| 156 public void requestUpdate() { | 148 public void requestUpdate() { |
| 157 mTriggerNextVSyncCount = MAX_AUTO_ONVSYNC_COUNT; | |
| 158 postCallback(); | 149 postCallback(); |
| 159 } | 150 } |
| 160 | 151 |
| 161 /** | 152 /** |
| 162 * Set the best guess of the point in the past when the vsync has happened. | 153 * Set the best guess of the point in the past when the vsync has happened. |
| 163 * @param goodStartingPointNano Known vsync point in the past. | 154 * @param goodStartingPointNano Known vsync point in the past. |
| 164 */ | 155 */ |
| 165 public void setVSyncPointForICS(long goodStartingPointNano) { | 156 public void setVSyncPointForICS(long goodStartingPointNano) { |
| 166 mGoodStartingPointNano = goodStartingPointNano; | 157 mGoodStartingPointNano = goodStartingPointNano; |
| 167 } | 158 } |
| 168 | 159 |
| 169 private long getCurrentNanoTime() { | 160 private long getCurrentNanoTime() { |
| 170 return System.nanoTime(); | 161 return System.nanoTime(); |
| 171 } | 162 } |
| 172 | 163 |
| 173 private void onVSyncCallback(long frameTimeNanos, long currentTimeNanos) { | 164 private void onVSyncCallback(long frameTimeNanos, long currentTimeNanos) { |
| 174 assert mHaveRequestInFlight; | 165 assert mHaveRequestInFlight; |
| 175 mHaveRequestInFlight = false; | 166 mHaveRequestInFlight = false; |
| 176 mLastVSyncCpuTimeNano = currentTimeNanos; | 167 mLastVSyncCpuTimeNano = currentTimeNanos; |
| 177 if (mTriggerNextVSyncCount >= 0) { | |
| 178 mTriggerNextVSyncCount--; | |
| 179 postCallback(); | |
| 180 } | |
| 181 if (mListener != null) { | 168 if (mListener != null) { |
| 182 mListener.onVSync(this, frameTimeNanos / NANOSECONDS_PER_MICROSECOND ); | 169 mListener.onVSync(this, frameTimeNanos / NANOSECONDS_PER_MICROSECOND ); |
| 183 } | 170 } |
| 184 } | 171 } |
| 185 | 172 |
| 186 private void postCallback() { | 173 private void postCallback() { |
| 187 if (mHaveRequestInFlight) return; | 174 if (mHaveRequestInFlight) return; |
| 188 mHaveRequestInFlight = true; | 175 mHaveRequestInFlight = true; |
| 189 if (postSyntheticVSync()) return; | 176 if (postSyntheticVSync()) return; |
| 190 if (isVSyncSignalAvailable()) { | 177 if (isVSyncSignalAvailable()) { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 219 | 206 |
| 220 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { | 207 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { |
| 221 delay += mRefreshPeriodNano; | 208 delay += mRefreshPeriodNano; |
| 222 } | 209 } |
| 223 | 210 |
| 224 mLastPostedNano = currentTime + delay; | 211 mLastPostedNano = currentTime + delay; |
| 225 if (delay == 0) mHandler.post(mVSyncRunnableCallback); | 212 if (delay == 0) mHandler.post(mVSyncRunnableCallback); |
| 226 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE R_MILLISECOND); | 213 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE R_MILLISECOND); |
| 227 } | 214 } |
| 228 } | 215 } |
| OLD | NEW |