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

Cherry pick TextureView #10370

Merged
merged 6 commits into from
Nov 3, 2017
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 @@ -15,6 +15,7 @@
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
Expand All @@ -27,8 +28,9 @@
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.MapRenderer;
import com.mapbox.mapboxsdk.maps.renderer.glsurfaceview.GLSurfaceViewMapRenderer;
import com.mapbox.mapboxsdk.maps.renderer.textureview.TextureViewMapRenderer;
import com.mapbox.mapboxsdk.maps.widgets.CompassView;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationView;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
Expand All @@ -48,7 +50,6 @@

import timber.log.Timber;

import static android.opengl.GLSurfaceView.RENDERMODE_WHEN_DIRTY;
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 @@ -87,7 +88,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) {
Expand Down Expand Up @@ -139,7 +140,7 @@ public void onGlobalLayout() {
} else {
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
initialiseDrawingSurface();
initialiseDrawingSurface(options);
}
});
}
Expand Down Expand Up @@ -289,33 +290,52 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
}
}

private void initialiseDrawingSurface() {
glSurfaceView = (GLSurfaceView) findViewById(R.id.surfaceView);
glSurfaceView.setZOrderMediaOverlay(mapboxMapOptions.getRenderSurfaceOnTop());
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setEGLConfigChooser(new EGLConfigChooser());
private void initialiseDrawingSurface(MapboxMapOptions options) {
if (options.getTextureMode()) {
TextureView textureView = new TextureView(getContext());
mapRenderer = new TextureViewMapRenderer(getContext(), textureView) {
@Override
protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.post(new Runnable() {
@Override
public void run() {
// Initialise only once
if (mapboxMap == null) {
initialiseMap();
mapboxMap.onStart();
}
}
});

MapRenderer mapRenderer = new MapRenderer(getContext(), glSurfaceView) {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.post(new Runnable() {
@Override
public void run() {
// Initialise only once
if (mapboxMap == null) {
initialiseMap();
mapboxMap.onStart();
super.onSurfaceCreated(gl, config);
}
};
addView(textureView, 0);
} else {
GLSurfaceView glSurfaceView = (GLSurfaceView) findViewById(R.id.surfaceView);
glSurfaceView.setZOrderMediaOverlay(mapboxMapOptions.getRenderSurfaceOnTop());

mapRenderer = new GLSurfaceViewMapRenderer(getContext(), glSurfaceView) {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.post(new Runnable() {
@Override
public void run() {
// Initialise only once
if (mapboxMap == null) {
initialiseMap();
mapboxMap.onStart();
}
}
}
});
});

super.onSurfaceCreated(gl, config);
}
};
super.onSurfaceCreated(gl, config);
}
};

glSurfaceView.setVisibility(View.VISIBLE);

glSurfaceView.setRenderer(mapRenderer);
glSurfaceView.setRenderMode(RENDERMODE_WHEN_DIRTY);
glSurfaceView.setVisibility(View.VISIBLE);
}

