Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ internal class MapboxRenderThread : Choreographer.FrameCallback {
handlerThread.post { prepareRenderFrame() }
}

private fun checkSurfaceReady(): Boolean {
private fun checkSurfaceReady(creatingSurface: Boolean): Boolean {
lock.withLock {
try {
surface?.let {
if (eglSurface == null || eglSurface == EGL10.EGL_NO_SURFACE) {
return prepareEglSurface(it)
return prepareEglSurface(it, creatingSurface)
}
} ?: return false
return true
Expand All @@ -102,7 +102,7 @@ internal class MapboxRenderThread : Choreographer.FrameCallback {
}
}

private fun prepareEglSurface(surface: Surface): Boolean {
private fun prepareEglSurface(surface: Surface, creatingSurface: Boolean): Boolean {
if (!eglPrepared) {
eglCore.prepareEgl()
if (eglCore.eglStatusSuccess) {
Expand All @@ -118,6 +118,11 @@ internal class MapboxRenderThread : Choreographer.FrameCallback {
postPrepareRenderFrame()
return false
}
// on Android SDK <= 23 at least on x86 emulators we need to force set EGL10.EGL_NO_CONTEXT
// when resuming activity
if (creatingSurface) {
eglCore.makeNothingCurrent()
}
eglSurface = eglCore.createWindowSurface(surface)
if (!eglCore.eglStatusSuccess) {
// Set EGL Surface as EGL_NO_SURFACE and try recreate it in next iteration.
Expand Down Expand Up @@ -234,7 +239,7 @@ internal class MapboxRenderThread : Choreographer.FrameCallback {
return
}
}
if (!checkSurfaceReady()) {
if (!checkSurfaceReady(creatingSurface)) {
return
}
checkSurfaceSizeChanged()
Expand Down Expand Up @@ -377,16 +382,14 @@ internal class MapboxRenderThread : Choreographer.FrameCallback {
if (nativeRenderCreated) {
releaseAll()
}
handlerThread.clearMessageQueue()
destroyCondition.signal()
}
}
destroyCondition.await()
}
}
handlerThread.apply {
clearMessageQueue()
stop()
}
handlerThread.stop()
}

companion object {
Expand Down
31 changes: 5 additions & 26 deletions sdk/src/main/java/com/mapbox/maps/renderer/egl/EGLCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ internal class EGLCore(
if (eglDisplay != EGL10.EGL_NO_DISPLAY) {
// Android is unusual in that it uses a reference-counted EGLDisplay. So for
// every eglInitialize() we need an eglTerminate().
egl.eglMakeCurrent(
eglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_CONTEXT
)
makeNothingCurrent()
egl.eglDestroyContext(eglDisplay, eglContext)
egl.eglTerminate(eglDisplay)
}
Expand All @@ -91,14 +88,6 @@ internal class EGLCore(
*/
fun releaseSurface(eglSurface: EGLSurface) {
if (eglSurface != EGL10.EGL_NO_SURFACE && eglDisplay != EGL10.EGL_NO_DISPLAY) {
if (egl.eglGetCurrentSurface(EGL10.EGL_DRAW) == eglSurface) {
egl.eglMakeCurrent(
eglDisplay,
EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_CONTEXT
)
}
egl.eglDestroySurface(eglDisplay, eglSurface)
}
}
Expand All @@ -124,22 +113,12 @@ internal class EGLCore(
}

/**
* Creates an EGL surface associated with an offscreen buffer.
* Makes no context current.
*/
fun createOffscreenSurface(width: Int, height: Int): EGLSurface? {
val surfaceAttribs =
intArrayOf(EGL10.EGL_WIDTH, width, EGL10.EGL_HEIGHT, height, EGL10.EGL_NONE)
val eglSurface = egl.eglCreatePbufferSurface(
eglDisplay,
eglConfig,
surfaceAttribs
)
checkEglErrorNoException("eglCreatePbufferSurface")
if (eglSurface == null) {
Logger.e(TAG, "surface was null")
eglStatusSuccess = false
fun makeNothingCurrent() {
if (!egl.eglMakeCurrent(eglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT)) {
checkEglErrorNoException("eglMakeNothingCurrent")
}
return eglSurface
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class MapboxRenderThreadTest {
}
verify {
mapboxRenderer.onSurfaceCreated()
eglCore.makeNothingCurrent()
mapboxRenderer.onSurfaceChanged(1, 1)
}
}
Expand Down Expand Up @@ -243,6 +244,10 @@ class MapboxRenderThreadTest {
verify(exactly = 3) {
eglCore.swapBuffers(any())
}
// make EGL context current only once when creating surface
verify(exactly = 1) {
eglCore.makeNothingCurrent()
}
}

@Test
Expand Down