blob: be36b6742037b342e525992b26a1dc81b3cd4729 [file] [log] [blame]
Torne (Richard Coles)0f03fbe2018-02-21 12:17:47 -05001/*
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
Torne (Richard Coles)0f03fbe2018-02-21 12:17:47 -050024#include <jni.h>
25#include <private/hwui/DrawGlInfo.h>
Torne (Richard Coles)0f03fbe2018-02-21 12:17:47 -050026#include <utils/Functor.h>
27#include <utils/Log.h>
28
Chris Blume41423392018-11-06 11:47:03 -080029#include "functor_utils.h"
30
Torne (Richard Coles)0f03fbe2018-02-21 12:17:47 -050031#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
35namespace android {
36namespace {
37
38AwDrawGLFunction* g_aw_drawgl_function = NULL;
39
40class DrawGLFunctor : public Functor {
41 public:
42 explicit DrawGLFunctor(jlong view_context) : view_context_(view_context) {}
Chris Blume0fd02aa2018-11-19 14:25:31 -080043 ~DrawGLFunctor() override {}
Torne (Richard Coles)0f03fbe2018-02-21 12:17:47 -050044
45 // Functor
Chris Blume0fd02aa2018-11-19 14:25:31 -080046 status_t operator ()(int what, void* data) override {
Torne (Richard Coles)0f03fbe2018-02-21 12:17:47 -050047 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;
John Reck18f442e2018-04-09 16:56:34 -070054 aw_info.version = kAwDrawGLInfoVersion;
Torne (Richard Coles)0f03fbe2018-02-21 12:17:47 -050055 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
Torne (Richard Coles)0f03fbe2018-02-21 12:17:47 -050099jlong CreateGLFunctor(JNIEnv*, jclass, jlong view_context) {
100 RaiseFileNumberLimit();
101 return reinterpret_cast<jlong>(new DrawGLFunctor(view_context));
102}
103
104void DestroyGLFunctor(JNIEnv*, jclass, jlong functor) {
105 delete reinterpret_cast<DrawGLFunctor*>(functor);
106}
107
108void SetChromiumAwDrawGLFunction(JNIEnv*, jclass, jlong draw_function) {
109 g_aw_drawgl_function = reinterpret_cast<AwDrawGLFunction*>(draw_function);
110}
111
112const char kClassName[] = "com/android/webview/chromium/DrawGLFunctor";
113const 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
124void 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