From 7d80690f91bc2c09de03a8bd69bb58e88347c6f5 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Fri, 15 Apr 2016 17:54:10 +0300 Subject: [PATCH] [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. --- platform/android/src/native_map_view.cpp | 13 ++++++++++++- platform/android/src/native_map_view.hpp | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 45d078a39..d6405c53c 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -127,6 +127,11 @@ std::array 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()); diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index 0c29c2fe5..5bae79ad9 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -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;