-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagetexturecache_p.h
81 lines (64 loc) · 1.73 KB
/
imagetexturecache_p.h
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
#pragma once
#include "imagetexturecache.h"
#include <QAtomicInteger>
#include <QImage>
#include <QMutex>
class ImageTextureCachePrivate : public QObject
{
Q_OBJECT
public:
static QHash<QQuickWindow*,std::weak_ptr<ImageTextureCache>> instances;
QQuickWindow *window;
QMutex mutex;
QHash<QString,std::shared_ptr<ImageTextureCacheData>> cache;
QAtomicInt cacheCost;
QMutex freeMutex;
QVector<std::shared_ptr<ImageTextureCacheData>> freeable;
int freeThrottle;
int softLimit;
ImageTextureCachePrivate(QQuickWindow *window);
~ImageTextureCachePrivate();
void setFreeable(const std::shared_ptr<ImageTextureCacheData> &data, bool freeable);
public slots:
void renderThreadFree();
};
// Internal representation of data in the cache, referenced by
// ImageTextureCacheEntry.
struct ImageTextureCacheData : public std::enable_shared_from_this<ImageTextureCacheData>
{
public:
ImageTextureCacheData(ImageTextureCachePrivate *cache, const QString &key)
: key(key)
, cache(cache)
, texture(nullptr)
, cost(1)
, refCount(0)
{
}
const QString key;
ImageTextureCachePrivate * const cache;
QImage image;
QString error;
QSize imageSize;
QSGTexture *texture;
int cost;
void ref() {
if (!refCount.fetchAndAddOrdered(1)) {
cache->setFreeable(shared_from_this(), false);
}
}
void deref()
{
if (!refCount.deref()) {
cache->setFreeable(shared_from_this(), true);
}
}
int getRefCount()
{
return refCount.loadAcquire();
}
void updateCost();
private:
// Number of Entry objects referencing this data
QAtomicInteger<int> refCount;
};