blob: c377a0706a7a924b385831c1a0d4041d2eb10b54 (
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
75
76
77
78
79
80
81
82
83
84
85
|
#include "StarAssetTextureGroup.hpp"
#include "StarIterator.hpp"
#include "StarTime.hpp"
#include "StarRoot.hpp"
#include "StarAssets.hpp"
#include "StarImageMetadataDatabase.hpp"
namespace Star {
AssetTextureGroup::AssetTextureGroup(TextureGroupPtr textureGroup)
: m_textureGroup(std::move(textureGroup)) {
m_reloadTracker = make_shared<TrackerListener>();
Root::singleton().registerReloadListener(m_reloadTracker);
}
TexturePtr AssetTextureGroup::loadTexture(AssetPath const& imagePath) {
return loadTexture(imagePath, false);
}
TexturePtr AssetTextureGroup::tryTexture(AssetPath const& imagePath) {
return loadTexture(imagePath, true);
}
bool AssetTextureGroup::textureLoaded(AssetPath const& imagePath) const {
return m_textureMap.contains(imagePath);
}
void AssetTextureGroup::cleanup(int64_t textureTimeout) {
if (m_reloadTracker->pullTriggered()) {
m_textureMap.clear();
m_textureDeduplicationMap.clear();
} else {
int64_t time = Time::monotonicMilliseconds();
List<Texture const*> liveTextures;
filter(m_textureMap, [&](auto const& pair) {
if (time - pair.second.second < textureTimeout) {
liveTextures.append(pair.second.first.get());
return true;
}
return false;
});
liveTextures.sort();
eraseWhere(m_textureDeduplicationMap, [&](auto const& p) {
return !liveTextures.containsSorted(p.second.get());
});
}
}
TexturePtr AssetTextureGroup::loadTexture(AssetPath const& imagePath, bool tryTexture) {
if (auto p = m_textureMap.ptr(imagePath)) {
p->second = Time::monotonicMilliseconds();
return p->first;
}
auto assets = Root::singleton().assets();
ImageConstPtr image;
if (tryTexture)
image = assets->tryImage(imagePath);
else
image = assets->image(imagePath);
if (!image)
return {};
// Assets will return the same image ptr if two different asset paths point
// to the same underlying cached image. We should not make duplicate entries
// in the texture group for these, so we keep track of the image pointers
// returned to deduplicate them.
if (auto existingTexture = m_textureDeduplicationMap.value(image)) {
m_textureMap.add(imagePath, {existingTexture, Time::monotonicMilliseconds()});
return existingTexture;
} else {
auto texture = m_textureGroup->create(*image);
m_textureMap.add(imagePath, {texture, Time::monotonicMilliseconds()});
m_textureDeduplicationMap.add(image, texture);
return texture;
}
}
}
|