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

Commit d9910dc4 authored by Ján Sebechlebský's avatar Ján Sebechlebský Committed by Automerger Merge Worker
Browse files

Merge "Refactor fetching Display-s in DisplayManager" into udc-dev am: 0acd2a68 am: 58d770ed

parents ca7ce2b5 58d770ed
Loading
Loading
Loading
Loading
+55 −64
Original line number Original line Diff line number Diff line
@@ -53,12 +53,16 @@ import android.view.Surface;
import com.android.internal.R;
import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.GuardedBy;


import libcore.util.EmptyArray;

import java.lang.annotation.Retention;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.Executor;
import java.util.function.Predicate;




/**
/**
@@ -86,9 +90,6 @@ public final class DisplayManager {
    @GuardedBy("mLock")
    @GuardedBy("mLock")
    private final WeakDisplayCache mDisplayCache = new WeakDisplayCache();
    private final WeakDisplayCache mDisplayCache = new WeakDisplayCache();


    @GuardedBy("mLock")
    private final ArrayList<Display> mTempDisplays = new ArrayList<Display>();

    /**
    /**
     * Broadcast receiver that indicates when the Wifi display status changes.
     * Broadcast receiver that indicates when the Wifi display status changes.
     * <p>
     * <p>
@@ -627,9 +628,7 @@ public final class DisplayManager {
     * @return The display object, or null if there is no valid display with the given id.
     * @return The display object, or null if there is no valid display with the given id.
     */
     */
    public Display getDisplay(int displayId) {
    public Display getDisplay(int displayId) {
        synchronized (mLock) {
        return getOrCreateDisplay(displayId, false /*assumeValid*/);
            return getOrCreateDisplayLocked(displayId, false /*assumeValid*/);
        }
    }
    }


    /**
    /**
@@ -661,66 +660,57 @@ public final class DisplayManager {
        boolean includeDisabled = (category != null
        boolean includeDisabled = (category != null
                && category.equals(DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED));
                && category.equals(DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED));
        final int[] displayIds = mGlobal.getDisplayIds(includeDisabled);
        final int[] displayIds = mGlobal.getDisplayIds(includeDisabled);
        synchronized (mLock) {
            try {
        if (DISPLAY_CATEGORY_PRESENTATION.equals(category)) {
        if (DISPLAY_CATEGORY_PRESENTATION.equals(category)) {
                    addDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_WIFI,
            return getDisplays(displayIds, DisplayManager::isPresentationDisplay);
                            Display.FLAG_PRESENTATION);
                    addDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_EXTERNAL,
                            Display.FLAG_PRESENTATION);
                    addDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_OVERLAY,
                            Display.FLAG_PRESENTATION);
                    addDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_VIRTUAL,
                            Display.FLAG_PRESENTATION);
                    addDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_INTERNAL,
                            Display.FLAG_PRESENTATION);
        } else if (DISPLAY_CATEGORY_REAR.equals(category)) {
        } else if (DISPLAY_CATEGORY_REAR.equals(category)) {
                    addDisplaysLocked(mTempDisplays, displayIds, Display.TYPE_INTERNAL,
            return getDisplays(displayIds, DisplayManager::isRearDisplay);
                            Display.FLAG_REAR);
        } else if (category == null || DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED.equals(category)) {
                } else if (category == null
            return getDisplays(displayIds, Objects::nonNull);
                        || DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED.equals(category)) {
                    addAllDisplaysLocked(mTempDisplays, displayIds);
                }
                return mTempDisplays.toArray(new Display[mTempDisplays.size()]);
            } finally {
                mTempDisplays.clear();
            }
        }
        }
        return (Display[]) EmptyArray.OBJECT;
    }
    }


    @GuardedBy("mLock")
    private Display[] getDisplays(int[] displayIds, Predicate<Display> predicate) {
    private void addAllDisplaysLocked(ArrayList<Display> displays, int[] displayIds) {
        ArrayList<Display> tmpDisplays = new ArrayList<>();
        for (int i = 0; i < displayIds.length; i++) {
        for (int displayId : displayIds) {
            Display display = getOrCreateDisplayLocked(displayIds[i], true /*assumeValid*/);
            Display display = getOrCreateDisplay(displayId, /*assumeValid=*/true);
            if (display != null) {
            if (predicate.test(display)) {
                displays.add(display);
                tmpDisplays.add(display);
            }
            }
        }
        }
        return tmpDisplays.toArray(new Display[tmpDisplays.size()]);
    }
    }


    @GuardedBy("mLock")
    private static boolean isPresentationDisplay(@Nullable Display display) {
    private void addDisplaysLocked(
        if (display == null || (display.getDisplayId() == DEFAULT_DISPLAY)
            ArrayList<Display> displays, int[] displayIds, int matchType, int flagMask) {
                || (display.getFlags() & Display.FLAG_PRESENTATION) == 0) {
        for (int displayId : displayIds) {
            return false;
            if (displayId == DEFAULT_DISPLAY) {
                continue;
        }
        }

        switch (display.getType()) {
            Display display = getOrCreateDisplayLocked(displayId, /* assumeValid= */ true);
            case Display.TYPE_INTERNAL:
            if (display != null
            case Display.TYPE_EXTERNAL:
                    && (display.getFlags() & flagMask) == flagMask
            case Display.TYPE_WIFI:
                    && display.getType() == matchType) {
            case Display.TYPE_OVERLAY:
                displays.add(display);
            case Display.TYPE_VIRTUAL:
                return true;
            default:
                return false;
        }
        }
    }
    }

    private static boolean isRearDisplay(@Nullable Display display) {
        return display != null && display.getDisplayId() != DEFAULT_DISPLAY
                && display.getType() == Display.TYPE_INTERNAL
                && (display.getFlags() & Display.FLAG_REAR) != 0;
    }
    }


    @GuardedBy("mLock")
    private Display getOrCreateDisplay(int displayId, boolean assumeValid) {
    private Display getOrCreateDisplayLocked(int displayId, boolean assumeValid) {
        Display display;
        Display display = mDisplayCache.get(displayId);
        synchronized (mLock) {
            display = mDisplayCache.get(displayId);
            if (display == null) {
            if (display == null) {
            // TODO: We cannot currently provide any override configurations for metrics on displays
                // TODO: We cannot currently provide any override configurations for metrics on
            // other than the display the context is associated with.
                // displays other than the display the context is associated with.
                final Resources resources = mContext.getDisplayId() == displayId
                final Resources resources = mContext.getDisplayId() == displayId
                        ? mContext.getResources() : null;
                        ? mContext.getResources() : null;


@@ -731,6 +721,7 @@ public final class DisplayManager {
            } else if (!assumeValid && !display.isValid()) {
            } else if (!assumeValid && !display.isValid()) {
                display = null;
                display = null;
            }
            }
        }
        return display;
        return display;
    }
    }