summaryrefslogtreecommitdiff
path: root/platform/default/mbgl/gl/offscreen_view.cpp
blob: 5424e03c96fd6f7eb211cd32324d5645dca15015 (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
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/gl/framebuffer.hpp>
#include <mbgl/gl/renderbuffer.hpp>
#include <mbgl/util/optional.hpp>

#include <cstring>
#include <cassert>

namespace mbgl {

class OffscreenView::Impl {
public:
    Impl(gl::Context& context_, const Size size_) : context(context_), size(std::move(size_)) {
        assert(!size.isEmpty());
    }

    void bind() {
        if (!framebuffer) {
            color = context.createRenderbuffer<gl::RenderbufferType::RGBA>(size);
            depthStencil = context.createRenderbuffer<gl::RenderbufferType::DepthStencil>(size);
            framebuffer = context.createFramebuffer(*color, *depthStencil);
        } else {
            context.bindFramebuffer = framebuffer->framebuffer;
        }

        context.viewport = { 0, 0, size };
    }

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

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

private:
    gl::Context& context;
    const Size size;
    optional<gl::Framebuffer> framebuffer;
    optional<gl::Renderbuffer<gl::RenderbufferType::RGBA>> color;
    optional<gl::Renderbuffer<gl::RenderbufferType::DepthStencil>> depthStencil;
};

OffscreenView::OffscreenView(gl::Context& context, const Size size)
    : impl(std::make_unique<Impl>(context, std::move(size))) {
}

OffscreenView::~OffscreenView() = default;

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

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

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

} // namespace mbgl