| 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 QQUICKSHAPE_P_P_H |
| 41 | #define QQUICKSHAPE_P_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 purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // 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.h> |
| 56 | #include <QtQuick/private/qquickitem_p.h> |
| 57 | #include <QPainterPath> |
| 58 | #include <QColor> |
| 59 | #include <QBrush> |
| 60 | #include <QElapsedTimer> |
| 61 | #include <private/qopenglcontext_p.h> |
| 62 | |
| 63 | QT_BEGIN_NAMESPACE |
| 64 | |
| 65 | class QSGPlainTexture; |
| 66 | class QRhi; |
| 67 | |
| 68 | class QQuickAbstractPathRenderer |
| 69 | { |
| 70 | public: |
| 71 | enum Flag { |
| 72 | SupportsAsync = 0x01 |
| 73 | }; |
| 74 | Q_DECLARE_FLAGS(Flags, Flag) |
| 75 | |
| 76 | enum FillGradientType { NoGradient = 0, LinearGradient, RadialGradient, ConicalGradient }; |
| 77 | struct GradientDesc { // can fully describe a linear/radial/conical gradient |
| 78 | QGradientStops stops; |
| 79 | QQuickShapeGradient::SpreadMode spread; |
| 80 | QPointF a; // start (L) or center point (R/C) |
| 81 | QPointF b; // end (L) or focal point (R) |
| 82 | qreal v0; // center radius (R) or start angle (C) |
| 83 | qreal v1; // focal radius (R) |
| 84 | }; |
| 85 | |
| 86 | virtual ~QQuickAbstractPathRenderer() { } |
| 87 | |
| 88 | // Gui thread |
| 89 | virtual void beginSync(int totalCount) = 0; |
| 90 | virtual void endSync(bool async) = 0; |
| 91 | virtual void setAsyncCallback(void (*)(void *), void *) { } |
| 92 | virtual Flags flags() const { return {}; } |
| 93 | virtual void setPath(int index, const QQuickPath *path) = 0; |
| 94 | virtual void setStrokeColor(int index, const QColor &color) = 0; |
| 95 | virtual void setStrokeWidth(int index, qreal w) = 0; |
| 96 | virtual void setFillColor(int index, const QColor &color) = 0; |
| 97 | virtual void setFillRule(int index, QQuickShapePath::FillRule fillRule) = 0; |
| 98 | virtual void setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit) = 0; |
| 99 | virtual void setCapStyle(int index, QQuickShapePath::CapStyle capStyle) = 0; |
| 100 | virtual void setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle, |
| 101 | qreal dashOffset, const QVector<qreal> &dashPattern) = 0; |
| 102 | virtual void setFillGradient(int index, QQuickShapeGradient *gradient) = 0; |
| 103 | |
| 104 | // Render thread, with gui blocked |
| 105 | virtual void updateNode() = 0; |
| 106 | }; |
| 107 | |
| 108 | Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickAbstractPathRenderer::Flags) |
| 109 | |
| 110 | struct QQuickShapeStrokeFillParams |
| 111 | { |
| 112 | QQuickShapeStrokeFillParams(); |
| 113 | |
| 114 | QColor strokeColor; |
| 115 | qreal strokeWidth; |
| 116 | QColor fillColor; |
| 117 | QQuickShapePath::FillRule fillRule; |
| 118 | QQuickShapePath::JoinStyle joinStyle; |
| 119 | int miterLimit; |
| 120 | QQuickShapePath::CapStyle capStyle; |
| 121 | QQuickShapePath::StrokeStyle strokeStyle; |
| 122 | qreal dashOffset; |
| 123 | QVector<qreal> dashPattern; |
| 124 | QQuickShapeGradient *fillGradient; |
| 125 | }; |
| 126 | |
| 127 | class Q_QUICKSHAPES_PRIVATE_EXPORT QQuickShapePathPrivate : public QQuickPathPrivate |
| 128 | { |
| 129 | Q_DECLARE_PUBLIC(QQuickShapePath) |
| 130 | |
| 131 | public: |
| 132 | enum Dirty { |
| 133 | DirtyPath = 0x01, |
| 134 | DirtyStrokeColor = 0x02, |
| 135 | DirtyStrokeWidth = 0x04, |
| 136 | DirtyFillColor = 0x08, |
| 137 | DirtyFillRule = 0x10, |
| 138 | DirtyStyle = 0x20, |
| 139 | DirtyDash = 0x40, |
| 140 | DirtyFillGradient = 0x80, |
| 141 | |
| 142 | DirtyAll = 0xFF |
| 143 | }; |
| 144 | |
| 145 | QQuickShapePathPrivate(); |
| 146 | |
| 147 | void _q_pathChanged(); |
| 148 | void _q_fillGradientChanged(); |
| 149 | |
| 150 | static QQuickShapePathPrivate *get(QQuickShapePath *p) { return p->d_func(); } |
| 151 | |
| 152 | int dirty; |
| 153 | QQuickShapeStrokeFillParams sfp; |
| 154 | }; |
| 155 | |
| 156 | class QQuickShapePrivate : public QQuickItemPrivate |
| 157 | { |
| 158 | Q_DECLARE_PUBLIC(QQuickShape) |
| 159 | |
| 160 | public: |
| 161 | QQuickShapePrivate(); |
| 162 | ~QQuickShapePrivate(); |
| 163 | |
| 164 | void createRenderer(); |
| 165 | QSGNode *createNode(); |
| 166 | void sync(); |
| 167 | |
| 168 | void _q_shapePathChanged(); |
| 169 | void setStatus(QQuickShape::Status newStatus); |
| 170 | |
| 171 | static QQuickShapePrivate *get(QQuickShape *item) { return item->d_func(); } |
| 172 | |
| 173 | static void asyncShapeReady(void *data); |
| 174 | |
| 175 | int effectRefCount; |
| 176 | QVector<QQuickShapePath *> sp; |
| 177 | QElapsedTimer syncTimer; |
| 178 | QQuickAbstractPathRenderer *renderer = nullptr; |
| 179 | int syncTimingTotalDirty = 0; |
| 180 | int syncTimeCounter = 0; |
| 181 | QQuickShape::Status status = QQuickShape::Null; |
| 182 | QQuickShape::RendererType rendererType = QQuickShape::UnknownRenderer; |
| 183 | QQuickShape::ContainsMode containsMode = QQuickShape::BoundingRectContains; |
| 184 | bool spChanged = false; |
| 185 | bool async = false; |
| 186 | bool enableVendorExts = false; |
| 187 | bool syncTimingActive = false; |
| 188 | }; |
| 189 | |
| 190 | struct QQuickShapeGradientCacheKey |
| 191 | { |
| 192 | QQuickShapeGradientCacheKey(const QGradientStops &stops, QQuickShapeGradient::SpreadMode spread) |
| 193 | : stops(stops), spread(spread) |
| 194 | { } |
| 195 | QGradientStops stops; |
| 196 | QQuickShapeGradient::SpreadMode spread; |
| 197 | bool operator==(const QQuickShapeGradientCacheKey &other) const |
| 198 | { |
| 199 | return spread == other.spread && stops == other.stops; |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | inline uint qHash(const QQuickShapeGradientCacheKey &v, uint seed = 0) |
| 204 | { |
| 205 | uint h = seed + v.spread; |
| 206 | for (int i = 0; i < 3 && i < v.stops.count(); ++i) |
| 207 | h += v.stops[i].second.rgba(); |
| 208 | return h; |
| 209 | } |
| 210 | |
| 211 | class QQuickShapeGradientCache |
| 212 | { |
| 213 | public: |
| 214 | ~QQuickShapeGradientCache(); |
| 215 | static QQuickShapeGradientCache *cacheForRhi(QRhi *rhi); |
| 216 | QSGTexture *get(const QQuickShapeGradientCacheKey &grad); |
| 217 | |
| 218 | private: |
| 219 | QHash<QQuickShapeGradientCacheKey, QSGPlainTexture *> m_textures; |
| 220 | }; |
| 221 | |
| 222 | #if QT_CONFIG(opengl) |
| 223 | |
| 224 | class QQuickShapeGradientOpenGLCache : public QOpenGLSharedResource |
| 225 | { |
| 226 | public: |
| 227 | QQuickShapeGradientOpenGLCache(QOpenGLContext *context) : QOpenGLSharedResource(context->shareGroup()) { } |
| 228 | ~QQuickShapeGradientOpenGLCache(); |
| 229 | |
| 230 | void invalidateResource() override; |
| 231 | void freeResource(QOpenGLContext *) override; |
| 232 | |
| 233 | QSGTexture *get(const QQuickShapeGradientCacheKey &grad); |
| 234 | |
| 235 | static QQuickShapeGradientOpenGLCache *currentCache(); |
| 236 | |
| 237 | private: |
| 238 | QHash<QQuickShapeGradientCacheKey, QSGPlainTexture *> m_cache; |
| 239 | }; |
| 240 | |
| 241 | #endif // QT_CONFIG(opengl) |
| 242 | |
| 243 | QT_END_NAMESPACE |
| 244 | |
| 245 | #endif |
| 246 | |