OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.media; | |
6 | |
7 import android.os.Handler; | |
8 import android.os.HandlerThread; | |
9 import android.os.IBinder; | |
10 | |
11 import org.chromium.base.annotations.CalledByNative; | |
12 import org.chromium.base.annotations.JNINamespace; | |
13 | |
14 /** | |
15 * Singleton class to create and manage DialogSurface. Normally, you won't need | |
16 * to access this directly. Instead, see the native wrappers in | |
17 * dialog_surface_manager.cc for usage. | |
18 * | |
19 * The wrappers do roughly the following on the gpu side: | |
20 * - Create an IDialogSurfaceCallback. For native, DialogSurfaceCallback hides | |
21 * the threading issues and provides the java peer. | |
22 * - Get an IDialogSurfaceManager from the browser via ChildProcessCallback . | |
23 * - dialogSurface = IDialogSurfaceManager.createSurface(callback) | |
24 * This calls into the browser, and returns an IDialogSurface IBinder. | |
25 * - Wrap the DialogSurface in a DialogSurfaceWrapper. This provides the JNI | |
26 * bindings for native. | |
27 * - Expect callbacks on the callback to let you know when the underlying | |
28 * android surface state changes. | |
29 * - Use the surface. | |
30 * - Call IDialogSurface::release() to destroy the surface when done. | |
31 */ | |
32 @JNINamespace("media") | |
33 public class DialogSurfaceManager extends IDialogSurfaceManager.Stub { | |
34 private static final String TAG = "cr_media"; | |
35 private static DialogSurfaceManager sInstance; | |
36 | |
37 // We maintain a thread with a Looper for the DialogSurfaces to use, | |
38 // since Dialog requires one. We don't want this to be the native thread | |
39 // that's used to create them, because it probably also has a native message | |
40 // loop. Including a looper on it is not a good idea. | |
41 private HandlerThread mThread; | |
42 private Handler mHandler; | |
43 private IDialogSurfaceActivityMapper mMapper; | |
44 | |
45 // Number of DialogSurfaces that have been created but not released. | |
46 private int mNumSurfaces; | |
47 | |
48 // Maximum number of concurrent surfaces we allow. | |
49 private static final int sMaxSurfaces = 1; | |
50 | |
51 private DialogSurfaceManager(IDialogSurfaceActivityMapper mapper) { | |
52 mMapper = mapper; | |
53 mNumSurfaces = 0; | |
54 } | |
55 | |
56 public static IBinder instance(IDialogSurfaceActivityMapper mapper) { | |
57 if (sInstance == null) sInstance = new DialogSurfaceManager(mapper); | |
58 | |
59 return sInstance.asBinder(); | |
60 } | |
61 | |
62 @Override | |
63 @CalledByNative | |
64 public IDialogSurface createSurface(int rendererPid, int renderFrameId, | |
65 IDialogSurfaceCallback callback, int x, int y, int width, int height ) { | |
66 // Limit the number of concurrent surfaces. | |
67 if (mNumSurfaces >= sMaxSurfaces) return null; | |
68 | |
69 ensureThread(); | |
70 | |
71 mNumSurfaces++; | |
72 | |
73 // TODO(liberato): would instantiate DialogSurface here. | |
74 return null; | |
75 } | |
76 | |
77 /** | |
78 * Called by DialogSurfaces when they no longer need the thread. | |
79 */ | |
80 public void notifyReleased() { | |
81 if (mNumSurfaces > 0) mNumSurfaces--; | |
82 | |
83 // We don't stop the looper thread here, else android can get mad when | |
84 // it tries to send a message from the dialog on this thread. | |
85 // DialogSurface might have to notify us separately to tell us | |
86 // when it's done with the thread, if we don't want to wait until then | |
87 // to start creating a new SV. | |
88 // Instead, we just avoid shutting down the thread at all for now. | |
89 } | |
90 | |
91 /** | |
92 * Make sure that mThread and mHandler are ready for use. | |
93 */ | |
94 private void ensureThread() { | |
DaleCurtis
2016/07/27 18:14:20
Needs a better name.
liberato (no reviews please)
2016/07/28 15:33:30
Done.
| |
95 // Called on main thread only. | |
96 if (mThread != null) return; | |
97 | |
98 // Just to avoid "Unread field" in findbugs. Normally, we wills send | |
99 // mHandler to DialogSurface. | |
100 if (mHandler != null) return; | |
101 | |
102 mThread = new HandlerThread("DialogSurfaceThread"); | |
103 mThread.start(); | |
104 mHandler = new Handler(mThread.getLooper()); | |
105 } | |
106 | |
107 /** | |
108 * Return the activity mapper. | |
109 */ | |
110 public IDialogSurfaceActivityMapper getMapper() { | |
111 return mMapper; | |
112 } | |
113 } | |
OLD | NEW |