1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/gfx/BufferedCairoImage.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,69 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.gfx; 1.10 + 1.11 +import org.mozilla.gecko.mozglue.DirectBufferAllocator; 1.12 + 1.13 +import android.graphics.Bitmap; 1.14 +import android.util.Log; 1.15 + 1.16 +import java.nio.ByteBuffer; 1.17 + 1.18 +/** A Cairo image that simply saves a buffer of pixel data. */ 1.19 +public class BufferedCairoImage extends CairoImage { 1.20 + private ByteBuffer mBuffer; 1.21 + private IntSize mSize; 1.22 + private int mFormat; 1.23 + 1.24 + private static String LOGTAG = "GeckoBufferedCairoImage"; 1.25 + 1.26 + /** Creates a buffered Cairo image from a byte buffer. */ 1.27 + public BufferedCairoImage(ByteBuffer inBuffer, int inWidth, int inHeight, int inFormat) { 1.28 + setBuffer(inBuffer, inWidth, inHeight, inFormat); 1.29 + } 1.30 + 1.31 + /** Creates a buffered Cairo image from an Android bitmap. */ 1.32 + public BufferedCairoImage(Bitmap bitmap) { 1.33 + setBitmap(bitmap); 1.34 + } 1.35 + 1.36 + private synchronized void freeBuffer() { 1.37 + mBuffer = DirectBufferAllocator.free(mBuffer); 1.38 + } 1.39 + 1.40 + @Override 1.41 + public void destroy() { 1.42 + try { 1.43 + freeBuffer(); 1.44 + } catch (Exception ex) { 1.45 + Log.e(LOGTAG, "error clearing buffer: ", ex); 1.46 + } 1.47 + } 1.48 + 1.49 + @Override 1.50 + public ByteBuffer getBuffer() { return mBuffer; } 1.51 + @Override 1.52 + public IntSize getSize() { return mSize; } 1.53 + @Override 1.54 + public int getFormat() { return mFormat; } 1.55 + 1.56 + 1.57 + public void setBuffer(ByteBuffer buffer, int width, int height, int format) { 1.58 + freeBuffer(); 1.59 + mBuffer = buffer; 1.60 + mSize = new IntSize(width, height); 1.61 + mFormat = format; 1.62 + } 1.63 + 1.64 + public void setBitmap(Bitmap bitmap) { 1.65 + mFormat = CairoUtils.bitmapConfigToCairoFormat(bitmap.getConfig()); 1.66 + mSize = new IntSize(bitmap.getWidth(), bitmap.getHeight()); 1.67 + 1.68 + int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat); 1.69 + mBuffer = DirectBufferAllocator.allocate(mSize.getArea() * bpp); 1.70 + bitmap.copyPixelsToBuffer(mBuffer.asIntBuffer()); 1.71 + } 1.72 +}