[android] Restore prior GLContext if exists

Apparently some versions of Android, notable 4.4.4 running
on my Nexus 5, creates a GLContext on the Android UIThread
which is the one we are now using for rendering after #2909.

If the context is not restored, nothing gets rendered (or
sometimes partially, or artifacts) because Android will try
to do GL stuff on the context used by Mapbox GL and mess things up.
This commit is contained in:
Thiago Marcos P. Santos
2016-04-15 17:54:10 +03:00
parent 378885e51a
commit 7d80690f91
2 changed files with 18 additions and 1 deletions

View File

@@ -127,6 +127,11 @@ std::array<uint16_t, 2> NativeMapView::getFramebufferSize() const {
void NativeMapView::activate() {
mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::activate");
oldDisplay = eglGetCurrentDisplay();
oldReadSurface = eglGetCurrentSurface(EGL_READ);
oldDrawSurface = eglGetCurrentSurface(EGL_DRAW);
oldContext = eglGetCurrentContext();
assert(vm != nullptr);
if ((display != EGL_NO_DISPLAY) && (surface != EGL_NO_SURFACE) && (context != EGL_NO_CONTEXT)) {
@@ -150,7 +155,13 @@ void NativeMapView::deactivate() {
assert(vm != nullptr);
if (display != EGL_NO_DISPLAY) {
if (oldContext != context && oldContext != EGL_NO_CONTEXT) {
if (!eglMakeCurrent(oldDisplay, oldDrawSurface, oldReadSurface, oldContext)) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglMakeCurrent() returned error %d",
eglGetError());
throw std::runtime_error("eglMakeCurrent() failed");
}
} else if (display != EGL_NO_DISPLAY) {
if (!eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglMakeCurrent(EGL_NO_CONTEXT) returned error %d",
eglGetError());

View File

@@ -61,6 +61,12 @@ private:
jweak obj = nullptr;
ANativeWindow *window = nullptr;
EGLDisplay oldDisplay = EGL_NO_DISPLAY;
EGLSurface oldReadSurface = EGL_NO_SURFACE;
EGLSurface oldDrawSurface = EGL_NO_SURFACE;
EGLContext oldContext = EGL_NO_CONTEXT;
EGLDisplay display = EGL_NO_DISPLAY;
EGLSurface surface = EGL_NO_SURFACE;
EGLContext context = EGL_NO_CONTEXT;