Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qsggradientcache.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtGui/private/qdrawhelper_p.h>
7#include <QtGui/rhi/qrhi.h>
8
9#include <QtQuick/qsgtexture.h>
10#include <QtQuick/private/qsgplaintexture_p.h>
11
13
15 uint *colorTable, int size, float opacity)
16{
17 int pos = 0;
18 const QGradientStops &s = gradient.stops;
19 Q_ASSERT(!s.isEmpty());
20 const bool colorInterpolation = true;
21
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));
27
28 while (fpos <= s.first().first) {
29 colorTable[pos] = colorTable[pos - 1];
30 pos++;
31 fpos += incr;
32 }
33
34 if (colorInterpolation)
35 current_color = qPremultiply(current_color);
36
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);
43
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));
49 else
50 colorTable[pos] = ARGB2RGBA(qPremultiply(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)));
51 ++pos;
52 fpos += incr;
53 }
54 current_color = next_color;
55 }
56
57 uint last_color = ARGB2RGBA(qPremultiply(ARGB_COMBINE_ALPHA(s[sLast].second.rgba(), alpha)));
58 for ( ; pos < size; ++pos)
59 colorTable[pos] = last_color;
60
61 colorTable[size-1] = last_color;
62}
63
68
70{
71 static QHash<QRhi *, QSGGradientCache *> caches;
72 auto it = caches.constFind(rhi);
73 if (it != caches.constEnd())
74 return *it;
75
77 rhi->addCleanupCallback([cache](QRhi *rhi) {
78 caches.remove(rhi);
79 delete cache;
80 });
81 caches.insert(rhi, cache);
82 return cache;
83}
84
86{
87 QSGPlainTexture *tx = m_textures[grad];
88 if (!tx) {
89 static const int W = 1024; // texture size is 1024x1
91 if (!grad.stops.isEmpty())
92 generateGradientColorTable(grad, reinterpret_cast<uint *>(gradTab.bits()), W, 1.0f);
93 else
94 gradTab.fill(Qt::black);
95 tx = new QSGPlainTexture;
96 tx->setImage(gradTab);
97 switch (grad.spread) {
101 break;
105 break;
109 break;
110 default:
111 qWarning("Unknown gradient spread mode %d", grad.spread);
112 break;
113 }
115 m_textures[grad] = tx;
116 }
117 return tx;
118}
119
120
@ ReflectSpread
Definition qbrush.h:148
@ RepeatSpread
Definition qbrush.h:149
@ PadSpread
Definition qbrush.h:147
\inmodule QtGui
Definition qimage.h:37
@ Format_RGBA8888_Premultiplied
Definition qimage.h:60
bool isEmpty() const noexcept
Definition qlist.h:401
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1804
void addCleanupCallback(const CleanupCallback &callback)
Registers a callback that is invoked either when the QRhi is destroyed, or when runCleanup() is calle...
Definition qrhi.cpp:8809
QSGTexture * get(const QSGGradientCacheKey &grad)
static QSGGradientCache * cacheForRhi(QRhi *rhi)
void setImage(const QImage &image)
\inmodule QtQuick
Definition qsgtexture.h:20
void setHorizontalWrapMode(WrapMode hwrap)
Sets the horizontal wrap mode to hwrap.
void setFiltering(Filtering filter)
Sets the sampling mode to filter.
void setVerticalWrapMode(WrapMode vwrap)
Sets the vertical wrap mode to vwrap.
qDeleteAll(list.begin(), list.end())
QCache< int, Employee > cache
[0]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
@ black
Definition qnamespace.h:30
#define ARGB_COMBINE_ALPHA(argb, alpha)
static uint INTERPOLATE_PIXEL_256(uint x, uint a, uint y, uint b)
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
#define qWarning
Definition qlogging.h:166
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint first
GLdouble s
[6]
Definition qopenglext.h:235
GLfloat GLfloat GLfloat alpha
Definition qopenglext.h:418
static quint32 ARGB2RGBA(quint32 x)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
constexpr QRgb qPremultiply(QRgb x)
Definition qrgb.h:45
static QT_BEGIN_NAMESPACE void generateGradientColorTable(const QSGGradientCacheKey &gradient, uint *colorTable, int size, float opacity)
unsigned int uint
Definition qtypes.h:34
double qreal
Definition qtypes.h:187
std::uniform_real_distribution dist(1, 2.5)
[2]
QGradient::Spread spread
QThreadStorage< QCache< QString, SomeClass > > caches
[7]
Definition threads.cpp:43