[android] - unify has surface state handling

This commit is contained in:
tobrun
2019-04-15 11:58:46 +02:00
committed by Tobrun
parent 425553646b
commit d89b13f955
8 changed files with 29 additions and 46 deletions

View File

@@ -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).

View File

@@ -37,10 +37,6 @@ interface NativeMap {
boolean isDestroyed();
boolean hasSurface();
void setHasSurface(boolean hasSurface);
//
// Camera API
//

View File

@@ -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();

View File

@@ -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;
}
}

View File

@@ -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();
}
});
}

View File

@@ -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();
}

View File

@@ -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,

View File

@@ -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;