[core] use raii to guard backend deactivation

This commit is contained in:
Ivo van Dongen
2016-12-06 17:24:32 +01:00
committed by Jesse Bounds
parent 1ab791c3dc
commit 18730d598d
4 changed files with 37 additions and 28 deletions

View File

@@ -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;
// Notifies a watcher of map x/y/scale/rotation changes.
virtual void notifyMapChange(MapChange change);
private:
const std::unique_ptr<gl::Context> context;
friend class BackendScope;
};
class BackendScope {
public:
BackendScope(Backend& backend_) : backend(backend_) {
backend.activate();
}
~BackendScope() {
backend.deactivate();
}
private:
Backend& backend;
};
} // namespace mbgl

View File

@@ -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<mbgl::gl::glProc>(eglGetProcAddress(name));
});
deactivate();
}
}

View File

@@ -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);

View File

@@ -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> 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) {
@@ -862,13 +857,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;
}
@@ -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();