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 <utils/Log.h> 28#include <utils/UniquePtr.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 // Workarounds for http://crbug.com/271096: SW draw only supports 64 // translate & scale transforms, and a simple rectangular clip. 65 // (This also avoids significant wasted time in calling 66 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex). 67 if (!canvas->getTotalClip().isRect() || 68 (canvas->getTotalMatrix().getType() & 69 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) { 70 return NULL; 71 } 72 73 UniquePtr<PixelInfo> pixels(new PixelInfo(canvas)); 74 return pixels->state ? pixels.release() : NULL; 75} 76 77void ReleasePixels(AwPixelInfo* pixels) { 78 delete static_cast<PixelInfo*>(pixels); 79} 80 81jobject CreatePicture(JNIEnv* env, SkPicture* picture) { 82 jclass clazz = env->FindClass("android/graphics/Picture"); 83 jmethodID constructor = env->GetMethodID(clazz, "<init>", "(IZ)V"); 84 ALOG_ASSERT(clazz); 85 ALOG_ASSERT(constructor); 86 return env->NewObject(clazz, constructor, picture, false); 87} 88 89bool IsSkiaVersionCompatible(SkiaVersionFunction function) { 90 bool compatible = false; 91 if (function && function == &SkGraphics::GetVersion) { 92 int android_major, android_minor, android_patch; 93 SkGraphics::GetVersion(&android_major, &android_minor, &android_patch); 94 95 int chromium_major, chromium_minor, chromium_patch; 96 (*function)(&chromium_major, &chromium_minor, &chromium_patch); 97 98 compatible = android_major == chromium_major && 99 android_minor == chromium_minor && 100 android_patch == chromium_patch; 101 } 102 return compatible; 103} 104 105jint GetDrawSWFunctionTable(JNIEnv* env, jclass) { 106 static const AwDrawSWFunctionTable function_table = { 107 &GetPixels, 108 &ReleasePixels, 109 &CreatePicture, 110 &IsSkiaVersionCompatible, 111 }; 112 return reinterpret_cast<jint>(&function_table); 113} 114 115jint GetDrawGLFunctionTable(JNIEnv* env, jclass) { 116 static const AwDrawGLFunctionTable function_table = { 117 &GraphicBufferImpl::Create, 118 &GraphicBufferImpl::Release, 119 &GraphicBufferImpl::MapStatic, 120 &GraphicBufferImpl::UnmapStatic, 121 &GraphicBufferImpl::GetNativeBufferStatic, 122 &GraphicBufferImpl::GetStrideStatic, 123 }; 124 return reinterpret_cast<jint>(&function_table); 125} 126 127const char kClassName[] = "com/android/webview/chromium/GraphicsUtils"; 128const JNINativeMethod kJniMethods[] = { 129 { "nativeGetDrawSWFunctionTable", "()I", 130 reinterpret_cast<void*>(GetDrawSWFunctionTable) }, 131 { "nativeGetDrawGLFunctionTable", "()I", 132 reinterpret_cast<void*>(GetDrawGLFunctionTable) }, 133}; 134 135} // namespace 136 137void RegisterGraphicsUtils(JNIEnv* env) { 138 jclass clazz = env->FindClass(kClassName); 139 LOG_ALWAYS_FATAL_IF(!clazz, "Unable to find class '%s'", kClassName); 140 141 int res = env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods)); 142 LOG_ALWAYS_FATAL_IF(res < 0, "register native methods failed: res=%d", res); 143} 144 145} // namespace android 146