blob: 4ddf10f1065de84121efb183559fc27113a06a36 [file] [log] [blame]
Jeff Browna506a6e2013-06-04 00:02:38 -07001/*
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 */
16package android.hardware.display;
17
18import android.os.IBinder;
19import android.view.Display;
Jeff Brown92207df2014-04-16 13:16:07 -070020import android.view.Surface;
Jeff Browna506a6e2013-06-04 00:02:38 -070021
22/**
Scott Maincc2195b2013-10-16 13:57:46 -070023 * Represents a virtual display. The content of a virtual display is rendered to a
24 * {@link android.view.Surface} that you must provide to {@link DisplayManager#createVirtualDisplay
25 * createVirtualDisplay()}.
Jeff Brown92207df2014-04-16 13:16:07 -070026 * <p>
27 * Because a virtual display renders to a surface provided by the application, it will be
Scott Maincc2195b2013-10-16 13:57:46 -070028 * released automatically when the process terminates and all remaining windows on it will
Jeff Brown92207df2014-04-16 13:16:07 -070029 * be forcibly removed. However, you should also explicitly call {@link #release} when
30 * you're done with it.
31 * </p>
Jeff Browna506a6e2013-06-04 00:02:38 -070032 *
Jeff Brown7d00aff2013-08-02 19:03:49 -070033 * @see DisplayManager#createVirtualDisplay
Jeff Browna506a6e2013-06-04 00:02:38 -070034 */
35public final class VirtualDisplay {
36 private final DisplayManagerGlobal mGlobal;
37 private final Display mDisplay;
Michael Wright75ee9fc2014-09-01 19:55:22 -070038 private IVirtualDisplayCallback mToken;
Jeff Brown92207df2014-04-16 13:16:07 -070039 private Surface mSurface;
Jeff Browna506a6e2013-06-04 00:02:38 -070040
Michael Wrightc39d47a2014-07-08 18:07:36 -070041 VirtualDisplay(DisplayManagerGlobal global, Display display,
Michael Wright75ee9fc2014-09-01 19:55:22 -070042 IVirtualDisplayCallback token, Surface surface) {
Jeff Browna506a6e2013-06-04 00:02:38 -070043 mGlobal = global;
44 mDisplay = display;
45 mToken = token;
Jeff Brown92207df2014-04-16 13:16:07 -070046 mSurface = surface;
Jeff Browna506a6e2013-06-04 00:02:38 -070047 }
48
49 /**
50 * Gets the virtual display.
51 */
52 public Display getDisplay() {
53 return mDisplay;
54 }
55
56 /**
Jeff Brown92207df2014-04-16 13:16:07 -070057 * Gets the surface that backs the virtual display.
58 */
59 public Surface getSurface() {
60 return mSurface;
61 }
62
63 /**
64 * Sets the surface that backs the virtual display.
65 * <p>
66 * Detaching the surface that backs a virtual display has a similar effect to
67 * turning off the screen.
68 * </p><p>
69 * It is still the caller's responsibility to destroy the surface after it has
70 * been detached.
71 * </p>
72 *
73 * @param surface The surface to set, or null to detach the surface from the virtual display.
74 */
75 public void setSurface(Surface surface) {
76 if (mSurface != surface) {
77 mGlobal.setVirtualDisplaySurface(mToken, surface);
78 mSurface = surface;
79 }
80 }
81
82 /**
Michael Wright01e840f2014-06-26 16:03:25 -070083 * Asks the virtual display to resize.
84 *<p>
85 * This is really just a convenience to allow applications using
86 * virtual displays to adapt to changing conditions without having
87 * to tear down and recreate the display.
88 * </p>
89 */
90 public void resize(int width, int height, int densityDpi) {
91 mGlobal.resizeVirtualDisplay(mToken, width, height, densityDpi);
92 }
93
94 /**
Jeff Browna506a6e2013-06-04 00:02:38 -070095 * Releases the virtual display and destroys its underlying surface.
96 * <p>
97 * All remaining windows on the virtual display will be forcibly removed
98 * as part of releasing the virtual display.
99 * </p>
100 */
101 public void release() {
102 if (mToken != null) {
103 mGlobal.releaseVirtualDisplay(mToken);
104 mToken = null;
105 }
106 }
107
108 @Override
109 public String toString() {
Jeff Brown92207df2014-04-16 13:16:07 -0700110 return "VirtualDisplay{display=" + mDisplay + ", token=" + mToken
111 + ", surface=" + mSurface + "}";
Jeff Browna506a6e2013-06-04 00:02:38 -0700112 }
Michael Wrightc39d47a2014-07-08 18:07:36 -0700113
114 /**
115 * Interface for receiving information about a {@link VirtualDisplay}'s state changes.
116 */
Michael Wright75ee9fc2014-09-01 19:55:22 -0700117 public static abstract class Callback {
Michael Wrightc39d47a2014-07-08 18:07:36 -0700118 /**
119 * Called when the virtual display video projection has been
120 * paused by the system or when the surface has been detached
121 * by the application by calling setSurface(null).
122 * The surface will not receive any more buffers while paused.
123 */
Michael Wright75ee9fc2014-09-01 19:55:22 -0700124 public void onPaused() { }
Michael Wrightc39d47a2014-07-08 18:07:36 -0700125
126 /**
127 * Called when the virtual display video projection has been
128 * resumed after having been paused.
129 */
Michael Wright75ee9fc2014-09-01 19:55:22 -0700130 public void onResumed() { }
Michael Wrightc39d47a2014-07-08 18:07:36 -0700131
132 /**
133 * Called when the virtual display video projection has been
134 * stopped by the system. It will no longer receive frames
135 * and it will never be resumed. It is still the responsibility
136 * of the application to release() the virtual display.
137 */
Michael Wright75ee9fc2014-09-01 19:55:22 -0700138 public void onStopped() { }
Michael Wrightc39d47a2014-07-08 18:07:36 -0700139 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700140}