From 82a751545cf38a6da39a4adde3572c8909e5720e Mon Sep 17 00:00:00 2001 From: vshcherb Date: Wed, 16 Oct 2013 20:37:03 +0200 Subject: [PATCH] Implement buffer image to increase FPS --- .../plus/resources/ResourceManager.java | 8 +++++++ .../osmand/plus/views/OsmandMapTileView.java | 21 +++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index d5de560254..749d0bf950 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -61,6 +61,7 @@ import android.content.res.AssetManager; import android.database.sqlite.SQLiteException; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.os.HandlerThread; import android.util.DisplayMetrics; import android.view.WindowManager; import net.osmand.map.OsmandRegions; @@ -126,6 +127,7 @@ public class ResourceManager { protected final MapTileDownloader tileDownloader; public final AsyncLoadingThread asyncLoadingThread = new AsyncLoadingThread(this); + private HandlerThread renderingBufferImageThread; protected boolean internetIsNotAccessible = false; @@ -133,6 +135,8 @@ public class ResourceManager { this.context = context; this.renderer = new MapRenderRepositories(context); asyncLoadingThread.start(); + renderingBufferImageThread = new HandlerThread("RenderingBaseImage"); + renderingBufferImageThread.start(); tileDownloader = MapTileDownloader.getInstance(Version.getFullVersion(context)); @@ -151,6 +155,10 @@ public class ResourceManager { public MapTileDownloader getMapTileDownloader() { return tileDownloader; } + + public HandlerThread getRenderingBufferImageThread() { + return renderingBufferImageThread; + } public void resetStoreDirectory() { diff --git a/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java b/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java index 35eac91969..75d7443040 100644 --- a/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java +++ b/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java @@ -36,7 +36,6 @@ import android.graphics.Paint.Style; import android.graphics.PointF; import android.graphics.RectF; import android.os.Handler; -import android.os.HandlerThread; import android.os.Message; import android.os.SystemClock; import android.util.AttributeSet; @@ -199,9 +198,8 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall handler = new Handler(); - HandlerThread ht = new HandlerThread("RenderingBaseImage"); - ht.start(); - baseHandler = new Handler(ht.getLooper()); + + baseHandler = new Handler(application.getResourceManager().getRenderingBufferImageThread().getLooper()); getHolder().addCallback(this); animatedDraggingThread = new AnimateDraggingMapThread(this); gestureDetector = new GestureDetector(getContext(), new MapExplorer(this, new MapTileViewOnGestureListener())); @@ -411,7 +409,6 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall if(tileBox.getPixHeight() == 0 || tileBox.getPixWidth() == 0){ return; } - baseHandler.removeMessages(BASE_REFRESH_MESSAGE); if(bufferBitmapTmp == null || tileBox.getPixHeight() != bufferBitmapTmp.getHeight() || tileBox.getPixWidth() != bufferBitmapTmp.getWidth()) { bufferBitmapTmp = Bitmap.createBitmap(tileBox.getPixWidth(), tileBox.getPixHeight(), Config.RGB_565); @@ -452,7 +449,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall Canvas canvas = holder.lockCanvas(); if (canvas != null) { try { - final float ratioy = mapPosition == OsmandSettings.BOTTOM_CONSTANT ? 0.8f : 0.5f; + final float ratioy = mapPosition == OsmandSettings.BOTTOM_CONSTANT ? 0.8f : 0.5f; final int cy = (int) (ratioy * getHeight()); if(currentViewport.getPixWidth() != getWidth() || currentViewport.getPixHeight() != getHeight() || currentViewport.getCenterPixelY() != cy) { @@ -534,7 +531,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall private void refreshBufferImage(final DrawSettings drawSettings) { if (!baseHandler.hasMessages(BASE_REFRESH_MESSAGE) || drawSettings.isUpdateVectorRendering()) { - Message msg = Message.obtain(handler, new Runnable() { + Message msg = Message.obtain(baseHandler, new Runnable() { @Override public void run() { baseHandler.removeMessages(BASE_REFRESH_MESSAGE); @@ -555,10 +552,12 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall // this method could be called in non UI thread public void refreshMap(final boolean updateVectorRendering) { - boolean nightMode = application.getDaynightHelper().isNightMode(); - DrawSettings drawSettings = new DrawSettings(nightMode, updateVectorRendering); - sendRefreshMapMsg(drawSettings, 20); - refreshBufferImage(drawSettings); + if (isShown()) { + boolean nightMode = application.getDaynightHelper().isNightMode(); + DrawSettings drawSettings = new DrawSettings(nightMode, updateVectorRendering); + sendRefreshMapMsg(drawSettings, 20); + refreshBufferImage(drawSettings); + } } private void sendRefreshMapMsg(final DrawSettings drawSettings, int delay) {