Add DisplayModeDirector to determine set of allowed modes.

SurfaceFlinger now knows how to automatically schedule between various
display modes, so we need to tell it what modes are available to switch
between based on overall system state as well as display specific
properties. To capture all of this system state we've introduced
DisplayModeDirector which monitors all of the various inputs for
deciding display mode and notifies the rest of the display
infrastructure when they change.

Bug: 123727652
Test: manual
Change-Id: I83184664bf63c99ebd31889764720bb55c2e15a8
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index d395a9f..b85ce61 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -3314,6 +3314,17 @@
                         ColorDisplayManager.COLOR_MODE_AUTOMATIC);
 
         /**
+         * The user selected peak refresh rate in frames per second.
+         *
+         * If this isn't set, the system falls back to a device specific default.
+         * @hide
+         */
+        public static final String PEAK_REFRESH_RATE = "peak_refresh_rate";
+
+        private static final Validator PEAK_REFRESH_RATE_VALIDATOR =
+                new SettingsValidators.InclusiveFloatRangeValidator(24f, Float.MAX_VALUE);
+
+        /**
          * The amount of time in milliseconds before the device goes to sleep or begins
          * to dream after a period of inactivity.  This value is also known as the
          * user activity timeout period since the screen isn't necessarily turned off
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 4e174c4..cbc8bd1 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -3951,4 +3951,7 @@
      and a second time clipped to the fill level to indicate charge -->
     <bool name="config_batterymeterDualTone">false</bool>
 
+    <!-- The default peak refresh rate for a given device. Change this value if you want to allow
+         for higher refresh rates to be automatically used out of the box -->
+    <integer name="config_defaultPeakRefreshRate">60</integer>
 </resources>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 426d813..d5444c0 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -3688,4 +3688,6 @@
   <java-symbol type="string" name="mime_type_spreadsheet_ext" />
   <java-symbol type="string" name="mime_type_presentation" />
   <java-symbol type="string" name="mime_type_presentation_ext" />
+
+  <java-symbol type="integer" name="config_defaultPeakRefreshRate" />
 </resources>
diff --git a/core/tests/coretests/src/android/provider/SettingsBackupTest.java b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
index 69632c1..ebc6be7 100644
--- a/core/tests/coretests/src/android/provider/SettingsBackupTest.java
+++ b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
@@ -92,7 +92,8 @@
                     Settings.System.VOLUME_SYSTEM, // deprecated since API 2?
                     Settings.System.VOLUME_VOICE, // deprecated since API 2?
                     Settings.System.WHEN_TO_MAKE_WIFI_CALLS, // bug?
-                    Settings.System.WINDOW_ORIENTATION_LISTENER_LOG // used for debugging only
+                    Settings.System.WINDOW_ORIENTATION_LISTENER_LOG, // used for debugging only
+                    Settings.System.PEAK_REFRESH_RATE // depends on hardware capabilities
                     );
 
     private static final Set<String> BACKUP_BLACKLISTED_GLOBAL_SETTINGS =
diff --git a/services/core/java/com/android/server/display/DisplayDevice.java b/services/core/java/com/android/server/display/DisplayDevice.java
index e9ae516..9882f6c 100644
--- a/services/core/java/com/android/server/display/DisplayDevice.java
+++ b/services/core/java/com/android/server/display/DisplayDevice.java
@@ -138,9 +138,19 @@
     }
 
     /**
-     * Sets the mode, if supported.
+     * Sets the display modes the system is allowed to switch between, roughly ordered by
+     * preference.
+     *
+     * Not all display devices will automatically switch between modes, so it's important that the
+     * most-desired modes are at the beginning of the allowed array.
      */
-    public void requestDisplayModesLocked(int colorMode, int modeId) {
+    public void setAllowedDisplayModesLocked(int[] modes) {
+    }
+
+    /**
+     * Sets the requested color mode.
+     */
+    public void setRequestedColorModeLocked(int colorMode) {
     }
 
     public void onOverlayChangedLocked() {
diff --git a/services/core/java/com/android/server/display/DisplayManagerService.java b/services/core/java/com/android/server/display/DisplayManagerService.java
index 1aaaf41..2f53951 100644
--- a/services/core/java/com/android/server/display/DisplayManagerService.java
+++ b/services/core/java/com/android/server/display/DisplayManagerService.java
@@ -167,13 +167,13 @@
     private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
     private static final int MSG_REQUEST_TRAVERSAL = 4;
     private static final int MSG_UPDATE_VIEWPORT = 5;
-    private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
-    private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 7;
+    private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 6;
 
     private final Context mContext;
     private final DisplayManagerHandler mHandler;
     private final Handler mUiHandler;
     private final DisplayAdapterListener mDisplayAdapterListener;
+    private final DisplayModeDirector mDisplayModeDirector;
     private WindowManagerInternal mWindowManagerInternal;
     private InputManagerInternal mInputManagerInternal;
     private IMediaProjectionManager mProjectionService;
@@ -310,6 +310,7 @@
         mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
         mUiHandler = UiThread.getHandler();
         mDisplayAdapterListener = new DisplayAdapterListener();
+        mDisplayModeDirector = new DisplayModeDirector(context, mHandler);
         mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
         Resources resources = mContext.getResources();
         mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
@@ -322,7 +323,7 @@
         mMinimumBrightnessCurve = new Curve(lux, nits);
         mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
 
-        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
+        PowerManager pm = mContext.getSystemService(PowerManager.class);
         mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
         mCurrentUserId = UserHandle.USER_SYSTEM;
         ColorSpace[] colorSpaces = SurfaceControl.getCompositionColorSpaces();
@@ -347,9 +348,9 @@
         // adapter is up so that we have it's configuration. We could load it lazily, but since
         // we're going to have to read it in eventually we may as well do it here rather than after
         // we've waited for the display to register itself with us.
-		synchronized(mSyncRoot) {
-			mPersistentDataStore.loadIfNeeded();
-			loadStableDisplayValuesLocked();
+        synchronized (mSyncRoot) {
+            mPersistentDataStore.loadIfNeeded();
+            loadStableDisplayValuesLocked();
         }
         mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
 
@@ -417,8 +418,10 @@
             mOnlyCore = onlyCore;
         }
 
+        mDisplayModeDirector.setListener(new AllowedDisplayModeObserver());
+        mDisplayModeDirector.start();
+
         mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
-        mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
     }
 
     @VisibleForTesting
@@ -1194,13 +1197,8 @@
                 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
                         requestedRefreshRate);
             }
-            if (display.getRequestedModeIdLocked() != requestedModeId) {
-                if (DEBUG) {
-                    Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
-                }
-                display.setRequestedModeIdLocked(requestedModeId);
-                scheduleTraversalLocked(inTraversal);
-            }
+            mDisplayModeDirector.getAppRequestObserver().setAppRequestedMode(
+                    displayId, requestedModeId);
         }
     }
 
