| 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.view.View; | 9 import android.view.View; |
| 9 | 10 |
| 10 import com.android.webview.chromium.WebViewDelegateFactory.WebViewDelegate; | 11 import com.android.webview.chromium.WebViewDelegateFactory.WebViewDelegate; |
| 11 | 12 |
| 12 import org.chromium.content.common.CleanupReference; | 13 import org.chromium.content.common.CleanupReference; |
| 13 | 14 |
| 14 /** | 15 /** |
| 15 * Simple Java abstraction and wrapper for the native DrawGLFunctor flow. | 16 * Simple Java abstraction and wrapper for the native DrawGLFunctor flow. |
| 16 * 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) |
| 17 * 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 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 45 mContainerView = null; | 46 mContainerView = null; |
| 46 } | 47 } |
| 47 } | 48 } |
| 48 | 49 |
| 49 public void detach() { | 50 public void detach() { |
| 50 if (mWebViewDelegate != null && mContainerView != null) { | 51 if (mWebViewDelegate != null && mContainerView != null) { |
| 51 mWebViewDelegate.detachDrawGlFunctor(mContainerView, mNativeDrawGLFu
nctor); | 52 mWebViewDelegate.detachDrawGlFunctor(mContainerView, mNativeDrawGLFu
nctor); |
| 52 } | 53 } |
| 53 } | 54 } |
| 54 | 55 |
| 55 public boolean requestDrawGL(Canvas canvas, View containerView, boolean wait
ForCompletion) { | 56 private static final boolean sSupportFunctorReleasedCallback = |
| 57 (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) || "N".equals(Build.
VERSION.CODENAME); |
| 58 public boolean requestDrawGL(Canvas canvas, View containerView, boolean wait
ForCompletion, |
| 59 Runnable releasedCallback) { |
| 56 if (mDestroyRunnable.mNativeDrawGLFunctor == 0) { | 60 if (mDestroyRunnable.mNativeDrawGLFunctor == 0) { |
| 57 throw new RuntimeException("requested DrawGL on already destroyed Dr
awGLFunctor"); | 61 throw new RuntimeException("requested DrawGL on already destroyed Dr
awGLFunctor"); |
| 58 } | 62 } |
| 59 | 63 |
| 60 if (canvas != null && waitForCompletion) { | 64 if (canvas != null && waitForCompletion) { |
| 61 throw new IllegalArgumentException( | 65 throw new IllegalArgumentException( |
| 62 "requested a blocking DrawGL with a not null canvas."); | 66 "requested a blocking DrawGL with a not null canvas."); |
| 63 } | 67 } |
| 64 | 68 |
| 65 if (!mWebViewDelegate.canInvokeDrawGlFunctor(containerView)) { | 69 if (!mWebViewDelegate.canInvokeDrawGlFunctor(containerView)) { |
| 66 return false; | 70 return false; |
| 67 } | 71 } |
| 68 | 72 |
| 69 mContainerView = containerView; | 73 mContainerView = containerView; |
| 70 | 74 |
| 71 if (canvas == null) { | 75 if (canvas == null) { |
| 76 assert releasedCallback == null; |
| 72 mWebViewDelegate.invokeDrawGlFunctor( | 77 mWebViewDelegate.invokeDrawGlFunctor( |
| 73 containerView, mDestroyRunnable.mNativeDrawGLFunctor, waitFo
rCompletion); | 78 containerView, mDestroyRunnable.mNativeDrawGLFunctor, waitFo
rCompletion); |
| 74 return true; | 79 return true; |
| 75 } | 80 } |
| 76 | 81 |
| 77 mWebViewDelegate.callDrawGlFunction(canvas, mDestroyRunnable.mNativeDraw
GLFunctor); | 82 if (sSupportFunctorReleasedCallback) { |
| 83 assert releasedCallback != null; |
| 84 mWebViewDelegate.callDrawGlFunction( |
| 85 canvas, mDestroyRunnable.mNativeDrawGLFunctor, releasedCallb
ack); |
| 86 } else { |
| 87 assert releasedCallback == null; |
| 88 mWebViewDelegate.callDrawGlFunction(canvas, mDestroyRunnable.mNative
DrawGLFunctor); |
| 89 } |
| 78 return true; | 90 return true; |
| 79 } | 91 } |
| 80 | 92 |
| 93 public static boolean supportsDrawGLFunctorReleasedCallback() { |
| 94 return sSupportFunctorReleasedCallback; |
| 95 } |
| 96 |
| 81 public static void setChromiumAwDrawGLFunction(long functionPointer) { | 97 public static void setChromiumAwDrawGLFunction(long functionPointer) { |
| 82 nativeSetChromiumAwDrawGLFunction(functionPointer); | 98 nativeSetChromiumAwDrawGLFunction(functionPointer); |
| 83 } | 99 } |
| 84 | 100 |
| 85 // Holds the core resources of the class, everything required to correctly c
leanup. | 101 // Holds the core resources of the class, everything required to correctly c
leanup. |
| 86 // IMPORTANT: this class must not hold any reference back to the outer DrawG
LFunctor | 102 // IMPORTANT: this class must not hold any reference back to the outer DrawG
LFunctor |
| 87 // instance, as that will defeat GC of that object. | 103 // instance, as that will defeat GC of that object. |
| 88 private static final class DestroyRunnable implements Runnable { | 104 private static final class DestroyRunnable implements Runnable { |
| 89 private long mNativeDrawGLFunctor; | 105 private long mNativeDrawGLFunctor; |
| 90 DestroyRunnable(long nativeDrawGLFunctor) { | 106 DestroyRunnable(long nativeDrawGLFunctor) { |
| 91 mNativeDrawGLFunctor = nativeDrawGLFunctor; | 107 mNativeDrawGLFunctor = nativeDrawGLFunctor; |
| 92 assert mNativeDrawGLFunctor != 0; | 108 assert mNativeDrawGLFunctor != 0; |
| 93 } | 109 } |
| 94 | 110 |
| 95 // Called when the outer DrawGLFunctor instance has been GC'ed, i.e this
is its finalizer. | 111 // Called when the outer DrawGLFunctor instance has been GC'ed, i.e this
is its finalizer. |
| 96 @Override | 112 @Override |
| 97 public void run() { | 113 public void run() { |
| 98 assert mNativeDrawGLFunctor != 0; | 114 assert mNativeDrawGLFunctor != 0; |
| 99 nativeDestroyGLFunctor(mNativeDrawGLFunctor); | 115 nativeDestroyGLFunctor(mNativeDrawGLFunctor); |
| 100 mNativeDrawGLFunctor = 0; | 116 mNativeDrawGLFunctor = 0; |
| 101 } | 117 } |
| 102 } | 118 } |
| 103 | 119 |
| 104 private static native long nativeCreateGLFunctor(long viewContext); | 120 private static native long nativeCreateGLFunctor(long viewContext); |
| 105 private static native void nativeDestroyGLFunctor(long functor); | 121 private static native void nativeDestroyGLFunctor(long functor); |
| 106 private static native void nativeSetChromiumAwDrawGLFunction(long functionPo
inter); | 122 private static native void nativeSetChromiumAwDrawGLFunction(long functionPo
inter); |
| 107 } | 123 } |
| OLD | NEW |