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

Commit ed890613 authored by Marzia Favaro's avatar Marzia Favaro Committed by Android (Google) Code Review
Browse files

Merge "Ensure dim layer not going away when changing activity" into main

parents 8a33128f e88db8a6
Loading
Loading
Loading
Loading
+26 −21
Original line number Diff line number Diff line
@@ -28,6 +28,9 @@ import com.android.window.flags.Flags;
 * black layers of varying opacity at various Z-levels which create the effect of a Dim.
 */
public abstract class Dimmer {

    static final boolean DIMMER_REFACTOR = Flags.dimmerRefactor();

    /**
     * The {@link WindowContainer} that our Dims are bounded to. We may be dimming on behalf of the
     * host, some controller of it, or one of the hosts children.
@@ -40,7 +43,7 @@ public abstract class Dimmer {

    // Constructs the correct type of dimmer
    static Dimmer create(WindowContainer host) {
        return Flags.dimmerRefactor() ? new SmoothDimmer(host) : new LegacyDimmer(host);
        return DIMMER_REFACTOR ? new SmoothDimmer(host) : new LegacyDimmer(host);
    }

    @NonNull
@@ -48,32 +51,34 @@ public abstract class Dimmer {
        return mHost;
    }

    protected abstract void dim(
            WindowContainer container, int relativeLayer, float alpha, int blurRadius);

    /**
     * Place a dim above the given container, which should be a child of the host container.
     * for each call to {@link WindowContainer#prepareSurfaces} the Dim state will be reset
     * and the child should call dimAbove again to request the Dim to continue.
     * Position the dim relatively to the dimming container.
     * Normally called together with #setAppearance, it can be called alone to keep the dim parented
     * to a visible container until the next dimming container is ready.
     * If multiple containers call this method, only the changes relative to the topmost will be
     * applied.
     *
     * @param container The container which to dim above. Should be a child of our host.
     * @param alpha     The alpha at which to Dim.
     * For each call to {@link WindowContainer#prepareSurfaces()} the DimState will be reset, and
     * the child of the host should call adjustRelativeLayer and {@link Dimmer#adjustAppearance} to
     * continue dimming. Indeed, this method won't be able to keep dimming or get a new DimState
     * without also adjusting the appearance.
     * @param container      The container which to dim above. Should be a child of the host.
     * @param relativeLayer  The position of the dim wrt the container
     */
    void dimAbove(@NonNull WindowContainer container, float alpha) {
        dim(container, 1, alpha, 0);
    }
    protected abstract void adjustRelativeLayer(WindowContainer container, int relativeLayer);

    /**
     * Like {@link #dimAbove} but places the dim below the given container.
     *
     * @param container  The container which to dim below. Should be a child of our host.
     * @param alpha      The alpha at which to Dim.
     * @param blurRadius The amount of blur added to the Dim.
     * Set the aspect of the dim layer, and request to keep dimming.
     * For each call to {@link WindowContainer#prepareSurfaces} the Dim state will be reset, and the
     * child should call setAppearance again to request the Dim to continue.
     * If multiple containers call this method, only the changes relative to the topmost will be
     * applied.
     * @param container  Container requesting the dim
     * @param alpha      Dim amount
     * @param blurRadius Blur amount
     */

    void dimBelow(@NonNull WindowContainer container, float alpha, int blurRadius) {
        dim(container, -1, alpha, blurRadius);
    }
    protected abstract void adjustAppearance(
            WindowContainer container, float alpha, int blurRadius);

    /**
     * Mark all dims as pending completion on the next call to {@link #updateDims}
+13 −6
Original line number Diff line number Diff line
@@ -134,8 +134,9 @@ public class LegacyDimmer extends Dimmer {
        boolean mAnimateExit = true;

        /**
         * Used for Dims not associated with a WindowContainer. See {@link Dimmer#dimAbove} for
         * details on Dim lifecycle.
         * Used for Dims not associated with a WindowContainer.
         * See {@link Dimmer#adjustRelativeLayer(WindowContainer, int)} for details on Dim
         * lifecycle.
         */
        boolean mDontReset;
        SurfaceAnimator mSurfaceAnimator;
@@ -218,9 +219,8 @@ public class LegacyDimmer extends Dimmer {
    }