@@ -1319,6 +1317,28 @@
         return SurfaceControl.getDisplayedContentSample(token, maxFrames, timestamp);
     }
 
+    private void onAllowedDisplayModesChangedInternal() {
+        boolean changed = false;
+        synchronized (mSyncRoot) {
+            final int count = mLogicalDisplays.size();
+            for (int i = 0; i < count; i++) {
+                LogicalDisplay display = mLogicalDisplays.valueAt(i);
+                int displayId = mLogicalDisplays.keyAt(i);
+                int[] allowedModes = mDisplayModeDirector.getAllowedModes(displayId);
+                // Note that order is important here since not all display devices are capable of
+                // automatically switching, so we do actually want to check for equality and not
+                // just equivalent contents (regardless of order).
+                if (!Arrays.equals(allowedModes, display.getAllowedDisplayModesLocked())) {
+                    display.setAllowedDisplayModesLocked(allowedModes);
+                    changed = true;
+                }
+            }
+            if (changed) {
+                scheduleTraversalLocked(false);
+            }
+        }
+    }
+
     private void clearViewportsLocked() {
         mViewports.clear();
     }
@@ -1518,6 +1538,9 @@
                 display.dumpLocked(ipw);
             }
 
+            pw.println();
+            mDisplayModeDirector.dump(pw);
+
             final int callbackCount = mCallbacks.size();
             pw.println();
             pw.println("Callbacks: size=" + callbackCount);
@@ -2425,4 +2448,10 @@
         }
 
     }
