opengl/qopengltexturecache.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4class QOpenGLTextureCacheWrapper -
5{ -
6public: -
7 QOpenGLTextureCacheWrapper() -
8 { -
9 QImagePixmapCleanupHooks::instance()->addPlatformPixmapModificationHook(cleanupTexturesForPixmapData); -
10 QImagePixmapCleanupHooks::instance()->addPlatformPixmapDestructionHook(cleanupTexturesForPixmapData); -
11 QImagePixmapCleanupHooks::instance()->addImageHook(cleanupTexturesForCacheKey); -
12 }
never executed: }
0
13 -
14 ~QOpenGLTextureCacheWrapper() -
15 { -
16 QImagePixmapCleanupHooks::instance()->removePlatformPixmapModificationHook(cleanupTexturesForPixmapData); -
17 QImagePixmapCleanupHooks::instance()->removePlatformPixmapDestructionHook(cleanupTexturesForPixmapData); -
18 QImagePixmapCleanupHooks::instance()->removeImageHook(cleanupTexturesForCacheKey); -
19 }
never executed: }
0
20 -
21 QOpenGLTextureCache *cacheForContext(QOpenGLContext *context) { -
22 QMutexLocker lock(&m_mutex); -
23 return m_resource.value<QOpenGLTextureCache>(context);
never executed: return m_resource.value<QOpenGLTextureCache>(context);
0
24 } -
25 -
26 static void cleanupTexturesForCacheKey(qint64 key); -
27 static void cleanupTexturesForPixmapData(QPlatformPixmap *pmd); -
28 -
29private: -
30 QOpenGLMultiGroupSharedResource m_resource; -
31 QMutex m_mutex; -
32}; -
33 -
34static QOpenGLTextureCacheWrapper *qt_texture_caches() { static QGlobalStatic<QOpenGLTextureCacheWrapper > thisGlobalStatic = { { (0) }, false }; if (!thisGlobalStatic.pointer.load() && !thisGlobalStatic.destroyed) { QOpenGLTextureCacheWrapper *x = new QOpenGLTextureCacheWrapper; if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x)) delete x; else static QGlobalStaticDeleter<QOpenGLTextureCacheWrapper > cleanup(thisGlobalStatic); } return thisGlobalStatic.pointer.load(); }
never executed: delete x;
never executed: return thisGlobalStatic.pointer.load();
never evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
never evaluated: !thisGlobalStatic.pointer.load()
never evaluated: !thisGlobalStatic.destroyed
0
35 -
36QOpenGLTextureCache *QOpenGLTextureCache::cacheForContext(QOpenGLContext *context) -
37{ -
38 return qt_texture_caches()->cacheForContext(context);
never executed: return qt_texture_caches()->cacheForContext(context);
0
39} -
40 -
41void QOpenGLTextureCacheWrapper::cleanupTexturesForCacheKey(qint64 key) -
42{ -
43 QList<QOpenGLSharedResource *> resources = qt_texture_caches()->m_resource.resources(); -
44 for (QList<QOpenGLSharedResource *>::iterator it = resources.begin(); it != resources.end(); ++it)
never evaluated: it != resources.end()
0
45 static_cast<QOpenGLTextureCache *>(*it)->invalidate(key);
never executed: static_cast<QOpenGLTextureCache *>(*it)->invalidate(key);
0
46}
never executed: }
0
47 -
48void QOpenGLTextureCacheWrapper::cleanupTexturesForPixmapData(QPlatformPixmap *pmd) -
49{ -
50 cleanupTexturesForCacheKey(pmd->cacheKey()); -
51}
never executed: }
0
52 -
53QOpenGLTextureCache::QOpenGLTextureCache(QOpenGLContext *ctx) -
54 : QOpenGLSharedResource(ctx->shareGroup()) -
55 , m_cache(64 * 1024) -
56{ -
57}
never executed: }
0
58 -
59QOpenGLTextureCache::~QOpenGLTextureCache() -
60{ -
61} -
62 -
63GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QPixmap &pixmap) -
64{ -
65 if (pixmap.isNull())
never evaluated: pixmap.isNull()
0
66 return 0;
never executed: return 0;
0
67 QMutexLocker locker(&m_mutex); -
68 qint64 key = pixmap.cacheKey(); -
69 -
70 -
71 if (!pixmap.paintingActive()) {
never evaluated: !pixmap.paintingActive()
0
72 QOpenGLCachedTexture *entry = m_cache.object(key); -
73 if (entry) {
never evaluated: entry
0
74 glBindTexture(0x0DE1, entry->id()); -
75 return entry->id();
never executed: return entry->id();
0
76 } -
77 }
never executed: }
0
78 -
79 GLuint id = bindTexture(context, key, pixmap.toImage()); -
80 if (id > 0)
never evaluated: id > 0
0
81 QImagePixmapCleanupHooks::enableCleanupHooks(pixmap);
never executed: QImagePixmapCleanupHooks::enableCleanupHooks(pixmap);
0
82 -
83 return id;
never executed: return id;
0
84} -
85 -
86GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QImage &image) -
87{ -
88 if (image.isNull())
never evaluated: image.isNull()
0
89 return 0;
never executed: return 0;
0
90 QMutexLocker locker(&m_mutex); -
91 qint64 key = image.cacheKey(); -
92 -
93 -
94 if (!image.paintingActive()) {
never evaluated: !image.paintingActive()
0
95 QOpenGLCachedTexture *entry = m_cache.object(key); -
96 if (entry) {
never evaluated: entry
0
97 glBindTexture(0x0DE1, entry->id()); -
98 return entry->id();
never executed: return entry->id();
0
99 } -
100 }
never executed: }
0
101 -
102 GLuint id = bindTexture(context, key, image); -
103 if (id > 0)
never evaluated: id > 0
0
104 QImagePixmapCleanupHooks::enableCleanupHooks(image);
never executed: QImagePixmapCleanupHooks::enableCleanupHooks(image);
0
105 -
106 return id;
never executed: return id;
0
107} -
108 -
109static inline void qgl_byteSwapImage(QImage &img) -
110{ -
111 const int width = img.width(); -
112 const int height = img.height(); -
113 -
114 if (QSysInfo::ByteOrder == QSysInfo::LittleEndian)
never evaluated: QSysInfo::ByteOrder == QSysInfo::LittleEndian
0
115 { -
116 for (int i = 0; i < height; ++i) {
never evaluated: i < height
0
117 uint *p = (uint *) img.scanLine(i); -
118 for (int x = 0; x < width; ++x)
never evaluated: x < width
0
119 p[x] = ((p[x] << 16) & 0xff0000) | ((p[x] >> 16) & 0xff) | (p[x] & 0xff00ff00);
never executed: p[x] = ((p[x] << 16) & 0xff0000) | ((p[x] >> 16) & 0xff) | (p[x] & 0xff00ff00);
0
120 }
never executed: }
0
121 } else {
never executed: }
0
122 for (int i = 0; i < height; ++i) {
never evaluated: i < height
0
123 uint *p = (uint *) img.scanLine(i); -
124 for (int x = 0; x < width; ++x)
never evaluated: x < width
0
125 p[x] = (p[x] << 8) | (p[x] >> 24);
never executed: p[x] = (p[x] << 8) | (p[x] >> 24);
0
126 }
never executed: }
0
127 }
never executed: }
0
128} -
129 -
130GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, qint64 key, const QImage &image) -
131{ -
132 GLuint id; -
133 glGenTextures(1, &id); -
134 glBindTexture(0x0DE1, id); -
135 -
136 QImage tx = image.convertToFormat(QImage::Format_ARGB32_Premultiplied); -
137 -
138 qgl_byteSwapImage(tx); -
139 -
140 glTexImage2D(0x0DE1, 0, 0x1908, tx.width(), tx.height(), 0, 0x1908, 0x1401, const_cast<const QImage &>(tx).bits()); -
141 -
142 int cost = tx.width() * tx.height() * 4 / 1024; -
143 m_cache.insert(key, new QOpenGLCachedTexture(id, context), cost); -
144 -
145 return id;
never executed: return id;
0
146} -
147 -
148void QOpenGLTextureCache::invalidate(qint64 key) -
149{ -
150 QMutexLocker locker(&m_mutex); -
151 m_cache.remove(key); -
152}
never executed: }
0
153 -
154void QOpenGLTextureCache::invalidateResource() -
155{ -
156 m_cache.clear(); -
157}
never executed: }
0
158 -
159void QOpenGLTextureCache::freeResource(QOpenGLContext *) -
160{ -
161 qt_noop(); -
162}
never executed: }
0
163 -
164static void freeTexture(QOpenGLFunctions *, GLuint id) -
165{ -
166 glDeleteTextures(1, &id); -
167}
never executed: }
0
168 -
169QOpenGLCachedTexture::QOpenGLCachedTexture(GLuint id, QOpenGLContext *context) -
170{ -
171 m_resource = new QOpenGLSharedResourceGuard(context, id, freeTexture); -
172}
never executed: }
0
173 -
174 -
175 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial