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

Commit f45cfa23 authored by Robert Carr's avatar Robert Carr
Browse files

SurfaceView Cleanup (1/n): Breakup updateSurface()

This has become a mega function, the number of variables
and levels of indentation is increasingly difficult to read.
This is a simple abstraction of one large chunk.

Test: Existing tests pass
Change-Id: If20185ce640802173ccd53c84b1947b011f379ad
parent 4a11c440
Loading
Loading
Loading
Loading
+103 −101
Original line number Original line Diff line number Diff line
@@ -927,98 +927,12 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
        }
        }
    }
    }


    /** @hide */
    private boolean performSurfaceTransaction(ViewRootImpl viewRoot, Translator translator,
    protected void updateSurface() {
        boolean creating, boolean sizeChanged, boolean needBLASTSync) {
        if (!mHaveFrame) {
            if (DEBUG) {
                Log.d(TAG, System.identityHashCode(this) + " updateSurface: has no frame");
            }
            return;
        }
        final ViewRootImpl viewRoot = getViewRootImpl();

        if (viewRoot == null) {
            return;
        }

        if (viewRoot.mSurface == null || !viewRoot.mSurface.isValid()) {
            notifySurfaceDestroyed();
            tryReleaseSurfaces();
            return;
        }

        final Translator translator = viewRoot.mTranslator;
        if (translator != null) {
            mSurface.setCompatibilityTranslator(translator);
        }

        int myWidth = mRequestedWidth;
        if (myWidth <= 0) myWidth = getWidth();
        int myHeight = mRequestedHeight;
        if (myHeight <= 0) myHeight = getHeight();

        final float alpha = getFixedAlpha();
        final boolean formatChanged = mFormat != mRequestedFormat;
        final boolean visibleChanged = mVisible != mRequestedVisible;
        final boolean alphaChanged = mSurfaceAlpha != alpha;
        final boolean creating = (mSurfaceControl == null || formatChanged || visibleChanged)
                && mRequestedVisible;
        final boolean sizeChanged = mSurfaceWidth != myWidth || mSurfaceHeight != myHeight;
        final boolean windowVisibleChanged = mWindowVisibility != mLastWindowVisibility;
        boolean redrawNeeded = false;
        getLocationInSurface(mLocation);
        final boolean positionChanged = mWindowSpaceLeft != mLocation[0]
            || mWindowSpaceTop != mLocation[1];
        final boolean layoutSizeChanged = getWidth() != mScreenRect.width()
            || getHeight() != mScreenRect.height();


        if (creating || formatChanged || sizeChanged || visibleChanged ||
                (mUseAlpha && alphaChanged) || windowVisibleChanged ||
                positionChanged || layoutSizeChanged) {
            getLocationInWindow(mLocation);

            if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
                    + "Changes: creating=" + creating
                    + " format=" + formatChanged + " size=" + sizeChanged
                    + " visible=" + visibleChanged + " alpha=" + alphaChanged
                    + " mUseAlpha=" + mUseAlpha
                    + " visible=" + visibleChanged
                    + " left=" + (mWindowSpaceLeft != mLocation[0])
                    + " top=" + (mWindowSpaceTop != mLocation[1]));

            try {
                final boolean visible = mVisible = mRequestedVisible;
                mWindowSpaceLeft = mLocation[0];
                mWindowSpaceTop = mLocation[1];
                mSurfaceWidth = myWidth;
                mSurfaceHeight = myHeight;
                mFormat = mRequestedFormat;
                mLastWindowVisibility = mWindowVisibility;

                mScreenRect.left = mWindowSpaceLeft;
                mScreenRect.top = mWindowSpaceTop;
                mScreenRect.right = mWindowSpaceLeft + getWidth();
                mScreenRect.bottom = mWindowSpaceTop + getHeight();
                if (translator != null) {
                    translator.translateRectInAppWindowToScreen(mScreenRect);
                }

                final Rect surfaceInsets = viewRoot.mWindowAttributes.surfaceInsets;
                mScreenRect.offset(surfaceInsets.left, surfaceInsets.top);

                if (creating) {
                    updateOpaqueFlag();
                    mDeferredDestroySurfaceControl = createSurfaceControls(viewRoot);
                } else if (mSurfaceControl == null) {
                    return;
                }

        boolean realSizeChanged = false;
        boolean realSizeChanged = false;

        mSurfaceLock.lock();
        mSurfaceLock.lock();
        try {
        try {
                    mDrawingStopped = !visible;
            mDrawingStopped = !mVisible;


            if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
            if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
                    + "Cur surface: " + mSurface);
                    + "Cur surface: " + mSurface);
@@ -1047,6 +961,7 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
            updateBackgroundVisibility(mTmpTransaction);
            updateBackgroundVisibility(mTmpTransaction);
            updateBackgroundColor(mTmpTransaction);
            updateBackgroundColor(mTmpTransaction);
            if (mUseAlpha) {
            if (mUseAlpha) {
                float alpha = getFixedAlpha();
                mTmpTransaction.setAlpha(mSurfaceControl, alpha);
                mTmpTransaction.setAlpha(mSurfaceControl, alpha);
                mSurfaceAlpha = alpha;
                mSurfaceAlpha = alpha;
            }
            }
@@ -1076,8 +991,7 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
                    mTmpTransaction.setWindowCrop(mSurfaceControl, mSurfaceWidth,
                    mTmpTransaction.setWindowCrop(mSurfaceControl, mSurfaceWidth,
                            mSurfaceHeight);
                            mSurfaceHeight);
                }
                }
                    } else if ((layoutSizeChanged || positionChanged || visibleChanged) &&
            } else if (needBLASTSync) {
                            viewRoot.useBLAST()) {
                viewRoot.setUseBLASTSyncTransaction();
                viewRoot.setUseBLASTSyncTransaction();
            }
            }
            mTmpTransaction.setCornerRadius(mSurfaceControl, mCornerRadius);
            mTmpTransaction.setCornerRadius(mSurfaceControl, mCornerRadius);
