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

Commit 683cac14 authored by Rupesh Bansal's avatar Rupesh Bansal
Browse files

Added private event flag for committed states

DisplayInfo currently sends display changed callbacks when display state
or the display committed state changes. Given a state change is followed
by a committed state change, users would often get 2 callbacks for a
single state change. To address this, the committed state is now being
ported to a separate private event for clients specifically interested
in this. Given committed state is a hidden field, we can be confident
that there are no apps interested in callbacks when this field changes

Bug: 342192387
Flag: com.android.server.display.feature.flags.committed_state_separate_event
Test: atest DisplayEventTest
Change-Id: I53b311f0bbc408acfd4cf02c8b1581506c957ab6
parent 7bbda01b
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -612,6 +612,7 @@ public final class DisplayManager {
            PRIVATE_EVENT_TYPE_DISPLAY_BRIGHTNESS,
            PRIVATE_EVENT_TYPE_HDR_SDR_RATIO_CHANGED,
            PRIVATE_EVENT_TYPE_DISPLAY_CONNECTION_CHANGED,
            PRIVATE_EVENT_TYPE_DISPLAY_COMMITTED_STATE_CHANGED
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface PrivateEventType {}
@@ -677,7 +678,7 @@ public final class DisplayManager {
     * through the {@link DisplayListener#onDisplayChanged} callback method. New brightness
     * values can be retrieved via {@link android.view.Display#getBrightnessInfo()}.
     *
     * @see #registerDisplayListener(DisplayListener, Handler, long)
     * @see #registerDisplayListener(DisplayListener, Handler, long, long)
     *
     * @hide
     */
@@ -690,7 +691,7 @@ public final class DisplayManager {
     *
     * Requires that {@link Display#isHdrSdrRatioAvailable()} is true.
     *
     * @see #registerDisplayListener(DisplayListener, Handler, long)
     * @see #registerDisplayListener(DisplayListener, Handler, long, long)
     *
     * @hide
     */
@@ -699,11 +700,19 @@ public final class DisplayManager {
    /**
     * Event type to register for a display's connection changed.
     *
     * @see #registerDisplayListener(DisplayListener, Handler, long)
     * @see #registerDisplayListener(DisplayListener, Handler, long, long)
     * @hide
     */
    public static final long PRIVATE_EVENT_TYPE_DISPLAY_CONNECTION_CHANGED = 1L << 2;

    /**
     * Event type to register for a display's committed state changes.
     *
     * @see #registerDisplayListener(DisplayListener, Handler, long, long)
     * @hide
     */
    public static final long PRIVATE_EVENT_TYPE_DISPLAY_COMMITTED_STATE_CHANGED = 1L << 3;


    /** @hide */
    public DisplayManager(Context context) {
+22 −1
Original line number Diff line number Diff line
@@ -113,7 +113,8 @@ public final class DisplayManagerGlobal {
            EVENT_DISPLAY_CONNECTED,
            EVENT_DISPLAY_DISCONNECTED,
            EVENT_DISPLAY_REFRESH_RATE_CHANGED,
            EVENT_DISPLAY_STATE_CHANGED
            EVENT_DISPLAY_STATE_CHANGED,
            EVENT_DISPLAY_COMMITTED_STATE_CHANGED
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface DisplayEvent {}
@@ -128,6 +129,8 @@ public final class DisplayManagerGlobal {
    public static final int EVENT_DISPLAY_DISCONNECTED = 7;
    public static final int EVENT_DISPLAY_REFRESH_RATE_CHANGED = 8;
    public static final int EVENT_DISPLAY_STATE_CHANGED = 9;
    public static final int EVENT_DISPLAY_COMMITTED_STATE_CHANGED = 10;


    @LongDef(prefix = {"INTERNAL_EVENT_FLAG_"}, flag = true, value = {
            INTERNAL_EVENT_FLAG_DISPLAY_ADDED,
@@ -139,6 +142,7 @@ public final class DisplayManagerGlobal {
            INTERNAL_EVENT_FLAG_DISPLAY_REFRESH_RATE,
            INTERNAL_EVENT_FLAG_DISPLAY_STATE,
            INTERNAL_EVENT_FLAG_TOPOLOGY_UPDATED,
            INTERNAL_EVENT_FLAG_DISPLAY_COMMITTED_STATE_CHANGED
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface InternalEventFlag {}
@@ -152,6 +156,8 @@ public final class DisplayManagerGlobal {
    public static final long INTERNAL_EVENT_FLAG_DISPLAY_REFRESH_RATE = 1L << 6;
    public static final long INTERNAL_EVENT_FLAG_DISPLAY_STATE = 1L << 7;
    public static final long INTERNAL_EVENT_FLAG_TOPOLOGY_UPDATED = 1L << 8;
    public static final long INTERNAL_EVENT_FLAG_DISPLAY_COMMITTED_STATE_CHANGED = 1L << 9;


    @UnsupportedAppUsage
    private static DisplayManagerGlobal sInstance;
@@ -1550,6 +1556,12 @@ public final class DisplayManagerGlobal {
                        mListener.onDisplayChanged(displayId);
                    }
                    break;
                case EVENT_DISPLAY_COMMITTED_STATE_CHANGED:
                    if ((mInternalEventFlagsMask
                            & INTERNAL_EVENT_FLAG_DISPLAY_COMMITTED_STATE_CHANGED) != 0) {
                        mListener.onDisplayChanged(displayId);
                    }
                    break;
            }
            if (DEBUG) {
                Trace.endSection();
@@ -1710,6 +1722,8 @@ public final class DisplayManagerGlobal {
                return "EVENT_DISPLAY_REFRESH_RATE_CHANGED";
            case EVENT_DISPLAY_STATE_CHANGED:
                return "EVENT_DISPLAY_STATE_CHANGED";
            case EVENT_DISPLAY_COMMITTED_STATE_CHANGED:
                return "EVENT_DISPLAY_COMMITTED_STATE_CHANGED";
        }
        return "UNKNOWN";
    }
@@ -1756,6 +1770,13 @@ public final class DisplayManagerGlobal {
                & DisplayManager.PRIVATE_EVENT_TYPE_DISPLAY_CONNECTION_CHANGED) != 0) {
            baseEventMask |= INTERNAL_EVENT_FLAG_DISPLAY_CONNECTION_CHANGED;
        }

        if (Flags.committedStateSeparateEvent()) {
            if ((privateEventFlags
                    & DisplayManager.PRIVATE_EVENT_TYPE_DISPLAY_COMMITTED_STATE_CHANGED) != 0) {
                baseEventMask |= INTERNAL_EVENT_FLAG_DISPLAY_COMMITTED_STATE_CHANGED;
            }
        }
        return baseEventMask;
    }

+11 −2
Original line number Diff line number Diff line
@@ -1595,8 +1595,17 @@ public abstract class WallpaperService extends Service {
            mWindow.setSession(mSession);

            mLayout.packageName = getPackageName();
            if (com.android.server.display.feature.flags.Flags
                    .displayListenerPerformanceImprovements()
                    && com.android.server.display.feature.flags.Flags
                    .committedStateSeparateEvent()) {
                mIWallpaperEngine.mDisplayManager.registerDisplayListener(mDisplayListener,
                        mCaller.getHandler(), DisplayManager.EVENT_TYPE_DISPLAY_CHANGED,
                        DisplayManager.PRIVATE_EVENT_TYPE_DISPLAY_COMMITTED_STATE_CHANGED);
            } else {
                mIWallpaperEngine.mDisplayManager.registerDisplayListener(mDisplayListener,
                        mCaller.getHandler());
            }
            mDisplay = mIWallpaperEngine.mDisplay;
            // Use window context of TYPE_WALLPAPER so client can access UI resources correctly.
            mDisplayContext = createDisplayContext(mDisplay)
+17 −10
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import android.util.SparseArray;
import android.util.proto.ProtoOutputStream;

import com.android.internal.display.BrightnessSynchronizer;
import com.android.server.display.feature.flags.Flags;

import java.util.Arrays;
import java.util.Objects;
@@ -447,18 +448,20 @@ public final class DisplayInfo implements Parcelable {
    }

    public boolean equals(DisplayInfo other) {
        return equals(other, /* compareRefreshRate */ true);
        return equals(other, /* compareOnlyBasicChanges */ false);
    }

    /**
     * Compares if the two DisplayInfo objects are equal or not
     * @param other The other DisplayInfo against which the comparison is to be done
     * @param compareRefreshRate Indicates if the refresh rate is also to be considered in
     *                           comparison
     * @param compareOnlyBasicChanges Indicates if the changes to be compared are the ones which
     *                               could lead to an emission of
     *                    {@link android.hardware.display.DisplayManager.EVENT_TYPE_DISPLAY_CHANGED}
     *                                event
     * @return
     */
    public boolean equals(DisplayInfo other, boolean compareRefreshRate) {
        boolean isEqualWithoutRefreshRate =  other != null
    public boolean equals(DisplayInfo other, boolean compareOnlyBasicChanges) {
        boolean isEqualWithOnlyBasicChanges =  other != null
                && layerStack == other.layerStack
                && flags == other.flags
                && type == other.type
@@ -494,7 +497,6 @@ public final class DisplayInfo implements Parcelable {
                && physicalXDpi == other.physicalXDpi
                && physicalYDpi == other.physicalYDpi
                && state == other.state
                && committedState == other.committedState
                && ownerUid == other.ownerUid
                && Objects.equals(ownerPackageName, other.ownerPackageName)
                && removeMode == other.removeMode
@@ -512,14 +514,19 @@ public final class DisplayInfo implements Parcelable {
                thermalBrightnessThrottlingDataId, other.thermalBrightnessThrottlingDataId)
                && canHostTasks == other.canHostTasks;

        if (compareRefreshRate) {
            return isEqualWithoutRefreshRate
        if (!Flags.committedStateSeparateEvent()) {
            isEqualWithOnlyBasicChanges = isEqualWithOnlyBasicChanges
                    && (committedState == other.committedState);
        }
        if (!compareOnlyBasicChanges) {
            return isEqualWithOnlyBasicChanges
                    && (getRefreshRate() == other.getRefreshRate())
                    && appVsyncOffsetNanos == other.appVsyncOffsetNanos
                    && presentationDeadlineNanos == other.presentationDeadlineNanos
                    && (modeId == other.modeId);
                    && (modeId == other.modeId)
                    && (committedState == other.committedState);
        }
        return isEqualWithoutRefreshRate;
        return isEqualWithOnlyBasicChanges;
    }

    @Override
+21 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.content.Context;
import android.os.Handler;
import android.os.RemoteException;
import android.platform.test.annotations.Presubmit;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
@@ -348,6 +349,26 @@ public class DisplayManagerGlobalTest {
                                DisplayManager.PRIVATE_EVENT_TYPE_DISPLAY_BRIGHTNESS));
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_COMMITTED_STATE_SEPARATE_EVENT)
    public void test_mapPrivateEventCommittedStateChanged_flagEnabled() {
        // Test public flags mapping
        assertEquals(DisplayManagerGlobal.INTERNAL_EVENT_FLAG_DISPLAY_COMMITTED_STATE_CHANGED,
                mDisplayManagerGlobal
                        .mapFiltersToInternalEventFlag(0,
                                DisplayManager.PRIVATE_EVENT_TYPE_DISPLAY_COMMITTED_STATE_CHANGED));
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_COMMITTED_STATE_SEPARATE_EVENT)
    public void test_mapPrivateEventCommittedStateChanged_flagDisabled() {
        // Test public flags mapping
        assertEquals(0,
                mDisplayManagerGlobal
                        .mapFiltersToInternalEventFlag(0,
                                DisplayManager.PRIVATE_EVENT_TYPE_DISPLAY_COMMITTED_STATE_CHANGED));
    }

    private void waitForHandler() {
        mHandler.runWithScissors(() -> {
        }, 0);
Loading