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