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
qquickshapesoftwarerenderer.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
6#include <private/qquickpath_p_p.h>
7
9
10void QQuickShapeSoftwareRenderer::beginSync(int totalCount, bool *countChanged)
11{
12 if (m_sp.size() != totalCount) {
13 m_sp.resize(totalCount);
14 m_accDirty |= DirtyList;
15 *countChanged = true;
16 } else {
17 *countChanged = false;
18 }
19}
20
21void QQuickShapeSoftwareRenderer::setPath(int index, const QPainterPath &path, QQuickShapePath::PathHints)
22{
23 ShapePathGuiData &d(m_sp[index]);
24 d.path = path;
25 d.dirty |= DirtyPath;
26 m_accDirty |= DirtyPath;
27}
28
29void QQuickShapeSoftwareRenderer::setStrokeColor(int index, const QColor &color)
30{
31 ShapePathGuiData &d(m_sp[index]);
32 d.pen.setColor(color);
33 d.dirty |= DirtyPen;
34 m_accDirty |= DirtyPen;
35}
36
38{
39 ShapePathGuiData &d(m_sp[index]);
40 d.strokeWidth = w;
41 if (w > 0.0f)
42 d.pen.setWidthF(w);
43 d.dirty |= DirtyPen;
44 m_accDirty |= DirtyPen;
45}
46
48{
49 ShapePathGuiData &d(m_sp[index]);
50 d.pen.setCosmetic(c);
51 d.dirty |= DirtyPen;
52 m_accDirty |= DirtyPen;
53}
54
55void QQuickShapeSoftwareRenderer::setFillColor(int index, const QColor &color)
56{
57 ShapePathGuiData &d(m_sp[index]);
58 d.fillColor = color;
59 d.brush.setColor(color);
60 d.dirty |= DirtyBrush;
61 m_accDirty |= DirtyBrush;
62}
63
64void QQuickShapeSoftwareRenderer::setFillRule(int index, QQuickShapePath::FillRule fillRule)
65{
66 ShapePathGuiData &d(m_sp[index]);
67 d.fillRule = Qt::FillRule(fillRule);
68 d.dirty |= DirtyFillRule;
69 m_accDirty |= DirtyFillRule;
70}
71
72void QQuickShapeSoftwareRenderer::setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit)
73{
74 ShapePathGuiData &d(m_sp[index]);
75 d.pen.setJoinStyle(Qt::PenJoinStyle(joinStyle));
76 d.pen.setMiterLimit(miterLimit);
77 d.dirty |= DirtyPen;
78 m_accDirty |= DirtyPen;
79}
80
81void QQuickShapeSoftwareRenderer::setCapStyle(int index, QQuickShapePath::CapStyle capStyle)
82{
83 ShapePathGuiData &d(m_sp[index]);
84 d.pen.setCapStyle(Qt::PenCapStyle(capStyle));
85 d.dirty |= DirtyPen;
86 m_accDirty |= DirtyPen;
87}
88
89void QQuickShapeSoftwareRenderer::setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle,
90 qreal dashOffset, const QList<qreal> &dashPattern)
91{
92 ShapePathGuiData &d(m_sp[index]);
93 switch (strokeStyle) {
94 case QQuickShapePath::SolidLine:
95 d.pen.setStyle(Qt::SolidLine);
96 break;
97 case QQuickShapePath::DashLine:
98 d.pen.setStyle(Qt::CustomDashLine);
99 d.pen.setDashPattern(dashPattern);
100 d.pen.setDashOffset(dashOffset);
101 break;
102 default:
103 break;
104 }
105 d.dirty |= DirtyPen;
106 m_accDirty |= DirtyPen;
107}
108
109static inline void setupPainterGradient(QGradient *painterGradient, const QQuickShapeGradient &g)
110{
111 painterGradient->setStops(g.gradientStops()); // sorted
112 switch (g.spread()) {
113 case QQuickShapeGradient::PadSpread:
114 painterGradient->setSpread(QGradient::PadSpread);
115 break;
116 case QQuickShapeGradient::RepeatSpread:
117 painterGradient->setSpread(QGradient::RepeatSpread);
118 break;
119 case QQuickShapeGradient::ReflectSpread:
120 painterGradient->setSpread(QGradient::ReflectSpread);
121 break;
122 default:
123 break;
124 }
125}
126
127static QBrush copyGradient(const QQuickShapeGradient *gradient, const QColor &color)
128{
129 if (const QQuickShapeLinearGradient *g = qobject_cast<const QQuickShapeLinearGradient *>(gradient)) {
130 QLinearGradient painterGradient(g->x1(), g->y1(), g->x2(), g->y2());
131 setupPainterGradient(&painterGradient, *g);
132 return QBrush(painterGradient);
133 } else if (const QQuickShapeRadialGradient *g = qobject_cast<const QQuickShapeRadialGradient *>(gradient)) {
134 QRadialGradient painterGradient(g->centerX(), g->centerY(), g->centerRadius(),
135 g->focalX(), g->focalY(), g->focalRadius());
136 setupPainterGradient(&painterGradient, *g);
137 return QBrush(painterGradient);
138 } else if (const QQuickShapeConicalGradient *g = qobject_cast<const QQuickShapeConicalGradient *>(gradient)) {
139 QConicalGradient painterGradient(g->centerX(), g->centerY(), g->angle());
140 setupPainterGradient(&painterGradient, *g);
141 return QBrush(painterGradient);
142 } else {
143 return QBrush(color);
144 }
145}
146
147void QQuickShapeSoftwareRenderer::setFillGradient(int index, QQuickShapeGradient *gradient)
148{
149 ShapePathGuiData &d(m_sp[index]);
150 d.brush = copyGradient(gradient, d.fillColor);
151 d.dirty |= DirtyBrush;
152 m_accDirty |= DirtyBrush;
153}
154
155void QQuickShapeSoftwareRenderer::setStrokeGradient(int index, QQuickShapeGradient *gradient)
156{
157 ShapePathGuiData &d(m_sp[index]);
158 d.pen.setBrush(copyGradient(gradient, d.pen.color()));
159 d.dirty |= DirtyPen;
160 m_accDirty |= DirtyPen;
161}
162
163void QQuickShapeSoftwareRenderer::setFillTextureProvider(int index, QQuickItem *textureProviderItem)
164{
165 Q_UNUSED(index);
166 Q_UNUSED(textureProviderItem);
167}
168
170{
171 Q_UNUSED(window);
172 // No action needed
173}
174
175void QQuickShapeSoftwareRenderer::setFillTransform(int index, const QSGTransform &transform)
176{
177 ShapePathGuiData &d(m_sp[index]);
178 if (!(transform.isIdentity() && d.brush.transform().isIdentity())) // No need to copy if both==I
179 d.brush.setTransform(transform.matrix().toTransform());
180 d.dirty |= DirtyBrush;
181 m_accDirty |= DirtyBrush;
182}
183
185{
186 ShapePathGuiData &d(m_sp[index]);
187 d.triangulationScale = scale;
188 d.dirty |= DirtyPen;
189 m_accDirty |= DirtyPen;
190}
191
195
197{
198 m_node = node;
199 m_accDirty |= DirtyList;
200}
201
203{
204 if (!m_accDirty)
205 return;
206
207 const int count = m_sp.size();
208 const bool listChanged = m_accDirty & DirtyList;
209 if (listChanged)
210 m_node->m_sp.resize(count);
211
212 m_node->m_boundingRect = QRectF();
213
214 for (int i = 0; i < count; ++i) {
215 ShapePathGuiData &src(m_sp[i]);
216 QQuickShapeSoftwareRenderNode::ShapePathRenderData &dst(m_node->m_sp[i]);
217
218 if (listChanged || (src.dirty & DirtyPath)) {
219 dst.path = src.path;
220 dst.path.setFillRule(src.fillRule);
221 }
222
223 if (listChanged || (src.dirty & DirtyFillRule))
224 dst.path.setFillRule(src.fillRule);
225
226 if (listChanged || (src.dirty & DirtyPen)) {
227 dst.pen = src.pen;
228 dst.strokeWidth = src.strokeWidth;
229 dst.triangulationScale = src.triangulationScale;
230 }
231
232 if (listChanged || (src.dirty & DirtyBrush))
233 dst.brush = src.brush;
234
235 src.dirty = 0;
236
237 QRectF br = dst.path.boundingRect();
238 float sw = qMax(1.0f, dst.strokeWidth);
239 if (dst.pen.isCosmetic())
240 sw *= 2.0f / dst.triangulationScale;
241 br.adjust(-sw, -sw, sw, sw);
242 m_node->m_boundingRect |= br;
243 }
244
245 m_node->markDirty(QSGNode::DirtyMaterial);
246 m_accDirty = 0;
247}
248
253
258
262
263void QQuickShapeSoftwareRenderNode::render(const RenderState *state)
264{
265 if (m_sp.isEmpty())
266 return;
267
268 QSGRendererInterface *rif = m_item->window()->rendererInterface();
269 QPainter *p = static_cast<QPainter *>(rif->getResource(m_item->window(), QSGRendererInterface::PainterResource));
270 Q_ASSERT(p);
271
272 const QRegion *clipRegion = state->clipRegion();
273 if (clipRegion && !clipRegion->isEmpty())
274 p->setClipRegion(*clipRegion, Qt::ReplaceClip); // must be done before setTransform
275
276 p->setTransform(matrix()->toTransform());
277 p->setOpacity(inheritedOpacity());
278
279 for (const ShapePathRenderData &d : std::as_const(m_sp)) {
280 p->setPen(d.strokeWidth > 0.0f && d.pen.color() != Qt::transparent ? d.pen : Qt::NoPen);
281 p->setBrush(d.brush.color() != Qt::transparent ? d.brush : Qt::NoBrush);
282 p->drawPath(d.path);
283 }
284}
285
290
292{
293 return BoundedRectRendering; // avoid fullscreen updates by saying we won't draw outside rect()
294}
295
297{
298 return m_boundingRect;
299}
300
301QT_END_NAMESPACE
RenderingFlags flags() const override
void releaseResources() override
This function is called when all custom graphics resources allocated by this node have to be freed im...
void render(const RenderState *state) override
This function is called by the renderer and should paint this node with directly invoking commands vi...
StateFlags changedStates() const override
This function should return a mask where each bit represents graphics states changed by the \l render...
void setPath(int index, const QPainterPath &path, QQuickShapePath::PathHints pathHints={}) override
void setStrokeColor(int index, const QColor &color) override
void setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit) override
void setStrokeGradient(int index, QQuickShapeGradient *gradient) override
void setFillColor(int index, const QColor &color) override
void setTriangulationScale(int index, qreal scale) override
void handleSceneChange(QQuickWindow *window) override
void setFillTextureProvider(int index, QQuickItem *textureProviderItem) override
void beginSync(int totalCount, bool *countChanged) override
void setCapStyle(int index, QQuickShapePath::CapStyle capStyle) override
void setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle, qreal dashOffset, const QList< qreal > &dashPattern) override
void setCosmeticStroke(int index, bool c) override
void setFillGradient(int index, QQuickShapeGradient *gradient) override
void setFillRule(int index, QQuickShapePath::FillRule fillRule) override
void setStrokeWidth(int index, qreal w) override
void setFillTransform(int index, const QSGTransform &transform) override
Combined button and popup list for selecting options.
static QBrush copyGradient(const QQuickShapeGradient *gradient, const QColor &color)
static void setupPainterGradient(QGradient *painterGradient, const QQuickShapeGradient &g)