Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 1 | package com.android.server.vr; |
| 2 | |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 3 | import static android.view.Display.INVALID_DISPLAY; |
| 4 | |
Karthik Ravi Shankar | 99493db | 2017-03-08 18:30:19 -0800 | [diff] [blame] | 5 | import android.app.ActivityManagerInternal; |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 6 | import android.app.Vr2dDisplayProperties; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 7 | import android.app.Service; |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 8 | import android.content.BroadcastReceiver; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 9 | import android.content.Context; |
| 10 | import android.content.Intent; |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 11 | import android.content.IntentFilter; |
Daniel Nicoara | 5a2ca72 | 2017-04-05 17:38:16 -0400 | [diff] [blame] | 12 | import android.graphics.PixelFormat; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 13 | import android.hardware.display.DisplayManager; |
| 14 | import android.hardware.display.VirtualDisplay; |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 15 | import android.media.ImageReader; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 16 | import android.os.Build; |
| 17 | import android.os.Handler; |
| 18 | import android.os.IBinder; |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 19 | import android.os.Message; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 20 | import android.os.RemoteException; |
| 21 | import android.os.ServiceManager; |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 22 | import android.os.SystemProperties; |
| 23 | import android.service.vr.IPersistentVrStateCallbacks; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 24 | import android.service.vr.IVrManager; |
| 25 | import android.util.Log; |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 26 | import android.view.Surface; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 27 | |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 28 | import com.android.server.wm.WindowManagerInternal; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Creates a 2D Virtual Display while VR Mode is enabled. This display will be used to run and |
| 32 | * render 2D app within a VR experience. For example, bringing up the 2D calculator app in VR. |
| 33 | */ |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 34 | class Vr2dDisplay { |
| 35 | private final static String TAG = "Vr2dDisplay"; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 36 | private final static boolean DEBUG = false; |
| 37 | |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 38 | private int mVirtualDisplayHeight; |
| 39 | private int mVirtualDisplayWidth; |
| 40 | private int mVirtualDisplayDpi; |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 41 | private final static int STOP_VIRTUAL_DISPLAY_DELAY_MILLIS = 2000; |
Santos Cordon | b060863 | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 42 | private final static String UNIQUE_DISPLAY_ID = "277f1a09-b88d-4d1e-8716-796f114d080b"; |
| 43 | private final static String DISPLAY_NAME = "VR 2D Display"; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 44 | |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 45 | private final static String DEBUG_ACTION_SET_MODE = |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 46 | "com.android.server.vr.Vr2dDisplay.SET_MODE"; |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 47 | private final static String DEBUG_EXTRA_MODE_ON = |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 48 | "com.android.server.vr.Vr2dDisplay.EXTRA_MODE_ON"; |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 49 | private final static String DEBUG_ACTION_SET_SURFACE = |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 50 | "com.android.server.vr.Vr2dDisplay.SET_SURFACE"; |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 51 | private final static String DEBUG_EXTRA_SURFACE = |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 52 | "com.android.server.vr.Vr2dDisplay.EXTRA_SURFACE"; |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 53 | |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 54 | /** |
| 55 | * The default width of the VR virtual display |
| 56 | */ |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 57 | public static final int DEFAULT_VIRTUAL_DISPLAY_WIDTH = 1400; |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * The default height of the VR virtual display |
| 61 | */ |
Karthik Ravi Shankar | 6d1575f | 2017-07-06 10:23:57 -0700 | [diff] [blame] | 62 | public static final int DEFAULT_VIRTUAL_DISPLAY_HEIGHT = 1800; |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * The default height of the VR virtual dpi. |
| 66 | */ |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 67 | public static final int DEFAULT_VIRTUAL_DISPLAY_DPI = 320; |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * The minimum height, width and dpi of VR virtual display. |
| 71 | */ |
| 72 | public static final int MIN_VR_DISPLAY_WIDTH = 1; |
| 73 | public static final int MIN_VR_DISPLAY_HEIGHT = 1; |
| 74 | public static final int MIN_VR_DISPLAY_DPI = 1; |
| 75 | |
Karthik Ravi Shankar | 99493db | 2017-03-08 18:30:19 -0800 | [diff] [blame] | 76 | private final ActivityManagerInternal mActivityManagerInternal; |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 77 | private final WindowManagerInternal mWindowManagerInternal; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 78 | private final DisplayManager mDisplayManager; |
| 79 | private final IVrManager mVrManager; |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 80 | private final Object mVdLock = new Object(); |
| 81 | private final Handler mHandler = new Handler(); |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 82 | |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Callback implementation to receive changes to VrMode. |
| 85 | **/ |
| 86 | private final IPersistentVrStateCallbacks mVrStateCallbacks = |
| 87 | new IPersistentVrStateCallbacks.Stub() { |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 88 | @Override |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 89 | public void onPersistentVrStateChanged(boolean enabled) { |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 90 | if (enabled != mIsPersistentVrModeEnabled) { |
| 91 | mIsPersistentVrModeEnabled = enabled; |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 92 | updateVirtualDisplay(); |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | private VirtualDisplay mVirtualDisplay; |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 98 | private Surface mSurface; |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 99 | private ImageReader mImageReader; |
| 100 | private Runnable mStopVDRunnable; |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 101 | private boolean mIsVrModeOverrideEnabled; // debug override to set vr mode. |
| 102 | private boolean mIsVirtualDisplayAllowed = true; // Virtual-display feature toggle |
| 103 | private boolean mIsPersistentVrModeEnabled; // indicates we are in vr persistent mode. |
| 104 | private boolean mBootsToVr = false; // The device boots into VR (standalone VR device) |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 105 | |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 106 | public Vr2dDisplay(DisplayManager displayManager, |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 107 | ActivityManagerInternal activityManagerInternal, |
| 108 | WindowManagerInternal windowManagerInternal, IVrManager vrManager) { |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 109 | mDisplayManager = displayManager; |
Karthik Ravi Shankar | 99493db | 2017-03-08 18:30:19 -0800 | [diff] [blame] | 110 | mActivityManagerInternal = activityManagerInternal; |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 111 | mWindowManagerInternal = windowManagerInternal; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 112 | mVrManager = vrManager; |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 113 | mVirtualDisplayWidth = DEFAULT_VIRTUAL_DISPLAY_WIDTH; |
| 114 | mVirtualDisplayHeight = DEFAULT_VIRTUAL_DISPLAY_HEIGHT; |
| 115 | mVirtualDisplayDpi = DEFAULT_VIRTUAL_DISPLAY_DPI; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Initializes the compabilitiy display by listening to VR mode changes. |
| 120 | */ |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 121 | public void init(Context context, boolean bootsToVr) { |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 122 | startVrModeListener(); |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 123 | startDebugOnlyBroadcastReceiver(context); |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 124 | mBootsToVr = bootsToVr; |
| 125 | if (mBootsToVr) { |
| 126 | // If we are booting into VR, we need to start the virtual display immediately. This |
| 127 | // ensures that the virtual display is up by the time Setup Wizard is started. |
| 128 | updateVirtualDisplay(); |
| 129 | } |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Creates and Destroys the virtual display depending on the current state of VrMode. |
| 134 | */ |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 135 | private void updateVirtualDisplay() { |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 136 | if (DEBUG) { |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 137 | Log.i(TAG, "isVrMode: " + mIsPersistentVrModeEnabled + ", override: " |
| 138 | + mIsVrModeOverrideEnabled + ", isAllowed: " + mIsVirtualDisplayAllowed |
| 139 | + ", bootsToVr: " + mBootsToVr); |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Santos Cordon | 627a68f | 2017-06-12 17:57:17 -0700 | [diff] [blame] | 142 | if (shouldRunVirtualDisplay()) { |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 143 | Log.i(TAG, "Attempting to start virtual display"); |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 144 | // TODO: Consider not creating the display until ActivityManager needs one on |
| 145 | // which to display a 2D application. |
Santos Cordon | 5dc6019 | 2017-05-09 11:36:53 -0700 | [diff] [blame] | 146 | startVirtualDisplay(); |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 147 | } else { |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 148 | // Stop virtual display to test exit condition |
| 149 | stopVirtualDisplay(); |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 153 | /** |
| 154 | * Creates a DEBUG-only BroadcastReceiver through which a test app can simulate VrMode and |
| 155 | * set a custom Surface for the virtual display. This allows testing of the virtual display |
| 156 | * without going into full 3D. |
| 157 | * |
| 158 | * @param context The context. |
| 159 | */ |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 160 | private void startDebugOnlyBroadcastReceiver(Context context) { |
Santos Cordon | 5dc6019 | 2017-05-09 11:36:53 -0700 | [diff] [blame] | 161 | if (DEBUG) { |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 162 | IntentFilter intentFilter = new IntentFilter(DEBUG_ACTION_SET_MODE); |
| 163 | intentFilter.addAction(DEBUG_ACTION_SET_SURFACE); |
| 164 | |
| 165 | context.registerReceiver(new BroadcastReceiver() { |
| 166 | @Override |
| 167 | public void onReceive(Context context, Intent intent) { |
| 168 | final String action = intent.getAction(); |
| 169 | if (DEBUG_ACTION_SET_MODE.equals(action)) { |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 170 | mIsVrModeOverrideEnabled = |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 171 | intent.getBooleanExtra(DEBUG_EXTRA_MODE_ON, false); |
| 172 | updateVirtualDisplay(); |
| 173 | } else if (DEBUG_ACTION_SET_SURFACE.equals(action)) { |
| 174 | if (mVirtualDisplay != null) { |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 175 | if (intent.hasExtra(DEBUG_EXTRA_SURFACE)) { |
| 176 | setSurfaceLocked(intent.getParcelableExtra(DEBUG_EXTRA_SURFACE)); |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 177 | } |
| 178 | } else { |
| 179 | Log.w(TAG, "Cannot set the surface because the VD is null."); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | }, intentFilter); |
| 184 | } |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 187 | /** |
| 188 | * Starts listening to VrMode changes. |
| 189 | */ |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 190 | private void startVrModeListener() { |
| 191 | if (mVrManager != null) { |
| 192 | try { |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 193 | mVrManager.registerPersistentVrStateListener(mVrStateCallbacks); |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 194 | } catch (RemoteException e) { |
| 195 | Log.e(TAG, "Could not register VR State listener.", e); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 200 | /** |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 201 | * Sets the resolution and DPI of the Vr2d virtual display used to display |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 202 | * 2D applications in VR mode. |
| 203 | * |
| 204 | * <p>Requires {@link android.Manifest.permission#ACCESS_VR_MANAGER} permission.</p> |
| 205 | * |
Santos Cordon | 627a68f | 2017-06-12 17:57:17 -0700 | [diff] [blame] | 206 | * @param displayProperties Properties of the virtual display for 2D applications |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 207 | * in VR mode. |
| 208 | */ |
Santos Cordon | 627a68f | 2017-06-12 17:57:17 -0700 | [diff] [blame] | 209 | public void setVirtualDisplayProperties(Vr2dDisplayProperties displayProperties) { |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 210 | synchronized(mVdLock) { |
| 211 | if (DEBUG) { |
Santos Cordon | 627a68f | 2017-06-12 17:57:17 -0700 | [diff] [blame] | 212 | Log.i(TAG, "VD setVirtualDisplayProperties: " + |
| 213 | displayProperties.toString()); |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Santos Cordon | 627a68f | 2017-06-12 17:57:17 -0700 | [diff] [blame] | 216 | int width = displayProperties.getWidth(); |
| 217 | int height = displayProperties.getHeight(); |
| 218 | int dpi = displayProperties.getDpi(); |
| 219 | boolean resized = false; |
| 220 | |
| 221 | if (width < MIN_VR_DISPLAY_WIDTH || height < MIN_VR_DISPLAY_HEIGHT || |
| 222 | dpi < MIN_VR_DISPLAY_DPI) { |
| 223 | Log.i(TAG, "Ignoring Width/Height/Dpi values of " + width + "," + height + "," |
| 224 | + dpi); |
| 225 | } else { |
| 226 | Log.i(TAG, "Setting width/height/dpi to " + width + "," + height + "," + dpi); |
| 227 | mVirtualDisplayWidth = width; |
| 228 | mVirtualDisplayHeight = height; |
| 229 | mVirtualDisplayDpi = dpi; |
| 230 | resized = true; |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Santos Cordon | 627a68f | 2017-06-12 17:57:17 -0700 | [diff] [blame] | 233 | if ((displayProperties.getFlags() & Vr2dDisplayProperties.FLAG_VIRTUAL_DISPLAY_ENABLED) |
| 234 | == Vr2dDisplayProperties.FLAG_VIRTUAL_DISPLAY_ENABLED) { |
| 235 | mIsVirtualDisplayAllowed = true; |
| 236 | } else if ((displayProperties.getRemovedFlags() & |
| 237 | Vr2dDisplayProperties.FLAG_VIRTUAL_DISPLAY_ENABLED) |
| 238 | == Vr2dDisplayProperties.FLAG_VIRTUAL_DISPLAY_ENABLED) { |
| 239 | mIsVirtualDisplayAllowed = false; |
| 240 | } |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 241 | |
Santos Cordon | 627a68f | 2017-06-12 17:57:17 -0700 | [diff] [blame] | 242 | if (mVirtualDisplay != null && resized && mIsVirtualDisplayAllowed) { |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 243 | mVirtualDisplay.resize(mVirtualDisplayWidth, mVirtualDisplayHeight, |
| 244 | mVirtualDisplayDpi); |
| 245 | ImageReader oldImageReader = mImageReader; |
| 246 | mImageReader = null; |
| 247 | startImageReader(); |
| 248 | oldImageReader.close(); |
| 249 | } |
Santos Cordon | 627a68f | 2017-06-12 17:57:17 -0700 | [diff] [blame] | 250 | |
| 251 | // Start/Stop the virtual display in case the updates indicated that we should. |
| 252 | updateVirtualDisplay(); |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 257 | * Returns the virtual display ID if one currently exists, otherwise returns |
| 258 | * {@link INVALID_DISPLAY_ID}. |
| 259 | * |
| 260 | * @return The virtual display ID. |
| 261 | */ |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 262 | public int getVirtualDisplayId() { |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 263 | synchronized(mVdLock) { |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 264 | if (mVirtualDisplay != null) { |
| 265 | int virtualDisplayId = mVirtualDisplay.getDisplay().getDisplayId(); |
| 266 | if (DEBUG) { |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 267 | Log.i(TAG, "VD id: " + virtualDisplayId); |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 268 | } |
| 269 | return virtualDisplayId; |
| 270 | } |
| 271 | } |
| 272 | return INVALID_DISPLAY; |
| 273 | } |
| 274 | |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 275 | /** |
| 276 | * Starts the virtual display if one does not already exist. |
| 277 | */ |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 278 | private void startVirtualDisplay() { |
| 279 | if (DEBUG) { |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 280 | Log.d(TAG, "Request to start VD, DM:" + mDisplayManager); |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | if (mDisplayManager == null) { |
| 284 | Log.w(TAG, "Cannot create virtual display because mDisplayManager == null"); |
| 285 | return; |
| 286 | } |
| 287 | |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 288 | synchronized (mVdLock) { |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 289 | if (mVirtualDisplay != null) { |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 290 | Log.i(TAG, "VD already exists, ignoring request"); |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 291 | return; |
| 292 | } |
| 293 | |
Santos Cordon | b060863 | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 294 | int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH; |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 295 | flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_ROTATES_WITH_CONTENT; |
Karthik Ravi Shankar | 4c65738 | 2017-09-26 13:40:58 -0700 | [diff] [blame] | 296 | flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC; |
| 297 | flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY; |
rongliu | 1e90fc3 | 2017-10-04 17:30:30 -0700 | [diff] [blame] | 298 | flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_DESTROY_CONTENT_ON_REMOVAL; |
Santos Cordon | b6992f2 | 2018-01-30 14:51:20 -0800 | [diff] [blame] | 299 | flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE; |
| 300 | |
Santos Cordon | b060863 | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 301 | mVirtualDisplay = mDisplayManager.createVirtualDisplay(null /* projection */, |
| 302 | DISPLAY_NAME, mVirtualDisplayWidth, mVirtualDisplayHeight, mVirtualDisplayDpi, |
| 303 | null /* surface */, flags, null /* callback */, null /* handler */, |
| 304 | UNIQUE_DISPLAY_ID); |
Karthik Ravi Shankar | 99493db | 2017-03-08 18:30:19 -0800 | [diff] [blame] | 305 | |
| 306 | if (mVirtualDisplay != null) { |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 307 | updateDisplayId(mVirtualDisplay.getDisplay().getDisplayId()); |
Santos Cordon | 728d81f | 2017-04-12 15:20:33 -0700 | [diff] [blame] | 308 | // Now create the ImageReader to supply a Surface to the new virtual display. |
| 309 | startImageReader(); |
Karthik Ravi Shankar | 99493db | 2017-03-08 18:30:19 -0800 | [diff] [blame] | 310 | } else { |
| 311 | Log.w(TAG, "Virtual display id is null after createVirtualDisplay"); |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 312 | updateDisplayId(INVALID_DISPLAY); |
Karthik Ravi Shankar | 99493db | 2017-03-08 18:30:19 -0800 | [diff] [blame] | 313 | return; |
| 314 | } |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 317 | Log.i(TAG, "VD created: " + mVirtualDisplay); |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 320 | private void updateDisplayId(int displayId) { |
| 321 | mActivityManagerInternal.setVr2dDisplayId(displayId); |
| 322 | mWindowManagerInternal.setVr2dDisplayId(displayId); |
| 323 | } |
| 324 | |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 325 | /** |
| 326 | * Stops the virtual display with a {@link #STOP_VIRTUAL_DISPLAY_DELAY_MILLIS} timeout. |
| 327 | * The timeout prevents the virtual display from bouncing in cases where VrMode goes in and out |
| 328 | * of being enabled. This can happen sometimes with our 2D test app. |
| 329 | */ |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 330 | private void stopVirtualDisplay() { |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 331 | if (mStopVDRunnable == null) { |
| 332 | mStopVDRunnable = new Runnable() { |
| 333 | @Override |
| 334 | public void run() { |
Santos Cordon | 627a68f | 2017-06-12 17:57:17 -0700 | [diff] [blame] | 335 | if (shouldRunVirtualDisplay()) { |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 336 | Log.i(TAG, "Virtual Display destruction stopped: VrMode is back on."); |
| 337 | } else { |
| 338 | Log.i(TAG, "Stopping Virtual Display"); |
| 339 | synchronized (mVdLock) { |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 340 | updateDisplayId(INVALID_DISPLAY); |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 341 | setSurfaceLocked(null); // clean up and release the surface first. |
| 342 | if (mVirtualDisplay != null) { |
| 343 | mVirtualDisplay.release(); |
| 344 | mVirtualDisplay = null; |
| 345 | } |
Santos Cordon | 728d81f | 2017-04-12 15:20:33 -0700 | [diff] [blame] | 346 | stopImageReader(); |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | } |
| 350 | }; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 353 | mHandler.removeCallbacks(mStopVDRunnable); |
| 354 | mHandler.postDelayed(mStopVDRunnable, STOP_VIRTUAL_DISPLAY_DELAY_MILLIS); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Set the surface to use with the virtual display. |
| 359 | * |
| 360 | * Code should be locked by {@link #mVdLock} before invoked. |
| 361 | * |
| 362 | * @param surface The Surface to set. |
| 363 | */ |
| 364 | private void setSurfaceLocked(Surface surface) { |
| 365 | // Change the surface to either a valid surface or a null value. |
| 366 | if (mSurface != surface && (surface == null || surface.isValid())) { |
| 367 | Log.i(TAG, "Setting the new surface from " + mSurface + " to " + surface); |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 368 | if (mVirtualDisplay != null) { |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 369 | mVirtualDisplay.setSurface(surface); |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 370 | } |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 371 | if (mSurface != null) { |
| 372 | mSurface.release(); |
| 373 | } |
| 374 | mSurface = surface; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Starts an ImageReader as a do-nothing Surface. The virtual display will not get fully |
| 380 | * initialized within surface flinger unless it has a valid Surface associated with it. We use |
| 381 | * the ImageReader as the default valid Surface. |
| 382 | */ |
| 383 | private void startImageReader() { |
| 384 | if (mImageReader == null) { |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 385 | mImageReader = ImageReader.newInstance(mVirtualDisplayWidth, mVirtualDisplayHeight, |
| 386 | PixelFormat.RGBA_8888, 2 /* maxImages */); |
| 387 | Log.i(TAG, "VD startImageReader: res = " + mVirtualDisplayWidth + "X" + |
| 388 | mVirtualDisplayHeight + ", dpi = " + mVirtualDisplayDpi); |
Santos Cordon | 975c00d | 2017-03-21 17:05:36 -0700 | [diff] [blame] | 389 | } |
| 390 | synchronized (mVdLock) { |
| 391 | setSurfaceLocked(mImageReader.getSurface()); |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
Santos Cordon | 728d81f | 2017-04-12 15:20:33 -0700 | [diff] [blame] | 394 | |
| 395 | /** |
| 396 | * Cleans up the ImageReader. |
| 397 | */ |
| 398 | private void stopImageReader() { |
| 399 | if (mImageReader != null) { |
| 400 | mImageReader.close(); |
| 401 | mImageReader = null; |
| 402 | } |
| 403 | } |
Santos Cordon | 627a68f | 2017-06-12 17:57:17 -0700 | [diff] [blame] | 404 | |
| 405 | private boolean shouldRunVirtualDisplay() { |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 406 | // Virtual Display should run whenever: |
| 407 | // * Virtual Display is allowed/enabled AND |
| 408 | // (1) BootsToVr is set indicating the device never leaves VR |
| 409 | // (2) VR (persistent) mode is enabled |
| 410 | // (3) VR mode is overridden to be enabled. |
| 411 | return mIsVirtualDisplayAllowed && |
| 412 | (mBootsToVr || mIsPersistentVrModeEnabled || mIsVrModeOverrideEnabled); |
Santos Cordon | 627a68f | 2017-06-12 17:57:17 -0700 | [diff] [blame] | 413 | } |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 414 | } |