summaryrefslogtreecommitdiff
path: root/platform/glfw/glfw_gl_backend.cpp
blob: 0a949016ed23b73dccd154638a3cb2a2dfeb56ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#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());
    }

    void swap() override {
        backend.swap();
    }

private:
    GLFWGLBackend& backend;
};

GLFWGLBackend::GLFWGLBackend(GLFWwindow* window_, const bool capFrameRate)
    : mbgl::gl::RendererBackend(mbgl::gfx::ContextMode::Unique),
      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_) {
    glfwMakeContextCurrent(window);
    if (!capFrameRate) {
        // Disables vsync on platforms that support it.
        glfwSwapInterval(0);
    } else {
        glfwSwapInterval(1);
    }
}

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);
    setViewport(0, 0, size);
}

mbgl::Size GLFWGLBackend::getSize() const {
    return size;
}

void GLFWGLBackend::setSize(const mbgl::Size newSize) {
    size = newSize;
}

void GLFWGLBackend::swap() {
    glfwSwapBuffers(window);
}

namespace mbgl {
namespace gfx {

template <>
std::unique_ptr<GLFWBackend>
Backend::Create<mbgl::gfx::Backend::Type::OpenGL>(GLFWwindow* window, bool capFrameRate) {
    return std::make_unique<GLFWGLBackend>(window, capFrameRate);
}

} // namespace gfx
} // namespace mbgl