Skip to content

Commit

Permalink
Use atlas to draw text. (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvpengwei authored Feb 21, 2022
1 parent 18cfd76 commit f2a00b3
Show file tree
Hide file tree
Showing 1,855 changed files with 4,974 additions and 4,001 deletions.
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

0 comments on commit f2a00b3

Please sign in to comment.