diff options
author | tobrun <tobrun.van.nuland@gmail.com> | 2019-04-15 11:58:46 +0200 |
---|---|---|
committer | Tobrun <tobrun.van.nuland@gmail.com> | 2019-04-17 11:40:15 +0200 |
commit | d89b13f9555b3cde33499ed7bcece20c166e4dfd (patch) | |
tree | 1d52f5f57f8ba6c7b13a11655f46766169933f88 | |
parent | 425553646bc79286687f240da34144b44949c9f1 (diff) | |
download | qtlocation-mapboxgl-d89b13f9555b3cde33499ed7bcece20c166e4dfd.tar.gz |
[android] - unify has surface state handling
8 files changed, 29 insertions, 46 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 452a0454be..b04b1f8271 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -289,12 +289,6 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { MapView.this.onSurfaceCreated(); super.onSurfaceCreated(gl, config); } - - @Override - protected void onSurfaceDestroyed() { - super.onSurfaceDestroyed(); - MapView.this.onSurfaceDestroyed(); - } }; addView(textureView, 0); @@ -307,12 +301,6 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { MapView.this.onSurfaceCreated(); super.onSurfaceCreated(gl, config); } - - @Override - protected void onSurfaceDestroyed() { - super.onSurfaceDestroyed(); - MapView.this.onSurfaceDestroyed(); - } }; addView(glSurfaceView, 0); @@ -325,7 +313,6 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { } private void onSurfaceCreated() { - nativeMapView.setHasSurface(true); post(new Runnable() { @Override public void run() { @@ -338,12 +325,6 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { }); } - private void onSurfaceDestroyed() { - if (nativeMapView != null) { - nativeMapView.setHasSurface(false); - } - } - /** * You must call this method from the parent's Activity#onSaveInstanceState(Bundle) * or Fragment#onSaveInstanceState(Bundle). diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMap.java index e4a1b17f40..66038e4d45 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMap.java @@ -37,10 +37,6 @@ interface NativeMap { boolean isDestroyed(); - boolean hasSurface(); - - void setHasSurface(boolean hasSurface); - // // Camera API // diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java index 7409a83136..249a5f8d7b 100755 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java @@ -68,9 +68,6 @@ final class NativeMapView implements NativeMap { // Flag to indicate destroy was called private boolean destroyed = false; - // Flag to indicate surface was destroyed - private boolean hasSurface = false; - // Holds the pointer to JNI NativeMapView @Keep private long nativePtr = 0; @@ -535,7 +532,7 @@ final class NativeMapView implements NativeMap { @Override @NonNull public long[] queryPointAnnotations(RectF rect) { - if (checkState("queryPointAnnotations")) { + if (checkState("queryPointAnnotations") || !mapRenderer.hasSurface()) { return new long[] {}; } return nativeQueryPointAnnotations(rect); @@ -544,7 +541,7 @@ final class NativeMapView implements NativeMap { @Override @NonNull public long[] queryShapeAnnotations(RectF rectF) { - if (checkState("queryShapeAnnotations")) { + if (checkState("queryShapeAnnotations") || !mapRenderer.hasSurface()) { return new long[] {}; } return nativeQueryShapeAnnotations(rectF); @@ -893,7 +890,7 @@ final class NativeMapView implements NativeMap { public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates, @Nullable String[] layerIds, @Nullable Expression filter) { - if (checkState("queryRenderedFeatures") || !hasSurface) { + if (checkState("queryRenderedFeatures") || !mapRenderer.hasSurface()) { return new ArrayList<>(); } Feature[] features = nativeQueryRenderedFeaturesForPoint(coordinates.x / pixelRatio, @@ -906,7 +903,7 @@ final class NativeMapView implements NativeMap { public List<Feature> queryRenderedFeatures(@NonNull RectF coordinates, @Nullable String[] layerIds, @Nullable Expression filter) { - if (checkState("queryRenderedFeatures") || !hasSurface) { + if (checkState("queryRenderedFeatures") || !mapRenderer.hasSurface()) { return new ArrayList<>(); } Feature[] features = nativeQueryRenderedFeaturesForBox( @@ -1426,16 +1423,6 @@ final class NativeMapView implements NativeMap { return destroyed; } - @Override - public boolean hasSurface() { - return hasSurface; - } - - @Override - public void setHasSurface(boolean hasSurface) { - this.hasSurface = hasSurface; - } - public interface ViewCallback { @Nullable Bitmap getViewContent(); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java index 094129ed5c..d8cfb1641a 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java @@ -30,9 +30,9 @@ public abstract class MapRenderer implements MapRendererScheduler { // Holds the pointer to the native peer after initialisation private long nativePtr = 0; - private double expectedRenderTime = 0; private MapboxMap.OnFpsChangedListener onFpsChangedListener; + protected boolean hasSurface; public MapRenderer(@NonNull Context context, String localIdeographFontFamily) { float pixelRatio = context.getResources().getDisplayMetrics().density; @@ -132,6 +132,8 @@ public abstract class MapRenderer implements MapRendererScheduler { private native void nativeOnSurfaceDestroyed(); + protected native void nativeReset(); + private native void nativeRender(); private long timeElapsed; @@ -156,4 +158,13 @@ public abstract class MapRenderer implements MapRendererScheduler { } expectedRenderTime = 1E9 / maximumFps; } + + /** + * Returns true if renderer has a surface to draw on. + * + * @return returns if renderer has a surface, false otherwise + */ + public boolean hasSurface() { + return hasSurface; + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/glsurfaceview/GLSurfaceViewMapRenderer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/glsurfaceview/GLSurfaceViewMapRenderer.java index 47cb35edbc..aa36dace76 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/glsurfaceview/GLSurfaceViewMapRenderer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/glsurfaceview/GLSurfaceViewMapRenderer.java @@ -22,7 +22,6 @@ public class GLSurfaceViewMapRenderer extends MapRenderer implements GLSurfaceVi @NonNull private final GLSurfaceView glSurfaceView; - private boolean hasSurface; public GLSurfaceViewMapRenderer(Context context, GLSurfaceView glSurfaceView, @@ -46,7 +45,7 @@ public class GLSurfaceViewMapRenderer extends MapRenderer implements GLSurfaceVi public void surfaceDestroyed(SurfaceHolder holder) { super.surfaceDestroyed(holder); hasSurface = false; - onSurfaceDestroyed(); + nativeReset(); } }); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/textureview/TextureViewMapRenderer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/textureview/TextureViewMapRenderer.java index 46e6463fe8..eade1e6aac 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/textureview/TextureViewMapRenderer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/textureview/TextureViewMapRenderer.java @@ -43,6 +43,7 @@ public class TextureViewMapRenderer extends MapRenderer { @Override protected void onSurfaceCreated(GL10 gl, EGLConfig config) { super.onSurfaceCreated(gl, config); + hasSurface = true; } /** @@ -58,6 +59,7 @@ public class TextureViewMapRenderer extends MapRenderer { */ @Override protected void onSurfaceDestroyed() { + hasSurface = false; super.onSurfaceDestroyed(); } diff --git a/platform/android/src/map_renderer.cpp b/platform/android/src/map_renderer.cpp index 36d23c1849..64c00585f8 100644 --- a/platform/android/src/map_renderer.cpp +++ b/platform/android/src/map_renderer.cpp @@ -195,12 +195,17 @@ void MapRenderer::onSurfaceChanged(JNIEnv& env, jint width, jint height) { requestRender(); } -void MapRenderer::onSurfaceDestroyed(JNIEnv&) { +void MapRenderer::onRendererReset(JNIEnv&) { // Make sure to destroy the renderer on the GL Thread auto self = ActorRef<MapRenderer>(*this, mailbox); self.ask(&MapRenderer::resetRenderer).wait(); } +// needs to be called on GL thread +void MapRenderer::onSurfaceDestroyed(JNIEnv&) { + resetRenderer(); +} + // Static methods // void MapRenderer::registerNative(jni::JNIEnv& env) { @@ -214,6 +219,7 @@ void MapRenderer::registerNative(jni::JNIEnv& env) { jni::MakePeer<MapRenderer, const jni::Object<MapRenderer>&, jni::jfloat, const jni::String&, const jni::String&>, "nativeInitialize", "finalize", METHOD(&MapRenderer::render, "nativeRender"), + METHOD(&MapRenderer::onRendererReset, "nativeReset"), METHOD(&MapRenderer::onSurfaceCreated, "nativeOnSurfaceCreated"), METHOD(&MapRenderer::onSurfaceChanged, diff --git a/platform/android/src/map_renderer.hpp b/platform/android/src/map_renderer.hpp index 57265cebb1..efbe55af7f 100644 --- a/platform/android/src/map_renderer.hpp +++ b/platform/android/src/map_renderer.hpp @@ -94,10 +94,11 @@ private: void onSurfaceChanged(JNIEnv&, jint width, jint height); + void onSurfaceDestroyed(JNIEnv&); + private: // Called on either Main or GL thread // - - void onSurfaceDestroyed(JNIEnv&); + void onRendererReset(JNIEnv&); private: jni::WeakReference<jni::Object<MapRenderer>, jni::EnvAttachingDeleter> javaPeer; |