synchronize clearing TileLayer on its TileRenderer instead of global MapRenderer.drawlock

This commit is contained in:
Hannes Janetzek 2014-01-19 15:15:07 +01:00
parent 2de6576765
commit 348e63b4fb
5 changed files with 46 additions and 73 deletions

View File

@ -24,9 +24,12 @@ import org.oscim.map.Map;
import org.oscim.tiling.TileLoader;
import org.oscim.tiling.TileManager;
import org.oscim.tiling.TileRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class TileLayer<T extends TileLoader> extends Layer implements Map.UpdateListener {
//static final Logger log = LoggerFactory.getLogger(TileLayer.class);
static final Logger log = LoggerFactory.getLogger(TileLayer.class);
private final static int MAX_ZOOMLEVEL = 17;
private final static int MIN_ZOOMLEVEL = 2;
private final static int CACHE_LIMIT = 250;
@ -37,8 +40,6 @@ public abstract class TileLayer<T extends TileLoader> extends Layer implements M
protected final int mNumTileLoader = 4;
protected final ArrayList<T> mTileLoader;
protected boolean mInitial = true;
public TileLayer(Map map) {
this(map, MIN_ZOOMLEVEL, MAX_ZOOMLEVEL, CACHE_LIMIT);
}
@ -71,10 +72,13 @@ public abstract class TileLayer<T extends TileLoader> extends Layer implements M
@Override
public void onMapUpdate(MapPosition mapPosition, boolean changed, boolean clear) {
if (clear || mInitial) {
mRenderLayer.clearTiles();
mTileManager.init(mInitial);
mInitial = false;
if (clear) {
// sync with TileRenderer
synchronized (mRenderLayer) {
mRenderLayer.clearTiles();
mTileManager.init();
}
changed = true;
}
@ -87,16 +91,13 @@ public abstract class TileLayer<T extends TileLoader> extends Layer implements M
for (T loader : mTileLoader) {
loader.pause();
loader.interrupt();
try {
loader.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
loader.cleanup();
//try {
// tileWorker.join(10000);
//} catch (InterruptedException e) {
// // restore the interrupted status
// Thread.currentThread().interrupt();
//}
}
mTileManager.destroy();
}
void notifyLoaders() {
@ -110,11 +111,12 @@ public abstract class TileLayer<T extends TileLoader> extends Layer implements M
if (!loader.isPausing())
loader.pause();
}
if (wait) {
for (T loader : mTileLoader) {
if (!loader.isPausing())
loader.awaitPausing();
}
if (!wait)
return;
for (T loader : mTileLoader) {
if (!loader.isPausing())
loader.awaitPausing();
}
}

View File

@ -90,10 +90,6 @@ public class MapRenderer {
private static volatile boolean mUpdateColor = false;
// drawlock to synchronize Main- and GL-Thread
// static ReentrantLock tilelock = new ReentrantLock();
public static Object drawlock = new Object();
public static long frametime;
// Do not use the same buffer to upload data within a frame twice
@ -210,13 +206,8 @@ public class MapRenderer {
}
public void onDrawFrame() {
// prevent main thread recreating all tiles (updateMap)
// while rendering is going on.
synchronized (drawlock) {
frametime = System.currentTimeMillis();
draw();
}
frametime = System.currentTimeMillis();
draw();
mBufferPool.releaseBuffers();
}

View File

@ -42,6 +42,7 @@ public abstract class TileLoader extends PausableThread {
@Override
protected void doWork() {
MapTile tile = mTileManager.getTileJob();
if (tile == null)
@ -53,7 +54,7 @@ public abstract class TileLoader extends PausableThread {
success = executeJob(tile);
} catch (Exception e) {
e.printStackTrace();
return;
success = false;
}
if (isInterrupted())

View File

@ -132,11 +132,6 @@ public class TileManager {
mUpdateSerial = 0;
}
public void destroy() {
// there might be some leaks in here
// ... free static pools
}
private int[] mZoomTable;
public void setZoomTable(int[] zoomLevel) {
@ -144,40 +139,23 @@ public class TileManager {
}
public void init(boolean first) {
public void init() {
// pass VBOs and VertexItems back to pools
for (int i = 0; i < mTilesSize; i++)
clearTile(mTiles[i]);
// sync with GLRender thread
// ... and labeling thread?
synchronized (MapRenderer.drawlock) {
// clear references to cached MapTiles
Arrays.fill(mTiles, null);
mTilesSize = 0;
mTilesCount = 0;
if (!first) {
// pass VBOs and VertexItems back to pools
for (int i = 0; i < mTilesSize; i++)
clearTile(mTiles[i]);
}
// set up TileSet large enough to hold current tiles
int num = Math.max(mMap.getWidth(), mMap.getHeight());
int size = Tile.SIZE >> 1;
int numTiles = (num * num) / (size * size) * 4;
// FIXME any of this still needed?
// mInitialized is set when surface changed
// and VBOs might be lost
// VertexPool.init();
// clear cache index
// QuadTree.init();
// clear references to cached MapTiles
Arrays.fill(mTiles, null);
mTilesSize = 0;
mTilesCount = 0;
// set up TileSet large enough to hold current tiles
int num = Math.max(mMap.getWidth(), mMap.getHeight());
int size = Tile.SIZE >> 1;
int numTiles = (num * num) / (size * size) * 4;
mNewTiles = new TileSet(numTiles);
mCurrentTiles = new TileSet(numTiles);
log.debug("max tiles: " + numTiles);
}
mNewTiles = new TileSet(numTiles);
mCurrentTiles = new TileSet(numTiles);
}
/**
@ -187,7 +165,7 @@ public class TileManager {
* @param pos
* current MapPosition
*/
public synchronized boolean update(MapPosition pos) {
public boolean update(MapPosition pos) {
// clear JobQueue and set tiles to state == NONE.
// one could also append new tiles and sort in JobQueue
// but this has the nice side-effect that MapWorkers dont

View File

@ -64,8 +64,11 @@ public class TileRenderer extends LayerRenderer {
mAlpha = alpha;
}
/**
* synced with clearTiles
*/
@Override
protected void update(MapPosition pos, boolean positionChanged, Matrices m) {
protected synchronized void update(MapPosition pos, boolean positionChanged, Matrices m) {
if (mAlpha == 0) {
mTileManager.releaseTiles(mDrawTiles);
@ -106,9 +109,7 @@ public class TileRenderer extends LayerRenderer {
public void clearTiles() {
// Clear all references to MapTiles as all current
// tiles will also be removed from TileManager.
synchronized (MapRenderer.drawlock) {
mDrawTiles = new TileSet();
}
mDrawTiles = new TileSet();
}
/** compile tile layer data and upload to VBOs */