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