From 18730d598d29cf876064ce6964ef88ef0351f78f Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Tue, 6 Dec 2016 17:24:32 +0100 Subject: [core] use raii to guard backend deactivation --- include/mbgl/map/backend.hpp | 31 +++++++++++++++++++++++++------ platform/android/src/native_map_view.cpp | 8 ++------ platform/android/src/native_map_view.hpp | 6 ++++-- src/mbgl/map/map.cpp | 18 +++++------------- 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 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 context; + Backend& backend; }; } // namespace mbgl diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 8f594f2c0e..84daf82e8b 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -190,7 +190,7 @@ void NativeMapView::invalidate() { } void NativeMapView::render() { - activate(); + BackendScope guard(*this); updateViewBinding(); map->render(*this); @@ -235,8 +235,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; } @@ -421,7 +419,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", @@ -442,8 +440,6 @@ void NativeMapView::createSurface(ANativeWindow *window_) { mbgl::gl::InitializeExtensions([] (const char * name) { return reinterpret_cast(eglGetProcAddress(name)); }); - - deactivate(); } } diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index 43fb0c1ccd..00cb6e591e 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -23,8 +23,6 @@ public: void updateViewBinding(); void bind() override; - void activate() override; - void deactivate() override; void invalidate() override; void notifyMapChange(mbgl::MapChange) override; @@ -53,6 +51,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 f6ee6a5650..868b748af4 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -140,7 +140,7 @@ Map::Impl::Impl(Backend& backend_, } Map::~Map() { - impl->backend.activate(); + BackendScope guard(impl->backend); impl->styleRequest = nullptr; @@ -149,8 +149,6 @@ Map::~Map() { impl->style.reset(); impl->annotationManager.reset(); impl->painter.reset(); - - impl->backend.deactivate(); } void Map::renderStill(View& view, StillImageCallback callback) { @@ -275,9 +273,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; @@ -848,12 +845,10 @@ void Map::addLayer(std::unique_ptr layer, const optional& 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 Map::removeLayer(const std::string& id) { @@ -862,13 +857,11 @@ std::unique_ptr 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; } @@ -1033,9 +1026,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(); -- cgit v1.2.1