1a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown/* 2a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * Copyright (C) 2013 The Android Open Source Project 3a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * 4a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * Licensed under the Apache License, Version 2.0 (the "License"); 5a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * you may not use this file except in compliance with the License. 6a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * You may obtain a copy of the License at 7a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * 8a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * http://www.apache.org/licenses/LICENSE-2.0 9a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * 10a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * Unless required by applicable law or agreed to in writing, software 11a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * distributed under the License is distributed on an "AS IS" BASIS, 12a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * See the License for the specific language governing permissions and 14a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * limitations under the License. 15a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown */ 16a506a6ec94863a35acca9feb165db76ddac3892cJeff Brownpackage android.hardware.display; 17a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown 18a506a6ec94863a35acca9feb165db76ddac3892cJeff Brownimport android.view.Display; 1992207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brownimport android.view.Surface; 20a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown 21a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown/** 22cc2195bb560cc155bf3e7d3a7f27fe619a497124Scott Main * Represents a virtual display. The content of a virtual display is rendered to a 23cc2195bb560cc155bf3e7d3a7f27fe619a497124Scott Main * {@link android.view.Surface} that you must provide to {@link DisplayManager#createVirtualDisplay 24cc2195bb560cc155bf3e7d3a7f27fe619a497124Scott Main * createVirtualDisplay()}. 2592207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * <p> 2692207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * Because a virtual display renders to a surface provided by the application, it will be 27cc2195bb560cc155bf3e7d3a7f27fe619a497124Scott Main * released automatically when the process terminates and all remaining windows on it will 2892207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * be forcibly removed. However, you should also explicitly call {@link #release} when 2992207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * you're done with it. 3092207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * </p> 31a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * 327d00affce6e25b22fd8fc135933b3bf6b547a0dcJeff Brown * @see DisplayManager#createVirtualDisplay 33a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown */ 34a506a6ec94863a35acca9feb165db76ddac3892cJeff Brownpublic final class VirtualDisplay { 35a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown private final DisplayManagerGlobal mGlobal; 36a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown private final Display mDisplay; 3775ee9fcde4d9e1be3883eba6c8d193db4375b052Michael Wright private IVirtualDisplayCallback mToken; 3892207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown private Surface mSurface; 39a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown 40c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright VirtualDisplay(DisplayManagerGlobal global, Display display, 4175ee9fcde4d9e1be3883eba6c8d193db4375b052Michael Wright IVirtualDisplayCallback token, Surface surface) { 42a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown mGlobal = global; 43a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown mDisplay = display; 44a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown mToken = token; 4592207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown mSurface = surface; 46a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown } 47a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown 48a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown /** 49a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * Gets the virtual display. 50a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown */ 51a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown public Display getDisplay() { 52a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown return mDisplay; 53a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown } 54a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown 55a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown /** 5692207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * Gets the surface that backs the virtual display. 5792207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown */ 5892207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown public Surface getSurface() { 5992207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown return mSurface; 6092207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown } 6192207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown 6292207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown /** 6392207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * Sets the surface that backs the virtual display. 6492207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * <p> 6592207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * Detaching the surface that backs a virtual display has a similar effect to 6692207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * turning off the screen. 6792207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * </p><p> 6892207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * It is still the caller's responsibility to destroy the surface after it has 6992207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * been detached. 7092207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * </p> 7192207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * 7292207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown * @param surface The surface to set, or null to detach the surface from the virtual display. 7392207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown */ 7492207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown public void setSurface(Surface surface) { 7592207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown if (mSurface != surface) { 7692207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown mGlobal.setVirtualDisplaySurface(mToken, surface); 7792207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown mSurface = surface; 7892207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown } 7992207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown } 8092207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown 8192207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown /** 8201e840ff9441e005153d799e71d65b38bcb21902Michael Wright * Asks the virtual display to resize. 8301e840ff9441e005153d799e71d65b38bcb21902Michael Wright *<p> 8401e840ff9441e005153d799e71d65b38bcb21902Michael Wright * This is really just a convenience to allow applications using 8501e840ff9441e005153d799e71d65b38bcb21902Michael Wright * virtual displays to adapt to changing conditions without having 8601e840ff9441e005153d799e71d65b38bcb21902Michael Wright * to tear down and recreate the display. 8701e840ff9441e005153d799e71d65b38bcb21902Michael Wright * </p> 8801e840ff9441e005153d799e71d65b38bcb21902Michael Wright */ 8901e840ff9441e005153d799e71d65b38bcb21902Michael Wright public void resize(int width, int height, int densityDpi) { 9001e840ff9441e005153d799e71d65b38bcb21902Michael Wright mGlobal.resizeVirtualDisplay(mToken, width, height, densityDpi); 9101e840ff9441e005153d799e71d65b38bcb21902Michael Wright } 9201e840ff9441e005153d799e71d65b38bcb21902Michael Wright 9301e840ff9441e005153d799e71d65b38bcb21902Michael Wright /** 94a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * Releases the virtual display and destroys its underlying surface. 95a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * <p> 96a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * All remaining windows on the virtual display will be forcibly removed 97a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * as part of releasing the virtual display. 98a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown * </p> 99a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown */ 100a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown public void release() { 101a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown if (mToken != null) { 102a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown mGlobal.releaseVirtualDisplay(mToken); 103a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown mToken = null; 104a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown } 105a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown } 106a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown 107a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown @Override 108a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown public String toString() { 10992207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown return "VirtualDisplay{display=" + mDisplay + ", token=" + mToken 11092207df753c27b094e9e0ca80d41bc0d54dc6bd5Jeff Brown + ", surface=" + mSurface + "}"; 111a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown } 112c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright 113c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright /** 114c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright * Interface for receiving information about a {@link VirtualDisplay}'s state changes. 115c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright */ 11675ee9fcde4d9e1be3883eba6c8d193db4375b052Michael Wright public static abstract class Callback { 117c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright /** 118c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright * Called when the virtual display video projection has been 119c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright * paused by the system or when the surface has been detached 120c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright * by the application by calling setSurface(null). 121c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright * The surface will not receive any more buffers while paused. 122c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright */ 12375ee9fcde4d9e1be3883eba6c8d193db4375b052Michael Wright public void onPaused() { } 124c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright 125c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright /** 126c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright * Called when the virtual display video projection has been 127c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright * resumed after having been paused. 128c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright */ 12975ee9fcde4d9e1be3883eba6c8d193db4375b052Michael Wright public void onResumed() { } 130c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright 131c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright /** 132c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright * Called when the virtual display video projection has been 133c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright * stopped by the system. It will no longer receive frames 134c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright * and it will never be resumed. It is still the responsibility 135c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright * of the application to release() the virtual display. 136c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright */ 13775ee9fcde4d9e1be3883eba6c8d193db4375b052Michael Wright public void onStopped() { } 138c39d47a8e7c74bd539104b0efab898ef6fc43ddfMichael Wright } 139a506a6ec94863a35acca9feb165db76ddac3892cJeff Brown} 140