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::setVisible(int index, bool visible)
65{
66 ShapePathGuiData &d(m_sp[index]);
67 d.visible = visible;
68 d.dirty |= DirtyVisible;
69 m_accDirty |= DirtyVisible;
70}
71
72void QQuickShapeSoftwareRenderer::setFillRule(int index, QQuickShapePath::FillRule fillRule)
73{
74 ShapePathGuiData &d(m_sp[index]);
75 d.fillRule = Qt::FillRule(fillRule);
76 d.dirty |= DirtyFillRule;
77 m_accDirty |= DirtyFillRule;
78}
79
80void QQuickShapeSoftwareRenderer::setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit)
81{
82 ShapePathGuiData &d(m_sp[index]);
83 d.pen.setJoinStyle(Qt::PenJoinStyle(joinStyle));
84 d.pen.setMiterLimit(miterLimit);
85 d.dirty |= DirtyPen;
86 m_accDirty |= DirtyPen;
87}
88
89void QQuickShapeSoftwareRenderer::setCapStyle(int index, QQuickShapePath::CapStyle capStyle)
90{
91 ShapePathGuiData &d(m_sp[index]);
92 d.pen.setCapStyle(Qt::PenCapStyle(capStyle));
93 d.dirty |= DirtyPen;
94 m_accDirty |= DirtyPen;
95}
96
97void QQuickShapeSoftwareRenderer::setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle,
98 qreal dashOffset, const QList<qreal> &dashPattern)
99{
100 ShapePathGuiData &d(m_sp[index]);
101 switch (strokeStyle) {
102 case QQuickShapePath::SolidLine:
103 d.pen.setStyle(Qt::SolidLine);
104 break;
105 case QQuickShapePath::DashLine:
106 d.pen.setStyle(Qt::CustomDashLine);
107 d.pen.setDashPattern(dashPattern);
108 d.pen.setDashOffset(dashOffset);
109 break;
110 default:
111 break;
112 }
113 d.dirty |= DirtyPen;
114 m_accDirty |= DirtyPen;
115}
116
117static inline void setupPainterGradient(QGradient *painterGradient, const QQuickShapeGradient &g)
118{
119 painterGradient->setStops(g.gradientStops()); // sorted
120 switch (g.spread()) {
121 case QQuickShapeGradient::PadSpread:
122 painterGradient->setSpread(QGradient::PadSpread);
123 break;
124 case QQuickShapeGradient::RepeatSpread:
125 painterGradient->setSpread(QGradient::RepeatSpread);
126 break;
127 case QQuickShapeGradient::ReflectSpread:
128 painterGradient->setSpread(QGradient::ReflectSpread);
129 break;
130 default:
131 break;
132 }
133}
134
135static QBrush copyGradient(const QQuickShapeGradient *gradient, const QColor &color)
136{
137 if (const QQuickShapeLinearGradient *g = qobject_cast<const QQuickShapeLinearGradient *>(gradient)) {
138 QLinearGradient painterGradient(g->x1(), g->y1(), g->x2(), g->y2());
139 setupPainterGradient(&painterGradient, *g);
140 return QBrush(painterGradient);
141 } else if (const QQuickShapeRadialGradient *g = qobject_cast<const QQuickShapeRadialGradient *>(gradient)) {
142 QRadialGradient painterGradient(g->centerX(), g->centerY(), g->centerRadius(),
143 g->focalX(), g->focalY(), g->focalRadius());
144 setupPainterGradient(&painterGradient, *g);
145 return QBrush(painterGradient);
146 } else if (const QQuickShapeConicalGradient *g = qobject_cast<const QQuickShapeConicalGradient *>(gradient)) {
147 QConicalGradient painterGradient(g->centerX(), g->centerY(), g->angle());
148 setupPainterGradient(&painterGradient, *g);
149 return QBrush(painterGradient);
150 } else {
151 return QBrush(color);
152 }
153}
154
155void QQuickShapeSoftwareRenderer::setFillGradient(int index, QQuickShapeGradient *gradient)
156{
157 ShapePathGuiData &d(m_sp[index]);
158 d.brush = copyGradient(gradient, d.fillColor);
159 d.dirty |= DirtyBrush;
160 m_accDirty |= DirtyBrush;
161}
162
163void QQuickShapeSoftwareRenderer::setStrokeGradient(int index, QQuickShapeGradient *gradient)
164{
165 ShapePathGuiData &d(m_sp[index]);
166 d.pen.setBrush(copyGradient(gradient, d.pen.color()));
167 d.dirty |= DirtyPen;
168 m_accDirty |= DirtyPen;
169}
170
171void QQuickShapeSoftwareRenderer::setFillTextureProvider(int index, QQuickItem *textureProviderItem)
172{
173 Q_UNUSED(index);
174 Q_UNUSED(textureProviderItem);
175}
176
178{
179 Q_UNUSED(window);
180 // No action needed
181}
182
183void QQuickShapeSoftwareRenderer::setFillTransform(int index, const QSGTransform &transform)
184{
185 ShapePathGuiData &d(m_sp[index]);
186 if (!(transform.isIdentity() && d.brush.transform().isIdentity())) // No need to copy if both==I
187 d.brush.setTransform(transform.matrix().toTransform());
188 d.dirty |= DirtyBrush;
189 m_accDirty |= DirtyBrush;
190}
191
193{
194 ShapePathGuiData &d(m_sp[index]);
195 d.triangulationScale = scale;
196 d.dirty |= DirtyPen;
197 m_accDirty |= DirtyPen;
198}
199
203
205{
206 m_node = node;
207 m_accDirty |= DirtyList;
208}
209
211{
212 if (!m_accDirty)
213 return;
214
215 const int count = m_sp.size();
216 const bool listChanged = m_accDirty & DirtyList;
217 if (listChanged)
218 m_node->m_sp.resize(count);
219
220 m_node->m_boundingRect = QRectF();
221
222 for (int i = 0; i < count; ++i) {
223 ShapePathGuiData &src(m_sp[i]);
224 QQuickShapeSoftwareRenderNode::ShapePathRenderData &dst(m_node->m_sp[i]);
225
226 if (listChanged || (src.dirty & DirtyPath)) {
227 dst.path = src.path;
228 dst.path.setFillRule(src.fillRule);
229 }
230
231 if (listChanged || (src.dirty & DirtyFillRule))
232 dst.path.setFillRule(src.fillRule);
233
234 if (listChanged || (src.dirty & DirtyPen)) {
235 dst.pen = src.pen;
236 dst.strokeWidth = src.strokeWidth;
237 dst.triangulationScale = src.triangulationScale;
238 }
239
240 if (listChanged || (src.dirty & DirtyBrush))
241 dst.brush = src.brush;
242
243 if (listChanged || (src.dirty & DirtyVisible))
244 dst.visible = src.visible;
245
246 src.dirty = 0;
247
248 if (!dst.visible)
249 continue;
250
251 QRectF br = dst.path.boundingRect();
252 float sw = qMax(1.0f, dst.strokeWidth);
253 if (dst.pen.isCosmetic())
254 sw *= 2.0f / dst.triangulationScale;
255 br.adjust(-sw, -sw, sw, sw);
256 m_node->m_boundingRect |= br;
257 }
258
259 m_node->markDirty(QSGNode::DirtyMaterial);
260 m_accDirty = 0;
261}
262
267
272
276
277void QQuickShapeSoftwareRenderNode::render(const RenderState *state)
278{
279 if (m_sp.isEmpty())
280 return;
281
282 QSGRendererInterface *rif = m_item->window()->rendererInterface();
283 QPainter *p = static_cast<QPainter *>(rif->getResource(m_item->window(), QSGRendererInterface::PainterResource));
284 Q_ASSERT(p);
285
286 const QRegion *clipRegion = state->clipRegion();
287 if (clipRegion && !clipRegion->isEmpty())
288 p->setClipRegion(*clipRegion, Qt::ReplaceClip); // must be done before setTransform
289
290 p->setTransform(matrix()->toTransform());
291 p->setOpacity(inheritedOpacity());
292
293 for (const ShapePathRenderData &d : std::as_const(m_sp)) {
294 if (!d.visible)
295 continue;
296 p->setPen(d.strokeWidth > 0.0f && d.pen.color() != Qt::transparent ? d.pen : Qt::NoPen);
297 p->setBrush(d.brush.color() != Qt::transparent ? d.brush : Qt::NoBrush);
298 p->drawPath(d.path);
299 }
300}
301
306
308{
309 return BoundedRectRendering; // avoid fullscreen updates by saying we won't draw outside rect()
310}
311
313{
314 return m_boundingRect;
315}
316
317QT_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 setVisible(int index, bool visible) 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)