diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2017-10-02 15:41:02 +0300 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2017-11-03 09:19:39 -0700 |
commit | 6adeee601e5752af6297265d2bd02e88eca85707 (patch) | |
tree | 15314781e127e0e5e357866801935bea3a8bd52b | |
parent | d9fe24176153f14d1c101f48993860ee776cbfb0 (diff) | |
download | qtlocation-mapboxgl-6adeee601e5752af6297265d2bd02e88eca85707.tar.gz |
[android] encapsulate gl surfaceview rendering specifics in map renderer subclass
- preparation for re-introduction of texture view
3 files changed, 113 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 e45e8be6ad..4fc2a1dffc 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 @@ -27,7 +27,7 @@ import com.mapbox.mapboxsdk.annotations.Annotation; import com.mapbox.mapboxsdk.annotations.MarkerViewManager; import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.constants.Style; -import com.mapbox.mapboxsdk.egl.EGLConfigChooser; +import com.mapbox.mapboxsdk.maps.renderer.GLSurfaceViewMapRenderer; import com.mapbox.mapboxsdk.maps.renderer.MapRenderer; import com.mapbox.mapboxsdk.maps.widgets.CompassView; import com.mapbox.mapboxsdk.maps.widgets.MyLocationView; @@ -87,7 +87,7 @@ public class MapView extends FrameLayout { private Bundle savedInstanceState; private final CopyOnWriteArrayList<OnMapChangedListener> onMapChangedListeners = new CopyOnWriteArrayList<>(); - private GLSurfaceView glSurfaceView; + private MapRenderer mapRenderer; @UiThread public MapView(@NonNull Context context) { @@ -290,12 +290,10 @@ public class MapView extends FrameLayout { } private void initialiseDrawingSurface() { - glSurfaceView = (GLSurfaceView) findViewById(R.id.surfaceView); + GLSurfaceView glSurfaceView = (GLSurfaceView) findViewById(R.id.surfaceView); glSurfaceView.setZOrderMediaOverlay(mapboxMapOptions.getRenderSurfaceOnTop()); - glSurfaceView.setEGLContextClientVersion(2); - glSurfaceView.setEGLConfigChooser(new EGLConfigChooser()); - MapRenderer mapRenderer = new MapRenderer(getContext(), glSurfaceView) { + GLSurfaceViewMapRenderer mapRenderer = new GLSurfaceViewMapRenderer(getContext(), glSurfaceView) { @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { MapView.this.post(new Runnable() { @@ -313,10 +311,9 @@ public class MapView extends FrameLayout { } }; - glSurfaceView.setRenderer(mapRenderer); - glSurfaceView.setRenderMode(RENDERMODE_WHEN_DIRTY); glSurfaceView.setVisibility(View.VISIBLE); + this.mapRenderer = mapRenderer; nativeMapView = new NativeMapView(this, mapRenderer); nativeMapView.resizeView(getMeasuredWidth(), getMeasuredHeight()); } @@ -352,8 +349,8 @@ public class MapView extends FrameLayout { */ @UiThread public void onResume() { - if (glSurfaceView != null) { - glSurfaceView.onResume(); + if (mapRenderer != null) { + mapRenderer.onResume(); } } @@ -362,8 +359,8 @@ public class MapView extends FrameLayout { */ @UiThread public void onPause() { - if (glSurfaceView != null) { - glSurfaceView.onPause(); + if (mapRenderer != null) { + mapRenderer.onPause(); } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/GLSurfaceViewMapRenderer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/GLSurfaceViewMapRenderer.java new file mode 100644 index 0000000000..d0ec9f9495 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/GLSurfaceViewMapRenderer.java @@ -0,0 +1,84 @@ +package com.mapbox.mapboxsdk.maps.renderer; + +import android.content.Context; +import android.opengl.GLSurfaceView; + +import com.mapbox.mapboxsdk.egl.EGLConfigChooser; + +import javax.microedition.khronos.egl.EGLConfig; +import javax.microedition.khronos.opengles.GL10; + +import static android.opengl.GLSurfaceView.RENDERMODE_WHEN_DIRTY; + +/** + * The {@link GLSurfaceViewMapRenderer} encapsulates the GL thread and + * {@link GLSurfaceView} specifics to render the map. + * + * @see MapRenderer + */ +public class GLSurfaceViewMapRenderer extends MapRenderer implements GLSurfaceView.Renderer { + + private final GLSurfaceView glSurfaceView; + + public GLSurfaceViewMapRenderer(Context context, GLSurfaceView glSurfaceView) { + super(context); + this.glSurfaceView = glSurfaceView; + glSurfaceView.setEGLContextClientVersion(2); + glSurfaceView.setEGLConfigChooser(new EGLConfigChooser()); + glSurfaceView.setRenderer(this); + glSurfaceView.setRenderMode(RENDERMODE_WHEN_DIRTY); + } + + @Override + public void onPause() { + if (glSurfaceView != null) { + glSurfaceView.onPause(); + } + } + + @Override + public void onResume() { + if (glSurfaceView != null) { + glSurfaceView.onResume(); + } + } + + @Override + public void onSurfaceCreated(GL10 gl, EGLConfig config) { + super.onSurfaceCreated(gl, config); + } + + @Override + public void onSurfaceChanged(GL10 gl, int width, int height) { + super.onSurfaceChanged(gl, width, height); + } + + @Override + public void onDrawFrame(GL10 gl) { + super.onDrawFrame(gl); + } + + /** + * May be called from any thread. + * <p> + * Called from the renderer frontend to schedule a render. + */ + @Override + public void requestRender() { + glSurfaceView.requestRender(); + } + + /** + * May be called from any thread. + * <p> + * Schedules work to be performed on the MapRenderer thread. + * + * @param runnable the runnable to execute + */ + @Override + public void queueEvent(Runnable runnable) { + glSurfaceView.queueEvent(runnable); + } + + +} 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 3f43522e01..a7c0fa3778 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 @@ -1,7 +1,7 @@ package com.mapbox.mapboxsdk.maps.renderer; import android.content.Context; -import android.opengl.GLSurfaceView; +import android.support.annotation.CallSuper; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.storage.FileSource; @@ -16,17 +16,14 @@ import javax.microedition.khronos.opengles.GL10; * render on the one end and acts as a scheduler to request work to * be performed on the GL thread on the other. */ -public class MapRenderer implements GLSurfaceView.Renderer, MapRendererScheduler { +public abstract class MapRenderer implements MapRendererScheduler { // Holds the pointer to the native peer after initialisation private long nativePtr = 0; - private final GLSurfaceView glSurfaceView; - private MapboxMap.OnFpsChangedListener onFpsChangedListener; - public MapRenderer(Context context, GLSurfaceView glSurfaceView) { - this.glSurfaceView = glSurfaceView; + public MapRenderer(Context context) { FileSource fileSource = FileSource.getInstance(context); float pixelRatio = context.getResources().getDisplayMetrics().density; @@ -36,17 +33,26 @@ public class MapRenderer implements GLSurfaceView.Renderer, MapRendererScheduler nativeInitialize(this, fileSource, pixelRatio, programCacheDir); } + public void onPause() { + // Implement if needed + } + + public void onResume() { + // Implement if needed + } + + public void setOnFpsChangedListener(MapboxMap.OnFpsChangedListener listener) { onFpsChangedListener = listener; } - @Override - public void onSurfaceCreated(GL10 gl, EGLConfig config) { + @CallSuper + protected void onSurfaceCreated(GL10 gl, EGLConfig config) { nativeOnSurfaceCreated(); } - @Override - public void onSurfaceChanged(GL10 gl, int width, int height) { + @CallSuper + protected void onSurfaceChanged(GL10 gl, int width, int height) { if (width < 0) { throw new IllegalArgumentException("fbWidth cannot be negative."); } @@ -69,8 +75,8 @@ public class MapRenderer implements GLSurfaceView.Renderer, MapRendererScheduler nativeOnSurfaceChanged(width, height); } - @Override - public void onDrawFrame(GL10 gl) { + @CallSuper + protected void onDrawFrame(GL10 gl) { nativeRender(); if (onFpsChangedListener != null) { @@ -81,34 +87,13 @@ public class MapRenderer implements GLSurfaceView.Renderer, MapRendererScheduler /** * May be called from any thread. * <p> - * Called from the renderer frontend to schedule a render. - */ - @Override - public void requestRender() { - glSurfaceView.requestRender(); - } - - /** - * May be called from any thread. - * <p> - * Schedules work to be performed on the MapRenderer thread. - * - * @param runnable the runnable to execute - */ - @Override - public void queueEvent(Runnable runnable) { - glSurfaceView.queueEvent(runnable); - } - - /** - * May be called from any thread. - * <p> * Called from the native peer to schedule work on the GL * thread. Explicit override for easier to read jni code. * * @param runnable the runnable to execute * @see MapRendererRunnable */ + @CallSuper void queueEvent(MapRendererRunnable runnable) { this.queueEvent((Runnable) runnable); } @@ -118,6 +103,7 @@ public class MapRenderer implements GLSurfaceView.Renderer, MapRendererScheduler float pixelRatio, String programCacheDir); + @CallSuper @Override protected native void finalize() throws Throwable; |