blob: 5e5ef26f66244d4394b8732bd3eaa11eeae8129b [file] [log] [blame]
Jeff Brown64a55af2012-08-26 02:47:39 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.display;
18
Adrian Roos30f53212018-01-05 16:14:34 +010019import android.app.ActivityThread;
Jeff Brown64a55af2012-08-26 02:47:39 -070020import android.content.Context;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080021import android.content.res.Resources;
Chris Phoenixbc839a32017-10-21 14:46:15 -070022import android.hardware.sidekick.SidekickInternal;
Griff Hazend3c454d2016-03-25 07:30:34 -070023import android.os.Build;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070024import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070025import android.os.IBinder;
Jeff Browne87bf032012-09-20 18:30:13 -070026import android.os.Looper;
Jeff Brown5d6443b2015-04-10 20:15:01 -070027import android.os.PowerManager;
Jeff Brown27f1d672012-10-17 18:32:34 -070028import android.os.SystemProperties;
Jeff Brown3edf5272014-08-14 19:25:14 -070029import android.os.Trace;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080030import android.util.LongSparseArray;
Dan Stoza00101052014-05-02 15:23:40 -070031import android.util.Slog;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070032import android.util.SparseArray;
Jeff Brown92130f62012-10-24 21:28:33 -070033import android.view.Display;
Dominik Laskowskidb845962019-01-27 21:20:00 -080034import android.view.DisplayAddress;
Adrian Roos1cf585052018-01-03 18:43:27 +010035import android.view.DisplayCutout;
Jeff Browne87bf032012-09-20 18:30:13 -070036import android.view.DisplayEventReceiver;
Jeff Brown64a55af2012-08-26 02:47:39 -070037import android.view.Surface;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080038import android.view.SurfaceControl;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080039
40import com.android.server.LocalServices;
41import com.android.server.lights.Light;
42import com.android.server.lights.LightsManager;
43
Jeff Brown4ed8fe72012-08-30 18:18:29 -070044import java.io.PrintWriter;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -070045import java.util.ArrayList;
Michael Wright58e829f2015-09-15 00:13:26 +010046import java.util.Arrays;
Michael Wright1c9977b2016-07-12 13:30:10 -070047import java.util.Collections;
48import java.util.List;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070049
Jeff Brown64a55af2012-08-26 02:47:39 -070050/**
51 * A display adapter for the local displays managed by Surface Flinger.
Jeff Brownbd6e1502012-08-28 03:27:37 -070052 * <p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -070053 * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock.
Jeff Brownbd6e1502012-08-28 03:27:37 -070054 * </p>
Jeff Brown64a55af2012-08-26 02:47:39 -070055 */
Jeff Brown4ed8fe72012-08-30 18:18:29 -070056final class LocalDisplayAdapter extends DisplayAdapter {
Jeff Brownbd6e1502012-08-28 03:27:37 -070057 private static final String TAG = "LocalDisplayAdapter";
Jeff Brown5d6443b2015-04-10 20:15:01 -070058 private static final boolean DEBUG = false;
Jeff Brown64a55af2012-08-26 02:47:39 -070059
Wale Ogunwale361ca212014-11-20 11:42:38 -080060 private static final String UNIQUE_ID_PREFIX = "local:";
61
Adam Powell01f280d2015-05-18 16:07:42 -070062 private static final String PROPERTY_EMULATOR_CIRCULAR = "ro.emulator.circular";
63
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080064 private final LongSparseArray<LocalDisplayDevice> mDevices =
65 new LongSparseArray<LocalDisplayDevice>();
Jeff Brown4ed8fe72012-08-30 18:18:29 -070066
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -070067 @SuppressWarnings("unused") // Becomes active at instantiation time.
Ady Abrahama5a21f72019-02-13 16:41:59 -080068 private PhysicalDisplayEventReceiver mPhysicalDisplayEventReceiver;
69
Jeff Brown4ed8fe72012-08-30 18:18:29 -070070
Jeff Brown66692502012-10-18 16:13:44 -070071 // Called with SyncRoot lock held.
Jeff Brown4ed8fe72012-08-30 18:18:29 -070072 public LocalDisplayAdapter(DisplayManagerService.SyncRoot syncRoot,
73 Context context, Handler handler, Listener listener) {
74 super(syncRoot, context, handler, listener, TAG);
Jeff Brown64a55af2012-08-26 02:47:39 -070075 }
76
77 @Override
Jeff Brown4ed8fe72012-08-30 18:18:29 -070078 public void registerLocked() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -070079 super.registerLocked();
Jeff Brown66692502012-10-18 16:13:44 -070080
Ady Abrahama5a21f72019-02-13 16:41:59 -080081 mPhysicalDisplayEventReceiver = new PhysicalDisplayEventReceiver(getHandler().getLooper());
Jesse Halle244db42012-11-08 11:55:14 -080082
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080083 for (long physicalDisplayId : SurfaceControl.getPhysicalDisplayIds()) {
84 tryConnectDisplayLocked(physicalDisplayId);
Jesse Halle244db42012-11-08 11:55:14 -080085 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -070086 }
87
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080088 private void tryConnectDisplayLocked(long physicalDisplayId) {
89 final IBinder displayToken = SurfaceControl.getPhysicalDisplayToken(physicalDisplayId);
Dan Stoza00101052014-05-02 15:23:40 -070090 if (displayToken != null) {
91 SurfaceControl.PhysicalDisplayInfo[] configs =
92 SurfaceControl.getDisplayConfigs(displayToken);
93 if (configs == null) {
94 // There are no valid configs for this device, so we can't use it
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080095 Slog.w(TAG, "No valid configs found for display device " + physicalDisplayId);
Dan Stoza00101052014-05-02 15:23:40 -070096 return;
97 }
98 int activeConfig = SurfaceControl.getActiveConfig(displayToken);
99 if (activeConfig < 0) {
100 // There is no active config, and for now we don't have the
101 // policy to set one.
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800102 Slog.w(TAG, "No active config found for display device " + physicalDisplayId);
Dan Stoza00101052014-05-02 15:23:40 -0700103 return;
104 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700105 int activeColorMode = SurfaceControl.getActiveColorMode(displayToken);
106 if (activeColorMode < 0) {
107 // We failed to get the active color mode. We don't bail out here since on the next
108 // configuration pass we'll go ahead and set it to whatever it was set to last (or
109 // COLOR_MODE_NATIVE if this is the first configuration).
110 Slog.w(TAG, "Unable to get active color mode for display device " +
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800111 physicalDisplayId);
Michael Wright1c9977b2016-07-12 13:30:10 -0700112 activeColorMode = Display.COLOR_MODE_INVALID;
113 }
114 int[] colorModes = SurfaceControl.getDisplayColorModes(displayToken);
Michael Wrighta3dab232019-02-22 16:54:21 +0000115 int[] allowedConfigs = SurfaceControl.getAllowedDisplayConfigs(displayToken);
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800116 LocalDisplayDevice device = mDevices.get(physicalDisplayId);
Jesse Halle244db42012-11-08 11:55:14 -0800117 if (device == null) {
118 // Display was added.
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800119 final boolean isInternal = mDevices.size() == 0;
120 device = new LocalDisplayDevice(displayToken, physicalDisplayId,
Michael Wrighta3dab232019-02-22 16:54:21 +0000121 configs, activeConfig, allowedConfigs, colorModes, activeColorMode,
122 isInternal);
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800123 mDevices.put(physicalDisplayId, device);
Jesse Halle244db42012-11-08 11:55:14 -0800124 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700125 } else if (device.updatePhysicalDisplayInfoLocked(configs, activeConfig,
Michael Wrighta3dab232019-02-22 16:54:21 +0000126 allowedConfigs, colorModes, activeColorMode)) {
Jesse Halle244db42012-11-08 11:55:14 -0800127 // Display properties changed.
128 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_CHANGED);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700129 }
Jesse Halle244db42012-11-08 11:55:14 -0800130 } else {
131 // The display is no longer available. Ignore the attempt to add it.
132 // If it was connected but has already been disconnected, we'll get a
133 // disconnect event that will remove it from mDevices.
134 }
135 }
136
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800137 private void tryDisconnectDisplayLocked(long physicalDisplayId) {
138 LocalDisplayDevice device = mDevices.get(physicalDisplayId);
Jesse Halle244db42012-11-08 11:55:14 -0800139 if (device != null) {
140 // Display was removed.
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800141 mDevices.remove(physicalDisplayId);
Jesse Halle244db42012-11-08 11:55:14 -0800142 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_REMOVED);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700143 }
Jeff Brown64a55af2012-08-26 02:47:39 -0700144 }
145
Prashant Malanic55929a2014-05-25 01:59:21 -0700146 static int getPowerModeForState(int state) {
147 switch (state) {
148 case Display.STATE_OFF:
149 return SurfaceControl.POWER_MODE_OFF;
Jeff Brown5dc21912014-07-17 18:50:18 -0700150 case Display.STATE_DOZE:
Prashant Malanic55929a2014-05-25 01:59:21 -0700151 return SurfaceControl.POWER_MODE_DOZE;
Jeff Brown5dc21912014-07-17 18:50:18 -0700152 case Display.STATE_DOZE_SUSPEND:
153 return SurfaceControl.POWER_MODE_DOZE_SUSPEND;
Chris Phoenix10a4a642017-09-25 13:21:00 -0700154 case Display.STATE_ON_SUSPEND:
155 return SurfaceControl.POWER_MODE_ON_SUSPEND;
Prashant Malanic55929a2014-05-25 01:59:21 -0700156 default:
157 return SurfaceControl.POWER_MODE_NORMAL;
158 }
Jeff Brown44b1f762014-04-22 18:07:24 -0700159 }
160
Jeff Brown64a55af2012-08-26 02:47:39 -0700161 private final class LocalDisplayDevice extends DisplayDevice {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800162 private final long mPhysicalDisplayId;
Jeff Brown5d6443b2015-04-10 20:15:01 -0700163 private final Light mBacklight;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700164 private final SparseArray<DisplayModeRecord> mSupportedModes = new SparseArray<>();
Michael Wright1c9977b2016-07-12 13:30:10 -0700165 private final ArrayList<Integer> mSupportedColorModes = new ArrayList<>();
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800166 private final boolean mIsInternal;
Jeff Brown64a55af2012-08-26 02:47:39 -0700167
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700168 private DisplayDeviceInfo mInfo;
169 private boolean mHavePendingChanges;
Jeff Brown037c33e2014-04-09 00:31:55 -0700170 private int mState = Display.STATE_UNKNOWN;
Jeff Brown5d6443b2015-04-10 20:15:01 -0700171 private int mBrightness = PowerManager.BRIGHTNESS_DEFAULT;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700172 private int mDefaultModeId;
173 private int mActiveModeId;
174 private boolean mActiveModeInvalid;
Michael Wrighta3dab232019-02-22 16:54:21 +0000175 private int[] mAllowedModeIds;
176 private boolean mAllowedModeIdsInvalid;
177 private int mActivePhysIndex;
178 private int[] mAllowedPhysIndexes;
Michael Wright1c9977b2016-07-12 13:30:10 -0700179 private int mActiveColorMode;
180 private boolean mActiveColorModeInvalid;
Michael Wright9ff94c02016-03-30 18:05:40 -0700181 private Display.HdrCapabilities mHdrCapabilities;
Chris Phoenixbc839a32017-10-21 14:46:15 -0700182 private boolean mSidekickActive;
183 private SidekickInternal mSidekickInternal;
Michael Wright58e829f2015-09-15 00:13:26 +0100184
Michael Wrighta3dab232019-02-22 16:54:21 +0000185 private SurfaceControl.PhysicalDisplayInfo[] mDisplayInfos;
Jeff Brown5d6443b2015-04-10 20:15:01 -0700186
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800187 LocalDisplayDevice(IBinder displayToken, long physicalDisplayId,
Michael Wright1c9977b2016-07-12 13:30:10 -0700188 SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo,
Michael Wrighta3dab232019-02-22 16:54:21 +0000189 int[] allowedDisplayInfos, int[] colorModes, int activeColorMode,
190 boolean isInternal) {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800191 super(LocalDisplayAdapter.this, displayToken, UNIQUE_ID_PREFIX + physicalDisplayId);
192 mPhysicalDisplayId = physicalDisplayId;
193 mIsInternal = isInternal;
Michael Wright1c9977b2016-07-12 13:30:10 -0700194 updatePhysicalDisplayInfoLocked(physicalDisplayInfos, activeDisplayInfo,
Michael Wrighta3dab232019-02-22 16:54:21 +0000195 allowedDisplayInfos, colorModes, activeColorMode);
Michael Wright1c9977b2016-07-12 13:30:10 -0700196 updateColorModesLocked(colorModes, activeColorMode);
Chris Phoenixbc839a32017-10-21 14:46:15 -0700197 mSidekickInternal = LocalServices.getService(SidekickInternal.class);
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800198 if (mIsInternal) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700199 LightsManager lights = LocalServices.getService(LightsManager.class);
200 mBacklight = lights.getLight(LightsManager.LIGHT_ID_BACKLIGHT);
201 } else {
202 mBacklight = null;
203 }
Michael Wright9ff94c02016-03-30 18:05:40 -0700204 mHdrCapabilities = SurfaceControl.getHdrCapabilities(displayToken);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700205 }
206
Michael Wright1c9977b2016-07-12 13:30:10 -0700207 @Override
208 public boolean hasStableUniqueId() {
209 return true;
210 }
211
Michael Wright3f145a22014-07-22 19:46:03 -0700212 public boolean updatePhysicalDisplayInfoLocked(
Michael Wright1c9977b2016-07-12 13:30:10 -0700213 SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo,
Michael Wrighta3dab232019-02-22 16:54:21 +0000214 int[] allowedDisplayInfos, int[] colorModes, int activeColorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100215 mDisplayInfos = Arrays.copyOf(physicalDisplayInfos, physicalDisplayInfos.length);
216 mActivePhysIndex = activeDisplayInfo;
Michael Wrighta3dab232019-02-22 16:54:21 +0000217 mAllowedPhysIndexes = Arrays.copyOf(allowedDisplayInfos, allowedDisplayInfos.length);
Michael Wright58e829f2015-09-15 00:13:26 +0100218 // Build an updated list of all existing modes.
219 ArrayList<DisplayModeRecord> records = new ArrayList<DisplayModeRecord>();
220 boolean modesAdded = false;
221 for (int i = 0; i < physicalDisplayInfos.length; i++) {
222 SurfaceControl.PhysicalDisplayInfo info = physicalDisplayInfos[i];
223 // First, check to see if we've already added a matching mode. Since not all
224 // configuration options are exposed via Display.Mode, it's possible that we have
225 // multiple PhysicalDisplayInfos that would generate the same Display.Mode.
226 boolean existingMode = false;
227 for (int j = 0; j < records.size(); j++) {
228 if (records.get(j).hasMatchingMode(info)) {
229 existingMode = true;
230 break;
231 }
232 }
233 if (existingMode) {
234 continue;
235 }
236 // If we haven't already added a mode for this configuration to the new set of
237 // supported modes then check to see if we have one in the prior set of supported
238 // modes to reuse.
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700239 DisplayModeRecord record = findDisplayModeRecord(info);
Michael Wright58e829f2015-09-15 00:13:26 +0100240 if (record == null) {
241 record = new DisplayModeRecord(info);
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700242 modesAdded = true;
243 }
244 records.add(record);
Michael Wright58e829f2015-09-15 00:13:26 +0100245 }
246
247 // Get the currently active mode
248 DisplayModeRecord activeRecord = null;
249 for (int i = 0; i < records.size(); i++) {
250 DisplayModeRecord record = records.get(i);
251 if (record.hasMatchingMode(physicalDisplayInfos[activeDisplayInfo])){
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700252 activeRecord = record;
Michael Wright58e829f2015-09-15 00:13:26 +0100253 break;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700254 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700255 }
Michael Wrighta3dab232019-02-22 16:54:21 +0000256
257 // Check whether surface flinger spontaneously changed modes out from under us.
258 // Schedule traversals to ensure that the correct state is reapplied if necessary.
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700259 if (mActiveModeId != 0
260 && mActiveModeId != activeRecord.mMode.getModeId()) {
261 mActiveModeInvalid = true;
262 sendTraversalRequestLocked();
263 }
Michael Wright58e829f2015-09-15 00:13:26 +0100264
Michael Wright58e829f2015-09-15 00:13:26 +0100265 boolean recordsChanged = records.size() != mSupportedModes.size() || modesAdded;
Michael Wright1c9977b2016-07-12 13:30:10 -0700266 // If the records haven't changed then we're done here.
267 if (!recordsChanged) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700268 return false;
269 }
270 // Update the index of modes.
271 mHavePendingChanges = true;
Michael Wright58e829f2015-09-15 00:13:26 +0100272
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700273 mSupportedModes.clear();
274 for (DisplayModeRecord record : records) {
275 mSupportedModes.put(record.mMode.getModeId(), record);
276 }
Michael Wrighta3dab232019-02-22 16:54:21 +0000277
Michael Wright1c9977b2016-07-12 13:30:10 -0700278 // Update the default mode, if needed.
279 if (findDisplayInfoIndexLocked(mDefaultModeId) < 0) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700280 if (mDefaultModeId != 0) {
Michael Wright58e829f2015-09-15 00:13:26 +0100281 Slog.w(TAG, "Default display mode no longer available, using currently"
282 + " active mode as default.");
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700283 }
284 mDefaultModeId = activeRecord.mMode.getModeId();
285 }
Michael Wrighta3dab232019-02-22 16:54:21 +0000286
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700287 // Determine whether the active mode is still there.
288 if (mSupportedModes.indexOfKey(mActiveModeId) < 0) {
289 if (mActiveModeId != 0) {
290 Slog.w(TAG, "Active display mode no longer available, reverting to default"
291 + " mode.");
292 }
293 mActiveModeId = mDefaultModeId;
294 mActiveModeInvalid = true;
295 }
Michael Wright58e829f2015-09-15 00:13:26 +0100296
Michael Wrighta3dab232019-02-22 16:54:21 +0000297 // Determine what the currently allowed modes are
298 mAllowedModeIds = new int[] { mActiveModeId };
299 int[] allowedModeIds = new int[mAllowedPhysIndexes.length];
300 int size = 0;
301 for (int physIndex : mAllowedPhysIndexes) {
302 int modeId = findMatchingModeIdLocked(physIndex);
303 if (modeId > 0) {
304 allowedModeIds[size++] = modeId;
305 }
306 }
307
308 // If this is different from our desired allowed modes, then mark our current set as
309 // invalid so we correct this on the next traversal.
310 mAllowedModeIdsInvalid = !Arrays.equals(allowedModeIds, mAllowedModeIds);
311
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700312 // Schedule traversals so that we apply pending changes.
313 sendTraversalRequestLocked();
314 return true;
315 }
316
Michael Wright1c9977b2016-07-12 13:30:10 -0700317 private boolean updateColorModesLocked(int[] colorModes,
318 int activeColorMode) {
319 List<Integer> pendingColorModes = new ArrayList<>();
320
thecrazyskullb34c81042016-12-18 12:48:20 -0500321 if (colorModes == null) return false;
Michael Wright1c9977b2016-07-12 13:30:10 -0700322 // Build an updated list of all existing color modes.
323 boolean colorModesAdded = false;
324 for (int colorMode: colorModes) {
325 if (!mSupportedColorModes.contains(colorMode)) {
326 colorModesAdded = true;
327 }
328 pendingColorModes.add(colorMode);
329 }
330
331 boolean colorModesChanged =
332 pendingColorModes.size() != mSupportedColorModes.size()
333 || colorModesAdded;
334
335 // If the supported color modes haven't changed then we're done here.
336 if (!colorModesChanged) {
337 return false;
338 }
339
340 mHavePendingChanges = true;
341
342 mSupportedColorModes.clear();
343 mSupportedColorModes.addAll(pendingColorModes);
344 Collections.sort(mSupportedColorModes);
345
346 // Determine whether the active color mode is still there.
347 if (!mSupportedColorModes.contains(mActiveColorMode)) {
348 if (mActiveColorMode != 0) {
349 Slog.w(TAG, "Active color mode no longer available, reverting"
350 + " to default mode.");
351 mActiveColorMode = Display.COLOR_MODE_DEFAULT;
352 mActiveColorModeInvalid = true;
353 } else {
354 if (!mSupportedColorModes.isEmpty()) {
355 // This should never happen.
356 Slog.e(TAG, "Default and active color mode is no longer available!"
357 + " Reverting to first available mode.");
358 mActiveColorMode = mSupportedColorModes.get(0);
359 mActiveColorModeInvalid = true;
360 } else {
361 // This should really never happen.
362 Slog.e(TAG, "No color modes available!");
363 }
364 }
365 }
366 return true;
367 }
368
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700369 private DisplayModeRecord findDisplayModeRecord(SurfaceControl.PhysicalDisplayInfo info) {
370 for (int i = 0; i < mSupportedModes.size(); i++) {
371 DisplayModeRecord record = mSupportedModes.valueAt(i);
Michael Wright58e829f2015-09-15 00:13:26 +0100372 if (record.hasMatchingMode(info)) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700373 return record;
374 }
375 }
376 return null;
Jeff Brown64a55af2012-08-26 02:47:39 -0700377 }
378
379 @Override
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700380 public void applyPendingDisplayDeviceInfoChangesLocked() {
381 if (mHavePendingChanges) {
382 mInfo = null;
383 mHavePendingChanges = false;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700384 }
Jeff Brown64a55af2012-08-26 02:47:39 -0700385 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700386
387 @Override
388 public DisplayDeviceInfo getDisplayDeviceInfoLocked() {
389 if (mInfo == null) {
Michael Wright58e829f2015-09-15 00:13:26 +0100390 SurfaceControl.PhysicalDisplayInfo phys = mDisplayInfos[mActivePhysIndex];
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700391 mInfo = new DisplayDeviceInfo();
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700392 mInfo.width = phys.width;
393 mInfo.height = phys.height;
394 mInfo.modeId = mActiveModeId;
395 mInfo.defaultModeId = mDefaultModeId;
Michael Wrighta3dab232019-02-22 16:54:21 +0000396 mInfo.supportedModes = getDisplayModes(mSupportedModes);
Michael Wright1c9977b2016-07-12 13:30:10 -0700397 mInfo.colorMode = mActiveColorMode;
398 mInfo.supportedColorModes =
399 new int[mSupportedColorModes.size()];
400 for (int i = 0; i < mSupportedColorModes.size(); i++) {
401 mInfo.supportedColorModes[i] = mSupportedColorModes.get(i);
Michael Wright58e829f2015-09-15 00:13:26 +0100402 }
Michael Wright9ff94c02016-03-30 18:05:40 -0700403 mInfo.hdrCapabilities = mHdrCapabilities;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700404 mInfo.appVsyncOffsetNanos = phys.appVsyncOffsetNanos;
405 mInfo.presentationDeadlineNanos = phys.presentationDeadlineNanos;
Jeff Brown037c33e2014-04-09 00:31:55 -0700406 mInfo.state = mState;
Wale Ogunwale361ca212014-11-20 11:42:38 -0800407 mInfo.uniqueId = getUniqueId();
Dominik Laskowskidb845962019-01-27 21:20:00 -0800408 mInfo.address = DisplayAddress.fromPhysicalDisplayId(mPhysicalDisplayId);
Jeff Brown77aebfd2012-10-01 21:07:03 -0700409
Jeff Brownf0681b32012-10-23 17:35:57 -0700410 // Assume that all built-in displays that have secure output (eg. HDCP) also
Jeff Brown77aebfd2012-10-01 21:07:03 -0700411 // support compositing from gralloc protected buffers.
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700412 if (phys.secure) {
Jeff Brownf0681b32012-10-23 17:35:57 -0700413 mInfo.flags = DisplayDeviceInfo.FLAG_SECURE
414 | DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS;
415 }
Jeff Brown77aebfd2012-10-01 21:07:03 -0700416
Adrian Roos30f53212018-01-05 16:14:34 +0100417 final Resources res = getOverlayContext().getResources();
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800418 if (mIsInternal) {
Adam Powell49e7ff92015-05-14 16:18:53 -0700419 mInfo.name = res.getString(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700420 com.android.internal.R.string.display_manager_built_in_display_name);
Jeff Brown77aebfd2012-10-01 21:07:03 -0700421 mInfo.flags |= DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY
Jeff Brown27f1d672012-10-17 18:32:34 -0700422 | DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT;
Adam Powell01f280d2015-05-18 16:07:42 -0700423 if (res.getBoolean(com.android.internal.R.bool.config_mainBuiltInDisplayIsRound)
Griff Hazend3c454d2016-03-25 07:30:34 -0700424 || (Build.IS_EMULATOR
Adam Powell01f280d2015-05-18 16:07:42 -0700425 && SystemProperties.getBoolean(PROPERTY_EMULATOR_CIRCULAR, false))) {
Adam Powell49e7ff92015-05-14 16:18:53 -0700426 mInfo.flags |= DisplayDeviceInfo.FLAG_ROUND;
427 }
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200428 if (res.getBoolean(
429 com.android.internal.R.bool.config_maskMainBuiltInDisplayCutout)) {
430 mInfo.flags |= DisplayDeviceInfo.FLAG_MASK_DISPLAY_CUTOUT;
431 }
Jorim Jaggi60640512018-06-29 01:14:31 +0200432 mInfo.displayCutout = DisplayCutout.fromResourcesRectApproximation(res,
433 mInfo.width, mInfo.height);
Jeff Brown92130f62012-10-24 21:28:33 -0700434 mInfo.type = Display.TYPE_BUILT_IN;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700435 mInfo.densityDpi = (int)(phys.density * 160 + 0.5f);
436 mInfo.xDpi = phys.xDpi;
437 mInfo.yDpi = phys.yDpi;
Jeff Brownd728bf52012-09-08 18:05:28 -0700438 mInfo.touch = DisplayDeviceInfo.TOUCH_INTERNAL;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700439 } else {
Adrian Roos1cf585052018-01-03 18:43:27 +0100440 mInfo.displayCutout = null;
Jeff Brown92130f62012-10-24 21:28:33 -0700441 mInfo.type = Display.TYPE_HDMI;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700442 mInfo.flags |= DisplayDeviceInfo.FLAG_PRESENTATION;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700443 mInfo.name = getContext().getResources().getString(
444 com.android.internal.R.string.display_manager_hdmi_display_name);
Jeff Brownd728bf52012-09-08 18:05:28 -0700445 mInfo.touch = DisplayDeviceInfo.TOUCH_EXTERNAL;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700446 mInfo.setAssumedDensityForExternalDisplay(phys.width, phys.height);
Jeff Brown27f1d672012-10-17 18:32:34 -0700447
448 // For demonstration purposes, allow rotation of the external display.
449 // In the future we might allow the user to configure this directly.
450 if ("portrait".equals(SystemProperties.get("persist.demo.hdmirotation"))) {
451 mInfo.rotation = Surface.ROTATION_270;
452 }
Scott Anderson8786ed92013-11-01 13:27:39 -0700453
454 // For demonstration purposes, allow rotation of the external display
455 // to follow the built-in display.
456 if (SystemProperties.getBoolean("persist.demo.hdmirotates", false)) {
457 mInfo.flags |= DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT;
458 }
Michael Wright4b0fa172016-03-11 17:15:24 -0800459
460 if (!res.getBoolean(
461 com.android.internal.R.bool.config_localDisplaysMirrorContent)) {
462 mInfo.flags |= DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY;
463 }
Pavel Maltseva6fec7b2018-04-04 10:14:55 -0700464
465 if (res.getBoolean(com.android.internal.R.bool.config_localDisplaysPrivate)) {
466 mInfo.flags |= DisplayDeviceInfo.FLAG_PRIVATE;
467 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700468 }
469 }
470 return mInfo;
471 }
472
473 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -0700474 public Runnable requestDisplayStateLocked(final int state, final int brightness) {
475 // Assume that the brightness is off if the display is being turned off.
476 assert state != Display.STATE_OFF || brightness == PowerManager.BRIGHTNESS_OFF;
477
478 final boolean stateChanged = (mState != state);
479 final boolean brightnessChanged = (mBrightness != brightness) && mBacklight != null;
480 if (stateChanged || brightnessChanged) {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800481 final long physicalDisplayId = mPhysicalDisplayId;
Jeff Browne75926d2014-09-18 15:24:49 -0700482 final IBinder token = getDisplayTokenLocked();
Jeff Brown5d6443b2015-04-10 20:15:01 -0700483 final int oldState = mState;
Jeff Browne75926d2014-09-18 15:24:49 -0700484
Jeff Brown5d6443b2015-04-10 20:15:01 -0700485 if (stateChanged) {
486 mState = state;
487 updateDeviceInfoLocked();
488 }
489
490 if (brightnessChanged) {
491 mBrightness = brightness;
492 }
493
494 // Defer actually setting the display state until after we have exited
Jeff Browne75926d2014-09-18 15:24:49 -0700495 // the critical section since it can take hundreds of milliseconds
496 // to complete.
497 return new Runnable() {
498 @Override
499 public void run() {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700500 // Exit a suspended state before making any changes.
501 int currentState = oldState;
502 if (Display.isSuspendedState(oldState)
503 || oldState == Display.STATE_UNKNOWN) {
504 if (!Display.isSuspendedState(state)) {
505 setDisplayState(state);
506 currentState = state;
507 } else if (state == Display.STATE_DOZE_SUSPEND
508 || oldState == Display.STATE_DOZE_SUSPEND) {
509 setDisplayState(Display.STATE_DOZE);
510 currentState = Display.STATE_DOZE;
Chris Phoenix10a4a642017-09-25 13:21:00 -0700511 } else if (state == Display.STATE_ON_SUSPEND
512 || oldState == Display.STATE_ON_SUSPEND) {
513 setDisplayState(Display.STATE_ON);
514 currentState = Display.STATE_ON;
Jeff Brown5d6443b2015-04-10 20:15:01 -0700515 } else {
516 return; // old state and new state is off
517 }
518 }
519
Santos Cordond6a56602016-09-20 15:50:35 -0700520 // If the state change was from or to VR, then we need to tell the light
Karthik Ravi Shankar66855312017-10-04 13:30:13 -0700521 // so that it can apply appropriate VR brightness settings. Also, update the
522 // brightness so the state is propogated to light.
523 boolean vrModeChange = false;
Santos Cordond6a56602016-09-20 15:50:35 -0700524 if ((state == Display.STATE_VR || currentState == Display.STATE_VR) &&
525 currentState != state) {
526 setVrMode(state == Display.STATE_VR);
Karthik Ravi Shankar66855312017-10-04 13:30:13 -0700527 vrModeChange = true;
Santos Cordond6a56602016-09-20 15:50:35 -0700528 }
529
530
Jeff Brown5d6443b2015-04-10 20:15:01 -0700531 // Apply brightness changes given that we are in a non-suspended state.
Karthik Ravi Shankar66855312017-10-04 13:30:13 -0700532 if (brightnessChanged || vrModeChange) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700533 setDisplayBrightness(brightness);
534 }
535
536 // Enter the final desired state, possibly suspended.
537 if (state != currentState) {
538 setDisplayState(state);
539 }
540 }
541
Santos Cordond6a56602016-09-20 15:50:35 -0700542 private void setVrMode(boolean isVrEnabled) {
543 if (DEBUG) {
544 Slog.d(TAG, "setVrMode("
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800545 + "id=" + physicalDisplayId
Santos Cordond6a56602016-09-20 15:50:35 -0700546 + ", state=" + Display.stateToString(state) + ")");
547 }
548 mBacklight.setVrMode(isVrEnabled);
549 }
550
Jeff Brown5d6443b2015-04-10 20:15:01 -0700551 private void setDisplayState(int state) {
552 if (DEBUG) {
553 Slog.d(TAG, "setDisplayState("
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800554 + "id=" + physicalDisplayId
Jeff Brown5d6443b2015-04-10 20:15:01 -0700555 + ", state=" + Display.stateToString(state) + ")");
556 }
557
Chris Phoenixbc839a32017-10-21 14:46:15 -0700558 // We must tell sidekick to stop controlling the display before we
559 // can change its power mode, so do that first.
560 if (mSidekickActive) {
561 Trace.traceBegin(Trace.TRACE_TAG_POWER,
562 "SidekickInternal#endDisplayControl");
563 try {
564 mSidekickInternal.endDisplayControl();
565 } finally {
566 Trace.traceEnd(Trace.TRACE_TAG_POWER);
567 }
568 mSidekickActive = false;
569 }
570 final int mode = getPowerModeForState(state);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700571 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setDisplayState("
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800572 + "id=" + physicalDisplayId
Jeff Brown5d6443b2015-04-10 20:15:01 -0700573 + ", state=" + Display.stateToString(state) + ")");
Jeff Browne75926d2014-09-18 15:24:49 -0700574 try {
575 SurfaceControl.setDisplayPowerMode(token, mode);
Michael Wrightc3e6af82017-07-25 22:31:03 +0100576 Trace.traceCounter(Trace.TRACE_TAG_POWER, "DisplayPowerMode", mode);
Jeff Browne75926d2014-09-18 15:24:49 -0700577 } finally {
578 Trace.traceEnd(Trace.TRACE_TAG_POWER);
579 }
Chris Phoenixbc839a32017-10-21 14:46:15 -0700580 // If we're entering a suspended (but not OFF) power state and we
581 // have a sidekick available, tell it now that it can take control.
582 if (Display.isSuspendedState(state) && state != Display.STATE_OFF
583 && mSidekickInternal != null && !mSidekickActive) {
584 Trace.traceBegin(Trace.TRACE_TAG_POWER,
585 "SidekickInternal#startDisplayControl");
586 try {
587 mSidekickActive = mSidekickInternal.startDisplayControl(state);
588 } finally {
589 Trace.traceEnd(Trace.TRACE_TAG_POWER);
590 }
591 }
Jeff Browne75926d2014-09-18 15:24:49 -0700592 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700593
594 private void setDisplayBrightness(int brightness) {
595 if (DEBUG) {
596 Slog.d(TAG, "setDisplayBrightness("
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800597 + "id=" + physicalDisplayId
598 + ", brightness=" + brightness + ")");
Jeff Brown5d6443b2015-04-10 20:15:01 -0700599 }
600
601 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setDisplayBrightness("
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800602 + "id=" + physicalDisplayId + ", brightness=" + brightness + ")");
Jeff Brown5d6443b2015-04-10 20:15:01 -0700603 try {
604 mBacklight.setBrightness(brightness);
Michael Wrightc3e6af82017-07-25 22:31:03 +0100605 Trace.traceCounter(Trace.TRACE_TAG_POWER,
Michael Wrighta9f37ab2017-08-15 17:14:20 +0100606 "ScreenBrightness", brightness);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700607 } finally {
608 Trace.traceEnd(Trace.TRACE_TAG_POWER);
609 }
610 }
Jeff Browne75926d2014-09-18 15:24:49 -0700611 };
Jeff Brown037c33e2014-04-09 00:31:55 -0700612 }
Jeff Browne75926d2014-09-18 15:24:49 -0700613 return null;
Jeff Brown9e316a12012-10-08 19:17:06 -0700614 }
615
616 @Override
Michael Wrighta3dab232019-02-22 16:54:21 +0000617 public void setRequestedColorModeLocked(int colorMode) {
618 if (requestColorModeLocked(colorMode)) {
Michael Wright1c9977b2016-07-12 13:30:10 -0700619 updateDeviceInfoLocked();
620 }
621 }
622
Adrian Roos898ec382018-01-17 12:54:50 +0100623 @Override
Michael Wrighta3dab232019-02-22 16:54:21 +0000624 public void setAllowedDisplayModesLocked(int[] modes) {
625 updateAllowedModesLocked(modes);
626 }
627
628 @Override
Adrian Roos898ec382018-01-17 12:54:50 +0100629 public void onOverlayChangedLocked() {
630 updateDeviceInfoLocked();
631 }
632
Michael Wrighta3dab232019-02-22 16:54:21 +0000633 public void onActivePhysicalDisplayModeChangedLocked(int physIndex) {
634 if (updateActiveModeLocked(physIndex)) {
635 updateDeviceInfoLocked();
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700636 }
Michael Wrighta3dab232019-02-22 16:54:21 +0000637 }
Michael Wright58e829f2015-09-15 00:13:26 +0100638
Michael Wrighta3dab232019-02-22 16:54:21 +0000639 public boolean updateActiveModeLocked(int activePhysIndex) {
640 if (mActivePhysIndex == activePhysIndex) {
Michael Wright1c9977b2016-07-12 13:30:10 -0700641 return false;
Michael Wright3f145a22014-07-22 19:46:03 -0700642 }
Michael Wrighta3dab232019-02-22 16:54:21 +0000643 mActivePhysIndex = activePhysIndex;
644 mActiveModeId = findMatchingModeIdLocked(activePhysIndex);
645 mActiveModeInvalid = mActiveModeId == 0;
646 if (mActiveModeInvalid) {
647 Slog.w(TAG, "In unknown mode after setting allowed configs"
648 + ": allowedPhysIndexes=" + mAllowedPhysIndexes
649 + ", activePhysIndex=" + mActivePhysIndex);
650 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700651 return true;
652 }
653
Michael Wrighta3dab232019-02-22 16:54:21 +0000654 public void updateAllowedModesLocked(int[] allowedModes) {
655 if (Arrays.equals(allowedModes, mAllowedModeIds) && !mAllowedModeIdsInvalid) {
656 return;
657 }
658 if (updateAllowedModesInternalLocked(allowedModes)) {
659 updateDeviceInfoLocked();
660 }
661 }
662
663 public boolean updateAllowedModesInternalLocked(int[] allowedModes) {
664 if (DEBUG) {
665 Slog.w(TAG, "updateAllowedModesInternalLocked(allowedModes="
666 + Arrays.toString(allowedModes) + ")");
667 }
668 int[] allowedPhysIndexes = new int[allowedModes.length];
669 int size = 0;
670 for (int modeId : allowedModes) {
671 int physIndex = findDisplayInfoIndexLocked(modeId);
672 if (physIndex < 0) {
673 Slog.w(TAG, "Requested mode ID " + modeId + " not available,"
674 + " dropping from allowed set.");
675 } else {
676 allowedPhysIndexes[size++] = physIndex;
677 }
678 }
679
680 // If we couldn't find one or more of the suggested allowed modes then we need to
681 // shrink the array to its actual size.
682 if (size != allowedModes.length) {
683 allowedPhysIndexes = Arrays.copyOf(allowedPhysIndexes, size);
684 }
685
686 // If we found no suitable modes, then we try again with the default mode which we
687 // assume has a suitable physical config.
688 if (size == 0) {
689 if (DEBUG) {
690 Slog.w(TAG, "No valid modes allowed, falling back to default mode (id="
691 + mDefaultModeId + ")");
692 }
693 allowedModes = new int[] { mDefaultModeId };
694 allowedPhysIndexes = new int[] { findDisplayInfoIndexLocked(mDefaultModeId) };
695 }
696
697 mAllowedModeIds = allowedModes;
698 mAllowedModeIdsInvalid = false;
699
700 if (Arrays.equals(mAllowedPhysIndexes, allowedPhysIndexes)) {
701 return false;
702 }
703 mAllowedPhysIndexes = allowedPhysIndexes;
704
705 if (DEBUG) {
706 Slog.w(TAG, "Setting allowed physical configs: allowedPhysIndexes="
707 + Arrays.toString(allowedPhysIndexes));
708 }
709
710 SurfaceControl.setAllowedDisplayConfigs(getDisplayTokenLocked(), allowedPhysIndexes);
711 int activePhysIndex = SurfaceControl.getActiveConfig(getDisplayTokenLocked());
712 return updateActiveModeLocked(activePhysIndex);
713 }
714
Robert Carrae606b42018-02-15 15:36:23 -0800715 public boolean requestColorModeLocked(int colorMode) {
Michael Wright1c9977b2016-07-12 13:30:10 -0700716 if (mActiveColorMode == colorMode) {
717 return false;
718 }
719 if (!mSupportedColorModes.contains(colorMode)) {
720 Slog.w(TAG, "Unable to find color mode " + colorMode
721 + ", ignoring request.");
722 return false;
723 }
724 SurfaceControl.setActiveColorMode(getDisplayTokenLocked(), colorMode);
725 mActiveColorMode = colorMode;
726 mActiveColorModeInvalid = false;
727 return true;
Michael Wright3f145a22014-07-22 19:46:03 -0700728 }
729
730 @Override
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700731 public void dumpLocked(PrintWriter pw) {
732 super.dumpLocked(pw);
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800733 pw.println("mPhysicalDisplayId=" + mPhysicalDisplayId);
Michael Wrighta3dab232019-02-22 16:54:21 +0000734 pw.println("mAllowedPhysIndexes=" + Arrays.toString(mAllowedPhysIndexes));
735 pw.println("mAllowedModeIds=" + Arrays.toString(mAllowedModeIds));
736 pw.println("mAllowedModeIdsInvalid=" + mAllowedModeIdsInvalid);
Michael Wright58e829f2015-09-15 00:13:26 +0100737 pw.println("mActivePhysIndex=" + mActivePhysIndex);
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700738 pw.println("mActiveModeId=" + mActiveModeId);
Michael Wright1c9977b2016-07-12 13:30:10 -0700739 pw.println("mActiveColorMode=" + mActiveColorMode);
Michael Wrighta3dab232019-02-22 16:54:21 +0000740 pw.println("mDefaultModeId=" + mDefaultModeId);
Jeff Brown037c33e2014-04-09 00:31:55 -0700741 pw.println("mState=" + Display.stateToString(mState));
Jeff Brown5d6443b2015-04-10 20:15:01 -0700742 pw.println("mBrightness=" + mBrightness);
743 pw.println("mBacklight=" + mBacklight);
Michael Wright58e829f2015-09-15 00:13:26 +0100744 pw.println("mDisplayInfos=");
745 for (int i = 0; i < mDisplayInfos.length; i++) {
746 pw.println(" " + mDisplayInfos[i]);
747 }
748 pw.println("mSupportedModes=");
749 for (int i = 0; i < mSupportedModes.size(); i++) {
750 pw.println(" " + mSupportedModes.valueAt(i));
751 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700752 pw.print("mSupportedColorModes=[");
753 for (int i = 0; i < mSupportedColorModes.size(); i++) {
Michael Wright58e829f2015-09-15 00:13:26 +0100754 if (i != 0) {
755 pw.print(", ");
756 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700757 pw.print(mSupportedColorModes.get(i));
Michael Wright58e829f2015-09-15 00:13:26 +0100758 }
759 pw.println("]");
760 }
761
Michael Wright1c9977b2016-07-12 13:30:10 -0700762 private int findDisplayInfoIndexLocked(int modeId) {
Michael Wright58e829f2015-09-15 00:13:26 +0100763 DisplayModeRecord record = mSupportedModes.get(modeId);
Michael Wright1c9977b2016-07-12 13:30:10 -0700764 if (record != null) {
Michael Wright58e829f2015-09-15 00:13:26 +0100765 for (int i = 0; i < mDisplayInfos.length; i++) {
766 SurfaceControl.PhysicalDisplayInfo info = mDisplayInfos[i];
Michael Wright1c9977b2016-07-12 13:30:10 -0700767 if (record.hasMatchingMode(info)){
Michael Wright58e829f2015-09-15 00:13:26 +0100768 return i;
769 }
770 }
771 }
772 return -1;
Jeff Brown037c33e2014-04-09 00:31:55 -0700773 }
774
Michael Wrighta3dab232019-02-22 16:54:21 +0000775 private int findMatchingModeIdLocked(int physIndex) {
776 SurfaceControl.PhysicalDisplayInfo info = mDisplayInfos[physIndex];
777 for (int i = 0; i < mSupportedModes.size(); i++) {
778 DisplayModeRecord record = mSupportedModes.valueAt(i);
779 if (record.hasMatchingMode(info)) {
780 return record.mMode.getModeId();
781 }
782 }
783 return 0;
784 }
785
Jeff Brown037c33e2014-04-09 00:31:55 -0700786 private void updateDeviceInfoLocked() {
787 mInfo = null;
788 sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700789 }
Michael Wrighta3dab232019-02-22 16:54:21 +0000790
791 private Display.Mode[] getDisplayModes(SparseArray<DisplayModeRecord> records) {
792 final int size = records.size();
793 Display.Mode[] modes = new Display.Mode[size];
794 for (int i = 0; i < size; i++) {
795 DisplayModeRecord record = records.valueAt(i);
796 modes[i] = record.mMode;
797 }
798 return modes;
799 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700800 }
Michael Wright3f145a22014-07-22 19:46:03 -0700801
Adrian Roos30f53212018-01-05 16:14:34 +0100802 /** Supplies a context whose Resources apply runtime-overlays */
803 Context getOverlayContext() {
804 return ActivityThread.currentActivityThread().getSystemUiContext();
805 }
806
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700807 /**
808 * Keeps track of a display configuration.
809 */
810 private static final class DisplayModeRecord {
811 public final Display.Mode mMode;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700812
Michael Wright58e829f2015-09-15 00:13:26 +0100813 public DisplayModeRecord(SurfaceControl.PhysicalDisplayInfo phys) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700814 mMode = createMode(phys.width, phys.height, phys.refreshRate);
Michael Wright58e829f2015-09-15 00:13:26 +0100815 }
816
817 /**
818 * Returns whether the mode generated by the given PhysicalDisplayInfo matches the mode
819 * contained by the record modulo mode ID.
820 *
821 * Note that this doesn't necessarily mean the the PhysicalDisplayInfos are identical, just
822 * that they generate identical modes.
823 */
824 public boolean hasMatchingMode(SurfaceControl.PhysicalDisplayInfo info) {
825 int modeRefreshRate = Float.floatToIntBits(mMode.getRefreshRate());
826 int displayInfoRefreshRate = Float.floatToIntBits(info.refreshRate);
827 return mMode.getPhysicalWidth() == info.width
828 && mMode.getPhysicalHeight() == info.height
829 && modeRefreshRate == displayInfoRefreshRate;
830 }
831
832 public String toString() {
833 return "DisplayModeRecord{mMode=" + mMode + "}";
Michael Wright3f145a22014-07-22 19:46:03 -0700834 }
Jeff Brown64a55af2012-08-26 02:47:39 -0700835 }
Jeff Browne87bf032012-09-20 18:30:13 -0700836
Ady Abrahama5a21f72019-02-13 16:41:59 -0800837 private final class PhysicalDisplayEventReceiver extends DisplayEventReceiver {
838 PhysicalDisplayEventReceiver(Looper looper) {
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -0700839 super(looper, VSYNC_SOURCE_APP);
Jeff Browne87bf032012-09-20 18:30:13 -0700840 }
841
842 @Override
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800843 public void onHotplug(long timestampNanos, long physicalDisplayId, boolean connected) {
Jeff Browne87bf032012-09-20 18:30:13 -0700844 synchronized (getSyncRoot()) {
Jesse Halle244db42012-11-08 11:55:14 -0800845 if (connected) {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800846 tryConnectDisplayLocked(physicalDisplayId);
Jesse Halle244db42012-11-08 11:55:14 -0800847 } else {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800848 tryDisconnectDisplayLocked(physicalDisplayId);
Jesse Halle244db42012-11-08 11:55:14 -0800849 }
Jeff Browne87bf032012-09-20 18:30:13 -0700850 }
851 }
Ady Abrahama5a21f72019-02-13 16:41:59 -0800852
853 @Override
Michael Wrighta3dab232019-02-22 16:54:21 +0000854 public void onConfigChanged(long timestampNanos, long physicalDisplayId, int physIndex) {
Ady Abrahama5a21f72019-02-13 16:41:59 -0800855 if (DEBUG) {
856 Slog.d(TAG, "onConfigChanged("
857 + "timestampNanos=" + timestampNanos
Michael Wrighta3dab232019-02-22 16:54:21 +0000858 + ", physicalDisplayId=" + physicalDisplayId
859 + ", physIndex=" + physIndex + ")");
860 }
861 synchronized (getSyncRoot()) {
862 LocalDisplayDevice device = mDevices.get(physicalDisplayId);
863 if (device == null) {
864 if (DEBUG) {
865 Slog.d(TAG, "Received config change for unhandled physical display: "
866 + "physicalDisplayId=" + physicalDisplayId);
867 }
868 return;
869 }
870 device.onActivePhysicalDisplayModeChangedLocked(physIndex);
Ady Abrahama5a21f72019-02-13 16:41:59 -0800871 }
872 }
Jeff Browne87bf032012-09-20 18:30:13 -0700873 }
Dan Stoza00101052014-05-02 15:23:40 -0700874}