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