Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.display; |
| 18 | |
Adam Powell | 49e7ff9 | 2015-05-14 16:18:53 -0700 | [diff] [blame] | 19 | import android.content.res.Resources; |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 20 | import com.android.server.LocalServices; |
| 21 | import com.android.server.lights.Light; |
| 22 | import com.android.server.lights.LightsManager; |
| 23 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Griff Hazen | d3c454d | 2016-03-25 07:30:34 -0700 | [diff] [blame] | 25 | import android.os.Build; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 26 | import android.os.Handler; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 27 | import android.os.IBinder; |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 28 | import android.os.Looper; |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 29 | import android.os.PowerManager; |
Jeff Brown | 27f1d67 | 2012-10-17 18:32:34 -0700 | [diff] [blame] | 30 | import android.os.SystemProperties; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 31 | import android.os.Trace; |
Dan Stoza | 0010105 | 2014-05-02 15:23:40 -0700 | [diff] [blame] | 32 | import android.util.Slog; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 33 | import android.util.SparseArray; |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 34 | import android.view.Display; |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 35 | import android.view.DisplayEventReceiver; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 36 | import android.view.Surface; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 37 | import android.view.SurfaceControl; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 38 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 39 | import java.io.PrintWriter; |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 40 | import java.util.ArrayList; |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 41 | import java.util.Arrays; |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 42 | import java.util.Collections; |
| 43 | import java.util.List; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 44 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 45 | /** |
| 46 | * A display adapter for the local displays managed by Surface Flinger. |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 47 | * <p> |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 48 | * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock. |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 49 | * </p> |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 50 | */ |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 51 | final class LocalDisplayAdapter extends DisplayAdapter { |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 52 | private static final String TAG = "LocalDisplayAdapter"; |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 53 | private static final boolean DEBUG = false; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 54 | |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 55 | private static final String UNIQUE_ID_PREFIX = "local:"; |
| 56 | |
Adam Powell | 01f280d | 2015-05-18 16:07:42 -0700 | [diff] [blame] | 57 | private static final String PROPERTY_EMULATOR_CIRCULAR = "ro.emulator.circular"; |
| 58 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 59 | private static final int[] BUILT_IN_DISPLAY_IDS_TO_SCAN = new int[] { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 60 | SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN, |
| 61 | SurfaceControl.BUILT_IN_DISPLAY_ID_HDMI, |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | private final SparseArray<LocalDisplayDevice> mDevices = |
| 65 | new SparseArray<LocalDisplayDevice>(); |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 66 | @SuppressWarnings("unused") // Becomes active at instantiation time. |
Jeff Brown | 6669250 | 2012-10-18 16:13:44 -0700 | [diff] [blame] | 67 | private HotplugDisplayEventReceiver mHotplugReceiver; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 68 | |
Jeff Brown | 6669250 | 2012-10-18 16:13:44 -0700 | [diff] [blame] | 69 | // Called with SyncRoot lock held. |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 70 | public LocalDisplayAdapter(DisplayManagerService.SyncRoot syncRoot, |
| 71 | Context context, Handler handler, Listener listener) { |
| 72 | super(syncRoot, context, handler, listener, TAG); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 76 | public void registerLocked() { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 77 | super.registerLocked(); |
Jeff Brown | 6669250 | 2012-10-18 16:13:44 -0700 | [diff] [blame] | 78 | |
| 79 | mHotplugReceiver = new HotplugDisplayEventReceiver(getHandler().getLooper()); |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 80 | |
| 81 | for (int builtInDisplayId : BUILT_IN_DISPLAY_IDS_TO_SCAN) { |
| 82 | tryConnectDisplayLocked(builtInDisplayId); |
| 83 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 86 | private void tryConnectDisplayLocked(int builtInDisplayId) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 87 | IBinder displayToken = SurfaceControl.getBuiltInDisplay(builtInDisplayId); |
Dan Stoza | 0010105 | 2014-05-02 15:23:40 -0700 | [diff] [blame] | 88 | if (displayToken != null) { |
| 89 | SurfaceControl.PhysicalDisplayInfo[] configs = |
| 90 | SurfaceControl.getDisplayConfigs(displayToken); |
| 91 | if (configs == null) { |
| 92 | // There are no valid configs for this device, so we can't use it |
| 93 | Slog.w(TAG, "No valid configs found for display device " + |
| 94 | builtInDisplayId); |
| 95 | return; |
| 96 | } |
| 97 | int activeConfig = SurfaceControl.getActiveConfig(displayToken); |
| 98 | if (activeConfig < 0) { |
| 99 | // There is no active config, and for now we don't have the |
| 100 | // policy to set one. |
| 101 | Slog.w(TAG, "No active config found for display device " + |
| 102 | builtInDisplayId); |
| 103 | return; |
| 104 | } |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 105 | 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 " + |
| 111 | builtInDisplayId); |
| 112 | activeColorMode = Display.COLOR_MODE_INVALID; |
| 113 | } |
| 114 | int[] colorModes = SurfaceControl.getDisplayColorModes(displayToken); |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 115 | LocalDisplayDevice device = mDevices.get(builtInDisplayId); |
| 116 | if (device == null) { |
| 117 | // Display was added. |
Dan Stoza | 0010105 | 2014-05-02 15:23:40 -0700 | [diff] [blame] | 118 | device = new LocalDisplayDevice(displayToken, builtInDisplayId, |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 119 | configs, activeConfig, colorModes, activeColorMode); |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 120 | mDevices.put(builtInDisplayId, device); |
| 121 | sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_ADDED); |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 122 | } else if (device.updatePhysicalDisplayInfoLocked(configs, activeConfig, |
| 123 | colorModes, activeColorMode)) { |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 124 | // Display properties changed. |
| 125 | sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_CHANGED); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 126 | } |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 127 | } else { |
| 128 | // The display is no longer available. Ignore the attempt to add it. |
| 129 | // If it was connected but has already been disconnected, we'll get a |
| 130 | // disconnect event that will remove it from mDevices. |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private void tryDisconnectDisplayLocked(int builtInDisplayId) { |
| 135 | LocalDisplayDevice device = mDevices.get(builtInDisplayId); |
| 136 | if (device != null) { |
| 137 | // Display was removed. |
| 138 | mDevices.remove(builtInDisplayId); |
| 139 | sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_REMOVED); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 140 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Prashant Malani | c55929a | 2014-05-25 01:59:21 -0700 | [diff] [blame] | 143 | static int getPowerModeForState(int state) { |
| 144 | switch (state) { |
| 145 | case Display.STATE_OFF: |
| 146 | return SurfaceControl.POWER_MODE_OFF; |
Jeff Brown | 5dc2191 | 2014-07-17 18:50:18 -0700 | [diff] [blame] | 147 | case Display.STATE_DOZE: |
Prashant Malani | c55929a | 2014-05-25 01:59:21 -0700 | [diff] [blame] | 148 | return SurfaceControl.POWER_MODE_DOZE; |
Jeff Brown | 5dc2191 | 2014-07-17 18:50:18 -0700 | [diff] [blame] | 149 | case Display.STATE_DOZE_SUSPEND: |
| 150 | return SurfaceControl.POWER_MODE_DOZE_SUSPEND; |
Prashant Malani | c55929a | 2014-05-25 01:59:21 -0700 | [diff] [blame] | 151 | default: |
| 152 | return SurfaceControl.POWER_MODE_NORMAL; |
| 153 | } |
Jeff Brown | 44b1f76 | 2014-04-22 18:07:24 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 156 | private final class LocalDisplayDevice extends DisplayDevice { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 157 | private final int mBuiltInDisplayId; |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 158 | private final Light mBacklight; |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 159 | private final SparseArray<DisplayModeRecord> mSupportedModes = new SparseArray<>(); |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 160 | private final ArrayList<Integer> mSupportedColorModes = new ArrayList<>(); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 161 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 162 | private DisplayDeviceInfo mInfo; |
| 163 | private boolean mHavePendingChanges; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 164 | private int mState = Display.STATE_UNKNOWN; |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 165 | private int mBrightness = PowerManager.BRIGHTNESS_DEFAULT; |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 166 | private int mActivePhysIndex; |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 167 | private int mDefaultModeId; |
| 168 | private int mActiveModeId; |
| 169 | private boolean mActiveModeInvalid; |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 170 | private int mActiveColorMode; |
| 171 | private boolean mActiveColorModeInvalid; |
Michael Wright | 9ff94c0 | 2016-03-30 18:05:40 -0700 | [diff] [blame] | 172 | private Display.HdrCapabilities mHdrCapabilities; |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 173 | |
| 174 | private SurfaceControl.PhysicalDisplayInfo mDisplayInfos[]; |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 175 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 176 | public LocalDisplayDevice(IBinder displayToken, int builtInDisplayId, |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 177 | SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo, |
| 178 | int[] colorModes, int activeColorMode) { |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 179 | super(LocalDisplayAdapter.this, displayToken, UNIQUE_ID_PREFIX + builtInDisplayId); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 180 | mBuiltInDisplayId = builtInDisplayId; |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 181 | updatePhysicalDisplayInfoLocked(physicalDisplayInfos, activeDisplayInfo, |
| 182 | colorModes, activeColorMode); |
| 183 | updateColorModesLocked(colorModes, activeColorMode); |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 184 | if (mBuiltInDisplayId == SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) { |
| 185 | LightsManager lights = LocalServices.getService(LightsManager.class); |
| 186 | mBacklight = lights.getLight(LightsManager.LIGHT_ID_BACKLIGHT); |
| 187 | } else { |
| 188 | mBacklight = null; |
| 189 | } |
Michael Wright | 9ff94c0 | 2016-03-30 18:05:40 -0700 | [diff] [blame] | 190 | mHdrCapabilities = SurfaceControl.getHdrCapabilities(displayToken); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 193 | @Override |
| 194 | public boolean hasStableUniqueId() { |
| 195 | return true; |
| 196 | } |
| 197 | |
Michael Wright | 3f145a2 | 2014-07-22 19:46:03 -0700 | [diff] [blame] | 198 | public boolean updatePhysicalDisplayInfoLocked( |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 199 | SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo, |
| 200 | int[] colorModes, int activeColorMode) { |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 201 | mDisplayInfos = Arrays.copyOf(physicalDisplayInfos, physicalDisplayInfos.length); |
| 202 | mActivePhysIndex = activeDisplayInfo; |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 203 | // Build an updated list of all existing modes. |
| 204 | ArrayList<DisplayModeRecord> records = new ArrayList<DisplayModeRecord>(); |
| 205 | boolean modesAdded = false; |
| 206 | for (int i = 0; i < physicalDisplayInfos.length; i++) { |
| 207 | SurfaceControl.PhysicalDisplayInfo info = physicalDisplayInfos[i]; |
| 208 | // First, check to see if we've already added a matching mode. Since not all |
| 209 | // configuration options are exposed via Display.Mode, it's possible that we have |
| 210 | // multiple PhysicalDisplayInfos that would generate the same Display.Mode. |
| 211 | boolean existingMode = false; |
| 212 | for (int j = 0; j < records.size(); j++) { |
| 213 | if (records.get(j).hasMatchingMode(info)) { |
| 214 | existingMode = true; |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | if (existingMode) { |
| 219 | continue; |
| 220 | } |
| 221 | // If we haven't already added a mode for this configuration to the new set of |
| 222 | // supported modes then check to see if we have one in the prior set of supported |
| 223 | // modes to reuse. |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 224 | DisplayModeRecord record = findDisplayModeRecord(info); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 225 | if (record == null) { |
| 226 | record = new DisplayModeRecord(info); |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 227 | modesAdded = true; |
| 228 | } |
| 229 | records.add(record); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Get the currently active mode |
| 233 | DisplayModeRecord activeRecord = null; |
| 234 | for (int i = 0; i < records.size(); i++) { |
| 235 | DisplayModeRecord record = records.get(i); |
| 236 | if (record.hasMatchingMode(physicalDisplayInfos[activeDisplayInfo])){ |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 237 | activeRecord = record; |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 238 | break; |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 239 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 240 | } |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 241 | // Check whether surface flinger spontaneously changed modes out from under us. Schedule |
| 242 | // traversals to ensure that the correct state is reapplied if necessary. |
| 243 | if (mActiveModeId != 0 |
| 244 | && mActiveModeId != activeRecord.mMode.getModeId()) { |
| 245 | mActiveModeInvalid = true; |
| 246 | sendTraversalRequestLocked(); |
| 247 | } |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 248 | |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 249 | boolean recordsChanged = records.size() != mSupportedModes.size() || modesAdded; |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 250 | // If the records haven't changed then we're done here. |
| 251 | if (!recordsChanged) { |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 252 | return false; |
| 253 | } |
| 254 | // Update the index of modes. |
| 255 | mHavePendingChanges = true; |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 256 | |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 257 | mSupportedModes.clear(); |
| 258 | for (DisplayModeRecord record : records) { |
| 259 | mSupportedModes.put(record.mMode.getModeId(), record); |
| 260 | } |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 261 | // Update the default mode, if needed. |
| 262 | if (findDisplayInfoIndexLocked(mDefaultModeId) < 0) { |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 263 | if (mDefaultModeId != 0) { |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 264 | Slog.w(TAG, "Default display mode no longer available, using currently" |
| 265 | + " active mode as default."); |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 266 | } |
| 267 | mDefaultModeId = activeRecord.mMode.getModeId(); |
| 268 | } |
| 269 | // Determine whether the active mode is still there. |
| 270 | if (mSupportedModes.indexOfKey(mActiveModeId) < 0) { |
| 271 | if (mActiveModeId != 0) { |
| 272 | Slog.w(TAG, "Active display mode no longer available, reverting to default" |
| 273 | + " mode."); |
| 274 | } |
| 275 | mActiveModeId = mDefaultModeId; |
| 276 | mActiveModeInvalid = true; |
| 277 | } |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 278 | |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 279 | // Schedule traversals so that we apply pending changes. |
| 280 | sendTraversalRequestLocked(); |
| 281 | return true; |
| 282 | } |
| 283 | |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 284 | private boolean updateColorModesLocked(int[] colorModes, |
| 285 | int activeColorMode) { |
| 286 | List<Integer> pendingColorModes = new ArrayList<>(); |
| 287 | |
thecrazyskull | b34c8104 | 2016-12-18 12:48:20 -0500 | [diff] [blame] | 288 | if (colorModes == null) return false; |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 289 | // Build an updated list of all existing color modes. |
| 290 | boolean colorModesAdded = false; |
| 291 | for (int colorMode: colorModes) { |
| 292 | if (!mSupportedColorModes.contains(colorMode)) { |
| 293 | colorModesAdded = true; |
| 294 | } |
| 295 | pendingColorModes.add(colorMode); |
| 296 | } |
| 297 | |
| 298 | boolean colorModesChanged = |
| 299 | pendingColorModes.size() != mSupportedColorModes.size() |
| 300 | || colorModesAdded; |
| 301 | |
| 302 | // If the supported color modes haven't changed then we're done here. |
| 303 | if (!colorModesChanged) { |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | mHavePendingChanges = true; |
| 308 | |
| 309 | mSupportedColorModes.clear(); |
| 310 | mSupportedColorModes.addAll(pendingColorModes); |
| 311 | Collections.sort(mSupportedColorModes); |
| 312 | |
| 313 | // Determine whether the active color mode is still there. |
| 314 | if (!mSupportedColorModes.contains(mActiveColorMode)) { |
| 315 | if (mActiveColorMode != 0) { |
| 316 | Slog.w(TAG, "Active color mode no longer available, reverting" |
| 317 | + " to default mode."); |
| 318 | mActiveColorMode = Display.COLOR_MODE_DEFAULT; |
| 319 | mActiveColorModeInvalid = true; |
| 320 | } else { |
| 321 | if (!mSupportedColorModes.isEmpty()) { |
| 322 | // This should never happen. |
| 323 | Slog.e(TAG, "Default and active color mode is no longer available!" |
| 324 | + " Reverting to first available mode."); |
| 325 | mActiveColorMode = mSupportedColorModes.get(0); |
| 326 | mActiveColorModeInvalid = true; |
| 327 | } else { |
| 328 | // This should really never happen. |
| 329 | Slog.e(TAG, "No color modes available!"); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | return true; |
| 334 | } |
| 335 | |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 336 | private DisplayModeRecord findDisplayModeRecord(SurfaceControl.PhysicalDisplayInfo info) { |
| 337 | for (int i = 0; i < mSupportedModes.size(); i++) { |
| 338 | DisplayModeRecord record = mSupportedModes.valueAt(i); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 339 | if (record.hasMatchingMode(info)) { |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 340 | return record; |
| 341 | } |
| 342 | } |
| 343 | return null; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | @Override |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 347 | public void applyPendingDisplayDeviceInfoChangesLocked() { |
| 348 | if (mHavePendingChanges) { |
| 349 | mInfo = null; |
| 350 | mHavePendingChanges = false; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 351 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 352 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 353 | |
| 354 | @Override |
| 355 | public DisplayDeviceInfo getDisplayDeviceInfoLocked() { |
| 356 | if (mInfo == null) { |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 357 | SurfaceControl.PhysicalDisplayInfo phys = mDisplayInfos[mActivePhysIndex]; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 358 | mInfo = new DisplayDeviceInfo(); |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 359 | mInfo.width = phys.width; |
| 360 | mInfo.height = phys.height; |
| 361 | mInfo.modeId = mActiveModeId; |
| 362 | mInfo.defaultModeId = mDefaultModeId; |
| 363 | mInfo.supportedModes = new Display.Mode[mSupportedModes.size()]; |
| 364 | for (int i = 0; i < mSupportedModes.size(); i++) { |
| 365 | DisplayModeRecord record = mSupportedModes.valueAt(i); |
| 366 | mInfo.supportedModes[i] = record.mMode; |
| 367 | } |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 368 | mInfo.colorMode = mActiveColorMode; |
| 369 | mInfo.supportedColorModes = |
| 370 | new int[mSupportedColorModes.size()]; |
| 371 | for (int i = 0; i < mSupportedColorModes.size(); i++) { |
| 372 | mInfo.supportedColorModes[i] = mSupportedColorModes.get(i); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 373 | } |
Michael Wright | 9ff94c0 | 2016-03-30 18:05:40 -0700 | [diff] [blame] | 374 | mInfo.hdrCapabilities = mHdrCapabilities; |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 375 | mInfo.appVsyncOffsetNanos = phys.appVsyncOffsetNanos; |
| 376 | mInfo.presentationDeadlineNanos = phys.presentationDeadlineNanos; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 377 | mInfo.state = mState; |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 378 | mInfo.uniqueId = getUniqueId(); |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 379 | |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 380 | // Assume that all built-in displays that have secure output (eg. HDCP) also |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 381 | // support compositing from gralloc protected buffers. |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 382 | if (phys.secure) { |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 383 | mInfo.flags = DisplayDeviceInfo.FLAG_SECURE |
| 384 | | DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS; |
| 385 | } |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 386 | |
Michael Wright | 4b0fa17 | 2016-03-11 17:15:24 -0800 | [diff] [blame] | 387 | final Resources res = getContext().getResources(); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 388 | if (mBuiltInDisplayId == SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) { |
Adam Powell | 49e7ff9 | 2015-05-14 16:18:53 -0700 | [diff] [blame] | 389 | mInfo.name = res.getString( |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 390 | com.android.internal.R.string.display_manager_built_in_display_name); |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 391 | mInfo.flags |= DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY |
Jeff Brown | 27f1d67 | 2012-10-17 18:32:34 -0700 | [diff] [blame] | 392 | | DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT; |
Adam Powell | 01f280d | 2015-05-18 16:07:42 -0700 | [diff] [blame] | 393 | if (res.getBoolean(com.android.internal.R.bool.config_mainBuiltInDisplayIsRound) |
Griff Hazen | d3c454d | 2016-03-25 07:30:34 -0700 | [diff] [blame] | 394 | || (Build.IS_EMULATOR |
Adam Powell | 01f280d | 2015-05-18 16:07:42 -0700 | [diff] [blame] | 395 | && SystemProperties.getBoolean(PROPERTY_EMULATOR_CIRCULAR, false))) { |
Adam Powell | 49e7ff9 | 2015-05-14 16:18:53 -0700 | [diff] [blame] | 396 | mInfo.flags |= DisplayDeviceInfo.FLAG_ROUND; |
| 397 | } |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 398 | mInfo.type = Display.TYPE_BUILT_IN; |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 399 | mInfo.densityDpi = (int)(phys.density * 160 + 0.5f); |
| 400 | mInfo.xDpi = phys.xDpi; |
| 401 | mInfo.yDpi = phys.yDpi; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 402 | mInfo.touch = DisplayDeviceInfo.TOUCH_INTERNAL; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 403 | } else { |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 404 | mInfo.type = Display.TYPE_HDMI; |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 405 | mInfo.flags |= DisplayDeviceInfo.FLAG_PRESENTATION; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 406 | mInfo.name = getContext().getResources().getString( |
| 407 | com.android.internal.R.string.display_manager_hdmi_display_name); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 408 | mInfo.touch = DisplayDeviceInfo.TOUCH_EXTERNAL; |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 409 | mInfo.setAssumedDensityForExternalDisplay(phys.width, phys.height); |
Jeff Brown | 27f1d67 | 2012-10-17 18:32:34 -0700 | [diff] [blame] | 410 | |
| 411 | // For demonstration purposes, allow rotation of the external display. |
| 412 | // In the future we might allow the user to configure this directly. |
| 413 | if ("portrait".equals(SystemProperties.get("persist.demo.hdmirotation"))) { |
| 414 | mInfo.rotation = Surface.ROTATION_270; |
| 415 | } |
Scott Anderson | 8786ed9 | 2013-11-01 13:27:39 -0700 | [diff] [blame] | 416 | |
| 417 | // For demonstration purposes, allow rotation of the external display |
| 418 | // to follow the built-in display. |
| 419 | if (SystemProperties.getBoolean("persist.demo.hdmirotates", false)) { |
| 420 | mInfo.flags |= DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT; |
| 421 | } |
Michael Wright | 4b0fa17 | 2016-03-11 17:15:24 -0800 | [diff] [blame] | 422 | |
| 423 | if (!res.getBoolean( |
| 424 | com.android.internal.R.bool.config_localDisplaysMirrorContent)) { |
| 425 | mInfo.flags |= DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY; |
| 426 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | return mInfo; |
| 430 | } |
| 431 | |
| 432 | @Override |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 433 | public Runnable requestDisplayStateLocked(final int state, final int brightness) { |
| 434 | // Assume that the brightness is off if the display is being turned off. |
| 435 | assert state != Display.STATE_OFF || brightness == PowerManager.BRIGHTNESS_OFF; |
| 436 | |
| 437 | final boolean stateChanged = (mState != state); |
| 438 | final boolean brightnessChanged = (mBrightness != brightness) && mBacklight != null; |
| 439 | if (stateChanged || brightnessChanged) { |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 440 | final int displayId = mBuiltInDisplayId; |
| 441 | final IBinder token = getDisplayTokenLocked(); |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 442 | final int oldState = mState; |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 443 | |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 444 | if (stateChanged) { |
| 445 | mState = state; |
| 446 | updateDeviceInfoLocked(); |
| 447 | } |
| 448 | |
| 449 | if (brightnessChanged) { |
| 450 | mBrightness = brightness; |
| 451 | } |
| 452 | |
| 453 | // Defer actually setting the display state until after we have exited |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 454 | // the critical section since it can take hundreds of milliseconds |
| 455 | // to complete. |
| 456 | return new Runnable() { |
| 457 | @Override |
| 458 | public void run() { |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 459 | // Exit a suspended state before making any changes. |
| 460 | int currentState = oldState; |
| 461 | if (Display.isSuspendedState(oldState) |
| 462 | || oldState == Display.STATE_UNKNOWN) { |
| 463 | if (!Display.isSuspendedState(state)) { |
| 464 | setDisplayState(state); |
| 465 | currentState = state; |
| 466 | } else if (state == Display.STATE_DOZE_SUSPEND |
| 467 | || oldState == Display.STATE_DOZE_SUSPEND) { |
| 468 | setDisplayState(Display.STATE_DOZE); |
| 469 | currentState = Display.STATE_DOZE; |
| 470 | } else { |
| 471 | return; // old state and new state is off |
| 472 | } |
| 473 | } |
| 474 | |
Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 475 | // If the state change was from or to VR, then we need to tell the light |
Karthik Ravi Shankar | 6685531 | 2017-10-04 13:30:13 -0700 | [diff] [blame] | 476 | // so that it can apply appropriate VR brightness settings. Also, update the |
| 477 | // brightness so the state is propogated to light. |
| 478 | boolean vrModeChange = false; |
Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 479 | if ((state == Display.STATE_VR || currentState == Display.STATE_VR) && |
| 480 | currentState != state) { |
| 481 | setVrMode(state == Display.STATE_VR); |
Karthik Ravi Shankar | 6685531 | 2017-10-04 13:30:13 -0700 | [diff] [blame] | 482 | vrModeChange = true; |
Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 486 | // Apply brightness changes given that we are in a non-suspended state. |
Karthik Ravi Shankar | 6685531 | 2017-10-04 13:30:13 -0700 | [diff] [blame] | 487 | if (brightnessChanged || vrModeChange) { |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 488 | setDisplayBrightness(brightness); |
| 489 | } |
| 490 | |
| 491 | // Enter the final desired state, possibly suspended. |
| 492 | if (state != currentState) { |
| 493 | setDisplayState(state); |
| 494 | } |
| 495 | } |
| 496 | |
Santos Cordon | d6a5660 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 497 | private void setVrMode(boolean isVrEnabled) { |
| 498 | if (DEBUG) { |
| 499 | Slog.d(TAG, "setVrMode(" |
| 500 | + "id=" + displayId |
| 501 | + ", state=" + Display.stateToString(state) + ")"); |
| 502 | } |
| 503 | mBacklight.setVrMode(isVrEnabled); |
| 504 | } |
| 505 | |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 506 | private void setDisplayState(int state) { |
| 507 | if (DEBUG) { |
| 508 | Slog.d(TAG, "setDisplayState(" |
| 509 | + "id=" + displayId |
| 510 | + ", state=" + Display.stateToString(state) + ")"); |
| 511 | } |
| 512 | |
| 513 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "setDisplayState(" |
| 514 | + "id=" + displayId |
| 515 | + ", state=" + Display.stateToString(state) + ")"); |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 516 | try { |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 517 | final int mode = getPowerModeForState(state); |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 518 | SurfaceControl.setDisplayPowerMode(token, mode); |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 519 | Trace.traceCounter(Trace.TRACE_TAG_POWER, "DisplayPowerMode", mode); |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 520 | } finally { |
| 521 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 522 | } |
| 523 | } |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 524 | |
| 525 | private void setDisplayBrightness(int brightness) { |
| 526 | if (DEBUG) { |
| 527 | Slog.d(TAG, "setDisplayBrightness(" |
| 528 | + "id=" + displayId + ", brightness=" + brightness + ")"); |
| 529 | } |
| 530 | |
| 531 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "setDisplayBrightness(" |
| 532 | + "id=" + displayId + ", brightness=" + brightness + ")"); |
| 533 | try { |
| 534 | mBacklight.setBrightness(brightness); |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 535 | Trace.traceCounter(Trace.TRACE_TAG_POWER, |
Michael Wright | a9f37ab | 2017-08-15 17:14:20 +0100 | [diff] [blame] | 536 | "ScreenBrightness", brightness); |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 537 | } finally { |
| 538 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 539 | } |
| 540 | } |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 541 | }; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 542 | } |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 543 | return null; |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | @Override |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 547 | public void requestDisplayModesInTransactionLocked( |
| 548 | int colorMode, int modeId) { |
| 549 | if (requestModeInTransactionLocked(modeId) || |
| 550 | requestColorModeInTransactionLocked(colorMode)) { |
| 551 | updateDeviceInfoLocked(); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | public boolean requestModeInTransactionLocked(int modeId) { |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 556 | if (modeId == 0) { |
| 557 | modeId = mDefaultModeId; |
| 558 | } else if (mSupportedModes.indexOfKey(modeId) < 0) { |
| 559 | Slog.w(TAG, "Requested mode " + modeId + " is not supported by this display," |
| 560 | + " reverting to default display mode."); |
| 561 | modeId = mDefaultModeId; |
| 562 | } |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 563 | |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 564 | int physIndex = findDisplayInfoIndexLocked(modeId); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 565 | if (physIndex < 0) { |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 566 | Slog.w(TAG, "Requested mode ID " + modeId + " not available," |
| 567 | + " trying with default mode ID"); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 568 | modeId = mDefaultModeId; |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 569 | physIndex = findDisplayInfoIndexLocked(modeId); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 570 | } |
Jorim Jaggi | 3b3cbb5 | 2015-09-24 17:35:58 -0700 | [diff] [blame] | 571 | if (mActivePhysIndex == physIndex) { |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 572 | return false; |
Michael Wright | 3f145a2 | 2014-07-22 19:46:03 -0700 | [diff] [blame] | 573 | } |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 574 | SurfaceControl.setActiveConfig(getDisplayTokenLocked(), physIndex); |
| 575 | mActivePhysIndex = physIndex; |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 576 | mActiveModeId = modeId; |
| 577 | mActiveModeInvalid = false; |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 578 | return true; |
| 579 | } |
| 580 | |
| 581 | public boolean requestColorModeInTransactionLocked(int colorMode) { |
| 582 | if (mActiveColorMode == colorMode) { |
| 583 | return false; |
| 584 | } |
| 585 | if (!mSupportedColorModes.contains(colorMode)) { |
| 586 | Slog.w(TAG, "Unable to find color mode " + colorMode |
| 587 | + ", ignoring request."); |
| 588 | return false; |
| 589 | } |
| 590 | SurfaceControl.setActiveColorMode(getDisplayTokenLocked(), colorMode); |
| 591 | mActiveColorMode = colorMode; |
| 592 | mActiveColorModeInvalid = false; |
| 593 | return true; |
Michael Wright | 3f145a2 | 2014-07-22 19:46:03 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | @Override |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 597 | public void dumpLocked(PrintWriter pw) { |
| 598 | super.dumpLocked(pw); |
| 599 | pw.println("mBuiltInDisplayId=" + mBuiltInDisplayId); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 600 | pw.println("mActivePhysIndex=" + mActivePhysIndex); |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 601 | pw.println("mActiveModeId=" + mActiveModeId); |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 602 | pw.println("mActiveColorMode=" + mActiveColorMode); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 603 | pw.println("mState=" + Display.stateToString(mState)); |
Jeff Brown | 5d6443b | 2015-04-10 20:15:01 -0700 | [diff] [blame] | 604 | pw.println("mBrightness=" + mBrightness); |
| 605 | pw.println("mBacklight=" + mBacklight); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 606 | pw.println("mDisplayInfos="); |
| 607 | for (int i = 0; i < mDisplayInfos.length; i++) { |
| 608 | pw.println(" " + mDisplayInfos[i]); |
| 609 | } |
| 610 | pw.println("mSupportedModes="); |
| 611 | for (int i = 0; i < mSupportedModes.size(); i++) { |
| 612 | pw.println(" " + mSupportedModes.valueAt(i)); |
| 613 | } |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 614 | pw.print("mSupportedColorModes=["); |
| 615 | for (int i = 0; i < mSupportedColorModes.size(); i++) { |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 616 | if (i != 0) { |
| 617 | pw.print(", "); |
| 618 | } |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 619 | pw.print(mSupportedColorModes.get(i)); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 620 | } |
| 621 | pw.println("]"); |
| 622 | } |
| 623 | |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 624 | private int findDisplayInfoIndexLocked(int modeId) { |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 625 | DisplayModeRecord record = mSupportedModes.get(modeId); |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 626 | if (record != null) { |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 627 | for (int i = 0; i < mDisplayInfos.length; i++) { |
| 628 | SurfaceControl.PhysicalDisplayInfo info = mDisplayInfos[i]; |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 629 | if (record.hasMatchingMode(info)){ |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 630 | return i; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | return -1; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | private void updateDeviceInfoLocked() { |
| 638 | mInfo = null; |
| 639 | sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 640 | } |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 641 | } |
Michael Wright | 3f145a2 | 2014-07-22 19:46:03 -0700 | [diff] [blame] | 642 | |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 643 | /** |
| 644 | * Keeps track of a display configuration. |
| 645 | */ |
| 646 | private static final class DisplayModeRecord { |
| 647 | public final Display.Mode mMode; |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 648 | |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 649 | public DisplayModeRecord(SurfaceControl.PhysicalDisplayInfo phys) { |
P.Y. Laligand | b3b9eb3 | 2015-05-11 15:02:07 -0700 | [diff] [blame] | 650 | mMode = createMode(phys.width, phys.height, phys.refreshRate); |
Michael Wright | 58e829f | 2015-09-15 00:13:26 +0100 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Returns whether the mode generated by the given PhysicalDisplayInfo matches the mode |
| 655 | * contained by the record modulo mode ID. |
| 656 | * |
| 657 | * Note that this doesn't necessarily mean the the PhysicalDisplayInfos are identical, just |
| 658 | * that they generate identical modes. |
| 659 | */ |
| 660 | public boolean hasMatchingMode(SurfaceControl.PhysicalDisplayInfo info) { |
| 661 | int modeRefreshRate = Float.floatToIntBits(mMode.getRefreshRate()); |
| 662 | int displayInfoRefreshRate = Float.floatToIntBits(info.refreshRate); |
| 663 | return mMode.getPhysicalWidth() == info.width |
| 664 | && mMode.getPhysicalHeight() == info.height |
| 665 | && modeRefreshRate == displayInfoRefreshRate; |
| 666 | } |
| 667 | |
| 668 | public String toString() { |
| 669 | return "DisplayModeRecord{mMode=" + mMode + "}"; |
Michael Wright | 3f145a2 | 2014-07-22 19:46:03 -0700 | [diff] [blame] | 670 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 671 | } |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 672 | |
| 673 | private final class HotplugDisplayEventReceiver extends DisplayEventReceiver { |
| 674 | public HotplugDisplayEventReceiver(Looper looper) { |
Jorim Jaggi | 34a0cdb | 2017-06-08 15:40:38 -0700 | [diff] [blame] | 675 | super(looper, VSYNC_SOURCE_APP); |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | @Override |
| 679 | public void onHotplug(long timestampNanos, int builtInDisplayId, boolean connected) { |
| 680 | synchronized (getSyncRoot()) { |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 681 | if (connected) { |
| 682 | tryConnectDisplayLocked(builtInDisplayId); |
| 683 | } else { |
| 684 | tryDisconnectDisplayLocked(builtInDisplayId); |
| 685 | } |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | } |
Dan Stoza | 0010105 | 2014-05-02 15:23:40 -0700 | [diff] [blame] | 689 | } |