Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b080660d authored by Jeff Brown's avatar Jeff Brown
Browse files

Fix a possible starvation issue related to vsync.

This makes a noticeable improvement in cases where applications
post messages that need to be processed between animation frames.

Bug: 6418353
Change-Id: If225742e37aeaf3f0ca9710f9bf43dbb03bcde12
parent d44a1686
Loading
Loading
Loading
Loading
+20 −2
Original line number Diff line number Diff line
@@ -491,14 +491,32 @@ public final class Choreographer {
        }
    }

    private final class FrameDisplayEventReceiver extends DisplayEventReceiver {
    private final class FrameDisplayEventReceiver extends DisplayEventReceiver
            implements Runnable {
        private long mTimestampNanos;
        private int mFrame;

        public FrameDisplayEventReceiver(Looper looper) {
            super(looper);
        }

        @Override
        public void onVsync(long timestampNanos, int frame) {
            doFrame(timestampNanos, frame);
            // 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.
            mTimestampNanos = timestampNanos;
            mFrame = frame;
            Message msg = Message.obtain(mHandler, this);
            msg.setAsynchronous(true);
            mHandler.sendMessageAtTime(msg, timestampNanos / NANOS_PER_MS);
        }

        @Override
        public void run() {
            doFrame(mTimestampNanos, mFrame);
        }
    }