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

Commit 42460b0b authored by Santos Cordon's avatar Santos Cordon
Browse files

Do not send updates for disabled displays.

If a display is disabled, do not share it outside of
DisplayManagerService.

This will help by not creating any unnecessary updates for displays that
are disabled, and also not informing Applications of displays that they
are unable to use.

Also fixing a concurrency issue with DMS.handleLogicalDisplaySwapped
which became apparent with this CL. Deferring the runnable with a
Handler.post() caused the display-state-OFF update to happen after a newer
change to turn the display ON.  This caused the device to startup into a
black screen. Removed the unnecessary call (since the displays are now
made to both go into the "transition" state) to update the state and
removed a lot of the redundant code with handleLogicalDisplayChange()
which was called at the end of the method anyway.

Bug: 221071695
Test: atest com.server.android.display
Test: Manually verify display layouts still successfully change, and
      also confirm logs no longer hand excessive add/remove
      display notifications.
Change-Id: I003fd91567630a447cf030b6fb83e5cb1041bcb5
parent d9cae77b
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -588,18 +588,20 @@ public final class DisplayManager {
     * @see #DISPLAY_CATEGORY_PRESENTATION
     */
    public Display[] getDisplays(String category) {
        final int[] displayIds = mGlobal.getDisplayIds();
        boolean includeDisabled = (category != null
                && category.equals(DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED));
        final int[] displayIds = mGlobal.getDisplayIds(includeDisabled);
        synchronized (mLock) {
            try {
                if (category == null
                        || DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED.equals(category)) {
                    addAllDisplaysLocked(mTempDisplays, displayIds);
                } else if (category.equals(DISPLAY_CATEGORY_PRESENTATION)) {
                if (DISPLAY_CATEGORY_PRESENTATION.equals(category)) {
                    addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_WIFI);
                    addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_EXTERNAL);
                    addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_OVERLAY);
                    addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_VIRTUAL);
                    addPresentationDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_INTERNAL);
                } else if (category == null
                        || DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED.equals(category)) {
                    addAllDisplaysLocked(mTempDisplays, displayIds);
                }
                return mTempDisplays.toArray(new Display[mTempDisplays.size()]);
            } finally {
+11 −1
Original line number Diff line number Diff line
@@ -212,6 +212,16 @@ public final class DisplayManagerGlobal {
     */
    @UnsupportedAppUsage
    public int[] getDisplayIds() {
        return getDisplayIds(/* includeDisabled= */ false);
    }

    /**
     * Gets all currently valid logical display ids.
     *
     * @param includeDisabled True if the returned list of displays includes disabled displays.
     * @return An array containing all display ids.
     */
    public int[] getDisplayIds(boolean includeDisabled) {
        try {
            synchronized (mLock) {
                if (USE_CACHE) {
@@ -220,7 +230,7 @@ public final class DisplayManagerGlobal {
                    }
                }

                int[] displayIds = mDm.getDisplayIds();
                int[] displayIds = mDm.getDisplayIds(includeDisabled);
                if (USE_CACHE) {
                    mDisplayIdCache = displayIds;
                }
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ import android.view.Surface;
interface IDisplayManager {
    @UnsupportedAppUsage
    DisplayInfo getDisplayInfo(int displayId);
    int[] getDisplayIds();
    int[] getDisplayIds(boolean includeDisabled);

    boolean isUidPresentOnDisplay(int uid, int displayId);

+21 −23
Original line number Diff line number Diff line
@@ -1573,7 +1573,7 @@ public final class DisplayManagerService extends SystemService {
            mSyncRoot.notifyAll();
        }

        sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
        sendDisplayEventLocked(display, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);

        Runnable work = updateDisplayStateLocked(device);
        if (work != null) {
@@ -1592,7 +1592,7 @@ public final class DisplayManagerService extends SystemService {
        // We don't bother invalidating the display info caches here because any changes to the
        // display info will trigger a cache invalidation inside of LogicalDisplay before we hit
        // this point.
        sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
        sendDisplayEventLocked(display, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
        scheduleTraversalLocked(false);
        mPersistentDataStore.saveIfNeeded();

@@ -1622,7 +1622,7 @@ public final class DisplayManagerService extends SystemService {
        mDisplayStates.delete(displayId);
        mDisplayBrightnesses.delete(displayId);
        DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
        sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
        sendDisplayEventLocked(display, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
        scheduleTraversalLocked(false);

        if (mDisplayWindowPolicyControllers.contains(displayId)) {
@@ -1638,23 +1638,13 @@ public final class DisplayManagerService extends SystemService {
    }

    private void handleLogicalDisplaySwappedLocked(@NonNull LogicalDisplay display) {
        final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
        final Runnable work = updateDisplayStateLocked(device);
        if (work != null) {
            mHandler.post(work);
        }
        final int displayId = display.getDisplayIdLocked();
        handleLogicalDisplayChangedLocked(display);

        final int displayId = display.getDisplayIdLocked();
        if (displayId == Display.DEFAULT_DISPLAY) {
            notifyDefaultDisplayDeviceUpdated(display);
        }
        DisplayPowerControllerInterface dpc = mDisplayPowerControllers.get(displayId);
        if (dpc != null) {
            dpc.onDisplayChanged();
        }
        mPersistentDataStore.saveIfNeeded();
        mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATIONS);
        handleLogicalDisplayChangedLocked(display);
    }

    private void notifyDefaultDisplayDeviceUpdated(LogicalDisplay display) {
@@ -1666,7 +1656,7 @@ public final class DisplayManagerService extends SystemService {
        final int displayId = display.getDisplayIdLocked();
        final DisplayPowerControllerInterface dpc = mDisplayPowerControllers.get(displayId);
        if (dpc != null) {
            dpc.onDeviceStateTransition();
            dpc.onDisplayChanged();
        }
    }

@@ -2366,10 +2356,14 @@ public final class DisplayManagerService extends SystemService {
        }
    }

    private void sendDisplayEventLocked(int displayId, @DisplayEvent int event) {
    private void sendDisplayEventLocked(@NonNull LogicalDisplay display, @DisplayEvent int event) {
        // Only send updates outside of DisplayManagerService for enabled displays
        if (display.isEnabledLocked()) {
            int displayId = display.getDisplayIdLocked();
            Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
            mHandler.sendMessage(msg);
        }
    }

    private void sendDisplayGroupEvent(int groupId, int event) {
        Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_GROUP_EVENT, groupId, event);
@@ -2653,8 +2647,7 @@ public final class DisplayManagerService extends SystemService {
    }

    private void handleBrightnessChange(LogicalDisplay display) {
        sendDisplayEventLocked(display.getDisplayIdLocked(),
                DisplayManagerGlobal.EVENT_DISPLAY_BRIGHTNESS_CHANGED);
        sendDisplayEventLocked(display, DisplayManagerGlobal.EVENT_DISPLAY_BRIGHTNESS_CHANGED);
    }

    private DisplayDevice getDeviceForDisplayLocked(int displayId) {
@@ -2871,12 +2864,12 @@ public final class DisplayManagerService extends SystemService {
         * Returns the list of all display ids.
         */
        @Override // Binder call
        public int[] getDisplayIds() {
        public int[] getDisplayIds(boolean includeDisabled) {
            final int callingUid = Binder.getCallingUid();
            final long token = Binder.clearCallingIdentity();
            try {
                synchronized (mSyncRoot) {
                    return mLogicalDisplayMapper.getDisplayIdsLocked(callingUid);
                    return mLogicalDisplayMapper.getDisplayIdsLocked(callingUid, includeDisabled);
                }
            } finally {
                Binder.restoreCallingIdentity(token);
@@ -3367,6 +3360,11 @@ public final class DisplayManagerService extends SystemService {
            final long token = Binder.clearCallingIdentity();
            try {
                synchronized (mSyncRoot) {
                    LogicalDisplay display = mLogicalDisplayMapper.getDisplayLocked(
                            displayId, /* includeDisabled= */ false);
                    if (display == null || !display.isEnabledLocked()) {
                        return null;
                    }
                    DisplayPowerControllerInterface dpc = mDisplayPowerControllers.get(displayId);
                    if (dpc != null) {
                        return dpc.getBrightnessInfo();
+6 −3
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.display;

import static android.hardware.display.DisplayManager.DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED;
import static android.hardware.display.DisplayManagerInternal.REFRESH_RATE_LIMIT_HIGH_BRIGHTNESS_MODE;
import static android.os.PowerManager.BRIGHTNESS_INVALID;

@@ -1640,7 +1641,7 @@ public class DisplayModeDirector {
            SparseArray<Display.Mode[]> modes = new SparseArray<>();
            SparseArray<Display.Mode> defaultModes = new SparseArray<>();
            DisplayInfo info = new DisplayInfo();
            Display[] displays = dm.getDisplays();
            Display[] displays = dm.getDisplays(DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED);
            for (Display d : displays) {
                final int displayId = d.getDisplayId();
                d.getDisplayInfo(info);
@@ -2517,7 +2518,8 @@ public class DisplayModeDirector {
            sensorManager.addProximityActiveListener(BackgroundThread.getExecutor(), this);

            synchronized (mSensorObserverLock) {
                for (Display d : mDisplayManager.getDisplays()) {
                for (Display d : mDisplayManager.getDisplays(
                        DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED)) {
                    mDozeStateByDisplay.put(d.getDisplayId(), mInjector.isDozeState(d));
                }
            }
@@ -2528,7 +2530,8 @@ public class DisplayModeDirector {
        }

        private void recalculateVotesLocked() {
            final Display[] displays = mDisplayManager.getDisplays();
            final Display[] displays = mDisplayManager.getDisplays(
                    DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED);
            for (Display d : displays) {
                int displayId = d.getDisplayId();
                Vote vote = null;
Loading