[android] encapsulate gl surfaceview rendering specifics in map renderer subclass

- preparation for re-introduction of texture view
This commit is contained in:
Ivo van Dongen
2017-10-02 15:41:02 +03:00
committed by Tobrun
parent d9fe241761
commit 6adeee601e
3 changed files with 113 additions and 46 deletions

View File

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

View File

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

View File

@@ -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) {
@@ -78,28 +84,6 @@ 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>
@@ -109,6 +93,7 @@ public class MapRenderer implements GLSurfaceView.Renderer, MapRendererScheduler
* @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;