[core] fold HeadlessDisplay into the headless RenderBackend implementation
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#include <mbgl/gl/headless_backend.hpp>
|
||||
#include <mbgl/gl/headless_display.hpp>
|
||||
|
||||
#include <mbgl/util/logging.hpp>
|
||||
|
||||
@@ -9,11 +8,82 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
// This class provides a singleton that contains information about the configuration used for
|
||||
// instantiating new headless rendering contexts.
|
||||
class EGLDisplayConfig {
|
||||
private:
|
||||
// Key for singleton construction.
|
||||
struct Key { explicit Key() = default; };
|
||||
|
||||
public:
|
||||
EGLDisplayConfig(Key) {
|
||||
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
if (display == EGL_NO_DISPLAY) {
|
||||
throw std::runtime_error("Failed to obtain a valid EGL display.\n");
|
||||
}
|
||||
|
||||
EGLint major, minor, numConfigs;
|
||||
if (!eglInitialize(display, &major, &minor)) {
|
||||
throw std::runtime_error("eglInitialize() failed.\n");
|
||||
}
|
||||
|
||||
if (!eglBindAPI(EGL_OPENGL_ES_API)) {
|
||||
mbgl::Log::Error(mbgl::Event::OpenGL, "eglBindAPI(EGL_OPENGL_ES_API) returned error %d",
|
||||
eglGetError());
|
||||
throw std::runtime_error("eglBindAPI() failed");
|
||||
}
|
||||
|
||||
const EGLint attribs[] = {
|
||||
#if MBGL_USE_GLES2
|
||||
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
||||
#endif
|
||||
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
|
||||
EGL_NONE
|
||||
};
|
||||
|
||||
// Note: we're choosing an arbitrary pixel format, since we're not using the default surface
|
||||
// anyway; all rendering will be directed to framebuffers which have their own configuration.
|
||||
if (!eglChooseConfig(display, attribs, &config, 1, &numConfigs) || numConfigs != 1) {
|
||||
throw std::runtime_error("Failed to choose ARGB config.\n");
|
||||
}
|
||||
}
|
||||
|
||||
~EGLDisplayConfig() {
|
||||
eglTerminate(display);
|
||||
}
|
||||
|
||||
static std::shared_ptr<const EGLDisplayConfig> create() {
|
||||
static std::weak_ptr<const EGLDisplayConfig> instance;
|
||||
auto shared = instance.lock();
|
||||
if (!shared) {
|
||||
instance = shared = std::make_shared<EGLDisplayConfig>(Key{});
|
||||
}
|
||||
return shared;
|
||||
}
|
||||
|
||||
public:
|
||||
EGLDisplay display = EGL_NO_DISPLAY;
|
||||
EGLConfig config = 0;
|
||||
};
|
||||
|
||||
struct EGLImpl : public HeadlessBackend::Impl {
|
||||
EGLImpl(EGLContext glContext_, EGLDisplay display_, EGLConfig config_)
|
||||
: glContext(glContext_),
|
||||
display(display_),
|
||||
config(config_) {
|
||||
EGLImpl() {
|
||||
// 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 = eglCreateContext(eglDisplay->display, eglDisplay->config, EGL_NO_CONTEXT, attribs);
|
||||
if (eglContext == 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");
|
||||
}
|
||||
|
||||
// Create a dummy pbuffer. We will render to framebuffers anyway, but we need a pbuffer to
|
||||
// activate the context.
|
||||
// Note that to be able to create pbuffer surfaces, we need to choose a config that
|
||||
@@ -25,77 +95,49 @@ struct EGLImpl : public HeadlessBackend::Impl {
|
||||
EGL_NONE
|
||||
};
|
||||
|
||||
glSurface = eglCreatePbufferSurface(display, config, surfAttribs);
|
||||
if (glSurface == EGL_NO_SURFACE) {
|
||||
eglSurface = eglCreatePbufferSurface(eglDisplay->display, eglDisplay->config, surfAttribs);
|
||||
if (eglSurface == EGL_NO_SURFACE) {
|
||||
throw std::runtime_error("Could not create surface: " + std::to_string(eglGetError()));
|
||||
}
|
||||
}
|
||||
|
||||
~EGLImpl() {
|
||||
if (glSurface != EGL_NO_SURFACE) {
|
||||
if (!eglDestroySurface(display, glSurface)) {
|
||||
throw std::runtime_error("Failed to destroy EGL surface.\n");
|
||||
~EGLImpl() final {
|
||||
if (eglSurface != EGL_NO_SURFACE) {
|
||||
if (!eglDestroySurface(eglDisplay->display, eglSurface)) {
|
||||
Log::Error(Event::OpenGL, "Failed to destroy EGL surface.");
|
||||
}
|
||||
glSurface = EGL_NO_SURFACE;
|
||||
eglSurface = EGL_NO_SURFACE;
|
||||
}
|
||||
if (!eglDestroyContext(display, glContext)) {
|
||||
throw std::runtime_error("Failed to destroy EGL context.\n");
|
||||
if (!eglDestroyContext(eglDisplay->display, eglContext)) {
|
||||
Log::Error(Event::OpenGL, "Failed to destroy EGL context.");
|
||||
}
|
||||
}
|
||||
|
||||
void activateContext() final {
|
||||
if (!eglMakeCurrent(display, glSurface, glSurface, glContext)) {
|
||||
if (!eglMakeCurrent(eglDisplay->display, eglSurface, eglSurface, eglContext)) {
|
||||
throw std::runtime_error("Switching OpenGL context failed.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void deactivateContext() final {
|
||||
if (!eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
|
||||
if (!eglMakeCurrent(eglDisplay->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;
|
||||
private:
|
||||
const std::shared_ptr<const EGLDisplayConfig> eglDisplay = EGLDisplayConfig::create();
|
||||
EGLContext eglContext = EGL_NO_CONTEXT;
|
||||
EGLSurface eglSurface = EGL_NO_SURFACE;
|
||||
};
|
||||
|
||||
gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) {
|
||||
return eglGetProcAddress(name);
|
||||
}
|
||||
|
||||
bool HeadlessBackend::hasDisplay() {
|
||||
if (!display) {
|
||||
display = HeadlessDisplay::create();
|
||||
}
|
||||
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));
|
||||
impl = std::make_unique<EGLImpl>();
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
Reference in New Issue
Block a user