1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // Provides a webviewchromium glue layer adapter from the internal Android
18 // GL Functor data types into the types the chromium stack expects, and back.
19
20 #define LOG_TAG "webviewchromium_plat_support"
21
22 #include "draw_gl.h"
23
24 #include <jni.h>
25 #include <private/hwui/DrawGlInfo.h>
26 #include <utils/Functor.h>
27 #include <utils/Log.h>
28
29 #include "functor_utils.h"
30
31 #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
32 #define COMPILE_ASSERT(expr, err) \
33 __unused static const char (err)[(expr) ? 1 : -1] = "";
34
35 namespace android {
36 namespace {
37
38 AwDrawGLFunction* g_aw_drawgl_function = NULL;
39
40 class DrawGLFunctor : public Functor {
41 public:
DrawGLFunctor(jlong view_context)42 explicit DrawGLFunctor(jlong view_context) : view_context_(view_context) {}
~DrawGLFunctor()43 ~DrawGLFunctor() override {}
44
45 // Functor
operator ()(int what,void * data)46 status_t operator ()(int what, void* data) override {
47 using uirenderer::DrawGlInfo;
48 if (!g_aw_drawgl_function) {
49 ALOGE("Cannot draw: no DrawGL Function installed");
50 return DrawGlInfo::kStatusDone;
51 }
52
53 AwDrawGLInfo aw_info;
54 aw_info.version = kAwDrawGLInfoVersion;
55 switch (what) {
56 case DrawGlInfo::kModeDraw: {
57 aw_info.mode = AwDrawGLInfo::kModeDraw;
58 DrawGlInfo* gl_info = reinterpret_cast<DrawGlInfo*>(data);
59
60 // Map across the input values.
61 aw_info.clip_left = gl_info->clipLeft;
62 aw_info.clip_top = gl_info->clipTop;
63 aw_info.clip_right = gl_info->clipRight;
64 aw_info.clip_bottom = gl_info->clipBottom;
65 aw_info.width = gl_info->width;
66 aw_info.height = gl_info->height;
67 aw_info.is_layer = gl_info->isLayer;
68 COMPILE_ASSERT(NELEM(aw_info.transform) == NELEM(gl_info->transform),
69 mismatched_transform_matrix_sizes);
70 for (int i = 0; i < NELEM(aw_info.transform); ++i) {
71 aw_info.transform[i] = gl_info->transform[i];
72 }
73 break;
74 }
75 case DrawGlInfo::kModeProcess:
76 aw_info.mode = AwDrawGLInfo::kModeProcess;
77 break;
78 case DrawGlInfo::kModeProcessNoContext:
79 aw_info.mode = AwDrawGLInfo::kModeProcessNoContext;
80 break;
81 case DrawGlInfo::kModeSync:
82 aw_info.mode = AwDrawGLInfo::kModeSync;
83 break;
84 default:
85 ALOGE("Unexpected DrawGLInfo type %d", what);
86 return DrawGlInfo::kStatusDone;
87 }
88
89 // Invoke the DrawGL method.
90 g_aw_drawgl_function(view_context_, &aw_info, NULL);
91
92 return DrawGlInfo::kStatusDone;
93 }
94
95 private:
96 intptr_t view_context_;
97 };
98
CreateGLFunctor(JNIEnv *,jclass,jlong view_context)99 jlong CreateGLFunctor(JNIEnv*, jclass, jlong view_context) {
100 RaiseFileNumberLimit();
101 return reinterpret_cast<jlong>(new DrawGLFunctor(view_context));
102 }
103
DestroyGLFunctor(JNIEnv *,jclass,jlong functor)104 void DestroyGLFunctor(JNIEnv*, jclass, jlong functor) {
105 delete reinterpret_cast<DrawGLFunctor*>(functor);
106 }
107
SetChromiumAwDrawGLFunction(JNIEnv *,jclass,jlong draw_function)108 void SetChromiumAwDrawGLFunction(JNIEnv*, jclass, jlong draw_function) {
109 g_aw_drawgl_function = reinterpret_cast<AwDrawGLFunction*>(draw_function);
110 }
111
112 const char kClassName[] = "com/android/webview/chromium/DrawGLFunctor";
113 const JNINativeMethod kJniMethods[] = {
114 { "nativeCreateGLFunctor", "(J)J",
115 reinterpret_cast<void*>(CreateGLFunctor) },
116 { "nativeDestroyGLFunctor", "(J)V",
117 reinterpret_cast<void*>(DestroyGLFunctor) },
118 { "nativeSetChromiumAwDrawGLFunction", "(J)V",
119 reinterpret_cast<void*>(SetChromiumAwDrawGLFunction) },
120 };
121
122 } // namespace
123
RegisterDrawGLFunctor(JNIEnv * env)124 void RegisterDrawGLFunctor(JNIEnv* env) {
125 jclass clazz = env->FindClass(kClassName);
126 LOG_ALWAYS_FATAL_IF(!clazz, "Unable to find class '%s'", kClassName);
127
128 int res = env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods));
129 LOG_ALWAYS_FATAL_IF(res < 0, "register native methods failed: res=%d", res);
130 }
131
132 } // namespace android
133