Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsggradientcache_p.h
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
4#ifndef QSGGRADIENTCACHE_P_H
5#define QSGGRADIENTCACHE_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of a number of Qt sources files. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qhash.h>
19#include <QtCore/qcache.h>
20#include <QtGui/qbrush.h>
21
22#include <QtQuick/qtquickexports.h>
23
25
26class QSGTexture;
27class QSGPlainTexture;
28class QRhi;
29
30struct Q_QUICK_EXPORT QSGGradientCacheKey
31{
32 QSGGradientCacheKey(const QGradientStops &stops, QGradient::Spread spread)
33 : stops(stops), spread(spread)
34 { }
35 QGradientStops stops;
37 bool operator==(const QSGGradientCacheKey &other) const
38 {
39 return spread == other.spread && stops == other.stops;
40 }
41};
42
43inline size_t qHash(const QSGGradientCacheKey &v, size_t seed = 0)
44{
45 size_t h = qHash(size_t(v.spread), seed);
46 for (int i = 0; i < 2 && i < v.stops.size(); ++i)
47 h = qHash(v.stops[i].first, qHash(v.stops[i].second.rgba64(), h));
48 return h;
49}
50
51class Q_QUICK_EXPORT QSGGradientCache
52{
53public:
54 struct GradientDesc { // can fully describe a linear/radial/conical gradient
55 QGradientStops stops;
56 QGradient::Spread spread = QGradient::PadSpread;
57 QPointF a; // start (L) or center point (R/C)
58 QPointF b; // end (L) or focal point (R)
59 qreal v0; // center radius (R) or start angle (C)
60 qreal v1; // focal radius (R)
61
62 int compare(const GradientDesc &other) const {
63 if (int d = a.x() - other.a.x())
64 return d;
65 if (int d = a.y() - other.a.y())
66 return d;
67 if (int d = b.x() - other.b.x())
68 return d;
69 if (int d = b.y() - other.b.y())
70 return d;
71
72 if (int d = v0 - other.v0)
73 return d;
74 if (int d = v1 - other.v1)
75 return d;
76
77 if (int d = spread - other.spread)
78 return d;
79
80 if (int d = stops.size() - other.stops.size())
81 return d;
82
83 for (int i = 0; i < stops.size(); ++i) {
84 if (int d = stops[i].first - other.stops[i].first)
85 return d;
86 if (int d = stops[i].second.rgba() - other.stops[i].second.rgba())
87 return d;
88 }
89
90 return 0;
91 }
92 };
93
94 QSGGradientCache();
95 ~QSGGradientCache();
96 static QSGGradientCache *cacheForRhi(QRhi *rhi);
97 QSGTexture *get(const QSGGradientCacheKey &grad);
98
99private:
100 void setTextureData(QSGPlainTexture *tx, const QSGGradientCacheKey &grad);
101
102 struct IndexHolder {
103 ~IndexHolder() { if (freeIndex && idx >= 0) *freeIndex = idx; }
104 qsizetype idx = -1;
105 qsizetype *freeIndex = nullptr;
106 };
107 QList<QSGPlainTexture *> m_textures;
108 QCache<QSGGradientCacheKey, IndexHolder> m_cache;
109 qsizetype m_freeIndex = -1;
110};
111
112QT_END_NAMESPACE
113
114#endif // QSGGRADIENTCACHE_P_H
void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to prepare use of sampled images in the shader,...
QSGCurveFillMaterialShader(QGradient::Type gradientType, bool useTextureFill, bool useDerivatives, int viewCount)
bool updateUniformData(RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
Combined button and popup list for selecting options.
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:192
QGradient::Spread spread
bool operator==(const QSGGradientCacheKey &other) const