summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Hunter <adam@mapbox.com>2015-12-09 16:49:34 -0800
committerMike Morris <mikemorris@users.noreply.github.com>2016-08-26 10:56:19 -0400
commitf75817a5df7139a59c08d67372905d422aac6be8 (patch)
treea301eb642353150c6608dc215f3ddb4fe4cb7d0b
parent51e6e2684496f9d858ce77e86fbd964b8b3abb9b (diff)
downloadqtlocation-mapboxgl-f75817a5df7139a59c08d67372905d422aac6be8.tar.gz
[node] Move to using PBOs for the readStillImage call in HeadlessView.
-rw-r--r--platform/default/headless_view.cpp39
1 files changed, 29 insertions, 10 deletions
diff --git a/platform/default/headless_view.cpp b/platform/default/headless_view.cpp
index 71fdf5885b..6f0d6c2198 100644
--- a/platform/default/headless_view.cpp
+++ b/platform/default/headless_view.cpp
@@ -49,23 +49,42 @@ PremultipliedImage HeadlessView::readStillImage() {
const unsigned int h = dimensions[1] * pixelRatio;
PremultipliedImage image { w, h };
+ GLuint bufferId;
+
+ MBGL_CHECK_ERROR(glGenBuffers(1, &bufferId));
+ MBGL_CHECK_ERROR(glBindBuffer(GL_PIXEL_PACK_BUFFER, bufferId));
+ MBGL_CHECK_ERROR(glBufferData(GL_PIXEL_PACK_BUFFER, h * image.stride(), 0, GL_STREAM_READ));
+
{
util::stopwatch stopwatch2("glReadPixels", EventSeverity::Info, Event::General);
- MBGL_CHECK_ERROR(glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, image.data.get()));
+ MBGL_CHECK_ERROR(glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, 0));
}
- const auto stride = image.stride();
- auto tmp = std::make_unique<uint8_t[]>(stride);
- uint8_t* rgba = image.data.get();
- {
- util::stopwatch stopwatch3("memcpy", EventSeverity::Info, Event::General);
- 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);
+ // map the PBO to process its data by CPU
+ GLubyte* ptr = (GLubyte*)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
+
+ if (ptr) {
+ const int stride = image.stride();
+ auto tmp = std::make_unique<uint8_t[]>(stride);
+ uint8_t* rgba = image.data.get();
+
+ {
+ util::stopwatch stopwatch3("memcpy", EventSeverity::Info, Event::General);
+ for (unsigned int i = 0, j = h - 1; i < h; i++, j--) {
+ std::memcpy(rgba + i * stride, ptr + j * stride, stride);
+ }
}
+
+ MBGL_CHECK_ERROR(glUnmapBuffer(GL_PIXEL_PACK_BUFFER));
+ } else {
+ throw std::runtime_error(std::string("Could not map buffer on return from read pixels."));
}
+ MBGL_CHECK_ERROR(glDeleteBuffers(1, &bufferId));
+
+ // back to conventional pixel operation
+ MBGL_CHECK_ERROR(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
+
return image;
}