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