From e780cdc29ee82459a75db5aba2e72c5536a2d1b0 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Thu, 20 Jul 2017 14:19:31 +0300 Subject: [android] extract RendererBackend from NativeMapView --- platform/android/config.cmake | 2 + platform/android/src/android_renderer_backend.cpp | 55 +++++++++++++ platform/android/src/android_renderer_backend.hpp | 37 +++++++++ platform/android/src/native_map_view.cpp | 95 +++-------------------- platform/android/src/native_map_view.hpp | 26 +------ 5 files changed, 109 insertions(+), 106 deletions(-) create mode 100755 platform/android/src/android_renderer_backend.cpp create mode 100755 platform/android/src/android_renderer_backend.hpp diff --git a/platform/android/config.cmake b/platform/android/config.cmake index dc6b1c80a6..539515a6e5 100644 --- a/platform/android/config.cmake +++ b/platform/android/config.cmake @@ -73,6 +73,8 @@ macro(mbgl_platform_core) PRIVATE platform/default/mbgl/util/default_thread_pool.hpp # Rendering + PRIVATE platform/android/src/android_renderer_backend.cpp + PRIVATE platform/android/src/android_renderer_backend.hpp PRIVATE platform/android/src/android_renderer_frontend.cpp PRIVATE platform/android/src/android_renderer_frontend.hpp diff --git a/platform/android/src/android_renderer_backend.cpp b/platform/android/src/android_renderer_backend.cpp new file mode 100755 index 0000000000..558395d296 --- /dev/null +++ b/platform/android/src/android_renderer_backend.cpp @@ -0,0 +1,55 @@ +#include "android_renderer_backend.hpp" + +#include + +#include + +namespace mbgl { +namespace android { + +/** + * From mbgl::View + */ +void AndroidRendererBackend::bind() { + assert(BackendScope::exists()); + setFramebufferBinding(0); + setViewport(0, 0, getFramebufferSize()); +} + +/** + * From mbgl::RendererBackend. + */ +gl::ProcAddress AndroidRendererBackend::initializeExtension(const char* name) { + assert(BackendScope::exists()); + return eglGetProcAddress(name); +} + +void AndroidRendererBackend::updateViewPort() { + assert(BackendScope::exists()); + setViewport(0, 0, getFramebufferSize()); +} + +void AndroidRendererBackend::resizeFramebuffer(int width, int height) { + fbWidth = width; + fbHeight = height; +} + +PremultipliedImage AndroidRendererBackend::readFramebuffer() const { + assert(BackendScope::exists()); + return RendererBackend::readFramebuffer(getFramebufferSize()); +} + +mbgl::Size AndroidRendererBackend::getFramebufferSize() const { + return { static_cast(fbWidth), static_cast(fbHeight) }; +} + +/** + * From mbgl::RendererBackend. + */ +void AndroidRendererBackend::updateAssumedState() { + assumeFramebufferBinding(0); + assumeViewport(0, 0, getFramebufferSize()); +} + +} +} diff --git a/platform/android/src/android_renderer_backend.hpp b/platform/android/src/android_renderer_backend.hpp new file mode 100755 index 0000000000..8dbc1f2be8 --- /dev/null +++ b/platform/android/src/android_renderer_backend.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include + +namespace mbgl { +namespace android { + +class AndroidRendererBackend : public RendererBackend { +public: + + // mbgl::RendererBackend // + void bind() override; + void updateAssumedState() override; + mbgl::Size getFramebufferSize() const override; + + void updateViewPort(); + + void resizeFramebuffer(int width, int height); + PremultipliedImage readFramebuffer() const; + +protected: + // mbgl::RendererBackend // + gl::ProcAddress initializeExtension(const char*) override; + void activate() override {}; + void deactivate() override {}; + + +private: + + // Minimum texture size according to OpenGL ES 2.0 specification. + int fbWidth = 64; + int fbHeight = 64; + +}; + +} // namespace android +} // namespace mbgl diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index fb00cec1e2..a0d88e8cf1 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -43,6 +43,7 @@ #include "jni.hpp" #include "attach_env.hpp" +#include "android_renderer_backend.hpp" #include "android_renderer_frontend.hpp" #include "bitmap.hpp" #include "run_loop_impl.hpp" @@ -60,7 +61,8 @@ NativeMapView::NativeMapView(jni::JNIEnv& _env, jni::Object jFileSource, jni::jfloat _pixelRatio, jni::String _programCacheDir) - : javaPeer(_obj.NewWeakGlobalRef(_env)), + : rendererBackend(std::make_unique()), + javaPeer(_obj.NewWeakGlobalRef(_env)), pixelRatio(_pixelRatio), threadPool(sharedThreadPool()) { @@ -73,13 +75,13 @@ NativeMapView::NativeMapView(jni::JNIEnv& _env, auto& fileSource = mbgl::android::FileSource::getDefaultFileSource(_env, jFileSource); // Create a renderer - auto renderer = std::make_unique(*this, pixelRatio, fileSource, *threadPool, - GLContextMode::Unique, + auto renderer = std::make_unique(*rendererBackend, pixelRatio, fileSource, + *threadPool, GLContextMode::Unique, jni::Make(_env, _programCacheDir)); // Create a renderer frontend rendererFrontend = std::make_unique(std::move(renderer), - *this, + *rendererBackend, [this] { this->invalidate(); }); // Create the core map @@ -108,74 +110,7 @@ NativeMapView::~NativeMapView() { } /** - * From mbgl::RendererBackend - */ -void NativeMapView::bind() { - setFramebufferBinding(0); - setViewport(0, 0, getFramebufferSize()); -} - -mbgl::Size NativeMapView::getFramebufferSize() const { - return { static_cast(fbWidth), static_cast(fbHeight) }; -} - -/** - * From mbgl::RendererBackend. - */ -gl::ProcAddress NativeMapView::initializeExtension(const char* name) { - return eglGetProcAddress(name); -} - -void 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)) { - if (!eglMakeCurrent(display, surface, surface, context)) { - mbgl::Log::Error(mbgl::Event::OpenGL, "eglMakeCurrent() returned error %d", - eglGetError()); - throw std::runtime_error("eglMakeCurrent() failed"); - } - - if (!eglSwapInterval(display, 0)) { - mbgl::Log::Error(mbgl::Event::OpenGL, "eglSwapInterval() returned error %d", eglGetError()); - throw std::runtime_error("eglSwapInterval() failed"); - } - } else { - mbgl::Log::Info(mbgl::Event::Android, "Not activating as we are not ready"); - } -} - -/** - * From mbgl::RendererBackend. - */ -void NativeMapView::deactivate() { - assert(vm != nullptr); - - 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()); - throw std::runtime_error("eglMakeCurrent() failed"); - } - } else { - mbgl::Log::Info(mbgl::Event::Android, "Not deactivating as we are not ready"); - } -} - -/** - * From mbgl::RendererBackend. Callback to java NativeMapView#onInvalidate(). + * Callback to java NativeMapView#onInvalidate(). * * May be called from any thread */ @@ -273,10 +208,10 @@ void NativeMapView::destroySurface(jni::JNIEnv&) { } void NativeMapView::render(jni::JNIEnv& env) { - BackendScope guard { *this }; + BackendScope guard { *rendererBackend }; if (framebufferSizeChanged) { - setViewport(0, 0, getFramebufferSize()); + rendererBackend->updateViewPort(); framebufferSizeChanged = false; } @@ -286,7 +221,7 @@ void NativeMapView::render(jni::JNIEnv& env) { snapshot = false; // take snapshot - auto image = readFramebuffer(getFramebufferSize()); + auto image = rendererBackend->readFramebuffer(); auto bitmap = Bitmap::CreateBitmap(env, std::move(image)); // invoke Mapview#OnSnapshotReady @@ -319,8 +254,7 @@ void NativeMapView::resizeView(jni::JNIEnv&, int w, int h) { } void NativeMapView::resizeFramebuffer(jni::JNIEnv&, int w, int h) { - fbWidth = w; - fbHeight = h; + rendererBackend->resizeFramebuffer(w, h); framebufferSizeChanged = true; invalidate(); } @@ -1404,7 +1338,7 @@ void NativeMapView::_createSurface(ANativeWindow *window_) { if (firstRender) { firstRender = false; - BackendScope guard(*this); + BackendScope guard(*rendererBackend); if (!eglMakeCurrent(display, surface, surface, context)) { mbgl::Log::Error(mbgl::Event::OpenGL, "eglMakeCurrent() returned error %d", @@ -1432,11 +1366,6 @@ void NativeMapView::_destroySurface() { } } -void NativeMapView::updateAssumedState() { - assumeFramebufferBinding(0); - assumeViewport(0, 0, getFramebufferSize()); -} - void NativeMapView::updateFps() { if (!fpsEnabled) { return; diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index bfd928d347..074906dbab 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -36,9 +36,10 @@ namespace mbgl { namespace android { +class AndroidRendererBackend; class AndroidRendererFrontend; -class NativeMapView : public RendererBackend, public MapObserver { +class NativeMapView : public MapObserver { public: static constexpr auto Name() { return "com/mapbox/mapboxsdk/maps/NativeMapView"; }; @@ -55,14 +56,6 @@ public: virtual ~NativeMapView(); - // mbgl::RendererBackend // - - void bind() override; - - mbgl::Size getFramebufferSize() const override; - - void updateAssumedState() override; - // Deprecated // void notifyMapChange(mbgl::MapChange); @@ -263,13 +256,6 @@ public: jni::jboolean getPrefetchesTiles(JNIEnv&); -protected: - // mbgl::RendererBackend // - - gl::ProcAddress initializeExtension(const char*) override; - void activate() override; - void deactivate() override; - private: void _initializeDisplay(); @@ -289,6 +275,7 @@ private: private: std::unique_ptr rendererFrontend; + std::unique_ptr rendererBackend; JavaVM *vm = nullptr; jni::UniqueWeakObject javaPeer; @@ -301,11 +288,6 @@ private: EGLConfig config = nullptr; EGLint format = -1; - 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; @@ -320,8 +302,6 @@ private: // Minimum texture size according to OpenGL ES 2.0 specification. int width = 64; int height = 64; - int fbWidth = 64; - int fbHeight = 64; bool framebufferSizeChanged = true; -- cgit v1.2.1