Compare commits

...

6 commits

Author SHA1 Message Date
Vitaliy
1e809d7915 Add MapTileLayerSize for cache size 2021-04-13 04:51:27 +03:00
Vitaliy
a0e52f355f Update bitmap cache 2021-04-12 19:10:02 +03:00
Vitaliy
5773a4ca13 Update check 2021-04-12 18:17:20 +03:00
Vitaliy
a2b6a24e5c Move update cache size to AsyncLoadingThread 2021-04-12 18:15:22 +03:00
Vitaliy
b1ba628987 add check for same cache size 2021-04-12 15:03:43 +03:00
Vitaliy
fe40ec722d Dynamic bitmap cache size initial commit 2021-04-12 15:00:13 +03:00
3 changed files with 101 additions and 17 deletions

View file

@ -7,6 +7,7 @@ import net.osmand.data.RotatedTileBox;
import net.osmand.map.ITileSource;
import net.osmand.map.MapTileDownloader.DownloadRequest;
import net.osmand.plus.SQLiteTileSource;
import net.osmand.plus.resources.ResourceManager.MapTileLayerSize;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
@ -21,12 +22,12 @@ import java.util.Stack;
* Thread to load map objects (POI, transport stops )async
*/
public class AsyncLoadingThread extends Thread {
public static final int LIMIT_TRANSPORT = 200;
private static final Log log = PlatformUtil.getLog(AsyncLoadingThread.class);
Stack<Object> requests = new Stack<Object>();
private static final int CACHE_LAYER_SIZE_EXPIRE_TIME_MS = 30 * 1000;
private static final Log log = PlatformUtil.getLog(AsyncLoadingThread.class);
private final Stack<Object> requests = new Stack<Object>();
private final ResourceManager resourceManger;
public AsyncLoadingThread(ResourceManager resourceManger) {
@ -38,9 +39,12 @@ public class AsyncLoadingThread extends Thread {
public void run() {
while (true) {
try {
updateBitmapTilesCache();
int cacheCounter = 0;
boolean tileLoaded = false;
boolean mapLoaded = false;
while (!requests.isEmpty()) {
cacheCounter++;
Object req = requests.pop();
if (req instanceof TileLoadDownloadRequest) {
TileLoadDownloadRequest r = (TileLoadDownloadRequest) req;
@ -55,8 +59,12 @@ public class AsyncLoadingThread extends Thread {
}
}
}
if (cacheCounter == 10) {
cacheCounter = 0;
updateBitmapTilesCache();
}
}
if (tileLoaded || mapLoaded) {
if (tileLoaded || mapLoaded) {
// use downloader callback
resourceManger.getMapTileDownloader().fireLoadCallback(null);
}
@ -77,7 +85,6 @@ public class AsyncLoadingThread extends Thread {
requests.push(req);
}
public boolean isFilePendingToDownload(File fileToSave) {
return resourceManger.getMapTileDownloader().isFilePendingToDownload(fileToSave);
}
@ -90,6 +97,29 @@ public class AsyncLoadingThread extends Thread {
resourceManger.getMapTileDownloader().requestToDownload(req);
}
private void updateBitmapTilesCache() {
int maxCacheSize = 0;
long currentTime = System.currentTimeMillis();
for (MapTileLayerSize layerSize : resourceManger.getMapTileLayerSizes()) {
if (layerSize.markToGCTimestamp != null && currentTime - layerSize.markToGCTimestamp > CACHE_LAYER_SIZE_EXPIRE_TIME_MS) {
resourceManger.removeMapTileLayerSize(layerSize.layer);
} else if (currentTime - layerSize.activeTimestamp > CACHE_LAYER_SIZE_EXPIRE_TIME_MS) {
layerSize.markToGCTimestamp = currentTime + CACHE_LAYER_SIZE_EXPIRE_TIME_MS;
} else if (layerSize.markToGCTimestamp == null) {
maxCacheSize += layerSize.tiles;
}
}
BitmapTilesCache bitmapTilesCache = resourceManger.getBitmapTilesCache();
int oldCacheSize = bitmapTilesCache.getMaxCacheSize();
if (maxCacheSize != 0 && maxCacheSize * 1.2 < oldCacheSize || maxCacheSize > oldCacheSize) {
if (maxCacheSize / 2.5 > oldCacheSize) {
bitmapTilesCache.clearTiles();
}
log.info("Bitmap tiles to load in memory : " + maxCacheSize);
bitmapTilesCache.setMaxCacheSize(maxCacheSize);
}
}
public static class TileLoadDownloadRequest extends DownloadRequest {
public final String tileId;
@ -115,7 +145,7 @@ public class AsyncLoadingThread extends Thread {
}
public void saveTile(InputStream inputStream) throws IOException {
if(tileSource instanceof SQLiteTileSource){
if (tileSource instanceof SQLiteTileSource) {
ByteArrayOutputStream stream = null;
try {
stream = new ByteArrayOutputStream(inputStream.available());
@ -125,14 +155,13 @@ public class AsyncLoadingThread extends Thread {
try {
((SQLiteTileSource) tileSource).insertImage(xTile, yTile, zoom, stream.toByteArray());
} catch (IOException e) {
log.warn("Tile x="+xTile +" y="+ yTile+" z="+ zoom+" couldn't be read", e); //$NON-NLS-1$//$NON-NLS-2$
log.warn("Tile x=" + xTile + " y=" + yTile + " z=" + zoom + " couldn't be read", e); //$NON-NLS-1$//$NON-NLS-2$
}
} finally {
Algorithms.closeStream(inputStream);
Algorithms.closeStream(stream);
}
}
else {
}
} else {
super.saveTile(inputStream);
}
}
@ -200,6 +229,4 @@ public class AsyncLoadingThread extends Thread {
this.mapLoadedListener = mapLoadedListener;
}
}
}

View file

@ -48,6 +48,7 @@ import net.osmand.plus.resources.AsyncLoadingThread.MapLoadRequest;
import net.osmand.plus.resources.AsyncLoadingThread.OnMapLoadedListener;
import net.osmand.plus.resources.AsyncLoadingThread.TileLoadDownloadRequest;
import net.osmand.plus.srtmplugin.SRTMPlugin;
import net.osmand.plus.views.MapTileLayer;
import net.osmand.plus.views.OsmandMapLayer.DrawSettings;
import net.osmand.router.TransportStopsRouteReader;
import net.osmand.util.Algorithms;
@ -99,6 +100,7 @@ public class ResourceManager {
private List<TilesCache> tilesCacheList = new ArrayList<>();
private BitmapTilesCache bitmapTilesCache;
private GeometryTilesCache geometryTilesCache;
private List<MapTileLayerSize> mapTileLayerSizes = new ArrayList<>();
private final OsmandApplication context;
private List<ResourceListener> resourceListeners = new ArrayList<>();
@ -119,7 +121,20 @@ public class ResourceManager {
ROUTING,
TRANSPORT_ROUTING
}
public static class MapTileLayerSize {
final MapTileLayer layer;
Long markToGCTimestamp = null;
long activeTimestamp;
int tiles;
public MapTileLayerSize(MapTileLayer layer, int tiles, long activeTimestamp) {
this.layer = layer;
this.tiles = tiles;
this.activeTimestamp = activeTimestamp;
}
}
public static class BinaryMapReaderResource {
private BinaryMapIndexReader initialReader;
private File filename;
@ -286,6 +301,43 @@ public class ResourceManager {
resourceListeners.remove(listener);
}
public List<MapTileLayerSize> getMapTileLayerSizes() {
return mapTileLayerSizes;
}
public void setMapTileLayerSizes(MapTileLayer layer, int tiles) {
MapTileLayerSize layerSize = getMapTileLayerSize(layer);
if (layerSize != null) {
if (layerSize.markToGCTimestamp != null) {
layerSize.markToGCTimestamp = null;
layerSize.activeTimestamp = System.currentTimeMillis();
}
layerSize.tiles = tiles;
} else {
List<MapTileLayerSize> layerSizes = new ArrayList<>(mapTileLayerSizes);
layerSizes.add(new MapTileLayerSize(layer, tiles, System.currentTimeMillis()));
mapTileLayerSizes = layerSizes;
}
}
public void removeMapTileLayerSize(MapTileLayer layer) {
MapTileLayerSize layerSize = getMapTileLayerSize(layer);
if (layerSize != null) {
List<MapTileLayerSize> layerSizes = new ArrayList<>(mapTileLayerSizes);
layerSizes.remove(layerSize);
mapTileLayerSizes = layerSizes;
}
}
private MapTileLayerSize getMapTileLayerSize(MapTileLayer layer) {
for (MapTileLayerSize layerSize : mapTileLayerSizes) {
if (layerSize.layer == layer) {
return layerSize;
}
}
return null;
}
public void resetStoreDirectory() {
dirWithTiles = context.getAppPath(IndexConstants.TILES_INDEX_DIR);
dirWithTiles.mkdirs();

View file

@ -16,11 +16,11 @@ import net.osmand.map.ITileSource;
import net.osmand.map.TileSourceManager;
import net.osmand.map.TileSourceManager.TileSourceTemplate;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.mapillary.MapillaryPlugin;
import net.osmand.plus.rastermaps.OsmandRasterMapsPlugin;
import net.osmand.plus.resources.ResourceManager;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.util.MapUtils;
public class MapTileLayer extends BaseMapLayer {
@ -154,6 +154,8 @@ public class MapTileLayer extends BaseMapLayer {
int width = (int) Math.ceil(tilesRect.right - left);
int height = (int) Math.ceil(tilesRect.bottom + ellipticTileCorrection - top);
mgr.setMapTileLayerSizes(this, width * height);
boolean useInternet = (OsmandPlugin.getEnabledPlugin(OsmandRasterMapsPlugin.class) != null || OsmandPlugin.getEnabledPlugin(MapillaryPlugin.class) != null)
&& settings.isInternetConnectionAvailable() && map.couldBeDownloadedFromInternet();
int maxLevel = map.getMaximumZoomSupported();
@ -287,6 +289,9 @@ public class MapTileLayer extends BaseMapLayer {
@Override
public void destroyLayer() {
setMapTileAdapter(null);
if (resourceManager != null) {
resourceManager.removeMapTileLayerSize(this);
}
}
public boolean isVisible() {