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