summaryrefslogtreecommitdiff
path: root/src/mbgl/util/offscreen_texture.cpp
blob: 40bb70b70ea47725ac148b077e1fa06d239a47ad (plain)
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
#include <mbgl/util/offscreen_texture.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/gl/gl.hpp>

#include <cstring>
#include <cassert>

namespace mbgl {

OffscreenTexture::OffscreenTexture(gl::Context& context_, std::array<uint16_t, 2> size_)
    : context(context_), size(std::move(size_)) {
    assert(size[0] > 0 && size[1] > 0);
}

void OffscreenTexture::bind() {
    if (!framebuffer) {
        texture = context.createTexture(size);
        framebuffer = context.createFramebuffer(*texture);
    } else {
        context.bindFramebuffer = framebuffer->framebuffer;
    }

    context.viewport = { 0, 0, size[0], size[1] };
}

gl::Texture& OffscreenTexture::getTexture() {
    assert(texture);
    return *texture;
}

PremultipliedImage OffscreenTexture::readStillImage() {
    PremultipliedImage image { size[0], size[1] };
    MBGL_CHECK_ERROR(glReadPixels(0, 0, size[0], size[1], GL_RGBA, GL_UNSIGNED_BYTE, image.data.get()));

    const auto stride = image.stride();
    auto tmp = std::make_unique<uint8_t[]>(stride);
    uint8_t* rgba = image.data.get();
    for (int i = 0, j = size[1] - 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 image;
}


} // namespace mbgl