    @Override
    protected void dim(WindowContainer container, int relativeLayer, float alpha, int blurRadius) {
    protected void adjustAppearance(WindowContainer container, float alpha, int blurRadius) {
        final DimState d = obtainDimState(container);

        if (d == null) {
            return;
        }
@@ -229,13 +229,20 @@ public class LegacyDimmer extends Dimmer {
        // in the correct Z from lowest Z to highest. This ensures that the dim layer is always
        // relative to the highest Z layer with a dim.
        SurfaceControl.Transaction t = mHost.getPendingTransaction();
        t.setRelativeLayer(d.mDimLayer, container.getSurfaceControl(), relativeLayer);
        t.setAlpha(d.mDimLayer, alpha);
        t.setBackgroundBlurRadius(d.mDimLayer, blurRadius);

        d.mDimming = true;
    }

    @Override
    protected void adjustRelativeLayer(WindowContainer container, int relativeLayer) {
        final DimState d = mDimState;
        if (d != null) {
            SurfaceControl.Transaction t = mHost.getPendingTransaction();
            t.setRelativeLayer(d.mDimLayer, container.getSurfaceControl(), relativeLayer);
        }
    }

    @Override
    boolean updateDims(SurfaceControl.Transaction t) {
        if (mDimState == null) {
+29 −11
Original line number Diff line number Diff line
@@ -63,8 +63,9 @@ class SmoothDimmer extends Dimmer {
        boolean mAnimateExit = true;

        /**
         * Used for Dims not associated with a WindowContainer. See {@link Dimmer#dimAbove} for
         * details on Dim lifecycle.
         * Used for Dims not associated with a WindowContainer.
         * See {@link Dimmer#adjustRelativeLayer(WindowContainer, int)} for details on Dim
         * lifecycle.
         */
        boolean mDontReset;

@@ -105,22 +106,34 @@ class SmoothDimmer extends Dimmer {
        }

        void setExitParameters(WindowContainer container) {
            setRequestedParameters(container, -1, 0, 0);
            setRequestedRelativeParent(container, -1 /* relativeLayer */);
            setRequestedAppearance(0f /* alpha */, 0 /* blur */);
        }

        // Sets a requested change without applying it immediately
        void setRequestedParameters(WindowContainer container, int relativeLayer, float alpha,
                int blurRadius) {
            mRequestedProperties.mDimmingContainer = container;
        void setRequestedRelativeParent(WindowContainer relativeParent, int relativeLayer) {
            mRequestedProperties.mDimmingContainer = relativeParent;
            mRequestedProperties.mRelativeLayer = relativeLayer;
        }

        // Sets a requested change without applying it immediately
        void setRequestedAppearance(float alpha, int blurRadius) {
            mRequestedProperties.mAlpha = alpha;
            mRequestedProperties.mBlurRadius = blurRadius;
        }

        /**
         * Commit the last changes we received. Called after
         * {@link Change#setRequestedParameters(WindowContainer, int, float, int)}
         * {@link Change#setExitParameters(WindowContainer)},
         * {@link Change#setRequestedRelativeParent(WindowContainer, int)}, or
         * {@link Change#setRequestedAppearance(float, int)}
         */
        void applyChanges(SurfaceControl.Transaction t) {
            if (mRequestedProperties.mDimmingContainer == null) {
                Log.e(TAG, this + " does not have a dimming container. Have you forgotten to "
                        + "call adjustRelativeLayer?");
                return;
            }
            if (mRequestedProperties.mDimmingContainer.mSurfaceControl == null) {
                Log.w(TAG, "container " + mRequestedProperties.mDimmingContainer
                        + "does not have a surface");
@@ -276,14 +289,19 @@ class SmoothDimmer extends Dimmer {
    }

    @Override
    protected void dim(WindowContainer container, int relativeLayer, float alpha, int blurRadius) {
    protected void adjustAppearance(WindowContainer container, float alpha, int blurRadius) {
        final DimState d = obtainDimState(container);

        mDimState.mRequestedProperties.mDimmingContainer = container;
        mDimState.setRequestedParameters(container, relativeLayer, alpha, blurRadius);
        mDimState.setRequestedAppearance(alpha, blurRadius);
        d.mDimming = true;
    }

    @Override
    protected void adjustRelativeLayer(WindowContainer container, int relativeLayer) {
        if (mDimState != null) {
            mDimState.setRequestedRelativeParent(container, relativeLayer);
        }
    }

    boolean updateDims(SurfaceControl.Transaction t) {
        if (mDimState == null) {
            return false;
+15 −4
Original line number Diff line number Diff line
@@ -5136,8 +5136,8 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP

    private void applyDims() {
        if (((mAttrs.flags & FLAG_DIM_BEHIND) != 0 || shouldDrawBlurBehind())
                && mToken.isVisibleRequested() && isVisibleNow() && !mHidden
                && mTransitionController.canApplyDim(getTask())) {
                && (Dimmer.DIMMER_REFACTOR ? mWinAnimator.getShown() : isVisibleNow())
                && !mHidden && mTransitionController.canApplyDim(getTask())) {
            // Only show the Dimmer when the following is satisfied:
            // 1. The window has the flag FLAG_DIM_BEHIND or blur behind is requested
            // 2. The WindowToken is not hidden so dims aren't shown when the window is exiting.
@@ -5147,7 +5147,13 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP
            mIsDimming = true;
            final float dimAmount = (mAttrs.flags & FLAG_DIM_BEHIND) != 0 ? mAttrs.dimAmount : 0;
            final int blurRadius = shouldDrawBlurBehind() ? mAttrs.getBlurBehindRadius() : 0;
            getDimmer().dimBelow(this, dimAmount, blurRadius);
            // If the window is visible from surface flinger perspective (mWinAnimator.getShown())
            // but not window manager visible (!isVisibleNow()), it can still be the parent of the
            // dim, but can not create a new surface or continue a dim alone.
            if (isVisibleNow()) {
                getDimmer().adjustAppearance(this, dimAmount, blurRadius);
            }
            getDimmer().adjustRelativeLayer(this, -1 /* relativeLayer */);
        }
    }

@@ -5207,12 +5213,17 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP
    void prepareSurfaces() {
        mIsDimming = false;
        if (mHasSurface) {
            if (!Dimmer.DIMMER_REFACTOR) {
                applyDims();
            }
            updateSurfacePositionNonOrganized();
            // Send information to SurfaceFlinger about the priority of the current window.
            updateFrameRateSelectionPriorityIfNeeded();
            updateScaleIfNeeded();
            mWinAnimator.prepareSurfaceLocked(getSyncTransaction());
            if (Dimmer.DIMMER_REFACTOR) {
                applyDims();
            }
        }
        super.prepareSurfaces();
    }
+29 −47
Original line number Diff line number Diff line
@@ -163,8 +163,8 @@ public class DimmerTests extends WindowTestsBase {
    public void testUpdateDimsAppliesCrop() {
        mHost.addChild(mChild, 0);

        final float alpha = 0.8f;
        mDimmer.dimAbove(mChild, alpha);
        mDimmer.adjustAppearance(mChild, 1, 1);
        mDimmer.adjustRelativeLayer(mChild, -1);

        int width = 100;
        int height = 300;
@@ -175,43 +175,14 @@ public class DimmerTests extends WindowTestsBase {
        verify(mTransaction).show(mDimmer.getDimLayer());
    }

    @Test
    public void testDimAboveWithChildCreatesSurfaceAboveChild_Smooth() {
        assumeTrue(Flags.dimmerRefactor());
        final float alpha = 0.8f;
        mHost.addChild(mChild, 0);
        mDimmer.dimAbove(mChild, alpha);
        SurfaceControl dimLayer = mDimmer.getDimLayer();

        assertNotNull("Dimmer should have created a surface", dimLayer);

        mDimmer.updateDims(mTransaction);
        verify(sTestAnimation).startAnimation(eq(dimLayer), eq(mTransaction),
                anyInt(), any(SurfaceAnimator.OnAnimationFinishedCallback.class));
        verify(mTransaction).setRelativeLayer(dimLayer, mChild.mControl, 1);
        verify(mTransaction, lastCall()).setAlpha(dimLayer, alpha);
    }

    @Test
    public void testDimAboveWithChildCreatesSurfaceAboveChild_Legacy() {
        assumeFalse(Flags.dimmerRefactor());
        final float alpha = 0.8f;
        mHost.addChild(mChild, 0);
        mDimmer.dimAbove(mChild, alpha);
        SurfaceControl dimLayer = mDimmer.getDimLayer();

        assertNotNull("Dimmer should have created a surface", dimLayer);

        verify(mHost.getPendingTransaction()).setAlpha(dimLayer, alpha);
        verify(mHost.getPendingTransaction()).setRelativeLayer(dimLayer, mChild.mControl, 1);
    }

    @Test
    public void testDimBelowWithChildSurfaceCreatesSurfaceBelowChild_Smooth() {
        assumeTrue(Flags.dimmerRefactor());
        final float alpha = 0.7f;
        final int blur = 50;
        mHost.addChild(mChild, 0);
        mDimmer.dimBelow(mChild, alpha, 50);
        mDimmer.adjustAppearance(mChild, alpha, blur);
        mDimmer.adjustRelativeLayer(mChild, -1);
        SurfaceControl dimLayer = mDimmer.getDimLayer();

        assertNotNull("Dimmer should have created a surface", dimLayer);
@@ -221,7 +192,7 @@ public class DimmerTests extends WindowTestsBase {
                anyInt(), any(SurfaceAnimator.OnAnimationFinishedCallback.class));
        verify(mTransaction).setRelativeLayer(dimLayer, mChild.mControl, -1);
        verify(mTransaction, lastCall()).setAlpha(dimLayer, alpha);
        verify(mTransaction).setBackgroundBlurRadius(dimLayer, 50);
        verify(mTransaction).setBackgroundBlurRadius(dimLayer, blur);
    }

    @Test
@@ -229,7 +200,8 @@ public class DimmerTests extends WindowTestsBase {
        assumeFalse(Flags.dimmerRefactor());
        final float alpha = 0.7f;
        mHost.addChild(mChild, 0);
        mDimmer.dimBelow(mChild, alpha, 50);
        mDimmer.adjustAppearance(mChild, alpha, 20);
        mDimmer.adjustRelativeLayer(mChild, -1);
        SurfaceControl dimLayer = mDimmer.getDimLayer();

        assertNotNull("Dimmer should have created a surface", dimLayer);
@@ -244,12 +216,15 @@ public class DimmerTests extends WindowTestsBase {
        mHost.addChild(mChild, 0);

        final float alpha = 0.8f;
        final int blur = 50;
        // Dim once
        mDimmer.dimBelow(mChild, alpha, 0);
        mDimmer.adjustAppearance(mChild, alpha, blur);
        mDimmer.adjustRelativeLayer(mChild, -1);
        SurfaceControl dimLayer = mDimmer.getDimLayer();
        mDimmer.updateDims(mTransaction);
        // Reset, and don't dim
        mDimmer.resetDimStates();
        mDimmer.adjustRelativeLayer(mChild, -1);
        mDimmer.updateDims(mTransaction);
        verify(mTransaction).show(dimLayer);
        verify(mTransaction).remove(dimLayer);
@@ -261,7 +236,8 @@ public class DimmerTests extends WindowTestsBase {
        mHost.addChild(mChild, 0);

        final float alpha = 0.8f;
        mDimmer.dimAbove(mChild, alpha);
        mDimmer.adjustAppearance(mChild, alpha, 20);
        mDimmer.adjustRelativeLayer(mChild, -1);
        SurfaceControl dimLayer = mDimmer.getDimLayer();
        mDimmer.resetDimStates();

@@ -278,13 +254,16 @@ public class DimmerTests extends WindowTestsBase {
        mHost.addChild(mChild, 0);

        final float alpha = 0.8f;
        final int blur = 20;
        // Dim once
        mDimmer.dimBelow(mChild, alpha, 0);
        mDimmer.adjustAppearance(mChild, alpha, blur);
        mDimmer.adjustRelativeLayer(mChild, -1);
        SurfaceControl dimLayer = mDimmer.getDimLayer();
        mDimmer.updateDims(mTransaction);
        // Reset and dim again
        mDimmer.resetDimStates();
        mDimmer.dimAbove(mChild, alpha);
        mDimmer.adjustAppearance(mChild, alpha, blur);
        mDimmer.adjustRelativeLayer(mChild, -1);
        mDimmer.updateDims(mTransaction);
        verify(mTransaction).show(dimLayer);
        verify(mTransaction, never()).remove(dimLayer);
@@ -294,7 +273,8 @@ public class DimmerTests extends WindowTestsBase {
    public void testDimUpdateWhileDimming() {
        mHost.addChild(mChild, 0);
        final float alpha = 0.8f;
        mDimmer.dimAbove(mChild, alpha);
        mDimmer.adjustAppearance(mChild, alpha, 20);
        mDimmer.adjustRelativeLayer(mChild, -1);
        final Rect bounds = mDimmer.getDimBounds();

        SurfaceControl dimLayer = mDimmer.getDimLayer();
@@ -314,7 +294,8 @@ public class DimmerTests extends WindowTestsBase {
    public void testRemoveDimImmediately_Smooth() {
        assumeTrue(Flags.dimmerRefactor());
        mHost.addChild(mChild, 0);
        mDimmer.dimAbove(mChild, 1);
        mDimmer.adjustAppearance(mChild, 1, 2);
        mDimmer.adjustRelativeLayer(mChild, -1);
        SurfaceControl dimLayer = mDimmer.getDimLayer();
        mDimmer.updateDims(mTransaction);
        verify(mTransaction, times(1)).show(dimLayer);
@@ -333,7 +314,8 @@ public class DimmerTests extends WindowTestsBase {
    public void testRemoveDimImmediately_Legacy() {
        assumeFalse(Flags.dimmerRefactor());
        mHost.addChild(mChild, 0);
        mDimmer.dimAbove(mChild, 1);
        mDimmer.adjustAppearance(mChild, 1, 0);
        mDimmer.adjustRelativeLayer(mChild, -1);
        SurfaceControl dimLayer = mDimmer.getDimLayer();
        mDimmer.updateDims(mTransaction);
        verify(mTransaction, times(1)).show(dimLayer);
@@ -351,16 +333,16 @@ public class DimmerTests extends WindowTestsBase {
    @Test
    public void testDimmerWithBlurUpdatesTransaction_Legacy() {
        assumeFalse(Flags.dimmerRefactor());
        TestWindowContainer child = new TestWindowContainer(mWm);
        mHost.addChild(child, 0);
        mHost.addChild(mChild, 0);

        final int blurRadius = 50;
        mDimmer.dimBelow(child, 0, blurRadius);
        mDimmer.adjustAppearance(mChild, 1, blurRadius);
        mDimmer.adjustRelativeLayer(mChild, -1);
        SurfaceControl dimLayer = mDimmer.getDimLayer();

        assertNotNull("Dimmer should have created a surface", dimLayer);

        verify(mHost.getPendingTransaction()).setBackgroundBlurRadius(dimLayer, blurRadius);
        verify(mHost.getPendingTransaction()).setRelativeLayer(dimLayer, child.mControl, -1);
        verify(mHost.getPendingTransaction()).setRelativeLayer(dimLayer, mChild.mControl, -1);
    }
}