summaryrefslogtreecommitdiff
path: root/platform/linux/src/headless_backend_glx.cpp
blob: 5791f1892f6ca9bc2481f6c25e6bbb2671e1f6cd (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
87
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/headless_display.hpp>

#include <mbgl/util/logging.hpp>

#include <cassert>

#include <GL/glx.h>

namespace mbgl {

struct GLXImpl : public HeadlessBackend::Impl {
    GLXImpl(GLXContext glContext_, GLXPbuffer glxPbuffer_, Display* xDisplay_, GLXFBConfig* fbConfigs_)
            : glContext(glContext_),
              glxPbuffer(glxPbuffer_),
              xDisplay(xDisplay_),
              fbConfigs(fbConfigs_) {
    }

    ~GLXImpl() {
        if (glxPbuffer) {
            glXDestroyPbuffer(xDisplay, glxPbuffer);
        }
        glXDestroyContext(xDisplay, glContext);
    }

    void activateContext() final {
        if (!glXMakeContextCurrent(xDisplay, glxPbuffer, glxPbuffer, glContext)) {
            throw std::runtime_error("Switching OpenGL context failed.\n");
        }
    }

    void deactivateContext() final {
        if (!glXMakeContextCurrent(xDisplay, 0, 0, nullptr)) {
            throw std::runtime_error("Removing OpenGL context failed.\n");
        }
    }

    GLXContext glContext = nullptr;
    GLXPbuffer glxPbuffer = 0;

    // Needed for ImplDeleter.
    Display* xDisplay = nullptr;
    GLXFBConfig* fbConfigs = nullptr;
};

gl::glProc HeadlessBackend::initializeExtension(const char* name) {
    return glXGetProcAddress(reinterpret_cast<const GLubyte*>(name));
}

bool HeadlessBackend::hasDisplay() {
    if (!display) {
        display.reset(new HeadlessDisplay);
    }
    return bool(display);
};

void HeadlessBackend::createContext() {
    assert(!hasContext());

    Display* xDisplay = display->attribute<Display*>();
    GLXFBConfig* fbConfigs = display->attribute<GLXFBConfig*>();

    // Try to create a legacy context.
    GLXContext glContext = glXCreateNewContext(xDisplay, fbConfigs[0], GLX_RGBA_TYPE, None, True);
    if (glContext && !glXIsDirect(xDisplay, glContext)) {
        Log::Error(Event::OpenGL, "failed to create direct OpenGL Legacy context");
        glXDestroyContext(xDisplay, glContext);
        glContext = nullptr;
    }
    if (glContext == nullptr) {
        throw std::runtime_error("Error creating GL context object.");
    }

    // Create a dummy pbuffer. We will render to framebuffers anyway, but we need a pbuffer to
    // activate the context.
    int pbufferAttributes[] = {
        GLX_PBUFFER_WIDTH, 8,
        GLX_PBUFFER_HEIGHT, 8,
        None
    };
    GLXPbuffer glxPbuffer = glXCreatePbuffer(xDisplay, fbConfigs[0], pbufferAttributes);

    impl.reset(new GLXImpl(glContext, glxPbuffer, xDisplay, fbConfigs));
}

} // namespace mbgl