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