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
5#include <private/qquickpath_p_p.h>
6
8
9void QQuickShapeSoftwareRenderer::beginSync(int totalCount, bool *countChanged)
10{
11 if (m_sp.size() != totalCount) {
12 m_sp.resize(totalCount);
13 m_accDirty |= DirtyList;
14 *countChanged = true;
15 } else {
16 *countChanged = false;
17 }
18}
19
20void QQuickShapeSoftwareRenderer::setPath(int index, const QPainterPath &path, QQuickShapePath::PathHints)
21{
22 ShapePathGuiData &d(m_sp[index]);
23 d.path = path;
24 d.dirty |= DirtyPath;
25 m_accDirty |= DirtyPath;
26}
27
28void QQuickShapeSoftwareRenderer::setStrokeColor(int index, const QColor &color)
29{
30 ShapePathGuiData &d(m_sp[index]);
31 d.pen.setColor(color);
32 d.dirty |= DirtyPen;
33 m_accDirty |= DirtyPen;
34}
35
37{
38 ShapePathGuiData &d(m_sp[index]);
39 d.strokeWidth = w;
40 if (w > 0.0f)
41 d.pen.setWidthF(w);
42 d.dirty |= DirtyPen;
43 m_accDirty |= DirtyPen;
44}
45
47{
48 ShapePathGuiData &d(m_sp[index]);
49 d.pen.setCosmetic(c);
50 d.dirty |= DirtyPen;
51 m_accDirty |= DirtyPen;
52}
53
54void QQuickShapeSoftwareRenderer::setFillColor(int index, const QColor &color)
55{
56 ShapePathGuiData &d(m_sp[index]);
57 d.fillColor = color;
58 d.brush.setColor(color);
59 d.dirty |= DirtyBrush;
60 m_accDirty |= DirtyBrush;
61}
62
63void QQuickShapeSoftwareRenderer::setFillRule(int index, QQuickShapePath::FillRule fillRule)
64{
65 ShapePathGuiData &d(m_sp[index]);
66 d.fillRule = Qt::FillRule(fillRule);
67 d.dirty |= DirtyFillRule;
68 m_accDirty |= DirtyFillRule;
69}
70
71void QQuickShapeSoftwareRenderer::setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit)
72{
73 ShapePathGuiData &d(m_sp[index]);
74 d.pen.setJoinStyle(Qt::PenJoinStyle(joinStyle));
75 d.pen.setMiterLimit(miterLimit);
76 d.dirty |= DirtyPen;
77 m_accDirty |= DirtyPen;
78}
79
80void QQuickShapeSoftwareRenderer::setCapStyle(int index, QQuickShapePath::CapStyle capStyle)
81{
82 ShapePathGuiData &d(m_sp[index]);
83 d.pen.setCapStyle(Qt::PenCapStyle(capStyle));
84 d.dirty |= DirtyPen;
85 m_accDirty |= DirtyPen;
86}
87
88void QQuickShapeSoftwareRenderer::setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle,
89 qreal dashOffset, const QVector<qreal> &dashPattern)
90{
91 ShapePathGuiData &d(m_sp[index]);
92 switch (strokeStyle) {
93 case QQuickShapePath::SolidLine:
94 d.pen.setStyle(Qt::SolidLine);
95 break;
96 case QQuickShapePath::DashLine:
97 d.pen.setStyle(Qt::CustomDashLine);
98 d.pen.setDashPattern(dashPattern);
99 d.pen.setDashOffset(dashOffset);
100 break;
101 default:
102 break;
103 }
104 d.dirty |= DirtyPen;
105 m_accDirty |= DirtyPen;
106}
107
108static inline void setupPainterGradient(QGradient *painterGradient, const QQuickShapeGradient &g)
109{
110 painterGradient->setStops(g.gradientStops()); // sorted
111 switch (g.spread()) {
112 case QQuickShapeGradient::PadSpread:
113 painterGradient->setSpread(QGradient::PadSpread);
114 break;
115 case QQuickShapeGradient::RepeatSpread:
116 painterGradient->setSpread(QGradient::RepeatSpread);
117 break;
118 case QQuickShapeGradient::ReflectSpread:
119 painterGradient->setSpread(QGradient::ReflectSpread);
120 break;
121 default:
122 break;
123 }
124}
125
126void QQuickShapeSoftwareRenderer::setFillGradient(int index, QQuickShapeGradient *gradient)
127{
128 ShapePathGuiData &d(m_sp[index]);
129 if (QQuickShapeLinearGradient *g = qobject_cast<QQuickShapeLinearGradient *>(gradient)) {
130 QLinearGradient painterGradient(g->x1(), g->y1(), g->x2(), g->y2());
131 setupPainterGradient(&painterGradient, *g);
132 d.brush = QBrush(painterGradient);
133 } else if (QQuickShapeRadialGradient *g = qobject_cast<QQuickShapeRadialGradient *>(gradient)) {
134 QRadialGradient painterGradient(g->centerX(), g->centerY(), g->centerRadius(),
135 g->focalX(), g->focalY(), g->focalRadius());
136 setupPainterGradient(&painterGradient, *g);
137 d.brush = QBrush(painterGradient);
138 } else if (QQuickShapeConicalGradient *g = qobject_cast<QQuickShapeConicalGradient *>(gradient)) {
139 QConicalGradient painterGradient(g->centerX(), g->centerY(), g->angle());
140 setupPainterGradient(&painterGradient, *g);
141 d.brush = QBrush(painterGradient);
142 } else {
143 d.brush = QBrush(d.fillColor);
144 }
145 d.dirty |= DirtyBrush;
146 m_accDirty |= DirtyBrush;
147}
148
149void QQuickShapeSoftwareRenderer::setFillTextureProvider(int index, QQuickItem *textureProviderItem)
150{
151 Q_UNUSED(index);
152 Q_UNUSED(textureProviderItem);
153}
154
156{
157 Q_UNUSED(window);
158 // No action needed
159}
160
161void QQuickShapeSoftwareRenderer::setFillTransform(int index, const QSGTransform &transform)
162{
163 ShapePathGuiData &d(m_sp[index]);
164 if (!(transform.isIdentity() && d.brush.transform().isIdentity())) // No need to copy if both==I
165 d.brush.setTransform(transform.matrix().toTransform());
166 d.dirty |= DirtyBrush;
167 m_accDirty |= DirtyBrush;
168}
169
171{
172 m_triangulationScale = scale;
173}
174
178
180{
181 m_node = node;
182 m_accDirty |= DirtyList;
183}
184
186{
187 if (!m_accDirty)
188 return;
189
190 const int count = m_sp.size();
191 const bool listChanged = m_accDirty & DirtyList;
192 if (listChanged)
193 m_node->m_sp.resize(count);
194
195 m_node->m_boundingRect = QRectF();
196
197 for (int i = 0; i < count; ++i) {
198 ShapePathGuiData &src(m_sp[i]);
199 QQuickShapeSoftwareRenderNode::ShapePathRenderData &dst(m_node->m_sp[i]);
200
201 if (listChanged || (src.dirty & DirtyPath)) {
202 dst.path = src.path;
203 dst.path.setFillRule(src.fillRule);
204 }
205
206 if (listChanged || (src.dirty & DirtyFillRule))
207 dst.path.setFillRule(src.fillRule);
208
209 if (listChanged || (src.dirty & DirtyPen)) {
210 dst.pen = src.pen;
211 dst.strokeWidth = src.strokeWidth;
212 }
213
214 if (listChanged || (src.dirty & DirtyBrush))
215 dst.brush = src.brush;
216
217 src.dirty = 0;
218
219 QRectF br = dst.path.boundingRect();
220 float sw = qMax(1.0f, dst.strokeWidth);
221 if (dst.pen.isCosmetic())
222 sw *= 2.0f / m_triangulationScale;
223 br.adjust(-sw, -sw, sw, sw);
224 m_node->m_boundingRect |= br;
225 }
226
227 m_node->markDirty(QSGNode::DirtyMaterial);
228 m_accDirty = 0;
229}
230
235
240
244
245void QQuickShapeSoftwareRenderNode::render(const RenderState *state)
246{
247 if (m_sp.isEmpty())
248 return;
249
250 QSGRendererInterface *rif = m_item->window()->rendererInterface();
251 QPainter *p = static_cast<QPainter *>(rif->getResource(m_item->window(), QSGRendererInterface::PainterResource));
252 Q_ASSERT(p);
253
254 const QRegion *clipRegion = state->clipRegion();
255 if (clipRegion && !clipRegion->isEmpty())
256 p->setClipRegion(*clipRegion, Qt::ReplaceClip); // must be done before setTransform
257
258 p->setTransform(matrix()->toTransform());
259 p->setOpacity(inheritedOpacity());
260
261 for (const ShapePathRenderData &d : std::as_const(m_sp)) {
262 p->setPen(d.strokeWidth > 0.0f && d.pen.color() != Qt::transparent ? d.pen : Qt::NoPen);
263 p->setBrush(d.brush.color() != Qt::transparent ? d.brush : Qt::NoBrush);
264 p->drawPath(d.path);
265 }
266}
267
272
274{
275 return BoundedRectRendering; // avoid fullscreen updates by saying we won't draw outside rect()
276}
277
279{
280 return m_boundingRect;
281}
282
283QT_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 handleSceneChange(QQuickWindow *window) override
void setFillTextureProvider(int index, QQuickItem *textureProviderItem) override
void setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle, qreal dashOffset, const QVector< qreal > &dashPattern) override
void beginSync(int totalCount, bool *countChanged) override
void setCapStyle(int index, QQuickShapePath::CapStyle capStyle) override
void setCosmeticStroke(int index, bool c) override
void setTriangulationScale(qreal scale) 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
static void setupPainterGradient(QGradient *painterGradient, const QQuickShapeGradient &g)