draw_gl_functor.cpp revision 8a8a1f9b5ea238437196790b89f4dbec78fda4e9
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 "android_webview/public/browser/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#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
30#define COMPILE_ASSERT(expr, err) static const char err[(expr) ? 1 : -1] = "";
31
32namespace android {
33namespace {
34
35AwDrawGLFunction* g_aw_drawgl_function = NULL;
36
37class DrawGLFunctor : public Functor {
38 public:
39  DrawGLFunctor(jint view_context) : view_context_(view_context) {}
40  virtual ~DrawGLFunctor() {}
41
42  // Functor
43  virtual status_t operator ()(int what, void* data) {
44    using uirenderer::DrawGlInfo;
45    if (!g_aw_drawgl_function) {
46      ALOGE("Cannot draw: no DrawGL Function installed");
47      return DrawGlInfo::kStatusDone;
48    }
49
50    AwDrawGLInfo aw_info;
51    aw_info.mode = (what == DrawGlInfo::kModeProcess) ?
52        AwDrawGLInfo::kModeProcess : AwDrawGLInfo::kModeDraw;
53    DrawGlInfo* gl_info = reinterpret_cast<DrawGlInfo*>(data);
54
55    // Map across the input values.
56    aw_info.clip_left = gl_info->clipLeft;
57    aw_info.clip_top = gl_info->clipTop;
58    aw_info.clip_right = gl_info->clipRight;
59    aw_info.clip_bottom = gl_info->clipBottom;
60    aw_info.width = gl_info->width;
61    aw_info.height = gl_info->height;
62    aw_info.is_layer = gl_info->isLayer;
63    COMPILE_ASSERT(NELEM(aw_info.transform) == NELEM(gl_info->transform),
64                   mismatched_transform_matrix_sizes);
65    for (int i = 0; i < NELEM(aw_info.transform); ++i) {
66      aw_info.transform[i] = gl_info->transform[i];
67    }
68
69    // Also pre-initialize the output fields in case the implementation does
70    // not modify them.
71    aw_info.status_mask = AwDrawGLInfo::kStatusMaskDone;
72    aw_info.dirty_left = gl_info->dirtyLeft;
73    aw_info.dirty_top = gl_info->dirtyTop;
74    aw_info.dirty_right = gl_info->dirtyRight;
75    aw_info.dirty_bottom = gl_info->dirtyBottom;
76
77    // Invoke the DrawGL method.
78    g_aw_drawgl_function(view_context_, &aw_info, NULL);
79
80    // Copy out the outputs.
81    gl_info->dirtyLeft = aw_info.dirty_left;
82    gl_info->dirtyTop = aw_info.dirty_top;
83    gl_info->dirtyRight = aw_info.dirty_right;
84    gl_info->dirtyBottom = aw_info.dirty_bottom;
85
86    // Calculate the return code.
87    status_t res = DrawGlInfo::kStatusDone;
88    if (aw_info.status_mask & AwDrawGLInfo::kStatusMaskDraw)
89      res |= DrawGlInfo::kStatusDraw;
90    if (aw_info.status_mask & AwDrawGLInfo::kStatusMaskInvoke)
91      res |= DrawGlInfo::kStatusInvoke;
92
93    return res;
94  }
95
96 private:
97  int view_context_;
98};
99
100jint CreateGLFunctor(JNIEnv*, jclass, jint view_context) {
101  return reinterpret_cast<jint>(new DrawGLFunctor(view_context));
102}
103
104void DestroyGLFunctor(JNIEnv*, jclass, jint functor) {
105  delete reinterpret_cast<DrawGLFunctor*>(functor);
106}
107
108void SetChromiumAwDrawGLFunction(JNIEnv*, jclass, jint 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", "(I)I",
115        reinterpret_cast<void*>(CreateGLFunctor) },
116    { "nativeDestroyGLFunctor", "(I)V",
117        reinterpret_cast<void*>(DestroyGLFunctor) },
118    { "nativeSetChromiumAwDrawGLFunction", "(I)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
133