//frameworks/base/core/java/android/view/Choreographer.javapublicfinalclassChoreographer{publicvoidpostCallbackDelayed(intcallbackType,Runnableaction,Objecttoken,longdelayMillis){if(action==null){thrownewIllegalArgumentException("action must not be null");}if(callbackType<0||callbackType>CALLBACK_LAST){thrownewIllegalArgumentException("callbackType is invalid");}postCallbackDelayedInternal(callbackType,action,token,delayMillis);}}
//frameworks/base/core/java/android/view/Choreographer.javapublicfinalclassChoreographer{privatevoidscheduleFrameLocked(longnow){if(!mFrameScheduled){mFrameScheduled=true;if(USE_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.//检测当前的Looper线程是不是主线程if(isRunningOnLooperThreadLocked()){//当运行在Looper线程,则立刻调度vsyncscheduleVsyncLocked();}else{//切换到主线程,调度vsyncMessagemsg=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);}}}}
//frameworks/base/core/java/android/view/DisplayEventReceiver.javapublicabstractclassDisplayEventReceiver{publicvoidscheduleVsync(){if(mReceiverPtr==0){Log.w(TAG,"Attempted to schedule a vertical sync pulse but the display event "+"receiver has already been disposed.");}else{nativeScheduleVsync(mReceiverPtr);}}}
//frameworks/base/core/jni/android_view_DisplayEventReceiver.cpp
staticvoidnativeScheduleVsync(JNIEnv*env,jclassclazz,jlongreceiverPtr){sp<NativeDisplayEventReceiver>receiver=reinterpret_cast<NativeDisplayEventReceiver*>(receiverPtr);status_tstatus=receiver->scheduleVsync();//安排垂直同步
if(status){String8message;message.appendFormat("Failed to schedule next vertical sync pulse. status=%d",status);jniThrowRuntimeException(env,message.string());}}
//frameworks/native/libs/gui/DisplayEventDispatcher.cpp
status_tDisplayEventDispatcher::scheduleVsync(){if(!mWaitingForVsync){ALOGV("dispatcher %p ~ Scheduling vsync.",this);// Drain all pending events.
nsecs_tvsyncTimestamp;PhysicalDisplayIdvsyncDisplayId;uint32_tvsyncCount;VsyncEventDatavsyncEventData;if(processPendingEvents(&vsyncTimestamp,&vsyncDisplayId,&vsyncCount,&vsyncEventData)){ALOGE("dispatcher %p ~ last event processed while scheduling was for %"PRId64"",this,ns2ms(static_cast<nsecs_t>(vsyncTimestamp)));}status_tstatus=mReceiver.requestNextVsync();if(status){ALOGW("Failed to request next vsync, status=%d",status);returnstatus;}mWaitingForVsync=true;mLastScheduleVsyncTime=systemTime(SYSTEM_TIME_MONOTONIC);}returnOK;}```调用processPendingEvents方法:```cpp//frameworks/native/libs/gui/DisplayEventDispatcher.cpp
boolDisplayEventDispatcher::processPendingEvents(nsecs_t*outTimestamp,PhysicalDisplayId*outDisplayId,uint32_t*outCount,VsyncEventData*outVsyncEventData){boolgotVsync=false;DisplayEventReceiver::Eventbuf[EVENT_BUFFER_SIZE];ssize_tn;while((n=mReceiver.getEvents(buf,EVENT_BUFFER_SIZE))>0){ALOGV("dispatcher %p ~ Read %d events.",this,int(n));mFrameRateOverrides.reserve(n);for(ssize_ti=0;i<n;i++){constDisplayEventReceiver::Event&ev=buf[i];switch(ev.header.type){caseDisplayEventReceiver::DISPLAY_EVENT_VSYNC:// Later vsync events will just overwrite the info from earlier
// ones. That's fine, we only care about the most recent.
gotVsync=true;*outTimestamp=ev.header.timestamp;*outDisplayId=ev.header.displayId;*outCount=ev.vsync.count;*outVsyncEventData=ev.vsync.vsyncData;break;caseDisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:dispatchHotplug(ev.header.timestamp,ev.header.displayId,ev.hotplug.connected);break;caseDisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE:dispatchModeChanged(ev.header.timestamp,ev.header.displayId,ev.modeChange.modeId,ev.modeChange.vsyncPeriod);break;caseDisplayEventReceiver::DISPLAY_EVENT_NULL:dispatchNullEvent(ev.header.timestamp,ev.header.displayId);break;caseDisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE:mFrameRateOverrides.emplace_back(ev.frameRateOverride);break;caseDisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH:dispatchFrameRateOverrides(ev.header.timestamp,ev.header.displayId,std::move(mFrameRateOverrides));break;default:ALOGW("dispatcher %p ~ ignoring unknown event type %#x",this,ev.header.type);break;}}}if(n<0){ALOGW("Failed to get events from display event dispatcher, status=%d",status_t(n));}returngotVsync;}