1// Copyright 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.
4
5package org.chromium.android_webview;
6
7import android.graphics.Bitmap;
8import android.graphics.Canvas;
9
10import org.chromium.base.CalledByNative;
11import org.chromium.base.JNINamespace;
12
13/**
14 * Provides auxiliary methods related to Picture objects and native SkPictures.
15 */
16@JNINamespace("android_webview")
17public class JavaBrowserViewRendererHelper {
18    private static final String LOGTAG = "JavaBrowserViewRendererHelper";
19
20    /**
21     * Provides a Bitmap object with a given width and height used for auxiliary rasterization.
22     * |canvas| is optional and if supplied indicates the Canvas that this Bitmap will be
23     * drawn into. Note the Canvas will not be modified in any way.
24     */
25    @CalledByNative
26    private static Bitmap createBitmap(int width, int height, Canvas canvas) {
27        if (canvas != null) {
28            // When drawing into a Canvas, there is a maximum size imposed
29            // on Bitmaps that can be drawn. Respect that limit.
30            width = Math.min(width, canvas.getMaximumBitmapWidth());
31            height = Math.min(height, canvas.getMaximumBitmapHeight());
32        }
33        Bitmap bitmap = null;
34        try {
35            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
36        } catch (OutOfMemoryError e) {
37            android.util.Log.w(LOGTAG, "Error allocating bitmap");
38        }
39        return bitmap;
40    }
41
42    /**
43     * Draws a provided bitmap into a canvas.
44     * Used for convenience from the native side and other static helper methods.
45     */
46    @CalledByNative
47    private static void drawBitmapIntoCanvas(
48            Bitmap bitmap, Canvas canvas, int scrollX, int scrollY) {
49        canvas.translate(scrollX, scrollY);
50        canvas.drawBitmap(bitmap, 0, 0, null);
51    }
52
53    // Should never be instantiated.
54    private JavaBrowserViewRendererHelper() {
55    }
56}
57