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
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/headless_display.hpp>
#include <mbgl/util/logging.hpp>
#include <EGL/egl.h>
#include <cassert>
namespace mbgl {
struct EGLImpl : public HeadlessBackend::Impl {
EGLImpl(EGLContext glContext_, EGLDisplay display_, EGLConfig config_)
: glContext(glContext_),
display(display_),
config(config_) {
#if __ANDROID__
// Create a pixel buffer surface (in conjunction with EGL_SURFACE_TYPE, EGL_PBUFFER_BIT).
const EGLint surfAttribs[] = {
EGL_WIDTH, 512,
EGL_HEIGHT, 512,
EGL_LARGEST_PBUFFER, EGL_TRUE,
EGL_NONE
};
glSurface = eglCreatePbufferSurface(display, config, surfAttribs);
if (glSurface == EGL_NO_SURFACE) {
throw std::runtime_error("Could not create surface: " + std::to_string(eglGetError()));
}
#endif // __ANDROID__
}
~EGLImpl() {
if (!eglDestroyContext(display, glContext)) {
throw std::runtime_error("Failed to destroy EGL context.\n");
}
if (glSurface != EGL_NO_SURFACE) {
if (!eglDestroySurface(display, glSurface)) {
throw std::runtime_error("Failed to destroy EGL context.\n");
}
glSurface = EGL_NO_SURFACE;
}
}
void activateContext() final {
if (!eglMakeCurrent(display, glSurface, glSurface, glContext)) {
throw std::runtime_error("Switching OpenGL context failed.\n");
}
}
void deactivateContext() final {
if (!eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
throw std::runtime_error("Removing OpenGL context failed.\n");
}
}
EGLContext glContext = EGL_NO_CONTEXT;
EGLDisplay display = EGL_NO_DISPLAY;
EGLConfig config = 0;
EGLSurface glSurface = EGL_NO_SURFACE;
};
gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) {
return eglGetProcAddress(name);
}
bool HeadlessBackend::hasDisplay() {
if (!display) {
display.reset(new HeadlessDisplay);
}
return bool(display);
};
void HeadlessBackend::createContext() {
assert(!hasContext());
assert(hasDisplay());
EGLDisplay display_ = display->attribute<EGLDisplay>();
EGLConfig& config = display->attribute<EGLConfig&>();
// EGL initializes the context client version to 1 by default. We want to
// use OpenGL ES 2.0 which has the ability to create shader and program
// objects and also to write vertex and fragment shaders in the OpenGL ES
// Shading Language.
const EGLint attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLContext glContext = eglCreateContext(display_, config, EGL_NO_CONTEXT, attribs);
if (glContext == EGL_NO_CONTEXT) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglCreateContext() returned error 0x%04x",
eglGetError());
throw std::runtime_error("Error creating the EGL context object.\n");
}
impl.reset(new EGLImpl(glContext, display_, config));
}
} // namespace mbgl
|