Avoid potential negative array index access to cached text.
[LibreOffice.git] / android / source / src / java / org / mozilla / gecko / gfx / BufferedCairoImage.java
bloba616fcc4da43f8ce5d1951b038088c621dee2b59
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;
16 /**
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;
23 private int mFormat;
25 /**
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);
32 /**
33 * Creates a buffered Cairo image from an Android bitmap.
35 public BufferedCairoImage(Bitmap bitmap) {
36 setBitmap(bitmap);
39 private synchronized void freeBuffer() {
40 mBuffer = DirectBufferAllocator.free(mBuffer);
43 @Override
44 public void destroy() {
45 try {
46 freeBuffer();
47 } catch (Exception ex) {
48 Log.e(LOGTAG, "error clearing buffer: ", ex);
52 @Override
53 public ByteBuffer getBuffer() {
54 return mBuffer;
57 @Override
58 public IntSize getSize() {
59 return mSize;
62 @Override
63 public int getFormat() {
64 return mFormat;
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;
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());