OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 |
| 5 package org.chromium.content.browser; |
| 6 |
| 7 import android.graphics.Canvas; |
| 8 import android.graphics.Paint; |
| 9 import android.graphics.Rect; |
| 10 import android.graphics.Bitmap; |
| 11 import android.graphics.RectF; |
| 12 |
| 13 import java.util.HashSet; |
| 14 import java.util.Set; |
| 15 |
| 16 import org.chromium.base.CalledByNative; |
| 17 |
| 18 class JNIHelper { |
| 19 // Holder for content backing store bitmaps of all ChromeViews. Note that |
| 20 // when in software mode, this will be one of the main consumers of RAM in |
| 21 // the Java code. This data structure exists solely to prevent garbage |
| 22 // collection and expose the allocations to the DDMS tool. The backing |
| 23 // stores are actually used in C++ with weak global references. |
| 24 private static final Set<Bitmap> mBitmapHolder = new HashSet<Bitmap>(); |
| 25 |
| 26 @CalledByNative |
| 27 private static Bitmap createJavaBitmap(int w, int h, boolean keep) { |
| 28 Bitmap newBitmap = |
| 29 Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
| 30 |
| 31 if (keep) { |
| 32 mBitmapHolder.add(newBitmap); |
| 33 } |
| 34 |
| 35 return newBitmap; |
| 36 } |
| 37 |
| 38 @CalledByNative |
| 39 private static void deleteJavaBitmap(Bitmap b) { |
| 40 mBitmapHolder.remove(b); |
| 41 b.recycle(); |
| 42 } |
| 43 |
| 44 // Statics used transiently in paintJavaBitmapToJavaBitmap, but retained to
reduce |
| 45 // heap churn on each call to that method. |
| 46 static private Rect sSourceRect; |
| 47 static private RectF sDestRect; |
| 48 static private Canvas sCanvas; |
| 49 static private Bitmap sNullBitmap; |
| 50 static private Paint sPaint; |
| 51 |
| 52 @CalledByNative |
| 53 public static void paintJavaBitmapToJavaBitmap( |
| 54 Bitmap sourceBitmap, |
| 55 int sourceX, int sourceY, int sourceBottom, int sourceRight, |
| 56 Bitmap destBitmap, |
| 57 float destX, float destY, float destBottom, float destRight) { |
| 58 if (sCanvas == null) { |
| 59 sSourceRect = new Rect(); |
| 60 sDestRect = new RectF(); |
| 61 sCanvas = new Canvas(); |
| 62 sNullBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8); |
| 63 sPaint = new Paint(Paint.FILTER_BITMAP_FLAG); |
| 64 } |
| 65 sSourceRect.set(sourceX, sourceY, sourceRight, sourceBottom); |
| 66 sDestRect.set(destX, destY, destRight, destBottom); |
| 67 sCanvas.setBitmap(destBitmap); |
| 68 sCanvas.drawBitmap(sourceBitmap, sSourceRect, sDestRect, sPaint); |
| 69 sCanvas.setBitmap(sNullBitmap); // To release |destBitmap| reference. |
| 70 } |
| 71 } |
OLD | NEW |