This source file includes following definitions.
- onVSync
- getVSyncPeriodInMicroseconds
- isVSyncSignalAvailable
- stop
- unregisterListener
- requestUpdate
- setVSyncPointForICS
- getCurrentNanoTime
- onVSyncCallback
- postCallback
- postSyntheticVSync
- estimateLastVSyncTime
- postRunnableCallback
package org.chromium.content.browser;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.view.Choreographer;
import android.view.WindowManager;
import org.chromium.base.TraceEvent;
public class VSyncMonitor {
private static final long NANOSECONDS_PER_SECOND = 1000000000;
private static final long NANOSECONDS_PER_MILLISECOND = 1000000;
private static final long NANOSECONDS_PER_MICROSECOND = 1000;
public static final int MAX_AUTO_ONVSYNC_COUNT = 5;
public interface Listener {
public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros);
}
private Listener mListener;
private final long mRefreshPeriodNano;
private boolean mHaveRequestInFlight;
private int mTriggerNextVSyncCount;
private final Choreographer mChoreographer;
private final Choreographer.FrameCallback mVSyncFrameCallback;
private final Runnable mVSyncRunnableCallback;
private long mGoodStartingPointNano;
private long mLastPostedNano;
private final Handler mHandler = new Handler();
private final Runnable mSyntheticVSyncRunnable;
private long mLastVSyncCpuTimeNano;
public VSyncMonitor(Context context, VSyncMonitor.Listener listener) {
this(context, listener, true);
}
VSyncMonitor(Context context, VSyncMonitor.Listener listener, boolean enableJBVSync) {
mListener = listener;
float refreshRate = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getRefreshRate();
if (refreshRate <= 0) refreshRate = 60;
mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate);
mTriggerNextVSyncCount = 0;
if (enableJBVSync && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mChoreographer = Choreographer.getInstance();
mVSyncFrameCallback = new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
TraceEvent.begin("VSync");
mGoodStartingPointNano = frameTimeNanos;
onVSyncCallback(frameTimeNanos, getCurrentNanoTime());
TraceEvent.end("VSync");
}
};
mVSyncRunnableCallback = null;
} else {
mChoreographer = null;
mVSyncFrameCallback = null;
mVSyncRunnableCallback = new Runnable() {
@Override
public void run() {
TraceEvent.begin("VSyncTimer");
final long currentTime = getCurrentNanoTime();
onVSyncCallback(currentTime, currentTime);
TraceEvent.end("VSyncTimer");
}
};
mLastPostedNano = 0;
}
mSyntheticVSyncRunnable = new Runnable() {
@Override
public void run() {
TraceEvent.begin("VSyncSynthetic");
final long currentTime = getCurrentNanoTime();
onVSyncCallback(estimateLastVSyncTime(currentTime), currentTime);
TraceEvent.end("VSyncSynthetic");
}
};
mGoodStartingPointNano = getCurrentNanoTime();
}
public long getVSyncPeriodInMicroseconds() {
return mRefreshPeriodNano / NANOSECONDS_PER_MICROSECOND;
}
public boolean isVSyncSignalAvailable() {
return mChoreographer != null;
}
public void stop() {
mTriggerNextVSyncCount = 0;
}
public void unregisterListener() {
stop();
mListener = null;
}
public void requestUpdate() {
mTriggerNextVSyncCount = MAX_AUTO_ONVSYNC_COUNT;
postCallback();
}
public void setVSyncPointForICS(long goodStartingPointNano) {
mGoodStartingPointNano = goodStartingPointNano;
}
private long getCurrentNanoTime() {
return System.nanoTime();
}
private void onVSyncCallback(long frameTimeNanos, long currentTimeNanos) {
assert mHaveRequestInFlight;
mHaveRequestInFlight = false;
mLastVSyncCpuTimeNano = currentTimeNanos;
if (mTriggerNextVSyncCount >= 0) {
mTriggerNextVSyncCount--;
postCallback();
}
if (mListener != null) {
mListener.onVSync(this, frameTimeNanos / NANOSECONDS_PER_MICROSECOND);
}
}
private void postCallback() {
if (mHaveRequestInFlight) return;
mHaveRequestInFlight = true;
if (postSyntheticVSync()) return;
if (isVSyncSignalAvailable()) {
mChoreographer.postFrameCallback(mVSyncFrameCallback);
} else {
postRunnableCallback();
}
}
private boolean postSyntheticVSync() {
final long currentTime = getCurrentNanoTime();
if (currentTime - mLastVSyncCpuTimeNano < 2 * mRefreshPeriodNano) return false;
if (currentTime - estimateLastVSyncTime(currentTime) > mRefreshPeriodNano / 2) return false;
mHandler.post(mSyntheticVSyncRunnable);
return true;
}
private long estimateLastVSyncTime(long currentTime) {
final long lastRefreshTime = mGoodStartingPointNano +
((currentTime - mGoodStartingPointNano) / mRefreshPeriodNano) * mRefreshPeriodNano;
return lastRefreshTime;
}
private void postRunnableCallback() {
assert !isVSyncSignalAvailable();
final long currentTime = getCurrentNanoTime();
final long lastRefreshTime = estimateLastVSyncTime(currentTime);
long delay = (lastRefreshTime + mRefreshPeriodNano) - currentTime;
assert delay > 0 && delay <= mRefreshPeriodNano;
if (currentTime + delay <= mLastPostedNano + mRefreshPeriodNano / 2) {
delay += mRefreshPeriodNano;
}
mLastPostedNano = currentTime + delay;
if (delay == 0) mHandler.post(mVSyncRunnableCallback);
else mHandler.postDelayed(mVSyncRunnableCallback, delay / NANOSECONDS_PER_MILLISECOND);
}
}