Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/androidoverlay/AndroidOverlayProviderImpl.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/androidoverlay/AndroidOverlayProviderImpl.java b/content/public/android/java/src/org/chromium/content/browser/androidoverlay/AndroidOverlayProviderImpl.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6a0e4f3cda863f0b6c522d6d371bd20a1cf8ed1c |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/androidoverlay/AndroidOverlayProviderImpl.java |
| @@ -0,0 +1,120 @@ |
| +// Copyright 2017 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.content.browser.androidoverlay; |
| + |
| +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.media.IAndroidOverlay; |
| +import org.chromium.media.IAndroidOverlayCallback; |
| +import org.chromium.media.IAndroidOverlayProvider; |
| + |
| +/** |
| + * Singleton class to create and manage AndroidOverlay. 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 IAndroidOverlayCallback. For native, AndroidOverlayCallback hides |
| + * the threading issues and provides the java peer. |
| + * - Get an IAndroidOverlayManager from the browser via ChildProcessCallback . |
| + * - dialogSurface = IAndroidOverlayManager.createOverlay(callback) |
| + * This calls into the browser, and returns an IAndroidOverlay IBinder. |
| + * - Wrap the AndroidOverlay in a AndroidOverlayWrapper. 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 IAndroidOverlay::release() to destroy the surface when done. |
| + */ |
| +public class AndroidOverlayProviderImpl extends IAndroidOverlayProvider.Stub { |
| + private static final String TAG = "AOProviderImpl"; |
| + private static AndroidOverlayProviderImpl sInstance; |
| + private static final Object sLock = new Object(); |
| + |
| + private final Context mContext; |
| + |
| + // We maintain a thread with a Looper for the AndroidOverlays 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. We don't want to use |
| + // the UI thread, since it will process synchronous callbacks from Android. |
| + private final Object mLock = new Object(); |
| + private HandlerThread mThread; |
| + private Handler mHandler; |
| + |
| + // Number of AndroidOverlays that have been created but not released. |
| + private int mNumOverlays; |
| + |
| + // Maximum number of concurrent surfaces we allow. |
| + private static final int MAX_SURFACES = 1; |
| + |
| + private AndroidOverlayProviderImpl() { |
| + mContext = ContextUtils.getApplicationContext(); |
| + mNumOverlays = 0; |
| + } |
| + |
| + // Return the singleton instance, as a convenience. |
| + public static IBinder getInstance() { |
| + synchronized (sLock) { |
| + if (sInstance == null) sInstance = new AndroidOverlayProviderImpl(); |
| + |
| + return sInstance.asBinder(); |
| + } |
| + } |
| + |
| + @Override |
| + @CalledByNative |
|
boliu
2017/02/08 00:01:58
not needed, this doesn't generate jni headers
|
| + public IAndroidOverlay createOverlay(int rendererPid, int renderFrameId, |
| + IAndroidOverlayCallback 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 (mNumOverlays >= MAX_SURFACES) return null; |
| + |
| + startThreadIfNeededLocked(); |
| + |
| + mNumOverlays++; |
| + } |
| + |
| + return new DialogAndroidOverlay(rendererPid, renderFrameId, mContext, this, mHandler, |
| + callback, x, y, width, height); |
| + } |
| + |
| + /** |
| + * Called by AndroidOverlays when they no longer need the thread. |
| + * Called on some random thread. |
| + */ |
| + public void notifyReleased() { |
| + synchronized (mLock) { |
| + assert mNumOverlays > 0; |
| + mNumOverlays--; |
| + |
| + // 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. |
| + // AndroidOverlay 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. |
|
boliu
2017/02/08 00:01:58
synhronized blocks are re-entrant safe, so rather
|
| + if (mThread != null) return; |
| + |
| + mThread = new HandlerThread("AndroidOverlayThread"); |
| + mThread.start(); |
| + mHandler = new Handler(mThread.getLooper()); |
| + } |
| +} |