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

Commit d1d0c20

Browse files
ivovandongentobrun
authored andcommitted
[android] TextureView - cleanup destruction code
1 parent 8753edf commit d1d0c20

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/textureview/TextureViewRenderThread.java

+29-12
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ public void run() {
213213
break;
214214
}
215215

216+
// (re-)Initialize EGL Surface if needed
217+
if (eglHolder.eglSurface == EGL10.EGL_NO_SURFACE) {
218+
recreateSurface = true;
219+
break;
220+
}
221+
216222
// Check if the size has changed
217223
if (sizeChanged) {
218224
recreateSurface = true;
@@ -247,7 +253,14 @@ public void run() {
247253
// Initialize EGL
248254
if (initializeEGL) {
249255
eglHolder.prepare();
250-
eglHolder.createSurface();
256+
if (!eglHolder.createSurface()) {
257+
synchronized (lock) {
258+
// Cleanup the surface if one could not be created
259+
// and wait for another to be ready.
260+
destroySurface = true;
261+
}
262+
continue;
263+
}
251264
mapRenderer.onSurfaceCreated(gl, eglHolder.eglConfig);
252265
mapRenderer.onSurfaceChanged(gl, w, h);
253266
continue;
@@ -345,7 +358,7 @@ void prepare() {
345358
// No texture view present
346359
eglConfig = null;
347360
eglContext = EGL10.EGL_NO_CONTEXT;
348-
} else /*if (eglContext == EGL10.EGL_NO_CONTEXT)*/ {
361+
} else if (eglContext == EGL10.EGL_NO_CONTEXT) {
349362
eglConfig = new EGLConfigChooser().chooseConfig(egl, eglDisplay);
350363
int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE};
351364
eglContext = egl.eglCreateContext(eglDisplay, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
@@ -408,29 +421,33 @@ private void destroySurface() {
408421
}
409422

410423
if (!egl.eglDestroySurface(eglDisplay, eglSurface)) {
411-
Timber.e("Could not destroy egl surface. Display %s, Surface %s", eglDisplay, eglSurface);
412-
throw new RuntimeException("eglDestroyContext: " + egl.eglGetError());
424+
Timber.w("Could not destroy egl surface. Display %s, Surface %s", eglDisplay, eglSurface);
413425
}
426+
427+
eglSurface = EGL10.EGL_NO_SURFACE;
414428
}
415429

416430
private void destroyContext() {
417-
if (eglSurface == null || eglSurface == EGL10.EGL_NO_SURFACE) {
431+
if (eglContext == EGL10.EGL_NO_CONTEXT) {
418432
return;
419433
}
420434

421435
if (!egl.eglDestroyContext(eglDisplay, eglContext)) {
422-
Timber.e("Could not destroy egl context. Display %s, Context %s", eglDisplay, eglContext);
423-
throw new RuntimeException("eglDestroyContext: " + egl.eglGetError());
436+
Timber.w("Could not destroy egl context. Display %s, Context %s", eglDisplay, eglContext);
424437
}
438+
439+
eglContext = EGL10.EGL_NO_CONTEXT;
425440
}
426441

427442
private void terminate() {
428-
if (eglDisplay != EGL10.EGL_NO_DISPLAY) {
429-
if (!egl.eglTerminate(eglDisplay)) {
430-
Timber.w("Could not terminate egl. Display %s", eglDisplay);
431-
}
432-
eglDisplay = EGL10.EGL_NO_DISPLAY;
443+
if (eglDisplay == EGL10.EGL_NO_DISPLAY) {
444+
return;
445+
}
446+
447+
if (!egl.eglTerminate(eglDisplay)) {
448+
Timber.w("Could not terminate egl. Display %s", eglDisplay);
433449
}
450+
eglDisplay = EGL10.EGL_NO_DISPLAY;
434451
}
435452

436453
void cleanup() {

0 commit comments

Comments
 (0)