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

Commit 21a47e6d authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Adding callback when surface params are applied

Bug: 123874711
Test: Verified that there are no leak logs
Change-Id: I5dcb27c9c44d674363824e703b923fa83e3132b9
parent 7feb1a1f
Loading
Loading
Loading
Loading
+58 −0
Original line number Original line Diff line number Diff line
@@ -19,6 +19,9 @@ package com.android.systemui.shared.system;
import android.graphics.HardwareRenderer;
import android.graphics.HardwareRenderer;
import android.graphics.Matrix;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.Surface;
import android.view.Surface;
import android.view.View;
import android.view.View;
import android.view.ViewRootImpl;
import android.view.ViewRootImpl;
@@ -33,8 +36,15 @@ import java.util.function.Consumer;
 */
 */
public class SyncRtSurfaceTransactionApplierCompat {
public class SyncRtSurfaceTransactionApplierCompat {


    private static final int MSG_UPDATE_SEQUENCE_NUMBER = 0;

    private final Surface mTargetSurface;
    private final Surface mTargetSurface;
    private final ViewRootImpl mTargetViewRootImpl;
    private final ViewRootImpl mTargetViewRootImpl;
    private final Handler mApplyHandler;

    private int mSequenceNumber = 0;
    private int mPendingSequenceNumber = 0;
    private Runnable mAfterApplyCallback;


    /**
    /**
     * @param targetView The view in the surface that acts as synchronization anchor.
     * @param targetView The view in the surface that acts as synchronization anchor.
@@ -42,6 +52,26 @@ public class SyncRtSurfaceTransactionApplierCompat {
    public SyncRtSurfaceTransactionApplierCompat(View targetView) {
    public SyncRtSurfaceTransactionApplierCompat(View targetView) {
        mTargetViewRootImpl = targetView != null ? targetView.getViewRootImpl() : null;
        mTargetViewRootImpl = targetView != null ? targetView.getViewRootImpl() : null;
        mTargetSurface = mTargetViewRootImpl != null ? mTargetViewRootImpl.mSurface : null;
        mTargetSurface = mTargetViewRootImpl != null ? mTargetViewRootImpl.mSurface : null;

        mApplyHandler = new Handler(new Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == MSG_UPDATE_SEQUENCE_NUMBER) {
                    onApplyMessage(msg.arg1);
                    return true;
                }
                return false;
            }
        });
    }

    private void onApplyMessage(int seqNo) {
        mSequenceNumber = seqNo;
        if (mSequenceNumber == mPendingSequenceNumber && mAfterApplyCallback != null) {
            Runnable r = mAfterApplyCallback;
            mAfterApplyCallback = null;
            r.run();
        }
    }
    }


    /**
    /**
@@ -54,10 +84,15 @@ public class SyncRtSurfaceTransactionApplierCompat {
        if (mTargetViewRootImpl == null || mTargetViewRootImpl.getView() == null) {
        if (mTargetViewRootImpl == null || mTargetViewRootImpl.getView() == null) {
            return;
            return;
        }
        }

        mPendingSequenceNumber++;
        final int toApplySeqNo = mPendingSequenceNumber;
        mTargetViewRootImpl.registerRtFrameCallback(new HardwareRenderer.FrameDrawingCallback() {
        mTargetViewRootImpl.registerRtFrameCallback(new HardwareRenderer.FrameDrawingCallback() {
            @Override
            @Override
            public void onFrameDraw(long frame) {
            public void onFrameDraw(long frame) {
                if (mTargetSurface == null || !mTargetSurface.isValid()) {
                if (mTargetSurface == null || !mTargetSurface.isValid()) {
                    Message.obtain(mApplyHandler, MSG_UPDATE_SEQUENCE_NUMBER, toApplySeqNo, 0)
                            .sendToTarget();
                    return;
                    return;
                }
                }
                TransactionCompat t = new TransactionCompat();
                TransactionCompat t = new TransactionCompat();
@@ -70,6 +105,7 @@ public class SyncRtSurfaceTransactionApplierCompat {
                }
                }
                t.setEarlyWakeup();
                t.setEarlyWakeup();
                t.apply();
                t.apply();
                mApplyHandler.sendEmptyMessage(toApplySeqNo);
            }
            }
        });
        });


@@ -77,6 +113,28 @@ public class SyncRtSurfaceTransactionApplierCompat {
        mTargetViewRootImpl.getView().invalidate();
        mTargetViewRootImpl.getView().invalidate();
    }
    }


    /**
     * Calls the runnable when any pending apply calls have completed
     */
    public void addAfterApplyCallback(final Runnable afterApplyCallback) {
        if (mSequenceNumber == mPendingSequenceNumber) {
            afterApplyCallback.run();
        } else {
            if (mAfterApplyCallback == null) {
                mAfterApplyCallback = afterApplyCallback;
            } else {
                final Runnable oldCallback = mAfterApplyCallback;
                mAfterApplyCallback = new Runnable() {
                    @Override
                    public void run() {
                        afterApplyCallback.run();
                        oldCallback.run();
                    }
                };
            }
        }
    }

    public static void applyParams(TransactionCompat t,
    public static void applyParams(TransactionCompat t,
            SyncRtSurfaceTransactionApplierCompat.SurfaceParams params) {
            SyncRtSurfaceTransactionApplierCompat.SurfaceParams params) {
        t.setMatrix(params.surface, params.matrix);
        t.setMatrix(params.surface, params.matrix);