Merge branch 'texture_pool_dispose'
This commit is contained in:
commit
ef36c8a3d8
@ -104,10 +104,16 @@ public class BitmapTileLayer extends TileLayer {
|
|||||||
return new BitmapTileLoader(this, mTileSource);
|
return new BitmapTileLoader(this, mTileSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDetach() {
|
||||||
|
super.onDetach();
|
||||||
|
pool.clear();
|
||||||
|
}
|
||||||
|
|
||||||
final static int POOL_FILL = 40;
|
final static int POOL_FILL = 40;
|
||||||
|
|
||||||
/** pool shared by TextLayers */
|
/** pool shared by TextLayers */
|
||||||
final static TexturePool pool = new TexturePool(POOL_FILL) {
|
final TexturePool pool = new TexturePool(POOL_FILL) {
|
||||||
|
|
||||||
// int sum = 0;
|
// int sum = 0;
|
||||||
//
|
//
|
||||||
|
@ -21,7 +21,6 @@ import static org.oscim.layers.tile.MapTile.State.CANCEL;
|
|||||||
import org.oscim.backend.canvas.Bitmap;
|
import org.oscim.backend.canvas.Bitmap;
|
||||||
import org.oscim.core.Tile;
|
import org.oscim.core.Tile;
|
||||||
import org.oscim.layers.tile.MapTile;
|
import org.oscim.layers.tile.MapTile;
|
||||||
import org.oscim.layers.tile.TileLayer;
|
|
||||||
import org.oscim.layers.tile.TileLoader;
|
import org.oscim.layers.tile.TileLoader;
|
||||||
import org.oscim.renderer.elements.BitmapLayer;
|
import org.oscim.renderer.elements.BitmapLayer;
|
||||||
import org.oscim.renderer.elements.ElementLayers;
|
import org.oscim.renderer.elements.ElementLayers;
|
||||||
@ -35,10 +34,12 @@ public class BitmapTileLoader extends TileLoader {
|
|||||||
protected static final Logger log = LoggerFactory.getLogger(BitmapTileLoader.class);
|
protected static final Logger log = LoggerFactory.getLogger(BitmapTileLoader.class);
|
||||||
|
|
||||||
private final ITileDataSource mTileDataSource;
|
private final ITileDataSource mTileDataSource;
|
||||||
|
private final BitmapTileLayer mLayer;
|
||||||
|
|
||||||
public BitmapTileLoader(TileLayer tileLayer, TileSource tileSource) {
|
public BitmapTileLoader(BitmapTileLayer tileLayer, TileSource tileSource) {
|
||||||
super(tileLayer.getManager());
|
super(tileLayer.getManager());
|
||||||
mTileDataSource = tileSource.getDataSource();
|
mTileDataSource = tileSource.getDataSource();
|
||||||
|
mLayer = tileLayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -58,7 +59,7 @@ public class BitmapTileLoader extends TileLoader {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
BitmapLayer l = new BitmapLayer(false);
|
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();
|
ElementLayers layers = new ElementLayers();
|
||||||
layers.setTextureLayers(l);
|
layers.setTextureLayers(l);
|
||||||
|
@ -27,6 +27,7 @@ import org.oscim.backend.GLAdapter;
|
|||||||
import org.oscim.backend.canvas.Color;
|
import org.oscim.backend.canvas.Color;
|
||||||
import org.oscim.map.Map;
|
import org.oscim.map.Map;
|
||||||
import org.oscim.renderer.elements.ElementLayers;
|
import org.oscim.renderer.elements.ElementLayers;
|
||||||
|
import org.oscim.renderer.elements.TextureItem;
|
||||||
import org.oscim.utils.pool.Inlist;
|
import org.oscim.utils.pool.Inlist;
|
||||||
import org.oscim.utils.pool.Pool;
|
import org.oscim.utils.pool.Pool;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -174,6 +175,7 @@ public class MapRenderer {
|
|||||||
draw();
|
draw();
|
||||||
|
|
||||||
mBufferPool.releaseBuffers();
|
mBufferPool.releaseBuffers();
|
||||||
|
TextureItem.disposeTextures();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void draw() {
|
private void draw() {
|
||||||
|
@ -144,8 +144,6 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class TexturePool extends SyncPool<TextureItem> {
|
public static class TexturePool extends SyncPool<TextureItem> {
|
||||||
|
|
||||||
private final ArrayList<Integer> mTexDisposed = new ArrayList<Integer>();
|
|
||||||
private final ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>(10);
|
private final ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>(10);
|
||||||
|
|
||||||
private final int mHeight;
|
private final int mHeight;
|
||||||
@ -226,13 +224,11 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void freeItem(TextureItem t) {
|
protected void freeItem(TextureItem t) {
|
||||||
|
if (!t.ref && t.id >= 0) {
|
||||||
if (!t.ref) {
|
mTexCnt--;
|
||||||
synchronized (mTexDisposed) {
|
synchronized (disposedTextures) {
|
||||||
if (t.id >= 0) {
|
disposedTextures.add(Integer.valueOf(t.id));
|
||||||
mTexDisposed.add(Integer.valueOf(t.id));
|
t.id = -1;
|
||||||
t.id = -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -253,19 +249,6 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
if (t.bitmap == null)
|
if (t.bitmap == null)
|
||||||
throw new RuntimeException("Missing bitmap for texture");
|
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) {
|
if (t.id < 0) {
|
||||||
int[] textureIds = GLUtils.glGenTextures(1);
|
int[] textureIds = GLUtils.glGenTextures(1);
|
||||||
t.id = textureIds[0];
|
t.id = textureIds[0];
|
||||||
@ -318,10 +301,28 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
/* Pool for not-pooled textures. Disposed items will only be released
|
/* Pool for not-pooled textures. Disposed items will only be released
|
||||||
* on the GL-Thread and will not be put back in any pool. */
|
* on the GL-Thread and will not be put back in any pool. */
|
||||||
final static TexturePool NOPOOL = new TexturePool(0);
|
final static TexturePool NOPOOL = new TexturePool(0);
|
||||||
|
final static ArrayList<Integer> disposedTextures = new ArrayList<Integer>();
|
||||||
|
|
||||||
private static GL20 GL;
|
private static GL20 GL;
|
||||||
|
|
||||||
static void init(GL20 gl) {
|
static void init(GL20 gl) {
|
||||||
GL = 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package org.oscim.utils.pool;
|
|||||||
|
|
||||||
import javax.annotation.CheckReturnValue;
|
import javax.annotation.CheckReturnValue;
|
||||||
|
|
||||||
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
public abstract class SyncPool<T extends Inlist<?>> {
|
public abstract class SyncPool<T extends Inlist<?>> {
|
||||||
protected final int mMaxFill;
|
protected final int mMaxFill;
|
||||||
protected final boolean mClearItems;
|
protected final boolean mClearItems;
|
||||||
@ -50,6 +51,13 @@ public abstract class SyncPool<T extends Inlist<?>> {
|
|||||||
mPool = null;
|
mPool = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized void clear() {
|
||||||
|
while (mPool != null) {
|
||||||
|
freeItem(mPool);
|
||||||
|
mPool = (T) mPool.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param item
|
* @param item
|
||||||
* set initial state
|
* set initial state
|
||||||
@ -81,7 +89,6 @@ public abstract class SyncPool<T extends Inlist<?>> {
|
|||||||
* Usage item = pool.release(item), to ensure to not keep a reference to
|
* Usage item = pool.release(item), to ensure to not keep a reference to
|
||||||
* item!
|
* item!
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
|
||||||
@CheckReturnValue
|
@CheckReturnValue
|
||||||
public T release(T item) {
|
public T release(T item) {
|
||||||
if (item == null)
|
if (item == null)
|
||||||
@ -111,7 +118,6 @@ public abstract class SyncPool<T extends Inlist<?>> {
|
|||||||
* Usage list = pool.releaseAll(list), to ensure to not keep a reference to
|
* Usage list = pool.releaseAll(list), to ensure to not keep a reference to
|
||||||
* list!
|
* list!
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
||||||
@CheckReturnValue
|
@CheckReturnValue
|
||||||
public T releaseAll(T item) {
|
public T releaseAll(T item) {
|
||||||
if (item == null)
|
if (item == null)
|
||||||
@ -156,7 +162,6 @@ public abstract class SyncPool<T extends Inlist<?>> {
|
|||||||
*
|
*
|
||||||
* @return the item
|
* @return the item
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public T get() {
|
public T get() {
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user