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 // TODO(liberato): would get the WindowToken / Activity here. |
| 70 |
| 71 startThreadIfNeeded(); |
| 72 |
| 73 mNumSurfaces++; |
| 74 |
| 75 // TODO(liberato): would instantiate DialogSurface here. |
| 76 return null; |
| 77 } |
| 78 |
| 79 /** |
| 80 * Called by DialogSurfaces when they no longer need the thread. |
| 81 */ |
| 82 public void notifyReleased() { |
| 83 if (mNumSurfaces > 0) mNumSurfaces--; |
| 84 |
| 85 // We don't stop the looper thread here, else android can get mad when |
| 86 // it tries to send a message from the dialog on this thread. |
| 87 // DialogSurface might have to notify us separately to tell us |
| 88 // when it's done with the thread, if we don't want to wait until then |
| 89 // to start creating a new SV. |
| 90 // Instead, we just avoid shutting down the thread at all for now. |
| 91 } |
| 92 |
| 93 /** |
| 94 * Make sure that mThread and mHandler are ready for use. Do nothing if |
| 95 * they already are. |
| 96 */ |
| 97 private void startThreadIfNeeded() { |
| 98 // Called on main thread only. |
| 99 if (mThread != null) return; |
| 100 |
| 101 // Just to avoid "Unread field" in findbugs. Normally, we wills send |
| 102 // mHandler to DialogSurface. |
| 103 if (mHandler != null) return; |
| 104 |
| 105 mThread = new HandlerThread("DialogSurfaceThread"); |
| 106 mThread.start(); |
| 107 mHandler = new Handler(mThread.getLooper()); |
| 108 } |
| 109 |
| 110 /** |
| 111 * Return the activity mapper. |
| 112 */ |
| 113 public IDialogSurfaceActivityMapper getMapper() { |
| 114 return mMapper; |
| 115 } |
| 116 } |
OLD | NEW |