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