diff options
Diffstat (limited to 'platform/glfw')
-rw-r--r-- | platform/glfw/glfw_backend.hpp | 21 | ||||
-rw-r--r-- | platform/glfw/glfw_gl_backend.cpp | 58 | ||||
-rw-r--r-- | platform/glfw/glfw_gl_backend.hpp | 42 | ||||
-rw-r--r-- | platform/glfw/glfw_renderer_frontend.cpp | 2 | ||||
-rw-r--r-- | platform/glfw/glfw_view.cpp | 41 | ||||
-rw-r--r-- | platform/glfw/glfw_view.hpp | 23 | ||||
-rw-r--r-- | platform/glfw/main.cpp | 4 |
7 files changed, 144 insertions, 47 deletions
diff --git a/platform/glfw/glfw_backend.hpp b/platform/glfw/glfw_backend.hpp new file mode 100644 index 0000000000..1a2c89ac7a --- /dev/null +++ b/platform/glfw/glfw_backend.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include <mbgl/util/size.hpp> + +namespace mbgl { +namespace gfx { +class RendererBackend; +} // namespace gfx +} // namespace mbgl + +class GLFWBackend { +public: + explicit GLFWBackend() = default; + GLFWBackend(const GLFWBackend&) = delete; + GLFWBackend& operator=(const GLFWBackend&) = delete; + virtual ~GLFWBackend() = default; + + virtual mbgl::gfx::RendererBackend& getRendererBackend() = 0; + virtual mbgl::Size getSize() const = 0; + virtual void setSize(mbgl::Size) = 0; +}; diff --git a/platform/glfw/glfw_gl_backend.cpp b/platform/glfw/glfw_gl_backend.cpp new file mode 100644 index 0000000000..b61e2775d5 --- /dev/null +++ b/platform/glfw/glfw_gl_backend.cpp @@ -0,0 +1,58 @@ +#include "glfw_gl_backend.hpp" + +#include <mbgl/gfx/backend_scope.hpp> +#include <mbgl/gl/renderable_resource.hpp> + +#include <GLFW/glfw3.h> + +class GLFWGLRenderableResource final : public mbgl::gl::RenderableResource { +public: + GLFWGLRenderableResource(GLFWGLBackend& backend_) : backend(backend_) { + } + + void bind() override { + backend.setFramebufferBinding(0); + backend.setViewport(0, 0, backend.getSize()); + } + +private: + GLFWGLBackend& backend; +}; + +GLFWGLBackend::GLFWGLBackend(GLFWwindow* window_) + : mbgl::gfx::Renderable( + [window_] { + int fbWidth, fbHeight; + glfwGetFramebufferSize(window_, &fbWidth, &fbHeight); + return mbgl::Size{ static_cast<uint32_t>(fbWidth), static_cast<uint32_t>(fbHeight) }; + }(), + std::make_unique<GLFWGLRenderableResource>(*this)), + window(window_) { +} + +GLFWGLBackend::~GLFWGLBackend() = default; + +void GLFWGLBackend::activate() { + glfwMakeContextCurrent(window); +} + +void GLFWGLBackend::deactivate() { + glfwMakeContextCurrent(nullptr); +} + +mbgl::gl::ProcAddress GLFWGLBackend::getExtensionFunctionPointer(const char* name) { + return glfwGetProcAddress(name); +} + +void GLFWGLBackend::updateAssumedState() { + assumeFramebufferBinding(0); + assumeViewport(0, 0, size); +} + +mbgl::Size GLFWGLBackend::getSize() const { + return size; +} + +void GLFWGLBackend::setSize(const mbgl::Size newSize) { + size = newSize; +} diff --git a/platform/glfw/glfw_gl_backend.hpp b/platform/glfw/glfw_gl_backend.hpp new file mode 100644 index 0000000000..ba4f6fa788 --- /dev/null +++ b/platform/glfw/glfw_gl_backend.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include "glfw_backend.hpp" + +#include <mbgl/gfx/renderable.hpp> +#include <mbgl/gl/renderer_backend.hpp> + +struct GLFWwindow; + +class GLFWGLBackend final : public GLFWBackend, + public mbgl::gl::RendererBackend, + public mbgl::gfx::Renderable { +public: + GLFWGLBackend(GLFWwindow*); + ~GLFWGLBackend() override; + + // GLFWRendererBackend implementation +public: + mbgl::gfx::RendererBackend& getRendererBackend() override { + return *this; + } + mbgl::Size getSize() const override; + void setSize(mbgl::Size) override; + + // mbgl::gfx::RendererBackend implementation +public: + mbgl::gfx::Renderable& getDefaultRenderable() override { + return *this; + } + +protected: + void activate() override; + void deactivate() override; + + // mbgl::gl::RendererBackend implementation +protected: + mbgl::gl::ProcAddress getExtensionFunctionPointer(const char*) override; + void updateAssumedState() override; + +private: + GLFWwindow* window; +}; diff --git a/platform/glfw/glfw_renderer_frontend.cpp b/platform/glfw/glfw_renderer_frontend.cpp index 82efbcdfb1..b8478a49f8 100644 --- a/platform/glfw/glfw_renderer_frontend.cpp +++ b/platform/glfw/glfw_renderer_frontend.cpp @@ -31,7 +31,7 @@ void GLFWRendererFrontend::render() { if (!updateParameters) return; - mbgl::gfx::BackendScope guard { glfwView, mbgl::gfx::BackendScope::ScopeType::Implicit }; + mbgl::gfx::BackendScope guard { glfwView.getRendererBackend(), mbgl::gfx::BackendScope::ScopeType::Implicit }; // onStyleImageMissing might be called during a render. The user implemented method // could trigger a call to MGLRenderFrontend#update which overwrites `updateParameters`. diff --git a/platform/glfw/glfw_view.cpp b/platform/glfw/glfw_view.cpp index 483ca14559..2772ed0773 100644 --- a/platform/glfw/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -1,4 +1,5 @@ #include "glfw_view.hpp" +#include "glfw_gl_backend.hpp" #include "glfw_renderer_frontend.hpp" #include "ny_route.hpp" @@ -105,8 +106,10 @@ GLFWView::GLFWView(bool fullscreen_, bool benchmark_) glfwSetKeyCallback(window, onKey); glfwGetWindowSize(window, &width, &height); - glfwGetFramebufferSize(window, &fbWidth, &fbHeight); - pixelRatio = static_cast<float>(fbWidth) / width; + + backend = std::make_unique<GLFWGLBackend>(window); + + pixelRatio = static_cast<float>(backend->getSize().width) / width; glfwMakeContextCurrent(nullptr); @@ -158,14 +161,8 @@ void GLFWView::setRenderFrontend(GLFWRendererFrontend* rendererFrontend_) { rendererFrontend = rendererFrontend_; } -void GLFWView::updateAssumedState() { - assumeFramebufferBinding(0); - assumeViewport(0, 0, getFramebufferSize()); -} - -void GLFWView::bind() { - setFramebufferBinding(0); - setViewport(0, 0, getFramebufferSize()); +mbgl::gfx::RendererBackend& GLFWView::getRendererBackend() { + return backend->getRendererBackend(); } void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, int mods) { @@ -499,11 +496,7 @@ void GLFWView::onWindowResize(GLFWwindow *window, int width, int height) { void GLFWView::onFramebufferResize(GLFWwindow *window, int width, int height) { auto *view = reinterpret_cast<GLFWView *>(glfwGetWindowUserPointer(window)); - view->fbWidth = width; - view->fbHeight = height; - - mbgl::gfx::BackendScope scope { *view, mbgl::gfx::BackendScope::ScopeType::Implicit }; - view->bind(); + view->backend->setSize({ static_cast<uint32_t>(width), static_cast<uint32_t>(height) }); // This is only triggered when the framebuffer is resized, but not the window. It can // happen when you move the window between screens with a different pixel ratio. @@ -578,7 +571,7 @@ void GLFWView::run() { updateAnimatedAnnotations(); - activate(); + mbgl::gfx::BackendScope scope { backend->getRendererBackend() }; rendererFrontend->render(); @@ -608,22 +601,6 @@ mbgl::Size GLFWView::getSize() const { return { static_cast<uint32_t>(width), static_cast<uint32_t>(height) }; } -mbgl::Size GLFWView::getFramebufferSize() const { - return { static_cast<uint32_t>(fbWidth), static_cast<uint32_t>(fbHeight) }; -} - -mbgl::gl::ProcAddress GLFWView::getExtensionFunctionPointer(const char* name) { - return glfwGetProcAddress(name); -} - -void GLFWView::activate() { - glfwMakeContextCurrent(window); -} - -void GLFWView::deactivate() { - glfwMakeContextCurrent(nullptr); -} - void GLFWView::invalidate() { dirty = true; glfwPostEmptyEvent(); diff --git a/platform/glfw/glfw_view.hpp b/platform/glfw/glfw_view.hpp index d5acf697f7..9b580f80cc 100644 --- a/platform/glfw/glfw_view.hpp +++ b/platform/glfw/glfw_view.hpp @@ -1,15 +1,21 @@ #pragma once #include <mbgl/map/map.hpp> -#include <mbgl/renderer/renderer_backend.hpp> #include <mbgl/util/run_loop.hpp> #include <mbgl/util/timer.hpp> #include <mbgl/util/geometry.hpp> struct GLFWwindow; +class GLFWBackend; class GLFWRendererFrontend; -class GLFWView : public mbgl::RendererBackend, public mbgl::MapObserver { +namespace mbgl { +namespace gfx { +class RendererBackend; +} // namespace gfx +} // namespace mbgl + +class GLFWView : public mbgl::MapObserver { public: GLFWView(bool fullscreen = false, bool benchmark = false); ~GLFWView() override; @@ -20,6 +26,8 @@ public: void setRenderFrontend(GLFWRendererFrontend*); + mbgl::gfx::RendererBackend& getRendererBackend(); + // Callback called when the user presses the key mapped to style change. // The expected action is to set a new style, different to the current one. void setChangeStyleCallback(std::function<void()> callback); @@ -41,20 +49,12 @@ public: void invalidate(); mbgl::Size getSize() const; - mbgl::Size getFramebufferSize() const override; - - // mbgl::RendererBackend implementation - void bind() override; - void updateAssumedState() override; // mbgl::MapObserver implementation void onDidFinishLoadingStyle() override; protected: // mbgl::Backend implementation - mbgl::gl::ProcAddress getExtensionFunctionPointer(const char*) override; - void activate() override; - void deactivate() override; private: // Window callbacks @@ -95,6 +95,7 @@ private: mbgl::Map* map = nullptr; GLFWRendererFrontend* rendererFrontend = nullptr; + std::unique_ptr<GLFWBackend> backend; bool fullscreen = false; const bool benchmark = false; @@ -110,8 +111,6 @@ private: int width = 1024; int height = 768; - int fbWidth; - int fbHeight; float pixelRatio; double lastX = 0, lastY = 0; diff --git a/platform/glfw/main.cpp b/platform/glfw/main.cpp index fb7c2b4ffb..0a4ace6a19 100644 --- a/platform/glfw/main.cpp +++ b/platform/glfw/main.cpp @@ -109,9 +109,9 @@ int main(int argc, char *argv[]) { } mbgl::ThreadPool threadPool(4); - GLFWRendererFrontend rendererFrontend { std::make_unique<mbgl::Renderer>(backend, view->getPixelRatio(), threadPool), backend }; + GLFWRendererFrontend rendererFrontend { std::make_unique<mbgl::Renderer>(view->getRendererBackend(), view->getPixelRatio(), threadPool), *view }; - mbgl::Map map(rendererFrontend, backend, threadPool, + mbgl::Map map(rendererFrontend, *view, threadPool, mbgl::MapOptions().withSize(view->getSize()).withPixelRatio(view->getPixelRatio()), resourceOptions); backend.setMap(&map); |