API: add TileSet.{lockTiles,releaseTiles,setTiles}

This commit is contained in:
Hannes Janetzek 2013-09-19 20:34:47 +02:00
parent e5761ef601
commit f6bb944907
4 changed files with 59 additions and 82 deletions

View File

@ -664,7 +664,7 @@ class TextRenderer extends ElementRenderer {
/* private */void cleanup() { /* private */void cleanup() {
mPool.releaseAll(mLabels); mPool.releaseAll(mLabels);
mLabels = null; mLabels = null;
mTileSet.clear(); mTileSet.releaseTiles();
mLabelTask = null; mLabelTask = null;
} }

View File

@ -33,10 +33,6 @@ import org.oscim.utils.ScanBox;
import org.oscim.utils.quadtree.QuadTree; import org.oscim.utils.quadtree.QuadTree;
import org.oscim.utils.quadtree.QuadTreeIndex; import org.oscim.utils.quadtree.QuadTreeIndex;
/**
* @TODO - prefetching to cache file - this class should probably not be in
* 'renderer' -> tilemap? - make it general for reuse in tile-overlays
*/
public class TileManager { public class TileManager {
static final String TAG = TileManager.class.getName(); static final String TAG = TileManager.class.getName();
@ -110,6 +106,7 @@ public class TileManager {
}; };
private final float[] mMapPlane = new float[8]; private final float[] mMapPlane = new float[8];
//private final TileLayer<?> mTileLayer; //private final TileLayer<?> mTileLayer;
public TileManager(Map map, int minZoom, int maxZoom, int cacheLimit) { public TileManager(Map map, int minZoom, int maxZoom, int cacheLimit) {
@ -182,7 +179,7 @@ public class TileManager {
/** /**
* 1. Update mCurrentTiles TileSet of currently visible tiles. 2. Add not * 1. Update mCurrentTiles TileSet of currently visible tiles. 2. Add not
* yet loaded (or loading) tiles to JobQueue. 3. Manage cache * yet loaded (or loading) tiles to JobQueue. 3. Manage cache
* *
* @param pos * @param pos
* current MapPosition * current MapPosition
*/ */
@ -242,14 +239,10 @@ public class TileManager {
if (changed) { if (changed) {
synchronized (mTilelock) { synchronized (mTilelock) {
// lock new tiles // lock new tiles
for (int i = 0; i < newCnt; i++) mNewTiles.lockTiles();
newTiles[i].lock();
// unlock previous tiles // unlock previous tiles
for (int i = 0; i < curCnt; i++) { mCurrentTiles.releaseTiles();
curTiles[i].unlock();
curTiles[i] = null;
}
// make new tiles current // make new tiles current
TileSet tmp = mCurrentTiles; TileSet tmp = mCurrentTiles;
@ -258,7 +251,7 @@ public class TileManager {
mUpdateSerial++; mUpdateSerial++;
} }
//Log.d(TAG, newCnt + " << " + Arrays.deepToString(mCurrentTiles.tiles));
// request rendering as tiles changed // request rendering as tiles changed
mMap.render(); mMap.render();
} }
@ -272,9 +265,7 @@ public class TileManager {
updateTileDistances(jobs, jobs.length, pos); updateTileDistances(jobs, jobs.length, pos);
// sets tiles to state == LOADING // sets tiles to state == LOADING
jobQueue.setJobs(jobs); jobQueue.setJobs(jobs);
//mTileLayer.notifyLoaders();
mJobs.clear(); mJobs.clear();
@ -298,7 +289,7 @@ public class TileManager {
* Retrive a TileSet of current tiles. Tiles remain locked in cache until * Retrive a TileSet of current tiles. Tiles remain locked in cache until
* the set is unlocked by either passing it again to this function or to * the set is unlocked by either passing it again to this function or to
* releaseTiles. If passed TileSet is null it will be allocated. * releaseTiles. If passed TileSet is null it will be allocated.
* *
* @param tileSet * @param tileSet
* to be updated * to be updated
* @return true if TileSet has changed * @return true if TileSet has changed
@ -313,44 +304,22 @@ public class TileManager {
if (tileSet.serial == mUpdateSerial) if (tileSet.serial == mUpdateSerial)
return false; return false;
// dont flip new/currentTiles while copying // dont flip mNew/mCurrentTiles while copying
synchronized (mTilelock) { synchronized (mTilelock) {
MapTile[] newTiles = mCurrentTiles.tiles; tileSet.setTiles(mCurrentTiles);
int cnt = mCurrentTiles.cnt;
// lock tiles (and their proxies) to not be removed from cache
for (int i = 0; i < cnt; i++)
newTiles[i].lock();
MapTile[] nextTiles;
nextTiles = tileSet.tiles;
// unlock previously active tiles
for (int i = 0, n = tileSet.cnt; i < n; i++)
nextTiles[i].unlock();
if (nextTiles.length != mCurrentTiles.tiles.length) {
tileSet.tiles = nextTiles = new MapTile[mCurrentTiles.tiles.length];
}
// copy newTiles to nextTiles
System.arraycopy(newTiles, 0, nextTiles, 0, cnt);
tileSet.serial = mUpdateSerial; tileSet.serial = mUpdateSerial;
tileSet.cnt = cnt;
} }
return true; return true;
} }
/** /**
* @param tiles ... * Unlock tiles and clear all item references.
*
* @param tiles
*/ */
public void releaseTiles(TileSet tileSet) { public void releaseTiles(TileSet tileSet) {
// unlock previously active tiles tileSet.releaseTiles();
for (int i = 0, n = tileSet.cnt; i < n; i++)
tileSet.tiles[i].unlock();
tileSet.cnt = 0;
} }
/* package */MapTile addTile(int x, int y, int zoomLevel) { /* package */MapTile addTile(int x, int y, int zoomLevel) {
@ -385,22 +354,6 @@ public class TileManager {
p.state = STATE_LOADING; p.state = STATE_LOADING;
mJobs.add(p); mJobs.add(p);
} }
// if (zoomLevel > 3) {
// // prefetch grand parent
// p = tile.rel.parent.parent.item;
// add = false;
// if (p == null) {
// p = mIndex.create(x >> 2, y >> 2, zoomLevel - 2);
// addToCache(p);
// add = true;
// }
//
// if (add || !p.isActive()) {
// p.state = STATE_LOADING;
// mJobs.add(p);
// }
// }
} }
return tile; return tile;
@ -570,7 +523,7 @@ public class TileManager {
/** /**
* called from MapWorker Thread when tile is loaded by MapTileLoader * called from MapWorker Thread when tile is loaded by MapTileLoader
* *
* @param tile * @param tile
* Tile ready for upload in TileRenderLayer * Tile ready for upload in TileRenderLayer
* @return caller does not care * @return caller does not care

View File

@ -221,10 +221,6 @@ public class TileRenderer extends LayerRenderer {
return false; return false;
} }
// same tiles as before
// if (tileSet.serial == mDrawTiles.serial)
// return false;
int prevSerial = tileSet.serial; int prevSerial = tileSet.serial;
// ensure tiles keep visible state // ensure tiles keep visible state
@ -234,9 +230,8 @@ public class TileRenderer extends LayerRenderer {
int cnt = mDrawTiles.cnt; int cnt = mDrawTiles.cnt;
// unlock previous tiles // unlock previous tiles
for (int i = 0; i < tileSet.cnt; i++) tileSet.releaseTiles();
tileSet.tiles[i].unlock();
// ensure same size // ensure same size
if (tileSet.tiles.length != mDrawTiles.tiles.length) { if (tileSet.tiles.length != mDrawTiles.tiles.length) {
tileSet.tiles = new MapTile[mDrawTiles.tiles.length]; tileSet.tiles = new MapTile[mDrawTiles.tiles.length];
@ -251,18 +246,15 @@ public class TileRenderer extends LayerRenderer {
tileSet.tiles[tileSet.cnt++] = t; tileSet.tiles[tileSet.cnt++] = t;
} }
} }
tileSet.serial = mUploadSerial; tileSet.serial = mUploadSerial;
} }
return prevSerial != tileSet.serial; return prevSerial != tileSet.serial;
} }
public void releaseTiles(TileSet td) { public void releaseTiles(TileSet tileSet) {
for (int i = 0; i < td.cnt; i++) { tileSet.releaseTiles();
td.tiles[i].unlock();
td.tiles[i] = null;
}
td.cnt = 0;
} }
// Add additional tiles that serve as placeholer when flipping // Add additional tiles that serve as placeholer when flipping

View File

@ -18,20 +18,17 @@ import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
/** /**
* use with TileManager.getActiveTiles(TileSet) to get the current tiles. tiles * TileSet for use with TileManager.getActiveTiles(TileSet) to get the current
* are locked to not be modifed until getActiveTiles passes them back on a * tiles. Tiles will locked and not be modifed until getActiveTiles passes them
* second invocation or TODO: implement TileManager.releaseTiles(TileSet). * back to TileManager on a second invocation or TileManager.releaseTiles().
*/ */
public final class TileSet { public final class TileSet {
public int cnt = 0; public int cnt = 0;
public MapTile[] tiles; public MapTile[] tiles;
/** update counter will be set by getActiveTiles when TileSet has changed */
int serial; int serial;
public int getSerial() {
return serial;
}
public TileSet() { public TileSet() {
tiles = new MapTile[1]; tiles = new MapTile[1];
} }
@ -48,11 +45,46 @@ public final class TileSet {
return null; return null;
} }
public void clear() { /**
* Locked tiles to ensure that they are not released from cache.
* Call releaseTiles() when tiles are not needed any longer.
*/
public void lockTiles() {
for (int i = 0; i < cnt; i++)
tiles[i].lock();
}
/**
* Release locked tiles.
*/
public void releaseTiles() {
for (int i = 0; i < cnt; i++)
tiles[i].unlock();
Arrays.fill(tiles, null); Arrays.fill(tiles, null);
cnt = 0; cnt = 0;
} }
/**
* Clone TileSet from source. Release previous tiles and lock
* new tiles.
*/
public void setTiles(TileSet source) {
//lock tiles (and their proxies) to not be removed from cache
source.lockTiles();
// unlock previous tiles
releaseTiles();
if (source.tiles.length != tiles.length) {
tiles = new MapTile[source.tiles.length];
}
System.arraycopy(source.tiles, 0, tiles, 0, source.cnt);
cnt = source.cnt;
}
public static Comparator<MapTile> coordComparator = new CoordComparator(); public static Comparator<MapTile> coordComparator = new CoordComparator();
public static class CoordComparator implements Comparator<MapTile> { public static class CoordComparator implements Comparator<MapTile> {