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

Query when renderer is being recreated #14395

Merged
merged 4 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.mapbox.android.gestures.AndroidGesturesManager;
import com.mapbox.mapboxsdk.MapStrictMode;
import com.mapbox.mapboxsdk.Mapbox;
Expand All @@ -36,13 +35,12 @@
import com.mapbox.mapboxsdk.storage.FileSource;
import com.mapbox.mapboxsdk.utils.BitmapUtils;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_MAP_NORTH_ANIMATION;
import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_WAIT_IDLE;

Expand Down Expand Up @@ -74,7 +72,6 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
private MapboxMapOptions mapboxMapOptions;
private MapRenderer mapRenderer;
private boolean destroyed;
private boolean hasSurface;

private CompassView compassView;
private PointF focalPoint;
Expand Down Expand Up @@ -292,6 +289,12 @@ 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 @@ -304,6 +307,12 @@ 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 @@ -316,7 +325,7 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}

private void onSurfaceCreated() {
hasSurface = true;
nativeMapView.setHasSurface(true);
post(new Runnable() {
@Override
public void run() {
Expand All @@ -329,6 +338,12 @@ 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 Expand Up @@ -427,7 +442,7 @@ public void onDestroy() {
mapboxMap.onDestroy();
}

if (nativeMapView != null && hasSurface) {
if (nativeMapView != null) {
// null when destroying an activity programmatically mapbox-navigation-android/issues/503
nativeMapView.destroy();
nativeMapView = null;
Expand All @@ -439,10 +454,10 @@ public void onDestroy() {
}

/**
* The maximum frame rate at which the map view is rendered,
* but it can't excess the ability of device hardware.
* The maximum frame rate at which the map view is rendered,
* but it can't excess the ability of device hardware.
*
* @param maximumFps Can be set to arbitrary integer values.
* @param maximumFps Can be set to arbitrary integer values.
*/
public void setMaximumFps(int maximumFps) {
if (mapRenderer != null) {
Expand Down Expand Up @@ -747,8 +762,7 @@ public void removeOnDidBecomeIdleListener(OnDidBecomeIdleListener listener) {
}

/**

/**
*
* Set a callback that's invoked when the style has finished loading.
*
* @param listener The callback that's invoked when the style has finished loading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.support.annotation.UiThread;
import android.text.TextUtils;
import android.view.View;

import com.mapbox.android.gestures.AndroidGesturesManager;
import com.mapbox.android.gestures.MoveGestureDetector;
import com.mapbox.android.gestures.RotateGestureDetector;
Expand Down Expand Up @@ -1882,20 +1881,25 @@ public void snapshot(@NonNull SnapshotReadyCallback callback) {
}

/**
* Queries the map for rendered features
* Queries the map for rendered features.
* <p>
* Returns an empty list if either the map or underlying render surface has been destroyed.
* </p>
*
* @param coordinates the point to query
* @param layerIds optionally - only query these layers
* @return the list of feature
*/
@NonNull
public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates, @Nullable String...
layerIds) {
public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates, @Nullable String... layerIds) {
return nativeMapView.queryRenderedFeatures(coordinates, layerIds, null);
}

/**
* Queries the map for rendered features
* <p>
* Returns an empty list if either the map or underlying render surface has been destroyed.
* </p>
*
* @param coordinates the point to query
* @param filter filters the returned features with an expression
Expand All @@ -1911,19 +1915,24 @@ public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates,

/**
* Queries the map for rendered features
* <p>
* Returns an empty list if either the map or underlying render surface has been destroyed.
* </p>
*
* @param coordinates the box to query
* @param layerIds optionally - only query these layers
* @return the list of feature
*/
@NonNull
public List<Feature> queryRenderedFeatures(@NonNull RectF coordinates,
@Nullable String... layerIds) {
public List<Feature> queryRenderedFeatures(@NonNull RectF coordinates, @Nullable String... layerIds) {
return nativeMapView.queryRenderedFeatures(coordinates, layerIds, null);
}

/**
* Queries the map for rendered features
* <p>
* Returns an empty list if either the map or underlying render surface has been destroyed.
* </p>
*
* @param coordinates the box to query
* @param filter filters the returned features with an expression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ 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 @@ -65,9 +65,12 @@ final class NativeMapView implements NativeMap {
// Device density
private final float pixelRatio;

// Flag to indicating destroy was called
// 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 @@ -890,7 +893,7 @@ public Bitmap getImage(String name) {
public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates,
@Nullable String[] layerIds,
@Nullable Expression filter) {
if (checkState("queryRenderedFeatures")) {
if (checkState("queryRenderedFeatures") || !hasSurface) {
return new ArrayList<>();
}
Feature[] features = nativeQueryRenderedFeaturesForPoint(coordinates.x / pixelRatio,
Expand All @@ -903,7 +906,7 @@ public List<Feature> queryRenderedFeatures(@NonNull PointF coordinates,
public List<Feature> queryRenderedFeatures(@NonNull RectF coordinates,
@Nullable String[] layerIds,
@Nullable Expression filter) {
if (checkState("queryRenderedFeatures")) {
if (checkState("queryRenderedFeatures") || !hasSurface) {
return new ArrayList<>();
}
Feature[] features = nativeQueryRenderedFeaturesForBox(
Expand Down Expand Up @@ -1418,6 +1421,16 @@ public boolean isDestroyed() {
return destroyed;
}

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

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

public interface ViewCallback {
int getWidth();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.support.annotation.CallSuper;
import android.support.annotation.Keep;
import android.support.annotation.NonNull;

import com.mapbox.mapboxsdk.LibraryLoader;
import com.mapbox.mapboxsdk.log.Logger;
import com.mapbox.mapboxsdk.maps.MapboxMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class GLSurfaceViewMapRenderer extends MapRenderer implements GLSurfaceVi

@NonNull
private final GLSurfaceView glSurfaceView;
private boolean hasSurface;

public GLSurfaceViewMapRenderer(Context context,
GLSurfaceView glSurfaceView,
Expand All @@ -33,11 +34,18 @@ public GLSurfaceViewMapRenderer(Context context,
glSurfaceView.setRenderer(this);
glSurfaceView.setRenderMode(RENDERMODE_WHEN_DIRTY);
glSurfaceView.setPreserveEGLContextOnPause(true);

glSurfaceView.getHolder().addCallback(new SurfaceHolderCallbackAdapter() {

@Override
public void surfaceCreated(SurfaceHolder holder) {
super.surfaceCreated(holder);
hasSurface = true;
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
super.surfaceDestroyed(holder);
hasSurface = false;
onSurfaceDestroyed();
}
});
Expand Down Expand Up @@ -73,6 +81,11 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
super.onSurfaceCreated(gl, config);
}

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

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
super.onSurfaceChanged(gl, width, height);
Expand All @@ -90,6 +103,9 @@ public void onDrawFrame(GL10 gl) {
*/
@Override
public void requestRender() {
if (!hasSurface) {
return;
}
glSurfaceView.requestRender();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mapbox.mapboxsdk.integration

import android.support.test.filters.LargeTest
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import com.mapbox.mapboxsdk.testapp.activity.feature.QueryRenderedFeaturesBoxCountActivity
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Regression test that validates reopening an Activity and querying the map before surface recreation #14394
*/
@RunWith(AndroidJUnit4::class)
class QueryRenderedFeaturesBoxCountTest : BaseIntegrationTest() {

@get:Rule
var activityRule: ActivityTestRule<QueryRenderedFeaturesBoxCountActivity> =
ActivityTestRule(QueryRenderedFeaturesBoxCountActivity::class.java)

@Test
@LargeTest
fun reopenQueryRendererFeaturesActivity() {
device.waitForIdle()
device.pressHome()
device.waitForIdle()
device.launchActivity(activityRule.activity, QueryRenderedFeaturesBoxCountActivity::class.java)
device.waitForIdle()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@
<activity
android:name=".activity.feature.QueryRenderedFeaturesBoxCountActivity"
android:description="@string/description_query_rendered_features_box_count"
android:label="@string/activity_query_rendered_features_box_count">
android:label="@string/activity_query_rendered_features_box_count"
android:launchMode="singleInstance">
<meta-data
android:name="@string/category"
android:value="@string/category_features" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapbox.mapboxsdk.testapp.activity.feature;

import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -89,6 +90,11 @@ public MapboxMap getMapboxMap() {
protected void onStart() {
super.onStart();
mapView.onStart();

if (mapboxMap != null) {
// Regression test for #14394
mapboxMap.queryRenderedFeatures(new PointF(0,0));
}
}

@Override
Expand Down