@@ -1087,11 +1001,6 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall


            mTmpTransaction.apply();
            mTmpTransaction.apply();
            updateScreenMatrixForEmbeddedHierarchy();
            updateScreenMatrixForEmbeddedHierarchy();

                    if (sizeChanged || creating) {
                        redrawNeeded = true;
                    }

            mSurfaceFrame.left = 0;
            mSurfaceFrame.left = 0;
            mSurfaceFrame.top = 0;
            mSurfaceFrame.top = 0;
            if (translator == null) {
            if (translator == null) {
@@ -1102,7 +1011,6 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
                mSurfaceFrame.right = (int) (mSurfaceWidth * appInvertedScale + 0.5f);
                mSurfaceFrame.right = (int) (mSurfaceWidth * appInvertedScale + 0.5f);
                mSurfaceFrame.bottom = (int) (mSurfaceHeight * appInvertedScale + 0.5f);
                mSurfaceFrame.bottom = (int) (mSurfaceHeight * appInvertedScale + 0.5f);
            }
            }

            final int surfaceWidth = mSurfaceFrame.right;
            final int surfaceWidth = mSurfaceFrame.right;
            final int surfaceHeight = mSurfaceFrame.bottom;
            final int surfaceHeight = mSurfaceFrame.bottom;
            realSizeChanged = mLastSurfaceWidth != surfaceWidth
            realSizeChanged = mLastSurfaceWidth != surfaceWidth
