Skip to content

Commit 88e7f6c

Browse files
committed
Use atlas to draw text.
1 parent 6aaefe9 commit 88e7f6c

File tree

1,855 files changed

+4974
-4001
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,855 files changed

+4974
-4001
lines changed

Diff for: src/rendering/caches/RenderCache.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ void RenderCache::attachToContext(tgfx::Context* current, bool forHitTest) {
195195
imageTasks.erase(assetID);
196196
clearSequenceCache(assetID);
197197
clearFilterCache(assetID);
198+
removeTextAtlas(assetID);
198199
}
199200
}
200201

@@ -288,6 +289,46 @@ void RenderCache::removeSnapshot(ID assetID) {
288289
snapshotCaches.erase(assetID);
289290
}
290291

292+
TextAtlas* RenderCache::getTextAtlas(ID assetID) {
293+
auto textAtlas = textAtlases.find(assetID);
294+
if (textAtlas == textAtlases.end()) {
295+
return nullptr;
296+
}
297+
return textAtlas->second;
298+
}
299+
300+
TextAtlas* RenderCache::getTextAtlas(const TextGlyphs* textGlyphs) {
301+
auto maxScaleFactor = stage->getAssetMaxScale(textGlyphs->assetID());
302+
auto textAtlas = getTextAtlas(textGlyphs->assetID());
303+
if (textAtlas && (textAtlas->textGlyphsID() != textGlyphs->id() ||
304+
fabsf(textAtlas->scaleFactor() - maxScaleFactor) > SCALE_FACTOR_PRECISION)) {
305+
removeTextAtlas(textGlyphs->assetID());
306+
textAtlas = nullptr;
307+
}
308+
if (textAtlas) {
309+
return textAtlas;
310+
}
311+
if (maxScaleFactor < SCALE_FACTOR_PRECISION) {
312+
return nullptr;
313+
}
314+
textAtlas = TextAtlas::Make(textGlyphs, this, maxScaleFactor).release();
315+
if (textAtlas) {
316+
graphicsMemory += textAtlas->memoryUsage();
317+
textAtlases[textGlyphs->assetID()] = textAtlas;
318+
}
319+
return textAtlas;
320+
}
321+
322+
void RenderCache::removeTextAtlas(ID assetID) {
323+
auto textAtlas = textAtlases.find(assetID);
324+
if (textAtlas == textAtlases.end()) {
325+
return;
326+
}
327+
graphicsMemory -= textAtlas->second->memoryUsage();
328+
delete textAtlas->second;
329+
textAtlases.erase(textAtlas);
330+
}
331+
291332
void RenderCache::clearAllSnapshots() {
292333
for (auto& item : snapshotCaches) {
293334
graphicsMemory -= item.second->memoryUsage();

Diff for: src/rendering/caches/RenderCache.h

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <memory>
2222
#include <unordered_set>
23+
#include "TextAtlas.h"
24+
#include "TextGlyphs.h"
2325
#include "gpu/Device.h"
2426
#include "pag/file.h"
2527
#include "pag/pag.h"
@@ -99,6 +101,12 @@ class RenderCache : public Performance {
99101
*/
100102
void removeSnapshot(ID assetID);
101103

104+
TextAtlas* getTextAtlas(ID assetID);
105+
106+
TextAtlas* getTextAtlas(const TextGlyphs* textGlyphs);
107+
108+
void removeTextAtlas(ID assetID);
109+
102110
/**
103111
* Prepares a bitmap task for next getImageBuffer() call.
104112
*/
@@ -149,6 +157,7 @@ class RenderCache : public Performance {
149157
std::unordered_set<ID> usedAssets = {};
150158
std::unordered_map<ID, Snapshot*> snapshotCaches = {};
151159
std::list<Snapshot*> snapshotLRU = {};
160+
std::unordered_map<ID, TextAtlas*> textAtlases = {};
152161
std::unordered_map<ID, std::shared_ptr<Task>> imageTasks;
153162
std::unordered_map<ID, std::shared_ptr<SequenceReader>> sequenceCaches;
154163
std::unordered_map<ID, Filter*> filterCaches;

0 commit comments

Comments
 (0)