summaryrefslogtreecommitdiff
path: root/src/mbgl/util/offscreen_texture.cpp
blob: 339e74b250bb1909aaaf283c3ec11deaaa503aa4 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <mbgl/gl/context.hpp>
#include <mbgl/util/offscreen_texture.hpp>

#include <cassert>
#include <cstring>

namespace mbgl {

OffscreenTexture::OffscreenTexture(OffscreenTexture&&) = default;
OffscreenTexture& OffscreenTexture::operator=(OffscreenTexture&&) = default;

class OffscreenTexture::Impl {
public:
    Impl(gl::Context& context_, const Size size_)
        : context(context_), size(std::move(size_)) {
        assert(!size.isEmpty());
    }
    Impl(gl::Context& context_,
         const Size size_,
         gl::Renderbuffer<gl::RenderbufferType::DepthComponent>& depth_)
        : context(context_), size(std::move(size_)), depth(&depth_) {
        assert(!size.isEmpty());
    }

    void bind() {
        if (!framebuffer) {
            texture = context.createTexture(size, gl::TextureFormat::RGBA);
            if (depth) {
                framebuffer = context.createFramebuffer(*texture, *depth);
            } else {
                framebuffer = context.createFramebuffer(*texture);
            }
        } else {
            context.bindFramebuffer = framebuffer->framebuffer;
        }

        context.activeTextureUnit = 0;
        context.scissorTest = false;
        context.viewport = { 0, 0, size };
    }

    PremultipliedImage readStillImage() {
        return context.readFramebuffer<PremultipliedImage>(size);
    }

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

    const Size& getSize() const {
        return size;
    }

private:
    gl::Context& context;
    const Size size;
    optional<gl::Framebuffer> framebuffer;
    optional<gl::Texture> texture;
    gl::Renderbuffer<gl::RenderbufferType::DepthComponent>* depth = nullptr;
};

OffscreenTexture::OffscreenTexture(gl::Context& context,
                                   const Size size)
    : impl(std::make_unique<Impl>(context, std::move(size))) {
    assert(!size.isEmpty());
}

OffscreenTexture::OffscreenTexture(gl::Context& context,
                                   const Size size,
                                   gl::Renderbuffer<gl::RenderbufferType::DepthComponent>& renderbuffer)
    : impl(std::make_unique<Impl>(context, std::move(size), renderbuffer)) {
    assert(!size.isEmpty());
}

OffscreenTexture::~OffscreenTexture() = default;

void OffscreenTexture::bind() {
    impl->bind();
}

PremultipliedImage OffscreenTexture::readStillImage() {
    return impl->readStillImage();
}

gl::Texture& OffscreenTexture::getTexture() {
    return impl->getTexture();
}

const Size& OffscreenTexture::getSize() const {
    return impl->getSize();
}

} // namespace mbgl