- changed abstract SyncPool.clearItem() to return whether item should be put back to pool
- fix leak in TextureItem
This commit is contained in:
parent
ae951dc4bd
commit
37781cad78
@ -47,7 +47,7 @@ public abstract class SpriteManager<T> {
|
|||||||
protected TextureItem mTexture;
|
protected TextureItem mTexture;
|
||||||
|
|
||||||
public SpriteManager() {
|
public SpriteManager() {
|
||||||
mTexture = TextureItem.get(true);
|
mTexture = TextureItem.get();
|
||||||
|
|
||||||
//mTexture.ownBitmap = true;
|
//mTexture.ownBitmap = true;
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ public abstract class SpriteManager<T> {
|
|||||||
items = null;
|
items = null;
|
||||||
|
|
||||||
//mTexture.bitmap.eraseColor(Color.TRANSPARENT);
|
//mTexture.bitmap.eraseColor(Color.TRANSPARENT);
|
||||||
mTexture = TextureItem.get(true);
|
mTexture = TextureItem.get();
|
||||||
mCanvas.setBitmap(mTexture.bitmap);
|
mCanvas.setBitmap(mTexture.bitmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -36,8 +36,6 @@ public class BitmapLayer extends TextureLayer {
|
|||||||
mReuseBitmap = reuseBitmap;
|
mReuseBitmap = reuseBitmap;
|
||||||
mVertices = new short[24];
|
mVertices = new short[24];
|
||||||
|
|
||||||
textures = new TextureItem(-1);
|
|
||||||
|
|
||||||
// used for size calculation of Layers buffer.
|
// used for size calculation of Layers buffer.
|
||||||
verticesCnt = 4;
|
verticesCnt = 4;
|
||||||
}
|
}
|
||||||
@ -47,12 +45,10 @@ public class BitmapLayer extends TextureLayer {
|
|||||||
mHeight = h;
|
mHeight = h;
|
||||||
|
|
||||||
mBitmap = bitmap;
|
mBitmap = bitmap;
|
||||||
|
if (this.textures == null)
|
||||||
|
this.textures = new TextureItem(mBitmap);
|
||||||
|
|
||||||
TextureItem ti = this.textures;
|
TextureItem ti = this.textures;
|
||||||
ti.ownBitmap = true;
|
|
||||||
ti.width = mBitmap.getWidth();
|
|
||||||
ti.height = mBitmap.getHeight();
|
|
||||||
ti.bitmap = mBitmap;
|
|
||||||
ti.vertices = TextureRenderer.INDICES_PER_SPRITE;
|
ti.vertices = TextureRenderer.INDICES_PER_SPRITE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -31,11 +31,12 @@ public class SymbolItem extends Inlist<SymbolItem> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void clearItem(SymbolItem it) {
|
protected boolean clearItem(SymbolItem it) {
|
||||||
// drop references
|
// drop references
|
||||||
it.bitmap = null;
|
it.bitmap = null;
|
||||||
it.symbol = null;
|
it.symbol = null;
|
||||||
it.offset = null;
|
it.offset = null;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -86,7 +86,7 @@ public final class SymbolLayer extends TextureLayer {
|
|||||||
to = it.symbol.atlas.compileTexture();
|
to = it.symbol.atlas.compileTexture();
|
||||||
// clone TextureItem to use same texID with
|
// clone TextureItem to use same texID with
|
||||||
// multiple TextureItem
|
// multiple TextureItem
|
||||||
to = new TextureItem(to);
|
to = TextureItem.clone(to);
|
||||||
textures = Inlist.append(textures, to);
|
textures = Inlist.append(textures, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -30,12 +30,13 @@ public class TextItem extends Inlist<TextItem> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void clearItem(TextItem ti) {
|
protected boolean clearItem(TextItem ti) {
|
||||||
// drop references
|
// drop references
|
||||||
ti.string = null;
|
ti.string = null;
|
||||||
ti.text = null;
|
ti.text = null;
|
||||||
ti.n1 = null;
|
ti.n1 = null;
|
||||||
ti.n2 = null;
|
ti.n2 = null;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -92,7 +92,7 @@ public final class TextLayer extends TextureLayer {
|
|||||||
float y = 0;
|
float y = 0;
|
||||||
float yy;
|
float yy;
|
||||||
|
|
||||||
TextureItem to = TextureItem.get(true);
|
TextureItem to = TextureItem.get();
|
||||||
textures = to;
|
textures = to;
|
||||||
mCanvas.setBitmap(to.bitmap);
|
mCanvas.setBitmap(to.bitmap);
|
||||||
|
|
||||||
@ -114,7 +114,7 @@ public final class TextLayer extends TextureLayer {
|
|||||||
to.vertices = (short) (numIndices - offsetIndices);
|
to.vertices = (short) (numIndices - offsetIndices);
|
||||||
offsetIndices = numIndices;
|
offsetIndices = numIndices;
|
||||||
|
|
||||||
to.next = TextureItem.get(true);
|
to.next = TextureItem.get();
|
||||||
to = to.next;
|
to = to.next;
|
||||||
|
|
||||||
mCanvas.setBitmap(to.bitmap);
|
mCanvas.setBitmap(to.bitmap);
|
||||||
|
|||||||
@ -46,22 +46,24 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
public Bitmap bitmap;
|
public Bitmap bitmap;
|
||||||
|
|
||||||
// external bitmap (not from pool)
|
// external bitmap (not from pool)
|
||||||
boolean ownBitmap;
|
private boolean ownBitmap;
|
||||||
|
|
||||||
// is only referencing a textureId, does not
|
// is only referencing a textureId, does not
|
||||||
// release the texture when TextureItem is
|
// release the texture when TextureItem is
|
||||||
// released.
|
// released.
|
||||||
boolean isClone;
|
private boolean isClone;
|
||||||
|
|
||||||
TextureItem(int id) {
|
private TextureItem(int id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
TextureItem(TextureItem ti) {
|
public static TextureItem clone(TextureItem ti) {
|
||||||
this.id = ti.id;
|
TextureItem clone = new TextureItem(ti.id);
|
||||||
this.width = ti.width;
|
clone.id = ti.id;
|
||||||
this.height = ti.height;
|
clone.width = ti.width;
|
||||||
this.isClone = true;
|
clone.height = ti.height;
|
||||||
|
clone.isClone = true;
|
||||||
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TextureItem(Bitmap bitmap) {
|
public TextureItem(Bitmap bitmap) {
|
||||||
@ -80,20 +82,19 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
* Retrieve a TextureItem from pool with default Bitmap with dimension
|
* Retrieve a TextureItem from pool with default Bitmap with dimension
|
||||||
* TextureRenderer.TEXTURE_WIDTH/HEIGHT.
|
* TextureRenderer.TEXTURE_WIDTH/HEIGHT.
|
||||||
*/
|
*/
|
||||||
public synchronized static TextureItem get(boolean initBitmap) {
|
public synchronized static TextureItem get() {
|
||||||
TextureItem ti = pool.get();
|
TextureItem ti = pool.get();
|
||||||
Log.d(TAG, "get texture item " + ti.id);
|
|
||||||
if (initBitmap) {
|
|
||||||
ti.bitmap = getBitmap();
|
ti.bitmap = getBitmap();
|
||||||
ti.bitmap.eraseColor(Color.TRANSPARENT);
|
ti.bitmap.eraseColor(Color.TRANSPARENT);
|
||||||
ti.ownBitmap = false;
|
|
||||||
} else {
|
|
||||||
ti.ownBitmap = true;
|
|
||||||
}
|
|
||||||
return ti;
|
return ti;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final static SyncPool<TextureItem> pool = new SyncPool<TextureItem>(20) {
|
static class TextureItemPool extends SyncPool<TextureItem> {
|
||||||
|
|
||||||
|
public TextureItemPool() {
|
||||||
|
super(20);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(int num) {
|
public void init(int num) {
|
||||||
@ -104,30 +105,38 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
TextureItem to = new TextureItem(textureIds[i]);
|
TextureItem to = new TextureItem(textureIds[i]);
|
||||||
pool = Inlist.push(pool, to);
|
pool = Inlist.push(pool, to);
|
||||||
}
|
}
|
||||||
count = num;
|
|
||||||
fill = num;
|
fill = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TextureItem get(int width, int height) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected TextureItem createItem() {
|
protected TextureItem createItem() {
|
||||||
return new TextureItem(-1);
|
return new TextureItem(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void clearItem(TextureItem it) {
|
protected boolean clearItem(TextureItem it) {
|
||||||
// Log.d(TAG, it.ownBitmap + " " + (it.bitmap == null));
|
// Log.d(TAG, it.ownBitmap + " " + (it.bitmap == null));
|
||||||
if (it.ownBitmap)
|
if (it.ownBitmap) {
|
||||||
return;
|
it.bitmap = null;
|
||||||
|
it.ownBitmap = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (it.isClone) {
|
if (it.isClone) {
|
||||||
it.isClone = false;
|
it.isClone = false;
|
||||||
it.id = -1;
|
it.id = -1;
|
||||||
it.width = -1;
|
it.width = -1;
|
||||||
it.height = -1;
|
it.height = -1;
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
releaseBitmap(it);
|
releaseBitmap(it);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -140,6 +149,8 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private final static SyncPool<TextureItem> pool = new TextureItemPool();
|
||||||
|
|
||||||
private final static ArrayList<Integer> mTextures = new ArrayList<Integer>();
|
private final static ArrayList<Integer> mTextures = new ArrayList<Integer>();
|
||||||
private final static ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>(10);
|
private final static ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>(10);
|
||||||
|
|
||||||
@ -189,8 +200,9 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
to.id = textureIds[0];
|
to.id = textureIds[0];
|
||||||
initTexture(to.id);
|
initTexture(to.id);
|
||||||
// if (TextureRenderer.debug)
|
// if (TextureRenderer.debug)
|
||||||
Log.d(TAG, "poolCnt:" + pool.getCount() + " poolFill:" + pool.getFill()
|
Log.d(TAG, "poolFill:" + pool.getFill()
|
||||||
+ " texCnt:" + mTexCnt + " new texture " + to.id);
|
+ " texCnt:" + mTexCnt
|
||||||
|
+ " new texture " + to.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadTexture(to, to.bitmap, mBitmapFormat, mBitmapType,
|
uploadTexture(to, to.bitmap, mBitmapFormat, mBitmapType,
|
||||||
@ -213,7 +225,7 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
GL.glBindTexture(GL20.GL_TEXTURE_2D, to.id);
|
GL.glBindTexture(GL20.GL_TEXTURE_2D, to.id);
|
||||||
Log.d(TAG, "upload " + to.id);
|
//Log.d(TAG, "upload " + to.id);
|
||||||
if (to.ownBitmap) {
|
if (to.ownBitmap) {
|
||||||
bitmap.uploadToTexture(false);
|
bitmap.uploadToTexture(false);
|
||||||
// GLUtils.texImage2D(GL20.GL_TEXTURE_2D, 0, bitmap, 0);
|
// GLUtils.texImage2D(GL20.GL_TEXTURE_2D, 0, bitmap, 0);
|
||||||
@ -249,7 +261,9 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void init(int num) {
|
static void init(int num) {
|
||||||
|
|
||||||
pool.init(num);
|
pool.init(num);
|
||||||
|
mTexCnt = num;
|
||||||
|
|
||||||
mBitmaps.clear();
|
mBitmaps.clear();
|
||||||
mTextures.clear();
|
mTextures.clear();
|
||||||
@ -266,7 +280,6 @@ public class TextureItem extends Inlist<TextureItem> {
|
|||||||
// mBitmapFormat = GLUtils.getInternalFormat(mBitmaps.get(0));
|
// mBitmapFormat = GLUtils.getInternalFormat(mBitmaps.get(0));
|
||||||
// mBitmapType = GLUtils.getType(mBitmaps.get(0));
|
// mBitmapType = GLUtils.getType(mBitmaps.get(0));
|
||||||
|
|
||||||
mTexCnt = num;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Bitmap getBitmap() {
|
static Bitmap getBitmap() {
|
||||||
|
|||||||
@ -29,8 +29,9 @@ public class VertexItem extends Inlist<VertexItem> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void clearItem(VertexItem it) {
|
protected boolean clearItem(VertexItem it) {
|
||||||
it.used = 0;
|
it.used = 0;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -17,24 +17,17 @@ package org.oscim.utils.pool;
|
|||||||
public abstract class SyncPool<T extends Inlist<T>> {
|
public abstract class SyncPool<T extends Inlist<T>> {
|
||||||
protected final int maxFill;
|
protected final int maxFill;
|
||||||
protected int fill;
|
protected int fill;
|
||||||
protected int count;
|
|
||||||
|
|
||||||
protected T pool;
|
protected T pool;
|
||||||
|
|
||||||
public SyncPool() {
|
public SyncPool() {
|
||||||
maxFill = 100;
|
maxFill = 100;
|
||||||
fill = 0;
|
fill = 0;
|
||||||
count = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SyncPool(int maxItemsInPool) {
|
public SyncPool(int maxItemsInPool) {
|
||||||
maxFill = maxItemsInPool;
|
maxFill = maxItemsInPool;
|
||||||
fill = 0;
|
fill = 0;
|
||||||
count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCount() {
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getFill() {
|
public int getFill() {
|
||||||
@ -42,21 +35,25 @@ public abstract class SyncPool<T extends Inlist<T>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param items number of initial items.
|
* @param items
|
||||||
* NOTE: default does nothing!
|
* number of initial items. NOTE: default does nothing!
|
||||||
*/
|
*/
|
||||||
public void init(int items) {
|
public void init(int items) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param item set initial state
|
* @param item
|
||||||
|
* set initial state
|
||||||
|
* @return true when item should be added back to pool
|
||||||
|
* when returning false freeItem will not be called.
|
||||||
*/
|
*/
|
||||||
protected void clearItem(T item) {
|
protected boolean clearItem(T item) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param item release resources
|
* @param item
|
||||||
|
* release resources
|
||||||
*/
|
*/
|
||||||
protected void freeItem(T item) {
|
protected void freeItem(T item) {
|
||||||
|
|
||||||
@ -68,8 +65,11 @@ public abstract class SyncPool<T extends Inlist<T>> {
|
|||||||
if (item == null)
|
if (item == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
clearItem(item);
|
if (!clearItem(item)) {
|
||||||
|
// dont add back to pool
|
||||||
|
freeItem(item);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (fill < maxFill) {
|
if (fill < maxFill) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
fill++;
|
fill++;
|
||||||
@ -79,7 +79,6 @@ public abstract class SyncPool<T extends Inlist<T>> {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
freeItem(item);
|
freeItem(item);
|
||||||
count--;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,8 +90,6 @@ public abstract class SyncPool<T extends Inlist<T>> {
|
|||||||
while (item != null) {
|
while (item != null) {
|
||||||
clearItem(item);
|
clearItem(item);
|
||||||
freeItem(item);
|
freeItem(item);
|
||||||
count--;
|
|
||||||
|
|
||||||
item = item.next;
|
item = item.next;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -102,7 +99,13 @@ public abstract class SyncPool<T extends Inlist<T>> {
|
|||||||
while (item != null) {
|
while (item != null) {
|
||||||
T next = item.next;
|
T next = item.next;
|
||||||
|
|
||||||
clearItem(item);
|
if (!clearItem(item)) {
|
||||||
|
// dont add back to pool
|
||||||
|
freeItem(item);
|
||||||
|
item = next;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
fill++;
|
fill++;
|
||||||
|
|
||||||
item.next = pool;
|
item.next = pool;
|
||||||
@ -117,7 +120,6 @@ public abstract class SyncPool<T extends Inlist<T>> {
|
|||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (pool == null) {
|
if (pool == null) {
|
||||||
count++;
|
|
||||||
return createItem();
|
return createItem();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user