[core] Ensure that a BackendScope exists when doing GL calls

This commit is contained in:
Konstantin Käfer
2017-02-27 18:33:16 +01:00
committed by John Firebaugh
parent d7227e13a7
commit 8e5214144e
20 changed files with 73 additions and 9 deletions

View File

@@ -2,6 +2,7 @@
#include <mbgl/benchmark/util.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/util/default_thread_pool.hpp>
@@ -34,6 +35,7 @@ public:
util::RunLoop loop;
HeadlessBackend backend;
BackendScope scope { backend };
OffscreenView view{ backend.getContext(), { 1000, 1000 } };
DefaultFileSource fileSource{ "benchmark/fixtures/api/cache.db", "." };
ThreadPool threadPool{ 4 };

View File

@@ -1,4 +1,5 @@
#include <mbgl/map/map.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>
@@ -85,6 +86,7 @@ int main(int argc, char *argv[]) {
}
HeadlessBackend backend;
BackendScope scope { backend };
OffscreenView view(backend.getContext(), { width * pixelRatio, height * pixelRatio });
ThreadPool threadPool(4);
Map map(backend, mbgl::Size { width, height }, pixelRatio, fileSource, threadPool, MapMode::Still);

View File

@@ -6,13 +6,26 @@ class Backend;
class BackendScope {
public:
BackendScope(Backend&);
// There are two types of scopes: Creating an "Implicit" scope tells Mapbox GL that the
// supporting windowing system has already activated the GL Backend and that no further actions
// are required. Creating an "Explicit" scope actually enables the GL Backend, and disables it
// when the BackendScope is destroyed.
enum class ScopeType : bool {
Implicit,
Explicit,
};
BackendScope(Backend&, ScopeType = ScopeType::Explicit);
~BackendScope();
// Returns true when there is currently a BackendScope active in this thread.
static bool exists();
private:
BackendScope* priorScope;
BackendScope* nextScope;
Backend& backend;
const ScopeType scopeType;
};
} // namespace mbgl

View File

@@ -10,6 +10,7 @@
#include <mbgl/util/platform.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/map/camera.hpp>
#include <mbgl/gl/state.hpp>
@@ -462,6 +463,7 @@ void GLFWView::run() {
const double started = glfwGetTime();
glfwMakeContextCurrent(window);
mbgl::BackendScope scope { *this, mbgl::BackendScope::ScopeType::Implicit };
updateViewBinding();
map->render(*this);

View File

@@ -21,6 +21,7 @@
#include <mbgl/style/transition_options.hpp>
#include <mbgl/style/layers/custom_layer.hpp>
#include <mbgl/map/backend.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/math/wrap.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/util/geo.hpp>
@@ -881,6 +882,9 @@ public:
{
if ( ! self.dormant)
{
// The OpenGL implementation automatically enables the OpenGL context for us.
mbgl::BackendScope scope { *_mbglView, mbgl::BackendScope::ScopeType::Implicit };
_mbglView->updateViewBinding();
_mbglMap->render(*_mbglView);

View File

@@ -29,6 +29,7 @@
#import <mbgl/gl/extension.hpp>
#import <mbgl/gl/context.hpp>
#import <mbgl/map/backend.hpp>
#import <mbgl/map/backend_scope.hpp>
#import <mbgl/sprite/sprite_image.hpp>
#import <mbgl/storage/default_file_source.hpp>
#import <mbgl/storage/network_status.hpp>
@@ -780,6 +781,8 @@ public:
return reinterpret_cast<mbgl::gl::glProc>(symbol);
});
// The OpenGL implementation automatically enables the OpenGL context for us.
mbgl::BackendScope scope { *_mbglView, mbgl::BackendScope::ScopeType::Implicit };
_mbglView->updateViewBinding();
_mbglMap->render(*_mbglView);
@@ -2872,7 +2875,8 @@ public:
fbo = mbgl::gl::value::BindFramebuffer::Get();
getContext().bindFramebuffer.setCurrentValue(fbo);
getContext().viewport.setCurrentValue(getViewport());
assert(mbgl::gl::value::Viewport::Get() == getContext().viewport.getCurrentValue());
auto actualViewport = mbgl::gl::value::Viewport::Get();
assert(actualViewport == getContext().viewport.getCurrentValue());
}
void bind() override {

View File

@@ -10,6 +10,7 @@
#include <mbgl/style/conversion/layer.hpp>
#include <mbgl/style/conversion/filter.hpp>
#include <mbgl/sprite/sprite_image.cpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/map/query.hpp>
#include <unistd.h>
@@ -369,6 +370,7 @@ void NodeMap::startRender(NodeMap::RenderOptions options) {
static_cast<uint32_t>(options.height * pixelRatio) };
if (!view || view->getSize() != fbSize) {
view.reset();
mbgl::BackendScope scope { backend };
view = std::make_unique<mbgl::OffscreenView>(backend.getContext(), fbSize);
}

View File

@@ -8,6 +8,7 @@
#include <mbgl/map/camera.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/layer.hpp>
#include <mbgl/style/conversion/source.hpp>
@@ -1495,6 +1496,9 @@ void QMapboxGL::render()
}
#endif
// The OpenGL implementation automatically enables the OpenGL context for us.
mbgl::BackendScope scope { *d_ptr, mbgl::BackendScope::ScopeType::Implicit };
d_ptr->dirty = false;
d_ptr->updateViewBinding();
d_ptr->mapObj->render(*d_ptr);

