DisplayDevice.java revision 5d6443bf7c087167e47ea39b13e6af09cb43ad97
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.graphics.Rect;
20import android.hardware.display.DisplayViewport;
21import android.os.IBinder;
22import android.view.Surface;
23import android.view.SurfaceControl;
24
25import java.io.PrintWriter;
26
27/**
28 * Represents a physical display device such as the built-in display
29 * an external monitor, or a WiFi display.
30 * <p>
31 * Display devices are guarded by the {@link DisplayManagerService.SyncRoot} lock.
32 * </p>
33 */
34abstract class DisplayDevice {
35    private final DisplayAdapter mDisplayAdapter;
36    private final IBinder mDisplayToken;
37    private final String mUniqueId;
38
39    // The display device does not manage these properties itself, they are set by
40    // the display manager service.  The display device shouldn't really be looking at these.
41    private int mCurrentLayerStack = -1;
42    private int mCurrentOrientation = -1;
43    private Rect mCurrentLayerStackRect;
44    private Rect mCurrentDisplayRect;
45
46    // The display device owns its surface, but it should only set it
47    // within a transaction from performTraversalInTransactionLocked.
48    private Surface mCurrentSurface;
49
50    public DisplayDevice(DisplayAdapter displayAdapter, IBinder displayToken, String uniqueId) {
51        mDisplayAdapter = displayAdapter;
52        mDisplayToken = displayToken;
53        mUniqueId = uniqueId;
54    }
55
56    /**
57     * Gets the display adapter that owns the display device.
58     *
59     * @return The display adapter.
60     */
61    public final DisplayAdapter getAdapterLocked() {
62        return mDisplayAdapter;
63    }
64
65    /**
66     * Gets the Surface Flinger display token for this display.
67     *
68     * @return The display token, or null if the display is not being managed
69     * by Surface Flinger.
70     */
71    public final IBinder getDisplayTokenLocked() {
72        return mDisplayToken;
73    }
74
75    /**
76     * Gets the name of the display device.
77     *
78     * @return The display device name.
79     */
80    public final String getNameLocked() {
81        return getDisplayDeviceInfoLocked().name;
82    }
83
84    /**
85     * Returns the unique id of the display device.
86     */
87    public final String getUniqueId() {
88        return mUniqueId;
89    }
90
91    /**
92     * Gets information about the display device.
93     *
94     * The information returned should not change between calls unless the display
95     * adapter sent a {@link DisplayAdapter#DISPLAY_DEVICE_EVENT_CHANGED} event and
96     * {@link #applyPendingDisplayDeviceInfoChangesLocked()} has been called to apply
97     * the pending changes.
98     *
99     * @return The display device info, which should be treated as immutable by the caller.
100     * The display device should allocate a new display device info object whenever
101     * the data changes.
102     */
103    public abstract DisplayDeviceInfo getDisplayDeviceInfoLocked();
104
105    /**
106     * Applies any pending changes to the observable state of the display device
107     * if the display adapter sent a {@link DisplayAdapter#DISPLAY_DEVICE_EVENT_CHANGED} event.
108     */
109    public void applyPendingDisplayDeviceInfoChangesLocked() {
110    }
111
112    /**
113     * Gives the display device a chance to update its properties while in a transaction.
114     */
115    public void performTraversalInTransactionLocked() {
116    }
117
118    /**
119     * Sets the display state, if supported.
120     *
121     * @param state The new display state.
122     * @param brightness The new display brightness.
123     * @return A runnable containing work to be deferred until after we have
124     * exited the critical section, or null if none.
125     */
126    public Runnable requestDisplayStateLocked(int state, int brightness) {
127        return null;
128    }
129
130    /**
131     * Sets the refresh rate, if supported.
132     */
133    public void requestRefreshRateLocked(float refreshRate) {
134    }
135
136    /**
137     * Sets the display layer stack while in a transaction.
138     */
139    public final void setLayerStackInTransactionLocked(int layerStack) {
140        if (mCurrentLayerStack != layerStack) {
141            mCurrentLayerStack = layerStack;
142            SurfaceControl.setDisplayLayerStack(mDisplayToken, layerStack);
143        }
144    }
145
146    /**
147     * Sets the display projection while in a transaction.
148     *
149     * @param orientation defines the display's orientation
150     * @param layerStackRect defines which area of the window manager coordinate
151     *            space will be used
152     * @param displayRect defines where on the display will layerStackRect be
153     *            mapped to. displayRect is specified post-orientation, that is
154     *            it uses the orientation seen by the end-user
155     */
156    public final void setProjectionInTransactionLocked(int orientation,
157            Rect layerStackRect, Rect displayRect) {
158        if (mCurrentOrientation != orientation
159                || mCurrentLayerStackRect == null
160                || !mCurrentLayerStackRect.equals(layerStackRect)
161                || mCurrentDisplayRect == null
162                || !mCurrentDisplayRect.equals(displayRect)) {
163            mCurrentOrientation = orientation;
164
165            if (mCurrentLayerStackRect == null) {
166                mCurrentLayerStackRect = new Rect();
167            }
168            mCurrentLayerStackRect.set(layerStackRect);
169
170            if (mCurrentDisplayRect == null) {
171                mCurrentDisplayRect = new Rect();
172            }
173            mCurrentDisplayRect.set(displayRect);
174
175            SurfaceControl.setDisplayProjection(mDisplayToken,
176                    orientation, layerStackRect, displayRect);
177        }
178    }
179
180    /**
181     * Sets the display surface while in a transaction.
182     */
183    public final void setSurfaceInTransactionLocked(Surface surface) {
184        if (mCurrentSurface != surface) {
185            mCurrentSurface = surface;
186            SurfaceControl.setDisplaySurface(mDisplayToken, surface);
187        }
188    }
189
190    /**
191     * Populates the specified viewport object with orientation,
192     * physical and logical rects based on the display's current projection.
193     */
194    public final void populateViewportLocked(DisplayViewport viewport) {
195        viewport.orientation = mCurrentOrientation;
196
197        if (mCurrentLayerStackRect != null) {
198            viewport.logicalFrame.set(mCurrentLayerStackRect);
199        } else {
200            viewport.logicalFrame.setEmpty();
201        }
202
203        if (mCurrentDisplayRect != null) {
204            viewport.physicalFrame.set(mCurrentDisplayRect);
205        } else {
206            viewport.physicalFrame.setEmpty();
207        }
208
209        boolean isRotated = (mCurrentOrientation == Surface.ROTATION_90
210                || mCurrentOrientation == Surface.ROTATION_270);
211        DisplayDeviceInfo info = getDisplayDeviceInfoLocked();
212        viewport.deviceWidth = isRotated ? info.height : info.width;
213        viewport.deviceHeight = isRotated ? info.width : info.height;
214    }
215
216    /**
217     * Dumps the local state of the display device.
218     * Does not need to dump the display device info because that is already dumped elsewhere.
219     */
220    public void dumpLocked(PrintWriter pw) {
221        pw.println("mAdapter=" + mDisplayAdapter.getName());
222        pw.println("mUniqueId=" + mUniqueId);
223        pw.println("mDisplayToken=" + mDisplayToken);
224        pw.println("mCurrentLayerStack=" + mCurrentLayerStack);
225        pw.println("mCurrentOrientation=" + mCurrentOrientation);
226        pw.println("mCurrentLayerStackRect=" + mCurrentLayerStackRect);
227        pw.println("mCurrentDisplayRect=" + mCurrentDisplayRect);
228        pw.println("mCurrentSurface=" + mCurrentSurface);
229    }
230}
231