-
Notifications
You must be signed in to change notification settings - Fork 0
/
texturecache.cpp
72 lines (61 loc) · 1.86 KB
/
texturecache.cpp
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
#include "texturecache.h"
#include "timer.h"
TextureCache::TextureCache(QGLContext *context) :
m_context(context),
m_cacheSize(0),
m_sizeLimit(10*1000*100)
{ }
TextureCache::~TextureCache()
{
for (TextureEntry *entry: m_entries) {
m_context->deleteTexture(entry->textureId);
}
qDeleteAll(m_entries);
}
bool TextureCache::find(const QString &key, GLuint *textureId, QPixmap *pixmap)
{
if (m_entries.contains(key)) {
TextureEntry *entry = m_entries[key];
*textureId = entry->textureId;
*pixmap = entry->pixmap;
// update last used
m_lastUsed.remove(entry->lastUsed, key);
entry->lastUsed = Timer::systemTime();
m_lastUsed.insertMulti(entry->lastUsed, key);
return true;
}
return false;
}
GLuint TextureCache::insert(const QString &key, const QPixmap &pixmap)
{
remove(key);
m_cacheSize += calcSize(pixmap);
while (m_cacheSize > m_sizeLimit && m_lastUsed.size() > 0) {
// warning: delKey musn't get passed to remove as reference as it will be deleted!!!
QString delKey = m_lastUsed.first();
remove(delKey);
}
TextureEntry *entry = new TextureEntry;
entry->pixmap = pixmap;
entry->textureId = m_context->bindTexture(entry->pixmap);
entry->lastUsed = Timer::systemTime();
m_entries[key] = entry;
m_lastUsed.insertMulti(entry->lastUsed, key);
return entry->textureId;
}
int TextureCache::calcSize(const QPixmap &pixmap)
{
return pixmap.width() * pixmap.height() * (pixmap.depth() / 8);
}
void TextureCache::remove(const QString &key)
{
if (!m_entries.contains(key)) {
return;
}
TextureEntry *entry = m_entries[key];
m_context->deleteTexture(entry->textureId);
m_cacheSize -= calcSize(entry->pixmap);
m_lastUsed.remove(entry->lastUsed, key);
m_entries.remove(key);
delete entry;
}