summaryrefslogtreecommitdiff
path: root/src/mbgl/text/glyph_store.cpp
blob: a3d1530e3db6c8b1c03daa45a62377f5218be015 (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
#include <mbgl/text/glyph_store.hpp>

#include <mbgl/text/glyph_pbf.hpp>
#include <mbgl/util/thread_context.hpp>

#include <cassert>

namespace mbgl {

GlyphStore::GlyphStore() = default;
GlyphStore::~GlyphStore() = default;

void GlyphStore::requestGlyphRange(const std::string& fontStackName, const GlyphRange& range) {
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));

    std::lock_guard<std::mutex> lock(rangesMutex);
    auto& rangeSets = ranges[fontStackName];

    const auto& rangeSetsIt = rangeSets.find(range);
    if (rangeSetsIt != rangeSets.end()) {
        return;
    }

    rangeSets.emplace(range,
        std::make_unique<GlyphPBF>(this, fontStackName, range, observer));
}


bool GlyphStore::hasGlyphRanges(const std::string& fontStackName, const std::set<GlyphRange>& glyphRanges) {
    if (glyphRanges.empty()) {
        return true;
    }

    std::lock_guard<std::mutex> lock(rangesMutex);
    const auto& rangeSets = ranges[fontStackName];

    bool hasRanges = true;
    for (const auto& range : glyphRanges) {
        const auto& rangeSetsIt = rangeSets.find(range);
        if (rangeSetsIt == rangeSets.end()) {
            // Push the request to the MapThread, so we can easly cancel
            // if it is still pending when we destroy this object.
            workQueue.push(std::bind(&GlyphStore::requestGlyphRange, this, fontStackName, range));

            hasRanges = false;
            continue;
        }

        if (!rangeSetsIt->second->isParsed()) {
            hasRanges = false;
        }
    }

    return hasRanges;
}

util::exclusive<FontStack> GlyphStore::getFontStack(const std::string& fontStack) {
    auto lock = std::make_unique<std::lock_guard<std::mutex>>(stacksMutex);

    auto it = stacks.find(fontStack);
    if (it == stacks.end()) {
        it = stacks.emplace(fontStack, std::make_unique<FontStack>()).first;
    }

    // FIXME: We lock all FontStacks, but what we should
    // really do is lock only the one we are returning.
    return { it->second.get(), std::move(lock) };
}

void GlyphStore::setObserver(Observer* observer_) {
    observer = observer_;
}

} // namespace mbgl