| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package com.android.webview.chromium; | 5 package com.android.webview.chromium; |
| 6 | 6 |
| 7 import android.graphics.Canvas; | 7 import android.graphics.Canvas; |
| 8 import android.os.Build; | 8 import android.os.Build; |
| 9 import android.view.View; | 9 import android.view.View; |
| 10 | 10 |
| 11 import com.android.webview.chromium.WebViewDelegateFactory.WebViewDelegate; | 11 import com.android.webview.chromium.WebViewDelegateFactory.WebViewDelegate; |
| 12 | 12 |
| 13 import org.chromium.android_webview.AwContents; | 13 import org.chromium.android_webview.AwContents; |
| 14 import org.chromium.content.common.CleanupReference; | |
| 15 | 14 |
| 16 /** | 15 /** |
| 17 * Simple Java abstraction and wrapper for the native DrawGLFunctor flow. | 16 * Simple Java abstraction and wrapper for the native DrawGLFunctor flow. |
| 18 * An instance of this class can be constructed, bound to a single view context
(i.e. AwContennts) | 17 * An instance of this class can be constructed, bound to a single view context
(i.e. AwContennts) |
| 19 * and then drawn and detached from the view tree any number of times (using req
uestDrawGL and | 18 * and then drawn and detached from the view tree any number of times (using req
uestDrawGL and |
| 20 * detach respectively). | 19 * detach respectively). |
| 21 */ | 20 */ |
| 22 class DrawGLFunctor implements AwContents.NativeDrawGLFunctor { | 21 class DrawGLFunctor implements AwContents.NativeDrawGLFunctor { |
| 23 private static final String TAG = DrawGLFunctor.class.getSimpleName(); | 22 private static final String TAG = DrawGLFunctor.class.getSimpleName(); |
| 24 | 23 |
| 25 // Pointer to native side instance | 24 // Pointer to native side instance |
| 26 private final CleanupReference mCleanupReference; | |
| 27 private final DestroyRunnable mDestroyRunnable; | 25 private final DestroyRunnable mDestroyRunnable; |
| 28 private final WebViewDelegate mWebViewDelegate; | 26 private final WebViewDelegate mWebViewDelegate; |
| 29 | 27 |
| 30 public DrawGLFunctor(long viewContext, WebViewDelegate webViewDelegate) { | 28 public DrawGLFunctor(long viewContext, WebViewDelegate webViewDelegate) { |
| 31 mDestroyRunnable = new DestroyRunnable(nativeCreateGLFunctor(viewContext
)); | 29 mDestroyRunnable = new DestroyRunnable(nativeCreateGLFunctor(viewContext
)); |
| 32 mCleanupReference = new CleanupReference(this, mDestroyRunnable); | |
| 33 mWebViewDelegate = webViewDelegate; | 30 mWebViewDelegate = webViewDelegate; |
| 34 } | 31 } |
| 35 | 32 |
| 36 @Override | 33 @Override |
| 37 public void detach(View containerView) { | 34 public void detach(View containerView) { |
| 35 if (mDestroyRunnable.mNativeDrawGLFunctor == 0) { |
| 36 throw new RuntimeException("detach on already destroyed DrawGLFuncto
r"); |
| 37 } |
| 38 mWebViewDelegate.detachDrawGlFunctor(containerView, mDestroyRunnable.mNa
tiveDrawGLFunctor); | 38 mWebViewDelegate.detachDrawGlFunctor(containerView, mDestroyRunnable.mNa
tiveDrawGLFunctor); |
| 39 } | 39 } |
| 40 | 40 |
| 41 private static final boolean sSupportFunctorReleasedCallback = | 41 private static final boolean sSupportFunctorReleasedCallback = |
| 42 (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) || "N".equals(Build.
VERSION.CODENAME); | 42 (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) || "N".equals(Build.
VERSION.CODENAME); |
| 43 | 43 |
| 44 @Override | 44 @Override |
| 45 public boolean requestDrawGL(Canvas canvas, Runnable releasedCallback) { | 45 public boolean requestDrawGL(Canvas canvas, Runnable releasedCallback) { |
| 46 if (mDestroyRunnable.mNativeDrawGLFunctor == 0) { |
| 47 throw new RuntimeException("requestDrawGL on already destroyed DrawG
LFunctor"); |
| 48 } |
| 46 assert canvas != null; | 49 assert canvas != null; |
| 47 if (sSupportFunctorReleasedCallback) { | 50 if (sSupportFunctorReleasedCallback) { |
| 48 assert releasedCallback != null; | 51 assert releasedCallback != null; |
| 49 mWebViewDelegate.callDrawGlFunction( | 52 mWebViewDelegate.callDrawGlFunction( |
| 50 canvas, mDestroyRunnable.mNativeDrawGLFunctor, releasedCallb
ack); | 53 canvas, mDestroyRunnable.mNativeDrawGLFunctor, releasedCallb
ack); |
| 51 } else { | 54 } else { |
| 52 assert releasedCallback == null; | 55 assert releasedCallback == null; |
| 53 mWebViewDelegate.callDrawGlFunction(canvas, mDestroyRunnable.mNative
DrawGLFunctor); | 56 mWebViewDelegate.callDrawGlFunction(canvas, mDestroyRunnable.mNative
DrawGLFunctor); |
| 54 } | 57 } |
| 55 return true; | 58 return true; |
| 56 } | 59 } |
| 57 | 60 |
| 58 @Override | 61 @Override |
| 59 public boolean requestInvokeGL(View containerView, boolean waitForCompletion
) { | 62 public boolean requestInvokeGL(View containerView, boolean waitForCompletion
) { |
| 63 if (mDestroyRunnable.mNativeDrawGLFunctor == 0) { |
| 64 throw new RuntimeException("requestInvokeGL on already destroyed Dra
wGLFunctor"); |
| 65 } |
| 60 if (!sSupportFunctorReleasedCallback | 66 if (!sSupportFunctorReleasedCallback |
| 61 && !mWebViewDelegate.canInvokeDrawGlFunctor(containerView)) { | 67 && !mWebViewDelegate.canInvokeDrawGlFunctor(containerView)) { |
| 62 return false; | 68 return false; |
| 63 } | 69 } |
| 64 | 70 |
| 65 mWebViewDelegate.invokeDrawGlFunctor( | 71 mWebViewDelegate.invokeDrawGlFunctor( |
| 66 containerView, mDestroyRunnable.mNativeDrawGLFunctor, waitForCom
pletion); | 72 containerView, mDestroyRunnable.mNativeDrawGLFunctor, waitForCom
pletion); |
| 67 return true; | 73 return true; |
| 68 } | 74 } |
| 69 | 75 |
| 70 @Override | 76 @Override |
| 71 public boolean supportsDrawGLFunctorReleasedCallback() { | 77 public boolean supportsDrawGLFunctorReleasedCallback() { |
| 72 return sSupportFunctorReleasedCallback; | 78 return sSupportFunctorReleasedCallback; |
| 73 } | 79 } |
| 74 | 80 |
| 81 @Override |
| 82 public Runnable getDestroyRunnable() { |
| 83 return mDestroyRunnable; |
| 84 } |
| 85 |
| 75 public static void setChromiumAwDrawGLFunction(long functionPointer) { | 86 public static void setChromiumAwDrawGLFunction(long functionPointer) { |
| 76 nativeSetChromiumAwDrawGLFunction(functionPointer); | 87 nativeSetChromiumAwDrawGLFunction(functionPointer); |
| 77 } | 88 } |
| 78 | 89 |
| 79 // Holds the core resources of the class, everything required to correctly c
leanup. | 90 // Holds the core resources of the class, everything required to correctly c
leanup. |
| 80 // IMPORTANT: this class must not hold any reference back to the outer DrawG
LFunctor | 91 // IMPORTANT: this class must not hold any reference back to the outer DrawG
LFunctor |
| 81 // instance, as that will defeat GC of that object. | 92 // instance, as that will defeat GC of that object. |
| 82 private static final class DestroyRunnable implements Runnable { | 93 private static final class DestroyRunnable implements Runnable { |
| 83 private long mNativeDrawGLFunctor; | 94 private long mNativeDrawGLFunctor; |
| 84 DestroyRunnable(long nativeDrawGLFunctor) { | 95 DestroyRunnable(long nativeDrawGLFunctor) { |
| 85 mNativeDrawGLFunctor = nativeDrawGLFunctor; | 96 mNativeDrawGLFunctor = nativeDrawGLFunctor; |
| 86 assert mNativeDrawGLFunctor != 0; | 97 assert mNativeDrawGLFunctor != 0; |
| 87 } | 98 } |
| 88 | 99 |
| 89 // Called when the outer DrawGLFunctor instance has been GC'ed, i.e this
is its finalizer. | 100 // Called when the outer DrawGLFunctor instance has been GC'ed, i.e this
is its finalizer. |
| 90 @Override | 101 @Override |
| 91 public void run() { | 102 public void run() { |
| 92 assert mNativeDrawGLFunctor != 0; | 103 assert mNativeDrawGLFunctor != 0; |
| 93 nativeDestroyGLFunctor(mNativeDrawGLFunctor); | 104 nativeDestroyGLFunctor(mNativeDrawGLFunctor); |
| 94 mNativeDrawGLFunctor = 0; | 105 mNativeDrawGLFunctor = 0; |
| 95 } | 106 } |
| 96 } | 107 } |
| 97 | 108 |
| 98 private static native long nativeCreateGLFunctor(long viewContext); | 109 private static native long nativeCreateGLFunctor(long viewContext); |
| 99 private static native void nativeDestroyGLFunctor(long functor); | 110 private static native void nativeDestroyGLFunctor(long functor); |
| 100 private static native void nativeSetChromiumAwDrawGLFunction(long functionPo
inter); | 111 private static native void nativeSetChromiumAwDrawGLFunction(long functionPo
inter); |
| 101 } | 112 } |
| OLD | NEW |