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