Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 18b38883 authored by Angus Kong's avatar Angus Kong
Browse files

Guard tiles by synchronized block.

Tiles might be touched in different threads.

bug:7490351
Change-Id: Ie30d43db90cf63f7a6ec6438faabfcae8bca151a
parent 139efdbc
Loading
Loading
Loading
Loading
+52 −40
Original line number Diff line number Diff line
@@ -55,7 +55,8 @@ public class TiledTexture implements Texture {

    private int mUploadIndex = 0;

    private final Tile[] mTiles;
    private final Tile[] mTiles;  // Can be modified in different threads.
                                  // Should be protected by "synchronized."
    private final int mWidth;
    private final int mHeight;
    private final RectF mSrcRect = new RectF();
@@ -171,6 +172,7 @@ public class TiledTexture implements Texture {
    private boolean uploadNextTile(GLCanvas canvas) {
        if (mUploadIndex == mTiles.length) return true;

        synchronized (mTiles) {
            Tile next = mTiles[mUploadIndex++];

            // Make sure tile has not already been recycled by the time
@@ -185,6 +187,7 @@ public class TiledTexture implements Texture {
                // been uploaded.
                if (!hasBeenLoad) next.draw(canvas, 0, 0);
            }
        }
        return mUploadIndex == mTiles.length;
    }

@@ -212,11 +215,14 @@ public class TiledTexture implements Texture {
        return mUploadIndex == mTiles.length;
    }

    // Can be called in UI thread.
    public void recycle() {
        synchronized (mTiles) {
            for (int i = 0, n = mTiles.length; i < n; ++i) {
                freeTile(mTiles[i]);
            }
        }
    }

    public static void freeResources() {
        sUploadBitmap = null;
@@ -265,6 +271,7 @@ public class TiledTexture implements Texture {
        RectF dest = mDestRect;
        float scaleX = (float) width / mWidth;
        float scaleY = (float) height / mHeight;
        synchronized (mTiles) {
            for (int i = 0, n = mTiles.length; i < n; ++i) {
                Tile t = mTiles[i];
                src.set(0, 0, t.contentWidth, t.contentHeight);
@@ -274,6 +281,7 @@ public class TiledTexture implements Texture {
                canvas.drawMixed(t, color, ratio, mSrcRect, mDestRect);
            }
        }
    }

    // Draws the texture on to the specified rectangle.
    @Override
@@ -282,6 +290,7 @@ public class TiledTexture implements Texture {
        RectF dest = mDestRect;
        float scaleX = (float) width / mWidth;
        float scaleY = (float) height / mHeight;
        synchronized (mTiles) {
            for (int i = 0, n = mTiles.length; i < n; ++i) {
                Tile t = mTiles[i];
                src.set(0, 0, t.contentWidth, t.contentHeight);
@@ -291,6 +300,7 @@ public class TiledTexture implements Texture {
                canvas.drawTexture(t, mSrcRect, mDestRect);
            }
        }
    }

    // Draws a sub region of this texture on to the specified rectangle.
    public void draw(GLCanvas canvas, RectF source, RectF target) {
@@ -303,6 +313,7 @@ public class TiledTexture implements Texture {
        float scaleX = target.width() / source.width();
        float scaleY = target.height() / source.height();

        synchronized (mTiles) {
            for (int i = 0, n = mTiles.length; i < n; ++i) {
                Tile t = mTiles[i];
                src.set(0, 0, t.contentWidth, t.contentHeight);
@@ -313,6 +324,7 @@ public class TiledTexture implements Texture {
                canvas.drawTexture(t, src, dest);
            }
        }
    }

    @Override
    public int getWidth() {