2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server.display;
19 import android.graphics.Rect;
20 import android.hardware.display.DisplayViewport;
21 import android.os.IBinder;
22 import android.view.Display;
23 import android.view.Surface;
24 import android.view.SurfaceControl;
26 import java.io.PrintWriter;
29 * Represents a physical display device such as the built-in display
30 * an external monitor, or a WiFi display.
32 * Display devices are guarded by the {@link DisplayManagerService.SyncRoot} lock.
35 abstract class DisplayDevice {
36 private final DisplayAdapter mDisplayAdapter;
37 private final IBinder mDisplayToken;
38 private final String mUniqueId;
40 // The display device does not manage these properties itself, they are set by
41 // the display manager service. The display device shouldn't really be looking at these.
42 private int mCurrentLayerStack = -1;
43 private int mCurrentOrientation = -1;
44 private Rect mCurrentLayerStackRect;
45 private Rect mCurrentDisplayRect;
47 // The display device owns its surface, but it should only set it
48 // within a transaction from performTraversalInTransactionLocked.
49 private Surface mCurrentSurface;
51 // DEBUG STATE: Last device info which was written to the log, or null if none.
52 // Do not use for any other purpose.
53 DisplayDeviceInfo mDebugLastLoggedDeviceInfo;
55 public DisplayDevice(DisplayAdapter displayAdapter, IBinder displayToken, String uniqueId) {
56 mDisplayAdapter = displayAdapter;
57 mDisplayToken = displayToken;
62 * Gets the display adapter that owns the display device.
64 * @return The display adapter.
66 public final DisplayAdapter getAdapterLocked() {
67 return mDisplayAdapter;
71 * Gets the Surface Flinger display token for this display.
73 * @return The display token, or null if the display is not being managed
76 public final IBinder getDisplayTokenLocked() {
81 * Gets the name of the display device.
83 * @return The display device name.
85 public final String getNameLocked() {
86 return getDisplayDeviceInfoLocked().name;
90 * Returns the unique id of the display device.
92 public final String getUniqueId() {
97 * Returns whether the unique id of the device is stable across reboots.
99 public abstract boolean hasStableUniqueId();
102 * Gets information about the display device.
104 * The information returned should not change between calls unless the display
105 * adapter sent a {@link DisplayAdapter#DISPLAY_DEVICE_EVENT_CHANGED} event and
106 * {@link #applyPendingDisplayDeviceInfoChangesLocked()} has been called to apply
107 * the pending changes.
109 * @return The display device info, which should be treated as immutable by the caller.
110 * The display device should allocate a new display device info object whenever
113 public abstract DisplayDeviceInfo getDisplayDeviceInfoLocked();
116 * Applies any pending changes to the observable state of the display device
117 * if the display adapter sent a {@link DisplayAdapter#DISPLAY_DEVICE_EVENT_CHANGED} event.
119 public void applyPendingDisplayDeviceInfoChangesLocked() {
123 * Gives the display device a chance to update its properties while in a transaction.
125 public void performTraversalInTransactionLocked() {
129 * Sets the display state, if supported.
131 * @param state The new display state.
132 * @param brightness The new display brightness.
133 * @return A runnable containing work to be deferred until after we have
134 * exited the critical section, or null if none.
136 public Runnable requestDisplayStateLocked(int state, int brightness) {
141 * Sets the mode, if supported.
143 public void requestDisplayModesInTransactionLocked(int colorMode, int modeId) {
147 * Sets the display layer stack while in a transaction.
149 public final void setLayerStackInTransactionLocked(int layerStack) {
150 if (mCurrentLayerStack != layerStack) {
151 mCurrentLayerStack = layerStack;
152 SurfaceControl.setDisplayLayerStack(mDisplayToken, layerStack);
157 * Sets the display projection while in a transaction.
159 * @param orientation defines the display's orientation
160 * @param layerStackRect defines which area of the window manager coordinate
162 * @param displayRect defines where on the display will layerStackRect be
163 * mapped to. displayRect is specified post-orientation, that is
164 * it uses the orientation seen by the end-user
166 public final void setProjectionInTransactionLocked(int orientation,
167 Rect layerStackRect, Rect displayRect) {
168 if (mCurrentOrientation != orientation
169 || mCurrentLayerStackRect == null
170 || !mCurrentLayerStackRect.equals(layerStackRect)
171 || mCurrentDisplayRect == null
172 || !mCurrentDisplayRect.equals(displayRect)) {
173 mCurrentOrientation = orientation;
175 if (mCurrentLayerStackRect == null) {
176 mCurrentLayerStackRect = new Rect();
178 mCurrentLayerStackRect.set(layerStackRect);
180 if (mCurrentDisplayRect == null) {
181 mCurrentDisplayRect = new Rect();
183 mCurrentDisplayRect.set(displayRect);
185 SurfaceControl.setDisplayProjection(mDisplayToken,
186 orientation, layerStackRect, displayRect);
191 * Sets the display surface while in a transaction.
193 public final void setSurfaceInTransactionLocked(Surface surface) {
194 if (mCurrentSurface != surface) {
195 mCurrentSurface = surface;
196 SurfaceControl.setDisplaySurface(mDisplayToken, surface);
201 * Populates the specified viewport object with orientation,
202 * physical and logical rects based on the display's current projection.
204 public final void populateViewportLocked(DisplayViewport viewport) {
205 viewport.orientation = mCurrentOrientation;
207 if (mCurrentLayerStackRect != null) {
208 viewport.logicalFrame.set(mCurrentLayerStackRect);
210 viewport.logicalFrame.setEmpty();
213 if (mCurrentDisplayRect != null) {
214 viewport.physicalFrame.set(mCurrentDisplayRect);
216 viewport.physicalFrame.setEmpty();
219 boolean isRotated = (mCurrentOrientation == Surface.ROTATION_90
220 || mCurrentOrientation == Surface.ROTATION_270);
221 DisplayDeviceInfo info = getDisplayDeviceInfoLocked();
222 viewport.deviceWidth = isRotated ? info.height : info.width;
223 viewport.deviceHeight = isRotated ? info.width : info.height;
227 * Dumps the local state of the display device.
228 * Does not need to dump the display device info because that is already dumped elsewhere.
230 public void dumpLocked(PrintWriter pw) {
231 pw.println("mAdapter=" + mDisplayAdapter.getName());
232 pw.println("mUniqueId=" + mUniqueId);
233 pw.println("mDisplayToken=" + mDisplayToken);
234 pw.println("mCurrentLayerStack=" + mCurrentLayerStack);
235 pw.println("mCurrentOrientation=" + mCurrentOrientation);
236 pw.println("mCurrentLayerStackRect=" + mCurrentLayerStackRect);
237 pw.println("mCurrentDisplayRect=" + mCurrentDisplayRect);
238 pw.println("mCurrentSurface=" + mCurrentSurface);