Torne (Richard Coles) | 0f03fbe | 2018-02-21 12:17:47 -0500 | [diff] [blame] | 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 <Properties.h> |
| 25 | #include <errno.h> |
| 26 | #include <jni.h> |
| 27 | #include <private/hwui/DrawGlInfo.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/resource.h> |
| 30 | #include <sys/time.h> |
| 31 | #include <utils/Functor.h> |
| 32 | #include <utils/Log.h> |
| 33 | |
| 34 | #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
| 35 | #define COMPILE_ASSERT(expr, err) \ |
| 36 | __unused static const char (err)[(expr) ? 1 : -1] = ""; |
| 37 | |
| 38 | namespace android { |
| 39 | namespace { |
| 40 | |
| 41 | AwDrawGLFunction* g_aw_drawgl_function = NULL; |
| 42 | |
| 43 | class DrawGLFunctor : public Functor { |
| 44 | public: |
| 45 | explicit DrawGLFunctor(jlong view_context) : view_context_(view_context) {} |
| 46 | virtual ~DrawGLFunctor() {} |
| 47 | |
| 48 | // Functor |
| 49 | virtual status_t operator ()(int what, void* data) { |
| 50 | using uirenderer::DrawGlInfo; |
| 51 | if (!g_aw_drawgl_function) { |
| 52 | ALOGE("Cannot draw: no DrawGL Function installed"); |
| 53 | return DrawGlInfo::kStatusDone; |
| 54 | } |
| 55 | |
| 56 | AwDrawGLInfo aw_info; |
| 57 | // TODO(boliu): Remove property check once OpenGL fallback is removed. |
| 58 | auto render_pipeline_type = |
| 59 | android::uirenderer::Properties::getRenderPipelineType(); |
| 60 | aw_info.version = (render_pipeline_type == |
| 61 | android::uirenderer::RenderPipelineType::OpenGL) |
| 62 | ? 2 |
| 63 | : kAwDrawGLInfoVersion; |
| 64 | switch (what) { |
| 65 | case DrawGlInfo::kModeDraw: { |
| 66 | aw_info.mode = AwDrawGLInfo::kModeDraw; |
| 67 | DrawGlInfo* gl_info = reinterpret_cast<DrawGlInfo*>(data); |
| 68 | |
| 69 | // Map across the input values. |
| 70 | aw_info.clip_left = gl_info->clipLeft; |
| 71 | aw_info.clip_top = gl_info->clipTop; |
| 72 | aw_info.clip_right = gl_info->clipRight; |
| 73 | aw_info.clip_bottom = gl_info->clipBottom; |
| 74 | aw_info.width = gl_info->width; |
| 75 | aw_info.height = gl_info->height; |
| 76 | aw_info.is_layer = gl_info->isLayer; |
| 77 | COMPILE_ASSERT(NELEM(aw_info.transform) == NELEM(gl_info->transform), |
| 78 | mismatched_transform_matrix_sizes); |
| 79 | for (int i = 0; i < NELEM(aw_info.transform); ++i) { |
| 80 | aw_info.transform[i] = gl_info->transform[i]; |
| 81 | } |
| 82 | break; |
| 83 | } |
| 84 | case DrawGlInfo::kModeProcess: |
| 85 | aw_info.mode = AwDrawGLInfo::kModeProcess; |
| 86 | break; |
| 87 | case DrawGlInfo::kModeProcessNoContext: |
| 88 | aw_info.mode = AwDrawGLInfo::kModeProcessNoContext; |
| 89 | break; |
| 90 | case DrawGlInfo::kModeSync: |
| 91 | aw_info.mode = AwDrawGLInfo::kModeSync; |
| 92 | break; |
| 93 | default: |
| 94 | ALOGE("Unexpected DrawGLInfo type %d", what); |
| 95 | return DrawGlInfo::kStatusDone; |
| 96 | } |
| 97 | |
| 98 | // Invoke the DrawGL method. |
| 99 | g_aw_drawgl_function(view_context_, &aw_info, NULL); |
| 100 | |
| 101 | return DrawGlInfo::kStatusDone; |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | intptr_t view_context_; |
| 106 | }; |
| 107 | |
| 108 | // Raise the file handle soft limit to the hard limit since gralloc buffers |
| 109 | // uses file handles. |
| 110 | void RaiseFileNumberLimit() { |
| 111 | static bool have_raised_limit = false; |
| 112 | if (have_raised_limit) |
| 113 | return; |
| 114 | |
| 115 | have_raised_limit = true; |
| 116 | struct rlimit limit_struct; |
| 117 | limit_struct.rlim_cur = 0; |
| 118 | limit_struct.rlim_max = 0; |
| 119 | if (getrlimit(RLIMIT_NOFILE, &limit_struct) == 0) { |
| 120 | limit_struct.rlim_cur = limit_struct.rlim_max; |
| 121 | if (setrlimit(RLIMIT_NOFILE, &limit_struct) != 0) { |
| 122 | ALOGE("setrlimit failed: %s", strerror(errno)); |
| 123 | } |
| 124 | } else { |
| 125 | ALOGE("getrlimit failed: %s", strerror(errno)); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | jlong CreateGLFunctor(JNIEnv*, jclass, jlong view_context) { |
| 130 | RaiseFileNumberLimit(); |
| 131 | return reinterpret_cast<jlong>(new DrawGLFunctor(view_context)); |
| 132 | } |
| 133 | |
| 134 | void DestroyGLFunctor(JNIEnv*, jclass, jlong functor) { |
| 135 | delete reinterpret_cast<DrawGLFunctor*>(functor); |
| 136 | } |
| 137 | |
| 138 | void SetChromiumAwDrawGLFunction(JNIEnv*, jclass, jlong draw_function) { |
| 139 | g_aw_drawgl_function = reinterpret_cast<AwDrawGLFunction*>(draw_function); |
| 140 | } |
| 141 | |
| 142 | const char kClassName[] = "com/android/webview/chromium/DrawGLFunctor"; |
| 143 | const JNINativeMethod kJniMethods[] = { |
| 144 | { "nativeCreateGLFunctor", "(J)J", |
| 145 | reinterpret_cast<void*>(CreateGLFunctor) }, |
| 146 | { "nativeDestroyGLFunctor", "(J)V", |
| 147 | reinterpret_cast<void*>(DestroyGLFunctor) }, |
| 148 | { "nativeSetChromiumAwDrawGLFunction", "(J)V", |
| 149 | reinterpret_cast<void*>(SetChromiumAwDrawGLFunction) }, |
| 150 | }; |
| 151 | |
| 152 | } // namespace |
| 153 | |
| 154 | void RegisterDrawGLFunctor(JNIEnv* env) { |
| 155 | jclass clazz = env->FindClass(kClassName); |
| 156 | LOG_ALWAYS_FATAL_IF(!clazz, "Unable to find class '%s'", kClassName); |
| 157 | |
| 158 | int res = env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods)); |
| 159 | LOG_ALWAYS_FATAL_IF(res < 0, "register native methods failed: res=%d", res); |
| 160 | } |
| 161 | |
| 162 | } // namespace android |