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 | |
| 19 | import android.content.Context; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 20 | import android.os.Handler; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 21 | import android.os.IBinder; |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 22 | import android.os.Looper; |
Jeff Brown | 27f1d67 | 2012-10-17 18:32:34 -0700 | [diff] [blame] | 23 | import android.os.SystemProperties; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 24 | import android.util.SparseArray; |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 25 | import android.view.Display; |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 26 | import android.view.DisplayEventReceiver; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 27 | import android.view.Surface; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 28 | import android.view.SurfaceControl; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 29 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 30 | import java.io.PrintWriter; |
| 31 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 32 | /** |
| 33 | * A display adapter for the local displays managed by Surface Flinger. |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 34 | * <p> |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 35 | * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock. |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 36 | * </p> |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 37 | */ |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 38 | final class LocalDisplayAdapter extends DisplayAdapter { |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 39 | private static final String TAG = "LocalDisplayAdapter"; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 40 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 41 | private static final int[] BUILT_IN_DISPLAY_IDS_TO_SCAN = new int[] { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 42 | SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN, |
| 43 | SurfaceControl.BUILT_IN_DISPLAY_ID_HDMI, |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | private final SparseArray<LocalDisplayDevice> mDevices = |
| 47 | new SparseArray<LocalDisplayDevice>(); |
Jeff Brown | 6669250 | 2012-10-18 16:13:44 -0700 | [diff] [blame] | 48 | private HotplugDisplayEventReceiver mHotplugReceiver; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 49 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 50 | private final SurfaceControl.PhysicalDisplayInfo mTempPhys = new SurfaceControl.PhysicalDisplayInfo(); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 51 | |
Jeff Brown | 6669250 | 2012-10-18 16:13:44 -0700 | [diff] [blame] | 52 | // Called with SyncRoot lock held. |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 53 | public LocalDisplayAdapter(DisplayManagerService.SyncRoot syncRoot, |
| 54 | Context context, Handler handler, Listener listener) { |
| 55 | super(syncRoot, context, handler, listener, TAG); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 59 | public void registerLocked() { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 60 | super.registerLocked(); |
Jeff Brown | 6669250 | 2012-10-18 16:13:44 -0700 | [diff] [blame] | 61 | |
| 62 | mHotplugReceiver = new HotplugDisplayEventReceiver(getHandler().getLooper()); |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 63 | |
| 64 | for (int builtInDisplayId : BUILT_IN_DISPLAY_IDS_TO_SCAN) { |
| 65 | tryConnectDisplayLocked(builtInDisplayId); |
| 66 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 69 | private void tryConnectDisplayLocked(int builtInDisplayId) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 70 | IBinder displayToken = SurfaceControl.getBuiltInDisplay(builtInDisplayId); |
| 71 | if (displayToken != null && SurfaceControl.getDisplayInfo(displayToken, mTempPhys)) { |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 72 | LocalDisplayDevice device = mDevices.get(builtInDisplayId); |
| 73 | if (device == null) { |
| 74 | // Display was added. |
| 75 | device = new LocalDisplayDevice(displayToken, builtInDisplayId, mTempPhys); |
| 76 | mDevices.put(builtInDisplayId, device); |
| 77 | sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_ADDED); |
| 78 | } else if (device.updatePhysicalDisplayInfoLocked(mTempPhys)) { |
| 79 | // Display properties changed. |
| 80 | sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_CHANGED); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 81 | } |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 82 | } else { |
| 83 | // The display is no longer available. Ignore the attempt to add it. |
| 84 | // If it was connected but has already been disconnected, we'll get a |
| 85 | // disconnect event that will remove it from mDevices. |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private void tryDisconnectDisplayLocked(int builtInDisplayId) { |
| 90 | LocalDisplayDevice device = mDevices.get(builtInDisplayId); |
| 91 | if (device != null) { |
| 92 | // Display was removed. |
| 93 | mDevices.remove(builtInDisplayId); |
| 94 | sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_REMOVED); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 95 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | private final class LocalDisplayDevice extends DisplayDevice { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 99 | private final int mBuiltInDisplayId; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 100 | private final SurfaceControl.PhysicalDisplayInfo mPhys; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 101 | |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 102 | private DisplayDeviceInfo mInfo; |
| 103 | private boolean mHavePendingChanges; |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 104 | private boolean mBlanked; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 105 | |
| 106 | public LocalDisplayDevice(IBinder displayToken, int builtInDisplayId, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 107 | SurfaceControl.PhysicalDisplayInfo phys) { |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 108 | super(LocalDisplayAdapter.this, displayToken); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 109 | mBuiltInDisplayId = builtInDisplayId; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 110 | mPhys = new SurfaceControl.PhysicalDisplayInfo(phys); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 113 | public boolean updatePhysicalDisplayInfoLocked(SurfaceControl.PhysicalDisplayInfo phys) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 114 | if (!mPhys.equals(phys)) { |
| 115 | mPhys.copyFrom(phys); |
| 116 | mHavePendingChanges = true; |
| 117 | return true; |
| 118 | } |
| 119 | return false; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 123 | public void applyPendingDisplayDeviceInfoChangesLocked() { |
| 124 | if (mHavePendingChanges) { |
| 125 | mInfo = null; |
| 126 | mHavePendingChanges = false; |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 127 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 128 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 129 | |
| 130 | @Override |
| 131 | public DisplayDeviceInfo getDisplayDeviceInfoLocked() { |
| 132 | if (mInfo == null) { |
| 133 | mInfo = new DisplayDeviceInfo(); |
| 134 | mInfo.width = mPhys.width; |
| 135 | mInfo.height = mPhys.height; |
| 136 | mInfo.refreshRate = mPhys.refreshRate; |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 137 | |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 138 | // 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] | 139 | // support compositing from gralloc protected buffers. |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 140 | if (mPhys.secure) { |
| 141 | mInfo.flags = DisplayDeviceInfo.FLAG_SECURE |
| 142 | | DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS; |
| 143 | } |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 144 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 145 | if (mBuiltInDisplayId == SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 146 | mInfo.name = getContext().getResources().getString( |
| 147 | com.android.internal.R.string.display_manager_built_in_display_name); |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 148 | mInfo.flags |= DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY |
Jeff Brown | 27f1d67 | 2012-10-17 18:32:34 -0700 | [diff] [blame] | 149 | | DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT; |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 150 | mInfo.type = Display.TYPE_BUILT_IN; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 151 | mInfo.densityDpi = (int)(mPhys.density * 160 + 0.5f); |
| 152 | mInfo.xDpi = mPhys.xDpi; |
| 153 | mInfo.yDpi = mPhys.yDpi; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 154 | mInfo.touch = DisplayDeviceInfo.TOUCH_INTERNAL; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 155 | } else { |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 156 | mInfo.type = Display.TYPE_HDMI; |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 157 | mInfo.flags |= DisplayDeviceInfo.FLAG_PRESENTATION; |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 158 | mInfo.name = getContext().getResources().getString( |
| 159 | com.android.internal.R.string.display_manager_hdmi_display_name); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 160 | mInfo.touch = DisplayDeviceInfo.TOUCH_EXTERNAL; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 161 | mInfo.setAssumedDensityForExternalDisplay(mPhys.width, mPhys.height); |
Jeff Brown | 27f1d67 | 2012-10-17 18:32:34 -0700 | [diff] [blame] | 162 | |
| 163 | // For demonstration purposes, allow rotation of the external display. |
| 164 | // In the future we might allow the user to configure this directly. |
| 165 | if ("portrait".equals(SystemProperties.get("persist.demo.hdmirotation"))) { |
| 166 | mInfo.rotation = Surface.ROTATION_270; |
| 167 | } |
Scott Anderson | 8786ed9 | 2013-11-01 13:27:39 -0700 | [diff] [blame] | 168 | |
| 169 | // For demonstration purposes, allow rotation of the external display |
| 170 | // to follow the built-in display. |
| 171 | if (SystemProperties.getBoolean("persist.demo.hdmirotates", false)) { |
| 172 | mInfo.flags |= DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT; |
| 173 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | return mInfo; |
| 177 | } |
| 178 | |
| 179 | @Override |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 180 | public void blankLocked() { |
| 181 | mBlanked = true; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 182 | SurfaceControl.blankDisplay(getDisplayTokenLocked()); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void unblankLocked() { |
| 187 | mBlanked = false; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 188 | SurfaceControl.unblankDisplay(getDisplayTokenLocked()); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 192 | public void dumpLocked(PrintWriter pw) { |
| 193 | super.dumpLocked(pw); |
| 194 | pw.println("mBuiltInDisplayId=" + mBuiltInDisplayId); |
| 195 | pw.println("mPhys=" + mPhys); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 196 | pw.println("mBlanked=" + mBlanked); |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 197 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 198 | } |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 199 | |
| 200 | private final class HotplugDisplayEventReceiver extends DisplayEventReceiver { |
| 201 | public HotplugDisplayEventReceiver(Looper looper) { |
| 202 | super(looper); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void onHotplug(long timestampNanos, int builtInDisplayId, boolean connected) { |
| 207 | synchronized (getSyncRoot()) { |
Jesse Hall | e244db4 | 2012-11-08 11:55:14 -0800 | [diff] [blame] | 208 | if (connected) { |
| 209 | tryConnectDisplayLocked(builtInDisplayId); |
| 210 | } else { |
| 211 | tryDisconnectDisplayLocked(builtInDisplayId); |
| 212 | } |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |