blob: b03dc3b2ae9349dd78b96f3e01756d400bd529dd [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();
Keun young Parkabf38e32019-04-23 19:18:34 -0700408 final DisplayAddress.Physical physicalAddress =
409 DisplayAddress.fromPhysicalDisplayId(mPhysicalDisplayId);
410 mInfo.address = physicalAddress;
Jeff Brown77aebfd2012-10-01 21:07:03 -0700411
Jeff Brownf0681b32012-10-23 17:35:57 -0700412 // Assume that all built-in displays that have secure output (eg. HDCP) also
Jeff Brown77aebfd2012-10-01 21:07:03 -0700413 // support compositing from gralloc protected buffers.
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700414 if (phys.secure) {
Jeff Brownf0681b32012-10-23 17:35:57 -0700415 mInfo.flags = DisplayDeviceInfo.FLAG_SECURE
416 | DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS;
417 }
Jeff Brown77aebfd2012-10-01 21:07:03 -0700418
Adrian Roos30f53212018-01-05 16:14:34 +0100419 final Resources res = getOverlayContext().getResources();
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800420 if (mIsInternal) {
Adam Powell49e7ff92015-05-14 16:18:53 -0700421 mInfo.name = res.getString(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700422 com.android.internal.R.string.display_manager_built_in_display_name);
Jeff Brown77aebfd2012-10-01 21:07:03 -0700423 mInfo.flags |= DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY
Jeff Brown27f1d672012-10-17 18:32:34 -0700424 | DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT;
Adam Powell01f280d2015-05-18 16:07:42 -0700425 if (res.getBoolean(com.android.internal.R.bool.config_mainBuiltInDisplayIsRound)
Griff Hazend3c454d2016-03-25 07:30:34 -0700426 || (Build.IS_EMULATOR
Adam Powell01f280d2015-05-18 16:07:42 -0700427 && SystemProperties.getBoolean(PROPERTY_EMULATOR_CIRCULAR, false))) {
Adam Powell49e7ff92015-05-14 16:18:53 -0700428 mInfo.flags |= DisplayDeviceInfo.FLAG_ROUND;
429 }
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200430 if (res.getBoolean(
431 com.android.internal.R.bool.config_maskMainBuiltInDisplayCutout)) {
432 mInfo.flags |= DisplayDeviceInfo.FLAG_MASK_DISPLAY_CUTOUT;
433 }
Jorim Jaggi60640512018-06-29 01:14:31 +0200434 mInfo.displayCutout = DisplayCutout.fromResourcesRectApproximation(res,
435 mInfo.width, mInfo.height);
Jeff Brown92130f62012-10-24 21:28:33 -0700436 mInfo.type = Display.TYPE_BUILT_IN;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700437 mInfo.densityDpi = (int)(phys.density * 160 + 0.5f);
438 mInfo.xDpi = phys.xDpi;
439 mInfo.yDpi = phys.yDpi;
Jeff Brownd728bf52012-09-08 18:05:28 -0700440 mInfo.touch = DisplayDeviceInfo.TOUCH_INTERNAL;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700441 } else {
Adrian Roos1cf585052018-01-03 18:43:27 +0100442 mInfo.displayCutout = null;
Jeff Brown92130f62012-10-24 21:28:33 -0700443 mInfo.type = Display.TYPE_HDMI;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700444 mInfo.flags |= DisplayDeviceInfo.FLAG_PRESENTATION;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700445 mInfo.name = getContext().getResources().getString(
446 com.android.internal.R.string.display_manager_hdmi_display_name);
Jeff Brownd728bf52012-09-08 18:05:28 -0700447 mInfo.touch = DisplayDeviceInfo.TOUCH_EXTERNAL;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700448 mInfo.setAssumedDensityForExternalDisplay(phys.width, phys.height);
Jeff Brown27f1d672012-10-17 18:32:34 -0700449
450 // For demonstration purposes, allow rotation of the external display.
451 // In the future we might allow the user to configure this directly.
452 if ("portrait".equals(SystemProperties.get("persist.demo.hdmirotation"))) {
453 mInfo.rotation = Surface.ROTATION_270;
454 }
Scott Anderson8786ed92013-11-01 13:27:39 -0700455
456 // For demonstration purposes, allow rotation of the external display
457 // to follow the built-in display.
458 if (SystemProperties.getBoolean("persist.demo.hdmirotates", false)) {
459 mInfo.flags |= DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT;
460 }
Michael Wright4b0fa172016-03-11 17:15:24 -0800461
462 if (!res.getBoolean(
463 com.android.internal.R.bool.config_localDisplaysMirrorContent)) {
464 mInfo.flags |= DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY;
465 }
Pavel Maltseva6fec7b2018-04-04 10:14:55 -0700466
Keun young Parkabf38e32019-04-23 19:18:34 -0700467 if (isDisplayPrivate(physicalAddress)) {
Pavel Maltseva6fec7b2018-04-04 10:14:55 -0700468 mInfo.flags |= DisplayDeviceInfo.FLAG_PRIVATE;
469 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700470 }
471 }
472 return mInfo;
473 }
474
475 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -0700476 public Runnable requestDisplayStateLocked(final int state, final int brightness) {
477 // Assume that the brightness is off if the display is being turned off.
478 assert state != Display.STATE_OFF || brightness == PowerManager.BRIGHTNESS_OFF;
479
480 final boolean stateChanged = (mState != state);
481 final boolean brightnessChanged = (mBrightness != brightness) && mBacklight != null;
482 if (stateChanged || brightnessChanged) {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800483 final long physicalDisplayId = mPhysicalDisplayId;
Jeff Browne75926d2014-09-18 15:24:49 -0700484 final IBinder token = getDisplayTokenLocked();
Jeff Brown5d6443b2015-04-10 20:15:01 -0700485 final int oldState = mState;
Jeff Browne75926d2014-09-18 15:24:49 -0700486
Jeff Brown5d6443b2015-04-10 20:15:01 -0700487 if (stateChanged) {
488 mState = state;
489 updateDeviceInfoLocked();
490 }
491
492 if (brightnessChanged) {
493 mBrightness = brightness;
494 }
495
496 // Defer actually setting the display state until after we have exited
Jeff Browne75926d2014-09-18 15:24:49 -0700497 // the critical section since it can take hundreds of milliseconds
498 // to complete.
499 return new Runnable() {
500 @Override
501 public void run() {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700502 // Exit a suspended state before making any changes.
503 int currentState = oldState;
504 if (Display.isSuspendedState(oldState)
505 || oldState == Display.STATE_UNKNOWN) {
506 if (!Display.isSuspendedState(state)) {
507 setDisplayState(state);
508 currentState = state;
509 } else if (state == Display.STATE_DOZE_SUSPEND
510 || oldState == Display.STATE_DOZE_SUSPEND) {
511 setDisplayState(Display.STATE_DOZE);
512 currentState = Display.STATE_DOZE;
Chris Phoenix10a4a642017-09-25 13:21:00 -0700513 } else if (state == Display.STATE_ON_SUSPEND
514 || oldState == Display.STATE_ON_SUSPEND) {
515 setDisplayState(Display.STATE_ON);
516 currentState = Display.STATE_ON;
Jeff Brown5d6443b2015-04-10 20:15:01 -0700517 } else {
518 return; // old state and new state is off
519 }
520 }
521
Santos Cordond6a56602016-09-20 15:50:35 -0700522 // If the state change was from or to VR, then we need to tell the light
Karthik Ravi Shankar66855312017-10-04 13:30:13 -0700523 // so that it can apply appropriate VR brightness settings. Also, update the
524 // brightness so the state is propogated to light.
525 boolean vrModeChange = false;
Santos Cordond6a56602016-09-20 15:50:35 -0700526 if ((state == Display.STATE_VR || currentState == Display.STATE_VR) &&
527 currentState != state) {
528 setVrMode(state == Display.STATE_VR);
Karthik Ravi Shankar66855312017-10-04 13:30:13 -0700529 vrModeChange = true;
Santos Cordond6a56602016-09-20 15:50:35 -0700530 }
531
532
Jeff Brown5d6443b2015-04-10 20:15:01 -0700533 // Apply brightness changes given that we are in a non-suspended state.
Karthik Ravi Shankar66855312017-10-04 13:30:13 -0700534 if (brightnessChanged || vrModeChange) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700535 setDisplayBrightness(brightness);
536 }
537
538 // Enter the final desired state, possibly suspended.
539 if (state != currentState) {
540 setDisplayState(state);
541 }
542 }
543
Santos Cordond6a56602016-09-20 15:50:35 -0700544 private void setVrMode(boolean isVrEnabled) {
545 if (DEBUG) {
546 Slog.d(TAG, "setVrMode("
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800547 + "id=" + physicalDisplayId
Santos Cordond6a56602016-09-20 15:50:35 -0700548 + ", state=" + Display.stateToString(state) + ")");
549 }
550 mBacklight.setVrMode(isVrEnabled);
551 }
552
Jeff Brown5d6443b2015-04-10 20:15:01 -0700553 private void setDisplayState(int state) {
554 if (DEBUG) {
555 Slog.d(TAG, "setDisplayState("
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800556 + "id=" + physicalDisplayId
Jeff Brown5d6443b2015-04-10 20:15:01 -0700557 + ", state=" + Display.stateToString(state) + ")");
558 }
559
Chris Phoenixbc839a32017-10-21 14:46:15 -0700560 // We must tell sidekick to stop controlling the display before we
561 // can change its power mode, so do that first.
562 if (mSidekickActive) {
563 Trace.traceBegin(Trace.TRACE_TAG_POWER,
564 "SidekickInternal#endDisplayControl");
565 try {
566 mSidekickInternal.endDisplayControl();
567 } finally {
568 Trace.traceEnd(Trace.TRACE_TAG_POWER);
569 }
570 mSidekickActive = false;
571 }
572 final int mode = getPowerModeForState(state);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700573 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setDisplayState("
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800574 + "id=" + physicalDisplayId
Jeff Brown5d6443b2015-04-10 20:15:01 -0700575 + ", state=" + Display.stateToString(state) + ")");
Jeff Browne75926d2014-09-18 15:24:49 -0700576 try {
577 SurfaceControl.setDisplayPowerMode(token, mode);
Michael Wrightc3e6af82017-07-25 22:31:03 +0100578 Trace.traceCounter(Trace.TRACE_TAG_POWER, "DisplayPowerMode", mode);
Jeff Browne75926d2014-09-18 15:24:49 -0700579 } finally {
580 Trace.traceEnd(Trace.TRACE_TAG_POWER);
581 }
Chris Phoenixbc839a32017-10-21 14:46:15 -0700582 // If we're entering a suspended (but not OFF) power state and we
583 // have a sidekick available, tell it now that it can take control.
584 if (Display.isSuspendedState(state) && state != Display.STATE_OFF
585 && mSidekickInternal != null && !mSidekickActive) {
586 Trace.traceBegin(Trace.TRACE_TAG_POWER,
587 "SidekickInternal#startDisplayControl");
588 try {
589 mSidekickActive = mSidekickInternal.startDisplayControl(state);
590 } finally {
591 Trace.traceEnd(Trace.TRACE_TAG_POWER);
592 }
593 }
Jeff Browne75926d2014-09-18 15:24:49 -0700594 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700595
596 private void setDisplayBrightness(int brightness) {
597 if (DEBUG) {
598 Slog.d(TAG, "setDisplayBrightness("
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800599 + "id=" + physicalDisplayId
600 + ", brightness=" + brightness + ")");
Jeff Brown5d6443b2015-04-10 20:15:01 -0700601 }
602
603 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setDisplayBrightness("
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800604 + "id=" + physicalDisplayId + ", brightness=" + brightness + ")");
Jeff Brown5d6443b2015-04-10 20:15:01 -0700605 try {
606 mBacklight.setBrightness(brightness);
Michael Wrightc3e6af82017-07-25 22:31:03 +0100607 Trace.traceCounter(Trace.TRACE_TAG_POWER,
Michael Wrighta9f37ab2017-08-15 17:14:20 +0100608 "ScreenBrightness", brightness);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700609 } finally {
610 Trace.traceEnd(Trace.TRACE_TAG_POWER);
611 }
612 }
Jeff Browne75926d2014-09-18 15:24:49 -0700613 };
Jeff Brown037c33e2014-04-09 00:31:55 -0700614 }
Jeff Browne75926d2014-09-18 15:24:49 -0700615 return null;
Jeff Brown9e316a12012-10-08 19:17:06 -0700616 }
617
618 @Override
Michael Wrighta3dab232019-02-22 16:54:21 +0000619 public void setRequestedColorModeLocked(int colorMode) {
620 if (requestColorModeLocked(colorMode)) {
Michael Wright1c9977b2016-07-12 13:30:10 -0700621 updateDeviceInfoLocked();
622 }
623 }
624
Adrian Roos898ec382018-01-17 12:54:50 +0100625 @Override
Michael Wrighta3dab232019-02-22 16:54:21 +0000626 public void setAllowedDisplayModesLocked(int[] modes) {
627 updateAllowedModesLocked(modes);
628 }
629
630 @Override
Adrian Roos898ec382018-01-17 12:54:50 +0100631 public void onOverlayChangedLocked() {
632 updateDeviceInfoLocked();
633 }
634
Michael Wrighta3dab232019-02-22 16:54:21 +0000635 public void onActivePhysicalDisplayModeChangedLocked(int physIndex) {
636 if (updateActiveModeLocked(physIndex)) {
637 updateDeviceInfoLocked();
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700638 }
Michael Wrighta3dab232019-02-22 16:54:21 +0000639 }
Michael Wright58e829f2015-09-15 00:13:26 +0100640
Michael Wrighta3dab232019-02-22 16:54:21 +0000641 public boolean updateActiveModeLocked(int activePhysIndex) {
642 if (mActivePhysIndex == activePhysIndex) {
Michael Wright1c9977b2016-07-12 13:30:10 -0700643 return false;
Michael Wright3f145a22014-07-22 19:46:03 -0700644 }
Michael Wrighta3dab232019-02-22 16:54:21 +0000645 mActivePhysIndex = activePhysIndex;
646 mActiveModeId = findMatchingModeIdLocked(activePhysIndex);
647 mActiveModeInvalid = mActiveModeId == 0;
648 if (mActiveModeInvalid) {
649 Slog.w(TAG, "In unknown mode after setting allowed configs"
650 + ": allowedPhysIndexes=" + mAllowedPhysIndexes
651 + ", activePhysIndex=" + mActivePhysIndex);
652 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700653 return true;
654 }
655
Michael Wrighta3dab232019-02-22 16:54:21 +0000656 public void updateAllowedModesLocked(int[] allowedModes) {
657 if (Arrays.equals(allowedModes, mAllowedModeIds) && !mAllowedModeIdsInvalid) {
658 return;
659 }
660 if (updateAllowedModesInternalLocked(allowedModes)) {
661 updateDeviceInfoLocked();
662 }
663 }
664
665 public boolean updateAllowedModesInternalLocked(int[] allowedModes) {
666 if (DEBUG) {
667 Slog.w(TAG, "updateAllowedModesInternalLocked(allowedModes="
668 + Arrays.toString(allowedModes) + ")");
669 }
670 int[] allowedPhysIndexes = new int[allowedModes.length];
671 int size = 0;
672 for (int modeId : allowedModes) {
673 int physIndex = findDisplayInfoIndexLocked(modeId);
674 if (physIndex < 0) {
675 Slog.w(TAG, "Requested mode ID " + modeId + " not available,"
676 + " dropping from allowed set.");
677 } else {
678 allowedPhysIndexes[size++] = physIndex;
679 }
680 }
681
682 // If we couldn't find one or more of the suggested allowed modes then we need to
683 // shrink the array to its actual size.
684 if (size != allowedModes.length) {
685 allowedPhysIndexes = Arrays.copyOf(allowedPhysIndexes, size);
686 }
687
688 // If we found no suitable modes, then we try again with the default mode which we
689 // assume has a suitable physical config.
690 if (size == 0) {
691 if (DEBUG) {
692 Slog.w(TAG, "No valid modes allowed, falling back to default mode (id="
693 + mDefaultModeId + ")");
694 }
695 allowedModes = new int[] { mDefaultModeId };
696 allowedPhysIndexes = new int[] { findDisplayInfoIndexLocked(mDefaultModeId) };
697 }
698
699 mAllowedModeIds = allowedModes;
700 mAllowedModeIdsInvalid = false;
701
702 if (Arrays.equals(mAllowedPhysIndexes, allowedPhysIndexes)) {
703 return false;
704 }
705 mAllowedPhysIndexes = allowedPhysIndexes;
706
707 if (DEBUG) {
708 Slog.w(TAG, "Setting allowed physical configs: allowedPhysIndexes="
709 + Arrays.toString(allowedPhysIndexes));
710 }
711
712 SurfaceControl.setAllowedDisplayConfigs(getDisplayTokenLocked(), allowedPhysIndexes);
713 int activePhysIndex = SurfaceControl.getActiveConfig(getDisplayTokenLocked());
714 return updateActiveModeLocked(activePhysIndex);
715 }
716
Robert Carrae606b42018-02-15 15:36:23 -0800717 public boolean requestColorModeLocked(int colorMode) {
Michael Wright1c9977b2016-07-12 13:30:10 -0700718 if (mActiveColorMode == colorMode) {
719 return false;
720 }
721 if (!mSupportedColorModes.contains(colorMode)) {
722 Slog.w(TAG, "Unable to find color mode " + colorMode
723 + ", ignoring request.");
724 return false;
725 }
726 SurfaceControl.setActiveColorMode(getDisplayTokenLocked(), colorMode);
727 mActiveColorMode = colorMode;
728 mActiveColorModeInvalid = false;
729 return true;
Michael Wright3f145a22014-07-22 19:46:03 -0700730 }
731
732 @Override
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700733 public void dumpLocked(PrintWriter pw) {
734 super.dumpLocked(pw);
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800735 pw.println("mPhysicalDisplayId=" + mPhysicalDisplayId);
Michael Wrighta3dab232019-02-22 16:54:21 +0000736 pw.println("mAllowedPhysIndexes=" + Arrays.toString(mAllowedPhysIndexes));
737 pw.println("mAllowedModeIds=" + Arrays.toString(mAllowedModeIds));
738 pw.println("mAllowedModeIdsInvalid=" + mAllowedModeIdsInvalid);
Michael Wright58e829f2015-09-15 00:13:26 +0100739 pw.println("mActivePhysIndex=" + mActivePhysIndex);
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700740 pw.println("mActiveModeId=" + mActiveModeId);
Michael Wright1c9977b2016-07-12 13:30:10 -0700741 pw.println("mActiveColorMode=" + mActiveColorMode);
Michael Wrighta3dab232019-02-22 16:54:21 +0000742 pw.println("mDefaultModeId=" + mDefaultModeId);
Jeff Brown037c33e2014-04-09 00:31:55 -0700743 pw.println("mState=" + Display.stateToString(mState));
Jeff Brown5d6443b2015-04-10 20:15:01 -0700744 pw.println("mBrightness=" + mBrightness);
745 pw.println("mBacklight=" + mBacklight);
Michael Wright58e829f2015-09-15 00:13:26 +0100746 pw.println("mDisplayInfos=");
747 for (int i = 0; i < mDisplayInfos.length; i++) {
748 pw.println(" " + mDisplayInfos[i]);
749 }
750 pw.println("mSupportedModes=");
751 for (int i = 0; i < mSupportedModes.size(); i++) {
752 pw.println(" " + mSupportedModes.valueAt(i));
753 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700754 pw.print("mSupportedColorModes=[");
755 for (int i = 0; i < mSupportedColorModes.size(); i++) {
Michael Wright58e829f2015-09-15 00:13:26 +0100756 if (i != 0) {
757 pw.print(", ");
758 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700759 pw.print(mSupportedColorModes.get(i));
Michael Wright58e829f2015-09-15 00:13:26 +0100760 }
761 pw.println("]");
762 }
763
Michael Wright1c9977b2016-07-12 13:30:10 -0700764 private int findDisplayInfoIndexLocked(int modeId) {
Michael Wright58e829f2015-09-15 00:13:26 +0100765 DisplayModeRecord record = mSupportedModes.get(modeId);
Michael Wright1c9977b2016-07-12 13:30:10 -0700766 if (record != null) {
Michael Wright58e829f2015-09-15 00:13:26 +0100767 for (int i = 0; i < mDisplayInfos.length; i++) {
768 SurfaceControl.PhysicalDisplayInfo info = mDisplayInfos[i];
Michael Wright1c9977b2016-07-12 13:30:10 -0700769 if (record.hasMatchingMode(info)){
Michael Wright58e829f2015-09-15 00:13:26 +0100770 return i;
771 }
772 }
773 }
774 return -1;
Jeff Brown037c33e2014-04-09 00:31:55 -0700775 }
776
Michael Wrighta3dab232019-02-22 16:54:21 +0000777 private int findMatchingModeIdLocked(int physIndex) {
778 SurfaceControl.PhysicalDisplayInfo info = mDisplayInfos[physIndex];
779 for (int i = 0; i < mSupportedModes.size(); i++) {
780 DisplayModeRecord record = mSupportedModes.valueAt(i);
781 if (record.hasMatchingMode(info)) {
782 return record.mMode.getModeId();
783 }
784 }
785 return 0;
786 }
787
Jeff Brown037c33e2014-04-09 00:31:55 -0700788 private void updateDeviceInfoLocked() {
789 mInfo = null;
790 sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700791 }
Michael Wrighta3dab232019-02-22 16:54:21 +0000792
793 private Display.Mode[] getDisplayModes(SparseArray<DisplayModeRecord> records) {
794 final int size = records.size();
795 Display.Mode[] modes = new Display.Mode[size];
796 for (int i = 0; i < size; i++) {
797 DisplayModeRecord record = records.valueAt(i);
798 modes[i] = record.mMode;
799 }
800 return modes;
801 }
Keun young Parkabf38e32019-04-23 19:18:34 -0700802
803 private boolean isDisplayPrivate(DisplayAddress.Physical physicalAddress) {
804 if (physicalAddress == null) {
805 return false;
806 }
807 final Resources res = getOverlayContext().getResources();
808 int[] ports = res.getIntArray(
809 com.android.internal.R.array.config_localPrivateDisplayPorts);
810 if (ports != null) {
811 int port = physicalAddress.getPort();
812 for (int p : ports) {
813 if (p == port) {
814 return true;
815 }
816 }
817 }
818 return false;
819 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700820 }
Michael Wright3f145a22014-07-22 19:46:03 -0700821
Adrian Roos30f53212018-01-05 16:14:34 +0100822 /** Supplies a context whose Resources apply runtime-overlays */
823 Context getOverlayContext() {
824 return ActivityThread.currentActivityThread().getSystemUiContext();
825 }
826
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700827 /**
828 * Keeps track of a display configuration.
829 */
830 private static final class DisplayModeRecord {
831 public final Display.Mode mMode;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700832
Michael Wright58e829f2015-09-15 00:13:26 +0100833 public DisplayModeRecord(SurfaceControl.PhysicalDisplayInfo phys) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700834 mMode = createMode(phys.width, phys.height, phys.refreshRate);
Michael Wright58e829f2015-09-15 00:13:26 +0100835 }
836
837 /**
838 * Returns whether the mode generated by the given PhysicalDisplayInfo matches the mode
839 * contained by the record modulo mode ID.
840 *
841 * Note that this doesn't necessarily mean the the PhysicalDisplayInfos are identical, just
842 * that they generate identical modes.
843 */
844 public boolean hasMatchingMode(SurfaceControl.PhysicalDisplayInfo info) {
845 int modeRefreshRate = Float.floatToIntBits(mMode.getRefreshRate());
846 int displayInfoRefreshRate = Float.floatToIntBits(info.refreshRate);
847 return mMode.getPhysicalWidth() == info.width
848 && mMode.getPhysicalHeight() == info.height
849 && modeRefreshRate == displayInfoRefreshRate;
850 }
851
852 public String toString() {
853 return "DisplayModeRecord{mMode=" + mMode + "}";
Michael Wright3f145a22014-07-22 19:46:03 -0700854 }
Jeff Brown64a55af2012-08-26 02:47:39 -0700855 }
Jeff Browne87bf032012-09-20 18:30:13 -0700856
Ady Abrahama5a21f72019-02-13 16:41:59 -0800857 private final class PhysicalDisplayEventReceiver extends DisplayEventReceiver {
858 PhysicalDisplayEventReceiver(Looper looper) {
Ady Abraham9c501aa2019-06-04 16:07:44 -0700859 super(looper, VSYNC_SOURCE_APP, CONFIG_CHANGED_EVENT_DISPATCH);
Jeff Browne87bf032012-09-20 18:30:13 -0700860 }
861
862 @Override
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800863 public void onHotplug(long timestampNanos, long physicalDisplayId, boolean connected) {
Jeff Browne87bf032012-09-20 18:30:13 -0700864 synchronized (getSyncRoot()) {
Jesse Halle244db42012-11-08 11:55:14 -0800865 if (connected) {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800866 tryConnectDisplayLocked(physicalDisplayId);
Jesse Halle244db42012-11-08 11:55:14 -0800867 } else {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800868 tryDisconnectDisplayLocked(physicalDisplayId);
Jesse Halle244db42012-11-08 11:55:14 -0800869 }
Jeff Browne87bf032012-09-20 18:30:13 -0700870 }
871 }
Ady Abrahama5a21f72019-02-13 16:41:59 -0800872
873 @Override
Michael Wrighta3dab232019-02-22 16:54:21 +0000874 public void onConfigChanged(long timestampNanos, long physicalDisplayId, int physIndex) {
Ady Abrahama5a21f72019-02-13 16:41:59 -0800875 if (DEBUG) {
876 Slog.d(TAG, "onConfigChanged("
877 + "timestampNanos=" + timestampNanos
Michael Wrighta3dab232019-02-22 16:54:21 +0000878 + ", physicalDisplayId=" + physicalDisplayId
879 + ", physIndex=" + physIndex + ")");
880 }
881 synchronized (getSyncRoot()) {
882 LocalDisplayDevice device = mDevices.get(physicalDisplayId);
883 if (device == null) {
884 if (DEBUG) {
885 Slog.d(TAG, "Received config change for unhandled physical display: "
886 + "physicalDisplayId=" + physicalDisplayId);
887 }
888 return;
889 }
890 device.onActivePhysicalDisplayModeChangedLocked(physIndex);
Ady Abrahama5a21f72019-02-13 16:41:59 -0800891 }
892 }
Jeff Browne87bf032012-09-20 18:30:13 -0700893 }
Dan Stoza00101052014-05-02 15:23:40 -0700894}