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

Commit 4a902819 authored by Chavi Weingarten's avatar Chavi Weingarten Committed by Android (Google) Code Review
Browse files

Merge "Replace SyncTarget with SurfaceSyncGroup"

parents 4ba0f815 1302b50e
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -140,13 +140,13 @@ public interface AttachedSurfaceControl {
    }

    /**
     * Returns a SyncTarget that can be used to sync {@link AttachedSurfaceControl} in a
     * Returns a SurfaceSyncGroup that can be used to sync {@link AttachedSurfaceControl} in a
     * {@link SurfaceSyncGroup}
     *
     * @hide
     */
    @Nullable
    default SurfaceSyncGroup.SyncTarget getSyncTarget() {
    default SurfaceSyncGroup getOrCreateSurfaceSyncGroup() {
        return null;
    }
}
+8 −8
Original line number Diff line number Diff line
@@ -403,14 +403,6 @@ public class SurfaceControlViewHost {
        return mWm;
    }

    /**
     * @hide
     */
    @TestApi
    public void relayout(WindowManager.LayoutParams attrs) {
        relayout(attrs, SurfaceControl.Transaction::apply);
    }

    /**
     * Forces relayout and draw and allows to set a custom callback when it is finished
     * @hide
@@ -422,6 +414,14 @@ public class SurfaceControlViewHost {
        mWm.setCompletionCallback(mViewRoot.mWindow.asBinder(), callback);
    }

    /**
     * @hide
     */
    @TestApi
    public void relayout(WindowManager.LayoutParams attrs) {
        mViewRoot.setLayoutParams(attrs, false);
    }

    /**
     * Modify the size of the root view.
     *
+23 −24
Original line number Diff line number Diff line
@@ -982,8 +982,8 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall

                final boolean redrawNeeded = sizeChanged || creating || hintChanged
                        || (mVisible && !mDrawFinished) || alphaChanged || relativeZChanged;
                boolean shouldSyncBuffer =
                        redrawNeeded && viewRoot.wasRelayoutRequested() && viewRoot.isInLocalSync();
                boolean shouldSyncBuffer = redrawNeeded && viewRoot.wasRelayoutRequested()
                        && viewRoot.isInWMSRequestedSync();
                SyncBufferTransactionCallback syncBufferTransactionCallback = null;
                if (shouldSyncBuffer) {
                    syncBufferTransactionCallback = new SyncBufferTransactionCallback();
@@ -1073,7 +1073,8 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
    private void handleSyncBufferCallback(SurfaceHolder.Callback[] callbacks,
            SyncBufferTransactionCallback syncBufferTransactionCallback) {

        getViewRootImpl().addToSync((parentSyncGroup, syncBufferCallback) ->
        final SurfaceSyncGroup surfaceSyncGroup = new SurfaceSyncGroup();
        getViewRootImpl().addToSync(surfaceSyncGroup);
        redrawNeededAsync(callbacks, () -> {
            Transaction t = null;
            if (mBlastBufferQueue != null) {
@@ -1081,27 +1082,25 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
                t = syncBufferTransactionCallback.waitForTransaction();
            }

                    syncBufferCallback.onTransactionReady(t);
            surfaceSyncGroup.onTransactionReady(t);
            onDrawFinished();
                }));
        });
    }

    private void handleSyncNoBuffer(SurfaceHolder.Callback[] callbacks) {
        final SurfaceSyncGroup syncGroup = new SurfaceSyncGroup();
        final SurfaceSyncGroup surfaceSyncGroup = new SurfaceSyncGroup();
        synchronized (mSyncGroups) {
            mSyncGroups.add(syncGroup);
            mSyncGroups.add(surfaceSyncGroup);
        }

        syncGroup.addToSync((parentSyncGroup, syncBufferCallback) ->
        redrawNeededAsync(callbacks, () -> {
                    syncBufferCallback.onTransactionReady(null);
                    onDrawFinished();
            synchronized (mSyncGroups) {
                        mSyncGroups.remove(syncGroup);
                mSyncGroups.remove(surfaceSyncGroup);
            }
                }));
            surfaceSyncGroup.onTransactionReady(null);
            onDrawFinished();
        });

        syncGroup.markSyncReady();
    }

    private void redrawNeededAsync(SurfaceHolder.Callback[] callbacks,
@@ -1119,7 +1118,7 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
        if (viewRoot != null) {
            synchronized (mSyncGroups) {
                for (SurfaceSyncGroup syncGroup : mSyncGroups) {
                    viewRoot.mergeSync(syncGroup);
                    viewRoot.addToSync(syncGroup);
                }
            }
        }
+78 −84
Original line number Diff line number Diff line
@@ -595,19 +595,21 @@ public final class ViewRootImpl implements ViewParent,
    String mLastPerformDrawSkippedReason;
    /** The reason the last call to performTraversals() returned without drawing */
    String mLastPerformTraversalsSkipDrawReason;
    /** The state of the local sync, if one is in progress. Can be one of the states below. */
    int mLocalSyncState;
    /** The state of the WMS requested sync, if one is in progress. Can be one of the states
     * below. */
    int mWmsRequestSyncGroupState;

    // The possible states of the local sync, see createSyncIfNeeded()
    private final int LOCAL_SYNC_NONE = 0;
    private final int LOCAL_SYNC_PENDING = 1;
    private final int LOCAL_SYNC_RETURNED = 2;
    private final int LOCAL_SYNC_MERGED = 3;
    // The possible states of the WMS requested sync, see createSyncIfNeeded()
    private static final int WMS_SYNC_NONE = 0;
    private static final int WMS_SYNC_PENDING = 1;
    private static final int WMS_SYNC_RETURNED = 2;
    private static final int WMS_SYNC_MERGED = 3;

    /**
     * Set whether the draw should send the buffer to system server. When set to true, VRI will
     * create a sync transaction with BBQ and send the resulting buffer to system server. If false,
     * VRI will not try to sync a buffer in BBQ, but still report when a draw occurred.
     * Set whether the requested SurfaceSyncGroup should sync the buffer. When set to true, VRI will
     * create a sync transaction with BBQ and send the resulting buffer back to the
     * SurfaceSyncGroup. If false, VRI will not try to sync a buffer in BBQ, but still report when a
     * draw occurred.
     */
    private boolean mSyncBuffer = false;

