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; |
| 20 | import android.os.Handler; |
| 21 | import android.os.IBinder; |
| 22 | import android.os.IBinder.DeathRecipient; |
| 23 | import android.os.RemoteException; |
| 24 | import android.util.ArrayMap; |
| 25 | import android.util.Slog; |
| 26 | import android.view.Display; |
| 27 | import android.view.Surface; |
| 28 | import android.view.SurfaceControl; |
| 29 | |
| 30 | /** |
| 31 | * A display adapter that provides virtual displays on behalf of applications. |
| 32 | * <p> |
| 33 | * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock. |
| 34 | * </p> |
| 35 | */ |
| 36 | final class VirtualDisplayAdapter extends DisplayAdapter { |
| 37 | static final String TAG = "VirtualDisplayAdapter"; |
| 38 | static final boolean DEBUG = false; |
| 39 | |
| 40 | private final ArrayMap<IBinder, VirtualDisplayDevice> mVirtualDisplayDevices = |
| 41 | new ArrayMap<IBinder, VirtualDisplayDevice>(); |
| 42 | |
| 43 | // Called with SyncRoot lock held. |
| 44 | public VirtualDisplayAdapter(DisplayManagerService.SyncRoot syncRoot, |
| 45 | Context context, Handler handler, Listener listener) { |
| 46 | super(syncRoot, context, handler, listener, TAG); |
| 47 | } |
| 48 | |
| 49 | public DisplayDevice createPrivateVirtualDisplayLocked(IBinder appToken, |
| 50 | int ownerUid, String ownerPackageName, |
| 51 | String name, int width, int height, int densityDpi, Surface surface) { |
| 52 | IBinder displayToken = SurfaceControl.createDisplay(name, false /*secure*/); |
| 53 | VirtualDisplayDevice device = new VirtualDisplayDevice(displayToken, appToken, |
| 54 | ownerUid, ownerPackageName, name, width, height, densityDpi, surface); |
| 55 | |
| 56 | try { |
| 57 | appToken.linkToDeath(device, 0); |
| 58 | } catch (RemoteException ex) { |
| 59 | device.releaseLocked(); |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | mVirtualDisplayDevices.put(appToken, device); |
| 64 | |
| 65 | // Return the display device without actually sending the event indicating |
| 66 | // that it was added. The caller will handle it. |
| 67 | return device; |
| 68 | } |
| 69 | |
| 70 | public DisplayDevice releaseVirtualDisplayLocked(IBinder appToken) { |
| 71 | VirtualDisplayDevice device = mVirtualDisplayDevices.remove(appToken); |
| 72 | if (device != null) { |
| 73 | appToken.unlinkToDeath(device, 0); |
| 74 | } |
| 75 | |
| 76 | // Return the display device that was removed without actually sending the |
| 77 | // event indicating that it was removed. The caller will handle it. |
| 78 | return device; |
| 79 | } |
| 80 | |
| 81 | private void handleBinderDiedLocked(IBinder appToken) { |
| 82 | VirtualDisplayDevice device = mVirtualDisplayDevices.remove(appToken); |
| 83 | if (device != null) { |
| 84 | Slog.i(TAG, "Virtual display device released because application token died: " |
| 85 | + device.mOwnerPackageName); |
| 86 | sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_REMOVED); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private final class VirtualDisplayDevice extends DisplayDevice |
| 91 | implements DeathRecipient { |
| 92 | private final IBinder mAppToken; |
| 93 | private final int mOwnerUid; |
| 94 | final String mOwnerPackageName; |
| 95 | private final String mName; |
| 96 | private final int mWidth; |
| 97 | private final int mHeight; |
| 98 | private final int mDensityDpi; |
| 99 | |
| 100 | private boolean mReleased; |
| 101 | private Surface mSurface; |
| 102 | private DisplayDeviceInfo mInfo; |
| 103 | |
| 104 | public VirtualDisplayDevice(IBinder displayToken, |
| 105 | IBinder appToken, int ownerUid, String ownerPackageName, |
| 106 | String name, int width, int height, int densityDpi, Surface surface) { |
| 107 | super(VirtualDisplayAdapter.this, displayToken); |
| 108 | mAppToken = appToken; |
| 109 | mOwnerUid = ownerUid; |
| 110 | mOwnerPackageName = ownerPackageName; |
| 111 | mName = name; |
| 112 | mWidth = width; |
| 113 | mHeight = height; |
| 114 | mDensityDpi = densityDpi; |
| 115 | mSurface = surface; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void binderDied() { |
| 120 | synchronized (getSyncRoot()) { |
| 121 | if (!mReleased) { |
| 122 | handleBinderDiedLocked(mAppToken); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | public void releaseLocked() { |
| 128 | mReleased = true; |
| 129 | sendTraversalRequestLocked(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void performTraversalInTransactionLocked() { |
| 134 | if (mReleased && mSurface != null) { |
| 135 | mSurface.destroy(); |
| 136 | mSurface = null; |
| 137 | } |
| 138 | setSurfaceInTransactionLocked(mSurface); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public DisplayDeviceInfo getDisplayDeviceInfoLocked() { |
| 143 | if (mInfo == null) { |
| 144 | mInfo = new DisplayDeviceInfo(); |
| 145 | mInfo.name = mName; |
| 146 | mInfo.width = mWidth; |
| 147 | mInfo.height = mHeight; |
| 148 | mInfo.refreshRate = 60; |
| 149 | mInfo.densityDpi = mDensityDpi; |
| 150 | mInfo.xDpi = mDensityDpi; |
| 151 | mInfo.yDpi = mDensityDpi; |
| 152 | mInfo.flags = DisplayDeviceInfo.FLAG_PRIVATE | DisplayDeviceInfo.FLAG_NEVER_BLANK; |
| 153 | mInfo.type = Display.TYPE_VIRTUAL; |
| 154 | mInfo.touch = DisplayDeviceInfo.TOUCH_NONE; |
| 155 | mInfo.ownerUid = mOwnerUid; |
| 156 | mInfo.ownerPackageName = mOwnerPackageName; |
| 157 | } |
| 158 | return mInfo; |
| 159 | } |
| 160 | } |
| 161 | } |