From ce2b0eacbf26d123dddd46054b2c8ab277001843 Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 9 Sep 2020 13:28:14 +0300 Subject: [PATCH] metatile cache added --- .../osmand/plus/server/OsmAndHttpServer.java | 6 ++- .../osmand/plus/server/ServerFragment.java | 3 +- .../plus/server/endpoints/TileEndpoint.java | 39 +++++++++++-------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java b/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java index 6bc3ea272e..ed15fe7172 100644 --- a/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java +++ b/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java @@ -58,7 +58,11 @@ public class OsmAndHttpServer extends NanoHTTPD { while (it.hasNext()) { Map.Entry e = it.next(); if (uri.startsWith(e.getKey())) { - return e.getValue().process(session, uri); + try { + return e.getValue().process(session, uri); + } catch (Exception exception) { + LOG.error("SERVER ERROR: " + exception.getMessage()); + } } } return ErrorResponses.response404; diff --git a/OsmAnd/src/net/osmand/plus/server/ServerFragment.java b/OsmAnd/src/net/osmand/plus/server/ServerFragment.java index 1b83e614a2..1b9f73e05f 100644 --- a/OsmAnd/src/net/osmand/plus/server/ServerFragment.java +++ b/OsmAnd/src/net/osmand/plus/server/ServerFragment.java @@ -85,14 +85,13 @@ public class ServerFragment extends BaseOsmAndFragment { } private void initServer() { - TrafficStats.setThreadStatsTag(THREAD_ID); String hostname = getDeviceAddress(); try { server = new OsmAndHttpServer(hostname, port); server.start((MapActivity) getActivity()); initialized = true; - updateTextView("Server started at: " + server.getUrl()); + updateTextView("Server started at " + server.getUrl()); } catch (IOException e) { Toast.makeText(requireContext(), e.getLocalizedMessage(), diff --git a/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java b/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java index 9a6c6dbdfb..de41803cef 100644 --- a/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java +++ b/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java @@ -21,7 +21,7 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { private static final int TILE_DENSITY = 2; private static final int TIMEOUT_STEP = 500; private static final int TIMEOUT = 5000; - private static final int METATILE_SIZE = 1; + private static final int METATILE_SIZE = 2; private static final int MAX_CACHE_SIZE = 4; private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class); @@ -43,8 +43,10 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { } public Bitmap getSubtile(int x, int y) { - // TODO cut subtitle - return bmp; + return Bitmap.createBitmap(bmp, + (x - sx) * TILE_SIZE_PX * TILE_DENSITY, + (y - sy) * TILE_SIZE_PX * TILE_DENSITY, + TILE_SIZE_PX * TILE_DENSITY, TILE_SIZE_PX * TILE_DENSITY); } } @@ -76,19 +78,24 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { int zoom = Integer.parseInt(prms[1]); int x = Integer.parseInt(prms[2]); int y = Integer.parseInt(prms[3]); - //incorrect condition -// for (MetaTileCache r : cache) { -// if (r.zoom == zoom && r.ex >= x && r.ey >= y && r.sx <= x && r.sy <= y) { -// res = r; -// } -// } - MetaTileCache res = requestMetatile(x, y, zoom); + + MetaTileCache res = null; + for (MetaTileCache r : cache) { + if (r.zoom == zoom && r.ex >= x && r.ey >= y && r.sx <= x && r.sy <= y) { + res = r; + } + } if (res == null) { - return OsmAndHttpServer.ErrorResponses.response500; + res = requestMetatile(x, y, zoom); + if (res == null) { + LOG.error("SERVER: Cannot request metatile"); + return OsmAndHttpServer.ErrorResponses.response500; + } } ByteArrayOutputStream stream = new ByteArrayOutputStream(); - Bitmap bmp = res.bmp; + Bitmap bmp = res.getSubtile(x, y); if (bmp == null) { + LOG.error("SERVER: Cannot cut bitmap"); return OsmAndHttpServer.ErrorResponses.response500; } bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); @@ -109,7 +116,8 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { .setLocation(lat, lon) .setMapDensity(TILE_DENSITY).density(TILE_DENSITY) .setZoom(zoom) - .setPixelDimensions(TILE_SIZE_PX * TILE_DENSITY * METATILE_SIZE, TILE_SIZE_PX * TILE_DENSITY * METATILE_SIZE, 0.5f, 0.5f).build(); + .setPixelDimensions(TILE_SIZE_PX * TILE_DENSITY * METATILE_SIZE, + TILE_SIZE_PX * TILE_DENSITY * METATILE_SIZE, 0.5f, 0.5f).build(); mapActivity.getMapView().setCurrentViewport(rotatedTileBox); int timeout = 0; try { @@ -127,9 +135,8 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { res.sy = my; res.ey = my + METATILE_SIZE - 1; res.zoom = zoom; - RotatedTileBox tilebox = mapActivity.getMapView().getBufferImgLoc(); - res.bmp = mapActivity.getMapView().getBufferBitmap(); - LOG.debug(mapActivity.getMapView().getBufferImgLoc()); + Bitmap tempBmp = mapActivity.getMapView().getBufferBitmap(); + res.bmp = tempBmp.copy(tempBmp.getConfig(), true); addToMemoryCache(res); } return res;