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