blob: be37f524eb8d6f442742d69196147672d8c8bf4f [file] [log] [blame]
Jeff Brown64a55af2012-08-26 02:47:39 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.display;
18
Adam Powell49e7ff92015-05-14 16:18:53 -070019import android.content.res.Resources;
Adam Powell01f280d2015-05-18 16:07:42 -070020import android.os.Build;
Jeff Brown5d6443b2015-04-10 20:15:01 -070021import com.android.server.LocalServices;
22import com.android.server.lights.Light;
23import com.android.server.lights.LightsManager;
24
Jeff Brown64a55af2012-08-26 02:47:39 -070025import android.content.Context;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070026import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070027import android.os.IBinder;
Jeff Browne87bf032012-09-20 18:30:13 -070028import android.os.Looper;
Jeff Brown5d6443b2015-04-10 20:15:01 -070029import android.os.PowerManager;
Jeff Brown27f1d672012-10-17 18:32:34 -070030import android.os.SystemProperties;
Jeff Brown3edf5272014-08-14 19:25:14 -070031import android.os.Trace;
Dan Stoza00101052014-05-02 15:23:40 -070032import android.util.Slog;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070033import android.util.SparseArray;
Michael Wright58e829f2015-09-15 00:13:26 +010034import android.util.SparseBooleanArray;
Jeff Brown92130f62012-10-24 21:28:33 -070035import android.view.Display;
Jeff Browne87bf032012-09-20 18:30:13 -070036import android.view.DisplayEventReceiver;
Jeff Brown64a55af2012-08-26 02:47:39 -070037import android.view.Surface;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080038import android.view.SurfaceControl;
Jeff Brown64a55af2012-08-26 02:47:39 -070039
Jeff Brown4ed8fe72012-08-30 18:18:29 -070040import java.io.PrintWriter;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -070041import java.util.ArrayList;
Michael Wright58e829f2015-09-15 00:13:26 +010042import java.util.Arrays;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070043
Jeff Brown64a55af2012-08-26 02:47:39 -070044/**
45 * A display adapter for the local displays managed by Surface Flinger.
Jeff Brownbd6e1502012-08-28 03:27:37 -070046 * <p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -070047 * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock.
Jeff Brownbd6e1502012-08-28 03:27:37 -070048 * </p>
Jeff Brown64a55af2012-08-26 02:47:39 -070049 */
Jeff Brown4ed8fe72012-08-30 18:18:29 -070050final class LocalDisplayAdapter extends DisplayAdapter {
Jeff Brownbd6e1502012-08-28 03:27:37 -070051 private static final String TAG = "LocalDisplayAdapter";
Jeff Brown5d6443b2015-04-10 20:15:01 -070052 private static final boolean DEBUG = false;
Jeff Brown64a55af2012-08-26 02:47:39 -070053
Wale Ogunwale361ca212014-11-20 11:42:38 -080054 private static final String UNIQUE_ID_PREFIX = "local:";
55
Adam Powell01f280d2015-05-18 16:07:42 -070056 private static final String PROPERTY_EMULATOR_CIRCULAR = "ro.emulator.circular";
57
Jeff Brown4ed8fe72012-08-30 18:18:29 -070058 private static final int[] BUILT_IN_DISPLAY_IDS_TO_SCAN = new int[] {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080059 SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN,
60 SurfaceControl.BUILT_IN_DISPLAY_ID_HDMI,
Jeff Brown4ed8fe72012-08-30 18:18:29 -070061 };
62
63 private final SparseArray<LocalDisplayDevice> mDevices =
64 new SparseArray<LocalDisplayDevice>();
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -070065 @SuppressWarnings("unused") // Becomes active at instantiation time.
Jeff Brown66692502012-10-18 16:13:44 -070066 private HotplugDisplayEventReceiver mHotplugReceiver;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070067
Jeff Brown66692502012-10-18 16:13:44 -070068 // Called with SyncRoot lock held.
Jeff Brown4ed8fe72012-08-30 18:18:29 -070069 public LocalDisplayAdapter(DisplayManagerService.SyncRoot syncRoot,
70 Context context, Handler handler, Listener listener) {
71 super(syncRoot, context, handler, listener, TAG);
Jeff Brown64a55af2012-08-26 02:47:39 -070072 }
73
74 @Override
Jeff Brown4ed8fe72012-08-30 18:18:29 -070075 public void registerLocked() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -070076 super.registerLocked();
Jeff Brown66692502012-10-18 16:13:44 -070077
78 mHotplugReceiver = new HotplugDisplayEventReceiver(getHandler().getLooper());
Jesse Halle244db42012-11-08 11:55:14 -080079
80 for (int builtInDisplayId : BUILT_IN_DISPLAY_IDS_TO_SCAN) {
81 tryConnectDisplayLocked(builtInDisplayId);
82 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -070083 }
84
Jesse Halle244db42012-11-08 11:55:14 -080085 private void tryConnectDisplayLocked(int builtInDisplayId) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080086 IBinder displayToken = SurfaceControl.getBuiltInDisplay(builtInDisplayId);
Dan Stoza00101052014-05-02 15:23:40 -070087 if (displayToken != null) {
88 SurfaceControl.PhysicalDisplayInfo[] configs =
89 SurfaceControl.getDisplayConfigs(displayToken);
90 if (configs == null) {
91 // There are no valid configs for this device, so we can't use it
92 Slog.w(TAG, "No valid configs found for display device " +
93 builtInDisplayId);
94 return;
95 }
96 int activeConfig = SurfaceControl.getActiveConfig(displayToken);
97 if (activeConfig < 0) {
98 // There is no active config, and for now we don't have the
99 // policy to set one.
100 Slog.w(TAG, "No active config found for display device " +
101 builtInDisplayId);
102 return;
103 }
Jesse Halle244db42012-11-08 11:55:14 -0800104 LocalDisplayDevice device = mDevices.get(builtInDisplayId);
105 if (device == null) {
106 // Display was added.
Dan Stoza00101052014-05-02 15:23:40 -0700107 device = new LocalDisplayDevice(displayToken, builtInDisplayId,
Michael Wright3f145a22014-07-22 19:46:03 -0700108 configs, activeConfig);
Jesse Halle244db42012-11-08 11:55:14 -0800109 mDevices.put(builtInDisplayId, device);
110 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_ADDED);
Michael Wright3f145a22014-07-22 19:46:03 -0700111 } else if (device.updatePhysicalDisplayInfoLocked(configs, activeConfig)) {
Jesse Halle244db42012-11-08 11:55:14 -0800112 // Display properties changed.
113 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_CHANGED);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700114 }
Jesse Halle244db42012-11-08 11:55:14 -0800115 } else {
116 // The display is no longer available. Ignore the attempt to add it.
117 // If it was connected but has already been disconnected, we'll get a
118 // disconnect event that will remove it from mDevices.
119 }
120 }
121
122 private void tryDisconnectDisplayLocked(int builtInDisplayId) {
123 LocalDisplayDevice device = mDevices.get(builtInDisplayId);
124 if (device != null) {
125 // Display was removed.
126 mDevices.remove(builtInDisplayId);
127 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_REMOVED);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700128 }
Jeff Brown64a55af2012-08-26 02:47:39 -0700129 }
130
Prashant Malanic55929a2014-05-25 01:59:21 -0700131 static int getPowerModeForState(int state) {
132 switch (state) {
133 case Display.STATE_OFF:
134 return SurfaceControl.POWER_MODE_OFF;
Jeff Brown5dc21912014-07-17 18:50:18 -0700135 case Display.STATE_DOZE:
Prashant Malanic55929a2014-05-25 01:59:21 -0700136 return SurfaceControl.POWER_MODE_DOZE;
Jeff Brown5dc21912014-07-17 18:50:18 -0700137 case Display.STATE_DOZE_SUSPEND:
138 return SurfaceControl.POWER_MODE_DOZE_SUSPEND;
Prashant Malanic55929a2014-05-25 01:59:21 -0700139 default:
140 return SurfaceControl.POWER_MODE_NORMAL;
141 }
Jeff Brown44b1f762014-04-22 18:07:24 -0700142 }
143
Jeff Brown64a55af2012-08-26 02:47:39 -0700144 private final class LocalDisplayDevice extends DisplayDevice {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700145 private final int mBuiltInDisplayId;
Jeff Brown5d6443b2015-04-10 20:15:01 -0700146 private final Light mBacklight;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700147 private final SparseArray<DisplayModeRecord> mSupportedModes = new SparseArray<>();
Michael Wright58e829f2015-09-15 00:13:26 +0100148 private final SparseArray<Display.ColorTransform> mSupportedColorTransforms =
149 new SparseArray<>();
Jeff Brown64a55af2012-08-26 02:47:39 -0700150
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700151 private DisplayDeviceInfo mInfo;
152 private boolean mHavePendingChanges;
Jeff Brown037c33e2014-04-09 00:31:55 -0700153 private int mState = Display.STATE_UNKNOWN;
Jeff Brown5d6443b2015-04-10 20:15:01 -0700154 private int mBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Michael Wright58e829f2015-09-15 00:13:26 +0100155 private int mActivePhysIndex;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700156 private int mDefaultModeId;
157 private int mActiveModeId;
158 private boolean mActiveModeInvalid;
Michael Wright58e829f2015-09-15 00:13:26 +0100159 private int mDefaultColorTransformId;
160 private int mActiveColorTransformId;
161 private boolean mActiveColorTransformInvalid;
162
163 private SurfaceControl.PhysicalDisplayInfo mDisplayInfos[];
Jeff Brown5d6443b2015-04-10 20:15:01 -0700164
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700165 public LocalDisplayDevice(IBinder displayToken, int builtInDisplayId,
Michael Wright3f145a22014-07-22 19:46:03 -0700166 SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo) {
Wale Ogunwale361ca212014-11-20 11:42:38 -0800167 super(LocalDisplayAdapter.this, displayToken, UNIQUE_ID_PREFIX + builtInDisplayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700168 mBuiltInDisplayId = builtInDisplayId;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700169 updatePhysicalDisplayInfoLocked(physicalDisplayInfos, activeDisplayInfo);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700170 if (mBuiltInDisplayId == SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) {
171 LightsManager lights = LocalServices.getService(LightsManager.class);
172 mBacklight = lights.getLight(LightsManager.LIGHT_ID_BACKLIGHT);
173 } else {
174 mBacklight = null;
175 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700176 }
177
Michael Wright3f145a22014-07-22 19:46:03 -0700178 public boolean updatePhysicalDisplayInfoLocked(
179 SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo) {
Michael Wright58e829f2015-09-15 00:13:26 +0100180 mDisplayInfos = Arrays.copyOf(physicalDisplayInfos, physicalDisplayInfos.length);
181 mActivePhysIndex = activeDisplayInfo;
182 ArrayList<Display.ColorTransform> colorTransforms = new ArrayList<>();
183
184 // Build an updated list of all existing color transforms.
185 boolean colorTransformsAdded = false;
186 Display.ColorTransform activeColorTransform = null;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700187 for (int i = 0; i < physicalDisplayInfos.length; i++) {
188 SurfaceControl.PhysicalDisplayInfo info = physicalDisplayInfos[i];
Michael Wright58e829f2015-09-15 00:13:26 +0100189 // First check to see if we've already added this color transform
190 boolean existingMode = false;
191 for (int j = 0; j < colorTransforms.size(); j++) {
192 if (colorTransforms.get(j).getColorTransform() == info.colorTransform) {
193 existingMode = true;
194 break;
195 }
196 }
197 if (existingMode) {
198 continue;
199 }
200 Display.ColorTransform colorTransform = findColorTransform(info);
201 if (colorTransform == null) {
202 colorTransform = createColorTransform(info.colorTransform);
203 colorTransformsAdded = true;
204 }
205 colorTransforms.add(colorTransform);
206 if (i == activeDisplayInfo) {
207 activeColorTransform = colorTransform;
208 }
209 }
210
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. Laligandb3b9eb32015-05-11 15:02:07 -0700232 DisplayModeRecord record = findDisplayModeRecord(info);
Michael Wright58e829f2015-09-15 00:13:26 +0100233 if (record == null) {
234 record = new DisplayModeRecord(info);
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700235 modesAdded = true;
236 }
237 records.add(record);
Michael Wright58e829f2015-09-15 00:13:26 +0100238 }
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. Laligandb3b9eb32015-05-11 15:02:07 -0700245 activeRecord = record;
Michael Wright58e829f2015-09-15 00:13:26 +0100246 break;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700247 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700248 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700249 // 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 Wright58e829f2015-09-15 00:13:26 +0100256 // Check whether surface flinger spontaneously changed color transforms out from under
257 // us.
258 if (mActiveColorTransformId != 0
259 && mActiveColorTransformId != activeColorTransform.getId()) {
260 mActiveColorTransformInvalid = true;
261 sendTraversalRequestLocked();
262 }
263
264 boolean colorTransformsChanged =
265 colorTransforms.size() != mSupportedColorTransforms.size()
266 || colorTransformsAdded;
267 boolean recordsChanged = records.size() != mSupportedModes.size() || modesAdded;
268 // If neither the records nor the supported color transforms have changed then we're
269 // done here.
270 if (!recordsChanged && !colorTransformsChanged) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700271 return false;
272 }
273 // Update the index of modes.
274 mHavePendingChanges = true;
Michael Wright58e829f2015-09-15 00:13:26 +0100275
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700276 mSupportedModes.clear();
277 for (DisplayModeRecord record : records) {
278 mSupportedModes.put(record.mMode.getModeId(), record);
279 }
Michael Wright58e829f2015-09-15 00:13:26 +0100280 mSupportedColorTransforms.clear();
281 for (Display.ColorTransform colorTransform : colorTransforms) {
282 mSupportedColorTransforms.put(colorTransform.getId(), colorTransform);
283 }
284
285 // Update the default mode and color transform if needed. This needs to be done in
286 // tandem so we always have a default state to fall back to.
287 if (findDisplayInfoIndexLocked(mDefaultColorTransformId, mDefaultModeId) < 0) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700288 if (mDefaultModeId != 0) {
Michael Wright58e829f2015-09-15 00:13:26 +0100289 Slog.w(TAG, "Default display mode no longer available, using currently"
290 + " active mode as default.");
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700291 }
292 mDefaultModeId = activeRecord.mMode.getModeId();
Michael Wright58e829f2015-09-15 00:13:26 +0100293 if (mDefaultColorTransformId != 0) {
294 Slog.w(TAG, "Default color transform no longer available, using currently"
295 + " active color transform as default");
296 }
297 mDefaultColorTransformId = activeColorTransform.getId();
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700298 }
299 // Determine whether the active mode is still there.
300 if (mSupportedModes.indexOfKey(mActiveModeId) < 0) {
301 if (mActiveModeId != 0) {
302 Slog.w(TAG, "Active display mode no longer available, reverting to default"
303 + " mode.");
304 }
305 mActiveModeId = mDefaultModeId;
306 mActiveModeInvalid = true;
307 }
Michael Wright58e829f2015-09-15 00:13:26 +0100308
309 // Determine whether the active color transform is still there.
310 if (mSupportedColorTransforms.indexOfKey(mActiveColorTransformId) < 0) {
311 if (mActiveColorTransformId != 0) {
312 Slog.w(TAG, "Active color transform no longer available, reverting"
313 + " to default transform.");
314 }
315 mActiveColorTransformId = mDefaultColorTransformId;
316 mActiveColorTransformInvalid = true;
317 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700318 // Schedule traversals so that we apply pending changes.
319 sendTraversalRequestLocked();
320 return true;
321 }
322
323 private DisplayModeRecord findDisplayModeRecord(SurfaceControl.PhysicalDisplayInfo info) {
324 for (int i = 0; i < mSupportedModes.size(); i++) {
325 DisplayModeRecord record = mSupportedModes.valueAt(i);
Michael Wright58e829f2015-09-15 00:13:26 +0100326 if (record.hasMatchingMode(info)) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700327 return record;
328 }
329 }
330 return null;
Jeff Brown64a55af2012-08-26 02:47:39 -0700331 }
332
Michael Wright58e829f2015-09-15 00:13:26 +0100333 private Display.ColorTransform findColorTransform(SurfaceControl.PhysicalDisplayInfo info) {
334 for (int i = 0; i < mSupportedColorTransforms.size(); i++) {
335 Display.ColorTransform transform = mSupportedColorTransforms.valueAt(i);
336 if (transform.getColorTransform() == info.colorTransform) {
337 return transform;
338 }
339 }
340 return null;
341 }
342
Jeff Brown64a55af2012-08-26 02:47:39 -0700343 @Override
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700344 public void applyPendingDisplayDeviceInfoChangesLocked() {
345 if (mHavePendingChanges) {
346 mInfo = null;
347 mHavePendingChanges = false;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700348 }
Jeff Brown64a55af2012-08-26 02:47:39 -0700349 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700350
351 @Override
352 public DisplayDeviceInfo getDisplayDeviceInfoLocked() {
353 if (mInfo == null) {
Michael Wright58e829f2015-09-15 00:13:26 +0100354 SurfaceControl.PhysicalDisplayInfo phys = mDisplayInfos[mActivePhysIndex];
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700355 mInfo = new DisplayDeviceInfo();
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700356 mInfo.width = phys.width;
357 mInfo.height = phys.height;
358 mInfo.modeId = mActiveModeId;
359 mInfo.defaultModeId = mDefaultModeId;
360 mInfo.supportedModes = new Display.Mode[mSupportedModes.size()];
361 for (int i = 0; i < mSupportedModes.size(); i++) {
362 DisplayModeRecord record = mSupportedModes.valueAt(i);
363 mInfo.supportedModes[i] = record.mMode;
364 }
Michael Wright58e829f2015-09-15 00:13:26 +0100365 mInfo.colorTransformId = mActiveColorTransformId;
366 mInfo.defaultColorTransformId = mDefaultColorTransformId;
367 mInfo.supportedColorTransforms =
368 new Display.ColorTransform[mSupportedColorTransforms.size()];
369 for (int i = 0; i < mSupportedColorTransforms.size(); i++) {
370 mInfo.supportedColorTransforms[i] = mSupportedColorTransforms.valueAt(i);
371 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700372 mInfo.appVsyncOffsetNanos = phys.appVsyncOffsetNanos;
373 mInfo.presentationDeadlineNanos = phys.presentationDeadlineNanos;
Jeff Brown037c33e2014-04-09 00:31:55 -0700374 mInfo.state = mState;
Wale Ogunwale361ca212014-11-20 11:42:38 -0800375 mInfo.uniqueId = getUniqueId();
Jeff Brown77aebfd2012-10-01 21:07:03 -0700376
Jeff Brownf0681b32012-10-23 17:35:57 -0700377 // Assume that all built-in displays that have secure output (eg. HDCP) also
Jeff Brown77aebfd2012-10-01 21:07:03 -0700378 // support compositing from gralloc protected buffers.
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700379 if (phys.secure) {
Jeff Brownf0681b32012-10-23 17:35:57 -0700380 mInfo.flags = DisplayDeviceInfo.FLAG_SECURE
381 | DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS;
382 }
Jeff Brown77aebfd2012-10-01 21:07:03 -0700383
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800384 if (mBuiltInDisplayId == SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) {
Adam Powell49e7ff92015-05-14 16:18:53 -0700385 final Resources res = getContext().getResources();
386 mInfo.name = res.getString(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700387 com.android.internal.R.string.display_manager_built_in_display_name);
Jeff Brown77aebfd2012-10-01 21:07:03 -0700388 mInfo.flags |= DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY
Jeff Brown27f1d672012-10-17 18:32:34 -0700389 | DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT;
Adam Powell01f280d2015-05-18 16:07:42 -0700390 if (res.getBoolean(com.android.internal.R.bool.config_mainBuiltInDisplayIsRound)
391 || (Build.HARDWARE.contains("goldfish")
392 && SystemProperties.getBoolean(PROPERTY_EMULATOR_CIRCULAR, false))) {
Adam Powell49e7ff92015-05-14 16:18:53 -0700393 mInfo.flags |= DisplayDeviceInfo.FLAG_ROUND;
394 }
Jeff Brown92130f62012-10-24 21:28:33 -0700395 mInfo.type = Display.TYPE_BUILT_IN;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700396 mInfo.densityDpi = (int)(phys.density * 160 + 0.5f);
397 mInfo.xDpi = phys.xDpi;
398 mInfo.yDpi = phys.yDpi;
Jeff Brownd728bf52012-09-08 18:05:28 -0700399 mInfo.touch = DisplayDeviceInfo.TOUCH_INTERNAL;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700400 } else {
Jeff Brown92130f62012-10-24 21:28:33 -0700401 mInfo.type = Display.TYPE_HDMI;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700402 mInfo.flags |= DisplayDeviceInfo.FLAG_PRESENTATION;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700403 mInfo.name = getContext().getResources().getString(
404 com.android.internal.R.string.display_manager_hdmi_display_name);
Jeff Brownd728bf52012-09-08 18:05:28 -0700405 mInfo.touch = DisplayDeviceInfo.TOUCH_EXTERNAL;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700406 mInfo.setAssumedDensityForExternalDisplay(phys.width, phys.height);
Jeff Brown27f1d672012-10-17 18:32:34 -0700407
408 // For demonstration purposes, allow rotation of the external display.
409 // In the future we might allow the user to configure this directly.
410 if ("portrait".equals(SystemProperties.get("persist.demo.hdmirotation"))) {
411 mInfo.rotation = Surface.ROTATION_270;
412 }
Scott Anderson8786ed92013-11-01 13:27:39 -0700413
414 // For demonstration purposes, allow rotation of the external display
415 // to follow the built-in display.
416 if (SystemProperties.getBoolean("persist.demo.hdmirotates", false)) {
417 mInfo.flags |= DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT;
418 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700419 }
420 }
421 return mInfo;
422 }
423
424 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -0700425 public Runnable requestDisplayStateLocked(final int state, final int brightness) {
426 // Assume that the brightness is off if the display is being turned off.
427 assert state != Display.STATE_OFF || brightness == PowerManager.BRIGHTNESS_OFF;
428
429 final boolean stateChanged = (mState != state);
430 final boolean brightnessChanged = (mBrightness != brightness) && mBacklight != null;
431 if (stateChanged || brightnessChanged) {
Jeff Browne75926d2014-09-18 15:24:49 -0700432 final int displayId = mBuiltInDisplayId;
433 final IBinder token = getDisplayTokenLocked();
Jeff Brown5d6443b2015-04-10 20:15:01 -0700434 final int oldState = mState;
Jeff Browne75926d2014-09-18 15:24:49 -0700435
Jeff Brown5d6443b2015-04-10 20:15:01 -0700436 if (stateChanged) {
437 mState = state;
438 updateDeviceInfoLocked();
439 }
440
441 if (brightnessChanged) {
442 mBrightness = brightness;
443 }
444
445 // Defer actually setting the display state until after we have exited
Jeff Browne75926d2014-09-18 15:24:49 -0700446 // the critical section since it can take hundreds of milliseconds
447 // to complete.
448 return new Runnable() {
449 @Override
450 public void run() {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700451 // Exit a suspended state before making any changes.
452 int currentState = oldState;
453 if (Display.isSuspendedState(oldState)
454 || oldState == Display.STATE_UNKNOWN) {
455 if (!Display.isSuspendedState(state)) {
456 setDisplayState(state);
457 currentState = state;
458 } else if (state == Display.STATE_DOZE_SUSPEND
459 || oldState == Display.STATE_DOZE_SUSPEND) {
460 setDisplayState(Display.STATE_DOZE);
461 currentState = Display.STATE_DOZE;
462 } else {
463 return; // old state and new state is off
464 }
465 }
466
467 // Apply brightness changes given that we are in a non-suspended state.
468 if (brightnessChanged) {
469 setDisplayBrightness(brightness);
470 }
471
472 // Enter the final desired state, possibly suspended.
473 if (state != currentState) {
474 setDisplayState(state);
475 }
476 }
477
478 private void setDisplayState(int state) {
479 if (DEBUG) {
480 Slog.d(TAG, "setDisplayState("
481 + "id=" + displayId
482 + ", state=" + Display.stateToString(state) + ")");
483 }
484
485 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setDisplayState("
486 + "id=" + displayId
487 + ", state=" + Display.stateToString(state) + ")");
Jeff Browne75926d2014-09-18 15:24:49 -0700488 try {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700489 final int mode = getPowerModeForState(state);
Jeff Browne75926d2014-09-18 15:24:49 -0700490 SurfaceControl.setDisplayPowerMode(token, mode);
491 } finally {
492 Trace.traceEnd(Trace.TRACE_TAG_POWER);
493 }
494 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700495
496 private void setDisplayBrightness(int brightness) {
497 if (DEBUG) {
498 Slog.d(TAG, "setDisplayBrightness("
499 + "id=" + displayId + ", brightness=" + brightness + ")");
500 }
501
502 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setDisplayBrightness("
503 + "id=" + displayId + ", brightness=" + brightness + ")");
504 try {
505 mBacklight.setBrightness(brightness);
506 } finally {
507 Trace.traceEnd(Trace.TRACE_TAG_POWER);
508 }
509 }
Jeff Browne75926d2014-09-18 15:24:49 -0700510 };
Jeff Brown037c33e2014-04-09 00:31:55 -0700511 }
Jeff Browne75926d2014-09-18 15:24:49 -0700512 return null;
Jeff Brown9e316a12012-10-08 19:17:06 -0700513 }
514
515 @Override
Michael Wright58e829f2015-09-15 00:13:26 +0100516 public void requestColorTransformAndModeInTransactionLocked(
517 int colorTransformId, int modeId) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700518 if (modeId == 0) {
519 modeId = mDefaultModeId;
520 } else if (mSupportedModes.indexOfKey(modeId) < 0) {
521 Slog.w(TAG, "Requested mode " + modeId + " is not supported by this display,"
522 + " reverting to default display mode.");
523 modeId = mDefaultModeId;
524 }
Michael Wright58e829f2015-09-15 00:13:26 +0100525
526 if (colorTransformId == 0) {
527 colorTransformId = mDefaultColorTransformId;
528 } else if (mSupportedColorTransforms.indexOfKey(colorTransformId) < 0) {
529 Slog.w(TAG, "Requested color transform " + colorTransformId + " is not supported"
530 + " by this display, reverting to the default color transform");
531 colorTransformId = mDefaultColorTransformId;
532 }
533 int physIndex = findDisplayInfoIndexLocked(colorTransformId, modeId);
534 if (physIndex < 0) {
535 Slog.w(TAG, "Requested color transform, mode ID pair (" + colorTransformId + ", "
536 + modeId + ") not available, trying color transform with default mode ID");
537 modeId = mDefaultModeId;
538 physIndex = findDisplayInfoIndexLocked(colorTransformId, modeId);
539 if (physIndex < 0) {
540 Slog.w(TAG, "Requested color transform with default mode ID still not"
541 + " available, falling back to default color transform with default"
542 + " mode.");
543 colorTransformId = mDefaultColorTransformId;
544 physIndex = findDisplayInfoIndexLocked(colorTransformId, modeId);
545 }
546 }
547 if (physIndex > 0 && mActivePhysIndex == physIndex) {
Michael Wright3f145a22014-07-22 19:46:03 -0700548 return;
549 }
Michael Wright58e829f2015-09-15 00:13:26 +0100550 SurfaceControl.setActiveConfig(getDisplayTokenLocked(), physIndex);
551 mActivePhysIndex = physIndex;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700552 mActiveModeId = modeId;
553 mActiveModeInvalid = false;
Michael Wright58e829f2015-09-15 00:13:26 +0100554 mActiveColorTransformId = colorTransformId;
555 mActiveColorTransformInvalid = false;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700556 updateDeviceInfoLocked();
Michael Wright3f145a22014-07-22 19:46:03 -0700557 }
558
559 @Override
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700560 public void dumpLocked(PrintWriter pw) {
561 super.dumpLocked(pw);
562 pw.println("mBuiltInDisplayId=" + mBuiltInDisplayId);
Michael Wright58e829f2015-09-15 00:13:26 +0100563 pw.println("mActivePhysIndex=" + mActivePhysIndex);
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700564 pw.println("mActiveModeId=" + mActiveModeId);
Michael Wright58e829f2015-09-15 00:13:26 +0100565 pw.println("mActiveColorTransformId=" + mActiveColorTransformId);
Jeff Brown037c33e2014-04-09 00:31:55 -0700566 pw.println("mState=" + Display.stateToString(mState));
Jeff Brown5d6443b2015-04-10 20:15:01 -0700567 pw.println("mBrightness=" + mBrightness);
568 pw.println("mBacklight=" + mBacklight);
Michael Wright58e829f2015-09-15 00:13:26 +0100569 pw.println("mDisplayInfos=");
570 for (int i = 0; i < mDisplayInfos.length; i++) {
571 pw.println(" " + mDisplayInfos[i]);
572 }
573 pw.println("mSupportedModes=");
574 for (int i = 0; i < mSupportedModes.size(); i++) {
575 pw.println(" " + mSupportedModes.valueAt(i));
576 }
577 pw.println("mSupportedColorTransforms=[");
578 for (int i = 0; i < mSupportedColorTransforms.size(); i++) {
579 if (i != 0) {
580 pw.print(", ");
581 }
582 pw.print(mSupportedColorTransforms.valueAt(i));
583 }
584 pw.println("]");
585 }
586
587 private int findDisplayInfoIndexLocked(int colorTransformId, int modeId) {
588 DisplayModeRecord record = mSupportedModes.get(modeId);
589 Display.ColorTransform transform = mSupportedColorTransforms.get(colorTransformId);
590 if (record != null && transform != null) {
591 for (int i = 0; i < mDisplayInfos.length; i++) {
592 SurfaceControl.PhysicalDisplayInfo info = mDisplayInfos[i];
593 if (info.colorTransform == transform.getColorTransform()
594 && record.hasMatchingMode(info)){
595 return i;
596 }
597 }
598 }
599 return -1;
Jeff Brown037c33e2014-04-09 00:31:55 -0700600 }
601
602 private void updateDeviceInfoLocked() {
603 mInfo = null;
604 sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700605 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700606 }
Michael Wright3f145a22014-07-22 19:46:03 -0700607
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700608 /**
609 * Keeps track of a display configuration.
610 */
611 private static final class DisplayModeRecord {
612 public final Display.Mode mMode;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700613
Michael Wright58e829f2015-09-15 00:13:26 +0100614 public DisplayModeRecord(SurfaceControl.PhysicalDisplayInfo phys) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700615 mMode = createMode(phys.width, phys.height, phys.refreshRate);
Michael Wright58e829f2015-09-15 00:13:26 +0100616 }
617
618 /**
619 * Returns whether the mode generated by the given PhysicalDisplayInfo matches the mode
620 * contained by the record modulo mode ID.
621 *
622 * Note that this doesn't necessarily mean the the PhysicalDisplayInfos are identical, just
623 * that they generate identical modes.
624 */
625 public boolean hasMatchingMode(SurfaceControl.PhysicalDisplayInfo info) {
626 int modeRefreshRate = Float.floatToIntBits(mMode.getRefreshRate());
627 int displayInfoRefreshRate = Float.floatToIntBits(info.refreshRate);
628 return mMode.getPhysicalWidth() == info.width
629 && mMode.getPhysicalHeight() == info.height
630 && modeRefreshRate == displayInfoRefreshRate;
631 }
632
633 public String toString() {
634 return "DisplayModeRecord{mMode=" + mMode + "}";
Michael Wright3f145a22014-07-22 19:46:03 -0700635 }
Jeff Brown64a55af2012-08-26 02:47:39 -0700636 }
Jeff Browne87bf032012-09-20 18:30:13 -0700637
638 private final class HotplugDisplayEventReceiver extends DisplayEventReceiver {
639 public HotplugDisplayEventReceiver(Looper looper) {
640 super(looper);
641 }
642
643 @Override
644 public void onHotplug(long timestampNanos, int builtInDisplayId, boolean connected) {
645 synchronized (getSyncRoot()) {
Jesse Halle244db42012-11-08 11:55:14 -0800646 if (connected) {
647 tryConnectDisplayLocked(builtInDisplayId);
648 } else {
649 tryDisconnectDisplayLocked(builtInDisplayId);
650 }
Jeff Browne87bf032012-09-20 18:30:13 -0700651 }
652 }
653 }
Dan Stoza00101052014-05-02 15:23:40 -0700654}