break;
case MSG_DO_SCHEDULE_CALLBACK:
//postCallbackDelayedInternal()方法中当未到期的时候发送过来的
doScheduleCallback(msg.arg1);
break;
}
}
}
以上代码我们可以看出这个,FramHandler拿到 whate属性值为MSG_DO_SCHEDULE_CALLBACK的时候会去执行 doScheduleCallback(msg.arg1);方法,跟进去看下
1.3 Choreography#doScheduleCallback
void doScheduleCallback(int callbackType) {
synchronized (mLock) {
if (!mFrameScheduled) {
final long now = SystemClock.uptimeMillis();
if (mCallbackQueues[callbackType].hasDueCallbacksLocked(now)) {
scheduleFrameLocked(now);
}
}
}
}
这个方法中先是做了一些判断,mFrameSceduled为false 并且hasDueCallbacksLocked()这个方法的返回值为true,看方法名就能猜出这个callback是否到期了,下面我们再分析这个。最终如果满足条件的情况下它会调用 scheduleFrameLocked()这个方法,咦这个方法眼熟不?对,没错,postCallbackDelayedInternal()方法中如果到期了的话就直接执行的那个方法。是时候看这个方法里面搞的什么事情了。
1.4 scheduleFrameLocked()
private void scheduleFrameLocked(long now) {
if (!mFrameScheduled) {
mFrameScheduled = true;//设置标记位,表示已经安排请求下一帧渲染了。
if (USE_VSYNC) {
// If running on the Looper thread, then schedule the vsync immediately,
// otherwise post a message to schedule the vsync from the UI thread
// as soon as possible.
/**
翻译一下,如果在主线程中,就直接调用立即安排垂直同步,否则也就是非主线程的化就发送一个消息在主线程尽快安排一个垂直同步
*/
if (isRunningOnLooperThreadLocked()) {
scheduleVsyncLocked();
} else {
Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC);
msg.setAsynchronous(true);
mHandler.sendMessageAtFrontOfQueue(msg);
}
} else {
final long nextFrameTime = Math.max(
mLastFrameTimeNanos / TimeUtils.NANOS_PER_MS + sFrameDelay, now);
if (DEBUG_FRAMES) {
Log.d(TAG, “Scheduling next frame in " + (nextFrameTime - no