align static render mode and still image render mode

- static rendering now also runs in a separate thread; you have to start it with map.start(Map::Mode::Static) and join the thread with map.stop() before destructing the Map object
- map.renderStill() takes a callback with will be invoked on the *map* thread, so you'll have to figure out your own method of dispatching back to the main thread.
This commit is contained in:
Konstantin Käfer
2015-04-16 10:34:30 +02:00
parent cd496645d4
commit 46bf226910
11 changed files with 301 additions and 120 deletions

View File

@@ -1,7 +1,10 @@
#include <mbgl/platform/default/headless_view.hpp>
#include <mbgl/platform/default/headless_display.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/map/still_image.hpp>
#include <mbgl/util/std.hpp>
#include <stdexcept>
@@ -36,17 +39,17 @@ CGLProc CGLGetProcAddress(const char *proc) {
namespace mbgl {
HeadlessView::HeadlessView()
: display_(std::make_shared<HeadlessDisplay>()) {
createContext();
loadExtensions();
HeadlessView::HeadlessView(uint16_t width, uint16_t height, float pixelRatio)
: display(std::make_shared<HeadlessDisplay>()) {
resize(width, height, pixelRatio);
}
HeadlessView::HeadlessView(std::shared_ptr<HeadlessDisplay> display)
: display_(display) {
createContext();
loadExtensions();
HeadlessView::HeadlessView(std::shared_ptr<HeadlessDisplay> display_,
uint16_t width,
uint16_t height,
float pixelRatio)
: display(display_) {
resize(width, height, pixelRatio);
}
void HeadlessView::loadExtensions() {
@@ -54,8 +57,6 @@ void HeadlessView::loadExtensions() {
return;
}
activate();
pthread_once(&loadGLExtensions, [] {
const char *extensionPtr = reinterpret_cast<const char *>(MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS)));
@@ -95,17 +96,15 @@ void HeadlessView::loadExtensions() {
gl::isDepth24Supported = true;
extensionsLoaded = true;
deactivate();
}
void HeadlessView::createContext() {
if (!display_) {
if (!display) {
throw std::runtime_error("Display is not set");
}
#if MBGL_USE_CGL
CGLError error = CGLCreateContext(display_->pixelFormat, NULL, &glContext);
CGLError error = CGLCreateContext(display->pixelFormat, NULL, &glContext);
if (error != kCGLNoError) {
throw std::runtime_error(std::string("Error creating GL context object:") + CGLErrorString(error) + "\n");
}
@@ -117,8 +116,8 @@ void HeadlessView::createContext() {
#endif
#if MBGL_USE_GLX
xDisplay = display_->xDisplay;
fbConfigs = display_->fbConfigs;
xDisplay = display->xDisplay;
fbConfigs = display->fbConfigs;
if (!glContext) {
// Try to create a legacy context
@@ -147,17 +146,34 @@ void HeadlessView::createContext() {
#endif
}
bool HeadlessView::isActive() {
return std::this_thread::get_id() == thread;
}
void HeadlessView::resize(const uint16_t width, const uint16_t height, const float pixelRatio) {
std::lock_guard<std::mutex> lock(prospectiveMutex);
prospective = { width, height, pixelRatio };
}
HeadlessView::Dimensions::Dimensions(uint16_t width_, uint16_t height_, float pixelRatio_)
: width(width_), height(height_), pixelRatio(pixelRatio_) {
}
void HeadlessView::discard() {
assert(isActive());
{ // Obtain the new values.
std::lock_guard<std::mutex> lock(prospectiveMutex);
if (current.pixelWidth() == prospective.pixelWidth() && current.pixelHeight() == prospective.pixelHeight()) {
return;
}
current = prospective;
}
clearBuffers();
width_ = width;
height_ = height;
pixelRatio_ = pixelRatio;
const unsigned int w = width_ * pixelRatio_;
const unsigned int h = height_ * pixelRatio_;
activate();
const unsigned int w = current.width * current.pixelRatio;
const unsigned int h = current.height * current.pixelRatio;
// Create depth/stencil buffer
MBGL_CHECK_ERROR(glGenRenderbuffersEXT(1, &fboDepthStencil));
@@ -193,35 +209,36 @@ void HeadlessView::resize(const uint16_t width, const uint16_t height, const flo
throw std::runtime_error(error.str());
}
View::resize(width, height, pixelRatio, w, h);
deactivate();
View::resize(current.width, current.height, current.pixelRatio, w, h);
}
std::unique_ptr<uint32_t[]> HeadlessView::readPixels() {
const unsigned int w = width_ * pixelRatio_;
const unsigned int h = height_ * pixelRatio_;
std::unique_ptr<StillImage> HeadlessView::readStillImage() {
assert(isActive());
auto pixels = util::make_unique<uint32_t[]>(w * h);
const unsigned int w = current.pixelWidth();
const unsigned int h = current.pixelHeight();
activate();
MBGL_CHECK_ERROR(glReadPixels(0, 0, width_, height_, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get()));
deactivate();
auto image = util::make_unique<StillImage>();
image->width = w;
image->height = h;
image->pixels = util::make_unique<uint32_t[]>(w * h);
MBGL_CHECK_ERROR(glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels.get()));
const int stride = w * 4;
auto tmp = util::make_unique<char[]>(stride);
char *rgba = reinterpret_cast<char *>(pixels.get());
for (int i = 0, j = height_ - 1; i < j; i++, j--) {
char *rgba = reinterpret_cast<char *>(image->pixels.get());
for (int i = 0, j = h - 1; i < j; i++, j--) {
std::memcpy(tmp.get(), rgba + i * stride, stride);
std::memcpy(rgba + i * stride, rgba + j * stride, stride);
std::memcpy(rgba + j * stride, tmp.get(), stride);
}
return pixels;
return image;
}
void HeadlessView::clearBuffers() {
activate();
assert(isActive());
MBGL_CHECK_ERROR(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
@@ -239,12 +256,12 @@ void HeadlessView::clearBuffers() {
MBGL_CHECK_ERROR(glDeleteRenderbuffersEXT(1, &fboDepthStencil));
fboDepthStencil = 0;
}
deactivate();
}
HeadlessView::~HeadlessView() {
activate();
clearBuffers();
deactivate();
#if MBGL_USE_CGL
CGLDestroyContext(glContext);
@@ -265,6 +282,15 @@ void HeadlessView::notify() {
}
void HeadlessView::activate() {
if (thread != std::thread::id()) {
throw std::runtime_error("OpenGL context was already current");
}
thread = std::this_thread::get_id();
if (!glContext) {
createContext();
}
#if MBGL_USE_CGL
CGLError error = CGLSetCurrentContext(glContext);
if (error != kCGLNoError) {
@@ -277,9 +303,17 @@ void HeadlessView::activate() {
throw std::runtime_error("Switching OpenGL context failed.\n");
}
#endif
loadExtensions();
discard();
}
void HeadlessView::deactivate() {
if (thread == std::thread::id()) {
throw std::runtime_error("OpenGL context was not current");
}
thread = std::thread::id();
#if MBGL_USE_CGL
CGLError error = CGLSetCurrentContext(nullptr);
if (error != kCGLNoError) {