+
+    class AllowedDisplayModeObserver implements DisplayModeDirector.Listener {
+        public void onAllowedDisplayModesChanged() {
+            onAllowedDisplayModesChangedInternal();
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/display/DisplayModeDirector.java b/services/core/java/com/android/server/display/DisplayModeDirector.java
new file mode 100644
index 0000000..af4db07
--- /dev/null
+++ b/services/core/java/com/android/server/display/DisplayModeDirector.java
@@ -0,0 +1,685 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.database.ContentObserver;
+import android.hardware.display.DisplayManager;
+import android.net.Uri;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.os.UserHandle;
+import android.provider.Settings;
+import android.util.Slog;
+import android.util.SparseArray;
+import android.view.Display;
+import android.view.DisplayInfo;
+
+import com.android.internal.R;
+
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Objects;
+
+/**
+ * The DisplayModeDirector is responsible for determining what modes are allowed to be
+ * automatically picked by the system based on system-wide and display-specific configuration.
+ */
+public class DisplayModeDirector {
+    private static final String TAG = "DisplayModeDirector";
+    private static final boolean DEBUG = false;
+
+    private static final int MSG_ALLOWED_MODES_CHANGED = 1;
+
+    // Special ID used to indicate that given vote is to be applied globally, rather than to a
+    // specific display.
+    private static final int GLOBAL_ID = -1;
+
+    // What we consider to be the system's "default" refresh rate.
+    private static final float DEFAULT_REFRESH_RATE = 60f;
+
+    // The tolerance within which we consider something approximately equals.
+    private static final float EPSILON = 0.001f;
+
+    private final Object mLock = new Object();
+    private final Context mContext;
+
+    private final DisplayModeDirectorHandler mHandler;
+
+    // A map from the display ID to the collection of votes and their priority. The latter takes
+    // the form of another map from the priority to the vote itself so that each priority is
+    // guaranteed to have exactly one vote, which is also easily and efficiently replaceable.
+    private final SparseArray<SparseArray<Vote>> mVotesByDisplay;
+    // A map from the display ID to the supported modes on that display.
+    private final SparseArray<Display.Mode[]> mSupportedModesByDisplay;
+    // A map from the display ID to the default mode of that display.
+    private final SparseArray<Display.Mode> mDefaultModeByDisplay;
+
+    private final AppRequestObserver mAppRequestObserver;
+    private final SettingsObserver mSettingsObserver;
+    private final DisplayObserver mDisplayObserver;
+
+
+    private Listener mListener;
+
+    public DisplayModeDirector(@NonNull Context context, @NonNull Handler handler) {
+        mContext = context;
+        mHandler = new DisplayModeDirectorHandler(handler.getLooper());
+        mVotesByDisplay = new SparseArray<>();
+        mSupportedModesByDisplay = new SparseArray<>();
+        mDefaultModeByDisplay =  new SparseArray<>();
+        mAppRequestObserver = new AppRequestObserver();
+        mSettingsObserver = new SettingsObserver(context, handler);
+        mDisplayObserver = new DisplayObserver(context, handler);
+    }
+
+    /**
+     * Tells the DisplayModeDirector to update allowed votes and begin observing relevant system
+     * state.
+     *
+     * This has to be deferred because the object may be constructed before the rest of the system
+     * is ready.
+     */
+    public void start() {
+        mSettingsObserver.observe();
+        mDisplayObserver.observe();
+        mSettingsObserver.observe();
+        synchronized (mLock) {
+            // We may have a listener already registered before the call to start, so go ahead and
+            // notify them to pick up our newly initialized state.
+            notifyAllowedModesChangedLocked();
+        }
+    }
+
+    /**
+     * Calculates the modes the system is allowed to freely switch between based on global and
+     * display-specific constraints.
+     *
+     * @param displayId The display to query for.
+     * @return The IDs of the modes the system is allowed to freely switch between.
+     */
+    @NonNull
+    public int[] getAllowedModes(int displayId) {
+        synchronized (mLock) {
+            SparseArray<Vote> votes = getVotesLocked(displayId);
+            Display.Mode[] modes = mSupportedModesByDisplay.get(displayId);
+            Display.Mode defaultMode = mDefaultModeByDisplay.get(displayId);
+            if (modes == null || defaultMode == null) {
+                Slog.e(TAG, "Asked about unknown display, returning empty allowed set! (id="
+                        + displayId + ")");
+                return new int[0];
+            }
+            return getAllowedModesLocked(votes, modes, defaultMode);
+        }
+    }
+
+    @NonNull
+    private SparseArray<Vote> getVotesLocked(int displayId) {
+        SparseArray<Vote> displayVotes = mVotesByDisplay.get(displayId);
+        final SparseArray<Vote> votes;
+        if (displayVotes != null) {
+            votes = displayVotes.clone();
+        } else {
+            votes = new SparseArray<>();
+        }
+
+        SparseArray<Vote> globalVotes = mVotesByDisplay.get(GLOBAL_ID);
+        if (globalVotes != null) {
+            for (int i = 0; i < globalVotes.size(); i++) {
+                int priority = globalVotes.keyAt(i);
+                if (votes.indexOfKey(priority) < 0) {
+                    votes.put(priority, globalVotes.valueAt(i));
+                }
+            }
+        }
+        return votes;
+    }
+
+    @NonNull
+    private int[] getAllowedModesLocked(@NonNull SparseArray<Vote> votes,
+            @NonNull Display.Mode[] modes, @NonNull Display.Mode defaultMode) {
+        int lowestConsideredPriority = Vote.MIN_PRIORITY;
+        while (lowestConsideredPriority <= Vote.MAX_PRIORITY) {
+            float minRefreshRate = 0f;
+            float maxRefreshRate = Float.POSITIVE_INFINITY;
+            int height = Vote.INVALID_SIZE;
+            int width = Vote.INVALID_SIZE;
+
+            for (int priority = Vote.MAX_PRIORITY;
+                    priority >= lowestConsideredPriority;
+                    priority--) {
+                Vote vote = votes.get(priority);
+                if (vote == null) {
+                    continue;
+                }
+                // For refresh rates, just use the tightest bounds of all the votes
+                minRefreshRate = Math.max(minRefreshRate, vote.minRefreshRate);
+                maxRefreshRate = Math.min(maxRefreshRate, vote.maxRefreshRate);
+                // For display size, use only the first vote we come across (i.e. the highest
+                // priority vote that includes the width / height).
+                if (height == Vote.INVALID_SIZE && width == Vote.INVALID_SIZE
+                        && vote.height > 0 && vote.width > 0) {
+                    width = vote.width;
+                    height = vote.height;
+                }
+            }
+
+            // If we don't have anything specifying the width / height of the display, just use the
+            // default width and height. We don't want these switching out from underneath us since
+            // it's a pretty disruptive behavior.
+            if (height == Vote.INVALID_SIZE || width == Vote.INVALID_SIZE) {
+                width = defaultMode.getPhysicalWidth();
+                height = defaultMode.getPhysicalHeight();
+            }
+
+            int[] availableModes =
+                    filterModes(modes, width, height, minRefreshRate, maxRefreshRate);
+            if (availableModes.length > 0) {
+                if (DEBUG) {
+                    Slog.w(TAG, "Found available modes=" + Arrays.toString(availableModes)
+                            + " with lowest priority considered "
+                            + Vote.priorityToString(lowestConsideredPriority)
+                            + " and constraints: "
+                            + "width=" + width
+                            + ", height=" + height
+                            + ", minRefreshRate=" + minRefreshRate
+                            + ", maxRefreshRate=" + maxRefreshRate);
+                }
+                return availableModes;
+            }
+
+            if (DEBUG) {
+                Slog.w(TAG, "Couldn't find available modes with lowest priority set to "
+                        + Vote.priorityToString(lowestConsideredPriority)
+                        + " and with the following constraints: "
+                        + "width=" + width
+                        + ", height=" + height
+                        + ", minRefreshRate=" + minRefreshRate
+                        + ", maxRefreshRate=" + maxRefreshRate);
+            }
+            // If we haven't found anything with the current set of votes, drop the current lowest
+            // priority vote.
+            lowestConsideredPriority++;
+        }
+
+        // If we still haven't found anything that matches our current set of votes, just fall back
+        // to the default mode.
+        return new int[] { defaultMode.getModeId() };
+    }
+
+    private int[] filterModes(Display.Mode[] supportedModes,
+            int width, int height, float minRefreshRate, float maxRefreshRate) {
+        ArrayList<Display.Mode> availableModes = new ArrayList<>();
+        for (Display.Mode mode : supportedModes) {
+            if (mode.getPhysicalWidth() != width || mode.getPhysicalHeight() != height) {
+                if (DEBUG) {
+                    Slog.w(TAG, "Discarding mode " + mode.getModeId() + ", wrong size"
+                            + ": desiredWidth=" + width
+                            + ": desiredHeight=" + height
+                            + ": actualWidth=" + mode.getPhysicalWidth()
+                            + ": actualHeight=" + mode.getPhysicalHeight());
+                }
+                continue;
+            }
+            final float refreshRate = mode.getRefreshRate();
+            // Some refresh rates are calculated based on frame timings, so they aren't *exactly*
+            // equal to expected refresh rate. Given that, we apply a bit of tolerance to this
+            // comparison.
+            if (refreshRate < (minRefreshRate - EPSILON)
+                    || refreshRate > (maxRefreshRate + EPSILON)) {
+                if (DEBUG) {
+                    Slog.w(TAG, "Discarding mode " + mode.getModeId()
+                            + ", outside refresh rate bounds"
+                            + ": minRefreshRate=" + minRefreshRate
+                            + ", maxRefreshRate=" + maxRefreshRate
+                            + ", modeRefreshRate=" + refreshRate);
+                }
+                continue;
+            }
+            availableModes.add(mode);
+        }
+        final int size = availableModes.size();
+        int[] availableModeIds = new int[size];
+        for (int i = 0; i < size; i++) {
+            availableModeIds[i] = availableModes.get(i).getModeId();
+        }
+        return availableModeIds;
+    }
+
+    /**
+     * Gets the observer responsible for application display mode requests.
+     */
+    @NonNull
+    public AppRequestObserver getAppRequestObserver() {
+        // We don't need to lock here because mAppRequestObserver is a final field, which is
+        // guaranteed to be visible on all threads after construction.
+        return mAppRequestObserver;
+    }
+
+    /**
+     * Sets the listener for changes to allowed display modes.
+     */
+    public void setListener(@Nullable Listener listener) {
+        synchronized (mLock) {
+            mListener = listener;
+        }
+    }
+
+    /**
+     * Print the object's state and debug information into the given stream.
+     *
+     * @param pw The stream to dump information to.
+     */
+    public void dump(PrintWriter pw) {
+        pw.println("DisplayModeDirector");
+        synchronized (mLock) {
+            pw.println("  mSupportedModesByDisplay:");
+            for (int i = 0; i < mSupportedModesByDisplay.size(); i++) {
+                final int id = mSupportedModesByDisplay.keyAt(i);
+                final Display.Mode[] modes = mSupportedModesByDisplay.valueAt(i);
+                pw.println("    " + id + " -> " + Arrays.toString(modes));
+            }
+            pw.println("  mDefaultModeByDisplay:");
+            for (int i = 0; i < mDefaultModeByDisplay.size(); i++) {
+                final int id = mDefaultModeByDisplay.keyAt(i);
+                final Display.Mode mode = mDefaultModeByDisplay.valueAt(i);
+                pw.println("    " + id + " -> " + mode);
+            }
+            pw.println("  mVotesByDisplay:");
+            for (int i = 0; i < mVotesByDisplay.size(); i++) {
+                pw.println("    " + mVotesByDisplay.keyAt(i) + ":");
+                SparseArray<Vote> votes = mVotesByDisplay.valueAt(i);
+                for (int p = Vote.MAX_PRIORITY; p >= Vote.MIN_PRIORITY; p--) {
+                    Vote vote = votes.get(p);
+                    if (vote == null) {
+                        continue;
+                    }
+                    pw.println("      " + Vote.priorityToString(p) + " -> " + vote);
+                }
+            }
+            mSettingsObserver.dumpLocked(pw);
+            mAppRequestObserver.dumpLocked(pw);
+        }
+    }
+
+    private void updateVoteLocked(int priority, Vote vote) {
+        updateVoteLocked(GLOBAL_ID, priority, vote);
+    }
+
+    private void updateVoteLocked(int displayId, int priority, Vote vote) {
+        if (DEBUG) {
+            Slog.i(TAG, "updateVoteLocked(displayId=" + displayId
+                    + ", priority=" + Vote.priorityToString(priority)
+                    + ", vote=" + vote + ")");
+        }
+        if (priority < Vote.MIN_PRIORITY || priority > Vote.MAX_PRIORITY) {
+            Slog.w(TAG, "Received a vote with an invalid priority, ignoring:"
+                    + " priority=" + Vote.priorityToString(priority)
+                    + ", vote=" + vote, new Throwable());
+            return;
+        }
+        final SparseArray<Vote> votes = getOrCreateVotesByDisplay(displayId);
+
+        Vote currentVote = votes.get(priority);
+        if (vote != null) {
+            votes.put(priority, vote);
+        } else {
+            votes.remove(priority);
+        }
+
+        if (votes.size() == 0) {
+            if (DEBUG) {
+                Slog.i(TAG, "No votes left for display " + displayId + ", removing.");
+            }
+            mVotesByDisplay.remove(displayId);
+        }
+
+        notifyAllowedModesChangedLocked();
+    }
+
+    private void notifyAllowedModesChangedLocked() {
+        if (mListener != null && !mHandler.hasMessages(MSG_ALLOWED_MODES_CHANGED)) {
+            // We need to post this to a handler to avoid calling out while holding the lock
+            // since we know there are things that both listen for changes as well as provide
+            // information. If we did call out while holding the lock, then there's no guaranteed
+            // lock order and we run the real of risk deadlock.
+            Message msg = mHandler.obtainMessage(MSG_ALLOWED_MODES_CHANGED, mListener);
+            msg.sendToTarget();
+        }
+    }
+
+    private SparseArray<Vote> getOrCreateVotesByDisplay(int displayId) {
+        int index = mVotesByDisplay.indexOfKey(displayId);
+        if (mVotesByDisplay.indexOfKey(displayId) >= 0) {
+            return mVotesByDisplay.get(displayId);
+        } else {
+            SparseArray<Vote> votes = new SparseArray<>();
+            mVotesByDisplay.put(displayId, votes);
+            return votes;
+        }
+    }
+
+    /**
+     * Listens for changes to display mode coordination.
+     */
+    public interface Listener {
+        /**
+         * Called when the allowed display modes may have changed.
+         */
+        void onAllowedDisplayModesChanged();
+    }
+
+    private static final class DisplayModeDirectorHandler extends Handler {
+        DisplayModeDirectorHandler(Looper looper) {
+            super(looper, null, true /*async*/);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case MSG_ALLOWED_MODES_CHANGED:
+                    Listener listener = (Listener) msg.obj;
+                    listener.onAllowedDisplayModesChanged();
+                    break;
+            }
+        }
+    }
+
+    private static final class Vote {
+        public static final int PRIORITY_USER_SETTING = 0;
+        // We split the app request into two priorities in case we can satisfy one desire without
+        // the other.
+        public static final int PRIORITY_APP_REQUEST_REFRESH_RATE = 1;
+        public static final int PRIORITY_APP_REQUEST_SIZE = 2;
+        public static final int PRIORITY_LOW_POWER_MODE = 3;
+
+        // Whenever a new priority is added, remember to update MIN_PRIORITY and/or MAX_PRIORITY as
+        // appropriate, as well as priorityToString.
+
+        public static final int MIN_PRIORITY = PRIORITY_USER_SETTING;
+        public static final int MAX_PRIORITY = PRIORITY_LOW_POWER_MODE;
+
+        /**
+         * A value signifying an invalid width or height in a vote.
+         */
+        public static final int INVALID_SIZE = -1;
+
+        /**
+         * The requested width of the display in pixels, or INVALID_SIZE;
+         */
+        public final int width;
+        /**
+         * The requested height of the display in pixels, or INVALID_SIZE;
+         */
+        public final int height;
+
+        /**
+         * The lowest desired refresh rate.
+         */
+        public final float minRefreshRate;
+        /**
+         * The highest desired refresh rate.
+         */
+        public final float maxRefreshRate;
+
+        public static Vote forRefreshRates(float minRefreshRate, float maxRefreshRate) {
+            return new Vote(INVALID_SIZE, INVALID_SIZE, minRefreshRate, maxRefreshRate);
+        }
+
+        public static Vote forSize(int width, int height) {
+            return new Vote(width, height, 0f, Float.POSITIVE_INFINITY);
+        }
+
+        private Vote(int width, int height,
+                float minRefreshRate, float maxRefreshRate) {
+            this.width = width;
+            this.height = height;
+            this.minRefreshRate = minRefreshRate;
+            this.maxRefreshRate = maxRefreshRate;
+        }
+
+        public static String priorityToString(int priority) {
+            switch (priority) {
+                case PRIORITY_USER_SETTING:
+                    return "PRIORITY_USER_SETTING";
+                case PRIORITY_APP_REQUEST_REFRESH_RATE:
+                    return "PRIORITY_APP_REQUEST_REFRESH_RATE";
+                case PRIORITY_APP_REQUEST_SIZE:
+                    return "PRIORITY_APP_REQUEST_SIZE";
+                case PRIORITY_LOW_POWER_MODE:
+                    return "PRIORITY_LOW_POWER_MODE";
+                default:
+                    return Integer.toString(priority);
+            }
+        }
+
+        @Override
+        public String toString() {
+            return "Vote{"
+                + "width=" + width
+                + ", height=" + height
+                + ", minRefreshRate=" + minRefreshRate
+                + ", maxRefreshRate=" + maxRefreshRate
+                + "}";
+        }
+    }
+
+    private final class SettingsObserver extends ContentObserver {
+        private final Uri mRefreshRateSetting =
+                Settings.System.getUriFor(Settings.System.PEAK_REFRESH_RATE);
+        private final Uri mLowPowerModeSetting =
+                Settings.Global.getUriFor(Settings.Global.LOW_POWER_MODE);
+
+        private final Context mContext;
+        private final float mDefaultPeakRefreshRate;
+
+        SettingsObserver(@NonNull Context context, @NonNull Handler handler) {
+            super(handler);
+            mContext = context;
+            mDefaultPeakRefreshRate = (float) context.getResources().getInteger(
+                    R.integer.config_defaultPeakRefreshRate);
+        }
+
+        public void observe() {
+            final ContentResolver cr = mContext.getContentResolver();
+            cr.registerContentObserver(mRefreshRateSetting, false /*notifyDescendants*/, this,
+                    UserHandle.USER_SYSTEM);
+            cr.registerContentObserver(mLowPowerModeSetting, false /*notifyDescendants*/, this,
+                    UserHandle.USER_SYSTEM);
+            synchronized (mLock) {
+                updateRefreshRateSettingLocked();
+                updateLowPowerModeSettingLocked();
+            }
+        }
+
+        @Override
+        public void onChange(boolean selfChange, Uri uri, int userId) {
+            synchronized (mLock) {
+                if (mRefreshRateSetting.equals(uri)) {
+                    updateRefreshRateSettingLocked();
+                } else if (mLowPowerModeSetting.equals(uri)) {
+                    updateLowPowerModeSettingLocked();
+                }
+            }
+        }
+
+        private void updateLowPowerModeSettingLocked() {
+            boolean inLowPowerMode = Settings.Global.getInt(mContext.getContentResolver(),
+                    Settings.Global.LOW_POWER_MODE, 0 /*default*/) != 0;
+            final Vote vote;
+            if (inLowPowerMode) {
+                vote = Vote.forRefreshRates(0f, 60f);
+            } else {
+                vote = null;
+            }
+            updateVoteLocked(Vote.PRIORITY_LOW_POWER_MODE, vote);
+        }
+
+        private void updateRefreshRateSettingLocked() {
+            float peakRefreshRate = Settings.System.getFloat(mContext.getContentResolver(),
+                    Settings.System.PEAK_REFRESH_RATE, DEFAULT_REFRESH_RATE);
+            Vote vote = Vote.forRefreshRates(0f, peakRefreshRate);
+            updateVoteLocked(Vote.PRIORITY_USER_SETTING, vote);
+        }
+
+        public void dumpLocked(PrintWriter pw) {
+            pw.println("  SettingsObserver");
+            pw.println("    mDefaultPeakRefreshRate: " + mDefaultPeakRefreshRate);
+        }
+    }
+
+    final class AppRequestObserver {
+        private SparseArray<Display.Mode> mAppRequestedModeByDisplay;
+
+        AppRequestObserver() {
+            mAppRequestedModeByDisplay = new SparseArray<>();
+        }
+
+        public void setAppRequestedMode(int displayId, int modeId) {
+            synchronized (mLock) {
+                setAppRequestedModeLocked(displayId, modeId);
+            }
+        }
+
+        private void setAppRequestedModeLocked(int displayId, int modeId) {
+            final Display.Mode requestedMode = findModeByIdLocked(displayId, modeId);
+            if (Objects.equals(requestedMode, mAppRequestedModeByDisplay.get(displayId))) {
+                return;
+            }
+
+            final Vote refreshRateVote;
+            final Vote sizeVote;
+            if (requestedMode != null) {
+                mAppRequestedModeByDisplay.put(displayId, requestedMode);
+                float refreshRate = requestedMode.getRefreshRate();
+                refreshRateVote = Vote.forRefreshRates(refreshRate, refreshRate);
+                sizeVote = Vote.forSize(requestedMode.getPhysicalWidth(),
+                        requestedMode.getPhysicalHeight());
+            } else {
+                mAppRequestedModeByDisplay.remove(displayId);
+                refreshRateVote = null;
+                sizeVote = null;
+            }
+            updateVoteLocked(displayId, Vote.PRIORITY_APP_REQUEST_REFRESH_RATE, refreshRateVote);
+            updateVoteLocked(displayId, Vote.PRIORITY_APP_REQUEST_SIZE, sizeVote);
+            return;
+        }
+
+        private Display.Mode findModeByIdLocked(int displayId, int modeId) {
+            Display.Mode[] modes = mSupportedModesByDisplay.get(displayId);
+            if (modes == null) {
+                return null;
+            }
+            for (Display.Mode mode : modes) {
+                if (mode.getModeId() == modeId) {
+                    return mode;
+                }
+            }
+            return null;
+        }
+
+        public void dumpLocked(PrintWriter pw) {
+            pw.println("  AppRequestObserver");
+            pw.println("    mAppRequestedModeByDisplay:");
+            for (int i = 0; i < mAppRequestedModeByDisplay.size(); i++) {
+                final int id = mAppRequestedModeByDisplay.keyAt(i);
+                final Display.Mode mode = mAppRequestedModeByDisplay.valueAt(i);
+                pw.println("    " + id + " -> " + mode);
+            }
+        }
+    }
+
+    private final class DisplayObserver implements DisplayManager.DisplayListener {
+        // Note that we can never call into DisplayManager or any of the non-POD classes it
+        // returns, while holding mLock since it may call into DMS, which might be simultaneously
+        // calling into us already holding its own lock.
+        private final Context mContext;
+        private final Handler mHandler;
+
+        DisplayObserver(Context context, Handler handler) {
+            mContext = context;
+            mHandler = handler;
+        }
+
+        public void observe() {
+            DisplayManager dm = mContext.getSystemService(DisplayManager.class);
+            dm.registerDisplayListener(this, mHandler);
+
+            // Populate existing displays
+            SparseArray<Display.Mode[]> modes = new SparseArray<>();
+            SparseArray<Display.Mode> defaultModes = new SparseArray<>();
+            DisplayInfo info = new DisplayInfo();
+            Display[] displays = dm.getDisplays();
+            for (Display d : displays) {
+                final int displayId = d.getDisplayId();
+                d.getDisplayInfo(info);
+                modes.put(displayId, info.supportedModes);
+                defaultModes.put(displayId, info.getDefaultMode());
+            }
+            synchronized (mLock) {
+                final int size = modes.size();
+                for (int i = 0; i < size; i++) {
+                    mSupportedModesByDisplay.put(modes.keyAt(i), modes.valueAt(i));
+                    mDefaultModeByDisplay.put(defaultModes.keyAt(i), defaultModes.valueAt(i));
+                }
+            }
+        }
+
+        @Override
+        public void onDisplayAdded(int displayId) {
+            updateDisplayModes(displayId);
+        }
+
+        @Override
+        public void onDisplayRemoved(int displayId) {
+            synchronized (mLock) {
+                mSupportedModesByDisplay.remove(displayId);
+                mDefaultModeByDisplay.remove(displayId);
+            }
+        }
+
+        @Override
+        public void onDisplayChanged(int displayId) {
+            updateDisplayModes(displayId);
+        }
+
+        private void updateDisplayModes(int displayId) {
+            Display d = mContext.getSystemService(DisplayManager.class).getDisplay(displayId);
+            DisplayInfo info = new DisplayInfo();
+            d.getDisplayInfo(info);
+            boolean changed = false;
+            synchronized (mLock) {
+                if (!Arrays.equals(mSupportedModesByDisplay.get(displayId), info.supportedModes)) {
+                    mSupportedModesByDisplay.put(displayId, info.supportedModes);
+                    changed = true;
+                }
+                if (!Objects.equals(mDefaultModeByDisplay.get(displayId), info.getDefaultMode())) {
+                    changed = true;
+                    mDefaultModeByDisplay.put(displayId, info.getDefaultMode());
+                }
+                if (changed) {
+                    notifyAllowedModesChangedLocked();
+                }
+            }
+        }
+    }
+}
diff --git a/services/core/java/com/android/server/display/LocalDisplayAdapter.java b/services/core/java/com/android/server/display/LocalDisplayAdapter.java
index 77df10b..5e5ef26 100644
--- a/services/core/java/com/android/server/display/LocalDisplayAdapter.java
+++ b/services/core/java/com/android/server/display/LocalDisplayAdapter.java
@@ -112,16 +112,18 @@
                 activeColorMode = Display.COLOR_MODE_INVALID;
             }
             int[] colorModes = SurfaceControl.getDisplayColorModes(displayToken);
