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