DisplayDevice.java revision cbad976b2a36a0895ca94510d5208a86f66cf596
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     * Sets the display layer stack while in a transaction.
109     */
110    public final void setLayerStackInTransactionLocked(int layerStack) {
111        if (mCurrentLayerStack != layerStack) {
112            mCurrentLayerStack = layerStack;
113            Surface.setDisplayLayerStack(mDisplayToken, layerStack);
114        }
115    }
116
117    /**
118     * Sets the display projection while in a transaction.
119     *
120     * @param orientation defines the display's orientation
121     * @param layerStackRect defines which area of the window manager coordinate
122     *            space will be used
123     * @param displayRect defines where on the display will layerStackRect be
124     *            mapped to. displayRect is specified post-orientation, that is
125     *            it uses the orientation seen by the end-user
126     */
127    public final void setProjectionInTransactionLocked(int orientation,
128            Rect layerStackRect, Rect displayRect) {
129        if (mCurrentOrientation != orientation
130                || mCurrentLayerStackRect == null
131                || !mCurrentLayerStackRect.equals(layerStackRect)
132                || mCurrentDisplayRect == null
133                || !mCurrentDisplayRect.equals(displayRect)) {
134            mCurrentOrientation = orientation;
135            if (mCurrentLayerStackRect == null) {
136                mCurrentLayerStackRect = new Rect();
137            }
138            mCurrentLayerStackRect.set(layerStackRect);
139            if (mCurrentDisplayRect == null) {
140                mCurrentDisplayRect = new Rect();
141            }
142            mCurrentDisplayRect.set(displayRect);
143            Surface.setDisplayProjection(mDisplayToken,
144                    orientation, layerStackRect, displayRect);
145        }
146    }
147
148    /**
149     * Sets the display surface while in a transaction.
150     */
151    public final void setSurfaceInTransactionLocked(Surface surface) {
152        if (mCurrentSurface != surface) {
153            mCurrentSurface = surface;
154            Surface.setDisplaySurface(mDisplayToken, surface);
155        }
156    }
157
158    /**
159     * Dumps the local state of the display device.
160     * Does not need to dump the display device info because that is already dumped elsewhere.
161     */
162    public void dumpLocked(PrintWriter pw) {
163        pw.println("mAdapter=" + mDisplayAdapter.getName());
164        pw.println("mDisplayToken=" + mDisplayToken);
165        pw.println("mCurrentLayerStack=" + mCurrentLayerStack);
166        pw.println("mCurrentOrientation=" + mCurrentOrientation);
167        pw.println("mCurrentLayerStackRect=" + mCurrentLayerStackRect);
168        pw.println("mCurrentDisplayRect=" + mCurrentDisplayRect);
169        pw.println("mCurrentSurface=" + mCurrentSurface);
170    }
171}
172