Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | class QGL2GradientCacheWrapper | - |
5 | { | - |
6 | public: | - |
7 | QGL2GradientCache *cacheForContext(const QGLContext *context) { | - |
8 | QMutexLocker lock(&m_mutex); | - |
9 | return m_resource.value<QGL2GradientCache>(context->contextHandle()); | - |
10 | } | - |
11 | | - |
12 | private: | - |
13 | QOpenGLMultiGroupSharedResource m_resource; | - |
14 | QMutex m_mutex; | - |
15 | }; | - |
16 | | - |
17 | namespace { namespace Q_QGS_qt_gradient_caches { typedef QGL2GradientCacheWrapper Type; QBasicAtomicInt guard = { QtGlobalStatic::Uninitialized }; __attribute__((visibility("hidden"))) inline Type *innerFunction() { struct HolderBase { ~HolderBase() noexcept { if (guard.load() == QtGlobalStatic::Initialized) guard.store(QtGlobalStatic::Destroyed); } }; static struct Holder : public HolderBase { Type value; Holder() noexcept(noexcept(Type ())) : value () { guard.store(QtGlobalStatic::Initialized); } } holder; return &holder.value; } } } static QGlobalStatic<QGL2GradientCacheWrapper, Q_QGS_qt_gradient_caches::innerFunction, Q_QGS_qt_gradient_caches::guard> qt_gradient_caches; | - |
18 | | - |
19 | QGL2GradientCache::QGL2GradientCache(QOpenGLContext *ctx) | - |
20 | : QOpenGLSharedResource(ctx->shareGroup()) | - |
21 | { | - |
22 | } | - |
23 | | - |
24 | QGL2GradientCache::~QGL2GradientCache() | - |
25 | { | - |
26 | cache.clear(); | - |
27 | } | - |
28 | | - |
29 | QGL2GradientCache *QGL2GradientCache::cacheForContext(const QGLContext *context) | - |
30 | { | - |
31 | return qt_gradient_caches()->cacheForContext(context); | - |
32 | } | - |
33 | | - |
34 | void QGL2GradientCache::invalidateResource() | - |
35 | { | - |
36 | QMutexLocker lock(&m_mutex); | - |
37 | cache.clear(); | - |
38 | } | - |
39 | | - |
40 | void QGL2GradientCache::freeResource(QOpenGLContext *) | - |
41 | { | - |
42 | cleanCache(); | - |
43 | } | - |
44 | | - |
45 | void QGL2GradientCache::cleanCache() | - |
46 | { | - |
47 | QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); | - |
48 | QMutexLocker lock(&m_mutex); | - |
49 | QGLGradientColorTableHash::const_iterator it = cache.constBegin(); | - |
50 | for (; it != cache.constEnd(); ++it) { | - |
51 | const CacheInfo &cache_info = it.value(); | - |
52 | funcs->glDeleteTextures(1, &cache_info.texId); | - |
53 | } | - |
54 | cache.clear(); | - |
55 | } | - |
56 | | - |
57 | GLuint QGL2GradientCache::getBuffer(const QGradient &gradient, qreal opacity) | - |
58 | { | - |
59 | QMutexLocker lock(&m_mutex); | - |
60 | quint64 hash_val = 0; | - |
61 | | - |
62 | const QGradientStops stops = gradient.stops(); | - |
63 | for (int i = 0; i < stops.size()TRUE | never evaluated | FALSE | never evaluated |
&& i <= 2TRUE | never evaluated | FALSE | never evaluated |
; i++) | 0 |
64 | hash_val += stops[i].second.rgba(); never executed: hash_val += stops[i].second.rgba(); | 0 |
65 | | - |
66 | QGLGradientColorTableHash::const_iterator it = cache.constFind(hash_val); | - |
67 | | - |
68 | if (it == cache.constEnd()TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
69 | return never executed: return addCacheElement(hash_val, gradient, opacity); addCacheElement(hash_val, gradient, opacity);never executed: return addCacheElement(hash_val, gradient, opacity); | 0 |
70 | else { | - |
71 | do { | - |
72 | const CacheInfo &cache_info = it.value(); | - |
73 | if (cache_info.stops == stopsTRUE | never evaluated | FALSE | never evaluated |
&& cache_info.opacity == opacityTRUE | never evaluated | FALSE | never evaluated |
| 0 |
74 | && cache_info.interpolationMode == gradient.interpolationMode()TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
75 | { | - |
76 | return never executed: return cache_info.texId; cache_info.texId;never executed: return cache_info.texId; | 0 |
77 | } | - |
78 | ++it; | - |
79 | } never executed: end of block while (it != cache.constEnd()TRUE | never evaluated | FALSE | never evaluated |
&& it.key() == hash_valTRUE | never evaluated | FALSE | never evaluated |
); | 0 |
80 | | - |
81 | return never executed: return addCacheElement(hash_val, gradient, opacity); addCacheElement(hash_val, gradient, opacity);never executed: return addCacheElement(hash_val, gradient, opacity); | 0 |
82 | } | - |
83 | } | - |
84 | | - |
85 | | - |
86 | GLuint QGL2GradientCache::addCacheElement(quint64 hash_val, const QGradient &gradient, qreal opacity) | - |
87 | { | - |
88 | QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); | - |
89 | if (cache.size() == maxCacheSize()) { | - |
90 | int elem_to_remove = qrand() % maxCacheSize(); | - |
91 | quint64 key = cache.keys()[elem_to_remove]; | - |
92 | | - |
93 | | - |
94 | QGLGradientColorTableHash::const_iterator it = cache.constFind(key); | - |
95 | do { | - |
96 | funcs->glDeleteTextures(1, &it.value().texId); | - |
97 | } while (++it != cache.constEnd() && it.key() == key); | - |
98 | cache.remove(key); | - |
99 | } | - |
100 | | - |
101 | CacheInfo cache_entry(gradient.stops(), opacity, gradient.interpolationMode()); | - |
102 | uint buffer[1024]; | - |
103 | generateGradientColorTable(gradient, buffer, paletteSize(), opacity); | - |
104 | funcs->glGenTextures(1, &cache_entry.texId); | - |
105 | funcs->glBindTexture(0x0DE1, cache_entry.texId); | - |
106 | funcs->glTexImage2D(0x0DE1, 0, 0x1908, paletteSize(), 1, | - |
107 | 0, 0x1908, 0x1401, buffer); | - |
108 | return cache.insert(hash_val, cache_entry).value().texId; | - |
109 | } | - |
110 | | - |
111 | | - |
112 | | - |
113 | | - |
114 | static inline uint qtToGlColor(uint c) | - |
115 | { | - |
116 | uint o; | - |
117 | | - |
118 | o = (c & 0xff00ff00) | - |
119 | | ((c >> 16) & 0x000000ff) | - |
120 | | ((c << 16) & 0x00ff0000); | - |
121 | | - |
122 | | - |
123 | | - |
124 | | - |
125 | return o; | - |
126 | } | - |
127 | | - |
128 | | - |
129 | void QGL2GradientCache::generateGradientColorTable(const QGradient& gradient, uint *colorTable, int size, qreal opacity) const | - |
130 | { | - |
131 | int pos = 0; | - |
132 | const QGradientStops s = gradient.stops(); | - |
| QVector<uint> colors(s.size()); | |
| | |
| for (int i = 0; i < s.size(); ++i) | |
| colors[i] = s[i].second.rgba(); | |
133 | bool colorInterpolation = (gradient.interpolationMode() == QGradient::ColorInterpolation); | - |
134 | | - |
135 | uint alpha = qRound(opacity * 256); | - |
136 | | - |
137 | uint current_color = ((((colorss[0]].second.rgba() >> 24) * alpha) >> 8) << 24) | (colorss[0]].second.rgba() & 0x00ffffff); | - |
138 | qreal incr = 1.0 / qreal(size); | - |
139 | qreal fpos = 1.5 * incr; | - |
140 | colorTable[pos++] = qtToGlColor(qPremultiply(current_color)); | - |
141 | | - |
142 | while (fpos <= s.first().firstTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
143 | colorTable[pos] = colorTable[pos - 1]; | - |
144 | pos++; | - |
145 | fpos += incr; | - |
146 | } never executed: end of block | 0 |
147 | | - |
148 | if (colorInterpolationTRUE | never evaluated | FALSE | never evaluated |
) | 0 |
149 | current_color = qPremultiply(current_color); never executed: current_color = qPremultiply(current_color); | 0 |
150 | | - |
151 | const int sLast = s.size() - 1; | - |
152 | for (int i = 0; i < s.size() - 1sLastTRUE | never evaluated | FALSE | never evaluated |
; ++i) { | 0 |
153 | qreal delta = 1/(s[i+1].first - s[i].first); | - |
154 | uint next_color = ((((colorss[i + 1]].second.rgba() >> 24) * alpha) >> 8) << 24) | (colorss[i + 1]].second.rgba() & 0x00ffffff); | - |
155 | if (colorInterpolationTRUE | never evaluated | FALSE | never evaluated |
) | 0 |
156 | next_color = qPremultiply(next_color); never executed: next_color = qPremultiply(next_color); | 0 |
157 | | - |
158 | while (fpos < s[i+1].firstTRUE | never evaluated | FALSE | never evaluated |
&& pos < sizeTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
159 | int dist = int(256 * ((fpos - s[i].first) * delta)); | - |
160 | int idist = 256 - dist; | - |
161 | if (colorInterpolationTRUE | never evaluated | FALSE | never evaluated |
) | 0 |
162 | 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 |
163 | else | - |
164 | 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 |
165 | ++pos; | - |
166 | fpos += incr; | - |
167 | } never executed: end of block | 0 |
168 | current_color = next_color; | - |
169 | } never executed: end of block | 0 |
170 | | - |
171 | ((!(s.size() > 0)) ? qt_assert("s.size() > 0",__FILE__,211214) : qt_noop()); | - |
172 | | - |
173 | uint last_color = qtToGlColor(qPremultiply(((((colors[s[sLast].second.sizergba() - 1]>> 24) * alpha) >> 8) << 24) | (colors[s[sLast].second.sizergba()- 1] & 0x00ffffff))); | - |
174 | for (;pos < sizeTRUE | never evaluated | FALSE | never evaluated |
; ++pos) | 0 |
175 | colorTable[pos] = last_color; never executed: colorTable[pos] = last_color; | 0 |
176 | | - |
177 | | - |
178 | colorTable[size-1] = last_color; | - |
179 | } never executed: end of block | 0 |
180 | | - |
181 | | - |
| | |