[surface] exception free GLSurfaceView implementation (#402)

This commit is contained in:
Tobrun
2020-05-29 09:31:58 +02:00
committed by GitHub
parent 5f7c224e76
commit 60e5d8c667
8 changed files with 1701 additions and 36 deletions

View File

@@ -2,13 +2,16 @@ package com.mapbox.mapboxsdk.maps.renderer.egl;
import android.opengl.GLSurfaceView;
import android.os.Build;
import androidx.annotation.NonNull;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.log.Logger;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -74,7 +77,6 @@ public class EGLConfigChooser implements GLSurfaceView.EGLConfigChooser {
int[] numConfigs = getNumberOfConfigurations(egl, display, configAttribs);
if (numConfigs[0] < 1) {
Logger.e(TAG, "eglChooseConfig() returned no configs.");
throw new EGLConfigException("eglChooseConfig() failed");
}
// Get all possible configurations
@@ -84,7 +86,6 @@ public class EGLConfigChooser implements GLSurfaceView.EGLConfigChooser {
EGLConfig config = chooseBestMatchConfig(egl, display, possibleConfigurations);
if (config == null) {
Logger.e(TAG, "No config chosen");
throw new EGLConfigException("No config chosen");
}
return config;
@@ -97,7 +98,6 @@ public class EGLConfigChooser implements GLSurfaceView.EGLConfigChooser {
Logger.e(TAG, String.format(
MapboxConstants.MAPBOX_LOCALE, "eglChooseConfig(NULL) returned error %d", egl.eglGetError())
);
throw new EGLConfigException("eglChooseConfig() failed");
}
return numConfigs;
}
@@ -110,7 +110,6 @@ public class EGLConfigChooser implements GLSurfaceView.EGLConfigChooser {
Logger.e(TAG, String.format(
MapboxConstants.MAPBOX_LOCALE, "eglChooseConfig() returned error %d", egl.eglGetError())
);
throw new EGLConfigException("eglChooseConfig() failed");
}
return configs;
}
@@ -259,7 +258,8 @@ public class EGLConfigChooser implements GLSurfaceView.EGLConfigChooser {
Collections.sort(matches);
if (matches.size() == 0) {
throw new EGLConfigException("No matching configurations after filtering");
Logger.e(TAG, "No matching configurations after filtering");
return null;
}
Config bestMatch = matches.get(0);
@@ -281,7 +281,6 @@ public class EGLConfigChooser implements GLSurfaceView.EGLConfigChooser {
Logger.e(TAG, String.format(
MapboxConstants.MAPBOX_LOCALE, "eglGetConfigAttrib(%d) returned error %d", attributeName, egl.eglGetError())
);
throw new EGLConfigException("eglGetConfigAttrib() failed");
}
return attributevalue[0];
}

View File

@@ -1,21 +0,0 @@
package com.mapbox.mapboxsdk.maps.renderer.egl;
/**
* Used for EGL configuration exceptions
*/
public class EGLConfigException extends RuntimeException {
public EGLConfigException() {
}
public EGLConfigException(String message) {
super(message);
}
public EGLConfigException(String message, Throwable cause) {
super(message, cause);
}
public EGLConfigException(Throwable cause) {
super(cause);
}
}

View File

@@ -0,0 +1,30 @@
package com.mapbox.mapboxsdk.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());
}
}
}

View File

@@ -0,0 +1,546 @@
package com.mapbox.mapboxsdk.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);
}
}
}

View File

@@ -0,0 +1,38 @@
package com.mapbox.mapboxsdk.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);
}
}

View File

@@ -2,10 +2,13 @@ package com.mapbox.mapboxsdk.maps.renderer.glsurfaceview;
import android.content.Context;
import android.opengl.GLSurfaceView;
import androidx.annotation.NonNull;
import com.mapbox.mapboxsdk.maps.renderer.MapRenderer;
import com.mapbox.mapboxsdk.maps.renderer.egl.EGLConfigChooser;
import com.mapbox.mapboxsdk.maps.renderer.egl.EGLContextFactory;
import com.mapbox.mapboxsdk.maps.renderer.egl.EGLWindowSurfaceFactory;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
@@ -28,7 +31,8 @@ public class GLSurfaceViewMapRenderer extends MapRenderer implements GLSurfaceVi
String localIdeographFontFamily) {
super(context, localIdeographFontFamily);
this.glSurfaceView = glSurfaceView;
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setEGLContextFactory(new EGLContextFactory());
glSurfaceView.setEGLWindowSurfaceFactory(new EGLWindowSurfaceFactory());
glSurfaceView.setEGLConfigChooser(new EGLConfigChooser());
glSurfaceView.setRenderer(this);
glSurfaceView.setRenderMode(RENDERMODE_WHEN_DIRTY);

View File

@@ -1,6 +1,5 @@
package com.mapbox.mapboxsdk.testapp.maps
import android.opengl.GLSurfaceView
import android.view.TextureView
import android.view.ViewGroup
import androidx.test.annotation.UiThreadTest
@@ -9,6 +8,7 @@ import androidx.test.rule.ActivityTestRule
import com.mapbox.mapboxsdk.AppCenter
import com.mapbox.mapboxsdk.maps.MapView
import com.mapbox.mapboxsdk.maps.MapboxMapOptions
import com.mapbox.mapboxsdk.maps.renderer.glsurfaceview.MapboxGLSurfaceView
import com.mapbox.mapboxsdk.testapp.activity.FeatureOverviewActivity
import java.util.concurrent.CountDownLatch
import junit.framework.Assert.assertNotNull
@@ -34,7 +34,7 @@ class RenderViewGetterTest : AppCenter() {
rootView = rule.activity.findViewById(android.R.id.content)
mapView = MapView(rule.activity)
assertNotNull(mapView.renderView)
assertTrue(mapView.renderView is GLSurfaceView)
assertTrue(mapView.renderView is MapboxGLSurfaceView)
}
@Test