graphics_utils.cpp revision 2479270939ee95cbf8f7f1f2f20d7d68b66bb1d2
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 "android_webview/public/browser/draw_gl.h" 23#include "android_webview/public/browser/draw_sw.h" 24 25#include <cstdlib> 26#include <jni.h> 27#include <UniquePtr.h> 28#include <utils/Log.h> 29#include "graphic_buffer_impl.h" 30#include "GraphicsJNI.h" 31#include "SkCanvasStateUtils.h" 32#include "SkGraphics.h" 33#include "SkPicture.h" 34 35#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) 36 37namespace android { 38namespace { 39 40class PixelInfo : public AwPixelInfo { 41 public: 42 PixelInfo(SkCanvas* canvas); 43 ~PixelInfo(); 44}; 45 46 47PixelInfo::PixelInfo(SkCanvas* canvas) { 48 memset(this, 0, sizeof(AwPixelInfo)); 49 version = kAwPixelInfoVersion; 50 state = SkCanvasStateUtils::CaptureCanvasState(canvas); 51} 52 53PixelInfo::~PixelInfo() { 54 if (state) 55 SkCanvasStateUtils::ReleaseCanvasState(state); 56} 57 58AwPixelInfo* GetPixels(JNIEnv* env, jobject java_canvas) { 59 SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, java_canvas); 60 if (!canvas) 61 return NULL; 62 63 UniquePtr<PixelInfo> pixels(new PixelInfo(canvas)); 64 return pixels->state ? pixels.release() : NULL; 65} 66 67void ReleasePixels(AwPixelInfo* pixels) { 68 delete static_cast<PixelInfo*>(pixels); 69} 70 71jobject CreatePicture(JNIEnv* env, SkPicture* picture) { 72 jclass clazz = env->FindClass("android/graphics/Picture"); 73 jmethodID constructor = env->GetMethodID(clazz, "<init>", "(IZ)V"); 74 ALOG_ASSERT(clazz); 75 ALOG_ASSERT(constructor); 76 return env->NewObject(clazz, constructor, picture, false); 77} 78 79bool IsSkiaVersionCompatible(SkiaVersionFunction function) { 80 bool compatible = false; 81 if (function && function == &SkGraphics::GetVersion) { 82 int android_major, android_minor, android_patch; 83 SkGraphics::GetVersion(&android_major, &android_minor, &android_patch); 84 85 int chromium_major, chromium_minor, chromium_patch; 86 (*function)(&chromium_major, &chromium_minor, &chromium_patch); 87 88 compatible = android_major == chromium_major && 89 android_minor == chromium_minor && 90 android_patch == chromium_patch; 91 } 92 return compatible; 93} 94 95jint GetDrawSWFunctionTable(JNIEnv* env, jclass) { 96 static const AwDrawSWFunctionTable function_table = { 97 &GetPixels, 98 &ReleasePixels, 99 &CreatePicture, 100 &IsSkiaVersionCompatible, 101 }; 102 return reinterpret_cast<jint>(&function_table); 103} 104 105jint GetDrawGLFunctionTable(JNIEnv* env, jclass) { 106 static const AwDrawGLFunctionTable function_table = { 107 &GraphicBufferImpl::Create, 108 &GraphicBufferImpl::Release, 109 &GraphicBufferImpl::MapStatic, 110 &GraphicBufferImpl::UnmapStatic, 111 &GraphicBufferImpl::GetNativeBufferStatic, 112 &GraphicBufferImpl::GetStrideStatic, 113 }; 114 return reinterpret_cast<jint>(&function_table); 115} 116 117const char kClassName[] = "com/android/webview/chromium/GraphicsUtils"; 118const JNINativeMethod kJniMethods[] = { 119 { "nativeGetDrawSWFunctionTable", "()I", 120 reinterpret_cast<void*>(GetDrawSWFunctionTable) }, 121 { "nativeGetDrawGLFunctionTable", "()I", 122 reinterpret_cast<void*>(GetDrawGLFunctionTable) }, 123}; 124 125} // namespace 126 127void RegisterGraphicsUtils(JNIEnv* env) { 128 jclass clazz = env->FindClass(kClassName); 129 LOG_ALWAYS_FATAL_IF(!clazz, "Unable to find class '%s'", kClassName); 130 131 int res = env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods)); 132 LOG_ALWAYS_FATAL_IF(res < 0, "register native methods failed: res=%d", res); 133} 134 135} // namespace android 136