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;
69QSGGradientCache *QSGGradientCache::cacheForRhi(QRhi *rhi)
71 static QHash<QRhi *, QSGGradientCache *> caches;
72 auto it = caches.constFind(rhi);
73 if (it != caches.constEnd())
76 QSGGradientCache *cache =
new QSGGradientCache;
77 rhi->addCleanupCallback([cache](QRhi *rhi) {
81 caches.insert(rhi, cache);
85QSGTexture *QSGGradientCache::get(
const QSGGradientCacheKey &grad)
87 QSGPlainTexture *tx = m_textures[grad];
89 static const int W = 1024;
90 QImage gradTab(W, 1, QImage::Format_RGBA8888_Premultiplied);
91 if (!grad.stops.isEmpty())
92 generateGradientColorTable(grad,
reinterpret_cast<uint *>(gradTab.bits()), W, 1.0f);
94 gradTab.fill(Qt::black);
95 tx =
new QSGPlainTexture;
96 tx->setImage(gradTab);
97 switch (grad.spread) {
98 case QGradient::PadSpread:
99 tx->setHorizontalWrapMode(QSGTexture::ClampToEdge);
100 tx->setVerticalWrapMode(QSGTexture::ClampToEdge);
102 case QGradient::RepeatSpread:
103 tx->setHorizontalWrapMode(QSGTexture::Repeat);
104 tx->setVerticalWrapMode(QSGTexture::Repeat);
106 case QGradient::ReflectSpread:
107 tx->setHorizontalWrapMode(QSGTexture::MirroredRepeat);
108 tx->setVerticalWrapMode(QSGTexture::MirroredRepeat);
111 qWarning(
"Unknown gradient spread mode %d", grad.spread);
114 tx->setFiltering(QSGTexture::Linear);
115 m_textures[grad] = tx;