1 /*
2  * Copyright (C) 2013 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 package android.hardware.display;
17 
18 import android.view.Display;
19 import android.view.Surface;
20 
21 /**
22  * Represents a virtual display. The content of a virtual display is rendered to a
23  * {@link android.view.Surface} that you must provide to {@link DisplayManager#createVirtualDisplay
24  * createVirtualDisplay()}.
25  * <p>
26  * Because a virtual display renders to a surface provided by the application, it will be
27  * released automatically when the process terminates and all remaining windows on it will
28  * be forcibly removed.  However, you should also explicitly call {@link #release} when
29  * you're done with it.
30  * </p>
31  *
32  * @see DisplayManager#createVirtualDisplay
33  */
34 public final class VirtualDisplay {
35     private final DisplayManagerGlobal mGlobal;
36     private final Display mDisplay;
37     private IVirtualDisplayCallback mToken;
38     private Surface mSurface;
VirtualDisplay(DisplayManagerGlobal global, Display display, IVirtualDisplayCallback token, Surface surface)39     VirtualDisplay(DisplayManagerGlobal global, Display display, IVirtualDisplayCallback token,
40             Surface surface) {
41         mGlobal = global;
42         mDisplay = display;
43         mToken = token;
44         mSurface = surface;
45     }
46 
47     /**
48      * Gets the virtual display.
49      */
getDisplay()50     public Display getDisplay() {
51         return mDisplay;
52     }
53 
54     /**
55      * Gets the surface that backs the virtual display.
56      */
getSurface()57     public Surface getSurface() {
58         return mSurface;
59     }
60 
61     /**
62      * @hide
63      */
getToken()64     public IVirtualDisplayCallback getToken() {
65         return mToken;
66     }
67 
68     /**
69      * Sets the surface that backs the virtual display.
70      * <p>
71      * Detaching the surface that backs a virtual display has a similar effect to
72      * turning off the screen.
73      * </p><p>
74      * It is still the caller's responsibility to destroy the surface after it has
75      * been detached.
76      * </p>
77      *
78      * @param surface The surface to set, or null to detach the surface from the virtual display.
79      */
setSurface(Surface surface)80     public void setSurface(Surface surface) {
81         if (mSurface != surface) {
82             mGlobal.setVirtualDisplaySurface(mToken, surface);
83             mSurface = surface;
84         }
85     }
86 
87     /**
88      * Asks the virtual display to resize.
89      *<p>
90      * This is really just a convenience to allow applications using
91      * virtual displays to adapt to changing conditions without having
92      * to tear down and recreate the display.
93      * </p>
94      */
resize(int width, int height, int densityDpi)95     public void resize(int width, int height, int densityDpi) {
96         mGlobal.resizeVirtualDisplay(mToken, width, height, densityDpi);
97     }
98 
99     /**
100      * Releases the virtual display and destroys its underlying surface.
101      * <p>
102      * All remaining windows on the virtual display will be forcibly removed
103      * as part of releasing the virtual display.
104      * </p>
105      */
release()106     public void release() {
107         if (mToken != null) {
108             mGlobal.releaseVirtualDisplay(mToken);
109             mToken = null;
110         }
111     }
112 
113     /**
114      * Sets the on/off state for a virtual display.
115      *
116      * @param isOn Whether the display should be on or off.
117      * @hide
118      */
setDisplayState(boolean isOn)119     public void setDisplayState(boolean isOn) {
120         if (mToken != null) {
121             mGlobal.setVirtualDisplayState(mToken, isOn);
122         }
123     }
124 
125     @Override
toString()126     public String toString() {
127         return "VirtualDisplay{display=" + mDisplay + ", token=" + mToken
128                 + ", surface=" + mSurface + "}";
129     }
130 
131     /**
132      * Interface for receiving information about a {@link VirtualDisplay}'s state changes.
133      */
134     public static abstract class Callback {
135         /**
136          * Called when the virtual display video projection has been
137          * paused by the system or when the surface has been detached
138          * by the application by calling setSurface(null).
139          * The surface will not receive any more buffers while paused.
140          */
onPaused()141          public void onPaused() { }
142 
143         /**
144          * Called when the virtual display video projection has been
145          * resumed after having been paused.
146          */
onResumed()147          public void onResumed() { }
148 
149         /**
150          * Called when the virtual display video projection has been
151          * stopped by the system.  It will no longer receive frames
152          * and it will never be resumed.  It is still the responsibility
153          * of the application to release() the virtual display.
154          */
onStopped()155         public void onStopped() { }
156     }
157 }
158