@@ -849,8 +851,19 @@ public final class ViewRootImpl implements ViewParent,
        return mHandwritingInitiator;
    }

    private SurfaceSyncGroup mSyncGroup;
    private SurfaceSyncGroup.TransactionReadyCallback mTransactionReadyCallback;
    /**
     * A SurfaceSyncGroup that is created when WMS requested to sync the buffer
     */
    private SurfaceSyncGroup mWmsRequestSyncGroup;

    /**
     * The SurfaceSyncGroup that represents the active VRI SurfaceSyncGroup. This is non null if
     * anyone requested the SurfaceSyncGroup for this VRI to ensure that anyone trying to sync with
     * this VRI are collected together. The SurfaceSyncGroup is cleared when the VRI draws since
     * that is the stop point where all changes are have been applied. A new SurfaceSyncGroup is
     * created after that point when something wants to sync VRI again.
     */
    private SurfaceSyncGroup mActiveSurfaceSyncGroup;

    private static final Object sSyncProgressLock = new Object();
    // The count needs to be static since it's used to enable or disable RT animations which is
@@ -3643,6 +3656,12 @@ public final class ViewRootImpl implements ViewParent,
        boolean cancelAndRedraw = cancelDueToPreDrawListener
                 || (cancelDraw && mDrewOnceForSync);
        if (!cancelAndRedraw) {
            // A sync was already requested before the WMS requested sync. This means we need to
            // sync the buffer, regardless if WMS wants to sync the buffer.
            if (mActiveSurfaceSyncGroup != null) {
                mSyncBuffer = true;
            }

            createSyncIfNeeded();
            mDrewOnceForSync = true;
        }
