summaryrefslogtreecommitdiff
path: root/platform/darwin/src/headless_backend_eagl.mm
blob: 1daaeaf54cb6a84ba98688066e23b2b3d7fb4579 (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
#include <mbgl/gl/headless_backend.hpp>

#include <OpenGLES/EAGL.h>

#include <stdexcept>

namespace mbgl {

struct EAGLImpl : public HeadlessBackend::Impl {
    EAGLImpl(EAGLContext* glContext_) : glContext(glContext_) {
        [reinterpret_cast<EAGLContext*>(glContext) retain];
        reinterpret_cast<EAGLContext*>(glContext).multiThreaded = YES;
    }

    ~EAGLImpl() {
        [glContext release];
    }

    void activateContext() {
        [EAGLContext setCurrentContext:glContext];
    }

    void deactivateContext() {
        [EAGLContext setCurrentContext:nil];
    }

    EAGLContext* glContext = nullptr;
};

gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) {
    static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengles"));
    if (!framework) {
        throw std::runtime_error("Failed to load OpenGL framework.");
    }

    CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingASCII);
    void* symbol = CFBundleGetFunctionPointerForName(framework, str);
    CFRelease(str);

    return reinterpret_cast<gl::ProcAddress>(symbol);
}

bool HeadlessBackend::hasDisplay() {
    return true;
}

void HeadlessBackend::createContext() {
    EAGLContext* glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    if (glContext == nil) {
        throw std::runtime_error("Error creating GL context object");
    }

    impl.reset(new EAGLImpl(glContext));
}

} // namespace mbgl