summaryrefslogtreecommitdiff
path: root/src/mbgl/text/glyph_atlas.hpp
blob: 2003c8e593f681cc097bdad21c674e39a83aeea2 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once

#include <mbgl/text/glyph.hpp>
#include <mbgl/text/glyph_atlas_observer.hpp>
#include <mbgl/text/glyph_range.hpp>
#include <mbgl/text/glyph_pbf.hpp>
#include <mbgl/geometry/binpack.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/font_stack.hpp>
#include <mbgl/util/work_queue.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/gl/texture.hpp>
#include <mbgl/gl/object.hpp>

#include <string>
#include <unordered_set>
#include <unordered_map>

class GlyphAtlasTest;

namespace mbgl {

class FileSource;
class AsyncRequest;
class Response;

namespace gl {
class Context;
} // namespace gl

class GlyphRequestor {
public:
    virtual void onGlyphsAvailable(GlyphPositionMap, GlyphRangeSet) = 0;
};
    
class GlyphAtlas : public util::noncopyable {
public:
    GlyphAtlas(Size, FileSource&);
    ~GlyphAtlas();

    std::map<uint32_t, SDFGlyph>& getGlyphSet(const FontStack&);

    // Workers send a `getGlyphs` message to the main thread once they have determined
    // which glyphs they will need. Invoking this method will increment reference
    // counts for all the glyphs in `GlyphDependencies`. If all glyphs are already
    // locally available, the observer will be notified that the glyphs are available
    // immediately. Otherwise, a request on the FileSource is made, and when all glyphs
    // are parsed and added to the atlas, the observer will be notified.
    // Workers are given a copied 'GlyphPositions' map to use for placing their glyphs.
    // The positions specified in this object are guaranteed to be
    // valid for the lifetime of the tile.
    void getGlyphs(GlyphRequestor&, GlyphDependencies);
    void removeGlyphs(GlyphRequestor&);

    void setURL(const std::string& url) {
        glyphURL = url;
    }

    void setObserver(GlyphAtlasObserver*);

    // Binds the atlas texture to the GPU, and uploads data if it is out of date.
    void bind(gl::Context&, gl::TextureUnit 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::Context&, gl::TextureUnit unit);

    Size getSize() const;

private:
    FileSource& fileSource;
    std::string glyphURL;

    struct GlyphValue {
        GlyphValue(Rect<uint16_t> rect_, GlyphRequestor* id)
            : rect(std::move(rect_)), ids({ id }) {}
        Rect<uint16_t> rect;
        std::unordered_set<GlyphRequestor*> ids;
    };

    struct GlyphRequest {
        bool parsed = false;
        std::unique_ptr<AsyncRequest> req;
        std::unordered_map<GlyphRequestor*, std::shared_ptr<GlyphDependencies>> requestors;
    };

    struct Entry {
        std::map<GlyphRange, GlyphRequest> ranges;
        std::map<uint32_t, SDFGlyph> sdfs;
        std::map<uint32_t, GlyphValue> glyphValues;
    };

    std::unordered_map<FontStack, Entry, FontStackHash> entries;

    // Only used by GlyphAtlasTest
    friend class ::GlyphAtlasTest;
    bool hasGlyphRanges(const FontStack&, const GlyphRangeSet&) const;
    bool hasGlyphRange(const FontStack&, const GlyphRange&) const;
    bool rangeIsParsed(const std::map<GlyphRange, GlyphRequest>&, const GlyphRange&) const;

    GlyphRequest& requestRange(Entry&, const FontStack&, const GlyphRange&);
    void processResponse(const Response&, const FontStack&, const GlyphRange&);

    void addGlyphs(GlyphRequestor&, const GlyphDependencies&);
    void addGlyph(GlyphRequestor&, const FontStack&, const SDFGlyph&);

    void removeGlyphValues(GlyphRequestor&, std::map<uint32_t, GlyphValue>&);
    void removePendingRanges(GlyphRequestor&, std::map<GlyphRange, GlyphRequest>&);

    GlyphAtlasObserver* observer = nullptr;

    BinPack<uint16_t> bin;
    AlphaImage image;
    bool dirty;
    mbgl::optional<gl::Texture> texture;
};

} // namespace mbgl