summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2016-12-06 17:24:32 +0100
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2016-12-12 09:05:23 -0500
commitc76a933514e4e1514a58ac0e668b13eebab37794 (patch)
treec369563359007d730f427e04d9fd4945f3f97fbc
parentffd74c309e0b44bdf3ff09c6c1dc715ad97f64cd (diff)
downloadqtlocation-mapboxgl-c76a933514e4e1514a58ac0e668b13eebab37794.tar.gz
[core] use raii to guard backend deactivation
-rw-r--r--include/mbgl/map/backend.hpp31
-rwxr-xr-xplatform/android/src/native_map_view.cpp8
-rwxr-xr-xplatform/android/src/native_map_view.hpp6
-rw-r--r--src/mbgl/map/map.cpp18
4 files changed, 36 insertions, 27 deletions
diff --git a/include/mbgl/map/backend.hpp b/include/mbgl/map/backend.hpp
index c11d094906..0468449155 100644
--- a/include/mbgl/map/backend.hpp
+++ b/include/mbgl/map/backend.hpp
@@ -10,6 +10,8 @@ namespace gl {
class Context;
} // namespace gl
+class BackendScope;
+
class Backend {
public:
Backend();
@@ -18,6 +20,14 @@ public:
// Returns the backend's context which manages OpenGL state.
gl::Context& getContext();
+ // Called when the map needs to be rendered; the backend should call Map::render() at some point
+ // in the near future. (Not called for Map::renderStill() mode.)
+ virtual void invalidate() = 0;
+
+ // Notifies a watcher of map x/y/scale/rotation changes.
+ virtual void notifyMapChange(MapChange change);
+
+protected:
// Called when the backend's GL context needs to be made active or inactive. These are called,
// as a matched pair, in four situations:
//
@@ -31,15 +41,24 @@ public:
virtual void activate() = 0;
virtual void deactivate() = 0;
- // Called when the map needs to be rendered; the backend should call Map::render() at some point
- // in the near future. (Not called for Map::renderStill() mode.)
- virtual void invalidate() = 0;
+private:
+ const std::unique_ptr<gl::Context> context;
- // Notifies a watcher of map x/y/scale/rotation changes.
- virtual void notifyMapChange(MapChange change);
+ friend class BackendScope;
+};
+
+class BackendScope {
+public:
+ BackendScope(Backend& backend_) : backend(backend_) {
+ backend.activate();
+ }
+
+ ~BackendScope() {
+ backend.deactivate();
+ }
private:
- const std::unique_ptr<gl::Context> context;
+ Backend& backend;
};
} // namespace mbgl
diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp
index 83f351b7b3..8234b74af2 100755
--- a/platform/android/src/native_map_view.cpp
+++ b/platform/android/src/native_map_view.cpp
@@ -175,7 +175,7 @@ void NativeMapView::invalidate() {
}
void NativeMapView::render() {
- activate();
+ BackendScope guard(*this);
if (framebufferSizeChanged) {
getContext().viewport = { 0, 0, getFramebufferSize() };
@@ -214,8 +214,6 @@ void NativeMapView::render() {
} else {
mbgl::Log::Info(mbgl::Event::Android, "Not swapping as we are not ready");
}
-
- deactivate();
}
mbgl::Map &NativeMapView::getMap() { return *map; }
@@ -410,7 +408,7 @@ void NativeMapView::createSurface(ANativeWindow *window_) {
if (!firstTime) {
firstTime = true;
- activate();
+ BackendScope guard(*this);
if (!eglMakeCurrent(display, surface, surface, context)) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglMakeCurrent() returned error %d",
@@ -421,8 +419,6 @@ void NativeMapView::createSurface(ANativeWindow *window_) {
mbgl::gl::InitializeExtensions([] (const char * name) {
return reinterpret_cast<mbgl::gl::glProc>(eglGetProcAddress(name));
});
-
- deactivate();
}
}
diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp
index 35c0307539..e7379700a9 100755
--- a/platform/android/src/native_map_view.hpp
+++ b/platform/android/src/native_map_view.hpp
@@ -24,8 +24,6 @@ public:
void updateViewBinding();
void bind() override;
- void activate() override;
- void deactivate() override;
void invalidate() override;
void notifyMapChange(mbgl::MapChange) override;
@@ -54,6 +52,10 @@ public:
void scheduleTakeSnapshot();
+protected:
+ void activate() override;
+ void deactivate() override;
+
private:
EGLConfig chooseConfig(const EGLConfig configs[], EGLint numConfigs);
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp
index 40f4e91c9d..7e7d0417b9 100644
--- a/src/mbgl/map/map.cpp
+++ b/src/mbgl/map/map.cpp
@@ -147,7 +147,7 @@ Map::Impl::Impl(Map& map_,
}
Map::~Map() {
- impl->backend.activate();
+ BackendScope guard(impl->backend);
impl->styleRequest = nullptr;
@@ -156,8 +156,6 @@ Map::~Map() {
impl->style.reset();
impl->annotationManager.reset();
impl->painter.reset();
-
- impl->backend.deactivate();
}
void Map::renderStill(View& view, StillImageCallback callback) {
@@ -282,9 +280,8 @@ void Map::Impl::update() {
backend.invalidate();
} else if (stillImageRequest && style->isLoaded()) {
// TODO: determine whether we need activate/deactivate
- backend.activate();
+ BackendScope guard(backend);
render(stillImageRequest->view);
- backend.deactivate();
}
updateFlags = Update::Nothing;
@@ -882,12 +879,10 @@ void Map::addLayer(std::unique_ptr<Layer> layer, const optional<std::string>& be
}
impl->styleMutated = true;
- impl->backend.activate();
+ BackendScope guard(impl->backend);
impl->style->addLayer(std::move(layer), before);
impl->onUpdate(Update::Classes);
-
- impl->backend.deactivate();
}
std::unique_ptr<Layer> Map::removeLayer(const std::string& id) {
@@ -896,13 +891,11 @@ std::unique_ptr<Layer> Map::removeLayer(const std::string& id) {
}
impl->styleMutated = true;
- impl->backend.activate();
+ BackendScope guard(impl->backend);
auto removedLayer = impl->style->removeLayer(id);
impl->onUpdate(Update::Classes);
- impl->backend.deactivate();
-
return removedLayer;
}
@@ -1060,9 +1053,8 @@ void Map::setSourceTileCacheSize(size_t size) {
void Map::onLowMemory() {
if (impl->painter) {
- impl->backend.activate();
+ BackendScope guard(impl->backend);
impl->painter->cleanup();
- impl->backend.deactivate();
}
if (impl->style) {
impl->style->onLowMemory();