+            int[] allowedConfigs = SurfaceControl.getAllowedDisplayConfigs(displayToken);
             LocalDisplayDevice device = mDevices.get(physicalDisplayId);
             if (device == null) {
                 // Display was added.
                 final boolean isInternal = mDevices.size() == 0;
                 device = new LocalDisplayDevice(displayToken, physicalDisplayId,
-                        configs, activeConfig, colorModes, activeColorMode, isInternal);
+                        configs, activeConfig, allowedConfigs, colorModes, activeColorMode,
+                        isInternal);
                 mDevices.put(physicalDisplayId, device);
                 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_ADDED);
             } else if (device.updatePhysicalDisplayInfoLocked(configs, activeConfig,
-                        colorModes, activeColorMode)) {
+                        allowedConfigs, colorModes, activeColorMode)) {
                 // Display properties changed.
                 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_CHANGED);
             }
@@ -167,26 +169,30 @@
         private boolean mHavePendingChanges;
         private int mState = Display.STATE_UNKNOWN;
         private int mBrightness = PowerManager.BRIGHTNESS_DEFAULT;
-        private int mActivePhysIndex;
         private int mDefaultModeId;
         private int mActiveModeId;
         private boolean mActiveModeInvalid;
+        private int[] mAllowedModeIds;
+        private boolean mAllowedModeIdsInvalid;
+        private int mActivePhysIndex;
+        private int[] mAllowedPhysIndexes;
         private int mActiveColorMode;
         private boolean mActiveColorModeInvalid;
         private Display.HdrCapabilities mHdrCapabilities;
         private boolean mSidekickActive;
         private SidekickInternal mSidekickInternal;
 
