1 | // Copyright (C) 2016 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 QQUICKSHAPE_P_P_H |
5 | #define QQUICKSHAPE_P_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 purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtQuickShapes/private/qquickshapesglobal_p.h> |
19 | #include <QtQuickShapes/private/qquickshape_p.h> |
20 | #include <QtQuick/private/qquickitem_p.h> |
21 | #include <QPainterPath> |
22 | #include <QColor> |
23 | #include <QBrush> |
24 | #include <QElapsedTimer> |
25 | #if QT_CONFIG(opengl) |
26 | # include <private/qopenglcontext_p.h> |
27 | #endif |
28 | QT_BEGIN_NAMESPACE |
29 | |
30 | class QSGPlainTexture; |
31 | class QRhi; |
32 | |
33 | class QQuickAbstractPathRenderer |
34 | { |
35 | public: |
36 | enum Flag { |
37 | SupportsAsync = 0x01 |
38 | }; |
39 | Q_DECLARE_FLAGS(Flags, Flag) |
40 | |
41 | enum FillGradientType { NoGradient = 0, LinearGradient, RadialGradient, ConicalGradient }; |
42 | struct GradientDesc { // can fully describe a linear/radial/conical gradient |
43 | QGradientStops stops; |
44 | QQuickShapeGradient::SpreadMode spread = QQuickShapeGradient::PadSpread; |
45 | QPointF a; // start (L) or center point (R/C) |
46 | QPointF b; // end (L) or focal point (R) |
47 | qreal v0; // center radius (R) or start angle (C) |
48 | qreal v1; // focal radius (R) |
49 | }; |
50 | |
51 | virtual ~QQuickAbstractPathRenderer() { } |
52 | |
53 | // Gui thread |
54 | virtual void beginSync(int totalCount, bool *countChanged) = 0; |
55 | virtual void endSync(bool async) = 0; |
56 | virtual void setAsyncCallback(void (*)(void *), void *) { } |
57 | virtual Flags flags() const { return {}; } |
58 | virtual void setPath(int index, const QQuickPath *path) = 0; |
59 | virtual void setStrokeColor(int index, const QColor &color) = 0; |
60 | virtual void setStrokeWidth(int index, qreal w) = 0; |
61 | virtual void setFillColor(int index, const QColor &color) = 0; |
62 | virtual void setFillRule(int index, QQuickShapePath::FillRule fillRule) = 0; |
63 | virtual void setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit) = 0; |
64 | virtual void setCapStyle(int index, QQuickShapePath::CapStyle capStyle) = 0; |
65 | virtual void setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle, |
66 | qreal dashOffset, const QVector<qreal> &dashPattern) = 0; |
67 | virtual void setFillGradient(int index, QQuickShapeGradient *gradient) = 0; |
68 | virtual void setTriangulationScale(qreal) { } |
69 | |
70 | // Render thread, with gui blocked |
71 | virtual void updateNode() = 0; |
72 | }; |
73 | |
74 | Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickAbstractPathRenderer::Flags) |
75 | |
76 | struct QQuickShapeStrokeFillParams |
77 | { |
78 | QQuickShapeStrokeFillParams(); |
79 | |
80 | QColor strokeColor; |
81 | qreal strokeWidth; |
82 | QColor fillColor; |
83 | QQuickShapePath::FillRule fillRule; |
84 | QQuickShapePath::JoinStyle joinStyle; |
85 | int miterLimit; |
86 | QQuickShapePath::CapStyle capStyle; |
87 | QQuickShapePath::StrokeStyle strokeStyle; |
88 | qreal dashOffset; |
89 | QVector<qreal> dashPattern; |
90 | QQuickShapeGradient *fillGradient; |
91 | }; |
92 | |
93 | class Q_QUICKSHAPES_PRIVATE_EXPORT QQuickShapePathPrivate : public QQuickPathPrivate |
94 | { |
95 | Q_DECLARE_PUBLIC(QQuickShapePath) |
96 | |
97 | public: |
98 | enum Dirty { |
99 | DirtyPath = 0x01, |
100 | DirtyStrokeColor = 0x02, |
101 | DirtyStrokeWidth = 0x04, |
102 | DirtyFillColor = 0x08, |
103 | DirtyFillRule = 0x10, |
104 | DirtyStyle = 0x20, |
105 | DirtyDash = 0x40, |
106 | DirtyFillGradient = 0x80, |
107 | |
108 | DirtyAll = 0xFF |
109 | }; |
110 | |
111 | QQuickShapePathPrivate(); |
112 | |
113 | void _q_pathChanged(); |
114 | void _q_fillGradientChanged(); |
115 | |
116 | static QQuickShapePathPrivate *get(QQuickShapePath *p) { return p->d_func(); } |
117 | |
118 | int dirty; |
119 | QQuickShapeStrokeFillParams sfp; |
120 | }; |
121 | |
122 | class QQuickShapePrivate : public QQuickItemPrivate |
123 | { |
124 | Q_DECLARE_PUBLIC(QQuickShape) |
125 | |
126 | public: |
127 | QQuickShapePrivate(); |
128 | ~QQuickShapePrivate(); |
129 | |
130 | QQuickShape::RendererType selectRendererType(); |
131 | void createRenderer(); |
132 | QSGNode *createNode(); |
133 | void sync(); |
134 | |
135 | void _q_shapePathChanged(); |
136 | void setStatus(QQuickShape::Status newStatus); |
137 | |
138 | static QQuickShapePrivate *get(QQuickShape *item) { return item->d_func(); } |
139 | |
140 | static void asyncShapeReady(void *data); |
141 | |
142 | int effectRefCount; |
143 | QVector<QQuickShapePath *> sp; |
144 | QElapsedTimer syncTimer; |
145 | QQuickAbstractPathRenderer *renderer = nullptr; |
146 | int syncTimingTotalDirty = 0; |
147 | int syncTimeCounter = 0; |
148 | QQuickShape::Status status = QQuickShape::Null; |
149 | QQuickShape::RendererType rendererType = QQuickShape::UnknownRenderer; |
150 | QQuickShape::RendererType preferredType = QQuickShape::UnknownRenderer; |
151 | QQuickShape::ContainsMode containsMode = QQuickShape::BoundingRectContains; |
152 | bool spChanged = false; |
153 | bool rendererChanged = false; |
154 | bool async = false; |
155 | bool enableVendorExts = false; |
156 | bool syncTimingActive = false; |
157 | qreal triangulationScale = 1.0; |
158 | }; |
159 | |
160 | struct QQuickShapeGradientCacheKey |
161 | { |
162 | QQuickShapeGradientCacheKey(const QGradientStops &stops, QQuickShapeGradient::SpreadMode spread) |
163 | : stops(stops), spread(spread) |
164 | { } |
165 | QGradientStops stops; |
166 | QQuickShapeGradient::SpreadMode spread; |
167 | bool operator==(const QQuickShapeGradientCacheKey &other) const |
168 | { |
169 | return spread == other.spread && stops == other.stops; |
170 | } |
171 | }; |
172 | |
173 | inline size_t qHash(const QQuickShapeGradientCacheKey &v, size_t seed = 0) |
174 | { |
175 | size_t h = seed + v.spread; |
176 | for (int i = 0; i < 3 && i < v.stops.size(); ++i) |
177 | h += v.stops[i].second.rgba(); |
178 | return h; |
179 | } |
180 | |
181 | class QQuickShapeGradientCache |
182 | { |
183 | public: |
184 | ~QQuickShapeGradientCache(); |
185 | static QQuickShapeGradientCache *cacheForRhi(QRhi *rhi); |
186 | QSGTexture *get(const QQuickShapeGradientCacheKey &grad); |
187 | |
188 | private: |
189 | QHash<QQuickShapeGradientCacheKey, QSGPlainTexture *> m_textures; |
190 | }; |
191 | |
192 | QT_END_NAMESPACE |
193 | |
194 | #endif |
195 | |