Bug 753334 - Audit and clean up code that deals with allocating/freeing direct buffers. r=Cwiiis

This commit is contained in:
Kartikaya Gupta 2012-05-10 09:46:53 -04:00
parent 69a8d9d976
commit 8b2a37c104
8 changed files with 60 additions and 70 deletions

View File

@ -48,7 +48,7 @@ public class BufferedCairoImage extends CairoImage {
private ByteBuffer mBuffer;
private IntSize mSize;
private int mFormat;
private boolean mNeedToFreeBuffer = false;
private boolean mNeedToFreeBuffer;
/** Creates a buffered Cairo image from a byte buffer. */
public BufferedCairoImage(ByteBuffer inBuffer, int inWidth, int inHeight, int inFormat) {
@ -60,18 +60,22 @@ public class BufferedCairoImage extends CairoImage {
setBitmap(bitmap);
}
protected void finalize() throws Throwable {
private void freeBuffer() {
if (mNeedToFreeBuffer && mBuffer != null)
GeckoAppShell.freeDirectBuffer(mBuffer);
mNeedToFreeBuffer = false;
mBuffer = null;
}
protected void finalize() throws Throwable {
try {
if (mNeedToFreeBuffer && mBuffer != null)
GeckoAppShell.freeDirectBuffer(mBuffer);
mNeedToFreeBuffer = false;
mBuffer = null;
freeBuffer();
} finally {
super.finalize();
}
}
@Override
@Override
public ByteBuffer getBuffer() { return mBuffer; }
@Override
public IntSize getSize() { return mSize; }
@ -80,6 +84,7 @@ public class BufferedCairoImage extends CairoImage {
public void setBuffer(ByteBuffer buffer, int width, int height, int format) {
freeBuffer();
mBuffer = buffer;
mSize = new IntSize(width, height);
mFormat = format;

View File

@ -146,6 +146,7 @@ public class CheckerboardImage extends CairoImage {
try {
if (mBuffer != null) {
GeckoAppShell.freeDirectBuffer(mBuffer);
mBuffer = null;
}
} finally {
super.finalize();

View File

@ -97,7 +97,8 @@ public class LayerRenderer implements GLSurfaceView.Renderer {
private final ScrollbarLayer mHorizScrollLayer;
private final ScrollbarLayer mVertScrollLayer;
private final FadeRunnable mFadeRunnable;
private final FloatBuffer mCoordBuffer;
private ByteBuffer mCoordByteBuffer;
private FloatBuffer mCoordBuffer;
private RenderContext mLastPageContext;
private int mMaxTextureSize;
private int mBackgroundColor;
@ -214,9 +215,22 @@ public class LayerRenderer implements GLSurfaceView.Renderer {
// Initialize the FloatBuffer that will be used to store all vertices and texture
// coordinates in draw() commands.
ByteBuffer byteBuffer = GeckoAppShell.allocateDirectBuffer(COORD_BUFFER_SIZE * 4);
byteBuffer.order(ByteOrder.nativeOrder());
mCoordBuffer = byteBuffer.asFloatBuffer();
mCoordByteBuffer = GeckoAppShell.allocateDirectBuffer(COORD_BUFFER_SIZE * 4);
mCoordByteBuffer.order(ByteOrder.nativeOrder());
mCoordBuffer = mCoordByteBuffer.asFloatBuffer();
}
@Override
protected void finalize() throws Throwable {
try {
if (mCoordByteBuffer != null) {
GeckoAppShell.freeDirectBuffer(mCoordByteBuffer);
mCoordByteBuffer = null;
mCoordBuffer = null;
}
} finally {
super.finalize();
}
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {

View File

@ -99,14 +99,18 @@ public class ScreenshotLayer extends SingleTileLayer {
/** Creates a buffered Cairo image from a byte buffer. */
public ScreenshotImage(ByteBuffer inBuffer, int inWidth, int inHeight, int inFormat) {
mBuffer = inBuffer; mSize = new IntSize(inWidth, inHeight); mFormat = inFormat;
mBuffer = inBuffer;
mSize = new IntSize(inWidth, inHeight);
mFormat = inFormat;
}
@Override
protected void finalize() throws Throwable {
try {
if (mBuffer != null)
if (mBuffer != null) {
GeckoAppShell.freeDirectBuffer(mBuffer);
mBuffer = null;
}
} finally {
super.finalize();
}

View File

@ -65,11 +65,9 @@ public class ScrollbarLayer extends TileLayer {
private static final int CAP_RADIUS = (BAR_SIZE / 2);
private final boolean mVertical;
private final ByteBuffer mBuffer;
private final Bitmap mBitmap;
private final Canvas mCanvas;
private float mOpacity;
private boolean mFinalized = false;
private LayerRenderer mRenderer;
private int mProgram;
@ -143,7 +141,6 @@ public class ScrollbarLayer extends TileLayer {
private ScrollbarLayer(LayerRenderer renderer, CairoImage image, boolean vertical, ByteBuffer buffer) {
super(image, TileLayer.PaintMode.NORMAL);
mVertical = vertical;
mBuffer = buffer;
mRenderer = renderer;
IntSize size = image.getSize();
@ -159,17 +156,7 @@ public class ScrollbarLayer extends TileLayer {
mCanvas.drawColor(Color.argb(0, 0, 0, 0), PorterDuff.Mode.CLEAR);
mCanvas.drawCircle(CAP_RADIUS, CAP_RADIUS, CAP_RADIUS, foregroundPaint);
mBitmap.copyPixelsToBuffer(mBuffer.asIntBuffer());
}
protected void finalize() throws Throwable {
try {
if (!mFinalized && mBuffer != null)
GeckoAppShell.freeDirectBuffer(mBuffer);
mFinalized = true;
} finally {
super.finalize();
}
mBitmap.copyPixelsToBuffer(buffer.asIntBuffer());
}
public static ScrollbarLayer create(LayerRenderer renderer, boolean vertical) {

View File

@ -165,19 +165,23 @@ public class SurfaceTextureLayer extends Layer implements SurfaceTexture.OnFrame
@Override
protected void finalize() throws Throwable {
if (mSurfaceTexture != null) {
try {
SurfaceTexture.class.getDeclaredMethod("release").invoke(mSurfaceTexture);
} catch (NoSuchMethodException nsme) {
Log.e(LOGTAG, "error finding release method on mSurfaceTexture", nsme);
} catch (IllegalAccessException iae) {
Log.e(LOGTAG, "error invoking release method on mSurfaceTexture", iae);
} catch (Exception e) {
Log.e(LOGTAG, "some other exception while invoking release method on mSurfaceTexture", e);
try {
if (mSurfaceTexture != null) {
try {
SurfaceTexture.class.getDeclaredMethod("release").invoke(mSurfaceTexture);
} catch (NoSuchMethodException nsme) {
Log.e(LOGTAG, "error finding release method on mSurfaceTexture", nsme);
} catch (IllegalAccessException iae) {
Log.e(LOGTAG, "error invoking release method on mSurfaceTexture", iae);
} catch (Exception e) {
Log.e(LOGTAG, "some other exception while invoking release method on mSurfaceTexture", e);
}
}
if (mTextureId > 0)
TextureReaper.get().add(mTextureId);
} finally {
super.finalize();
}
if (mTextureId > 0)
TextureReaper.get().add(mTextureId);
}
@Override

View File

@ -55,9 +55,8 @@ import java.nio.IntBuffer;
* Draws text on a layer. This is used for the frame rate meter.
*/
public class TextLayer extends SingleTileLayer {
private final ByteBuffer mBuffer;
private final ByteBuffer mBuffer; // this buffer is owned by the BufferedCairoImage
private final IntSize mSize;
private boolean mFinalized = false;
/*
* This awkward pattern is necessary due to Java's restrictions on when one can call superclass
@ -70,16 +69,6 @@ public class TextLayer extends SingleTileLayer {
renderText(text);
}
protected void finalize() throws Throwable {
try {
if (!mFinalized && mBuffer != null)
GeckoAppShell.freeDirectBuffer(mBuffer);
mFinalized = true;
} finally {
super.finalize();
}
}
public static TextLayer create(IntSize size, String text) {
ByteBuffer buffer = GeckoAppShell.allocateDirectBuffer(size.width * size.height * 4);
BufferedCairoImage image = new BufferedCairoImage(buffer, size.width, size.height,

View File

@ -81,8 +81,12 @@ public abstract class TileLayer extends Layer {
@Override
protected void finalize() throws Throwable {
if (mTextureIDs != null)
TextureReaper.get().add(mTextureIDs);
try {
if (mTextureIDs != null)
TextureReaper.get().add(mTextureIDs);
} finally {
super.finalize();
}
}
public void setPaintMode(PaintMode mode) {
@ -182,24 +186,6 @@ public abstract class TileLayer extends Layer {
// Our texture has been expanded to the next power of two.
// XXX We probably never want to take this path, so throw an exception.
throw new RuntimeException("Buffer/image size mismatch in TileLayer!");
/*
int bpp = CairoUtils.bitsPerPixelForCairoFormat(cairoFormat)/8;
ByteBuffer tempBuffer =
GeckoAppShell.allocateDirectBuffer(mSize.width * mSize.height * bpp);
for (int y = 0; y < bufferSize.height; y++) {
tempBuffer.position(y * mSize.width * bpp);
imageBuffer.limit((y + 1) * bufferSize.width * bpp);
imageBuffer.position(y * bufferSize.width * bpp);
tempBuffer.put(imageBuffer);
}
imageBuffer.position(0);
tempBuffer.position(0);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, glInfo.internalFormat, mSize.width,
mSize.height, 0, glInfo.format, glInfo.type, tempBuffer);
GeckoAppShell.freeDirectBuffer(tempBuffer);
*/
}
}