From d646e43cc088c70c2d8c2decaf1590ede0f3e898 Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Tue, 8 Jul 2014 16:43:31 +0200 Subject: LOAndroid3: CairoImage, BufferedCairoImage update to latest code Change-Id: If6b7e63a89c13015d4a96fae1862c9ccf6b04237 --- .../org/mozilla/gecko/gfx/BufferedCairoImage.java | 89 ++++++++-------------- .../src/java/org/mozilla/gecko/gfx/CairoImage.java | 40 ++-------- .../org/mozilla/gecko/gfx/CheckerboardImage.java | 5 ++ 3 files changed, 43 insertions(+), 91 deletions(-) (limited to 'android') diff --git a/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/BufferedCairoImage.java b/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/BufferedCairoImage.java index 7a98be339c0c..ce836fe07e8f 100644 --- a/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/BufferedCairoImage.java +++ b/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/BufferedCairoImage.java @@ -1,90 +1,51 @@ /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- - * ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Mozilla Android code. - * - * The Initial Developer of the Original Code is Mozilla Foundation. - * Portions created by the Initial Developer are Copyright (C) 2009-2010 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Patrick Walton - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.mozilla.gecko.gfx; + import android.graphics.Bitmap; +import android.util.Log; import org.libreoffice.DirectBufferAllocator; import java.nio.ByteBuffer; -//import org.mozilla.gecko.GeckoAppShell; - /** * A Cairo image that simply saves a buffer of pixel data. */ public class BufferedCairoImage extends CairoImage { + private static String LOGTAG = "GeckoBufferedCairoImage"; private ByteBuffer mBuffer; private IntSize mSize; private int mFormat; - private boolean mNeedToFreeBuffer = false; /** * Creates a buffered Cairo image from a byte buffer. */ public BufferedCairoImage(ByteBuffer inBuffer, int inWidth, int inHeight, int inFormat) { - mBuffer = inBuffer; - mSize = new IntSize(inWidth, inHeight); - mFormat = inFormat; + setBuffer(inBuffer, inWidth, inHeight, inFormat); } /** * Creates a buffered Cairo image from an Android bitmap. */ public BufferedCairoImage(Bitmap bitmap) { - mFormat = CairoUtils.bitmapConfigToCairoFormat(bitmap.getConfig()); - mSize = new IntSize(bitmap.getWidth(), bitmap.getHeight()); - mNeedToFreeBuffer = true; - // XXX Why is this * 4? Shouldn't it depend on mFormat? - mBuffer = DirectBufferAllocator.allocate(mSize.getArea() * 4); + setBitmap(bitmap); + } - bitmap.copyPixelsToBuffer(mBuffer.asIntBuffer()); + private synchronized void freeBuffer() { + mBuffer = DirectBufferAllocator.free(mBuffer); } - protected void finalize() throws Throwable { + @Override + public void destroy() { try { - if (mNeedToFreeBuffer && mBuffer != null) { - DirectBufferAllocator.free(mBuffer); - } - mNeedToFreeBuffer = false; - mBuffer = null; - } finally { - super.finalize(); + freeBuffer(); + } catch (Exception ex) { + Log.e(LOGTAG, "error clearing buffer: ", ex); } } @@ -102,5 +63,21 @@ public class BufferedCairoImage extends CairoImage { public int getFormat() { return mFormat; } -} + + public void setBuffer(ByteBuffer buffer, int width, int height, int format) { + freeBuffer(); + mBuffer = buffer; + mSize = new IntSize(width, height); + mFormat = format; + } + + public void setBitmap(Bitmap bitmap) { + mFormat = CairoUtils.bitmapConfigToCairoFormat(bitmap.getConfig()); + mSize = new IntSize(bitmap.getWidth(), bitmap.getHeight()); + + int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat); + mBuffer = DirectBufferAllocator.allocate(mSize.getArea() * bpp); + bitmap.copyPixelsToBuffer(mBuffer.asIntBuffer()); + } +} diff --git a/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/CairoImage.java b/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/CairoImage.java index 06c389dd0524..5a18a4bb1995 100644 --- a/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/CairoImage.java +++ b/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/CairoImage.java @@ -1,39 +1,7 @@ /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- - * ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Mozilla Android code. - * - * The Initial Developer of the Original Code is Mozilla Foundation. - * Portions created by the Initial Developer are Copyright (C) 2009-2010 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Patrick Walton - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.mozilla.gecko.gfx; @@ -45,6 +13,8 @@ import java.nio.ByteBuffer; public abstract class CairoImage { public abstract ByteBuffer getBuffer(); + public abstract void destroy(); + public abstract IntSize getSize(); public abstract int getFormat(); diff --git a/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/CheckerboardImage.java b/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/CheckerboardImage.java index 05a4d57a7346..7af94fa7052a 100644 --- a/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/CheckerboardImage.java +++ b/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/CheckerboardImage.java @@ -159,6 +159,11 @@ public class CheckerboardImage extends CairoImage { return mBuffer; } + @Override + public void destroy() { + + } + @Override public IntSize getSize() { return new IntSize(SIZE, SIZE); -- cgit