diff --git a/vtm/src/org/oscim/layers/tile/bitmap/BitmapTileLayer.java b/vtm/src/org/oscim/layers/tile/bitmap/BitmapTileLayer.java index 9d192109..fca6b19e 100644 --- a/vtm/src/org/oscim/layers/tile/bitmap/BitmapTileLayer.java +++ b/vtm/src/org/oscim/layers/tile/bitmap/BitmapTileLayer.java @@ -104,10 +104,16 @@ public class BitmapTileLayer extends TileLayer { return new BitmapTileLoader(this, mTileSource); } + @Override + public void onDetach() { + super.onDetach(); + pool.clear(); + } + final static int POOL_FILL = 40; /** pool shared by TextLayers */ - final static TexturePool pool = new TexturePool(POOL_FILL) { + final TexturePool pool = new TexturePool(POOL_FILL) { // int sum = 0; // diff --git a/vtm/src/org/oscim/layers/tile/bitmap/BitmapTileLoader.java b/vtm/src/org/oscim/layers/tile/bitmap/BitmapTileLoader.java index ad4c2905..7035585e 100644 --- a/vtm/src/org/oscim/layers/tile/bitmap/BitmapTileLoader.java +++ b/vtm/src/org/oscim/layers/tile/bitmap/BitmapTileLoader.java @@ -21,7 +21,6 @@ import static org.oscim.layers.tile.MapTile.State.CANCEL; import org.oscim.backend.canvas.Bitmap; import org.oscim.core.Tile; import org.oscim.layers.tile.MapTile; -import org.oscim.layers.tile.TileLayer; import org.oscim.layers.tile.TileLoader; import org.oscim.renderer.elements.BitmapLayer; import org.oscim.renderer.elements.ElementLayers; @@ -35,10 +34,12 @@ public class BitmapTileLoader extends TileLoader { protected static final Logger log = LoggerFactory.getLogger(BitmapTileLoader.class); private final ITileDataSource mTileDataSource; + private final BitmapTileLayer mLayer; - public BitmapTileLoader(TileLayer tileLayer, TileSource tileSource) { + public BitmapTileLoader(BitmapTileLayer tileLayer, TileSource tileSource) { super(tileLayer.getManager()); mTileDataSource = tileSource.getDataSource(); + mLayer = tileLayer; } @Override @@ -58,7 +59,7 @@ public class BitmapTileLoader extends TileLoader { return; BitmapLayer l = new BitmapLayer(false); - l.setBitmap(bitmap, Tile.SIZE, Tile.SIZE, BitmapTileLayer.pool); + l.setBitmap(bitmap, Tile.SIZE, Tile.SIZE, mLayer.pool); ElementLayers layers = new ElementLayers(); layers.setTextureLayers(l); diff --git a/vtm/src/org/oscim/renderer/MapRenderer.java b/vtm/src/org/oscim/renderer/MapRenderer.java index f72e6734..58535580 100644 --- a/vtm/src/org/oscim/renderer/MapRenderer.java +++ b/vtm/src/org/oscim/renderer/MapRenderer.java @@ -27,6 +27,7 @@ import org.oscim.backend.GLAdapter; import org.oscim.backend.canvas.Color; import org.oscim.map.Map; import org.oscim.renderer.elements.ElementLayers; +import org.oscim.renderer.elements.TextureItem; import org.oscim.utils.pool.Inlist; import org.oscim.utils.pool.Pool; import org.slf4j.Logger; @@ -174,6 +175,7 @@ public class MapRenderer { draw(); mBufferPool.releaseBuffers(); + TextureItem.disposeTextures(); } private void draw() { diff --git a/vtm/src/org/oscim/renderer/elements/TextureItem.java b/vtm/src/org/oscim/renderer/elements/TextureItem.java index 1ebe8a07..ae79677a 100644 --- a/vtm/src/org/oscim/renderer/elements/TextureItem.java +++ b/vtm/src/org/oscim/renderer/elements/TextureItem.java @@ -144,8 +144,6 @@ public class TextureItem extends Inlist { } public static class TexturePool extends SyncPool { - - private final ArrayList mTexDisposed = new ArrayList(); private final ArrayList mBitmaps = new ArrayList(10); private final int mHeight; @@ -226,13 +224,11 @@ public class TextureItem extends Inlist { @Override protected void freeItem(TextureItem t) { - - if (!t.ref) { - synchronized (mTexDisposed) { - if (t.id >= 0) { - mTexDisposed.add(Integer.valueOf(t.id)); - t.id = -1; - } + if (!t.ref && t.id >= 0) { + mTexCnt--; + synchronized (disposedTextures) { + disposedTextures.add(Integer.valueOf(t.id)); + t.id = -1; } } } @@ -253,19 +249,6 @@ public class TextureItem extends Inlist { if (t.bitmap == null) throw new RuntimeException("Missing bitmap for texture"); - synchronized (mTexDisposed) { - int size = mTexDisposed.size(); - if (size > 0) { - int[] tmp = new int[size]; - for (int i = 0; i < size; i++) - tmp[i] = mTexDisposed.get(i).intValue(); - - mTexDisposed.clear(); - GLUtils.glDeleteTextures(size, tmp); - mTexCnt -= size; - } - } - if (t.id < 0) { int[] textureIds = GLUtils.glGenTextures(1); t.id = textureIds[0]; @@ -318,10 +301,28 @@ public class TextureItem extends Inlist { /* Pool for not-pooled textures. Disposed items will only be released * on the GL-Thread and will not be put back in any pool. */ final static TexturePool NOPOOL = new TexturePool(0); + final static ArrayList disposedTextures = new ArrayList(); private static GL20 GL; static void init(GL20 gl) { GL = gl; } + + /** disposed textures are released by MapRenderer after each frame */ + public static void disposeTextures() { + synchronized (disposedTextures) { + + int size = disposedTextures.size(); + if (size > 0) { + int[] tmp = new int[size]; + for (int i = 0; i < size; i++) + tmp[i] = disposedTextures.get(i).intValue(); + + disposedTextures.clear(); + GLUtils.glDeleteTextures(size, tmp); + //mTexCnt -= size; + } + } + } } diff --git a/vtm/src/org/oscim/utils/pool/SyncPool.java b/vtm/src/org/oscim/utils/pool/SyncPool.java index 4a704d10..56e69b60 100644 --- a/vtm/src/org/oscim/utils/pool/SyncPool.java +++ b/vtm/src/org/oscim/utils/pool/SyncPool.java @@ -18,6 +18,7 @@ package org.oscim.utils.pool; import javax.annotation.CheckReturnValue; +@SuppressWarnings({ "rawtypes", "unchecked" }) public abstract class SyncPool> { protected final int mMaxFill; protected final boolean mClearItems; @@ -50,6 +51,13 @@ public abstract class SyncPool> { mPool = null; } + public synchronized void clear() { + while (mPool != null) { + freeItem(mPool); + mPool = (T) mPool.next; + } + } + /** * @param item * set initial state @@ -81,7 +89,6 @@ public abstract class SyncPool> { * Usage item = pool.release(item), to ensure to not keep a reference to * item! */ - @SuppressWarnings({ "rawtypes", "unchecked" }) @CheckReturnValue public T release(T item) { if (item == null) @@ -111,7 +118,6 @@ public abstract class SyncPool> { * Usage list = pool.releaseAll(list), to ensure to not keep a reference to * list! */ - @SuppressWarnings({ "unchecked", "rawtypes" }) @CheckReturnValue public T releaseAll(T item) { if (item == null) @@ -156,7 +162,6 @@ public abstract class SyncPool> { * * @return the item */ - @SuppressWarnings("unchecked") public T get() { synchronized (this) {