-        private  SurfaceControl.PhysicalDisplayInfo mDisplayInfos[];
+        private SurfaceControl.PhysicalDisplayInfo[] mDisplayInfos;
 
         LocalDisplayDevice(IBinder displayToken, long physicalDisplayId,
                 SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo,
-                int[] colorModes, int activeColorMode, boolean isInternal) {
+                int[] allowedDisplayInfos, int[] colorModes, int activeColorMode,
+                boolean isInternal) {
             super(LocalDisplayAdapter.this, displayToken, UNIQUE_ID_PREFIX + physicalDisplayId);
             mPhysicalDisplayId = physicalDisplayId;
             mIsInternal = isInternal;
             updatePhysicalDisplayInfoLocked(physicalDisplayInfos, activeDisplayInfo,
-                    colorModes, activeColorMode);
+                    allowedDisplayInfos, colorModes, activeColorMode);
             updateColorModesLocked(colorModes, activeColorMode);
             mSidekickInternal = LocalServices.getService(SidekickInternal.class);
             if (mIsInternal) {
@@ -205,9 +211,10 @@
 
         public boolean updatePhysicalDisplayInfoLocked(
                 SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo,
-                int[] colorModes, int activeColorMode) {
+                int[] allowedDisplayInfos, int[] colorModes, int activeColorMode) {
             mDisplayInfos = Arrays.copyOf(physicalDisplayInfos, physicalDisplayInfos.length);
             mActivePhysIndex = activeDisplayInfo;
+            mAllowedPhysIndexes = Arrays.copyOf(allowedDisplayInfos, allowedDisplayInfos.length);
             // Build an updated list of all existing modes.
             ArrayList<DisplayModeRecord> records = new ArrayList<DisplayModeRecord>();
             boolean modesAdded = false;
@@ -246,8 +253,9 @@
                     break;
                 }
             }
