From ded178be6cc945a7062344270c61b9da1a954f29 Mon Sep 17 00:00:00 2001
From: Hannes Janetzek <hannes.janetzek@gmail.com>
Date: Mon, 6 Oct 2014 17:36:09 +0200
Subject: [PATCH] TileManager: just drop index on re-init

- dont modify index of locked tiles..
- fixes one more bug from monkey test
---
 vtm/src/org/oscim/layers/tile/MapTile.java    | 43 ++++++++-----------
 .../org/oscim/layers/tile/TileManager.java    | 17 ++++----
 .../org/oscim/utils/quadtree/TileIndex.java   |  7 +++
 3 files changed, 33 insertions(+), 34 deletions(-)

diff --git a/vtm/src/org/oscim/layers/tile/MapTile.java b/vtm/src/org/oscim/layers/tile/MapTile.java
index 299b00b7..6a91e026 100644
--- a/vtm/src/org/oscim/layers/tile/MapTile.java
+++ b/vtm/src/org/oscim/layers/tile/MapTile.java
@@ -22,7 +22,6 @@ import static org.oscim.layers.tile.MapTile.State.LOADING;
 import static org.oscim.layers.tile.MapTile.State.NEW_DATA;
 import static org.oscim.layers.tile.MapTile.State.NONE;
 import static org.oscim.layers.tile.MapTile.State.READY;
-import static org.oscim.layers.tile.MapTile.State.TIMEOUT;
 
 import org.oscim.core.Tile;
 import org.oscim.layers.tile.vector.VectorTileLoader;
@@ -74,8 +73,6 @@ public class MapTile extends Tile {
 		 */
 		public final static byte CANCEL = (1 << 4);
 
-		public final static byte TIMEOUT = (1 << 5);
-
 		/**
 		 * Dont touch if you find some.
 		 */
@@ -231,12 +228,6 @@ public class MapTile extends Tile {
 		if (--locked > 0)
 			return;
 
-		if (state == DEADBEEF) {
-			log.debug("Unlock dead tile {}", this);
-			clear();
-			return;
-		}
-
 		TileNode parent = node.parent;
 		if ((proxy & PROXY_PARENT) != 0)
 			parent.item.refs--;
@@ -252,6 +243,11 @@ public class MapTile extends Tile {
 
 		/* removed all proxy references for this tile */
 		proxy = 0;
+
+		if (state == DEADBEEF) {
+			log.debug("Unlock dead tile {}", this);
+			clear();
+		}
 	}
 
 	/**
@@ -278,10 +274,7 @@ public class MapTile extends Tile {
 			data.dispose();
 			data = data.next;
 		}
-		if (state == DEADBEEF)
-			return;
-
-		state = NONE;
+		setState(NONE);
 	}
 
 	/**
@@ -402,26 +395,27 @@ public class MapTile extends Tile {
 				return "Ready";
 			case State.CANCEL:
 				return "Cancel";
-			case State.TIMEOUT:
-				return "Timeout";
 			case State.DEADBEEF:
 				return "Dead";
 		}
 		return "";
 	}
 
-	void setState(byte newState) {
+	public synchronized void setState(byte newState) {
 		if (state == newState)
 			return;
 
+		/* Renderer could have uploaded the tile while the layer
+		 * was cleared. This prevents to set tile to READY state. */
+		/* All other state changes are on the main-thread. */
+		if (state == DEADBEEF)
+			return;
+
 		switch (newState) {
 			case NONE:
-				if (state(LOADING | CANCEL)) {
-					state = newState;
-					return;
-				}
-				throw new IllegalStateException("None"
-				        + " <= " + state() + " " + this);
+				state = newState;
+				return;
+
 			case LOADING:
 				if (state == NONE) {
 					state = newState;
@@ -452,12 +446,9 @@ public class MapTile extends Tile {
 				}
 				throw new IllegalStateException("Cancel" +
 				        " <= " + state() + " " + this);
-			case TIMEOUT:
-				// TODO
-				break;
 			case DEADBEEF:
 				state = newState;
-				break;
+				return;
 		}
 	}
 }
diff --git a/vtm/src/org/oscim/layers/tile/TileManager.java b/vtm/src/org/oscim/layers/tile/TileManager.java
index 5ae0f3fc..d4aa791e 100644
--- a/vtm/src/org/oscim/layers/tile/TileManager.java
+++ b/vtm/src/org/oscim/layers/tile/TileManager.java
@@ -167,20 +167,22 @@ public class TileManager {
 	public void init() {
 		if (mCurrentTiles != null)
 			mCurrentTiles.releaseTiles();
-		/* pass VBOs and VertexItems back to pools */
+
+		mIndex.drop();
+
+		/* Pass VBOs and VertexItems back to pools */
 		for (int i = 0; i < mTilesEnd; i++) {
 			MapTile t = mTiles[i];
 			if (t == null)
 				continue;
 
-			if (!t.isLocked()) {
-				//log.debug("init clear {} {}", t, t.state());
+			/* Check if tile is used by another thread */
+			if (!t.isLocked())
 				t.clear();
-			}
-			mIndex.removeItem(t);
 
-			/* in case the tile is still loading:
-			 * clear when returned from loader */
+			/* In case the tile is still loading or used by
+			 * another thread: clear when returned from loader
+			 * or becomes unlocked */
 			t.setState(DEADBEEF);
 		}
 
@@ -592,7 +594,6 @@ public class TileManager {
 				mTilesToUpload++;
 				return;
 			}
-
 			// TODO use mMap.update(true) to retry tile loading?
 			log.debug("Load: {} {} state:{}",
 			          tile, success ? "success" : "failed",
diff --git a/vtm/src/org/oscim/utils/quadtree/TileIndex.java b/vtm/src/org/oscim/utils/quadtree/TileIndex.java
index b55ede0b..d30bfedd 100644
--- a/vtm/src/org/oscim/utils/quadtree/TileIndex.java
+++ b/vtm/src/org/oscim/utils/quadtree/TileIndex.java
@@ -195,4 +195,11 @@ public abstract class TileIndex<T extends TreeNode<T, E>, E> {
 		return root.refs;
 	}
 
+	public void drop() {
+		root.item = null;
+		root.child00 = null;
+		root.child01 = null;
+		root.child10 = null;
+		root.child11 = null;
+	}
 }