Index: media/base/android/java/src/org/chromium/media/DialogSurfaceManagerImpl.java |
diff --git a/media/base/android/java/src/org/chromium/media/DialogSurfaceManagerImpl.java b/media/base/android/java/src/org/chromium/media/DialogSurfaceManagerImpl.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9252b0ec147482ff829dfdca4e144df471586580 |
--- /dev/null |
+++ b/media/base/android/java/src/org/chromium/media/DialogSurfaceManagerImpl.java |
@@ -0,0 +1,130 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.media; |
+ |
+import android.content.Context; |
+import android.os.Handler; |
+import android.os.HandlerThread; |
+import android.os.IBinder; |
+ |
+import org.chromium.base.ContextUtils; |
+import org.chromium.base.annotations.CalledByNative; |
+import org.chromium.base.annotations.JNINamespace; |
+ |
+/** |
+ * Singleton class to create and manage DialogSurface. Normally, you won't need |
+ * to access this directly. Instead, see the native wrappers in |
+ * dialog_surface_manager.cc for usage. |
+ * |
+ * The wrappers do roughly the following on the gpu side: |
+ * - Create an IDialogSurfaceCallback. For native, DialogSurfaceCallback hides |
+ * the threading issues and provides the java peer. |
+ * - Get an IDialogSurfaceManager from the browser via ChildProcessCallback . |
+ * - dialogSurface = IDialogSurfaceManager.createSurface(callback) |
+ * This calls into the browser, and returns an IDialogSurface IBinder. |
+ * - Wrap the DialogSurface in a DialogSurfaceWrapper. This provides the JNI |
+ * bindings for native. |
+ * - Expect callbacks on the callback to let you know when the underlying |
+ * android surface state changes. |
+ * - Use the surface. |
+ * - Call IDialogSurface::release() to destroy the surface when done. |
+ */ |
+@JNINamespace("media") |
+public class DialogSurfaceManagerImpl extends IDialogSurfaceManager.Stub { |
+ private static final String TAG = "cr_media"; |
+ private static DialogSurfaceManagerImpl sInstance; |
+ private static final Object sLock = new Object(); |
+ |
+ private final Context mContext; |
+ |
+ // We maintain a thread with a Looper for the DialogSurfaces to use, |
+ // since Dialog requires one. We don't want this to be the native thread |
+ // that's used to create them, because it probably also has a native message |
+ // loop. Including a looper on it is not a good idea. |
boliu
2017/01/04 23:14:39
the browser UI thread does have a java looper, so
liberato (no reviews please)
2017/01/11 22:17:57
i want to avoid that so that synchronous callbacks
boliu
2017/01/12 20:24:19
Ahh, I was curious what surface view implement, bu
liberato (no reviews please)
2017/02/03 21:28:32
i'll have to dig up where it does this, but CVV do
|
+ private final Object mLock = new Object(); |
+ private HandlerThread mThread; |
+ private Handler mHandler; |
+ // TODO(liberato): why does this have to be a binder interface? i suppose |
+ // it has to live in the browser process. however, it's a lot more |
+ // convenient if we don't need a binder object for it to call back on. |
+ // maybe we should just make it a normal interface with inner client. |
+ private final DialogSurfaceWindowTokenProvider mTokenProvider; |
+ |
+ // Number of DialogSurfaces that have been created but not released. |
+ private int mNumSurfaces; |
+ |
+ // Maximum number of concurrent surfaces we allow. |
+ private static final int MAX_SURFACES = 1; |
+ |
+ private DialogSurfaceManagerImpl(DialogSurfaceWindowTokenProvider tokenProvider) { |
+ mContext = ContextUtils.getApplicationContext(); |
+ mTokenProvider = tokenProvider; |
+ mNumSurfaces = 0; |
+ } |
+ |
+ // Return the singleton instance, as a convenience. |
+ public static IBinder instance(DialogSurfaceWindowTokenProvider tokenProvider) { |
+ synchronized (sLock) { |
+ if (sInstance == null) sInstance = new DialogSurfaceManagerImpl(tokenProvider); |
+ |
+ return sInstance.asBinder(); |
+ } |
+ } |
+ |
+ @Override |
+ @CalledByNative |
boliu
2017/01/04 01:48:44
this class doesn't generate jni code?
liberato (no reviews please)
2017/01/11 22:17:57
whoops. thanks.
|
+ public IDialogSurface createSurface(int rendererPid, int renderFrameId, |
+ IDialogSurfaceCallback callback, int x, int y, int width, int height) { |
+ // Note that we may be called on any random binder thread, so lock. |
+ synchronized (mLock) { |
+ // Limit the number of concurrent surfaces. |
+ if (mNumSurfaces >= MAX_SURFACES) return null; |
+ |
+ startThreadIfNeededLocked(); |
+ |
+ mNumSurfaces++; |
+ } |
+ |
+ return new DialogSurfaceImpl(rendererPid, renderFrameId, mContext, this, mHandler, callback, |
+ x, y, width, height); |
+ } |
+ |
+ /** |
+ * Called by DialogSurfaces when they no longer need the thread. |
+ * Called on some random thread. |
+ */ |
+ public void notifyReleased() { |
+ synchronized (mLock) { |
+ if (mNumSurfaces > 0) mNumSurfaces--; |
boliu
2017/01/04 01:48:44
this should be an assert? (which works as DCHECKs
liberato (no reviews please)
2017/01/11 22:17:57
Done.
|
+ |
+ // We don't stop the looper thread here, else android can get mad when |
+ // it tries to send a message from the dialog on this thread. |
+ // DialogSurface might have to notify us separately to tell us |
+ // when it's done with the thread, if we don't want to wait until then |
+ // to start creating a new SV. |
+ // Instead, we just avoid shutting down the thread at all for now. |
+ } |
+ } |
+ |
+ /** |
+ * Make sure that mThread and mHandler are ready for use. Do nothing if |
+ * they already are. |
+ */ |
+ private void startThreadIfNeededLocked() { |
+ // Called with mLock held only. |
+ if (mThread != null) return; |
+ |
+ mThread = new HandlerThread("DialogSurfaceThread"); |
+ mThread.start(); |
+ mHandler = new Handler(mThread.getLooper()); |
+ } |
+ |
+ /** |
+ * Return the window token provider. |
+ */ |
+ public DialogSurfaceWindowTokenProvider getWindowTokenProvider() { |
+ return mTokenProvider; |
+ } |
+} |