-            // Check whether surface flinger spontaneously changed modes out from under us. Schedule
-            // traversals to ensure that the correct state is reapplied if necessary.
+
+            // Check whether surface flinger spontaneously changed modes out from under us.
+            // Schedule traversals to ensure that the correct state is reapplied if necessary.
             if (mActiveModeId != 0
                     && mActiveModeId != activeRecord.mMode.getModeId()) {
                 mActiveModeInvalid = true;
@@ -266,6 +274,7 @@
             for (DisplayModeRecord record : records) {
                 mSupportedModes.put(record.mMode.getModeId(), record);
             }
+
             // Update the default mode, if needed.
             if (findDisplayInfoIndexLocked(mDefaultModeId) < 0) {
                 if (mDefaultModeId != 0) {
@@ -274,6 +283,7 @@
                 }
                 mDefaultModeId = activeRecord.mMode.getModeId();
             }
+
             // Determine whether the active mode is still there.
             if (mSupportedModes.indexOfKey(mActiveModeId) < 0) {
                 if (mActiveModeId != 0) {
@@ -284,6 +294,21 @@
                 mActiveModeInvalid = true;
             }
 
+            // Determine what the currently allowed modes are
+            mAllowedModeIds = new int[] { mActiveModeId };
+            int[] allowedModeIds = new int[mAllowedPhysIndexes.length];
+            int size = 0;
+            for (int physIndex : mAllowedPhysIndexes) {
+                int modeId = findMatchingModeIdLocked(physIndex);
+                if (modeId > 0) {
+                    allowedModeIds[size++] = modeId;
+                }
+            }
+
+            // If this is different from our desired allowed modes, then mark our current set as
+            // invalid so we correct this on the next traversal.
+            mAllowedModeIdsInvalid = !Arrays.equals(allowedModeIds, mAllowedModeIds);
+
             // Schedule traversals so that we apply pending changes.
             sendTraversalRequestLocked();
             return true;
@@ -368,11 +393,7 @@
                 mInfo.height = phys.height;
                 mInfo.modeId = mActiveModeId;
                 mInfo.defaultModeId = mDefaultModeId;
-                mInfo.supportedModes = new Display.Mode[mSupportedModes.size()];
-                for (int i = 0; i < mSupportedModes.size(); i++) {
-                    DisplayModeRecord record = mSupportedModes.valueAt(i);
-                    mInfo.supportedModes[i] = record.mMode;
-                }
+                mInfo.supportedModes = getDisplayModes(mSupportedModes);
                 mInfo.colorMode = mActiveColorMode;
                 mInfo.supportedColorModes =
                         new int[mSupportedColorModes.size()];
@@ -593,44 +614,104 @@
         }
 
         @Override
-        public void requestDisplayModesLocked(int colorMode, int modeId) {
-            if (requestModeLocked(modeId) ||
-                    requestColorModeLocked(colorMode)) {
+        public void setRequestedColorModeLocked(int colorMode) {
+            if (requestColorModeLocked(colorMode)) {
                 updateDeviceInfoLocked();
             }
         }
 
         @Override
+        public void setAllowedDisplayModesLocked(int[] modes) {
+            updateAllowedModesLocked(modes);
+        }
+
+        @Override
         public void onOverlayChangedLocked() {
             updateDeviceInfoLocked();
         }
 
-        public boolean requestModeLocked(int modeId) {
-            if (modeId == 0) {
-                modeId = mDefaultModeId;
-            } else if (mSupportedModes.indexOfKey(modeId) < 0) {
-                Slog.w(TAG, "Requested mode " + modeId + " is not supported by this display,"
-                        + " reverting to default display mode.");
-                modeId = mDefaultModeId;
+        public void onActivePhysicalDisplayModeChangedLocked(int physIndex) {
+            if (updateActiveModeLocked(physIndex)) {
+                updateDeviceInfoLocked();
             }
+        }
 
-            int physIndex = findDisplayInfoIndexLocked(modeId);
-            if (physIndex < 0) {
-                Slog.w(TAG, "Requested mode ID " + modeId + " not available,"
-                        + " trying with default mode ID");
-                modeId = mDefaultModeId;
-                physIndex = findDisplayInfoIndexLocked(modeId);
-            }
-            if (mActivePhysIndex == physIndex) {
+        public boolean updateActiveModeLocked(int activePhysIndex) {
+            if (mActivePhysIndex == activePhysIndex) {
                 return false;
             }
-            SurfaceControl.setActiveConfig(getDisplayTokenLocked(), physIndex);
-            mActivePhysIndex = physIndex;
-            mActiveModeId = modeId;
-            mActiveModeInvalid = false;
+            mActivePhysIndex = activePhysIndex;
+            mActiveModeId = findMatchingModeIdLocked(activePhysIndex);
+            mActiveModeInvalid = mActiveModeId == 0;
+            if (mActiveModeInvalid) {
+                Slog.w(TAG, "In unknown mode after setting allowed configs"
+                        + ": allowedPhysIndexes=" + mAllowedPhysIndexes
+                        + ", activePhysIndex=" + mActivePhysIndex);
+            }
             return true;
         }
 
+        public void updateAllowedModesLocked(int[] allowedModes) {
+            if (Arrays.equals(allowedModes, mAllowedModeIds) && !mAllowedModeIdsInvalid) {
+                return;
+            }
+            if (updateAllowedModesInternalLocked(allowedModes)) {
+                updateDeviceInfoLocked();
+            }
+        }
+
+        public boolean updateAllowedModesInternalLocked(int[] allowedModes) {
+            if (DEBUG) {
+                Slog.w(TAG, "updateAllowedModesInternalLocked(allowedModes="
+                        + Arrays.toString(allowedModes) + ")");
+            }
+            int[] allowedPhysIndexes = new int[allowedModes.length];
+            int size = 0;
+            for (int modeId : allowedModes) {
+                int physIndex = findDisplayInfoIndexLocked(modeId);
+                if (physIndex < 0) {
+                    Slog.w(TAG, "Requested mode ID " + modeId + " not available,"
+                            + " dropping from allowed set.");
+                } else {
+                    allowedPhysIndexes[size++] = physIndex;
+                }
+            }
+
+            // If we couldn't find one or more of the suggested allowed modes then we need to
+            // shrink the array to its actual size.
+            if (size != allowedModes.length) {
+                allowedPhysIndexes = Arrays.copyOf(allowedPhysIndexes, size);
+            }
+
+            // If we found no suitable modes, then we try again with the default mode which we
+            // assume has a suitable physical config.
+            if (size == 0) {
+                if (DEBUG) {
+                    Slog.w(TAG, "No valid modes allowed, falling back to default mode (id="
+                            + mDefaultModeId + ")");
+                }
+                allowedModes = new int[] { mDefaultModeId };
+                allowedPhysIndexes = new int[] { findDisplayInfoIndexLocked(mDefaultModeId) };
+            }
+
+            mAllowedModeIds = allowedModes;
+            mAllowedModeIdsInvalid = false;
+
+            if (Arrays.equals(mAllowedPhysIndexes, allowedPhysIndexes)) {
+                return false;
+            }
+            mAllowedPhysIndexes = allowedPhysIndexes;
+
+            if (DEBUG) {
+                Slog.w(TAG, "Setting allowed physical configs: allowedPhysIndexes="
+                        + Arrays.toString(allowedPhysIndexes));
+            }
+
+            SurfaceControl.setAllowedDisplayConfigs(getDisplayTokenLocked(), allowedPhysIndexes);
+            int activePhysIndex = SurfaceControl.getActiveConfig(getDisplayTokenLocked());
+            return updateActiveModeLocked(activePhysIndex);
+        }
+
         public boolean requestColorModeLocked(int colorMode) {
             if (mActiveColorMode == colorMode) {
                 return false;
@@ -650,9 +731,13 @@
         public void dumpLocked(PrintWriter pw) {
             super.dumpLocked(pw);
             pw.println("mPhysicalDisplayId=" + mPhysicalDisplayId);
+            pw.println("mAllowedPhysIndexes=" + Arrays.toString(mAllowedPhysIndexes));
+            pw.println("mAllowedModeIds=" + Arrays.toString(mAllowedModeIds));
+            pw.println("mAllowedModeIdsInvalid=" + mAllowedModeIdsInvalid);
             pw.println("mActivePhysIndex=" + mActivePhysIndex);
             pw.println("mActiveModeId=" + mActiveModeId);
             pw.println("mActiveColorMode=" + mActiveColorMode);
+            pw.println("mDefaultModeId=" + mDefaultModeId);
             pw.println("mState=" + Display.stateToString(mState));
             pw.println("mBrightness=" + mBrightness);
             pw.println("mBacklight=" + mBacklight);
@@ -687,10 +772,31 @@
             return -1;
         }
 
+        private int findMatchingModeIdLocked(int physIndex) {
+            SurfaceControl.PhysicalDisplayInfo info = mDisplayInfos[physIndex];
+            for (int i = 0; i < mSupportedModes.size(); i++) {
+                DisplayModeRecord record = mSupportedModes.valueAt(i);
+                if (record.hasMatchingMode(info)) {
+                    return record.mMode.getModeId();
+                }
+            }
+            return 0;
+        }
+
         private void updateDeviceInfoLocked() {
             mInfo = null;
             sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED);
         }
+
+        private Display.Mode[] getDisplayModes(SparseArray<DisplayModeRecord> records) {
+            final int size = records.size();
+            Display.Mode[] modes = new Display.Mode[size];
+            for (int i = 0; i < size; i++) {
+                DisplayModeRecord record = records.valueAt(i);
+                modes[i] = record.mMode;
+            }
+            return modes;
+        }
     }
 
     /** Supplies a context whose Resources apply runtime-overlays */
@@ -745,12 +851,23 @@
         }
 
         @Override
-        public void onConfigChanged(long timestampNanos, long physicalDisplayId, int configId) {
+        public void onConfigChanged(long timestampNanos, long physicalDisplayId, int physIndex) {
             if (DEBUG) {
                 Slog.d(TAG, "onConfigChanged("
                         + "timestampNanos=" + timestampNanos
-                        + ", builtInDisplayId=" + physicalDisplayId
-                        + ", configId=" + configId + ")");
+                        + ", physicalDisplayId=" + physicalDisplayId
+                        + ", physIndex=" + physIndex + ")");
+            }
+            synchronized (getSyncRoot()) {
+                LocalDisplayDevice device = mDevices.get(physicalDisplayId);
+                if (device == null) {
+                    if (DEBUG) {
+                        Slog.d(TAG, "Received config change for unhandled physical display: "
+                                + "physicalDisplayId=" + physicalDisplayId);
+                    }
+                    return;
+                }
+                device.onActivePhysicalDisplayModeChangedLocked(physIndex);
             }
         }
     }
diff --git a/services/core/java/com/android/server/display/LogicalDisplay.java b/services/core/java/com/android/server/display/LogicalDisplay.java
index 7ee8422..ca9bbd1 100644
--- a/services/core/java/com/android/server/display/LogicalDisplay.java
+++ b/services/core/java/com/android/server/display/LogicalDisplay.java
@@ -87,7 +87,7 @@
     // True if the logical display has unique content.
     private boolean mHasContent;
 
-    private int mRequestedModeId;
+    private int[] mAllowedDisplayModes = new int[0];
     private int mRequestedColorMode;
 
     // The display offsets to apply to the display projection.
@@ -353,12 +353,14 @@
         // Set the layer stack.
         device.setLayerStackLocked(t, isBlanked ? BLANK_LAYER_STACK : mLayerStack);
 
-        // Set the color mode and mode.
+        // Set the color mode and allowed display mode.
         if (device == mPrimaryDisplayDevice) {
-            device.requestDisplayModesLocked(
-                    mRequestedColorMode, mRequestedModeId);
+            device.setAllowedDisplayModesLocked(mAllowedDisplayModes);
+            device.setRequestedColorModeLocked(mRequestedColorMode);
         } else {
-            device.requestDisplayModesLocked(0, 0);  // Revert to default.
+            // Reset to default for non primary displays
+            device.setAllowedDisplayModesLocked(new int[] {0});
+            device.setRequestedColorModeLocked(0);
         }
 
         // Only grab the display info now as it may have been changed based on the requests above.
@@ -462,17 +464,17 @@
     }
 
     /**
-     * Requests the given mode.
+     * Sets the display modes the system is free to switch between.
      */
-    public void setRequestedModeIdLocked(int modeId) {
-        mRequestedModeId = modeId;
+    public void setAllowedDisplayModesLocked(int[] modes) {
+        mAllowedDisplayModes = modes;
     }
 
     /**
-     * Returns the pending requested mode.
+     * Returns the display modes the system is free to switch between.
      */
-    public int getRequestedModeIdLocked() {
-        return mRequestedModeId;
+    public int[] getAllowedDisplayModesLocked() {
+        return mAllowedDisplayModes;
     }
 
     /**
@@ -531,7 +533,7 @@
         pw.println("mDisplayId=" + mDisplayId);
         pw.println("mLayerStack=" + mLayerStack);
         pw.println("mHasContent=" + mHasContent);
-        pw.println("mRequestedMode=" + mRequestedModeId);
+        pw.println("mAllowedDisplayModes=" + Arrays.toString(mAllowedDisplayModes));
         pw.println("mRequestedColorMode=" + mRequestedColorMode);
         pw.println("mDisplayOffset=(" + mDisplayOffsetX + ", " + mDisplayOffsetY + ")");
         pw.println("mDisplayScalingDisabled=" + mDisplayScalingDisabled);
diff --git a/services/core/java/com/android/server/display/OverlayDisplayAdapter.java b/services/core/java/com/android/server/display/OverlayDisplayAdapter.java
index 2f507d1..60cfbd0 100644
--- a/services/core/java/com/android/server/display/OverlayDisplayAdapter.java
+++ b/services/core/java/com/android/server/display/OverlayDisplayAdapter.java
@@ -16,9 +16,6 @@
 
 package com.android.server.display;
 
-import com.android.internal.util.DumpUtils;
-import com.android.internal.util.IndentingPrintWriter;
-
 import android.content.Context;
 import android.database.ContentObserver;
 import android.graphics.SurfaceTexture;
@@ -32,6 +29,9 @@
 import android.view.Surface;
 import android.view.SurfaceControl;
 
+import com.android.internal.util.DumpUtils;
+import com.android.internal.util.IndentingPrintWriter;
+
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -315,7 +315,16 @@
         }
 
         @Override
-        public void requestDisplayModesLocked(int color, int id) {
+        public void setAllowedDisplayModesLocked(int[] modes) {
+            final int id;
+            if (modes.length > 0) {
+                // The allowed modes should be ordered by preference, so just use the first mode
+                // here.
+                id = modes[0];
+            } else {
+                // If we don't have any allowed modes, just use the default mode.
+                id = 0;
+            }
             int index = -1;
             if (id == 0) {
                 // Use the default.