15 uint *colorTable,
int size,
float opacity)
18 const QGradientStops &s = gradient.stops;
19 Q_ASSERT(!s.isEmpty());
20 const bool colorInterpolation =
true;
22 uint alpha = qRound(opacity * 256);
23 uint current_color = ARGB_COMBINE_ALPHA(s[0].second.rgba(), alpha);
24 qreal incr = 1.0 / qreal(size);
25 qreal fpos = 1.5 * incr;
26 colorTable[pos++] = ARGB2RGBA(qPremultiply(current_color));
28 while (fpos <= s.first().first) {
29 colorTable[pos] = colorTable[pos - 1];
34 if (colorInterpolation)
35 current_color = qPremultiply(current_color);
37 const int sLast = s.size() - 1;
38 for (
int i = 0; i < sLast; ++i) {
39 qreal delta = 1/(s[i+1].first - s[i].first);
40 uint next_color = ARGB_COMBINE_ALPHA(s[i + 1].second.rgba(), alpha);
41 if (colorInterpolation)
42 next_color = qPremultiply(next_color);
44 while (fpos < s[i+1].first && pos < size) {
45 int dist =
int(256 * ((fpos - s[i].first) * delta));
46 int idist = 256 - dist;
47 if (colorInterpolation)
48 colorTable[pos] = ARGB2RGBA(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist));
50 colorTable[pos] = ARGB2RGBA(qPremultiply(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)));
54 current_color = next_color;
57 uint last_color = ARGB2RGBA(qPremultiply(ARGB_COMBINE_ALPHA(s[sLast].second.rgba(), alpha)));
58 for ( ; pos < size; ++pos)
59 colorTable[pos] = last_color;
61 colorTable[size-1] = last_color;
75QSGGradientCache *QSGGradientCache::cacheForRhi(QRhi *rhi)
77 static QHash<QRhi *, QSGGradientCache *> caches;
78 auto it = caches.constFind(rhi);
79 if (it != caches.constEnd())
82 QSGGradientCache *cache =
new QSGGradientCache;
83 rhi->addCleanupCallback([cache](QRhi *rhi) {
87 caches.insert(rhi, cache);
91QSGTexture *QSGGradientCache::get(
const QSGGradientCacheKey &grad)
93 QSGPlainTexture *tx =
nullptr;
107 const IndexHolder *ih = m_cache[grad];
109 tx = m_textures[ih->idx];
111 qsizetype newIdx = m_freeIndex;
113 newIdx = m_textures.size();
114 m_textures.append(
new QSGPlainTexture);
116 tx = m_textures[newIdx];
117 setTextureData(tx, grad);
118 m_cache.insert(grad,
new IndexHolder{newIdx, &m_freeIndex});
123void QSGGradientCache::setTextureData(QSGPlainTexture *tx,
const QSGGradientCacheKey &grad)
128 generateGradientColorTable(grad,
reinterpret_cast<uint *>(gradTab.bits()), W, 1.0f);