@@ -3656,8 +3675,8 @@ public final class ViewRootImpl implements ViewParent,
                mPendingTransitions.clear();
            }

            if (mTransactionReadyCallback != null) {
                mTransactionReadyCallback.onTransactionReady(null);
            if (mActiveSurfaceSyncGroup != null) {
                mActiveSurfaceSyncGroup.onTransactionReady(null);
            }
        } else if (cancelAndRedraw) {
            mLastPerformTraversalsSkipDrawReason = cancelDueToPreDrawListener
@@ -3672,8 +3691,8 @@ public final class ViewRootImpl implements ViewParent,
                }
                mPendingTransitions.clear();
            }
            if (!performDraw() && mTransactionReadyCallback != null) {
                mTransactionReadyCallback.onTransactionReady(null);
            if (!performDraw() && mActiveSurfaceSyncGroup != null) {
                mActiveSurfaceSyncGroup.onTransactionReady(null);
            }
        }

@@ -3687,39 +3706,40 @@ public final class ViewRootImpl implements ViewParent,
        if (!cancelAndRedraw) {
            mReportNextDraw = false;
            mLastReportNextDrawReason = null;
            mTransactionReadyCallback = null;
            mActiveSurfaceSyncGroup = null;
            mSyncBuffer = false;
            if (isInLocalSync()) {
                mSyncGroup.markSyncReady();
                mSyncGroup = null;
                mLocalSyncState = LOCAL_SYNC_NONE;
            if (isInWMSRequestedSync()) {
                mWmsRequestSyncGroup.onTransactionReady(null);
                mWmsRequestSyncGroup = null;
                mWmsRequestSyncGroupState = WMS_SYNC_NONE;
            }
        }
    }

    private void createSyncIfNeeded() {
        // Started a sync already or there's nothing needing to sync
        if (isInLocalSync() || !mReportNextDraw) {
        // WMS requested sync already started or there's nothing needing to sync
        if (isInWMSRequestedSync() || !mReportNextDraw) {
            return;
        }

        final int seqId = mSyncSeqId;
        mLocalSyncState = LOCAL_SYNC_PENDING;
        mSyncGroup = new SurfaceSyncGroup(transaction -> {
            mLocalSyncState = LOCAL_SYNC_RETURNED;
        mWmsRequestSyncGroupState = WMS_SYNC_PENDING;
        mWmsRequestSyncGroup = new SurfaceSyncGroup(t -> {
            mWmsRequestSyncGroupState = WMS_SYNC_RETURNED;
            // Callback will be invoked on executor thread so post to main thread.
            mHandler.postAtFrontOfQueue(() -> {
                if (transaction != null) {
                    mSurfaceChangedTransaction.merge(transaction);
                if (t != null) {
                    mSurfaceChangedTransaction.merge(t);
                }
                mLocalSyncState = LOCAL_SYNC_MERGED;
                mWmsRequestSyncGroupState = WMS_SYNC_MERGED;
                reportDrawFinished(seqId);
            });
        });
        if (DEBUG_BLAST) {
            Log.d(mTag, "Setup new sync id=" + mSyncGroup);
            Log.d(mTag, "Setup new sync id=" + mWmsRequestSyncGroup);
        }
        mSyncGroup.addToSync(mSyncTarget);

        mWmsRequestSyncGroup.addToSync(this);
        notifySurfaceSyncStarted();
    }

@@ -4370,19 +4390,11 @@ public final class ViewRootImpl implements ViewParent,
        return mAttachInfo.mThreadedRenderer != null && mAttachInfo.mThreadedRenderer.isEnabled();
    }

    void addToSync(SurfaceSyncGroup.SyncTarget syncable) {
        if (!isInLocalSync()) {
            return;
        }
        mSyncGroup.addToSync(syncable);
    }

    /**
     * This VRI is currently in the middle of a sync request, but specifically one initiated from
     * within VRI.
     * This VRI is currently in the middle of a sync request that was initiated by WMS.
     */
    public boolean isInLocalSync() {
        return mSyncGroup != null;
    public boolean isInWMSRequestedSync() {
        return mWmsRequestSyncGroup != null;
    }

    private void addFrameCommitCallbackIfNeeded() {
@@ -4449,7 +4461,7 @@ public final class ViewRootImpl implements ViewParent,
            return false;
        }

        final boolean fullRedrawNeeded = mFullRedrawNeeded || mTransactionReadyCallback != null;
        final boolean fullRedrawNeeded = mFullRedrawNeeded || mActiveSurfaceSyncGroup != null;
        mFullRedrawNeeded = false;

        mIsDrawing = true;
@@ -4457,9 +4469,9 @@ public final class ViewRootImpl implements ViewParent,

        addFrameCommitCallbackIfNeeded();

        boolean usingAsyncReport = isHardwareEnabled() && mTransactionReadyCallback != null;
        boolean usingAsyncReport = isHardwareEnabled() && mActiveSurfaceSyncGroup != null;
        if (usingAsyncReport) {
            registerCallbacksForSync(mSyncBuffer, mTransactionReadyCallback);
            registerCallbacksForSync(mSyncBuffer, mActiveSurfaceSyncGroup);
        } else if (mHasPendingTransactions) {
            // These callbacks are only needed if there's no sync involved and there were calls to
            // applyTransactionOnDraw. These callbacks check if the draw failed for any reason and
@@ -4510,11 +4522,10 @@ public final class ViewRootImpl implements ViewParent,
            }

            if (mSurfaceHolder != null && mSurface.isValid()) {
                final SurfaceSyncGroup.TransactionReadyCallback transactionReadyCallback =
                        mTransactionReadyCallback;
                final SurfaceSyncGroup surfaceSyncGroup = mActiveSurfaceSyncGroup;
                SurfaceCallbackHelper sch = new SurfaceCallbackHelper(() ->
                        mHandler.post(() -> transactionReadyCallback.onTransactionReady(null)));
                mTransactionReadyCallback = null;
                        mHandler.post(() -> surfaceSyncGroup.onTransactionReady(null)));
                mActiveSurfaceSyncGroup = null;

                SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();

@@ -4525,8 +4536,8 @@ public final class ViewRootImpl implements ViewParent,
                }
            }
        }
        if (mTransactionReadyCallback != null && !usingAsyncReport) {
            mTransactionReadyCallback.onTransactionReady(null);
        if (mActiveSurfaceSyncGroup != null && !usingAsyncReport) {
            mActiveSurfaceSyncGroup.onTransactionReady(null);
        }
        if (mPerformContentCapture) {
            performContentCaptureInitialReport();
@@ -8616,8 +8627,8 @@ public final class ViewRootImpl implements ViewParent,
            writer.println(innerPrefix + "mLastPerformDrawFailedReason="
                + mLastPerformDrawSkippedReason);
        }
        if (mLocalSyncState != LOCAL_SYNC_NONE) {
            writer.println(innerPrefix + "mLocalSyncState=" + mLocalSyncState);
        if (mWmsRequestSyncGroupState != WMS_SYNC_NONE) {
            writer.println(innerPrefix + "mWmsRequestSyncGroupState=" + mWmsRequestSyncGroupState);
        }
        writer.println(innerPrefix + "mLastReportedMergedConfiguration="
                + mLastReportedMergedConfiguration);
@@ -11205,7 +11216,7 @@ public final class ViewRootImpl implements ViewParent,
    }

    private void registerCallbacksForSync(boolean syncBuffer,
            final SurfaceSyncGroup.TransactionReadyCallback transactionReadyCallback) {
            final SurfaceSyncGroup surfaceSyncGroup) {
        if (!isHardwareEnabled()) {
            return;
        }
@@ -11232,7 +11243,7 @@ public final class ViewRootImpl implements ViewParent,
                // pendingDrawFinished.
                if ((syncResult
                        & (SYNC_LOST_SURFACE_REWARD_IF_FOUND | SYNC_CONTEXT_IS_STOPPED)) != 0) {
                    transactionReadyCallback.onTransactionReady(
                    surfaceSyncGroup.onTransactionReady(
                            mBlastBufferQueue.gatherPendingTransactions(frame));
                    return null;
                }
@@ -11243,7 +11254,7 @@ public final class ViewRootImpl implements ViewParent,

                if (syncBuffer) {
                    mBlastBufferQueue.syncNextTransaction(
                            transactionReadyCallback::onTransactionReady);
                            surfaceSyncGroup::onTransactionReady);
                }

                return didProduceBuffer -> {
@@ -11263,7 +11274,7 @@ public final class ViewRootImpl implements ViewParent,
                        // since the frame didn't draw on this vsync. It's possible the frame will
                        // draw later, but it's better to not be sync than to block on a frame that
                        // may never come.
                        transactionReadyCallback.onTransactionReady(
                        surfaceSyncGroup.onTransactionReady(
                                mBlastBufferQueue.gatherPendingTransactions(frame));
                        return;
                    }
@@ -11272,35 +11283,23 @@ public final class ViewRootImpl implements ViewParent,
                    // syncNextTransaction callback. Instead, just report back to the Syncer so it
                    // knows that this sync request is complete.
                    if (!syncBuffer) {
                        transactionReadyCallback.onTransactionReady(null);
                        surfaceSyncGroup.onTransactionReady(null);
                    }
                };
            }
        });
    }

    public final SurfaceSyncGroup.SyncTarget mSyncTarget = new SurfaceSyncGroup.SyncTarget() {
    @Override
        public void onAddedToSyncGroup(SurfaceSyncGroup parentSyncGroup,
                SurfaceSyncGroup.TransactionReadyCallback transactionReadyCallback) {
            updateSyncInProgressCount(parentSyncGroup);
            if (!isInLocalSync()) {
                // Always sync the buffer if the sync request did not come from VRI.
                mSyncBuffer = true;
            }

            if (mTransactionReadyCallback != null) {
                Log.d(mTag, "Already set sync for the next draw.");
                mTransactionReadyCallback.onTransactionReady(null);
            }
            if (DEBUG_BLAST) {
                Log.d(mTag, "Setting syncFrameCallback");
            }
            mTransactionReadyCallback = transactionReadyCallback;
    public SurfaceSyncGroup getOrCreateSurfaceSyncGroup() {
        if (mActiveSurfaceSyncGroup == null) {
            mActiveSurfaceSyncGroup = new SurfaceSyncGroup();
            updateSyncInProgressCount(mActiveSurfaceSyncGroup);
            if (!mIsInTraversal && !mTraversalScheduled) {
                scheduleTraversals();
            }
        }
        return mActiveSurfaceSyncGroup;
    };

    private final Executor mSimpleExecutor = Runnable::run;
@@ -11325,15 +11324,10 @@ public final class ViewRootImpl implements ViewParent,
        });
    }

    @Override
    public SurfaceSyncGroup.SyncTarget getSyncTarget() {
        return mSyncTarget;
    }

    void mergeSync(SurfaceSyncGroup otherSyncGroup) {
        if (!isInLocalSync()) {
    void addToSync(SurfaceSyncGroup syncable) {
        if (mActiveSurfaceSyncGroup == null) {
            return;
        }
        mSyncGroup.merge(otherSyncGroup);
        mActiveSurfaceSyncGroup.addToSync(syncable, false /* parentSyncGroupMerge */);
    }
}
+126 −148

File changed.

Preview size limit exceeded, changes collapsed.

Loading