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

Commit 1d9a0b97 authored by Kean Mariotti's avatar Kean Mariotti Committed by Android (Google) Code Review
Browse files

Merge "viewcapture: guarantee happens-before relationship" into main

parents 1033074c 762ac978
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -311,8 +311,12 @@ public abstract class ViewCapture {
            captureViewTree(mRoot, mViewPropertyRef);
            ViewPropertyRef captured = mViewPropertyRef.next;
            if (captured != null) {
                captured.callback = mCaptureCallback;
                captured.elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos();

                // Main thread writes volatile field:
                // guarantee that variable changes prior the field write are visible to bg thread
                captured.volatileCallback = mCaptureCallback;

                mBgExecutor.execute(captured);
            }
            mIsFirstFrame = false;
@@ -552,9 +556,11 @@ public abstract class ViewCapture {

        public ViewPropertyRef next;

        public Consumer<ViewPropertyRef> callback = null;
        public long elapsedRealtimeNanos = 0;

        // Volatile field to establish happens-before relationship between main and bg threads
        // (see JSR-133: Java Memory Model and Thread Specification)
        public volatile Consumer<ViewPropertyRef> volatileCallback = null;

        public void transferFrom(View in) {
            view = in;
@@ -651,8 +657,10 @@ public abstract class ViewCapture {

        @Override
        public void run() {
            Consumer<ViewPropertyRef> oldCallback = callback;
            callback = null;
            // Bg thread reads volatile field:
            // guarantee that variable changes in main thread prior the field write are visible
            Consumer<ViewPropertyRef> oldCallback = volatileCallback;
            volatileCallback = null;
            if (oldCallback != null) {
                oldCallback.accept(this);
            }