[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / android_webview / native / java_browser_view_renderer_helper.cc
blobf874676b4a5fbd50ea73b2dbc1516bd367c76dee
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "android_webview/native/java_browser_view_renderer_helper.h"
7 #include <android/bitmap.h>
9 #include "android_webview/common/aw_switches.h"
10 #include "android_webview/public/browser/draw_sw.h"
11 #include "base/debug/trace_event.h"
12 #include "jni/JavaBrowserViewRendererHelper_jni.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "third_party/skia/include/utils/SkCanvasStateUtils.h"
16 using base::android::JavaRef;
17 using base::android::ScopedJavaLocalRef;
19 namespace android_webview {
21 namespace {
23 // Provides software rendering functions from the Android glue layer.
24 // Allows preventing extra copies of data when rendering.
25 AwDrawSWFunctionTable* g_sw_draw_functions = NULL;
27 class ScopedPixelAccess {
28 public:
29 ScopedPixelAccess(JNIEnv* env, jobject java_canvas) : pixels_(NULL) {
30 if (g_sw_draw_functions && !switches::ForceAuxiliaryBitmap())
31 pixels_ = g_sw_draw_functions->access_pixels(env, java_canvas);
34 ~ScopedPixelAccess() {
35 if (pixels_)
36 g_sw_draw_functions->release_pixels(pixels_);
39 AwPixelInfo* pixels() { return pixels_; }
41 private:
42 AwPixelInfo* pixels_;
44 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedPixelAccess);
47 } // namespace
49 // static
50 void JavaBrowserViewRendererHelper::SetAwDrawSWFunctionTable(
51 AwDrawSWFunctionTable* table) {
52 g_sw_draw_functions = table;
55 // static
56 JavaBrowserViewRendererHelper* JavaBrowserViewRendererHelper::GetInstance() {
57 static JavaBrowserViewRendererHelper* g_instance =
58 new JavaBrowserViewRendererHelper;
59 return g_instance;
62 // static
63 BrowserViewRendererJavaHelper* BrowserViewRendererJavaHelper::GetInstance() {
64 return JavaBrowserViewRendererHelper::GetInstance();
67 JavaBrowserViewRendererHelper::JavaBrowserViewRendererHelper() {}
69 JavaBrowserViewRendererHelper::~JavaBrowserViewRendererHelper() {}
71 bool JavaBrowserViewRendererHelper::RenderViaAuxilaryBitmapIfNeeded(
72 jobject java_canvas,
73 const gfx::Vector2d& scroll_correction,
74 const gfx::Rect& auxiliary_bitmap_rect,
75 RenderMethod render_source) {
76 TRACE_EVENT0("android_webview", "RenderViaAuxilaryBitmapIfNeeded");
78 JNIEnv* env = base::android::AttachCurrentThread();
79 ScopedPixelAccess auto_release_pixels(env, java_canvas);
80 AwPixelInfo* pixels = auto_release_pixels.pixels();
81 if (pixels && pixels->state) {
82 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
83 SkCanvasStateUtils::CreateFromCanvasState(pixels->state));
85 // Workarounds for http://crbug.com/271096: SW draw only supports
86 // translate & scale transforms, and a simple rectangular clip.
87 if (canvas && (!canvas->isClipRect() ||
88 (canvas->getTotalMatrix().getType() &
89 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)))) {
90 canvas.clear();
92 if (canvas) {
93 canvas->translate(scroll_correction.x(), scroll_correction.y());
94 return render_source.Run(canvas.get());
97 return RenderViaAuxilaryBitmap(env,
98 java_canvas,
99 scroll_correction,
100 auxiliary_bitmap_rect,
101 render_source);
104 bool JavaBrowserViewRendererHelper::RenderViaAuxilaryBitmap(
105 JNIEnv* env,
106 jobject java_canvas,
107 const gfx::Vector2d& scroll_correction,
108 const gfx::Rect& auxiliary_bitmap_rect,
109 const RenderMethod& render_source) {
110 // Render into an auxiliary bitmap if pixel info is not available.
111 ScopedJavaLocalRef<jobject> jcanvas(env, java_canvas);
112 TRACE_EVENT0("android_webview", "RenderToAuxBitmap");
114 if (auxiliary_bitmap_rect.width() <= 0 || auxiliary_bitmap_rect.height() <= 0)
115 return false;
117 ScopedJavaLocalRef<jobject> jbitmap(
118 Java_JavaBrowserViewRendererHelper_createBitmap(
119 env,
120 auxiliary_bitmap_rect.width(),
121 auxiliary_bitmap_rect.height(),
122 jcanvas.obj()));
123 if (!jbitmap.obj())
124 return false;
126 if (!RasterizeIntoBitmap(env,
127 jbitmap,
128 auxiliary_bitmap_rect.x() - scroll_correction.x(),
129 auxiliary_bitmap_rect.y() - scroll_correction.y(),
130 render_source)) {
131 return false;
134 Java_JavaBrowserViewRendererHelper_drawBitmapIntoCanvas(
135 env,
136 jbitmap.obj(),
137 jcanvas.obj(),
138 auxiliary_bitmap_rect.x(),
139 auxiliary_bitmap_rect.y());
140 return true;
143 bool RegisterJavaBrowserViewRendererHelper(JNIEnv* env) {
144 return RegisterNativesImpl(env);
147 bool JavaBrowserViewRendererHelper::RasterizeIntoBitmap(
148 JNIEnv* env,
149 const JavaRef<jobject>& jbitmap,
150 int scroll_x,
151 int scroll_y,
152 const JavaBrowserViewRendererHelper::RenderMethod& renderer) {
153 DCHECK(jbitmap.obj());
155 AndroidBitmapInfo bitmap_info;
156 if (AndroidBitmap_getInfo(env, jbitmap.obj(), &bitmap_info) < 0) {
157 LOG(ERROR) << "Error getting java bitmap info.";
158 return false;
161 void* pixels = NULL;
162 if (AndroidBitmap_lockPixels(env, jbitmap.obj(), &pixels) < 0) {
163 LOG(ERROR) << "Error locking java bitmap pixels.";
164 return false;
167 bool succeeded;
169 SkImageInfo info =
170 SkImageInfo::MakeN32Premul(bitmap_info.width, bitmap_info.height);
171 SkBitmap bitmap;
172 bitmap.installPixels(info, pixels, bitmap_info.stride);
174 SkCanvas canvas(bitmap);
175 canvas.translate(-scroll_x, -scroll_y);
176 succeeded = renderer.Run(&canvas);
179 if (AndroidBitmap_unlockPixels(env, jbitmap.obj()) < 0) {
180 LOG(ERROR) << "Error unlocking java bitmap pixels.";
181 return false;
184 return succeeded;
187 } // namespace android_webview