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

Commit

Permalink
[android] - translucent surface on TextureView
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Feb 13, 2018
1 parent 734b420 commit 143c346
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
private void initialiseDrawingSurface(MapboxMapOptions options) {
if (options.getTextureMode()) {
TextureView textureView = new TextureView(getContext());
mapRenderer = new TextureViewMapRenderer(getContext(), textureView, options.getLocalIdeographFontFamily()) {
String localFontFamily = options.getLocalIdeographFontFamily();
boolean translucentSurface = options.getTranslucentTextureSurface();
mapRenderer = new TextureViewMapRenderer(getContext(), textureView, localFontFamily, translucentSurface) {
@Override
protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.onSurfaceCreated();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class MapboxMapOptions implements Parcelable {
private String apiBaseUrl;

private boolean textureMode;
private boolean translucentTextureSurface;

private String style;

Expand Down Expand Up @@ -156,6 +157,7 @@ private MapboxMapOptions(Parcel in) {
style = in.readString();
apiBaseUrl = in.readString();
textureMode = in.readByte() != 0;
translucentTextureSurface = in.readByte() != 0;
prefetchesTiles = in.readByte() != 0;
zMediaOverlay = in.readByte() != 0;
localIdeographFontFamily = in.readString();
Expand Down Expand Up @@ -289,6 +291,8 @@ public static MapboxMapOptions createFromAttributes(@NonNull Context context, @N
typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_myLocationAccuracyThreshold, 0));
mapboxMapOptions.textureMode(
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_renderTextureMode, false));
mapboxMapOptions.translucentTextureSurface(
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_renderTextureTranslucentSurface, false));
mapboxMapOptions.setPrefetchesTiles(
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_enableTilePrefetch, true));
mapboxMapOptions.renderSurfaceOnTop(
Expand Down Expand Up @@ -711,6 +715,11 @@ public MapboxMapOptions textureMode(boolean textureMode) {
return this;
}

public MapboxMapOptions translucentTextureSurface(boolean translucentTextureSurface) {
this.translucentTextureSurface = translucentTextureSurface;
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.
Expand Down Expand Up @@ -1085,6 +1094,10 @@ public boolean getTextureMode() {
return textureMode;
}

public boolean getTranslucentTextureSurface() {
return translucentTextureSurface;
}

/**
* Returns the font-family for locally overriding generation of glyphs in the
* ‘CJK Unified Ideographs’ and ‘Hangul Syllables’ ranges.
Expand Down Expand Up @@ -1159,6 +1172,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(style);
dest.writeString(apiBaseUrl);
dest.writeByte((byte) (textureMode ? 1 : 0));
dest.writeByte((byte) (translucentTextureSurface ? 1 : 0));
dest.writeByte((byte) (prefetchesTiles ? 1 : 0));
dest.writeByte((byte) (zMediaOverlay ? 1 : 0));
dest.writeString(localIdeographFontFamily);
Expand Down Expand Up @@ -1340,6 +1354,7 @@ public int hashCode() {
? Float.floatToIntBits(myLocationAccuracyThreshold) : 0);
result = 31 * result + (apiBaseUrl != null ? apiBaseUrl.hashCode() : 0);
result = 31 * result + (textureMode ? 1 : 0);
result = 31 * result + (translucentTextureSurface ? 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
Expand Up @@ -54,6 +54,17 @@ public class EGLConfigChooser implements GLSurfaceView.EGLConfigChooser {
@SuppressWarnings("JavadocReference")
private static final int EGL_OPENGL_ES2_BIT = 0x0004;

private boolean translucentSurface;

public EGLConfigChooser() {
this(false);
}

public EGLConfigChooser(boolean translucentSurface) {
super();
this.translucentSurface = translucentSurface;
}

@Override
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
int[] configAttribs = getConfigAttributes();
Expand Down Expand Up @@ -274,7 +285,7 @@ private int[] getConfigAttributes() {
EGL_RED_SIZE, 5,
EGL_GREEN_SIZE, 6,
EGL_BLUE_SIZE, 5,
EGL_ALPHA_SIZE, 0,
EGL_ALPHA_SIZE, translucentSurface ? 8 : 0,
EGL_DEPTH_SIZE, 16,
EGL_STENCIL_SIZE, 8,
(emulator ? EGL_NONE : EGL_CONFORMANT), EGL_OPENGL_ES2_BIT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@
*/
public class TextureViewMapRenderer extends MapRenderer {
private TextureViewRenderThread renderThread;
private boolean translucentSurface;

/**
* Create a {@link MapRenderer} for the given {@link TextureView}
*
* @param context the current Context
* @param textureView the TextureView
* @param context the current Context
* @param textureView the TextureView
* @param localIdeographFontFamily the local font family
* @param translucentSurface the translucency flag
*/
public TextureViewMapRenderer(@NonNull Context context,
@NonNull TextureView textureView,
String localIdeographFontFamily) {
String localIdeographFontFamily,
boolean translucentSurface) {
super(context, localIdeographFontFamily);
this.translucentSurface = translucentSurface;
renderThread = new TextureViewRenderThread(textureView, this);
renderThread.start();
}
Expand Down Expand Up @@ -95,4 +100,8 @@ public void onStart() {
public void onDestroy() {
renderThread.onDestroy();
}

public boolean isTranslucentSurface() {
return translucentSurface;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ class TextureViewRenderThread extends Thread implements TextureView.SurfaceTextu
*/
@UiThread
TextureViewRenderThread(@NonNull TextureView textureView, @NonNull TextureViewMapRenderer mapRenderer) {
textureView.setOpaque(!mapRenderer.isTranslucentSurface());
textureView.setSurfaceTextureListener(this);
this.mapRenderer = mapRenderer;
this.eglHolder = new EGLHolder(new WeakReference<>(textureView));
this.eglHolder = new EGLHolder(new WeakReference<>(textureView), mapRenderer.isTranslucentSurface());
}

// SurfaceTextureListener methods
Expand Down Expand Up @@ -324,15 +325,17 @@ public void run() {
private static class EGLHolder {
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
private final WeakReference<TextureView> textureViewWeakRef;
private boolean translucentSurface;

private EGL10 egl;
private EGLConfig eglConfig;
private EGLDisplay eglDisplay = EGL10.EGL_NO_DISPLAY;
private EGLContext eglContext = EGL10.EGL_NO_CONTEXT;
private EGLSurface eglSurface = EGL10.EGL_NO_SURFACE;

EGLHolder(WeakReference<TextureView> textureViewWeakRef) {
EGLHolder(WeakReference<TextureView> textureViewWeakRef, boolean translucentSurface) {
this.textureViewWeakRef = textureViewWeakRef;
this.translucentSurface = translucentSurface;
}

void prepare() {
Expand All @@ -357,7 +360,7 @@ void prepare() {
eglConfig = null;
eglContext = EGL10.EGL_NO_CONTEXT;
} else if (eglContext == EGL10.EGL_NO_CONTEXT) {
eglConfig = new EGLConfigChooser().chooseConfig(egl, eglDisplay);
eglConfig = new EGLConfigChooser(translucentSurface).chooseConfig(egl, eglDisplay);
int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE};
eglContext = egl.eglCreateContext(eglDisplay, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@

<!-- Use TextureView-->
<public name="mapbox_renderTextureMode" type="attr" />
<public name="mapbox_renderTextureTranslucentSurface" type="attr" />

<public name="mapbox_enableTilePrefetch" type="attr" />
<public name="mapbox_enableZMediaOverlay" type="attr" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@

<!-- Use TextureView-->
<attr name="mapbox_renderTextureMode" format="boolean"/>
<attr name="mapbox_renderTextureTranslucentSurface" format="boolean"/>

<attr name="mapbox_enableTilePrefetch" format="boolean"/>
<attr name="mapbox_enableZMediaOverlay" format="boolean"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,17 @@
android:name="@string/category"
android:value="@string/category_textureview"/>
</activity>
<activity
android:name=".activity.textureview.TextureViewTransparentBackgroundActivity"
android:description="@string/description_textureview_transparent"
android:label="@string/activity_textureview_transparent">
<meta-data
android:name="@string/category"
android:value="@string/category_textureview"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity
android:name=".activity.textureview.TextureViewAnimationActivity"
android:description="@string/description_textureview_animate"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ public void onLowMemory() {
mapView.onLowMemory();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.mapbox.mapboxsdk.testapp.activity.textureview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;

import java.io.IOException;

import timber.log.Timber;

/**
* Example showcasing how to create a TextureView with a transparent background.
*/
public class TextureViewTransparentBackgroundActivity extends AppCompatActivity {

private MapView mapView;
private MapboxMap mapboxMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_textureview_transparent);
setupBackground();
setupMapView(savedInstanceState);
}

private void setupBackground() {
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.water);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}

private void setupMapView(Bundle savedInstanceState) {
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(map -> {
mapboxMap = map;

try {
map.setStyleJson(ResourceUtils.readRawResource(getApplicationContext(), R.raw.no_bg_style));
} catch (IOException exception) {
Timber.e(exception);
}
});
}

@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}

@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}

@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:layout_height="match_parent"
android:orientation="vertical">


<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@null"/>

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_cameraTargetLat="48.507879"
app:mapbox_cameraTargetLng="8.363795"
app:mapbox_cameraZoom="2"
app:mapbox_renderTextureMode="true"
app:mapbox_renderTextureTranslucentSurface="true"
app:mapbox_styleUrl="mapbox://styles/agerace-globant/cja02de7193b02suvy4gpbt2c"/>

</android.support.design.widget.CoordinatorLayout>

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"version": 8,
"name": "Land",
"metadata": {
"mapbox:autocomposite": true,
},
"sources": {
"composite": {
"url": "mapbox://mapbox.mapbox-terrain-v2",
"type": "vector"
}
},
"sprite": "mapbox://sprites/mapbox/mapbox-terrain-v2",
"glyphs": "mapbox://fonts/mapbox/{fontstack}/{range}.pbf",
"layers": [
{
"layout": {
"visibility": "visible"
},
"type": "fill",
"source": "composite",
"id": "admin",
"paint": {
"fill-color": "hsl(359, 100%, 50%)",
"fill-opacity": 1
},
"source-layer": "landcover"
},
{
"layout": {
"visibility": "visible"
},
"type": "fill",
"source": "composite",
"id": "layer-0",
"paint": {
"fill-opacity": 1,
"fill-color": "hsl(359, 100%, 50%)"
},
"source-layer": "Layer_0"
}
]
}
Loading

0 comments on commit 143c346

Please sign in to comment.