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 // graphics 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 #include "draw_sw.h"
24
25 #include <cstdlib>
26 #include <jni.h>
27 #include <utils/Log.h>
28 #include "graphic_buffer_impl.h"
29 #include "GraphicsJNI.h"
30 #include "SkCanvasStateUtils.h"
31 #include "SkGraphics.h"
32 #include "SkPicture.h"
33
34 #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
35
36 namespace android {
37 namespace {
38
39 class PixelInfo : public AwPixelInfo {
40 public:
41 PixelInfo(SkCanvas* canvas);
42 ~PixelInfo();
43 };
44
45
PixelInfo(SkCanvas * canvas)46 PixelInfo::PixelInfo(SkCanvas* canvas) {
47 memset(this, 0, sizeof(AwPixelInfo));
48 version = kAwPixelInfoVersion;
49 state = SkCanvasStateUtils::CaptureCanvasState(canvas);
50 }
51
~PixelInfo()52 PixelInfo::~PixelInfo() {
53 if (state)
54 SkCanvasStateUtils::ReleaseCanvasState(state);
55 }
56
GetPixels(JNIEnv * env,jobject java_canvas)57 AwPixelInfo* GetPixels(JNIEnv* env, jobject java_canvas) {
58 android::Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, java_canvas);
59 if (!nativeCanvas)
60 return NULL;
61
62 SkCanvas* canvas = nativeCanvas->asSkCanvas();
63 if (!canvas)
64 return NULL;
65
66 // Workarounds for http://crbug.com/271096: SW draw only supports
67 // translate & scale transforms, and a simple rectangular clip.
68 // (This also avoids significant wasted time in calling
69 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
70 if (!canvas->isClipRect() ||
71 (canvas->getTotalMatrix().getType() &
72 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
73 return NULL;
74 }
75
76 PixelInfo* pixels = new PixelInfo(canvas);
77 if (!pixels->state) {
78 delete pixels;
79 pixels = NULL;
80 }
81 return pixels;
82 }
83
ReleasePixels(AwPixelInfo * pixels)84 void ReleasePixels(AwPixelInfo* pixels) {
85 delete static_cast<PixelInfo*>(pixels);
86 }
87
GetDrawSWFunctionTable(JNIEnv * env,jclass)88 jlong GetDrawSWFunctionTable(JNIEnv* env, jclass) {
89 static AwDrawSWFunctionTable function_table;
90 function_table.version = kAwDrawSWFunctionTableVersion;
91 function_table.access_pixels = &GetPixels;
92 function_table.release_pixels = &ReleasePixels;
93 return reinterpret_cast<intptr_t>(&function_table);
94 }
95
GetDrawGLFunctionTable(JNIEnv * env,jclass)96 jlong GetDrawGLFunctionTable(JNIEnv* env, jclass) {
97 static AwDrawGLFunctionTable function_table;
98 function_table.version = kAwDrawGLFunctionTableVersion;
99 function_table.create_graphic_buffer = &GraphicBufferImpl::Create;
100 function_table.release_graphic_buffer = &GraphicBufferImpl::Release;
101 function_table.map = &GraphicBufferImpl::MapStatic;
102 function_table.unmap = &GraphicBufferImpl::UnmapStatic;
103 function_table.get_native_buffer = &GraphicBufferImpl::GetNativeBufferStatic;
104 function_table.get_stride = &GraphicBufferImpl::GetStrideStatic;
105 return reinterpret_cast<intptr_t>(&function_table);
106 }
107
108 const char kClassName[] = "com/android/webview/chromium/GraphicsUtils";
109 const JNINativeMethod kJniMethods[] = {
110 { "nativeGetDrawSWFunctionTable", "()J",
111 reinterpret_cast<void*>(GetDrawSWFunctionTable) },
112 { "nativeGetDrawGLFunctionTable", "()J",
113 reinterpret_cast<void*>(GetDrawGLFunctionTable) },
114 };
115
116 } // namespace
117
RegisterGraphicsUtils(JNIEnv * env)118 void RegisterGraphicsUtils(JNIEnv* env) {
119 jclass clazz = env->FindClass(kClassName);
120 LOG_ALWAYS_FATAL_IF(!clazz, "Unable to find class '%s'", kClassName);
121
122 int res = env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods));
123 LOG_ALWAYS_FATAL_IF(res < 0, "register native methods failed: res=%d", res);
124 }
125
126 } // namespace android
127