View File

@@ -1,4 +1,5 @@
#include <mbgl/map/backend.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/gl/context.hpp>
#include <cassert>
@@ -9,6 +10,7 @@ Backend::Backend() : context(std::make_unique<gl::Context>()) {
}
gl::Context& Backend::getContext() {
assert(BackendScope::exists());
return *context;
}

View File

@@ -8,15 +8,19 @@ namespace mbgl {
static util::ThreadLocal<BackendScope> currentScope;
BackendScope::BackendScope(Backend& backend_)
BackendScope::BackendScope(Backend& backend_, ScopeType scopeType_)
: priorScope(currentScope.get()),
nextScope(nullptr),
backend(backend_) {
backend(backend_),
scopeType(scopeType_) {
if (priorScope) {
assert(priorScope->nextScope == nullptr);
priorScope->nextScope = this;
}
backend.activate();
if (scopeType == ScopeType::Explicit) {
backend.activate();
}
currentScope.set(this);
}
@@ -28,9 +32,16 @@ BackendScope::~BackendScope() {
assert(priorScope->nextScope == this);
priorScope->nextScope = nullptr;
} else {
backend.deactivate();
if (scopeType == ScopeType::Explicit) {
backend.deactivate();
}
currentScope.set(nullptr);
}
}
bool BackendScope::exists() {
return currentScope.get();
}
} // namespace mbgl

View File

