Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use atlas to draw text. #128

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
41 changes: 41 additions & 0 deletions src/rendering/caches/RenderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ void RenderCache::attachToContext(tgfx::Context* current, bool forHitTest) {
imageTasks.erase(assetID);
clearSequenceCache(assetID);
clearFilterCache(assetID);
removeTextAtlas(assetID);
}
}

Expand Down Expand Up @@ -288,6 +289,46 @@ void RenderCache::removeSnapshot(ID assetID) {
snapshotCaches.erase(assetID);
}

TextAtlas* RenderCache::getTextAtlas(ID assetID) {
auto textAtlas = textAtlases.find(assetID);
if (textAtlas == textAtlases.end()) {
return nullptr;
}
return textAtlas->second;
}

TextAtlas* RenderCache::getTextAtlas(const TextGlyphs* textGlyphs) {
auto maxScaleFactor = stage->getAssetMaxScale(textGlyphs->assetID());
auto textAtlas = getTextAtlas(textGlyphs->assetID());
if (textAtlas && (textAtlas->textGlyphsID() != textGlyphs->id() ||
fabsf(textAtlas->scaleFactor() - maxScaleFactor) > SCALE_FACTOR_PRECISION)) {
removeTextAtlas(textGlyphs->assetID());
textAtlas = nullptr;
}
if (textAtlas) {
return textAtlas;
}
if (maxScaleFactor < SCALE_FACTOR_PRECISION) {
return nullptr;
}
textAtlas = TextAtlas::Make(textGlyphs, this, maxScaleFactor).release();
if (textAtlas) {
graphicsMemory += textAtlas->memoryUsage();
textAtlases[textGlyphs->assetID()] = textAtlas;
}
return textAtlas;
}

void RenderCache::removeTextAtlas(ID assetID) {
auto textAtlas = textAtlases.find(assetID);
if (textAtlas == textAtlases.end()) {
return;
}
graphicsMemory -= textAtlas->second->memoryUsage();
delete textAtlas->second;
textAtlases.erase(textAtlas);
}

void RenderCache::clearAllSnapshots() {
for (auto& item : snapshotCaches) {
graphicsMemory -= item.second->memoryUsage();
Expand Down
9 changes: 9 additions & 0 deletions src/rendering/caches/RenderCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include <memory>
#include <unordered_set>
#include "TextAtlas.h"
#include "TextGlyphs.h"
#include "gpu/Device.h"
#include "pag/file.h"
#include "pag/pag.h"
Expand Down Expand Up @@ -99,6 +101,12 @@ class RenderCache : public Performance {
*/
void removeSnapshot(ID assetID);

TextAtlas* getTextAtlas(ID assetID);

TextAtlas* getTextAtlas(const TextGlyphs* textGlyphs);

void removeTextAtlas(ID assetID);

/**
* Prepares a bitmap task for next getImageBuffer() call.
*/
Expand Down Expand Up @@ -149,6 +157,7 @@ class RenderCache : public Performance {
std::unordered_set<ID> usedAssets = {};
std::unordered_map<ID, Snapshot*> snapshotCaches = {};
std::list<Snapshot*> snapshotLRU = {};
std::unordered_map<ID, TextAtlas*> textAtlases = {};
std::unordered_map<ID, std::shared_ptr<Task>> imageTasks;
std::unordered_map<ID, std::shared_ptr<SequenceReader>> sequenceCaches;
std::unordered_map<ID, Filter*> filterCaches;
Expand Down
Loading