1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QQUICKSHAPENVPRRENDERER_P_H
41 #define QQUICKSHAPENVPRRENDERER_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists for the convenience
48 // of a number of Qt sources files.  This header file may change from
49 // version to version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtQuickShapes/private/qquickshapesglobal_p.h>
55 #include <QtQuickShapes/private/qquickshape_p_p.h>
56 #include <QtQuickShapes/private/qquicknvprfunctions_p.h>
57 #include <qsgrendernode.h>
58 #include <QColor>
59 #include <QVector4D>
60 #include <QDebug>
61 
62 #if QT_CONFIG(opengl)
63 
64 QT_BEGIN_NAMESPACE
65 
66 class QQuickShapeNvprRenderNode;
67 class QOpenGLFramebufferObject;
68 class QOpenGLBuffer;
69 class QOpenGLExtraFunctions;
70 
71 class QQuickShapeNvprRenderer : public QQuickAbstractPathRenderer
72 {
73 public:
74     enum Dirty {
75         DirtyPath = 0x01,
76         DirtyStyle = 0x02,
77         DirtyFillRule = 0x04,
78         DirtyDash = 0x08,
79         DirtyFillGradient = 0x10,
80         DirtyList = 0x20
81     };
82 
83     void beginSync(int totalCount) override;
84     void setPath(int index, const QQuickPath *path) override;
85     void setStrokeColor(int index, const QColor &color) override;
86     void setStrokeWidth(int index, qreal w) override;
87     void setFillColor(int index, const QColor &color) override;
88     void setFillRule(int index, QQuickShapePath::FillRule fillRule) override;
89     void setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit) override;
90     void setCapStyle(int index, QQuickShapePath::CapStyle capStyle) override;
91     void setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle,
92                         qreal dashOffset, const QVector<qreal> &dashPattern) override;
93     void setFillGradient(int index, QQuickShapeGradient *gradient) override;
94     void endSync(bool async) override;
95 
96     void updateNode() override;
97 
98     void setNode(QQuickShapeNvprRenderNode *node);
99 
100     struct NvprPath {
101         QVector<GLubyte> cmd;
102         QVector<GLfloat> coord;
103         QByteArray str;
104     };
105 
106 private:
107     struct ShapePathGuiData {
108         int dirty = 0;
109         NvprPath path;
110         qreal strokeWidth;
111         QColor strokeColor;
112         QColor fillColor;
113         QQuickShapePath::JoinStyle joinStyle;
114         int miterLimit;
115         QQuickShapePath::CapStyle capStyle;
116         QQuickShapePath::FillRule fillRule;
117         bool dashActive;
118         qreal dashOffset;
119         QVector<qreal> dashPattern;
120         FillGradientType fillGradientActive;
121         GradientDesc fillGradient;
122     };
123 
124     void convertPath(const QQuickPath *path, ShapePathGuiData *d);
125 
126     QQuickShapeNvprRenderNode *m_node = nullptr;
127     int m_accDirty = 0;
128 
129     QVector<ShapePathGuiData> m_sp;
130 };
131 
132 QDebug operator<<(QDebug debug, const QQuickShapeNvprRenderer::NvprPath &path);
133 
134 class QQuickNvprMaterialManager
135 {
136 public:
137     enum Material {
138         MatSolid,
139         MatLinearGradient,
140         MatRadialGradient,
141         MatConicalGradient,
142 
143         NMaterials
144     };
145 
146     struct MaterialDesc {
147         GLuint ppl = 0;
148         GLuint prg = 0;
149         int uniLoc[8];
150     };
151 
152     void create(QQuickNvprFunctions *nvpr);
153     MaterialDesc *activateMaterial(Material m);
154     void releaseResources();
155 
156 private:
157     QQuickNvprFunctions *m_nvpr = nullptr;
158     MaterialDesc m_materials[NMaterials];
159 };
160 
161 class QQuickNvprBlitter
162 {
163 public:
164     bool create();
165     void destroy();
isCreated()166     bool isCreated() const { return m_program != nullptr; }
167     void texturedQuad(GLuint textureId, const QSize &size,
168                       const QMatrix4x4 &proj, const QMatrix4x4 &modelview,
169                       float opacity);
170 
171 private:
172     QOpenGLShaderProgram *m_program = nullptr;
173     QOpenGLBuffer *m_buffer = nullptr;
174     int m_matrixLoc = -1;
175     int m_opacityLoc = -1;
176     QSize m_prevSize;
177 };
178 
179 class QQuickShapeNvprRenderNode : public QSGRenderNode
180 {
181 public:
182     ~QQuickShapeNvprRenderNode();
183 
184     void render(const RenderState *state) override;
185     void releaseResources() override;
186     StateFlags changedStates() const override;
187     RenderingFlags flags() const override;
188 
189     static bool isSupported();
190 
191 private:
192     struct ShapePathRenderData {
193         GLuint path = 0;
194         int dirty = 0;
195         QQuickShapeNvprRenderer::NvprPath source;
196         GLfloat strokeWidth;
197         QVector4D strokeColor;
198         QVector4D fillColor;
199         GLenum joinStyle;
200         GLint miterLimit;
201         GLenum capStyle;
202         GLenum fillRule;
203         GLfloat dashOffset;
204         QVector<GLfloat> dashPattern;
205         QQuickAbstractPathRenderer::FillGradientType fillGradientActive;
206         QQuickAbstractPathRenderer::GradientDesc fillGradient;
207         QOpenGLFramebufferObject *fallbackFbo = nullptr;
208         bool fallbackValid = false;
209         QSize fallbackSize;
210         QPointF fallbackTopLeft;
211 
hasFillShapePathRenderData212         bool hasFill() const { return !qFuzzyIsNull(fillColor.w()) || fillGradientActive; }
hasStrokeShapePathRenderData213         bool hasStroke() const { return strokeWidth >= 0.0f && !qFuzzyIsNull(strokeColor.w()); }
214     };
215 
216     void updatePath(ShapePathRenderData *d);
217     void renderStroke(ShapePathRenderData *d, int strokeStencilValue, int writeMask);
218     void renderFill(ShapePathRenderData *d);
219     void renderOffscreenFill(ShapePathRenderData *d);
220     void setupStencilForCover(bool stencilClip, int sv);
221 
222     static bool nvprInited;
223     static QQuickNvprFunctions nvpr;
224     static QQuickNvprMaterialManager mtlmgr;
225 
226     QQuickNvprBlitter m_fallbackBlitter;
227     QOpenGLExtraFunctions *f = nullptr;
228 
229     QVector<ShapePathRenderData> m_sp;
230 
231     friend class QQuickShapeNvprRenderer;
232 };
233 
234 QT_END_NAMESPACE
235 
236 #endif // QT_CONFIG(opengl)
237 
238 #endif // QQUICKSHAPENVPRRENDERER_P_H
239