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; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 mVSyncFrameCallback = new Choreographer.FrameCallback() { | 94 mVSyncFrameCallback = new Choreographer.FrameCallback() { |
95 @Override | 95 @Override |
96 public void doFrame(long frameTimeNanos) { | 96 public void doFrame(long frameTimeNanos) { |
97 TraceEvent.begin("VSync"); | 97 TraceEvent.begin("VSync"); |
98 if (useEstimatedRefreshPeriod && mConsecutiveVSync) { | 98 if (useEstimatedRefreshPeriod && mConsecutiveVSync) { |
99 // Display.getRefreshRate() is unreliable on some platfo
rms. | 99 // Display.getRefreshRate() is unreliable on some platfo
rms. |
100 // Adjust refresh period- initial value is based on Disp
lay.getRefreshRate() | 100 // Adjust refresh period- initial value is based on Disp
lay.getRefreshRate() |
101 // after that it asymptotically approaches the real valu
e. | 101 // after that it asymptotically approaches the real valu
e. |
102 long lastRefreshDurationNano = frameTimeNanos - mGoodSta
rtingPointNano; | 102 long lastRefreshDurationNano = frameTimeNanos - mGoodSta
rtingPointNano; |
103 float lastRefreshDurationWeight = 0.1f; | 103 float lastRefreshDurationWeight = 0.1f; |
104 mRefreshPeriodNano += (long) (lastRefreshDurationWeight
* | 104 mRefreshPeriodNano += (long) (lastRefreshDurationWeight |
105 (lastRefreshDurationNano - mRefreshPeriodNano)); | 105 * (lastRefreshDurationNano - mRefreshPeriodNano)
); |
106 } | 106 } |
107 mGoodStartingPointNano = frameTimeNanos; | 107 mGoodStartingPointNano = frameTimeNanos; |
108 onVSyncCallback(frameTimeNanos, getCurrentNanoTime()); | 108 onVSyncCallback(frameTimeNanos, getCurrentNanoTime()); |
109 TraceEvent.end("VSync"); | 109 TraceEvent.end("VSync"); |
110 } | 110 } |
111 }; | 111 }; |
112 mVSyncRunnableCallback = null; | 112 mVSyncRunnableCallback = null; |
113 } else { | 113 } else { |
114 // On ICS we just hope that running tasks is relatively predictable. | 114 // On ICS we just hope that running tasks is relatively predictable. |
115 mChoreographer = null; | 115 mChoreographer = null; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 final long currentTime = getCurrentNanoTime(); | 212 final long currentTime = getCurrentNanoTime(); |
213 // 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 |
214 // vsync is more than half a frame away. | 214 // vsync is more than half a frame away. |
215 if (currentTime - mLastVSyncCpuTimeNano < 2 * mRefreshPeriodNano) return
false; | 215 if (currentTime - mLastVSyncCpuTimeNano < 2 * mRefreshPeriodNano) return
false; |
216 if (currentTime - estimateLastVSyncTime(currentTime) > mRefreshPeriodNan
o / 2) return false; | 216 if (currentTime - estimateLastVSyncTime(currentTime) > mRefreshPeriodNan
o / 2) return false; |
217 mHandler.post(mSyntheticVSyncRunnable); | 217 mHandler.post(mSyntheticVSyncRunnable); |
218 return true; | 218 return true; |
219 } | 219 } |
220 | 220 |
221 private long estimateLastVSyncTime(long currentTime) { | 221 private long estimateLastVSyncTime(long currentTime) { |
222 final long lastRefreshTime = mGoodStartingPointNano + | 222 final long lastRefreshTime = mGoodStartingPointNano |
223 ((currentTime - mGoodStartingPointNano) / mRefreshPeriodNano) *
mRefreshPeriodNano; | 223 + ((currentTime - mGoodStartingPointNano) / mRefreshPeriodNano) |
| 224 * mRefreshPeriodNano; |
224 return lastRefreshTime; | 225 return lastRefreshTime; |
225 } | 226 } |
226 | 227 |
227 private void postRunnableCallback() { | 228 private void postRunnableCallback() { |
228 assert !isVSyncSignalAvailable(); | 229 assert !isVSyncSignalAvailable(); |
229 final long currentTime = getCurrentNanoTime(); | 230 final long currentTime = getCurrentNanoTime(); |
230 final long lastRefreshTime = estimateLastVSyncTime(currentTime); | 231 final long lastRefreshTime = estimateLastVSyncTime(currentTime); |
231 long delay = (lastRefreshTime + mRefreshPeriodNano) - currentTime; | 232 long delay = (lastRefreshTime + mRefreshPeriodNano) - currentTime; |
232 assert delay > 0 && delay <= mRefreshPeriodNano; | 233 assert delay > 0 && delay <= mRefreshPeriodNano; |
233 | 234 |
234 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { | 235 if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) { |
235 delay += mRefreshPeriodNano; | 236 delay += mRefreshPeriodNano; |
236 } | 237 } |
237 | 238 |
238 mLastPostedNano = currentTime + delay; | 239 mLastPostedNano = currentTime + delay; |
239 if (delay == 0) mHandler.post(mVSyncRunnableCallback); | 240 if (delay == 0) mHandler.post(mVSyncRunnableCallback); |
240 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE
R_MILLISECOND); | 241 else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PE
R_MILLISECOND); |
241 } | 242 } |
242 } | 243 } |
OLD | NEW |