summaryrefslogtreecommitdiff
path: root/src/mbgl/geometry/glyph_atlas.hpp
blob: 5252963f51f41d99d5c4aa7608f5647b910a9766 (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
#pragma once

#include <mbgl/geometry/binpack.hpp>
#include <mbgl/text/glyph_store.hpp>
#include <mbgl/util/atomic.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/object_store.hpp>

#include <string>
#include <set>
#include <unordered_map>
#include <mutex>

namespace mbgl {

namespace gl {
class Config;
} // namespace gl

class GlyphAtlas : public util::noncopyable {
public:
    GlyphAtlas(uint16_t width, uint16_t height);
    ~GlyphAtlas();

    void addGlyphs(uintptr_t tileUID,
                   const std::u32string& text,
                   const FontStack&,
                   const GlyphSet&,
                   GlyphPositions&);
    void removeGlyphs(uintptr_t tileUID);

    // Binds the atlas texture to the GPU, and uploads data if it is out of date.
    void bind(gl::ObjectStore&, gl::Config&, uint32_t unit);

    // Uploads the texture to the GPU to be available when we need it. This is a lazy operation;
    // the texture is only bound when the data is out of date (=dirty).
    void upload(gl::ObjectStore&, gl::Config&, uint32_t unit);

    const GLsizei width;
    const GLsizei height;

private:
    struct GlyphValue {
        GlyphValue(Rect<uint16_t> rect_, uintptr_t id)
            : rect(std::move(rect_)), ids({ id }) {}
        Rect<uint16_t> rect;
        std::set<uintptr_t> ids;
    };

    Rect<uint16_t> addGlyph(uintptr_t tileID,
                            const FontStack&,
                            const SDFGlyph&);

    std::mutex mtx;
    BinPack<uint16_t> bin;
    std::unordered_map<FontStack, std::map<uint32_t, GlyphValue>, FontStackHash> index;
    const std::unique_ptr<uint8_t[]> data;
    util::Atomic<bool> dirty;
    mbgl::optional<gl::UniqueTexture> texture;
};

} // namespace mbgl