How does Choreographer work

700 views Asked by At

I was just studying about code of Android framework (v4.1).

I know that the UI message triggered by invalidate() now is posted to Choreographer instead of UI messageQueue directly, and these messages will not be executed until next VSYNC signal comes.

Before this post operation is performed, ViewRootImpl calls Looper's postSyncBarrier() once in order to block the UI MessageQueue which means messages that post into messagesQueue later won't be executed until this block is removed, which happens before function performTraversals().

Based on the above understanding,

If another invalidate() is called even once somewhere within performTraversals() (such as onDraw() of any view instances) the UI messageQueue forever be blocked?

From practical observations, I know that this never happens.
So, where am i wrong?

1

There are 1 answers

0
G. Blake Meike On

I think you can get the answer to this question by looking at the code for scheduleTraversals:

void scheduleTraversals() {
    if (!mTraversalScheduled) {
        mTraversalScheduled = true;
        mTraversalBarrier = mHandler.getLooper().postSyncBarrier();
        mChoreographer.postCallback(
                Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
        if (!mUnbufferedInputDispatch) {
            scheduleConsumeBatchedInput();
        }
        notifyRendererOfFramePending();
    }
}

After the first call to scheduleTraversals mTraversalScheduled is set true. Subsequent calls have no affect, postSyncBarrier is not be called and no second barrier is put on the queue, until unscheduleTraversals is called.