1// Copyright (C) 2024 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 QQUICKSHAPECURVERENDERER_P_H
5#define QQUICKSHAPECURVERENDERER_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 for the convenience
12// of a number of Qt sources files. This header file may change from
13// version to 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_p.h>
20#include <QtQuick/private/qquadpath_p.h>
21#include <QtQuick/private/qsgcurveabstractnode_p.h>
22#include <QtQuick/private/qsggradientcache_p.h>
23#include <qsgnode.h>
24#include <qsggeometry.h>
25#include <qsgmaterial.h>
26#include <qsgrendererinterface.h>
27#include <qsgtexture.h>
28#include <QtCore/qrunnable.h>
29#include <QRunnable>
30
31#include <QtGui/private/qtriangulator_p.h>
32#include <QtQuick/private/qsgcurvefillnode_p.h>
33
34QT_BEGIN_NAMESPACE
35
36class QQuickShapeCurveRunnable;
37
38class Q_QUICKSHAPES_EXPORT QQuickShapeCurveRenderer : public QQuickAbstractPathRenderer
39{
40public:
41 QQuickShapeCurveRenderer(QQuickItem *item)
42 : m_item(item)
43 { }
44 ~QQuickShapeCurveRenderer() override;
45
46 void beginSync(int totalCount, bool *countChanged) override;
47 void setPath(int index, const QPainterPath &path, QQuickShapePath::PathHints pathHints = {}) override;
48 void setStrokeColor(int index, const QColor &color) override;
49 void setStrokeWidth(int index, qreal w) override;
50 void setFillColor(int index, const QColor &color) override;
51 void setFillRule(int index, QQuickShapePath::FillRule fillRule) override;
52 void setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit) override;
53 void setCapStyle(int index, QQuickShapePath::CapStyle capStyle) override;
54 void setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle,
55 qreal dashOffset, const QVector<qreal> &dashPattern) override;
56 void setFillGradient(int index, QQuickShapeGradient *gradient) override;
57 void setFillTextureProvider(int index, QQuickItem *textureProviderItem) override;
58 void setFillTransform(int index, const QSGTransform &transform) override;
59 void endSync(bool async) override;
60 void setAsyncCallback(void (*)(void *), void *) override;
61 Flags flags() const override { return SupportsAsync; }
62 void handleSceneChange(QQuickWindow *window) override;
63
64 void updateNode() override;
65
66 void setRootNode(QSGNode *node);
67 void clearNodeReferences();
68
69 using NodeList = QVector<QSGCurveAbstractNode *>;
70
71 enum DirtyFlag
72 {
73 PathDirty = 0x01,
74 FillDirty = 0x02,
75 StrokeDirty = 0x04,
76 UniformsDirty = 0x08
77 };
78
79 enum DebugVisualizationOption {
80 NoDebug = 0,
81 DebugCurves = 0x01,
82 DebugWireframe = 0x02
83 };
84
85 static int debugVisualization();
86 static void setDebugVisualization(int options);
87
88private:
89 struct PathData {
90
91 bool isFillVisible() const
92 {
93 return gradientType != QGradient::NoGradient
94 || fillTextureProviderItem != nullptr
95 || fillColor.alpha() > 0;
96 }
97
98 bool isStrokeVisible() const
99 {
100 return validPenWidth && pen.color().alpha() > 0 && pen.style() != Qt::NoPen;
101 }
102
103 QGradient::Type gradientType = QGradient::NoGradient;
104 QSGGradientCache::GradientDesc gradient;
105 QSGTransform fillTransform;
106 QColor fillColor;
107 Qt::FillRule fillRule = Qt::OddEvenFill;
108 QPen pen;
109 bool validPenWidth = true;
110 int m_dirty = 0;
111 QQuickShapePath::PathHints pathHints;
112
113 QPainterPath originalPath;
114 QQuadPath path;
115 QQuadPath fillPath;
116
117 NodeList fillNodes;
118 NodeList strokeNodes;
119
120 QQuickShapeCurveRunnable *currentRunner = nullptr;
121 QQuickItem *fillTextureProviderItem = nullptr;
122 };
123
124 void setUpRunner(PathData *pathData);
125 void maybeUpdateAsyncItem();
126
127 static void processPath(PathData *pathData);
128 static NodeList addFillNodes(const QQuadPath &path);
129 static NodeList addTriangulatingStrokerNodes(const QQuadPath &path, const QPen &pen);
130 static NodeList addCurveStrokeNodes(const QQuadPath &path, const QPen &pen);
131
132 QQuickItem *m_item;
133 QSGNode *m_rootNode = nullptr;
134 QVector<PathData> m_paths;
135 QVector<PathData> m_removedPaths;
136 void (*m_asyncCallback)(void *) = nullptr;
137 void *m_asyncCallbackData = nullptr;
138 static int debugVisualizationFlags;
139
140 friend class QQuickShapeCurveRunnable;
141};
142
143class QQuickShapeCurveRunnable : public QObject, public QRunnable
144{
145 Q_OBJECT
146
147public:
148 ~QQuickShapeCurveRunnable() override;
149 void run() override;
150
151 bool isInitialized = false;
152 bool isAsync = false;
153 bool isDone = false;
154 bool orphaned = false;
155
156 // input / output
157 QQuickShapeCurveRenderer::PathData pathData;
158
159Q_SIGNALS:
160 void done(QQuickShapeCurveRunnable *self);
161};
162
163QT_END_NAMESPACE
164
165#endif // QQUICKSHAPECURVERENDERER_P_H
166

source code of qtdeclarative/src/quickshapes/qquickshapecurverenderer_p.h