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 package org.chromium.android_webview;
7 import android.graphics.Bitmap;
8 import android.graphics.Canvas;
9 import android.graphics.Color;
10 import android.graphics.Picture;
11 import android.util.Log;
12 import android.util.LruCache;
14 import org.chromium.base.CalledByNative;
15 import org.chromium.base.JNINamespace;
16 import org.chromium.content.common.TraceEvent;
19 * Provides auxiliary methods related to Picture objects and native SkPictures.
21 @JNINamespace("android_webview")
22 public class JavaBrowserViewRendererHelper {
23 private static final String LOGTAG = "JavaBrowserViewRendererHelper";
25 // Until the full HW path is ready, we limit to 5 AwContents on the screen at once.
26 private static LruCache<Integer, Bitmap> sBitmapCache = new LruCache<Integer, Bitmap>(5);
29 * Provides a Bitmap object with a given width and height used for auxiliary rasterization.
30 * |canvas| is optional and if supplied indicates the Canvas that this Bitmap will be
31 * drawn into. Note the Canvas will not be modified in any way. If |ownerKey| is non-zero
32 * the Bitmap will be cached in sBitmapCache for future use.
35 private static Bitmap createBitmap(int width, int height, Canvas canvas, int ownerKey) {
37 // When drawing into a Canvas, there is a maximum size imposed
38 // on Bitmaps that can be drawn. Respect that limit.
39 width = Math.min(width, canvas.getMaximumBitmapWidth());
40 height = Math.min(height, canvas.getMaximumBitmapHeight());
42 Bitmap bitmap = sBitmapCache.get(ownerKey);
43 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
45 bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
46 } catch (OutOfMemoryError e) {
47 android.util.Log.w(LOGTAG, "Error allocating bitmap");
51 if (sBitmapCache.size() > AwContents.getNativeInstanceCount()) {
52 sBitmapCache.evictAll();
54 sBitmapCache.put(ownerKey, bitmap);
61 * Draws a provided bitmap into a canvas.
62 * Used for convenience from the native side and other static helper methods.
65 private static void drawBitmapIntoCanvas(Bitmap bitmap, Canvas canvas, int x, int y) {
66 canvas.drawBitmap(bitmap, x, y, null);
70 * Creates a new Picture that records drawing a provided bitmap.
71 * Will return an empty Picture if the Bitmap is null.
74 private static Picture recordBitmapIntoPicture(Bitmap bitmap) {
75 Picture picture = new Picture();
77 Canvas recordingCanvas = picture.beginRecording(bitmap.getWidth(), bitmap.getHeight());
78 drawBitmapIntoCanvas(bitmap, recordingCanvas, 0, 0);
79 picture.endRecording();
84 // Should never be instantiated.
85 private JavaBrowserViewRendererHelper() {