summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobrun <tobrun.van.nuland@gmail.com>2017-01-13 15:51:22 +0100
committerTobrun <tobrun.van.nuland@gmail.com>2017-02-10 16:37:53 +0100
commit9b05af8f45f669dc1f79938a1112adac3db7ca00 (patch)
tree6e4881a485fa3d7729f3c73769b3c4af7e9eeafa
parentb1e623246f3dd6339f9e182fb5650bb4d7060059 (diff)
downloadqtlocation-mapboxgl-9b05af8f45f669dc1f79938a1112adac3db7ca00.tar.gz
[android] - glsurfaceview
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java139
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java34
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/res-public/values/public.xml3
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/res/layout/mapbox_mapview_internal.xml5
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java2
-rwxr-xr-xplatform/android/src/native_map_view.cpp79
6 files changed, 78 insertions, 184 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 edff9b036d..efc1001467 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
@@ -10,10 +10,10 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Canvas;
import android.graphics.PointF;
-import android.graphics.SurfaceTexture;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
+import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.IntDef;
@@ -26,10 +26,6 @@ import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
-import android.view.Surface;
-import android.view.SurfaceHolder;
-import android.view.SurfaceView;
-import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
@@ -54,6 +50,11 @@ 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 android.opengl.GLSurfaceView.RENDERMODE_WHEN_DIRTY;
+
/**
* <p>
* A {@code MapView} provides an embeddable map interface.
@@ -68,12 +69,13 @@ import java.util.List;
* <strong>Warning:</strong> Please note that you are responsible for getting permission to use the map data,
* and for ensuring your use adheres to the relevant terms of use.
*/
-public class MapView extends FrameLayout {
+public class MapView extends FrameLayout implements GLSurfaceView.Renderer {
private NativeMapView nativeMapView;
private boolean destroyed;
private boolean hasSurface = false;
+ private GLSurfaceView glSurfaceView;
private MapboxMap mapboxMap;
private MapCallback mapCallback;
private boolean onStartCalled;
@@ -121,7 +123,7 @@ public class MapView extends FrameLayout {
CompassView compassView = (CompassView) view.findViewById(R.id.compassView);
MyLocationView myLocationView = (MyLocationView) view.findViewById(R.id.userLocationView);
ImageView attrView = (ImageView) view.findViewById(R.id.attributionView);
- initalizeDrawingSurface(context, options);
+ initialiseDrawingSurface();
// create native Map object
nativeMapView = new NativeMapView(this);
@@ -173,16 +175,30 @@ public class MapView extends FrameLayout {
mapboxMap.initialise(context, options);
}
- private void initalizeDrawingSurface(Context context, MapboxMapOptions options) {
- if (options.getTextureMode()) {
- TextureView textureView = new TextureView(context);
- textureView.setSurfaceTextureListener(new SurfaceTextureListener());
- addView(textureView, 0);
- } else {
- SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
- surfaceView.getHolder().addCallback(new SurfaceCallback());
- surfaceView.setVisibility(View.VISIBLE);
- }
+ private void initialiseDrawingSurface() {
+ glSurfaceView = (GLSurfaceView) findViewById(R.id.surfaceView);
+ glSurfaceView.setEGLContextClientVersion(2);
+ glSurfaceView.setRenderer(this);
+ glSurfaceView.setRenderMode(RENDERMODE_WHEN_DIRTY);
+ }
+
+ @Override
+ public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
+ nativeMapView.initializeDisplay();
+ nativeMapView.initializeContext();
+ nativeMapView.createSurface(glSurfaceView.getHolder().getSurface());
+ hasSurface = true;
+ }
+
+ @Override
+ public void onSurfaceChanged(GL10 gl10, int i, int i1) {
+ nativeMapView.resizeView(i, i1);
+ nativeMapView.resizeFramebuffer(i, i1);
+ }
+
+ @Override
+ public void onDrawFrame(GL10 gl10) {
+
}
//
@@ -210,10 +226,6 @@ public class MapView extends FrameLayout {
mapboxMap.onRestoreInstanceState(savedInstanceState);
}
- // Initialize EGL
- nativeMapView.initializeDisplay();
- nativeMapView.initializeContext();
-
addOnMapChangedListener(mapCallback = new MapCallback(mapboxMap));
}
@@ -236,8 +248,9 @@ public class MapView extends FrameLayout {
@UiThread
public void onStart() {
onStartCalled = true;
- mapboxMap.onStart();
registerConnectivityReceiver();
+ mapboxMap.onStart();
+ glSurfaceView.onResume();
}
/**
@@ -266,8 +279,9 @@ public class MapView extends FrameLayout {
@UiThread
public void onStop() {
onStopCalled = true;
- mapboxMap.onStop();
unregisterConnectivityReceiver();
+ mapboxMap.onStop();
+ glSurfaceView.onPause();
}
/**
@@ -282,6 +296,7 @@ public class MapView extends FrameLayout {
}
destroyed = true;
+ hasSurface = false;
nativeMapView.terminateContext();
nativeMapView.terminateDisplay();
nativeMapView.destroySurface();
@@ -455,84 +470,6 @@ public class MapView extends FrameLayout {
}
}
- private class SurfaceCallback implements SurfaceHolder.Callback {
-
- private Surface surface;
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- nativeMapView.createSurface(surface = holder.getSurface());
- hasSurface = true;
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- if (destroyed) {
- return;
- }
- nativeMapView.resizeFramebuffer(width, height);
- }
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- hasSurface = false;
-
- if (nativeMapView != null) {
- nativeMapView.destroySurface();
- }
- surface.release();
- }
- }
-
- // This class handles TextureView callbacks
- private class SurfaceTextureListener implements TextureView.SurfaceTextureListener {
-
- private Surface surface;
-
- // Called when the native surface texture has been created
- // Must do all EGL/GL ES initialization here
- @Override
- public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
- nativeMapView.createSurface(this.surface = new Surface(surface));
- nativeMapView.resizeFramebuffer(width, height);
- hasSurface = true;
- }
-
- // Called when the native surface texture has been destroyed
- // Must do all EGL/GL ES destruction here
- @Override
- public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
- hasSurface = false;
-
- if (nativeMapView != null) {
- nativeMapView.destroySurface();
- }
- this.surface.release();
- return true;
- }
-
- // Called when the format or size of the native surface texture has been changed
- // Must handle window resizing here.
- @Override
- public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
- if (destroyed) {
- return;
- }
-
- nativeMapView.resizeFramebuffer(width, height);
- }
-
- // Called when the SurfaceTexure frame is drawn to screen
- // Must sync with UI here
- @Override
- public void onSurfaceTextureUpdated(SurfaceTexture surface) {
- if (destroyed) {
- return;
- }
- mapboxMap.onUpdate();
- }
- }
-
//
// View events
//
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java
index 6467033ead..69e5f574ef 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java
@@ -80,9 +80,6 @@ public class MapboxMapOptions implements Parcelable {
private String apiBaseUrl;
- @Deprecated
- private boolean textureMode;
-
private String style;
/**
@@ -144,7 +141,6 @@ public class MapboxMapOptions implements Parcelable {
style = in.readString();
apiBaseUrl = in.readString();
- textureMode = in.readByte() != 0;
}
public static Bitmap getBitmapFromDrawable(Drawable drawable) {
@@ -279,8 +275,6 @@ public class MapboxMapOptions implements Parcelable {
mapboxMapOptions.myLocationAccuracyTint(
typedArray.getColor(R.styleable.mapbox_MapView_mapbox_myLocationAccuracyTintColor,
ColorUtils.getPrimaryColor(context)));
- mapboxMapOptions.textureMode(
- typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_renderTextureMode, false));
} finally {
typedArray.recycle();
}
@@ -655,22 +649,6 @@ public class MapboxMapOptions implements Parcelable {
}
/**
- * Enable TextureView as rendered surface.
- * <p>
- * Since the 4.2.0 release we replaced our TextureView with an SurfaceView implemenation.
- * Enabling this option will use the deprecated TextureView instead.
- * </p>
- *
- * @param textureMode True to enable texture mode
- * @return This
- * @deprecated As of the 4.2.0 release, using TextureView is deprecated.
- */
- public MapboxMapOptions textureMode(boolean textureMode) {
- this.textureMode = textureMode;
- return this;
- }
-
- /**
* Get the current configured API endpoint base URL.
*
* @return Base URL to be used API endpoint.
@@ -959,16 +937,6 @@ public class MapboxMapOptions implements Parcelable {
return debugActive;
}
- /**
- * Returns true if TextureView is being used a render view.
- *
- * @return True if TextureView is used.
- * @deprecated As of the 4.2.0 release, using TextureView is deprecated.
- */
- public boolean getTextureMode() {
- return textureMode;
- }
-
public static final Parcelable.Creator<MapboxMapOptions> CREATOR = new Parcelable.Creator<MapboxMapOptions>() {
public MapboxMapOptions createFromParcel(Parcel in) {
return new MapboxMapOptions(in);
@@ -1029,7 +997,6 @@ public class MapboxMapOptions implements Parcelable {
dest.writeString(style);
dest.writeString(apiBaseUrl);
- dest.writeByte((byte) (textureMode ? 1 : 0));
}
@Override
@@ -1186,7 +1153,6 @@ public class MapboxMapOptions implements Parcelable {
result = 31 * result + myLocationAccuracyTintColor;
result = 31 * result + myLocationAccuracyAlpha;
result = 31 * result + (apiBaseUrl != null ? apiBaseUrl.hashCode() : 0);
- result = 31 * result + (textureMode ? 1 : 0);
result = 31 * result + (style != null ? style.hashCode() : 0);
return result;
}
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/res-public/values/public.xml b/platform/android/MapboxGLAndroidSDK/src/main/res-public/values/public.xml
index e786aaca4c..e4abe1fc13 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/res-public/values/public.xml
+++ b/platform/android/MapboxGLAndroidSDK/src/main/res-public/values/public.xml
@@ -71,9 +71,6 @@
<public name="mapbox_uiAttributionMarginRight" type="attr" />
<public name="mapbox_uiAttributionMarginBottom" type="attr" />
- <!-- Deprecated to use TextureView-->
- <public name="mapbox_renderTextureMode" type="attr" />
-
<!-- Exposed styles -->
<public name="mapbox_style_mapbox_streets" type="string" />
<public name="mapbox_style_emerald" type="string" />
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/res/layout/mapbox_mapview_internal.xml b/platform/android/MapboxGLAndroidSDK/src/main/res/layout/mapbox_mapview_internal.xml
index 9810e8900b..c407dc344a 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/res/layout/mapbox_mapview_internal.xml
+++ b/platform/android/MapboxGLAndroidSDK/src/main/res/layout/mapbox_mapview_internal.xml
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
- <SurfaceView
+ <android.opengl.GLSurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:visibility="gone" />
+ android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/markerViewContainer"
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java
index c37d9bb92e..a88d98161f 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java
@@ -52,8 +52,6 @@ public class PolygonActivity extends AppCompatActivity implements OnMapReadyCall
// configure inital map state
MapboxMapOptions options = new MapboxMapOptions()
.attributionTintColor(RED_COLOR)
- // deprecated feature!
- .textureMode(true)
.compassFadesWhenFacingNorth(false)
.styleUrl(Style.MAPBOX_STREETS)
.camera(new CameraPosition.Builder()
diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp
index 663b38963d..e994afc7b8 100755
--- a/platform/android/src/native_map_view.cpp
+++ b/platform/android/src/native_map_view.cpp
@@ -151,10 +151,13 @@ void NativeMapView::deactivate() {
void NativeMapView::invalidate() {
assert(vm != nullptr);
assert(obj != nullptr);
-
- env->CallVoidMethod(obj, onInvalidateId);
- if (env->ExceptionCheck()) {
- env->ExceptionDescribe();
+ JNIEnv *env2;
+ jboolean renderDetach = attach_jni_thread(theJVM, &env2, "Callback Thread");
+ if (!renderDetach) {
+ env2->CallVoidMethod(obj, onInvalidateId);
+ if(env2->ExceptionCheck()) {
+ env2->ExceptionDescribe();
+ }
}
}
@@ -169,18 +172,21 @@ void NativeMapView::render() {
updateViewBinding();
map->render(*this);
- if(snapshot){
- snapshot = false;
+ if (snapshot){
+ snapshot = false;
- // take snapshot
- auto image = getContext().readFramebuffer<mbgl::PremultipliedImage>(getFramebufferSize());
- auto bitmap = Bitmap::CreateBitmap(*env, std::move(image));
+ // take snapshot
+ auto image = getContext().readFramebuffer<mbgl::PremultipliedImage>(getFramebufferSize());
+ auto bitmap = Bitmap::CreateBitmap(*env, std::move(image));
- // invoke Mapview#OnSnapshotReady
- env->CallVoidMethod(obj, onSnapshotReadyId, jni::Unwrap(*bitmap));
- if (env->ExceptionCheck()) {
- env->ExceptionDescribe();
- }
+ JNIEnv *env2;
+ jboolean renderDetach = attach_jni_thread(theJVM, &env2, "Callback Thread");
+ if (!renderDetach) {
+ env2->CallVoidMethod(obj, onSnapshotReadyId, jni::Unwrap(*bitmap));
+ if (env2->ExceptionCheck()) {
+ env2->ExceptionDescribe();
+ }
+ }
}
if ((display != EGL_NO_DISPLAY) && (surface != EGL_NO_SURFACE)) {
@@ -205,7 +211,7 @@ void NativeMapView::initializeDisplay() {
assert(config == nullptr);
assert(format < 0);
- display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+ display = eglGetCurrentDisplay();
if (display == EGL_NO_DISPLAY) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglGetDisplay() returned error %d", eglGetError());
throw std::runtime_error("eglGetDisplay() failed");
@@ -287,15 +293,7 @@ void NativeMapView::initializeDisplay() {
void NativeMapView::terminateDisplay() {
if (display != EGL_NO_DISPLAY) {
- // Destroy the surface first, if it still exists. This call needs a valid surface.
- if (surface != EGL_NO_SURFACE) {
- if (!eglDestroySurface(display, surface)) {
- mbgl::Log::Error(mbgl::Event::OpenGL, "eglDestroySurface() returned error %d",
- eglGetError());
- throw std::runtime_error("eglDestroySurface() failed");
- }
- surface = EGL_NO_SURFACE;
- }
+ surface = EGL_NO_SURFACE;
if (!eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
mbgl::Log::Error(mbgl::Event::OpenGL,
@@ -320,8 +318,7 @@ void NativeMapView::initializeContext() {
assert(context == EGL_NO_CONTEXT);
assert(config != nullptr);
- const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
- context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
+ context = eglGetCurrentContext();
if (context == EGL_NO_CONTEXT) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglCreateContext() returned error %d",
eglGetError());
@@ -338,13 +335,6 @@ void NativeMapView::terminateContext() {
throw std::runtime_error("eglMakeCurrent() failed");
}
- if (context != EGL_NO_CONTEXT) {
- if (!eglDestroyContext(display, context)) {
- mbgl::Log::Error(mbgl::Event::OpenGL, "eglDestroyContext() returned error %d",
- eglGetError());
- throw std::runtime_error("eglDestroyContext() failed");
- }
- }
}
context = EGL_NO_CONTEXT;
@@ -362,8 +352,7 @@ void NativeMapView::createSurface(ANativeWindow *window_) {
ANativeWindow_setBuffersGeometry(window, 0, 0, format);
- const EGLint surfaceAttribs[] = {EGL_NONE};
- surface = eglCreateWindowSurface(display, config, window, surfaceAttribs);
+ surface = eglGetCurrentSurface(EGL_DRAW);
if (surface == EGL_NO_SURFACE) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglCreateWindowSurface() returned error %d",
eglGetError());
@@ -592,9 +581,13 @@ void NativeMapView::notifyMapChange(mbgl::MapChange change) {
assert(vm != nullptr);
assert(obj != nullptr);
- env->CallVoidMethod(obj, onMapChangedId, change);
- if (env->ExceptionCheck()) {
- env->ExceptionDescribe();
+ JNIEnv *env2;
+ jboolean renderDetach = attach_jni_thread(theJVM, &env2, "Callback Thread");
+ if (!renderDetach) {
+ env2->CallVoidMethod(obj, onMapChangedId, change);
+ if(env2->ExceptionCheck()) {
+ env2->ExceptionDescribe();
+ }
}
}
@@ -625,9 +618,13 @@ void NativeMapView::updateFps() {
assert(vm != nullptr);
assert(obj != nullptr);
- env->CallVoidMethod(obj, onFpsChangedId, fps);
- if (env->ExceptionCheck()) {
- env->ExceptionDescribe();
+ JNIEnv *env2;
+ jboolean renderDetach = attach_jni_thread(theJVM, &env2, "Callback Thread");
+ if (!renderDetach) {
+ env2->CallVoidMethod(obj, onFpsChangedId, fps);
+ if(env2->ExceptionCheck()) {
+ env2->ExceptionDescribe();
+ }
}
}