| 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 "GraphicsJNI.h" |
| 29 | #include "graphic_buffer_impl.h" |
| 30 | #include "SkCanvasStateUtils.h" |
| 31 | |
| 32 | #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
| 33 | |
| 34 | namespace android { |
| 35 | namespace { |
| 36 | |
| 37 | class PixelInfo : public AwPixelInfo { |
| 38 | public: |
| 39 | explicit PixelInfo(android::Canvas* canvas); |
| 40 | ~PixelInfo(); |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | PixelInfo::PixelInfo(android::Canvas* canvas) { |
| 45 | memset(this, 0, sizeof(AwPixelInfo)); |
| 46 | version = kAwPixelInfoVersion; |
| 47 | state = canvas->captureCanvasState(); |
| 48 | } |
| 49 | |
| 50 | PixelInfo::~PixelInfo() { |
| 51 | if (state) |
| 52 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 53 | } |
| 54 | |
| 55 | AwPixelInfo* GetPixels(JNIEnv* env, jobject java_canvas) { |
| 56 | android::Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, java_canvas); |
| 57 | if (!nativeCanvas) |
| 58 | return NULL; |
| 59 | |
| 60 | PixelInfo* pixels = new PixelInfo(nativeCanvas); |
| 61 | if (!pixels->state) { |
| 62 | delete pixels; |
| 63 | pixels = NULL; |
| 64 | } |
| 65 | return pixels; |
| 66 | } |
| 67 | |
| 68 | void ReleasePixels(AwPixelInfo* pixels) { |
| 69 | delete static_cast<PixelInfo*>(pixels); |
| 70 | } |
| 71 | |
| 72 | jlong GetDrawSWFunctionTable(JNIEnv* env, jclass) { |
| 73 | static AwDrawSWFunctionTable function_table; |
| 74 | function_table.version = kAwDrawSWFunctionTableVersion; |
| 75 | function_table.access_pixels = &GetPixels; |
| 76 | function_table.release_pixels = &ReleasePixels; |
| 77 | return reinterpret_cast<intptr_t>(&function_table); |
| 78 | } |
| 79 | |
| 80 | jlong GetDrawGLFunctionTable(JNIEnv* env, jclass) { |
| 81 | static AwDrawGLFunctionTable function_table; |
| 82 | function_table.version = kAwDrawGLFunctionTableVersion; |
| 83 | function_table.create_graphic_buffer = &GraphicBufferImpl::Create; |
| 84 | function_table.release_graphic_buffer = &GraphicBufferImpl::Release; |
| 85 | function_table.map = &GraphicBufferImpl::MapStatic; |
| 86 | function_table.unmap = &GraphicBufferImpl::UnmapStatic; |
| 87 | function_table.get_native_buffer = &GraphicBufferImpl::GetNativeBufferStatic; |
| 88 | function_table.get_stride = &GraphicBufferImpl::GetStrideStatic; |
| 89 | return reinterpret_cast<intptr_t>(&function_table); |
| 90 | } |
| 91 | |
| 92 | const char kClassName[] = "com/android/webview/chromium/GraphicsUtils"; |
| 93 | const JNINativeMethod kJniMethods[] = { |
| 94 | { "nativeGetDrawSWFunctionTable", "()J", |
| 95 | reinterpret_cast<void*>(GetDrawSWFunctionTable) }, |
| 96 | { "nativeGetDrawGLFunctionTable", "()J", |
| 97 | reinterpret_cast<void*>(GetDrawGLFunctionTable) }, |
| 98 | }; |
| 99 | |
| 100 | } // namespace |
| 101 | |
| 102 | void RegisterGraphicsUtils(JNIEnv* env) { |
| 103 | jclass clazz = env->FindClass(kClassName); |
| 104 | LOG_ALWAYS_FATAL_IF(!clazz, "Unable to find class '%s'", kClassName); |
| 105 | |
| 106 | int res = env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods)); |
| 107 | LOG_ALWAYS_FATAL_IF(res < 0, "register native methods failed: res=%d", res); |
| 108 | } |
| 109 | |
| 110 | } // namespace android |