Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - unify has surface state handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Apr 17, 2019
1 parent 4255536 commit d89b13f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,6 @@ protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.onSurfaceCreated();
super.onSurfaceCreated(gl, config);
}

@Override
protected void onSurfaceDestroyed() {
super.onSurfaceDestroyed();
MapView.this.onSurfaceDestroyed();
}
};

addView(textureView, 0);
Expand All @@ -307,12 +301,6 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.onSurfaceCreated();
super.onSurfaceCreated(gl, config);
}

@Override
protected void onSurfaceDestroyed() {
super.onSurfaceDestroyed();
MapView.this.onSurfaceDestroyed();
}
};

addView(glSurfaceView, 0);
Expand All @@ -325,7 +313,6 @@ protected void onSurfaceDestroyed() {
}

private void onSurfaceCreated() {
nativeMapView.setHasSurface(true);
post(new Runnable() {
@Override
public void run() {
Expand All @@ -338,12 +325,6 @@ public void run() {
});
}

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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ interface NativeMap {

boolean isDestroyed();

boolean hasSurface();

void setHasSurface(boolean hasSurface);

//
// Camera API
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -535,7 +532,7 @@ public void removeAnnotations(long[] ids) {
@Override
@NonNull
public long[] queryPointAnnotations(RectF rect) {
if (checkState("queryPointAnnotations")) {
if (checkState("queryPointAnnotations") || !mapRenderer.hasSurface()) {
return new long[] {};
}
return nativeQueryPointAnnotations(rect);
Expand All @@ -544,7 +541,7 @@ public long[] queryPointAnnotations(RectF rect) {
@Override
@NonNull
public long[] queryShapeAnnotations(RectF rectF) {
if (checkState("queryShapeAnnotations")) {
if (checkState("queryShapeAnnotations") || !mapRenderer.hasSurface()) {
return new long[] {};
}
return nativeQueryShapeAnnotations(rectF);
Expand Down Expand Up @@ -893,7 +890,7 @@ public Bitmap getImage(String name) {
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,
Expand All @@ -906,7 +903,7 @@ public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates,
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(
Expand Down Expand Up @@ -1426,16 +1423,6 @@ public boolean isDestroyed() {
return destroyed;
}

@Override
public boolean hasSurface() {
return hasSurface;
}

@Override
public void setHasSurface(boolean hasSurface) {
this.hasSurface = hasSurface;
}

public interface ViewCallback {
@Nullable
Bitmap getViewContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,6 +132,8 @@ private native void nativeInitialize(MapRenderer self,

private native void nativeOnSurfaceDestroyed();

protected native void nativeReset();

private native void nativeRender();

private long timeElapsed;
Expand All @@ -156,4 +158,13 @@ public void setMaximumFps(int maximumFps) {
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -46,7 +45,7 @@ public void surfaceCreated(SurfaceHolder holder) {
public void surfaceDestroyed(SurfaceHolder holder) {
super.surfaceDestroyed(holder);
hasSurface = false;
onSurfaceDestroyed();
nativeReset();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public TextureViewMapRenderer(@NonNull Context context,
@Override
protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
super.onSurfaceCreated(gl, config);
hasSurface = true;
}

/**
Expand All @@ -58,6 +59,7 @@ protected void onSurfaceChanged(GL10 gl, int width, int height) {
*/
@Override
protected void onSurfaceDestroyed() {
hasSurface = false;
super.onSurfaceDestroyed();
}

Expand Down
8 changes: 7 additions & 1 deletion platform/android/src/map_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions platform/android/src/map_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ class MapRenderer : public Scheduler {

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;
Expand Down

0 comments on commit d89b13f

Please sign in to comment.