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.view.View; | 8 import android.view.View; |
9 | 9 |
10 import com.android.webview.chromium.WebViewDelegateFactory.WebViewDelegate; | 10 import com.android.webview.chromium.WebViewDelegateFactory.WebViewDelegate; |
11 | 11 |
12 import org.chromium.content.common.CleanupReference; | 12 import org.chromium.content.common.CleanupReference; |
13 | 13 |
14 /** | 14 /** |
15 * Simple Java abstraction and wrapper for the native DrawGLFunctor flow. | 15 * 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) | 16 * 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 | 17 * and then drawn and detached from the view tree any number of times (using req
uestDrawGL and |
18 * detach respectively). Then when finished with, it can be explicitly released
by calling | 18 * detach respectively). Then when finished with, it can be explicitly released
by calling |
19 * destroy() or will clean itself up as required via finalizer / CleanupReferenc
e. | 19 * destroy() or will clean itself up as required via finalizer / CleanupReferenc
e. |
20 */ | 20 */ |
21 class DrawGLFunctor { | 21 class DrawGLFunctor { |
22 private static final String TAG = DrawGLFunctor.class.getSimpleName(); | 22 private static final String TAG = DrawGLFunctor.class.getSimpleName(); |
23 | 23 |
24 // Pointer to native side instance | 24 // Pointer to native side instance |
25 private CleanupReference mCleanupReference; | 25 private CleanupReference mCleanupReference; |
26 private DestroyRunnable mDestroyRunnable; | 26 private DestroyRunnable mDestroyRunnable; |
| 27 private final long mNativeDrawGLFunctor; |
27 private WebViewDelegate mWebViewDelegate; | 28 private WebViewDelegate mWebViewDelegate; |
| 29 View mContainerView; |
28 | 30 |
29 public DrawGLFunctor(long viewContext, WebViewDelegate webViewDelegate) { | 31 public DrawGLFunctor(long viewContext, WebViewDelegate webViewDelegate) { |
30 mDestroyRunnable = new DestroyRunnable(nativeCreateGLFunctor(viewContext
), webViewDelegate); | 32 mNativeDrawGLFunctor = nativeCreateGLFunctor(viewContext); |
| 33 mDestroyRunnable = new DestroyRunnable(mNativeDrawGLFunctor); |
31 mCleanupReference = new CleanupReference(this, mDestroyRunnable); | 34 mCleanupReference = new CleanupReference(this, mDestroyRunnable); |
32 mWebViewDelegate = webViewDelegate; | 35 mWebViewDelegate = webViewDelegate; |
33 } | 36 } |
34 | 37 |
35 public void destroy() { | 38 public void destroy() { |
36 detach(); | 39 detach(); |
37 if (mCleanupReference != null) { | 40 if (mCleanupReference != null) { |
38 mCleanupReference.cleanupNow(); | 41 mCleanupReference.cleanupNow(); |
39 mCleanupReference = null; | 42 mCleanupReference = null; |
40 mDestroyRunnable = null; | 43 mDestroyRunnable = null; |
41 mWebViewDelegate = null; | 44 mWebViewDelegate = null; |
| 45 mContainerView = null; |
42 } | 46 } |
43 } | 47 } |
44 | 48 |
45 public void detach() { | 49 public void detach() { |
46 mDestroyRunnable.detachNativeFunctor(); | 50 if (mWebViewDelegate != null && mContainerView != null) { |
| 51 mWebViewDelegate.detachDrawGlFunctor(mContainerView, mNativeDrawGLFu
nctor); |
| 52 } |
47 } | 53 } |
48 | 54 |
49 public boolean requestDrawGL(Canvas canvas, View containerView, boolean wait
ForCompletion) { | 55 public boolean requestDrawGL(Canvas canvas, View containerView, boolean wait
ForCompletion) { |
50 if (mDestroyRunnable.mNativeDrawGLFunctor == 0) { | 56 if (mDestroyRunnable.mNativeDrawGLFunctor == 0) { |
51 throw new RuntimeException("requested DrawGL on already destroyed Dr
awGLFunctor"); | 57 throw new RuntimeException("requested DrawGL on already destroyed Dr
awGLFunctor"); |
52 } | 58 } |
53 | 59 |
54 if (canvas != null && waitForCompletion) { | 60 if (canvas != null && waitForCompletion) { |
55 throw new IllegalArgumentException( | 61 throw new IllegalArgumentException( |
56 "requested a blocking DrawGL with a not null canvas."); | 62 "requested a blocking DrawGL with a not null canvas."); |
57 } | 63 } |
58 | 64 |
59 if (!mWebViewDelegate.canInvokeDrawGlFunctor(containerView)) { | 65 if (!mWebViewDelegate.canInvokeDrawGlFunctor(containerView)) { |
60 return false; | 66 return false; |
61 } | 67 } |
62 | 68 |
63 mDestroyRunnable.mContainerView = containerView; | 69 mContainerView = containerView; |
64 | 70 |
65 if (canvas == null) { | 71 if (canvas == null) { |
66 mWebViewDelegate.invokeDrawGlFunctor( | 72 mWebViewDelegate.invokeDrawGlFunctor( |
67 containerView, mDestroyRunnable.mNativeDrawGLFunctor, waitFo
rCompletion); | 73 containerView, mDestroyRunnable.mNativeDrawGLFunctor, waitFo
rCompletion); |
68 return true; | 74 return true; |
69 } | 75 } |
70 | 76 |
71 mWebViewDelegate.callDrawGlFunction(canvas, mDestroyRunnable.mNativeDraw
GLFunctor); | 77 mWebViewDelegate.callDrawGlFunction(canvas, mDestroyRunnable.mNativeDraw
GLFunctor); |
72 return true; | 78 return true; |
73 } | 79 } |
74 | 80 |
75 public static void setChromiumAwDrawGLFunction(long functionPointer) { | 81 public static void setChromiumAwDrawGLFunction(long functionPointer) { |
76 nativeSetChromiumAwDrawGLFunction(functionPointer); | 82 nativeSetChromiumAwDrawGLFunction(functionPointer); |
77 } | 83 } |
78 | 84 |
79 // Holds the core resources of the class, everything required to correctly c
leanup. | 85 // 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 | 86 // IMPORTANT: this class must not hold any reference back to the outer DrawG
LFunctor |
81 // instance, as that will defeat GC of that object. | 87 // instance, as that will defeat GC of that object. |
82 private static final class DestroyRunnable implements Runnable { | 88 private static final class DestroyRunnable implements Runnable { |
83 private WebViewDelegate mWebViewDelegate; | 89 private long mNativeDrawGLFunctor; |
84 View mContainerView; | 90 DestroyRunnable(long nativeDrawGLFunctor) { |
85 long mNativeDrawGLFunctor; | |
86 DestroyRunnable(long nativeDrawGLFunctor, WebViewDelegate webViewDelegat
e) { | |
87 mNativeDrawGLFunctor = nativeDrawGLFunctor; | 91 mNativeDrawGLFunctor = nativeDrawGLFunctor; |
88 mWebViewDelegate = webViewDelegate; | 92 assert mNativeDrawGLFunctor != 0; |
89 } | 93 } |
90 | 94 |
91 // Called when the outer DrawGLFunctor instance has been GC'ed, i.e this
is its finalizer. | 95 // Called when the outer DrawGLFunctor instance has been GC'ed, i.e this
is its finalizer. |
92 @Override | 96 @Override |
93 public void run() { | 97 public void run() { |
94 detachNativeFunctor(); | 98 assert mNativeDrawGLFunctor != 0; |
95 nativeDestroyGLFunctor(mNativeDrawGLFunctor); | 99 nativeDestroyGLFunctor(mNativeDrawGLFunctor); |
96 mNativeDrawGLFunctor = 0; | 100 mNativeDrawGLFunctor = 0; |
97 } | 101 } |
98 | |
99 void detachNativeFunctor() { | |
100 if (mNativeDrawGLFunctor != 0 && mContainerView != null && mWebViewD
elegate != null) { | |
101 mWebViewDelegate.detachDrawGlFunctor(mContainerView, mNativeDraw
GLFunctor); | |
102 } | |
103 mContainerView = null; | |
104 mWebViewDelegate = null; | |
105 } | |
106 } | 102 } |
107 | 103 |
108 private static native long nativeCreateGLFunctor(long viewContext); | 104 private static native long nativeCreateGLFunctor(long viewContext); |
109 private static native void nativeDestroyGLFunctor(long functor); | 105 private static native void nativeDestroyGLFunctor(long functor); |
110 private static native void nativeSetChromiumAwDrawGLFunction(long functionPo
inter); | 106 private static native void nativeSetChromiumAwDrawGLFunction(long functionPo
inter); |
111 } | 107 } |
OLD | NEW |