Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package com.android.server.display; |
| 18 | |
Andrii Kulian | 7211d2e | 2017-01-27 15:58:05 -0800 | [diff] [blame] | 19 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR; |
Louis Chang | bd48dca | 2018-08-29 17:44:34 +0800 | [diff] [blame] | 20 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD; |
| 21 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_DESTROY_CONTENT_ON_REMOVAL; |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 22 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION; |
| 23 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC; |
| 24 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_ROTATES_WITH_CONTENT; |
| 25 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE; |
Louis Chang | bd48dca | 2018-08-29 17:44:34 +0800 | [diff] [blame] | 26 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS; |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 27 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH; |
Charles Chen | b28fb72 | 2020-05-21 17:19:32 +0800 | [diff] [blame] | 28 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_TRUSTED; |
| 29 | |
| 30 | import static com.android.server.display.DisplayDeviceInfo.FLAG_TRUSTED; |
Andrii Kulian | 7211d2e | 2017-01-27 15:58:05 -0800 | [diff] [blame] | 31 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 32 | import android.content.Context; |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 33 | import android.hardware.display.IVirtualDisplayCallback; |
b0202.jung | 925435c | 2020-01-08 18:46:59 +0900 | [diff] [blame] | 34 | import android.hardware.display.VirtualDisplayConfig; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 35 | import android.media.projection.IMediaProjection; |
| 36 | import android.media.projection.IMediaProjectionCallback; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 37 | import android.os.Handler; |
| 38 | import android.os.IBinder; |
| 39 | import android.os.IBinder.DeathRecipient; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 40 | import android.os.Message; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 41 | import android.os.RemoteException; |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 42 | import android.os.SystemProperties; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 43 | import android.util.ArrayMap; |
| 44 | import android.util.Slog; |
| 45 | import android.view.Display; |
| 46 | import android.view.Surface; |
| 47 | import android.view.SurfaceControl; |
| 48 | |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 49 | import com.android.internal.annotations.VisibleForTesting; |
| 50 | |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 51 | import java.io.PrintWriter; |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 52 | import java.util.Iterator; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 53 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 54 | /** |
| 55 | * A display adapter that provides virtual displays on behalf of applications. |
| 56 | * <p> |
| 57 | * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock. |
| 58 | * </p> |
| 59 | */ |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 60 | @VisibleForTesting |
| 61 | public class VirtualDisplayAdapter extends DisplayAdapter { |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 62 | static final String TAG = "VirtualDisplayAdapter"; |
| 63 | static final boolean DEBUG = false; |
| 64 | |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 65 | // Unique id prefix for virtual displays |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 66 | @VisibleForTesting |
| 67 | static final String UNIQUE_ID_PREFIX = "virtual:"; |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 68 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 69 | private final ArrayMap<IBinder, VirtualDisplayDevice> mVirtualDisplayDevices = |
| 70 | new ArrayMap<IBinder, VirtualDisplayDevice>(); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 71 | private final Handler mHandler; |
| 72 | private final SurfaceControlDisplayFactory mSurfaceControlDisplayFactory; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 73 | |
| 74 | // Called with SyncRoot lock held. |
| 75 | public VirtualDisplayAdapter(DisplayManagerService.SyncRoot syncRoot, |
| 76 | Context context, Handler handler, Listener listener) { |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 77 | this(syncRoot, context, handler, listener, |
| 78 | (String name, boolean secure) -> SurfaceControl.createDisplay(name, secure)); |
| 79 | } |
| 80 | |
| 81 | @VisibleForTesting |
| 82 | VirtualDisplayAdapter(DisplayManagerService.SyncRoot syncRoot, |
| 83 | Context context, Handler handler, Listener listener, |
| 84 | SurfaceControlDisplayFactory surfaceControlDisplayFactory) { |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 85 | super(syncRoot, context, handler, listener, TAG); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 86 | mHandler = handler; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 87 | mSurfaceControlDisplayFactory = surfaceControlDisplayFactory; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 90 | public DisplayDevice createVirtualDisplayLocked(IVirtualDisplayCallback callback, |
b0202.jung | 925435c | 2020-01-08 18:46:59 +0900 | [diff] [blame] | 91 | IMediaProjection projection, int ownerUid, String ownerPackageName, Surface surface, |
| 92 | int flags, VirtualDisplayConfig virtualDisplayConfig) { |
| 93 | String name = virtualDisplayConfig.getName(); |
Andrii Kulian | 7211d2e | 2017-01-27 15:58:05 -0800 | [diff] [blame] | 94 | boolean secure = (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0; |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 95 | IBinder appToken = callback.asBinder(); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 96 | IBinder displayToken = mSurfaceControlDisplayFactory.createDisplay(name, secure); |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 97 | final String baseUniqueId = |
| 98 | UNIQUE_ID_PREFIX + ownerPackageName + "," + ownerUid + "," + name + ","; |
| 99 | final int uniqueIndex = getNextUniqueIndex(baseUniqueId); |
b0202.jung | 925435c | 2020-01-08 18:46:59 +0900 | [diff] [blame] | 100 | String uniqueId = virtualDisplayConfig.getUniqueId(); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 101 | if (uniqueId == null) { |
| 102 | uniqueId = baseUniqueId + uniqueIndex; |
| 103 | } else { |
| 104 | uniqueId = UNIQUE_ID_PREFIX + ownerPackageName + ":" + uniqueId; |
| 105 | } |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 106 | VirtualDisplayDevice device = new VirtualDisplayDevice(displayToken, appToken, |
b0202.jung | 925435c | 2020-01-08 18:46:59 +0900 | [diff] [blame] | 107 | ownerUid, ownerPackageName, surface, flags, new Callback(callback, mHandler), |
| 108 | uniqueId, uniqueIndex, virtualDisplayConfig); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 109 | |
| 110 | mVirtualDisplayDevices.put(appToken, device); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 111 | |
| 112 | try { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 113 | if (projection != null) { |
Michael Wright | cde5bb4 | 2014-09-08 13:26:34 -0700 | [diff] [blame] | 114 | projection.registerCallback(new MediaProjectionCallback(appToken)); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 115 | } |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 116 | appToken.linkToDeath(device, 0); |
| 117 | } catch (RemoteException ex) { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 118 | mVirtualDisplayDevices.remove(appToken); |
Michael Wright | d1a5fae | 2015-05-05 14:16:35 +0100 | [diff] [blame] | 119 | device.destroyLocked(false); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 120 | return null; |
| 121 | } |
| 122 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 123 | // Return the display device without actually sending the event indicating |
| 124 | // that it was added. The caller will handle it. |
| 125 | return device; |
| 126 | } |
| 127 | |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 128 | public void resizeVirtualDisplayLocked(IBinder appToken, |
| 129 | int width, int height, int densityDpi) { |
| 130 | VirtualDisplayDevice device = mVirtualDisplayDevices.get(appToken); |
| 131 | if (device != null) { |
| 132 | device.resizeLocked(width, height, densityDpi); |
| 133 | } |
| 134 | } |
| 135 | |
b0202.jung | 925435c | 2020-01-08 18:46:59 +0900 | [diff] [blame] | 136 | @VisibleForTesting |
| 137 | Surface getVirtualDisplaySurfaceLocked(IBinder appToken) { |
| 138 | VirtualDisplayDevice device = mVirtualDisplayDevices.get(appToken); |
| 139 | if (device != null) { |
| 140 | return device.getSurfaceLocked(); |
| 141 | } |
| 142 | return null; |
| 143 | } |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 144 | |
Jeff Brown | 92207df | 2014-04-16 13:16:07 -0700 | [diff] [blame] | 145 | public void setVirtualDisplaySurfaceLocked(IBinder appToken, Surface surface) { |
| 146 | VirtualDisplayDevice device = mVirtualDisplayDevices.get(appToken); |
| 147 | if (device != null) { |
| 148 | device.setSurfaceLocked(surface); |
| 149 | } |
| 150 | } |
| 151 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 152 | public DisplayDevice releaseVirtualDisplayLocked(IBinder appToken) { |
| 153 | VirtualDisplayDevice device = mVirtualDisplayDevices.remove(appToken); |
| 154 | if (device != null) { |
Michael Wright | d1a5fae | 2015-05-05 14:16:35 +0100 | [diff] [blame] | 155 | device.destroyLocked(true); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 156 | appToken.unlinkToDeath(device, 0); |
| 157 | } |
| 158 | |
| 159 | // Return the display device that was removed without actually sending the |
| 160 | // event indicating that it was removed. The caller will handle it. |
| 161 | return device; |
| 162 | } |
| 163 | |
chaviw | da4c694 | 2018-11-07 15:52:56 -0800 | [diff] [blame] | 164 | void setVirtualDisplayStateLocked(IBinder appToken, boolean isOn) { |
| 165 | VirtualDisplayDevice device = mVirtualDisplayDevices.get(appToken); |
| 166 | if (device != null) { |
| 167 | device.setDisplayState(isOn); |
| 168 | } |
| 169 | } |
| 170 | |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 171 | /** |
| 172 | * Returns the next unique index for the uniqueIdPrefix |
| 173 | */ |
| 174 | private int getNextUniqueIndex(String uniqueIdPrefix) { |
| 175 | if (mVirtualDisplayDevices.isEmpty()) { |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | int nextUniqueIndex = 0; |
| 180 | Iterator<VirtualDisplayDevice> it = mVirtualDisplayDevices.values().iterator(); |
| 181 | while (it.hasNext()) { |
| 182 | VirtualDisplayDevice device = it.next(); |
| 183 | if (device.getUniqueId().startsWith(uniqueIdPrefix) |
| 184 | && device.mUniqueIndex >= nextUniqueIndex) { |
| 185 | // Increment the next unique index to be greater than ones we have already ran |
| 186 | // across for displays that have the same unique Id prefix. |
| 187 | nextUniqueIndex = device.mUniqueIndex + 1; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return nextUniqueIndex; |
| 192 | } |
| 193 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 194 | private void handleBinderDiedLocked(IBinder appToken) { |
Bryce Lee | a3320fe | 2017-02-15 17:47:25 -0800 | [diff] [blame] | 195 | mVirtualDisplayDevices.remove(appToken); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 198 | private void handleMediaProjectionStoppedLocked(IBinder appToken) { |
| 199 | VirtualDisplayDevice device = mVirtualDisplayDevices.remove(appToken); |
| 200 | if (device != null) { |
| 201 | Slog.i(TAG, "Virtual display device released because media projection stopped: " |
| 202 | + device.mName); |
| 203 | device.stopLocked(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | private final class VirtualDisplayDevice extends DisplayDevice implements DeathRecipient { |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 208 | private static final int PENDING_SURFACE_CHANGE = 0x01; |
| 209 | private static final int PENDING_RESIZE = 0x02; |
| 210 | |
P.Y. Laligand | 5c7773d | 2015-05-04 13:30:58 -0700 | [diff] [blame] | 211 | private static final float REFRESH_RATE = 60.0f; |
| 212 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 213 | private final IBinder mAppToken; |
| 214 | private final int mOwnerUid; |
| 215 | final String mOwnerPackageName; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 216 | final String mName; |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 217 | private final int mFlags; |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 218 | private final Callback mCallback; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 219 | |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 220 | private int mWidth; |
| 221 | private int mHeight; |
| 222 | private int mDensityDpi; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 223 | private Surface mSurface; |
| 224 | private DisplayDeviceInfo mInfo; |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 225 | private int mDisplayState; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 226 | private boolean mStopped; |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 227 | private int mPendingChanges; |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 228 | private int mUniqueIndex; |
P.Y. Laligand | 5c7773d | 2015-05-04 13:30:58 -0700 | [diff] [blame] | 229 | private Display.Mode mMode; |
chaviw | da4c694 | 2018-11-07 15:52:56 -0800 | [diff] [blame] | 230 | private boolean mIsDisplayOn; |
b0202.jung | 925435c | 2020-01-08 18:46:59 +0900 | [diff] [blame] | 231 | private int mDisplayIdToMirror; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 232 | |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 233 | public VirtualDisplayDevice(IBinder displayToken, IBinder appToken, |
b0202.jung | 925435c | 2020-01-08 18:46:59 +0900 | [diff] [blame] | 234 | int ownerUid, String ownerPackageName, Surface surface, int flags, |
| 235 | Callback callback, String uniqueId, int uniqueIndex, |
| 236 | VirtualDisplayConfig virtualDisplayConfig) { |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 237 | super(VirtualDisplayAdapter.this, displayToken, uniqueId); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 238 | mAppToken = appToken; |
| 239 | mOwnerUid = ownerUid; |
| 240 | mOwnerPackageName = ownerPackageName; |
b0202.jung | 925435c | 2020-01-08 18:46:59 +0900 | [diff] [blame] | 241 | mName = virtualDisplayConfig.getName(); |
| 242 | mWidth = virtualDisplayConfig.getWidth(); |
| 243 | mHeight = virtualDisplayConfig.getHeight(); |
| 244 | mMode = createMode(mWidth, mHeight, REFRESH_RATE); |
| 245 | mDensityDpi = virtualDisplayConfig.getDensityDpi(); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 246 | mSurface = surface; |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 247 | mFlags = flags; |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 248 | mCallback = callback; |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 249 | mDisplayState = Display.STATE_UNKNOWN; |
| 250 | mPendingChanges |= PENDING_SURFACE_CHANGE; |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 251 | mUniqueIndex = uniqueIndex; |
chaviw | da4c694 | 2018-11-07 15:52:56 -0800 | [diff] [blame] | 252 | mIsDisplayOn = surface != null; |
b0202.jung | 925435c | 2020-01-08 18:46:59 +0900 | [diff] [blame] | 253 | mDisplayIdToMirror = virtualDisplayConfig.getDisplayIdToMirror(); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void binderDied() { |
| 258 | synchronized (getSyncRoot()) { |
Michael Wright | d1a5fae | 2015-05-05 14:16:35 +0100 | [diff] [blame] | 259 | handleBinderDiedLocked(mAppToken); |
Bryce Lee | a3320fe | 2017-02-15 17:47:25 -0800 | [diff] [blame] | 260 | Slog.i(TAG, "Virtual display device released because application token died: " |
| 261 | + mOwnerPackageName); |
| 262 | destroyLocked(false); |
| 263 | sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_REMOVED); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Michael Wright | d1a5fae | 2015-05-05 14:16:35 +0100 | [diff] [blame] | 267 | public void destroyLocked(boolean binderAlive) { |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 268 | if (mSurface != null) { |
| 269 | mSurface.release(); |
| 270 | mSurface = null; |
| 271 | } |
| 272 | SurfaceControl.destroyDisplay(getDisplayTokenLocked()); |
Michael Wright | d1a5fae | 2015-05-05 14:16:35 +0100 | [diff] [blame] | 273 | if (binderAlive) { |
| 274 | mCallback.dispatchDisplayStopped(); |
| 275 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | @Override |
b0202.jung | 925435c | 2020-01-08 18:46:59 +0900 | [diff] [blame] | 279 | public int getDisplayIdToMirrorLocked() { |
| 280 | return mDisplayIdToMirror; |
| 281 | } |
| 282 | |
| 283 | @VisibleForTesting |
| 284 | Surface getSurfaceLocked() { |
| 285 | return mSurface; |
| 286 | } |
| 287 | |
| 288 | @Override |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 289 | public boolean hasStableUniqueId() { |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | @Override |
Fiona Campbell | d4eb295 | 2019-11-04 17:19:56 +0000 | [diff] [blame] | 294 | public Runnable requestDisplayStateLocked(int state, float brightnessState) { |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 295 | if (state != mDisplayState) { |
| 296 | mDisplayState = state; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 297 | if (state == Display.STATE_OFF) { |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 298 | mCallback.dispatchDisplayPaused(); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 299 | } else { |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 300 | mCallback.dispatchDisplayResumed(); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
Jeff Brown | e75926d | 2014-09-18 15:24:49 -0700 | [diff] [blame] | 303 | return null; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 307 | public void performTraversalLocked(SurfaceControl.Transaction t) { |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 308 | if ((mPendingChanges & PENDING_RESIZE) != 0) { |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 309 | t.setDisplaySize(getDisplayTokenLocked(), mWidth, mHeight); |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 310 | } |
| 311 | if ((mPendingChanges & PENDING_SURFACE_CHANGE) != 0) { |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 312 | setSurfaceLocked(t, mSurface); |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 313 | } |
| 314 | mPendingChanges = 0; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Jeff Brown | 92207df | 2014-04-16 13:16:07 -0700 | [diff] [blame] | 317 | public void setSurfaceLocked(Surface surface) { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 318 | if (!mStopped && mSurface != surface) { |
Jeff Brown | 92207df | 2014-04-16 13:16:07 -0700 | [diff] [blame] | 319 | if ((mSurface != null) != (surface != null)) { |
| 320 | sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED); |
| 321 | } |
| 322 | sendTraversalRequestLocked(); |
| 323 | mSurface = surface; |
| 324 | mInfo = null; |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 325 | mPendingChanges |= PENDING_SURFACE_CHANGE; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | public void resizeLocked(int width, int height, int densityDpi) { |
| 330 | if (mWidth != width || mHeight != height || mDensityDpi != densityDpi) { |
| 331 | sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED); |
| 332 | sendTraversalRequestLocked(); |
| 333 | mWidth = width; |
| 334 | mHeight = height; |
P.Y. Laligand | 5c7773d | 2015-05-04 13:30:58 -0700 | [diff] [blame] | 335 | mMode = createMode(width, height, REFRESH_RATE); |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 336 | mDensityDpi = densityDpi; |
| 337 | mInfo = null; |
| 338 | mPendingChanges |= PENDING_RESIZE; |
Jeff Brown | 92207df | 2014-04-16 13:16:07 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
chaviw | da4c694 | 2018-11-07 15:52:56 -0800 | [diff] [blame] | 342 | void setDisplayState(boolean isOn) { |
| 343 | if (mIsDisplayOn != isOn) { |
| 344 | mIsDisplayOn = isOn; |
| 345 | mInfo = null; |
| 346 | sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED); |
| 347 | } |
| 348 | } |
| 349 | |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 350 | public void stopLocked() { |
| 351 | setSurfaceLocked(null); |
| 352 | mStopped = true; |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public void dumpLocked(PrintWriter pw) { |
| 357 | super.dumpLocked(pw); |
| 358 | pw.println("mFlags=" + mFlags); |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 359 | pw.println("mDisplayState=" + Display.stateToString(mDisplayState)); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 360 | pw.println("mStopped=" + mStopped); |
b0202.jung | 925435c | 2020-01-08 18:46:59 +0900 | [diff] [blame] | 361 | pw.println("mDisplayIdToMirror=" + mDisplayIdToMirror); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 365 | @Override |
| 366 | public DisplayDeviceInfo getDisplayDeviceInfoLocked() { |
| 367 | if (mInfo == null) { |
| 368 | mInfo = new DisplayDeviceInfo(); |
| 369 | mInfo.name = mName; |
Wale Ogunwale | 361ca21 | 2014-11-20 11:42:38 -0800 | [diff] [blame] | 370 | mInfo.uniqueId = getUniqueId(); |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 371 | mInfo.width = mWidth; |
| 372 | mInfo.height = mHeight; |
P.Y. Laligand | 5c7773d | 2015-05-04 13:30:58 -0700 | [diff] [blame] | 373 | mInfo.modeId = mMode.getModeId(); |
| 374 | mInfo.defaultModeId = mMode.getModeId(); |
| 375 | mInfo.supportedModes = new Display.Mode[] { mMode }; |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 376 | mInfo.densityDpi = mDensityDpi; |
| 377 | mInfo.xDpi = mDensityDpi; |
| 378 | mInfo.yDpi = mDensityDpi; |
P.Y. Laligand | 5c7773d | 2015-05-04 13:30:58 -0700 | [diff] [blame] | 379 | mInfo.presentationDeadlineNanos = 1000000000L / (int) REFRESH_RATE; // 1 frame |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 380 | mInfo.flags = 0; |
Andrii Kulian | 7211d2e | 2017-01-27 15:58:05 -0800 | [diff] [blame] | 381 | if ((mFlags & VIRTUAL_DISPLAY_FLAG_PUBLIC) == 0) { |
Michael Wright | 6720be4 | 2014-07-29 19:14:16 -0700 | [diff] [blame] | 382 | mInfo.flags |= DisplayDeviceInfo.FLAG_PRIVATE |
| 383 | | DisplayDeviceInfo.FLAG_NEVER_BLANK; |
| 384 | } |
Andrii Kulian | 7211d2e | 2017-01-27 15:58:05 -0800 | [diff] [blame] | 385 | if ((mFlags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) { |
Michael Wright | 6720be4 | 2014-07-29 19:14:16 -0700 | [diff] [blame] | 386 | mInfo.flags &= ~DisplayDeviceInfo.FLAG_NEVER_BLANK; |
| 387 | } else { |
Jeff Brown | d14c8c9 | 2014-01-07 18:13:09 -0800 | [diff] [blame] | 388 | mInfo.flags |= DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY; |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 389 | } |
Michael Wright | 6720be4 | 2014-07-29 19:14:16 -0700 | [diff] [blame] | 390 | |
Andrii Kulian | 7211d2e | 2017-01-27 15:58:05 -0800 | [diff] [blame] | 391 | if ((mFlags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) { |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 392 | mInfo.flags |= DisplayDeviceInfo.FLAG_SECURE; |
| 393 | } |
Andrii Kulian | 7211d2e | 2017-01-27 15:58:05 -0800 | [diff] [blame] | 394 | if ((mFlags & VIRTUAL_DISPLAY_FLAG_PRESENTATION) != 0) { |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 395 | mInfo.flags |= DisplayDeviceInfo.FLAG_PRESENTATION; |
Chong Zhang | ae6119ff | 2014-11-11 18:54:39 -0800 | [diff] [blame] | 396 | |
Andrii Kulian | 7211d2e | 2017-01-27 15:58:05 -0800 | [diff] [blame] | 397 | if ((mFlags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) { |
Chong Zhang | ae6119ff | 2014-11-11 18:54:39 -0800 | [diff] [blame] | 398 | // For demonstration purposes, allow rotation of the external display. |
| 399 | // In the future we might allow the user to configure this directly. |
| 400 | if ("portrait".equals(SystemProperties.get( |
| 401 | "persist.demo.remoterotation"))) { |
| 402 | mInfo.rotation = Surface.ROTATION_270; |
| 403 | } |
| 404 | } |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 405 | } |
Andrii Kulian | 7211d2e | 2017-01-27 15:58:05 -0800 | [diff] [blame] | 406 | if ((mFlags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) { |
| 407 | mInfo.flags |= DisplayDeviceInfo.FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD; |
Andrii Kulian | fc8f82b | 2017-01-26 13:17:27 -0800 | [diff] [blame] | 408 | } |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 409 | if ((mFlags & VIRTUAL_DISPLAY_FLAG_ROTATES_WITH_CONTENT) != 0) { |
| 410 | mInfo.flags |= DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT; |
| 411 | } |
rongliu | 1e90fc3 | 2017-10-04 17:30:30 -0700 | [diff] [blame] | 412 | if ((mFlags & VIRTUAL_DISPLAY_FLAG_DESTROY_CONTENT_ON_REMOVAL) != 0) { |
Louis Chang | bd48dca | 2018-08-29 17:44:34 +0800 | [diff] [blame] | 413 | mInfo.flags |= DisplayDeviceInfo.FLAG_DESTROY_CONTENT_ON_REMOVAL; |
| 414 | } |
| 415 | if ((mFlags & VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0) { |
| 416 | mInfo.flags |= DisplayDeviceInfo.FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS; |
rongliu | 1e90fc3 | 2017-10-04 17:30:30 -0700 | [diff] [blame] | 417 | } |
Charles Chen | b28fb72 | 2020-05-21 17:19:32 +0800 | [diff] [blame] | 418 | if ((mFlags & VIRTUAL_DISPLAY_FLAG_TRUSTED) != 0) { |
| 419 | mInfo.flags |= FLAG_TRUSTED; |
| 420 | } |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 421 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 422 | mInfo.type = Display.TYPE_VIRTUAL; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 423 | mInfo.touch = ((mFlags & VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH) == 0) ? |
| 424 | DisplayDeviceInfo.TOUCH_NONE : DisplayDeviceInfo.TOUCH_VIRTUAL; |
chaviw | da4c694 | 2018-11-07 15:52:56 -0800 | [diff] [blame] | 425 | |
| 426 | mInfo.state = mIsDisplayOn ? Display.STATE_ON : Display.STATE_OFF; |
| 427 | |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 428 | mInfo.ownerUid = mOwnerUid; |
| 429 | mInfo.ownerPackageName = mOwnerPackageName; |
| 430 | } |
| 431 | return mInfo; |
| 432 | } |
| 433 | } |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 434 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 435 | private static class Callback extends Handler { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 436 | private static final int MSG_ON_DISPLAY_PAUSED = 0; |
| 437 | private static final int MSG_ON_DISPLAY_RESUMED = 1; |
| 438 | private static final int MSG_ON_DISPLAY_STOPPED = 2; |
| 439 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 440 | private final IVirtualDisplayCallback mCallback; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 441 | |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 442 | public Callback(IVirtualDisplayCallback callback, Handler handler) { |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 443 | super(handler.getLooper()); |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 444 | mCallback = callback; |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public void handleMessage(Message msg) { |
| 449 | try { |
| 450 | switch (msg.what) { |
| 451 | case MSG_ON_DISPLAY_PAUSED: |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 452 | mCallback.onPaused(); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 453 | break; |
| 454 | case MSG_ON_DISPLAY_RESUMED: |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 455 | mCallback.onResumed(); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 456 | break; |
| 457 | case MSG_ON_DISPLAY_STOPPED: |
Michael Wright | 75ee9fc | 2014-09-01 19:55:22 -0700 | [diff] [blame] | 458 | mCallback.onStopped(); |
Michael Wright | c39d47a | 2014-07-08 18:07:36 -0700 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | } catch (RemoteException e) { |
| 462 | Slog.w(TAG, "Failed to notify listener of virtual display event.", e); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | public void dispatchDisplayPaused() { |
| 467 | sendEmptyMessage(MSG_ON_DISPLAY_PAUSED); |
| 468 | } |
| 469 | |
| 470 | public void dispatchDisplayResumed() { |
| 471 | sendEmptyMessage(MSG_ON_DISPLAY_RESUMED); |
| 472 | } |
| 473 | |
| 474 | public void dispatchDisplayStopped() { |
| 475 | sendEmptyMessage(MSG_ON_DISPLAY_STOPPED); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | private final class MediaProjectionCallback extends IMediaProjectionCallback.Stub { |
| 480 | private IBinder mAppToken; |
| 481 | public MediaProjectionCallback(IBinder appToken) { |
| 482 | mAppToken = appToken; |
| 483 | } |
| 484 | |
| 485 | @Override |
| 486 | public void onStop() { |
| 487 | synchronized (getSyncRoot()) { |
| 488 | handleMediaProjectionStoppedLocked(mAppToken); |
| 489 | } |
| 490 | } |
| 491 | } |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 492 | |
| 493 | @VisibleForTesting |
| 494 | public interface SurfaceControlDisplayFactory { |
| 495 | public IBinder createDisplay(String name, boolean secure); |
| 496 | } |
Jeff Brown | a506a6e | 2013-06-04 00:02:38 -0700 | [diff] [blame] | 497 | } |