@@ -267,8 +267,9 @@ void Map::Impl::render(View& view) {
updateFlags = Update::Nothing;
gl::Context& context = backend.getContext();
if (!painter) {
painter = std::make_unique<Painter>(backend.getContext(), transform.getState(), pixelRatio, programCacheDir);
painter = std::make_unique<Painter>(context, transform.getState(), pixelRatio, programCacheDir);
}
if (mode == MapMode::Continuous) {

View File

@@ -5,6 +5,7 @@
#include <mbgl/annotation/annotation.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/util/io.hpp>
@@ -24,6 +25,7 @@ class AnnotationTest {
public:
util::RunLoop loop;
HeadlessBackend backend { test::sharedDisplay() };
BackendScope scope { backend };
OffscreenView view { backend.getContext() };
StubFileSource fileSource;
ThreadPool threadPool { 4 };

View File

@@ -3,6 +3,7 @@
#include <mbgl/test/fixture_log_observer.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/storage/online_file_source.hpp>
@@ -22,6 +23,7 @@ TEST(API, RenderWithoutCallback) {
util::RunLoop loop;
HeadlessBackend backend { test::sharedDisplay() };
BackendScope scope { backend };
OffscreenView view { backend.getContext(), { 128, 512 } };
StubFileSource fileSource;
ThreadPool threadPool(4);
@@ -47,6 +49,7 @@ TEST(API, RenderWithoutStyle) {
util::RunLoop loop;
HeadlessBackend backend { test::sharedDisplay() };
BackendScope scope { backend };
OffscreenView view { backend.getContext(), { 128, 512 } };
StubFileSource fileSource;
ThreadPool threadPool(4);

View File

@@ -2,6 +2,7 @@
#include <mbgl/gl/gl.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/util/default_thread_pool.hpp>
@@ -86,6 +87,7 @@ TEST(CustomLayer, Basic) {
util::RunLoop loop;
HeadlessBackend backend { test::sharedDisplay() };
BackendScope scope { backend };
OffscreenView view { backend.getContext() };
#ifdef MBGL_ASSET_ZIP

View File

@@ -1,4 +1,5 @@
#include <mbgl/map/map.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/util/default_thread_pool.hpp>
@@ -29,6 +30,7 @@ public:
util::RunLoop loop;
HeadlessBackend backend { test::sharedDisplay() };
BackendScope scope { backend };
OffscreenView view { backend.getContext() };
StubFileSource fileSource;
ThreadPool threadPool { 4 };

View File

@@ -2,6 +2,7 @@
#include <mbgl/test/fixture_log_observer.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/util/default_thread_pool.hpp>
@@ -26,6 +27,7 @@ TEST(API, TEST_REQUIRES_SERVER(RenderMissingTile)) {
const auto style = util::read_file("test/fixtures/api/water_missing_tiles.json");
HeadlessBackend backend { test::sharedDisplay() };
BackendScope scope { backend };
OffscreenView view { backend.getContext(), { 256, 512 } };
#ifdef MBGL_ASSET_ZIP
// Regenerate with `cd test/fixtures/api/ && zip -r assets.zip assets/`

View File

@@ -2,6 +2,7 @@
#include <mbgl/test/fixture_log_observer.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/util/default_thread_pool.hpp>
@@ -22,6 +23,7 @@ TEST(API, RepeatedRender) {
const auto style = util::read_file("test/fixtures/api/water.json");
HeadlessBackend backend { test::sharedDisplay() };
BackendScope scope { backend };
OffscreenView view { backend.getContext(), { 256, 512 } };
#ifdef MBGL_ASSET_ZIP
// Regenerate with `cd test/fixtures/api/ && zip -r assets.zip assets/`

View File

@@ -62,8 +62,8 @@ TEST(GLObject, Value) {
TEST(GLObject, Store) {
HeadlessBackend backend { test::sharedDisplay() };
OffscreenView view(backend.getContext());
BackendScope scope { backend };
OffscreenView view(backend.getContext());
gl::Context context;
EXPECT_TRUE(context.empty());

View File

@@ -47,6 +47,7 @@ public:
struct MapTest {
util::RunLoop runLoop;
BackendTest backend;
BackendScope scope { backend };
OffscreenView view { backend.getContext() };
StubFileSource fileSource;
ThreadPool threadPool { 4 };
@@ -552,6 +553,7 @@ public:
TEST(Map, TEST_DISABLED_ON_CI(ContinuousRendering)) {
util::RunLoop runLoop;
MockBackend backend { test::sharedDisplay() };
BackendScope scope { backend };
OffscreenView view { backend.getContext() };
ThreadPool threadPool { 4 };
@@ -585,7 +587,7 @@ TEST(Map, TEST_DISABLED_ON_CI(ContinuousRendering)) {
});
}
BackendScope scope(backend);
BackendScope scope2(backend);
map.render(view);
}};

View File

@@ -3,6 +3,7 @@
#include <mbgl/test/util.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/util/default_thread_pool.hpp>
@@ -35,6 +36,7 @@ public:
util::RunLoop runLoop;
HeadlessBackend backend { test::sharedDisplay() };
BackendScope scope { backend };
OffscreenView view{ backend.getContext(), { 512, 512 } };
StubFileSource fileSource;
ThreadPool threadPool { 4 };