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

Commit

Permalink
[core] use raii to guard backend deactivation
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen committed Dec 7, 2016
1 parent 11229d7 commit a1cf025
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
31 changes: 25 additions & 6 deletions include/mbgl/map/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace gl {
class Context;
} // namespace gl

class BackendScope;

class Backend {
public:
Backend();
Expand All @@ -18,6 +20,14 @@ class Backend {
// 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:
//
Expand All @@ -31,15 +41,24 @@ class Backend {
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
8 changes: 2 additions & 6 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void NativeMapView::invalidate() {
}

void NativeMapView::render() {
activate();
BackendScope guard(*this);

if (framebufferSizeChanged) {
getContext().viewport = { 0, 0, getFramebufferSize() };
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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",
Expand All @@ -421,8 +419,6 @@ void NativeMapView::createSurface(ANativeWindow *window_) {
mbgl::gl::InitializeExtensions([] (const char * name) {
return reinterpret_cast<mbgl::gl::glProc>(eglGetProcAddress(name));
});

deactivate();
}
}

Expand Down
6 changes: 4 additions & 2 deletions platform/android/src/native_map_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class NativeMapView : public mbgl::View, public mbgl::Backend {
void updateViewBinding();
void bind() override;

void activate() override;
void deactivate() override;
void invalidate() override;

void notifyMapChange(mbgl::MapChange) override;
Expand Down Expand Up @@ -54,6 +52,10 @@ class NativeMapView : public mbgl::View, public mbgl::Backend {

void scheduleTakeSnapshot();

protected:
void activate() override;
void deactivate() override;

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

Expand Down
18 changes: 5 additions & 13 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Map::Impl::Impl(Map& map_,
}

Map::~Map() {
impl->backend.activate();
BackendScope guard(impl->backend);

impl->styleRequest = nullptr;

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a1cf025

Please sign in to comment.