[Qt] Implement a Qt headless renderer
Using legacy QGLWidget because we support Qt4.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __APPLE__
|
||||
#if defined(__QT__)
|
||||
#define MBGL_USE_QT 1
|
||||
class QGLWidget;
|
||||
#elif defined(__APPLE__)
|
||||
#include <TargetConditionals.h>
|
||||
#if TARGET_OS_IOS
|
||||
#define MBGL_USE_EAGL 1
|
||||
@@ -63,6 +66,10 @@ private:
|
||||
bool extensionsLoaded = false;
|
||||
bool active = false;
|
||||
|
||||
#if MBGL_USE_QT
|
||||
QGLWidget* glContext = nullptr;
|
||||
#endif
|
||||
|
||||
#if MBGL_USE_CGL
|
||||
CGLContextObj glContext = nullptr;
|
||||
#endif
|
||||
|
||||
@@ -40,15 +40,20 @@ macro(mbgl_platform_core)
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
# TODO: Implement a Qt headless view, using offscreen
|
||||
# graphics system.
|
||||
macro(mbgl_platform_test)
|
||||
target_sources(mbgl-test
|
||||
PRIVATE test/src/main.cpp
|
||||
PRIVATE platform/qt/test/headless_view_qt.cpp
|
||||
PRIVATE platform/default/headless_display.cpp
|
||||
PRIVATE platform/default/headless_view.cpp
|
||||
)
|
||||
|
||||
set_source_files_properties(
|
||||
test/src/main.cpp
|
||||
PROPERTIES COMPILE_FLAGS -DWORK_DIRECTORY="${CMAKE_SOURCE_DIR}"
|
||||
)
|
||||
|
||||
target_link_libraries(mbgl-test
|
||||
${MBGL_QT_LIBRARIES}
|
||||
)
|
||||
endmacro()
|
||||
|
||||
@@ -29,10 +29,6 @@ set(MBGL_QT_FILES
|
||||
# Misc
|
||||
PRIVATE platform/default/log_stderr.cpp
|
||||
|
||||
# Headless headless_view_cgl
|
||||
PRIVATE platform/default/headless_display.cpp
|
||||
PRIVATE platform/default/headless_view.cpp
|
||||
|
||||
# Platform integration
|
||||
PRIVATE platform/qt/src/async_task.cpp
|
||||
PRIVATE platform/qt/src/async_task_impl.hpp
|
||||
@@ -77,7 +73,6 @@ endif()
|
||||
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
|
||||
list(APPEND MBGL_QT_FILES
|
||||
PRIVATE platform/darwin/src/nsthread.mm
|
||||
PRIVATE platform/darwin/src/headless_view_cgl.cpp
|
||||
)
|
||||
list(APPEND MBGL_QT_LIBRARIES
|
||||
PRIVATE "-framework Foundation"
|
||||
@@ -86,10 +81,8 @@ if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
|
||||
else()
|
||||
list(APPEND MBGL_QT_FILES
|
||||
PRIVATE platform/default/thread.cpp
|
||||
PRIVATE platform/default/headless_view_glx.cpp
|
||||
)
|
||||
list(APPEND MBGL_QT_LIBRARIES
|
||||
PRIVATE -lGL
|
||||
PRIVATE -lX11
|
||||
)
|
||||
endif()
|
||||
|
||||
106
platform/qt/test/headless_view_qt.cpp
Normal file
106
platform/qt/test/headless_view_qt.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <mbgl/platform/default/headless_display.hpp>
|
||||
#include <mbgl/platform/default/headless_view.hpp>
|
||||
|
||||
#include <QApplication>
|
||||
#include <QGLContext>
|
||||
#include <QGLWidget>
|
||||
|
||||
#if QT_VERSION >= 0x050000
|
||||
#include <QOpenGLContext>
|
||||
#endif
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
gl::glProc HeadlessView::initializeExtension(const char* name) {
|
||||
#if QT_VERSION >= 0x050000
|
||||
QOpenGLContext* thisContext = QOpenGLContext::currentContext();
|
||||
return thisContext->getProcAddress(name);
|
||||
#else
|
||||
const QGLContext* thisContext = QGLContext::currentContext();
|
||||
return reinterpret_cast<mbgl::gl::glProc>(thisContext->getProcAddress(name));
|
||||
#endif
|
||||
}
|
||||
|
||||
void HeadlessView::createContext() {
|
||||
static const char* argv[] = { "mbgl" };
|
||||
static int argc = 1;
|
||||
static auto* app = new QApplication(argc, const_cast<char**>(argv));
|
||||
|
||||
Q_UNUSED(app);
|
||||
|
||||
glContext = new QGLWidget;
|
||||
}
|
||||
|
||||
void HeadlessView::destroyContext() {
|
||||
delete glContext;
|
||||
}
|
||||
|
||||
void HeadlessView::resizeFramebuffer() {
|
||||
const unsigned int w = dimensions[0] * pixelRatio;
|
||||
const unsigned int h = dimensions[1] * pixelRatio;
|
||||
|
||||
// Create depth/stencil buffer
|
||||
MBGL_CHECK_ERROR(glGenRenderbuffersEXT(1, &fboDepthStencil));
|
||||
MBGL_CHECK_ERROR(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fboDepthStencil));
|
||||
MBGL_CHECK_ERROR(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT, w, h));
|
||||
MBGL_CHECK_ERROR(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0));
|
||||
|
||||
MBGL_CHECK_ERROR(glGenRenderbuffersEXT(1, &fboColor));
|
||||
MBGL_CHECK_ERROR(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fboColor));
|
||||
MBGL_CHECK_ERROR(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, w, h));
|
||||
MBGL_CHECK_ERROR(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0));
|
||||
|
||||
MBGL_CHECK_ERROR(glGenFramebuffersEXT(1, &fbo));
|
||||
MBGL_CHECK_ERROR(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo));
|
||||
|
||||
MBGL_CHECK_ERROR(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, fboColor));
|
||||
MBGL_CHECK_ERROR(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER_EXT, fboDepthStencil));
|
||||
|
||||
GLenum status = MBGL_CHECK_ERROR(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
||||
|
||||
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
|
||||
std::string error("Couldn't create framebuffer: ");
|
||||
switch (status) {
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: (error += "incomplete attachment"); break;
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: error += "incomplete missing attachment"; break;
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: error += "incomplete dimensions"; break;
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: error += "incomplete formats"; break;
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: error += "incomplete draw buffer"; break;
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: error += "incomplete read buffer"; break;
|
||||
case GL_FRAMEBUFFER_UNSUPPORTED: error += "unsupported"; break;
|
||||
default: error += "other"; break;
|
||||
}
|
||||
throw std::runtime_error(error);
|
||||
}
|
||||
|
||||
MBGL_CHECK_ERROR(glViewport(0, 0, w, h));
|
||||
}
|
||||
|
||||
void HeadlessView::clearBuffers() {
|
||||
MBGL_CHECK_ERROR(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
|
||||
|
||||
if (fbo) {
|
||||
MBGL_CHECK_ERROR(glDeleteFramebuffersEXT(1, &fbo));
|
||||
fbo = 0;
|
||||
}
|
||||
|
||||
if (fboColor) {
|
||||
MBGL_CHECK_ERROR(glDeleteRenderbuffersEXT(1, &fboColor));
|
||||
fboColor = 0;
|
||||
}
|
||||
|
||||
if (fboDepthStencil) {
|
||||
MBGL_CHECK_ERROR(glDeleteRenderbuffersEXT(1, &fboDepthStencil));
|
||||
fboDepthStencil = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void HeadlessView::activateContext() {
|
||||
glContext->makeCurrent();
|
||||
}
|
||||
|
||||
void HeadlessView::deactivateContext() {
|
||||
glContext->doneCurrent();
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
Reference in New Issue
Block a user