nativeMapView = new NativeMapView(this, mapRenderer);
nativeMapView.resizeView(getMeasuredWidth(), getMeasuredHeight());
Expand Down Expand Up @@ -352,8 +372,8 @@ public void onStart() {
*/
@UiThread
public void onResume() {
if (glSurfaceView != null) {
glSurfaceView.onResume();
if (mapRenderer != null) {
mapRenderer.onResume();
}
}

Expand All @@ -362,8 +382,8 @@ public void onResume() {
*/
@UiThread
public void onPause() {
if (glSurfaceView != null) {
glSurfaceView.onPause();
if (mapRenderer != null) {
mapRenderer.onPause();
}
}

Expand All @@ -389,6 +409,10 @@ public void onDestroy() {
mapCallback.clearOnMapReadyCallbacks();
nativeMapView.destroy();
nativeMapView = null;

if (mapRenderer != null) {
mapRenderer.onDestroy();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public class MapboxMapOptions implements Parcelable {

private String apiBaseUrl;

private boolean textureMode;

private String style;

/**
Expand Down Expand Up @@ -152,7 +154,7 @@ private MapboxMapOptions(Parcel in) {

style = in.readString();
apiBaseUrl = in.readString();

textureMode = in.readByte() != 0;
prefetchesTiles = in.readByte() != 0;
zMediaOverlay = in.readByte() != 0;
}
Expand Down Expand Up @@ -296,6 +298,8 @@ public static MapboxMapOptions createFromAttributes(@NonNull Context context, @N
ColorUtils.getPrimaryColor(context)));
mapboxMapOptions.myLocationAccuracyThreshold(
typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_myLocationAccuracyThreshold, 0));
mapboxMapOptions.textureMode(
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_renderTextureMode, false));
mapboxMapOptions.setPrefetchesTiles(
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_enableTilePrefetch, true));
mapboxMapOptions.renderSurfaceOnTop(
Expand Down Expand Up @@ -698,13 +702,30 @@ public MapboxMapOptions myLocationAccuracyThreshold(float myLocationAccuracyThre
return this;
}

/**
* Enable {@link android.view.TextureView} as rendered surface.
* <p>
* Since the 5.2.0 release we replaced our TextureView with an {@link android.opengl.GLSurfaceView}
* implementation. Enabling this option will use the {@link android.view.TextureView} instead.
* {@link android.view.TextureView} can be useful in situations where you need to animate, scale
* or transform the view. This comes at a siginficant performance penalty and should not be considered
* unless absolutely needed.
* </p>
*
* @param textureMode True to enable texture mode
* @return This
*/
public MapboxMapOptions textureMode(boolean textureMode) {
this.textureMode = textureMode;
return this;
}

/**
* Enable tile pre-fetching. Loads tiles at a lower zoom-level to pre-render
* a low resolution preview while more detailed tiles are loaded.
* Enabled by default
*
* @param enable true to enable
*
* @return This
*/
public MapboxMapOptions setPrefetchesTiles(boolean enable) {
Expand Down Expand Up @@ -1049,6 +1070,15 @@ public boolean getDebugActive() {
return debugActive;
}

/**
* Returns true if TextureView is being used the render view.
*
* @return True if TextureView is used.
*/
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);
Expand Down Expand Up @@ -1112,7 +1142,7 @@ public void writeToParcel(Parcel dest, int flags) {

dest.writeString(style);
dest.writeString(apiBaseUrl);

dest.writeByte((byte) (textureMode ? 1 : 0));
dest.writeByte((byte) (prefetchesTiles ? 1 : 0));
dest.writeByte((byte) (zMediaOverlay ? 1 : 0));
}
Expand Down Expand Up @@ -1289,6 +1319,7 @@ public int hashCode() {
result = 31 * result + (myLocationAccuracyThreshold != +0.0f
? Float.floatToIntBits(myLocationAccuracyThreshold) : 0);
result = 31 * result + (apiBaseUrl != null ? apiBaseUrl.hashCode() : 0);
result = 31 * result + (textureMode ? 1 : 0);
result = 31 * result + (style != null ? style.hashCode() : 0);
result = 31 * result + (prefetchesTiles ? 1 : 0);
result = 31 * result + (zMediaOverlay ? 1 : 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,17 +16,14 @@
* 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;
Expand All @@ -36,17 +33,29 @@ public MapRenderer(Context context, GLSurfaceView glSurfaceView) {
nativeInitialize(this, fileSource, pixelRatio, programCacheDir);
}

public void onPause() {
// Implement if needed
}

public void onResume() {
// Implement if needed
}

public void onDestroy() {
// 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.");
}
Expand All @@ -69,37 +78,15 @@ public void onSurfaceChanged(GL10 gl, int width, int height) {
nativeOnSurfaceChanged(width, height);
}

@Override
public void onDrawFrame(GL10 gl) {
@CallSuper
protected void onDrawFrame(GL10 gl) {
nativeRender();

if (onFpsChangedListener != null) {
updateFps();
}
}

/**
* 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>
Expand All @@ -109,6 +96,7 @@ public void queueEvent(Runnable runnable) {
* @param runnable the runnable to execute
* @see MapRendererRunnable
*/
@CallSuper
void queueEvent(MapRendererRunnable runnable) {
this.queueEvent((Runnable) runnable);
}
Expand All @@ -118,6 +106,7 @@ private native void nativeInitialize(MapRenderer self,
float pixelRatio,
String programCacheDir);

@CallSuper
@Override
protected native void finalize() throws Throwable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mapbox.mapboxsdk.egl;
package com.mapbox.mapboxsdk.maps.renderer.egl;

import android.opengl.GLSurfaceView;
import android.support.annotation.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mapbox.mapboxsdk.egl;
package com.mapbox.mapboxsdk.maps.renderer.egl;

/**
* Used for EGL configuration exceptions
Expand Down
Loading