/** * Posts a callback to run on the next frame. * <p> * The callback runs once then is automatically removed. * </p> * * @param callbackType The callback type. * @param action The callback action to run during the next frame. * @param token The callback token, or null if none. * * @see #removeCallbacks * @hide */ publicvoidpostCallback(int callbackType, Runnable action, Object token) { postCallbackDelayed(callbackType, action, token, 0); }
publicvoidpostCallbackDelayed(int callbackType, Runnable action, Object token, long delayMillis) { ......
privatevoidscheduleFrameLocked(long now) { if (!mFrameScheduled) { mFrameScheduled = true; if (USE_VSYNC) { // 如果使用vsync if (DEBUG_FRAMES) { Log.d(TAG, "Scheduling next frame on 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 { Messagemsg= mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC); msg.setAsynchronous(true); mHandler.sendMessageAtFrontOfQueue(msg); } } else { // 不使用VSYNC,那么用自定义的定时操作触发,可用于调试 finallongnextFrameTime= Math.max( mLastFrameTimeNanos / TimeUtils.NANOS_PER_MS + sFrameDelay, now); if (DEBUG_FRAMES) { Log.d(TAG, "Scheduling next frame in " + (nextFrameTime - now) + " ms."); } Messagemsg= mHandler.obtainMessage(MSG_DO_FRAME); msg.setAsynchronous(true); mHandler.sendMessageAtTime(msg, nextFrameTime); } } }
/** * Schedules a single vertical sync pulse to be delivered when the next * display frame begins. */ publicvoidscheduleVsync() { ...... nativeScheduleVsync(mReceiverPtr); }
// Called from native code. @SuppressWarnings("unused") privatevoiddispatchVsync(long timestampNanos, int builtInDisplayId, int frame) { onVsync(timestampNanos, builtInDisplayId, frame); }
publicFrameDisplayEventReceiver(Looper looper, int vsyncSource) { super(looper, vsyncSource); }
@Override publicvoidonVsync(long timestampNanos, int builtInDisplayId, int frame) { // vsync信号发生时回调 ......
// Post the vsync event to the Handler. // The idea is to prevent incoming vsync events from completely starving // the message queue. If there are no messages in the queue with timestamps // earlier than the frame time, then the vsync event will be processed immediately. // Otherwise, messages that predate the vsync event will be handled first. // 使用Handler来保证vsync信号依次按序执行,防止因为vsync信号,导致整个队列饿死, // 两个问题:1. 为啥要用Handler?而不是直接调用执行 // 2. prevent这句是什么意思? ...... Messagemsg= Message.obtain(mHandler, this); msg.setAsynchronous(true); mHandler.sendMessageAtTime(msg, timestampNanos / TimeUtils.NANOS_PER_MS); }