Index: media/base/android/java/src/org/chromium/media/DialogSurfaceManager.java |
diff --git a/media/base/android/java/src/org/chromium/media/DialogSurfaceManager.java b/media/base/android/java/src/org/chromium/media/DialogSurfaceManager.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3ca75e8892f812b843815291ae384ba3fc3a841f |
--- /dev/null |
+++ b/media/base/android/java/src/org/chromium/media/DialogSurfaceManager.java |
@@ -0,0 +1,110 @@ |
+// 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 DialogSurfaceControllers. 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 . |
+ * - controller = IDialogSurfaceManager.createSurface(callback) |
+ * This calls into the browser, and returns an IDialogController IBinder. |
+ * - Wrap the controller in a DialogSurfaceControllerWrapper. 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 IDialogSurfaceController::release() to destroy the surface when done. |
+ */ |
+@JNINamespace("media") |
+public class DialogSurfaceManager extends IDialogSurfaceManager.Stub { |
+ private static final String TAG = "cr_media"; |
+ private static DialogSurfaceManager sInstance; |
+ |
+ private final Context mContext; |
+ |
+ // We maintain a thread with a Looper for the DialogSurfaceControllers 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. |
+ private HandlerThread mThread; |
+ private Handler mHandler; |
+ |
+ // Number of SurfaceControllers that have been created but not released. |
+ private int mNumSurfaces; |
+ |
+ // Maximum number of concurrent surfaces we allow. |
+ private static int sMaxSurfaces = 1; |
watk
2016/06/10 21:46:38
final
liberato (no reviews please)
2016/06/10 22:50:05
Done.
|
+ |
+ private DialogSurfaceManager() { |
+ mContext = ContextUtils.getApplicationContext(); |
+ mNumSurfaces = 0; |
+ } |
+ |
+ public static IBinder instance() { |
+ if (sInstance == null) sInstance = new DialogSurfaceManager(); |
+ |
+ return sInstance.asBinder(); |
+ } |
+ |
+ @Override |
+ @CalledByNative |
+ public IDialogSurfaceController createSurface( |
+ IDialogSurfaceCallback callback, int x, int y, int width, int height) { |
+ // Limit the number of concurrent surfaces. |
+ if (mNumSurfaces >= sMaxSurfaces) return null; |
+ |
+ ensureThread(); |
+ |
+ mNumSurfaces++; |
+ return new DialogSurfaceController(mContext, this, mHandler, callback, x, y, width, height); |
+ } |
+ |
+ /** |
+ * Called by controllers when they no longer need the thread. |
+ */ |
+ public void notifyReleased(DialogSurfaceController controller) { |
+ if (mNumSurfaces > 0) mNumSurfaces--; |
+ |
+ // 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. |
+ // DialogSurfaceController 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. |
+ /* |
+ if (mNumSurfaces == 0) { |
+ mThread.quitSafely(); |
+ mThread = null; |
+ mHandler = null; |
+ } |
+ */ |
+ } |
+ |
+ /** |
+ * Make sure that mThread and mHandler are ready for use. |
+ */ |
+ private void ensureThread() { |
+ // Called on main thread only. |
+ if (mThread != null) return; |
+ |
+ mThread = new HandlerThread("DialogSurfaceThread"); |
+ mThread.start(); |
+ mHandler = new Handler(mThread.getLooper()); |
+ } |
+} |