mobile/android/base/gfx/BufferedCairoImage.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.gfx;
michael@0 7
michael@0 8 import org.mozilla.gecko.mozglue.DirectBufferAllocator;
michael@0 9
michael@0 10 import android.graphics.Bitmap;
michael@0 11 import android.util.Log;
michael@0 12
michael@0 13 import java.nio.ByteBuffer;
michael@0 14
michael@0 15 /** A Cairo image that simply saves a buffer of pixel data. */
michael@0 16 public class BufferedCairoImage extends CairoImage {
michael@0 17 private ByteBuffer mBuffer;
michael@0 18 private IntSize mSize;
michael@0 19 private int mFormat;
michael@0 20
michael@0 21 private static String LOGTAG = "GeckoBufferedCairoImage";
michael@0 22
michael@0 23 /** Creates a buffered Cairo image from a byte buffer. */
michael@0 24 public BufferedCairoImage(ByteBuffer inBuffer, int inWidth, int inHeight, int inFormat) {
michael@0 25 setBuffer(inBuffer, inWidth, inHeight, inFormat);
michael@0 26 }
michael@0 27
michael@0 28 /** Creates a buffered Cairo image from an Android bitmap. */
michael@0 29 public BufferedCairoImage(Bitmap bitmap) {
michael@0 30 setBitmap(bitmap);
michael@0 31 }
michael@0 32
michael@0 33 private synchronized void freeBuffer() {
michael@0 34 mBuffer = DirectBufferAllocator.free(mBuffer);
michael@0 35 }
michael@0 36
michael@0 37 @Override
michael@0 38 public void destroy() {
michael@0 39 try {
michael@0 40 freeBuffer();
michael@0 41 } catch (Exception ex) {
michael@0 42 Log.e(LOGTAG, "error clearing buffer: ", ex);
michael@0 43 }
michael@0 44 }
michael@0 45
michael@0 46 @Override
michael@0 47 public ByteBuffer getBuffer() { return mBuffer; }
michael@0 48 @Override
michael@0 49 public IntSize getSize() { return mSize; }
michael@0 50 @Override
michael@0 51 public int getFormat() { return mFormat; }
michael@0 52
michael@0 53
michael@0 54 public void setBuffer(ByteBuffer buffer, int width, int height, int format) {
michael@0 55 freeBuffer();
michael@0 56 mBuffer = buffer;
michael@0 57 mSize = new IntSize(width, height);
michael@0 58 mFormat = format;
michael@0 59 }
michael@0 60
michael@0 61 public void setBitmap(Bitmap bitmap) {
michael@0 62 mFormat = CairoUtils.bitmapConfigToCairoFormat(bitmap.getConfig());
michael@0 63 mSize = new IntSize(bitmap.getWidth(), bitmap.getHeight());
michael@0 64
michael@0 65 int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat);
michael@0 66 mBuffer = DirectBufferAllocator.allocate(mSize.getArea() * bpp);
michael@0 67 bitmap.copyPixelsToBuffer(mBuffer.asIntBuffer());
michael@0 68 }
michael@0 69 }

mercurial