VirtualDisplayAdapter.java revision cde5bb45cc86d181d96ee69da1832e6132162871
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
17package com.android.server.display;
18
19import android.content.Context;
20import android.hardware.display.DisplayManager;
21import android.hardware.display.IVirtualDisplayCallback;
22import android.media.projection.IMediaProjection;
23import android.media.projection.IMediaProjectionCallback;
24import android.os.Handler;
25import android.os.IBinder;
26import android.os.IBinder.DeathRecipient;
27import android.os.Message;
28import android.os.RemoteException;
29import android.util.ArrayMap;
30import android.util.Slog;
31import android.view.Display;
32import android.view.Surface;
33import android.view.SurfaceControl;
34
35import java.io.PrintWriter;
36
37/**
38 * A display adapter that provides virtual displays on behalf of applications.
39 * <p>
40 * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock.
41 * </p>
42 */
43final class VirtualDisplayAdapter extends DisplayAdapter {
44    static final String TAG = "VirtualDisplayAdapter";
45    static final boolean DEBUG = false;
46
47    private final ArrayMap<IBinder, VirtualDisplayDevice> mVirtualDisplayDevices =
48            new ArrayMap<IBinder, VirtualDisplayDevice>();
49    private Handler mHandler;
50
51    // Called with SyncRoot lock held.
52    public VirtualDisplayAdapter(DisplayManagerService.SyncRoot syncRoot,
53            Context context, Handler handler, Listener listener) {
54        super(syncRoot, context, handler, listener, TAG);
55        mHandler = handler;
56    }
57
58    public DisplayDevice createVirtualDisplayLocked(IVirtualDisplayCallback callback,
59            IMediaProjection projection, int ownerUid, String ownerPackageName,
60            String name, int width, int height, int densityDpi, Surface surface, int flags) {
61        boolean secure = (flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE) != 0;
62        IBinder appToken = callback.asBinder();
63        IBinder displayToken = SurfaceControl.createDisplay(name, secure);
64        VirtualDisplayDevice device = new VirtualDisplayDevice(displayToken, appToken,
65                ownerUid, ownerPackageName, name, width, height, densityDpi, surface, flags,
66                new Callback(callback, mHandler));
67
68        mVirtualDisplayDevices.put(appToken, device);
69
70        try {
71            if (projection != null) {
72                projection.registerCallback(new MediaProjectionCallback(appToken));
73            }
74            appToken.linkToDeath(device, 0);
75        } catch (RemoteException ex) {
76            mVirtualDisplayDevices.remove(appToken);
77            device.destroyLocked();
78            return null;
79        }
80
81        // Return the display device without actually sending the event indicating
82        // that it was added.  The caller will handle it.
83        return device;
84    }
85
86    public void resizeVirtualDisplayLocked(IBinder appToken,
87            int width, int height, int densityDpi) {
88        VirtualDisplayDevice device = mVirtualDisplayDevices.get(appToken);
89        if (device != null) {
90            device.resizeLocked(width, height, densityDpi);
91        }
92    }
93
94
95    public void setVirtualDisplaySurfaceLocked(IBinder appToken, Surface surface) {
96        VirtualDisplayDevice device = mVirtualDisplayDevices.get(appToken);
97        if (device != null) {
98            device.setSurfaceLocked(surface);
99        }
100    }
101
102    public DisplayDevice releaseVirtualDisplayLocked(IBinder appToken) {
103        VirtualDisplayDevice device = mVirtualDisplayDevices.remove(appToken);
104        if (device != null) {
105            device.destroyLocked();
106            appToken.unlinkToDeath(device, 0);
107        }
108
109        // Return the display device that was removed without actually sending the
110        // event indicating that it was removed.  The caller will handle it.
111        return device;
112    }
113
114    private void handleBinderDiedLocked(IBinder appToken) {
115        VirtualDisplayDevice device = mVirtualDisplayDevices.remove(appToken);
116        if (device != null) {
117            Slog.i(TAG, "Virtual display device released because application token died: "
118                    + device.mOwnerPackageName);
119            device.destroyLocked();
120            sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_REMOVED);
121        }
122    }
123
124    private void handleMediaProjectionStoppedLocked(IBinder appToken) {
125        VirtualDisplayDevice device = mVirtualDisplayDevices.remove(appToken);
126        if (device != null) {
127            Slog.i(TAG, "Virtual display device released because media projection stopped: "
128                    + device.mName);
129            device.stopLocked();
130        }
131    }
132
133    private final class VirtualDisplayDevice extends DisplayDevice implements DeathRecipient {
134        private static final int PENDING_SURFACE_CHANGE = 0x01;
135        private static final int PENDING_RESIZE = 0x02;
136
137        private final IBinder mAppToken;
138        private final int mOwnerUid;
139        final String mOwnerPackageName;
140        final String mName;
141        private final int mFlags;
142        private final Callback mCallback;
143
144        private int mWidth;
145        private int mHeight;
146        private int mDensityDpi;
147        private Surface mSurface;
148        private DisplayDeviceInfo mInfo;
149        private int mDisplayState;
150        private boolean mStopped;
151        private int mPendingChanges;
152
153        public VirtualDisplayDevice(IBinder displayToken, IBinder appToken,
154                int ownerUid, String ownerPackageName,
155                String name, int width, int height, int densityDpi, Surface surface, int flags,
156                Callback callback) {
157            super(VirtualDisplayAdapter.this, displayToken);
158            mAppToken = appToken;
159            mOwnerUid = ownerUid;
160            mOwnerPackageName = ownerPackageName;
161            mName = name;
162            mWidth = width;
163            mHeight = height;
164            mDensityDpi = densityDpi;
165            mSurface = surface;
166            mFlags = flags;
167            mCallback = callback;
168            mDisplayState = Display.STATE_UNKNOWN;
169            mPendingChanges |= PENDING_SURFACE_CHANGE;
170        }
171
172        @Override
173        public void binderDied() {
174            synchronized (getSyncRoot()) {
175                if (mSurface != null) {
176                    handleBinderDiedLocked(mAppToken);
177                }
178            }
179        }
180
181        public void destroyLocked() {
182            if (mSurface != null) {
183                mSurface.release();
184                mSurface = null;
185            }
186            SurfaceControl.destroyDisplay(getDisplayTokenLocked());
187            mCallback.dispatchDisplayStopped();
188        }
189
190        @Override
191        public void requestDisplayStateLocked(int state) {
192            if (state != mDisplayState) {
193                mDisplayState = state;
194                if (state == Display.STATE_OFF) {
195                    mCallback.dispatchDisplayPaused();
196                } else {
197                    mCallback.dispatchDisplayResumed();
198                }
199            }
200        }
201
202        @Override
203        public void performTraversalInTransactionLocked() {
204            if ((mPendingChanges & PENDING_RESIZE) != 0) {
205                SurfaceControl.setDisplaySize(getDisplayTokenLocked(), mWidth, mHeight);
206            }
207            if ((mPendingChanges & PENDING_SURFACE_CHANGE) != 0) {
208                setSurfaceInTransactionLocked(mSurface);
209            }
210            mPendingChanges = 0;
211        }
212
213        public void setSurfaceLocked(Surface surface) {
214            if (!mStopped && mSurface != surface) {
215                if ((mSurface != null) != (surface != null)) {
216                    sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED);
217                }
218                sendTraversalRequestLocked();
219                mSurface = surface;
220                mInfo = null;
221                mPendingChanges |= PENDING_SURFACE_CHANGE;
222            }
223        }
224
225        public void resizeLocked(int width, int height, int densityDpi) {
226            if (mWidth != width || mHeight != height || mDensityDpi != densityDpi) {
227                sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED);
228                sendTraversalRequestLocked();
229                mWidth = width;
230                mHeight = height;
231                mDensityDpi = densityDpi;
232                mInfo = null;
233                mPendingChanges |= PENDING_RESIZE;
234            }
235        }
236
237        public void stopLocked() {
238            setSurfaceLocked(null);
239            mStopped = true;
240        }
241
242        @Override
243        public void dumpLocked(PrintWriter pw) {
244            super.dumpLocked(pw);
245            pw.println("mFlags=" + mFlags);
246            pw.println("mDisplayState=" + Display.stateToString(mDisplayState));
247            pw.println("mStopped=" + mStopped);
248        }
249
250
251        @Override
252        public DisplayDeviceInfo getDisplayDeviceInfoLocked() {
253            if (mInfo == null) {
254                mInfo = new DisplayDeviceInfo();
255                mInfo.name = mName;
256                mInfo.width = mWidth;
257                mInfo.height = mHeight;
258                mInfo.refreshRate = 60;
259                mInfo.densityDpi = mDensityDpi;
260                mInfo.xDpi = mDensityDpi;
261                mInfo.yDpi = mDensityDpi;
262                mInfo.presentationDeadlineNanos = 1000000000L / (int) mInfo.refreshRate; // 1 frame
263                mInfo.flags = 0;
264                if ((mFlags & DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC) == 0) {
265                    mInfo.flags |= DisplayDeviceInfo.FLAG_PRIVATE
266                            | DisplayDeviceInfo.FLAG_NEVER_BLANK;
267                }
268                if ((mFlags & DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
269                    mInfo.flags &= ~DisplayDeviceInfo.FLAG_NEVER_BLANK;
270                } else {
271                    mInfo.flags |= DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY;
272                }
273
274                if ((mFlags & DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
275                    mInfo.flags |= DisplayDeviceInfo.FLAG_SECURE;
276                }
277                if ((mFlags & DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION) != 0) {
278                    mInfo.flags |= DisplayDeviceInfo.FLAG_PRESENTATION;
279                }
280                mInfo.type = Display.TYPE_VIRTUAL;
281                mInfo.touch = DisplayDeviceInfo.TOUCH_NONE;
282                mInfo.state = mSurface != null ? Display.STATE_ON : Display.STATE_OFF;
283                mInfo.ownerUid = mOwnerUid;
284                mInfo.ownerPackageName = mOwnerPackageName;
285            }
286            return mInfo;
287        }
288    }
289
290    private static class Callback extends Handler {
291        private static final int MSG_ON_DISPLAY_PAUSED = 0;
292        private static final int MSG_ON_DISPLAY_RESUMED = 1;
293        private static final int MSG_ON_DISPLAY_STOPPED = 2;
294
295        private final IVirtualDisplayCallback mCallback;
296
297        public Callback(IVirtualDisplayCallback callback, Handler handler) {
298            super(handler.getLooper());
299            mCallback = callback;
300        }
301
302        @Override
303        public void handleMessage(Message msg) {
304            try {
305                switch (msg.what) {
306                    case MSG_ON_DISPLAY_PAUSED:
307                        mCallback.onPaused();
308                        break;
309                    case MSG_ON_DISPLAY_RESUMED:
310                        mCallback.onResumed();
311                        break;
312                    case MSG_ON_DISPLAY_STOPPED:
313                        mCallback.onStopped();
314                        break;
315                }
316            } catch (RemoteException e) {
317                Slog.w(TAG, "Failed to notify listener of virtual display event.", e);
318            }
319        }
320
321        public void dispatchDisplayPaused() {
322            sendEmptyMessage(MSG_ON_DISPLAY_PAUSED);
323        }
324
325        public void dispatchDisplayResumed() {
326            sendEmptyMessage(MSG_ON_DISPLAY_RESUMED);
327        }
328
329        public void dispatchDisplayStopped() {
330            sendEmptyMessage(MSG_ON_DISPLAY_STOPPED);
331        }
332    }
333
334    private final class MediaProjectionCallback extends IMediaProjectionCallback.Stub {
335        private IBinder mAppToken;
336        public MediaProjectionCallback(IBinder appToken) {
337            mAppToken = appToken;
338        }
339
340        @Override
341        public void onStop() {
342            synchronized (getSyncRoot()) {
343                handleMediaProjectionStoppedLocked(mAppToken);
344            }
345        }
346    }
347}
348