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
127void QQuickShapeSoftwareRenderer::setFillGradient(int index, QQuickShapeGradient *gradient)
128{
129 ShapePathGuiData &d(m_sp[index]);
130 if (QQuickShapeLinearGradient *g = qobject_cast<QQuickShapeLinearGradient *>(gradient)) {
131 QLinearGradient painterGradient(g->x1(), g->y1(), g->x2(), g->y2());
132 setupPainterGradient(&painterGradient, *g);
133 d.brush = QBrush(painterGradient);
134 } else if (QQuickShapeRadialGradient *g = qobject_cast<QQuickShapeRadialGradient *>(gradient)) {
135 QRadialGradient painterGradient(g->centerX(), g->centerY(), g->centerRadius(),
136 g->focalX(), g->focalY(), g->focalRadius());
137 setupPainterGradient(&painterGradient, *g);
138 d.brush = QBrush(painterGradient);
139 } else if (QQuickShapeConicalGradient *g = qobject_cast<QQuickShapeConicalGradient *>(gradient)) {
140 QConicalGradient painterGradient(g->centerX(), g->centerY(), g->angle());
141 setupPainterGradient(&painterGradient, *g);
142 d.brush = QBrush(painterGradient);
143 } else {
144 d.brush = QBrush(d.fillColor);
145 }
146 d.dirty |= DirtyBrush;
147 m_accDirty |= DirtyBrush;
148}
149
150void QQuickShapeSoftwareRenderer::setFillTextureProvider(int index, QQuickItem *textureProviderItem)
151{
152 Q_UNUSED(index);
153 Q_UNUSED(textureProviderItem);
154}
155
157{
158 Q_UNUSED(window);
159 // No action needed
160}
161
162void QQuickShapeSoftwareRenderer::setFillTransform(int index, const QSGTransform &transform)
163{
164 ShapePathGuiData &d(m_sp[index]);
165 if (!(transform.isIdentity() && d.brush.transform().isIdentity())) // No need to copy if both==I
166 d.brush.setTransform(transform.matrix().toTransform());
167 d.dirty |= DirtyBrush;
168 m_accDirty |= DirtyBrush;
169}
170
172{
173 ShapePathGuiData &d(m_sp[index]);
174 d.triangulationScale = scale;
175 d.dirty |= DirtyPen;
176 m_accDirty |= DirtyPen;
177}
178
182
184{
185 m_node = node;
186 m_accDirty |= DirtyList;
187}
188
190{
191 if (!m_accDirty)
192 return;
193
194 const int count = m_sp.size();
195 const bool listChanged = m_accDirty & DirtyList;
196 if (listChanged)
197 m_node->m_sp.resize(count);
198
199 m_node->m_boundingRect = QRectF();
200
201 for (int i = 0; i < count; ++i) {
202 ShapePathGuiData &src(m_sp[i]);
203 QQuickShapeSoftwareRenderNode::ShapePathRenderData &dst(m_node->m_sp[i]);
204
205 if (listChanged || (src.dirty & DirtyPath)) {
206 dst.path = src.path;
207 dst.path.setFillRule(src.fillRule);
208 }
209
210 if (listChanged || (src.dirty & DirtyFillRule))
211 dst.path.setFillRule(src.fillRule);
212
213 if (listChanged || (src.dirty & DirtyPen)) {
214 dst.pen = src.pen;
215 dst.strokeWidth = src.strokeWidth;
216 dst.triangulationScale = src.triangulationScale;
217 }
218
219 if (listChanged || (src.dirty & DirtyBrush))
220 dst.brush = src.brush;
221
222 src.dirty = 0;
223
224 QRectF br = dst.path.boundingRect();
225 float sw = qMax(1.0f, dst.strokeWidth);
226 if (dst.pen.isCosmetic())
227 sw *= 2.0f / dst.triangulationScale;
228 br.adjust(-sw, -sw, sw, sw);
229 m_node->m_boundingRect |= br;
230 }
231
232 m_node->markDirty(QSGNode::DirtyMaterial);
233 m_accDirty = 0;
234}
235
240
245
249
250void QQuickShapeSoftwareRenderNode::render(const RenderState *state)
251{
252 if (m_sp.isEmpty())
253 return;
254
255 QSGRendererInterface *rif = m_item->window()->rendererInterface();
256 QPainter *p = static_cast<QPainter *>(rif->getResource(m_item->window(), QSGRendererInterface::PainterResource));
257 Q_ASSERT(p);
258
259 const QRegion *clipRegion = state->clipRegion();
260 if (clipRegion && !clipRegion->isEmpty())
261 p->setClipRegion(*clipRegion, Qt::ReplaceClip); // must be done before setTransform
262
263 p->setTransform(matrix()->toTransform());
264 p->setOpacity(inheritedOpacity());
265
266 for (const ShapePathRenderData &d : std::as_const(m_sp)) {
267 p->setPen(d.strokeWidth > 0.0f && d.pen.color() != Qt::transparent ? d.pen : Qt::NoPen);
268 p->setBrush(d.brush.color() != Qt::transparent ? d.brush : Qt::NoBrush);
269 p->drawPath(d.path);
270 }
271}
272
277
279{
280 return BoundedRectRendering; // avoid fullscreen updates by saying we won't draw outside rect()
281}
282
284{
285 return m_boundingRect;
286}
287
288QT_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 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 void setupPainterGradient(QGradient *painterGradient, const QQuickShapeGradient &g)