draw_gl_functor.cpp revision 5c5385f3e5f86c5183408e52f76f1bb7798cda9a
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 <errno.h>
25#include <jni.h>
26#include <private/hwui/DrawGlInfo.h>
27#include <string.h>
28#include <sys/resource.h>
29#include <sys/time.h>
30#include <utils/Functor.h>
31#include <utils/Log.h>
32
33#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
34#define COMPILE_ASSERT(expr, err) static const char err[(expr) ? 1 : -1] = "";
35
36namespace android {
37namespace {
38
39AwDrawGLFunction* g_aw_drawgl_function = NULL;
40
41class DrawGLFunctor : public Functor {
42 public:
43  DrawGLFunctor(jlong view_context) : view_context_(view_context) {}
44  virtual ~DrawGLFunctor() {}
45
46  // Functor
47  virtual status_t operator ()(int what, void* data) {
48    using uirenderer::DrawGlInfo;
49    if (!g_aw_drawgl_function) {
50      ALOGE("Cannot draw: no DrawGL Function installed");
51      return DrawGlInfo::kStatusDone;
52    }
53
54    AwDrawGLInfo aw_info;
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        // TODO: Fix once ProcessNoContext is in chromium.
80        aw_info.mode = AwDrawGLInfo::kModeProcess;
81        break;
82      default:
83        ALOGE("Unexpected DrawGLInfo type %d", what);
84        return DrawGlInfo::kStatusDone;
85    }
86
87    // Invoke the DrawGL method.
88    g_aw_drawgl_function(view_context_, &aw_info, NULL);
89
90    return DrawGlInfo::kStatusDone;
91  }
92
93 private:
94  intptr_t view_context_;
95};
96
97// Raise the file handle soft limit to the hard limit since gralloc buffers
98// uses file handles.
99void RaiseFileNumberLimit() {
100  static bool have_raised_limit = false;
101  if (have_raised_limit)
102    return;
103
104  have_raised_limit = true;
105  struct rlimit limit_struct;
106  limit_struct.rlim_cur = 0;
107  limit_struct.rlim_max = 0;
108  if (getrlimit(RLIMIT_NOFILE, &limit_struct) == 0) {
109    limit_struct.rlim_cur = limit_struct.rlim_max;
110    if (setrlimit(RLIMIT_NOFILE, &limit_struct) != 0) {
111      ALOGE("setrlimit failed: %s", strerror(errno));
112    }
113  } else {
114    ALOGE("getrlimit failed: %s", strerror(errno));
115  }
116}
117
118jlong CreateGLFunctor(JNIEnv*, jclass, jlong view_context) {
119  RaiseFileNumberLimit();
120  return reinterpret_cast<jlong>(new DrawGLFunctor(view_context));
121}
122
123void DestroyGLFunctor(JNIEnv*, jclass, jlong functor) {
124  delete reinterpret_cast<DrawGLFunctor*>(functor);
125}
126
127void SetChromiumAwDrawGLFunction(JNIEnv*, jclass, jlong draw_function) {
128  g_aw_drawgl_function = reinterpret_cast<AwDrawGLFunction*>(draw_function);
129}
130
131const char kClassName[] = "com/android/webview/chromium/DrawGLFunctor";
132const JNINativeMethod kJniMethods[] = {
133    { "nativeCreateGLFunctor", "(J)J",
134        reinterpret_cast<void*>(CreateGLFunctor) },
135    { "nativeDestroyGLFunctor", "(J)V",
136        reinterpret_cast<void*>(DestroyGLFunctor) },
137    { "nativeSetChromiumAwDrawGLFunction", "(J)V",
138        reinterpret_cast<void*>(SetChromiumAwDrawGLFunction) },
139};
140
141}  // namespace
142
143void RegisterDrawGLFunctor(JNIEnv* env) {
144  jclass clazz = env->FindClass(kClassName);
145  LOG_ALWAYS_FATAL_IF(!clazz, "Unable to find class '%s'", kClassName);
146
147  int res = env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods));
148  LOG_ALWAYS_FATAL_IF(res < 0, "register native methods failed: res=%d", res);
149}
150
151}  // namespace android
152