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
3 // found in the LICENSE file.
5 package com
.android
.webview
.chromium
;
7 import android
.graphics
.Canvas
;
8 import android
.view
.View
;
10 import com
.android
.webview
.chromium
.WebViewDelegateFactory
.WebViewDelegate
;
12 import org
.chromium
.content
.common
.CleanupReference
;
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)
17 * and then drawn and detached from the view tree any number of times (using requestDrawGL and
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 / CleanupReference.
22 private static final String TAG
= DrawGLFunctor
.class.getSimpleName();
24 // Pointer to native side instance
25 private CleanupReference mCleanupReference
;
26 private DestroyRunnable mDestroyRunnable
;
27 private final long mNativeDrawGLFunctor
;
28 private WebViewDelegate mWebViewDelegate
;
31 public DrawGLFunctor(long viewContext
, WebViewDelegate webViewDelegate
) {
32 mNativeDrawGLFunctor
= nativeCreateGLFunctor(viewContext
);
33 mDestroyRunnable
= new DestroyRunnable(mNativeDrawGLFunctor
);
34 mCleanupReference
= new CleanupReference(this, mDestroyRunnable
);
35 mWebViewDelegate
= webViewDelegate
;
38 public void destroy() {
40 if (mCleanupReference
!= null) {
41 mCleanupReference
.cleanupNow();
42 mCleanupReference
= null;
43 mDestroyRunnable
= null;
44 mWebViewDelegate
= null;
45 mContainerView
= null;
49 public void detach() {
50 if (mWebViewDelegate
!= null && mContainerView
!= null) {
51 mWebViewDelegate
.detachDrawGlFunctor(mContainerView
, mNativeDrawGLFunctor
);
55 public boolean requestDrawGL(Canvas canvas
, View containerView
, boolean waitForCompletion
) {
56 if (mDestroyRunnable
.mNativeDrawGLFunctor
== 0) {
57 throw new RuntimeException("requested DrawGL on already destroyed DrawGLFunctor");
60 if (canvas
!= null && waitForCompletion
) {
61 throw new IllegalArgumentException(
62 "requested a blocking DrawGL with a not null canvas.");
65 if (!mWebViewDelegate
.canInvokeDrawGlFunctor(containerView
)) {
69 mContainerView
= containerView
;
72 mWebViewDelegate
.invokeDrawGlFunctor(
73 containerView
, mDestroyRunnable
.mNativeDrawGLFunctor
, waitForCompletion
);
77 mWebViewDelegate
.callDrawGlFunction(canvas
, mDestroyRunnable
.mNativeDrawGLFunctor
);
81 public static void setChromiumAwDrawGLFunction(long functionPointer
) {
82 nativeSetChromiumAwDrawGLFunction(functionPointer
);
85 // Holds the core resources of the class, everything required to correctly cleanup.
86 // IMPORTANT: this class must not hold any reference back to the outer DrawGLFunctor
87 // instance, as that will defeat GC of that object.
88 private static final class DestroyRunnable
implements Runnable
{
89 private long mNativeDrawGLFunctor
;
90 DestroyRunnable(long nativeDrawGLFunctor
) {
91 mNativeDrawGLFunctor
= nativeDrawGLFunctor
;
92 assert mNativeDrawGLFunctor
!= 0;
95 // Called when the outer DrawGLFunctor instance has been GC'ed, i.e this is its finalizer.
98 assert mNativeDrawGLFunctor
!= 0;
99 nativeDestroyGLFunctor(mNativeDrawGLFunctor
);
100 mNativeDrawGLFunctor
= 0;
104 private static native long nativeCreateGLFunctor(long viewContext
);
105 private static native void nativeDestroyGLFunctor(long functor
);
106 private static native void nativeSetChromiumAwDrawGLFunction(long functionPointer
);