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/. */
6 package org
.mozilla
.gecko
.gfx
;
9 import android
.graphics
.Bitmap
;
10 import android
.util
.Log
;
12 import org
.libreoffice
.kit
.DirectBufferAllocator
;
14 import java
.nio
.ByteBuffer
;
17 * A Cairo image that simply saves a buffer of pixel data.
19 public class BufferedCairoImage
extends CairoImage
{
20 private static String LOGTAG
= "GeckoBufferedCairoImage";
21 private ByteBuffer mBuffer
;
22 private IntSize mSize
;
26 * Creates a buffered Cairo image from a byte buffer.
28 public BufferedCairoImage(ByteBuffer inBuffer
, int inWidth
, int inHeight
, int inFormat
) {
29 setBuffer(inBuffer
, inWidth
, inHeight
, inFormat
);
33 * Creates a buffered Cairo image from an Android bitmap.
35 public BufferedCairoImage(Bitmap bitmap
) {
39 private synchronized void freeBuffer() {
40 mBuffer
= DirectBufferAllocator
.free(mBuffer
);
44 public void destroy() {
47 } catch (Exception ex
) {
48 Log
.e(LOGTAG
, "error clearing buffer: ", ex
);
53 public ByteBuffer
getBuffer() {
58 public IntSize
getSize() {
63 public int getFormat() {
68 public void setBuffer(ByteBuffer buffer
, int width
, int height
, int format
) {
71 mSize
= new IntSize(width
, height
);
75 public void setBitmap(Bitmap bitmap
) {
76 mFormat
= CairoUtils
.bitmapConfigToCairoFormat(bitmap
.getConfig());
77 mSize
= new IntSize(bitmap
.getWidth(), bitmap
.getHeight());
79 int bpp
= CairoUtils
.bitsPerPixelForCairoFormat(mFormat
) / 8;
80 mBuffer
= DirectBufferAllocator
.allocate(mSize
.getArea() * bpp
);
81 bitmap
.copyPixelsToBuffer(mBuffer
.asIntBuffer());