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

Commit

Permalink
[android] extract View and RendererBackend from NativeMapView
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen committed Jul 21, 2017
1 parent 748b815 commit 4067c2a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 92 deletions.
2 changes: 2 additions & 0 deletions platform/android/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
55 changes: 55 additions & 0 deletions platform/android/src/android_renderer_backend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "android_renderer_backend.hpp"

#include <EGL/egl.h>

#include <cassert>

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());
}

const mbgl::Size AndroidRendererBackend::getFramebufferSize() const {
return { static_cast<uint32_t>(fbWidth), static_cast<uint32_t>(fbHeight) };
}

/**
* From mbgl::RendererBackend.
*/
void AndroidRendererBackend::updateAssumedState() {
assumeFramebufferBinding(0);
assumeViewport(0, 0, getFramebufferSize());
}

}
}
40 changes: 40 additions & 0 deletions platform/android/src/android_renderer_backend.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <mbgl/renderer/renderer_backend.hpp>
#include <mbgl/map/view.hpp>

namespace mbgl {
namespace android {

class AndroidRendererBackend : public RendererBackend, public View {
public:

// mbgl::View //
void bind() override;

// mbgl::RendererBackend //
void updateAssumedState() override;

void updateViewPort();

void resizeFramebuffer(int width, int height);
const mbgl::Size getFramebufferSize() const;
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
82 changes: 10 additions & 72 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -59,7 +60,8 @@ NativeMapView::NativeMapView(jni::JNIEnv& _env,
jni::Object<FileSource> jFileSource,
jni::jfloat _pixelRatio,
jni::String _programCacheDir)
: javaPeer(_obj.NewWeakGlobalRef(_env)),
: rendererBackend(std::make_unique<AndroidRendererBackend>()),
javaPeer(_obj.NewWeakGlobalRef(_env)),
pixelRatio(_pixelRatio),
threadPool(sharedThreadPool()) {

Expand All @@ -72,8 +74,8 @@ NativeMapView::NativeMapView(jni::JNIEnv& _env,
auto& fileSource = mbgl::android::FileSource::getDefaultFileSource(_env, jFileSource);

// Create a renderer
auto renderer = std::make_unique<Renderer>(*this, pixelRatio, fileSource, *threadPool,
GLContextMode::Unique,
auto renderer = std::make_unique<Renderer>(*rendererBackend, pixelRatio, fileSource,
*threadPool, GLContextMode::Unique,
jni::Make<std::string>(_env, _programCacheDir));

// Create a renderer frontend
Expand Down Expand Up @@ -105,69 +107,6 @@ NativeMapView::~NativeMapView() {
vm = nullptr;
}

/**
* From mbgl::View
*/
void NativeMapView::bind() {
setFramebufferBinding(0);
setViewport(0, 0, getFramebufferSize());
}

/**
* 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().
*
Expand Down Expand Up @@ -267,20 +206,20 @@ 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;
}

rendererFrontend->render(*this);
rendererFrontend->render(*rendererBackend);

if(snapshot){
snapshot = false;

// take snapshot
auto image = readFramebuffer(getFramebufferSize());
auto image = rendererBackend->readFramebuffer();
auto bitmap = Bitmap::CreateBitmap(env, std::move(image));

// invoke Mapview#OnSnapshotReady
Expand Down Expand Up @@ -313,8 +252,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();
}
Expand Down
23 changes: 3 additions & 20 deletions platform/android/src/native_map_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
namespace mbgl {
namespace android {

class AndroidRendererBackend;
class AndroidRendererFrontend;

class NativeMapView : public View, public RendererBackend, public MapObserver {
class NativeMapView : public MapObserver {
public:

static constexpr auto Name() { return "com/mapbox/mapboxsdk/maps/NativeMapView"; };
Expand All @@ -55,14 +56,6 @@ class NativeMapView : public View, public RendererBackend, public MapObserver {

virtual ~NativeMapView();

// mbgl::View //

void bind() override;

// mbgl::RendererBackend //

void updateAssumedState() override;

// Deprecated //
void notifyMapChange(mbgl::MapChange);

Expand Down Expand Up @@ -259,13 +252,6 @@ class NativeMapView : public View, public RendererBackend, public MapObserver {

jni::jboolean getPrefetchesTiles(JNIEnv&);

protected:
// mbgl::RendererBackend //

gl::ProcAddress initializeExtension(const char*) override;
void activate() override;
void deactivate() override;

private:
void _initializeDisplay();

Expand All @@ -281,12 +267,11 @@ class NativeMapView : public View, public RendererBackend, public MapObserver {

EGLConfig chooseConfig(const EGLConfig configs[], EGLint numConfigs);

mbgl::Size getFramebufferSize() const;

void updateFps();

private:
std::unique_ptr<AndroidRendererFrontend> rendererFrontend;
std::unique_ptr<AndroidRendererBackend> rendererBackend;

JavaVM *vm = nullptr;
jni::UniqueWeakObject<NativeMapView> javaPeer;
Expand Down Expand Up @@ -318,8 +303,6 @@ class NativeMapView : public View, public RendererBackend, public MapObserver {
// 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;

Expand Down

0 comments on commit 4067c2a

Please sign in to comment.