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