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