summaryrefslogtreecommitdiff
path: root/chromium/plat_support/graphics_utils.cpp
blob: 5d8d0a578f012438d682de3c1938b624dbe9c7bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Provides a webviewchromium glue layer adapter from the internal Android
// graphics types into the types the chromium stack expects, and back.

#define LOG_TAG "webviewchromium_plat_support"

#include "draw_gl.h"
#include "draw_sw.h"

#include <cstdlib>
#include <jni.h>
#include <utils/Log.h>
#include "graphic_buffer_impl.h"
#include "GraphicsJNI.h"
#include "SkCanvasStateUtils.h"
#include "SkGraphics.h"
#include "SkPicture.h"

#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))

namespace android {
namespace {

class PixelInfo : public AwPixelInfo {
 public:
  explicit PixelInfo(SkCanvas* canvas);
  ~PixelInfo();
};


PixelInfo::PixelInfo(SkCanvas* canvas) {
  memset(this, 0, sizeof(AwPixelInfo));
  version = kAwPixelInfoVersion;
  state = SkCanvasStateUtils::CaptureCanvasState(canvas);
}

PixelInfo::~PixelInfo() {
  if (state)
    SkCanvasStateUtils::ReleaseCanvasState(state);
}

AwPixelInfo* GetPixels(JNIEnv* env, jobject java_canvas) {
  android::Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, java_canvas);
  if (!nativeCanvas)
    return NULL;

  SkCanvas* canvas = nativeCanvas->asSkCanvas();
  if (!canvas)
    return NULL;

  // Workarounds for http://crbug.com/271096: SW draw only supports
  // translate & scale transforms, and a simple rectangular clip.
  // (This also avoids significant wasted time in calling
  // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
  if (!canvas->isClipRect() ||
      (canvas->getTotalMatrix().getType() &
                ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
    return NULL;
  }

  PixelInfo* pixels = new PixelInfo(canvas);
  if (!pixels->state) {
      delete pixels;
      pixels = NULL;
  }
  return pixels;
}

void ReleasePixels(AwPixelInfo* pixels) {
  delete static_cast<PixelInfo*>(pixels);
}

jlong GetDrawSWFunctionTable(JNIEnv* env, jclass) {
  static AwDrawSWFunctionTable function_table;
  function_table.version = kAwDrawSWFunctionTableVersion;
  function_table.access_pixels = &GetPixels;
  function_table.release_pixels = &ReleasePixels;
  return reinterpret_cast<intptr_t>(&function_table);
}

jlong GetDrawGLFunctionTable(JNIEnv* env, jclass) {
  static AwDrawGLFunctionTable function_table;
  function_table.version = kAwDrawGLFunctionTableVersion;
  function_table.create_graphic_buffer = &GraphicBufferImpl::Create;
  function_table.release_graphic_buffer = &GraphicBufferImpl::Release;
  function_table.map = &GraphicBufferImpl::MapStatic;
  function_table.unmap = &GraphicBufferImpl::UnmapStatic;
  function_table.get_native_buffer = &GraphicBufferImpl::GetNativeBufferStatic;
  function_table.get_stride = &GraphicBufferImpl::GetStrideStatic;
  return reinterpret_cast<intptr_t>(&function_table);
}

const char kClassName[] = "com/android/webview/chromium/GraphicsUtils";
const JNINativeMethod kJniMethods[] = {
    { "nativeGetDrawSWFunctionTable", "()J",
        reinterpret_cast<void*>(GetDrawSWFunctionTable) },
    { "nativeGetDrawGLFunctionTable", "()J",
        reinterpret_cast<void*>(GetDrawGLFunctionTable) },
};

}  // namespace

void RegisterGraphicsUtils(JNIEnv* env) {
  jclass clazz = env->FindClass(kClassName);
  LOG_ALWAYS_FATAL_IF(!clazz, "Unable to find class '%s'", kClassName);

  int res = env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods));
  LOG_ALWAYS_FATAL_IF(res < 0, "register native methods failed: res=%d", res);
}

}  // namespace android