@@ -1112,10 +1020,104 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
        } finally {
        } finally {
            mSurfaceLock.unlock();
            mSurfaceLock.unlock();
        }
        }
        return realSizeChanged;
    }

    /** @hide */
    protected void updateSurface() {
        if (!mHaveFrame) {
            if (DEBUG) {
                Log.d(TAG, System.identityHashCode(this) + " updateSurface: has no frame");
            }
            return;
        }
        final ViewRootImpl viewRoot = getViewRootImpl();

        if (viewRoot == null) {
            return;
        }

        if (viewRoot.mSurface == null || !viewRoot.mSurface.isValid()) {
            notifySurfaceDestroyed();
            tryReleaseSurfaces();
            return;
        }

        final Translator translator = viewRoot.mTranslator;
        if (translator != null) {
            mSurface.setCompatibilityTranslator(translator);
        }

        int myWidth = mRequestedWidth;
        if (myWidth <= 0) myWidth = getWidth();
        int myHeight = mRequestedHeight;
        if (myHeight <= 0) myHeight = getHeight();

        final float alpha = getFixedAlpha();
        final boolean formatChanged = mFormat != mRequestedFormat;
        final boolean visibleChanged = mVisible != mRequestedVisible;
        final boolean alphaChanged = mSurfaceAlpha != alpha;
        final boolean creating = (mSurfaceControl == null || formatChanged || visibleChanged)
                && mRequestedVisible;
        final boolean sizeChanged = mSurfaceWidth != myWidth || mSurfaceHeight != myHeight;
        final boolean windowVisibleChanged = mWindowVisibility != mLastWindowVisibility;
        getLocationInSurface(mLocation);
        final boolean positionChanged = mWindowSpaceLeft != mLocation[0]
            || mWindowSpaceTop != mLocation[1];
        final boolean layoutSizeChanged = getWidth() != mScreenRect.width()
            || getHeight() != mScreenRect.height();


        if (creating || formatChanged || sizeChanged || visibleChanged ||
                (mUseAlpha && alphaChanged) || windowVisibleChanged ||
                positionChanged || layoutSizeChanged) {
            getLocationInWindow(mLocation);

            if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
                    + "Changes: creating=" + creating
                    + " format=" + formatChanged + " size=" + sizeChanged
                    + " visible=" + visibleChanged + " alpha=" + alphaChanged
                    + " mUseAlpha=" + mUseAlpha
                    + " visible=" + visibleChanged
                    + " left=" + (mWindowSpaceLeft != mLocation[0])
                    + " top=" + (mWindowSpaceTop != mLocation[1]));


            try {
            try {
                    redrawNeeded |= visible && !mDrawFinished;
                final boolean visible = mVisible = mRequestedVisible;
                mWindowSpaceLeft = mLocation[0];
                mWindowSpaceTop = mLocation[1];
                mSurfaceWidth = myWidth;
                mSurfaceHeight = myHeight;
                mFormat = mRequestedFormat;
                mLastWindowVisibility = mWindowVisibility;

                mScreenRect.left = mWindowSpaceLeft;
                mScreenRect.top = mWindowSpaceTop;
                mScreenRect.right = mWindowSpaceLeft + getWidth();
                mScreenRect.bottom = mWindowSpaceTop + getHeight();
                if (translator != null) {
                    translator.translateRectInAppWindowToScreen(mScreenRect);
                }

                final Rect surfaceInsets = viewRoot.mWindowAttributes.surfaceInsets;
                mScreenRect.offset(surfaceInsets.left, surfaceInsets.top);


                if (creating) {
                    updateOpaqueFlag();
                    mDeferredDestroySurfaceControl = createSurfaceControls(viewRoot);
                } else if (mSurfaceControl == null) {
                    return;
                }

                final boolean needBLASTSync =
                    (layoutSizeChanged || positionChanged || visibleChanged) &&
                        viewRoot.useBLAST();
                final boolean realSizeChanged = performSurfaceTransaction(viewRoot,
                    translator, creating, sizeChanged, needBLASTSync);
                final boolean redrawNeeded = sizeChanged || creating ||
                    (visible && !mDrawFinished);

                try {
                    SurfaceHolder.Callback[] callbacks = null;
                    SurfaceHolder.Callback[] callbacks = null;


                    final boolean surfaceChanged = creating;
                    final boolean surfaceChanged = creating;