diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2019-04-01 16:56:24 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2019-04-05 11:49:17 +0200 |
commit | 66c020034e8260e5e071481e68ab61cd264723ba (patch) | |
tree | b5263772d72175474d91ce2daa9dc037b99fab0d /src/mbgl | |
parent | a782a6d15b80dd83105604f3f779f6c83ba222e5 (diff) | |
download | qtlocation-mapboxgl-66c020034e8260e5e071481e68ab61cd264723ba.tar.gz |
[core] refactor RendererBackend
Diffstat (limited to 'src/mbgl')
-rw-r--r-- | src/mbgl/gfx/renderer_backend.cpp | 22 | ||||
-rw-r--r-- | src/mbgl/gl/context.cpp | 6 | ||||
-rw-r--r-- | src/mbgl/gl/context.hpp | 4 | ||||
-rw-r--r-- | src/mbgl/gl/renderer_backend.cpp | 71 | ||||
-rw-r--r-- | src/mbgl/renderer/backend_scope.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/renderer/layers/render_custom_layer.cpp | 5 | ||||
-rw-r--r-- | src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/renderer/layers/render_fill_layer.cpp | 11 | ||||
-rw-r--r-- | src/mbgl/renderer/paint_parameters.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/renderer/paint_parameters.hpp | 6 | ||||
-rw-r--r-- | src/mbgl/renderer/renderer.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/renderer/renderer_backend.cpp | 68 | ||||
-rw-r--r-- | src/mbgl/renderer/renderer_impl.cpp | 16 | ||||
-rw-r--r-- | src/mbgl/renderer/renderer_impl.hpp | 9 |
14 files changed, 129 insertions, 97 deletions
diff --git a/src/mbgl/gfx/renderer_backend.cpp b/src/mbgl/gfx/renderer_backend.cpp new file mode 100644 index 0000000000..f0336f8b60 --- /dev/null +++ b/src/mbgl/gfx/renderer_backend.cpp @@ -0,0 +1,22 @@ +#include <mbgl/gfx/renderer_backend.hpp> +#include <mbgl/gfx/backend_scope.hpp> +#include <mbgl/gfx/context.hpp> + +namespace mbgl { +namespace gfx { + +RendererBackend::RendererBackend() = default; + +RendererBackend::~RendererBackend() = default; + +gfx::Context& RendererBackend::getContext() { + assert(BackendScope::exists()); + std::call_once(initialized, [this] { + context = createContext(); + }); + assert(context); + return *context; +} + +} // namespace gfx +} // namespace mbgl diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp index d33ae067f6..458bf9b649 100644 --- a/src/mbgl/gl/context.cpp +++ b/src/mbgl/gl/context.cpp @@ -1,5 +1,6 @@ #include <mbgl/gl/context.hpp> #include <mbgl/gl/enum.hpp> +#include <mbgl/gl/renderer_backend.hpp> #include <mbgl/gl/vertex_buffer_resource.hpp> #include <mbgl/gl/index_buffer_resource.hpp> #include <mbgl/gl/texture_resource.hpp> @@ -53,12 +54,12 @@ static_assert(underlying_type(UniformDataType::SamplerCube) == GL_SAMPLER_CUBE, static_assert(std::is_same<BinaryProgramFormat, GLenum>::value, "OpenGL type mismatch"); -Context::Context() +Context::Context(RendererBackend& backend_) : gfx::Context(gfx::ContextType::OpenGL, [] { GLint value; MBGL_CHECK_ERROR(glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &value)); return value; - }()) { + }()), backend(backend_) { } Context::~Context() { @@ -701,6 +702,7 @@ void Context::setColorMode(const gfx::ColorMode& color) { } std::unique_ptr<gfx::CommandEncoder> Context::createCommandEncoder() { + backend.updateAssumedState(); return std::make_unique<gl::CommandEncoder>(*this); } diff --git a/src/mbgl/gl/context.hpp b/src/mbgl/gl/context.hpp index 2fb1d242ce..0fad92f3df 100644 --- a/src/mbgl/gl/context.hpp +++ b/src/mbgl/gl/context.hpp @@ -27,6 +27,7 @@ namespace gl { constexpr size_t TextureMax = 64; using ProcAddress = void (*)(); +class RendererBackend; namespace extension { class VertexArray; @@ -36,7 +37,7 @@ class ProgramBinary; class Context final : public gfx::Context { public: - Context(); + Context(RendererBackend&); ~Context() override; Context(const Context&) = delete; Context& operator=(const Context& other) = delete; @@ -136,6 +137,7 @@ public: } private: + RendererBackend& backend; bool cleanupOnDestruction = true; std::unique_ptr<extension::Debugging> debugging; diff --git a/src/mbgl/gl/renderer_backend.cpp b/src/mbgl/gl/renderer_backend.cpp new file mode 100644 index 0000000000..ed3f38193f --- /dev/null +++ b/src/mbgl/gl/renderer_backend.cpp @@ -0,0 +1,71 @@ +#include <mbgl/gl/renderer_backend.hpp> +#include <mbgl/gfx/backend_scope.hpp> +#include <mbgl/gl/context.hpp> +#include <mbgl/gl/extension.hpp> + +#include <cassert> + +namespace mbgl { +namespace gl { + +RendererBackend::RendererBackend() = default; + +std::unique_ptr<gfx::Context> RendererBackend::createContext() { + auto result = std::make_unique<gl::Context>(*this); + result->enableDebugging(); + result->initializeExtensions( + std::bind(&RendererBackend::getExtensionFunctionPointer, this, std::placeholders::_1)); + // Needs move to placate GCC 4.9 + return std::move(result); +} + +gl::Context& RendererBackend::getGLContext() { + return static_cast<gl::Context&>(getContext()); +} + +PremultipliedImage RendererBackend::readFramebuffer(const Size& size) { + return getGLContext().readFramebuffer<PremultipliedImage>(size); +} + +void RendererBackend::assumeFramebufferBinding(const gl::FramebufferID fbo) { + getGLContext().bindFramebuffer.setCurrentValue(fbo); + if (fbo != ImplicitFramebufferBinding) { + assert(gl::value::BindFramebuffer::Get() == getGLContext().bindFramebuffer.getCurrentValue()); + } +} + +void RendererBackend::assumeViewport(int32_t x, int32_t y, const Size& size) { + getGLContext().viewport.setCurrentValue({ x, y, size }); + assert(gl::value::Viewport::Get() == getGLContext().viewport.getCurrentValue()); +} + +void RendererBackend::assumeScissorTest(bool enabled) { + getGLContext().scissorTest.setCurrentValue(enabled); + assert(gl::value::ScissorTest::Get() == getGLContext().scissorTest.getCurrentValue()); +} + +bool RendererBackend::implicitFramebufferBound() { + return getGLContext().bindFramebuffer.getCurrentValue() == ImplicitFramebufferBinding; +} + +void RendererBackend::setFramebufferBinding(const gl::FramebufferID fbo) { + getGLContext().bindFramebuffer = fbo; + if (fbo != ImplicitFramebufferBinding) { + assert(gl::value::BindFramebuffer::Get() == getGLContext().bindFramebuffer.getCurrentValue()); + } +} + +void RendererBackend::setViewport(int32_t x, int32_t y, const Size& size) { + getGLContext().viewport = { x, y, size }; + assert(gl::value::Viewport::Get() == getGLContext().viewport.getCurrentValue()); +} + +void RendererBackend::setScissorTest(bool enabled) { + getGLContext().scissorTest = enabled; + assert(gl::value::ScissorTest::Get() == getGLContext().scissorTest.getCurrentValue()); +} + +RendererBackend::~RendererBackend() = default; + +} // namespace gl +} // namespace mbgl diff --git a/src/mbgl/renderer/backend_scope.cpp b/src/mbgl/renderer/backend_scope.cpp index e4988a37c2..f3d7871473 100644 --- a/src/mbgl/renderer/backend_scope.cpp +++ b/src/mbgl/renderer/backend_scope.cpp @@ -1,5 +1,5 @@ -#include <mbgl/renderer/renderer_backend.hpp> #include <mbgl/gfx/backend_scope.hpp> +#include <mbgl/gfx/renderer_backend.hpp> #include <mbgl/util/thread_local.hpp> #include <cassert> diff --git a/src/mbgl/renderer/layers/render_custom_layer.cpp b/src/mbgl/renderer/layers/render_custom_layer.cpp index 240aef84ce..0b99460e76 100644 --- a/src/mbgl/renderer/layers/render_custom_layer.cpp +++ b/src/mbgl/renderer/layers/render_custom_layer.cpp @@ -1,12 +1,13 @@ #include <mbgl/renderer/layers/render_custom_layer.hpp> #include <mbgl/renderer/paint_parameters.hpp> -#include <mbgl/renderer/renderer_backend.hpp> +#include <mbgl/gfx/renderer_backend.hpp> #include <mbgl/gfx/backend_scope.hpp> #include <mbgl/renderer/bucket.hpp> #include <mbgl/platform/gl_functions.hpp> #include <mbgl/style/layers/custom_layer_impl.hpp> #include <mbgl/map/transform_state.hpp> #include <mbgl/gl/context.hpp> +#include <mbgl/gl/renderable_resource.hpp> #include <mbgl/util/mat4.hpp> namespace mbgl { @@ -86,7 +87,7 @@ void RenderCustomLayer::render(PaintParameters& paintParameters, RenderSource*) // Reset the view back to our original one, just in case the CustomLayer changed // the viewport or Framebuffer. - paintParameters.backend.bind(); + paintParameters.backend.getDefaultRenderable().getResource<gl::RenderableResource>().bind(); glContext.setDirtyState(); } diff --git a/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp b/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp index d7c59570c1..720d2ff2f8 100644 --- a/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp +++ b/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp @@ -4,7 +4,6 @@ #include <mbgl/renderer/paint_parameters.hpp> #include <mbgl/renderer/image_manager.hpp> #include <mbgl/renderer/render_static_data.hpp> -#include <mbgl/renderer/renderer_backend.hpp> #include <mbgl/programs/programs.hpp> #include <mbgl/programs/fill_extrusion_program.hpp> #include <mbgl/tile/tile.hpp> @@ -13,6 +12,7 @@ #include <mbgl/util/math.hpp> #include <mbgl/util/intersection_tests.hpp> #include <mbgl/tile/geometry_tile.hpp> +#include <mbgl/gfx/renderer_backend.hpp> #include <mbgl/gfx/cull_face_mode.hpp> #include <mbgl/gl/context.hpp> #include <mbgl/gl/renderable_resource.hpp> diff --git a/src/mbgl/renderer/layers/render_fill_layer.cpp b/src/mbgl/renderer/layers/render_fill_layer.cpp index d2ff5841ec..3e44f7bfb0 100644 --- a/src/mbgl/renderer/layers/render_fill_layer.cpp +++ b/src/mbgl/renderer/layers/render_fill_layer.cpp @@ -8,8 +8,10 @@ #include <mbgl/tile/tile.hpp> #include <mbgl/style/layers/fill_layer_impl.hpp> #include <mbgl/geometry/feature_index.hpp> +#include <mbgl/gfx/renderer_backend.hpp> #include <mbgl/gfx/cull_face_mode.hpp> -#include <mbgl/gl/context.hpp> +#include <mbgl/gfx/context.hpp> +#include <mbgl/gfx/renderable.hpp> #include <mbgl/util/math.hpp> #include <mbgl/util/intersection_tests.hpp> #include <mbgl/tile/geometry_tile.hpp> @@ -63,9 +65,6 @@ bool RenderFillLayer::hasCrossfade() const { } void RenderFillLayer::render(PaintParameters& parameters, RenderSource*) { - // TODO: remove cast - gl::Context& glContext = static_cast<gl::Context&>(parameters.context); - if (unevaluated.get<FillPattern>().isUndefined()) { for (const RenderTile& tile : renderTiles) { auto bucket_ = tile.tile.getBucket<FillBucket>(*baseImpl); @@ -89,7 +88,7 @@ void RenderFillLayer::render(PaintParameters& parameters, RenderSource*) { evaluated.get<FillTranslateAnchor>(), parameters.state) ), - uniforms::world::Value( glContext.viewport.getCurrentValue().size ), + uniforms::world::Value( parameters.backend.getDefaultRenderable().getSize() ), }, paintPropertyBinders, evaluated, @@ -174,7 +173,7 @@ void RenderFillLayer::render(PaintParameters& parameters, RenderSource*) { tile.translatedMatrix(evaluated.get<FillTranslate>(), evaluated.get<FillTranslateAnchor>(), parameters.state), - glContext.viewport.getCurrentValue().size, + parameters.backend.getDefaultRenderable().getSize(), geometryTile.iconAtlasTexture->size, crossfade, tile.id, diff --git a/src/mbgl/renderer/paint_parameters.cpp b/src/mbgl/renderer/paint_parameters.cpp index 3458c47da0..c162beaf61 100644 --- a/src/mbgl/renderer/paint_parameters.cpp +++ b/src/mbgl/renderer/paint_parameters.cpp @@ -9,7 +9,7 @@ namespace mbgl { PaintParameters::PaintParameters(gfx::Context& context_, float pixelRatio_, GLContextMode contextMode_, - RendererBackend& backend_, + gfx::RendererBackend& backend_, const UpdateParameters& updateParameters, const EvaluatedLight& evaluatedLight_, RenderStaticData& staticData_, diff --git a/src/mbgl/renderer/paint_parameters.hpp b/src/mbgl/renderer/paint_parameters.hpp index e553028f90..1329a14404 100644 --- a/src/mbgl/renderer/paint_parameters.hpp +++ b/src/mbgl/renderer/paint_parameters.hpp @@ -15,7 +15,6 @@ namespace mbgl { -class RendererBackend; class UpdateParameters; class RenderStaticData; class Programs; @@ -26,6 +25,7 @@ class UnwrappedTileID; namespace gfx { class Context; +class RendererBackend; class CommandEncoder; } // namespace gfx @@ -34,7 +34,7 @@ public: PaintParameters(gfx::Context&, float pixelRatio, GLContextMode, - RendererBackend&, + gfx::RendererBackend&, const UpdateParameters&, const EvaluatedLight&, RenderStaticData&, @@ -44,7 +44,7 @@ public: ~PaintParameters(); gfx::Context& context; - RendererBackend& backend; + gfx::RendererBackend& backend; const std::unique_ptr<gfx::CommandEncoder> encoder; const TransformState& state; diff --git a/src/mbgl/renderer/renderer.cpp b/src/mbgl/renderer/renderer.cpp index a345ed6e12..b70d25dd86 100644 --- a/src/mbgl/renderer/renderer.cpp +++ b/src/mbgl/renderer/renderer.cpp @@ -7,7 +7,7 @@ namespace mbgl { -Renderer::Renderer(RendererBackend& backend, +Renderer::Renderer(gfx::RendererBackend& backend, float pixelRatio_, Scheduler& scheduler_, GLContextMode contextMode_, diff --git a/src/mbgl/renderer/renderer_backend.cpp b/src/mbgl/renderer/renderer_backend.cpp deleted file mode 100644 index 24249e06a4..0000000000 --- a/src/mbgl/renderer/renderer_backend.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include <mbgl/renderer/renderer_backend.hpp> -#include <mbgl/gfx/backend_scope.hpp> -#include <mbgl/gl/context.hpp> -#include <mbgl/gl/extension.hpp> - -#include <cassert> - -namespace mbgl { - -RendererBackend::RendererBackend() = default; - -gl::Context& RendererBackend::getContext() { - assert(gfx::BackendScope::exists()); - std::call_once(initialized, [this] { - context = std::make_unique<gl::Context>(); - context->enableDebugging(); - context->initializeExtensions( - std::bind(&RendererBackend::getExtensionFunctionPointer, this, std::placeholders::_1)); - }); - return *context; -} - -PremultipliedImage RendererBackend::readFramebuffer(const Size& size) const { - assert(context); - return context->readFramebuffer<PremultipliedImage>(size); -} - -void RendererBackend::assumeFramebufferBinding(const gl::FramebufferID fbo) { - getContext().bindFramebuffer.setCurrentValue(fbo); - if (fbo != ImplicitFramebufferBinding) { - assert(gl::value::BindFramebuffer::Get() == getContext().bindFramebuffer.getCurrentValue()); - } -} - -void RendererBackend::assumeViewport(int32_t x, int32_t y, const Size& size) { - getContext().viewport.setCurrentValue({ x, y, size }); - assert(gl::value::Viewport::Get() == getContext().viewport.getCurrentValue()); -} - -void RendererBackend::assumeScissorTest(bool enabled) { - getContext().scissorTest.setCurrentValue(enabled); - assert(gl::value::ScissorTest::Get() == getContext().scissorTest.getCurrentValue()); -} - -bool RendererBackend::implicitFramebufferBound() { - return getContext().bindFramebuffer.getCurrentValue() == ImplicitFramebufferBinding; -} - -void RendererBackend::setFramebufferBinding(const gl::FramebufferID fbo) { - getContext().bindFramebuffer = fbo; - if (fbo != ImplicitFramebufferBinding) { - assert(gl::value::BindFramebuffer::Get() == getContext().bindFramebuffer.getCurrentValue()); - } -} - -void RendererBackend::setViewport(int32_t x, int32_t y, const Size& size) { - getContext().viewport = { x, y, size }; - assert(gl::value::Viewport::Get() == getContext().viewport.getCurrentValue()); -} - -void RendererBackend::setScissorTest(bool enabled) { - getContext().scissorTest = enabled; - assert(gl::value::ScissorTest::Get() == getContext().scissorTest.getCurrentValue()); -} - -RendererBackend::~RendererBackend() = default; - -} // namespace mbgl diff --git a/src/mbgl/renderer/renderer_impl.cpp b/src/mbgl/renderer/renderer_impl.cpp index 3a85928cc3..f5f87fc7fd 100644 --- a/src/mbgl/renderer/renderer_impl.cpp +++ b/src/mbgl/renderer/renderer_impl.cpp @@ -1,7 +1,6 @@ #include <mbgl/annotation/annotation_manager.hpp> #include <mbgl/layermanager/layer_manager.hpp> #include <mbgl/renderer/renderer_impl.hpp> -#include <mbgl/renderer/renderer_backend.hpp> #include <mbgl/renderer/renderer_observer.hpp> #include <mbgl/renderer/render_source.hpp> #include <mbgl/renderer/render_layer.hpp> @@ -16,7 +15,10 @@ #include <mbgl/renderer/query.hpp> #include <mbgl/gfx/backend_scope.hpp> #include <mbgl/renderer/image_manager.hpp> +#include <mbgl/gfx/renderer_backend.hpp> +#include <mbgl/gfx/cull_face_mode.hpp> #include <mbgl/gl/context.hpp> +#include <mbgl/gl/renderable_resource.hpp> #include <mbgl/geometry/line_atlas.hpp> #include <mbgl/style/source_impl.hpp> #include <mbgl/style/transition_options.hpp> @@ -35,7 +37,7 @@ static RendererObserver& nullObserver() { return observer; } -Renderer::Impl::Impl(RendererBackend& backend_, +Renderer::Impl::Impl(gfx::RendererBackend& backend_, float pixelRatio_, Scheduler& scheduler_, GLContextMode contextMode_, @@ -307,8 +309,6 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) { observer->onWillStartRenderingFrame(); - backend.updateAssumedState(); - // Set render tiles to the render items. for (auto& renderItem : renderItems) { if (!renderItem.source) { @@ -364,7 +364,6 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) { } } - // TODO: remove cast gl::Context& glContext = static_cast<gl::Context&>(parameters.context); @@ -392,7 +391,7 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) { // Renders any 3D layers bottom-to-top to unique FBOs with texture attachments, but share the same // depth rbo between them. if (parameters.staticData.has3D) { - parameters.staticData.backendSize = parameters.backend.getFramebufferSize(); + parameters.staticData.backendSize = parameters.backend.getDefaultRenderable().getSize(); const auto debugGroup(parameters.encoder->createDebugGroup("3d")); parameters.pass = RenderPass::Pass3D; @@ -420,7 +419,7 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) { { using namespace gl::value; - parameters.backend.bind(); + parameters.backend.getDefaultRenderable().getResource<gl::RenderableResource>().bind(); if (parameters.debugOptions & MapDebugOptions::Overdraw) { glContext.clear(Color::black(), ClearDepth::Default, ClearStencil::Default); } else if (parameters.contextMode == GLContextMode::Shared) { @@ -600,7 +599,8 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) { void Renderer::Impl::flush() { assert(gfx::BackendScope::exists()); - backend.getContext().flush(); + gl::Context& glContext = static_cast<gl::Context&>(backend.getContext()); + glContext.flush(); } std::vector<Feature> Renderer::Impl::queryRenderedFeatures(const ScreenLineString& geometry, const RenderedQueryOptions& options) const { diff --git a/src/mbgl/renderer/renderer_impl.hpp b/src/mbgl/renderer/renderer_impl.hpp index 624b4c273c..c8ee939e7a 100644 --- a/src/mbgl/renderer/renderer_impl.hpp +++ b/src/mbgl/renderer/renderer_impl.hpp @@ -20,7 +20,6 @@ namespace mbgl { -class RendererBackend; class RendererObserver; class RenderSource; class RenderLayer; @@ -34,11 +33,15 @@ class ImageManager; class LineAtlas; class CrossTileSymbolIndex; +namespace gfx { +class RendererBackend; +} // namespace gfx + class Renderer::Impl : public GlyphManagerObserver, public ImageManagerObserver, public RenderSourceObserver{ public: - Impl(RendererBackend&, float pixelRatio_, Scheduler&, GLContextMode, + Impl(gfx::RendererBackend&, float pixelRatio_, Scheduler&, GLContextMode, const optional<std::string> programCacheDir, const optional<std::string> localFontFamily_); ~Impl() final; @@ -95,7 +98,7 @@ private: friend class Renderer; - RendererBackend& backend; + gfx::RendererBackend& backend; Scheduler& scheduler; RendererObserver* observer; |