aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickshapes/qquickshapecurverenderer_p.h
blob: f2b10fb44cc3a13369aada54f59b7694106f6104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QQUICKSHAPECURVERENDERER_P_H
#define QQUICKSHAPECURVERENDERER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of a number of Qt sources files.  This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//

#include <QtQuickShapes/private/qquickshapesglobal_p.h>
#include <QtQuickShapes/private/qquickshape_p_p.h>
#include <QtQuick/private/qquadpath_p.h>
#include <QtQuick/private/qsgcurveabstractnode_p.h>
#include <QtQuick/private/qsggradientcache_p.h>
#include <qsgnode.h>
#include <qsggeometry.h>
#include <qsgmaterial.h>
#include <qsgrendererinterface.h>
#include <qsgtexture.h>
#include <QtCore/qrunnable.h>
#include <QRunnable>

#include <QtGui/private/qtriangulator_p.h>
#include <QtQuick/private/qsgcurvefillnode_p.h>

QT_BEGIN_NAMESPACE

class QQuickShapeCurveRunnable;

class Q_QUICKSHAPES_EXPORT QQuickShapeCurveRenderer : public QQuickAbstractPathRenderer
{
public:
    QQuickShapeCurveRenderer(QQuickItem *item)
        : m_item(item)
    { }
    ~QQuickShapeCurveRenderer() override;

    void beginSync(int totalCount, bool *countChanged) override;
    void setPath(int index, const QQuickPath *path) override;
    void setPath(int index, const QPainterPath &path, QQuickShapePath::PathHints pathHints = {});
    void setStrokeColor(int index, const QColor &color) override;
    void setStrokeWidth(int index, qreal w) override;
    void setFillColor(int index, const QColor &color) override;
    void setFillRule(int index, QQuickShapePath::FillRule fillRule) override;
    void setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit) override;
    void setCapStyle(int index, QQuickShapePath::CapStyle capStyle) override;
    void setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle,
                        qreal dashOffset, const QVector<qreal> &dashPattern) override;
    void setFillGradient(int index, QQuickShapeGradient *gradient) override;
    void endSync(bool async) override;
    void setAsyncCallback(void (*)(void *), void *) override;
    Flags flags() const override { return SupportsAsync; }

    void updateNode() override;

    void setRootNode(QSGNode *node);

    using NodeList = QVector<QSGCurveAbstractNode *>;

    enum DirtyFlag
    {
        PathDirty = 0x01,
        FillDirty = 0x02,
        StrokeDirty = 0x04,
        UniformsDirty = 0x08
    };

    enum DebugVisualizationOption {
        NoDebug = 0,
        DebugCurves = 0x01,
        DebugWireframe = 0x02
    };

    static int debugVisualization();
    static void setDebugVisualization(int options);

private:
    struct PathData {

        bool isFillVisible() const { return fillColor.alpha() > 0 || gradientType != QGradient::NoGradient; }

        bool isStrokeVisible() const
        {
            return validPenWidth && pen.color().alpha() > 0 && pen.style() != Qt::NoPen;
        }

        QGradient::Type gradientType = QGradient::NoGradient;
        QSGGradientCache::GradientDesc gradient;
        QColor fillColor;
        Qt::FillRule fillRule = Qt::OddEvenFill;
        QPen pen;
        bool validPenWidth = true;
        int m_dirty = 0;
        QQuickShapePath::PathHints pathHints;

        QPainterPath originalPath;
        QQuadPath path;
        QQuadPath fillPath;
        QQuadPath strokePath;

        NodeList fillNodes;
        NodeList strokeNodes;

        QQuickShapeCurveRunnable *currentRunner = nullptr;
    };

    void createRunner(PathData *pathData);
    void maybeUpdateAsyncItem();

    static void processPath(PathData *pathData);
    static NodeList addFillNodes(const PathData &pathData);
    static NodeList addTriangulatingStrokerNodes(const PathData &pathData);
    static NodeList addCurveStrokeNodes(const PathData &pathData);

    void solveIntersections(QQuadPath &path);
    QQuickItem *m_item;
    QSGNode *m_rootNode = nullptr;
    QVector<PathData> m_paths;
    void (*m_asyncCallback)(void *) = nullptr;
    void *m_asyncCallbackData = nullptr;
    static int debugVisualizationFlags;

    friend class QQuickShapeCurveRunnable;
};

class QQuickShapeCurveRunnable : public QObject, public QRunnable
{
    Q_OBJECT

public:
    void run() override;

    bool isAsync = false;
    bool isDone = false;
    bool orphaned = false;

    // input / output
    QQuickShapeCurveRenderer::PathData pathData;

Q_SIGNALS:
    void done(QQuickShapeCurveRunnable *self);
};

QT_END_NAMESPACE

#endif // QQUICKSHAPECURVERENDERER_P_H