LocalDisplayAdapter.java revision 00101052b5bf99a29acea3525864ed64b709c16c
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 17package com.android.server.display; 18 19import android.content.Context; 20import android.os.Handler; 21import android.os.IBinder; 22import android.os.Looper; 23import android.os.SystemProperties; 24import android.util.Slog; 25import android.util.SparseArray; 26import android.view.Display; 27import android.view.DisplayEventReceiver; 28import android.view.Surface; 29import android.view.SurfaceControl; 30 31import java.io.PrintWriter; 32 33/** 34 * A display adapter for the local displays managed by Surface Flinger. 35 * <p> 36 * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock. 37 * </p> 38 */ 39final class LocalDisplayAdapter extends DisplayAdapter { 40 private static final String TAG = "LocalDisplayAdapter"; 41 42 private static final int[] BUILT_IN_DISPLAY_IDS_TO_SCAN = new int[] { 43 SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN, 44 SurfaceControl.BUILT_IN_DISPLAY_ID_HDMI, 45 }; 46 47 private final SparseArray<LocalDisplayDevice> mDevices = 48 new SparseArray<LocalDisplayDevice>(); 49 private HotplugDisplayEventReceiver mHotplugReceiver; 50 51 // Called with SyncRoot lock held. 52 public LocalDisplayAdapter(DisplayManagerService.SyncRoot syncRoot, 53 Context context, Handler handler, Listener listener) { 54 super(syncRoot, context, handler, listener, TAG); 55 } 56 57 @Override 58 public void registerLocked() { 59 super.registerLocked(); 60 61 mHotplugReceiver = new HotplugDisplayEventReceiver(getHandler().getLooper()); 62 63 for (int builtInDisplayId : BUILT_IN_DISPLAY_IDS_TO_SCAN) { 64 tryConnectDisplayLocked(builtInDisplayId); 65 } 66 } 67 68 private void tryConnectDisplayLocked(int builtInDisplayId) { 69 IBinder displayToken = SurfaceControl.getBuiltInDisplay(builtInDisplayId); 70 if (displayToken != null) { 71 SurfaceControl.PhysicalDisplayInfo[] configs = 72 SurfaceControl.getDisplayConfigs(displayToken); 73 if (configs == null) { 74 // There are no valid configs for this device, so we can't use it 75 Slog.w(TAG, "No valid configs found for display device " + 76 builtInDisplayId); 77 return; 78 } 79 int activeConfig = SurfaceControl.getActiveConfig(displayToken); 80 if (activeConfig < 0) { 81 // There is no active config, and for now we don't have the 82 // policy to set one. 83 Slog.w(TAG, "No active config found for display device " + 84 builtInDisplayId); 85 return; 86 } 87 LocalDisplayDevice device = mDevices.get(builtInDisplayId); 88 if (device == null) { 89 // Display was added. 90 device = new LocalDisplayDevice(displayToken, builtInDisplayId, 91 configs[activeConfig]); 92 mDevices.put(builtInDisplayId, device); 93 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_ADDED); 94 } else if (device.updatePhysicalDisplayInfoLocked(configs[activeConfig])) { 95 // Display properties changed. 96 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_CHANGED); 97 } 98 } else { 99 // The display is no longer available. Ignore the attempt to add it. 100 // If it was connected but has already been disconnected, we'll get a 101 // disconnect event that will remove it from mDevices. 102 } 103 } 104 105 private void tryDisconnectDisplayLocked(int builtInDisplayId) { 106 LocalDisplayDevice device = mDevices.get(builtInDisplayId); 107 if (device != null) { 108 // Display was removed. 109 mDevices.remove(builtInDisplayId); 110 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_REMOVED); 111 } 112 } 113 114 static boolean shouldBlank(int state) { 115 return state == Display.STATE_OFF; 116 } 117 118 static boolean shouldUnblank(int state) { 119 return state == Display.STATE_ON || state == Display.STATE_DOZING; 120 } 121 122 private final class LocalDisplayDevice extends DisplayDevice { 123 private final int mBuiltInDisplayId; 124 private final SurfaceControl.PhysicalDisplayInfo mPhys; 125 126 private DisplayDeviceInfo mInfo; 127 private boolean mHavePendingChanges; 128 private int mState = Display.STATE_UNKNOWN; 129 130 public LocalDisplayDevice(IBinder displayToken, int builtInDisplayId, 131 SurfaceControl.PhysicalDisplayInfo phys) { 132 super(LocalDisplayAdapter.this, displayToken); 133 mBuiltInDisplayId = builtInDisplayId; 134 mPhys = new SurfaceControl.PhysicalDisplayInfo(phys); 135 } 136 137 public boolean updatePhysicalDisplayInfoLocked(SurfaceControl.PhysicalDisplayInfo phys) { 138 if (!mPhys.equals(phys)) { 139 mPhys.copyFrom(phys); 140 mHavePendingChanges = true; 141 return true; 142 } 143 return false; 144 } 145 146 @Override 147 public void applyPendingDisplayDeviceInfoChangesLocked() { 148 if (mHavePendingChanges) { 149 mInfo = null; 150 mHavePendingChanges = false; 151 } 152 } 153 154 @Override 155 public DisplayDeviceInfo getDisplayDeviceInfoLocked() { 156 if (mInfo == null) { 157 mInfo = new DisplayDeviceInfo(); 158 mInfo.width = mPhys.width; 159 mInfo.height = mPhys.height; 160 mInfo.refreshRate = mPhys.refreshRate; 161 mInfo.state = mState; 162 163 // Assume that all built-in displays that have secure output (eg. HDCP) also 164 // support compositing from gralloc protected buffers. 165 if (mPhys.secure) { 166 mInfo.flags = DisplayDeviceInfo.FLAG_SECURE 167 | DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS; 168 } 169 170 if (mBuiltInDisplayId == SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) { 171 mInfo.name = getContext().getResources().getString( 172 com.android.internal.R.string.display_manager_built_in_display_name); 173 mInfo.flags |= DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY 174 | DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT; 175 mInfo.type = Display.TYPE_BUILT_IN; 176 mInfo.densityDpi = (int)(mPhys.density * 160 + 0.5f); 177 mInfo.xDpi = mPhys.xDpi; 178 mInfo.yDpi = mPhys.yDpi; 179 mInfo.touch = DisplayDeviceInfo.TOUCH_INTERNAL; 180 } else { 181 mInfo.type = Display.TYPE_HDMI; 182 mInfo.flags |= DisplayDeviceInfo.FLAG_PRESENTATION; 183 mInfo.name = getContext().getResources().getString( 184 com.android.internal.R.string.display_manager_hdmi_display_name); 185 mInfo.touch = DisplayDeviceInfo.TOUCH_EXTERNAL; 186 mInfo.setAssumedDensityForExternalDisplay(mPhys.width, mPhys.height); 187 188 // For demonstration purposes, allow rotation of the external display. 189 // In the future we might allow the user to configure this directly. 190 if ("portrait".equals(SystemProperties.get("persist.demo.hdmirotation"))) { 191 mInfo.rotation = Surface.ROTATION_270; 192 } 193 194 // For demonstration purposes, allow rotation of the external display 195 // to follow the built-in display. 196 if (SystemProperties.getBoolean("persist.demo.hdmirotates", false)) { 197 mInfo.flags |= DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT; 198 } 199 } 200 } 201 return mInfo; 202 } 203 204 @Override 205 public void requestDisplayStateLocked(int state) { 206 if (mState != state) { 207 if (shouldBlank(state) && !shouldBlank(mState)) { 208 SurfaceControl.blankDisplay(getDisplayTokenLocked()); 209 } else if (shouldUnblank(state) && !shouldUnblank(mState)) { 210 SurfaceControl.unblankDisplay(getDisplayTokenLocked()); 211 } 212 mState = state; 213 updateDeviceInfoLocked(); 214 } 215 } 216 217 @Override 218 public void dumpLocked(PrintWriter pw) { 219 super.dumpLocked(pw); 220 pw.println("mBuiltInDisplayId=" + mBuiltInDisplayId); 221 pw.println("mPhys=" + mPhys); 222 pw.println("mState=" + Display.stateToString(mState)); 223 } 224 225 private void updateDeviceInfoLocked() { 226 mInfo = null; 227 sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED); 228 } 229 } 230 231 private final class HotplugDisplayEventReceiver extends DisplayEventReceiver { 232 public HotplugDisplayEventReceiver(Looper looper) { 233 super(looper); 234 } 235 236 @Override 237 public void onHotplug(long timestampNanos, int builtInDisplayId, boolean connected) { 238 synchronized (getSyncRoot()) { 239 if (connected) { 240 tryConnectDisplayLocked(builtInDisplayId); 241 } else { 242 tryDisconnectDisplayLocked(builtInDisplayId); 243 } 244 } 245 } 246 } 247} 248