Commit e9e70041 authored by Kartikaya Gupta's avatar Kartikaya Gupta
Browse files

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

parent a2af2b05
Loading
Loading
Loading
Loading
+12 −7
Original line number Diff line number Diff line
@@ -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,12 +60,16 @@ public class BufferedCairoImage extends CairoImage {
        setBitmap(bitmap);
    }

     protected void finalize() throws Throwable {
        try {
    private void freeBuffer() {
        if (mNeedToFreeBuffer && mBuffer != null)
            GeckoAppShell.freeDirectBuffer(mBuffer);
        mNeedToFreeBuffer = false;
        mBuffer = null;
    }

    protected void finalize() throws Throwable {
        try {
            freeBuffer();
        } finally {
            super.finalize();
        }
@@ -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;
+1 −0
Original line number Diff line number Diff line
@@ -146,6 +146,7 @@ public class CheckerboardImage extends CairoImage {
        try {
            if (mBuffer != null) {
                GeckoAppShell.freeDirectBuffer(mBuffer);
                mBuffer = null;
            }
        } finally {
            super.finalize();
+18 −4
Original line number Diff line number Diff line
@@ -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) {
+6 −2
Original line number Diff line number Diff line
@@ -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();
            }
+1 −14
Original line number Diff line number Diff line
@@ -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) {
Loading