diff --git a/src/org/oscim/renderer/GLRenderer.java b/src/org/oscim/renderer/GLRenderer.java index 7d10e19c..1b65a7fb 100644 --- a/src/org/oscim/renderer/GLRenderer.java +++ b/src/org/oscim/renderer/GLRenderer.java @@ -563,7 +563,7 @@ public class GLRenderer implements GLSurfaceView.Renderer { @Override public void onSurfaceChanged(GL10 glUnused, int width, int height) { - Log.d(TAG, "SurfaceChanged:" + mNewSurface + " " + width + " " + height); + Log.d(TAG, "SurfaceChanged:" + mNewSurface + " " + width + "x" + height); if (width <= 0 || height <= 0) return; diff --git a/src/org/oscim/renderer/TileManager.java b/src/org/oscim/renderer/TileManager.java index c8477c75..533bc451 100644 --- a/src/org/oscim/renderer/TileManager.java +++ b/src/org/oscim/renderer/TileManager.java @@ -51,10 +51,8 @@ public class TileManager { private final MapView mMapView; private final MapViewPosition mMapViewPosition; - private final MapPosition mMapPosition; + private boolean mInitialized; - private int mWidth = 0; - private int mHeight = 0; // cache for all tiles private MapTile[] mTiles; @@ -85,7 +83,6 @@ public class TileManager { public TileManager(MapView mapView) { mMapView = mapView; mMapViewPosition = mapView.getMapViewPosition(); - mMapPosition = new MapPosition(); mJobs = new ArrayList(); mTiles = new MapTile[GLRenderer.CACHE_TILES]; @@ -99,7 +96,50 @@ public class TileManager { public void destroy() { // there might be some leaks in here - // ... free pools + // ... free static pools + } + + public synchronized void init(int width, int height) { + + // sync with GLRender thread + // ... and labeling thread? + GLRenderer.drawlock.lock(); + + if (mInitialized) { + // pass VBOs and VertexItems back to pools + for (int i = 0; i < mTilesSize; i++) + clearTile(mTiles[i]); + } else { + // 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; + + // clear all references to previous tiles + for (TileSet td : mTileSets) { + Arrays.fill(td.tiles, null); + td.cnt = 0; + } + + // set up TileSet large enough to hold current tiles + int num = Math.max(width, height); + int size = Tile.TILE_SIZE >> 1; + int numTiles = (num * num) / (size * size) * 4; + mNewTiles = new TileSet(numTiles); + mCurrentTiles = new TileSet(numTiles); + Log.d(TAG, "max tiles: " + numTiles); + + mInitialized = true; + + GLRenderer.drawlock.unlock(); } /** @@ -107,75 +147,32 @@ public class TileManager { * available tiles are created and added to JobQueue (mapView.addJobs) for * loading by TileGenerator class * - * @param clear - * whether to clear and reload all tiles + * @param mapPosition + * current MapPosition */ - public synchronized void updateMap(final boolean clear) { - boolean changedPos = false; - - if (mMapView == null) + public synchronized void updateMap(MapPosition mapPosition) { + if (!mInitialized) { + Log.d(TAG, "not initialized"); return; - - if (clear || !mInitialized) { - // make sure onDrawFrame is not running - // - and labeling thread? - GLRenderer.drawlock.lock(); - - // clear all tiles references - Log.d(TAG, "CLEAR " + mInitialized); - - if (clear) { - // pass VBOs and VertexItems back to pools - for (int i = 0; i < mTilesSize; i++) - clearTile(mTiles[i]); - } else { - // mInitialized is set when surface changed - // and VBOs might be lost - VertexPool.init(); - } - - QuadTree.init(); - - Arrays.fill(mTiles, null); - mTilesSize = 0; - mTilesCount = 0; - - for (TileSet td : mTileSets) { - Arrays.fill(td.tiles, null); - td.cnt = 0; - } - - // set up TileData arrays that are passed to gl-thread - int num = Math.max(mWidth, mHeight); - int size = Tile.TILE_SIZE >> 1; - int numTiles = (num * num) / (size * size) * 4; - mNewTiles = new TileSet(numTiles); - mCurrentTiles = new TileSet(numTiles); - - // make sure mMapPosition will be updated - mMapPosition.zoomLevel = -1; - mInitialized = true; - - GLRenderer.drawlock.unlock(); } - MapPosition mapPosition = mMapPosition; + //MapPosition mapPosition = mMapPosition; float[] coords = mTileCoords; - synchronized (mMapViewPosition) { - changedPos = mMapViewPosition.getMapPosition(mapPosition); - mMapViewPosition.getMapViewProjection(coords); - } + //synchronized (mMapViewPosition) { + // changedPos = mMapViewPosition.getMapPosition(mapPosition); + mMapViewPosition.getMapViewProjection(coords); + //} - if (changedPos) { - mMapView.render(); - } else { - return; - } + //if (changedPos) { + // mMapView.render(); + //} else { + // return; + //} // load some tiles more than currently visible // TODO limit how many more... - float scale = mapPosition.scale * 0.5f; + float scale = mapPosition.scale * 0.7f; float px = (float) mapPosition.x; float py = (float) mapPosition.y; @@ -590,13 +587,6 @@ public class TileManager { return true; } - public void onSizeChanged(int w, int h) { - Log.d(TAG, "onSizeChanged" + w + " " + h); - - mWidth = w; - mHeight = h; - } - private final ScanBox mScanBox = new ScanBox() { @Override public void setVisible(int y, int x1, int x2) { diff --git a/src/org/oscim/view/MapView.java b/src/org/oscim/view/MapView.java index 91502208..30bf343d 100644 --- a/src/org/oscim/view/MapView.java +++ b/src/org/oscim/view/MapView.java @@ -98,7 +98,10 @@ public class MapView extends RelativeLayout { private String mRenderTheme; private DebugSettings mDebugSettings; - private boolean mClearTiles; + private boolean mClearMap; + + private int mWidth; + private int mHeight; // FIXME: keep until old pbmap reader is removed public static boolean enableClosePolygons = false; @@ -198,9 +201,41 @@ public class MapView extends RelativeLayout { // if (testRegionZoom) // mRegionLookup = new RegionLookup(this); + clearMap(); } + @Override + public boolean onTouchEvent(MotionEvent motionEvent) { + // mMapZoomControlsjonMapViewTouchEvent(motionEvent.getAction() + // & MotionEvent.ACTION_MASK); + + if (this.isClickable()) + return mTouchEventHandler.handleMotionEvent(motionEvent); + + return false; + } + + @Override + protected synchronized void onSizeChanged(int width, int height, + int oldWidth, int oldHeight) { + Log.d(TAG, "onSizeChanged: " + width + "x" + height); + + mJobQueue.clear(); + mapWorkersPause(true); + + super.onSizeChanged(width, height, oldWidth, oldHeight); + + mWidth = width; + mHeight = height; + + if (width != 0 && height != 0) + mMapViewPosition.setViewport(width, height); + + clearMap(); + mapWorkersProceed(); + } + public void render() { if (!MapView.debugFrameTime) mGLView.requestRender(); @@ -244,44 +279,46 @@ public class MapView extends RelativeLayout { return mRotationEnabled; } - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - // mMapZoomControls.onMapViewTouchEvent(motionEvent.getAction() - // & MotionEvent.ACTION_MASK); - if (this.isClickable()) - return mTouchEventHandler.handleMotionEvent(motionEvent); - - return false; - } /** * Calculates all necessary tiles and adds jobs accordingly. * - * @param changedPos TODO + * @param forceRedraw TODO */ - public void redrawMap(boolean changedPos) { - if (mPausing || this.getWidth() == 0 || this.getHeight() == 0) + public void redrawMap(boolean forceRedraw) { + if (mPausing || mWidth == 0 || mHeight == 0) return; - //if (changedPos) - // render(); + if (forceRedraw) + render(); - if (AndroidUtils.currentThreadIsUiThread()) { - boolean changed = mMapViewPosition.getMapPosition(mMapPosition); + if (mClearMap){ + mTileManager.init(mWidth, mHeight); + mClearMap = false; - mOverlayManager.onUpdate(mMapPosition, changed); + // make sure mMapPosition will be updated + mMapPosition.zoomLevel = -1; + + // TODO clear overlays + } + + boolean changed = mMapViewPosition.getMapPosition(mMapPosition); + + //Log.d(TAG, "redraw " + changed + " " + forceRedraw); + + // required when not changed? + if (AndroidUtils.currentThreadIsUiThread()) + mOverlayManager.onUpdate(mMapPosition, changed); + + if (changed) { + mTileManager.updateMap(mMapPosition); } - mTileManager.updateMap(mClearTiles); - mClearTiles = false; } - public void clearAndRedrawMap() { - if (mPausing || this.getWidth() == 0 || this.getHeight() == 0) - return; - - //if (AndroidUtils.currentThreadIsUiThread()) - mTileManager.updateMap(true); + private void clearMap(){ + // clear tile and overlay data before next draw + mClearMap = true; } /** @@ -291,7 +328,7 @@ public class MapView extends RelativeLayout { public void setDebugSettings(DebugSettings debugSettings) { mDebugSettings = debugSettings; TileGenerator.setDebugSettings(debugSettings); - clearAndRedrawMap(); + clearMap(); } /** @@ -332,13 +369,13 @@ public class MapView extends RelativeLayout { * * @param options * the new MapDatabase options. - * @return ... + * @return true if MapDatabase changed */ public boolean setMapDatabase(MapOptions options) { if (debugDatabase) return false; - Log.i(TAG, "setMapDatabase " + options.db.name()); + Log.i(TAG, "setMapDatabase: " + options.db.name()); if (mMapOptions != null && mMapOptions.equals(options)) return true; @@ -346,7 +383,6 @@ public class MapView extends RelativeLayout { mapWorkersPause(true); mJobQueue.clear(); - mClearTiles = true; mMapOptions = options; for (int i = 0; i < mNumMapWorkers; i++) { @@ -370,6 +406,8 @@ public class MapView extends RelativeLayout { else MapView.enableClosePolygons = false; + clearMap(); + mapWorkersProceed(); return true; @@ -400,7 +438,9 @@ public class MapView extends RelativeLayout { if (ret) { mRenderTheme = internalRenderTheme.name(); } - clearAndRedrawMap(); + + clearMap(); + return ret; } @@ -424,7 +464,8 @@ public class MapView extends RelativeLayout { if (ret) { mRenderTheme = renderThemePath; } - clearAndRedrawMap(); + + clearMap(); } private boolean setRenderTheme(Theme theme) { @@ -456,26 +497,9 @@ public class MapView extends RelativeLayout { } mapWorkersProceed(); } - return false; } - @Override - protected synchronized void onSizeChanged(int width, int height, - int oldWidth, int oldHeight) { - - mJobQueue.clear(); - mapWorkersPause(true); - Log.d(TAG, "onSizeChanged" + width + " " + height); - super.onSizeChanged(width, height, oldWidth, oldHeight); - - if (width != 0 && height != 0) - mMapViewPosition.setViewport(width, height); - - mTileManager.onSizeChanged(width, height); - - mapWorkersProceed(); - } void destroy() { for (MapWorker mapWorker : mMapWorkers) {