Add Android Vulkan backend (#2711)
Co-authored-by: Bart Louwers <bart.louwers@gmail.com>
This commit is contained in:
@@ -8,7 +8,6 @@ import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.TextureView;
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
@@ -30,9 +29,6 @@ import org.maplibre.android.constants.MapLibreConstants;
|
||||
import org.maplibre.android.exceptions.MapLibreConfigurationException;
|
||||
import org.maplibre.android.location.LocationComponent;
|
||||
import org.maplibre.android.maps.renderer.MapRenderer;
|
||||
import org.maplibre.android.maps.renderer.glsurfaceview.GLSurfaceViewMapRenderer;
|
||||
import org.maplibre.android.maps.renderer.glsurfaceview.MapLibreGLSurfaceView;
|
||||
import org.maplibre.android.maps.renderer.textureview.TextureViewMapRenderer;
|
||||
import org.maplibre.android.maps.widgets.CompassView;
|
||||
import org.maplibre.android.net.ConnectivityReceiver;
|
||||
import org.maplibre.android.storage.FileSource;
|
||||
@@ -43,9 +39,6 @@ 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 org.maplibre.android.maps.widgets.CompassView.TIME_MAP_NORTH_ANIMATION;
|
||||
import static org.maplibre.android.maps.widgets.CompassView.TIME_WAIT_IDLE;
|
||||
|
||||
@@ -313,37 +306,12 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
|
||||
}
|
||||
|
||||
private void initialiseDrawingSurface(MapLibreMapOptions options) {
|
||||
String localFontFamily = options.getLocalIdeographFontFamily();
|
||||
if (options.getTextureMode()) {
|
||||
TextureView textureView = new TextureView(getContext());
|
||||
boolean translucentSurface = options.getTranslucentTextureSurface();
|
||||
mapRenderer = new TextureViewMapRenderer(getContext(),
|
||||
textureView, localFontFamily, translucentSurface) {
|
||||
@Override
|
||||
protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
MapView.this.onSurfaceCreated();
|
||||
super.onSurfaceCreated(gl, config);
|
||||
}
|
||||
};
|
||||
mapRenderer = MapRenderer.create(options, getContext(), () -> MapView.this.onSurfaceCreated());
|
||||
renderView = mapRenderer.getView();
|
||||
|
||||
addView(textureView, 0);
|
||||
renderView = textureView;
|
||||
} else {
|
||||
MapLibreGLSurfaceView glSurfaceView = new MapLibreGLSurfaceView(getContext());
|
||||
glSurfaceView.setZOrderMediaOverlay(maplibreMapOptions.getRenderSurfaceOnTop());
|
||||
mapRenderer = new GLSurfaceViewMapRenderer(getContext(), glSurfaceView, localFontFamily) {
|
||||
@Override
|
||||
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
MapView.this.onSurfaceCreated();
|
||||
super.onSurfaceCreated(gl, config);
|
||||
}
|
||||
};
|
||||
addView(renderView, 0);
|
||||
|
||||
addView(glSurfaceView, 0);
|
||||
renderView = glSurfaceView;
|
||||
}
|
||||
|
||||
boolean crossSourceCollisions = maplibreMapOptions.getCrossSourceCollisions();
|
||||
boolean crossSourceCollisions = options.getCrossSourceCollisions();
|
||||
nativeMapView = new NativeMapView(
|
||||
getContext(), getPixelRatio(), crossSourceCollisions, this, mapChangeReceiver, mapRenderer
|
||||
);
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package org.maplibre.android.maps.renderer;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.Surface;
|
||||
import android.view.TextureView;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.CallSuper;
|
||||
import androidx.annotation.Keep;
|
||||
@@ -9,16 +12,14 @@ import androidx.annotation.NonNull;
|
||||
import org.maplibre.android.LibraryLoader;
|
||||
import org.maplibre.android.log.Logger;
|
||||
import org.maplibre.android.maps.MapLibreMap;
|
||||
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
import org.maplibre.android.maps.MapLibreMapOptions;
|
||||
|
||||
/**
|
||||
* The {@link MapRenderer} encapsulates the GL thread.
|
||||
* The {@link MapRenderer} encapsulates the render thread.
|
||||
* <p>
|
||||
* Performs actions on the GL thread to manage the GL resources and
|
||||
* Performs actions on the render thread to manage the resources and
|
||||
* render on the one end and acts as a scheduler to request work to
|
||||
* be performed on the GL thread on the other.
|
||||
* be performed on the render thread on the other.
|
||||
*/
|
||||
@Keep
|
||||
public abstract class MapRenderer implements MapRendererScheduler {
|
||||
@@ -34,6 +35,25 @@ public abstract class MapRenderer implements MapRendererScheduler {
|
||||
private double expectedRenderTime = 0;
|
||||
private MapLibreMap.OnFpsChangedListener onFpsChangedListener;
|
||||
|
||||
public static MapRenderer create(MapLibreMapOptions options, @NonNull Context context, Runnable initCallback) {
|
||||
|
||||
MapRenderer renderer = null;
|
||||
String localFontFamily = options.getLocalIdeographFontFamily();
|
||||
|
||||
if (options.getTextureMode()) {
|
||||
TextureView textureView = new TextureView(context);
|
||||
boolean translucentSurface = options.getTranslucentTextureSurface();
|
||||
renderer = MapRendererFactory.newTextureViewMapRenderer(context, textureView, localFontFamily,
|
||||
translucentSurface, initCallback);
|
||||
} else {
|
||||
boolean renderSurfaceOnTop = options.getRenderSurfaceOnTop();
|
||||
renderer = MapRendererFactory.newSurfaceViewMapRenderer(context, localFontFamily,
|
||||
renderSurfaceOnTop, initCallback);
|
||||
}
|
||||
|
||||
return renderer;
|
||||
}
|
||||
|
||||
public MapRenderer(@NonNull Context context, String localIdeographFontFamily) {
|
||||
float pixelRatio = context.getResources().getDisplayMetrics().density;
|
||||
|
||||
@@ -41,6 +61,8 @@ public abstract class MapRenderer implements MapRendererScheduler {
|
||||
nativeInitialize(this, pixelRatio, localIdeographFontFamily);
|
||||
}
|
||||
|
||||
public abstract View getView();
|
||||
|
||||
public void onStart() {
|
||||
// Implement if needed
|
||||
}
|
||||
@@ -66,13 +88,12 @@ public abstract class MapRenderer implements MapRendererScheduler {
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
nativeOnSurfaceCreated();
|
||||
protected void onSurfaceCreated(Surface surface) {
|
||||
nativeOnSurfaceCreated(surface);
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
protected void onSurfaceChanged(@NonNull GL10 gl, int width, int height) {
|
||||
gl.glViewport(0, 0, width, height);
|
||||
protected void onSurfaceChanged(int width, int height) {
|
||||
nativeOnSurfaceChanged(width, height);
|
||||
}
|
||||
|
||||
@@ -82,7 +103,7 @@ public abstract class MapRenderer implements MapRendererScheduler {
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
protected void onDrawFrame(GL10 gl) {
|
||||
protected void onDrawFrame() {
|
||||
long startTime = System.nanoTime();
|
||||
try {
|
||||
nativeRender();
|
||||
@@ -128,7 +149,7 @@ public abstract class MapRenderer implements MapRendererScheduler {
|
||||
@Override
|
||||
protected native void finalize() throws Throwable;
|
||||
|
||||
private native void nativeOnSurfaceCreated();
|
||||
private native void nativeOnSurfaceCreated(Surface surface);
|
||||
|
||||
private native void nativeOnSurfaceChanged(int width, int height);
|
||||
|
||||
|
||||
@@ -1,309 +0,0 @@
|
||||
package org.maplibre.android.maps.renderer.egl;
|
||||
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.os.Build;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.maplibre.android.constants.MapLibreConstants;
|
||||
import org.maplibre.android.log.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.microedition.khronos.egl.EGL10;
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.egl.EGLDisplay;
|
||||
|
||||
import static org.maplibre.android.utils.Compare.compare;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_ALPHA_MASK_SIZE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_ALPHA_SIZE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_BLUE_SIZE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_BUFFER_SIZE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_COLOR_BUFFER_TYPE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_CONFIG_CAVEAT;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_DEPTH_SIZE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_GREEN_SIZE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_NONE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_RED_SIZE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_RENDERABLE_TYPE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_RGB_BUFFER;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_SAMPLES;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_SAMPLE_BUFFERS;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_STENCIL_SIZE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_SURFACE_TYPE;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_WINDOW_BIT;
|
||||
|
||||
/**
|
||||
* Selects the right EGLConfig needed for `mapbox-gl-native`
|
||||
*/
|
||||
public class EGLConfigChooser implements GLSurfaceView.EGLConfigChooser {
|
||||
|
||||
private static final String TAG = "Mbgl-EGLConfigChooser";
|
||||
|
||||
/**
|
||||
* Requires API level 18
|
||||
*
|
||||
* @see android.opengl.EGL15.EGL_OPENGL_ES3_BIT;
|
||||
*/
|
||||
@SuppressWarnings("JavadocReference")
|
||||
private static final int EGL_OPENGL_ES3_BIT = 0x0040;
|
||||
|
||||
private boolean translucentSurface;
|
||||
|
||||
public EGLConfigChooser() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
public EGLConfigChooser(boolean translucentSurface) {
|
||||
super();
|
||||
this.translucentSurface = translucentSurface;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EGLConfig chooseConfig(@NonNull EGL10 egl, EGLDisplay display) {
|
||||
int[] configAttribs = getConfigAttributes();
|
||||
|
||||
// Determine number of possible configurations
|
||||
int[] numConfigs = getNumberOfConfigurations(egl, display, configAttribs);
|
||||
if (numConfigs[0] < 1) {
|
||||
Logger.e(TAG, "eglChooseConfig() returned no configs.");
|
||||
}
|
||||
|
||||
// Get all possible configurations
|
||||
EGLConfig[] possibleConfigurations = getPossibleConfigurations(egl, display, configAttribs, numConfigs);
|
||||
|
||||
// Choose best match
|
||||
EGLConfig config = chooseBestMatchConfig(egl, display, possibleConfigurations);
|
||||
if (config == null) {
|
||||
Logger.e(TAG, "No config chosen");
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private int[] getNumberOfConfigurations(EGL10 egl, EGLDisplay display, int[] configAttributes) {
|
||||
int[] numConfigs = new int[1];
|
||||
if (!egl.eglChooseConfig(display, configAttributes, null, 0, numConfigs)) {
|
||||
Logger.e(TAG, String.format(
|
||||
MapLibreConstants.MAPLIBRE_LOCALE, "eglChooseConfig(NULL) returned error %d", egl.eglGetError())
|
||||
);
|
||||
}
|
||||
return numConfigs;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private EGLConfig[] getPossibleConfigurations(EGL10 egl, EGLDisplay display,
|
||||
int[] configAttributes, int[] numConfigs) {
|
||||
EGLConfig[] configs = new EGLConfig[numConfigs[0]];
|
||||
if (!egl.eglChooseConfig(display, configAttributes, configs, numConfigs[0], numConfigs)) {
|
||||
Logger.e(TAG, String.format(
|
||||
MapLibreConstants.MAPLIBRE_LOCALE, "eglChooseConfig() returned error %d", egl.eglGetError())
|
||||
);
|
||||
}
|
||||
return configs;
|
||||
}
|
||||
|
||||
// Quality
|
||||
enum BufferFormat {
|
||||
Format16Bit(3),
|
||||
Format32BitNoAlpha(1),
|
||||
Format32BitAlpha(2),
|
||||
Format24Bit(0),
|
||||
Unknown(4);
|
||||
|
||||
int value;
|
||||
|
||||
BufferFormat(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
enum DepthStencilFormat {
|
||||
Format16Depth8Stencil(1),
|
||||
Format24Depth8Stencil(0);
|
||||
|
||||
int value;
|
||||
|
||||
DepthStencilFormat(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
private EGLConfig chooseBestMatchConfig(@NonNull EGL10 egl, EGLDisplay display, EGLConfig[] configs) {
|
||||
class Config implements Comparable<Config> {
|
||||
private final BufferFormat bufferFormat;
|
||||
private final DepthStencilFormat depthStencilFormat;
|
||||
private final boolean isCaveat;
|
||||
private final int index;
|
||||
private final EGLConfig config;
|
||||
|
||||
public Config(BufferFormat bufferFormat, DepthStencilFormat depthStencilFormat,
|
||||
boolean isCaveat, int index, EGLConfig config) {
|
||||
this.bufferFormat = bufferFormat;
|
||||
this.depthStencilFormat = depthStencilFormat;
|
||||
this.isCaveat = isCaveat;
|
||||
this.index = index;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int compareTo(@NonNull Config other) {
|
||||
int i = compare(bufferFormat.value, other.bufferFormat.value);
|
||||
if (i != 0) {
|
||||
return i;
|
||||
}
|
||||
|
||||
i = compare(depthStencilFormat.value, other.depthStencilFormat.value);
|
||||
if (i != 0) {
|
||||
return i;
|
||||
}
|
||||
|
||||
i = compare(isCaveat, other.isCaveat);
|
||||
if (i != 0) {
|
||||
return i;
|
||||
}
|
||||
|
||||
i = compare(index, other.index);
|
||||
if (i != 0) {
|
||||
return i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
List<Config> matches = new ArrayList<>();
|
||||
|
||||
int i = 0;
|
||||
for (EGLConfig config : configs) {
|
||||
if (config == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
i++;
|
||||
|
||||
int caveat = getConfigAttr(egl, display, config, EGL_CONFIG_CAVEAT);
|
||||
int bits = getConfigAttr(egl, display, config, EGL_BUFFER_SIZE);
|
||||
int red = getConfigAttr(egl, display, config, EGL_RED_SIZE);
|
||||
int green = getConfigAttr(egl, display, config, EGL_GREEN_SIZE);
|
||||
int blue = getConfigAttr(egl, display, config, EGL_BLUE_SIZE);
|
||||
int alpha = getConfigAttr(egl, display, config, EGL_ALPHA_SIZE);
|
||||
int alphaMask = getConfigAttr(egl, display, config, EGL_ALPHA_MASK_SIZE);
|
||||
int depth = getConfigAttr(egl, display, config, EGL_DEPTH_SIZE);
|
||||
int stencil = getConfigAttr(egl, display, config, EGL_STENCIL_SIZE);
|
||||
int sampleBuffers = getConfigAttr(egl, display, config, EGL_SAMPLE_BUFFERS);
|
||||
int samples = getConfigAttr(egl, display, config, EGL_SAMPLES);
|
||||
|
||||
boolean configOk = (depth == 24) || (depth == 16);
|
||||
configOk &= stencil == 8;
|
||||
configOk &= sampleBuffers == 0;
|
||||
configOk &= samples == 0;
|
||||
|
||||
// Filter our configs first for depth, stencil and anti-aliasing
|
||||
if (configOk) {
|
||||
// Work out the config's buffer format
|
||||
BufferFormat bufferFormat;
|
||||
if ((bits == 16) && (red == 5) && (green == 6) && (blue == 5) && (alpha == 0)) {
|
||||
bufferFormat = BufferFormat.Format16Bit;
|
||||
} else if ((bits == 32) && (red == 8) && (green == 8) && (blue == 8) && (alpha == 0)) {
|
||||
bufferFormat = BufferFormat.Format32BitNoAlpha;
|
||||
} else if ((bits == 32) && (red == 8) && (green == 8) && (blue == 8) && (alpha == 8)) {
|
||||
bufferFormat = BufferFormat.Format32BitAlpha;
|
||||
} else if ((bits == 24) && (red == 8) && (green == 8) && (blue == 8) && (alpha == 0)) {
|
||||
bufferFormat = BufferFormat.Format24Bit;
|
||||
} else {
|
||||
bufferFormat = BufferFormat.Unknown;
|
||||
}
|
||||
|
||||
// Work out the config's depth stencil format
|
||||
DepthStencilFormat depthStencilFormat;
|
||||
if ((depth == 16) && (stencil == 8)) {
|
||||
depthStencilFormat = DepthStencilFormat.Format16Depth8Stencil;
|
||||
} else {
|
||||
depthStencilFormat = DepthStencilFormat.Format24Depth8Stencil;
|
||||
}
|
||||
|
||||
boolean isCaveat = caveat != EGL_NONE;
|
||||
|
||||
// Ignore formats we don't recognise
|
||||
if (bufferFormat != BufferFormat.Unknown) {
|
||||
matches.add(new Config(bufferFormat, depthStencilFormat, isCaveat, i, config));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Sort
|
||||
Collections.sort(matches);
|
||||
|
||||
if (matches.size() == 0) {
|
||||
Logger.e(TAG, "No matching configurations after filtering");
|
||||
return null;
|
||||
}
|
||||
|
||||
Config bestMatch = matches.get(0);
|
||||
|
||||
if (bestMatch.isCaveat) {
|
||||
Logger.w(TAG, "Chosen config has a caveat.");
|
||||
}
|
||||
|
||||
return bestMatch.config;
|
||||
}
|
||||
|
||||
private int getConfigAttr(EGL10 egl, EGLDisplay display, EGLConfig config, int attributeName) {
|
||||
int[] attributevalue = new int[1];
|
||||
if (!egl.eglGetConfigAttrib(display, config, attributeName, attributevalue)) {
|
||||
Logger.e(TAG, String.format(
|
||||
MapLibreConstants.MAPLIBRE_LOCALE, "eglGetConfigAttrib(%d) returned error %d", attributeName, egl.eglGetError())
|
||||
);
|
||||
}
|
||||
return attributevalue[0];
|
||||
}
|
||||
|
||||
private int[] getConfigAttributes() {
|
||||
boolean emulator = inEmulator() || inGenymotion();
|
||||
Logger.i(TAG, String.format("In emulator: %s", emulator));
|
||||
|
||||
// Get all configs at least RGB 565 with 16 depth and 8 stencil
|
||||
return new int[] {
|
||||
EGL_CONFIG_CAVEAT, EGL_NONE,
|
||||
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
||||
EGL_BUFFER_SIZE, 16,
|
||||
EGL_RED_SIZE, 5,
|
||||
EGL_GREEN_SIZE, 6,
|
||||
EGL_BLUE_SIZE, 5,
|
||||
EGL_ALPHA_SIZE, translucentSurface ? 8 : 0,
|
||||
EGL_DEPTH_SIZE, 16,
|
||||
EGL_STENCIL_SIZE, 8,
|
||||
(emulator ? EGL_NONE : EGL_COLOR_BUFFER_TYPE), EGL_RGB_BUFFER,
|
||||
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
|
||||
EGL_NONE
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect if we are in emulator.
|
||||
*/
|
||||
private boolean inEmulator() {
|
||||
return Build.FINGERPRINT.startsWith("generic")
|
||||
|| Build.FINGERPRINT.startsWith("unknown")
|
||||
|| Build.MODEL.contains("google_sdk")
|
||||
|| Build.MODEL.contains("Emulator")
|
||||
|| Build.MODEL.contains("Android SDK built for x86")
|
||||
|| (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
|
||||
|| "google_sdk".equals(Build.PRODUCT)
|
||||
|| System.getProperty("ro.kernel.qemu") != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect if we are in genymotion
|
||||
*/
|
||||
private boolean inGenymotion() {
|
||||
return Build.MANUFACTURER.contains("Genymotion");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package org.maplibre.android.maps.renderer.egl;
|
||||
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import javax.microedition.khronos.egl.EGL10;
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.egl.EGLContext;
|
||||
import javax.microedition.khronos.egl.EGLDisplay;
|
||||
|
||||
public class EGLContextFactory implements GLSurfaceView.EGLContextFactory {
|
||||
|
||||
public EGLContext createContext(EGL10 egl, @Nullable EGLDisplay display, @Nullable EGLConfig config) {
|
||||
if (display == null || config == null) {
|
||||
return EGL10.EGL_NO_CONTEXT;
|
||||
}
|
||||
int[] attrib_list = {0x3098, 2, EGL10.EGL_NONE};
|
||||
return egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, attrib_list);
|
||||
}
|
||||
|
||||
public void destroyContext(EGL10 egl, EGLDisplay display,
|
||||
EGLContext context) {
|
||||
if (!egl.eglDestroyContext(display, context)) {
|
||||
Log.e("DefaultContextFactory", "display:" + display + " context: " + context);
|
||||
Log.i("DefaultContextFactory", "tid=" + Thread.currentThread().getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,546 +0,0 @@
|
||||
package org.maplibre.android.maps.renderer.egl;
|
||||
|
||||
import android.opengl.GLDebugHelper;
|
||||
import android.opengl.GLException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
import javax.microedition.khronos.egl.EGL;
|
||||
import javax.microedition.khronos.egl.EGL10;
|
||||
import javax.microedition.khronos.egl.EGL11;
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.egl.EGLContext;
|
||||
import javax.microedition.khronos.egl.EGLDisplay;
|
||||
import javax.microedition.khronos.egl.EGLSurface;
|
||||
|
||||
public class EGLLogWrapper implements EGL11 {
|
||||
private EGL10 egl10;
|
||||
private Writer log;
|
||||
private boolean logArgumentNames;
|
||||
private boolean checkError;
|
||||
private int argCount;
|
||||
|
||||
public EGLLogWrapper(EGL egl, int configFlags, Writer log) {
|
||||
egl10 = (EGL10) egl;
|
||||
this.log = log;
|
||||
logArgumentNames = (GLDebugHelper.CONFIG_LOG_ARGUMENT_NAMES & configFlags) != 0;
|
||||
checkError = (GLDebugHelper.CONFIG_CHECK_GL_ERROR & configFlags) != 0;
|
||||
}
|
||||
|
||||
public boolean eglChooseConfig(EGLDisplay display, int[] attrib_list,
|
||||
EGLConfig[] configs, int config_size, int[] num_config) {
|
||||
begin("eglChooseConfig");
|
||||
arg("display", display);
|
||||
arg("attrib_list", attrib_list);
|
||||
arg("config_size", config_size);
|
||||
end();
|
||||
|
||||
boolean result = egl10.eglChooseConfig(display, attrib_list, configs,
|
||||
config_size, num_config);
|
||||
arg("configs", configs);
|
||||
arg("num_config", num_config);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglCopyBuffers(EGLDisplay display, EGLSurface surface,
|
||||
Object native_pixmap) {
|
||||
begin("eglCopyBuffers");
|
||||
arg("display", display);
|
||||
arg("surface", surface);
|
||||
arg("native_pixmap", native_pixmap);
|
||||
end();
|
||||
|
||||
boolean result = egl10.eglCopyBuffers(display, surface, native_pixmap);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public EGLContext eglCreateContext(EGLDisplay display, EGLConfig config,
|
||||
EGLContext share_context, int[] attrib_list) {
|
||||
begin("eglCreateContext");
|
||||
arg("display", display);
|
||||
arg("config", config);
|
||||
arg("share_context", share_context);
|
||||
arg("attrib_list", attrib_list);
|
||||
end();
|
||||
|
||||
EGLContext result = egl10.eglCreateContext(display, config,
|
||||
share_context, attrib_list);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public EGLSurface eglCreatePbufferSurface(EGLDisplay display,
|
||||
EGLConfig config, int[] attrib_list) {
|
||||
begin("eglCreatePbufferSurface");
|
||||
arg("display", display);
|
||||
arg("config", config);
|
||||
arg("attrib_list", attrib_list);
|
||||
end();
|
||||
|
||||
EGLSurface result = egl10.eglCreatePbufferSurface(display, config,
|
||||
attrib_list);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public EGLSurface eglCreatePixmapSurface(EGLDisplay display,
|
||||
EGLConfig config, Object native_pixmap, int[] attrib_list) {
|
||||
begin("eglCreatePixmapSurface");
|
||||
arg("display", display);
|
||||
arg("config", config);
|
||||
arg("native_pixmap", native_pixmap);
|
||||
arg("attrib_list", attrib_list);
|
||||
end();
|
||||
|
||||
EGLSurface result = egl10.eglCreatePixmapSurface(display, config,
|
||||
native_pixmap, attrib_list);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public EGLSurface eglCreateWindowSurface(EGLDisplay display,
|
||||
EGLConfig config, Object native_window, int[] attrib_list) {
|
||||
begin("eglCreateWindowSurface");
|
||||
arg("display", display);
|
||||
arg("config", config);
|
||||
arg("native_window", native_window);
|
||||
arg("attrib_list", attrib_list);
|
||||
end();
|
||||
|
||||
EGLSurface result = egl10.eglCreateWindowSurface(display, config,
|
||||
native_window, attrib_list);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglDestroyContext(EGLDisplay display, EGLContext context) {
|
||||
begin("eglDestroyContext");
|
||||
arg("display", display);
|
||||
arg("context", context);
|
||||
end();
|
||||
|
||||
boolean result = egl10.eglDestroyContext(display, context);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglDestroySurface(EGLDisplay display, EGLSurface surface) {
|
||||
begin("eglDestroySurface");
|
||||
arg("display", display);
|
||||
arg("surface", surface);
|
||||
end();
|
||||
|
||||
boolean result = egl10.eglDestroySurface(display, surface);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglGetConfigAttrib(EGLDisplay display, EGLConfig config,
|
||||
int attribute, int[] value) {
|
||||
begin("eglGetConfigAttrib");
|
||||
arg("display", display);
|
||||
arg("config", config);
|
||||
arg("attribute", attribute);
|
||||
end();
|
||||
boolean result = egl10.eglGetConfigAttrib(display, config, attribute,
|
||||
value);
|
||||
arg("value", value);
|
||||
returns(result);
|
||||
checkError();
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean eglGetConfigs(EGLDisplay display, EGLConfig[] configs,
|
||||
int config_size, int[] num_config) {
|
||||
begin("eglGetConfigs");
|
||||
arg("display", display);
|
||||
arg("config_size", config_size);
|
||||
end();
|
||||
|
||||
boolean result = egl10.eglGetConfigs(display, configs, config_size,
|
||||
num_config);
|
||||
arg("configs", configs);
|
||||
arg("num_config", num_config);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public EGLContext eglGetCurrentContext() {
|
||||
begin("eglGetCurrentContext");
|
||||
end();
|
||||
|
||||
EGLContext result = egl10.eglGetCurrentContext();
|
||||
returns(result);
|
||||
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public EGLDisplay eglGetCurrentDisplay() {
|
||||
begin("eglGetCurrentDisplay");
|
||||
end();
|
||||
|
||||
EGLDisplay result = egl10.eglGetCurrentDisplay();
|
||||
returns(result);
|
||||
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public EGLSurface eglGetCurrentSurface(int readdraw) {
|
||||
begin("eglGetCurrentSurface");
|
||||
arg("readdraw", readdraw);
|
||||
end();
|
||||
|
||||
EGLSurface result = egl10.eglGetCurrentSurface(readdraw);
|
||||
returns(result);
|
||||
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public EGLDisplay eglGetDisplay(Object native_display) {
|
||||
begin("eglGetDisplay");
|
||||
arg("native_display", native_display);
|
||||
end();
|
||||
|
||||
EGLDisplay result = egl10.eglGetDisplay(native_display);
|
||||
returns(result);
|
||||
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public int eglGetError() {
|
||||
begin("eglGetError");
|
||||
end();
|
||||
|
||||
int result = egl10.eglGetError();
|
||||
returns(getErrorString(result));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglInitialize(EGLDisplay display, int[] major_minor) {
|
||||
begin("eglInitialize");
|
||||
arg("display", display);
|
||||
end();
|
||||
boolean result = egl10.eglInitialize(display, major_minor);
|
||||
returns(result);
|
||||
arg("major_minor", major_minor);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglMakeCurrent(EGLDisplay display, EGLSurface draw,
|
||||
EGLSurface read, EGLContext context) {
|
||||
begin("eglMakeCurrent");
|
||||
arg("display", display);
|
||||
arg("draw", draw);
|
||||
arg("read", read);
|
||||
arg("context", context);
|
||||
end();
|
||||
boolean result = egl10.eglMakeCurrent(display, draw, read, context);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglQueryContext(EGLDisplay display, EGLContext context,
|
||||
int attribute, int[] value) {
|
||||
begin("eglQueryContext");
|
||||
arg("display", display);
|
||||
arg("context", context);
|
||||
arg("attribute", attribute);
|
||||
end();
|
||||
boolean result = egl10.eglQueryContext(display, context, attribute,
|
||||
value);
|
||||
returns(value[0]);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public String eglQueryString(EGLDisplay display, int name) {
|
||||
begin("eglQueryString");
|
||||
arg("display", display);
|
||||
arg("name", name);
|
||||
end();
|
||||
String result = egl10.eglQueryString(display, name);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglQuerySurface(EGLDisplay display, EGLSurface surface,
|
||||
int attribute, int[] value) {
|
||||
begin("eglQuerySurface");
|
||||
arg("display", display);
|
||||
arg("surface", surface);
|
||||
arg("attribute", attribute);
|
||||
end();
|
||||
boolean result = egl10.eglQuerySurface(display, surface, attribute,
|
||||
value);
|
||||
returns(value[0]);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglSwapBuffers(EGLDisplay display, EGLSurface surface) {
|
||||
begin("eglSwapBuffers");
|
||||
arg("display", display);
|
||||
arg("surface", surface);
|
||||
end();
|
||||
boolean result = egl10.eglSwapBuffers(display, surface);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglTerminate(EGLDisplay display) {
|
||||
begin("eglTerminate");
|
||||
arg("display", display);
|
||||
end();
|
||||
boolean result = egl10.eglTerminate(display);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglWaitGL() {
|
||||
begin("eglWaitGL");
|
||||
end();
|
||||
boolean result = egl10.eglWaitGL();
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean eglWaitNative(int engine, Object bindTarget) {
|
||||
begin("eglWaitNative");
|
||||
arg("engine", engine);
|
||||
arg("bindTarget", bindTarget);
|
||||
end();
|
||||
boolean result = egl10.eglWaitNative(engine, bindTarget);
|
||||
returns(result);
|
||||
checkError();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void checkError() {
|
||||
int eglError;
|
||||
if ((eglError = egl10.eglGetError()) != EGL_SUCCESS) {
|
||||
String errorMessage = "eglError: " + getErrorString(eglError);
|
||||
logLine(errorMessage);
|
||||
if (checkError) {
|
||||
throw new GLException(eglError, errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void logLine(String message) {
|
||||
log(message + '\n');
|
||||
}
|
||||
|
||||
private void log(String message) {
|
||||
try {
|
||||
log.write(message);
|
||||
} catch (IOException exception) {
|
||||
// Ignore exception, keep on trying
|
||||
}
|
||||
}
|
||||
|
||||
private void begin(String name) {
|
||||
log(name + '(');
|
||||
argCount = 0;
|
||||
}
|
||||
|
||||
private void arg(String name, String value) {
|
||||
if (argCount++ > 0) {
|
||||
log(", ");
|
||||
}
|
||||
if (logArgumentNames) {
|
||||
log(name + "=");
|
||||
}
|
||||
log(value);
|
||||
}
|
||||
|
||||
private void end() {
|
||||
log(");\n");
|
||||
flush();
|
||||
}
|
||||
|
||||
private void flush() {
|
||||
try {
|
||||
log.flush();
|
||||
} catch (IOException exception) {
|
||||
log = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void arg(String name, int value) {
|
||||
arg(name, Integer.toString(value));
|
||||
}
|
||||
|
||||
private void arg(String name, Object object) {
|
||||
arg(name, toString(object));
|
||||
}
|
||||
|
||||
private void arg(String name, EGLDisplay object) {
|
||||
if (object == EGL10.EGL_DEFAULT_DISPLAY) {
|
||||
arg(name, "EGL10.EGL_DEFAULT_DISPLAY");
|
||||
} else if (object == EGL_NO_DISPLAY) {
|
||||
arg(name, "EGL10.EGL_NO_DISPLAY");
|
||||
} else {
|
||||
arg(name, toString(object));
|
||||
}
|
||||
}
|
||||
|
||||
private void arg(String name, EGLContext object) {
|
||||
if (object == EGL10.EGL_NO_CONTEXT) {
|
||||
arg(name, "EGL10.EGL_NO_CONTEXT");
|
||||
} else {
|
||||
arg(name, toString(object));
|
||||
}
|
||||
}
|
||||
|
||||
private void arg(String name, EGLSurface object) {
|
||||
if (object == EGL10.EGL_NO_SURFACE) {
|
||||
arg(name, "EGL10.EGL_NO_SURFACE");
|
||||
} else {
|
||||
arg(name, toString(object));
|
||||
}
|
||||
}
|
||||
|
||||
private void returns(String result) {
|
||||
log(" returns " + result + ";\n");
|
||||
flush();
|
||||
}
|
||||
|
||||
private void returns(int result) {
|
||||
returns(Integer.toString(result));
|
||||
}
|
||||
|
||||
private void returns(boolean result) {
|
||||
returns(Boolean.toString(result));
|
||||
}
|
||||
|
||||
private void returns(Object result) {
|
||||
returns(toString(result));
|
||||
}
|
||||
|
||||
private String toString(Object obj) {
|
||||
if (obj == null) {
|
||||
return "null";
|
||||
} else {
|
||||
return obj.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private void arg(String name, int[] arr) {
|
||||
if (arr == null) {
|
||||
arg(name, "null");
|
||||
} else {
|
||||
arg(name, toString(arr.length, arr, 0));
|
||||
}
|
||||
}
|
||||
|
||||
private void arg(String name, Object[] arr) {
|
||||
if (arr == null) {
|
||||
arg(name, "null");
|
||||
} else {
|
||||
arg(name, toString(arr.length, arr, 0));
|
||||
}
|
||||
}
|
||||
|
||||
private String toString(int n, int[] arr, int offset) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("{\n");
|
||||
int arrLen = arr.length;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int index = offset + i;
|
||||
buf.append(" [" + index + "] = ");
|
||||
if (index < 0 || index >= arrLen) {
|
||||
buf.append("out of bounds");
|
||||
} else {
|
||||
buf.append(arr[index]);
|
||||
}
|
||||
buf.append('\n');
|
||||
}
|
||||
buf.append("}");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
private String toString(int n, Object[] arr, int offset) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("{\n");
|
||||
int arrLen = arr.length;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int index = offset + i;
|
||||
buf.append(" [" + index + "] = ");
|
||||
if (index < 0 || index >= arrLen) {
|
||||
buf.append("out of bounds");
|
||||
} else {
|
||||
buf.append(arr[index]);
|
||||
}
|
||||
buf.append('\n');
|
||||
}
|
||||
buf.append("}");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
private static String getHex(int value) {
|
||||
return "0x" + Integer.toHexString(value);
|
||||
}
|
||||
|
||||
public static String getErrorString(int error) {
|
||||
switch (error) {
|
||||
case EGL_SUCCESS:
|
||||
return "EGL_SUCCESS";
|
||||
case EGL_NOT_INITIALIZED:
|
||||
return "EGL_NOT_INITIALIZED";
|
||||
case EGL_BAD_ACCESS:
|
||||
return "EGL_BAD_ACCESS";
|
||||
case EGL_BAD_ALLOC:
|
||||
return "EGL_BAD_ALLOC";
|
||||
case EGL_BAD_ATTRIBUTE:
|
||||
return "EGL_BAD_ATTRIBUTE";
|
||||
case EGL_BAD_CONFIG:
|
||||
return "EGL_BAD_CONFIG";
|
||||
case EGL_BAD_CONTEXT:
|
||||
return "EGL_BAD_CONTEXT";
|
||||
case EGL_BAD_CURRENT_SURFACE:
|
||||
return "EGL_BAD_CURRENT_SURFACE";
|
||||
case EGL_BAD_DISPLAY:
|
||||
return "EGL_BAD_DISPLAY";
|
||||
case EGL_BAD_MATCH:
|
||||
return "EGL_BAD_MATCH";
|
||||
case EGL_BAD_NATIVE_PIXMAP:
|
||||
return "EGL_BAD_NATIVE_PIXMAP";
|
||||
case EGL_BAD_NATIVE_WINDOW:
|
||||
return "EGL_BAD_NATIVE_WINDOW";
|
||||
case EGL_BAD_PARAMETER:
|
||||
return "EGL_BAD_PARAMETER";
|
||||
case EGL_BAD_SURFACE:
|
||||
return "EGL_BAD_SURFACE";
|
||||
case EGL11.EGL_CONTEXT_LOST:
|
||||
return "EGL_CONTEXT_LOST";
|
||||
default:
|
||||
return getHex(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package org.maplibre.android.maps.renderer.egl;
|
||||
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import javax.microedition.khronos.egl.EGL10;
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.egl.EGLDisplay;
|
||||
import javax.microedition.khronos.egl.EGLSurface;
|
||||
|
||||
public class EGLWindowSurfaceFactory implements GLSurfaceView.EGLWindowSurfaceFactory {
|
||||
public EGLSurface createWindowSurface(@NonNull EGL10 egl, @Nullable EGLDisplay display, @Nullable EGLConfig config,
|
||||
@Nullable Object nativeWindow) {
|
||||
EGLSurface result = null;
|
||||
if (display != null && config != null && nativeWindow != null) {
|
||||
try {
|
||||
result = egl.eglCreateWindowSurface(display, config, nativeWindow, null);
|
||||
} catch (Exception exception) {
|
||||
// This exception indicates that the surface flinger surface
|
||||
// is not valid. This can happen if the surface flinger surface has
|
||||
// been torn down, but the application has not yet been
|
||||
// notified via SurfaceHolder.Callback.surfaceDestroyed.
|
||||
// In theory the application should be notified first,
|
||||
// but in practice sometimes it is not. See b/4588890
|
||||
Log.e("EGLWindowSurfaceFactory", "eglCreateWindowSurface", exception);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void destroySurface(EGL10 egl, EGLDisplay display,
|
||||
EGLSurface surface) {
|
||||
egl.eglDestroySurface(display, surface);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
/**
|
||||
* Contains the MapLibre Maps Android EGL API classes.
|
||||
*/
|
||||
package org.maplibre.android.maps.renderer.egl;
|
||||
@@ -1,126 +0,0 @@
|
||||
package org.maplibre.android.maps.renderer.glsurfaceview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.opengl.GLSurfaceView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.maplibre.android.maps.renderer.MapRenderer;
|
||||
import org.maplibre.android.maps.renderer.egl.EGLConfigChooser;
|
||||
import org.maplibre.android.maps.renderer.egl.EGLContextFactory;
|
||||
import org.maplibre.android.maps.renderer.egl.EGLWindowSurfaceFactory;
|
||||
|
||||
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 {
|
||||
|
||||
@NonNull
|
||||
private final MapLibreGLSurfaceView glSurfaceView;
|
||||
|
||||
public GLSurfaceViewMapRenderer(Context context,
|
||||
MapLibreGLSurfaceView glSurfaceView,
|
||||
String localIdeographFontFamily) {
|
||||
super(context, localIdeographFontFamily);
|
||||
this.glSurfaceView = glSurfaceView;
|
||||
glSurfaceView.setEGLContextFactory(new EGLContextFactory());
|
||||
glSurfaceView.setEGLWindowSurfaceFactory(new EGLWindowSurfaceFactory());
|
||||
glSurfaceView.setEGLConfigChooser(new EGLConfigChooser());
|
||||
glSurfaceView.setRenderer(this);
|
||||
glSurfaceView.setRenderMode(RENDERMODE_WHEN_DIRTY);
|
||||
glSurfaceView.setPreserveEGLContextOnPause(true);
|
||||
glSurfaceView.setDetachedListener(new MapLibreGLSurfaceView.OnGLSurfaceViewDetachedListener() {
|
||||
@Override
|
||||
public void onGLSurfaceViewDetached() {
|
||||
// because the GL thread is destroyed when the view is detached from window,
|
||||
// we need to ensure releasing the native renderer as well.
|
||||
// This avoids releasing it only when the view is being recreated, which is already on a new GL thread,
|
||||
// and leads to JNI crashes like https://github.com/mapbox/mapbox-gl-native/issues/14618
|
||||
nativeReset();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
glSurfaceView.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
glSurfaceView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
super.onSurfaceCreated(gl, config);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSurfaceDestroyed() {
|
||||
super.onSurfaceDestroyed();
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void waitForEmpty() {
|
||||
glSurfaceView.waitForEmpty();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,543 @@
|
||||
package org.maplibre.android.maps.renderer.surfaceview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class MapLibreSurfaceView extends SurfaceView implements SurfaceHolder.Callback2 {
|
||||
|
||||
protected static final String TAG = "MapLibreSurfaceView";
|
||||
protected static final RenderThreadManager renderThreadManager = new RenderThreadManager();
|
||||
|
||||
protected SurfaceViewMapRenderer renderer;
|
||||
protected RenderThread renderThread;
|
||||
protected OnSurfaceViewDetachedListener detachedListener;
|
||||
|
||||
protected boolean detached;
|
||||
|
||||
/**
|
||||
* The renderer only renders
|
||||
* when the surface is created, or when {@link #requestRender} is called.
|
||||
*
|
||||
* @see #getRenderMode()
|
||||
* @see #setRenderMode(int)
|
||||
* @see #requestRender()
|
||||
*/
|
||||
public static final int RENDERMODE_WHEN_DIRTY = 0;
|
||||
/**
|
||||
* The renderer is called
|
||||
* continuously to re-render the scene.
|
||||
*
|
||||
* @see #getRenderMode()
|
||||
* @see #setRenderMode(int)
|
||||
*/
|
||||
public static final int RENDERMODE_CONTINUOUSLY = 1;
|
||||
|
||||
/**
|
||||
* Standard View constructor. In order to render something, you
|
||||
* must call {@link #setRenderer} to register a renderer.
|
||||
*/
|
||||
public MapLibreSurfaceView(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard View constructor. In order to render something, you
|
||||
* must call {@link #setRenderer} to register a renderer.
|
||||
*/
|
||||
public MapLibreSurfaceView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
// Install a SurfaceHolder.Callback so we get notified when the
|
||||
// underlying surface is created and destroyed
|
||||
SurfaceHolder holder = getHolder();
|
||||
holder.setFormat(PixelFormat.TRANSPARENT);
|
||||
holder.addCallback(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
try {
|
||||
if (renderThread != null) {
|
||||
// thread may still be running if this view was never
|
||||
// attached to a window.
|
||||
renderThread.requestExitAndWait();
|
||||
}
|
||||
} finally {
|
||||
super.finalize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a listener that gets notified when the view is detached from window.
|
||||
*
|
||||
* @param detachedListener listener
|
||||
*/
|
||||
public void setDetachedListener(@NonNull OnSurfaceViewDetachedListener detachedListener) {
|
||||
if (this.detachedListener != null) {
|
||||
throw new IllegalArgumentException("Detached from window listener has been already set.");
|
||||
}
|
||||
this.detachedListener = detachedListener;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Set the renderer associated with this view. Also starts the thread that
|
||||
* will call the renderer, which in turn causes the rendering to start.
|
||||
* <p>This method should be called once and only once in the life-cycle of
|
||||
* a SurfaceView.
|
||||
* The following SurfaceView methods can only be called <em>after</em>
|
||||
* setRenderer is called:
|
||||
* <ul>
|
||||
* <li>{@link #getRenderMode()}
|
||||
* <li>{@link #onPause()}
|
||||
* <li>{@link #onResume()}
|
||||
* <li>{@link #queueEvent(Runnable)}
|
||||
* <li>{@link #requestRender()}
|
||||
* <li>{@link #setRenderMode(int)}
|
||||
* </ul>
|
||||
*
|
||||
* @param renderer the renderer to use to perform drawing.
|
||||
*/
|
||||
public void setRenderer(SurfaceViewMapRenderer renderer) {
|
||||
checkRenderThreadState();
|
||||
this.renderer = renderer;
|
||||
|
||||
createRenderThread();
|
||||
renderThread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the rendering mode. When renderMode is
|
||||
* RENDERMODE_CONTINUOUSLY, the renderer is called
|
||||
* repeatedly to re-render the scene. When renderMode
|
||||
* is RENDERMODE_WHEN_DIRTY, the renderer only rendered when the surface
|
||||
* is created, or when {@link #requestRender} is called. Defaults to RENDERMODE_CONTINUOUSLY.
|
||||
* <p>
|
||||
* Using RENDERMODE_WHEN_DIRTY can improve battery life and overall system performance
|
||||
* by allowing the GPU and CPU to idle when the view does not need to be updated.
|
||||
* <p>
|
||||
* This method can only be called after {@link #setRenderer(SurfaceViewMapRenderer)}
|
||||
*
|
||||
* @param renderMode one of the RENDERMODE_X constants
|
||||
*/
|
||||
public void setRenderMode(int renderMode) {
|
||||
renderThread.setRenderMode(renderMode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current rendering mode. May be called
|
||||
* from any thread. Must not be called before a renderer has been set.
|
||||
*
|
||||
* @return the current rendering mode.
|
||||
*/
|
||||
public int getRenderMode() {
|
||||
return renderThread.getRenderMode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Request that the renderer render a frame.
|
||||
* This method is typically used when the render mode has been set to
|
||||
* RENDERMODE_WHEN_DIRTY, so that frames are only rendered on demand.
|
||||
* May be called
|
||||
* from any thread. Must not be called before a renderer has been set.
|
||||
*/
|
||||
public void requestRender() {
|
||||
renderThread.requestRender();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is part of the SurfaceHolder.Callback interface, and is
|
||||
* not normally called or subclassed by clients of MapLibreSurfaceView.
|
||||
*/
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
renderThread.surfaceCreated();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is part of the SurfaceHolder.Callback interface, and is
|
||||
* not normally called or subclassed by clients of MapLibreSurfaceView.
|
||||
*/
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
// Surface will be destroyed when we return
|
||||
renderThread.surfaceDestroyed();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is part of the SurfaceHolder.Callback interface, and is
|
||||
* not normally called or subclassed by clients of MapLibreSurfaceView.
|
||||
*/
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
|
||||
renderThread.onWindowResize(w, h);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is part of the SurfaceHolder.Callback2 interface, and is
|
||||
* not normally called or subclassed by clients of MapLibreSurfaceView.
|
||||
*/
|
||||
@Override
|
||||
public void surfaceRedrawNeededAsync(SurfaceHolder holder, Runnable finishDrawing) {
|
||||
if (renderThread != null) {
|
||||
renderThread.requestRenderAndNotify(finishDrawing);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is part of the SurfaceHolder.Callback2 interface, and is
|
||||
* not normally called or subclassed by clients of MapLibreSurfaceView.
|
||||
*/
|
||||
@Deprecated
|
||||
@Override
|
||||
public void surfaceRedrawNeeded(SurfaceHolder holder) {
|
||||
// Since we are part of the framework we know only surfaceRedrawNeededAsync
|
||||
// will be called.
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause the rendering thread.
|
||||
* <p>
|
||||
* Must not be called before a renderer has been set.
|
||||
*/
|
||||
public void onPause() {
|
||||
renderThread.onPause();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resumes the rendering thread. It
|
||||
* is the counterpart to {@link #onPause()}.
|
||||
* <p>
|
||||
* Must not be called before a renderer has been set.
|
||||
*/
|
||||
public void onResume() {
|
||||
renderThread.onResume();
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue a runnable to be run on the rendering thread. This can be used
|
||||
* to communicate with the Renderer on the rendering thread.
|
||||
* Must not be called before a renderer has been set.
|
||||
*
|
||||
* @param r the runnable to be run on the rendering thread.
|
||||
*/
|
||||
public void queueEvent(Runnable r) {
|
||||
renderThread.queueEvent(r);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for the queue to become empty
|
||||
*/
|
||||
public void waitForEmpty() {
|
||||
renderThread.waitForEmpty();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method is used as part of the View class and is not normally
|
||||
* called or subclassed by clients of MapLibreSurfaceView.
|
||||
*/
|
||||
@Override
|
||||
protected void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
if (detached && (renderer != null)) {
|
||||
int renderMode = RENDERMODE_CONTINUOUSLY;
|
||||
if (renderThread != null) {
|
||||
renderMode = renderThread.getRenderMode();
|
||||
}
|
||||
createRenderThread();
|
||||
if (renderMode != RENDERMODE_CONTINUOUSLY) {
|
||||
renderThread.setRenderMode(renderMode);
|
||||
}
|
||||
renderThread.start();
|
||||
}
|
||||
detached = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDetachedFromWindow() {
|
||||
if (detachedListener != null) {
|
||||
detachedListener.onSurfaceViewDetached();
|
||||
}
|
||||
if (renderThread != null) {
|
||||
renderThread.requestExitAndWait();
|
||||
}
|
||||
detached = true;
|
||||
super.onDetachedFromWindow();
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* A generic render Thread. Delegates
|
||||
* to a Renderer instance to do the actual drawing. Can be configured to
|
||||
* render continuously or on request.
|
||||
* <p>
|
||||
* All potentially blocking synchronization is done through the
|
||||
* sRenderThreadManager object. This avoids multiple-lock ordering issues.
|
||||
*/
|
||||
abstract static class RenderThread extends Thread {
|
||||
RenderThread() {
|
||||
super();
|
||||
width = 0;
|
||||
height = 0;
|
||||
requestRender = true;
|
||||
renderMode = RENDERMODE_CONTINUOUSLY;
|
||||
wantRenderNotification = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
setName("RenderThread " + getId());
|
||||
|
||||
try {
|
||||
guardedRun();
|
||||
} catch (InterruptedException exception) {
|
||||
// fall thru and exit normally
|
||||
} finally {
|
||||
renderThreadManager.threadExiting(this);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void guardedRun() throws InterruptedException;
|
||||
|
||||
protected boolean readyToDraw() {
|
||||
return (!paused) && hasSurface
|
||||
&& (width > 0) && (height > 0)
|
||||
&& (requestRender || (renderMode == RENDERMODE_CONTINUOUSLY));
|
||||
}
|
||||
|
||||
public boolean ableToDraw() {
|
||||
return readyToDraw();
|
||||
}
|
||||
|
||||
public void setRenderMode(int renderMode) {
|
||||
if ( !((RENDERMODE_WHEN_DIRTY <= renderMode) && (renderMode <= RENDERMODE_CONTINUOUSLY)) ) {
|
||||
throw new IllegalArgumentException("renderMode");
|
||||
}
|
||||
synchronized (renderThreadManager) {
|
||||
this.renderMode = renderMode;
|
||||
renderThreadManager.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public int getRenderMode() {
|
||||
synchronized (renderThreadManager) {
|
||||
return renderMode;
|
||||
}
|
||||
}
|
||||
|
||||
public void requestRender() {
|
||||
synchronized (renderThreadManager) {
|
||||
requestRender = true;
|
||||
renderThreadManager.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public void requestRenderAndNotify(Runnable finishDrawing) {
|
||||
synchronized (renderThreadManager) {
|
||||
// If we are already on the render thread, this means a client callback
|
||||
// has caused reentrancy, for example via updating the SurfaceView parameters.
|
||||
// We will return to the client rendering code, so here we don't need to
|
||||
// do anything.
|
||||
if (Thread.currentThread() == this) {
|
||||
return;
|
||||
}
|
||||
|
||||
wantRenderNotification = true;
|
||||
requestRender = true;
|
||||
renderComplete = false;
|
||||
finishDrawingRunnable = finishDrawing;
|
||||
|
||||
renderThreadManager.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public void surfaceCreated() {
|
||||
synchronized (renderThreadManager) {
|
||||
hasSurface = true;
|
||||
renderThreadManager.notifyAll();
|
||||
while (!exited && waitingForSurface) {
|
||||
try {
|
||||
renderThreadManager.wait();
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void surfaceDestroyed() {
|
||||
synchronized (renderThreadManager) {
|
||||
hasSurface = false;
|
||||
renderThreadManager.notifyAll();
|
||||
while (!exited && !waitingForSurface) {
|
||||
try {
|
||||
renderThreadManager.wait();
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onPause() {
|
||||
synchronized (renderThreadManager) {
|
||||
requestPaused = true;
|
||||
renderThreadManager.notifyAll();
|
||||
while ((!exited) && (!paused)) {
|
||||
try {
|
||||
renderThreadManager.wait();
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onResume() {
|
||||
synchronized (renderThreadManager) {
|
||||
requestPaused = false;
|
||||
requestRender = true;
|
||||
renderComplete = false;
|
||||
renderThreadManager.notifyAll();
|
||||
while ((!exited) && paused && (!renderComplete)) {
|
||||
try {
|
||||
renderThreadManager.wait();
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onWindowResize(int w, int h) {
|
||||
synchronized (renderThreadManager) {
|
||||
width = w;
|
||||
height = h;
|
||||
sizeChanged = true;
|
||||
requestRender = true;
|
||||
renderComplete = false;
|
||||
|
||||
// If we are already on the render thread, this means a client callback
|
||||
// has caused reentrancy, for example via updating the SurfaceView parameters.
|
||||
// We need to process the size change eventually though and update our surface.
|
||||
// So we set the parameters and return so they can be processed on our
|
||||
// next iteration.
|
||||
if (Thread.currentThread() == this) {
|
||||
return;
|
||||
}
|
||||
|
||||
renderThreadManager.notifyAll();
|
||||
|
||||
// Wait for thread to react to resize and render a frame
|
||||
while (!exited && !paused && !renderComplete
|
||||
&& ableToDraw()) {
|
||||
try {
|
||||
renderThreadManager.wait();
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void requestExitAndWait() {
|
||||
// don't call this from renderThread thread or it is a guaranteed
|
||||
// deadlock!
|
||||
synchronized (renderThreadManager) {
|
||||
shouldExit = true;
|
||||
renderThreadManager.notifyAll();
|
||||
while (!exited) {
|
||||
try {
|
||||
renderThreadManager.wait();
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue an "event" to be run on the rendering thread.
|
||||
*
|
||||
* @param r the runnable to be run on the rendering thread.
|
||||
*/
|
||||
public void queueEvent(@NonNull Runnable r) {
|
||||
synchronized (renderThreadManager) {
|
||||
eventQueue.add(r);
|
||||
renderThreadManager.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for the queue to become empty
|
||||
*/
|
||||
public void waitForEmpty() {
|
||||
synchronized (renderThreadManager) {
|
||||
// Wait for the queue to be empty
|
||||
while (!this.eventQueue.isEmpty()) {
|
||||
try {
|
||||
renderThreadManager.wait();
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Once the thread is started, all accesses to the following member
|
||||
// variables are protected by the sRenderThreadManager monitor
|
||||
protected boolean shouldExit;
|
||||
protected boolean exited;
|
||||
protected boolean requestPaused;
|
||||
protected boolean paused;
|
||||
protected boolean hasSurface;
|
||||
protected boolean waitingForSurface;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected int renderMode;
|
||||
protected boolean requestRender;
|
||||
protected boolean wantRenderNotification;
|
||||
protected boolean renderComplete;
|
||||
protected ArrayList<Runnable> eventQueue = new ArrayList<>();
|
||||
protected boolean sizeChanged = true;
|
||||
protected Runnable finishDrawingRunnable = null;
|
||||
// End of member variables protected by the sRenderThreadManager monitor.
|
||||
|
||||
}
|
||||
|
||||
protected abstract void createRenderThread();
|
||||
|
||||
protected void checkRenderThreadState() {
|
||||
if (renderThread != null) {
|
||||
throw new IllegalStateException("setRenderer has already been called for this instance.");
|
||||
}
|
||||
}
|
||||
|
||||
protected static class RenderThreadManager {
|
||||
|
||||
synchronized void threadExiting(RenderThread thread) {
|
||||
thread.exited = true;
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listener interface that notifies when a {@link MapLibreSurfaceView} is detached from window.
|
||||
*/
|
||||
public interface OnSurfaceViewDetachedListener {
|
||||
|
||||
/**
|
||||
* Called when a {@link MapLibreSurfaceView} is detached from window.
|
||||
*/
|
||||
void onSurfaceViewDetached();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package org.maplibre.android.maps.renderer.surfaceview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.Surface;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.maplibre.android.maps.renderer.MapRenderer;
|
||||
|
||||
/**
|
||||
* The {@link SurfaceViewMapRenderer} encapsulates the render thread and
|
||||
* {@link MapLibreSurfaceView} specifics to render the map.
|
||||
*
|
||||
* @see MapRenderer
|
||||
*/
|
||||
public class SurfaceViewMapRenderer extends MapRenderer {
|
||||
|
||||
@NonNull
|
||||
protected final MapLibreSurfaceView surfaceView;
|
||||
|
||||
public SurfaceViewMapRenderer(Context context,
|
||||
MapLibreSurfaceView surfaceView,
|
||||
String localIdeographFontFamily) {
|
||||
super(context, localIdeographFontFamily);
|
||||
this.surfaceView = surfaceView;
|
||||
|
||||
surfaceView.setDetachedListener(new MapLibreSurfaceView.OnSurfaceViewDetachedListener() {
|
||||
@Override
|
||||
public void onSurfaceViewDetached() {
|
||||
// because the GL thread is destroyed when the view is detached from window,
|
||||
// we need to ensure releasing the native renderer as well.
|
||||
// This avoids releasing it only when the view is being recreated, which is already on a new GL thread,
|
||||
// and leads to JNI crashes like https://github.com/mapbox/mapbox-gl-native/issues/14618
|
||||
nativeReset();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView() {
|
||||
return this.surfaceView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
surfaceView.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
surfaceView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
public void onSurfaceCreated(Surface surface) {
|
||||
super.onSurfaceCreated(surface);
|
||||
}
|
||||
|
||||
public void onSurfaceDestroyed() {
|
||||
super.onSurfaceDestroyed();
|
||||
}
|
||||
|
||||
public void onSurfaceChanged(int width, int height) {
|
||||
super.onSurfaceChanged(width, height);
|
||||
}
|
||||
|
||||
public void onDrawFrame() {
|
||||
super.onDrawFrame();
|
||||
}
|
||||
|
||||
/**
|
||||
* May be called from any thread.
|
||||
* <p>
|
||||
* Called from the renderer frontend to schedule a render.
|
||||
*/
|
||||
@Override
|
||||
public void requestRender() {
|
||||
surfaceView.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) {
|
||||
surfaceView.queueEvent(runnable);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void waitForEmpty() {
|
||||
surfaceView.waitForEmpty();
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
package org.maplibre.android.maps.renderer.textureview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.Surface;
|
||||
import android.view.TextureView;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.maplibre.android.maps.renderer.MapRenderer;
|
||||
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
|
||||
/**
|
||||
* The {@link TextureViewMapRenderer} encapsulates the GL thread and
|
||||
* {@link TextureView} specifics to render the map.
|
||||
@@ -19,6 +18,7 @@ import javax.microedition.khronos.opengles.GL10;
|
||||
public class TextureViewMapRenderer extends MapRenderer {
|
||||
private TextureViewRenderThread renderThread;
|
||||
private boolean translucentSurface;
|
||||
private TextureView textureView;
|
||||
|
||||
/**
|
||||
* Create a {@link MapRenderer} for the given {@link TextureView}
|
||||
@@ -33,26 +33,36 @@ public class TextureViewMapRenderer extends MapRenderer {
|
||||
String localIdeographFontFamily,
|
||||
boolean translucentSurface) {
|
||||
super(context, localIdeographFontFamily);
|
||||
this.textureView = textureView;
|
||||
this.translucentSurface = translucentSurface;
|
||||
renderThread = new TextureViewRenderThread(textureView, this);
|
||||
|
||||
}
|
||||
|
||||
public void setRenderThread(TextureViewRenderThread thread) {
|
||||
renderThread = thread;
|
||||
renderThread.setName("TextureViewRenderer");
|
||||
renderThread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden to provide package access
|
||||
*/
|
||||
@Override
|
||||
protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
super.onSurfaceCreated(gl, config);
|
||||
public View getView() {
|
||||
return this.textureView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden to provide package access
|
||||
*/
|
||||
@Override
|
||||
protected void onSurfaceChanged(GL10 gl, int width, int height) {
|
||||
super.onSurfaceChanged(gl, width, height);
|
||||
protected void onSurfaceCreated(Surface surface) {
|
||||
super.onSurfaceCreated(surface);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden to provide package access
|
||||
*/
|
||||
@Override
|
||||
protected void onSurfaceChanged(int width, int height) {
|
||||
super.onSurfaceChanged(width, height);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,8 +77,8 @@ public class TextureViewMapRenderer extends MapRenderer {
|
||||
* Overridden to provide package access
|
||||
*/
|
||||
@Override
|
||||
protected void onDrawFrame(GL10 gl) {
|
||||
super.onDrawFrame(gl);
|
||||
protected void onDrawFrame() {
|
||||
super.onDrawFrame();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,50 +7,35 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.UiThread;
|
||||
|
||||
import org.maplibre.android.log.Logger;
|
||||
import org.maplibre.android.maps.renderer.egl.EGLConfigChooser;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.microedition.khronos.egl.EGL10;
|
||||
import javax.microedition.khronos.egl.EGL11;
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.egl.EGLContext;
|
||||
import javax.microedition.khronos.egl.EGLDisplay;
|
||||
import javax.microedition.khronos.egl.EGLSurface;
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
|
||||
/**
|
||||
* The render thread is responsible for managing the communication between the
|
||||
* ui thread and the render thread it creates. Also, the EGL and GL contexts
|
||||
* are managed from here.
|
||||
* ui thread and the render thread it creates
|
||||
*/
|
||||
class TextureViewRenderThread extends Thread implements TextureView.SurfaceTextureListener {
|
||||
abstract class TextureViewRenderThread extends Thread implements TextureView.SurfaceTextureListener {
|
||||
|
||||
private static final String TAG = "Mbgl-TextureViewRenderThread";
|
||||
protected static final String TAG = "Mbgl-TextureViewRenderThread";
|
||||
|
||||
@NonNull
|
||||
private final TextureViewMapRenderer mapRenderer;
|
||||
@NonNull
|
||||
private final EGLHolder eglHolder;
|
||||
protected final TextureViewMapRenderer mapRenderer;
|
||||
|
||||
// Lock used for synchronization
|
||||
private final Object lock = new Object();
|
||||
protected final Object lock = new Object();
|
||||
|
||||
// Guarded by lock
|
||||
private final ArrayList<Runnable> eventQueue = new ArrayList<>();
|
||||
protected final ArrayList<Runnable> eventQueue = new ArrayList<>();
|
||||
@Nullable
|
||||
private SurfaceTexture surface;
|
||||
private int width;
|
||||
private int height;
|
||||
private boolean requestRender;
|
||||
private boolean sizeChanged;
|
||||
private boolean paused;
|
||||
private boolean destroyContext;
|
||||
private boolean destroySurface;
|
||||
private boolean shouldExit;
|
||||
private boolean exited;
|
||||
protected SurfaceTexture surfaceTexture;
|
||||
protected boolean hasNativeSurface;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected boolean requestRender;
|
||||
protected boolean sizeChanged;
|
||||
protected boolean paused;
|
||||
protected boolean destroySurface;
|
||||
protected boolean shouldExit;
|
||||
protected boolean exited;
|
||||
|
||||
/**
|
||||
* Create a render thread for the given TextureView / Maprenderer combination.
|
||||
@@ -63,16 +48,15 @@ class TextureViewRenderThread extends Thread implements TextureView.SurfaceTextu
|
||||
textureView.setOpaque(!mapRenderer.isTranslucentSurface());
|
||||
textureView.setSurfaceTextureListener(this);
|
||||
this.mapRenderer = mapRenderer;
|
||||
this.eglHolder = new EGLHolder(new WeakReference<>(textureView), mapRenderer.isTranslucentSurface());
|
||||
}
|
||||
|
||||
// SurfaceTextureListener methods
|
||||
|
||||
@UiThread
|
||||
@Override
|
||||
public void onSurfaceTextureAvailable(final SurfaceTexture surface, final int width, final int height) {
|
||||
public void onSurfaceTextureAvailable(final SurfaceTexture surfaceTexture, final int width, final int height) {
|
||||
synchronized (lock) {
|
||||
this.surface = surface;
|
||||
this.surfaceTexture = surfaceTexture;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.requestRender = true;
|
||||
@@ -82,7 +66,7 @@ class TextureViewRenderThread extends Thread implements TextureView.SurfaceTextu
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, final int width, final int height) {
|
||||
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, final int width, final int height) {
|
||||
synchronized (lock) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
@@ -94,9 +78,9 @@ class TextureViewRenderThread extends Thread implements TextureView.SurfaceTextu
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
|
||||
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
|
||||
synchronized (lock) {
|
||||
this.surface = null;
|
||||
this.surfaceTexture = null;
|
||||
this.destroySurface = true;
|
||||
this.requestRender = false;
|
||||
lock.notifyAll();
|
||||
@@ -106,7 +90,7 @@ class TextureViewRenderThread extends Thread implements TextureView.SurfaceTextu
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
|
||||
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
|
||||
// Ignored
|
||||
}
|
||||
|
||||
@@ -137,7 +121,6 @@ class TextureViewRenderThread extends Thread implements TextureView.SurfaceTextu
|
||||
|
||||
/**
|
||||
* Wait for the queue to be empty.
|
||||
* @param timeoutMillis Maximum time to wait, in milliseconds
|
||||
* @return The number of items remaining in the queue
|
||||
*/
|
||||
@UiThread
|
||||
@@ -170,7 +153,6 @@ class TextureViewRenderThread extends Thread implements TextureView.SurfaceTextu
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@UiThread
|
||||
void onDestroy() {
|
||||
synchronized (lock) {
|
||||
@@ -187,303 +169,4 @@ class TextureViewRenderThread extends Thread implements TextureView.SurfaceTextu
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Thread implementation
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
|
||||
while (true) {
|
||||
Runnable event = null;
|
||||
boolean initializeEGL = false;
|
||||
boolean recreateSurface = false;
|
||||
int w = -1;
|
||||
int h = -1;
|
||||
|
||||
// Guarded block
|
||||
synchronized (lock) {
|
||||
while (true) {
|
||||
|
||||
if (shouldExit) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If any events are scheduled, pop one for processing
|
||||
if (!eventQueue.isEmpty()) {
|
||||
event = eventQueue.remove(0);
|
||||
break;
|
||||
}
|
||||
|
||||
if (destroySurface) {
|
||||
eglHolder.destroySurface();
|
||||
destroySurface = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (destroyContext) {
|
||||
eglHolder.destroyContext();
|
||||
destroyContext = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (surface != null && !paused && requestRender) {
|
||||
|
||||
w = width;
|
||||
h = height;
|
||||
|
||||
// Initialize EGL if needed
|
||||
if (eglHolder.eglContext == EGL10.EGL_NO_CONTEXT) {
|
||||
initializeEGL = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// (re-)Initialize EGL Surface if needed
|
||||
if (eglHolder.eglSurface == EGL10.EGL_NO_SURFACE) {
|
||||
recreateSurface = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// Reset the request render flag now, so we can catch new requests
|
||||
// while rendering
|
||||
requestRender = false;
|
||||
|
||||
// Break the guarded loop and continue to process
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// Wait until needed
|
||||
lock.wait();
|
||||
|
||||
} // end guarded while loop
|
||||
|
||||
} // end guarded block
|
||||
|
||||
// Run event, if any
|
||||
if (event != null) {
|
||||
event.run();
|
||||
continue;
|
||||
}
|
||||
|
||||
GL10 gl = eglHolder.createGL();
|
||||
|
||||
// Initialize EGL
|
||||
if (initializeEGL) {
|
||||
eglHolder.prepare();
|
||||
synchronized (lock) {
|
||||
if (!eglHolder.createSurface()) {
|
||||
// Cleanup the surface if one could not be created
|
||||
// and wait for another to be ready.
|
||||
destroySurface = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
mapRenderer.onSurfaceCreated(gl, eglHolder.eglConfig);
|
||||
mapRenderer.onSurfaceChanged(gl, w, h);
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the surface size has changed inform the map renderer.
|
||||
if (recreateSurface) {
|
||||
synchronized (lock) {
|
||||
eglHolder.createSurface();
|
||||
}
|
||||
mapRenderer.onSurfaceChanged(gl, w, h);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sizeChanged) {
|
||||
mapRenderer.onSurfaceChanged(gl, w, h);
|
||||
sizeChanged = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't continue without a surface
|
||||
if (eglHolder.eglSurface == EGL10.EGL_NO_SURFACE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Time to render a frame
|
||||
mapRenderer.onDrawFrame(gl);
|
||||
|
||||
// Swap and check the result
|
||||
int swapError = eglHolder.swap();
|
||||
switch (swapError) {
|
||||
case EGL10.EGL_SUCCESS:
|
||||
break;
|
||||
case EGL11.EGL_CONTEXT_LOST:
|
||||
Logger.w(TAG, "Context lost. Waiting for re-aquire");
|
||||
synchronized (lock) {
|
||||
surface = null;
|
||||
destroySurface = true;
|
||||
destroyContext = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Logger.w(TAG, String.format("eglSwapBuffer error: %s. Waiting or new surface", swapError));
|
||||
// Probably lost the surface. Clear the current one and
|
||||
// wait for a new one to be set
|
||||
synchronized (lock) {
|
||||
surface = null;
|
||||
destroySurface = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (InterruptedException err) {
|
||||
// To be expected
|
||||
} finally {
|
||||
// Cleanup
|
||||
eglHolder.cleanup();
|
||||
|
||||
// Signal we're done
|
||||
synchronized (lock) {
|
||||
this.exited = true;
|
||||
lock.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds the EGL state and offers methods to mutate it.
|
||||
*/
|
||||
private static class EGLHolder {
|
||||
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
|
||||
private final WeakReference<TextureView> textureViewWeakRef;
|
||||
private boolean translucentSurface;
|
||||
|
||||
private EGL10 egl;
|
||||
@Nullable
|
||||
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, boolean translucentSurface) {
|
||||
this.textureViewWeakRef = textureViewWeakRef;
|
||||
this.translucentSurface = translucentSurface;
|
||||
}
|
||||
|
||||
void prepare() {
|
||||
this.egl = (EGL10) EGLContext.getEGL();
|
||||
|
||||
// Only re-initialize display when needed
|
||||
if (eglDisplay == EGL10.EGL_NO_DISPLAY) {
|
||||
this.eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
|
||||
|
||||
if (eglDisplay == EGL10.EGL_NO_DISPLAY) {
|
||||
throw new RuntimeException("eglGetDisplay failed");
|
||||
}
|
||||
|
||||
int[] version = new int[2];
|
||||
if (!egl.eglInitialize(eglDisplay, version)) {
|
||||
throw new RuntimeException("eglInitialize failed");
|
||||
}
|
||||
}
|
||||
|
||||
if (textureViewWeakRef == null) {
|
||||
// No texture view present
|
||||
eglConfig = null;
|
||||
eglContext = EGL10.EGL_NO_CONTEXT;
|
||||
} else if (eglContext == EGL10.EGL_NO_CONTEXT) {
|
||||
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);
|
||||
}
|
||||
|
||||
if (eglContext == EGL10.EGL_NO_CONTEXT) {
|
||||
throw new RuntimeException("createContext");
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
GL10 createGL() {
|
||||
return (GL10) eglContext.getGL();
|
||||
}
|
||||
|
||||
boolean createSurface() {
|
||||
// The window size has changed, so we need to create a new surface.
|
||||
destroySurface();
|
||||
|
||||
// Create an EGL surface we can render into.
|
||||
TextureView view = textureViewWeakRef.get();
|
||||
if (view != null && view.getSurfaceTexture() != null) {
|
||||
int[] surfaceAttribs = {EGL10.EGL_NONE};
|
||||
eglSurface = egl.eglCreateWindowSurface(eglDisplay, eglConfig, view.getSurfaceTexture(), surfaceAttribs);
|
||||
} else {
|
||||
eglSurface = EGL10.EGL_NO_SURFACE;
|
||||
}
|
||||
|
||||
if (eglSurface == null || eglSurface == EGL10.EGL_NO_SURFACE) {
|
||||
int error = egl.eglGetError();
|
||||
if (error == EGL10.EGL_BAD_NATIVE_WINDOW) {
|
||||
Logger.e(TAG, "createWindowSurface returned EGL_BAD_NATIVE_WINDOW.");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return makeCurrent();
|
||||
}
|
||||
|
||||
boolean makeCurrent() {
|
||||
if (!egl.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) {
|
||||
// Could not make the context current, probably because the underlying
|
||||
// SurfaceView surface has been destroyed.
|
||||
Logger.w(TAG, String.format("eglMakeCurrent: %s", egl.eglGetError()));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int swap() {
|
||||
if (!egl.eglSwapBuffers(eglDisplay, eglSurface)) {
|
||||
return egl.eglGetError();
|
||||
}
|
||||
return EGL10.EGL_SUCCESS;
|
||||
}
|
||||
|
||||
private void destroySurface() {
|
||||
if (eglSurface == EGL10.EGL_NO_SURFACE) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!egl.eglDestroySurface(eglDisplay, eglSurface)) {
|
||||
Logger.w(TAG, String.format("Could not destroy egl surface. Display %s, Surface %s", eglDisplay, eglSurface));
|
||||
}
|
||||
|
||||
eglSurface = EGL10.EGL_NO_SURFACE;
|
||||
}
|
||||
|
||||
private void destroyContext() {
|
||||
if (eglContext == EGL10.EGL_NO_CONTEXT) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!egl.eglDestroyContext(eglDisplay, eglContext)) {
|
||||
Logger.w(TAG, String.format("Could not destroy egl context. Display %s, Context %s", eglDisplay, eglContext));
|
||||
}
|
||||
|
||||
eglContext = EGL10.EGL_NO_CONTEXT;
|
||||
}
|
||||
|
||||
private void terminate() {
|
||||
if (eglDisplay == EGL10.EGL_NO_DISPLAY) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!egl.eglTerminate(eglDisplay)) {
|
||||
Logger.w(TAG, String.format("Could not terminate egl. Display %s", eglDisplay));
|
||||
}
|
||||
eglDisplay = EGL10.EGL_NO_DISPLAY;
|
||||
}
|
||||
|
||||
void cleanup() {
|
||||
destroySurface();
|
||||
destroyContext();
|
||||
terminate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user