summaryrefslogtreecommitdiff
path: root/src/mbgl/text/glyph_atlas.cpp
blob: b43971f7043d8b01a393e4c053e3f5166b136e00 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <mbgl/text/glyph_atlas.hpp>
#include <mbgl/text/glyph_atlas_observer.hpp>
#include <mbgl/text/glyph_pbf.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>

#include <cassert>
#include <algorithm>

namespace mbgl {

static GlyphAtlasObserver nullObserver;

GlyphAtlas::GlyphAtlas(const Size size, FileSource& fileSource_)
    : fileSource(fileSource_),
      observer(&nullObserver),
      bin(size.width, size.height),
      image(size),
      dirty(true) {
}

GlyphAtlas::~GlyphAtlas() = default;

void GlyphAtlas::getGlyphs(GlyphRequestor& requestor, GlyphDependencies glyphDependencies) {
    auto dependencies = std::make_shared<GlyphDependencies>(std::move(glyphDependencies));

    // Figure out which glyph ranges need to be fetched. For each range that does need to
    // be fetched, record an entry mapping the requestor to a shared pointer containing the
    // dependencies. When the shared pointer becomes unique, we know that all the dependencies
    // for that requestor have been fetched, and can notify it of completion.
    for (const auto& dependency : *dependencies) {
        const FontStack& fontStack = dependency.first;
        Entry& entry = entries[fontStack];

        const GlyphIDs& glyphIDs = dependency.second;
        GlyphRangeSet ranges;
        for (const auto& glyphID : glyphIDs) {
            ranges.insert(getGlyphRange(glyphID));
        }

        for (const auto& range : ranges) {
            auto it = entry.ranges.find(range);
            if (it == entry.ranges.end() || !it->second.parsed) {
                GlyphRequest& request = requestRange(entry, fontStack, range);
                request.requestors[&requestor] = dependencies;
            }
        }
    }

    // If the shared dependencies pointer is already unique, then all dependent glyph ranges
    // have already been loaded. Send a notification immediately.
    if (dependencies.unique()) {
        addGlyphs(requestor, *dependencies);
    }
}

GlyphAtlas::GlyphRequest& GlyphAtlas::requestRange(Entry& entry, const FontStack& fontStack, const GlyphRange& range) {
    GlyphRequest& request = entry.ranges[range];

    if (request.req) {
        return request;
    }

    request.req = fileSource.request(Resource::glyphs(glyphURL, fontStack, range), [this, fontStack, range](Response res) {
        processResponse(res, fontStack, range);
    });

    return request;
}

void GlyphAtlas::processResponse(const Response& res, const FontStack& fontStack, const GlyphRange& range) {
    if (res.error) {
        observer->onGlyphsError(fontStack, range, std::make_exception_ptr(std::runtime_error(res.error->message)));
        return;
    }

    if (res.notModified) {
        return;
    }

    Entry& entry = entries[fontStack];
    GlyphRequest& request = entry.ranges[range];

    if (!res.noContent) {
        std::vector<SDFGlyph> glyphs;

        try {
            glyphs = parseGlyphPBF(range, *res.data);
        } catch (...) {
            observer->onGlyphsError(fontStack, range, std::current_exception());
            return;
        }

        for (auto& glyph : glyphs) {
            auto it = entry.glyphs.find(glyph.id);
            if (it == entry.glyphs.end()) {
                // Glyph doesn't exist yet.
                entry.glyphs.emplace(glyph.id, GlyphValue {
                    std::move(glyph.bitmap),
                    std::move(glyph.metrics),
                    {}, {}
                });
            } else if (it->second.metrics == glyph.metrics) {
                if (it->second.bitmap != glyph.bitmap) {
                    // The actual bitmap was updated; this is unsupported.
                    Log::Warning(Event::Glyph, "Modified glyph changed bitmap represenation");
                }
                // At least try to update it in case it's currently unused.
                // If it is already used, we won't attempt to update the glyph atlas texture.
                it->second.bitmap = std::move(glyph.bitmap);
            } else {
                // The metrics were updated; this is unsupported.
                Log::Warning(Event::Glyph, "Modified glyph has different metrics");
                return;
            }
        }
    }

    request.parsed = true;

    for (auto& pair : request.requestors) {
        GlyphRequestor& requestor = *pair.first;
        const std::shared_ptr<GlyphDependencies>& dependencies = pair.second;
        if (dependencies.unique()) {
            addGlyphs(requestor, *dependencies);
        }
    }

    request.requestors.clear();

    observer->onGlyphsLoaded(fontStack, range);
}

void GlyphAtlas::setObserver(GlyphAtlasObserver* observer_) {
    observer = observer_ ? observer_ : &nullObserver;
}

void GlyphAtlas::addGlyphs(GlyphRequestor& requestor, const GlyphDependencies& glyphDependencies) {
    GlyphPositionMap glyphPositions;

    for (const auto& dependency : glyphDependencies) {
        const FontStack& fontStack = dependency.first;
        const GlyphIDs& glyphIDs = dependency.second;

        GlyphPositions& positions = glyphPositions[fontStack];
        Entry& entry = entries[fontStack];

        for (const auto& glyphID : glyphIDs) {
            // Make a glyph position entry even if we didn't get an SDF for the glyph. During layout,
            // an empty optional is treated as "loaded but nothing to show", wheras no entry in the
            // positions map means "not loaded yet".
            optional<Glyph>& glyph = positions[glyphID];

            auto it = entry.glyphs.find(glyphID);
            if (it == entry.glyphs.end())
                continue;

            it->second.ids.insert(&requestor);

            glyph = Glyph {
                addGlyph(it->second),
                it->second.metrics
            };
        }
    }

    requestor.onGlyphsAvailable(glyphPositions);
}

Rect<uint16_t> GlyphAtlas::addGlyph(GlyphValue& value) {
    // The glyph is already in this texture.
    if (value.rect) {
        return *value.rect;
    }

    // We don't need to add glyphs without a bitmap (e.g. whitespace).
    if (!value.bitmap.valid()) {
        return {};
    }

    // Add a 1px border around every image.
    const uint32_t padding = 1;
    uint16_t width = value.bitmap.size.width + 2 * padding;
    uint16_t height = value.bitmap.size.height + 2 * padding;

    // Increase to next number divisible by 4, but at least 1.
    // This is so we can scale down the texture coordinates and pack them
    // into 2 bytes rather than 4 bytes.
    width += (4 - width % 4);
    height += (4 - height % 4);

    Rect<uint16_t> rect = bin.allocate(width, height);
    if (rect.w == 0) {
        Log::Error(Event::OpenGL, "glyph bitmap overflow");
        return {};
    }

    AlphaImage::copy(value.bitmap, image, { 0, 0 }, { rect.x + padding, rect.y + padding }, value.bitmap.size);
    value.rect = rect;
    dirty = true;

    return rect;
}

void GlyphAtlas::removeGlyphValues(GlyphRequestor& requestor, std::map<GlyphID, GlyphValue>& values) {
    for (auto& it : values) {
        GlyphValue& value = it.second;
        if (value.ids.erase(&requestor) && value.ids.empty() && value.rect) {
            const Rect<uint16_t>& rect = *value.rect;

            // Clear out the bitmap.
            uint8_t *target = image.data.get();
            for (uint32_t y = 0; y < rect.h; y++) {
                uint32_t y1 = image.size.width * (rect.y + y) + rect.x;
                for (uint32_t x = 0; x < rect.w; x++) {
                    target[y1 + x] = 0;
                }
            }

            bin.release(rect);
            value.rect = {};
        }
    }
}

void GlyphAtlas::removePendingRanges(mbgl::GlyphRequestor& requestor, std::map<GlyphRange, GlyphRequest>& ranges) {
    for (auto& range : ranges) {
        range.second.requestors.erase(&requestor);
    }
}

void GlyphAtlas::removeGlyphs(GlyphRequestor& requestor) {
    for (auto& entry : entries) {
        removeGlyphValues(requestor, entry.second.glyphs);
        removePendingRanges(requestor, entry.second.ranges);
    }
}

Size GlyphAtlas::getSize() const {
    return image.size;
}

void GlyphAtlas::upload(gl::Context& context, gl::TextureUnit unit) {
    if (!texture) {
        texture = context.createTexture(image, unit);
    } else if (dirty) {
        context.updateTexture(*texture, image, unit);
    }
    
    dirty = false;
}

void GlyphAtlas::bind(gl::Context& context, gl::TextureUnit unit) {
    upload(context, unit);
    context.bindTexture(*texture, unit, gl::TextureFilter::Linear);
}

} // namespace mbgl