VirtualDisplay.java revision 92207df753c27b094e9e0ca80d41bc0d54dc6bd5
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 */ 16package android.hardware.display; 17 18import android.os.IBinder; 19import android.view.Display; 20import android.view.Surface; 21 22/** 23 * 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()}. 26 * <p> 27 * Because a virtual display renders to a surface provided by the application, it will be 28 * released automatically when the process terminates and all remaining windows on it will 29 * be forcibly removed. However, you should also explicitly call {@link #release} when 30 * you're done with it. 31 * </p> 32 * 33 * @see DisplayManager#createVirtualDisplay 34 */ 35public final class VirtualDisplay { 36 private final DisplayManagerGlobal mGlobal; 37 private final Display mDisplay; 38 private IBinder mToken; 39 private Surface mSurface; 40 41 VirtualDisplay(DisplayManagerGlobal global, Display display, IBinder token, 42 Surface surface) { 43 mGlobal = global; 44 mDisplay = display; 45 mToken = token; 46 mSurface = surface; 47 } 48 49 /** 50 * Gets the virtual display. 51 */ 52 public Display getDisplay() { 53 return mDisplay; 54 } 55 56 /** 57 * 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 /** 83 * Releases the virtual display and destroys its underlying surface. 84 * <p> 85 * All remaining windows on the virtual display will be forcibly removed 86 * as part of releasing the virtual display. 87 * </p> 88 */ 89 public void release() { 90 if (mToken != null) { 91 mGlobal.releaseVirtualDisplay(mToken); 92 mToken = null; 93 } 94 } 95 96 @Override 97 public String toString() { 98 return "VirtualDisplay{display=" + mDisplay + ", token=" + mToken 99 + ", surface=" + mSurface + "}"; 100 } 101} 102