Files
MapLibreParticleLab/src/mbgl/text/glyph_manager.cpp
Alexander Shalamov 879c44f661 [core] Modularize FileSource codebase (#15768)
* [core] Introduce FileSourceManager and use it for default platform impl

  - Add `FileSourceManager` interface that provides access to `FileSource`
    instances and means of registering / unregistering `FileSource` factories
  - Split `DefaultFileSource` into smaller parts
  - Add `DatabaseFileSource` interface and it's default implementation
  - Remove inter-dependencies between concrete `FileSource` classes

* [build] Add files to next build system

* [core] Add generic property setters / getters

* [core] Remove setOnlineStatus from OnlineFileSource interface

* [core] Hide threading implementation details from DatabaseFileSource interface

* [core] Make DB file source methods virtual

* [core] Add documentation for DatabaseFileSource and rename one method

* [core] Use simple callback instead of ActorRef

* [core] Remove ActorRef from OnlineFileSource public header

* [core] Add callback to FileSource::forward async API

* [core] Pass OfflineRegionDefinition by value

* [core] Update tests to use modular file sources

* [core] Update unit tests

* [core] Update unit tests after rebase

* [core] Backport low prio fix for cached requests

* [core] Backport pack database

* [core] Return removed factory from unRegisterFileSourceFactory

* [core] Rename shadowed args in onlinefilesource

* [core] Remove simple std::function callback aliases

* [core] Expose online file source property keys in public header file

* [test-runner] Add proxy file source test runner

* [cache] Update mbgl-cache utility to use new file source

* [metrics] Rebaseline binary size metrics

* [offline] Update offline utility

* [core] Update changelog
2020-01-13 10:57:23 +02:00

167 lines
5.6 KiB
C++

#include <mbgl/storage/file_source.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/text/glyph_manager.hpp>
#include <mbgl/text/glyph_manager_observer.hpp>
#include <mbgl/text/glyph_pbf.hpp>
#include <mbgl/util/async_request.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/tiny_sdf.hpp>
namespace mbgl {
static GlyphManagerObserver nullObserver;
GlyphManager::GlyphManager(std::unique_ptr<LocalGlyphRasterizer> localGlyphRasterizer_)
: observer(&nullObserver),
localGlyphRasterizer(std::move(localGlyphRasterizer_)) {
}
GlyphManager::~GlyphManager() = default;
void GlyphManager::getGlyphs(GlyphRequestor& requestor, GlyphDependencies glyphDependencies, FileSource& fileSource) {
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;
std::unordered_set<GlyphRange> ranges;
for (const auto& glyphID : glyphIDs) {
if (localGlyphRasterizer->canRasterizeGlyph(fontStack, glyphID)) {
if (entry.glyphs.find(glyphID) == entry.glyphs.end()) {
entry.glyphs.emplace(glyphID, makeMutable<Glyph>(generateLocalSDF(fontStack, glyphID)));
}
} else {
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 = entry.ranges[range];
request.requestors[&requestor] = dependencies;
requestRange(request, fontStack, range, fileSource);
}
}
}
// If the shared dependencies pointer is already unique, then all dependent glyph ranges
// have already been loaded. Send a notification immediately.
if (dependencies.unique()) {
notify(requestor, *dependencies);
}
}
Glyph GlyphManager::generateLocalSDF(const FontStack& fontStack, GlyphID glyphID) {
Glyph local = localGlyphRasterizer->rasterizeGlyph(fontStack, glyphID);
local.bitmap = util::transformRasterToSDF(local.bitmap, 8, .25);
return local;
}
void GlyphManager::requestRange(GlyphRequest& request, const FontStack& fontStack, const GlyphRange& range, FileSource& fileSource) {
if (request.req) {
return;
}
request.req = fileSource.request(Resource::glyphs(glyphURL, fontStack, range), [this, fontStack, range](Response res) {
processResponse(res, fontStack, range);
});
}
void GlyphManager::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<Glyph> glyphs;
try {
glyphs = parseGlyphPBF(range, *res.data);
} catch (...) {
observer->onGlyphsError(fontStack, range, std::current_exception());
return;
}
for (auto& glyph : glyphs) {
auto id = glyph.id;
if (!localGlyphRasterizer->canRasterizeGlyph(fontStack, id)) {
entry.glyphs.erase(id);
entry.glyphs.emplace(id, makeMutable<Glyph>(std::move(glyph)));
}
}
}
request.parsed = true;
for (auto& pair : request.requestors) {
GlyphRequestor& requestor = *pair.first;
const std::shared_ptr<GlyphDependencies>& dependencies = pair.second;
if (dependencies.unique()) {
notify(requestor, *dependencies);
}
}
request.requestors.clear();
observer->onGlyphsLoaded(fontStack, range);
}
void GlyphManager::setObserver(GlyphManagerObserver* observer_) {
observer = observer_ ? observer_ : &nullObserver;
}
void GlyphManager::notify(GlyphRequestor& requestor, const GlyphDependencies& glyphDependencies) {
GlyphMap response;
for (const auto& dependency : glyphDependencies) {
const FontStack& fontStack = dependency.first;
const GlyphIDs& glyphIDs = dependency.second;
Glyphs& glyphs = response[FontStackHasher()(fontStack)];
Entry& entry = entries[fontStack];
for (const auto& glyphID : glyphIDs) {
auto it = entry.glyphs.find(glyphID);
if (it != entry.glyphs.end()) {
glyphs.emplace(*it);
} else {
glyphs.emplace(glyphID, std::experimental::nullopt);
}
}
}
requestor.onGlyphsAvailable(response);
}
void GlyphManager::removeRequestor(GlyphRequestor& requestor) {
for (auto& entry : entries) {
for (auto& range : entry.second.ranges) {
range.second.requestors.erase(&requestor);
}
}
}
void GlyphManager::evict(const std::set<FontStack>& keep) {
util::erase_if(entries, [&] (const auto& entry) {
return keep.count(entry.first) == 0;
});
}
} // namespace mbgl