Line | Source | Count |
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 | #include "qglgradientcache_p.h" | - |
41 | #include <private/qdrawhelper_p.h> | - |
42 | #include <private/qgl_p.h> | - |
43 | #include <QtCore/qmutex.h> | - |
44 | | - |
45 | QT_BEGIN_NAMESPACE | - |
46 | | - |
47 | class QGL2GradientCacheWrapper | - |
48 | { | - |
49 | public: | - |
50 | QGL2GradientCache *cacheForContext(const QGLContext *context) { | - |
51 | QMutexLocker lock(&m_mutex); | - |
52 | return m_resource.value<QGL2GradientCache>(context->contextHandle()); | - |
53 | } | - |
54 | | - |
55 | private: | - |
56 | QOpenGLMultiGroupSharedResource m_resource; | - |
57 | QMutex m_mutex; | - |
58 | }; | - |
59 | | - |
60 | Q_GLOBAL_STATIC(QGL2GradientCacheWrapper, qt_gradient_caches) | - |
61 | | - |
62 | QGL2GradientCache::QGL2GradientCache(QOpenGLContext *ctx) | - |
63 | : QOpenGLSharedResource(ctx->shareGroup()) | - |
64 | { | - |
65 | } | - |
66 | | - |
67 | QGL2GradientCache::~QGL2GradientCache() | - |
68 | { | - |
69 | cache.clear(); | - |
70 | } | - |
71 | | - |
72 | QGL2GradientCache *QGL2GradientCache::cacheForContext(const QGLContext *context) | - |
73 | { | - |
74 | return qt_gradient_caches()->cacheForContext(context); | - |
75 | } | - |
76 | | - |
77 | void QGL2GradientCache::invalidateResource() | - |
78 | { | - |
79 | QMutexLocker lock(&m_mutex); | - |
80 | cache.clear(); | - |
81 | } | - |
82 | | - |
83 | void QGL2GradientCache::freeResource(QOpenGLContext *) | - |
84 | { | - |
85 | cleanCache(); | - |
86 | } | - |
87 | | - |
88 | void QGL2GradientCache::cleanCache() | - |
89 | { | - |
90 | QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); | - |
91 | QMutexLocker lock(&m_mutex); | - |
92 | QGLGradientColorTableHash::const_iterator it = cache.constBegin(); | - |
93 | for (; it != cache.constEnd(); ++it) { | - |
94 | const CacheInfo &cache_info = it.value(); | - |
95 | funcs->glDeleteTextures(1, &cache_info.texId); | - |
96 | } | - |
97 | cache.clear(); | - |
98 | } | - |
99 | | - |
100 | GLuint QGL2GradientCache::getBuffer(const QGradient &gradient, qreal opacity) | - |
101 | { | - |
102 | QMutexLocker lock(&m_mutex); | - |
103 | quint64 hash_val = 0; | - |
104 | | - |
105 | const QGradientStops stops = gradient.stops(); | - |
106 | for (int i = 0; i < stops.size() && i <= 2; i++)TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
107 | hash_val += stops[i].second.rgba(); never executed: hash_val += stops[i].second.rgba(); | 0 |
108 | | - |
109 | QGLGradientColorTableHash::const_iterator it = cache.constFind(hash_val); | - |
110 | | - |
111 | if (it == cache.constEnd())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
112 | return addCacheElement(hash_val, gradient, opacity); never executed: return addCacheElement(hash_val, gradient, opacity); | 0 |
113 | else { | - |
114 | do { | - |
115 | const CacheInfo &cache_info = it.value(); | - |
116 | if (cache_info.stops == stops && cache_info.opacity == opacityTRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
117 | && cache_info.interpolationMode == gradient.interpolationMode())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
118 | { | - |
119 | return cache_info.texId; never executed: return cache_info.texId; | 0 |
120 | } | - |
121 | ++it; | - |
122 | } while (it != cache.constEnd() && it.key() == hash_val); never executed: end of block TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
123 | | - |
124 | return addCacheElement(hash_val, gradient, opacity); never executed: return addCacheElement(hash_val, gradient, opacity); | 0 |
125 | } | - |
126 | } | - |
127 | | - |
128 | | - |
129 | GLuint QGL2GradientCache::addCacheElement(quint64 hash_val, const QGradient &gradient, qreal opacity) | - |
130 | { | - |
131 | QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); | - |
132 | if (cache.size() == maxCacheSize()) { | - |
133 | int elem_to_remove = qrand() % maxCacheSize(); | - |
134 | quint64 key = cache.keys()[elem_to_remove]; | - |
135 | | - |
136 | | - |
137 | QGLGradientColorTableHash::const_iterator it = cache.constFind(key); | - |
138 | do { | - |
139 | funcs->glDeleteTextures(1, &it.value().texId); | - |
140 | } while (++it != cache.constEnd() && it.key() == key); | - |
141 | cache.remove(key); | - |
142 | } | - |
143 | | - |
144 | CacheInfo cache_entry(gradient.stops(), opacity, gradient.interpolationMode()); | - |
145 | uint buffer[1024]; | - |
146 | generateGradientColorTable(gradient, buffer, paletteSize(), opacity); | - |
147 | funcs->glGenTextures(1, &cache_entry.texId); | - |
148 | funcs->glBindTexture(GL_TEXTURE_2D, cache_entry.texId); | - |
149 | funcs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, paletteSize(), 1, | - |
150 | 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); | - |
151 | return cache.insert(hash_val, cache_entry).value().texId; | - |
152 | } | - |
153 | | - |
154 | | - |
155 | | - |
156 | | - |
157 | static inline uint qtToGlColor(uint c) | - |
158 | { | - |
159 | uint o; | - |
160 | #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN | - |
161 | o = (c & 0xff00ff00) | - |
162 | | ((c >> 16) & 0x000000ff) | - |
163 | | ((c << 16) & 0x00ff0000); | - |
164 | #else //Q_BIG_ENDIAN | - |
165 | o = (c << 8) | - |
166 | | ((c >> 24) & 0x000000ff); | - |
167 | #endif // Q_BYTE_ORDER | - |
168 | return o; | - |
169 | } | - |
170 | | - |
171 | | - |
172 | void QGL2GradientCache::generateGradientColorTable(const QGradient& gradient, uint *colorTable, int size, qreal opacity) const | - |
173 | { | - |
174 | int pos = 0; | - |
175 | const QGradientStops s = gradient.stops(); | - |
| QVector<uint> colors(s.size()); | |
| | |
| for (int i = 0; i < s.size(); ++i) | |
| colors[i] = s[i].second.rgba(); | |
176 | bool colorInterpolation = (gradient.interpolationMode() == QGradient::ColorInterpolation); | - |
177 | | - |
178 | uint alpha = qRound(opacity * 256); | - |
179 | | - |
180 | uint current_color = ARGB_COMBINE_ALPHA(colorss[0],].second.rgba(), alpha); | - |
181 | qreal incr = 1.0 / qreal(size); | - |
182 | qreal fpos = 1.5 * incr; | - |
183 | colorTable[pos++] = qtToGlColor(qPremultiply(current_color)); | - |
184 | | - |
185 | while (fpos <= s.first().first) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
186 | colorTable[pos] = colorTable[pos - 1]; | - |
187 | pos++; | - |
188 | fpos += incr; | - |
189 | } never executed: end of block | 0 |
190 | | - |
191 | if (colorInterpolation)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
192 | current_color = qPremultiply(current_color); never executed: current_color = qPremultiply(current_color); | 0 |
193 | | - |
194 | const int sLast = s.size() - 1; | - |
195 | for (int i = 0; i < s.size() - 1sLast; ++i) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
196 | qreal delta = 1/(s[i+1].first - s[i].first); | - |
197 | uint next_color = ARGB_COMBINE_ALPHA(colorss[i + 1],].second.rgba(), alpha); | - |
198 | if (colorInterpolation)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
199 | next_color = qPremultiply(next_color); never executed: next_color = qPremultiply(next_color); | 0 |
200 | | - |
201 | while (fpos < s[i+1].first && pos < size) {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
202 | int dist = int(256 * ((fpos - s[i].first) * delta)); | - |
203 | int idist = 256 - dist; | - |
204 | if (colorInterpolation)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
205 | colorTable[pos] = qtToGlColor(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)); never executed: colorTable[pos] = qtToGlColor(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)); | 0 |
206 | else | - |
207 | colorTable[pos] = qtToGlColor(qPremultiply(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist))); never executed: colorTable[pos] = qtToGlColor(qPremultiply(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist))); | 0 |
208 | ++pos; | - |
209 | fpos += incr; | - |
210 | } never executed: end of block | 0 |
211 | current_color = next_color; | - |
212 | } never executed: end of block | 0 |
213 | | - |
214 | Q_ASSERT(s.size() > 0); | - |
215 | | - |
216 | uint last_color = qtToGlColor(qPremultiply(ARGB_COMBINE_ALPHA(colors[s[sLast].second.size() - 1],rgba(), alpha))); | - |
217 | for (;pos < size; ++pos)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
218 | colorTable[pos] = last_color; never executed: colorTable[pos] = last_color; | 0 |
219 | | - |
220 | | - |
221 | colorTable[size-1] = last_color; | - |
222 | } never executed: end of block | 0 |
223 | | - |
224 | QT_END_NAMESPACE | - |
| | |