From c9023c28764e70cd1c6f9cfc3506e6185299548e Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Sat, 3 Dec 2016 21:30:12 +0100 Subject: Add QQuickPathItem and its backend infra The generic backend uses the triangulator from QtGui, but is in fact OpenGL-only for now due to materials. The NVPR backend uses GL_NV_path_rendering on NVIDIA hardware with OpenGL 4.3+ or OpenGL ES 3.1+. The software backend simply uses QPainter. With the generic backend each PathItem is backed by a non-visual root node and 0, 1 or 2 child geometry nodes, depending on the presence of visible stroking and filling. The potentially expensive triangulation happens on updatePolish(), on the gui thread. This is proven to provide much smoother results when compared to doing the geometry generation on the render thread in updatePaintNode(), in particular on power-limited embedded devices. The NVPR backend uses a QSGRenderNode in DepthAware mode so that the batch renderer can continue to rely on the depth buffer and use opaque batches. Due to not relying on slow CPU-side triangulation, this backend uses 5-10 times less CPU, even when properties of the path or its elements are animated. The path itself is specified with the PathView's Path, PathLine, PathArc, PathQuad, etc. types. This allows for consistency with PathView and the 2D Canvas and avoids a naming mess in the API. However, there won't be a 100% symmetry: backends like NVPR will not rely on QPainterPath but process the path elements on their own (as QPainterPath is essentially useless with these APIs), which can lead to differences in the supported path elements. The supported common set is currently Move, Line, Quad, Cubic, Arc. The patch introduces PathMove, which is essentially PathLine but maps to moveTo instead of lineTo. More types may get added later (e.g. NVPR can do a wide variety of optimized rounded rects, but this requires directly specifying a GL_ROUNDED_RECTx_NV command, thus neededing a dedicated Path type on our side too) For filling with gradients only linear gradients are supported at the moment. In addition to the declarative API, a more lightweight, QObject-less JS-callable API should be considered as well for the future. Change-Id: I335ad64b425ee279505d60e3e57ac6841e1cbd24 Reviewed-by: Andy Nichols --- src/quick/items/items.pri | 15 +- src/quick/items/items.qrc | 4 + src/quick/items/qquickitemsmodule.cpp | 9 + src/quick/items/qquickpathitem.cpp | 792 +++++++++++++++++++++ src/quick/items/qquickpathitem_p.h | 281 ++++++++ src/quick/items/qquickpathitem_p_p.h | 177 +++++ src/quick/items/qquickpathitemgenericrenderer.cpp | 510 +++++++++++++ src/quick/items/qquickpathitemgenericrenderer_p.h | 232 ++++++ src/quick/items/qquickpathitemnvprrenderer.cpp | 567 +++++++++++++++ src/quick/items/qquickpathitemnvprrenderer_p.h | 194 +++++ src/quick/items/qquickpathitemsoftwarerenderer.cpp | 234 ++++++ src/quick/items/qquickpathitemsoftwarerenderer_p.h | 127 ++++ src/quick/items/shaders/lineargradient.frag | 9 + src/quick/items/shaders/lineargradient.vert | 15 + src/quick/items/shaders/lineargradient_core.frag | 12 + src/quick/items/shaders/lineargradient_core.vert | 17 + 16 files changed, 3193 insertions(+), 2 deletions(-) create mode 100644 src/quick/items/qquickpathitem.cpp create mode 100644 src/quick/items/qquickpathitem_p.h create mode 100644 src/quick/items/qquickpathitem_p_p.h create mode 100644 src/quick/items/qquickpathitemgenericrenderer.cpp create mode 100644 src/quick/items/qquickpathitemgenericrenderer_p.h create mode 100644 src/quick/items/qquickpathitemnvprrenderer.cpp create mode 100644 src/quick/items/qquickpathitemnvprrenderer_p.h create mode 100644 src/quick/items/qquickpathitemsoftwarerenderer.cpp create mode 100644 src/quick/items/qquickpathitemsoftwarerenderer_p.h create mode 100644 src/quick/items/shaders/lineargradient.frag create mode 100644 src/quick/items/shaders/lineargradient.vert create mode 100644 src/quick/items/shaders/lineargradient_core.frag create mode 100644 src/quick/items/shaders/lineargradient_core.vert (limited to 'src/quick/items') diff --git a/src/quick/items/items.pri b/src/quick/items/items.pri index 0f8061b5ef..511c6f18d8 100644 --- a/src/quick/items/items.pri +++ b/src/quick/items/items.pri @@ -148,9 +148,20 @@ qtConfig(quick-listview) { qtConfig(quick-pathview) { HEADERS += \ $$PWD/qquickpathview_p.h \ - $$PWD/qquickpathview_p_p.h + $$PWD/qquickpathview_p_p.h \ + $$PWD/qquickpathitem_p.h \ + $$PWD/qquickpathitem_p_p.h \ + $$PWD/qquickpathitemgenericrenderer_p.h \ + $$PWD/qquickpathitemsoftwarerenderer_p.h SOURCES += \ - $$PWD/qquickpathview.cpp + $$PWD/qquickpathview.cpp \ + $$PWD/qquickpathitem.cpp \ + $$PWD/qquickpathitemgenericrenderer.cpp \ + $$PWD/qquickpathitemsoftwarerenderer.cpp + qtConfig(opengl) { + HEADERS += $$PWD/qquickpathitemnvprrenderer_p.h + SOURCES += $$PWD/qquickpathitemnvprrenderer.cpp + } } qtConfig(quick-positioners) { diff --git a/src/quick/items/items.qrc b/src/quick/items/items.qrc index 6aaf757c29..da9bf0c828 100644 --- a/src/quick/items/items.qrc +++ b/src/quick/items/items.qrc @@ -8,5 +8,9 @@ shaders/shadereffect_core.vert shaders/shadereffectfallback_core.frag shaders/shadereffectfallback_core.vert + shaders/lineargradient.vert + shaders/lineargradient.frag + shaders/lineargradient_core.vert + shaders/lineargradient_core.frag diff --git a/src/quick/items/qquickitemsmodule.cpp b/src/quick/items/qquickitemsmodule.cpp index dbe30fbc83..b0d7f7f8a3 100644 --- a/src/quick/items/qquickitemsmodule.cpp +++ b/src/quick/items/qquickitemsmodule.cpp @@ -70,6 +70,7 @@ #if QT_CONFIG(quick_path) #include #include +#include "qquickpathitem_p.h" #endif #if QT_CONFIG(quick_positioners) #include "qquickpositioners_p.h" @@ -370,6 +371,14 @@ static void qt_quickitems_defineModule(const char *uri, int major, int minor) #endif qmlRegisterType(uri, 2, 9, "MouseArea"); + +#if QT_CONFIG(quick_path) + qmlRegisterType(uri, 2, 9, "PathMove"); + qmlRegisterType(uri, 2, 9, "PathItem"); + qmlRegisterType(uri, 2, 9, "PathGradientStop"); + qmlRegisterUncreatableType(uri, 2, 9, "PathGradient", QQuickPathGradient::tr("PathGradient is an abstract base class")); + qmlRegisterType(uri, 2, 9, "PathLinearGradient"); +#endif } static void initResources() diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp new file mode 100644 index 0000000000..0d2c7a8bbe --- /dev/null +++ b/src/quick/items/qquickpathitem.cpp @@ -0,0 +1,792 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickpathitem_p.h" +#include "qquickpathitem_p_p.h" +#include "qquickpathitemgenericrenderer_p.h" +#include "qquickpathitemnvprrenderer_p.h" +#include "qquickpathitemsoftwarerenderer_p.h" +#include +#include +#include + +QT_BEGIN_NAMESPACE + +QQuickPathItemPrivate::QQuickPathItemPrivate() + : rendererType(QQuickPathItem::UnknownRenderer), + renderer(nullptr), + path(nullptr), + dirty(DirtyAll), + strokeColor(Qt::white), + strokeWidth(1), + fillColor(Qt::white), + fillRule(QQuickPathItem::OddEvenFill), + joinStyle(QQuickPathItem::BevelJoin), + miterLimit(2), + capStyle(QQuickPathItem::SquareCap), + strokeStyle(QQuickPathItem::SolidLine), + dashOffset(0), + fillGradient(nullptr) +{ + dashPattern << 4 << 2; // 4 * strokeWidth dash followed by 2 * strokeWidth space +} + +QQuickPathItemPrivate::~QQuickPathItemPrivate() +{ + delete renderer; +} + +/*! + \qmltype PathItem + \instantiates QQuickPathItem + \inqmlmodule QtQuick + \ingroup qtquick-paths + \ingroup qtquick-views + \inherits Item + \brief Renders a path + + Renders a path either by generating geometry via QPainterPath and manual + triangulation or by using an extension like \c{GL_NV_path_rendering}. + + This approach is different from rendering shapes via QQuickPaintedItem or + the 2D Canvas because the path never gets rasterized in software. Therefore + it is suitable for creating shapes spreading over larger areas of the + screen, avoiding the performance penalty for texture uploads or framebuffer + blits. + + Nonetheless it is important to be aware of performance implications, in + particular when the application is running on the generic PathItem + implementation due to not having support for accelerated path rendering. + The geometry generation happens entirely on the CPU in this case, and this + is potentially expensive. Changing the set of path elements, changing the + properties of these elements, or changing certain properties of the + PathItem itself all lead to retriangulation on every change. Therefore, + applying animation to such properties can heavily affect performance on + less powerful systems. If animating properties other than stroke and fill + colors is a must, it is recommended to target systems providing + \c{GL_NV_path_rendering} where the cost of path property changes is much + smaller. + + \note The types for specifying path elements are shared between PathView + and PathItem. However, not all PathItem implementations support all path + element types, while some may not make sense for PathView. PathItem's + currently supported subset is: PathMove, PathLine, PathQuad, PathCubic, + PathArc. + + \sa Path, PathMove, PathLine, PathQuad, PathCubic, PathArc +*/ + +QQuickPathItem::QQuickPathItem(QQuickItem *parent) + : QQuickItem(*(new QQuickPathItemPrivate), parent) +{ + setFlag(ItemHasContents); +} + +QQuickPathItem::~QQuickPathItem() +{ +} + +QQuickPathItem::RendererType QQuickPathItem::rendererType() const +{ + Q_D(const QQuickPathItem); + return d->rendererType; +} + +/*! + \qmlproperty Path QtQuick::PathItem::path + This property holds the path to be rendered. + For more information see the \l Path documentation. +*/ +QQuickPath *QQuickPathItem::path() const +{ + Q_D(const QQuickPathItem); + return d->path; +} + +void QQuickPathItem::setPath(QQuickPath *path) +{ + Q_D(QQuickPathItem); + if (d->path == path) + return; + + if (d->path) + qmlobject_disconnect(d->path, QQuickPath, SIGNAL(changed()), + this, QQuickPathItem, SLOT(_q_pathChanged())); + d->path = path; + qmlobject_connect(d->path, QQuickPath, SIGNAL(changed()), + this, QQuickPathItem, SLOT(_q_pathChanged())); + + d->dirty |= QQuickPathItemPrivate::DirtyPath; + emit pathChanged(); + polish(); +} + +void QQuickPathItemPrivate::_q_pathChanged() +{ + Q_Q(QQuickPathItem); + dirty |= DirtyPath; + q->polish(); +} + +QColor QQuickPathItem::strokeColor() const +{ + Q_D(const QQuickPathItem); + return d->strokeColor; +} + +void QQuickPathItem::setStrokeColor(const QColor &color) +{ + Q_D(QQuickPathItem); + if (d->strokeColor != color) { + d->strokeColor = color; + d->dirty |= QQuickPathItemPrivate::DirtyStrokeColor; + emit strokeColorChanged(); + polish(); + } +} + +qreal QQuickPathItem::strokeWidth() const +{ + Q_D(const QQuickPathItem); + return d->strokeWidth; +} + +void QQuickPathItem::setStrokeWidth(qreal w) +{ + Q_D(QQuickPathItem); + if (d->strokeWidth != w) { + d->strokeWidth = w; + d->dirty |= QQuickPathItemPrivate::DirtyStrokeWidth; + emit strokeWidthChanged(); + polish(); + } +} + +QColor QQuickPathItem::fillColor() const +{ + Q_D(const QQuickPathItem); + return d->fillColor; +} + +void QQuickPathItem::setFillColor(const QColor &color) +{ + Q_D(QQuickPathItem); + if (d->fillColor != color) { + d->fillColor = color; + d->dirty |= QQuickPathItemPrivate::DirtyFillColor; + emit fillColorChanged(); + polish(); + } +} + +QQuickPathItem::FillRule QQuickPathItem::fillRule() const +{ + Q_D(const QQuickPathItem); + return d->fillRule; +} + +void QQuickPathItem::setFillRule(FillRule fillRule) +{ + Q_D(QQuickPathItem); + if (d->fillRule != fillRule) { + d->fillRule = fillRule; + d->dirty |= QQuickPathItemPrivate::DirtyFillRule; + emit fillRuleChanged(); + polish(); + } +} + +QQuickPathItem::JoinStyle QQuickPathItem::joinStyle() const +{ + Q_D(const QQuickPathItem); + return d->joinStyle; +} + +void QQuickPathItem::setJoinStyle(JoinStyle style) +{ + Q_D(QQuickPathItem); + if (d->joinStyle != style) { + d->joinStyle = style; + d->dirty |= QQuickPathItemPrivate::DirtyStyle; + emit joinStyleChanged(); + polish(); + } +} + +int QQuickPathItem::miterLimit() const +{ + Q_D(const QQuickPathItem); + return d->miterLimit; +} + +void QQuickPathItem::setMiterLimit(int limit) +{ + Q_D(QQuickPathItem); + if (d->miterLimit != limit) { + d->miterLimit = limit; + d->dirty |= QQuickPathItemPrivate::DirtyStyle; + emit miterLimitChanged(); + polish(); + } +} + +QQuickPathItem::CapStyle QQuickPathItem::capStyle() const +{ + Q_D(const QQuickPathItem); + return d->capStyle; +} + +void QQuickPathItem::setCapStyle(CapStyle style) +{ + Q_D(QQuickPathItem); + if (d->capStyle != style) { + d->capStyle = style; + d->dirty |= QQuickPathItemPrivate::DirtyStyle; + emit capStyleChanged(); + polish(); + } +} + +QQuickPathItem::StrokeStyle QQuickPathItem::strokeStyle() const +{ + Q_D(const QQuickPathItem); + return d->strokeStyle; +} + +void QQuickPathItem::setStrokeStyle(StrokeStyle style) +{ + Q_D(QQuickPathItem); + if (d->strokeStyle != style) { + d->strokeStyle = style; + d->dirty |= QQuickPathItemPrivate::DirtyDash; + emit strokeStyleChanged(); + polish(); + } +} + +qreal QQuickPathItem::dashOffset() const +{ + Q_D(const QQuickPathItem); + return d->dashOffset; +} + +void QQuickPathItem::setDashOffset(qreal offset) +{ + Q_D(QQuickPathItem); + if (d->dashOffset != offset) { + d->dashOffset = offset; + d->dirty |= QQuickPathItemPrivate::DirtyDash; + emit dashOffsetChanged(); + polish(); + } +} + +QVector QQuickPathItem::dashPattern() const +{ + Q_D(const QQuickPathItem); + return d->dashPattern; +} + +void QQuickPathItem::setDashPattern(const QVector &array) +{ + Q_D(QQuickPathItem); + if (d->dashPattern != array) { + d->dashPattern = array; + d->dirty |= QQuickPathItemPrivate::DirtyDash; + emit dashPatternChanged(); + polish(); + } +} + +QQuickPathGradient *QQuickPathItem::fillGradient() const +{ + Q_D(const QQuickPathItem); + return d->fillGradient; +} + +void QQuickPathItem::setFillGradient(QQuickPathGradient *gradient) +{ + Q_D(QQuickPathItem); + if (d->fillGradient != gradient) { + if (d->fillGradient) + qmlobject_disconnect(d->fillGradient, QQuickPathGradient, SIGNAL(updated()), + this, QQuickPathItem, SLOT(_q_fillGradientChanged())); + d->fillGradient = gradient; + if (d->fillGradient) + qmlobject_connect(d->fillGradient, QQuickPathGradient, SIGNAL(updated()), + this, QQuickPathItem, SLOT(_q_fillGradientChanged())); + d->dirty |= QQuickPathItemPrivate::DirtyFillGradient; + polish(); + } +} + +void QQuickPathItemPrivate::_q_fillGradientChanged() +{ + Q_Q(QQuickPathItem); + dirty |= DirtyFillGradient; + q->polish(); +} + +void QQuickPathItem::resetFillGradient() +{ + setFillGradient(nullptr); +} + +void QQuickPathItem::updatePolish() +{ + Q_D(QQuickPathItem); + + if (!d->dirty) + return; + + if (!d->renderer) { + d->createRenderer(); + if (!d->renderer) + return; + emit rendererChanged(); + } + + // endSync() is where expensive calculations may happen, depending on the + // backend. Therefore do this only when the item is visible. + if (isVisible()) + d->sync(); + + update(); +} + +void QQuickPathItem::itemChange(ItemChange change, const ItemChangeData &data) +{ + // sync may have been deferred; do it now if the item became visible + if (change == ItemVisibleHasChanged && data.boolValue) + polish(); + + QQuickItem::itemChange(change, data); +} + +QSGNode *QQuickPathItem::updatePaintNode(QSGNode *node, UpdatePaintNodeData *) +{ + // Called on the render thread, with the gui thread blocked. We can now + // safely access gui thread data. + + Q_D(QQuickPathItem); + if (d->renderer) { + if (!node) + node = d->createRenderNode(); + d->renderer->updatePathRenderNode(); + } + return node; +} + +// the renderer object lives on the gui thread +void QQuickPathItemPrivate::createRenderer() +{ + Q_Q(QQuickPathItem); + QSGRendererInterface *ri = q->window()->rendererInterface(); + if (!ri) + return; + + switch (ri->graphicsApi()) { +#ifndef QT_NO_OPENGL + case QSGRendererInterface::OpenGL: + if (QQuickPathItemNvprRenderNode::isSupported()) { + rendererType = QQuickPathItem::NvprRenderer; + renderer = new QQuickPathItemNvprRenderer; + } else { + rendererType = QQuickPathItem::GeometryRenderer; + renderer = new QQuickPathItemGenericRenderer(q); + } + break; +#endif + case QSGRendererInterface::Software: + rendererType = QQuickPathItem::SoftwareRenderer; + renderer = new QQuickPathItemSoftwareRenderer; + break; + default: + qWarning("No path backend for this graphics API yet"); + break; + } +} + +// the node lives on the render thread +QSGNode *QQuickPathItemPrivate::createRenderNode() +{ + Q_Q(QQuickPathItem); + QSGNode *node = nullptr; + if (!q->window()) + return node; + QSGRendererInterface *ri = q->window()->rendererInterface(); + if (!ri) + return node; + + const bool hasFill = fillColor != Qt::transparent; + const bool hasStroke = !qFuzzyIsNull(strokeWidth) && strokeColor != Qt::transparent; + + switch (ri->graphicsApi()) { +#ifndef QT_NO_OPENGL + case QSGRendererInterface::OpenGL: + if (QQuickPathItemNvprRenderNode::isSupported()) { + node = new QQuickPathItemNvprRenderNode(q); + static_cast(renderer)->setNode( + static_cast(node)); + } else { + node = new QQuickPathItemGenericRootRenderNode(q->window(), hasFill, hasStroke); + static_cast(renderer)->setRootNode( + static_cast(node)); + } + break; +#endif + case QSGRendererInterface::Software: + node = new QQuickPathItemSoftwareRenderNode(q); + static_cast(renderer)->setNode( + static_cast(node)); + break; + default: + qWarning("No path backend for this graphics API yet"); + break; + } + + return node; +} + +void QQuickPathItemPrivate::sync() +{ + renderer->beginSync(); + + if (dirty & QQuickPathItemPrivate::DirtyPath) + renderer->setPath(path); + if (dirty & DirtyStrokeColor) + renderer->setStrokeColor(strokeColor); + if (dirty & DirtyStrokeWidth) + renderer->setStrokeWidth(strokeWidth); + if (dirty & DirtyFillColor) + renderer->setFillColor(fillColor); + if (dirty & DirtyFillRule) + renderer->setFillRule(fillRule); + if (dirty & DirtyStyle) { + renderer->setJoinStyle(joinStyle, miterLimit); + renderer->setCapStyle(capStyle); + } + if (dirty & DirtyDash) + renderer->setStrokeStyle(strokeStyle, dashOffset, dashPattern); + if (dirty & DirtyFillGradient) + renderer->setFillGradient(fillGradient); + + renderer->endSync(); + dirty = 0; +} + +// ***** gradient support ***** + +QQuickPathGradientStop::QQuickPathGradientStop(QObject *parent) + : QObject(parent), + m_position(0), + m_color(Qt::black) +{ +} + +qreal QQuickPathGradientStop::position() const +{ + return m_position; +} + +void QQuickPathGradientStop::setPosition(qreal position) +{ + if (m_position != position) { + m_position = position; + if (QQuickPathGradient *grad = qobject_cast(parent())) + emit grad->updated(); + } +} + +QColor QQuickPathGradientStop::color() const +{ + return m_color; +} + +void QQuickPathGradientStop::setColor(const QColor &color) +{ + if (m_color != color) { + m_color = color; + if (QQuickPathGradient *grad = qobject_cast(parent())) + emit grad->updated(); + } +} + +QQuickPathGradient::QQuickPathGradient(QObject *parent) + : QObject(parent), + m_spread(PadSpread) +{ +} + +void QQuickPathGradient::appendStop(QQmlListProperty *list, QObject *stop) +{ + QQuickPathGradientStop *sstop = qobject_cast(stop); + if (!sstop) { + qWarning("Gradient stop list only supports QQuickPathGradientStop elements"); + return; + } + QQuickPathGradient *grad = qobject_cast(list->object); + Q_ASSERT(grad); + sstop->setParent(grad); + grad->m_stops.append(sstop); +} + +QQmlListProperty QQuickPathGradient::stops() +{ + return QQmlListProperty(this, nullptr, &QQuickPathGradient::appendStop, nullptr, nullptr, nullptr); +} + +QGradientStops QQuickPathGradient::sortedGradientStops() const +{ + QGradientStops result; + for (int i = 0; i < m_stops.count(); ++i) { + QQuickPathGradientStop *s = static_cast(m_stops[i]); + int j = 0; + while (j < result.count() && result[j].first < s->position()) + ++j; + result.insert(j, QGradientStop(s->position(), s->color())); + } + return result; +} + +QQuickPathGradient::SpreadMode QQuickPathGradient::spread() const +{ + return m_spread; +} + +void QQuickPathGradient::setSpread(SpreadMode mode) +{ + if (m_spread != mode) { + m_spread = mode; + emit spreadChanged(); + emit updated(); + } +} + +QQuickPathLinearGradient::QQuickPathLinearGradient(QObject *parent) + : QQuickPathGradient(parent) +{ +} + +qreal QQuickPathLinearGradient::x1() const +{ + return m_start.x(); +} + +void QQuickPathLinearGradient::setX1(qreal v) +{ + if (m_start.x() != v) { + m_start.setX(v); + emit x1Changed(); + emit updated(); + } +} + +qreal QQuickPathLinearGradient::y1() const +{ + return m_start.y(); +} + +void QQuickPathLinearGradient::setY1(qreal v) +{ + if (m_start.y() != v) { + m_start.setY(v); + emit y1Changed(); + emit updated(); + } +} + +qreal QQuickPathLinearGradient::x2() const +{ + return m_end.x(); +} + +void QQuickPathLinearGradient::setX2(qreal v) +{ + if (m_end.x() != v) { + m_end.setX(v); + emit x2Changed(); + emit updated(); + } +} + +qreal QQuickPathLinearGradient::y2() const +{ + return m_end.y(); +} + +void QQuickPathLinearGradient::setY2(qreal v) +{ + if (m_end.y() != v) { + m_end.setY(v); + emit y2Changed(); + emit updated(); + } +} + +#ifndef QT_NO_OPENGL + +// contexts sharing with each other get the same cache instance +class QQuickPathItemGradientCacheWrapper +{ +public: + QQuickPathItemGradientCache *get(QOpenGLContext *context) + { + return m_resource.value(context); + } + +private: + QOpenGLMultiGroupSharedResource m_resource; +}; + +QQuickPathItemGradientCache *QQuickPathItemGradientCache::currentCache() +{ + static QQuickPathItemGradientCacheWrapper qt_path_gradient_caches; + return qt_path_gradient_caches.get(QOpenGLContext::currentContext()); +} + +// let QOpenGLContext manage the lifetime of the cached textures +QQuickPathItemGradientCache::~QQuickPathItemGradientCache() +{ + m_cache.clear(); +} + +void QQuickPathItemGradientCache::invalidateResource() +{ + m_cache.clear(); +} + +void QQuickPathItemGradientCache::freeResource(QOpenGLContext *) +{ + qDeleteAll(m_cache); + m_cache.clear(); +} + +static void generateGradientColorTable(const QQuickPathItemGradientCache::GradientDesc &gradient, + uint *colorTable, int size, float opacity) +{ + int pos = 0; + const QGradientStops &s = gradient.stops; + const bool colorInterpolation = true; + + uint alpha = qRound(opacity * 256); + uint current_color = ARGB_COMBINE_ALPHA(s[0].second.rgba(), alpha); + qreal incr = 1.0 / qreal(size); + qreal fpos = 1.5 * incr; + colorTable[pos++] = ARGB2RGBA(qPremultiply(current_color)); + + while (fpos <= s.first().first) { + colorTable[pos] = colorTable[pos - 1]; + pos++; + fpos += incr; + } + + if (colorInterpolation) + current_color = qPremultiply(current_color); + + const int sLast = s.size() - 1; + for (int i = 0; i < sLast; ++i) { + qreal delta = 1/(s[i+1].first - s[i].first); + uint next_color = ARGB_COMBINE_ALPHA(s[i + 1].second.rgba(), alpha); + if (colorInterpolation) + next_color = qPremultiply(next_color); + + while (fpos < s[i+1].first && pos < size) { + int dist = int(256 * ((fpos - s[i].first) * delta)); + int idist = 256 - dist; + if (colorInterpolation) + colorTable[pos] = ARGB2RGBA(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)); + else + colorTable[pos] = ARGB2RGBA(qPremultiply(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist))); + ++pos; + fpos += incr; + } + current_color = next_color; + } + + Q_ASSERT(s.size() > 0); + + uint last_color = ARGB2RGBA(qPremultiply(ARGB_COMBINE_ALPHA(s[sLast].second.rgba(), alpha))); + for ( ; pos < size; ++pos) + colorTable[pos] = last_color; + + colorTable[size-1] = last_color; +} + +QSGTexture *QQuickPathItemGradientCache::get(const GradientDesc &grad) +{ + QSGPlainTexture *tx = m_cache[grad]; + if (!tx) { + QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); + GLuint id; + f->glGenTextures(1, &id); + f->glBindTexture(GL_TEXTURE_2D, id); + static const uint W = 1024; // texture size is 1024x1 + uint buf[W]; + generateGradientColorTable(grad, buf, W, 1.0f); + f->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, W, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); + tx = new QSGPlainTexture; + tx->setTextureId(id); + switch (grad.spread) { + case QQuickPathGradient::PadSpread: + tx->setHorizontalWrapMode(QSGTexture::ClampToEdge); + tx->setVerticalWrapMode(QSGTexture::ClampToEdge); + break; + case QQuickPathGradient::RepeatSpread: + tx->setHorizontalWrapMode(QSGTexture::Repeat); + tx->setVerticalWrapMode(QSGTexture::Repeat); + break; + case QQuickPathGradient::ReflectSpread: + tx->setHorizontalWrapMode(QSGTexture::MirroredRepeat); + tx->setVerticalWrapMode(QSGTexture::MirroredRepeat); + break; + default: + qWarning("Unknown gradient spread mode %d", grad.spread); + break; + } + m_cache[grad] = tx; + } + return tx; +} + +#endif // QT_NO_OPENGL + +QT_END_NAMESPACE + +#include "moc_qquickpathitem_p.cpp" diff --git a/src/quick/items/qquickpathitem_p.h b/src/quick/items/qquickpathitem_p.h new file mode 100644 index 0000000000..39b407cf87 --- /dev/null +++ b/src/quick/items/qquickpathitem_p.h @@ -0,0 +1,281 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKPATHITEM_P_H +#define QQUICKPATHITEM_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qquickitem.h" + +#include +#include +#include + +QT_REQUIRE_CONFIG(quick_path); + +QT_BEGIN_NAMESPACE + +class QQuickPathItemPrivate; + +class Q_QUICK_PRIVATE_EXPORT QQuickPathGradientStop : public QObject +{ + Q_OBJECT + Q_PROPERTY(qreal position READ position WRITE setPosition) + Q_PROPERTY(QColor color READ color WRITE setColor) + +public: + QQuickPathGradientStop(QObject *parent = nullptr); + + qreal position() const; + void setPosition(qreal position); + + QColor color() const; + void setColor(const QColor &color); + +private: + qreal m_position; + QColor m_color; +}; + +class Q_QUICK_PRIVATE_EXPORT QQuickPathGradient : public QObject +{ + Q_OBJECT + Q_PROPERTY(QQmlListProperty stops READ stops) + Q_PROPERTY(SpreadMode spread READ spread WRITE setSpread NOTIFY spreadChanged) + Q_CLASSINFO("DefaultProperty", "stops") + +public: + enum SpreadMode { + PadSpread, + RepeatSpread, + ReflectSpread + }; + Q_ENUM(SpreadMode) + + QQuickPathGradient(QObject *parent = nullptr); + + QQmlListProperty stops(); + + QGradientStops sortedGradientStops() const; + + SpreadMode spread() const; + void setSpread(SpreadMode mode); + +signals: + void updated(); + void spreadChanged(); + +private: + static void appendStop(QQmlListProperty *list, QObject *stop); + + QVector m_stops; + SpreadMode m_spread; +}; + +class Q_QUICK_PRIVATE_EXPORT QQuickPathLinearGradient : public QQuickPathGradient +{ + Q_OBJECT + Q_PROPERTY(qreal x1 READ x1 WRITE setX1 NOTIFY x1Changed) + Q_PROPERTY(qreal y1 READ y1 WRITE setY1 NOTIFY y1Changed) + Q_PROPERTY(qreal x2 READ x2 WRITE setX2 NOTIFY x2Changed) + Q_PROPERTY(qreal y2 READ y2 WRITE setY2 NOTIFY y2Changed) + Q_CLASSINFO("DefaultProperty", "stops") + +public: + QQuickPathLinearGradient(QObject *parent = nullptr); + + qreal x1() const; + void setX1(qreal v); + qreal y1() const; + void setY1(qreal v); + qreal x2() const; + void setX2(qreal v); + qreal y2() const; + void setY2(qreal v); + +signals: + void x1Changed(); + void y1Changed(); + void x2Changed(); + void y2Changed(); + +private: + QPointF m_start; + QPointF m_end; +}; + +class Q_QUICK_PRIVATE_EXPORT QQuickPathItem : public QQuickItem +{ + Q_OBJECT + + Q_PROPERTY(RendererType renderer READ rendererType NOTIFY rendererChanged) + + Q_PROPERTY(QQuickPath *path READ path WRITE setPath NOTIFY pathChanged) + + Q_PROPERTY(QColor strokeColor READ strokeColor WRITE setStrokeColor NOTIFY strokeColorChanged) + Q_PROPERTY(qreal strokeWidth READ strokeWidth WRITE setStrokeWidth NOTIFY strokeWidthChanged) + Q_PROPERTY(QColor fillColor READ fillColor WRITE setFillColor NOTIFY fillColorChanged) + Q_PROPERTY(FillRule fillRule READ fillRule WRITE setFillRule NOTIFY fillRuleChanged) + Q_PROPERTY(JoinStyle joinStyle READ joinStyle WRITE setJoinStyle NOTIFY joinStyleChanged) + Q_PROPERTY(int miterLimit READ miterLimit WRITE setMiterLimit NOTIFY miterLimitChanged) + Q_PROPERTY(CapStyle capStyle READ capStyle WRITE setCapStyle NOTIFY capStyleChanged) + Q_PROPERTY(StrokeStyle strokeStyle READ strokeStyle WRITE setStrokeStyle NOTIFY strokeStyleChanged) + Q_PROPERTY(qreal dashOffset READ dashOffset WRITE setDashOffset NOTIFY dashOffsetChanged) + Q_PROPERTY(QVector dashPattern READ dashPattern WRITE setDashPattern NOTIFY dashPatternChanged) + Q_PROPERTY(QQuickPathGradient *fillGradient READ fillGradient WRITE setFillGradient RESET resetFillGradient) + +public: + enum FillRule { + OddEvenFill = Qt::OddEvenFill, + WindingFill = Qt::WindingFill + }; + Q_ENUM(FillRule) + + enum JoinStyle { + MiterJoin = Qt::MiterJoin, + BevelJoin = Qt::BevelJoin, + RoundJoin = Qt::RoundJoin + }; + Q_ENUM(JoinStyle) + + enum CapStyle { + FlatCap = Qt::FlatCap, + SquareCap = Qt::SquareCap, + RoundCap = Qt::RoundCap + }; + Q_ENUM(CapStyle) + + enum StrokeStyle { + SolidLine = Qt::SolidLine, + DashLine = Qt::DashLine + }; + Q_ENUM(StrokeStyle) + + enum RendererType { + UnknownRenderer, + GeometryRenderer, + NvprRenderer, + SoftwareRenderer + }; + Q_ENUM(RendererType) + + QQuickPathItem(QQuickItem *parent = nullptr); + ~QQuickPathItem(); + + RendererType rendererType() const; + + QQuickPath *path() const; + void setPath(QQuickPath *path); + + QColor strokeColor() const; + void setStrokeColor(const QColor &color); + + qreal strokeWidth() const; + void setStrokeWidth(qreal w); + + QColor fillColor() const; + void setFillColor(const QColor &color); + + FillRule fillRule() const; + void setFillRule(FillRule fillRule); + + JoinStyle joinStyle() const; + void setJoinStyle(JoinStyle style); + + int miterLimit() const; + void setMiterLimit(int limit); + + CapStyle capStyle() const; + void setCapStyle(CapStyle style); + + StrokeStyle strokeStyle() const; + void setStrokeStyle(StrokeStyle style); + + qreal dashOffset() const; + void setDashOffset(qreal offset); + + QVector dashPattern() const; + void setDashPattern(const QVector &array); + + QQuickPathGradient *fillGradient() const; + void setFillGradient(QQuickPathGradient *gradient); + void resetFillGradient(); + +protected: + QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override; + void updatePolish() override; + void itemChange(ItemChange change, const ItemChangeData &data) override; + +Q_SIGNALS: + void rendererChanged(); + void pathChanged(); + void strokeColorChanged(); + void strokeWidthChanged(); + void fillColorChanged(); + void fillRuleChanged(); + void joinStyleChanged(); + void miterLimitChanged(); + void capStyleChanged(); + void strokeStyleChanged(); + void dashOffsetChanged(); + void dashPatternChanged(); + void fillGradientChanged(); + +private: + Q_DISABLE_COPY(QQuickPathItem) + Q_DECLARE_PRIVATE(QQuickPathItem) + Q_PRIVATE_SLOT(d_func(), void _q_pathChanged()) + Q_PRIVATE_SLOT(d_func(), void _q_fillGradientChanged()) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickPathItem) + +#endif // QQUICKPATHITEM_P_H diff --git a/src/quick/items/qquickpathitem_p_p.h b/src/quick/items/qquickpathitem_p_p.h new file mode 100644 index 0000000000..366628d867 --- /dev/null +++ b/src/quick/items/qquickpathitem_p_p.h @@ -0,0 +1,177 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKPATHITEM_P_P_H +#define QQUICKPATHITEM_P_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qquickpathitem_p.h" +#include "qquickitem_p.h" +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QSGPlainTexture; + +class QQuickAbstractPathRenderer +{ +public: + virtual ~QQuickAbstractPathRenderer() { } + + // Gui thread + virtual void beginSync() = 0; + virtual void setPath(const QQuickPath *path) = 0; + virtual void setStrokeColor(const QColor &color) = 0; + virtual void setStrokeWidth(qreal w) = 0; + virtual void setFillColor(const QColor &color) = 0; + virtual void setFillRule(QQuickPathItem::FillRule fillRule) = 0; + virtual void setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) = 0; + virtual void setCapStyle(QQuickPathItem::CapStyle capStyle) = 0; + virtual void setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, + qreal dashOffset, const QVector &dashPattern) = 0; + virtual void setFillGradient(QQuickPathGradient *gradient) = 0; + virtual void endSync() = 0; + + // Render thread, with gui blocked + virtual void updatePathRenderNode() = 0; +}; + +class QQuickPathItemPrivate : public QQuickItemPrivate +{ + Q_DECLARE_PUBLIC(QQuickPathItem) + +public: + enum Dirty { + DirtyPath = 0x01, + DirtyStrokeColor = 0x02, + DirtyStrokeWidth = 0x04, + DirtyFillColor = 0x08, + DirtyFillRule = 0x10, + DirtyStyle = 0x20, + DirtyDash = 0x40, + DirtyFillGradient = 0x80, + + DirtyAll = 0xFF + }; + + QQuickPathItemPrivate(); + ~QQuickPathItemPrivate(); + + void createRenderer(); + QSGNode *createRenderNode(); + void sync(); + + void _q_pathChanged(); + void _q_fillGradientChanged(); + + QQuickPathItem::RendererType rendererType; + QQuickAbstractPathRenderer *renderer; + QQuickPath *path; + int dirty; + QColor strokeColor; + qreal strokeWidth; + QColor fillColor; + QQuickPathItem::FillRule fillRule; + QQuickPathItem::JoinStyle joinStyle; + int miterLimit; + QQuickPathItem::CapStyle capStyle; + QQuickPathItem::StrokeStyle strokeStyle; + qreal dashOffset; + QVector dashPattern; + QQuickPathGradient *fillGradient; +}; + +#ifndef QT_NO_OPENGL + +class QQuickPathItemGradientCache : public QOpenGLSharedResource +{ +public: + struct GradientDesc { + QGradientStops stops; + QPointF start; + QPointF end; + QQuickPathGradient::SpreadMode spread; + bool operator==(const GradientDesc &other) const + { + return start == other.start && end == other.end && spread == other.spread + && stops == other.stops; + } + }; + + QQuickPathItemGradientCache(QOpenGLContext *context) : QOpenGLSharedResource(context->shareGroup()) { } + ~QQuickPathItemGradientCache(); + + void invalidateResource() override; + void freeResource(QOpenGLContext *) override; + + QSGTexture *get(const GradientDesc &grad); + + static QQuickPathItemGradientCache *currentCache(); + +private: + QHash m_cache; +}; + +inline uint qHash(const QQuickPathItemGradientCache::GradientDesc &v, uint seed = 0) +{ + uint h = seed; + h += v.start.x() + v.end.y() + v.spread; + for (int i = 0; i < 3 && i < v.stops.count(); ++i) + h += v.stops[i].second.rgba(); + return h; +} + +#endif // QT_NO_OPENGL + +QT_END_NAMESPACE + +#endif diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp new file mode 100644 index 0000000000..9bd03c0e51 --- /dev/null +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -0,0 +1,510 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickpathitemgenericrenderer_p.h" +#include +#include + +QT_BEGIN_NAMESPACE + +static const qreal SCALE = 100; + +struct ColoredVertex // must match QSGGeometry::ColoredPoint2D +{ + float x, y; + QQuickPathItemGenericRenderer::Color4ub color; + void set(float nx, float ny, QQuickPathItemGenericRenderer::Color4ub ncolor) + { + x = nx; y = ny; color = ncolor; + } +}; + +static inline QQuickPathItemGenericRenderer::Color4ub colorToColor4ub(const QColor &c) +{ + QQuickPathItemGenericRenderer::Color4ub color = { + uchar(qRound(c.redF() * c.alphaF() * 255)), + uchar(qRound(c.greenF() * c.alphaF() * 255)), + uchar(qRound(c.blueF() * c.alphaF() * 255)), + uchar(qRound(c.alphaF() * 255)) + }; + return color; +} + +QQuickPathItemGenericRootRenderNode::QQuickPathItemGenericRootRenderNode(QQuickWindow *window, + bool hasFill, + bool hasStroke) + : m_fillNode(nullptr), + m_strokeNode(nullptr) +{ + if (hasFill) { + m_fillNode = new QQuickPathItemGenericRenderNode(window, this); + appendChildNode(m_fillNode); + } + if (hasStroke) { + m_strokeNode = new QQuickPathItemGenericRenderNode(window, this); + appendChildNode(m_strokeNode); + } +} + +QQuickPathItemGenericRootRenderNode::~QQuickPathItemGenericRootRenderNode() +{ +} + +QQuickPathItemGenericRenderNode::QQuickPathItemGenericRenderNode(QQuickWindow *window, + QQuickPathItemGenericRootRenderNode *rootNode) + : m_geometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), 0, 0), + m_window(window), + m_rootNode(rootNode), + m_material(nullptr) +{ + setGeometry(&m_geometry); + activateMaterial(MatSolidColor); +} + +QQuickPathItemGenericRenderNode::~QQuickPathItemGenericRenderNode() +{ +} + +void QQuickPathItemGenericRenderNode::activateMaterial(Material m) +{ + switch (m) { + case MatSolidColor: + // Use vertexcolor material. Items with different colors remain batchable + // this way, at the expense of having to provide per-vertex color values. + if (!m_solidColorMaterial) + m_solidColorMaterial.reset(QQuickPathItemGenericMaterialFactory::createVertexColor(m_window)); + m_material = m_solidColorMaterial.data(); + break; + case MatLinearGradient: + if (!m_linearGradientMaterial) + m_linearGradientMaterial.reset(QQuickPathItemGenericMaterialFactory::createLinearGradient(m_window, this)); + m_material = m_linearGradientMaterial.data(); + break; + default: + qWarning("Unknown material %d", m); + return; + } + + if (material() != m_material) + setMaterial(m_material); +} + +// sync, and so triangulation too, happens on the gui thread +void QQuickPathItemGenericRenderer::beginSync() +{ + m_syncDirty = 0; +} + +void QQuickPathItemGenericRenderer::setPath(const QQuickPath *path) +{ + m_path = path ? path->path() : QPainterPath(); + m_syncDirty |= DirtyGeom; +} + +void QQuickPathItemGenericRenderer::setStrokeColor(const QColor &color) +{ + m_strokeColor = colorToColor4ub(color); + m_syncDirty |= DirtyColor; +} + +void QQuickPathItemGenericRenderer::setStrokeWidth(qreal w) +{ + m_pen.setWidthF(w); + m_syncDirty |= DirtyGeom; +} + +void QQuickPathItemGenericRenderer::setFillColor(const QColor &color) +{ + m_fillColor = colorToColor4ub(color); + m_syncDirty |= DirtyColor; +} + +void QQuickPathItemGenericRenderer::setFillRule(QQuickPathItem::FillRule fillRule) +{ + m_fillRule = Qt::FillRule(fillRule); + m_syncDirty |= DirtyGeom; +} + +void QQuickPathItemGenericRenderer::setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) +{ + m_pen.setJoinStyle(Qt::PenJoinStyle(joinStyle)); + m_pen.setMiterLimit(miterLimit); + m_syncDirty |= DirtyGeom; +} + +void QQuickPathItemGenericRenderer::setCapStyle(QQuickPathItem::CapStyle capStyle) +{ + m_pen.setCapStyle(Qt::PenCapStyle(capStyle)); + m_syncDirty |= DirtyGeom; +} + +void QQuickPathItemGenericRenderer::setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, + qreal dashOffset, const QVector &dashPattern) +{ + m_pen.setStyle(Qt::PenStyle(strokeStyle)); + if (strokeStyle == QQuickPathItem::DashLine) { + m_pen.setDashPattern(dashPattern); + m_pen.setDashOffset(dashOffset); + } + m_syncDirty |= DirtyGeom; +} + +void QQuickPathItemGenericRenderer::setFillGradient(QQuickPathGradient *gradient) +{ + m_fillGradientActive = gradient != nullptr; + if (gradient) { + m_fillGradient.stops = gradient->sortedGradientStops(); + m_fillGradient.spread = gradient->spread(); + if (QQuickPathLinearGradient *g = qobject_cast(gradient)) { + m_fillGradient.start = QPointF(g->x1(), g->y1()); + m_fillGradient.end = QPointF(g->x2(), g->y2()); + } else { + Q_UNREACHABLE(); + } + } + m_syncDirty |= DirtyFillGradient; +} + +void QQuickPathItemGenericRenderer::endSync() +{ + if (!m_syncDirty) + return; + + // Use a shadow dirty flag in order to avoid losing state in case there are + // multiple syncs with different dirty flags before we get to + // updatePathRenderNode() on the render thread (with the gui thread + // blocked). For our purposes here m_syncDirty is still required since + // geometry regeneration must only happen when there was an actual change + // in this particular sync round. + m_effectiveDirty |= m_syncDirty; + + if (m_path.isEmpty()) { + m_fillVertices.clear(); + m_fillIndices.clear(); + m_strokeVertices.clear(); + return; + } + + triangulateFill(); + triangulateStroke(); +} + +void QQuickPathItemGenericRenderer::triangulateFill() +{ + m_path.setFillRule(m_fillRule); + + const QVectorPath &vp = qtVectorPathForPath(m_path); + + QTriangleSet ts = qTriangulate(vp, QTransform::fromScale(SCALE, SCALE)); + const int vertexCount = ts.vertices.count() / 2; // just a qreal vector with x,y hence the / 2 + m_fillVertices.resize(vertexCount); + ColoredVertex *vdst = reinterpret_cast(m_fillVertices.data()); + const qreal *vsrc = ts.vertices.constData(); + for (int i = 0; i < vertexCount; ++i) + vdst[i].set(vsrc[i * 2] / SCALE, vsrc[i * 2 + 1] / SCALE, m_fillColor); + + m_fillIndices.resize(ts.indices.size()); + quint16 *idst = m_fillIndices.data(); + if (ts.indices.type() == QVertexIndexVector::UnsignedShort) { + memcpy(idst, ts.indices.data(), m_fillIndices.count() * sizeof(quint16)); + } else { + const quint32 *isrc = (const quint32 *) ts.indices.data(); + for (int i = 0; i < m_fillIndices.count(); ++i) + idst[i] = isrc[i]; + } +} + +void QQuickPathItemGenericRenderer::triangulateStroke() +{ + const QVectorPath &vp = qtVectorPathForPath(m_path); + + const QRectF clip(0, 0, m_item->width(), m_item->height()); + const qreal inverseScale = 1.0 / SCALE; + m_stroker.setInvScale(inverseScale); + if (m_pen.style() == Qt::SolidLine) { + m_stroker.process(vp, m_pen, clip, 0); + } else { + m_dashStroker.setInvScale(inverseScale); + m_dashStroker.process(vp, m_pen, clip, 0); + QVectorPath dashStroke(m_dashStroker.points(), m_dashStroker.elementCount(), + m_dashStroker.elementTypes(), 0); + m_stroker.process(dashStroke, m_pen, clip, 0); + } + + if (!m_stroker.vertexCount()) { + m_strokeVertices.clear(); + return; + } + + const int vertexCount = m_stroker.vertexCount() / 2; // just a float vector with x,y hence the / 2 + m_strokeVertices.resize(vertexCount); + ColoredVertex *vdst = reinterpret_cast(m_strokeVertices.data()); + const float *vsrc = m_stroker.vertices(); + for (int i = 0; i < vertexCount; ++i) + vdst[i].set(vsrc[i * 2], vsrc[i * 2 + 1], m_strokeColor); +} + +void QQuickPathItemGenericRenderer::setRootNode(QQuickPathItemGenericRootRenderNode *rn) +{ + if (m_rootNode != rn) { + m_rootNode = rn; + // Scenegraph nodes can be destroyed and then replaced by new ones over + // time; hence it is important to mark everything dirty for + // updatePathRenderNode(). We can assume the renderer has a full sync + // of the data at this point. + m_effectiveDirty = DirtyAll; + } +} + +// on the render thread with gui blocked +void QQuickPathItemGenericRenderer::updatePathRenderNode() +{ + if (!m_effectiveDirty || !m_rootNode) + return; + + if (m_fillColor.a == 0) { + delete m_rootNode->m_fillNode; + m_rootNode->m_fillNode = nullptr; + } else if (!m_rootNode->m_fillNode) { + m_rootNode->m_fillNode = new QQuickPathItemGenericRenderNode(m_item->window(), m_rootNode); + if (m_rootNode->m_strokeNode) + m_rootNode->removeChildNode(m_rootNode->m_strokeNode); + m_rootNode->appendChildNode(m_rootNode->m_fillNode); + if (m_rootNode->m_strokeNode) + m_rootNode->appendChildNode(m_rootNode->m_strokeNode); + m_effectiveDirty |= DirtyGeom; + } + + if (qFuzzyIsNull(m_pen.widthF()) || m_strokeColor.a == 0) { + delete m_rootNode->m_strokeNode; + m_rootNode->m_strokeNode = nullptr; + } else if (!m_rootNode->m_strokeNode) { + m_rootNode->m_strokeNode = new QQuickPathItemGenericRenderNode(m_item->window(), m_rootNode); + m_rootNode->appendChildNode(m_rootNode->m_strokeNode); + m_effectiveDirty |= DirtyGeom; + } + + updateFillNode(); + updateStrokeNode(); + + m_effectiveDirty = 0; +} + +void QQuickPathItemGenericRenderer::updateFillNode() +{ + if (!m_rootNode->m_fillNode) + return; + + QQuickPathItemGenericRenderNode *n = m_rootNode->m_fillNode; + QSGGeometry *g = &n->m_geometry; + if (m_fillVertices.isEmpty()) { + g->allocate(0, 0); + n->markDirty(QSGNode::DirtyGeometry); + return; + } + + if (m_fillGradientActive) { + n->activateMaterial(QQuickPathItemGenericRenderNode::MatLinearGradient); + if (m_effectiveDirty & DirtyFillGradient) { + // Make a copy of the data that will be accessed by the material on + // the render thread. + n->m_fillGradient = m_fillGradient; + // Gradients are implemented via a texture-based material. + n->markDirty(QSGNode::DirtyMaterial); + // stop here if only the gradient changed; no need to touch the geometry + if (!(m_effectiveDirty & DirtyGeom)) + return; + } + } else { + n->activateMaterial(QQuickPathItemGenericRenderNode::MatSolidColor); + // fast path for updating only color values when no change in vertex positions + if ((m_effectiveDirty & DirtyColor) && !(m_effectiveDirty & DirtyGeom)) { + ColoredVertex *vdst = reinterpret_cast(g->vertexData()); + for (int i = 0; i < g->vertexCount(); ++i) + vdst[i].set(vdst[i].x, vdst[i].y, m_fillColor); + n->markDirty(QSGNode::DirtyGeometry); + return; + } + } + + g->allocate(m_fillVertices.count(), m_fillIndices.count()); + g->setDrawingMode(QSGGeometry::DrawTriangles); + memcpy(g->vertexData(), m_fillVertices.constData(), g->vertexCount() * g->sizeOfVertex()); + memcpy(g->indexData(), m_fillIndices.constData(), g->indexCount() * g->sizeOfIndex()); + + n->markDirty(QSGNode::DirtyGeometry); +} + +void QQuickPathItemGenericRenderer::updateStrokeNode() +{ + if (!m_rootNode->m_strokeNode) + return; + if (m_effectiveDirty == DirtyFillGradient) // not applicable + return; + + QQuickPathItemGenericRenderNode *n = m_rootNode->m_strokeNode; + n->markDirty(QSGNode::DirtyGeometry); + + QSGGeometry *g = &n->m_geometry; + if (m_strokeVertices.isEmpty()) { + g->allocate(0, 0); + return; + } + + if ((m_effectiveDirty & DirtyColor) && !(m_effectiveDirty & DirtyGeom)) { + ColoredVertex *vdst = reinterpret_cast(g->vertexData()); + for (int i = 0; i < g->vertexCount(); ++i) + vdst[i].set(vdst[i].x, vdst[i].y, m_strokeColor); + return; + } + + g->allocate(m_strokeVertices.count(), 0); + g->setDrawingMode(QSGGeometry::DrawTriangleStrip); + memcpy(g->vertexData(), m_strokeVertices.constData(), g->vertexCount() * g->sizeOfVertex()); +} + +QSGMaterial *QQuickPathItemGenericMaterialFactory::createVertexColor(QQuickWindow *window) +{ + QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi(); + +#ifndef QT_NO_OPENGL + if (api == QSGRendererInterface::OpenGL) // ### so much for "generic"... + return new QSGVertexColorMaterial; +#endif + + qWarning("Vertex-color material: Unsupported graphics API %d", api); + return nullptr; +} + +QSGMaterial *QQuickPathItemGenericMaterialFactory::createLinearGradient(QQuickWindow *window, + QQuickPathItemGenericRenderNode *node) +{ + QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi(); + +#ifndef QT_NO_OPENGL + if (api == QSGRendererInterface::OpenGL) // ### so much for "generic"... + return new QQuickPathItemLinearGradientMaterial(node); +#endif + + qWarning("Linear gradient material: Unsupported graphics API %d", api); + return nullptr; +} + +#ifndef QT_NO_OPENGL + +QSGMaterialType QQuickPathItemLinearGradientShader::type; + +QQuickPathItemLinearGradientShader::QQuickPathItemLinearGradientShader() +{ + setShaderSourceFile(QOpenGLShader::Vertex, + QStringLiteral(":/qt-project.org/items/shaders/lineargradient.vert")); + setShaderSourceFile(QOpenGLShader::Fragment, + QStringLiteral(":/qt-project.org/items/shaders/lineargradient.frag")); +} + +void QQuickPathItemLinearGradientShader::initialize() +{ + m_opacityLoc = program()->uniformLocation("opacity"); + m_matrixLoc = program()->uniformLocation("matrix"); + m_gradStartLoc = program()->uniformLocation("gradStart"); + m_gradEndLoc = program()->uniformLocation("gradEnd"); +} + +void QQuickPathItemLinearGradientShader::updateState(const RenderState &state, QSGMaterial *mat, QSGMaterial *) +{ + QQuickPathItemLinearGradientMaterial *m = static_cast(mat); + + if (state.isOpacityDirty()) + program()->setUniformValue(m_opacityLoc, state.opacity()); + + if (state.isMatrixDirty()) + program()->setUniformValue(m_matrixLoc, state.combinedMatrix()); + + QQuickPathItemGenericRenderNode *node = m->node(); + program()->setUniformValue(m_gradStartLoc, QVector2D(node->m_fillGradient.start)); + program()->setUniformValue(m_gradEndLoc, QVector2D(node->m_fillGradient.end)); + + QSGTexture *tx = QQuickPathItemGradientCache::currentCache()->get(node->m_fillGradient); + tx->bind(); +} + +char const *const *QQuickPathItemLinearGradientShader::attributeNames() const +{ + static const char *const attr[] = { "vertexCoord", "vertexColor", nullptr }; + return attr; +} + +int QQuickPathItemLinearGradientMaterial::compare(const QSGMaterial *other) const +{ + Q_ASSERT(other && type() == other->type()); + const QQuickPathItemLinearGradientMaterial *m = static_cast(other); + + QQuickPathItemGenericRenderNode *a = node(); + QQuickPathItemGenericRenderNode *b = m->node(); + Q_ASSERT(a && b); + if (a == b) + return 0; + + const QQuickPathItemGradientCache::GradientDesc *ga = &a->m_fillGradient; + const QQuickPathItemGradientCache::GradientDesc *gb = &b->m_fillGradient; + if (int d = ga->start.x() - gb->start.x()) + return d; + if (int d = ga->start.y() - gb->start.y()) + return d; + if (int d = ga->end.x() - gb->end.x()) + return d; + if (int d = ga->end.y() - gb->end.y()) + return d; + + if (int d = ga->stops.count() - gb->stops.count()) + return d; + + for (int i = 0; i < ga->stops.count(); ++i) { + if (int d = ga->stops[i].first - gb->stops[i].first) + return d; + if (int d = ga->stops[i].second.rgba() - gb->stops[i].second.rgba()) + return d; + } + + return 0; +} + +#endif // QT_NO_OPENGL + +QT_END_NAMESPACE diff --git a/src/quick/items/qquickpathitemgenericrenderer_p.h b/src/quick/items/qquickpathitemgenericrenderer_p.h new file mode 100644 index 0000000000..c186959c88 --- /dev/null +++ b/src/quick/items/qquickpathitemgenericrenderer_p.h @@ -0,0 +1,232 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKPATHITEMGENERICRENDERER_P_H +#define QQUICKPATHITEMGENERICRENDERER_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 "qquickpathitem_p_p.h" +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QQuickPathItemGenericRootRenderNode; + +class QQuickPathItemGenericRenderer : public QQuickAbstractPathRenderer +{ +public: + enum Dirty { + DirtyGeom = 0x01, + DirtyColor = 0x02, + DirtyFillGradient = 0x04, + + DirtyAll = 0xFF + }; + + QQuickPathItemGenericRenderer(QQuickItem *item) + : m_item(item), + m_rootNode(nullptr), + m_effectiveDirty(0) + { } + + void beginSync() override; + void setPath(const QQuickPath *path) override; + void setStrokeColor(const QColor &color) override; + void setStrokeWidth(qreal w) override; + void setFillColor(const QColor &color) override; + void setFillRule(QQuickPathItem::FillRule fillRule) override; + void setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) override; + void setCapStyle(QQuickPathItem::CapStyle capStyle) override; + void setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, + qreal dashOffset, const QVector &dashPattern) override; + void setFillGradient(QQuickPathGradient *gradient) override; + void endSync() override; + void updatePathRenderNode() override; + + void setRootNode(QQuickPathItemGenericRootRenderNode *rn); + + struct Color4ub { unsigned char r, g, b, a; }; + +private: + void triangulateFill(); + void triangulateStroke(); + void updateFillNode(); + void updateStrokeNode(); + + QQuickItem *m_item; + QQuickPathItemGenericRootRenderNode *m_rootNode; + QTriangulatingStroker m_stroker; + QDashedStrokeProcessor m_dashStroker; + + QPen m_pen; + Color4ub m_strokeColor; + Color4ub m_fillColor; + Qt::FillRule m_fillRule; + QPainterPath m_path; + bool m_fillGradientActive; + QQuickPathItemGradientCache::GradientDesc m_fillGradient; + + QVector m_fillVertices; + QVector m_fillIndices; + QVector m_strokeVertices; + + int m_syncDirty; + int m_effectiveDirty; +}; + +class QQuickPathItemGenericRenderNode : public QSGGeometryNode +{ +public: + QQuickPathItemGenericRenderNode(QQuickWindow *window, QQuickPathItemGenericRootRenderNode *rootNode); + ~QQuickPathItemGenericRenderNode(); + + enum Material { + MatSolidColor, + MatLinearGradient + }; + + void activateMaterial(Material m); + + QQuickWindow *window() const { return m_window; } + QQuickPathItemGenericRootRenderNode *rootNode() const { return m_rootNode; } + + // shadow data for custom materials + QQuickPathItemGradientCache::GradientDesc m_fillGradient; + +private: + QSGGeometry m_geometry; + QQuickWindow *m_window; + QQuickPathItemGenericRootRenderNode *m_rootNode; + QSGMaterial *m_material; + QScopedPointer m_solidColorMaterial; + QScopedPointer m_linearGradientMaterial; + + friend class QQuickPathItemGenericRenderer; +}; + +class QQuickPathItemGenericRootRenderNode : public QSGNode +{ +public: + QQuickPathItemGenericRootRenderNode(QQuickWindow *window, bool hasFill, bool hasStroke); + ~QQuickPathItemGenericRootRenderNode(); + +private: + QQuickPathItemGenericRenderNode *m_fillNode; + QQuickPathItemGenericRenderNode *m_strokeNode; + + friend class QQuickPathItemGenericRenderer; +}; + +class QQuickPathItemGenericMaterialFactory +{ +public: + static QSGMaterial *createVertexColor(QQuickWindow *window); + static QSGMaterial *createLinearGradient(QQuickWindow *window, QQuickPathItemGenericRenderNode *node); +}; + +#ifndef QT_NO_OPENGL + +class QQuickPathItemLinearGradientShader : public QSGMaterialShader +{ +public: + QQuickPathItemLinearGradientShader(); + + void initialize() override; + void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect) override; + char const *const *attributeNames() const override; + + static QSGMaterialType type; + +private: + int m_opacityLoc; + int m_matrixLoc; + int m_gradStartLoc; + int m_gradEndLoc; +}; + +class QQuickPathItemLinearGradientMaterial : public QSGMaterial +{ +public: + QQuickPathItemLinearGradientMaterial(QQuickPathItemGenericRenderNode *node) + : m_node(node) + { + // Passing RequiresFullMatrix is essential in order to prevent the + // batch renderer from baking in simple, translate-only transforms into + // the vertex data. The shader will rely on the fact that + // vertexCoord.xy is the PathItem-space coordinate and so no modifications + // are welcome. + setFlag(Blending | RequiresFullMatrix); + } + + QSGMaterialType *type() const override + { + return &QQuickPathItemLinearGradientShader::type; + } + + int compare(const QSGMaterial *other) const override; + + QSGMaterialShader *createShader() const override + { + return new QQuickPathItemLinearGradientShader; + } + + QQuickPathItemGenericRenderNode *node() const { return m_node; } + +private: + QQuickPathItemGenericRenderNode *m_node; +}; + +#endif // QT_NO_OPENGL + +QT_END_NAMESPACE + +#endif // QQUICKPATHITEMGENERICRENDERER_P_H diff --git a/src/quick/items/qquickpathitemnvprrenderer.cpp b/src/quick/items/qquickpathitemnvprrenderer.cpp new file mode 100644 index 0000000000..d07a63c86d --- /dev/null +++ b/src/quick/items/qquickpathitemnvprrenderer.cpp @@ -0,0 +1,567 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickpathitemnvprrenderer_p.h" +#include +#include + +QT_BEGIN_NAMESPACE + +void QQuickPathItemNvprRenderer::beginSync() +{ + // nothing to do here +} + +void QQuickPathItemNvprRenderer::setPath(const QQuickPath *path) +{ + convertPath(path); + m_dirty |= DirtyPath; +} + +void QQuickPathItemNvprRenderer::setStrokeColor(const QColor &color) +{ + m_strokeColor = color; + m_dirty |= DirtyStyle; +} + +void QQuickPathItemNvprRenderer::setStrokeWidth(qreal w) +{ + m_strokeWidth = w; + m_dirty |= DirtyStyle; +} + +void QQuickPathItemNvprRenderer::setFillColor(const QColor &color) +{ + m_fillColor = color; + m_dirty |= DirtyStyle; +} + +void QQuickPathItemNvprRenderer::setFillRule(QQuickPathItem::FillRule fillRule) +{ + m_fillRule = fillRule; + m_dirty |= DirtyFillRule; +} + +void QQuickPathItemNvprRenderer::setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) +{ + m_joinStyle = joinStyle; + m_miterLimit = miterLimit; + m_dirty |= DirtyStyle; +} + +void QQuickPathItemNvprRenderer::setCapStyle(QQuickPathItem::CapStyle capStyle) +{ + m_capStyle = capStyle; + m_dirty |= DirtyStyle; +} + +void QQuickPathItemNvprRenderer::setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, + qreal dashOffset, const QVector &dashPattern) +{ + m_dashActive = strokeStyle == QQuickPathItem::DashLine; + m_dashOffset = dashOffset; + m_dashPattern = dashPattern; + m_dirty |= DirtyDash; +} + +void QQuickPathItemNvprRenderer::setFillGradient(QQuickPathGradient *gradient) +{ + m_fillGradientActive = gradient != nullptr; + if (gradient) { + m_fillGradient.stops = gradient->sortedGradientStops(); + m_fillGradient.spread = gradient->spread(); + if (QQuickPathLinearGradient *g = qobject_cast(gradient)) { + m_fillGradient.start = QPointF(g->x1(), g->y1()); + m_fillGradient.end = QPointF(g->x2(), g->y2()); + } else { + Q_UNREACHABLE(); + } + } + m_dirty |= DirtyFillGradient; +} + +void QQuickPathItemNvprRenderer::endSync() +{ + // nothing to do here +} + +void QQuickPathItemNvprRenderer::setNode(QQuickPathItemNvprRenderNode *node) +{ + if (m_node != node) { + m_node = node; + // Scenegraph nodes can be destroyed and then replaced by new ones over + // time; hence it is important to mark everything dirty for + // updatePathRenderNode(). We can assume the renderer has a full sync + // of the data at this point. + m_dirty = DirtyAll; + } +} + +QDebug operator<<(QDebug debug, const QQuickPathItemNvprRenderer::NvprPath &path) +{ + QDebugStateSaver saver(debug); + debug.space().noquote(); + debug << "Path with" << path.cmd.count() << "commands"; + int ci = 0; + for (GLubyte cmd : path.cmd) { + static struct { GLubyte cmd; const char *s; int coordCount; } nameTab[] = { + { GL_MOVE_TO_NV, "moveTo", 2 }, + { GL_LINE_TO_NV, "lineTo", 2 }, + { GL_QUADRATIC_CURVE_TO_NV, "quadTo", 4 }, + { GL_CUBIC_CURVE_TO_NV, "cubicTo", 6 }, + { GL_LARGE_CW_ARC_TO_NV, "arcTo-large-CW", 5 }, + { GL_LARGE_CCW_ARC_TO_NV, "arcTo-large-CCW", 5 }, + { GL_SMALL_CW_ARC_TO_NV, "arcTo-small-CW", 5 }, + { GL_SMALL_CCW_ARC_TO_NV, "arcTo-small-CCW", 5 }, + { GL_CLOSE_PATH_NV, "closePath", 0 } }; + for (size_t i = 0; i < sizeof(nameTab) / sizeof(nameTab[0]); ++i) { + if (nameTab[i].cmd == cmd) { + QByteArray cs; + for (int j = 0; j < nameTab[i].coordCount; ++j) { + cs.append(QByteArray::number(path.coord[ci++])); + cs.append(' '); + } + debug << "\n " << nameTab[i].s << " " << cs; + break; + } + } + } + return debug; +} + +static inline void appendCoords(QVector *v, QQuickCurve *c, QPointF *pos) +{ + QPointF p(c->hasRelativeX() ? pos->x() + c->relativeX() : c->x(), + c->hasRelativeY() ? pos->y() + c->relativeY() : c->y()); + v->append(p.x()); + v->append(p.y()); + *pos = p; +} + +static inline void appendControlCoords(QVector *v, QQuickPathQuad *c, const QPointF &pos) +{ + QPointF p(c->hasRelativeControlX() ? pos.x() + c->relativeControlX() : c->controlX(), + c->hasRelativeControlY() ? pos.y() + c->relativeControlY() : c->controlY()); + v->append(p.x()); + v->append(p.y()); +} + +static inline void appendControl1Coords(QVector *v, QQuickPathCubic *c, const QPointF &pos) +{ + QPointF p(c->hasRelativeControl1X() ? pos.x() + c->relativeControl1X() : c->control1X(), + c->hasRelativeControl1Y() ? pos.y() + c->relativeControl1Y() : c->control1Y()); + v->append(p.x()); + v->append(p.y()); +} + +static inline void appendControl2Coords(QVector *v, QQuickPathCubic *c, const QPointF &pos) +{ + QPointF p(c->hasRelativeControl2X() ? pos.x() + c->relativeControl2X() : c->control2X(), + c->hasRelativeControl2Y() ? pos.y() + c->relativeControl2Y() : c->control2Y()); + v->append(p.x()); + v->append(p.y()); +} + +void QQuickPathItemNvprRenderer::convertPath(const QQuickPath *path) +{ + m_path = NvprPath(); + if (!path) + return; + + const QList &pp(QQuickPathPrivate::get(path)->_pathElements); + if (pp.isEmpty()) + return; + + QPointF pos(path->startX(), path->startY()); + m_path.cmd.append(GL_MOVE_TO_NV); + m_path.coord.append(pos.x()); + m_path.coord.append(pos.y()); + + for (QQuickPathElement *e : pp) { + if (QQuickPathMove *o = qobject_cast(e)) { + m_path.cmd.append(GL_MOVE_TO_NV); + appendCoords(&m_path.coord, o, &pos); + } else if (QQuickPathLine *o = qobject_cast(e)) { + m_path.cmd.append(GL_LINE_TO_NV); + appendCoords(&m_path.coord, o, &pos); + } else if (QQuickPathQuad *o = qobject_cast(e)) { + m_path.cmd.append(GL_QUADRATIC_CURVE_TO_NV); + appendControlCoords(&m_path.coord, o, pos); + appendCoords(&m_path.coord, o, &pos); + } else if (QQuickPathCubic *o = qobject_cast(e)) { + m_path.cmd.append(GL_CUBIC_CURVE_TO_NV); + appendControl1Coords(&m_path.coord, o, pos); + appendControl2Coords(&m_path.coord, o, pos); + appendCoords(&m_path.coord, o, &pos); + } else if (QQuickPathArc *o = qobject_cast(e)) { + const bool sweepFlag = o->direction() == QQuickPathArc::Clockwise; // maps to CCW, not a typo + GLenum cmd; + if (o->useLargeArc()) + cmd = sweepFlag ? GL_LARGE_CCW_ARC_TO_NV : GL_LARGE_CW_ARC_TO_NV; + else + cmd = sweepFlag ? GL_SMALL_CCW_ARC_TO_NV : GL_SMALL_CW_ARC_TO_NV; + m_path.cmd.append(cmd); + m_path.coord.append(o->radiusX()); + m_path.coord.append(o->radiusY()); + m_path.coord.append(0.0f); // X axis rotation + appendCoords(&m_path.coord, o, &pos); + } else { + qWarning() << "PathItem/NVPR: unsupported Path element" << e; + } + } + + if (qFuzzyCompare(pos.x(), path->startX()) && qFuzzyCompare(pos.y(), path->startY())) + m_path.cmd.append(GL_CLOSE_PATH_NV); +} + +static inline QVector4D qsg_premultiply(const QColor &c, float globalOpacity) +{ + const float o = c.alphaF() * globalOpacity; + return QVector4D(c.redF() * o, c.greenF() * o, c.blueF() * o, o); +} + +void QQuickPathItemNvprRenderer::updatePathRenderNode() +{ + // Called on the render thread with gui blocked -> update the node with its + // own copy of all relevant data. + + if (!m_dirty) + return; + + // updatePathRenderNode() can be called several times with different dirty + // state before render() gets invoked. So accumulate. + m_node->m_dirty |= m_dirty; + + if (m_dirty & DirtyPath) + m_node->m_source = m_path; + + if (m_dirty & DirtyStyle) { + m_node->m_strokeWidth = m_strokeWidth; + m_node->m_strokeColor = qsg_premultiply(m_strokeColor, 1.0f); + m_node->m_fillColor = qsg_premultiply(m_fillColor, 1.0f); + switch (m_joinStyle) { + case QQuickPathItem::MiterJoin: + m_node->m_joinStyle = GL_MITER_TRUNCATE_NV; + break; + case QQuickPathItem::BevelJoin: + m_node->m_joinStyle = GL_BEVEL_NV; + break; + case QQuickPathItem::RoundJoin: + m_node->m_joinStyle = GL_ROUND_NV; + break; + default: + Q_UNREACHABLE(); + } + m_node->m_miterLimit = m_miterLimit; + switch (m_capStyle) { + case QQuickPathItem::FlatCap: + m_node->m_capStyle = GL_FLAT; + break; + case QQuickPathItem::SquareCap: + m_node->m_capStyle = GL_SQUARE_NV; + break; + case QQuickPathItem::RoundCap: + m_node->m_capStyle = GL_ROUND_NV; + break; + default: + Q_UNREACHABLE(); + } + } + + if (m_dirty & DirtyFillRule) { + switch (m_fillRule) { + case QQuickPathItem::OddEvenFill: + m_node->m_fillRule = GL_COUNT_UP_NV; + break; + case QQuickPathItem::WindingFill: + m_node->m_fillRule = GL_INVERT; + break; + default: + Q_UNREACHABLE(); + } + } + + if (m_dirty & DirtyDash) { + m_node->m_dashOffset = m_dashOffset; + if (m_dashActive) { + m_node->m_dashPattern.resize(m_dashPattern.count()); + // Multiply by strokeWidth because the PathItem API follows QPen + // meaning the input dash pattern here is in width units. + for (int i = 0; i < m_dashPattern.count(); ++i) + m_node->m_dashPattern[i] = GLfloat(m_dashPattern[i]) * m_strokeWidth; + } else { + m_node->m_dashPattern.clear(); + } + } + + if (m_dirty & DirtyFillGradient) { + m_node->m_fillGradientActive = m_fillGradientActive; + if (m_fillGradientActive) + m_node->m_fillGradient = m_fillGradient; + } + + m_node->markDirty(QSGNode::DirtyMaterial); + m_dirty = 0; +} + +bool QQuickPathItemNvprRenderNode::nvprInited = false; +QQuickNvprFunctions QQuickPathItemNvprRenderNode::nvpr; +QQuickNvprMaterialManager QQuickPathItemNvprRenderNode::mtlmgr; + +QQuickPathItemNvprRenderNode::QQuickPathItemNvprRenderNode(QQuickPathItem *item) + : m_item(item) +{ +} + +QQuickPathItemNvprRenderNode::~QQuickPathItemNvprRenderNode() +{ + releaseResources(); +} + +void QQuickPathItemNvprRenderNode::releaseResources() +{ + if (m_path) { + nvpr.deletePaths(m_path, 1); + m_path = 0; + } +} + +void QQuickNvprMaterialManager::create(QQuickNvprFunctions *nvpr) +{ + m_nvpr = nvpr; +} + +void QQuickNvprMaterialManager::releaseResources() +{ + QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions(); + for (MaterialDesc &mtl : m_materials) { + if (mtl.ppl) { + f->glDeleteProgramPipelines(1, &mtl.ppl); + mtl = MaterialDesc(); + } + } +} + +QQuickNvprMaterialManager::MaterialDesc *QQuickNvprMaterialManager::activateMaterial(Material m) +{ + QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions(); + MaterialDesc &mtl(m_materials[m]); + + if (!mtl.ppl) { + if (m == MatSolid) { + static const char *fragSrc = + "#version 310 es\n" + "precision highp float;\n" + "out vec4 fragColor;\n" + "uniform vec4 color;\n" + "uniform float opacity;\n" + "void main() {\n" + " fragColor = color * opacity;\n" + "}\n"; + if (!m_nvpr->createFragmentOnlyPipeline(fragSrc, &mtl.ppl, &mtl.prg)) { + qWarning("NVPR: Failed to create shader pipeline for solid fill"); + return nullptr; + } + Q_ASSERT(mtl.ppl && mtl.prg); + mtl.uniLoc[0] = f->glGetProgramResourceLocation(mtl.prg, GL_UNIFORM, "color"); + Q_ASSERT(mtl.uniLoc[0] >= 0); + mtl.uniLoc[1] = f->glGetProgramResourceLocation(mtl.prg, GL_UNIFORM, "opacity"); + Q_ASSERT(mtl.uniLoc[1] >= 0); + } else if (m == MatLinearGradient) { + static const char *fragSrc = + "#version 310 es\n" + "precision highp float;\n" + "layout(location = 0) in vec2 uv;" + "uniform float opacity;\n" + "uniform sampler2D gradTab;\n" + "uniform vec2 gradStart;\n" + "uniform vec2 gradEnd;\n" + "out vec4 fragColor;\n" + "void main() {\n" + " vec2 gradVec = gradEnd - gradStart;\n" + " float gradTabIndex = dot(gradVec, uv - gradStart) / (gradVec.x * gradVec.x + gradVec.y * gradVec.y);\n" + " fragColor = texture(gradTab, vec2(gradTabIndex, 0.5)) * opacity;\n" + "}\n"; + if (!m_nvpr->createFragmentOnlyPipeline(fragSrc, &mtl.ppl, &mtl.prg)) { + qWarning("NVPR: Failed to create shader pipeline for linear gradient"); + return nullptr; + } + Q_ASSERT(mtl.ppl && mtl.prg); + mtl.uniLoc[1] = f->glGetProgramResourceLocation(mtl.prg, GL_UNIFORM, "opacity"); + Q_ASSERT(mtl.uniLoc[1] >= 0); + mtl.uniLoc[2] = f->glGetProgramResourceLocation(mtl.prg, GL_UNIFORM, "gradStart"); + Q_ASSERT(mtl.uniLoc[2] >= 0); + mtl.uniLoc[3] = f->glGetProgramResourceLocation(mtl.prg, GL_UNIFORM, "gradEnd"); + Q_ASSERT(mtl.uniLoc[3] >= 0); + } else { + Q_UNREACHABLE(); + } + } + + f->glBindProgramPipeline(mtl.ppl); + + return &mtl; +} + +void QQuickPathItemNvprRenderNode::updatePath() +{ + if (m_dirty & QQuickPathItemNvprRenderer::DirtyPath) { + if (!m_path) { + m_path = nvpr.genPaths(1); + Q_ASSERT(m_path != 0); + } + nvpr.pathCommands(m_path, m_source.cmd.count(), m_source.cmd.constData(), + m_source.coord.count(), GL_FLOAT, m_source.coord.constData()); + } + + if (m_dirty & QQuickPathItemNvprRenderer::DirtyStyle) { + nvpr.pathParameterf(m_path, GL_PATH_STROKE_WIDTH_NV, m_strokeWidth); + nvpr.pathParameteri(m_path, GL_PATH_JOIN_STYLE_NV, m_joinStyle); + nvpr.pathParameteri(m_path, GL_PATH_MITER_LIMIT_NV, m_miterLimit); + nvpr.pathParameteri(m_path, GL_PATH_END_CAPS_NV, m_capStyle); + nvpr.pathParameteri(m_path, GL_PATH_DASH_CAPS_NV, m_capStyle); + } + + if (m_dirty & QQuickPathItemNvprRenderer::DirtyDash) { + nvpr.pathParameterf(m_path, GL_PATH_DASH_OFFSET_NV, m_dashOffset); + // count == 0 -> no dash + nvpr.pathDashArray(m_path, m_dashPattern.count(), m_dashPattern.constData()); + } +} + +void QQuickPathItemNvprRenderNode::render(const RenderState *state) +{ + if (!nvprInited) { + if (!nvpr.create()) { + qWarning("NVPR init failed"); + return; + } + mtlmgr.create(&nvpr); + nvprInited = true; + } + + updatePath(); + + QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions(); + f->glUseProgram(0); + QQuickNvprMaterialManager::MaterialDesc *mtl; + if (m_fillGradientActive) + mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatLinearGradient); + else + mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatSolid); + if (!mtl) + return; + + // Assume stencil buffer is cleared to 0 for each frame. + // Within the frame dppass=GL_ZERO for glStencilOp ensures stencil is reset and so no need to clear. + f->glStencilMask(~0); + f->glEnable(GL_STENCIL_TEST); + f->glStencilFunc(GL_NOTEQUAL, 0, 0xFF); + f->glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO); + + // Depth test against the opaque batches rendered before. + f->glEnable(GL_DEPTH_TEST); + f->glDepthFunc(GL_LESS); + nvpr.pathCoverDepthFunc(GL_LESS); + nvpr.pathStencilDepthOffset(-0.05f, -1); + + nvpr.matrixLoadf(GL_PATH_MODELVIEW_NV, matrix()->constData()); + nvpr.matrixLoadf(GL_PATH_PROJECTION_NV, state->projectionMatrix()->constData()); + + if (state->scissorEnabled()) { + // scissor rect is already set, just enable scissoring + f->glEnable(GL_SCISSOR_TEST); + } + + if (!qFuzzyIsNull(m_fillColor.w()) || m_fillGradientActive) { + if (m_fillGradientActive) { + QSGTexture *tx = QQuickPathItemGradientCache::currentCache()->get(m_fillGradient); + tx->bind(); + // uv = vec2(coeff[0] * x + coeff[1] * y + coeff[2], coeff[3] * x + coeff[4] * y + coeff[5]) + // where x and y are in path coordinate space, which is just what + // we need since the gradient's start and stop are in that space too. + GLfloat coeff[6] = { 1, 0, 0, + 0, 1, 0 }; + nvpr.programPathFragmentInputGen(mtl->prg, 0, GL_OBJECT_LINEAR_NV, 2, coeff); + f->glProgramUniform2f(mtl->prg, mtl->uniLoc[2], m_fillGradient.start.x(), m_fillGradient.start.y()); + f->glProgramUniform2f(mtl->prg, mtl->uniLoc[3], m_fillGradient.end.x(), m_fillGradient.end.y()); + } else { + f->glProgramUniform4f(mtl->prg, mtl->uniLoc[0], + m_fillColor.x(), m_fillColor.y(), m_fillColor.z(), m_fillColor.w()); + } + f->glProgramUniform1f(mtl->prg, mtl->uniLoc[1], inheritedOpacity()); + nvpr.stencilThenCoverFillPath(m_path, m_fillRule, 0xFF, GL_BOUNDING_BOX_NV); + } + + if (!qFuzzyIsNull(m_strokeWidth) && !qFuzzyIsNull(m_strokeColor.w())) { + if (m_fillGradientActive) + mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatSolid); + f->glProgramUniform4f(mtl->prg, mtl->uniLoc[0], + m_strokeColor.x(), m_strokeColor.y(), m_strokeColor.z(), m_strokeColor.w()); + f->glProgramUniform1f(mtl->prg, mtl->uniLoc[1], inheritedOpacity()); + nvpr.stencilThenCoverStrokePath(m_path, 0x1, ~0, GL_CONVEX_HULL_NV); + } + + f->glBindProgramPipeline(0); + + m_dirty = 0; +} + +QSGRenderNode::StateFlags QQuickPathItemNvprRenderNode::changedStates() const +{ + return BlendState | StencilState | DepthState | ScissorState; +} + +QSGRenderNode::RenderingFlags QQuickPathItemNvprRenderNode::flags() const +{ + return DepthAwareRendering; // avoid hitting the less optimal no-opaque-batch path in the renderer +} + +QRectF QQuickPathItemNvprRenderNode::rect() const +{ + return QRect(0, 0, m_item->width(), m_item->height()); +} + +bool QQuickPathItemNvprRenderNode::isSupported() +{ + static const bool nvprDisabled = qEnvironmentVariableIntValue("QT_NO_NVPR") != 0; + return !nvprDisabled && QQuickNvprFunctions::isSupported(); +} + +QT_END_NAMESPACE diff --git a/src/quick/items/qquickpathitemnvprrenderer_p.h b/src/quick/items/qquickpathitemnvprrenderer_p.h new file mode 100644 index 0000000000..0075572324 --- /dev/null +++ b/src/quick/items/qquickpathitemnvprrenderer_p.h @@ -0,0 +1,194 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKPATHITEMNVPRRENDERER_P_H +#define QQUICKPATHITEMNVPRRENDERER_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 "qquickpathitem_p_p.h" +#include +#include +#include +#include +#include + +#ifndef QT_NO_OPENGL + +QT_BEGIN_NAMESPACE + +class QQuickPathItemNvprRenderNode; + +class QQuickPathItemNvprRenderer : public QQuickAbstractPathRenderer +{ +public: + enum Dirty { + DirtyPath = 0x01, + DirtyStyle = 0x02, + DirtyFillRule = 0x04, + DirtyDash = 0x08, + DirtyFillGradient = 0x10, + + DirtyAll = 0xFF + }; + + void beginSync() override; + void setPath(const QQuickPath *path) override; + void setStrokeColor(const QColor &color) override; + void setStrokeWidth(qreal w) override; + void setFillColor(const QColor &color) override; + void setFillRule(QQuickPathItem::FillRule fillRule) override; + void setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) override; + void setCapStyle(QQuickPathItem::CapStyle capStyle) override; + void setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, + qreal dashOffset, const QVector &dashPattern) override; + void setFillGradient(QQuickPathGradient *gradient) override; + void endSync() override; + void updatePathRenderNode() override; + + void setNode(QQuickPathItemNvprRenderNode *node); + + struct NvprPath { + QVector cmd; + QVector coord; + }; + +private: + void convertPath(const QQuickPath *path); + + QQuickPathItemNvprRenderNode *m_node = nullptr; + int m_dirty = 0; + + NvprPath m_path; + qreal m_strokeWidth; + QColor m_strokeColor; + QColor m_fillColor; + QQuickPathItem::JoinStyle m_joinStyle; + int m_miterLimit; + QQuickPathItem::CapStyle m_capStyle; + QQuickPathItem::FillRule m_fillRule; + bool m_dashActive; + qreal m_dashOffset; + QVector m_dashPattern; + bool m_fillGradientActive; + QQuickPathItemGradientCache::GradientDesc m_fillGradient; +}; + +QDebug operator<<(QDebug debug, const QQuickPathItemNvprRenderer::NvprPath &path); + +class QQuickNvprMaterialManager +{ +public: + enum Material { + MatSolid, + MatLinearGradient, + + NMaterials + }; + + struct MaterialDesc { + GLuint ppl = 0; + GLuint prg = 0; + int uniLoc[4]; + }; + + void create(QQuickNvprFunctions *nvpr); + MaterialDesc *activateMaterial(Material m); + void releaseResources(); + +private: + QQuickNvprFunctions *m_nvpr; + MaterialDesc m_materials[NMaterials]; +}; + +class QQuickPathItemNvprRenderNode : public QSGRenderNode +{ +public: + QQuickPathItemNvprRenderNode(QQuickPathItem *item); + ~QQuickPathItemNvprRenderNode(); + + void render(const RenderState *state) override; + void releaseResources() override; + StateFlags changedStates() const override; + RenderingFlags flags() const override; + QRectF rect() const override; + + static bool isSupported(); + +private: + void updatePath(); + + static bool nvprInited; + static QQuickNvprFunctions nvpr; + static QQuickNvprMaterialManager mtlmgr; + + QQuickPathItem *m_item; + GLuint m_path = 0; + int m_dirty = 0; + + QQuickPathItemNvprRenderer::NvprPath m_source; + GLfloat m_strokeWidth; + QVector4D m_strokeColor; + QVector4D m_fillColor; + GLenum m_joinStyle; + GLint m_miterLimit; + GLenum m_capStyle; + GLenum m_fillRule; + GLfloat m_dashOffset; + QVector m_dashPattern; + bool m_fillGradientActive; + QQuickPathItemGradientCache::GradientDesc m_fillGradient; + + friend class QQuickPathItemNvprRenderer; +}; + +QT_END_NAMESPACE + +#endif // QT_NO_OPENGL + +#endif // QQUICKPATHITEMNVPRRENDERER_P_H diff --git a/src/quick/items/qquickpathitemsoftwarerenderer.cpp b/src/quick/items/qquickpathitemsoftwarerenderer.cpp new file mode 100644 index 0000000000..40732e4bf9 --- /dev/null +++ b/src/quick/items/qquickpathitemsoftwarerenderer.cpp @@ -0,0 +1,234 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickpathitemsoftwarerenderer_p.h" +#include + +QT_BEGIN_NAMESPACE + +void QQuickPathItemSoftwareRenderer::beginSync() +{ + // nothing to do here +} + +void QQuickPathItemSoftwareRenderer::setPath(const QQuickPath *path) +{ + m_path = path ? path->path() : QPainterPath(); + m_dirty |= DirtyPath; +} + +void QQuickPathItemSoftwareRenderer::setStrokeColor(const QColor &color) +{ + m_pen.setColor(color); + m_dirty |= DirtyPen; +} + +void QQuickPathItemSoftwareRenderer::setStrokeWidth(qreal w) +{ + m_pen.setWidthF(w); + m_dirty |= DirtyPen; +} + +void QQuickPathItemSoftwareRenderer::setFillColor(const QColor &color) +{ + m_fillColor = color; + m_brush.setColor(m_fillColor); + m_dirty |= DirtyBrush; +} + +void QQuickPathItemSoftwareRenderer::setFillRule(QQuickPathItem::FillRule fillRule) +{ + m_fillRule = Qt::FillRule(fillRule); + m_dirty |= DirtyFillRule; +} + +void QQuickPathItemSoftwareRenderer::setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) +{ + m_pen.setJoinStyle(Qt::PenJoinStyle(joinStyle)); + m_pen.setMiterLimit(miterLimit); + m_dirty |= DirtyPen; +} + +void QQuickPathItemSoftwareRenderer::setCapStyle(QQuickPathItem::CapStyle capStyle) +{ + m_pen.setCapStyle(Qt::PenCapStyle(capStyle)); + m_dirty |= DirtyPen; +} + +void QQuickPathItemSoftwareRenderer::setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, + qreal dashOffset, const QVector &dashPattern) +{ + switch (strokeStyle) { + case QQuickPathItem::SolidLine: + m_pen.setStyle(Qt::SolidLine); + break; + case QQuickPathItem::DashLine: + m_pen.setStyle(Qt::CustomDashLine); + m_pen.setDashPattern(dashPattern); + m_pen.setDashOffset(dashOffset); + break; + default: + break; + } + m_dirty |= DirtyPen; +} + +void QQuickPathItemSoftwareRenderer::setFillGradient(QQuickPathGradient *gradient) +{ + if (QQuickPathLinearGradient *linearGradient = qobject_cast(gradient)) { + QLinearGradient painterGradient(linearGradient->x1(), linearGradient->y1(), + linearGradient->x2(), linearGradient->y2()); + painterGradient.setStops(linearGradient->sortedGradientStops()); + switch (gradient->spread()) { + case QQuickPathGradient::PadSpread: + painterGradient.setSpread(QGradient::PadSpread); + break; + case QQuickPathGradient::RepeatSpread: + painterGradient.setSpread(QGradient::RepeatSpread); + break; + case QQuickPathGradient::ReflectSpread: + painterGradient.setSpread(QGradient::ReflectSpread); + break; + default: + break; + } + m_brush = QBrush(painterGradient); + } else { + m_brush = QBrush(m_fillColor); + } + m_dirty |= DirtyBrush; +} + +void QQuickPathItemSoftwareRenderer::endSync() +{ + // nothing to do here +} + +void QQuickPathItemSoftwareRenderer::setNode(QQuickPathItemSoftwareRenderNode *node) +{ + if (m_node != node) { + m_node = node; + // Scenegraph nodes can be destroyed and then replaced by new ones over + // time; hence it is important to mark everything dirty for + // updatePathRenderNode(). We can assume the renderer has a full sync + // of the data at this point. + m_dirty = DirtyAll; + } +} + +void QQuickPathItemSoftwareRenderer::updatePathRenderNode() +{ + if (!m_dirty) + return; + + // updatePathRenderNode() can be called several times with different dirty + // state before render() gets invoked. So accumulate. + m_node->m_dirty |= m_dirty; + + if (m_dirty & DirtyPath) { + m_node->m_path = m_path; + m_node->m_path.setFillRule(m_fillRule); + } + + if (m_dirty & DirtyFillRule) + m_node->m_path.setFillRule(m_fillRule); + + if (m_dirty & DirtyPen) + m_node->m_pen = m_pen; + + if (m_dirty & DirtyBrush) + m_node->m_brush = m_brush; + + m_node->markDirty(QSGNode::DirtyMaterial); + m_dirty = 0; +} + +QQuickPathItemSoftwareRenderNode::QQuickPathItemSoftwareRenderNode(QQuickPathItem *item) + : m_item(item) +{ +} + +QQuickPathItemSoftwareRenderNode::~QQuickPathItemSoftwareRenderNode() +{ + releaseResources(); +} + +void QQuickPathItemSoftwareRenderNode::releaseResources() +{ +} + +void QQuickPathItemSoftwareRenderNode::render(const RenderState *state) +{ + if (m_path.isEmpty()) + return; + + QSGRendererInterface *rif = m_item->window()->rendererInterface(); + QPainter *p = static_cast(rif->getResource(m_item->window(), QSGRendererInterface::PainterResource)); + Q_ASSERT(p); + + p->setTransform(matrix()->toTransform()); + p->setOpacity(inheritedOpacity()); + + const QRegion *clipRegion = state->clipRegion(); + if (clipRegion && !clipRegion->isEmpty()) + p->setClipRegion(*clipRegion, Qt::IntersectClip); + + p->setPen(!qFuzzyIsNull(m_pen.widthF()) && m_pen.color() != Qt::transparent ? m_pen : Qt::NoPen); + p->setBrush(m_brush.color() != Qt::transparent ? m_brush : Qt::NoBrush); + p->drawPath(m_path); + + m_dirty = 0; +} + +QSGRenderNode::StateFlags QQuickPathItemSoftwareRenderNode::changedStates() const +{ + return 0; +} + +QSGRenderNode::RenderingFlags QQuickPathItemSoftwareRenderNode::flags() const +{ + return BoundedRectRendering; // avoid fullscreen updates by saying we won't draw outside rect() +} + +QRectF QQuickPathItemSoftwareRenderNode::rect() const +{ + return QRect(0, 0, m_item->width(), m_item->height()); +} + +QT_END_NAMESPACE diff --git a/src/quick/items/qquickpathitemsoftwarerenderer_p.h b/src/quick/items/qquickpathitemsoftwarerenderer_p.h new file mode 100644 index 0000000000..584771425d --- /dev/null +++ b/src/quick/items/qquickpathitemsoftwarerenderer_p.h @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKPATHITEMSOFTWARERENDERER_P_H +#define QQUICKPATHITEMSOFTWARERENDERER_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 "qquickpathitem_p_p.h" +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QQuickPathItemSoftwareRenderNode; + +class QQuickPathItemSoftwareRenderer : public QQuickAbstractPathRenderer +{ +public: + enum Dirty { + DirtyPath = 0x01, + DirtyPen = 0x02, + DirtyFillRule = 0x04, + DirtyBrush = 0x08, + + DirtyAll = 0xFF + }; + + void beginSync() override; + void setPath(const QQuickPath *path) override; + void setStrokeColor(const QColor &color) override; + void setStrokeWidth(qreal w) override; + void setFillColor(const QColor &color) override; + void setFillRule(QQuickPathItem::FillRule fillRule) override; + void setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) override; + void setCapStyle(QQuickPathItem::CapStyle capStyle) override; + void setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, + qreal dashOffset, const QVector &dashPattern) override; + void setFillGradient(QQuickPathGradient *gradient) override; + void endSync() override; + void updatePathRenderNode() override; + + void setNode(QQuickPathItemSoftwareRenderNode *node); + +private: + QQuickPathItemSoftwareRenderNode *m_node = nullptr; + int m_dirty = 0; + + QPainterPath m_path; + QPen m_pen; + QColor m_fillColor; + QBrush m_brush; + Qt::FillRule m_fillRule; +}; + +class QQuickPathItemSoftwareRenderNode : public QSGRenderNode +{ +public: + QQuickPathItemSoftwareRenderNode(QQuickPathItem *item); + ~QQuickPathItemSoftwareRenderNode(); + + void render(const RenderState *state) override; + void releaseResources() override; + StateFlags changedStates() const override; + RenderingFlags flags() const override; + QRectF rect() const override; + +private: + QQuickPathItem *m_item; + int m_dirty = 0; + + QPainterPath m_path; + QPen m_pen; + QBrush m_brush; + + friend class QQuickPathItemSoftwareRenderer; +}; + +QT_END_NAMESPACE + +#endif // QQUICKPATHITEMSOFTWARERENDERER_P_H diff --git a/src/quick/items/shaders/lineargradient.frag b/src/quick/items/shaders/lineargradient.frag new file mode 100644 index 0000000000..7f4a739109 --- /dev/null +++ b/src/quick/items/shaders/lineargradient.frag @@ -0,0 +1,9 @@ +uniform sampler2D gradTabTexture; +uniform highp float opacity; + +varying highp float gradTabIndex; + +void main() +{ + gl_FragColor = texture2D(gradTabTexture, vec2(gradTabIndex, 0.5)) * opacity; +} diff --git a/src/quick/items/shaders/lineargradient.vert b/src/quick/items/shaders/lineargradient.vert new file mode 100644 index 0000000000..eb21b8886b --- /dev/null +++ b/src/quick/items/shaders/lineargradient.vert @@ -0,0 +1,15 @@ +attribute vec4 vertexCoord; +attribute vec4 vertexColor; + +uniform mat4 matrix; +uniform vec2 gradStart; +uniform vec2 gradEnd; + +varying float gradTabIndex; + +void main() +{ + vec2 gradVec = gradEnd - gradStart; + gradTabIndex = dot(gradVec, vertexCoord.xy - gradStart) / (gradVec.x * gradVec.x + gradVec.y * gradVec.y); + gl_Position = matrix * vertexCoord; +} diff --git a/src/quick/items/shaders/lineargradient_core.frag b/src/quick/items/shaders/lineargradient_core.frag new file mode 100644 index 0000000000..5908acfa67 --- /dev/null +++ b/src/quick/items/shaders/lineargradient_core.frag @@ -0,0 +1,12 @@ +#version 150 core + +uniform sampler2D gradTabTexture; +uniform float opacity; + +in float gradTabIndex; +out vec4 fragColor; + +void main() +{ + fragColor = texture(gradTabTexture, vec2(gradTabIndex, 0.5)) * opacity; +} diff --git a/src/quick/items/shaders/lineargradient_core.vert b/src/quick/items/shaders/lineargradient_core.vert new file mode 100644 index 0000000000..60b56f38e3 --- /dev/null +++ b/src/quick/items/shaders/lineargradient_core.vert @@ -0,0 +1,17 @@ +#version 150 core + +in vec4 vertexCoord; +in vec4 vertexColor; + +uniform mat4 matrix; +uniform vec2 gradStart; +uniform vec2 gradEnd; + +out float gradTabIndex; + +void main() +{ + vec2 gradVec = gradEnd - gradStart; + gradTabIndex = dot(gradVec, vertexCoord.xy - gradStart) / (gradVec.x * gradVec.x + gradVec.y * gradVec.y); + gl_Position = matrix * vertexCoord; +} -- cgit v1.2.3 From bf7c6226264bd45095711b2c0556c42b6f267f72 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Mon, 19 Dec 2016 13:53:29 +0100 Subject: software backend: Fix clipping of QSGRenderNodes Change-Id: I27aa5f94165fb07807d2bb711d81eade552b9f76 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemsoftwarerenderer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemsoftwarerenderer.cpp b/src/quick/items/qquickpathitemsoftwarerenderer.cpp index 40732e4bf9..f089904fdf 100644 --- a/src/quick/items/qquickpathitemsoftwarerenderer.cpp +++ b/src/quick/items/qquickpathitemsoftwarerenderer.cpp @@ -202,12 +202,12 @@ void QQuickPathItemSoftwareRenderNode::render(const RenderState *state) QPainter *p = static_cast(rif->getResource(m_item->window(), QSGRendererInterface::PainterResource)); Q_ASSERT(p); - p->setTransform(matrix()->toTransform()); - p->setOpacity(inheritedOpacity()); - const QRegion *clipRegion = state->clipRegion(); if (clipRegion && !clipRegion->isEmpty()) - p->setClipRegion(*clipRegion, Qt::IntersectClip); + p->setClipRegion(*clipRegion, Qt::ReplaceClip); // must be done before setTransform + + p->setTransform(matrix()->toTransform()); + p->setOpacity(inheritedOpacity()); p->setPen(!qFuzzyIsNull(m_pen.widthF()) && m_pen.color() != Qt::transparent ? m_pen : Qt::NoPen); p->setBrush(m_brush.color() != Qt::transparent ? m_brush : Qt::NoBrush); -- cgit v1.2.3 From 79831caa0533ad5f48322568557622596b85ed0f Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 21 Dec 2016 13:15:38 +0100 Subject: Treat 0 as a valid strokeWidth value Use negative values as the trigger to disable stroking altogether. This matches both QPainter and NVPR better. Change-Id: I51395ae310fce8a8da0c06174eafa1dc17aae1db Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitem.cpp | 2 +- src/quick/items/qquickpathitemgenericrenderer.cpp | 6 ++++-- src/quick/items/qquickpathitemgenericrenderer_p.h | 1 + src/quick/items/qquickpathitemnvprrenderer.cpp | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index 0d2c7a8bbe..720916c900 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -455,7 +455,7 @@ QSGNode *QQuickPathItemPrivate::createRenderNode() return node; const bool hasFill = fillColor != Qt::transparent; - const bool hasStroke = !qFuzzyIsNull(strokeWidth) && strokeColor != Qt::transparent; + const bool hasStroke = strokeWidth >= 0.0f && strokeColor != Qt::transparent; switch (ri->graphicsApi()) { #ifndef QT_NO_OPENGL diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index 9bd03c0e51..4b15daef9a 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -145,7 +145,9 @@ void QQuickPathItemGenericRenderer::setStrokeColor(const QColor &color) void QQuickPathItemGenericRenderer::setStrokeWidth(qreal w) { - m_pen.setWidthF(w); + m_strokeWidth = w; + if (w >= 0.0f) + m_pen.setWidthF(w); m_syncDirty |= DirtyGeom; } @@ -311,7 +313,7 @@ void QQuickPathItemGenericRenderer::updatePathRenderNode() m_effectiveDirty |= DirtyGeom; } - if (qFuzzyIsNull(m_pen.widthF()) || m_strokeColor.a == 0) { + if (m_strokeWidth < 0.0f || m_strokeColor.a == 0) { delete m_rootNode->m_strokeNode; m_rootNode->m_strokeNode = nullptr; } else if (!m_rootNode->m_strokeNode) { diff --git a/src/quick/items/qquickpathitemgenericrenderer_p.h b/src/quick/items/qquickpathitemgenericrenderer_p.h index c186959c88..a094b9fca6 100644 --- a/src/quick/items/qquickpathitemgenericrenderer_p.h +++ b/src/quick/items/qquickpathitemgenericrenderer_p.h @@ -107,6 +107,7 @@ private: QTriangulatingStroker m_stroker; QDashedStrokeProcessor m_dashStroker; + float m_strokeWidth; QPen m_pen; Color4ub m_strokeColor; Color4ub m_fillColor; diff --git a/src/quick/items/qquickpathitemnvprrenderer.cpp b/src/quick/items/qquickpathitemnvprrenderer.cpp index d07a63c86d..e0c458bfca 100644 --- a/src/quick/items/qquickpathitemnvprrenderer.cpp +++ b/src/quick/items/qquickpathitemnvprrenderer.cpp @@ -529,7 +529,7 @@ void QQuickPathItemNvprRenderNode::render(const RenderState *state) nvpr.stencilThenCoverFillPath(m_path, m_fillRule, 0xFF, GL_BOUNDING_BOX_NV); } - if (!qFuzzyIsNull(m_strokeWidth) && !qFuzzyIsNull(m_strokeColor.w())) { + if (m_strokeWidth >= 0.0f && !qFuzzyIsNull(m_strokeColor.w())) { if (m_fillGradientActive) mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatSolid); f->glProgramUniform4f(mtl->prg, mtl->uniLoc[0], -- cgit v1.2.3 From aa24b8938bb03e688633e544dddeca5aff91940e Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Mon, 19 Dec 2016 13:54:58 +0100 Subject: Stencil clipping for NVPR Fix also the fill rule interpretation on NVPR - it was the opposite of what QPainter was doing. Change-Id: I23ff3b20e3b066d4b4e07aaa68b7da1e09d9127d Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitem.cpp | 2 +- src/quick/items/qquickpathitemnvprrenderer.cpp | 274 +++++++++++++++++++++---- src/quick/items/qquickpathitemnvprrenderer_p.h | 33 +++ 3 files changed, 270 insertions(+), 39 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index 720916c900..0a4c721a74 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -426,7 +426,7 @@ void QQuickPathItemPrivate::createRenderer() case QSGRendererInterface::OpenGL: if (QQuickPathItemNvprRenderNode::isSupported()) { rendererType = QQuickPathItem::NvprRenderer; - renderer = new QQuickPathItemNvprRenderer; + renderer = new QQuickPathItemNvprRenderer(q); } else { rendererType = QQuickPathItem::GeometryRenderer; renderer = new QQuickPathItemGenericRenderer(q); diff --git a/src/quick/items/qquickpathitemnvprrenderer.cpp b/src/quick/items/qquickpathitemnvprrenderer.cpp index e0c458bfca..bd88023891 100644 --- a/src/quick/items/qquickpathitemnvprrenderer.cpp +++ b/src/quick/items/qquickpathitemnvprrenderer.cpp @@ -39,6 +39,9 @@ #include "qquickpathitemnvprrenderer_p.h" #include +#include +#include +#include #include QT_BEGIN_NAMESPACE @@ -307,10 +310,10 @@ void QQuickPathItemNvprRenderer::updatePathRenderNode() if (m_dirty & DirtyFillRule) { switch (m_fillRule) { case QQuickPathItem::OddEvenFill: - m_node->m_fillRule = GL_COUNT_UP_NV; + m_node->m_fillRule = GL_INVERT; break; case QQuickPathItem::WindingFill: - m_node->m_fillRule = GL_INVERT; + m_node->m_fillRule = GL_COUNT_UP_NV; break; default: Q_UNREACHABLE(); @@ -360,6 +363,13 @@ void QQuickPathItemNvprRenderNode::releaseResources() nvpr.deletePaths(m_path, 1); m_path = 0; } + + if (m_fallbackFbo) { + delete m_fallbackFbo; + m_fallbackFbo = nullptr; + } + + m_fallbackBlitter.destroy(); } void QQuickNvprMaterialManager::create(QQuickNvprFunctions *nvpr) @@ -465,8 +475,95 @@ void QQuickPathItemNvprRenderNode::updatePath() } } +void QQuickPathItemNvprRenderNode::renderStroke(int strokeStencilValue, int writeMask) +{ + QQuickNvprMaterialManager::MaterialDesc *mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatSolid); + f->glProgramUniform4f(mtl->prg, mtl->uniLoc[0], + m_strokeColor.x(), m_strokeColor.y(), m_strokeColor.z(), m_strokeColor.w()); + f->glProgramUniform1f(mtl->prg, mtl->uniLoc[1], inheritedOpacity()); + + nvpr.stencilThenCoverStrokePath(m_path, strokeStencilValue, writeMask, GL_CONVEX_HULL_NV); +} + +void QQuickPathItemNvprRenderNode::renderFill() +{ + QQuickNvprMaterialManager::MaterialDesc *mtl = nullptr; + if (m_fillGradientActive) { + mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatLinearGradient); + QSGTexture *tx = QQuickPathItemGradientCache::currentCache()->get(m_fillGradient); + tx->bind(); + // uv = vec2(coeff[0] * x + coeff[1] * y + coeff[2], coeff[3] * x + coeff[4] * y + coeff[5]) + // where x and y are in path coordinate space, which is just what + // we need since the gradient's start and stop are in that space too. + GLfloat coeff[6] = { 1, 0, 0, + 0, 1, 0 }; + nvpr.programPathFragmentInputGen(mtl->prg, 0, GL_OBJECT_LINEAR_NV, 2, coeff); + f->glProgramUniform2f(mtl->prg, mtl->uniLoc[2], m_fillGradient.start.x(), m_fillGradient.start.y()); + f->glProgramUniform2f(mtl->prg, mtl->uniLoc[3], m_fillGradient.end.x(), m_fillGradient.end.y()); + } else { + mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatSolid); + f->glProgramUniform4f(mtl->prg, mtl->uniLoc[0], + m_fillColor.x(), m_fillColor.y(), m_fillColor.z(), m_fillColor.w()); + } + f->glProgramUniform1f(mtl->prg, mtl->uniLoc[1], inheritedOpacity()); + + const int writeMask = 0xFF; + nvpr.stencilThenCoverFillPath(m_path, m_fillRule, writeMask, GL_BOUNDING_BOX_NV); +} + +void QQuickPathItemNvprRenderNode::renderOffscreenFill() +{ + QQuickWindow *w = m_item->window(); + const qreal dpr = w->effectiveDevicePixelRatio(); + QSize itemSize = QSize(m_item->width(), m_item->height()) * dpr; + QSize rtSize = w->renderTargetSize(); + if (rtSize.isEmpty()) + rtSize = w->size() * dpr; + + if (m_fallbackFbo && m_fallbackFbo->size() != itemSize) { + delete m_fallbackFbo; + m_fallbackFbo = nullptr; + } + if (!m_fallbackFbo) + m_fallbackFbo = new QOpenGLFramebufferObject(itemSize, QOpenGLFramebufferObject::CombinedDepthStencil); + if (!m_fallbackFbo->bind()) + return; + + f->glViewport(0, 0, itemSize.width(), itemSize.height()); + f->glClearColor(0, 0, 0, 0); + f->glClearStencil(0); + f->glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + f->glStencilFunc(GL_NOTEQUAL, 0, 0xFF); + f->glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); + + nvpr.matrixLoadIdentity(GL_PATH_MODELVIEW_NV); + QMatrix4x4 proj; + proj.ortho(0, itemSize.width(), itemSize.height(), 0, 1, -1); + nvpr.matrixLoadf(GL_PATH_PROJECTION_NV, proj.constData()); + + renderFill(); + + m_fallbackFbo->release(); + f->glViewport(0, 0, rtSize.width(), rtSize.height()); +} + +void QQuickPathItemNvprRenderNode::setupStencilForCover(bool stencilClip, int sv) +{ + if (!stencilClip) { + // Assume stencil buffer is cleared to 0 for each frame. + // Within the frame dppass=GL_ZERO for glStencilOp ensures stencil is reset and so no need to clear. + f->glStencilFunc(GL_NOTEQUAL, 0, 0xFF); + f->glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO); + } else { + f->glStencilFunc(GL_LESS, sv, 0xFF); // pass if (sv & 0xFF) < (stencil_value & 0xFF) + f->glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); // dppass: replace with the original value (clip's stencil ref value) + } +} + void QQuickPathItemNvprRenderNode::render(const RenderState *state) { + f = QOpenGLContext::currentContext()->extraFunctions(); + if (!nvprInited) { if (!nvpr.create()) { qWarning("NVPR init failed"); @@ -478,22 +575,23 @@ void QQuickPathItemNvprRenderNode::render(const RenderState *state) updatePath(); - QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions(); f->glUseProgram(0); - QQuickNvprMaterialManager::MaterialDesc *mtl; - if (m_fillGradientActive) - mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatLinearGradient); - else - mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatSolid); - if (!mtl) - return; - - // Assume stencil buffer is cleared to 0 for each frame. - // Within the frame dppass=GL_ZERO for glStencilOp ensures stencil is reset and so no need to clear. f->glStencilMask(~0); f->glEnable(GL_STENCIL_TEST); - f->glStencilFunc(GL_NOTEQUAL, 0, 0xFF); - f->glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO); + + const bool stencilClip = state->stencilEnabled(); + // when true, the stencil buffer already has a clip path with a ref value of sv + const int sv = state->stencilValue(); + + const bool hasFill = !qFuzzyIsNull(m_fillColor.w()) || m_fillGradientActive; + const bool hasStroke = m_strokeWidth >= 0.0f && !qFuzzyIsNull(m_strokeColor.w()); + + if (hasFill && stencilClip) { + // Fall back to a texture when complex clipping is in use and we have + // to fill. Reconciling glStencilFillPath's and the scenegraph's clip + // stencil semantics has not succeeded so far... + renderOffscreenFill(); + } // Depth test against the opaque batches rendered before. f->glEnable(GL_DEPTH_TEST); @@ -509,35 +607,43 @@ void QQuickPathItemNvprRenderNode::render(const RenderState *state) f->glEnable(GL_SCISSOR_TEST); } - if (!qFuzzyIsNull(m_fillColor.w()) || m_fillGradientActive) { - if (m_fillGradientActive) { - QSGTexture *tx = QQuickPathItemGradientCache::currentCache()->get(m_fillGradient); - tx->bind(); - // uv = vec2(coeff[0] * x + coeff[1] * y + coeff[2], coeff[3] * x + coeff[4] * y + coeff[5]) - // where x and y are in path coordinate space, which is just what - // we need since the gradient's start and stop are in that space too. - GLfloat coeff[6] = { 1, 0, 0, - 0, 1, 0 }; - nvpr.programPathFragmentInputGen(mtl->prg, 0, GL_OBJECT_LINEAR_NV, 2, coeff); - f->glProgramUniform2f(mtl->prg, mtl->uniLoc[2], m_fillGradient.start.x(), m_fillGradient.start.y()); - f->glProgramUniform2f(mtl->prg, mtl->uniLoc[3], m_fillGradient.end.x(), m_fillGradient.end.y()); + // Fill! + if (hasFill) { + if (!stencilClip) { + setupStencilForCover(false, 0); + renderFill(); } else { - f->glProgramUniform4f(mtl->prg, mtl->uniLoc[0], - m_fillColor.x(), m_fillColor.y(), m_fillColor.z(), m_fillColor.w()); + if (!m_fallbackBlitter.isCreated()) + m_fallbackBlitter.create(); + f->glStencilFunc(GL_EQUAL, sv, 0xFF); + f->glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); + m_fallbackBlitter.texturedQuad(m_fallbackFbo->texture(), m_fallbackFbo->size(), + *state->projectionMatrix(), *matrix(), + inheritedOpacity()); } - f->glProgramUniform1f(mtl->prg, mtl->uniLoc[1], inheritedOpacity()); - nvpr.stencilThenCoverFillPath(m_path, m_fillRule, 0xFF, GL_BOUNDING_BOX_NV); } - if (m_strokeWidth >= 0.0f && !qFuzzyIsNull(m_strokeColor.w())) { - if (m_fillGradientActive) - mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatSolid); - f->glProgramUniform4f(mtl->prg, mtl->uniLoc[0], - m_strokeColor.x(), m_strokeColor.y(), m_strokeColor.z(), m_strokeColor.w()); - f->glProgramUniform1f(mtl->prg, mtl->uniLoc[1], inheritedOpacity()); - nvpr.stencilThenCoverStrokePath(m_path, 0x1, ~0, GL_CONVEX_HULL_NV); + // Stroke! + if (hasStroke) { + const int strokeStencilValue = 0x80; + const int writeMask = 0x80; + + setupStencilForCover(stencilClip, sv); + if (stencilClip) { + // for the stencil step (eff. read mask == 0xFF & ~writeMask) + nvpr.pathStencilFunc(GL_EQUAL, sv, 0xFF); + // With stencilCLip == true the read mask for the stencil test before the stencil step is 0x7F. + // This assumes the clip stencil value is <= 127. + if (sv >= strokeStencilValue) + qWarning("PathItem/NVPR: stencil clip ref value %d too large; expect rendering errors", sv); + } + + renderStroke(strokeStencilValue, writeMask); } + if (stencilClip) + nvpr.pathStencilFunc(GL_ALWAYS, 0, ~0); + f->glBindProgramPipeline(0); m_dirty = 0; @@ -564,4 +670,96 @@ bool QQuickPathItemNvprRenderNode::isSupported() return !nvprDisabled && QQuickNvprFunctions::isSupported(); } +bool QQuickNvprBlitter::create() +{ + if (isCreated()) + destroy(); + + m_program = new QOpenGLShaderProgram; + if (QOpenGLContext::currentContext()->format().profile() == QSurfaceFormat::CoreProfile) { + m_program->addCacheableShaderFromSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/qt-project.org/items/shaders/shadereffect_core.vert")); + m_program->addCacheableShaderFromSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/qt-project.org/items/shaders/shadereffect_core.frag")); + } else { + m_program->addCacheableShaderFromSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/qt-project.org/items/shaders/shadereffect.vert")); + m_program->addCacheableShaderFromSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/qt-project.org/items/shaders/shadereffect.frag")); + } + m_program->bindAttributeLocation("qt_Vertex", 0); + m_program->bindAttributeLocation("qt_MultiTexCoord0", 1); + if (!m_program->link()) + return false; + + m_matrixLoc = m_program->uniformLocation("qt_Matrix"); + m_opacityLoc = m_program->uniformLocation("qt_Opacity"); + + m_buffer = new QOpenGLBuffer; + if (!m_buffer->create()) + return false; + m_buffer->bind(); + m_buffer->allocate(4 * sizeof(GLfloat) * 6); + m_buffer->release(); + + return true; +} + +void QQuickNvprBlitter::destroy() +{ + if (m_program) { + delete m_program; + m_program = nullptr; + } + if (m_buffer) { + delete m_buffer; + m_buffer = nullptr; + } +} + +void QQuickNvprBlitter::texturedQuad(GLuint textureId, const QSize &size, + const QMatrix4x4 &proj, const QMatrix4x4 &modelview, + float opacity) +{ + QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions(); + + m_program->bind(); + + QMatrix4x4 m = proj * modelview; + m_program->setUniformValue(m_matrixLoc, m); + m_program->setUniformValue(m_opacityLoc, opacity); + + m_buffer->bind(); + + if (size != m_prevSize) { + m_prevSize = size; + + QPointF p0(size.width() - 1, size.height() - 1); + QPointF p1(0, 0); + QPointF p2(0, size.height() - 1); + QPointF p3(size.width() - 1, 0); + + GLfloat vertices[6 * 4] = { + GLfloat(p0.x()), GLfloat(p0.y()), 1, 0, + GLfloat(p1.x()), GLfloat(p1.y()), 0, 1, + GLfloat(p2.x()), GLfloat(p2.y()), 0, 0, + + GLfloat(p0.x()), GLfloat(p0.y()), 1, 0, + GLfloat(p3.x()), GLfloat(p3.y()), 1, 1, + GLfloat(p1.x()), GLfloat(p1.y()), 0, 1, + }; + + m_buffer->write(0, vertices, sizeof(vertices)); + } + + m_program->enableAttributeArray(0); + m_program->enableAttributeArray(1); + f->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0); + f->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (const void *) (2 * sizeof(GLfloat))); + + f->glBindTexture(GL_TEXTURE_2D, textureId); + + f->glDrawArrays(GL_TRIANGLES, 0, 6); + + f->glBindTexture(GL_TEXTURE_2D, 0); + m_buffer->release(); + m_program->release(); +} + QT_END_NAMESPACE diff --git a/src/quick/items/qquickpathitemnvprrenderer_p.h b/src/quick/items/qquickpathitemnvprrenderer_p.h index 0075572324..7dd2d564c5 100644 --- a/src/quick/items/qquickpathitemnvprrenderer_p.h +++ b/src/quick/items/qquickpathitemnvprrenderer_p.h @@ -63,6 +63,9 @@ QT_BEGIN_NAMESPACE class QQuickPathItemNvprRenderNode; +class QOpenGLFramebufferObject; +class QOpenGLBuffer; +class QOpenGLExtraFunctions; class QQuickPathItemNvprRenderer : public QQuickAbstractPathRenderer { @@ -77,6 +80,10 @@ public: DirtyAll = 0xFF }; + QQuickPathItemNvprRenderer(QQuickItem *item) + : m_item(item) + { } + void beginSync() override; void setPath(const QQuickPath *path) override; void setStrokeColor(const QColor &color) override; @@ -101,6 +108,7 @@ public: private: void convertPath(const QQuickPath *path); + QQuickItem *m_item; QQuickPathItemNvprRenderNode *m_node = nullptr; int m_dirty = 0; @@ -146,6 +154,24 @@ private: MaterialDesc m_materials[NMaterials]; }; +class QQuickNvprBlitter +{ +public: + bool create(); + void destroy(); + bool isCreated() const { return m_program != nullptr; } + void texturedQuad(GLuint textureId, const QSize &size, + const QMatrix4x4 &proj, const QMatrix4x4 &modelview, + float opacity); + +private: + QOpenGLShaderProgram *m_program = nullptr; + QOpenGLBuffer *m_buffer = nullptr; + int m_matrixLoc; + int m_opacityLoc; + QSize m_prevSize; +}; + class QQuickPathItemNvprRenderNode : public QSGRenderNode { public: @@ -162,6 +188,10 @@ public: private: void updatePath(); + void renderStroke(int strokeStencilValue, int writeMask); + void renderFill(); + void renderOffscreenFill(); + void setupStencilForCover(bool stencilClip, int sv); static bool nvprInited; static QQuickNvprFunctions nvpr; @@ -183,6 +213,9 @@ private: QVector m_dashPattern; bool m_fillGradientActive; QQuickPathItemGradientCache::GradientDesc m_fillGradient; + QOpenGLFramebufferObject *m_fallbackFbo = nullptr; + QQuickNvprBlitter m_fallbackBlitter; + QOpenGLExtraFunctions *f = nullptr; friend class QQuickPathItemNvprRenderer; }; -- cgit v1.2.3 From 917a9382df8a5a673556a21b1b46e4343231d674 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 4 Jan 2017 14:14:04 +0100 Subject: Fix negative stroke width for software PathItem Like the generic one, this also needs to deal with the fact that QPen does not take negative widths. Store the value separately. Change-Id: Ia332c2d83e98c03125a05c838cb8184346f8303a Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemsoftwarerenderer.cpp | 10 +++++++--- src/quick/items/qquickpathitemsoftwarerenderer_p.h | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemsoftwarerenderer.cpp b/src/quick/items/qquickpathitemsoftwarerenderer.cpp index f089904fdf..c7b52e641f 100644 --- a/src/quick/items/qquickpathitemsoftwarerenderer.cpp +++ b/src/quick/items/qquickpathitemsoftwarerenderer.cpp @@ -61,7 +61,9 @@ void QQuickPathItemSoftwareRenderer::setStrokeColor(const QColor &color) void QQuickPathItemSoftwareRenderer::setStrokeWidth(qreal w) { - m_pen.setWidthF(w); + m_strokeWidth = w; + if (w >= 0.0f) + m_pen.setWidthF(w); m_dirty |= DirtyPen; } @@ -169,8 +171,10 @@ void QQuickPathItemSoftwareRenderer::updatePathRenderNode() if (m_dirty & DirtyFillRule) m_node->m_path.setFillRule(m_fillRule); - if (m_dirty & DirtyPen) + if (m_dirty & DirtyPen) { m_node->m_pen = m_pen; + m_node->m_strokeWidth = m_strokeWidth; + } if (m_dirty & DirtyBrush) m_node->m_brush = m_brush; @@ -209,7 +213,7 @@ void QQuickPathItemSoftwareRenderNode::render(const RenderState *state) p->setTransform(matrix()->toTransform()); p->setOpacity(inheritedOpacity()); - p->setPen(!qFuzzyIsNull(m_pen.widthF()) && m_pen.color() != Qt::transparent ? m_pen : Qt::NoPen); + p->setPen(m_strokeWidth >= 0.0f && m_pen.color() != Qt::transparent ? m_pen : Qt::NoPen); p->setBrush(m_brush.color() != Qt::transparent ? m_brush : Qt::NoBrush); p->drawPath(m_path); diff --git a/src/quick/items/qquickpathitemsoftwarerenderer_p.h b/src/quick/items/qquickpathitemsoftwarerenderer_p.h index 584771425d..6c7d052596 100644 --- a/src/quick/items/qquickpathitemsoftwarerenderer_p.h +++ b/src/quick/items/qquickpathitemsoftwarerenderer_p.h @@ -94,6 +94,7 @@ private: QPainterPath m_path; QPen m_pen; + float m_strokeWidth; QColor m_fillColor; QBrush m_brush; Qt::FillRule m_fillRule; @@ -117,6 +118,7 @@ private: QPainterPath m_path; QPen m_pen; + float m_strokeWidth; QBrush m_brush; friend class QQuickPathItemSoftwareRenderer; -- cgit v1.2.3 From 81d204e2a5118b2d81862fa9d9a45e5522396a45 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 4 Jan 2017 14:31:23 +0100 Subject: Add X axis rotation property to PathArc It is a standard feature of elliptical arc. While perhaps deemed too advanced for PathView purposes, rendering the path using PathItem must offer the ability to specify a non-zero X axis rotation for the ellipses of which the arc is a section of. Change-Id: I53f01713b7e0e97c40f22d75d46f75a140830683 Reviewed-by: Andy Nichols --- src/quick/items/qquickitemsmodule.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickitemsmodule.cpp b/src/quick/items/qquickitemsmodule.cpp index b0d7f7f8a3..9c70daab1f 100644 --- a/src/quick/items/qquickitemsmodule.cpp +++ b/src/quick/items/qquickitemsmodule.cpp @@ -373,6 +373,7 @@ static void qt_quickitems_defineModule(const char *uri, int major, int minor) qmlRegisterType(uri, 2, 9, "MouseArea"); #if QT_CONFIG(quick_path) + qmlRegisterType(uri, 2, 9, "PathArc"); qmlRegisterType(uri, 2, 9, "PathMove"); qmlRegisterType(uri, 2, 9, "PathItem"); qmlRegisterType(uri, 2, 9, "PathGradientStop"); -- cgit v1.2.3 From 7599fbffbf8ad88fa4487cd7d8bad90eb0b2d952 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 4 Jan 2017 14:34:51 +0100 Subject: NVPR: Take X axis rotation into account for PathArc Change-Id: I70238e0e4b458ddfde9f32c40b76382c1a183e98 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemnvprrenderer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemnvprrenderer.cpp b/src/quick/items/qquickpathitemnvprrenderer.cpp index bd88023891..5641d33f85 100644 --- a/src/quick/items/qquickpathitemnvprrenderer.cpp +++ b/src/quick/items/qquickpathitemnvprrenderer.cpp @@ -242,7 +242,7 @@ void QQuickPathItemNvprRenderer::convertPath(const QQuickPath *path) m_path.cmd.append(cmd); m_path.coord.append(o->radiusX()); m_path.coord.append(o->radiusY()); - m_path.coord.append(0.0f); // X axis rotation + m_path.coord.append(o->xAxisRotation()); appendCoords(&m_path.coord, o, &pos); } else { qWarning() << "PathItem/NVPR: unsupported Path element" << e; -- cgit v1.2.3 From 0271da9ff4001d26596a9172329691674e147ada Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 4 Jan 2017 15:12:32 +0100 Subject: Allow multiple paths in a PathItem Instead of PathItem { item properties stroke/fill properties path: Path { ... } } switch to PathItem { item properties VisualPath { stroke/fill settings Path { ... } } VisualPath { stroke/fill settings Path { ... } } ... } Limiting PathItem to a single path is arguably too limited. Applications will likely try to work this around by using multiple PathItems. While this is not particularly bad for the generic (geometry node based) implementation, it is a massive overkill for the rendernode-based ones. Therefore, avoid the hassle and allow multiple paths with different stroke/fill parameters inside a single PathItem. Change-Id: Ie7980cd656deb7d4cb1ee4eaa3c090c4b0493c7d Reviewed-by: Andy Nichols --- src/quick/items/qquickitemsmodule.cpp | 1 + src/quick/items/qquickpathitem.cpp | 460 +++++++++++------- src/quick/items/qquickpathitem_p.h | 70 ++- src/quick/items/qquickpathitem_p_p.h | 68 ++- src/quick/items/qquickpathitemgenericrenderer.cpp | 358 +++++++------- src/quick/items/qquickpathitemgenericrenderer_p.h | 105 ++--- src/quick/items/qquickpathitemnvprrenderer.cpp | 519 ++++++++++++--------- src/quick/items/qquickpathitemnvprrenderer_p.h | 108 +++-- src/quick/items/qquickpathitemsoftwarerenderer.cpp | 162 ++++--- src/quick/items/qquickpathitemsoftwarerenderer_p.h | 57 +-- 10 files changed, 1085 insertions(+), 823 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickitemsmodule.cpp b/src/quick/items/qquickitemsmodule.cpp index 9c70daab1f..e9439275ac 100644 --- a/src/quick/items/qquickitemsmodule.cpp +++ b/src/quick/items/qquickitemsmodule.cpp @@ -376,6 +376,7 @@ static void qt_quickitems_defineModule(const char *uri, int major, int minor) qmlRegisterType(uri, 2, 9, "PathArc"); qmlRegisterType(uri, 2, 9, "PathMove"); qmlRegisterType(uri, 2, 9, "PathItem"); + qmlRegisterType(uri, 2, 9, "VisualPath"); qmlRegisterType(uri, 2, 9, "PathGradientStop"); qmlRegisterUncreatableType(uri, 2, 9, "PathGradient", QQuickPathGradient::tr("PathGradient is an abstract base class")); qmlRegisterType(uri, 2, 9, "PathLinearGradient"); diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index 0a4c721a74..0dce376945 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -48,333 +48,425 @@ QT_BEGIN_NAMESPACE -QQuickPathItemPrivate::QQuickPathItemPrivate() - : rendererType(QQuickPathItem::UnknownRenderer), - renderer(nullptr), - path(nullptr), +QQuickVisualPathPrivate::QQuickVisualPathPrivate() + : path(nullptr), dirty(DirtyAll), strokeColor(Qt::white), strokeWidth(1), fillColor(Qt::white), - fillRule(QQuickPathItem::OddEvenFill), - joinStyle(QQuickPathItem::BevelJoin), + fillRule(QQuickVisualPath::OddEvenFill), + joinStyle(QQuickVisualPath::BevelJoin), miterLimit(2), - capStyle(QQuickPathItem::SquareCap), - strokeStyle(QQuickPathItem::SolidLine), + capStyle(QQuickVisualPath::SquareCap), + strokeStyle(QQuickVisualPath::SolidLine), dashOffset(0), fillGradient(nullptr) { dashPattern << 4 << 2; // 4 * strokeWidth dash followed by 2 * strokeWidth space } -QQuickPathItemPrivate::~QQuickPathItemPrivate() +QQuickVisualPath::QQuickVisualPath(QObject *parent) + : QObject(*(new QQuickVisualPathPrivate), parent) { - delete renderer; } -/*! - \qmltype PathItem - \instantiates QQuickPathItem - \inqmlmodule QtQuick - \ingroup qtquick-paths - \ingroup qtquick-views - \inherits Item - \brief Renders a path - - Renders a path either by generating geometry via QPainterPath and manual - triangulation or by using an extension like \c{GL_NV_path_rendering}. - - This approach is different from rendering shapes via QQuickPaintedItem or - the 2D Canvas because the path never gets rasterized in software. Therefore - it is suitable for creating shapes spreading over larger areas of the - screen, avoiding the performance penalty for texture uploads or framebuffer - blits. - - Nonetheless it is important to be aware of performance implications, in - particular when the application is running on the generic PathItem - implementation due to not having support for accelerated path rendering. - The geometry generation happens entirely on the CPU in this case, and this - is potentially expensive. Changing the set of path elements, changing the - properties of these elements, or changing certain properties of the - PathItem itself all lead to retriangulation on every change. Therefore, - applying animation to such properties can heavily affect performance on - less powerful systems. If animating properties other than stroke and fill - colors is a must, it is recommended to target systems providing - \c{GL_NV_path_rendering} where the cost of path property changes is much - smaller. - - \note The types for specifying path elements are shared between PathView - and PathItem. However, not all PathItem implementations support all path - element types, while some may not make sense for PathView. PathItem's - currently supported subset is: PathMove, PathLine, PathQuad, PathCubic, - PathArc. - - \sa Path, PathMove, PathLine, PathQuad, PathCubic, PathArc -*/ - -QQuickPathItem::QQuickPathItem(QQuickItem *parent) - : QQuickItem(*(new QQuickPathItemPrivate), parent) +QQuickVisualPath::~QQuickVisualPath() { - setFlag(ItemHasContents); } -QQuickPathItem::~QQuickPathItem() +QQuickPath *QQuickVisualPath::path() const { -} - -QQuickPathItem::RendererType QQuickPathItem::rendererType() const -{ - Q_D(const QQuickPathItem); - return d->rendererType; -} - -/*! - \qmlproperty Path QtQuick::PathItem::path - This property holds the path to be rendered. - For more information see the \l Path documentation. -*/ -QQuickPath *QQuickPathItem::path() const -{ - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->path; } -void QQuickPathItem::setPath(QQuickPath *path) +void QQuickVisualPath::setPath(QQuickPath *path) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->path == path) return; if (d->path) qmlobject_disconnect(d->path, QQuickPath, SIGNAL(changed()), - this, QQuickPathItem, SLOT(_q_pathChanged())); + this, QQuickVisualPath, SLOT(_q_pathChanged())); d->path = path; qmlobject_connect(d->path, QQuickPath, SIGNAL(changed()), - this, QQuickPathItem, SLOT(_q_pathChanged())); + this, QQuickVisualPath, SLOT(_q_pathChanged())); - d->dirty |= QQuickPathItemPrivate::DirtyPath; + d->dirty |= QQuickVisualPathPrivate::DirtyPath; emit pathChanged(); - polish(); + emit changed(); } -void QQuickPathItemPrivate::_q_pathChanged() +void QQuickVisualPathPrivate::_q_pathChanged() { - Q_Q(QQuickPathItem); + Q_Q(QQuickVisualPath); dirty |= DirtyPath; - q->polish(); + emit q->changed(); } -QColor QQuickPathItem::strokeColor() const +QColor QQuickVisualPath::strokeColor() const { - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->strokeColor; } -void QQuickPathItem::setStrokeColor(const QColor &color) +void QQuickVisualPath::setStrokeColor(const QColor &color) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->strokeColor != color) { d->strokeColor = color; - d->dirty |= QQuickPathItemPrivate::DirtyStrokeColor; + d->dirty |= QQuickVisualPathPrivate::DirtyStrokeColor; emit strokeColorChanged(); - polish(); + emit changed(); } } -qreal QQuickPathItem::strokeWidth() const +qreal QQuickVisualPath::strokeWidth() const { - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->strokeWidth; } -void QQuickPathItem::setStrokeWidth(qreal w) +void QQuickVisualPath::setStrokeWidth(qreal w) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->strokeWidth != w) { d->strokeWidth = w; - d->dirty |= QQuickPathItemPrivate::DirtyStrokeWidth; + d->dirty |= QQuickVisualPathPrivate::DirtyStrokeWidth; emit strokeWidthChanged(); - polish(); + emit changed(); } } -QColor QQuickPathItem::fillColor() const +QColor QQuickVisualPath::fillColor() const { - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->fillColor; } -void QQuickPathItem::setFillColor(const QColor &color) +void QQuickVisualPath::setFillColor(const QColor &color) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->fillColor != color) { d->fillColor = color; - d->dirty |= QQuickPathItemPrivate::DirtyFillColor; + d->dirty |= QQuickVisualPathPrivate::DirtyFillColor; emit fillColorChanged(); - polish(); + emit changed(); } } -QQuickPathItem::FillRule QQuickPathItem::fillRule() const +QQuickVisualPath::FillRule QQuickVisualPath::fillRule() const { - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->fillRule; } -void QQuickPathItem::setFillRule(FillRule fillRule) +void QQuickVisualPath::setFillRule(FillRule fillRule) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->fillRule != fillRule) { d->fillRule = fillRule; - d->dirty |= QQuickPathItemPrivate::DirtyFillRule; + d->dirty |= QQuickVisualPathPrivate::DirtyFillRule; emit fillRuleChanged(); - polish(); + emit changed(); } } -QQuickPathItem::JoinStyle QQuickPathItem::joinStyle() const +QQuickVisualPath::JoinStyle QQuickVisualPath::joinStyle() const { - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->joinStyle; } -void QQuickPathItem::setJoinStyle(JoinStyle style) +void QQuickVisualPath::setJoinStyle(JoinStyle style) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->joinStyle != style) { d->joinStyle = style; - d->dirty |= QQuickPathItemPrivate::DirtyStyle; + d->dirty |= QQuickVisualPathPrivate::DirtyStyle; emit joinStyleChanged(); - polish(); + emit changed(); } } -int QQuickPathItem::miterLimit() const +int QQuickVisualPath::miterLimit() const { - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->miterLimit; } -void QQuickPathItem::setMiterLimit(int limit) +void QQuickVisualPath::setMiterLimit(int limit) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->miterLimit != limit) { d->miterLimit = limit; - d->dirty |= QQuickPathItemPrivate::DirtyStyle; + d->dirty |= QQuickVisualPathPrivate::DirtyStyle; emit miterLimitChanged(); - polish(); + emit changed(); } } -QQuickPathItem::CapStyle QQuickPathItem::capStyle() const +QQuickVisualPath::CapStyle QQuickVisualPath::capStyle() const { - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->capStyle; } -void QQuickPathItem::setCapStyle(CapStyle style) +void QQuickVisualPath::setCapStyle(CapStyle style) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->capStyle != style) { d->capStyle = style; - d->dirty |= QQuickPathItemPrivate::DirtyStyle; + d->dirty |= QQuickVisualPathPrivate::DirtyStyle; emit capStyleChanged(); - polish(); + emit changed(); } } -QQuickPathItem::StrokeStyle QQuickPathItem::strokeStyle() const +QQuickVisualPath::StrokeStyle QQuickVisualPath::strokeStyle() const { - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->strokeStyle; } -void QQuickPathItem::setStrokeStyle(StrokeStyle style) +void QQuickVisualPath::setStrokeStyle(StrokeStyle style) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->strokeStyle != style) { d->strokeStyle = style; - d->dirty |= QQuickPathItemPrivate::DirtyDash; + d->dirty |= QQuickVisualPathPrivate::DirtyDash; emit strokeStyleChanged(); - polish(); + emit changed(); } } -qreal QQuickPathItem::dashOffset() const +qreal QQuickVisualPath::dashOffset() const { - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->dashOffset; } -void QQuickPathItem::setDashOffset(qreal offset) +void QQuickVisualPath::setDashOffset(qreal offset) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->dashOffset != offset) { d->dashOffset = offset; - d->dirty |= QQuickPathItemPrivate::DirtyDash; + d->dirty |= QQuickVisualPathPrivate::DirtyDash; emit dashOffsetChanged(); - polish(); + emit changed(); } } -QVector QQuickPathItem::dashPattern() const +QVector QQuickVisualPath::dashPattern() const { - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->dashPattern; } -void QQuickPathItem::setDashPattern(const QVector &array) +void QQuickVisualPath::setDashPattern(const QVector &array) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->dashPattern != array) { d->dashPattern = array; - d->dirty |= QQuickPathItemPrivate::DirtyDash; + d->dirty |= QQuickVisualPathPrivate::DirtyDash; emit dashPatternChanged(); - polish(); + emit changed(); } } -QQuickPathGradient *QQuickPathItem::fillGradient() const +QQuickPathGradient *QQuickVisualPath::fillGradient() const { - Q_D(const QQuickPathItem); + Q_D(const QQuickVisualPath); return d->fillGradient; } -void QQuickPathItem::setFillGradient(QQuickPathGradient *gradient) +void QQuickVisualPath::setFillGradient(QQuickPathGradient *gradient) { - Q_D(QQuickPathItem); + Q_D(QQuickVisualPath); if (d->fillGradient != gradient) { if (d->fillGradient) qmlobject_disconnect(d->fillGradient, QQuickPathGradient, SIGNAL(updated()), - this, QQuickPathItem, SLOT(_q_fillGradientChanged())); + this, QQuickVisualPath, SLOT(_q_fillGradientChanged())); d->fillGradient = gradient; if (d->fillGradient) qmlobject_connect(d->fillGradient, QQuickPathGradient, SIGNAL(updated()), - this, QQuickPathItem, SLOT(_q_fillGradientChanged())); - d->dirty |= QQuickPathItemPrivate::DirtyFillGradient; - polish(); + this, QQuickVisualPath, SLOT(_q_fillGradientChanged())); + d->dirty |= QQuickVisualPathPrivate::DirtyFillGradient; + emit changed(); } } -void QQuickPathItemPrivate::_q_fillGradientChanged() +void QQuickVisualPathPrivate::_q_fillGradientChanged() { - Q_Q(QQuickPathItem); + Q_Q(QQuickVisualPath); dirty |= DirtyFillGradient; - q->polish(); + emit q->changed(); } -void QQuickPathItem::resetFillGradient() +void QQuickVisualPath::resetFillGradient() { setFillGradient(nullptr); } +/*! + \qmltype PathItem + \instantiates QQuickPathItem + \inqmlmodule QtQuick + \ingroup qtquick-paths + \ingroup qtquick-views + \inherits Item + \brief Renders a path + \since 5.10 + + Renders a path either by generating geometry via QPainterPath and manual + triangulation or by using an extension like \c{GL_NV_path_rendering}. + + This approach is different from rendering shapes via QQuickPaintedItem or + the 2D Canvas because the path never gets rasterized in software. Therefore + it is suitable for creating shapes spreading over larger areas of the + screen, avoiding the performance penalty for texture uploads or framebuffer + blits. + + Nonetheless it is important to be aware of performance implications, in + particular when the application is running on the generic PathItem + implementation due to not having support for accelerated path rendering. + The geometry generation happens entirely on the CPU in this case, and this + is potentially expensive. Changing the set of path elements, changing the + properties of these elements, or changing certain properties of the + PathItem itself all lead to retriangulation on every change. Therefore, + applying animation to such properties can heavily affect performance on + less powerful systems. If animating properties other than stroke and fill + colors is a must, it is recommended to target systems providing + \c{GL_NV_path_rendering} where the cost of path property changes is much + smaller. + + \note The types for specifying path elements are shared between \l PathView + and PathItem. However, not all PathItem implementations support all path + element types, while some may not make sense for PathView. PathItem's + currently supported subset is: PathMove, PathLine, PathQuad, PathCubic, + PathArc. + + \note Limited support for PathSvg is also provided in most cases. However, + there is no guarantee that this element is going to be supported for all + future PathItem backends. It is recommended to avoid the PathSvg element in + practice. + + See \l Path for a detailed overview of the supported path elements. + + \sa Path, PathMove, PathLine, PathQuad, PathCubic, PathArc, PathSvg +*/ + +QQuickPathItemPrivate::QQuickPathItemPrivate() + : componentComplete(true), + vpChanged(false), + rendererType(QQuickPathItem::UnknownRenderer), + renderer(nullptr) +{ +} + +QQuickPathItemPrivate::~QQuickPathItemPrivate() +{ + delete renderer; +} + +void QQuickPathItemPrivate::_q_visualPathChanged() +{ + Q_Q(QQuickPathItem); + vpChanged = true; + q->polish(); +} + +QQuickPathItem::QQuickPathItem(QQuickItem *parent) + : QQuickItem(*(new QQuickPathItemPrivate), parent) +{ + setFlag(ItemHasContents); +} + +QQuickPathItem::~QQuickPathItem() +{ +} + +QQuickPathItem::RendererType QQuickPathItem::rendererType() const +{ + Q_D(const QQuickPathItem); + return d->rendererType; +} + +static QQuickVisualPath *vpe_at(QQmlListProperty *property, int index) +{ + QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(static_cast(property->object)); + return d->vp.at(index); +} + +static void vpe_append(QQmlListProperty *property, QQuickVisualPath *obj) +{ + QQuickPathItem *item = static_cast(property->object); + QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(item); + d->vp.append(obj); + + if (d->componentComplete) { + QObject::connect(obj, SIGNAL(changed()), item, SLOT(_q_visualPathChanged())); + d->_q_visualPathChanged(); + } +} + +static int vpe_count(QQmlListProperty *property) +{ + QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(static_cast(property->object)); + return d->vp.count(); +} + +static void vpe_clear(QQmlListProperty *property) +{ + QQuickPathItem *item = static_cast(property->object); + QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(item); + + for (QQuickVisualPath *p : d->vp) + QObject::disconnect(p, SIGNAL(changed()), item, SLOT(_q_visualPathChanged())); + + d->vp.clear(); + + if (d->componentComplete) + d->_q_visualPathChanged(); +} + +QQmlListProperty QQuickPathItem::visualPaths() +{ + return QQmlListProperty(this, + nullptr, + vpe_append, + vpe_count, + vpe_at, + vpe_clear); +} + +void QQuickPathItem::classBegin() +{ + Q_D(QQuickPathItem); + d->componentComplete = false; +} + +void QQuickPathItem::componentComplete() +{ + Q_D(QQuickPathItem); + d->componentComplete = true; + + for (QQuickVisualPath *p : d->vp) + connect(p, SIGNAL(changed()), this, SLOT(_q_visualPathChanged())); + + d->_q_visualPathChanged(); +} + void QQuickPathItem::updatePolish() { Q_D(QQuickPathItem); - if (!d->dirty) + if (!d->vpChanged) return; + d->vpChanged = false; + if (!d->renderer) { d->createRenderer(); if (!d->renderer) @@ -392,9 +484,11 @@ void QQuickPathItem::updatePolish() void QQuickPathItem::itemChange(ItemChange change, const ItemChangeData &data) { + Q_D(QQuickPathItem); + // sync may have been deferred; do it now if the item became visible if (change == ItemVisibleHasChanged && data.boolValue) - polish(); + d->_q_visualPathChanged(); QQuickItem::itemChange(change, data); } @@ -407,8 +501,8 @@ QSGNode *QQuickPathItem::updatePaintNode(QSGNode *node, UpdatePaintNodeData *) Q_D(QQuickPathItem); if (d->renderer) { if (!node) - node = d->createRenderNode(); - d->renderer->updatePathRenderNode(); + node = d->createNode(); + d->renderer->updateNode(); } return node; } @@ -444,7 +538,7 @@ void QQuickPathItemPrivate::createRenderer() } // the node lives on the render thread -QSGNode *QQuickPathItemPrivate::createRenderNode() +QSGNode *QQuickPathItemPrivate::createNode() { Q_Q(QQuickPathItem); QSGNode *node = nullptr; @@ -454,9 +548,6 @@ QSGNode *QQuickPathItemPrivate::createRenderNode() if (!ri) return node; - const bool hasFill = fillColor != Qt::transparent; - const bool hasStroke = strokeWidth >= 0.0f && strokeColor != Qt::transparent; - switch (ri->graphicsApi()) { #ifndef QT_NO_OPENGL case QSGRendererInterface::OpenGL: @@ -465,9 +556,9 @@ QSGNode *QQuickPathItemPrivate::createRenderNode() static_cast(renderer)->setNode( static_cast(node)); } else { - node = new QQuickPathItemGenericRootRenderNode(q->window(), hasFill, hasStroke); + node = new QQuickPathItemGenericNode; static_cast(renderer)->setRootNode( - static_cast(node)); + static_cast(node)); } break; #endif @@ -486,29 +577,36 @@ QSGNode *QQuickPathItemPrivate::createRenderNode() void QQuickPathItemPrivate::sync() { - renderer->beginSync(); - - if (dirty & QQuickPathItemPrivate::DirtyPath) - renderer->setPath(path); - if (dirty & DirtyStrokeColor) - renderer->setStrokeColor(strokeColor); - if (dirty & DirtyStrokeWidth) - renderer->setStrokeWidth(strokeWidth); - if (dirty & DirtyFillColor) - renderer->setFillColor(fillColor); - if (dirty & DirtyFillRule) - renderer->setFillRule(fillRule); - if (dirty & DirtyStyle) { - renderer->setJoinStyle(joinStyle, miterLimit); - renderer->setCapStyle(capStyle); + const int count = vp.count(); + renderer->beginSync(count); + + for (int i = 0; i < count; ++i) { + QQuickVisualPath *p = vp[i]; + int &dirty(QQuickVisualPathPrivate::get(p)->dirty); + + if (dirty & QQuickVisualPathPrivate::DirtyPath) + renderer->setPath(i, p->path()); + if (dirty & QQuickVisualPathPrivate::DirtyStrokeColor) + renderer->setStrokeColor(i, p->strokeColor()); + if (dirty & QQuickVisualPathPrivate::DirtyStrokeWidth) + renderer->setStrokeWidth(i, p->strokeWidth()); + if (dirty & QQuickVisualPathPrivate::DirtyFillColor) + renderer->setFillColor(i, p->fillColor()); + if (dirty & QQuickVisualPathPrivate::DirtyFillRule) + renderer->setFillRule(i, p->fillRule()); + if (dirty & QQuickVisualPathPrivate::DirtyStyle) { + renderer->setJoinStyle(i, p->joinStyle(), p->miterLimit()); + renderer->setCapStyle(i, p->capStyle()); + } + if (dirty & QQuickVisualPathPrivate::DirtyDash) + renderer->setStrokeStyle(i, p->strokeStyle(), p->dashOffset(), p->dashPattern()); + if (dirty & QQuickVisualPathPrivate::DirtyFillGradient) + renderer->setFillGradient(i, p->fillGradient()); + + dirty = 0; } - if (dirty & DirtyDash) - renderer->setStrokeStyle(strokeStyle, dashOffset, dashPattern); - if (dirty & DirtyFillGradient) - renderer->setFillGradient(fillGradient); renderer->endSync(); - dirty = 0; } // ***** gradient support ***** diff --git a/src/quick/items/qquickpathitem_p.h b/src/quick/items/qquickpathitem_p.h index 39b407cf87..0c1d0f061b 100644 --- a/src/quick/items/qquickpathitem_p.h +++ b/src/quick/items/qquickpathitem_p.h @@ -61,6 +61,7 @@ QT_REQUIRE_CONFIG(quick_path); QT_BEGIN_NAMESPACE +class QQuickVisualPathPrivate; class QQuickPathItemPrivate; class Q_QUICK_PRIVATE_EXPORT QQuickPathGradientStop : public QObject @@ -150,13 +151,12 @@ private: QPointF m_end; }; -class Q_QUICK_PRIVATE_EXPORT QQuickPathItem : public QQuickItem +class Q_QUICK_PRIVATE_EXPORT QQuickVisualPath : public QObject { Q_OBJECT - Q_PROPERTY(RendererType renderer READ rendererType NOTIFY rendererChanged) - Q_PROPERTY(QQuickPath *path READ path WRITE setPath NOTIFY pathChanged) + Q_CLASSINFO("DefaultProperty", "path") Q_PROPERTY(QColor strokeColor READ strokeColor WRITE setStrokeColor NOTIFY strokeColorChanged) Q_PROPERTY(qreal strokeWidth READ strokeWidth WRITE setStrokeWidth NOTIFY strokeWidthChanged) @@ -197,18 +197,8 @@ public: }; Q_ENUM(StrokeStyle) - enum RendererType { - UnknownRenderer, - GeometryRenderer, - NvprRenderer, - SoftwareRenderer - }; - Q_ENUM(RendererType) - - QQuickPathItem(QQuickItem *parent = nullptr); - ~QQuickPathItem(); - - RendererType rendererType() const; + QQuickVisualPath(QObject *parent = nullptr); + ~QQuickVisualPath(); QQuickPath *path() const; void setPath(QQuickPath *path); @@ -247,13 +237,8 @@ public: void setFillGradient(QQuickPathGradient *gradient); void resetFillGradient(); -protected: - QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override; - void updatePolish() override; - void itemChange(ItemChange change, const ItemChangeData &data) override; - Q_SIGNALS: - void rendererChanged(); + void changed(); void pathChanged(); void strokeColorChanged(); void strokeWidthChanged(); @@ -268,12 +253,51 @@ Q_SIGNALS: void fillGradientChanged(); private: - Q_DISABLE_COPY(QQuickPathItem) - Q_DECLARE_PRIVATE(QQuickPathItem) + Q_DISABLE_COPY(QQuickVisualPath) + Q_DECLARE_PRIVATE(QQuickVisualPath) Q_PRIVATE_SLOT(d_func(), void _q_pathChanged()) Q_PRIVATE_SLOT(d_func(), void _q_fillGradientChanged()) }; +class Q_QUICK_PRIVATE_EXPORT QQuickPathItem : public QQuickItem +{ + Q_OBJECT + Q_PROPERTY(RendererType renderer READ rendererType NOTIFY rendererChanged) + Q_PROPERTY(QQmlListProperty visualPaths READ visualPaths) + Q_CLASSINFO("DefaultProperty", "visualPaths") + +public: + enum RendererType { + UnknownRenderer, + GeometryRenderer, + NvprRenderer, + SoftwareRenderer + }; + Q_ENUM(RendererType) + + QQuickPathItem(QQuickItem *parent = nullptr); + ~QQuickPathItem(); + + RendererType rendererType() const; + + QQmlListProperty visualPaths(); + +protected: + QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override; + void updatePolish() override; + void itemChange(ItemChange change, const ItemChangeData &data) override; + void componentComplete() override; + void classBegin() override; + +Q_SIGNALS: + void rendererChanged(); + +private: + Q_DISABLE_COPY(QQuickPathItem) + Q_DECLARE_PRIVATE(QQuickPathItem) + Q_PRIVATE_SLOT(d_func(), void _q_visualPathChanged()) +}; + QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickPathItem) diff --git a/src/quick/items/qquickpathitem_p_p.h b/src/quick/items/qquickpathitem_p_p.h index 366628d867..5e6400edc6 100644 --- a/src/quick/items/qquickpathitem_p_p.h +++ b/src/quick/items/qquickpathitem_p_p.h @@ -68,26 +68,26 @@ public: virtual ~QQuickAbstractPathRenderer() { } // Gui thread - virtual void beginSync() = 0; - virtual void setPath(const QQuickPath *path) = 0; - virtual void setStrokeColor(const QColor &color) = 0; - virtual void setStrokeWidth(qreal w) = 0; - virtual void setFillColor(const QColor &color) = 0; - virtual void setFillRule(QQuickPathItem::FillRule fillRule) = 0; - virtual void setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) = 0; - virtual void setCapStyle(QQuickPathItem::CapStyle capStyle) = 0; - virtual void setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, + virtual void beginSync(int totalCount) = 0; + virtual void setPath(int index, const QQuickPath *path) = 0; + virtual void setStrokeColor(int index, const QColor &color) = 0; + virtual void setStrokeWidth(int index, qreal w) = 0; + virtual void setFillColor(int index, const QColor &color) = 0; + virtual void setFillRule(int index, QQuickVisualPath::FillRule fillRule) = 0; + virtual void setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) = 0; + virtual void setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) = 0; + virtual void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, qreal dashOffset, const QVector &dashPattern) = 0; - virtual void setFillGradient(QQuickPathGradient *gradient) = 0; + virtual void setFillGradient(int index, QQuickPathGradient *gradient) = 0; virtual void endSync() = 0; // Render thread, with gui blocked - virtual void updatePathRenderNode() = 0; + virtual void updateNode() = 0; }; -class QQuickPathItemPrivate : public QQuickItemPrivate +class QQuickVisualPathPrivate : public QObjectPrivate { - Q_DECLARE_PUBLIC(QQuickPathItem) + Q_DECLARE_PUBLIC(QQuickVisualPath) public: enum Dirty { @@ -103,33 +103,51 @@ public: DirtyAll = 0xFF }; - QQuickPathItemPrivate(); - ~QQuickPathItemPrivate(); - - void createRenderer(); - QSGNode *createRenderNode(); - void sync(); + QQuickVisualPathPrivate(); void _q_pathChanged(); void _q_fillGradientChanged(); - QQuickPathItem::RendererType rendererType; - QQuickAbstractPathRenderer *renderer; + static QQuickVisualPathPrivate *get(QQuickVisualPath *p) { return p->d_func(); } + QQuickPath *path; int dirty; QColor strokeColor; qreal strokeWidth; QColor fillColor; - QQuickPathItem::FillRule fillRule; - QQuickPathItem::JoinStyle joinStyle; + QQuickVisualPath::FillRule fillRule; + QQuickVisualPath::JoinStyle joinStyle; int miterLimit; - QQuickPathItem::CapStyle capStyle; - QQuickPathItem::StrokeStyle strokeStyle; + QQuickVisualPath::CapStyle capStyle; + QQuickVisualPath::StrokeStyle strokeStyle; qreal dashOffset; QVector dashPattern; QQuickPathGradient *fillGradient; }; +class QQuickPathItemPrivate : public QQuickItemPrivate +{ + Q_DECLARE_PUBLIC(QQuickPathItem) + +public: + QQuickPathItemPrivate(); + ~QQuickPathItemPrivate(); + + void createRenderer(); + QSGNode *createNode(); + void sync(); + + void _q_visualPathChanged(); + + static QQuickPathItemPrivate *get(QQuickPathItem *item) { return item->d_func(); } + + bool componentComplete; + bool vpChanged; + QQuickPathItem::RendererType rendererType; + QQuickAbstractPathRenderer *renderer; + QVector vp; +}; + #ifndef QT_NO_OPENGL class QQuickPathItemGradientCache : public QOpenGLSharedResource diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index 4b15daef9a..b42ff1d4b0 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -66,42 +66,16 @@ static inline QQuickPathItemGenericRenderer::Color4ub colorToColor4ub(const QCol return color; } -QQuickPathItemGenericRootRenderNode::QQuickPathItemGenericRootRenderNode(QQuickWindow *window, - bool hasFill, - bool hasStroke) - : m_fillNode(nullptr), - m_strokeNode(nullptr) -{ - if (hasFill) { - m_fillNode = new QQuickPathItemGenericRenderNode(window, this); - appendChildNode(m_fillNode); - } - if (hasStroke) { - m_strokeNode = new QQuickPathItemGenericRenderNode(window, this); - appendChildNode(m_strokeNode); - } -} - -QQuickPathItemGenericRootRenderNode::~QQuickPathItemGenericRootRenderNode() -{ -} - -QQuickPathItemGenericRenderNode::QQuickPathItemGenericRenderNode(QQuickWindow *window, - QQuickPathItemGenericRootRenderNode *rootNode) +QQuickPathItemGenericStrokeFillNode::QQuickPathItemGenericStrokeFillNode(QQuickWindow *window) : m_geometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), 0, 0), m_window(window), - m_rootNode(rootNode), m_material(nullptr) { setGeometry(&m_geometry); activateMaterial(MatSolidColor); } -QQuickPathItemGenericRenderNode::~QQuickPathItemGenericRenderNode() -{ -} - -void QQuickPathItemGenericRenderNode::activateMaterial(Material m) +void QQuickPathItemGenericStrokeFillNode::activateMaterial(Material m) { switch (m) { case MatSolidColor: @@ -126,279 +100,335 @@ void QQuickPathItemGenericRenderNode::activateMaterial(Material m) } // sync, and so triangulation too, happens on the gui thread -void QQuickPathItemGenericRenderer::beginSync() +void QQuickPathItemGenericRenderer::beginSync(int totalCount) { - m_syncDirty = 0; + if (m_vp.count() != totalCount) { + m_vp.resize(totalCount); + m_accDirty |= DirtyList; + } + for (VisualPathData &d : m_vp) + d.syncDirty = 0; } -void QQuickPathItemGenericRenderer::setPath(const QQuickPath *path) +void QQuickPathItemGenericRenderer::setPath(int index, const QQuickPath *path) { - m_path = path ? path->path() : QPainterPath(); - m_syncDirty |= DirtyGeom; + VisualPathData &d(m_vp[index]); + d.path = path ? path->path() : QPainterPath(); + d.syncDirty |= DirtyGeom; } -void QQuickPathItemGenericRenderer::setStrokeColor(const QColor &color) +void QQuickPathItemGenericRenderer::setStrokeColor(int index, const QColor &color) { - m_strokeColor = colorToColor4ub(color); - m_syncDirty |= DirtyColor; + VisualPathData &d(m_vp[index]); + d.strokeColor = colorToColor4ub(color); + d.syncDirty |= DirtyColor; } -void QQuickPathItemGenericRenderer::setStrokeWidth(qreal w) +void QQuickPathItemGenericRenderer::setStrokeWidth(int index, qreal w) { - m_strokeWidth = w; + VisualPathData &d(m_vp[index]); + d.strokeWidth = w; if (w >= 0.0f) - m_pen.setWidthF(w); - m_syncDirty |= DirtyGeom; + d.pen.setWidthF(w); + d.syncDirty |= DirtyGeom; } -void QQuickPathItemGenericRenderer::setFillColor(const QColor &color) +void QQuickPathItemGenericRenderer::setFillColor(int index, const QColor &color) { - m_fillColor = colorToColor4ub(color); - m_syncDirty |= DirtyColor; + VisualPathData &d(m_vp[index]); + d.fillColor = colorToColor4ub(color); + d.syncDirty |= DirtyColor; } -void QQuickPathItemGenericRenderer::setFillRule(QQuickPathItem::FillRule fillRule) +void QQuickPathItemGenericRenderer::setFillRule(int index, QQuickVisualPath::FillRule fillRule) { - m_fillRule = Qt::FillRule(fillRule); - m_syncDirty |= DirtyGeom; + VisualPathData &d(m_vp[index]); + d.fillRule = Qt::FillRule(fillRule); + d.syncDirty |= DirtyGeom; } -void QQuickPathItemGenericRenderer::setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) +void QQuickPathItemGenericRenderer::setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) { - m_pen.setJoinStyle(Qt::PenJoinStyle(joinStyle)); - m_pen.setMiterLimit(miterLimit); - m_syncDirty |= DirtyGeom; + VisualPathData &d(m_vp[index]); + d.pen.setJoinStyle(Qt::PenJoinStyle(joinStyle)); + d.pen.setMiterLimit(miterLimit); + d.syncDirty |= DirtyGeom; } -void QQuickPathItemGenericRenderer::setCapStyle(QQuickPathItem::CapStyle capStyle) +void QQuickPathItemGenericRenderer::setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) { - m_pen.setCapStyle(Qt::PenCapStyle(capStyle)); - m_syncDirty |= DirtyGeom; + VisualPathData &d(m_vp[index]); + d.pen.setCapStyle(Qt::PenCapStyle(capStyle)); + d.syncDirty |= DirtyGeom; } -void QQuickPathItemGenericRenderer::setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, +void QQuickPathItemGenericRenderer::setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, qreal dashOffset, const QVector &dashPattern) { - m_pen.setStyle(Qt::PenStyle(strokeStyle)); - if (strokeStyle == QQuickPathItem::DashLine) { - m_pen.setDashPattern(dashPattern); - m_pen.setDashOffset(dashOffset); + VisualPathData &d(m_vp[index]); + d.pen.setStyle(Qt::PenStyle(strokeStyle)); + if (strokeStyle == QQuickVisualPath::DashLine) { + d.pen.setDashPattern(dashPattern); + d.pen.setDashOffset(dashOffset); } - m_syncDirty |= DirtyGeom; + d.syncDirty |= DirtyGeom; } -void QQuickPathItemGenericRenderer::setFillGradient(QQuickPathGradient *gradient) +void QQuickPathItemGenericRenderer::setFillGradient(int index, QQuickPathGradient *gradient) { - m_fillGradientActive = gradient != nullptr; + VisualPathData &d(m_vp[index]); + d.fillGradientActive = gradient != nullptr; if (gradient) { - m_fillGradient.stops = gradient->sortedGradientStops(); - m_fillGradient.spread = gradient->spread(); + d.fillGradient.stops = gradient->sortedGradientStops(); + d.fillGradient.spread = gradient->spread(); if (QQuickPathLinearGradient *g = qobject_cast(gradient)) { - m_fillGradient.start = QPointF(g->x1(), g->y1()); - m_fillGradient.end = QPointF(g->x2(), g->y2()); + d.fillGradient.start = QPointF(g->x1(), g->y1()); + d.fillGradient.end = QPointF(g->x2(), g->y2()); } else { Q_UNREACHABLE(); } } - m_syncDirty |= DirtyFillGradient; + d.syncDirty |= DirtyFillGradient; } void QQuickPathItemGenericRenderer::endSync() { - if (!m_syncDirty) - return; + for (VisualPathData &d : m_vp) { + if (!d.syncDirty) + continue; + + m_accDirty |= d.syncDirty; + + // Use a shadow dirty flag in order to avoid losing state in case there are + // multiple syncs with different dirty flags before we get to updateNode() + // on the render thread (with the gui thread blocked). For our purposes + // here syncDirty is still required since geometry regeneration must only + // happen when there was an actual change in this particular sync round. + d.effectiveDirty |= d.syncDirty; + + if (d.path.isEmpty()) { + d.fillVertices.clear(); + d.fillIndices.clear(); + d.strokeVertices.clear(); + continue; + } - // Use a shadow dirty flag in order to avoid losing state in case there are - // multiple syncs with different dirty flags before we get to - // updatePathRenderNode() on the render thread (with the gui thread - // blocked). For our purposes here m_syncDirty is still required since - // geometry regeneration must only happen when there was an actual change - // in this particular sync round. - m_effectiveDirty |= m_syncDirty; - - if (m_path.isEmpty()) { - m_fillVertices.clear(); - m_fillIndices.clear(); - m_strokeVertices.clear(); - return; + if (d.syncDirty & DirtyGeom) { + if (d.fillColor.a) + triangulateFill(&d); + if (d.strokeWidth >= 0.0f && d.strokeColor.a) + triangulateStroke(&d); + } } - - triangulateFill(); - triangulateStroke(); } -void QQuickPathItemGenericRenderer::triangulateFill() +void QQuickPathItemGenericRenderer::triangulateFill(VisualPathData *d) { - m_path.setFillRule(m_fillRule); + d->path.setFillRule(d->fillRule); - const QVectorPath &vp = qtVectorPathForPath(m_path); + const QVectorPath &vp = qtVectorPathForPath(d->path); QTriangleSet ts = qTriangulate(vp, QTransform::fromScale(SCALE, SCALE)); const int vertexCount = ts.vertices.count() / 2; // just a qreal vector with x,y hence the / 2 - m_fillVertices.resize(vertexCount); - ColoredVertex *vdst = reinterpret_cast(m_fillVertices.data()); + d->fillVertices.resize(vertexCount); + ColoredVertex *vdst = reinterpret_cast(d->fillVertices.data()); const qreal *vsrc = ts.vertices.constData(); for (int i = 0; i < vertexCount; ++i) - vdst[i].set(vsrc[i * 2] / SCALE, vsrc[i * 2 + 1] / SCALE, m_fillColor); + vdst[i].set(vsrc[i * 2] / SCALE, vsrc[i * 2 + 1] / SCALE, d->fillColor); - m_fillIndices.resize(ts.indices.size()); - quint16 *idst = m_fillIndices.data(); + d->fillIndices.resize(ts.indices.size()); + quint16 *idst = d->fillIndices.data(); if (ts.indices.type() == QVertexIndexVector::UnsignedShort) { - memcpy(idst, ts.indices.data(), m_fillIndices.count() * sizeof(quint16)); + memcpy(idst, ts.indices.data(), d->fillIndices.count() * sizeof(quint16)); } else { const quint32 *isrc = (const quint32 *) ts.indices.data(); - for (int i = 0; i < m_fillIndices.count(); ++i) + for (int i = 0; i < d->fillIndices.count(); ++i) idst[i] = isrc[i]; } } -void QQuickPathItemGenericRenderer::triangulateStroke() +void QQuickPathItemGenericRenderer::triangulateStroke(VisualPathData *d) { - const QVectorPath &vp = qtVectorPathForPath(m_path); + const QVectorPath &vp = qtVectorPathForPath(d->path); const QRectF clip(0, 0, m_item->width(), m_item->height()); const qreal inverseScale = 1.0 / SCALE; m_stroker.setInvScale(inverseScale); - if (m_pen.style() == Qt::SolidLine) { - m_stroker.process(vp, m_pen, clip, 0); + if (d->pen.style() == Qt::SolidLine) { + m_stroker.process(vp, d->pen, clip, 0); } else { m_dashStroker.setInvScale(inverseScale); - m_dashStroker.process(vp, m_pen, clip, 0); + m_dashStroker.process(vp, d->pen, clip, 0); QVectorPath dashStroke(m_dashStroker.points(), m_dashStroker.elementCount(), m_dashStroker.elementTypes(), 0); - m_stroker.process(dashStroke, m_pen, clip, 0); + m_stroker.process(dashStroke, d->pen, clip, 0); } if (!m_stroker.vertexCount()) { - m_strokeVertices.clear(); + d->strokeVertices.clear(); return; } const int vertexCount = m_stroker.vertexCount() / 2; // just a float vector with x,y hence the / 2 - m_strokeVertices.resize(vertexCount); - ColoredVertex *vdst = reinterpret_cast(m_strokeVertices.data()); + d->strokeVertices.resize(vertexCount); + ColoredVertex *vdst = reinterpret_cast(d->strokeVertices.data()); const float *vsrc = m_stroker.vertices(); for (int i = 0; i < vertexCount; ++i) - vdst[i].set(vsrc[i * 2], vsrc[i * 2 + 1], m_strokeColor); + vdst[i].set(vsrc[i * 2], vsrc[i * 2 + 1], d->strokeColor); } -void QQuickPathItemGenericRenderer::setRootNode(QQuickPathItemGenericRootRenderNode *rn) +void QQuickPathItemGenericRenderer::setRootNode(QQuickPathItemGenericNode *node) { - if (m_rootNode != rn) { - m_rootNode = rn; - // Scenegraph nodes can be destroyed and then replaced by new ones over - // time; hence it is important to mark everything dirty for - // updatePathRenderNode(). We can assume the renderer has a full sync - // of the data at this point. - m_effectiveDirty = DirtyAll; + if (m_rootNode != node) { + m_rootNode = node; + m_accDirty |= DirtyList; } } // on the render thread with gui blocked -void QQuickPathItemGenericRenderer::updatePathRenderNode() +void QQuickPathItemGenericRenderer::updateNode() { - if (!m_effectiveDirty || !m_rootNode) + if (!m_rootNode || !m_accDirty) return; - if (m_fillColor.a == 0) { - delete m_rootNode->m_fillNode; - m_rootNode->m_fillNode = nullptr; - } else if (!m_rootNode->m_fillNode) { - m_rootNode->m_fillNode = new QQuickPathItemGenericRenderNode(m_item->window(), m_rootNode); - if (m_rootNode->m_strokeNode) - m_rootNode->removeChildNode(m_rootNode->m_strokeNode); - m_rootNode->appendChildNode(m_rootNode->m_fillNode); - if (m_rootNode->m_strokeNode) - m_rootNode->appendChildNode(m_rootNode->m_strokeNode); - m_effectiveDirty |= DirtyGeom; - } +// [ m_rootNode ] +// / / / +// #0 [ fill ] [ stroke ] [ next ] +// / / | +// #1 [ fill ] [ stroke ] [ next ] +// / / | +// #2 [ fill ] [ stroke ] [ next ] +// ... +// ... + + QQuickPathItemGenericNode **nodePtr = &m_rootNode; + QQuickPathItemGenericNode *prevNode = nullptr; + + for (VisualPathData &d : m_vp) { + if (!*nodePtr) { + *nodePtr = new QQuickPathItemGenericNode; + prevNode->m_next = *nodePtr; + prevNode->appendChildNode(*nodePtr); + } - if (m_strokeWidth < 0.0f || m_strokeColor.a == 0) { - delete m_rootNode->m_strokeNode; - m_rootNode->m_strokeNode = nullptr; - } else if (!m_rootNode->m_strokeNode) { - m_rootNode->m_strokeNode = new QQuickPathItemGenericRenderNode(m_item->window(), m_rootNode); - m_rootNode->appendChildNode(m_rootNode->m_strokeNode); - m_effectiveDirty |= DirtyGeom; + QQuickPathItemGenericNode *node = *nodePtr; + + if (m_accDirty & DirtyList) + d.effectiveDirty |= DirtyGeom; + if (!d.effectiveDirty) + continue; + + if (d.fillColor.a == 0) { + delete node->m_fillNode; + node->m_fillNode = nullptr; + } else if (!node->m_fillNode) { + node->m_fillNode = new QQuickPathItemGenericStrokeFillNode(m_item->window()); + if (node->m_strokeNode) + node->removeChildNode(node->m_strokeNode); + node->appendChildNode(node->m_fillNode); + if (node->m_strokeNode) + node->appendChildNode(node->m_strokeNode); + d.effectiveDirty |= DirtyGeom; + } + + if (d.strokeWidth < 0.0f || d.strokeColor.a == 0) { + delete node->m_strokeNode; + node->m_strokeNode = nullptr; + } else if (!node->m_strokeNode) { + node->m_strokeNode = new QQuickPathItemGenericStrokeFillNode(m_item->window()); + node->appendChildNode(node->m_strokeNode); + d.effectiveDirty |= DirtyGeom; + } + + updateFillNode(&d, node); + updateStrokeNode(&d, node); + + d.effectiveDirty = 0; + + prevNode = node; + nodePtr = &node->m_next; } - updateFillNode(); - updateStrokeNode(); + if (*nodePtr && prevNode) { + prevNode->removeChildNode(*nodePtr); + delete *nodePtr; + *nodePtr = nullptr; + } - m_effectiveDirty = 0; + m_accDirty = 0; } -void QQuickPathItemGenericRenderer::updateFillNode() +void QQuickPathItemGenericRenderer::updateFillNode(VisualPathData *d, QQuickPathItemGenericNode *node) { - if (!m_rootNode->m_fillNode) + if (!node->m_fillNode) return; - QQuickPathItemGenericRenderNode *n = m_rootNode->m_fillNode; + QQuickPathItemGenericStrokeFillNode *n = node->m_fillNode; QSGGeometry *g = &n->m_geometry; - if (m_fillVertices.isEmpty()) { + if (d->fillVertices.isEmpty()) { g->allocate(0, 0); n->markDirty(QSGNode::DirtyGeometry); return; } - if (m_fillGradientActive) { - n->activateMaterial(QQuickPathItemGenericRenderNode::MatLinearGradient); - if (m_effectiveDirty & DirtyFillGradient) { + if (d->fillGradientActive) { + n->activateMaterial(QQuickPathItemGenericStrokeFillNode::MatLinearGradient); + if (d->effectiveDirty & DirtyFillGradient) { // Make a copy of the data that will be accessed by the material on // the render thread. - n->m_fillGradient = m_fillGradient; + n->m_fillGradient = d->fillGradient; // Gradients are implemented via a texture-based material. n->markDirty(QSGNode::DirtyMaterial); // stop here if only the gradient changed; no need to touch the geometry - if (!(m_effectiveDirty & DirtyGeom)) + if (!(d->effectiveDirty & DirtyGeom)) return; } } else { - n->activateMaterial(QQuickPathItemGenericRenderNode::MatSolidColor); + n->activateMaterial(QQuickPathItemGenericStrokeFillNode::MatSolidColor); // fast path for updating only color values when no change in vertex positions - if ((m_effectiveDirty & DirtyColor) && !(m_effectiveDirty & DirtyGeom)) { + if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyGeom)) { ColoredVertex *vdst = reinterpret_cast(g->vertexData()); for (int i = 0; i < g->vertexCount(); ++i) - vdst[i].set(vdst[i].x, vdst[i].y, m_fillColor); + vdst[i].set(vdst[i].x, vdst[i].y, d->fillColor); n->markDirty(QSGNode::DirtyGeometry); return; } } - g->allocate(m_fillVertices.count(), m_fillIndices.count()); + g->allocate(d->fillVertices.count(), d->fillIndices.count()); g->setDrawingMode(QSGGeometry::DrawTriangles); - memcpy(g->vertexData(), m_fillVertices.constData(), g->vertexCount() * g->sizeOfVertex()); - memcpy(g->indexData(), m_fillIndices.constData(), g->indexCount() * g->sizeOfIndex()); + memcpy(g->vertexData(), d->fillVertices.constData(), g->vertexCount() * g->sizeOfVertex()); + memcpy(g->indexData(), d->fillIndices.constData(), g->indexCount() * g->sizeOfIndex()); n->markDirty(QSGNode::DirtyGeometry); } -void QQuickPathItemGenericRenderer::updateStrokeNode() +void QQuickPathItemGenericRenderer::updateStrokeNode(VisualPathData *d, QQuickPathItemGenericNode *node) { - if (!m_rootNode->m_strokeNode) + if (!node->m_strokeNode) return; - if (m_effectiveDirty == DirtyFillGradient) // not applicable + if (d->effectiveDirty == DirtyFillGradient) // not applicable return; - QQuickPathItemGenericRenderNode *n = m_rootNode->m_strokeNode; + QQuickPathItemGenericStrokeFillNode *n = node->m_strokeNode; n->markDirty(QSGNode::DirtyGeometry); QSGGeometry *g = &n->m_geometry; - if (m_strokeVertices.isEmpty()) { + if (d->strokeVertices.isEmpty()) { g->allocate(0, 0); return; } - if ((m_effectiveDirty & DirtyColor) && !(m_effectiveDirty & DirtyGeom)) { + if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyGeom)) { ColoredVertex *vdst = reinterpret_cast(g->vertexData()); for (int i = 0; i < g->vertexCount(); ++i) - vdst[i].set(vdst[i].x, vdst[i].y, m_strokeColor); + vdst[i].set(vdst[i].x, vdst[i].y, d->strokeColor); return; } - g->allocate(m_strokeVertices.count(), 0); + g->allocate(d->strokeVertices.count(), 0); g->setDrawingMode(QSGGeometry::DrawTriangleStrip); - memcpy(g->vertexData(), m_strokeVertices.constData(), g->vertexCount() * g->sizeOfVertex()); + memcpy(g->vertexData(), d->strokeVertices.constData(), g->vertexCount() * g->sizeOfVertex()); } QSGMaterial *QQuickPathItemGenericMaterialFactory::createVertexColor(QQuickWindow *window) @@ -415,7 +445,7 @@ QSGMaterial *QQuickPathItemGenericMaterialFactory::createVertexColor(QQuickWindo } QSGMaterial *QQuickPathItemGenericMaterialFactory::createLinearGradient(QQuickWindow *window, - QQuickPathItemGenericRenderNode *node) + QQuickPathItemGenericStrokeFillNode *node) { QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi(); @@ -458,7 +488,7 @@ void QQuickPathItemLinearGradientShader::updateState(const RenderState &state, Q if (state.isMatrixDirty()) program()->setUniformValue(m_matrixLoc, state.combinedMatrix()); - QQuickPathItemGenericRenderNode *node = m->node(); + QQuickPathItemGenericStrokeFillNode *node = m->node(); program()->setUniformValue(m_gradStartLoc, QVector2D(node->m_fillGradient.start)); program()->setUniformValue(m_gradEndLoc, QVector2D(node->m_fillGradient.end)); @@ -477,8 +507,8 @@ int QQuickPathItemLinearGradientMaterial::compare(const QSGMaterial *other) cons Q_ASSERT(other && type() == other->type()); const QQuickPathItemLinearGradientMaterial *m = static_cast(other); - QQuickPathItemGenericRenderNode *a = node(); - QQuickPathItemGenericRenderNode *b = m->node(); + QQuickPathItemGenericStrokeFillNode *a = node(); + QQuickPathItemGenericStrokeFillNode *b = m->node(); Q_ASSERT(a && b); if (a == b) return 0; diff --git a/src/quick/items/qquickpathitemgenericrenderer_p.h b/src/quick/items/qquickpathitemgenericrenderer_p.h index a094b9fca6..4454b14a13 100644 --- a/src/quick/items/qquickpathitemgenericrenderer_p.h +++ b/src/quick/items/qquickpathitemgenericrenderer_p.h @@ -59,7 +59,7 @@ QT_BEGIN_NAMESPACE -class QQuickPathItemGenericRootRenderNode; +class QQuickPathItemGenericNode; class QQuickPathItemGenericRenderer : public QQuickAbstractPathRenderer { @@ -68,67 +68,69 @@ public: DirtyGeom = 0x01, DirtyColor = 0x02, DirtyFillGradient = 0x04, - - DirtyAll = 0xFF + DirtyList = 0x08 }; QQuickPathItemGenericRenderer(QQuickItem *item) : m_item(item), m_rootNode(nullptr), - m_effectiveDirty(0) + m_accDirty(0) { } - void beginSync() override; - void setPath(const QQuickPath *path) override; - void setStrokeColor(const QColor &color) override; - void setStrokeWidth(qreal w) override; - void setFillColor(const QColor &color) override; - void setFillRule(QQuickPathItem::FillRule fillRule) override; - void setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) override; - void setCapStyle(QQuickPathItem::CapStyle capStyle) override; - void setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, + void beginSync(int totalCount) override; + void setPath(int index, const QQuickPath *path) override; + 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, QQuickVisualPath::FillRule fillRule) override; + void setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) override; + void setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) override; + void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, qreal dashOffset, const QVector &dashPattern) override; - void setFillGradient(QQuickPathGradient *gradient) override; + void setFillGradient(int index, QQuickPathGradient *gradient) override; void endSync() override; - void updatePathRenderNode() override; - void setRootNode(QQuickPathItemGenericRootRenderNode *rn); + void updateNode() override; + + void setRootNode(QQuickPathItemGenericNode *node); struct Color4ub { unsigned char r, g, b, a; }; private: - void triangulateFill(); - void triangulateStroke(); - void updateFillNode(); - void updateStrokeNode(); + struct VisualPathData { + float strokeWidth; + QPen pen; + Color4ub strokeColor; + Color4ub fillColor; + Qt::FillRule fillRule; + QPainterPath path; + bool fillGradientActive; + QQuickPathItemGradientCache::GradientDesc fillGradient; + QVector fillVertices; + QVector fillIndices; + QVector strokeVertices; + int syncDirty; + int effectiveDirty = 0; + }; + + void triangulateFill(VisualPathData *d); + void triangulateStroke(VisualPathData *d); + + void updateFillNode(VisualPathData *d, QQuickPathItemGenericNode *node); + void updateStrokeNode(VisualPathData *d, QQuickPathItemGenericNode *node); QQuickItem *m_item; - QQuickPathItemGenericRootRenderNode *m_rootNode; + QQuickPathItemGenericNode *m_rootNode; QTriangulatingStroker m_stroker; QDashedStrokeProcessor m_dashStroker; - - float m_strokeWidth; - QPen m_pen; - Color4ub m_strokeColor; - Color4ub m_fillColor; - Qt::FillRule m_fillRule; - QPainterPath m_path; - bool m_fillGradientActive; - QQuickPathItemGradientCache::GradientDesc m_fillGradient; - - QVector m_fillVertices; - QVector m_fillIndices; - QVector m_strokeVertices; - - int m_syncDirty; - int m_effectiveDirty; + QVector m_vp; + int m_accDirty; }; -class QQuickPathItemGenericRenderNode : public QSGGeometryNode +class QQuickPathItemGenericStrokeFillNode : public QSGGeometryNode { public: - QQuickPathItemGenericRenderNode(QQuickWindow *window, QQuickPathItemGenericRootRenderNode *rootNode); - ~QQuickPathItemGenericRenderNode(); + QQuickPathItemGenericStrokeFillNode(QQuickWindow *window); enum Material { MatSolidColor, @@ -138,7 +140,6 @@ public: void activateMaterial(Material m); QQuickWindow *window() const { return m_window; } - QQuickPathItemGenericRootRenderNode *rootNode() const { return m_rootNode; } // shadow data for custom materials QQuickPathItemGradientCache::GradientDesc m_fillGradient; @@ -146,7 +147,6 @@ public: private: QSGGeometry m_geometry; QQuickWindow *m_window; - QQuickPathItemGenericRootRenderNode *m_rootNode; QSGMaterial *m_material; QScopedPointer m_solidColorMaterial; QScopedPointer m_linearGradientMaterial; @@ -154,24 +154,19 @@ private: friend class QQuickPathItemGenericRenderer; }; -class QQuickPathItemGenericRootRenderNode : public QSGNode +class QQuickPathItemGenericNode : public QSGNode { public: - QQuickPathItemGenericRootRenderNode(QQuickWindow *window, bool hasFill, bool hasStroke); - ~QQuickPathItemGenericRootRenderNode(); - -private: - QQuickPathItemGenericRenderNode *m_fillNode; - QQuickPathItemGenericRenderNode *m_strokeNode; - - friend class QQuickPathItemGenericRenderer; + QQuickPathItemGenericStrokeFillNode *m_fillNode = nullptr; + QQuickPathItemGenericStrokeFillNode *m_strokeNode = nullptr; + QQuickPathItemGenericNode *m_next = nullptr; }; class QQuickPathItemGenericMaterialFactory { public: static QSGMaterial *createVertexColor(QQuickWindow *window); - static QSGMaterial *createLinearGradient(QQuickWindow *window, QQuickPathItemGenericRenderNode *node); + static QSGMaterial *createLinearGradient(QQuickWindow *window, QQuickPathItemGenericStrokeFillNode *node); }; #ifndef QT_NO_OPENGL @@ -197,7 +192,7 @@ private: class QQuickPathItemLinearGradientMaterial : public QSGMaterial { public: - QQuickPathItemLinearGradientMaterial(QQuickPathItemGenericRenderNode *node) + QQuickPathItemLinearGradientMaterial(QQuickPathItemGenericStrokeFillNode *node) : m_node(node) { // Passing RequiresFullMatrix is essential in order to prevent the @@ -220,10 +215,10 @@ public: return new QQuickPathItemLinearGradientShader; } - QQuickPathItemGenericRenderNode *node() const { return m_node; } + QQuickPathItemGenericStrokeFillNode *node() const { return m_node; } private: - QQuickPathItemGenericRenderNode *m_node; + QQuickPathItemGenericStrokeFillNode *m_node; }; #endif // QT_NO_OPENGL diff --git a/src/quick/items/qquickpathitemnvprrenderer.cpp b/src/quick/items/qquickpathitemnvprrenderer.cpp index 5641d33f85..2338e51ff8 100644 --- a/src/quick/items/qquickpathitemnvprrenderer.cpp +++ b/src/quick/items/qquickpathitemnvprrenderer.cpp @@ -46,93 +46,109 @@ QT_BEGIN_NAMESPACE -void QQuickPathItemNvprRenderer::beginSync() +void QQuickPathItemNvprRenderer::beginSync(int totalCount) { - // nothing to do here + if (m_vp.count() != totalCount) { + m_vp.resize(totalCount); + m_accDirty |= DirtyList; + } } -void QQuickPathItemNvprRenderer::setPath(const QQuickPath *path) +void QQuickPathItemNvprRenderer::setPath(int index, const QQuickPath *path) { - convertPath(path); - m_dirty |= DirtyPath; + VisualPathGuiData &d(m_vp[index]); + convertPath(path, &d); + d.dirty |= DirtyPath; + m_accDirty |= DirtyPath; } -void QQuickPathItemNvprRenderer::setStrokeColor(const QColor &color) +void QQuickPathItemNvprRenderer::setStrokeColor(int index, const QColor &color) { - m_strokeColor = color; - m_dirty |= DirtyStyle; + VisualPathGuiData &d(m_vp[index]); + d.strokeColor = color; + d.dirty |= DirtyStyle; + m_accDirty |= DirtyStyle; } -void QQuickPathItemNvprRenderer::setStrokeWidth(qreal w) +void QQuickPathItemNvprRenderer::setStrokeWidth(int index, qreal w) { - m_strokeWidth = w; - m_dirty |= DirtyStyle; + VisualPathGuiData &d(m_vp[index]); + d.strokeWidth = w; + d.dirty |= DirtyStyle; + m_accDirty |= DirtyStyle; } -void QQuickPathItemNvprRenderer::setFillColor(const QColor &color) +void QQuickPathItemNvprRenderer::setFillColor(int index, const QColor &color) { - m_fillColor = color; - m_dirty |= DirtyStyle; + VisualPathGuiData &d(m_vp[index]); + d.fillColor = color; + d.dirty |= DirtyStyle; + m_accDirty |= DirtyStyle; } -void QQuickPathItemNvprRenderer::setFillRule(QQuickPathItem::FillRule fillRule) +void QQuickPathItemNvprRenderer::setFillRule(int index, QQuickVisualPath::FillRule fillRule) { - m_fillRule = fillRule; - m_dirty |= DirtyFillRule; + VisualPathGuiData &d(m_vp[index]); + d.fillRule = fillRule; + d.dirty |= DirtyFillRule; + m_accDirty |= DirtyFillRule; } -void QQuickPathItemNvprRenderer::setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) +void QQuickPathItemNvprRenderer::setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) { - m_joinStyle = joinStyle; - m_miterLimit = miterLimit; - m_dirty |= DirtyStyle; + VisualPathGuiData &d(m_vp[index]); + d.joinStyle = joinStyle; + d.miterLimit = miterLimit; + d.dirty |= DirtyStyle; + m_accDirty |= DirtyStyle; } -void QQuickPathItemNvprRenderer::setCapStyle(QQuickPathItem::CapStyle capStyle) +void QQuickPathItemNvprRenderer::setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) { - m_capStyle = capStyle; - m_dirty |= DirtyStyle; + VisualPathGuiData &d(m_vp[index]); + d.capStyle = capStyle; + d.dirty |= DirtyStyle; + m_accDirty |= DirtyStyle; } -void QQuickPathItemNvprRenderer::setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, - qreal dashOffset, const QVector &dashPattern) +void QQuickPathItemNvprRenderer::setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, + qreal dashOffset, const QVector &dashPattern) { - m_dashActive = strokeStyle == QQuickPathItem::DashLine; - m_dashOffset = dashOffset; - m_dashPattern = dashPattern; - m_dirty |= DirtyDash; + VisualPathGuiData &d(m_vp[index]); + d.dashActive = strokeStyle == QQuickVisualPath::DashLine; + d.dashOffset = dashOffset; + d.dashPattern = dashPattern; + d.dirty |= DirtyDash; + m_accDirty |= DirtyDash; } -void QQuickPathItemNvprRenderer::setFillGradient(QQuickPathGradient *gradient) +void QQuickPathItemNvprRenderer::setFillGradient(int index, QQuickPathGradient *gradient) { - m_fillGradientActive = gradient != nullptr; + VisualPathGuiData &d(m_vp[index]); + d.fillGradientActive = gradient != nullptr; if (gradient) { - m_fillGradient.stops = gradient->sortedGradientStops(); - m_fillGradient.spread = gradient->spread(); + d.fillGradient.stops = gradient->sortedGradientStops(); + d.fillGradient.spread = gradient->spread(); if (QQuickPathLinearGradient *g = qobject_cast(gradient)) { - m_fillGradient.start = QPointF(g->x1(), g->y1()); - m_fillGradient.end = QPointF(g->x2(), g->y2()); + d.fillGradient.start = QPointF(g->x1(), g->y1()); + d.fillGradient.end = QPointF(g->x2(), g->y2()); } else { Q_UNREACHABLE(); } } - m_dirty |= DirtyFillGradient; + d.dirty |= DirtyFillGradient; + m_accDirty |= DirtyFillGradient; } void QQuickPathItemNvprRenderer::endSync() { - // nothing to do here } void QQuickPathItemNvprRenderer::setNode(QQuickPathItemNvprRenderNode *node) { if (m_node != node) { m_node = node; - // Scenegraph nodes can be destroyed and then replaced by new ones over - // time; hence it is important to mark everything dirty for - // updatePathRenderNode(). We can assume the renderer has a full sync - // of the data at this point. - m_dirty = DirtyAll; + m_accDirty |= DirtyList; } } @@ -140,6 +156,10 @@ QDebug operator<<(QDebug debug, const QQuickPathItemNvprRenderer::NvprPath &path { QDebugStateSaver saver(debug); debug.space().noquote(); + if (!path.str.isEmpty()) { + debug << "Path with SVG string" << path.str; + return debug; + } debug << "Path with" << path.cmd.count() << "commands"; int ci = 0; for (GLubyte cmd : path.cmd) { @@ -201,9 +221,9 @@ static inline void appendControl2Coords(QVector *v, QQuickPathCubic *c, v->append(p.y()); } -void QQuickPathItemNvprRenderer::convertPath(const QQuickPath *path) +void QQuickPathItemNvprRenderer::convertPath(const QQuickPath *path, VisualPathGuiData *d) { - m_path = NvprPath(); + d->path = NvprPath(); if (!path) return; @@ -211,27 +231,31 @@ void QQuickPathItemNvprRenderer::convertPath(const QQuickPath *path) if (pp.isEmpty()) return; - QPointF pos(path->startX(), path->startY()); - m_path.cmd.append(GL_MOVE_TO_NV); - m_path.coord.append(pos.x()); - m_path.coord.append(pos.y()); + QPointF startPos(path->startX(), path->startY()); + QPointF pos(startPos); + if (!qFuzzyIsNull(pos.x()) || !qFuzzyIsNull(pos.y())) { + d->path.cmd.append(GL_MOVE_TO_NV); + d->path.coord.append(pos.x()); + d->path.coord.append(pos.y()); + } for (QQuickPathElement *e : pp) { if (QQuickPathMove *o = qobject_cast(e)) { - m_path.cmd.append(GL_MOVE_TO_NV); - appendCoords(&m_path.coord, o, &pos); + d->path.cmd.append(GL_MOVE_TO_NV); + appendCoords(&d->path.coord, o, &pos); + startPos = pos; } else if (QQuickPathLine *o = qobject_cast(e)) { - m_path.cmd.append(GL_LINE_TO_NV); - appendCoords(&m_path.coord, o, &pos); + d->path.cmd.append(GL_LINE_TO_NV); + appendCoords(&d->path.coord, o, &pos); } else if (QQuickPathQuad *o = qobject_cast(e)) { - m_path.cmd.append(GL_QUADRATIC_CURVE_TO_NV); - appendControlCoords(&m_path.coord, o, pos); - appendCoords(&m_path.coord, o, &pos); + d->path.cmd.append(GL_QUADRATIC_CURVE_TO_NV); + appendControlCoords(&d->path.coord, o, pos); + appendCoords(&d->path.coord, o, &pos); } else if (QQuickPathCubic *o = qobject_cast(e)) { - m_path.cmd.append(GL_CUBIC_CURVE_TO_NV); - appendControl1Coords(&m_path.coord, o, pos); - appendControl2Coords(&m_path.coord, o, pos); - appendCoords(&m_path.coord, o, &pos); + d->path.cmd.append(GL_CUBIC_CURVE_TO_NV); + appendControl1Coords(&d->path.coord, o, pos); + appendControl2Coords(&d->path.coord, o, pos); + appendCoords(&d->path.coord, o, &pos); } else if (QQuickPathArc *o = qobject_cast(e)) { const bool sweepFlag = o->direction() == QQuickPathArc::Clockwise; // maps to CCW, not a typo GLenum cmd; @@ -239,18 +263,29 @@ void QQuickPathItemNvprRenderer::convertPath(const QQuickPath *path) cmd = sweepFlag ? GL_LARGE_CCW_ARC_TO_NV : GL_LARGE_CW_ARC_TO_NV; else cmd = sweepFlag ? GL_SMALL_CCW_ARC_TO_NV : GL_SMALL_CW_ARC_TO_NV; - m_path.cmd.append(cmd); - m_path.coord.append(o->radiusX()); - m_path.coord.append(o->radiusY()); - m_path.coord.append(o->xAxisRotation()); - appendCoords(&m_path.coord, o, &pos); + d->path.cmd.append(cmd); + d->path.coord.append(o->radiusX()); + d->path.coord.append(o->radiusY()); + d->path.coord.append(o->xAxisRotation()); + appendCoords(&d->path.coord, o, &pos); + } else if (QQuickPathSvg *o = qobject_cast(e)) { + // PathSvg cannot be combined with other elements. But take at + // least startX and startY into account. + if (d->path.str.isEmpty()) + d->path.str = QString(QStringLiteral("M %1 %2 ")).arg(pos.x()).arg(pos.y()).toUtf8(); + d->path.str.append(o->path().toUtf8()); } else { qWarning() << "PathItem/NVPR: unsupported Path element" << e; } } - if (qFuzzyCompare(pos.x(), path->startX()) && qFuzzyCompare(pos.y(), path->startY())) - m_path.cmd.append(GL_CLOSE_PATH_NV); + // For compatibility with QTriangulatingStroker. SVG and others would not + // implicitly close the path when end_pos == start_pos (start_pos being the + // last moveTo pos); that would still need an explicit 'z' or similar. We + // don't have an explicit close command, so just fake a close when the + // positions match. + if (pos == startPos) + d->path.cmd.append(GL_CLOSE_PATH_NV); } static inline QVector4D qsg_premultiply(const QColor &c, float globalOpacity) @@ -259,88 +294,103 @@ static inline QVector4D qsg_premultiply(const QColor &c, float globalOpacity) return QVector4D(c.redF() * o, c.greenF() * o, c.blueF() * o, o); } -void QQuickPathItemNvprRenderer::updatePathRenderNode() +void QQuickPathItemNvprRenderer::updateNode() { // Called on the render thread with gui blocked -> update the node with its // own copy of all relevant data. - if (!m_dirty) + if (!m_accDirty) return; - // updatePathRenderNode() can be called several times with different dirty - // state before render() gets invoked. So accumulate. - m_node->m_dirty |= m_dirty; - - if (m_dirty & DirtyPath) - m_node->m_source = m_path; - - if (m_dirty & DirtyStyle) { - m_node->m_strokeWidth = m_strokeWidth; - m_node->m_strokeColor = qsg_premultiply(m_strokeColor, 1.0f); - m_node->m_fillColor = qsg_premultiply(m_fillColor, 1.0f); - switch (m_joinStyle) { - case QQuickPathItem::MiterJoin: - m_node->m_joinStyle = GL_MITER_TRUNCATE_NV; - break; - case QQuickPathItem::BevelJoin: - m_node->m_joinStyle = GL_BEVEL_NV; - break; - case QQuickPathItem::RoundJoin: - m_node->m_joinStyle = GL_ROUND_NV; - break; - default: - Q_UNREACHABLE(); - } - m_node->m_miterLimit = m_miterLimit; - switch (m_capStyle) { - case QQuickPathItem::FlatCap: - m_node->m_capStyle = GL_FLAT; - break; - case QQuickPathItem::SquareCap: - m_node->m_capStyle = GL_SQUARE_NV; - break; - case QQuickPathItem::RoundCap: - m_node->m_capStyle = GL_ROUND_NV; - break; - default: - Q_UNREACHABLE(); + const int count = m_vp.count(); + const bool listChanged = m_accDirty & DirtyList; + if (listChanged) + m_node->m_vp.resize(count); + + for (int i = 0; i < count; ++i) { + VisualPathGuiData &src(m_vp[i]); + QQuickPathItemNvprRenderNode::VisualPathRenderData &dst(m_node->m_vp[i]); + + int dirty = src.dirty; + src.dirty = 0; + if (listChanged) + dirty |= DirtyPath | DirtyStyle | DirtyFillRule | DirtyDash | DirtyFillGradient; + + // updateNode() can be called several times with different dirty + // states before render() gets invoked. So accumulate. + dst.dirty |= dirty; + + if (dirty & DirtyPath) + dst.source = src.path; + + if (dirty & DirtyStyle) { + dst.strokeWidth = src.strokeWidth; + dst.strokeColor = qsg_premultiply(src.strokeColor, 1.0f); + dst.fillColor = qsg_premultiply(src.fillColor, 1.0f); + switch (src.joinStyle) { + case QQuickVisualPath::MiterJoin: + dst.joinStyle = GL_MITER_TRUNCATE_NV; + break; + case QQuickVisualPath::BevelJoin: + dst.joinStyle = GL_BEVEL_NV; + break; + case QQuickVisualPath::RoundJoin: + dst.joinStyle = GL_ROUND_NV; + break; + default: + Q_UNREACHABLE(); + } + dst.miterLimit = src.miterLimit; + switch (src.capStyle) { + case QQuickVisualPath::FlatCap: + dst.capStyle = GL_FLAT; + break; + case QQuickVisualPath::SquareCap: + dst.capStyle = GL_SQUARE_NV; + break; + case QQuickVisualPath::RoundCap: + dst.capStyle = GL_ROUND_NV; + break; + default: + Q_UNREACHABLE(); + } } - } - if (m_dirty & DirtyFillRule) { - switch (m_fillRule) { - case QQuickPathItem::OddEvenFill: - m_node->m_fillRule = GL_INVERT; - break; - case QQuickPathItem::WindingFill: - m_node->m_fillRule = GL_COUNT_UP_NV; - break; - default: - Q_UNREACHABLE(); + if (dirty & DirtyFillRule) { + switch (src.fillRule) { + case QQuickVisualPath::OddEvenFill: + dst.fillRule = GL_INVERT; + break; + case QQuickVisualPath::WindingFill: + dst.fillRule = GL_COUNT_UP_NV; + break; + default: + Q_UNREACHABLE(); + } } - } - if (m_dirty & DirtyDash) { - m_node->m_dashOffset = m_dashOffset; - if (m_dashActive) { - m_node->m_dashPattern.resize(m_dashPattern.count()); - // Multiply by strokeWidth because the PathItem API follows QPen - // meaning the input dash pattern here is in width units. - for (int i = 0; i < m_dashPattern.count(); ++i) - m_node->m_dashPattern[i] = GLfloat(m_dashPattern[i]) * m_strokeWidth; - } else { - m_node->m_dashPattern.clear(); + if (dirty & DirtyDash) { + dst.dashOffset = src.dashOffset; + if (src.dashActive) { + dst.dashPattern.resize(src.dashPattern.count()); + // Multiply by strokeWidth because the PathItem API follows QPen + // meaning the input dash pattern here is in width units. + for (int i = 0; i < src.dashPattern.count(); ++i) + dst.dashPattern[i] = GLfloat(src.dashPattern[i]) * src.strokeWidth; + } else { + dst.dashPattern.clear(); + } } - } - if (m_dirty & DirtyFillGradient) { - m_node->m_fillGradientActive = m_fillGradientActive; - if (m_fillGradientActive) - m_node->m_fillGradient = m_fillGradient; + if (dirty & DirtyFillGradient) { + dst.fillGradientActive = src.fillGradientActive; + if (src.fillGradientActive) + dst.fillGradient = src.fillGradient; + } } m_node->markDirty(QSGNode::DirtyMaterial); - m_dirty = 0; + m_accDirty = 0; } bool QQuickPathItemNvprRenderNode::nvprInited = false; @@ -359,14 +409,15 @@ QQuickPathItemNvprRenderNode::~QQuickPathItemNvprRenderNode() void QQuickPathItemNvprRenderNode::releaseResources() { - if (m_path) { - nvpr.deletePaths(m_path, 1); - m_path = 0; - } - - if (m_fallbackFbo) { - delete m_fallbackFbo; - m_fallbackFbo = nullptr; + for (VisualPathRenderData &d : m_vp) { + if (d.path) { + nvpr.deletePaths(d.path, 1); + d.path = 0; + } + if (d.fallbackFbo) { + delete d.fallbackFbo; + d.fallbackFbo = nullptr; + } } m_fallbackBlitter.destroy(); @@ -449,48 +500,52 @@ QQuickNvprMaterialManager::MaterialDesc *QQuickNvprMaterialManager::activateMate return &mtl; } -void QQuickPathItemNvprRenderNode::updatePath() +void QQuickPathItemNvprRenderNode::updatePath(VisualPathRenderData *d) { - if (m_dirty & QQuickPathItemNvprRenderer::DirtyPath) { - if (!m_path) { - m_path = nvpr.genPaths(1); - Q_ASSERT(m_path != 0); + if (d->dirty & QQuickPathItemNvprRenderer::DirtyPath) { + if (!d->path) { + d->path = nvpr.genPaths(1); + Q_ASSERT(d->path != 0); + } + if (d->source.str.isEmpty()) { + nvpr.pathCommands(d->path, d->source.cmd.count(), d->source.cmd.constData(), + d->source.coord.count(), GL_FLOAT, d->source.coord.constData()); + } else { + nvpr.pathString(d->path, GL_PATH_FORMAT_SVG_NV, d->source.str.count(), d->source.str.constData()); } - nvpr.pathCommands(m_path, m_source.cmd.count(), m_source.cmd.constData(), - m_source.coord.count(), GL_FLOAT, m_source.coord.constData()); } - if (m_dirty & QQuickPathItemNvprRenderer::DirtyStyle) { - nvpr.pathParameterf(m_path, GL_PATH_STROKE_WIDTH_NV, m_strokeWidth); - nvpr.pathParameteri(m_path, GL_PATH_JOIN_STYLE_NV, m_joinStyle); - nvpr.pathParameteri(m_path, GL_PATH_MITER_LIMIT_NV, m_miterLimit); - nvpr.pathParameteri(m_path, GL_PATH_END_CAPS_NV, m_capStyle); - nvpr.pathParameteri(m_path, GL_PATH_DASH_CAPS_NV, m_capStyle); + if (d->dirty & QQuickPathItemNvprRenderer::DirtyStyle) { + nvpr.pathParameterf(d->path, GL_PATH_STROKE_WIDTH_NV, d->strokeWidth); + nvpr.pathParameteri(d->path, GL_PATH_JOIN_STYLE_NV, d->joinStyle); + nvpr.pathParameteri(d->path, GL_PATH_MITER_LIMIT_NV, d->miterLimit); + nvpr.pathParameteri(d->path, GL_PATH_END_CAPS_NV, d->capStyle); + nvpr.pathParameteri(d->path, GL_PATH_DASH_CAPS_NV, d->capStyle); } - if (m_dirty & QQuickPathItemNvprRenderer::DirtyDash) { - nvpr.pathParameterf(m_path, GL_PATH_DASH_OFFSET_NV, m_dashOffset); + if (d->dirty & QQuickPathItemNvprRenderer::DirtyDash) { + nvpr.pathParameterf(d->path, GL_PATH_DASH_OFFSET_NV, d->dashOffset); // count == 0 -> no dash - nvpr.pathDashArray(m_path, m_dashPattern.count(), m_dashPattern.constData()); + nvpr.pathDashArray(d->path, d->dashPattern.count(), d->dashPattern.constData()); } } -void QQuickPathItemNvprRenderNode::renderStroke(int strokeStencilValue, int writeMask) +void QQuickPathItemNvprRenderNode::renderStroke(VisualPathRenderData *d, int strokeStencilValue, int writeMask) { QQuickNvprMaterialManager::MaterialDesc *mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatSolid); f->glProgramUniform4f(mtl->prg, mtl->uniLoc[0], - m_strokeColor.x(), m_strokeColor.y(), m_strokeColor.z(), m_strokeColor.w()); + d->strokeColor.x(), d->strokeColor.y(), d->strokeColor.z(), d->strokeColor.w()); f->glProgramUniform1f(mtl->prg, mtl->uniLoc[1], inheritedOpacity()); - nvpr.stencilThenCoverStrokePath(m_path, strokeStencilValue, writeMask, GL_CONVEX_HULL_NV); + nvpr.stencilThenCoverStrokePath(d->path, strokeStencilValue, writeMask, GL_CONVEX_HULL_NV); } -void QQuickPathItemNvprRenderNode::renderFill() +void QQuickPathItemNvprRenderNode::renderFill(VisualPathRenderData *d) { QQuickNvprMaterialManager::MaterialDesc *mtl = nullptr; - if (m_fillGradientActive) { + if (d->fillGradientActive) { mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatLinearGradient); - QSGTexture *tx = QQuickPathItemGradientCache::currentCache()->get(m_fillGradient); + QSGTexture *tx = QQuickPathItemGradientCache::currentCache()->get(d->fillGradient); tx->bind(); // uv = vec2(coeff[0] * x + coeff[1] * y + coeff[2], coeff[3] * x + coeff[4] * y + coeff[5]) // where x and y are in path coordinate space, which is just what @@ -498,20 +553,20 @@ void QQuickPathItemNvprRenderNode::renderFill() GLfloat coeff[6] = { 1, 0, 0, 0, 1, 0 }; nvpr.programPathFragmentInputGen(mtl->prg, 0, GL_OBJECT_LINEAR_NV, 2, coeff); - f->glProgramUniform2f(mtl->prg, mtl->uniLoc[2], m_fillGradient.start.x(), m_fillGradient.start.y()); - f->glProgramUniform2f(mtl->prg, mtl->uniLoc[3], m_fillGradient.end.x(), m_fillGradient.end.y()); + f->glProgramUniform2f(mtl->prg, mtl->uniLoc[2], d->fillGradient.start.x(), d->fillGradient.start.y()); + f->glProgramUniform2f(mtl->prg, mtl->uniLoc[3], d->fillGradient.end.x(), d->fillGradient.end.y()); } else { mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatSolid); f->glProgramUniform4f(mtl->prg, mtl->uniLoc[0], - m_fillColor.x(), m_fillColor.y(), m_fillColor.z(), m_fillColor.w()); + d->fillColor.x(), d->fillColor.y(), d->fillColor.z(), d->fillColor.w()); } f->glProgramUniform1f(mtl->prg, mtl->uniLoc[1], inheritedOpacity()); const int writeMask = 0xFF; - nvpr.stencilThenCoverFillPath(m_path, m_fillRule, writeMask, GL_BOUNDING_BOX_NV); + nvpr.stencilThenCoverFillPath(d->path, d->fillRule, writeMask, GL_BOUNDING_BOX_NV); } -void QQuickPathItemNvprRenderNode::renderOffscreenFill() +void QQuickPathItemNvprRenderNode::renderOffscreenFill(VisualPathRenderData *d) { QQuickWindow *w = m_item->window(); const qreal dpr = w->effectiveDevicePixelRatio(); @@ -520,13 +575,13 @@ void QQuickPathItemNvprRenderNode::renderOffscreenFill() if (rtSize.isEmpty()) rtSize = w->size() * dpr; - if (m_fallbackFbo && m_fallbackFbo->size() != itemSize) { - delete m_fallbackFbo; - m_fallbackFbo = nullptr; + if (d->fallbackFbo && d->fallbackFbo->size() != itemSize) { + delete d->fallbackFbo; + d->fallbackFbo = nullptr; } - if (!m_fallbackFbo) - m_fallbackFbo = new QOpenGLFramebufferObject(itemSize, QOpenGLFramebufferObject::CombinedDepthStencil); - if (!m_fallbackFbo->bind()) + if (!d->fallbackFbo) + d->fallbackFbo = new QOpenGLFramebufferObject(itemSize, QOpenGLFramebufferObject::CombinedDepthStencil); + if (!d->fallbackFbo->bind()) return; f->glViewport(0, 0, itemSize.width(), itemSize.height()); @@ -541,9 +596,9 @@ void QQuickPathItemNvprRenderNode::renderOffscreenFill() proj.ortho(0, itemSize.width(), itemSize.height(), 0, 1, -1); nvpr.matrixLoadf(GL_PATH_PROJECTION_NV, proj.constData()); - renderFill(); + renderFill(d); - m_fallbackFbo->release(); + d->fallbackFbo->release(); f->glViewport(0, 0, rtSize.width(), rtSize.height()); } @@ -573,8 +628,6 @@ void QQuickPathItemNvprRenderNode::render(const RenderState *state) nvprInited = true; } - updatePath(); - f->glUseProgram(0); f->glStencilMask(~0); f->glEnable(GL_STENCIL_TEST); @@ -583,70 +636,74 @@ void QQuickPathItemNvprRenderNode::render(const RenderState *state) // when true, the stencil buffer already has a clip path with a ref value of sv const int sv = state->stencilValue(); - const bool hasFill = !qFuzzyIsNull(m_fillColor.w()) || m_fillGradientActive; - const bool hasStroke = m_strokeWidth >= 0.0f && !qFuzzyIsNull(m_strokeColor.w()); + for (VisualPathRenderData &d : m_vp) { + updatePath(&d); - if (hasFill && stencilClip) { - // Fall back to a texture when complex clipping is in use and we have - // to fill. Reconciling glStencilFillPath's and the scenegraph's clip - // stencil semantics has not succeeded so far... - renderOffscreenFill(); - } + const bool hasFill = !qFuzzyIsNull(d.fillColor.w()) || d.fillGradientActive; + const bool hasStroke = d.strokeWidth >= 0.0f && !qFuzzyIsNull(d.strokeColor.w()); + + if (hasFill && stencilClip) { + // Fall back to a texture when complex clipping is in use and we have + // to fill. Reconciling glStencilFillPath's and the scenegraph's clip + // stencil semantics has not succeeded so far... + renderOffscreenFill(&d); + } - // Depth test against the opaque batches rendered before. - f->glEnable(GL_DEPTH_TEST); - f->glDepthFunc(GL_LESS); - nvpr.pathCoverDepthFunc(GL_LESS); - nvpr.pathStencilDepthOffset(-0.05f, -1); + // Depth test against the opaque batches rendered before. + f->glEnable(GL_DEPTH_TEST); + f->glDepthFunc(GL_LESS); + nvpr.pathCoverDepthFunc(GL_LESS); + nvpr.pathStencilDepthOffset(-0.05f, -1); - nvpr.matrixLoadf(GL_PATH_MODELVIEW_NV, matrix()->constData()); - nvpr.matrixLoadf(GL_PATH_PROJECTION_NV, state->projectionMatrix()->constData()); + nvpr.matrixLoadf(GL_PATH_MODELVIEW_NV, matrix()->constData()); + nvpr.matrixLoadf(GL_PATH_PROJECTION_NV, state->projectionMatrix()->constData()); - if (state->scissorEnabled()) { - // scissor rect is already set, just enable scissoring - f->glEnable(GL_SCISSOR_TEST); - } + if (state->scissorEnabled()) { + // scissor rect is already set, just enable scissoring + f->glEnable(GL_SCISSOR_TEST); + } - // Fill! - if (hasFill) { - if (!stencilClip) { - setupStencilForCover(false, 0); - renderFill(); - } else { - if (!m_fallbackBlitter.isCreated()) - m_fallbackBlitter.create(); - f->glStencilFunc(GL_EQUAL, sv, 0xFF); - f->glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); - m_fallbackBlitter.texturedQuad(m_fallbackFbo->texture(), m_fallbackFbo->size(), - *state->projectionMatrix(), *matrix(), - inheritedOpacity()); + // Fill! + if (hasFill) { + if (!stencilClip) { + setupStencilForCover(false, 0); + renderFill(&d); + } else { + if (!m_fallbackBlitter.isCreated()) + m_fallbackBlitter.create(); + f->glStencilFunc(GL_EQUAL, sv, 0xFF); + f->glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); + m_fallbackBlitter.texturedQuad(d.fallbackFbo->texture(), d.fallbackFbo->size(), + *state->projectionMatrix(), *matrix(), + inheritedOpacity()); + } } - } - // Stroke! - if (hasStroke) { - const int strokeStencilValue = 0x80; - const int writeMask = 0x80; - - setupStencilForCover(stencilClip, sv); - if (stencilClip) { - // for the stencil step (eff. read mask == 0xFF & ~writeMask) - nvpr.pathStencilFunc(GL_EQUAL, sv, 0xFF); - // With stencilCLip == true the read mask for the stencil test before the stencil step is 0x7F. - // This assumes the clip stencil value is <= 127. - if (sv >= strokeStencilValue) - qWarning("PathItem/NVPR: stencil clip ref value %d too large; expect rendering errors", sv); + // Stroke! + if (hasStroke) { + const int strokeStencilValue = 0x80; + const int writeMask = 0x80; + + setupStencilForCover(stencilClip, sv); + if (stencilClip) { + // for the stencil step (eff. read mask == 0xFF & ~writeMask) + nvpr.pathStencilFunc(GL_EQUAL, sv, 0xFF); + // With stencilCLip == true the read mask for the stencil test before the stencil step is 0x7F. + // This assumes the clip stencil value is <= 127. + if (sv >= strokeStencilValue) + qWarning("PathItem/NVPR: stencil clip ref value %d too large; expect rendering errors", sv); + } + + renderStroke(&d, strokeStencilValue, writeMask); } - renderStroke(strokeStencilValue, writeMask); - } + if (stencilClip) + nvpr.pathStencilFunc(GL_ALWAYS, 0, ~0); - if (stencilClip) - nvpr.pathStencilFunc(GL_ALWAYS, 0, ~0); + d.dirty = 0; + } f->glBindProgramPipeline(0); - - m_dirty = 0; } QSGRenderNode::StateFlags QQuickPathItemNvprRenderNode::changedStates() const diff --git a/src/quick/items/qquickpathitemnvprrenderer_p.h b/src/quick/items/qquickpathitemnvprrenderer_p.h index 7dd2d564c5..f594609e83 100644 --- a/src/quick/items/qquickpathitemnvprrenderer_p.h +++ b/src/quick/items/qquickpathitemnvprrenderer_p.h @@ -76,55 +76,61 @@ public: DirtyFillRule = 0x04, DirtyDash = 0x08, DirtyFillGradient = 0x10, - - DirtyAll = 0xFF + DirtyList = 0x20 }; QQuickPathItemNvprRenderer(QQuickItem *item) : m_item(item) { } - void beginSync() override; - void setPath(const QQuickPath *path) override; - void setStrokeColor(const QColor &color) override; - void setStrokeWidth(qreal w) override; - void setFillColor(const QColor &color) override; - void setFillRule(QQuickPathItem::FillRule fillRule) override; - void setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) override; - void setCapStyle(QQuickPathItem::CapStyle capStyle) override; - void setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, + void beginSync(int totalCount) override; + void setPath(int index, const QQuickPath *path) override; + 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, QQuickVisualPath::FillRule fillRule) override; + void setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) override; + void setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) override; + void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, qreal dashOffset, const QVector &dashPattern) override; - void setFillGradient(QQuickPathGradient *gradient) override; + void setFillGradient(int index, QQuickPathGradient *gradient) override; void endSync() override; - void updatePathRenderNode() override; + + void updateNode() override; void setNode(QQuickPathItemNvprRenderNode *node); struct NvprPath { QVector cmd; QVector coord; + QByteArray str; }; private: - void convertPath(const QQuickPath *path); + struct VisualPathGuiData { + int dirty = 0; + NvprPath path; + qreal strokeWidth; + QColor strokeColor; + QColor fillColor; + QQuickVisualPath::JoinStyle joinStyle; + int miterLimit; + QQuickVisualPath::CapStyle capStyle; + QQuickVisualPath::FillRule fillRule; + bool dashActive; + qreal dashOffset; + QVector dashPattern; + bool fillGradientActive; + QQuickPathItemGradientCache::GradientDesc fillGradient; + }; + + void convertPath(const QQuickPath *path, VisualPathGuiData *d); QQuickItem *m_item; QQuickPathItemNvprRenderNode *m_node = nullptr; - int m_dirty = 0; - - NvprPath m_path; - qreal m_strokeWidth; - QColor m_strokeColor; - QColor m_fillColor; - QQuickPathItem::JoinStyle m_joinStyle; - int m_miterLimit; - QQuickPathItem::CapStyle m_capStyle; - QQuickPathItem::FillRule m_fillRule; - bool m_dashActive; - qreal m_dashOffset; - QVector m_dashPattern; - bool m_fillGradientActive; - QQuickPathItemGradientCache::GradientDesc m_fillGradient; + int m_accDirty = 0; + + QVector m_vp; }; QDebug operator<<(QDebug debug, const QQuickPathItemNvprRenderer::NvprPath &path); @@ -187,10 +193,28 @@ public: static bool isSupported(); private: - void updatePath(); - void renderStroke(int strokeStencilValue, int writeMask); - void renderFill(); - void renderOffscreenFill(); + struct VisualPathRenderData { + GLuint path = 0; + int dirty = 0; + QQuickPathItemNvprRenderer::NvprPath source; + GLfloat strokeWidth; + QVector4D strokeColor; + QVector4D fillColor; + GLenum joinStyle; + GLint miterLimit; + GLenum capStyle; + GLenum fillRule; + GLfloat dashOffset; + QVector dashPattern; + bool fillGradientActive; + QQuickPathItemGradientCache::GradientDesc fillGradient; + QOpenGLFramebufferObject *fallbackFbo = nullptr; + }; + + void updatePath(VisualPathRenderData *d); + void renderStroke(VisualPathRenderData *d, int strokeStencilValue, int writeMask); + void renderFill(VisualPathRenderData *d); + void renderOffscreenFill(VisualPathRenderData *d); void setupStencilForCover(bool stencilClip, int sv); static bool nvprInited; @@ -198,25 +222,11 @@ private: static QQuickNvprMaterialManager mtlmgr; QQuickPathItem *m_item; - GLuint m_path = 0; - int m_dirty = 0; - - QQuickPathItemNvprRenderer::NvprPath m_source; - GLfloat m_strokeWidth; - QVector4D m_strokeColor; - QVector4D m_fillColor; - GLenum m_joinStyle; - GLint m_miterLimit; - GLenum m_capStyle; - GLenum m_fillRule; - GLfloat m_dashOffset; - QVector m_dashPattern; - bool m_fillGradientActive; - QQuickPathItemGradientCache::GradientDesc m_fillGradient; - QOpenGLFramebufferObject *m_fallbackFbo = nullptr; QQuickNvprBlitter m_fallbackBlitter; QOpenGLExtraFunctions *f = nullptr; + QVector m_vp; + friend class QQuickPathItemNvprRenderer; }; diff --git a/src/quick/items/qquickpathitemsoftwarerenderer.cpp b/src/quick/items/qquickpathitemsoftwarerenderer.cpp index c7b52e641f..6c3cf73a56 100644 --- a/src/quick/items/qquickpathitemsoftwarerenderer.cpp +++ b/src/quick/items/qquickpathitemsoftwarerenderer.cpp @@ -42,77 +42,97 @@ QT_BEGIN_NAMESPACE -void QQuickPathItemSoftwareRenderer::beginSync() +void QQuickPathItemSoftwareRenderer::beginSync(int totalCount) { - // nothing to do here + if (m_vp.count() != totalCount) { + m_vp.resize(totalCount); + m_accDirty |= DirtyList; + } } -void QQuickPathItemSoftwareRenderer::setPath(const QQuickPath *path) +void QQuickPathItemSoftwareRenderer::setPath(int index, const QQuickPath *path) { - m_path = path ? path->path() : QPainterPath(); - m_dirty |= DirtyPath; + VisualPathGuiData &d(m_vp[index]); + d.path = path ? path->path() : QPainterPath(); + d.dirty |= DirtyPath; + m_accDirty |= DirtyPath; } -void QQuickPathItemSoftwareRenderer::setStrokeColor(const QColor &color) +void QQuickPathItemSoftwareRenderer::setStrokeColor(int index, const QColor &color) { - m_pen.setColor(color); - m_dirty |= DirtyPen; + VisualPathGuiData &d(m_vp[index]); + d.pen.setColor(color); + d.dirty |= DirtyPen; + m_accDirty |= DirtyPen; } -void QQuickPathItemSoftwareRenderer::setStrokeWidth(qreal w) +void QQuickPathItemSoftwareRenderer::setStrokeWidth(int index, qreal w) { - m_strokeWidth = w; + VisualPathGuiData &d(m_vp[index]); + d.strokeWidth = w; if (w >= 0.0f) - m_pen.setWidthF(w); - m_dirty |= DirtyPen; + d.pen.setWidthF(w); + d.dirty |= DirtyPen; + m_accDirty |= DirtyPen; } -void QQuickPathItemSoftwareRenderer::setFillColor(const QColor &color) +void QQuickPathItemSoftwareRenderer::setFillColor(int index, const QColor &color) { - m_fillColor = color; - m_brush.setColor(m_fillColor); - m_dirty |= DirtyBrush; + VisualPathGuiData &d(m_vp[index]); + d.fillColor = color; + d.brush.setColor(color); + d.dirty |= DirtyBrush; + m_accDirty |= DirtyBrush; } -void QQuickPathItemSoftwareRenderer::setFillRule(QQuickPathItem::FillRule fillRule) +void QQuickPathItemSoftwareRenderer::setFillRule(int index, QQuickVisualPath::FillRule fillRule) { - m_fillRule = Qt::FillRule(fillRule); - m_dirty |= DirtyFillRule; + VisualPathGuiData &d(m_vp[index]); + d.fillRule = Qt::FillRule(fillRule); + d.dirty |= DirtyFillRule; + m_accDirty |= DirtyFillRule; } -void QQuickPathItemSoftwareRenderer::setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) +void QQuickPathItemSoftwareRenderer::setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) { - m_pen.setJoinStyle(Qt::PenJoinStyle(joinStyle)); - m_pen.setMiterLimit(miterLimit); - m_dirty |= DirtyPen; + VisualPathGuiData &d(m_vp[index]); + d.pen.setJoinStyle(Qt::PenJoinStyle(joinStyle)); + d.pen.setMiterLimit(miterLimit); + d.dirty |= DirtyPen; + m_accDirty |= DirtyPen; } -void QQuickPathItemSoftwareRenderer::setCapStyle(QQuickPathItem::CapStyle capStyle) +void QQuickPathItemSoftwareRenderer::setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) { - m_pen.setCapStyle(Qt::PenCapStyle(capStyle)); - m_dirty |= DirtyPen; + VisualPathGuiData &d(m_vp[index]); + d.pen.setCapStyle(Qt::PenCapStyle(capStyle)); + d.dirty |= DirtyPen; + m_accDirty |= DirtyPen; } -void QQuickPathItemSoftwareRenderer::setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, - qreal dashOffset, const QVector &dashPattern) +void QQuickPathItemSoftwareRenderer::setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, + qreal dashOffset, const QVector &dashPattern) { + VisualPathGuiData &d(m_vp[index]); switch (strokeStyle) { - case QQuickPathItem::SolidLine: - m_pen.setStyle(Qt::SolidLine); + case QQuickVisualPath::SolidLine: + d.pen.setStyle(Qt::SolidLine); break; - case QQuickPathItem::DashLine: - m_pen.setStyle(Qt::CustomDashLine); - m_pen.setDashPattern(dashPattern); - m_pen.setDashOffset(dashOffset); + case QQuickVisualPath::DashLine: + d.pen.setStyle(Qt::CustomDashLine); + d.pen.setDashPattern(dashPattern); + d.pen.setDashOffset(dashOffset); break; default: break; } - m_dirty |= DirtyPen; + d.dirty |= DirtyPen; + m_accDirty |= DirtyPen; } -void QQuickPathItemSoftwareRenderer::setFillGradient(QQuickPathGradient *gradient) +void QQuickPathItemSoftwareRenderer::setFillGradient(int index, QQuickPathGradient *gradient) { + VisualPathGuiData &d(m_vp[index]); if (QQuickPathLinearGradient *linearGradient = qobject_cast(gradient)) { QLinearGradient painterGradient(linearGradient->x1(), linearGradient->y1(), linearGradient->x2(), linearGradient->y2()); @@ -130,57 +150,61 @@ void QQuickPathItemSoftwareRenderer::setFillGradient(QQuickPathGradient *gradien default: break; } - m_brush = QBrush(painterGradient); + d.brush = QBrush(painterGradient); } else { - m_brush = QBrush(m_fillColor); + d.brush = QBrush(d.fillColor); } - m_dirty |= DirtyBrush; + d.dirty |= DirtyBrush; + m_accDirty |= DirtyBrush; } void QQuickPathItemSoftwareRenderer::endSync() { - // nothing to do here } void QQuickPathItemSoftwareRenderer::setNode(QQuickPathItemSoftwareRenderNode *node) { if (m_node != node) { m_node = node; - // Scenegraph nodes can be destroyed and then replaced by new ones over - // time; hence it is important to mark everything dirty for - // updatePathRenderNode(). We can assume the renderer has a full sync - // of the data at this point. - m_dirty = DirtyAll; + m_accDirty |= DirtyList; } } -void QQuickPathItemSoftwareRenderer::updatePathRenderNode() +void QQuickPathItemSoftwareRenderer::updateNode() { - if (!m_dirty) + if (!m_accDirty) return; - // updatePathRenderNode() can be called several times with different dirty - // state before render() gets invoked. So accumulate. - m_node->m_dirty |= m_dirty; + const int count = m_vp.count(); + const bool listChanged = m_accDirty & DirtyList; + if (listChanged) + m_node->m_vp.resize(count); - if (m_dirty & DirtyPath) { - m_node->m_path = m_path; - m_node->m_path.setFillRule(m_fillRule); - } + for (int i = 0; i < count; ++i) { + VisualPathGuiData &src(m_vp[i]); + QQuickPathItemSoftwareRenderNode::VisualPathRenderData &dst(m_node->m_vp[i]); - if (m_dirty & DirtyFillRule) - m_node->m_path.setFillRule(m_fillRule); + if (listChanged || (src.dirty & DirtyPath)) { + dst.path = src.path; + dst.path.setFillRule(src.fillRule); + } - if (m_dirty & DirtyPen) { - m_node->m_pen = m_pen; - m_node->m_strokeWidth = m_strokeWidth; - } + if (listChanged || (src.dirty & DirtyFillRule)) + dst.path.setFillRule(src.fillRule); + + if (listChanged || (src.dirty & DirtyPen)) { + dst.pen = src.pen; + dst.strokeWidth = src.strokeWidth; + } + + if (listChanged || (src.dirty & DirtyBrush)) + dst.brush = src.brush; - if (m_dirty & DirtyBrush) - m_node->m_brush = m_brush; + src.dirty = 0; + } m_node->markDirty(QSGNode::DirtyMaterial); - m_dirty = 0; + m_accDirty = 0; } QQuickPathItemSoftwareRenderNode::QQuickPathItemSoftwareRenderNode(QQuickPathItem *item) @@ -199,7 +223,7 @@ void QQuickPathItemSoftwareRenderNode::releaseResources() void QQuickPathItemSoftwareRenderNode::render(const RenderState *state) { - if (m_path.isEmpty()) + if (m_vp.isEmpty()) return; QSGRendererInterface *rif = m_item->window()->rendererInterface(); @@ -213,11 +237,11 @@ void QQuickPathItemSoftwareRenderNode::render(const RenderState *state) p->setTransform(matrix()->toTransform()); p->setOpacity(inheritedOpacity()); - p->setPen(m_strokeWidth >= 0.0f && m_pen.color() != Qt::transparent ? m_pen : Qt::NoPen); - p->setBrush(m_brush.color() != Qt::transparent ? m_brush : Qt::NoBrush); - p->drawPath(m_path); - - m_dirty = 0; + for (const VisualPathRenderData &d : qAsConst(m_vp)) { + p->setPen(d.strokeWidth >= 0.0f && d.pen.color() != Qt::transparent ? d.pen : Qt::NoPen); + p->setBrush(d.brush.color() != Qt::transparent ? d.brush : Qt::NoBrush); + p->drawPath(d.path); + } } QSGRenderNode::StateFlags QQuickPathItemSoftwareRenderNode::changedStates() const diff --git a/src/quick/items/qquickpathitemsoftwarerenderer_p.h b/src/quick/items/qquickpathitemsoftwarerenderer_p.h index 6c7d052596..60b52e2def 100644 --- a/src/quick/items/qquickpathitemsoftwarerenderer_p.h +++ b/src/quick/items/qquickpathitemsoftwarerenderer_p.h @@ -68,36 +68,39 @@ public: DirtyPen = 0x02, DirtyFillRule = 0x04, DirtyBrush = 0x08, - - DirtyAll = 0xFF + DirtyList = 0x10 }; - void beginSync() override; - void setPath(const QQuickPath *path) override; - void setStrokeColor(const QColor &color) override; - void setStrokeWidth(qreal w) override; - void setFillColor(const QColor &color) override; - void setFillRule(QQuickPathItem::FillRule fillRule) override; - void setJoinStyle(QQuickPathItem::JoinStyle joinStyle, int miterLimit) override; - void setCapStyle(QQuickPathItem::CapStyle capStyle) override; - void setStrokeStyle(QQuickPathItem::StrokeStyle strokeStyle, + void beginSync(int totalCount) override; + void setPath(int index, const QQuickPath *path) override; + 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, QQuickVisualPath::FillRule fillRule) override; + void setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) override; + void setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) override; + void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, qreal dashOffset, const QVector &dashPattern) override; - void setFillGradient(QQuickPathGradient *gradient) override; + void setFillGradient(int index, QQuickPathGradient *gradient) override; void endSync() override; - void updatePathRenderNode() override; + + void updateNode() override; void setNode(QQuickPathItemSoftwareRenderNode *node); private: QQuickPathItemSoftwareRenderNode *m_node = nullptr; - int m_dirty = 0; - - QPainterPath m_path; - QPen m_pen; - float m_strokeWidth; - QColor m_fillColor; - QBrush m_brush; - Qt::FillRule m_fillRule; + int m_accDirty = 0; + struct VisualPathGuiData { + int dirty = 0; + QPainterPath path; + QPen pen; + float strokeWidth; + QColor fillColor; + QBrush brush; + Qt::FillRule fillRule; + }; + QVector m_vp; }; class QQuickPathItemSoftwareRenderNode : public QSGRenderNode @@ -114,12 +117,14 @@ public: private: QQuickPathItem *m_item; - int m_dirty = 0; - QPainterPath m_path; - QPen m_pen; - float m_strokeWidth; - QBrush m_brush; + struct VisualPathRenderData { + QPainterPath path; + QPen pen; + float strokeWidth; + QBrush brush; + }; + QVector m_vp; friend class QQuickPathItemSoftwareRenderer; }; -- cgit v1.2.3 From d57ede19dff1c45840f0d60881cada194330a6f3 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 10 Jan 2017 14:08:31 +0100 Subject: PathItem/NVPR: Remove unused member variable Change-Id: I8228b39d01097179e8b969b2ed479c18b80e9759 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitem.cpp | 2 +- src/quick/items/qquickpathitemnvprrenderer_p.h | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index 0dce376945..d7131b3833 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -520,7 +520,7 @@ void QQuickPathItemPrivate::createRenderer() case QSGRendererInterface::OpenGL: if (QQuickPathItemNvprRenderNode::isSupported()) { rendererType = QQuickPathItem::NvprRenderer; - renderer = new QQuickPathItemNvprRenderer(q); + renderer = new QQuickPathItemNvprRenderer; } else { rendererType = QQuickPathItem::GeometryRenderer; renderer = new QQuickPathItemGenericRenderer(q); diff --git a/src/quick/items/qquickpathitemnvprrenderer_p.h b/src/quick/items/qquickpathitemnvprrenderer_p.h index f594609e83..067ecf7355 100644 --- a/src/quick/items/qquickpathitemnvprrenderer_p.h +++ b/src/quick/items/qquickpathitemnvprrenderer_p.h @@ -79,10 +79,6 @@ public: DirtyList = 0x20 }; - QQuickPathItemNvprRenderer(QQuickItem *item) - : m_item(item) - { } - void beginSync(int totalCount) override; void setPath(int index, const QQuickPath *path) override; void setStrokeColor(int index, const QColor &color) override; @@ -126,7 +122,6 @@ private: void convertPath(const QQuickPath *path, VisualPathGuiData *d); - QQuickItem *m_item; QQuickPathItemNvprRenderNode *m_node = nullptr; int m_accDirty = 0; -- cgit v1.2.3 From e0200d5a72d29a3918bf66290e498f99c0a62a0a Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Sun, 8 Jan 2017 14:47:27 +0100 Subject: Clean up pathitem example grid view Combine stroke-only and fill-only. Drop the Repeater since it shows the bad practice of unnecessarily creating multiple PathItem instances. Drop the PathSvg example for now since this is a discouraged element. Move the tiger into a separate file and use an async Loader in order to avoid the 2 step activation. Change-Id: Ie65eb0284bdb8957f8fd5af7557542a6d044ef61 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemgenericrenderer.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index b42ff1d4b0..dbbc14d3c8 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -73,6 +73,9 @@ QQuickPathItemGenericStrokeFillNode::QQuickPathItemGenericStrokeFillNode(QQuickW { setGeometry(&m_geometry); activateMaterial(MatSolidColor); +#ifdef QSG_RUNTIME_DESCRIPTION + qsgnode_set_description(this, QLatin1String("stroke-fill")); +#endif } void QQuickPathItemGenericStrokeFillNode::activateMaterial(Material m) -- cgit v1.2.3 From f02e234fc012f4430378bc5205f32914822e4dff Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Sun, 8 Jan 2017 16:08:34 +0100 Subject: Reduce state changes with NVPR-backed PathItems Not setting the matrices and other states again and again for every path in the item saves a little CPU time. Change-Id: Iec9447de4c22cd25343462160bd4a8b9aa44368a Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemnvprrenderer.cpp | 37 +++++++++++++++++--------- 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemnvprrenderer.cpp b/src/quick/items/qquickpathitemnvprrenderer.cpp index 2338e51ff8..0c4357145a 100644 --- a/src/quick/items/qquickpathitemnvprrenderer.cpp +++ b/src/quick/items/qquickpathitemnvprrenderer.cpp @@ -585,6 +585,7 @@ void QQuickPathItemNvprRenderNode::renderOffscreenFill(VisualPathRenderData *d) return; f->glViewport(0, 0, itemSize.width(), itemSize.height()); + f->glDisable(GL_DEPTH_TEST); f->glClearColor(0, 0, 0, 0); f->glClearStencil(0); f->glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); @@ -599,6 +600,7 @@ void QQuickPathItemNvprRenderNode::renderOffscreenFill(VisualPathRenderData *d) renderFill(d); d->fallbackFbo->release(); + f->glEnable(GL_DEPTH_TEST); f->glViewport(0, 0, rtSize.width(), rtSize.height()); } @@ -635,6 +637,20 @@ void QQuickPathItemNvprRenderNode::render(const RenderState *state) const bool stencilClip = state->stencilEnabled(); // when true, the stencil buffer already has a clip path with a ref value of sv const int sv = state->stencilValue(); + const bool hasScissor = state->scissorEnabled(); + + if (hasScissor) { + // scissor rect is already set, just enable scissoring + f->glEnable(GL_SCISSOR_TEST); + } + + // Depth test against the opaque batches rendered before. + f->glEnable(GL_DEPTH_TEST); + f->glDepthFunc(GL_LESS); + nvpr.pathCoverDepthFunc(GL_LESS); + nvpr.pathStencilDepthOffset(-0.05f, -1); + + bool reloadMatrices = true; for (VisualPathRenderData &d : m_vp) { updatePath(&d); @@ -646,21 +662,18 @@ void QQuickPathItemNvprRenderNode::render(const RenderState *state) // Fall back to a texture when complex clipping is in use and we have // to fill. Reconciling glStencilFillPath's and the scenegraph's clip // stencil semantics has not succeeded so far... + if (hasScissor) + f->glDisable(GL_SCISSOR_TEST); renderOffscreenFill(&d); + reloadMatrices = true; + if (hasScissor) + f->glEnable(GL_SCISSOR_TEST); } - // Depth test against the opaque batches rendered before. - f->glEnable(GL_DEPTH_TEST); - f->glDepthFunc(GL_LESS); - nvpr.pathCoverDepthFunc(GL_LESS); - nvpr.pathStencilDepthOffset(-0.05f, -1); - - nvpr.matrixLoadf(GL_PATH_MODELVIEW_NV, matrix()->constData()); - nvpr.matrixLoadf(GL_PATH_PROJECTION_NV, state->projectionMatrix()->constData()); - - if (state->scissorEnabled()) { - // scissor rect is already set, just enable scissoring - f->glEnable(GL_SCISSOR_TEST); + if (reloadMatrices) { + reloadMatrices = false; + nvpr.matrixLoadf(GL_PATH_MODELVIEW_NV, matrix()->constData()); + nvpr.matrixLoadf(GL_PATH_PROJECTION_NV, state->projectionMatrix()->constData()); } // Fill! -- cgit v1.2.3 From 5575226f6b25bcf507c3412677756e07ce671cef Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Mon, 9 Jan 2017 11:43:31 +0100 Subject: PathItem: add optional threaded triangulation Spending 300+ ms in polish for the tiger when running with the generic backend is not going to fly. Therefore, add an optional mode for asynchronous triangulation. Change-Id: Ida44c7b8a28d38fb11243a6657221f039c62e21b Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitem.cpp | 20 ++- src/quick/items/qquickpathitem_p.h | 5 + src/quick/items/qquickpathitem_p_p.h | 3 +- src/quick/items/qquickpathitemgenericrenderer.cpp | 197 ++++++++++++++++----- src/quick/items/qquickpathitemgenericrenderer_p.h | 70 ++++++-- src/quick/items/qquickpathitemnvprrenderer.cpp | 2 +- src/quick/items/qquickpathitemnvprrenderer_p.h | 2 +- src/quick/items/qquickpathitemsoftwarerenderer.cpp | 2 +- src/quick/items/qquickpathitemsoftwarerenderer_p.h | 2 +- 9 files changed, 243 insertions(+), 60 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index d7131b3833..964d997806 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -361,6 +361,7 @@ QQuickPathItemPrivate::QQuickPathItemPrivate() : componentComplete(true), vpChanged(false), rendererType(QQuickPathItem::UnknownRenderer), + async(false), renderer(nullptr) { } @@ -393,6 +394,23 @@ QQuickPathItem::RendererType QQuickPathItem::rendererType() const return d->rendererType; } +bool QQuickPathItem::asynchronous() const +{ + Q_D(const QQuickPathItem); + return d->async; +} + +void QQuickPathItem::setAsynchronous(bool async) +{ + Q_D(QQuickPathItem); + if (d->async != async) { + d->async = async; + emit asynchronousChanged(); + if (d->componentComplete) + d->_q_visualPathChanged(); + } +} + static QQuickVisualPath *vpe_at(QQmlListProperty *property, int index) { QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(static_cast(property->object)); @@ -606,7 +624,7 @@ void QQuickPathItemPrivate::sync() dirty = 0; } - renderer->endSync(); + renderer->endSync(async); } // ***** gradient support ***** diff --git a/src/quick/items/qquickpathitem_p.h b/src/quick/items/qquickpathitem_p.h index 0c1d0f061b..7c962d01fc 100644 --- a/src/quick/items/qquickpathitem_p.h +++ b/src/quick/items/qquickpathitem_p.h @@ -263,6 +263,7 @@ class Q_QUICK_PRIVATE_EXPORT QQuickPathItem : public QQuickItem { Q_OBJECT Q_PROPERTY(RendererType renderer READ rendererType NOTIFY rendererChanged) + Q_PROPERTY(bool asynchronous READ asynchronous WRITE setAsynchronous NOTIFY asynchronousChanged) Q_PROPERTY(QQmlListProperty visualPaths READ visualPaths) Q_CLASSINFO("DefaultProperty", "visualPaths") @@ -280,6 +281,9 @@ public: RendererType rendererType() const; + bool asynchronous() const; + void setAsynchronous(bool async); + QQmlListProperty visualPaths(); protected: @@ -291,6 +295,7 @@ protected: Q_SIGNALS: void rendererChanged(); + void asynchronousChanged(); private: Q_DISABLE_COPY(QQuickPathItem) diff --git a/src/quick/items/qquickpathitem_p_p.h b/src/quick/items/qquickpathitem_p_p.h index 5e6400edc6..faf7b1e451 100644 --- a/src/quick/items/qquickpathitem_p_p.h +++ b/src/quick/items/qquickpathitem_p_p.h @@ -79,7 +79,7 @@ public: virtual void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, qreal dashOffset, const QVector &dashPattern) = 0; virtual void setFillGradient(int index, QQuickPathGradient *gradient) = 0; - virtual void endSync() = 0; + virtual void endSync(bool async) = 0; // Render thread, with gui blocked virtual void updateNode() = 0; @@ -144,6 +144,7 @@ public: bool componentComplete; bool vpChanged; QQuickPathItem::RendererType rendererType; + bool async; QQuickAbstractPathRenderer *renderer; QVector vp; }; diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index dbbc14d3c8..6e4c89742a 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -39,7 +39,9 @@ #include "qquickpathitemgenericrenderer_p.h" #include +#include #include +#include QT_BEGIN_NAMESPACE @@ -103,6 +105,8 @@ void QQuickPathItemGenericStrokeFillNode::activateMaterial(Material m) } // sync, and so triangulation too, happens on the gui thread +// - except when async is set, in which case triangulation is moved to worker threads + void QQuickPathItemGenericRenderer::beginSync(int totalCount) { if (m_vp.count() != totalCount) { @@ -194,9 +198,22 @@ void QQuickPathItemGenericRenderer::setFillGradient(int index, QQuickPathGradien d.syncDirty |= DirtyFillGradient; } -void QQuickPathItemGenericRenderer::endSync() +void QQuickPathItemFillRunnable::run() { - for (VisualPathData &d : m_vp) { + QQuickPathItemGenericRenderer::triangulateFill(path, fillColor, &fillVertices, &fillIndices); + emit done(this); +} + +void QQuickPathItemStrokeRunnable::run() +{ + QQuickPathItemGenericRenderer::triangulateStroke(path, pen, strokeColor, &strokeVertices, clipSize); + emit done(this); +} + +void QQuickPathItemGenericRenderer::endSync(bool async) +{ + for (int i = 0; i < m_vp.count(); ++i) { + VisualPathData &d(m_vp[i]); if (!d.syncDirty) continue; @@ -217,67 +234,145 @@ void QQuickPathItemGenericRenderer::endSync() } if (d.syncDirty & DirtyGeom) { - if (d.fillColor.a) - triangulateFill(&d); - if (d.strokeWidth >= 0.0f && d.strokeColor.a) - triangulateStroke(&d); + static QThreadPool threadPool; + static bool threadPoolReady = false; + if (async && !threadPoolReady) { + threadPoolReady = true; + const int idealCount = QThread::idealThreadCount(); + threadPool.setMaxThreadCount(idealCount > 0 ? idealCount * 2 : 4); + } + if (d.fillColor.a) { + d.path.setFillRule(d.fillRule); + if (async) { + QQuickPathItemFillRunnable *r = new QQuickPathItemFillRunnable; + r->setAutoDelete(false); + if (d.pendingFill) + d.pendingFill->orphaned = true; + d.pendingFill = r; + r->path = d.path; + r->fillColor = d.fillColor; + // Unlikely in practice but in theory m_vp could be + // resized. Therefore, capture 'i' instead of 'd'. + QObject::connect(r, &QQuickPathItemFillRunnable::done, qApp, [this, i](QQuickPathItemFillRunnable *r) { + if (!r->orphaned && i < m_vp.count()) { + VisualPathData &d(m_vp[i]); + d.fillVertices = r->fillVertices; + d.fillIndices = r->fillIndices; + d.pendingFill = nullptr; + d.effectiveDirty |= DirtyGeom; + maybeUpdateAsyncItem(); + } + r->deleteLater(); + }); + threadPool.start(r); + } else { + triangulateFill(d.path, d.fillColor, &d.fillVertices, &d.fillIndices); + } + } + if (d.strokeWidth >= 0.0f && d.strokeColor.a) { + if (async) { + QQuickPathItemStrokeRunnable *r = new QQuickPathItemStrokeRunnable; + r->setAutoDelete(false); + if (d.pendingStroke) + d.pendingStroke->orphaned = true; + d.pendingStroke = r; + r->path = d.path; + r->pen = d.pen; + r->strokeColor = d.strokeColor; + r->clipSize = QSize(m_item->width(), m_item->height()); + QObject::connect(r, &QQuickPathItemStrokeRunnable::done, qApp, [this, i](QQuickPathItemStrokeRunnable *r) { + if (!r->orphaned && i < m_vp.count()) { + VisualPathData &d(m_vp[i]); + d.strokeVertices = r->strokeVertices; + d.pendingStroke = nullptr; + d.effectiveDirty |= DirtyGeom; + maybeUpdateAsyncItem(); + } + r->deleteLater(); + }); + threadPool.start(r); + } else { + triangulateStroke(d.path, d.pen, d.strokeColor, &d.strokeVertices, + QSize(m_item->width(), m_item->height())); + } + } } } } -void QQuickPathItemGenericRenderer::triangulateFill(VisualPathData *d) +void QQuickPathItemGenericRenderer::maybeUpdateAsyncItem() { - d->path.setFillRule(d->fillRule); + for (const VisualPathData &d : qAsConst(m_vp)) { + if (d.pendingFill || d.pendingStroke) + return; + } + m_accDirty |= DirtyGeom; + m_item->update(); +} - const QVectorPath &vp = qtVectorPathForPath(d->path); +// the stroke/fill triangulation functions may be invoked either on the gui +// thread or some worker thread and must thus be self-contained. +void QQuickPathItemGenericRenderer::triangulateFill(const QPainterPath &path, + const Color4ub &fillColor, + VerticesType *fillVertices, + IndicesType *fillIndices) +{ + const QVectorPath &vp = qtVectorPathForPath(path); QTriangleSet ts = qTriangulate(vp, QTransform::fromScale(SCALE, SCALE)); const int vertexCount = ts.vertices.count() / 2; // just a qreal vector with x,y hence the / 2 - d->fillVertices.resize(vertexCount); - ColoredVertex *vdst = reinterpret_cast(d->fillVertices.data()); + fillVertices->resize(vertexCount); + ColoredVertex *vdst = reinterpret_cast(fillVertices->data()); const qreal *vsrc = ts.vertices.constData(); for (int i = 0; i < vertexCount; ++i) - vdst[i].set(vsrc[i * 2] / SCALE, vsrc[i * 2 + 1] / SCALE, d->fillColor); + vdst[i].set(vsrc[i * 2] / SCALE, vsrc[i * 2 + 1] / SCALE, fillColor); - d->fillIndices.resize(ts.indices.size()); - quint16 *idst = d->fillIndices.data(); + fillIndices->resize(ts.indices.size()); + quint16 *idst = fillIndices->data(); if (ts.indices.type() == QVertexIndexVector::UnsignedShort) { - memcpy(idst, ts.indices.data(), d->fillIndices.count() * sizeof(quint16)); + memcpy(idst, ts.indices.data(), fillIndices->count() * sizeof(quint16)); } else { const quint32 *isrc = (const quint32 *) ts.indices.data(); - for (int i = 0; i < d->fillIndices.count(); ++i) + for (int i = 0; i < fillIndices->count(); ++i) idst[i] = isrc[i]; } } -void QQuickPathItemGenericRenderer::triangulateStroke(VisualPathData *d) +void QQuickPathItemGenericRenderer::triangulateStroke(const QPainterPath &path, + const QPen &pen, + const Color4ub &strokeColor, + VerticesType *strokeVertices, + const QSize &clipSize) { - const QVectorPath &vp = qtVectorPathForPath(d->path); - - const QRectF clip(0, 0, m_item->width(), m_item->height()); + const QVectorPath &vp = qtVectorPathForPath(path); + const QRectF clip(QPointF(0, 0), clipSize); const qreal inverseScale = 1.0 / SCALE; - m_stroker.setInvScale(inverseScale); - if (d->pen.style() == Qt::SolidLine) { - m_stroker.process(vp, d->pen, clip, 0); + + QTriangulatingStroker stroker; + stroker.setInvScale(inverseScale); + + if (pen.style() == Qt::SolidLine) { + stroker.process(vp, pen, clip, 0); } else { - m_dashStroker.setInvScale(inverseScale); - m_dashStroker.process(vp, d->pen, clip, 0); - QVectorPath dashStroke(m_dashStroker.points(), m_dashStroker.elementCount(), - m_dashStroker.elementTypes(), 0); - m_stroker.process(dashStroke, d->pen, clip, 0); + QDashedStrokeProcessor dashStroker; + dashStroker.setInvScale(inverseScale); + dashStroker.process(vp, pen, clip, 0); + QVectorPath dashStroke(dashStroker.points(), dashStroker.elementCount(), + dashStroker.elementTypes(), 0); + stroker.process(dashStroke, pen, clip, 0); } - if (!m_stroker.vertexCount()) { - d->strokeVertices.clear(); + if (!stroker.vertexCount()) { + strokeVertices->clear(); return; } - const int vertexCount = m_stroker.vertexCount() / 2; // just a float vector with x,y hence the / 2 - d->strokeVertices.resize(vertexCount); - ColoredVertex *vdst = reinterpret_cast(d->strokeVertices.data()); - const float *vsrc = m_stroker.vertices(); + const int vertexCount = stroker.vertexCount() / 2; // just a float vector with x,y hence the / 2 + strokeVertices->resize(vertexCount); + ColoredVertex *vdst = reinterpret_cast(strokeVertices->data()); + const float *vsrc = stroker.vertices(); for (int i = 0; i < vertexCount; ++i) - vdst[i].set(vsrc[i * 2], vsrc[i * 2 + 1], d->strokeColor); + vdst[i].set(vsrc[i * 2], vsrc[i * 2 + 1], strokeColor); } void QQuickPathItemGenericRenderer::setRootNode(QQuickPathItemGenericNode *node) @@ -317,7 +412,7 @@ void QQuickPathItemGenericRenderer::updateNode() QQuickPathItemGenericNode *node = *nodePtr; if (m_accDirty & DirtyList) - d.effectiveDirty |= DirtyGeom; + d.effectiveDirty |= DirtyGeom | DirtyColor | DirtyFillGradient; if (!d.effectiveDirty) continue; @@ -361,25 +456,36 @@ void QQuickPathItemGenericRenderer::updateNode() m_accDirty = 0; } +void QQuickPathItemGenericRenderer::updateShadowDataInNode(VisualPathData *d, QQuickPathItemGenericStrokeFillNode *n) +{ + if (d->fillGradientActive) { + if (d->effectiveDirty & DirtyFillGradient) + n->m_fillGradient = d->fillGradient; + } +} + void QQuickPathItemGenericRenderer::updateFillNode(VisualPathData *d, QQuickPathItemGenericNode *node) { if (!node->m_fillNode) return; + // Make a copy of the data that will be accessed by the material on + // the render thread. This must be done even when we bail out below. QQuickPathItemGenericStrokeFillNode *n = node->m_fillNode; + updateShadowDataInNode(d, n); + QSGGeometry *g = &n->m_geometry; if (d->fillVertices.isEmpty()) { - g->allocate(0, 0); - n->markDirty(QSGNode::DirtyGeometry); + if (g->vertexCount() || g->indexCount()) { + g->allocate(0, 0); + n->markDirty(QSGNode::DirtyGeometry); + } return; } if (d->fillGradientActive) { n->activateMaterial(QQuickPathItemGenericStrokeFillNode::MatLinearGradient); if (d->effectiveDirty & DirtyFillGradient) { - // Make a copy of the data that will be accessed by the material on - // the render thread. - n->m_fillGradient = d->fillGradient; // Gradients are implemented via a texture-based material. n->markDirty(QSGNode::DirtyMaterial); // stop here if only the gradient changed; no need to touch the geometry @@ -414,14 +520,17 @@ void QQuickPathItemGenericRenderer::updateStrokeNode(VisualPathData *d, QQuickPa return; QQuickPathItemGenericStrokeFillNode *n = node->m_strokeNode; - n->markDirty(QSGNode::DirtyGeometry); - QSGGeometry *g = &n->m_geometry; if (d->strokeVertices.isEmpty()) { - g->allocate(0, 0); + if (g->vertexCount() || g->indexCount()) { + g->allocate(0, 0); + n->markDirty(QSGNode::DirtyGeometry); + } return; } + n->markDirty(QSGNode::DirtyGeometry); + if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyGeom)) { ColoredVertex *vdst = reinterpret_cast(g->vertexData()); for (int i = 0; i < g->vertexCount(); ++i) diff --git a/src/quick/items/qquickpathitemgenericrenderer_p.h b/src/quick/items/qquickpathitemgenericrenderer_p.h index 4454b14a13..a4ed090004 100644 --- a/src/quick/items/qquickpathitemgenericrenderer_p.h +++ b/src/quick/items/qquickpathitemgenericrenderer_p.h @@ -55,11 +55,14 @@ #include #include #include -#include +#include QT_BEGIN_NAMESPACE class QQuickPathItemGenericNode; +class QQuickPathItemGenericStrokeFillNode; +class QQuickPathItemFillRunnable; +class QQuickPathItemStrokeRunnable; class QQuickPathItemGenericRenderer : public QQuickAbstractPathRenderer { @@ -88,15 +91,29 @@ public: void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, qreal dashOffset, const QVector &dashPattern) override; void setFillGradient(int index, QQuickPathGradient *gradient) override; - void endSync() override; + void endSync(bool async) override; void updateNode() override; void setRootNode(QQuickPathItemGenericNode *node); struct Color4ub { unsigned char r, g, b, a; }; + typedef QVector VerticesType; + typedef QVector IndicesType; + + static void triangulateFill(const QPainterPath &path, + const Color4ub &fillColor, + VerticesType *fillVertices, + IndicesType *fillIndices); + static void triangulateStroke(const QPainterPath &path, + const QPen &pen, + const Color4ub &strokeColor, + VerticesType *strokeVertices, + const QSize &clipSize); private: + void maybeUpdateAsyncItem(); + struct VisualPathData { float strokeWidth; QPen pen; @@ -106,27 +123,60 @@ private: QPainterPath path; bool fillGradientActive; QQuickPathItemGradientCache::GradientDesc fillGradient; - QVector fillVertices; - QVector fillIndices; - QVector strokeVertices; + VerticesType fillVertices; + IndicesType fillIndices; + VerticesType strokeVertices; int syncDirty; int effectiveDirty = 0; + QQuickPathItemFillRunnable *pendingFill = nullptr; + QQuickPathItemStrokeRunnable *pendingStroke = nullptr; }; - void triangulateFill(VisualPathData *d); - void triangulateStroke(VisualPathData *d); - + void updateShadowDataInNode(VisualPathData *d, QQuickPathItemGenericStrokeFillNode *n); void updateFillNode(VisualPathData *d, QQuickPathItemGenericNode *node); void updateStrokeNode(VisualPathData *d, QQuickPathItemGenericNode *node); QQuickItem *m_item; QQuickPathItemGenericNode *m_rootNode; - QTriangulatingStroker m_stroker; - QDashedStrokeProcessor m_dashStroker; QVector m_vp; int m_accDirty; }; +class QQuickPathItemFillRunnable : public QObject, public QRunnable +{ + Q_OBJECT + +public: + void run() override; + + bool orphaned = false; + QPainterPath path; + QQuickPathItemGenericRenderer::Color4ub fillColor; + QQuickPathItemGenericRenderer::VerticesType fillVertices; + QQuickPathItemGenericRenderer::IndicesType fillIndices; + +Q_SIGNALS: + void done(QQuickPathItemFillRunnable *self); +}; + +class QQuickPathItemStrokeRunnable : public QObject, public QRunnable +{ + Q_OBJECT + +public: + void run() override; + + bool orphaned = false; + QPainterPath path; + QPen pen; + QQuickPathItemGenericRenderer::Color4ub strokeColor; + QQuickPathItemGenericRenderer::VerticesType strokeVertices; + QSize clipSize; + +Q_SIGNALS: + void done(QQuickPathItemStrokeRunnable *self); +}; + class QQuickPathItemGenericStrokeFillNode : public QSGGeometryNode { public: diff --git a/src/quick/items/qquickpathitemnvprrenderer.cpp b/src/quick/items/qquickpathitemnvprrenderer.cpp index 0c4357145a..9303f698ac 100644 --- a/src/quick/items/qquickpathitemnvprrenderer.cpp +++ b/src/quick/items/qquickpathitemnvprrenderer.cpp @@ -140,7 +140,7 @@ void QQuickPathItemNvprRenderer::setFillGradient(int index, QQuickPathGradient * m_accDirty |= DirtyFillGradient; } -void QQuickPathItemNvprRenderer::endSync() +void QQuickPathItemNvprRenderer::endSync(bool) { } diff --git a/src/quick/items/qquickpathitemnvprrenderer_p.h b/src/quick/items/qquickpathitemnvprrenderer_p.h index 067ecf7355..1617de17e6 100644 --- a/src/quick/items/qquickpathitemnvprrenderer_p.h +++ b/src/quick/items/qquickpathitemnvprrenderer_p.h @@ -90,7 +90,7 @@ public: void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, qreal dashOffset, const QVector &dashPattern) override; void setFillGradient(int index, QQuickPathGradient *gradient) override; - void endSync() override; + void endSync(bool async) override; void updateNode() override; diff --git a/src/quick/items/qquickpathitemsoftwarerenderer.cpp b/src/quick/items/qquickpathitemsoftwarerenderer.cpp index 6c3cf73a56..63d4f8f6d4 100644 --- a/src/quick/items/qquickpathitemsoftwarerenderer.cpp +++ b/src/quick/items/qquickpathitemsoftwarerenderer.cpp @@ -158,7 +158,7 @@ void QQuickPathItemSoftwareRenderer::setFillGradient(int index, QQuickPathGradie m_accDirty |= DirtyBrush; } -void QQuickPathItemSoftwareRenderer::endSync() +void QQuickPathItemSoftwareRenderer::endSync(bool) { } diff --git a/src/quick/items/qquickpathitemsoftwarerenderer_p.h b/src/quick/items/qquickpathitemsoftwarerenderer_p.h index 60b52e2def..38130d7301 100644 --- a/src/quick/items/qquickpathitemsoftwarerenderer_p.h +++ b/src/quick/items/qquickpathitemsoftwarerenderer_p.h @@ -82,7 +82,7 @@ public: void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, qreal dashOffset, const QVector &dashPattern) override; void setFillGradient(int index, QQuickPathGradient *gradient) override; - void endSync() override; + void endSync(bool async) override; void updateNode() override; -- cgit v1.2.3 From 72cfdac50b752b8f2d45929265cf4f09cb990bd2 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 10 Jan 2017 10:29:38 +0100 Subject: Add PathItem.status to report async processing progress Change-Id: I09ccdf9c542ebca56187284a0d1776b64c98bfe8 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitem.cpp | 38 +++++++++++++++++++++-- src/quick/items/qquickpathitem_p.h | 11 +++++++ src/quick/items/qquickpathitem_p_p.h | 11 +++++++ src/quick/items/qquickpathitemgenericrenderer.cpp | 15 +++++++++ src/quick/items/qquickpathitemgenericrenderer_p.h | 7 ++++- 5 files changed, 78 insertions(+), 4 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index 964d997806..306e79dc1e 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -362,6 +362,7 @@ QQuickPathItemPrivate::QQuickPathItemPrivate() vpChanged(false), rendererType(QQuickPathItem::UnknownRenderer), async(false), + status(QQuickPathItem::Null), renderer(nullptr) { } @@ -378,6 +379,15 @@ void QQuickPathItemPrivate::_q_visualPathChanged() q->polish(); } +void QQuickPathItemPrivate::setStatus(QQuickPathItem::Status newStatus) +{ + Q_Q(QQuickPathItem); + if (status != newStatus) { + status = newStatus; + emit q->statusChanged(); + } +} + QQuickPathItem::QQuickPathItem(QQuickItem *parent) : QQuickItem(*(new QQuickPathItemPrivate), parent) { @@ -411,6 +421,12 @@ void QQuickPathItem::setAsynchronous(bool async) } } +QQuickPathItem::Status QQuickPathItem::status() const +{ + Q_D(const QQuickPathItem); + return d->status; +} + static QQuickVisualPath *vpe_at(QQmlListProperty *property, int index) { QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(static_cast(property->object)); @@ -492,8 +508,9 @@ void QQuickPathItem::updatePolish() emit rendererChanged(); } - // endSync() is where expensive calculations may happen, depending on the - // backend. Therefore do this only when the item is visible. + // endSync() is where expensive calculations may happen (or get kicked off + // on worker threads), depending on the backend. Therefore do this only + // when the item is visible. if (isVisible()) d->sync(); @@ -593,8 +610,20 @@ QSGNode *QQuickPathItemPrivate::createNode() return node; } +static void q_asyncPathItemReady(void *data) +{ + QQuickPathItemPrivate *self = static_cast(data); + self->setStatus(QQuickPathItem::Ready); +} + void QQuickPathItemPrivate::sync() { + const bool useAsync = async && renderer->flags().testFlag(QQuickAbstractPathRenderer::SupportsAsync); + if (useAsync) { + setStatus(QQuickPathItem::Processing); + renderer->setAsyncCallback(q_asyncPathItemReady, this); + } + const int count = vp.count(); renderer->beginSync(count); @@ -624,7 +653,10 @@ void QQuickPathItemPrivate::sync() dirty = 0; } - renderer->endSync(async); + renderer->endSync(useAsync); + + if (!useAsync) + setStatus(QQuickPathItem::Ready); } // ***** gradient support ***** diff --git a/src/quick/items/qquickpathitem_p.h b/src/quick/items/qquickpathitem_p.h index 7c962d01fc..1b36348cd2 100644 --- a/src/quick/items/qquickpathitem_p.h +++ b/src/quick/items/qquickpathitem_p.h @@ -264,6 +264,7 @@ class Q_QUICK_PRIVATE_EXPORT QQuickPathItem : public QQuickItem Q_OBJECT Q_PROPERTY(RendererType renderer READ rendererType NOTIFY rendererChanged) Q_PROPERTY(bool asynchronous READ asynchronous WRITE setAsynchronous NOTIFY asynchronousChanged) + Q_PROPERTY(Status status READ status NOTIFY statusChanged) Q_PROPERTY(QQmlListProperty visualPaths READ visualPaths) Q_CLASSINFO("DefaultProperty", "visualPaths") @@ -276,6 +277,13 @@ public: }; Q_ENUM(RendererType) + enum Status { + Null, + Ready, + Processing + }; + Q_ENUM(Status) + QQuickPathItem(QQuickItem *parent = nullptr); ~QQuickPathItem(); @@ -284,6 +292,8 @@ public: bool asynchronous() const; void setAsynchronous(bool async); + Status status() const; + QQmlListProperty visualPaths(); protected: @@ -296,6 +306,7 @@ protected: Q_SIGNALS: void rendererChanged(); void asynchronousChanged(); + void statusChanged(); private: Q_DISABLE_COPY(QQuickPathItem) diff --git a/src/quick/items/qquickpathitem_p_p.h b/src/quick/items/qquickpathitem_p_p.h index faf7b1e451..3c63ec6dc2 100644 --- a/src/quick/items/qquickpathitem_p_p.h +++ b/src/quick/items/qquickpathitem_p_p.h @@ -65,6 +65,11 @@ class QSGPlainTexture; class QQuickAbstractPathRenderer { public: + enum Flag { + SupportsAsync = 0x01 + }; + Q_DECLARE_FLAGS(Flags, Flag) + virtual ~QQuickAbstractPathRenderer() { } // Gui thread @@ -80,11 +85,15 @@ public: qreal dashOffset, const QVector &dashPattern) = 0; virtual void setFillGradient(int index, QQuickPathGradient *gradient) = 0; virtual void endSync(bool async) = 0; + virtual void setAsyncCallback(void (*)(void *), void *) { } + virtual Flags flags() const { return 0; } // Render thread, with gui blocked virtual void updateNode() = 0; }; +Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickAbstractPathRenderer::Flags) + class QQuickVisualPathPrivate : public QObjectPrivate { Q_DECLARE_PUBLIC(QQuickVisualPath) @@ -138,6 +147,7 @@ public: void sync(); void _q_visualPathChanged(); + void setStatus(QQuickPathItem::Status newStatus); static QQuickPathItemPrivate *get(QQuickPathItem *item) { return item->d_func(); } @@ -145,6 +155,7 @@ public: bool vpChanged; QQuickPathItem::RendererType rendererType; bool async; + QQuickPathItem::Status status; QQuickAbstractPathRenderer *renderer; QVector vp; }; diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index 6e4c89742a..59b03e0fd2 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -210,8 +210,16 @@ void QQuickPathItemStrokeRunnable::run() emit done(this); } +void QQuickPathItemGenericRenderer::setAsyncCallback(void (*callback)(void *), void *data) +{ + m_asyncCallback = callback; + m_asyncCallbackData = data; +} + void QQuickPathItemGenericRenderer::endSync(bool async) { + bool didKickOffAsync = false; + for (int i = 0; i < m_vp.count(); ++i) { VisualPathData &d(m_vp[i]); if (!d.syncDirty) @@ -264,6 +272,7 @@ void QQuickPathItemGenericRenderer::endSync(bool async) } r->deleteLater(); }); + didKickOffAsync = true; threadPool.start(r); } else { triangulateFill(d.path, d.fillColor, &d.fillVertices, &d.fillIndices); @@ -290,6 +299,7 @@ void QQuickPathItemGenericRenderer::endSync(bool async) } r->deleteLater(); }); + didKickOffAsync = true; threadPool.start(r); } else { triangulateStroke(d.path, d.pen, d.strokeColor, &d.strokeVertices, @@ -298,6 +308,9 @@ void QQuickPathItemGenericRenderer::endSync(bool async) } } } + + if (!didKickOffAsync && async && m_asyncCallback) + m_asyncCallback(m_asyncCallbackData); } void QQuickPathItemGenericRenderer::maybeUpdateAsyncItem() @@ -308,6 +321,8 @@ void QQuickPathItemGenericRenderer::maybeUpdateAsyncItem() } m_accDirty |= DirtyGeom; m_item->update(); + if (m_asyncCallback) + m_asyncCallback(m_asyncCallbackData); } // the stroke/fill triangulation functions may be invoked either on the gui diff --git a/src/quick/items/qquickpathitemgenericrenderer_p.h b/src/quick/items/qquickpathitemgenericrenderer_p.h index a4ed090004..3193c27cb3 100644 --- a/src/quick/items/qquickpathitemgenericrenderer_p.h +++ b/src/quick/items/qquickpathitemgenericrenderer_p.h @@ -77,7 +77,8 @@ public: QQuickPathItemGenericRenderer(QQuickItem *item) : m_item(item), m_rootNode(nullptr), - m_accDirty(0) + m_accDirty(0), + m_asyncCallback(nullptr) { } void beginSync(int totalCount) override; @@ -92,6 +93,8 @@ public: qreal dashOffset, const QVector &dashPattern) override; void setFillGradient(int index, QQuickPathGradient *gradient) override; void endSync(bool async) override; + void setAsyncCallback(void (*)(void *), void *) override; + Flags flags() const override { return SupportsAsync; } void updateNode() override; @@ -140,6 +143,8 @@ private: QQuickPathItemGenericNode *m_rootNode; QVector m_vp; int m_accDirty; + void (*m_asyncCallback)(void *); + void *m_asyncCallbackData; }; class QQuickPathItemFillRunnable : public QObject, public QRunnable -- cgit v1.2.3 From 20c2c8b627b119b9ed72dd40f5b1f77786fad1de Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 10 Jan 2017 11:42:44 +0100 Subject: Separate fill and stroke geometry updates in generic PathItem A change in parameters affecting fill or stroke only does not need touching the other set of geometry at all. Split DirtyGeom into DirtyFillGeom and DirtyStrokeGeom. As a result, neither triangulation nor the QSGGeometry update will happen for fill when only a stroke-related parameter changes, and vice versa. Change-Id: I839873899d687b6c1c8541c9186c9d93438b999a Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemgenericrenderer.cpp | 157 +++++++++++----------- src/quick/items/qquickpathitemgenericrenderer_p.h | 9 +- 2 files changed, 85 insertions(+), 81 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index 59b03e0fd2..367fac985e 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -121,7 +121,7 @@ void QQuickPathItemGenericRenderer::setPath(int index, const QQuickPath *path) { VisualPathData &d(m_vp[index]); d.path = path ? path->path() : QPainterPath(); - d.syncDirty |= DirtyGeom; + d.syncDirty |= DirtyFillGeom | DirtyStrokeGeom; } void QQuickPathItemGenericRenderer::setStrokeColor(int index, const QColor &color) @@ -137,7 +137,7 @@ void QQuickPathItemGenericRenderer::setStrokeWidth(int index, qreal w) d.strokeWidth = w; if (w >= 0.0f) d.pen.setWidthF(w); - d.syncDirty |= DirtyGeom; + d.syncDirty |= DirtyStrokeGeom; } void QQuickPathItemGenericRenderer::setFillColor(int index, const QColor &color) @@ -151,7 +151,7 @@ void QQuickPathItemGenericRenderer::setFillRule(int index, QQuickVisualPath::Fil { VisualPathData &d(m_vp[index]); d.fillRule = Qt::FillRule(fillRule); - d.syncDirty |= DirtyGeom; + d.syncDirty |= DirtyFillGeom; } void QQuickPathItemGenericRenderer::setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) @@ -159,14 +159,14 @@ void QQuickPathItemGenericRenderer::setJoinStyle(int index, QQuickVisualPath::Jo VisualPathData &d(m_vp[index]); d.pen.setJoinStyle(Qt::PenJoinStyle(joinStyle)); d.pen.setMiterLimit(miterLimit); - d.syncDirty |= DirtyGeom; + d.syncDirty |= DirtyStrokeGeom; } void QQuickPathItemGenericRenderer::setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) { VisualPathData &d(m_vp[index]); d.pen.setCapStyle(Qt::PenCapStyle(capStyle)); - d.syncDirty |= DirtyGeom; + d.syncDirty |= DirtyStrokeGeom; } void QQuickPathItemGenericRenderer::setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, @@ -178,7 +178,7 @@ void QQuickPathItemGenericRenderer::setStrokeStyle(int index, QQuickVisualPath:: d.pen.setDashPattern(dashPattern); d.pen.setDashOffset(dashOffset); } - d.syncDirty |= DirtyGeom; + d.syncDirty |= DirtyStrokeGeom; } void QQuickPathItemGenericRenderer::setFillGradient(int index, QQuickPathGradient *gradient) @@ -241,70 +241,71 @@ void QQuickPathItemGenericRenderer::endSync(bool async) continue; } - if (d.syncDirty & DirtyGeom) { - static QThreadPool threadPool; - static bool threadPoolReady = false; - if (async && !threadPoolReady) { - threadPoolReady = true; - const int idealCount = QThread::idealThreadCount(); - threadPool.setMaxThreadCount(idealCount > 0 ? idealCount * 2 : 4); - } - if (d.fillColor.a) { - d.path.setFillRule(d.fillRule); - if (async) { - QQuickPathItemFillRunnable *r = new QQuickPathItemFillRunnable; - r->setAutoDelete(false); - if (d.pendingFill) - d.pendingFill->orphaned = true; - d.pendingFill = r; - r->path = d.path; - r->fillColor = d.fillColor; - // Unlikely in practice but in theory m_vp could be - // resized. Therefore, capture 'i' instead of 'd'. - QObject::connect(r, &QQuickPathItemFillRunnable::done, qApp, [this, i](QQuickPathItemFillRunnable *r) { - if (!r->orphaned && i < m_vp.count()) { - VisualPathData &d(m_vp[i]); - d.fillVertices = r->fillVertices; - d.fillIndices = r->fillIndices; - d.pendingFill = nullptr; - d.effectiveDirty |= DirtyGeom; - maybeUpdateAsyncItem(); - } - r->deleteLater(); - }); - didKickOffAsync = true; - threadPool.start(r); - } else { - triangulateFill(d.path, d.fillColor, &d.fillVertices, &d.fillIndices); - } + static QThreadPool threadPool; + static bool threadPoolReady = false; + + if (async && !threadPoolReady) { + threadPoolReady = true; + const int idealCount = QThread::idealThreadCount(); + threadPool.setMaxThreadCount(idealCount > 0 ? idealCount * 2 : 4); + } + + if ((d.syncDirty & DirtyFillGeom) && d.fillColor.a) { + d.path.setFillRule(d.fillRule); + if (async) { + QQuickPathItemFillRunnable *r = new QQuickPathItemFillRunnable; + r->setAutoDelete(false); + if (d.pendingFill) + d.pendingFill->orphaned = true; + d.pendingFill = r; + r->path = d.path; + r->fillColor = d.fillColor; + // Unlikely in practice but in theory m_vp could be + // resized. Therefore, capture 'i' instead of 'd'. + QObject::connect(r, &QQuickPathItemFillRunnable::done, qApp, [this, i](QQuickPathItemFillRunnable *r) { + if (!r->orphaned && i < m_vp.count()) { + VisualPathData &d(m_vp[i]); + d.fillVertices = r->fillVertices; + d.fillIndices = r->fillIndices; + d.pendingFill = nullptr; + d.effectiveDirty |= DirtyFillGeom; + maybeUpdateAsyncItem(); + } + r->deleteLater(); + }); + didKickOffAsync = true; + threadPool.start(r); + } else { + triangulateFill(d.path, d.fillColor, &d.fillVertices, &d.fillIndices); } - if (d.strokeWidth >= 0.0f && d.strokeColor.a) { - if (async) { - QQuickPathItemStrokeRunnable *r = new QQuickPathItemStrokeRunnable; - r->setAutoDelete(false); - if (d.pendingStroke) - d.pendingStroke->orphaned = true; - d.pendingStroke = r; - r->path = d.path; - r->pen = d.pen; - r->strokeColor = d.strokeColor; - r->clipSize = QSize(m_item->width(), m_item->height()); - QObject::connect(r, &QQuickPathItemStrokeRunnable::done, qApp, [this, i](QQuickPathItemStrokeRunnable *r) { - if (!r->orphaned && i < m_vp.count()) { - VisualPathData &d(m_vp[i]); - d.strokeVertices = r->strokeVertices; - d.pendingStroke = nullptr; - d.effectiveDirty |= DirtyGeom; - maybeUpdateAsyncItem(); - } - r->deleteLater(); - }); - didKickOffAsync = true; - threadPool.start(r); - } else { - triangulateStroke(d.path, d.pen, d.strokeColor, &d.strokeVertices, - QSize(m_item->width(), m_item->height())); - } + } + + if ((d.syncDirty & DirtyStrokeGeom) && d.strokeWidth >= 0.0f && d.strokeColor.a) { + if (async) { + QQuickPathItemStrokeRunnable *r = new QQuickPathItemStrokeRunnable; + r->setAutoDelete(false); + if (d.pendingStroke) + d.pendingStroke->orphaned = true; + d.pendingStroke = r; + r->path = d.path; + r->pen = d.pen; + r->strokeColor = d.strokeColor; + r->clipSize = QSize(m_item->width(), m_item->height()); + QObject::connect(r, &QQuickPathItemStrokeRunnable::done, qApp, [this, i](QQuickPathItemStrokeRunnable *r) { + if (!r->orphaned && i < m_vp.count()) { + VisualPathData &d(m_vp[i]); + d.strokeVertices = r->strokeVertices; + d.pendingStroke = nullptr; + d.effectiveDirty |= DirtyStrokeGeom; + maybeUpdateAsyncItem(); + } + r->deleteLater(); + }); + didKickOffAsync = true; + threadPool.start(r); + } else { + triangulateStroke(d.path, d.pen, d.strokeColor, &d.strokeVertices, + QSize(m_item->width(), m_item->height())); } } } @@ -319,7 +320,7 @@ void QQuickPathItemGenericRenderer::maybeUpdateAsyncItem() if (d.pendingFill || d.pendingStroke) return; } - m_accDirty |= DirtyGeom; + m_accDirty |= DirtyFillGeom | DirtyStrokeGeom; m_item->update(); if (m_asyncCallback) m_asyncCallback(m_asyncCallbackData); @@ -427,7 +428,7 @@ void QQuickPathItemGenericRenderer::updateNode() QQuickPathItemGenericNode *node = *nodePtr; if (m_accDirty & DirtyList) - d.effectiveDirty |= DirtyGeom | DirtyColor | DirtyFillGradient; + d.effectiveDirty |= DirtyFillGeom | DirtyStrokeGeom | DirtyColor | DirtyFillGradient; if (!d.effectiveDirty) continue; @@ -441,7 +442,7 @@ void QQuickPathItemGenericRenderer::updateNode() node->appendChildNode(node->m_fillNode); if (node->m_strokeNode) node->appendChildNode(node->m_strokeNode); - d.effectiveDirty |= DirtyGeom; + d.effectiveDirty |= DirtyFillGeom; } if (d.strokeWidth < 0.0f || d.strokeColor.a == 0) { @@ -450,7 +451,7 @@ void QQuickPathItemGenericRenderer::updateNode() } else if (!node->m_strokeNode) { node->m_strokeNode = new QQuickPathItemGenericStrokeFillNode(m_item->window()); node->appendChildNode(node->m_strokeNode); - d.effectiveDirty |= DirtyGeom; + d.effectiveDirty |= DirtyStrokeGeom; } updateFillNode(&d, node); @@ -483,6 +484,8 @@ void QQuickPathItemGenericRenderer::updateFillNode(VisualPathData *d, QQuickPath { if (!node->m_fillNode) return; + if (!(d->effectiveDirty & (DirtyFillGeom | DirtyColor | DirtyFillGradient))) + return; // Make a copy of the data that will be accessed by the material on // the render thread. This must be done even when we bail out below. @@ -504,13 +507,13 @@ void QQuickPathItemGenericRenderer::updateFillNode(VisualPathData *d, QQuickPath // Gradients are implemented via a texture-based material. n->markDirty(QSGNode::DirtyMaterial); // stop here if only the gradient changed; no need to touch the geometry - if (!(d->effectiveDirty & DirtyGeom)) + if (!(d->effectiveDirty & DirtyFillGeom)) return; } } else { n->activateMaterial(QQuickPathItemGenericStrokeFillNode::MatSolidColor); // fast path for updating only color values when no change in vertex positions - if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyGeom)) { + if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyFillGeom)) { ColoredVertex *vdst = reinterpret_cast(g->vertexData()); for (int i = 0; i < g->vertexCount(); ++i) vdst[i].set(vdst[i].x, vdst[i].y, d->fillColor); @@ -531,7 +534,7 @@ void QQuickPathItemGenericRenderer::updateStrokeNode(VisualPathData *d, QQuickPa { if (!node->m_strokeNode) return; - if (d->effectiveDirty == DirtyFillGradient) // not applicable + if (!(d->effectiveDirty & (DirtyStrokeGeom | DirtyColor))) return; QQuickPathItemGenericStrokeFillNode *n = node->m_strokeNode; @@ -546,7 +549,7 @@ void QQuickPathItemGenericRenderer::updateStrokeNode(VisualPathData *d, QQuickPa n->markDirty(QSGNode::DirtyGeometry); - if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyGeom)) { + if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyStrokeGeom)) { ColoredVertex *vdst = reinterpret_cast(g->vertexData()); for (int i = 0; i < g->vertexCount(); ++i) vdst[i].set(vdst[i].x, vdst[i].y, d->strokeColor); diff --git a/src/quick/items/qquickpathitemgenericrenderer_p.h b/src/quick/items/qquickpathitemgenericrenderer_p.h index 3193c27cb3..17c1f73310 100644 --- a/src/quick/items/qquickpathitemgenericrenderer_p.h +++ b/src/quick/items/qquickpathitemgenericrenderer_p.h @@ -68,10 +68,11 @@ class QQuickPathItemGenericRenderer : public QQuickAbstractPathRenderer { public: enum Dirty { - DirtyGeom = 0x01, - DirtyColor = 0x02, - DirtyFillGradient = 0x04, - DirtyList = 0x08 + DirtyFillGeom = 0x01, + DirtyStrokeGeom = 0x02, + DirtyColor = 0x04, + DirtyFillGradient = 0x08, + DirtyList = 0x10 // only for accDirty }; QQuickPathItemGenericRenderer(QQuickItem *item) -- cgit v1.2.3 From 77614c6c48ce8caf5697afb53a26caf1b165fed3 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 10 Jan 2017 17:17:11 +0100 Subject: Correct software PathItem bounding rect Reporting the QQuickItem geometry is incorrect. Report the area touched by the path instead. This fixes the tiger in the pathitem example. Change-Id: Ib443f442411befabe7864eff6473604926527f4e Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemsoftwarerenderer.cpp | 9 ++++++++- src/quick/items/qquickpathitemsoftwarerenderer_p.h | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemsoftwarerenderer.cpp b/src/quick/items/qquickpathitemsoftwarerenderer.cpp index 63d4f8f6d4..46ebcbfe6d 100644 --- a/src/quick/items/qquickpathitemsoftwarerenderer.cpp +++ b/src/quick/items/qquickpathitemsoftwarerenderer.cpp @@ -180,6 +180,8 @@ void QQuickPathItemSoftwareRenderer::updateNode() if (listChanged) m_node->m_vp.resize(count); + m_node->m_boundingRect = QRectF(); + for (int i = 0; i < count; ++i) { VisualPathGuiData &src(m_vp[i]); QQuickPathItemSoftwareRenderNode::VisualPathRenderData &dst(m_node->m_vp[i]); @@ -201,6 +203,11 @@ void QQuickPathItemSoftwareRenderer::updateNode() dst.brush = src.brush; src.dirty = 0; + + QRectF br = dst.path.boundingRect(); + const float sw = qMax(1.0f, dst.strokeWidth); + br.adjust(-sw, -sw, sw, sw); + m_node->m_boundingRect |= br; } m_node->markDirty(QSGNode::DirtyMaterial); @@ -256,7 +263,7 @@ QSGRenderNode::RenderingFlags QQuickPathItemSoftwareRenderNode::flags() const QRectF QQuickPathItemSoftwareRenderNode::rect() const { - return QRect(0, 0, m_item->width(), m_item->height()); + return m_boundingRect; } QT_END_NAMESPACE diff --git a/src/quick/items/qquickpathitemsoftwarerenderer_p.h b/src/quick/items/qquickpathitemsoftwarerenderer_p.h index 38130d7301..64280b436e 100644 --- a/src/quick/items/qquickpathitemsoftwarerenderer_p.h +++ b/src/quick/items/qquickpathitemsoftwarerenderer_p.h @@ -125,6 +125,7 @@ private: QBrush brush; }; QVector m_vp; + QRectF m_boundingRect; friend class QQuickPathItemSoftwareRenderer; }; -- cgit v1.2.3 From a2252ba7b3942bbafbbb8f4db144f2cfbf0787ab Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Sun, 22 Jan 2017 16:48:15 +0100 Subject: Fix case when async PathItem goes down before finish Marking the work orphaned (to be ignored) is sufficient since runnables exist independently of the renderer or item instances. (even if the lambda executes after the entire Quick infra is gone, we are still fine, as long as we bail out in time and do not dereference the captured this) Change-Id: I0cfb09aaa8b363191ff576b17a88bb7c1c38f3ad Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemgenericrenderer.cpp | 12 ++++++++++++ src/quick/items/qquickpathitemgenericrenderer_p.h | 1 + 2 files changed, 13 insertions(+) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index 367fac985e..e36654ad0c 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -104,6 +104,16 @@ void QQuickPathItemGenericStrokeFillNode::activateMaterial(Material m) setMaterial(m_material); } +QQuickPathItemGenericRenderer::~QQuickPathItemGenericRenderer() +{ + for (VisualPathData &d : m_vp) { + if (d.pendingFill) + d.pendingFill->orphaned = true; + if (d.pendingStroke) + d.pendingStroke->orphaned = true; + } +} + // sync, and so triangulation too, happens on the gui thread // - except when async is set, in which case triangulation is moved to worker threads @@ -263,6 +273,8 @@ void QQuickPathItemGenericRenderer::endSync(bool async) // Unlikely in practice but in theory m_vp could be // resized. Therefore, capture 'i' instead of 'd'. QObject::connect(r, &QQuickPathItemFillRunnable::done, qApp, [this, i](QQuickPathItemFillRunnable *r) { + // Bail out when orphaned (meaning either another run was + // started after this one, or the renderer got destroyed). if (!r->orphaned && i < m_vp.count()) { VisualPathData &d(m_vp[i]); d.fillVertices = r->fillVertices; diff --git a/src/quick/items/qquickpathitemgenericrenderer_p.h b/src/quick/items/qquickpathitemgenericrenderer_p.h index 17c1f73310..ca05492841 100644 --- a/src/quick/items/qquickpathitemgenericrenderer_p.h +++ b/src/quick/items/qquickpathitemgenericrenderer_p.h @@ -81,6 +81,7 @@ public: m_accDirty(0), m_asyncCallback(nullptr) { } + ~QQuickPathItemGenericRenderer(); void beginSync(int totalCount) override; void setPath(int index, const QQuickPath *path) override; -- cgit v1.2.3 From b9f060f6fae5734216d7088afd0e3b52165551c5 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 11 Jan 2017 12:49:33 +0100 Subject: Path generic PathItem work with both ushort and uint index type Default to uint but keep supporting systems without ElementIndexUint. Change-Id: Ic15f66408e2e2ffb6406c7e854fa65ee1e0f56b3 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemgenericrenderer.cpp | 100 ++++++++++++++++++---- src/quick/items/qquickpathitemgenericrenderer_p.h | 41 ++++++--- 2 files changed, 111 insertions(+), 30 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index e36654ad0c..bf9506ba65 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -40,9 +40,15 @@ #include "qquickpathitemgenericrenderer_p.h" #include #include -#include #include +#ifndef QT_NO_OPENGL +#include +#include +#include +#include +#endif + QT_BEGIN_NAMESPACE static const qreal SCALE = 100; @@ -69,17 +75,22 @@ static inline QQuickPathItemGenericRenderer::Color4ub colorToColor4ub(const QCol } QQuickPathItemGenericStrokeFillNode::QQuickPathItemGenericStrokeFillNode(QQuickWindow *window) - : m_geometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), 0, 0), + : m_geometry(new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), 0, 0)), m_window(window), m_material(nullptr) { - setGeometry(&m_geometry); + setGeometry(m_geometry); activateMaterial(MatSolidColor); #ifdef QSG_RUNTIME_DESCRIPTION qsgnode_set_description(this, QLatin1String("stroke-fill")); #endif } +QQuickPathItemGenericStrokeFillNode::~QQuickPathItemGenericStrokeFillNode() +{ + delete m_geometry; +} + void QQuickPathItemGenericStrokeFillNode::activateMaterial(Material m) { switch (m) { @@ -104,6 +115,39 @@ void QQuickPathItemGenericStrokeFillNode::activateMaterial(Material m) setMaterial(m_material); } +static bool q_supportsElementIndexUint(QSGRendererInterface::GraphicsApi api) +{ + static bool elementIndexUint = true; +#ifndef QT_NO_OPENGL + if (api == QSGRendererInterface::OpenGL) { + static bool elementIndexUintChecked = false; + if (!elementIndexUintChecked) { + elementIndexUintChecked = true; + QOpenGLContext *context = QOpenGLContext::currentContext(); + QScopedPointer dummyContext; + QScopedPointer dummySurface; + bool ok = true; + if (!context) { + dummyContext.reset(new QOpenGLContext); + dummyContext->create(); + context = dummyContext.data(); + dummySurface.reset(new QOffscreenSurface); + dummySurface->setFormat(context->format()); + dummySurface->create(); + ok = context->makeCurrent(dummySurface.data()); + } + if (ok) { + elementIndexUint = static_cast(context->functions())->hasOpenGLExtension( + QOpenGLExtensions::ElementIndexUint); + } + } + } +#else + Q_UNUSED(api); +#endif + return elementIndexUint; +} + QQuickPathItemGenericRenderer::~QQuickPathItemGenericRenderer() { for (VisualPathData &d : m_vp) { @@ -210,7 +254,7 @@ void QQuickPathItemGenericRenderer::setFillGradient(int index, QQuickPathGradien void QQuickPathItemFillRunnable::run() { - QQuickPathItemGenericRenderer::triangulateFill(path, fillColor, &fillVertices, &fillIndices); + QQuickPathItemGenericRenderer::triangulateFill(path, fillColor, &fillVertices, &fillIndices, &indexType, supportsElementIndexUint); emit done(this); } @@ -262,6 +306,8 @@ void QQuickPathItemGenericRenderer::endSync(bool async) if ((d.syncDirty & DirtyFillGeom) && d.fillColor.a) { d.path.setFillRule(d.fillRule); + if (m_api == QSGRendererInterface::Unknown) + m_api = m_item->window()->rendererInterface()->graphicsApi(); if (async) { QQuickPathItemFillRunnable *r = new QQuickPathItemFillRunnable; r->setAutoDelete(false); @@ -270,6 +316,7 @@ void QQuickPathItemGenericRenderer::endSync(bool async) d.pendingFill = r; r->path = d.path; r->fillColor = d.fillColor; + r->supportsElementIndexUint = q_supportsElementIndexUint(m_api); // Unlikely in practice but in theory m_vp could be // resized. Therefore, capture 'i' instead of 'd'. QObject::connect(r, &QQuickPathItemFillRunnable::done, qApp, [this, i](QQuickPathItemFillRunnable *r) { @@ -279,6 +326,7 @@ void QQuickPathItemGenericRenderer::endSync(bool async) VisualPathData &d(m_vp[i]); d.fillVertices = r->fillVertices; d.fillIndices = r->fillIndices; + d.indexType = r->indexType; d.pendingFill = nullptr; d.effectiveDirty |= DirtyFillGeom; maybeUpdateAsyncItem(); @@ -288,7 +336,7 @@ void QQuickPathItemGenericRenderer::endSync(bool async) didKickOffAsync = true; threadPool.start(r); } else { - triangulateFill(d.path, d.fillColor, &d.fillVertices, &d.fillIndices); + triangulateFill(d.path, d.fillColor, &d.fillVertices, &d.fillIndices, &d.indexType, q_supportsElementIndexUint(m_api)); } } @@ -342,12 +390,14 @@ void QQuickPathItemGenericRenderer::maybeUpdateAsyncItem() // thread or some worker thread and must thus be self-contained. void QQuickPathItemGenericRenderer::triangulateFill(const QPainterPath &path, const Color4ub &fillColor, - VerticesType *fillVertices, - IndicesType *fillIndices) + VertexContainerType *fillVertices, + IndexContainerType *fillIndices, + QSGGeometry::Type *indexType, + bool supportsElementIndexUint) { const QVectorPath &vp = qtVectorPathForPath(path); - QTriangleSet ts = qTriangulate(vp, QTransform::fromScale(SCALE, SCALE)); + QTriangleSet ts = qTriangulate(vp, QTransform::fromScale(SCALE, SCALE), 1, supportsElementIndexUint); const int vertexCount = ts.vertices.count() / 2; // just a qreal vector with x,y hence the / 2 fillVertices->resize(vertexCount); ColoredVertex *vdst = reinterpret_cast(fillVertices->data()); @@ -355,21 +405,25 @@ void QQuickPathItemGenericRenderer::triangulateFill(const QPainterPath &path, for (int i = 0; i < vertexCount; ++i) vdst[i].set(vsrc[i * 2] / SCALE, vsrc[i * 2 + 1] / SCALE, fillColor); - fillIndices->resize(ts.indices.size()); - quint16 *idst = fillIndices->data(); + size_t indexByteSize; if (ts.indices.type() == QVertexIndexVector::UnsignedShort) { - memcpy(idst, ts.indices.data(), fillIndices->count() * sizeof(quint16)); + *indexType = QSGGeometry::UnsignedShortType; + // fillIndices is still QVector. Just resize to N/2 and pack + // the N quint16s into it. + fillIndices->resize(ts.indices.size() / 2); + indexByteSize = ts.indices.size() * sizeof(quint16); } else { - const quint32 *isrc = (const quint32 *) ts.indices.data(); - for (int i = 0; i < fillIndices->count(); ++i) - idst[i] = isrc[i]; + *indexType = QSGGeometry::UnsignedIntType; + fillIndices->resize(ts.indices.size()); + indexByteSize = ts.indices.size() * sizeof(quint32); } + memcpy(fillIndices->data(), ts.indices.data(), indexByteSize); } void QQuickPathItemGenericRenderer::triangulateStroke(const QPainterPath &path, const QPen &pen, const Color4ub &strokeColor, - VerticesType *strokeVertices, + VertexContainerType *strokeVertices, const QSize &clipSize) { const QVectorPath &vp = qtVectorPathForPath(path); @@ -504,7 +558,7 @@ void QQuickPathItemGenericRenderer::updateFillNode(VisualPathData *d, QQuickPath QQuickPathItemGenericStrokeFillNode *n = node->m_fillNode; updateShadowDataInNode(d, n); - QSGGeometry *g = &n->m_geometry; + QSGGeometry *g = n->m_geometry; if (d->fillVertices.isEmpty()) { if (g->vertexCount() || g->indexCount()) { g->allocate(0, 0); @@ -534,7 +588,17 @@ void QQuickPathItemGenericRenderer::updateFillNode(VisualPathData *d, QQuickPath } } - g->allocate(d->fillVertices.count(), d->fillIndices.count()); + const int indexCount = d->indexType == QSGGeometry::UnsignedShortType + ? d->fillIndices.count() * 2 : d->fillIndices.count(); + if (g->indexType() != d->indexType) { + g = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), + d->fillVertices.count(), indexCount, d->indexType); + n->setGeometry(g); + delete n->m_geometry; + n->m_geometry = g; + } else { + g->allocate(d->fillVertices.count(), indexCount); + } g->setDrawingMode(QSGGeometry::DrawTriangles); memcpy(g->vertexData(), d->fillVertices.constData(), g->vertexCount() * g->sizeOfVertex()); memcpy(g->indexData(), d->fillIndices.constData(), g->indexCount() * g->sizeOfIndex()); @@ -550,7 +614,7 @@ void QQuickPathItemGenericRenderer::updateStrokeNode(VisualPathData *d, QQuickPa return; QQuickPathItemGenericStrokeFillNode *n = node->m_strokeNode; - QSGGeometry *g = &n->m_geometry; + QSGGeometry *g = n->m_geometry; if (d->strokeVertices.isEmpty()) { if (g->vertexCount() || g->indexCount()) { g->allocate(0, 0); diff --git a/src/quick/items/qquickpathitemgenericrenderer_p.h b/src/quick/items/qquickpathitemgenericrenderer_p.h index ca05492841..045c52d610 100644 --- a/src/quick/items/qquickpathitemgenericrenderer_p.h +++ b/src/quick/items/qquickpathitemgenericrenderer_p.h @@ -55,6 +55,7 @@ #include #include #include +#include #include QT_BEGIN_NAMESPACE @@ -77,6 +78,7 @@ public: QQuickPathItemGenericRenderer(QQuickItem *item) : m_item(item), + m_api(QSGRendererInterface::Unknown), m_rootNode(nullptr), m_accDirty(0), m_asyncCallback(nullptr) @@ -103,17 +105,19 @@ public: void setRootNode(QQuickPathItemGenericNode *node); struct Color4ub { unsigned char r, g, b, a; }; - typedef QVector VerticesType; - typedef QVector IndicesType; + typedef QVector VertexContainerType; + typedef QVector IndexContainerType; static void triangulateFill(const QPainterPath &path, const Color4ub &fillColor, - VerticesType *fillVertices, - IndicesType *fillIndices); + VertexContainerType *fillVertices, + IndexContainerType *fillIndices, + QSGGeometry::Type *indexType, + bool supportsElementIndexUint); static void triangulateStroke(const QPainterPath &path, const QPen &pen, const Color4ub &strokeColor, - VerticesType *strokeVertices, + VertexContainerType *strokeVertices, const QSize &clipSize); private: @@ -128,9 +132,10 @@ private: QPainterPath path; bool fillGradientActive; QQuickPathItemGradientCache::GradientDesc fillGradient; - VerticesType fillVertices; - IndicesType fillIndices; - VerticesType strokeVertices; + VertexContainerType fillVertices; + IndexContainerType fillIndices; + QSGGeometry::Type indexType; + VertexContainerType strokeVertices; int syncDirty; int effectiveDirty = 0; QQuickPathItemFillRunnable *pendingFill = nullptr; @@ -142,6 +147,7 @@ private: void updateStrokeNode(VisualPathData *d, QQuickPathItemGenericNode *node); QQuickItem *m_item; + QSGRendererInterface::GraphicsApi m_api; QQuickPathItemGenericNode *m_rootNode; QVector m_vp; int m_accDirty; @@ -157,10 +163,16 @@ public: void run() override; bool orphaned = false; + + // input QPainterPath path; QQuickPathItemGenericRenderer::Color4ub fillColor; - QQuickPathItemGenericRenderer::VerticesType fillVertices; - QQuickPathItemGenericRenderer::IndicesType fillIndices; + bool supportsElementIndexUint; + + // output + QQuickPathItemGenericRenderer::VertexContainerType fillVertices; + QQuickPathItemGenericRenderer::IndexContainerType fillIndices; + QSGGeometry::Type indexType; Q_SIGNALS: void done(QQuickPathItemFillRunnable *self); @@ -174,12 +186,16 @@ public: void run() override; bool orphaned = false; + + // input QPainterPath path; QPen pen; QQuickPathItemGenericRenderer::Color4ub strokeColor; - QQuickPathItemGenericRenderer::VerticesType strokeVertices; QSize clipSize; + // output + QQuickPathItemGenericRenderer::VertexContainerType strokeVertices; + Q_SIGNALS: void done(QQuickPathItemStrokeRunnable *self); }; @@ -188,6 +204,7 @@ class QQuickPathItemGenericStrokeFillNode : public QSGGeometryNode { public: QQuickPathItemGenericStrokeFillNode(QQuickWindow *window); + ~QQuickPathItemGenericStrokeFillNode(); enum Material { MatSolidColor, @@ -202,7 +219,7 @@ public: QQuickPathItemGradientCache::GradientDesc m_fillGradient; private: - QSGGeometry m_geometry; + QSGGeometry *m_geometry; QQuickWindow *m_window; QSGMaterial *m_material; QScopedPointer m_solidColorMaterial; -- cgit v1.2.3 From 5daaec1e193bc69f55d4ddbfef8911ce9810ea28 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 11 Jan 2017 17:05:14 +0100 Subject: Improve stencil clipping with NVPR So that it actually performs when clipping the tiger. Add an example. Change-Id: I7c1c6244710febdb6b02852ebca094665adec417 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitem.cpp | 2 +- src/quick/items/qquickpathitemnvprrenderer.cpp | 54 ++++++++++++++------------ src/quick/items/qquickpathitemnvprrenderer_p.h | 9 +++-- 3 files changed, 36 insertions(+), 29 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index 306e79dc1e..b07a7a6c06 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -587,7 +587,7 @@ QSGNode *QQuickPathItemPrivate::createNode() #ifndef QT_NO_OPENGL case QSGRendererInterface::OpenGL: if (QQuickPathItemNvprRenderNode::isSupported()) { - node = new QQuickPathItemNvprRenderNode(q); + node = new QQuickPathItemNvprRenderNode; static_cast(renderer)->setNode( static_cast(node)); } else { diff --git a/src/quick/items/qquickpathitemnvprrenderer.cpp b/src/quick/items/qquickpathitemnvprrenderer.cpp index 9303f698ac..13fab2dc76 100644 --- a/src/quick/items/qquickpathitemnvprrenderer.cpp +++ b/src/quick/items/qquickpathitemnvprrenderer.cpp @@ -397,11 +397,6 @@ bool QQuickPathItemNvprRenderNode::nvprInited = false; QQuickNvprFunctions QQuickPathItemNvprRenderNode::nvpr; QQuickNvprMaterialManager QQuickPathItemNvprRenderNode::mtlmgr; -QQuickPathItemNvprRenderNode::QQuickPathItemNvprRenderNode(QQuickPathItem *item) - : m_item(item) -{ -} - QQuickPathItemNvprRenderNode::~QQuickPathItemNvprRenderNode() { releaseResources(); @@ -528,6 +523,9 @@ void QQuickPathItemNvprRenderNode::updatePath(VisualPathRenderData *d) // count == 0 -> no dash nvpr.pathDashArray(d->path, d->dashPattern.count(), d->dashPattern.constData()); } + + if (d->dirty) + d->fallbackValid = false; } void QQuickPathItemNvprRenderNode::renderStroke(VisualPathRenderData *d, int strokeStencilValue, int writeMask) @@ -568,23 +566,28 @@ void QQuickPathItemNvprRenderNode::renderFill(VisualPathRenderData *d) void QQuickPathItemNvprRenderNode::renderOffscreenFill(VisualPathRenderData *d) { - QQuickWindow *w = m_item->window(); - const qreal dpr = w->effectiveDevicePixelRatio(); - QSize itemSize = QSize(m_item->width(), m_item->height()) * dpr; - QSize rtSize = w->renderTargetSize(); - if (rtSize.isEmpty()) - rtSize = w->size() * dpr; + if (d->fallbackValid && d->fallbackFbo) + return; + + GLfloat bb[4]; + nvpr.getPathParameterfv(d->path, GL_PATH_STROKE_BOUNDING_BOX_NV, bb); + QSize sz = QSizeF(bb[2] - bb[0] + 1, bb[3] - bb[1] + 1).toSize(); + d->fallbackSize = QSize(qMax(32, sz.width()), qMax(32, sz.height())); + d->fallbackTopLeft = QPointF(bb[0], bb[1]); - if (d->fallbackFbo && d->fallbackFbo->size() != itemSize) { + if (d->fallbackFbo && d->fallbackFbo->size() != d->fallbackSize) { delete d->fallbackFbo; d->fallbackFbo = nullptr; } if (!d->fallbackFbo) - d->fallbackFbo = new QOpenGLFramebufferObject(itemSize, QOpenGLFramebufferObject::CombinedDepthStencil); + d->fallbackFbo = new QOpenGLFramebufferObject(d->fallbackSize, QOpenGLFramebufferObject::CombinedDepthStencil); if (!d->fallbackFbo->bind()) return; - f->glViewport(0, 0, itemSize.width(), itemSize.height()); + GLint prevViewport[4]; + f->glGetIntegerv(GL_VIEWPORT, prevViewport); + + f->glViewport(0, 0, d->fallbackSize.width(), d->fallbackSize.height()); f->glDisable(GL_DEPTH_TEST); f->glClearColor(0, 0, 0, 0); f->glClearStencil(0); @@ -592,16 +595,20 @@ void QQuickPathItemNvprRenderNode::renderOffscreenFill(VisualPathRenderData *d) f->glStencilFunc(GL_NOTEQUAL, 0, 0xFF); f->glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); - nvpr.matrixLoadIdentity(GL_PATH_MODELVIEW_NV); + QMatrix4x4 mv; + mv.translate(-d->fallbackTopLeft.x(), -d->fallbackTopLeft.y()); + nvpr.matrixLoadf(GL_PATH_MODELVIEW_NV, mv.constData()); QMatrix4x4 proj; - proj.ortho(0, itemSize.width(), itemSize.height(), 0, 1, -1); + proj.ortho(0, d->fallbackSize.width(), d->fallbackSize.height(), 0, 1, -1); nvpr.matrixLoadf(GL_PATH_PROJECTION_NV, proj.constData()); renderFill(d); d->fallbackFbo->release(); f->glEnable(GL_DEPTH_TEST); - f->glViewport(0, 0, rtSize.width(), rtSize.height()); + f->glViewport(prevViewport[0], prevViewport[1], prevViewport[2], prevViewport[3]); + + d->fallbackValid = true; } void QQuickPathItemNvprRenderNode::setupStencilForCover(bool stencilClip, int sv) @@ -655,8 +662,8 @@ void QQuickPathItemNvprRenderNode::render(const RenderState *state) for (VisualPathRenderData &d : m_vp) { updatePath(&d); - const bool hasFill = !qFuzzyIsNull(d.fillColor.w()) || d.fillGradientActive; - const bool hasStroke = d.strokeWidth >= 0.0f && !qFuzzyIsNull(d.strokeColor.w()); + const bool hasFill = d.hasFill(); + const bool hasStroke = d.hasStroke(); if (hasFill && stencilClip) { // Fall back to a texture when complex clipping is in use and we have @@ -686,8 +693,10 @@ void QQuickPathItemNvprRenderNode::render(const RenderState *state) m_fallbackBlitter.create(); f->glStencilFunc(GL_EQUAL, sv, 0xFF); f->glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); + QMatrix4x4 mv = *matrix(); + mv.translate(d.fallbackTopLeft.x(), d.fallbackTopLeft.y()); m_fallbackBlitter.texturedQuad(d.fallbackFbo->texture(), d.fallbackFbo->size(), - *state->projectionMatrix(), *matrix(), + *state->projectionMatrix(), mv, inheritedOpacity()); } } @@ -729,11 +738,6 @@ QSGRenderNode::RenderingFlags QQuickPathItemNvprRenderNode::flags() const return DepthAwareRendering; // avoid hitting the less optimal no-opaque-batch path in the renderer } -QRectF QQuickPathItemNvprRenderNode::rect() const -{ - return QRect(0, 0, m_item->width(), m_item->height()); -} - bool QQuickPathItemNvprRenderNode::isSupported() { static const bool nvprDisabled = qEnvironmentVariableIntValue("QT_NO_NVPR") != 0; diff --git a/src/quick/items/qquickpathitemnvprrenderer_p.h b/src/quick/items/qquickpathitemnvprrenderer_p.h index 1617de17e6..61f8b5ebb9 100644 --- a/src/quick/items/qquickpathitemnvprrenderer_p.h +++ b/src/quick/items/qquickpathitemnvprrenderer_p.h @@ -176,14 +176,12 @@ private: class QQuickPathItemNvprRenderNode : public QSGRenderNode { public: - QQuickPathItemNvprRenderNode(QQuickPathItem *item); ~QQuickPathItemNvprRenderNode(); void render(const RenderState *state) override; void releaseResources() override; StateFlags changedStates() const override; RenderingFlags flags() const override; - QRectF rect() const override; static bool isSupported(); @@ -204,6 +202,12 @@ private: bool fillGradientActive; QQuickPathItemGradientCache::GradientDesc fillGradient; QOpenGLFramebufferObject *fallbackFbo = nullptr; + bool fallbackValid = false; + QSize fallbackSize; + QPointF fallbackTopLeft; + + bool hasFill() const { return !qFuzzyIsNull(fillColor.w()) || fillGradientActive; } + bool hasStroke() const { return strokeWidth >= 0.0f && !qFuzzyIsNull(strokeColor.w()); } }; void updatePath(VisualPathRenderData *d); @@ -216,7 +220,6 @@ private: static QQuickNvprFunctions nvpr; static QQuickNvprMaterialManager mtlmgr; - QQuickPathItem *m_item; QQuickNvprBlitter m_fallbackBlitter; QOpenGLExtraFunctions *f = nullptr; -- cgit v1.2.3 From 364de6e01c88cef1ec2f1a6d13dc8d356e159ad0 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Mon, 16 Jan 2017 11:26:52 +0100 Subject: PathItem API cleanup: rename visualPaths to elements visualPaths looks somewhat odd. Not that it matters much since it is the default property anyway, but pick the commonly used "elements" instead. This also allows easier renaming of VisualPath to something better in the future, in case a suitable candidate comes up. Change-Id: I0cdc18cdec49ff821a75f45073598b31f5de9bf8 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitem.cpp | 2 +- src/quick/items/qquickpathitem_p.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index b07a7a6c06..c5ad73e960 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -465,7 +465,7 @@ static void vpe_clear(QQmlListProperty *property) d->_q_visualPathChanged(); } -QQmlListProperty QQuickPathItem::visualPaths() +QQmlListProperty QQuickPathItem::elements() { return QQmlListProperty(this, nullptr, diff --git a/src/quick/items/qquickpathitem_p.h b/src/quick/items/qquickpathitem_p.h index 1b36348cd2..9ca1418d9e 100644 --- a/src/quick/items/qquickpathitem_p.h +++ b/src/quick/items/qquickpathitem_p.h @@ -265,8 +265,8 @@ class Q_QUICK_PRIVATE_EXPORT QQuickPathItem : public QQuickItem Q_PROPERTY(RendererType renderer READ rendererType NOTIFY rendererChanged) Q_PROPERTY(bool asynchronous READ asynchronous WRITE setAsynchronous NOTIFY asynchronousChanged) Q_PROPERTY(Status status READ status NOTIFY statusChanged) - Q_PROPERTY(QQmlListProperty visualPaths READ visualPaths) - Q_CLASSINFO("DefaultProperty", "visualPaths") + Q_PROPERTY(QQmlListProperty elements READ elements) + Q_CLASSINFO("DefaultProperty", "elements") public: enum RendererType { @@ -294,7 +294,7 @@ public: Status status() const; - QQmlListProperty visualPaths(); + QQmlListProperty elements(); protected: QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override; -- cgit v1.2.3 From 5c2ba6db53b2190160081695faff521bc367e33d Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Sun, 22 Jan 2017 17:25:17 +0100 Subject: Destroy the async PathItem work pool properly Avoid getting warnings from QWaitCondition on Windows by managing the lifetime of the QThreadPool ourselves. Change-Id: Idc3545a2336f7971bd382560aa958a325ac69fc6 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemgenericrenderer.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index bf9506ba65..3de01a5bc7 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -270,6 +270,14 @@ void QQuickPathItemGenericRenderer::setAsyncCallback(void (*callback)(void *), v m_asyncCallbackData = data; } +static QThreadPool *pathWorkThreadPool = nullptr; + +static void deletePathWorkThreadPool() +{ + delete pathWorkThreadPool; + pathWorkThreadPool = nullptr; +} + void QQuickPathItemGenericRenderer::endSync(bool async) { bool didKickOffAsync = false; @@ -295,13 +303,11 @@ void QQuickPathItemGenericRenderer::endSync(bool async) continue; } - static QThreadPool threadPool; - static bool threadPoolReady = false; - - if (async && !threadPoolReady) { - threadPoolReady = true; + if (async && !pathWorkThreadPool) { + qAddPostRoutine(deletePathWorkThreadPool); + pathWorkThreadPool = new QThreadPool; const int idealCount = QThread::idealThreadCount(); - threadPool.setMaxThreadCount(idealCount > 0 ? idealCount * 2 : 4); + pathWorkThreadPool->setMaxThreadCount(idealCount > 0 ? idealCount * 2 : 4); } if ((d.syncDirty & DirtyFillGeom) && d.fillColor.a) { @@ -334,7 +340,7 @@ void QQuickPathItemGenericRenderer::endSync(bool async) r->deleteLater(); }); didKickOffAsync = true; - threadPool.start(r); + pathWorkThreadPool->start(r); } else { triangulateFill(d.path, d.fillColor, &d.fillVertices, &d.fillIndices, &d.indexType, q_supportsElementIndexUint(m_api)); } @@ -362,7 +368,7 @@ void QQuickPathItemGenericRenderer::endSync(bool async) r->deleteLater(); }); didKickOffAsync = true; - threadPool.start(r); + pathWorkThreadPool->start(r); } else { triangulateStroke(d.path, d.pen, d.strokeColor, &d.strokeVertices, QSize(m_item->width(), m_item->height())); -- cgit v1.2.3 From a48244d5aa1d14c286b7cd39afebcfff9c9dcb60 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 24 Jan 2017 18:27:27 +0100 Subject: JS API for defining static paths and draw params In order to avoid the over head of one QObject for each VisualPath, Path and PathXxxx element, provide an optional JavaScript API. PathItem { VisualPath { strokeWidth: 4; fillColor: "blue"; dashPattern: [ 1, 2, 3, 4]; capStyle: RoundCap path: Path { PathMove { x: 100; y: 200 } PathLine { x: 300; y: 300 } } } can now also be written as, at least when the path and the stroke/fill params are static and do not need changing/animating later on: PathItem { Component.onCompleted: { var path = newPath(); var sfp = newStrokeFillParams(); path.moveTo(100, 200); path.lineTo(300, 300); sfp.strokeWidth = 4; sfp.fillColor = "blue"; sfp.dashPattern = [ 1, 2, 3, 4 ]; sfp.capStyle = VisualPath.RoundCap; appendVisualPath(path, sfp); commitVisualPaths(); } } In order to emphasize the difference from an imperative API (like context2d), keep the path and the path stroke/fill parameters separate. To preserve our sanity, extras like gradients are not mapped to JavaScript, instead, one still references an QML-defined object from properties like fillGradient. The objects from newPath() and newStrokeFillParams() are reusable by calling clear(). This avoids the need for multiple temp objects when there are multiple paths. Add a simple test and a hidden stress test with the tiger to the manual test. Change-Id: I3b1e275bacf8c8fc52f585fbed5d6f9354d5ae8e Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitem.cpp | 814 +++++++++++++++++++-- src/quick/items/qquickpathitem_p.h | 7 + src/quick/items/qquickpathitem_p_p.h | 103 ++- src/quick/items/qquickpathitemgenericrenderer.cpp | 7 + src/quick/items/qquickpathitemgenericrenderer_p.h | 1 + src/quick/items/qquickpathitemnvprrenderer.cpp | 84 +++ src/quick/items/qquickpathitemnvprrenderer_p.h | 2 + src/quick/items/qquickpathitemsoftwarerenderer.cpp | 8 + src/quick/items/qquickpathitemsoftwarerenderer_p.h | 1 + 9 files changed, 938 insertions(+), 89 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index c5ad73e960..61216be9b0 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -43,15 +43,20 @@ #include "qquickpathitemnvprrenderer_p.h" #include "qquickpathitemsoftwarerenderer_p.h" #include +#include #include #include +#include +#include +#include +#include +#include + QT_BEGIN_NAMESPACE -QQuickVisualPathPrivate::QQuickVisualPathPrivate() - : path(nullptr), - dirty(DirtyAll), - strokeColor(Qt::white), +QQuickPathItemStrokeFillParams::QQuickPathItemStrokeFillParams() + : strokeColor(Qt::white), strokeWidth(1), fillColor(Qt::white), fillRule(QQuickVisualPath::OddEvenFill), @@ -65,6 +70,56 @@ QQuickVisualPathPrivate::QQuickVisualPathPrivate() dashPattern << 4 << 2; // 4 * strokeWidth dash followed by 2 * strokeWidth space } +QPainterPath QQuickPathItemPath::toPainterPath() const +{ + QPainterPath p; + int coordIdx = 0; + for (int i = 0; i < cmd.count(); ++i) { + switch (cmd[i]) { + case QQuickPathItemPath::MoveTo: + p.moveTo(coords[coordIdx], coords[coordIdx + 1]); + coordIdx += 2; + break; + case QQuickPathItemPath::LineTo: + p.lineTo(coords[coordIdx], coords[coordIdx + 1]); + coordIdx += 2; + break; + case QQuickPathItemPath::QuadTo: + p.quadTo(coords[coordIdx], coords[coordIdx + 1], + coords[coordIdx + 2], coords[coordIdx + 3]); + coordIdx += 4; + break; + case QQuickPathItemPath::CubicTo: + p.cubicTo(coords[coordIdx], coords[coordIdx + 1], + coords[coordIdx + 2], coords[coordIdx + 3], + coords[coordIdx + 4], coords[coordIdx + 5]); + coordIdx += 6; + break; + case QQuickPathItemPath::ArcTo: + // does not map to the QPainterPath API; reuse the helper code from QQuickSvgParser + QQuickSvgParser::pathArc(p, + coords[coordIdx], coords[coordIdx + 1], // radius + coords[coordIdx + 2], // xAxisRotation + !qFuzzyIsNull(coords[coordIdx + 6]), // useLargeArc + !qFuzzyIsNull(coords[coordIdx + 5]), // sweep flag + coords[coordIdx + 3], coords[coordIdx + 4], // end + p.currentPosition().x(), p.currentPosition().y()); + coordIdx += 7; + break; + default: + qWarning("Unknown JS path command: %d", cmd[i]); + break; + } + } + return p; +} + +QQuickVisualPathPrivate::QQuickVisualPathPrivate() + : path(nullptr), + dirty(DirtyAll) +{ +} + QQuickVisualPath::QQuickVisualPath(QObject *parent) : QObject(*(new QQuickVisualPathPrivate), parent) { @@ -108,14 +163,14 @@ void QQuickVisualPathPrivate::_q_pathChanged() QColor QQuickVisualPath::strokeColor() const { Q_D(const QQuickVisualPath); - return d->strokeColor; + return d->sfp.strokeColor; } void QQuickVisualPath::setStrokeColor(const QColor &color) { Q_D(QQuickVisualPath); - if (d->strokeColor != color) { - d->strokeColor = color; + if (d->sfp.strokeColor != color) { + d->sfp.strokeColor = color; d->dirty |= QQuickVisualPathPrivate::DirtyStrokeColor; emit strokeColorChanged(); emit changed(); @@ -125,14 +180,14 @@ void QQuickVisualPath::setStrokeColor(const QColor &color) qreal QQuickVisualPath::strokeWidth() const { Q_D(const QQuickVisualPath); - return d->strokeWidth; + return d->sfp.strokeWidth; } void QQuickVisualPath::setStrokeWidth(qreal w) { Q_D(QQuickVisualPath); - if (d->strokeWidth != w) { - d->strokeWidth = w; + if (d->sfp.strokeWidth != w) { + d->sfp.strokeWidth = w; d->dirty |= QQuickVisualPathPrivate::DirtyStrokeWidth; emit strokeWidthChanged(); emit changed(); @@ -142,14 +197,14 @@ void QQuickVisualPath::setStrokeWidth(qreal w) QColor QQuickVisualPath::fillColor() const { Q_D(const QQuickVisualPath); - return d->fillColor; + return d->sfp.fillColor; } void QQuickVisualPath::setFillColor(const QColor &color) { Q_D(QQuickVisualPath); - if (d->fillColor != color) { - d->fillColor = color; + if (d->sfp.fillColor != color) { + d->sfp.fillColor = color; d->dirty |= QQuickVisualPathPrivate::DirtyFillColor; emit fillColorChanged(); emit changed(); @@ -159,14 +214,14 @@ void QQuickVisualPath::setFillColor(const QColor &color) QQuickVisualPath::FillRule QQuickVisualPath::fillRule() const { Q_D(const QQuickVisualPath); - return d->fillRule; + return d->sfp.fillRule; } void QQuickVisualPath::setFillRule(FillRule fillRule) { Q_D(QQuickVisualPath); - if (d->fillRule != fillRule) { - d->fillRule = fillRule; + if (d->sfp.fillRule != fillRule) { + d->sfp.fillRule = fillRule; d->dirty |= QQuickVisualPathPrivate::DirtyFillRule; emit fillRuleChanged(); emit changed(); @@ -176,14 +231,14 @@ void QQuickVisualPath::setFillRule(FillRule fillRule) QQuickVisualPath::JoinStyle QQuickVisualPath::joinStyle() const { Q_D(const QQuickVisualPath); - return d->joinStyle; + return d->sfp.joinStyle; } void QQuickVisualPath::setJoinStyle(JoinStyle style) { Q_D(QQuickVisualPath); - if (d->joinStyle != style) { - d->joinStyle = style; + if (d->sfp.joinStyle != style) { + d->sfp.joinStyle = style; d->dirty |= QQuickVisualPathPrivate::DirtyStyle; emit joinStyleChanged(); emit changed(); @@ -193,14 +248,14 @@ void QQuickVisualPath::setJoinStyle(JoinStyle style) int QQuickVisualPath::miterLimit() const { Q_D(const QQuickVisualPath); - return d->miterLimit; + return d->sfp.miterLimit; } void QQuickVisualPath::setMiterLimit(int limit) { Q_D(QQuickVisualPath); - if (d->miterLimit != limit) { - d->miterLimit = limit; + if (d->sfp.miterLimit != limit) { + d->sfp.miterLimit = limit; d->dirty |= QQuickVisualPathPrivate::DirtyStyle; emit miterLimitChanged(); emit changed(); @@ -210,14 +265,14 @@ void QQuickVisualPath::setMiterLimit(int limit) QQuickVisualPath::CapStyle QQuickVisualPath::capStyle() const { Q_D(const QQuickVisualPath); - return d->capStyle; + return d->sfp.capStyle; } void QQuickVisualPath::setCapStyle(CapStyle style) { Q_D(QQuickVisualPath); - if (d->capStyle != style) { - d->capStyle = style; + if (d->sfp.capStyle != style) { + d->sfp.capStyle = style; d->dirty |= QQuickVisualPathPrivate::DirtyStyle; emit capStyleChanged(); emit changed(); @@ -227,14 +282,14 @@ void QQuickVisualPath::setCapStyle(CapStyle style) QQuickVisualPath::StrokeStyle QQuickVisualPath::strokeStyle() const { Q_D(const QQuickVisualPath); - return d->strokeStyle; + return d->sfp.strokeStyle; } void QQuickVisualPath::setStrokeStyle(StrokeStyle style) { Q_D(QQuickVisualPath); - if (d->strokeStyle != style) { - d->strokeStyle = style; + if (d->sfp.strokeStyle != style) { + d->sfp.strokeStyle = style; d->dirty |= QQuickVisualPathPrivate::DirtyDash; emit strokeStyleChanged(); emit changed(); @@ -244,14 +299,14 @@ void QQuickVisualPath::setStrokeStyle(StrokeStyle style) qreal QQuickVisualPath::dashOffset() const { Q_D(const QQuickVisualPath); - return d->dashOffset; + return d->sfp.dashOffset; } void QQuickVisualPath::setDashOffset(qreal offset) { Q_D(QQuickVisualPath); - if (d->dashOffset != offset) { - d->dashOffset = offset; + if (d->sfp.dashOffset != offset) { + d->sfp.dashOffset = offset; d->dirty |= QQuickVisualPathPrivate::DirtyDash; emit dashOffsetChanged(); emit changed(); @@ -261,14 +316,14 @@ void QQuickVisualPath::setDashOffset(qreal offset) QVector QQuickVisualPath::dashPattern() const { Q_D(const QQuickVisualPath); - return d->dashPattern; + return d->sfp.dashPattern; } void QQuickVisualPath::setDashPattern(const QVector &array) { Q_D(QQuickVisualPath); - if (d->dashPattern != array) { - d->dashPattern = array; + if (d->sfp.dashPattern != array) { + d->sfp.dashPattern = array; d->dirty |= QQuickVisualPathPrivate::DirtyDash; emit dashPatternChanged(); emit changed(); @@ -278,19 +333,19 @@ void QQuickVisualPath::setDashPattern(const QVector &array) QQuickPathGradient *QQuickVisualPath::fillGradient() const { Q_D(const QQuickVisualPath); - return d->fillGradient; + return d->sfp.fillGradient; } void QQuickVisualPath::setFillGradient(QQuickPathGradient *gradient) { Q_D(QQuickVisualPath); - if (d->fillGradient != gradient) { - if (d->fillGradient) - qmlobject_disconnect(d->fillGradient, QQuickPathGradient, SIGNAL(updated()), + if (d->sfp.fillGradient != gradient) { + if (d->sfp.fillGradient) + qmlobject_disconnect(d->sfp.fillGradient, QQuickPathGradient, SIGNAL(updated()), this, QQuickVisualPath, SLOT(_q_fillGradientChanged())); - d->fillGradient = gradient; - if (d->fillGradient) - qmlobject_connect(d->fillGradient, QQuickPathGradient, SIGNAL(updated()), + d->sfp.fillGradient = gradient; + if (d->sfp.fillGradient) + qmlobject_connect(d->sfp.fillGradient, QQuickPathGradient, SIGNAL(updated()), this, QQuickVisualPath, SLOT(_q_fillGradientChanged())); d->dirty |= QQuickVisualPathPrivate::DirtyFillGradient; emit changed(); @@ -430,14 +485,14 @@ QQuickPathItem::Status QQuickPathItem::status() const static QQuickVisualPath *vpe_at(QQmlListProperty *property, int index) { QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(static_cast(property->object)); - return d->vp.at(index); + return d->qmlData.vp.at(index); } static void vpe_append(QQmlListProperty *property, QQuickVisualPath *obj) { QQuickPathItem *item = static_cast(property->object); QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(item); - d->vp.append(obj); + d->qmlData.vp.append(obj); if (d->componentComplete) { QObject::connect(obj, SIGNAL(changed()), item, SLOT(_q_visualPathChanged())); @@ -448,7 +503,7 @@ static void vpe_append(QQmlListProperty *property, QQuickVisua static int vpe_count(QQmlListProperty *property) { QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(static_cast(property->object)); - return d->vp.count(); + return d->qmlData.vp.count(); } static void vpe_clear(QQmlListProperty *property) @@ -456,10 +511,10 @@ static void vpe_clear(QQmlListProperty *property) QQuickPathItem *item = static_cast(property->object); QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(item); - for (QQuickVisualPath *p : d->vp) + for (QQuickVisualPath *p : d->qmlData.vp) QObject::disconnect(p, SIGNAL(changed()), item, SLOT(_q_visualPathChanged())); - d->vp.clear(); + d->qmlData.vp.clear(); if (d->componentComplete) d->_q_visualPathChanged(); @@ -486,7 +541,7 @@ void QQuickPathItem::componentComplete() Q_D(QQuickPathItem); d->componentComplete = true; - for (QQuickVisualPath *p : d->vp) + for (QQuickVisualPath *p : d->qmlData.vp) connect(p, SIGNAL(changed()), this, SLOT(_q_visualPathChanged())); d->_q_visualPathChanged(); @@ -624,36 +679,60 @@ void QQuickPathItemPrivate::sync() renderer->setAsyncCallback(q_asyncPathItemReady, this); } - const int count = vp.count(); - renderer->beginSync(count); - - for (int i = 0; i < count; ++i) { - QQuickVisualPath *p = vp[i]; - int &dirty(QQuickVisualPathPrivate::get(p)->dirty); - - if (dirty & QQuickVisualPathPrivate::DirtyPath) - renderer->setPath(i, p->path()); - if (dirty & QQuickVisualPathPrivate::DirtyStrokeColor) - renderer->setStrokeColor(i, p->strokeColor()); - if (dirty & QQuickVisualPathPrivate::DirtyStrokeWidth) - renderer->setStrokeWidth(i, p->strokeWidth()); - if (dirty & QQuickVisualPathPrivate::DirtyFillColor) - renderer->setFillColor(i, p->fillColor()); - if (dirty & QQuickVisualPathPrivate::DirtyFillRule) - renderer->setFillRule(i, p->fillRule()); - if (dirty & QQuickVisualPathPrivate::DirtyStyle) { - renderer->setJoinStyle(i, p->joinStyle(), p->miterLimit()); - renderer->setCapStyle(i, p->capStyle()); + if (!jsData.isValid()) { + // Standard route: The path and stroke/fill parameters are provided via + // VisualPath and Path. + const int count = qmlData.vp.count(); + renderer->beginSync(count); + + for (int i = 0; i < count; ++i) { + QQuickVisualPath *p = qmlData.vp[i]; + int &dirty(QQuickVisualPathPrivate::get(p)->dirty); + + if (dirty & QQuickVisualPathPrivate::DirtyPath) + renderer->setPath(i, p->path()); + if (dirty & QQuickVisualPathPrivate::DirtyStrokeColor) + renderer->setStrokeColor(i, p->strokeColor()); + if (dirty & QQuickVisualPathPrivate::DirtyStrokeWidth) + renderer->setStrokeWidth(i, p->strokeWidth()); + if (dirty & QQuickVisualPathPrivate::DirtyFillColor) + renderer->setFillColor(i, p->fillColor()); + if (dirty & QQuickVisualPathPrivate::DirtyFillRule) + renderer->setFillRule(i, p->fillRule()); + if (dirty & QQuickVisualPathPrivate::DirtyStyle) { + renderer->setJoinStyle(i, p->joinStyle(), p->miterLimit()); + renderer->setCapStyle(i, p->capStyle()); + } + if (dirty & QQuickVisualPathPrivate::DirtyDash) + renderer->setStrokeStyle(i, p->strokeStyle(), p->dashOffset(), p->dashPattern()); + if (dirty & QQuickVisualPathPrivate::DirtyFillGradient) + renderer->setFillGradient(i, p->fillGradient()); + + dirty = 0; } - if (dirty & QQuickVisualPathPrivate::DirtyDash) - renderer->setStrokeStyle(i, p->strokeStyle(), p->dashOffset(), p->dashPattern()); - if (dirty & QQuickVisualPathPrivate::DirtyFillGradient) - renderer->setFillGradient(i, p->fillGradient()); - dirty = 0; - } + renderer->endSync(useAsync); + } else { + // Path and stroke/fill params provided from JavaScript. This avoids + // QObjects at the expense of not supporting changes afterwards. + const int count = jsData.paths.count(); + renderer->beginSync(count); + + for (int i = 0; i < count; ++i) { + renderer->setJSPath(i, jsData.paths[i]); + const QQuickPathItemStrokeFillParams sfp(jsData.sfp[i]); + renderer->setStrokeColor(i, sfp.strokeColor); + renderer->setStrokeWidth(i, sfp.strokeWidth); + renderer->setFillColor(i, sfp.fillColor); + renderer->setFillRule(i, sfp.fillRule); + renderer->setJoinStyle(i, sfp.joinStyle, sfp.miterLimit); + renderer->setCapStyle(i, sfp.capStyle); + renderer->setStrokeStyle(i, sfp.strokeStyle, sfp.dashOffset, sfp.dashPattern); + renderer->setFillGradient(i, sfp.fillGradient); + } - renderer->endSync(useAsync); + renderer->endSync(useAsync); + } if (!useAsync) setStatus(QQuickPathItem::Ready); @@ -935,6 +1014,593 @@ QSGTexture *QQuickPathItemGradientCache::get(const GradientDesc &grad) #endif // QT_NO_OPENGL +// ***** JS-based alternative for creating static paths, (mostly) without QObjects ***** + +class QQuickPathItemJSEngineData : public QV8Engine::Deletable +{ +public: + QQuickPathItemJSEngineData(QV4::ExecutionEngine *engine); + + QV4::PersistentValue pathProto; + QV4::PersistentValue strokeFillParamsProto; +}; + +V4_DEFINE_EXTENSION(QQuickPathItemJSEngineData, engineData) + +namespace QV4 { +namespace Heap { + +struct QQuickPathItemJSPathPrototype : Object { + void init() { Object::init(); } +}; + +struct QQuickPathItemJSPath : Object { + void init() { Object::init(); } + QQuickPathItemPathObject *obj; +}; + +struct QQuickPathItemJSStrokeFillParamsPrototype : Object { + void init() { Object::init(); } +}; + +struct QQuickPathItemJSStrokeFillParams : Object { + void init() { Object::init(); } + QQuickPathItemStrokeFillParamsObject *obj; +}; + +} // namespace Heap +} // namespace QV4 + +struct QQuickPathItemJSPathPrototype : public QV4::Object +{ + V4_OBJECT2(QQuickPathItemJSPathPrototype, QV4::Object) +public: + static QV4::Heap::QQuickPathItemJSPathPrototype *create(QV4::ExecutionEngine *engine) + { + QV4::Scope scope(engine); + auto obj = engine->memoryManager->allocObject(); + QV4::Scoped o(scope, obj); + + o->defineDefaultProperty(QStringLiteral("clear"), method_clear, 0); + o->defineDefaultProperty(QStringLiteral("moveTo"), method_moveTo, 0); + o->defineDefaultProperty(QStringLiteral("lineTo"), method_lineTo, 0); + o->defineDefaultProperty(QStringLiteral("quadTo"), method_quadTo, 0); + o->defineDefaultProperty(QStringLiteral("cubicTo"), method_cubicTo, 0); + o->defineDefaultProperty(QStringLiteral("arcTo"), method_arcTo, 0); + + return o->d(); + } + + static void method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_moveTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_lineTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_quadTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_cubicTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_arcTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); +}; + +DEFINE_OBJECT_VTABLE(QQuickPathItemJSPathPrototype); + +struct QQuickPathItemJSStrokeFillParamsPrototype : public QV4::Object +{ + V4_OBJECT2(QQuickPathItemJSStrokeFillParamsPrototype, QV4::Object) +public: + static QV4::Heap::QQuickPathItemJSStrokeFillParamsPrototype *create(QV4::ExecutionEngine *engine) + { + QV4::Scope scope(engine); + auto obj = engine->memoryManager->allocObject(); + QV4::Scoped o(scope, obj); + + o->defineDefaultProperty(QStringLiteral("clear"), method_clear, 0); + + return o->d(); + } + + static void method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); +}; + +DEFINE_OBJECT_VTABLE(QQuickPathItemJSStrokeFillParamsPrototype); + +struct QQuickPathItemJSPath : public QV4::Object +{ + V4_OBJECT2(QQuickPathItemJSPath, QV4::Object) +}; + +DEFINE_OBJECT_VTABLE(QQuickPathItemJSPath); + +struct QQuickPathItemJSStrokeFillParams : public QV4::Object +{ + V4_OBJECT2(QQuickPathItemJSStrokeFillParams, QV4::Object) + + static void method_get_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_set_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_get_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_set_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_get_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_set_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_get_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_set_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_get_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_set_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_get_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_set_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_get_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_set_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_get_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_set_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_get_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_set_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_get_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_set_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_get_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); + static void method_set_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); +}; + +DEFINE_OBJECT_VTABLE(QQuickPathItemJSStrokeFillParams); + +void QQuickPathItemJSPathPrototype::method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + r->d()->obj->clear(); + + scope.result = callData->thisObject.asReturnedValue(); +} + +void QQuickPathItemJSPathPrototype::method_moveTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + if (callData->argc >= 2) { + QQuickPathItemPathObject *p = r->d()->obj; + p->path.cmd.append(QQuickPathItemPath::MoveTo); + p->path.coords.append(callData->args[0].toNumber()); + p->path.coords.append(callData->args[1].toNumber()); + } + + scope.result = callData->thisObject.asReturnedValue(); +} + +void QQuickPathItemJSPathPrototype::method_lineTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + if (callData->argc >= 2) { + QQuickPathItemPathObject *p = r->d()->obj; + p->path.cmd.append(QQuickPathItemPath::LineTo); + p->path.coords.append(callData->args[0].toNumber()); + p->path.coords.append(callData->args[1].toNumber()); + } + + scope.result = callData->thisObject.asReturnedValue(); +} + +void QQuickPathItemJSPathPrototype::method_quadTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + if (callData->argc >= 4) { + QQuickPathItemPathObject *p = r->d()->obj; + p->path.cmd.append(QQuickPathItemPath::QuadTo); + const QV4::Value *v = callData->args; + p->path.coords.append(v[0].toNumber()); // cx + p->path.coords.append(v[1].toNumber()); // cy + p->path.coords.append(v[2].toNumber()); // x + p->path.coords.append(v[3].toNumber()); // y + } + + scope.result = callData->thisObject.asReturnedValue(); +} + +void QQuickPathItemJSPathPrototype::method_cubicTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + if (callData->argc >= 6) { + QQuickPathItemPathObject *p = r->d()->obj; + p->path.cmd.append(QQuickPathItemPath::CubicTo); + const QV4::Value *v = callData->args; + p->path.coords.append(v[0].toNumber()); // c1x + p->path.coords.append(v[1].toNumber()); // c1y + p->path.coords.append(v[2].toNumber()); // c2x + p->path.coords.append(v[3].toNumber()); // c2y + p->path.coords.append(v[4].toNumber()); // x + p->path.coords.append(v[5].toNumber()); // y + } + + scope.result = callData->thisObject.asReturnedValue(); +} + +void QQuickPathItemJSPathPrototype::method_arcTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + if (callData->argc >= 7) { + QQuickPathItemPathObject *p = r->d()->obj; + p->path.cmd.append(QQuickPathItemPath::ArcTo); + const QV4::Value *v = callData->args; + p->path.coords.append(v[0].toNumber()); // radiusX + p->path.coords.append(v[1].toNumber()); // radiusY + p->path.coords.append(v[2].toNumber()); // xAxisRotation + p->path.coords.append(v[3].toNumber()); // x + p->path.coords.append(v[4].toNumber()); // y + p->path.coords.append(v[5].toNumber()); // sweepFlag + p->path.coords.append(v[6].toNumber()); // largeArc + } + + scope.result = callData->thisObject.asReturnedValue(); +} + +void QQuickPathItemJSStrokeFillParamsPrototype::method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + r->d()->obj->clear(); + + scope.result = callData->thisObject.asReturnedValue(); +} + +extern QColor qt_color_from_string(const QV4::Value &name); // qquickcontext2d.cpp + +static inline QString qt_color_string(const QColor &color) +{ + if (color.alpha() == 255) + return color.name(); + QString alphaString = QString::number(color.alphaF(), 'f'); + while (alphaString.endsWith(QLatin1Char('0'))) + alphaString.chop(1); + if (alphaString.endsWith(QLatin1Char('.'))) + alphaString += QLatin1Char('0'); + return QString::fromLatin1("rgba(%1, %2, %3, %4)").arg(color.red()).arg(color.green()).arg(color.blue()).arg(alphaString); +} + +void QQuickPathItemJSStrokeFillParams::method_get_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + scope.result = QV4::Encode(scope.engine->newString(qt_color_string(r->d()->obj->sfp.strokeColor))); +} + +void QQuickPathItemJSStrokeFillParams::method_set_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedValue value(scope, callData->argument(0)); + if (value->isString()) + r->d()->obj->sfp.strokeColor = qt_color_from_string(value); + + scope.result = QV4::Encode::undefined(); +} + +void QQuickPathItemJSStrokeFillParams::method_get_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + scope.result = scope.engine->fromVariant(r->d()->obj->sfp.strokeWidth); +} + +void QQuickPathItemJSStrokeFillParams::method_set_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedValue value(scope, callData->argument(0)); + r->d()->obj->sfp.strokeWidth = value->toNumber(); + + scope.result = QV4::Encode::undefined(); +} + +void QQuickPathItemJSStrokeFillParams::method_get_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + scope.result = QV4::Encode(scope.engine->newString(qt_color_string(r->d()->obj->sfp.fillColor))); +} + +void QQuickPathItemJSStrokeFillParams::method_set_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedValue value(scope, callData->argument(0)); + if (value->isString()) + r->d()->obj->sfp.fillColor = qt_color_from_string(value); + + scope.result = QV4::Encode::undefined(); +} + +void QQuickPathItemJSStrokeFillParams::method_get_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + scope.result = scope.engine->fromVariant(r->d()->obj->sfp.fillRule); +} + +void QQuickPathItemJSStrokeFillParams::method_set_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedValue value(scope, callData->argument(0)); + if (value->isInt32()) + r->d()->obj->sfp.fillRule = QQuickVisualPath::FillRule(value->integerValue()); + + scope.result = QV4::Encode::undefined(); +} + +void QQuickPathItemJSStrokeFillParams::method_get_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + scope.result = scope.engine->fromVariant(r->d()->obj->sfp.joinStyle); +} + +void QQuickPathItemJSStrokeFillParams::method_set_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedValue value(scope, callData->argument(0)); + if (value->isInt32()) + r->d()->obj->sfp.joinStyle = QQuickVisualPath::JoinStyle(value->integerValue()); + + scope.result = QV4::Encode::undefined(); +} + +void QQuickPathItemJSStrokeFillParams::method_get_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + scope.result = scope.engine->fromVariant(r->d()->obj->sfp.miterLimit); +} + +void QQuickPathItemJSStrokeFillParams::method_set_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedValue value(scope, callData->argument(0)); + r->d()->obj->sfp.miterLimit = value->toNumber(); + + scope.result = QV4::Encode::undefined(); +} + +void QQuickPathItemJSStrokeFillParams::method_get_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + scope.result = scope.engine->fromVariant(r->d()->obj->sfp.capStyle); +} + +void QQuickPathItemJSStrokeFillParams::method_set_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedValue value(scope, callData->argument(0)); + if (value->isInt32()) + r->d()->obj->sfp.capStyle = QQuickVisualPath::CapStyle(value->integerValue()); + + scope.result = QV4::Encode::undefined(); +} + +void QQuickPathItemJSStrokeFillParams::method_get_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + scope.result = scope.engine->fromVariant(r->d()->obj->sfp.strokeStyle); +} + +void QQuickPathItemJSStrokeFillParams::method_set_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedValue value(scope, callData->argument(0)); + if (value->isInt32()) + r->d()->obj->sfp.strokeStyle = QQuickVisualPath::StrokeStyle(value->integerValue()); + + scope.result = QV4::Encode::undefined(); +} + +void QQuickPathItemJSStrokeFillParams::method_get_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + scope.result = scope.engine->fromVariant(r->d()->obj->sfp.dashOffset); +} + +void QQuickPathItemJSStrokeFillParams::method_set_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedValue value(scope, callData->argument(0)); + r->d()->obj->sfp.dashOffset = value->toNumber(); + + scope.result = QV4::Encode::undefined(); +} + +void QQuickPathItemJSStrokeFillParams::method_get_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedArrayObject a(scope, scope.engine->newArrayObject()); + QQuickPathItemStrokeFillParamsObject *p = r->d()->obj; + a->arrayReserve(p->sfp.dashPattern.count()); + QV4::ScopedValue v(scope); + for (int i = 0; i < p->sfp.dashPattern.count(); ++i) + a->arrayPut(i, (v = scope.engine->fromVariant(p->sfp.dashPattern[i]))); + a->setArrayLengthUnchecked(p->sfp.dashPattern.count()); + + scope.result = a.asReturnedValue(); +} + +void QQuickPathItemJSStrokeFillParams::method_set_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedValue value(scope, callData->argument(0)); + if (value->isObject()) { + QV4::Scoped ao(scope, value); + if (!!ao) { + QQuickPathItemStrokeFillParamsObject *p = r->d()->obj; + p->sfp.dashPattern.resize(ao->getLength()); + QV4::ScopedValue val(scope); + for (int i = 0; i < p->sfp.dashPattern.count(); ++i) { + val = ao->getIndexed(i); + p->sfp.dashPattern[i] = val->toNumber(); + } + } + } + + scope.result = QV4::Encode::undefined(); +} + +void QQuickPathItemJSStrokeFillParams::method_get_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + scope.result = r->d()->obj->v4fillGradient.value(); +} + +void QQuickPathItemJSStrokeFillParams::method_set_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) +{ + QV4::Scoped r(scope, callData->thisObject.as()); + + QV4::ScopedValue value(scope, callData->argument(0)); + QV4::Scoped qobjectWrapper(scope, value); + if (!!qobjectWrapper) { + if (QQuickPathGradient *grad = qobject_cast(qobjectWrapper->object())) { + r->d()->obj->v4fillGradient.set(scope.engine, value); + r->d()->obj->sfp.fillGradient = grad; + } + } else { + r->d()->obj->v4fillGradient.set(scope.engine, nullptr); + r->d()->obj->sfp.fillGradient = nullptr; + } + + scope.result = QV4::Encode::undefined(); +} + +QQuickPathItemJSEngineData::QQuickPathItemJSEngineData(QV4::ExecutionEngine *v4) +{ + QV4::Scope scope(v4); + + QV4::ScopedObject proto(scope, QQuickPathItemJSPathPrototype::create(v4)); + pathProto = proto; + + proto = QV4::ScopedObject(scope, QQuickPathItemJSStrokeFillParamsPrototype::create(v4)); + + proto->defineAccessorProperty(QStringLiteral("strokeColor"), + QQuickPathItemJSStrokeFillParams::method_get_strokeColor, + QQuickPathItemJSStrokeFillParams::method_set_strokeColor); + proto->defineAccessorProperty(QStringLiteral("strokeWidth"), + QQuickPathItemJSStrokeFillParams::method_get_strokeWidth, + QQuickPathItemJSStrokeFillParams::method_set_strokeWidth); + proto->defineAccessorProperty(QStringLiteral("fillColor"), + QQuickPathItemJSStrokeFillParams::method_get_fillColor, + QQuickPathItemJSStrokeFillParams::method_set_fillColor); + proto->defineAccessorProperty(QStringLiteral("fillRule"), + QQuickPathItemJSStrokeFillParams::method_get_fillRule, + QQuickPathItemJSStrokeFillParams::method_set_fillRule); + proto->defineAccessorProperty(QStringLiteral("joinStyle"), + QQuickPathItemJSStrokeFillParams::method_get_joinStyle, + QQuickPathItemJSStrokeFillParams::method_set_joinStyle); + proto->defineAccessorProperty(QStringLiteral("miterLimit"), + QQuickPathItemJSStrokeFillParams::method_get_miterLimit, + QQuickPathItemJSStrokeFillParams::method_set_miterLimit); + proto->defineAccessorProperty(QStringLiteral("capStyle"), + QQuickPathItemJSStrokeFillParams::method_get_capStyle, + QQuickPathItemJSStrokeFillParams::method_set_capStyle); + proto->defineAccessorProperty(QStringLiteral("strokeStyle"), + QQuickPathItemJSStrokeFillParams::method_get_strokeStyle, + QQuickPathItemJSStrokeFillParams::method_set_strokeStyle); + proto->defineAccessorProperty(QStringLiteral("dashOffset"), + QQuickPathItemJSStrokeFillParams::method_get_dashOffset, + QQuickPathItemJSStrokeFillParams::method_set_dashOffset); + proto->defineAccessorProperty(QStringLiteral("dashPattern"), + QQuickPathItemJSStrokeFillParams::method_get_dashPattern, + QQuickPathItemJSStrokeFillParams::method_set_dashPattern); + proto->defineAccessorProperty(QStringLiteral("fillGradient"), + QQuickPathItemJSStrokeFillParams::method_get_fillGradient, + QQuickPathItemJSStrokeFillParams::method_set_fillGradient); + + strokeFillParamsProto = proto; +} + +void QQuickPathItemPathObject::setV4Engine(QV4::ExecutionEngine *engine) +{ + QQuickPathItemJSEngineData *ed = engineData(engine); + QV4::Scope scope(engine); + QV4::Scoped wrapper(scope, engine->memoryManager->allocObject()); + QV4::ScopedObject p(scope, ed->pathProto.value()); + wrapper->setPrototype(p); + wrapper->d()->obj = this; + m_v4value = wrapper; +} + +void QQuickPathItemPathObject::clear() +{ + path = QQuickPathItemPath(); +} + +void QQuickPathItemStrokeFillParamsObject::clear() +{ + sfp = QQuickPathItemStrokeFillParams(); + if (!v4fillGradient.isNullOrUndefined()) + v4fillGradient.set(v4fillGradient.engine(), nullptr); +} + +void QQuickPathItemStrokeFillParamsObject::setV4Engine(QV4::ExecutionEngine *engine) +{ + QQuickPathItemJSEngineData *ed = engineData(engine); + QV4::Scope scope(engine); + QV4::Scoped wrapper(scope, engine->memoryManager->allocObject()); + QV4::ScopedObject p(scope, ed->strokeFillParamsProto.value()); + wrapper->setPrototype(p); + wrapper->d()->obj = this; + m_v4value = wrapper; +} + +void QQuickPathItem::newPath(QQmlV4Function *args) +{ + QQuickPathItemPathObject *obj = new QQuickPathItemPathObject(this); + obj->setV4Engine(QQmlEnginePrivate::get(qmlEngine(this))->v4engine()); + args->setReturnValue(obj->v4value()); +} + +void QQuickPathItem::newStrokeFillParams(QQmlV4Function *args) +{ + QQuickPathItemStrokeFillParamsObject *obj = new QQuickPathItemStrokeFillParamsObject(this); + obj->setV4Engine(QQmlEnginePrivate::get(qmlEngine(this))->v4engine()); + args->setReturnValue(obj->v4value()); +} + +void QQuickPathItem::clearVisualPaths(QQmlV4Function *args) +{ + Q_UNUSED(args); + Q_D(QQuickPathItem); + d->jsData.paths.clear(); + d->jsData.sfp.clear(); +} + +void QQuickPathItem::commitVisualPaths(QQmlV4Function *args) +{ + Q_UNUSED(args); + Q_D(QQuickPathItem); + d->_q_visualPathChanged(); +} + +void QQuickPathItem::appendVisualPath(QQmlV4Function *args) +{ + if (args->length() < 2) + return; + + Q_D(QQuickPathItem); + QV4::Scope scope(args->v4engine()); + QV4::Scoped jsp(scope, (*args)[0]); + QV4::Scoped jssfp(scope, (*args)[1]); + + const QQuickPathItemPath &path(jsp->d()->obj->path); + const QQuickPathItemStrokeFillParams &sfp(jssfp->d()->obj->sfp); + + d->jsData.paths.append(path); + d->jsData.sfp.append(sfp); +} + QT_END_NAMESPACE #include "moc_qquickpathitem_p.cpp" diff --git a/src/quick/items/qquickpathitem_p.h b/src/quick/items/qquickpathitem_p.h index 9ca1418d9e..991ab7a2bf 100644 --- a/src/quick/items/qquickpathitem_p.h +++ b/src/quick/items/qquickpathitem_p.h @@ -55,6 +55,7 @@ #include #include +#include #include QT_REQUIRE_CONFIG(quick_path); @@ -296,6 +297,12 @@ public: QQmlListProperty elements(); + Q_INVOKABLE void newPath(QQmlV4Function *args); + Q_INVOKABLE void newStrokeFillParams(QQmlV4Function *args); + Q_INVOKABLE void clearVisualPaths(QQmlV4Function *args); + Q_INVOKABLE void commitVisualPaths(QQmlV4Function *args); + Q_INVOKABLE void appendVisualPath(QQmlV4Function *args); + protected: QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override; void updatePolish() override; diff --git a/src/quick/items/qquickpathitem_p_p.h b/src/quick/items/qquickpathitem_p_p.h index 3c63ec6dc2..091a453c0b 100644 --- a/src/quick/items/qquickpathitem_p_p.h +++ b/src/quick/items/qquickpathitem_p_p.h @@ -62,6 +62,39 @@ QT_BEGIN_NAMESPACE class QSGPlainTexture; +struct QQuickPathItemPath +{ + enum Command { + MoveTo, + LineTo, + QuadTo, + CubicTo, + ArcTo + }; + + QVector cmd; + QVector coords; + + QPainterPath toPainterPath() const; +}; + +struct QQuickPathItemStrokeFillParams +{ + QQuickPathItemStrokeFillParams(); + + QColor strokeColor; + qreal strokeWidth; + QColor fillColor; + QQuickVisualPath::FillRule fillRule; + QQuickVisualPath::JoinStyle joinStyle; + int miterLimit; + QQuickVisualPath::CapStyle capStyle; + QQuickVisualPath::StrokeStyle strokeStyle; + qreal dashOffset; + QVector dashPattern; + QQuickPathGradient *fillGradient; +}; + class QQuickAbstractPathRenderer { public: @@ -74,7 +107,14 @@ public: // Gui thread virtual void beginSync(int totalCount) = 0; + virtual void endSync(bool async) = 0; + virtual void setAsyncCallback(void (*)(void *), void *) { } + virtual Flags flags() const { return 0; } + // - QML API virtual void setPath(int index, const QQuickPath *path) = 0; + // - JS API + virtual void setJSPath(int index, const QQuickPathItemPath &path) = 0; + // - stroke/fill parameters virtual void setStrokeColor(int index, const QColor &color) = 0; virtual void setStrokeWidth(int index, qreal w) = 0; virtual void setFillColor(int index, const QColor &color) = 0; @@ -84,9 +124,6 @@ public: virtual void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, qreal dashOffset, const QVector &dashPattern) = 0; virtual void setFillGradient(int index, QQuickPathGradient *gradient) = 0; - virtual void endSync(bool async) = 0; - virtual void setAsyncCallback(void (*)(void *), void *) { } - virtual Flags flags() const { return 0; } // Render thread, with gui blocked virtual void updateNode() = 0; @@ -121,17 +158,7 @@ public: QQuickPath *path; int dirty; - QColor strokeColor; - qreal strokeWidth; - QColor fillColor; - QQuickVisualPath::FillRule fillRule; - QQuickVisualPath::JoinStyle joinStyle; - int miterLimit; - QQuickVisualPath::CapStyle capStyle; - QQuickVisualPath::StrokeStyle strokeStyle; - qreal dashOffset; - QVector dashPattern; - QQuickPathGradient *fillGradient; + QQuickPathItemStrokeFillParams sfp; }; class QQuickPathItemPrivate : public QQuickItemPrivate @@ -157,7 +184,53 @@ public: bool async; QQuickPathItem::Status status; QQuickAbstractPathRenderer *renderer; - QVector vp; + + struct { + QVector vp; + } qmlData; + + struct { + bool isValid() const { Q_ASSERT(paths.count() == sfp.count()); return !paths.isEmpty(); } + QVector paths; + QVector sfp; + } jsData; +}; + +class QQuickPathItemPathObject : public QObject +{ + Q_OBJECT + +public: + QQuickPathItemPathObject(QObject *parent = nullptr) : QObject(parent) { } + + void setV4Engine(QV4::ExecutionEngine *engine); + QV4::ReturnedValue v4value() const { return m_v4value.value(); } + + QQuickPathItemPath path; + + void clear(); + +private: + QV4::PersistentValue m_v4value; +}; + +class QQuickPathItemStrokeFillParamsObject : public QObject +{ + Q_OBJECT + +public: + QQuickPathItemStrokeFillParamsObject(QObject *parent = nullptr) : QObject(parent) { } + + void setV4Engine(QV4::ExecutionEngine *engine); + QV4::ReturnedValue v4value() const { return m_v4value.value(); } + + QQuickPathItemStrokeFillParams sfp; + QV4::PersistentValue v4fillGradient; + + void clear(); + +private: + QV4::PersistentValue m_v4value; }; #ifndef QT_NO_OPENGL diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index 3de01a5bc7..a76a5e2b43 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -178,6 +178,13 @@ void QQuickPathItemGenericRenderer::setPath(int index, const QQuickPath *path) d.syncDirty |= DirtyFillGeom | DirtyStrokeGeom; } +void QQuickPathItemGenericRenderer::setJSPath(int index, const QQuickPathItemPath &path) +{ + VisualPathData &d(m_vp[index]); + d.path = path.toPainterPath(); + d.syncDirty |= DirtyFillGeom | DirtyStrokeGeom; +} + void QQuickPathItemGenericRenderer::setStrokeColor(int index, const QColor &color) { VisualPathData &d(m_vp[index]); diff --git a/src/quick/items/qquickpathitemgenericrenderer_p.h b/src/quick/items/qquickpathitemgenericrenderer_p.h index 045c52d610..70a9e88d2f 100644 --- a/src/quick/items/qquickpathitemgenericrenderer_p.h +++ b/src/quick/items/qquickpathitemgenericrenderer_p.h @@ -87,6 +87,7 @@ public: void beginSync(int totalCount) override; void setPath(int index, const QQuickPath *path) override; + void setJSPath(int index, const QQuickPathItemPath &path) override; void setStrokeColor(int index, const QColor &color) override; void setStrokeWidth(int index, qreal w) override; void setFillColor(int index, const QColor &color) override; diff --git a/src/quick/items/qquickpathitemnvprrenderer.cpp b/src/quick/items/qquickpathitemnvprrenderer.cpp index 13fab2dc76..f8504f9985 100644 --- a/src/quick/items/qquickpathitemnvprrenderer.cpp +++ b/src/quick/items/qquickpathitemnvprrenderer.cpp @@ -62,6 +62,14 @@ void QQuickPathItemNvprRenderer::setPath(int index, const QQuickPath *path) m_accDirty |= DirtyPath; } +void QQuickPathItemNvprRenderer::setJSPath(int index, const QQuickPathItemPath &path) +{ + VisualPathGuiData &d(m_vp[index]); + convertJSPath(path, &d); + d.dirty |= DirtyPath; + m_accDirty |= DirtyPath; +} + void QQuickPathItemNvprRenderer::setStrokeColor(int index, const QColor &color) { VisualPathGuiData &d(m_vp[index]); @@ -288,6 +296,82 @@ void QQuickPathItemNvprRenderer::convertPath(const QQuickPath *path, VisualPathG d->path.cmd.append(GL_CLOSE_PATH_NV); } +void QQuickPathItemNvprRenderer::convertJSPath(const QQuickPathItemPath &path, VisualPathGuiData *d) +{ + d->path = NvprPath(); + if (path.cmd.isEmpty()) + return; + + QPointF startPos(0, 0); + QPointF pos(startPos); + int coordIdx = 0; + + for (QQuickPathItemPath::Command cmd : path.cmd) { + switch (cmd) { + case QQuickPathItemPath::MoveTo: + d->path.cmd.append(GL_MOVE_TO_NV); + pos = QPointF(path.coords[coordIdx], path.coords[coordIdx + 1]); + startPos = pos; + d->path.coord.append(pos.x()); + d->path.coord.append(pos.y()); + coordIdx += 2; + break; + case QQuickPathItemPath::LineTo: + d->path.cmd.append(GL_LINE_TO_NV); + pos = QPointF(path.coords[coordIdx], path.coords[coordIdx + 1]); + d->path.coord.append(pos.x()); + d->path.coord.append(pos.y()); + coordIdx += 2; + break; + case QQuickPathItemPath::QuadTo: + d->path.cmd.append(GL_QUADRATIC_CURVE_TO_NV); + d->path.coord.append(path.coords[coordIdx]); + d->path.coord.append(path.coords[coordIdx + 1]); + pos = QPointF(path.coords[coordIdx + 2], path.coords[coordIdx + 3]); + d->path.coord.append(pos.x()); + d->path.coord.append(pos.y()); + coordIdx += 4; + break; + case QQuickPathItemPath::CubicTo: + d->path.cmd.append(GL_CUBIC_CURVE_TO_NV); + d->path.coord.append(path.coords[coordIdx]); + d->path.coord.append(path.coords[coordIdx + 1]); + d->path.coord.append(path.coords[coordIdx + 2]); + d->path.coord.append(path.coords[coordIdx + 3]); + pos = QPointF(path.coords[coordIdx + 4], path.coords[coordIdx + 5]); + d->path.coord.append(pos.x()); + d->path.coord.append(pos.y()); + coordIdx += 6; + break; + case QQuickPathItemPath::ArcTo: + { + const bool sweepFlag = !qFuzzyIsNull(path.coords[coordIdx + 5]); + const bool useLargeArc = !qFuzzyIsNull(path.coords[coordIdx + 6]); + GLenum cmd; + if (useLargeArc) + cmd = sweepFlag ? GL_LARGE_CCW_ARC_TO_NV : GL_LARGE_CW_ARC_TO_NV; + else + cmd = sweepFlag ? GL_SMALL_CCW_ARC_TO_NV : GL_SMALL_CW_ARC_TO_NV; + d->path.cmd.append(cmd); + d->path.coord.append(path.coords[coordIdx]); // rx + d->path.coord.append(path.coords[coordIdx + 1]); // ry + d->path.coord.append(path.coords[coordIdx + 2]); // xrot + pos = QPointF(path.coords[coordIdx + 3], path.coords[coordIdx + 4]); + d->path.coord.append(pos.x()); + d->path.coord.append(pos.y()); + coordIdx += 7; + } + break; + default: + qWarning("Unknown JS path command: %d", cmd); + break; + } + } + + if (pos == startPos) + d->path.cmd.append(GL_CLOSE_PATH_NV); +} + static inline QVector4D qsg_premultiply(const QColor &c, float globalOpacity) { const float o = c.alphaF() * globalOpacity; diff --git a/src/quick/items/qquickpathitemnvprrenderer_p.h b/src/quick/items/qquickpathitemnvprrenderer_p.h index 61f8b5ebb9..deab9cf7f9 100644 --- a/src/quick/items/qquickpathitemnvprrenderer_p.h +++ b/src/quick/items/qquickpathitemnvprrenderer_p.h @@ -81,6 +81,7 @@ public: void beginSync(int totalCount) override; void setPath(int index, const QQuickPath *path) override; + void setJSPath(int index, const QQuickPathItemPath &path) override; void setStrokeColor(int index, const QColor &color) override; void setStrokeWidth(int index, qreal w) override; void setFillColor(int index, const QColor &color) override; @@ -121,6 +122,7 @@ private: }; void convertPath(const QQuickPath *path, VisualPathGuiData *d); + void convertJSPath(const QQuickPathItemPath &path, VisualPathGuiData *d); QQuickPathItemNvprRenderNode *m_node = nullptr; int m_accDirty = 0; diff --git a/src/quick/items/qquickpathitemsoftwarerenderer.cpp b/src/quick/items/qquickpathitemsoftwarerenderer.cpp index 46ebcbfe6d..b7aa93bf65 100644 --- a/src/quick/items/qquickpathitemsoftwarerenderer.cpp +++ b/src/quick/items/qquickpathitemsoftwarerenderer.cpp @@ -58,6 +58,14 @@ void QQuickPathItemSoftwareRenderer::setPath(int index, const QQuickPath *path) m_accDirty |= DirtyPath; } +void QQuickPathItemSoftwareRenderer::setJSPath(int index, const QQuickPathItemPath &path) +{ + VisualPathGuiData &d(m_vp[index]); + d.path = path.toPainterPath(); + d.dirty |= DirtyPath; + m_accDirty |= DirtyPath; +} + void QQuickPathItemSoftwareRenderer::setStrokeColor(int index, const QColor &color) { VisualPathGuiData &d(m_vp[index]); diff --git a/src/quick/items/qquickpathitemsoftwarerenderer_p.h b/src/quick/items/qquickpathitemsoftwarerenderer_p.h index 64280b436e..e76590bdfe 100644 --- a/src/quick/items/qquickpathitemsoftwarerenderer_p.h +++ b/src/quick/items/qquickpathitemsoftwarerenderer_p.h @@ -73,6 +73,7 @@ public: void beginSync(int totalCount) override; void setPath(int index, const QQuickPath *path) override; + void setJSPath(int index, const QQuickPathItemPath &path) override; void setStrokeColor(int index, const QColor &color) override; void setStrokeWidth(int index, qreal w) override; void setFillColor(int index, const QColor &color) override; -- cgit v1.2.3 From d55f0433c102dbdbd0b69c8ddbc397a0854d86a0 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Fri, 17 Mar 2017 14:18:40 +0100 Subject: Fix for disappearing VisualPath entries in generic PathItem Prevent internal overflows in tesselation due to 100x scale and keep the correct scenegraph node - VisualPath entry associations. Change-Id: I181eae6872ea5c5af5783b68a8757a5249c074e5 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemgenericrenderer.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index a76a5e2b43..dfa01f4d42 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -51,7 +51,7 @@ QT_BEGIN_NAMESPACE -static const qreal SCALE = 100; +static const qreal TRI_SCALE = 1; struct ColoredVertex // must match QSGGeometry::ColoredPoint2D { @@ -410,13 +410,13 @@ void QQuickPathItemGenericRenderer::triangulateFill(const QPainterPath &path, { const QVectorPath &vp = qtVectorPathForPath(path); - QTriangleSet ts = qTriangulate(vp, QTransform::fromScale(SCALE, SCALE), 1, supportsElementIndexUint); + QTriangleSet ts = qTriangulate(vp, QTransform::fromScale(TRI_SCALE, TRI_SCALE), 1, supportsElementIndexUint); const int vertexCount = ts.vertices.count() / 2; // just a qreal vector with x,y hence the / 2 fillVertices->resize(vertexCount); ColoredVertex *vdst = reinterpret_cast(fillVertices->data()); const qreal *vsrc = ts.vertices.constData(); for (int i = 0; i < vertexCount; ++i) - vdst[i].set(vsrc[i * 2] / SCALE, vsrc[i * 2 + 1] / SCALE, fillColor); + vdst[i].set(vsrc[i * 2] / TRI_SCALE, vsrc[i * 2 + 1] / TRI_SCALE, fillColor); size_t indexByteSize; if (ts.indices.type() == QVertexIndexVector::UnsignedShort) { @@ -441,7 +441,7 @@ void QQuickPathItemGenericRenderer::triangulateStroke(const QPainterPath &path, { const QVectorPath &vp = qtVectorPathForPath(path); const QRectF clip(QPointF(0, 0), clipSize); - const qreal inverseScale = 1.0 / SCALE; + const qreal inverseScale = 1.0 / TRI_SCALE; QTriangulatingStroker stroker; stroker.setInvScale(inverseScale); @@ -508,8 +508,12 @@ void QQuickPathItemGenericRenderer::updateNode() if (m_accDirty & DirtyList) d.effectiveDirty |= DirtyFillGeom | DirtyStrokeGeom | DirtyColor | DirtyFillGradient; - if (!d.effectiveDirty) + + if (!d.effectiveDirty) { + prevNode = node; + nodePtr = &node->m_next; continue; + } if (d.fillColor.a == 0) { delete node->m_fillNode; -- cgit v1.2.3 From 55970b212fff45161f94ffe637b49473e8f5925b Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Mon, 27 Mar 2017 13:29:04 +0200 Subject: Fix async PathItem results not always showing up Change-Id: Ie8eb1dd5ea8a560f8160384637cc5d76e3bb7c60 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemgenericrenderer.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index dfa01f4d42..7a259617dd 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -642,6 +642,12 @@ void QQuickPathItemGenericRenderer::updateStrokeNode(VisualPathData *d, QQuickPa n->markDirty(QSGNode::DirtyGeometry); + // Async loading runs update once, bails out above, then updates again once + // ready. Set the material dirty then. This is in-line with fill where the + // first activateMaterial() achieves the same. + if (!g->vertexCount()) + n->markDirty(QSGNode::DirtyMaterial); + if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyStrokeGeom)) { ColoredVertex *vdst = reinterpret_cast(g->vertexData()); for (int i = 0; i < g->vertexCount(); ++i) -- cgit v1.2.3 From 83fc08cc6faa5f52a010d7bd821c9606f13d5ae9 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Mon, 27 Mar 2017 15:37:29 +0200 Subject: PathItem docs for the declarative API Also add the missing property for disabling vendor extensions like NVPR. Change-Id: Id7dfe245305c8ecdfad87359894e7f496c4100d0 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitem.cpp | 457 +++++++++++++++++++++++++++++++++-- src/quick/items/qquickpathitem_p.h | 5 + src/quick/items/qquickpathitem_p_p.h | 2 + 3 files changed, 447 insertions(+), 17 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index 61216be9b0..b14fc1caba 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -114,6 +114,60 @@ QPainterPath QQuickPathItemPath::toPainterPath() const return p; } +/*! + \qmltype VisualPath + \instantiates QQuickVisualPath + \inqmlmodule QtQuick + \ingroup qtquick-paths + \ingroup qtquick-views + \inherits Object + \brief Describes a Path and associated properties for stroking and filling + \since 5.10 + + A PathItem contains one or more VisualPath elements. At least one + VisualPath is necessary in order to have a PathItem output anything + visible. A VisualPath in turn contains a Path and properties describing the + stroking and filling parameters, such as the stroke width and color, the + fill color or gradient, join and cap styles, and so on. Finally, the Path + object contains a list of path elements like PathMove, PathLine, PathCubic, + PathQuad, PathArc. + + Any property changes in these data sets will be bubble up and change the + output of the PathItem. This means that it is simple and easy to change, or + even animate, the starting and ending position, control points, or any + stroke or fill parameters using the usual QML bindings and animation types + like NumberAnimation. + + In the following example the line join style changes automatically based on + the value of joinStyleIndex: + + \code + VisualPath { + strokeColor: "black" + strokeWidth: 16 + fillColor: "transparent" + capStyle: VisualPath.RoundCap + + property int joinStyleIndex: 0 + property variant styles: [ VisualPath.BevelJoin, VisualPath.MiterJoin, VisualPath.RoundJoin ] + + joinStyle: styles[joinStyleIndex] + + Path { + startX: 30 + startY: 30 + PathLine { x: 100; y: 100 } + PathLine { x: 30; y: 100 } + } + } + \endcode + + Once associated with a PathItem, here is the output with a joinStyleIndex + of 2 (VisualPath.RoundJoin): + + \image visualpath-code-example.png + */ + QQuickVisualPathPrivate::QQuickVisualPathPrivate() : path(nullptr), dirty(DirtyAll) @@ -129,6 +183,14 @@ QQuickVisualPath::~QQuickVisualPath() { } +/*! + \qmlproperty Path QtQuick::VisualPath::path + + This property holds the Path object. + + \default + */ + QQuickPath *QQuickVisualPath::path() const { Q_D(const QQuickVisualPath); @@ -160,6 +222,16 @@ void QQuickVisualPathPrivate::_q_pathChanged() emit q->changed(); } +/*! + \qmlproperty color QtQuick::VisualPath::strokeColor + + This property holds the stroking color. + + When set to \c transparent, no stroking occurs. + + The default value is \c white. + */ + QColor QQuickVisualPath::strokeColor() const { Q_D(const QQuickVisualPath); @@ -177,6 +249,16 @@ void QQuickVisualPath::setStrokeColor(const QColor &color) } } +/*! + \qmlproperty color QtQuick::VisualPath::strokeWidth + + This property holds the stroke width. + + When set to a negative value, no stroking occurs. + + The default value is 1. + */ + qreal QQuickVisualPath::strokeWidth() const { Q_D(const QQuickVisualPath); @@ -194,6 +276,16 @@ void QQuickVisualPath::setStrokeWidth(qreal w) } } +/*! + \qmlproperty color QtQuick::VisualPath::fillColor + + This property holds the fill color. + + When set to \c transparent, no filling occurs. + + The default value is \c white. + */ + QColor QQuickVisualPath::fillColor() const { Q_D(const QQuickVisualPath); @@ -211,6 +303,19 @@ void QQuickVisualPath::setFillColor(const QColor &color) } } +/*! + \qmlproperty enumeration QtQuick::VisualPath::fillRule + + This property holds the fill rule. The default value is + VisualPath.OddEvenFill. For an example on fill rules, see + QPainterPath::setFillRule(). + + \list + \li VisualPath.OddEvenFill + \li VisualPath.WindingFill + \endlist + */ + QQuickVisualPath::FillRule QQuickVisualPath::fillRule() const { Q_D(const QQuickVisualPath); @@ -228,6 +333,19 @@ void QQuickVisualPath::setFillRule(FillRule fillRule) } } +/*! + \qmlproperty enumeration QtQuick::VisualPath::joinStyle + + This property defines how joins between two connected lines are drawn. The + default value is VisualPath.BevelJoin. + + \list + \li VisualPath.MiterJoin - The outer edges of the lines are extended to meet at an angle, and this area is filled. + \li VisualPath.BevelJoin - The triangular notch between the two lines is filled. + \li VisualPath.RoundJoin - A circular arc between the two lines is filled. + \endlist + */ + QQuickVisualPath::JoinStyle QQuickVisualPath::joinStyle() const { Q_D(const QQuickVisualPath); @@ -245,6 +363,15 @@ void QQuickVisualPath::setJoinStyle(JoinStyle style) } } +/*! + \qmlproperty int QtQuick::VisualPath::miterLimit + + When VisualPath.joinStyle is set to VisualPath.MiterJoin, this property + specifies how far the miter join can extend from the join point. + + The default value is 2. + */ + int QQuickVisualPath::miterLimit() const { Q_D(const QQuickVisualPath); @@ -262,6 +389,19 @@ void QQuickVisualPath::setMiterLimit(int limit) } } +/*! + \qmlproperty enumeration QtQuick::VisualPath::capStyle + + This property defines how the end points of lines are drawn. The + default value is VisualPath.SquareCap. + + \list + \li VisualPath.FlatCap - A square line end that does not cover the end point of the line. + \li VisualPath.SquareCap - A square line end that covers the end point and extends beyond it by half the line width. + \li VisualPath.RoundCap - A rounded line end. + \endlist + */ + QQuickVisualPath::CapStyle QQuickVisualPath::capStyle() const { Q_D(const QQuickVisualPath); @@ -279,6 +419,18 @@ void QQuickVisualPath::setCapStyle(CapStyle style) } } +/*! + \qmlproperty enumeration QtQuick::VisualPath::strokeStyle + + This property defines the style of stroking. The default value is + VisualPath.SolidLine. + + \list + \li VisualPath.SolidLine - A plain line. + \li VisualPath.DashLine - Dashes separated by a few pixels. + \endlist + */ + QQuickVisualPath::StrokeStyle QQuickVisualPath::strokeStyle() const { Q_D(const QQuickVisualPath); @@ -296,6 +448,17 @@ void QQuickVisualPath::setStrokeStyle(StrokeStyle style) } } +/*! + \qmlproperty real QtQuick::VisualPath::dashOffset + + This property defines the starting point on the dash pattern, measured in + units used to specify the dash pattern. + + The default value is 0. + + \sa QPen::setDashOffset() + */ + qreal QQuickVisualPath::dashOffset() const { Q_D(const QQuickVisualPath); @@ -313,6 +476,20 @@ void QQuickVisualPath::setDashOffset(qreal offset) } } +/*! + \qmlproperty list QtQuick::VisualPath::dashPattern + + This property defines the dash pattern when VisualPath.strokeStyle is set + to VisualPath.DashLine. The pattern must be specified as an even number of + positive entries where the entries 1, 3, 5... are the dashes and 2, 4, 6... + are the spaces. The pattern is specified in units of the pen's width. + + The default value is (4, 2), meaning a dash of 4 * VisualPath.strokeWidth + pixels followed by a space of 2 * VisualPath.strokeWidth pixels. + + \sa QPen::setDashPattern() + */ + QVector QQuickVisualPath::dashPattern() const { Q_D(const QQuickVisualPath); @@ -330,6 +507,17 @@ void QQuickVisualPath::setDashPattern(const QVector &array) } } +/*! + \qmlproperty PathGradient QtQuick::VisualPath::fillGradient + + This property defines the fill gradient. By default no gradient is enabled + and the value is \c null. In this case the fill uses a solid color based on + the value of VisuaLPath.fillColor. + + When set, VisualPath.fillColor is ignored and filling is done using one of + the PathGradient subtypes. + */ + QQuickPathGradient *QQuickVisualPath::fillGradient() const { Q_D(const QQuickVisualPath); @@ -375,15 +563,56 @@ void QQuickVisualPath::resetFillGradient() \since 5.10 Renders a path either by generating geometry via QPainterPath and manual - triangulation or by using an extension like \c{GL_NV_path_rendering}. + triangulation or by using a GPU vendor extension like \c{GL_NV_path_rendering}. This approach is different from rendering shapes via QQuickPaintedItem or the 2D Canvas because the path never gets rasterized in software. Therefore - it is suitable for creating shapes spreading over larger areas of the + PathItem is suitable for creating shapes spreading over larger areas of the screen, avoiding the performance penalty for texture uploads or framebuffer - blits. + blits. In addition, the declarative API allows manipulating, binding to, + and even animating the path element properties like starting and ending + position, the control points, etc. + + The types for specifying path elements are shared between \l PathView and + PathItem. However, not all PathItem implementations support all path + element types, while some may not make sense for PathView. PathItem's + currently supported subset is: PathMove, PathLine, PathQuad, PathCubic, + PathArc, PathSvg. + + See \l Path for a detailed overview of the supported path elements. + + \code + PathItem { + width: 200 + height: 150 + anchors.centerIn: parent + VisualPath { + strokeWidth: 4 + strokeColor: "red" + fillGradient: PathLinearGradient { + x1: 20; y1: 20 + x2: 180; y2: 130 + PathGradientStop { position: 0; color: "blue" } + PathGradientStop { position: 0.2; color: "green" } + PathGradientStop { position: 0.4; color: "red" } + PathGradientStop { position: 0.6; color: "yellow" } + PathGradientStop { position: 1; color: "cyan" } + } + strokeStyle: VisualPath.DashLine + dashPattern: [ 1, 4 ] + Path { + startX: 20; startY: 20 + PathLine { x: 180; y: 130 } + PathLine { x: 20; y: 130 } + PathLine { x: 20; y: 20 } + } + } + } + \endcode + + \image pathitem-code-example.png - Nonetheless it is important to be aware of performance implications, in + \note It is important to be aware of performance implications, in particular when the application is running on the generic PathItem implementation due to not having support for accelerated path rendering. The geometry generation happens entirely on the CPU in this case, and this @@ -396,18 +625,25 @@ void QQuickVisualPath::resetFillGradient() \c{GL_NV_path_rendering} where the cost of path property changes is much smaller. - \note The types for specifying path elements are shared between \l PathView - and PathItem. However, not all PathItem implementations support all path - element types, while some may not make sense for PathView. PathItem's - currently supported subset is: PathMove, PathLine, PathQuad, PathCubic, - PathArc. + The following list summarizes the available PathItem rendering approaches: - \note Limited support for PathSvg is also provided in most cases. However, - there is no guarantee that this element is going to be supported for all - future PathItem backends. It is recommended to avoid the PathSvg element in - practice. + \list - See \l Path for a detailed overview of the supported path elements. + \li When running with the default, OpenGL backend of Qt Quick, both the + generic, triangulation-based and the NVIDIA-specific + \c{GL_NV_path_rendering} methods are available. The choice is made at + runtime, depending on the graphics driver's capabilities. When this is not + desired, applications can force using the generic method by setting the + PathItem.enableVendorExtensions property to \c false. + + \li The \c software backend is fully supported. The path is rendered via + QPainter::strokePath() and QPainter::fillPath() in this case. + + \li The Direct 3D 12 backend is not currently supported. + + \li The OpenVG backend is not currently supported. + + \endlist \sa Path, PathMove, PathLine, PathQuad, PathCubic, PathArc, PathSvg */ @@ -418,7 +654,8 @@ QQuickPathItemPrivate::QQuickPathItemPrivate() rendererType(QQuickPathItem::UnknownRenderer), async(false), status(QQuickPathItem::Null), - renderer(nullptr) + renderer(nullptr), + enableVendorExts(true) { } @@ -453,12 +690,54 @@ QQuickPathItem::~QQuickPathItem() { } +/*! + \qmlproperty enumeration QtQuick::PathItem::rendererType + + This property determines which path rendering backend is active. + + \list + + \li PathItem.UnknownRenderer - The renderer is unknown. + + \li PathItem.GeometryRenderer - The generic, driver independent solution + for OpenGL. Uses the same CPU-based triangulation approach as QPainter's + OpenGL 2 paint engine. This is the default on non-NVIDIA hardware when the + default, OpenGL Qt Quick scenegraph backend is in use. + + \li PathItem.NvprRenderer - Path items are rendered by performing OpenGL + calls using the \c{GL_NV_path_rendering} extension. This is the default on + NVIDIA hardware when the default, OpenGL Qt Quick scenegraph backend is in + use. + + \li PathItem.SoftwareRenderer - Pure QPainter drawing using the raster + paint engine. This is the default, and only, option when the Qt Quick + scenegraph is running with the \c software backend. + + \endlist +*/ + QQuickPathItem::RendererType QQuickPathItem::rendererType() const { Q_D(const QQuickPathItem); return d->rendererType; } +/*! + \qmlproperty bool QtQuick::PathItem::asynchronous + + When PathItem.rendererType is PathItem.GeometryRenderer, the input path is + triangulated on the CPU during the polishing phase of the PathItem. This is + potentially expensive. To offload this work to separate worker threads, set + this property to \c true. + + When enabled, making a PathItem visible will not wait for the content to + become available. Instead, the gui/main thread is not blocked and the + results of the path rendering are shown only when all the asynchronous work + has been finished. + + The default value is \c false. + */ + bool QQuickPathItem::asynchronous() const { Q_D(const QQuickPathItem); @@ -476,6 +755,49 @@ void QQuickPathItem::setAsynchronous(bool async) } } +/*! + \qmlproperty bool QtQuick::PathItem::enableVendorExtensions + + This property controls the usage of non-standard OpenGL extensions like + GL_NV_path_rendering. To disable PathItem.NvprRenderer and force a uniform + behavior regardless of the graphics card and drivers, set this property to + \c false. + + The default value is \c true. + */ + +bool QQuickPathItem::enableVendorExtensions() const +{ + Q_D(const QQuickPathItem); + return d->enableVendorExts; +} + +void QQuickPathItem::setEnableVendorExtensions(bool enable) +{ + Q_D(QQuickPathItem); + if (d->enableVendorExts != enable) { + d->enableVendorExts = enable; + emit enableVendorExtensionsChanged(); + } +} + +/*! + \qmlproperty enumeration QtQuick::PathItem::status + + This property determines the status of the PathItem and is relevant when + PathItem.asynchronous is set to \c true. + + \list + + \li PathItem.Null - Not yet initialized. + + \li PathItem.Ready - The PathItem has finished processing. + + \li PathItem.Processing - The path is being processed. + + \endlist + */ + QQuickPathItem::Status QQuickPathItem::status() const { Q_D(const QQuickPathItem); @@ -520,6 +842,15 @@ static void vpe_clear(QQmlListProperty *property) d->_q_visualPathChanged(); } +/*! + \qmlproperty list QtQuick::PathItem::elements + + This property holds the VisualPath objects that define the contents of the + PathItem. + + \default + */ + QQmlListProperty QQuickPathItem::elements() { return QQmlListProperty(this, @@ -608,7 +939,7 @@ void QQuickPathItemPrivate::createRenderer() switch (ri->graphicsApi()) { #ifndef QT_NO_OPENGL case QSGRendererInterface::OpenGL: - if (QQuickPathItemNvprRenderNode::isSupported()) { + if (enableVendorExts && QQuickPathItemNvprRenderNode::isSupported()) { rendererType = QQuickPathItem::NvprRenderer; renderer = new QQuickPathItemNvprRenderer; } else { @@ -641,7 +972,7 @@ QSGNode *QQuickPathItemPrivate::createNode() switch (ri->graphicsApi()) { #ifndef QT_NO_OPENGL case QSGRendererInterface::OpenGL: - if (QQuickPathItemNvprRenderNode::isSupported()) { + if (enableVendorExts && QQuickPathItemNvprRenderNode::isSupported()) { node = new QQuickPathItemNvprRenderNode; static_cast(renderer)->setNode( static_cast(node)); @@ -740,6 +1071,17 @@ void QQuickPathItemPrivate::sync() // ***** gradient support ***** +/*! + \qmltype PathGradientStop + \instantiates QQuickPathGradientStop + \inqmlmodule QtQuick + \ingroup qtquick-paths + \ingroup qtquick-views + \inherits Object + \brief Defines a color at a position in a gradient + \since 5.10 + */ + QQuickPathGradientStop::QQuickPathGradientStop(QObject *parent) : QObject(parent), m_position(0), @@ -747,6 +1089,15 @@ QQuickPathGradientStop::QQuickPathGradientStop(QObject *parent) { } +/*! + \qmlproperty real QtQuick::PathGradientStop::position + + The position and color properties describe the color used at a given + position in a gradient, as represented by a gradient stop. + + The default value is 0. + */ + qreal QQuickPathGradientStop::position() const { return m_position; @@ -761,6 +1112,15 @@ void QQuickPathGradientStop::setPosition(qreal position) } } +/*! + \qmlproperty real QtQuick::PathGradientStop::color + + The position and color properties describe the color used at a given + position in a gradient, as represented by a gradient stop. + + The default value is \c black. + */ + QColor QQuickPathGradientStop::color() const { return m_color; @@ -775,6 +1135,20 @@ void QQuickPathGradientStop::setColor(const QColor &color) } } +/*! + \qmltype PathGradient + \instantiates QQuickPathGradient + \inqmlmodule QtQuick + \ingroup qtquick-paths + \ingroup qtquick-views + \inherits Object + \brief Base type of PathItem fill gradients + \since 5.10 + + This is an abstract base class for gradients like PathLinearGradient and + cannot be created directly. + */ + QQuickPathGradient::QQuickPathGradient(QObject *parent) : QObject(parent), m_spread(PadSpread) @@ -794,6 +1168,14 @@ void QQuickPathGradient::appendStop(QQmlListProperty *list, QObject *st grad->m_stops.append(sstop); } +/*! + \qmlproperty list QtQuick::PathGradient::stops + \default + + The list of PathGradientStop objects defining the colors at given positions + in the gradient. + */ + QQmlListProperty QQuickPathGradient::stops() { return QQmlListProperty(this, nullptr, &QQuickPathGradient::appendStop, nullptr, nullptr, nullptr); @@ -812,6 +1194,19 @@ QGradientStops QQuickPathGradient::sortedGradientStops() const return result; } +/*! + \qmlproperty enumeration QtQuick::PathGradient::spred + + Specifies how the area outside the gradient area should be filled. The + default value is PathGradient.PadSpread. + + \list + \li PathGradient.PadSpread - The area is filled with the closest stop color. + \li PathGradient.RepeatSpread - The gradient is repeated outside the gradient area. + \li PathGradient.ReflectSpread - The gradient is reflected outside the gradient area. + \endlist + */ + QQuickPathGradient::SpreadMode QQuickPathGradient::spread() const { return m_spread; @@ -826,11 +1221,39 @@ void QQuickPathGradient::setSpread(SpreadMode mode) } } +/*! + \qmltype PathLinearGradient + \instantiates QQuickPathLinearGradient + \inqmlmodule QtQuick + \ingroup qtquick-paths + \ingroup qtquick-views + \inherits PathGradient + \brief Linear gradient + \since 5.10 + + Linear gradients interpolate colors between start and end points. Outside + these points the gradient is either padded, reflected or repeated depending + on the spread type. + + \sa QLinearGradient + */ + QQuickPathLinearGradient::QQuickPathLinearGradient(QObject *parent) : QQuickPathGradient(parent) { } +/*! + \qmlproperty real QtQuick::PathLinearGradient::x1 + \qmlproperty real QtQuick::PathLinearGradient::y1 + \qmlproperty real QtQuick::PathLinearGradient::x2 + \qmlproperty real QtQuick::PathLinearGradient::y2 + + These properties define the start and end points between which color + interpolation occurs. By default both the stard and end points are set to + (0, 0). + */ + qreal QQuickPathLinearGradient::x1() const { return m_start.x(); diff --git a/src/quick/items/qquickpathitem_p.h b/src/quick/items/qquickpathitem_p.h index 991ab7a2bf..6d789aadbc 100644 --- a/src/quick/items/qquickpathitem_p.h +++ b/src/quick/items/qquickpathitem_p.h @@ -265,6 +265,7 @@ class Q_QUICK_PRIVATE_EXPORT QQuickPathItem : public QQuickItem Q_OBJECT Q_PROPERTY(RendererType renderer READ rendererType NOTIFY rendererChanged) Q_PROPERTY(bool asynchronous READ asynchronous WRITE setAsynchronous NOTIFY asynchronousChanged) + Q_PROPERTY(bool enableVendorExtensions READ enableVendorExtensions WRITE setEnableVendorExtensions NOTIFY enableVendorExtensionsChanged) Q_PROPERTY(Status status READ status NOTIFY statusChanged) Q_PROPERTY(QQmlListProperty elements READ elements) Q_CLASSINFO("DefaultProperty", "elements") @@ -293,6 +294,9 @@ public: bool asynchronous() const; void setAsynchronous(bool async); + bool enableVendorExtensions() const; + void setEnableVendorExtensions(bool enable); + Status status() const; QQmlListProperty elements(); @@ -313,6 +317,7 @@ protected: Q_SIGNALS: void rendererChanged(); void asynchronousChanged(); + void enableVendorExtensionsChanged(); void statusChanged(); private: diff --git a/src/quick/items/qquickpathitem_p_p.h b/src/quick/items/qquickpathitem_p_p.h index 091a453c0b..c9a2904a25 100644 --- a/src/quick/items/qquickpathitem_p_p.h +++ b/src/quick/items/qquickpathitem_p_p.h @@ -194,6 +194,8 @@ public: QVector paths; QVector sfp; } jsData; + + bool enableVendorExts; }; class QQuickPathItemPathObject : public QObject -- cgit v1.2.3 From 4cddb73882ed950f652fd1e079bc4cf8ccde93d5 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 28 Mar 2017 13:19:29 +0200 Subject: Add a PathItem autotest for the declarative API Change-Id: I276c185c93122e5eb05ef6678ab62fa6928f2523 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitem.cpp | 20 +++++++++++++++++++- src/quick/items/qquickpathitem_p.h | 2 ++ 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index b14fc1caba..fff666c205 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -1155,6 +1155,20 @@ QQuickPathGradient::QQuickPathGradient(QObject *parent) { } +int QQuickPathGradient::countStops(QQmlListProperty *list) +{ + QQuickPathGradient *grad = qobject_cast(list->object); + Q_ASSERT(grad); + return grad->m_stops.count(); +} + +QObject *QQuickPathGradient::atStop(QQmlListProperty *list, int index) +{ + QQuickPathGradient *grad = qobject_cast(list->object); + Q_ASSERT(grad); + return grad->m_stops.at(index); +} + void QQuickPathGradient::appendStop(QQmlListProperty *list, QObject *stop) { QQuickPathGradientStop *sstop = qobject_cast(stop); @@ -1178,7 +1192,11 @@ void QQuickPathGradient::appendStop(QQmlListProperty *list, QObject *st QQmlListProperty QQuickPathGradient::stops() { - return QQmlListProperty(this, nullptr, &QQuickPathGradient::appendStop, nullptr, nullptr, nullptr); + return QQmlListProperty(this, nullptr, + &QQuickPathGradient::appendStop, + &QQuickPathGradient::countStops, + &QQuickPathGradient::atStop, + nullptr); } QGradientStops QQuickPathGradient::sortedGradientStops() const diff --git a/src/quick/items/qquickpathitem_p.h b/src/quick/items/qquickpathitem_p.h index 6d789aadbc..37b23dee6f 100644 --- a/src/quick/items/qquickpathitem_p.h +++ b/src/quick/items/qquickpathitem_p.h @@ -114,6 +114,8 @@ signals: void spreadChanged(); private: + static int countStops(QQmlListProperty *list); + static QObject *atStop(QQmlListProperty *list, int index); static void appendStop(QQmlListProperty *list, QObject *stop); QVector m_stops; -- cgit v1.2.3 From 4f2c5aa8970958b333998ad841d9162a5bfee644 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 29 Mar 2017 13:37:56 +0200 Subject: PathItem/generic: Fix spread modes in gradient material Not including the mode in the comparison makes the scenegraph think that two Pathitems with two linear gradients that only differ in the spread mode can reuse the same material. That would be wrong. Change-Id: I34ad1b74ec8f98ec3a11d6a2565e0a7e5ae3673a Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitemgenericrenderer.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp index 7a259617dd..4e8fe55df2 100644 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ b/src/quick/items/qquickpathitemgenericrenderer.cpp @@ -744,6 +744,10 @@ int QQuickPathItemLinearGradientMaterial::compare(const QSGMaterial *other) cons const QQuickPathItemGradientCache::GradientDesc *ga = &a->m_fillGradient; const QQuickPathItemGradientCache::GradientDesc *gb = &b->m_fillGradient; + + if (int d = ga->spread - gb->spread) + return d; + if (int d = ga->start.x() - gb->start.x()) return d; if (int d = ga->start.y() - gb->start.y()) -- cgit v1.2.3 From 349d3400c11c0ad1c9aaec01c44b174dbb6ebf9a Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 29 Mar 2017 14:17:51 +0200 Subject: PathItem: add some docs for the JS API Change-Id: I25a1e14e755350350f9a37ab0ac711576c25f535 Reviewed-by: Andy Nichols --- src/quick/items/qquickpathitem.cpp | 211 +++++++++++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp index fff666c205..fae16064e5 100644 --- a/src/quick/items/qquickpathitem.cpp +++ b/src/quick/items/qquickpathitem.cpp @@ -1973,11 +1973,142 @@ void QQuickPathItemPathObject::setV4Engine(QV4::ExecutionEngine *engine) m_v4value = wrapper; } +/*! + \qmltype JSPath + \inqmlmodule QtQuick + \ingroup qtquick-path + \brief Describes a path via a JavaScript API + */ + +/*! + \qmlmethod void QtQuick::JSPath::moveTo(x, y) + + Moves the path's position to the absolute position specified by (\a x, \a y). + */ + +/*! + \qmlmethod void QtQuick::JSPath::lineTo(x, y) + + Defines a straight line to the absolute position specified by (\a x, \a y). + */ + +/*! + \qmlmethod void QtQuick::JSPath::quadTo(cx, cy, x, y) + + Defines a quadratic Bezier curve with a control point (\a cx, \a cy) and an + end point of (\a x, \a y). + */ + +/*! + \qmlmethod void QtQuick::JSPath::cubicTo(c1x, c1y, c2x, c2y, x, y) + + Defines a cubic Bezier curve with two control points (\a c1x, \a c1y) and + (\a c2x, \a c2y), and an end point of (\a x, \a y). + */ + +/*! + \qmlmethod void QtQuick::JSPath::arcTo(radiusX, radiusY, xAxisRotation, x, y, sweepFlag, largeArc) + + Defines an elliptical arc, following the elliptical arc command in SVG. See + \l{https://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands}{the + SVG path specification} for details on the parameters. + */ + +/*! + \qmlmethod void QtQuick::JSPath::clear() + + Clears the path object removing all path elements. This is more lightweight + than creating a new JSPath object. + */ + void QQuickPathItemPathObject::clear() { path = QQuickPathItemPath(); } +/*! + \qmltype StrokeFillParams + \inqmlmodule QtQuick + \ingroup qtquick-path + \brief Describes stroke and fill parameters via a JavaScript API + + The properties of StrokeFillParams objects correspond 1:1 to VisualPath + properties. The possible values for enumerations are the same as well, for + example: + + \code + sfp.strokeStyle = VisualPath.DashLine; + sfp.capStyle = VisualPath.RoundCap; + \endcode + */ + +/*! + \qmlproperty color QtQuick::StrokeFillParams::strokeColor + */ + +/*! + \qmlproperty real QtQuick::StrokeFillParams::strokeWidth + */ + +/*! + \qmlproperty color QtQuick::StrokeFillParams::fillColor + */ + +/*! + \qmlproperty enumeration QtQuick::StrokeFillParams::fillRule + */ + +/*! + \qmlproperty enumeration QtQuick::StrokeFillParams::joinStyle + */ + +/*! + \qmlproperty int QtQuick::StrokeFillParams::miterLimit + */ + +/*! + \qmlproperty enumeration QtQuick::StrokeFillParams::capStyle + */ + +/*! + \qmlproperty enumeration QtQuick::StrokeFillParams::strokeStyle + */ + +/*! + \qmlproperty real QtQuick::StrokeFillParams::dashOffset + */ + +/*! + \qmlproperty list QtQuick::StrokeFillParams::dashPattern + + The dash pattern can be specified using JavaScript arrays. + + \code + sfp.dashPattern = [ 4, 2 ]; + \endcode + */ + +/*! + \qmlproperty object QtQuick::StrokeFillParams::fillGradient + + Sets the fill gradient. The default value is null. Gradients cannot be + created from JavaScript. Instead, reference a PathLinearGradient or other + item by id. + + \code + PathLinearGradient { id: grad; ... } + ... + sfp.fillGradient = grad; + \endcode + */ + +/*! + \qmlmethod void QtQuick::StrokeFillParams::clear() + + Resets all values to their defaults. This is more lightweight than creating + a new StrokeFillParams object. + */ + void QQuickPathItemStrokeFillParamsObject::clear() { sfp = QQuickPathItemStrokeFillParams(); @@ -1996,6 +2127,45 @@ void QQuickPathItemStrokeFillParamsObject::setV4Engine(QV4::ExecutionEngine *eng m_v4value = wrapper; } +/*! + \qmlmethod JSPath QtQuick::PathItem::newPath() + + Creates and returns a new object that describes a path and offers a + JavaScript API. Paired with a stroke-fill parameter object it is + equivalent to a VisualPath. + + The following two snippets are equivalent when it comes to the end result: + + \code + var p = pathItem.newPath(); + var sfp = pathItem.newStrokeFillParams(); + sfp.fillColor = "white"; + sfp.strokeColor = "black"; + sfp.strokeWidth = 0.172; + p.moveTo(-122.304, 84.285); + p.cubicTo(-122.304, 84.285, -122.203, 86.179, -123.027, 86.16); + pathItem.appendVisualPath(p, sfp); + \endcode + + \code + PathItem { + VisualPath { + fillColor: "white" + strokeColor: "black" + strokeWidth: 0.172 + Path { + startX: -122.304; startY: 84.285 + PathCubic { control1X: -122.304; control1Y: 84.285; control2X: -122.203; control2Y: 86.179; x: -123.027; y: 86.16 } + } + } + } + \endcode + + The latter offers a full declarative API, with the possibility to binding + to and animating properties, while the former uses less resources due to + greatly reducing the number of QObject instances created. +*/ + void QQuickPathItem::newPath(QQmlV4Function *args) { QQuickPathItemPathObject *obj = new QQuickPathItemPathObject(this); @@ -2003,6 +2173,14 @@ void QQuickPathItem::newPath(QQmlV4Function *args) args->setReturnValue(obj->v4value()); } +/*! + \qmlmethod StrokeFillParams QtQuick::PathItem::newStrokeFillParams() + + Creates and returns a new object that describes stroke and fill parameters + and offers a JavaScript API. Paired with a path object it is equivalent to + a VisualPath. + */ + void QQuickPathItem::newStrokeFillParams(QQmlV4Function *args) { QQuickPathItemStrokeFillParamsObject *obj = new QQuickPathItemStrokeFillParamsObject(this); @@ -2010,6 +2188,15 @@ void QQuickPathItem::newStrokeFillParams(QQmlV4Function *args) args->setReturnValue(obj->v4value()); } +/*! + \qmlmethod void QtQuick::PathItem::clearVisualPaths() + + Clears the list of visual paths. + + \note This applies only to path and stroke-fill parameter objects registered + via appendVisualPaths(). + */ + void QQuickPathItem::clearVisualPaths(QQmlV4Function *args) { Q_UNUSED(args); @@ -2018,6 +2205,17 @@ void QQuickPathItem::clearVisualPaths(QQmlV4Function *args) d->jsData.sfp.clear(); } +/*! + \qmlmethod void QtQuick::PathItem::commitVisualPaths() + + Updates the PathItem. + + In order to avoid rendering a half-prepared PathItem, calling + PathItem.appendVisualPath() does not trigger any processing. Instead, + applications must call this function when all path and stroke-fill + parameter objects are registered. + */ + void QQuickPathItem::commitVisualPaths(QQmlV4Function *args) { Q_UNUSED(args); @@ -2025,6 +2223,19 @@ void QQuickPathItem::commitVisualPaths(QQmlV4Function *args) d->_q_visualPathChanged(); } +/*! + \qmlmethod void QtQuick::PathItem::appendVisualPath(object path, object strokeFillParams) + + Adds the visual path compoes of \a path and \a strokeFillParams into the + PathItem. + + \note The declarative and imprative (JavaScript) APIs of PathItem use + independent data structures. Calling this function has no effect on the + PathItem.elements property and vice versa. Once this function is called, + the PathItem will only consider the data registered via this function and + will ignore the declarative elements property. + */ + void QQuickPathItem::appendVisualPath(QQmlV4Function *args) { if (args->length() < 2) -- cgit v1.2.3 From e2520ff76be49c5aa917741cc6a380fe1549e47d Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Mon, 3 Apr 2017 11:58:46 +0200 Subject: Move PathItem to qt.labs Change-Id: I1cd686cff60bd40fe2cbbc34f917fac7835b6b7d Reviewed-by: Qt CI Bot Reviewed-by: Andy Nichols Reviewed-by: Robin Burchell --- src/quick/items/context2d/qquickcontext2d.cpp | 3 +- src/quick/items/items.pri | 15 +- src/quick/items/qquickitemsmodule.cpp | 6 - src/quick/items/qquickpathitem.cpp | 2258 -------------------- src/quick/items/qquickpathitem_p.h | 335 --- src/quick/items/qquickpathitem_p_p.h | 282 --- src/quick/items/qquickpathitemgenericrenderer.cpp | 775 ------- src/quick/items/qquickpathitemgenericrenderer_p.h | 303 --- src/quick/items/qquickpathitemnvprrenderer.cpp | 923 -------- src/quick/items/qquickpathitemnvprrenderer_p.h | 237 -- src/quick/items/qquickpathitemsoftwarerenderer.cpp | 277 --- src/quick/items/qquickpathitemsoftwarerenderer_p.h | 136 -- 12 files changed, 4 insertions(+), 5546 deletions(-) delete mode 100644 src/quick/items/qquickpathitem.cpp delete mode 100644 src/quick/items/qquickpathitem_p.h delete mode 100644 src/quick/items/qquickpathitem_p_p.h delete mode 100644 src/quick/items/qquickpathitemgenericrenderer.cpp delete mode 100644 src/quick/items/qquickpathitemgenericrenderer_p.h delete mode 100644 src/quick/items/qquickpathitemnvprrenderer.cpp delete mode 100644 src/quick/items/qquickpathitemnvprrenderer_p.h delete mode 100644 src/quick/items/qquickpathitemsoftwarerenderer.cpp delete mode 100644 src/quick/items/qquickpathitemsoftwarerenderer_p.h (limited to 'src/quick/items') diff --git a/src/quick/items/context2d/qquickcontext2d.cpp b/src/quick/items/context2d/qquickcontext2d.cpp index 1a6f530bfa..715fc4b2c7 100644 --- a/src/quick/items/context2d/qquickcontext2d.cpp +++ b/src/quick/items/context2d/qquickcontext2d.cpp @@ -40,6 +40,7 @@ #include "qquickcontext2d_p.h" #include "qquickcontext2dcommandbuffer_p.h" #include "qquickcanvasitem_p.h" +#include #include #include #if QT_CONFIG(quick_shadereffect) @@ -136,7 +137,7 @@ Q_CORE_EXPORT double qstrtod(const char *s00, char const **se, bool *ok); THROW_GENERIC_ERROR("Not a Context2D object"); #define qClamp(val, min, max) qMin(qMax(val, min), max) #define CHECK_RGBA(c) (c == '-' || c == '.' || (c >=0 && c <= 9)) -QColor qt_color_from_string(const QV4::Value &name) +Q_QUICK_PRIVATE_EXPORT QColor qt_color_from_string(const QV4::Value &name) { QByteArray str = name.toQString().toUtf8(); diff --git a/src/quick/items/items.pri b/src/quick/items/items.pri index 511c6f18d8..0f8061b5ef 100644 --- a/src/quick/items/items.pri +++ b/src/quick/items/items.pri @@ -148,20 +148,9 @@ qtConfig(quick-listview) { qtConfig(quick-pathview) { HEADERS += \ $$PWD/qquickpathview_p.h \ - $$PWD/qquickpathview_p_p.h \ - $$PWD/qquickpathitem_p.h \ - $$PWD/qquickpathitem_p_p.h \ - $$PWD/qquickpathitemgenericrenderer_p.h \ - $$PWD/qquickpathitemsoftwarerenderer_p.h + $$PWD/qquickpathview_p_p.h SOURCES += \ - $$PWD/qquickpathview.cpp \ - $$PWD/qquickpathitem.cpp \ - $$PWD/qquickpathitemgenericrenderer.cpp \ - $$PWD/qquickpathitemsoftwarerenderer.cpp - qtConfig(opengl) { - HEADERS += $$PWD/qquickpathitemnvprrenderer_p.h - SOURCES += $$PWD/qquickpathitemnvprrenderer.cpp - } + $$PWD/qquickpathview.cpp } qtConfig(quick-positioners) { diff --git a/src/quick/items/qquickitemsmodule.cpp b/src/quick/items/qquickitemsmodule.cpp index 9e692da442..e6321e9365 100644 --- a/src/quick/items/qquickitemsmodule.cpp +++ b/src/quick/items/qquickitemsmodule.cpp @@ -70,7 +70,6 @@ #if QT_CONFIG(quick_path) #include #include -#include "qquickpathitem_p.h" #endif #if QT_CONFIG(quick_positioners) #include "qquickpositioners_p.h" @@ -381,11 +380,6 @@ static void qt_quickitems_defineModule(const char *uri, int major, int minor) #if QT_CONFIG(quick_path) qmlRegisterType(uri, 2, 9, "PathArc"); qmlRegisterType(uri, 2, 9, "PathMove"); - qmlRegisterType(uri, 2, 9, "PathItem"); - qmlRegisterType(uri, 2, 9, "VisualPath"); - qmlRegisterType(uri, 2, 9, "PathGradientStop"); - qmlRegisterUncreatableType(uri, 2, 9, "PathGradient", QQuickPathGradient::tr("PathGradient is an abstract base class")); - qmlRegisterType(uri, 2, 9, "PathLinearGradient"); #endif qmlRegisterType(uri, 2, 9, "Text"); diff --git a/src/quick/items/qquickpathitem.cpp b/src/quick/items/qquickpathitem.cpp deleted file mode 100644 index fae16064e5..0000000000 --- a/src/quick/items/qquickpathitem.cpp +++ /dev/null @@ -1,2258 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qquickpathitem_p.h" -#include "qquickpathitem_p_p.h" -#include "qquickpathitemgenericrenderer_p.h" -#include "qquickpathitemnvprrenderer_p.h" -#include "qquickpathitemsoftwarerenderer_p.h" -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -QQuickPathItemStrokeFillParams::QQuickPathItemStrokeFillParams() - : strokeColor(Qt::white), - strokeWidth(1), - fillColor(Qt::white), - fillRule(QQuickVisualPath::OddEvenFill), - joinStyle(QQuickVisualPath::BevelJoin), - miterLimit(2), - capStyle(QQuickVisualPath::SquareCap), - strokeStyle(QQuickVisualPath::SolidLine), - dashOffset(0), - fillGradient(nullptr) -{ - dashPattern << 4 << 2; // 4 * strokeWidth dash followed by 2 * strokeWidth space -} - -QPainterPath QQuickPathItemPath::toPainterPath() const -{ - QPainterPath p; - int coordIdx = 0; - for (int i = 0; i < cmd.count(); ++i) { - switch (cmd[i]) { - case QQuickPathItemPath::MoveTo: - p.moveTo(coords[coordIdx], coords[coordIdx + 1]); - coordIdx += 2; - break; - case QQuickPathItemPath::LineTo: - p.lineTo(coords[coordIdx], coords[coordIdx + 1]); - coordIdx += 2; - break; - case QQuickPathItemPath::QuadTo: - p.quadTo(coords[coordIdx], coords[coordIdx + 1], - coords[coordIdx + 2], coords[coordIdx + 3]); - coordIdx += 4; - break; - case QQuickPathItemPath::CubicTo: - p.cubicTo(coords[coordIdx], coords[coordIdx + 1], - coords[coordIdx + 2], coords[coordIdx + 3], - coords[coordIdx + 4], coords[coordIdx + 5]); - coordIdx += 6; - break; - case QQuickPathItemPath::ArcTo: - // does not map to the QPainterPath API; reuse the helper code from QQuickSvgParser - QQuickSvgParser::pathArc(p, - coords[coordIdx], coords[coordIdx + 1], // radius - coords[coordIdx + 2], // xAxisRotation - !qFuzzyIsNull(coords[coordIdx + 6]), // useLargeArc - !qFuzzyIsNull(coords[coordIdx + 5]), // sweep flag - coords[coordIdx + 3], coords[coordIdx + 4], // end - p.currentPosition().x(), p.currentPosition().y()); - coordIdx += 7; - break; - default: - qWarning("Unknown JS path command: %d", cmd[i]); - break; - } - } - return p; -} - -/*! - \qmltype VisualPath - \instantiates QQuickVisualPath - \inqmlmodule QtQuick - \ingroup qtquick-paths - \ingroup qtquick-views - \inherits Object - \brief Describes a Path and associated properties for stroking and filling - \since 5.10 - - A PathItem contains one or more VisualPath elements. At least one - VisualPath is necessary in order to have a PathItem output anything - visible. A VisualPath in turn contains a Path and properties describing the - stroking and filling parameters, such as the stroke width and color, the - fill color or gradient, join and cap styles, and so on. Finally, the Path - object contains a list of path elements like PathMove, PathLine, PathCubic, - PathQuad, PathArc. - - Any property changes in these data sets will be bubble up and change the - output of the PathItem. This means that it is simple and easy to change, or - even animate, the starting and ending position, control points, or any - stroke or fill parameters using the usual QML bindings and animation types - like NumberAnimation. - - In the following example the line join style changes automatically based on - the value of joinStyleIndex: - - \code - VisualPath { - strokeColor: "black" - strokeWidth: 16 - fillColor: "transparent" - capStyle: VisualPath.RoundCap - - property int joinStyleIndex: 0 - property variant styles: [ VisualPath.BevelJoin, VisualPath.MiterJoin, VisualPath.RoundJoin ] - - joinStyle: styles[joinStyleIndex] - - Path { - startX: 30 - startY: 30 - PathLine { x: 100; y: 100 } - PathLine { x: 30; y: 100 } - } - } - \endcode - - Once associated with a PathItem, here is the output with a joinStyleIndex - of 2 (VisualPath.RoundJoin): - - \image visualpath-code-example.png - */ - -QQuickVisualPathPrivate::QQuickVisualPathPrivate() - : path(nullptr), - dirty(DirtyAll) -{ -} - -QQuickVisualPath::QQuickVisualPath(QObject *parent) - : QObject(*(new QQuickVisualPathPrivate), parent) -{ -} - -QQuickVisualPath::~QQuickVisualPath() -{ -} - -/*! - \qmlproperty Path QtQuick::VisualPath::path - - This property holds the Path object. - - \default - */ - -QQuickPath *QQuickVisualPath::path() const -{ - Q_D(const QQuickVisualPath); - return d->path; -} - -void QQuickVisualPath::setPath(QQuickPath *path) -{ - Q_D(QQuickVisualPath); - if (d->path == path) - return; - - if (d->path) - qmlobject_disconnect(d->path, QQuickPath, SIGNAL(changed()), - this, QQuickVisualPath, SLOT(_q_pathChanged())); - d->path = path; - qmlobject_connect(d->path, QQuickPath, SIGNAL(changed()), - this, QQuickVisualPath, SLOT(_q_pathChanged())); - - d->dirty |= QQuickVisualPathPrivate::DirtyPath; - emit pathChanged(); - emit changed(); -} - -void QQuickVisualPathPrivate::_q_pathChanged() -{ - Q_Q(QQuickVisualPath); - dirty |= DirtyPath; - emit q->changed(); -} - -/*! - \qmlproperty color QtQuick::VisualPath::strokeColor - - This property holds the stroking color. - - When set to \c transparent, no stroking occurs. - - The default value is \c white. - */ - -QColor QQuickVisualPath::strokeColor() const -{ - Q_D(const QQuickVisualPath); - return d->sfp.strokeColor; -} - -void QQuickVisualPath::setStrokeColor(const QColor &color) -{ - Q_D(QQuickVisualPath); - if (d->sfp.strokeColor != color) { - d->sfp.strokeColor = color; - d->dirty |= QQuickVisualPathPrivate::DirtyStrokeColor; - emit strokeColorChanged(); - emit changed(); - } -} - -/*! - \qmlproperty color QtQuick::VisualPath::strokeWidth - - This property holds the stroke width. - - When set to a negative value, no stroking occurs. - - The default value is 1. - */ - -qreal QQuickVisualPath::strokeWidth() const -{ - Q_D(const QQuickVisualPath); - return d->sfp.strokeWidth; -} - -void QQuickVisualPath::setStrokeWidth(qreal w) -{ - Q_D(QQuickVisualPath); - if (d->sfp.strokeWidth != w) { - d->sfp.strokeWidth = w; - d->dirty |= QQuickVisualPathPrivate::DirtyStrokeWidth; - emit strokeWidthChanged(); - emit changed(); - } -} - -/*! - \qmlproperty color QtQuick::VisualPath::fillColor - - This property holds the fill color. - - When set to \c transparent, no filling occurs. - - The default value is \c white. - */ - -QColor QQuickVisualPath::fillColor() const -{ - Q_D(const QQuickVisualPath); - return d->sfp.fillColor; -} - -void QQuickVisualPath::setFillColor(const QColor &color) -{ - Q_D(QQuickVisualPath); - if (d->sfp.fillColor != color) { - d->sfp.fillColor = color; - d->dirty |= QQuickVisualPathPrivate::DirtyFillColor; - emit fillColorChanged(); - emit changed(); - } -} - -/*! - \qmlproperty enumeration QtQuick::VisualPath::fillRule - - This property holds the fill rule. The default value is - VisualPath.OddEvenFill. For an example on fill rules, see - QPainterPath::setFillRule(). - - \list - \li VisualPath.OddEvenFill - \li VisualPath.WindingFill - \endlist - */ - -QQuickVisualPath::FillRule QQuickVisualPath::fillRule() const -{ - Q_D(const QQuickVisualPath); - return d->sfp.fillRule; -} - -void QQuickVisualPath::setFillRule(FillRule fillRule) -{ - Q_D(QQuickVisualPath); - if (d->sfp.fillRule != fillRule) { - d->sfp.fillRule = fillRule; - d->dirty |= QQuickVisualPathPrivate::DirtyFillRule; - emit fillRuleChanged(); - emit changed(); - } -} - -/*! - \qmlproperty enumeration QtQuick::VisualPath::joinStyle - - This property defines how joins between two connected lines are drawn. The - default value is VisualPath.BevelJoin. - - \list - \li VisualPath.MiterJoin - The outer edges of the lines are extended to meet at an angle, and this area is filled. - \li VisualPath.BevelJoin - The triangular notch between the two lines is filled. - \li VisualPath.RoundJoin - A circular arc between the two lines is filled. - \endlist - */ - -QQuickVisualPath::JoinStyle QQuickVisualPath::joinStyle() const -{ - Q_D(const QQuickVisualPath); - return d->sfp.joinStyle; -} - -void QQuickVisualPath::setJoinStyle(JoinStyle style) -{ - Q_D(QQuickVisualPath); - if (d->sfp.joinStyle != style) { - d->sfp.joinStyle = style; - d->dirty |= QQuickVisualPathPrivate::DirtyStyle; - emit joinStyleChanged(); - emit changed(); - } -} - -/*! - \qmlproperty int QtQuick::VisualPath::miterLimit - - When VisualPath.joinStyle is set to VisualPath.MiterJoin, this property - specifies how far the miter join can extend from the join point. - - The default value is 2. - */ - -int QQuickVisualPath::miterLimit() const -{ - Q_D(const QQuickVisualPath); - return d->sfp.miterLimit; -} - -void QQuickVisualPath::setMiterLimit(int limit) -{ - Q_D(QQuickVisualPath); - if (d->sfp.miterLimit != limit) { - d->sfp.miterLimit = limit; - d->dirty |= QQuickVisualPathPrivate::DirtyStyle; - emit miterLimitChanged(); - emit changed(); - } -} - -/*! - \qmlproperty enumeration QtQuick::VisualPath::capStyle - - This property defines how the end points of lines are drawn. The - default value is VisualPath.SquareCap. - - \list - \li VisualPath.FlatCap - A square line end that does not cover the end point of the line. - \li VisualPath.SquareCap - A square line end that covers the end point and extends beyond it by half the line width. - \li VisualPath.RoundCap - A rounded line end. - \endlist - */ - -QQuickVisualPath::CapStyle QQuickVisualPath::capStyle() const -{ - Q_D(const QQuickVisualPath); - return d->sfp.capStyle; -} - -void QQuickVisualPath::setCapStyle(CapStyle style) -{ - Q_D(QQuickVisualPath); - if (d->sfp.capStyle != style) { - d->sfp.capStyle = style; - d->dirty |= QQuickVisualPathPrivate::DirtyStyle; - emit capStyleChanged(); - emit changed(); - } -} - -/*! - \qmlproperty enumeration QtQuick::VisualPath::strokeStyle - - This property defines the style of stroking. The default value is - VisualPath.SolidLine. - - \list - \li VisualPath.SolidLine - A plain line. - \li VisualPath.DashLine - Dashes separated by a few pixels. - \endlist - */ - -QQuickVisualPath::StrokeStyle QQuickVisualPath::strokeStyle() const -{ - Q_D(const QQuickVisualPath); - return d->sfp.strokeStyle; -} - -void QQuickVisualPath::setStrokeStyle(StrokeStyle style) -{ - Q_D(QQuickVisualPath); - if (d->sfp.strokeStyle != style) { - d->sfp.strokeStyle = style; - d->dirty |= QQuickVisualPathPrivate::DirtyDash; - emit strokeStyleChanged(); - emit changed(); - } -} - -/*! - \qmlproperty real QtQuick::VisualPath::dashOffset - - This property defines the starting point on the dash pattern, measured in - units used to specify the dash pattern. - - The default value is 0. - - \sa QPen::setDashOffset() - */ - -qreal QQuickVisualPath::dashOffset() const -{ - Q_D(const QQuickVisualPath); - return d->sfp.dashOffset; -} - -void QQuickVisualPath::setDashOffset(qreal offset) -{ - Q_D(QQuickVisualPath); - if (d->sfp.dashOffset != offset) { - d->sfp.dashOffset = offset; - d->dirty |= QQuickVisualPathPrivate::DirtyDash; - emit dashOffsetChanged(); - emit changed(); - } -} - -/*! - \qmlproperty list QtQuick::VisualPath::dashPattern - - This property defines the dash pattern when VisualPath.strokeStyle is set - to VisualPath.DashLine. The pattern must be specified as an even number of - positive entries where the entries 1, 3, 5... are the dashes and 2, 4, 6... - are the spaces. The pattern is specified in units of the pen's width. - - The default value is (4, 2), meaning a dash of 4 * VisualPath.strokeWidth - pixels followed by a space of 2 * VisualPath.strokeWidth pixels. - - \sa QPen::setDashPattern() - */ - -QVector QQuickVisualPath::dashPattern() const -{ - Q_D(const QQuickVisualPath); - return d->sfp.dashPattern; -} - -void QQuickVisualPath::setDashPattern(const QVector &array) -{ - Q_D(QQuickVisualPath); - if (d->sfp.dashPattern != array) { - d->sfp.dashPattern = array; - d->dirty |= QQuickVisualPathPrivate::DirtyDash; - emit dashPatternChanged(); - emit changed(); - } -} - -/*! - \qmlproperty PathGradient QtQuick::VisualPath::fillGradient - - This property defines the fill gradient. By default no gradient is enabled - and the value is \c null. In this case the fill uses a solid color based on - the value of VisuaLPath.fillColor. - - When set, VisualPath.fillColor is ignored and filling is done using one of - the PathGradient subtypes. - */ - -QQuickPathGradient *QQuickVisualPath::fillGradient() const -{ - Q_D(const QQuickVisualPath); - return d->sfp.fillGradient; -} - -void QQuickVisualPath::setFillGradient(QQuickPathGradient *gradient) -{ - Q_D(QQuickVisualPath); - if (d->sfp.fillGradient != gradient) { - if (d->sfp.fillGradient) - qmlobject_disconnect(d->sfp.fillGradient, QQuickPathGradient, SIGNAL(updated()), - this, QQuickVisualPath, SLOT(_q_fillGradientChanged())); - d->sfp.fillGradient = gradient; - if (d->sfp.fillGradient) - qmlobject_connect(d->sfp.fillGradient, QQuickPathGradient, SIGNAL(updated()), - this, QQuickVisualPath, SLOT(_q_fillGradientChanged())); - d->dirty |= QQuickVisualPathPrivate::DirtyFillGradient; - emit changed(); - } -} - -void QQuickVisualPathPrivate::_q_fillGradientChanged() -{ - Q_Q(QQuickVisualPath); - dirty |= DirtyFillGradient; - emit q->changed(); -} - -void QQuickVisualPath::resetFillGradient() -{ - setFillGradient(nullptr); -} - -/*! - \qmltype PathItem - \instantiates QQuickPathItem - \inqmlmodule QtQuick - \ingroup qtquick-paths - \ingroup qtquick-views - \inherits Item - \brief Renders a path - \since 5.10 - - Renders a path either by generating geometry via QPainterPath and manual - triangulation or by using a GPU vendor extension like \c{GL_NV_path_rendering}. - - This approach is different from rendering shapes via QQuickPaintedItem or - the 2D Canvas because the path never gets rasterized in software. Therefore - PathItem is suitable for creating shapes spreading over larger areas of the - screen, avoiding the performance penalty for texture uploads or framebuffer - blits. In addition, the declarative API allows manipulating, binding to, - and even animating the path element properties like starting and ending - position, the control points, etc. - - The types for specifying path elements are shared between \l PathView and - PathItem. However, not all PathItem implementations support all path - element types, while some may not make sense for PathView. PathItem's - currently supported subset is: PathMove, PathLine, PathQuad, PathCubic, - PathArc, PathSvg. - - See \l Path for a detailed overview of the supported path elements. - - \code - PathItem { - width: 200 - height: 150 - anchors.centerIn: parent - VisualPath { - strokeWidth: 4 - strokeColor: "red" - fillGradient: PathLinearGradient { - x1: 20; y1: 20 - x2: 180; y2: 130 - PathGradientStop { position: 0; color: "blue" } - PathGradientStop { position: 0.2; color: "green" } - PathGradientStop { position: 0.4; color: "red" } - PathGradientStop { position: 0.6; color: "yellow" } - PathGradientStop { position: 1; color: "cyan" } - } - strokeStyle: VisualPath.DashLine - dashPattern: [ 1, 4 ] - Path { - startX: 20; startY: 20 - PathLine { x: 180; y: 130 } - PathLine { x: 20; y: 130 } - PathLine { x: 20; y: 20 } - } - } - } - \endcode - - \image pathitem-code-example.png - - \note It is important to be aware of performance implications, in - particular when the application is running on the generic PathItem - implementation due to not having support for accelerated path rendering. - The geometry generation happens entirely on the CPU in this case, and this - is potentially expensive. Changing the set of path elements, changing the - properties of these elements, or changing certain properties of the - PathItem itself all lead to retriangulation on every change. Therefore, - applying animation to such properties can heavily affect performance on - less powerful systems. If animating properties other than stroke and fill - colors is a must, it is recommended to target systems providing - \c{GL_NV_path_rendering} where the cost of path property changes is much - smaller. - - The following list summarizes the available PathItem rendering approaches: - - \list - - \li When running with the default, OpenGL backend of Qt Quick, both the - generic, triangulation-based and the NVIDIA-specific - \c{GL_NV_path_rendering} methods are available. The choice is made at - runtime, depending on the graphics driver's capabilities. When this is not - desired, applications can force using the generic method by setting the - PathItem.enableVendorExtensions property to \c false. - - \li The \c software backend is fully supported. The path is rendered via - QPainter::strokePath() and QPainter::fillPath() in this case. - - \li The Direct 3D 12 backend is not currently supported. - - \li The OpenVG backend is not currently supported. - - \endlist - - \sa Path, PathMove, PathLine, PathQuad, PathCubic, PathArc, PathSvg -*/ - -QQuickPathItemPrivate::QQuickPathItemPrivate() - : componentComplete(true), - vpChanged(false), - rendererType(QQuickPathItem::UnknownRenderer), - async(false), - status(QQuickPathItem::Null), - renderer(nullptr), - enableVendorExts(true) -{ -} - -QQuickPathItemPrivate::~QQuickPathItemPrivate() -{ - delete renderer; -} - -void QQuickPathItemPrivate::_q_visualPathChanged() -{ - Q_Q(QQuickPathItem); - vpChanged = true; - q->polish(); -} - -void QQuickPathItemPrivate::setStatus(QQuickPathItem::Status newStatus) -{ - Q_Q(QQuickPathItem); - if (status != newStatus) { - status = newStatus; - emit q->statusChanged(); - } -} - -QQuickPathItem::QQuickPathItem(QQuickItem *parent) - : QQuickItem(*(new QQuickPathItemPrivate), parent) -{ - setFlag(ItemHasContents); -} - -QQuickPathItem::~QQuickPathItem() -{ -} - -/*! - \qmlproperty enumeration QtQuick::PathItem::rendererType - - This property determines which path rendering backend is active. - - \list - - \li PathItem.UnknownRenderer - The renderer is unknown. - - \li PathItem.GeometryRenderer - The generic, driver independent solution - for OpenGL. Uses the same CPU-based triangulation approach as QPainter's - OpenGL 2 paint engine. This is the default on non-NVIDIA hardware when the - default, OpenGL Qt Quick scenegraph backend is in use. - - \li PathItem.NvprRenderer - Path items are rendered by performing OpenGL - calls using the \c{GL_NV_path_rendering} extension. This is the default on - NVIDIA hardware when the default, OpenGL Qt Quick scenegraph backend is in - use. - - \li PathItem.SoftwareRenderer - Pure QPainter drawing using the raster - paint engine. This is the default, and only, option when the Qt Quick - scenegraph is running with the \c software backend. - - \endlist -*/ - -QQuickPathItem::RendererType QQuickPathItem::rendererType() const -{ - Q_D(const QQuickPathItem); - return d->rendererType; -} - -/*! - \qmlproperty bool QtQuick::PathItem::asynchronous - - When PathItem.rendererType is PathItem.GeometryRenderer, the input path is - triangulated on the CPU during the polishing phase of the PathItem. This is - potentially expensive. To offload this work to separate worker threads, set - this property to \c true. - - When enabled, making a PathItem visible will not wait for the content to - become available. Instead, the gui/main thread is not blocked and the - results of the path rendering are shown only when all the asynchronous work - has been finished. - - The default value is \c false. - */ - -bool QQuickPathItem::asynchronous() const -{ - Q_D(const QQuickPathItem); - return d->async; -} - -void QQuickPathItem::setAsynchronous(bool async) -{ - Q_D(QQuickPathItem); - if (d->async != async) { - d->async = async; - emit asynchronousChanged(); - if (d->componentComplete) - d->_q_visualPathChanged(); - } -} - -/*! - \qmlproperty bool QtQuick::PathItem::enableVendorExtensions - - This property controls the usage of non-standard OpenGL extensions like - GL_NV_path_rendering. To disable PathItem.NvprRenderer and force a uniform - behavior regardless of the graphics card and drivers, set this property to - \c false. - - The default value is \c true. - */ - -bool QQuickPathItem::enableVendorExtensions() const -{ - Q_D(const QQuickPathItem); - return d->enableVendorExts; -} - -void QQuickPathItem::setEnableVendorExtensions(bool enable) -{ - Q_D(QQuickPathItem); - if (d->enableVendorExts != enable) { - d->enableVendorExts = enable; - emit enableVendorExtensionsChanged(); - } -} - -/*! - \qmlproperty enumeration QtQuick::PathItem::status - - This property determines the status of the PathItem and is relevant when - PathItem.asynchronous is set to \c true. - - \list - - \li PathItem.Null - Not yet initialized. - - \li PathItem.Ready - The PathItem has finished processing. - - \li PathItem.Processing - The path is being processed. - - \endlist - */ - -QQuickPathItem::Status QQuickPathItem::status() const -{ - Q_D(const QQuickPathItem); - return d->status; -} - -static QQuickVisualPath *vpe_at(QQmlListProperty *property, int index) -{ - QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(static_cast(property->object)); - return d->qmlData.vp.at(index); -} - -static void vpe_append(QQmlListProperty *property, QQuickVisualPath *obj) -{ - QQuickPathItem *item = static_cast(property->object); - QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(item); - d->qmlData.vp.append(obj); - - if (d->componentComplete) { - QObject::connect(obj, SIGNAL(changed()), item, SLOT(_q_visualPathChanged())); - d->_q_visualPathChanged(); - } -} - -static int vpe_count(QQmlListProperty *property) -{ - QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(static_cast(property->object)); - return d->qmlData.vp.count(); -} - -static void vpe_clear(QQmlListProperty *property) -{ - QQuickPathItem *item = static_cast(property->object); - QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(item); - - for (QQuickVisualPath *p : d->qmlData.vp) - QObject::disconnect(p, SIGNAL(changed()), item, SLOT(_q_visualPathChanged())); - - d->qmlData.vp.clear(); - - if (d->componentComplete) - d->_q_visualPathChanged(); -} - -/*! - \qmlproperty list QtQuick::PathItem::elements - - This property holds the VisualPath objects that define the contents of the - PathItem. - - \default - */ - -QQmlListProperty QQuickPathItem::elements() -{ - return QQmlListProperty(this, - nullptr, - vpe_append, - vpe_count, - vpe_at, - vpe_clear); -} - -void QQuickPathItem::classBegin() -{ - Q_D(QQuickPathItem); - d->componentComplete = false; -} - -void QQuickPathItem::componentComplete() -{ - Q_D(QQuickPathItem); - d->componentComplete = true; - - for (QQuickVisualPath *p : d->qmlData.vp) - connect(p, SIGNAL(changed()), this, SLOT(_q_visualPathChanged())); - - d->_q_visualPathChanged(); -} - -void QQuickPathItem::updatePolish() -{ - Q_D(QQuickPathItem); - - if (!d->vpChanged) - return; - - d->vpChanged = false; - - if (!d->renderer) { - d->createRenderer(); - if (!d->renderer) - return; - emit rendererChanged(); - } - - // endSync() is where expensive calculations may happen (or get kicked off - // on worker threads), depending on the backend. Therefore do this only - // when the item is visible. - if (isVisible()) - d->sync(); - - update(); -} - -void QQuickPathItem::itemChange(ItemChange change, const ItemChangeData &data) -{ - Q_D(QQuickPathItem); - - // sync may have been deferred; do it now if the item became visible - if (change == ItemVisibleHasChanged && data.boolValue) - d->_q_visualPathChanged(); - - QQuickItem::itemChange(change, data); -} - -QSGNode *QQuickPathItem::updatePaintNode(QSGNode *node, UpdatePaintNodeData *) -{ - // Called on the render thread, with the gui thread blocked. We can now - // safely access gui thread data. - - Q_D(QQuickPathItem); - if (d->renderer) { - if (!node) - node = d->createNode(); - d->renderer->updateNode(); - } - return node; -} - -// the renderer object lives on the gui thread -void QQuickPathItemPrivate::createRenderer() -{ - Q_Q(QQuickPathItem); - QSGRendererInterface *ri = q->window()->rendererInterface(); - if (!ri) - return; - - switch (ri->graphicsApi()) { -#ifndef QT_NO_OPENGL - case QSGRendererInterface::OpenGL: - if (enableVendorExts && QQuickPathItemNvprRenderNode::isSupported()) { - rendererType = QQuickPathItem::NvprRenderer; - renderer = new QQuickPathItemNvprRenderer; - } else { - rendererType = QQuickPathItem::GeometryRenderer; - renderer = new QQuickPathItemGenericRenderer(q); - } - break; -#endif - case QSGRendererInterface::Software: - rendererType = QQuickPathItem::SoftwareRenderer; - renderer = new QQuickPathItemSoftwareRenderer; - break; - default: - qWarning("No path backend for this graphics API yet"); - break; - } -} - -// the node lives on the render thread -QSGNode *QQuickPathItemPrivate::createNode() -{ - Q_Q(QQuickPathItem); - QSGNode *node = nullptr; - if (!q->window()) - return node; - QSGRendererInterface *ri = q->window()->rendererInterface(); - if (!ri) - return node; - - switch (ri->graphicsApi()) { -#ifndef QT_NO_OPENGL - case QSGRendererInterface::OpenGL: - if (enableVendorExts && QQuickPathItemNvprRenderNode::isSupported()) { - node = new QQuickPathItemNvprRenderNode; - static_cast(renderer)->setNode( - static_cast(node)); - } else { - node = new QQuickPathItemGenericNode; - static_cast(renderer)->setRootNode( - static_cast(node)); - } - break; -#endif - case QSGRendererInterface::Software: - node = new QQuickPathItemSoftwareRenderNode(q); - static_cast(renderer)->setNode( - static_cast(node)); - break; - default: - qWarning("No path backend for this graphics API yet"); - break; - } - - return node; -} - -static void q_asyncPathItemReady(void *data) -{ - QQuickPathItemPrivate *self = static_cast(data); - self->setStatus(QQuickPathItem::Ready); -} - -void QQuickPathItemPrivate::sync() -{ - const bool useAsync = async && renderer->flags().testFlag(QQuickAbstractPathRenderer::SupportsAsync); - if (useAsync) { - setStatus(QQuickPathItem::Processing); - renderer->setAsyncCallback(q_asyncPathItemReady, this); - } - - if (!jsData.isValid()) { - // Standard route: The path and stroke/fill parameters are provided via - // VisualPath and Path. - const int count = qmlData.vp.count(); - renderer->beginSync(count); - - for (int i = 0; i < count; ++i) { - QQuickVisualPath *p = qmlData.vp[i]; - int &dirty(QQuickVisualPathPrivate::get(p)->dirty); - - if (dirty & QQuickVisualPathPrivate::DirtyPath) - renderer->setPath(i, p->path()); - if (dirty & QQuickVisualPathPrivate::DirtyStrokeColor) - renderer->setStrokeColor(i, p->strokeColor()); - if (dirty & QQuickVisualPathPrivate::DirtyStrokeWidth) - renderer->setStrokeWidth(i, p->strokeWidth()); - if (dirty & QQuickVisualPathPrivate::DirtyFillColor) - renderer->setFillColor(i, p->fillColor()); - if (dirty & QQuickVisualPathPrivate::DirtyFillRule) - renderer->setFillRule(i, p->fillRule()); - if (dirty & QQuickVisualPathPrivate::DirtyStyle) { - renderer->setJoinStyle(i, p->joinStyle(), p->miterLimit()); - renderer->setCapStyle(i, p->capStyle()); - } - if (dirty & QQuickVisualPathPrivate::DirtyDash) - renderer->setStrokeStyle(i, p->strokeStyle(), p->dashOffset(), p->dashPattern()); - if (dirty & QQuickVisualPathPrivate::DirtyFillGradient) - renderer->setFillGradient(i, p->fillGradient()); - - dirty = 0; - } - - renderer->endSync(useAsync); - } else { - // Path and stroke/fill params provided from JavaScript. This avoids - // QObjects at the expense of not supporting changes afterwards. - const int count = jsData.paths.count(); - renderer->beginSync(count); - - for (int i = 0; i < count; ++i) { - renderer->setJSPath(i, jsData.paths[i]); - const QQuickPathItemStrokeFillParams sfp(jsData.sfp[i]); - renderer->setStrokeColor(i, sfp.strokeColor); - renderer->setStrokeWidth(i, sfp.strokeWidth); - renderer->setFillColor(i, sfp.fillColor); - renderer->setFillRule(i, sfp.fillRule); - renderer->setJoinStyle(i, sfp.joinStyle, sfp.miterLimit); - renderer->setCapStyle(i, sfp.capStyle); - renderer->setStrokeStyle(i, sfp.strokeStyle, sfp.dashOffset, sfp.dashPattern); - renderer->setFillGradient(i, sfp.fillGradient); - } - - renderer->endSync(useAsync); - } - - if (!useAsync) - setStatus(QQuickPathItem::Ready); -} - -// ***** gradient support ***** - -/*! - \qmltype PathGradientStop - \instantiates QQuickPathGradientStop - \inqmlmodule QtQuick - \ingroup qtquick-paths - \ingroup qtquick-views - \inherits Object - \brief Defines a color at a position in a gradient - \since 5.10 - */ - -QQuickPathGradientStop::QQuickPathGradientStop(QObject *parent) - : QObject(parent), - m_position(0), - m_color(Qt::black) -{ -} - -/*! - \qmlproperty real QtQuick::PathGradientStop::position - - The position and color properties describe the color used at a given - position in a gradient, as represented by a gradient stop. - - The default value is 0. - */ - -qreal QQuickPathGradientStop::position() const -{ - return m_position; -} - -void QQuickPathGradientStop::setPosition(qreal position) -{ - if (m_position != position) { - m_position = position; - if (QQuickPathGradient *grad = qobject_cast(parent())) - emit grad->updated(); - } -} - -/*! - \qmlproperty real QtQuick::PathGradientStop::color - - The position and color properties describe the color used at a given - position in a gradient, as represented by a gradient stop. - - The default value is \c black. - */ - -QColor QQuickPathGradientStop::color() const -{ - return m_color; -} - -void QQuickPathGradientStop::setColor(const QColor &color) -{ - if (m_color != color) { - m_color = color; - if (QQuickPathGradient *grad = qobject_cast(parent())) - emit grad->updated(); - } -} - -/*! - \qmltype PathGradient - \instantiates QQuickPathGradient - \inqmlmodule QtQuick - \ingroup qtquick-paths - \ingroup qtquick-views - \inherits Object - \brief Base type of PathItem fill gradients - \since 5.10 - - This is an abstract base class for gradients like PathLinearGradient and - cannot be created directly. - */ - -QQuickPathGradient::QQuickPathGradient(QObject *parent) - : QObject(parent), - m_spread(PadSpread) -{ -} - -int QQuickPathGradient::countStops(QQmlListProperty *list) -{ - QQuickPathGradient *grad = qobject_cast(list->object); - Q_ASSERT(grad); - return grad->m_stops.count(); -} - -QObject *QQuickPathGradient::atStop(QQmlListProperty *list, int index) -{ - QQuickPathGradient *grad = qobject_cast(list->object); - Q_ASSERT(grad); - return grad->m_stops.at(index); -} - -void QQuickPathGradient::appendStop(QQmlListProperty *list, QObject *stop) -{ - QQuickPathGradientStop *sstop = qobject_cast(stop); - if (!sstop) { - qWarning("Gradient stop list only supports QQuickPathGradientStop elements"); - return; - } - QQuickPathGradient *grad = qobject_cast(list->object); - Q_ASSERT(grad); - sstop->setParent(grad); - grad->m_stops.append(sstop); -} - -/*! - \qmlproperty list QtQuick::PathGradient::stops - \default - - The list of PathGradientStop objects defining the colors at given positions - in the gradient. - */ - -QQmlListProperty QQuickPathGradient::stops() -{ - return QQmlListProperty(this, nullptr, - &QQuickPathGradient::appendStop, - &QQuickPathGradient::countStops, - &QQuickPathGradient::atStop, - nullptr); -} - -QGradientStops QQuickPathGradient::sortedGradientStops() const -{ - QGradientStops result; - for (int i = 0; i < m_stops.count(); ++i) { - QQuickPathGradientStop *s = static_cast(m_stops[i]); - int j = 0; - while (j < result.count() && result[j].first < s->position()) - ++j; - result.insert(j, QGradientStop(s->position(), s->color())); - } - return result; -} - -/*! - \qmlproperty enumeration QtQuick::PathGradient::spred - - Specifies how the area outside the gradient area should be filled. The - default value is PathGradient.PadSpread. - - \list - \li PathGradient.PadSpread - The area is filled with the closest stop color. - \li PathGradient.RepeatSpread - The gradient is repeated outside the gradient area. - \li PathGradient.ReflectSpread - The gradient is reflected outside the gradient area. - \endlist - */ - -QQuickPathGradient::SpreadMode QQuickPathGradient::spread() const -{ - return m_spread; -} - -void QQuickPathGradient::setSpread(SpreadMode mode) -{ - if (m_spread != mode) { - m_spread = mode; - emit spreadChanged(); - emit updated(); - } -} - -/*! - \qmltype PathLinearGradient - \instantiates QQuickPathLinearGradient - \inqmlmodule QtQuick - \ingroup qtquick-paths - \ingroup qtquick-views - \inherits PathGradient - \brief Linear gradient - \since 5.10 - - Linear gradients interpolate colors between start and end points. Outside - these points the gradient is either padded, reflected or repeated depending - on the spread type. - - \sa QLinearGradient - */ - -QQuickPathLinearGradient::QQuickPathLinearGradient(QObject *parent) - : QQuickPathGradient(parent) -{ -} - -/*! - \qmlproperty real QtQuick::PathLinearGradient::x1 - \qmlproperty real QtQuick::PathLinearGradient::y1 - \qmlproperty real QtQuick::PathLinearGradient::x2 - \qmlproperty real QtQuick::PathLinearGradient::y2 - - These properties define the start and end points between which color - interpolation occurs. By default both the stard and end points are set to - (0, 0). - */ - -qreal QQuickPathLinearGradient::x1() const -{ - return m_start.x(); -} - -void QQuickPathLinearGradient::setX1(qreal v) -{ - if (m_start.x() != v) { - m_start.setX(v); - emit x1Changed(); - emit updated(); - } -} - -qreal QQuickPathLinearGradient::y1() const -{ - return m_start.y(); -} - -void QQuickPathLinearGradient::setY1(qreal v) -{ - if (m_start.y() != v) { - m_start.setY(v); - emit y1Changed(); - emit updated(); - } -} - -qreal QQuickPathLinearGradient::x2() const -{ - return m_end.x(); -} - -void QQuickPathLinearGradient::setX2(qreal v) -{ - if (m_end.x() != v) { - m_end.setX(v); - emit x2Changed(); - emit updated(); - } -} - -qreal QQuickPathLinearGradient::y2() const -{ - return m_end.y(); -} - -void QQuickPathLinearGradient::setY2(qreal v) -{ - if (m_end.y() != v) { - m_end.setY(v); - emit y2Changed(); - emit updated(); - } -} - -#ifndef QT_NO_OPENGL - -// contexts sharing with each other get the same cache instance -class QQuickPathItemGradientCacheWrapper -{ -public: - QQuickPathItemGradientCache *get(QOpenGLContext *context) - { - return m_resource.value(context); - } - -private: - QOpenGLMultiGroupSharedResource m_resource; -}; - -QQuickPathItemGradientCache *QQuickPathItemGradientCache::currentCache() -{ - static QQuickPathItemGradientCacheWrapper qt_path_gradient_caches; - return qt_path_gradient_caches.get(QOpenGLContext::currentContext()); -} - -// let QOpenGLContext manage the lifetime of the cached textures -QQuickPathItemGradientCache::~QQuickPathItemGradientCache() -{ - m_cache.clear(); -} - -void QQuickPathItemGradientCache::invalidateResource() -{ - m_cache.clear(); -} - -void QQuickPathItemGradientCache::freeResource(QOpenGLContext *) -{ - qDeleteAll(m_cache); - m_cache.clear(); -} - -static void generateGradientColorTable(const QQuickPathItemGradientCache::GradientDesc &gradient, - uint *colorTable, int size, float opacity) -{ - int pos = 0; - const QGradientStops &s = gradient.stops; - const bool colorInterpolation = true; - - uint alpha = qRound(opacity * 256); - uint current_color = ARGB_COMBINE_ALPHA(s[0].second.rgba(), alpha); - qreal incr = 1.0 / qreal(size); - qreal fpos = 1.5 * incr; - colorTable[pos++] = ARGB2RGBA(qPremultiply(current_color)); - - while (fpos <= s.first().first) { - colorTable[pos] = colorTable[pos - 1]; - pos++; - fpos += incr; - } - - if (colorInterpolation) - current_color = qPremultiply(current_color); - - const int sLast = s.size() - 1; - for (int i = 0; i < sLast; ++i) { - qreal delta = 1/(s[i+1].first - s[i].first); - uint next_color = ARGB_COMBINE_ALPHA(s[i + 1].second.rgba(), alpha); - if (colorInterpolation) - next_color = qPremultiply(next_color); - - while (fpos < s[i+1].first && pos < size) { - int dist = int(256 * ((fpos - s[i].first) * delta)); - int idist = 256 - dist; - if (colorInterpolation) - colorTable[pos] = ARGB2RGBA(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)); - else - colorTable[pos] = ARGB2RGBA(qPremultiply(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist))); - ++pos; - fpos += incr; - } - current_color = next_color; - } - - Q_ASSERT(s.size() > 0); - - uint last_color = ARGB2RGBA(qPremultiply(ARGB_COMBINE_ALPHA(s[sLast].second.rgba(), alpha))); - for ( ; pos < size; ++pos) - colorTable[pos] = last_color; - - colorTable[size-1] = last_color; -} - -QSGTexture *QQuickPathItemGradientCache::get(const GradientDesc &grad) -{ - QSGPlainTexture *tx = m_cache[grad]; - if (!tx) { - QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); - GLuint id; - f->glGenTextures(1, &id); - f->glBindTexture(GL_TEXTURE_2D, id); - static const uint W = 1024; // texture size is 1024x1 - uint buf[W]; - generateGradientColorTable(grad, buf, W, 1.0f); - f->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, W, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); - tx = new QSGPlainTexture; - tx->setTextureId(id); - switch (grad.spread) { - case QQuickPathGradient::PadSpread: - tx->setHorizontalWrapMode(QSGTexture::ClampToEdge); - tx->setVerticalWrapMode(QSGTexture::ClampToEdge); - break; - case QQuickPathGradient::RepeatSpread: - tx->setHorizontalWrapMode(QSGTexture::Repeat); - tx->setVerticalWrapMode(QSGTexture::Repeat); - break; - case QQuickPathGradient::ReflectSpread: - tx->setHorizontalWrapMode(QSGTexture::MirroredRepeat); - tx->setVerticalWrapMode(QSGTexture::MirroredRepeat); - break; - default: - qWarning("Unknown gradient spread mode %d", grad.spread); - break; - } - m_cache[grad] = tx; - } - return tx; -} - -#endif // QT_NO_OPENGL - -// ***** JS-based alternative for creating static paths, (mostly) without QObjects ***** - -class QQuickPathItemJSEngineData : public QV8Engine::Deletable -{ -public: - QQuickPathItemJSEngineData(QV4::ExecutionEngine *engine); - - QV4::PersistentValue pathProto; - QV4::PersistentValue strokeFillParamsProto; -}; - -V4_DEFINE_EXTENSION(QQuickPathItemJSEngineData, engineData) - -namespace QV4 { -namespace Heap { - -struct QQuickPathItemJSPathPrototype : Object { - void init() { Object::init(); } -}; - -struct QQuickPathItemJSPath : Object { - void init() { Object::init(); } - QQuickPathItemPathObject *obj; -}; - -struct QQuickPathItemJSStrokeFillParamsPrototype : Object { - void init() { Object::init(); } -}; - -struct QQuickPathItemJSStrokeFillParams : Object { - void init() { Object::init(); } - QQuickPathItemStrokeFillParamsObject *obj; -}; - -} // namespace Heap -} // namespace QV4 - -struct QQuickPathItemJSPathPrototype : public QV4::Object -{ - V4_OBJECT2(QQuickPathItemJSPathPrototype, QV4::Object) -public: - static QV4::Heap::QQuickPathItemJSPathPrototype *create(QV4::ExecutionEngine *engine) - { - QV4::Scope scope(engine); - auto obj = engine->memoryManager->allocObject(); - QV4::Scoped o(scope, obj); - - o->defineDefaultProperty(QStringLiteral("clear"), method_clear, 0); - o->defineDefaultProperty(QStringLiteral("moveTo"), method_moveTo, 0); - o->defineDefaultProperty(QStringLiteral("lineTo"), method_lineTo, 0); - o->defineDefaultProperty(QStringLiteral("quadTo"), method_quadTo, 0); - o->defineDefaultProperty(QStringLiteral("cubicTo"), method_cubicTo, 0); - o->defineDefaultProperty(QStringLiteral("arcTo"), method_arcTo, 0); - - return o->d(); - } - - static void method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_moveTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_lineTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_quadTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_cubicTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_arcTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); -}; - -DEFINE_OBJECT_VTABLE(QQuickPathItemJSPathPrototype); - -struct QQuickPathItemJSStrokeFillParamsPrototype : public QV4::Object -{ - V4_OBJECT2(QQuickPathItemJSStrokeFillParamsPrototype, QV4::Object) -public: - static QV4::Heap::QQuickPathItemJSStrokeFillParamsPrototype *create(QV4::ExecutionEngine *engine) - { - QV4::Scope scope(engine); - auto obj = engine->memoryManager->allocObject(); - QV4::Scoped o(scope, obj); - - o->defineDefaultProperty(QStringLiteral("clear"), method_clear, 0); - - return o->d(); - } - - static void method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); -}; - -DEFINE_OBJECT_VTABLE(QQuickPathItemJSStrokeFillParamsPrototype); - -struct QQuickPathItemJSPath : public QV4::Object -{ - V4_OBJECT2(QQuickPathItemJSPath, QV4::Object) -}; - -DEFINE_OBJECT_VTABLE(QQuickPathItemJSPath); - -struct QQuickPathItemJSStrokeFillParams : public QV4::Object -{ - V4_OBJECT2(QQuickPathItemJSStrokeFillParams, QV4::Object) - - static void method_get_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_set_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_get_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_set_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_get_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_set_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_get_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_set_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_get_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_set_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_get_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_set_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_get_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_set_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_get_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_set_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_get_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_set_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_get_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_set_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_get_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); - static void method_set_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData); -}; - -DEFINE_OBJECT_VTABLE(QQuickPathItemJSStrokeFillParams); - -void QQuickPathItemJSPathPrototype::method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - r->d()->obj->clear(); - - scope.result = callData->thisObject.asReturnedValue(); -} - -void QQuickPathItemJSPathPrototype::method_moveTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - if (callData->argc >= 2) { - QQuickPathItemPathObject *p = r->d()->obj; - p->path.cmd.append(QQuickPathItemPath::MoveTo); - p->path.coords.append(callData->args[0].toNumber()); - p->path.coords.append(callData->args[1].toNumber()); - } - - scope.result = callData->thisObject.asReturnedValue(); -} - -void QQuickPathItemJSPathPrototype::method_lineTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - if (callData->argc >= 2) { - QQuickPathItemPathObject *p = r->d()->obj; - p->path.cmd.append(QQuickPathItemPath::LineTo); - p->path.coords.append(callData->args[0].toNumber()); - p->path.coords.append(callData->args[1].toNumber()); - } - - scope.result = callData->thisObject.asReturnedValue(); -} - -void QQuickPathItemJSPathPrototype::method_quadTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - if (callData->argc >= 4) { - QQuickPathItemPathObject *p = r->d()->obj; - p->path.cmd.append(QQuickPathItemPath::QuadTo); - const QV4::Value *v = callData->args; - p->path.coords.append(v[0].toNumber()); // cx - p->path.coords.append(v[1].toNumber()); // cy - p->path.coords.append(v[2].toNumber()); // x - p->path.coords.append(v[3].toNumber()); // y - } - - scope.result = callData->thisObject.asReturnedValue(); -} - -void QQuickPathItemJSPathPrototype::method_cubicTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - if (callData->argc >= 6) { - QQuickPathItemPathObject *p = r->d()->obj; - p->path.cmd.append(QQuickPathItemPath::CubicTo); - const QV4::Value *v = callData->args; - p->path.coords.append(v[0].toNumber()); // c1x - p->path.coords.append(v[1].toNumber()); // c1y - p->path.coords.append(v[2].toNumber()); // c2x - p->path.coords.append(v[3].toNumber()); // c2y - p->path.coords.append(v[4].toNumber()); // x - p->path.coords.append(v[5].toNumber()); // y - } - - scope.result = callData->thisObject.asReturnedValue(); -} - -void QQuickPathItemJSPathPrototype::method_arcTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - if (callData->argc >= 7) { - QQuickPathItemPathObject *p = r->d()->obj; - p->path.cmd.append(QQuickPathItemPath::ArcTo); - const QV4::Value *v = callData->args; - p->path.coords.append(v[0].toNumber()); // radiusX - p->path.coords.append(v[1].toNumber()); // radiusY - p->path.coords.append(v[2].toNumber()); // xAxisRotation - p->path.coords.append(v[3].toNumber()); // x - p->path.coords.append(v[4].toNumber()); // y - p->path.coords.append(v[5].toNumber()); // sweepFlag - p->path.coords.append(v[6].toNumber()); // largeArc - } - - scope.result = callData->thisObject.asReturnedValue(); -} - -void QQuickPathItemJSStrokeFillParamsPrototype::method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - r->d()->obj->clear(); - - scope.result = callData->thisObject.asReturnedValue(); -} - -extern QColor qt_color_from_string(const QV4::Value &name); // qquickcontext2d.cpp - -static inline QString qt_color_string(const QColor &color) -{ - if (color.alpha() == 255) - return color.name(); - QString alphaString = QString::number(color.alphaF(), 'f'); - while (alphaString.endsWith(QLatin1Char('0'))) - alphaString.chop(1); - if (alphaString.endsWith(QLatin1Char('.'))) - alphaString += QLatin1Char('0'); - return QString::fromLatin1("rgba(%1, %2, %3, %4)").arg(color.red()).arg(color.green()).arg(color.blue()).arg(alphaString); -} - -void QQuickPathItemJSStrokeFillParams::method_get_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - scope.result = QV4::Encode(scope.engine->newString(qt_color_string(r->d()->obj->sfp.strokeColor))); -} - -void QQuickPathItemJSStrokeFillParams::method_set_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedValue value(scope, callData->argument(0)); - if (value->isString()) - r->d()->obj->sfp.strokeColor = qt_color_from_string(value); - - scope.result = QV4::Encode::undefined(); -} - -void QQuickPathItemJSStrokeFillParams::method_get_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - scope.result = scope.engine->fromVariant(r->d()->obj->sfp.strokeWidth); -} - -void QQuickPathItemJSStrokeFillParams::method_set_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedValue value(scope, callData->argument(0)); - r->d()->obj->sfp.strokeWidth = value->toNumber(); - - scope.result = QV4::Encode::undefined(); -} - -void QQuickPathItemJSStrokeFillParams::method_get_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - scope.result = QV4::Encode(scope.engine->newString(qt_color_string(r->d()->obj->sfp.fillColor))); -} - -void QQuickPathItemJSStrokeFillParams::method_set_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedValue value(scope, callData->argument(0)); - if (value->isString()) - r->d()->obj->sfp.fillColor = qt_color_from_string(value); - - scope.result = QV4::Encode::undefined(); -} - -void QQuickPathItemJSStrokeFillParams::method_get_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - scope.result = scope.engine->fromVariant(r->d()->obj->sfp.fillRule); -} - -void QQuickPathItemJSStrokeFillParams::method_set_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedValue value(scope, callData->argument(0)); - if (value->isInt32()) - r->d()->obj->sfp.fillRule = QQuickVisualPath::FillRule(value->integerValue()); - - scope.result = QV4::Encode::undefined(); -} - -void QQuickPathItemJSStrokeFillParams::method_get_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - scope.result = scope.engine->fromVariant(r->d()->obj->sfp.joinStyle); -} - -void QQuickPathItemJSStrokeFillParams::method_set_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedValue value(scope, callData->argument(0)); - if (value->isInt32()) - r->d()->obj->sfp.joinStyle = QQuickVisualPath::JoinStyle(value->integerValue()); - - scope.result = QV4::Encode::undefined(); -} - -void QQuickPathItemJSStrokeFillParams::method_get_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - scope.result = scope.engine->fromVariant(r->d()->obj->sfp.miterLimit); -} - -void QQuickPathItemJSStrokeFillParams::method_set_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedValue value(scope, callData->argument(0)); - r->d()->obj->sfp.miterLimit = value->toNumber(); - - scope.result = QV4::Encode::undefined(); -} - -void QQuickPathItemJSStrokeFillParams::method_get_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - scope.result = scope.engine->fromVariant(r->d()->obj->sfp.capStyle); -} - -void QQuickPathItemJSStrokeFillParams::method_set_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedValue value(scope, callData->argument(0)); - if (value->isInt32()) - r->d()->obj->sfp.capStyle = QQuickVisualPath::CapStyle(value->integerValue()); - - scope.result = QV4::Encode::undefined(); -} - -void QQuickPathItemJSStrokeFillParams::method_get_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - scope.result = scope.engine->fromVariant(r->d()->obj->sfp.strokeStyle); -} - -void QQuickPathItemJSStrokeFillParams::method_set_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedValue value(scope, callData->argument(0)); - if (value->isInt32()) - r->d()->obj->sfp.strokeStyle = QQuickVisualPath::StrokeStyle(value->integerValue()); - - scope.result = QV4::Encode::undefined(); -} - -void QQuickPathItemJSStrokeFillParams::method_get_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - scope.result = scope.engine->fromVariant(r->d()->obj->sfp.dashOffset); -} - -void QQuickPathItemJSStrokeFillParams::method_set_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedValue value(scope, callData->argument(0)); - r->d()->obj->sfp.dashOffset = value->toNumber(); - - scope.result = QV4::Encode::undefined(); -} - -void QQuickPathItemJSStrokeFillParams::method_get_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedArrayObject a(scope, scope.engine->newArrayObject()); - QQuickPathItemStrokeFillParamsObject *p = r->d()->obj; - a->arrayReserve(p->sfp.dashPattern.count()); - QV4::ScopedValue v(scope); - for (int i = 0; i < p->sfp.dashPattern.count(); ++i) - a->arrayPut(i, (v = scope.engine->fromVariant(p->sfp.dashPattern[i]))); - a->setArrayLengthUnchecked(p->sfp.dashPattern.count()); - - scope.result = a.asReturnedValue(); -} - -void QQuickPathItemJSStrokeFillParams::method_set_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedValue value(scope, callData->argument(0)); - if (value->isObject()) { - QV4::Scoped ao(scope, value); - if (!!ao) { - QQuickPathItemStrokeFillParamsObject *p = r->d()->obj; - p->sfp.dashPattern.resize(ao->getLength()); - QV4::ScopedValue val(scope); - for (int i = 0; i < p->sfp.dashPattern.count(); ++i) { - val = ao->getIndexed(i); - p->sfp.dashPattern[i] = val->toNumber(); - } - } - } - - scope.result = QV4::Encode::undefined(); -} - -void QQuickPathItemJSStrokeFillParams::method_get_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - scope.result = r->d()->obj->v4fillGradient.value(); -} - -void QQuickPathItemJSStrokeFillParams::method_set_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData) -{ - QV4::Scoped r(scope, callData->thisObject.as()); - - QV4::ScopedValue value(scope, callData->argument(0)); - QV4::Scoped qobjectWrapper(scope, value); - if (!!qobjectWrapper) { - if (QQuickPathGradient *grad = qobject_cast(qobjectWrapper->object())) { - r->d()->obj->v4fillGradient.set(scope.engine, value); - r->d()->obj->sfp.fillGradient = grad; - } - } else { - r->d()->obj->v4fillGradient.set(scope.engine, nullptr); - r->d()->obj->sfp.fillGradient = nullptr; - } - - scope.result = QV4::Encode::undefined(); -} - -QQuickPathItemJSEngineData::QQuickPathItemJSEngineData(QV4::ExecutionEngine *v4) -{ - QV4::Scope scope(v4); - - QV4::ScopedObject proto(scope, QQuickPathItemJSPathPrototype::create(v4)); - pathProto = proto; - - proto = QV4::ScopedObject(scope, QQuickPathItemJSStrokeFillParamsPrototype::create(v4)); - - proto->defineAccessorProperty(QStringLiteral("strokeColor"), - QQuickPathItemJSStrokeFillParams::method_get_strokeColor, - QQuickPathItemJSStrokeFillParams::method_set_strokeColor); - proto->defineAccessorProperty(QStringLiteral("strokeWidth"), - QQuickPathItemJSStrokeFillParams::method_get_strokeWidth, - QQuickPathItemJSStrokeFillParams::method_set_strokeWidth); - proto->defineAccessorProperty(QStringLiteral("fillColor"), - QQuickPathItemJSStrokeFillParams::method_get_fillColor, - QQuickPathItemJSStrokeFillParams::method_set_fillColor); - proto->defineAccessorProperty(QStringLiteral("fillRule"), - QQuickPathItemJSStrokeFillParams::method_get_fillRule, - QQuickPathItemJSStrokeFillParams::method_set_fillRule); - proto->defineAccessorProperty(QStringLiteral("joinStyle"), - QQuickPathItemJSStrokeFillParams::method_get_joinStyle, - QQuickPathItemJSStrokeFillParams::method_set_joinStyle); - proto->defineAccessorProperty(QStringLiteral("miterLimit"), - QQuickPathItemJSStrokeFillParams::method_get_miterLimit, - QQuickPathItemJSStrokeFillParams::method_set_miterLimit); - proto->defineAccessorProperty(QStringLiteral("capStyle"), - QQuickPathItemJSStrokeFillParams::method_get_capStyle, - QQuickPathItemJSStrokeFillParams::method_set_capStyle); - proto->defineAccessorProperty(QStringLiteral("strokeStyle"), - QQuickPathItemJSStrokeFillParams::method_get_strokeStyle, - QQuickPathItemJSStrokeFillParams::method_set_strokeStyle); - proto->defineAccessorProperty(QStringLiteral("dashOffset"), - QQuickPathItemJSStrokeFillParams::method_get_dashOffset, - QQuickPathItemJSStrokeFillParams::method_set_dashOffset); - proto->defineAccessorProperty(QStringLiteral("dashPattern"), - QQuickPathItemJSStrokeFillParams::method_get_dashPattern, - QQuickPathItemJSStrokeFillParams::method_set_dashPattern); - proto->defineAccessorProperty(QStringLiteral("fillGradient"), - QQuickPathItemJSStrokeFillParams::method_get_fillGradient, - QQuickPathItemJSStrokeFillParams::method_set_fillGradient); - - strokeFillParamsProto = proto; -} - -void QQuickPathItemPathObject::setV4Engine(QV4::ExecutionEngine *engine) -{ - QQuickPathItemJSEngineData *ed = engineData(engine); - QV4::Scope scope(engine); - QV4::Scoped wrapper(scope, engine->memoryManager->allocObject()); - QV4::ScopedObject p(scope, ed->pathProto.value()); - wrapper->setPrototype(p); - wrapper->d()->obj = this; - m_v4value = wrapper; -} - -/*! - \qmltype JSPath - \inqmlmodule QtQuick - \ingroup qtquick-path - \brief Describes a path via a JavaScript API - */ - -/*! - \qmlmethod void QtQuick::JSPath::moveTo(x, y) - - Moves the path's position to the absolute position specified by (\a x, \a y). - */ - -/*! - \qmlmethod void QtQuick::JSPath::lineTo(x, y) - - Defines a straight line to the absolute position specified by (\a x, \a y). - */ - -/*! - \qmlmethod void QtQuick::JSPath::quadTo(cx, cy, x, y) - - Defines a quadratic Bezier curve with a control point (\a cx, \a cy) and an - end point of (\a x, \a y). - */ - -/*! - \qmlmethod void QtQuick::JSPath::cubicTo(c1x, c1y, c2x, c2y, x, y) - - Defines a cubic Bezier curve with two control points (\a c1x, \a c1y) and - (\a c2x, \a c2y), and an end point of (\a x, \a y). - */ - -/*! - \qmlmethod void QtQuick::JSPath::arcTo(radiusX, radiusY, xAxisRotation, x, y, sweepFlag, largeArc) - - Defines an elliptical arc, following the elliptical arc command in SVG. See - \l{https://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands}{the - SVG path specification} for details on the parameters. - */ - -/*! - \qmlmethod void QtQuick::JSPath::clear() - - Clears the path object removing all path elements. This is more lightweight - than creating a new JSPath object. - */ - -void QQuickPathItemPathObject::clear() -{ - path = QQuickPathItemPath(); -} - -/*! - \qmltype StrokeFillParams - \inqmlmodule QtQuick - \ingroup qtquick-path - \brief Describes stroke and fill parameters via a JavaScript API - - The properties of StrokeFillParams objects correspond 1:1 to VisualPath - properties. The possible values for enumerations are the same as well, for - example: - - \code - sfp.strokeStyle = VisualPath.DashLine; - sfp.capStyle = VisualPath.RoundCap; - \endcode - */ - -/*! - \qmlproperty color QtQuick::StrokeFillParams::strokeColor - */ - -/*! - \qmlproperty real QtQuick::StrokeFillParams::strokeWidth - */ - -/*! - \qmlproperty color QtQuick::StrokeFillParams::fillColor - */ - -/*! - \qmlproperty enumeration QtQuick::StrokeFillParams::fillRule - */ - -/*! - \qmlproperty enumeration QtQuick::StrokeFillParams::joinStyle - */ - -/*! - \qmlproperty int QtQuick::StrokeFillParams::miterLimit - */ - -/*! - \qmlproperty enumeration QtQuick::StrokeFillParams::capStyle - */ - -/*! - \qmlproperty enumeration QtQuick::StrokeFillParams::strokeStyle - */ - -/*! - \qmlproperty real QtQuick::StrokeFillParams::dashOffset - */ - -/*! - \qmlproperty list QtQuick::StrokeFillParams::dashPattern - - The dash pattern can be specified using JavaScript arrays. - - \code - sfp.dashPattern = [ 4, 2 ]; - \endcode - */ - -/*! - \qmlproperty object QtQuick::StrokeFillParams::fillGradient - - Sets the fill gradient. The default value is null. Gradients cannot be - created from JavaScript. Instead, reference a PathLinearGradient or other - item by id. - - \code - PathLinearGradient { id: grad; ... } - ... - sfp.fillGradient = grad; - \endcode - */ - -/*! - \qmlmethod void QtQuick::StrokeFillParams::clear() - - Resets all values to their defaults. This is more lightweight than creating - a new StrokeFillParams object. - */ - -void QQuickPathItemStrokeFillParamsObject::clear() -{ - sfp = QQuickPathItemStrokeFillParams(); - if (!v4fillGradient.isNullOrUndefined()) - v4fillGradient.set(v4fillGradient.engine(), nullptr); -} - -void QQuickPathItemStrokeFillParamsObject::setV4Engine(QV4::ExecutionEngine *engine) -{ - QQuickPathItemJSEngineData *ed = engineData(engine); - QV4::Scope scope(engine); - QV4::Scoped wrapper(scope, engine->memoryManager->allocObject()); - QV4::ScopedObject p(scope, ed->strokeFillParamsProto.value()); - wrapper->setPrototype(p); - wrapper->d()->obj = this; - m_v4value = wrapper; -} - -/*! - \qmlmethod JSPath QtQuick::PathItem::newPath() - - Creates and returns a new object that describes a path and offers a - JavaScript API. Paired with a stroke-fill parameter object it is - equivalent to a VisualPath. - - The following two snippets are equivalent when it comes to the end result: - - \code - var p = pathItem.newPath(); - var sfp = pathItem.newStrokeFillParams(); - sfp.fillColor = "white"; - sfp.strokeColor = "black"; - sfp.strokeWidth = 0.172; - p.moveTo(-122.304, 84.285); - p.cubicTo(-122.304, 84.285, -122.203, 86.179, -123.027, 86.16); - pathItem.appendVisualPath(p, sfp); - \endcode - - \code - PathItem { - VisualPath { - fillColor: "white" - strokeColor: "black" - strokeWidth: 0.172 - Path { - startX: -122.304; startY: 84.285 - PathCubic { control1X: -122.304; control1Y: 84.285; control2X: -122.203; control2Y: 86.179; x: -123.027; y: 86.16 } - } - } - } - \endcode - - The latter offers a full declarative API, with the possibility to binding - to and animating properties, while the former uses less resources due to - greatly reducing the number of QObject instances created. -*/ - -void QQuickPathItem::newPath(QQmlV4Function *args) -{ - QQuickPathItemPathObject *obj = new QQuickPathItemPathObject(this); - obj->setV4Engine(QQmlEnginePrivate::get(qmlEngine(this))->v4engine()); - args->setReturnValue(obj->v4value()); -} - -/*! - \qmlmethod StrokeFillParams QtQuick::PathItem::newStrokeFillParams() - - Creates and returns a new object that describes stroke and fill parameters - and offers a JavaScript API. Paired with a path object it is equivalent to - a VisualPath. - */ - -void QQuickPathItem::newStrokeFillParams(QQmlV4Function *args) -{ - QQuickPathItemStrokeFillParamsObject *obj = new QQuickPathItemStrokeFillParamsObject(this); - obj->setV4Engine(QQmlEnginePrivate::get(qmlEngine(this))->v4engine()); - args->setReturnValue(obj->v4value()); -} - -/*! - \qmlmethod void QtQuick::PathItem::clearVisualPaths() - - Clears the list of visual paths. - - \note This applies only to path and stroke-fill parameter objects registered - via appendVisualPaths(). - */ - -void QQuickPathItem::clearVisualPaths(QQmlV4Function *args) -{ - Q_UNUSED(args); - Q_D(QQuickPathItem); - d->jsData.paths.clear(); - d->jsData.sfp.clear(); -} - -/*! - \qmlmethod void QtQuick::PathItem::commitVisualPaths() - - Updates the PathItem. - - In order to avoid rendering a half-prepared PathItem, calling - PathItem.appendVisualPath() does not trigger any processing. Instead, - applications must call this function when all path and stroke-fill - parameter objects are registered. - */ - -void QQuickPathItem::commitVisualPaths(QQmlV4Function *args) -{ - Q_UNUSED(args); - Q_D(QQuickPathItem); - d->_q_visualPathChanged(); -} - -/*! - \qmlmethod void QtQuick::PathItem::appendVisualPath(object path, object strokeFillParams) - - Adds the visual path compoes of \a path and \a strokeFillParams into the - PathItem. - - \note The declarative and imprative (JavaScript) APIs of PathItem use - independent data structures. Calling this function has no effect on the - PathItem.elements property and vice versa. Once this function is called, - the PathItem will only consider the data registered via this function and - will ignore the declarative elements property. - */ - -void QQuickPathItem::appendVisualPath(QQmlV4Function *args) -{ - if (args->length() < 2) - return; - - Q_D(QQuickPathItem); - QV4::Scope scope(args->v4engine()); - QV4::Scoped jsp(scope, (*args)[0]); - QV4::Scoped jssfp(scope, (*args)[1]); - - const QQuickPathItemPath &path(jsp->d()->obj->path); - const QQuickPathItemStrokeFillParams &sfp(jssfp->d()->obj->sfp); - - d->jsData.paths.append(path); - d->jsData.sfp.append(sfp); -} - -QT_END_NAMESPACE - -#include "moc_qquickpathitem_p.cpp" diff --git a/src/quick/items/qquickpathitem_p.h b/src/quick/items/qquickpathitem_p.h deleted file mode 100644 index 37b23dee6f..0000000000 --- a/src/quick/items/qquickpathitem_p.h +++ /dev/null @@ -1,335 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKPATHITEM_P_H -#define QQUICKPATHITEM_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "qquickitem.h" - -#include -#include -#include -#include - -QT_REQUIRE_CONFIG(quick_path); - -QT_BEGIN_NAMESPACE - -class QQuickVisualPathPrivate; -class QQuickPathItemPrivate; - -class Q_QUICK_PRIVATE_EXPORT QQuickPathGradientStop : public QObject -{ - Q_OBJECT - Q_PROPERTY(qreal position READ position WRITE setPosition) - Q_PROPERTY(QColor color READ color WRITE setColor) - -public: - QQuickPathGradientStop(QObject *parent = nullptr); - - qreal position() const; - void setPosition(qreal position); - - QColor color() const; - void setColor(const QColor &color); - -private: - qreal m_position; - QColor m_color; -}; - -class Q_QUICK_PRIVATE_EXPORT QQuickPathGradient : public QObject -{ - Q_OBJECT - Q_PROPERTY(QQmlListProperty stops READ stops) - Q_PROPERTY(SpreadMode spread READ spread WRITE setSpread NOTIFY spreadChanged) - Q_CLASSINFO("DefaultProperty", "stops") - -public: - enum SpreadMode { - PadSpread, - RepeatSpread, - ReflectSpread - }; - Q_ENUM(SpreadMode) - - QQuickPathGradient(QObject *parent = nullptr); - - QQmlListProperty stops(); - - QGradientStops sortedGradientStops() const; - - SpreadMode spread() const; - void setSpread(SpreadMode mode); - -signals: - void updated(); - void spreadChanged(); - -private: - static int countStops(QQmlListProperty *list); - static QObject *atStop(QQmlListProperty *list, int index); - static void appendStop(QQmlListProperty *list, QObject *stop); - - QVector m_stops; - SpreadMode m_spread; -}; - -class Q_QUICK_PRIVATE_EXPORT QQuickPathLinearGradient : public QQuickPathGradient -{ - Q_OBJECT - Q_PROPERTY(qreal x1 READ x1 WRITE setX1 NOTIFY x1Changed) - Q_PROPERTY(qreal y1 READ y1 WRITE setY1 NOTIFY y1Changed) - Q_PROPERTY(qreal x2 READ x2 WRITE setX2 NOTIFY x2Changed) - Q_PROPERTY(qreal y2 READ y2 WRITE setY2 NOTIFY y2Changed) - Q_CLASSINFO("DefaultProperty", "stops") - -public: - QQuickPathLinearGradient(QObject *parent = nullptr); - - qreal x1() const; - void setX1(qreal v); - qreal y1() const; - void setY1(qreal v); - qreal x2() const; - void setX2(qreal v); - qreal y2() const; - void setY2(qreal v); - -signals: - void x1Changed(); - void y1Changed(); - void x2Changed(); - void y2Changed(); - -private: - QPointF m_start; - QPointF m_end; -}; - -class Q_QUICK_PRIVATE_EXPORT QQuickVisualPath : public QObject -{ - Q_OBJECT - - Q_PROPERTY(QQuickPath *path READ path WRITE setPath NOTIFY pathChanged) - Q_CLASSINFO("DefaultProperty", "path") - - Q_PROPERTY(QColor strokeColor READ strokeColor WRITE setStrokeColor NOTIFY strokeColorChanged) - Q_PROPERTY(qreal strokeWidth READ strokeWidth WRITE setStrokeWidth NOTIFY strokeWidthChanged) - Q_PROPERTY(QColor fillColor READ fillColor WRITE setFillColor NOTIFY fillColorChanged) - Q_PROPERTY(FillRule fillRule READ fillRule WRITE setFillRule NOTIFY fillRuleChanged) - Q_PROPERTY(JoinStyle joinStyle READ joinStyle WRITE setJoinStyle NOTIFY joinStyleChanged) - Q_PROPERTY(int miterLimit READ miterLimit WRITE setMiterLimit NOTIFY miterLimitChanged) - Q_PROPERTY(CapStyle capStyle READ capStyle WRITE setCapStyle NOTIFY capStyleChanged) - Q_PROPERTY(StrokeStyle strokeStyle READ strokeStyle WRITE setStrokeStyle NOTIFY strokeStyleChanged) - Q_PROPERTY(qreal dashOffset READ dashOffset WRITE setDashOffset NOTIFY dashOffsetChanged) - Q_PROPERTY(QVector dashPattern READ dashPattern WRITE setDashPattern NOTIFY dashPatternChanged) - Q_PROPERTY(QQuickPathGradient *fillGradient READ fillGradient WRITE setFillGradient RESET resetFillGradient) - -public: - enum FillRule { - OddEvenFill = Qt::OddEvenFill, - WindingFill = Qt::WindingFill - }; - Q_ENUM(FillRule) - - enum JoinStyle { - MiterJoin = Qt::MiterJoin, - BevelJoin = Qt::BevelJoin, - RoundJoin = Qt::RoundJoin - }; - Q_ENUM(JoinStyle) - - enum CapStyle { - FlatCap = Qt::FlatCap, - SquareCap = Qt::SquareCap, - RoundCap = Qt::RoundCap - }; - Q_ENUM(CapStyle) - - enum StrokeStyle { - SolidLine = Qt::SolidLine, - DashLine = Qt::DashLine - }; - Q_ENUM(StrokeStyle) - - QQuickVisualPath(QObject *parent = nullptr); - ~QQuickVisualPath(); - - QQuickPath *path() const; - void setPath(QQuickPath *path); - - QColor strokeColor() const; - void setStrokeColor(const QColor &color); - - qreal strokeWidth() const; - void setStrokeWidth(qreal w); - - QColor fillColor() const; - void setFillColor(const QColor &color); - - FillRule fillRule() const; - void setFillRule(FillRule fillRule); - - JoinStyle joinStyle() const; - void setJoinStyle(JoinStyle style); - - int miterLimit() const; - void setMiterLimit(int limit); - - CapStyle capStyle() const; - void setCapStyle(CapStyle style); - - StrokeStyle strokeStyle() const; - void setStrokeStyle(StrokeStyle style); - - qreal dashOffset() const; - void setDashOffset(qreal offset); - - QVector dashPattern() const; - void setDashPattern(const QVector &array); - - QQuickPathGradient *fillGradient() const; - void setFillGradient(QQuickPathGradient *gradient); - void resetFillGradient(); - -Q_SIGNALS: - void changed(); - void pathChanged(); - void strokeColorChanged(); - void strokeWidthChanged(); - void fillColorChanged(); - void fillRuleChanged(); - void joinStyleChanged(); - void miterLimitChanged(); - void capStyleChanged(); - void strokeStyleChanged(); - void dashOffsetChanged(); - void dashPatternChanged(); - void fillGradientChanged(); - -private: - Q_DISABLE_COPY(QQuickVisualPath) - Q_DECLARE_PRIVATE(QQuickVisualPath) - Q_PRIVATE_SLOT(d_func(), void _q_pathChanged()) - Q_PRIVATE_SLOT(d_func(), void _q_fillGradientChanged()) -}; - -class Q_QUICK_PRIVATE_EXPORT QQuickPathItem : public QQuickItem -{ - Q_OBJECT - Q_PROPERTY(RendererType renderer READ rendererType NOTIFY rendererChanged) - Q_PROPERTY(bool asynchronous READ asynchronous WRITE setAsynchronous NOTIFY asynchronousChanged) - Q_PROPERTY(bool enableVendorExtensions READ enableVendorExtensions WRITE setEnableVendorExtensions NOTIFY enableVendorExtensionsChanged) - Q_PROPERTY(Status status READ status NOTIFY statusChanged) - Q_PROPERTY(QQmlListProperty elements READ elements) - Q_CLASSINFO("DefaultProperty", "elements") - -public: - enum RendererType { - UnknownRenderer, - GeometryRenderer, - NvprRenderer, - SoftwareRenderer - }; - Q_ENUM(RendererType) - - enum Status { - Null, - Ready, - Processing - }; - Q_ENUM(Status) - - QQuickPathItem(QQuickItem *parent = nullptr); - ~QQuickPathItem(); - - RendererType rendererType() const; - - bool asynchronous() const; - void setAsynchronous(bool async); - - bool enableVendorExtensions() const; - void setEnableVendorExtensions(bool enable); - - Status status() const; - - QQmlListProperty elements(); - - Q_INVOKABLE void newPath(QQmlV4Function *args); - Q_INVOKABLE void newStrokeFillParams(QQmlV4Function *args); - Q_INVOKABLE void clearVisualPaths(QQmlV4Function *args); - Q_INVOKABLE void commitVisualPaths(QQmlV4Function *args); - Q_INVOKABLE void appendVisualPath(QQmlV4Function *args); - -protected: - QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override; - void updatePolish() override; - void itemChange(ItemChange change, const ItemChangeData &data) override; - void componentComplete() override; - void classBegin() override; - -Q_SIGNALS: - void rendererChanged(); - void asynchronousChanged(); - void enableVendorExtensionsChanged(); - void statusChanged(); - -private: - Q_DISABLE_COPY(QQuickPathItem) - Q_DECLARE_PRIVATE(QQuickPathItem) - Q_PRIVATE_SLOT(d_func(), void _q_visualPathChanged()) -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QQuickPathItem) - -#endif // QQUICKPATHITEM_P_H diff --git a/src/quick/items/qquickpathitem_p_p.h b/src/quick/items/qquickpathitem_p_p.h deleted file mode 100644 index c9a2904a25..0000000000 --- a/src/quick/items/qquickpathitem_p_p.h +++ /dev/null @@ -1,282 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKPATHITEM_P_P_H -#define QQUICKPATHITEM_P_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "qquickpathitem_p.h" -#include "qquickitem_p.h" -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QSGPlainTexture; - -struct QQuickPathItemPath -{ - enum Command { - MoveTo, - LineTo, - QuadTo, - CubicTo, - ArcTo - }; - - QVector cmd; - QVector coords; - - QPainterPath toPainterPath() const; -}; - -struct QQuickPathItemStrokeFillParams -{ - QQuickPathItemStrokeFillParams(); - - QColor strokeColor; - qreal strokeWidth; - QColor fillColor; - QQuickVisualPath::FillRule fillRule; - QQuickVisualPath::JoinStyle joinStyle; - int miterLimit; - QQuickVisualPath::CapStyle capStyle; - QQuickVisualPath::StrokeStyle strokeStyle; - qreal dashOffset; - QVector dashPattern; - QQuickPathGradient *fillGradient; -}; - -class QQuickAbstractPathRenderer -{ -public: - enum Flag { - SupportsAsync = 0x01 - }; - Q_DECLARE_FLAGS(Flags, Flag) - - virtual ~QQuickAbstractPathRenderer() { } - - // Gui thread - virtual void beginSync(int totalCount) = 0; - virtual void endSync(bool async) = 0; - virtual void setAsyncCallback(void (*)(void *), void *) { } - virtual Flags flags() const { return 0; } - // - QML API - virtual void setPath(int index, const QQuickPath *path) = 0; - // - JS API - virtual void setJSPath(int index, const QQuickPathItemPath &path) = 0; - // - stroke/fill parameters - virtual void setStrokeColor(int index, const QColor &color) = 0; - virtual void setStrokeWidth(int index, qreal w) = 0; - virtual void setFillColor(int index, const QColor &color) = 0; - virtual void setFillRule(int index, QQuickVisualPath::FillRule fillRule) = 0; - virtual void setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) = 0; - virtual void setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) = 0; - virtual void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, - qreal dashOffset, const QVector &dashPattern) = 0; - virtual void setFillGradient(int index, QQuickPathGradient *gradient) = 0; - - // Render thread, with gui blocked - virtual void updateNode() = 0; -}; - -Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickAbstractPathRenderer::Flags) - -class QQuickVisualPathPrivate : public QObjectPrivate -{ - Q_DECLARE_PUBLIC(QQuickVisualPath) - -public: - enum Dirty { - DirtyPath = 0x01, - DirtyStrokeColor = 0x02, - DirtyStrokeWidth = 0x04, - DirtyFillColor = 0x08, - DirtyFillRule = 0x10, - DirtyStyle = 0x20, - DirtyDash = 0x40, - DirtyFillGradient = 0x80, - - DirtyAll = 0xFF - }; - - QQuickVisualPathPrivate(); - - void _q_pathChanged(); - void _q_fillGradientChanged(); - - static QQuickVisualPathPrivate *get(QQuickVisualPath *p) { return p->d_func(); } - - QQuickPath *path; - int dirty; - QQuickPathItemStrokeFillParams sfp; -}; - -class QQuickPathItemPrivate : public QQuickItemPrivate -{ - Q_DECLARE_PUBLIC(QQuickPathItem) - -public: - QQuickPathItemPrivate(); - ~QQuickPathItemPrivate(); - - void createRenderer(); - QSGNode *createNode(); - void sync(); - - void _q_visualPathChanged(); - void setStatus(QQuickPathItem::Status newStatus); - - static QQuickPathItemPrivate *get(QQuickPathItem *item) { return item->d_func(); } - - bool componentComplete; - bool vpChanged; - QQuickPathItem::RendererType rendererType; - bool async; - QQuickPathItem::Status status; - QQuickAbstractPathRenderer *renderer; - - struct { - QVector vp; - } qmlData; - - struct { - bool isValid() const { Q_ASSERT(paths.count() == sfp.count()); return !paths.isEmpty(); } - QVector paths; - QVector sfp; - } jsData; - - bool enableVendorExts; -}; - -class QQuickPathItemPathObject : public QObject -{ - Q_OBJECT - -public: - QQuickPathItemPathObject(QObject *parent = nullptr) : QObject(parent) { } - - void setV4Engine(QV4::ExecutionEngine *engine); - QV4::ReturnedValue v4value() const { return m_v4value.value(); } - - QQuickPathItemPath path; - - void clear(); - -private: - QV4::PersistentValue m_v4value; -}; - -class QQuickPathItemStrokeFillParamsObject : public QObject -{ - Q_OBJECT - -public: - QQuickPathItemStrokeFillParamsObject(QObject *parent = nullptr) : QObject(parent) { } - - void setV4Engine(QV4::ExecutionEngine *engine); - QV4::ReturnedValue v4value() const { return m_v4value.value(); } - - QQuickPathItemStrokeFillParams sfp; - QV4::PersistentValue v4fillGradient; - - void clear(); - -private: - QV4::PersistentValue m_v4value; -}; - -#ifndef QT_NO_OPENGL - -class QQuickPathItemGradientCache : public QOpenGLSharedResource -{ -public: - struct GradientDesc { - QGradientStops stops; - QPointF start; - QPointF end; - QQuickPathGradient::SpreadMode spread; - bool operator==(const GradientDesc &other) const - { - return start == other.start && end == other.end && spread == other.spread - && stops == other.stops; - } - }; - - QQuickPathItemGradientCache(QOpenGLContext *context) : QOpenGLSharedResource(context->shareGroup()) { } - ~QQuickPathItemGradientCache(); - - void invalidateResource() override; - void freeResource(QOpenGLContext *) override; - - QSGTexture *get(const GradientDesc &grad); - - static QQuickPathItemGradientCache *currentCache(); - -private: - QHash m_cache; -}; - -inline uint qHash(const QQuickPathItemGradientCache::GradientDesc &v, uint seed = 0) -{ - uint h = seed; - h += v.start.x() + v.end.y() + v.spread; - for (int i = 0; i < 3 && i < v.stops.count(); ++i) - h += v.stops[i].second.rgba(); - return h; -} - -#endif // QT_NO_OPENGL - -QT_END_NAMESPACE - -#endif diff --git a/src/quick/items/qquickpathitemgenericrenderer.cpp b/src/quick/items/qquickpathitemgenericrenderer.cpp deleted file mode 100644 index 4e8fe55df2..0000000000 --- a/src/quick/items/qquickpathitemgenericrenderer.cpp +++ /dev/null @@ -1,775 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qquickpathitemgenericrenderer_p.h" -#include -#include -#include - -#ifndef QT_NO_OPENGL -#include -#include -#include -#include -#endif - -QT_BEGIN_NAMESPACE - -static const qreal TRI_SCALE = 1; - -struct ColoredVertex // must match QSGGeometry::ColoredPoint2D -{ - float x, y; - QQuickPathItemGenericRenderer::Color4ub color; - void set(float nx, float ny, QQuickPathItemGenericRenderer::Color4ub ncolor) - { - x = nx; y = ny; color = ncolor; - } -}; - -static inline QQuickPathItemGenericRenderer::Color4ub colorToColor4ub(const QColor &c) -{ - QQuickPathItemGenericRenderer::Color4ub color = { - uchar(qRound(c.redF() * c.alphaF() * 255)), - uchar(qRound(c.greenF() * c.alphaF() * 255)), - uchar(qRound(c.blueF() * c.alphaF() * 255)), - uchar(qRound(c.alphaF() * 255)) - }; - return color; -} - -QQuickPathItemGenericStrokeFillNode::QQuickPathItemGenericStrokeFillNode(QQuickWindow *window) - : m_geometry(new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), 0, 0)), - m_window(window), - m_material(nullptr) -{ - setGeometry(m_geometry); - activateMaterial(MatSolidColor); -#ifdef QSG_RUNTIME_DESCRIPTION - qsgnode_set_description(this, QLatin1String("stroke-fill")); -#endif -} - -QQuickPathItemGenericStrokeFillNode::~QQuickPathItemGenericStrokeFillNode() -{ - delete m_geometry; -} - -void QQuickPathItemGenericStrokeFillNode::activateMaterial(Material m) -{ - switch (m) { - case MatSolidColor: - // Use vertexcolor material. Items with different colors remain batchable - // this way, at the expense of having to provide per-vertex color values. - if (!m_solidColorMaterial) - m_solidColorMaterial.reset(QQuickPathItemGenericMaterialFactory::createVertexColor(m_window)); - m_material = m_solidColorMaterial.data(); - break; - case MatLinearGradient: - if (!m_linearGradientMaterial) - m_linearGradientMaterial.reset(QQuickPathItemGenericMaterialFactory::createLinearGradient(m_window, this)); - m_material = m_linearGradientMaterial.data(); - break; - default: - qWarning("Unknown material %d", m); - return; - } - - if (material() != m_material) - setMaterial(m_material); -} - -static bool q_supportsElementIndexUint(QSGRendererInterface::GraphicsApi api) -{ - static bool elementIndexUint = true; -#ifndef QT_NO_OPENGL - if (api == QSGRendererInterface::OpenGL) { - static bool elementIndexUintChecked = false; - if (!elementIndexUintChecked) { - elementIndexUintChecked = true; - QOpenGLContext *context = QOpenGLContext::currentContext(); - QScopedPointer dummyContext; - QScopedPointer dummySurface; - bool ok = true; - if (!context) { - dummyContext.reset(new QOpenGLContext); - dummyContext->create(); - context = dummyContext.data(); - dummySurface.reset(new QOffscreenSurface); - dummySurface->setFormat(context->format()); - dummySurface->create(); - ok = context->makeCurrent(dummySurface.data()); - } - if (ok) { - elementIndexUint = static_cast(context->functions())->hasOpenGLExtension( - QOpenGLExtensions::ElementIndexUint); - } - } - } -#else - Q_UNUSED(api); -#endif - return elementIndexUint; -} - -QQuickPathItemGenericRenderer::~QQuickPathItemGenericRenderer() -{ - for (VisualPathData &d : m_vp) { - if (d.pendingFill) - d.pendingFill->orphaned = true; - if (d.pendingStroke) - d.pendingStroke->orphaned = true; - } -} - -// sync, and so triangulation too, happens on the gui thread -// - except when async is set, in which case triangulation is moved to worker threads - -void QQuickPathItemGenericRenderer::beginSync(int totalCount) -{ - if (m_vp.count() != totalCount) { - m_vp.resize(totalCount); - m_accDirty |= DirtyList; - } - for (VisualPathData &d : m_vp) - d.syncDirty = 0; -} - -void QQuickPathItemGenericRenderer::setPath(int index, const QQuickPath *path) -{ - VisualPathData &d(m_vp[index]); - d.path = path ? path->path() : QPainterPath(); - d.syncDirty |= DirtyFillGeom | DirtyStrokeGeom; -} - -void QQuickPathItemGenericRenderer::setJSPath(int index, const QQuickPathItemPath &path) -{ - VisualPathData &d(m_vp[index]); - d.path = path.toPainterPath(); - d.syncDirty |= DirtyFillGeom | DirtyStrokeGeom; -} - -void QQuickPathItemGenericRenderer::setStrokeColor(int index, const QColor &color) -{ - VisualPathData &d(m_vp[index]); - d.strokeColor = colorToColor4ub(color); - d.syncDirty |= DirtyColor; -} - -void QQuickPathItemGenericRenderer::setStrokeWidth(int index, qreal w) -{ - VisualPathData &d(m_vp[index]); - d.strokeWidth = w; - if (w >= 0.0f) - d.pen.setWidthF(w); - d.syncDirty |= DirtyStrokeGeom; -} - -void QQuickPathItemGenericRenderer::setFillColor(int index, const QColor &color) -{ - VisualPathData &d(m_vp[index]); - d.fillColor = colorToColor4ub(color); - d.syncDirty |= DirtyColor; -} - -void QQuickPathItemGenericRenderer::setFillRule(int index, QQuickVisualPath::FillRule fillRule) -{ - VisualPathData &d(m_vp[index]); - d.fillRule = Qt::FillRule(fillRule); - d.syncDirty |= DirtyFillGeom; -} - -void QQuickPathItemGenericRenderer::setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) -{ - VisualPathData &d(m_vp[index]); - d.pen.setJoinStyle(Qt::PenJoinStyle(joinStyle)); - d.pen.setMiterLimit(miterLimit); - d.syncDirty |= DirtyStrokeGeom; -} - -void QQuickPathItemGenericRenderer::setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) -{ - VisualPathData &d(m_vp[index]); - d.pen.setCapStyle(Qt::PenCapStyle(capStyle)); - d.syncDirty |= DirtyStrokeGeom; -} - -void QQuickPathItemGenericRenderer::setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, - qreal dashOffset, const QVector &dashPattern) -{ - VisualPathData &d(m_vp[index]); - d.pen.setStyle(Qt::PenStyle(strokeStyle)); - if (strokeStyle == QQuickVisualPath::DashLine) { - d.pen.setDashPattern(dashPattern); - d.pen.setDashOffset(dashOffset); - } - d.syncDirty |= DirtyStrokeGeom; -} - -void QQuickPathItemGenericRenderer::setFillGradient(int index, QQuickPathGradient *gradient) -{ - VisualPathData &d(m_vp[index]); - d.fillGradientActive = gradient != nullptr; - if (gradient) { - d.fillGradient.stops = gradient->sortedGradientStops(); - d.fillGradient.spread = gradient->spread(); - if (QQuickPathLinearGradient *g = qobject_cast(gradient)) { - d.fillGradient.start = QPointF(g->x1(), g->y1()); - d.fillGradient.end = QPointF(g->x2(), g->y2()); - } else { - Q_UNREACHABLE(); - } - } - d.syncDirty |= DirtyFillGradient; -} - -void QQuickPathItemFillRunnable::run() -{ - QQuickPathItemGenericRenderer::triangulateFill(path, fillColor, &fillVertices, &fillIndices, &indexType, supportsElementIndexUint); - emit done(this); -} - -void QQuickPathItemStrokeRunnable::run() -{ - QQuickPathItemGenericRenderer::triangulateStroke(path, pen, strokeColor, &strokeVertices, clipSize); - emit done(this); -} - -void QQuickPathItemGenericRenderer::setAsyncCallback(void (*callback)(void *), void *data) -{ - m_asyncCallback = callback; - m_asyncCallbackData = data; -} - -static QThreadPool *pathWorkThreadPool = nullptr; - -static void deletePathWorkThreadPool() -{ - delete pathWorkThreadPool; - pathWorkThreadPool = nullptr; -} - -void QQuickPathItemGenericRenderer::endSync(bool async) -{ - bool didKickOffAsync = false; - - for (int i = 0; i < m_vp.count(); ++i) { - VisualPathData &d(m_vp[i]); - if (!d.syncDirty) - continue; - - m_accDirty |= d.syncDirty; - - // Use a shadow dirty flag in order to avoid losing state in case there are - // multiple syncs with different dirty flags before we get to updateNode() - // on the render thread (with the gui thread blocked). For our purposes - // here syncDirty is still required since geometry regeneration must only - // happen when there was an actual change in this particular sync round. - d.effectiveDirty |= d.syncDirty; - - if (d.path.isEmpty()) { - d.fillVertices.clear(); - d.fillIndices.clear(); - d.strokeVertices.clear(); - continue; - } - - if (async && !pathWorkThreadPool) { - qAddPostRoutine(deletePathWorkThreadPool); - pathWorkThreadPool = new QThreadPool; - const int idealCount = QThread::idealThreadCount(); - pathWorkThreadPool->setMaxThreadCount(idealCount > 0 ? idealCount * 2 : 4); - } - - if ((d.syncDirty & DirtyFillGeom) && d.fillColor.a) { - d.path.setFillRule(d.fillRule); - if (m_api == QSGRendererInterface::Unknown) - m_api = m_item->window()->rendererInterface()->graphicsApi(); - if (async) { - QQuickPathItemFillRunnable *r = new QQuickPathItemFillRunnable; - r->setAutoDelete(false); - if (d.pendingFill) - d.pendingFill->orphaned = true; - d.pendingFill = r; - r->path = d.path; - r->fillColor = d.fillColor; - r->supportsElementIndexUint = q_supportsElementIndexUint(m_api); - // Unlikely in practice but in theory m_vp could be - // resized. Therefore, capture 'i' instead of 'd'. - QObject::connect(r, &QQuickPathItemFillRunnable::done, qApp, [this, i](QQuickPathItemFillRunnable *r) { - // Bail out when orphaned (meaning either another run was - // started after this one, or the renderer got destroyed). - if (!r->orphaned && i < m_vp.count()) { - VisualPathData &d(m_vp[i]); - d.fillVertices = r->fillVertices; - d.fillIndices = r->fillIndices; - d.indexType = r->indexType; - d.pendingFill = nullptr; - d.effectiveDirty |= DirtyFillGeom; - maybeUpdateAsyncItem(); - } - r->deleteLater(); - }); - didKickOffAsync = true; - pathWorkThreadPool->start(r); - } else { - triangulateFill(d.path, d.fillColor, &d.fillVertices, &d.fillIndices, &d.indexType, q_supportsElementIndexUint(m_api)); - } - } - - if ((d.syncDirty & DirtyStrokeGeom) && d.strokeWidth >= 0.0f && d.strokeColor.a) { - if (async) { - QQuickPathItemStrokeRunnable *r = new QQuickPathItemStrokeRunnable; - r->setAutoDelete(false); - if (d.pendingStroke) - d.pendingStroke->orphaned = true; - d.pendingStroke = r; - r->path = d.path; - r->pen = d.pen; - r->strokeColor = d.strokeColor; - r->clipSize = QSize(m_item->width(), m_item->height()); - QObject::connect(r, &QQuickPathItemStrokeRunnable::done, qApp, [this, i](QQuickPathItemStrokeRunnable *r) { - if (!r->orphaned && i < m_vp.count()) { - VisualPathData &d(m_vp[i]); - d.strokeVertices = r->strokeVertices; - d.pendingStroke = nullptr; - d.effectiveDirty |= DirtyStrokeGeom; - maybeUpdateAsyncItem(); - } - r->deleteLater(); - }); - didKickOffAsync = true; - pathWorkThreadPool->start(r); - } else { - triangulateStroke(d.path, d.pen, d.strokeColor, &d.strokeVertices, - QSize(m_item->width(), m_item->height())); - } - } - } - - if (!didKickOffAsync && async && m_asyncCallback) - m_asyncCallback(m_asyncCallbackData); -} - -void QQuickPathItemGenericRenderer::maybeUpdateAsyncItem() -{ - for (const VisualPathData &d : qAsConst(m_vp)) { - if (d.pendingFill || d.pendingStroke) - return; - } - m_accDirty |= DirtyFillGeom | DirtyStrokeGeom; - m_item->update(); - if (m_asyncCallback) - m_asyncCallback(m_asyncCallbackData); -} - -// the stroke/fill triangulation functions may be invoked either on the gui -// thread or some worker thread and must thus be self-contained. -void QQuickPathItemGenericRenderer::triangulateFill(const QPainterPath &path, - const Color4ub &fillColor, - VertexContainerType *fillVertices, - IndexContainerType *fillIndices, - QSGGeometry::Type *indexType, - bool supportsElementIndexUint) -{ - const QVectorPath &vp = qtVectorPathForPath(path); - - QTriangleSet ts = qTriangulate(vp, QTransform::fromScale(TRI_SCALE, TRI_SCALE), 1, supportsElementIndexUint); - const int vertexCount = ts.vertices.count() / 2; // just a qreal vector with x,y hence the / 2 - fillVertices->resize(vertexCount); - ColoredVertex *vdst = reinterpret_cast(fillVertices->data()); - const qreal *vsrc = ts.vertices.constData(); - for (int i = 0; i < vertexCount; ++i) - vdst[i].set(vsrc[i * 2] / TRI_SCALE, vsrc[i * 2 + 1] / TRI_SCALE, fillColor); - - size_t indexByteSize; - if (ts.indices.type() == QVertexIndexVector::UnsignedShort) { - *indexType = QSGGeometry::UnsignedShortType; - // fillIndices is still QVector. Just resize to N/2 and pack - // the N quint16s into it. - fillIndices->resize(ts.indices.size() / 2); - indexByteSize = ts.indices.size() * sizeof(quint16); - } else { - *indexType = QSGGeometry::UnsignedIntType; - fillIndices->resize(ts.indices.size()); - indexByteSize = ts.indices.size() * sizeof(quint32); - } - memcpy(fillIndices->data(), ts.indices.data(), indexByteSize); -} - -void QQuickPathItemGenericRenderer::triangulateStroke(const QPainterPath &path, - const QPen &pen, - const Color4ub &strokeColor, - VertexContainerType *strokeVertices, - const QSize &clipSize) -{ - const QVectorPath &vp = qtVectorPathForPath(path); - const QRectF clip(QPointF(0, 0), clipSize); - const qreal inverseScale = 1.0 / TRI_SCALE; - - QTriangulatingStroker stroker; - stroker.setInvScale(inverseScale); - - if (pen.style() == Qt::SolidLine) { - stroker.process(vp, pen, clip, 0); - } else { - QDashedStrokeProcessor dashStroker; - dashStroker.setInvScale(inverseScale); - dashStroker.process(vp, pen, clip, 0); - QVectorPath dashStroke(dashStroker.points(), dashStroker.elementCount(), - dashStroker.elementTypes(), 0); - stroker.process(dashStroke, pen, clip, 0); - } - - if (!stroker.vertexCount()) { - strokeVertices->clear(); - return; - } - - const int vertexCount = stroker.vertexCount() / 2; // just a float vector with x,y hence the / 2 - strokeVertices->resize(vertexCount); - ColoredVertex *vdst = reinterpret_cast(strokeVertices->data()); - const float *vsrc = stroker.vertices(); - for (int i = 0; i < vertexCount; ++i) - vdst[i].set(vsrc[i * 2], vsrc[i * 2 + 1], strokeColor); -} - -void QQuickPathItemGenericRenderer::setRootNode(QQuickPathItemGenericNode *node) -{ - if (m_rootNode != node) { - m_rootNode = node; - m_accDirty |= DirtyList; - } -} - -// on the render thread with gui blocked -void QQuickPathItemGenericRenderer::updateNode() -{ - if (!m_rootNode || !m_accDirty) - return; - -// [ m_rootNode ] -// / / / -// #0 [ fill ] [ stroke ] [ next ] -// / / | -// #1 [ fill ] [ stroke ] [ next ] -// / / | -// #2 [ fill ] [ stroke ] [ next ] -// ... -// ... - - QQuickPathItemGenericNode **nodePtr = &m_rootNode; - QQuickPathItemGenericNode *prevNode = nullptr; - - for (VisualPathData &d : m_vp) { - if (!*nodePtr) { - *nodePtr = new QQuickPathItemGenericNode; - prevNode->m_next = *nodePtr; - prevNode->appendChildNode(*nodePtr); - } - - QQuickPathItemGenericNode *node = *nodePtr; - - if (m_accDirty & DirtyList) - d.effectiveDirty |= DirtyFillGeom | DirtyStrokeGeom | DirtyColor | DirtyFillGradient; - - if (!d.effectiveDirty) { - prevNode = node; - nodePtr = &node->m_next; - continue; - } - - if (d.fillColor.a == 0) { - delete node->m_fillNode; - node->m_fillNode = nullptr; - } else if (!node->m_fillNode) { - node->m_fillNode = new QQuickPathItemGenericStrokeFillNode(m_item->window()); - if (node->m_strokeNode) - node->removeChildNode(node->m_strokeNode); - node->appendChildNode(node->m_fillNode); - if (node->m_strokeNode) - node->appendChildNode(node->m_strokeNode); - d.effectiveDirty |= DirtyFillGeom; - } - - if (d.strokeWidth < 0.0f || d.strokeColor.a == 0) { - delete node->m_strokeNode; - node->m_strokeNode = nullptr; - } else if (!node->m_strokeNode) { - node->m_strokeNode = new QQuickPathItemGenericStrokeFillNode(m_item->window()); - node->appendChildNode(node->m_strokeNode); - d.effectiveDirty |= DirtyStrokeGeom; - } - - updateFillNode(&d, node); - updateStrokeNode(&d, node); - - d.effectiveDirty = 0; - - prevNode = node; - nodePtr = &node->m_next; - } - - if (*nodePtr && prevNode) { - prevNode->removeChildNode(*nodePtr); - delete *nodePtr; - *nodePtr = nullptr; - } - - m_accDirty = 0; -} - -void QQuickPathItemGenericRenderer::updateShadowDataInNode(VisualPathData *d, QQuickPathItemGenericStrokeFillNode *n) -{ - if (d->fillGradientActive) { - if (d->effectiveDirty & DirtyFillGradient) - n->m_fillGradient = d->fillGradient; - } -} - -void QQuickPathItemGenericRenderer::updateFillNode(VisualPathData *d, QQuickPathItemGenericNode *node) -{ - if (!node->m_fillNode) - return; - if (!(d->effectiveDirty & (DirtyFillGeom | DirtyColor | DirtyFillGradient))) - return; - - // Make a copy of the data that will be accessed by the material on - // the render thread. This must be done even when we bail out below. - QQuickPathItemGenericStrokeFillNode *n = node->m_fillNode; - updateShadowDataInNode(d, n); - - QSGGeometry *g = n->m_geometry; - if (d->fillVertices.isEmpty()) { - if (g->vertexCount() || g->indexCount()) { - g->allocate(0, 0); - n->markDirty(QSGNode::DirtyGeometry); - } - return; - } - - if (d->fillGradientActive) { - n->activateMaterial(QQuickPathItemGenericStrokeFillNode::MatLinearGradient); - if (d->effectiveDirty & DirtyFillGradient) { - // Gradients are implemented via a texture-based material. - n->markDirty(QSGNode::DirtyMaterial); - // stop here if only the gradient changed; no need to touch the geometry - if (!(d->effectiveDirty & DirtyFillGeom)) - return; - } - } else { - n->activateMaterial(QQuickPathItemGenericStrokeFillNode::MatSolidColor); - // fast path for updating only color values when no change in vertex positions - if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyFillGeom)) { - ColoredVertex *vdst = reinterpret_cast(g->vertexData()); - for (int i = 0; i < g->vertexCount(); ++i) - vdst[i].set(vdst[i].x, vdst[i].y, d->fillColor); - n->markDirty(QSGNode::DirtyGeometry); - return; - } - } - - const int indexCount = d->indexType == QSGGeometry::UnsignedShortType - ? d->fillIndices.count() * 2 : d->fillIndices.count(); - if (g->indexType() != d->indexType) { - g = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), - d->fillVertices.count(), indexCount, d->indexType); - n->setGeometry(g); - delete n->m_geometry; - n->m_geometry = g; - } else { - g->allocate(d->fillVertices.count(), indexCount); - } - g->setDrawingMode(QSGGeometry::DrawTriangles); - memcpy(g->vertexData(), d->fillVertices.constData(), g->vertexCount() * g->sizeOfVertex()); - memcpy(g->indexData(), d->fillIndices.constData(), g->indexCount() * g->sizeOfIndex()); - - n->markDirty(QSGNode::DirtyGeometry); -} - -void QQuickPathItemGenericRenderer::updateStrokeNode(VisualPathData *d, QQuickPathItemGenericNode *node) -{ - if (!node->m_strokeNode) - return; - if (!(d->effectiveDirty & (DirtyStrokeGeom | DirtyColor))) - return; - - QQuickPathItemGenericStrokeFillNode *n = node->m_strokeNode; - QSGGeometry *g = n->m_geometry; - if (d->strokeVertices.isEmpty()) { - if (g->vertexCount() || g->indexCount()) { - g->allocate(0, 0); - n->markDirty(QSGNode::DirtyGeometry); - } - return; - } - - n->markDirty(QSGNode::DirtyGeometry); - - // Async loading runs update once, bails out above, then updates again once - // ready. Set the material dirty then. This is in-line with fill where the - // first activateMaterial() achieves the same. - if (!g->vertexCount()) - n->markDirty(QSGNode::DirtyMaterial); - - if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyStrokeGeom)) { - ColoredVertex *vdst = reinterpret_cast(g->vertexData()); - for (int i = 0; i < g->vertexCount(); ++i) - vdst[i].set(vdst[i].x, vdst[i].y, d->strokeColor); - return; - } - - g->allocate(d->strokeVertices.count(), 0); - g->setDrawingMode(QSGGeometry::DrawTriangleStrip); - memcpy(g->vertexData(), d->strokeVertices.constData(), g->vertexCount() * g->sizeOfVertex()); -} - -QSGMaterial *QQuickPathItemGenericMaterialFactory::createVertexColor(QQuickWindow *window) -{ - QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi(); - -#ifndef QT_NO_OPENGL - if (api == QSGRendererInterface::OpenGL) // ### so much for "generic"... - return new QSGVertexColorMaterial; -#endif - - qWarning("Vertex-color material: Unsupported graphics API %d", api); - return nullptr; -} - -QSGMaterial *QQuickPathItemGenericMaterialFactory::createLinearGradient(QQuickWindow *window, - QQuickPathItemGenericStrokeFillNode *node) -{ - QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi(); - -#ifndef QT_NO_OPENGL - if (api == QSGRendererInterface::OpenGL) // ### so much for "generic"... - return new QQuickPathItemLinearGradientMaterial(node); -#endif - - qWarning("Linear gradient material: Unsupported graphics API %d", api); - return nullptr; -} - -#ifndef QT_NO_OPENGL - -QSGMaterialType QQuickPathItemLinearGradientShader::type; - -QQuickPathItemLinearGradientShader::QQuickPathItemLinearGradientShader() -{ - setShaderSourceFile(QOpenGLShader::Vertex, - QStringLiteral(":/qt-project.org/items/shaders/lineargradient.vert")); - setShaderSourceFile(QOpenGLShader::Fragment, - QStringLiteral(":/qt-project.org/items/shaders/lineargradient.frag")); -} - -void QQuickPathItemLinearGradientShader::initialize() -{ - m_opacityLoc = program()->uniformLocation("opacity"); - m_matrixLoc = program()->uniformLocation("matrix"); - m_gradStartLoc = program()->uniformLocation("gradStart"); - m_gradEndLoc = program()->uniformLocation("gradEnd"); -} - -void QQuickPathItemLinearGradientShader::updateState(const RenderState &state, QSGMaterial *mat, QSGMaterial *) -{ - QQuickPathItemLinearGradientMaterial *m = static_cast(mat); - - if (state.isOpacityDirty()) - program()->setUniformValue(m_opacityLoc, state.opacity()); - - if (state.isMatrixDirty()) - program()->setUniformValue(m_matrixLoc, state.combinedMatrix()); - - QQuickPathItemGenericStrokeFillNode *node = m->node(); - program()->setUniformValue(m_gradStartLoc, QVector2D(node->m_fillGradient.start)); - program()->setUniformValue(m_gradEndLoc, QVector2D(node->m_fillGradient.end)); - - QSGTexture *tx = QQuickPathItemGradientCache::currentCache()->get(node->m_fillGradient); - tx->bind(); -} - -char const *const *QQuickPathItemLinearGradientShader::attributeNames() const -{ - static const char *const attr[] = { "vertexCoord", "vertexColor", nullptr }; - return attr; -} - -int QQuickPathItemLinearGradientMaterial::compare(const QSGMaterial *other) const -{ - Q_ASSERT(other && type() == other->type()); - const QQuickPathItemLinearGradientMaterial *m = static_cast(other); - - QQuickPathItemGenericStrokeFillNode *a = node(); - QQuickPathItemGenericStrokeFillNode *b = m->node(); - Q_ASSERT(a && b); - if (a == b) - return 0; - - const QQuickPathItemGradientCache::GradientDesc *ga = &a->m_fillGradient; - const QQuickPathItemGradientCache::GradientDesc *gb = &b->m_fillGradient; - - if (int d = ga->spread - gb->spread) - return d; - - if (int d = ga->start.x() - gb->start.x()) - return d; - if (int d = ga->start.y() - gb->start.y()) - return d; - if (int d = ga->end.x() - gb->end.x()) - return d; - if (int d = ga->end.y() - gb->end.y()) - return d; - - if (int d = ga->stops.count() - gb->stops.count()) - return d; - - for (int i = 0; i < ga->stops.count(); ++i) { - if (int d = ga->stops[i].first - gb->stops[i].first) - return d; - if (int d = ga->stops[i].second.rgba() - gb->stops[i].second.rgba()) - return d; - } - - return 0; -} - -#endif // QT_NO_OPENGL - -QT_END_NAMESPACE diff --git a/src/quick/items/qquickpathitemgenericrenderer_p.h b/src/quick/items/qquickpathitemgenericrenderer_p.h deleted file mode 100644 index 70a9e88d2f..0000000000 --- a/src/quick/items/qquickpathitemgenericrenderer_p.h +++ /dev/null @@ -1,303 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKPATHITEMGENERICRENDERER_P_H -#define QQUICKPATHITEMGENERICRENDERER_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 "qquickpathitem_p_p.h" -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QQuickPathItemGenericNode; -class QQuickPathItemGenericStrokeFillNode; -class QQuickPathItemFillRunnable; -class QQuickPathItemStrokeRunnable; - -class QQuickPathItemGenericRenderer : public QQuickAbstractPathRenderer -{ -public: - enum Dirty { - DirtyFillGeom = 0x01, - DirtyStrokeGeom = 0x02, - DirtyColor = 0x04, - DirtyFillGradient = 0x08, - DirtyList = 0x10 // only for accDirty - }; - - QQuickPathItemGenericRenderer(QQuickItem *item) - : m_item(item), - m_api(QSGRendererInterface::Unknown), - m_rootNode(nullptr), - m_accDirty(0), - m_asyncCallback(nullptr) - { } - ~QQuickPathItemGenericRenderer(); - - void beginSync(int totalCount) override; - void setPath(int index, const QQuickPath *path) override; - void setJSPath(int index, const QQuickPathItemPath &path) override; - 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, QQuickVisualPath::FillRule fillRule) override; - void setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) override; - void setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) override; - void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, - qreal dashOffset, const QVector &dashPattern) override; - void setFillGradient(int index, QQuickPathGradient *gradient) override; - void endSync(bool async) override; - void setAsyncCallback(void (*)(void *), void *) override; - Flags flags() const override { return SupportsAsync; } - - void updateNode() override; - - void setRootNode(QQuickPathItemGenericNode *node); - - struct Color4ub { unsigned char r, g, b, a; }; - typedef QVector VertexContainerType; - typedef QVector IndexContainerType; - - static void triangulateFill(const QPainterPath &path, - const Color4ub &fillColor, - VertexContainerType *fillVertices, - IndexContainerType *fillIndices, - QSGGeometry::Type *indexType, - bool supportsElementIndexUint); - static void triangulateStroke(const QPainterPath &path, - const QPen &pen, - const Color4ub &strokeColor, - VertexContainerType *strokeVertices, - const QSize &clipSize); - -private: - void maybeUpdateAsyncItem(); - - struct VisualPathData { - float strokeWidth; - QPen pen; - Color4ub strokeColor; - Color4ub fillColor; - Qt::FillRule fillRule; - QPainterPath path; - bool fillGradientActive; - QQuickPathItemGradientCache::GradientDesc fillGradient; - VertexContainerType fillVertices; - IndexContainerType fillIndices; - QSGGeometry::Type indexType; - VertexContainerType strokeVertices; - int syncDirty; - int effectiveDirty = 0; - QQuickPathItemFillRunnable *pendingFill = nullptr; - QQuickPathItemStrokeRunnable *pendingStroke = nullptr; - }; - - void updateShadowDataInNode(VisualPathData *d, QQuickPathItemGenericStrokeFillNode *n); - void updateFillNode(VisualPathData *d, QQuickPathItemGenericNode *node); - void updateStrokeNode(VisualPathData *d, QQuickPathItemGenericNode *node); - - QQuickItem *m_item; - QSGRendererInterface::GraphicsApi m_api; - QQuickPathItemGenericNode *m_rootNode; - QVector m_vp; - int m_accDirty; - void (*m_asyncCallback)(void *); - void *m_asyncCallbackData; -}; - -class QQuickPathItemFillRunnable : public QObject, public QRunnable -{ - Q_OBJECT - -public: - void run() override; - - bool orphaned = false; - - // input - QPainterPath path; - QQuickPathItemGenericRenderer::Color4ub fillColor; - bool supportsElementIndexUint; - - // output - QQuickPathItemGenericRenderer::VertexContainerType fillVertices; - QQuickPathItemGenericRenderer::IndexContainerType fillIndices; - QSGGeometry::Type indexType; - -Q_SIGNALS: - void done(QQuickPathItemFillRunnable *self); -}; - -class QQuickPathItemStrokeRunnable : public QObject, public QRunnable -{ - Q_OBJECT - -public: - void run() override; - - bool orphaned = false; - - // input - QPainterPath path; - QPen pen; - QQuickPathItemGenericRenderer::Color4ub strokeColor; - QSize clipSize; - - // output - QQuickPathItemGenericRenderer::VertexContainerType strokeVertices; - -Q_SIGNALS: - void done(QQuickPathItemStrokeRunnable *self); -}; - -class QQuickPathItemGenericStrokeFillNode : public QSGGeometryNode -{ -public: - QQuickPathItemGenericStrokeFillNode(QQuickWindow *window); - ~QQuickPathItemGenericStrokeFillNode(); - - enum Material { - MatSolidColor, - MatLinearGradient - }; - - void activateMaterial(Material m); - - QQuickWindow *window() const { return m_window; } - - // shadow data for custom materials - QQuickPathItemGradientCache::GradientDesc m_fillGradient; - -private: - QSGGeometry *m_geometry; - QQuickWindow *m_window; - QSGMaterial *m_material; - QScopedPointer m_solidColorMaterial; - QScopedPointer m_linearGradientMaterial; - - friend class QQuickPathItemGenericRenderer; -}; - -class QQuickPathItemGenericNode : public QSGNode -{ -public: - QQuickPathItemGenericStrokeFillNode *m_fillNode = nullptr; - QQuickPathItemGenericStrokeFillNode *m_strokeNode = nullptr; - QQuickPathItemGenericNode *m_next = nullptr; -}; - -class QQuickPathItemGenericMaterialFactory -{ -public: - static QSGMaterial *createVertexColor(QQuickWindow *window); - static QSGMaterial *createLinearGradient(QQuickWindow *window, QQuickPathItemGenericStrokeFillNode *node); -}; - -#ifndef QT_NO_OPENGL - -class QQuickPathItemLinearGradientShader : public QSGMaterialShader -{ -public: - QQuickPathItemLinearGradientShader(); - - void initialize() override; - void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect) override; - char const *const *attributeNames() const override; - - static QSGMaterialType type; - -private: - int m_opacityLoc; - int m_matrixLoc; - int m_gradStartLoc; - int m_gradEndLoc; -}; - -class QQuickPathItemLinearGradientMaterial : public QSGMaterial -{ -public: - QQuickPathItemLinearGradientMaterial(QQuickPathItemGenericStrokeFillNode *node) - : m_node(node) - { - // Passing RequiresFullMatrix is essential in order to prevent the - // batch renderer from baking in simple, translate-only transforms into - // the vertex data. The shader will rely on the fact that - // vertexCoord.xy is the PathItem-space coordinate and so no modifications - // are welcome. - setFlag(Blending | RequiresFullMatrix); - } - - QSGMaterialType *type() const override - { - return &QQuickPathItemLinearGradientShader::type; - } - - int compare(const QSGMaterial *other) const override; - - QSGMaterialShader *createShader() const override - { - return new QQuickPathItemLinearGradientShader; - } - - QQuickPathItemGenericStrokeFillNode *node() const { return m_node; } - -private: - QQuickPathItemGenericStrokeFillNode *m_node; -}; - -#endif // QT_NO_OPENGL - -QT_END_NAMESPACE - -#endif // QQUICKPATHITEMGENERICRENDERER_P_H diff --git a/src/quick/items/qquickpathitemnvprrenderer.cpp b/src/quick/items/qquickpathitemnvprrenderer.cpp deleted file mode 100644 index f8504f9985..0000000000 --- a/src/quick/items/qquickpathitemnvprrenderer.cpp +++ /dev/null @@ -1,923 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qquickpathitemnvprrenderer_p.h" -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -void QQuickPathItemNvprRenderer::beginSync(int totalCount) -{ - if (m_vp.count() != totalCount) { - m_vp.resize(totalCount); - m_accDirty |= DirtyList; - } -} - -void QQuickPathItemNvprRenderer::setPath(int index, const QQuickPath *path) -{ - VisualPathGuiData &d(m_vp[index]); - convertPath(path, &d); - d.dirty |= DirtyPath; - m_accDirty |= DirtyPath; -} - -void QQuickPathItemNvprRenderer::setJSPath(int index, const QQuickPathItemPath &path) -{ - VisualPathGuiData &d(m_vp[index]); - convertJSPath(path, &d); - d.dirty |= DirtyPath; - m_accDirty |= DirtyPath; -} - -void QQuickPathItemNvprRenderer::setStrokeColor(int index, const QColor &color) -{ - VisualPathGuiData &d(m_vp[index]); - d.strokeColor = color; - d.dirty |= DirtyStyle; - m_accDirty |= DirtyStyle; -} - -void QQuickPathItemNvprRenderer::setStrokeWidth(int index, qreal w) -{ - VisualPathGuiData &d(m_vp[index]); - d.strokeWidth = w; - d.dirty |= DirtyStyle; - m_accDirty |= DirtyStyle; -} - -void QQuickPathItemNvprRenderer::setFillColor(int index, const QColor &color) -{ - VisualPathGuiData &d(m_vp[index]); - d.fillColor = color; - d.dirty |= DirtyStyle; - m_accDirty |= DirtyStyle; -} - -void QQuickPathItemNvprRenderer::setFillRule(int index, QQuickVisualPath::FillRule fillRule) -{ - VisualPathGuiData &d(m_vp[index]); - d.fillRule = fillRule; - d.dirty |= DirtyFillRule; - m_accDirty |= DirtyFillRule; -} - -void QQuickPathItemNvprRenderer::setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) -{ - VisualPathGuiData &d(m_vp[index]); - d.joinStyle = joinStyle; - d.miterLimit = miterLimit; - d.dirty |= DirtyStyle; - m_accDirty |= DirtyStyle; -} - -void QQuickPathItemNvprRenderer::setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) -{ - VisualPathGuiData &d(m_vp[index]); - d.capStyle = capStyle; - d.dirty |= DirtyStyle; - m_accDirty |= DirtyStyle; -} - -void QQuickPathItemNvprRenderer::setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, - qreal dashOffset, const QVector &dashPattern) -{ - VisualPathGuiData &d(m_vp[index]); - d.dashActive = strokeStyle == QQuickVisualPath::DashLine; - d.dashOffset = dashOffset; - d.dashPattern = dashPattern; - d.dirty |= DirtyDash; - m_accDirty |= DirtyDash; -} - -void QQuickPathItemNvprRenderer::setFillGradient(int index, QQuickPathGradient *gradient) -{ - VisualPathGuiData &d(m_vp[index]); - d.fillGradientActive = gradient != nullptr; - if (gradient) { - d.fillGradient.stops = gradient->sortedGradientStops(); - d.fillGradient.spread = gradient->spread(); - if (QQuickPathLinearGradient *g = qobject_cast(gradient)) { - d.fillGradient.start = QPointF(g->x1(), g->y1()); - d.fillGradient.end = QPointF(g->x2(), g->y2()); - } else { - Q_UNREACHABLE(); - } - } - d.dirty |= DirtyFillGradient; - m_accDirty |= DirtyFillGradient; -} - -void QQuickPathItemNvprRenderer::endSync(bool) -{ -} - -void QQuickPathItemNvprRenderer::setNode(QQuickPathItemNvprRenderNode *node) -{ - if (m_node != node) { - m_node = node; - m_accDirty |= DirtyList; - } -} - -QDebug operator<<(QDebug debug, const QQuickPathItemNvprRenderer::NvprPath &path) -{ - QDebugStateSaver saver(debug); - debug.space().noquote(); - if (!path.str.isEmpty()) { - debug << "Path with SVG string" << path.str; - return debug; - } - debug << "Path with" << path.cmd.count() << "commands"; - int ci = 0; - for (GLubyte cmd : path.cmd) { - static struct { GLubyte cmd; const char *s; int coordCount; } nameTab[] = { - { GL_MOVE_TO_NV, "moveTo", 2 }, - { GL_LINE_TO_NV, "lineTo", 2 }, - { GL_QUADRATIC_CURVE_TO_NV, "quadTo", 4 }, - { GL_CUBIC_CURVE_TO_NV, "cubicTo", 6 }, - { GL_LARGE_CW_ARC_TO_NV, "arcTo-large-CW", 5 }, - { GL_LARGE_CCW_ARC_TO_NV, "arcTo-large-CCW", 5 }, - { GL_SMALL_CW_ARC_TO_NV, "arcTo-small-CW", 5 }, - { GL_SMALL_CCW_ARC_TO_NV, "arcTo-small-CCW", 5 }, - { GL_CLOSE_PATH_NV, "closePath", 0 } }; - for (size_t i = 0; i < sizeof(nameTab) / sizeof(nameTab[0]); ++i) { - if (nameTab[i].cmd == cmd) { - QByteArray cs; - for (int j = 0; j < nameTab[i].coordCount; ++j) { - cs.append(QByteArray::number(path.coord[ci++])); - cs.append(' '); - } - debug << "\n " << nameTab[i].s << " " << cs; - break; - } - } - } - return debug; -} - -static inline void appendCoords(QVector *v, QQuickCurve *c, QPointF *pos) -{ - QPointF p(c->hasRelativeX() ? pos->x() + c->relativeX() : c->x(), - c->hasRelativeY() ? pos->y() + c->relativeY() : c->y()); - v->append(p.x()); - v->append(p.y()); - *pos = p; -} - -static inline void appendControlCoords(QVector *v, QQuickPathQuad *c, const QPointF &pos) -{ - QPointF p(c->hasRelativeControlX() ? pos.x() + c->relativeControlX() : c->controlX(), - c->hasRelativeControlY() ? pos.y() + c->relativeControlY() : c->controlY()); - v->append(p.x()); - v->append(p.y()); -} - -static inline void appendControl1Coords(QVector *v, QQuickPathCubic *c, const QPointF &pos) -{ - QPointF p(c->hasRelativeControl1X() ? pos.x() + c->relativeControl1X() : c->control1X(), - c->hasRelativeControl1Y() ? pos.y() + c->relativeControl1Y() : c->control1Y()); - v->append(p.x()); - v->append(p.y()); -} - -static inline void appendControl2Coords(QVector *v, QQuickPathCubic *c, const QPointF &pos) -{ - QPointF p(c->hasRelativeControl2X() ? pos.x() + c->relativeControl2X() : c->control2X(), - c->hasRelativeControl2Y() ? pos.y() + c->relativeControl2Y() : c->control2Y()); - v->append(p.x()); - v->append(p.y()); -} - -void QQuickPathItemNvprRenderer::convertPath(const QQuickPath *path, VisualPathGuiData *d) -{ - d->path = NvprPath(); - if (!path) - return; - - const QList &pp(QQuickPathPrivate::get(path)->_pathElements); - if (pp.isEmpty()) - return; - - QPointF startPos(path->startX(), path->startY()); - QPointF pos(startPos); - if (!qFuzzyIsNull(pos.x()) || !qFuzzyIsNull(pos.y())) { - d->path.cmd.append(GL_MOVE_TO_NV); - d->path.coord.append(pos.x()); - d->path.coord.append(pos.y()); - } - - for (QQuickPathElement *e : pp) { - if (QQuickPathMove *o = qobject_cast(e)) { - d->path.cmd.append(GL_MOVE_TO_NV); - appendCoords(&d->path.coord, o, &pos); - startPos = pos; - } else if (QQuickPathLine *o = qobject_cast(e)) { - d->path.cmd.append(GL_LINE_TO_NV); - appendCoords(&d->path.coord, o, &pos); - } else if (QQuickPathQuad *o = qobject_cast(e)) { - d->path.cmd.append(GL_QUADRATIC_CURVE_TO_NV); - appendControlCoords(&d->path.coord, o, pos); - appendCoords(&d->path.coord, o, &pos); - } else if (QQuickPathCubic *o = qobject_cast(e)) { - d->path.cmd.append(GL_CUBIC_CURVE_TO_NV); - appendControl1Coords(&d->path.coord, o, pos); - appendControl2Coords(&d->path.coord, o, pos); - appendCoords(&d->path.coord, o, &pos); - } else if (QQuickPathArc *o = qobject_cast(e)) { - const bool sweepFlag = o->direction() == QQuickPathArc::Clockwise; // maps to CCW, not a typo - GLenum cmd; - if (o->useLargeArc()) - cmd = sweepFlag ? GL_LARGE_CCW_ARC_TO_NV : GL_LARGE_CW_ARC_TO_NV; - else - cmd = sweepFlag ? GL_SMALL_CCW_ARC_TO_NV : GL_SMALL_CW_ARC_TO_NV; - d->path.cmd.append(cmd); - d->path.coord.append(o->radiusX()); - d->path.coord.append(o->radiusY()); - d->path.coord.append(o->xAxisRotation()); - appendCoords(&d->path.coord, o, &pos); - } else if (QQuickPathSvg *o = qobject_cast(e)) { - // PathSvg cannot be combined with other elements. But take at - // least startX and startY into account. - if (d->path.str.isEmpty()) - d->path.str = QString(QStringLiteral("M %1 %2 ")).arg(pos.x()).arg(pos.y()).toUtf8(); - d->path.str.append(o->path().toUtf8()); - } else { - qWarning() << "PathItem/NVPR: unsupported Path element" << e; - } - } - - // For compatibility with QTriangulatingStroker. SVG and others would not - // implicitly close the path when end_pos == start_pos (start_pos being the - // last moveTo pos); that would still need an explicit 'z' or similar. We - // don't have an explicit close command, so just fake a close when the - // positions match. - if (pos == startPos) - d->path.cmd.append(GL_CLOSE_PATH_NV); -} - -void QQuickPathItemNvprRenderer::convertJSPath(const QQuickPathItemPath &path, VisualPathGuiData *d) -{ - d->path = NvprPath(); - if (path.cmd.isEmpty()) - return; - - QPointF startPos(0, 0); - QPointF pos(startPos); - int coordIdx = 0; - - for (QQuickPathItemPath::Command cmd : path.cmd) { - switch (cmd) { - case QQuickPathItemPath::MoveTo: - d->path.cmd.append(GL_MOVE_TO_NV); - pos = QPointF(path.coords[coordIdx], path.coords[coordIdx + 1]); - startPos = pos; - d->path.coord.append(pos.x()); - d->path.coord.append(pos.y()); - coordIdx += 2; - break; - case QQuickPathItemPath::LineTo: - d->path.cmd.append(GL_LINE_TO_NV); - pos = QPointF(path.coords[coordIdx], path.coords[coordIdx + 1]); - d->path.coord.append(pos.x()); - d->path.coord.append(pos.y()); - coordIdx += 2; - break; - case QQuickPathItemPath::QuadTo: - d->path.cmd.append(GL_QUADRATIC_CURVE_TO_NV); - d->path.coord.append(path.coords[coordIdx]); - d->path.coord.append(path.coords[coordIdx + 1]); - pos = QPointF(path.coords[coordIdx + 2], path.coords[coordIdx + 3]); - d->path.coord.append(pos.x()); - d->path.coord.append(pos.y()); - coordIdx += 4; - break; - case QQuickPathItemPath::CubicTo: - d->path.cmd.append(GL_CUBIC_CURVE_TO_NV); - d->path.coord.append(path.coords[coordIdx]); - d->path.coord.append(path.coords[coordIdx + 1]); - d->path.coord.append(path.coords[coordIdx + 2]); - d->path.coord.append(path.coords[coordIdx + 3]); - pos = QPointF(path.coords[coordIdx + 4], path.coords[coordIdx + 5]); - d->path.coord.append(pos.x()); - d->path.coord.append(pos.y()); - coordIdx += 6; - break; - case QQuickPathItemPath::ArcTo: - { - const bool sweepFlag = !qFuzzyIsNull(path.coords[coordIdx + 5]); - const bool useLargeArc = !qFuzzyIsNull(path.coords[coordIdx + 6]); - GLenum cmd; - if (useLargeArc) - cmd = sweepFlag ? GL_LARGE_CCW_ARC_TO_NV : GL_LARGE_CW_ARC_TO_NV; - else - cmd = sweepFlag ? GL_SMALL_CCW_ARC_TO_NV : GL_SMALL_CW_ARC_TO_NV; - d->path.cmd.append(cmd); - d->path.coord.append(path.coords[coordIdx]); // rx - d->path.coord.append(path.coords[coordIdx + 1]); // ry - d->path.coord.append(path.coords[coordIdx + 2]); // xrot - pos = QPointF(path.coords[coordIdx + 3], path.coords[coordIdx + 4]); - d->path.coord.append(pos.x()); - d->path.coord.append(pos.y()); - coordIdx += 7; - } - break; - default: - qWarning("Unknown JS path command: %d", cmd); - break; - } - } - - if (pos == startPos) - d->path.cmd.append(GL_CLOSE_PATH_NV); -} - -static inline QVector4D qsg_premultiply(const QColor &c, float globalOpacity) -{ - const float o = c.alphaF() * globalOpacity; - return QVector4D(c.redF() * o, c.greenF() * o, c.blueF() * o, o); -} - -void QQuickPathItemNvprRenderer::updateNode() -{ - // Called on the render thread with gui blocked -> update the node with its - // own copy of all relevant data. - - if (!m_accDirty) - return; - - const int count = m_vp.count(); - const bool listChanged = m_accDirty & DirtyList; - if (listChanged) - m_node->m_vp.resize(count); - - for (int i = 0; i < count; ++i) { - VisualPathGuiData &src(m_vp[i]); - QQuickPathItemNvprRenderNode::VisualPathRenderData &dst(m_node->m_vp[i]); - - int dirty = src.dirty; - src.dirty = 0; - if (listChanged) - dirty |= DirtyPath | DirtyStyle | DirtyFillRule | DirtyDash | DirtyFillGradient; - - // updateNode() can be called several times with different dirty - // states before render() gets invoked. So accumulate. - dst.dirty |= dirty; - - if (dirty & DirtyPath) - dst.source = src.path; - - if (dirty & DirtyStyle) { - dst.strokeWidth = src.strokeWidth; - dst.strokeColor = qsg_premultiply(src.strokeColor, 1.0f); - dst.fillColor = qsg_premultiply(src.fillColor, 1.0f); - switch (src.joinStyle) { - case QQuickVisualPath::MiterJoin: - dst.joinStyle = GL_MITER_TRUNCATE_NV; - break; - case QQuickVisualPath::BevelJoin: - dst.joinStyle = GL_BEVEL_NV; - break; - case QQuickVisualPath::RoundJoin: - dst.joinStyle = GL_ROUND_NV; - break; - default: - Q_UNREACHABLE(); - } - dst.miterLimit = src.miterLimit; - switch (src.capStyle) { - case QQuickVisualPath::FlatCap: - dst.capStyle = GL_FLAT; - break; - case QQuickVisualPath::SquareCap: - dst.capStyle = GL_SQUARE_NV; - break; - case QQuickVisualPath::RoundCap: - dst.capStyle = GL_ROUND_NV; - break; - default: - Q_UNREACHABLE(); - } - } - - if (dirty & DirtyFillRule) { - switch (src.fillRule) { - case QQuickVisualPath::OddEvenFill: - dst.fillRule = GL_INVERT; - break; - case QQuickVisualPath::WindingFill: - dst.fillRule = GL_COUNT_UP_NV; - break; - default: - Q_UNREACHABLE(); - } - } - - if (dirty & DirtyDash) { - dst.dashOffset = src.dashOffset; - if (src.dashActive) { - dst.dashPattern.resize(src.dashPattern.count()); - // Multiply by strokeWidth because the PathItem API follows QPen - // meaning the input dash pattern here is in width units. - for (int i = 0; i < src.dashPattern.count(); ++i) - dst.dashPattern[i] = GLfloat(src.dashPattern[i]) * src.strokeWidth; - } else { - dst.dashPattern.clear(); - } - } - - if (dirty & DirtyFillGradient) { - dst.fillGradientActive = src.fillGradientActive; - if (src.fillGradientActive) - dst.fillGradient = src.fillGradient; - } - } - - m_node->markDirty(QSGNode::DirtyMaterial); - m_accDirty = 0; -} - -bool QQuickPathItemNvprRenderNode::nvprInited = false; -QQuickNvprFunctions QQuickPathItemNvprRenderNode::nvpr; -QQuickNvprMaterialManager QQuickPathItemNvprRenderNode::mtlmgr; - -QQuickPathItemNvprRenderNode::~QQuickPathItemNvprRenderNode() -{ - releaseResources(); -} - -void QQuickPathItemNvprRenderNode::releaseResources() -{ - for (VisualPathRenderData &d : m_vp) { - if (d.path) { - nvpr.deletePaths(d.path, 1); - d.path = 0; - } - if (d.fallbackFbo) { - delete d.fallbackFbo; - d.fallbackFbo = nullptr; - } - } - - m_fallbackBlitter.destroy(); -} - -void QQuickNvprMaterialManager::create(QQuickNvprFunctions *nvpr) -{ - m_nvpr = nvpr; -} - -void QQuickNvprMaterialManager::releaseResources() -{ - QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions(); - for (MaterialDesc &mtl : m_materials) { - if (mtl.ppl) { - f->glDeleteProgramPipelines(1, &mtl.ppl); - mtl = MaterialDesc(); - } - } -} - -QQuickNvprMaterialManager::MaterialDesc *QQuickNvprMaterialManager::activateMaterial(Material m) -{ - QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions(); - MaterialDesc &mtl(m_materials[m]); - - if (!mtl.ppl) { - if (m == MatSolid) { - static const char *fragSrc = - "#version 310 es\n" - "precision highp float;\n" - "out vec4 fragColor;\n" - "uniform vec4 color;\n" - "uniform float opacity;\n" - "void main() {\n" - " fragColor = color * opacity;\n" - "}\n"; - if (!m_nvpr->createFragmentOnlyPipeline(fragSrc, &mtl.ppl, &mtl.prg)) { - qWarning("NVPR: Failed to create shader pipeline for solid fill"); - return nullptr; - } - Q_ASSERT(mtl.ppl && mtl.prg); - mtl.uniLoc[0] = f->glGetProgramResourceLocation(mtl.prg, GL_UNIFORM, "color"); - Q_ASSERT(mtl.uniLoc[0] >= 0); - mtl.uniLoc[1] = f->glGetProgramResourceLocation(mtl.prg, GL_UNIFORM, "opacity"); - Q_ASSERT(mtl.uniLoc[1] >= 0); - } else if (m == MatLinearGradient) { - static const char *fragSrc = - "#version 310 es\n" - "precision highp float;\n" - "layout(location = 0) in vec2 uv;" - "uniform float opacity;\n" - "uniform sampler2D gradTab;\n" - "uniform vec2 gradStart;\n" - "uniform vec2 gradEnd;\n" - "out vec4 fragColor;\n" - "void main() {\n" - " vec2 gradVec = gradEnd - gradStart;\n" - " float gradTabIndex = dot(gradVec, uv - gradStart) / (gradVec.x * gradVec.x + gradVec.y * gradVec.y);\n" - " fragColor = texture(gradTab, vec2(gradTabIndex, 0.5)) * opacity;\n" - "}\n"; - if (!m_nvpr->createFragmentOnlyPipeline(fragSrc, &mtl.ppl, &mtl.prg)) { - qWarning("NVPR: Failed to create shader pipeline for linear gradient"); - return nullptr; - } - Q_ASSERT(mtl.ppl && mtl.prg); - mtl.uniLoc[1] = f->glGetProgramResourceLocation(mtl.prg, GL_UNIFORM, "opacity"); - Q_ASSERT(mtl.uniLoc[1] >= 0); - mtl.uniLoc[2] = f->glGetProgramResourceLocation(mtl.prg, GL_UNIFORM, "gradStart"); - Q_ASSERT(mtl.uniLoc[2] >= 0); - mtl.uniLoc[3] = f->glGetProgramResourceLocation(mtl.prg, GL_UNIFORM, "gradEnd"); - Q_ASSERT(mtl.uniLoc[3] >= 0); - } else { - Q_UNREACHABLE(); - } - } - - f->glBindProgramPipeline(mtl.ppl); - - return &mtl; -} - -void QQuickPathItemNvprRenderNode::updatePath(VisualPathRenderData *d) -{ - if (d->dirty & QQuickPathItemNvprRenderer::DirtyPath) { - if (!d->path) { - d->path = nvpr.genPaths(1); - Q_ASSERT(d->path != 0); - } - if (d->source.str.isEmpty()) { - nvpr.pathCommands(d->path, d->source.cmd.count(), d->source.cmd.constData(), - d->source.coord.count(), GL_FLOAT, d->source.coord.constData()); - } else { - nvpr.pathString(d->path, GL_PATH_FORMAT_SVG_NV, d->source.str.count(), d->source.str.constData()); - } - } - - if (d->dirty & QQuickPathItemNvprRenderer::DirtyStyle) { - nvpr.pathParameterf(d->path, GL_PATH_STROKE_WIDTH_NV, d->strokeWidth); - nvpr.pathParameteri(d->path, GL_PATH_JOIN_STYLE_NV, d->joinStyle); - nvpr.pathParameteri(d->path, GL_PATH_MITER_LIMIT_NV, d->miterLimit); - nvpr.pathParameteri(d->path, GL_PATH_END_CAPS_NV, d->capStyle); - nvpr.pathParameteri(d->path, GL_PATH_DASH_CAPS_NV, d->capStyle); - } - - if (d->dirty & QQuickPathItemNvprRenderer::DirtyDash) { - nvpr.pathParameterf(d->path, GL_PATH_DASH_OFFSET_NV, d->dashOffset); - // count == 0 -> no dash - nvpr.pathDashArray(d->path, d->dashPattern.count(), d->dashPattern.constData()); - } - - if (d->dirty) - d->fallbackValid = false; -} - -void QQuickPathItemNvprRenderNode::renderStroke(VisualPathRenderData *d, int strokeStencilValue, int writeMask) -{ - QQuickNvprMaterialManager::MaterialDesc *mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatSolid); - f->glProgramUniform4f(mtl->prg, mtl->uniLoc[0], - d->strokeColor.x(), d->strokeColor.y(), d->strokeColor.z(), d->strokeColor.w()); - f->glProgramUniform1f(mtl->prg, mtl->uniLoc[1], inheritedOpacity()); - - nvpr.stencilThenCoverStrokePath(d->path, strokeStencilValue, writeMask, GL_CONVEX_HULL_NV); -} - -void QQuickPathItemNvprRenderNode::renderFill(VisualPathRenderData *d) -{ - QQuickNvprMaterialManager::MaterialDesc *mtl = nullptr; - if (d->fillGradientActive) { - mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatLinearGradient); - QSGTexture *tx = QQuickPathItemGradientCache::currentCache()->get(d->fillGradient); - tx->bind(); - // uv = vec2(coeff[0] * x + coeff[1] * y + coeff[2], coeff[3] * x + coeff[4] * y + coeff[5]) - // where x and y are in path coordinate space, which is just what - // we need since the gradient's start and stop are in that space too. - GLfloat coeff[6] = { 1, 0, 0, - 0, 1, 0 }; - nvpr.programPathFragmentInputGen(mtl->prg, 0, GL_OBJECT_LINEAR_NV, 2, coeff); - f->glProgramUniform2f(mtl->prg, mtl->uniLoc[2], d->fillGradient.start.x(), d->fillGradient.start.y()); - f->glProgramUniform2f(mtl->prg, mtl->uniLoc[3], d->fillGradient.end.x(), d->fillGradient.end.y()); - } else { - mtl = mtlmgr.activateMaterial(QQuickNvprMaterialManager::MatSolid); - f->glProgramUniform4f(mtl->prg, mtl->uniLoc[0], - d->fillColor.x(), d->fillColor.y(), d->fillColor.z(), d->fillColor.w()); - } - f->glProgramUniform1f(mtl->prg, mtl->uniLoc[1], inheritedOpacity()); - - const int writeMask = 0xFF; - nvpr.stencilThenCoverFillPath(d->path, d->fillRule, writeMask, GL_BOUNDING_BOX_NV); -} - -void QQuickPathItemNvprRenderNode::renderOffscreenFill(VisualPathRenderData *d) -{ - if (d->fallbackValid && d->fallbackFbo) - return; - - GLfloat bb[4]; - nvpr.getPathParameterfv(d->path, GL_PATH_STROKE_BOUNDING_BOX_NV, bb); - QSize sz = QSizeF(bb[2] - bb[0] + 1, bb[3] - bb[1] + 1).toSize(); - d->fallbackSize = QSize(qMax(32, sz.width()), qMax(32, sz.height())); - d->fallbackTopLeft = QPointF(bb[0], bb[1]); - - if (d->fallbackFbo && d->fallbackFbo->size() != d->fallbackSize) { - delete d->fallbackFbo; - d->fallbackFbo = nullptr; - } - if (!d->fallbackFbo) - d->fallbackFbo = new QOpenGLFramebufferObject(d->fallbackSize, QOpenGLFramebufferObject::CombinedDepthStencil); - if (!d->fallbackFbo->bind()) - return; - - GLint prevViewport[4]; - f->glGetIntegerv(GL_VIEWPORT, prevViewport); - - f->glViewport(0, 0, d->fallbackSize.width(), d->fallbackSize.height()); - f->glDisable(GL_DEPTH_TEST); - f->glClearColor(0, 0, 0, 0); - f->glClearStencil(0); - f->glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - f->glStencilFunc(GL_NOTEQUAL, 0, 0xFF); - f->glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); - - QMatrix4x4 mv; - mv.translate(-d->fallbackTopLeft.x(), -d->fallbackTopLeft.y()); - nvpr.matrixLoadf(GL_PATH_MODELVIEW_NV, mv.constData()); - QMatrix4x4 proj; - proj.ortho(0, d->fallbackSize.width(), d->fallbackSize.height(), 0, 1, -1); - nvpr.matrixLoadf(GL_PATH_PROJECTION_NV, proj.constData()); - - renderFill(d); - - d->fallbackFbo->release(); - f->glEnable(GL_DEPTH_TEST); - f->glViewport(prevViewport[0], prevViewport[1], prevViewport[2], prevViewport[3]); - - d->fallbackValid = true; -} - -void QQuickPathItemNvprRenderNode::setupStencilForCover(bool stencilClip, int sv) -{ - if (!stencilClip) { - // Assume stencil buffer is cleared to 0 for each frame. - // Within the frame dppass=GL_ZERO for glStencilOp ensures stencil is reset and so no need to clear. - f->glStencilFunc(GL_NOTEQUAL, 0, 0xFF); - f->glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO); - } else { - f->glStencilFunc(GL_LESS, sv, 0xFF); // pass if (sv & 0xFF) < (stencil_value & 0xFF) - f->glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); // dppass: replace with the original value (clip's stencil ref value) - } -} - -void QQuickPathItemNvprRenderNode::render(const RenderState *state) -{ - f = QOpenGLContext::currentContext()->extraFunctions(); - - if (!nvprInited) { - if (!nvpr.create()) { - qWarning("NVPR init failed"); - return; - } - mtlmgr.create(&nvpr); - nvprInited = true; - } - - f->glUseProgram(0); - f->glStencilMask(~0); - f->glEnable(GL_STENCIL_TEST); - - const bool stencilClip = state->stencilEnabled(); - // when true, the stencil buffer already has a clip path with a ref value of sv - const int sv = state->stencilValue(); - const bool hasScissor = state->scissorEnabled(); - - if (hasScissor) { - // scissor rect is already set, just enable scissoring - f->glEnable(GL_SCISSOR_TEST); - } - - // Depth test against the opaque batches rendered before. - f->glEnable(GL_DEPTH_TEST); - f->glDepthFunc(GL_LESS); - nvpr.pathCoverDepthFunc(GL_LESS); - nvpr.pathStencilDepthOffset(-0.05f, -1); - - bool reloadMatrices = true; - - for (VisualPathRenderData &d : m_vp) { - updatePath(&d); - - const bool hasFill = d.hasFill(); - const bool hasStroke = d.hasStroke(); - - if (hasFill && stencilClip) { - // Fall back to a texture when complex clipping is in use and we have - // to fill. Reconciling glStencilFillPath's and the scenegraph's clip - // stencil semantics has not succeeded so far... - if (hasScissor) - f->glDisable(GL_SCISSOR_TEST); - renderOffscreenFill(&d); - reloadMatrices = true; - if (hasScissor) - f->glEnable(GL_SCISSOR_TEST); - } - - if (reloadMatrices) { - reloadMatrices = false; - nvpr.matrixLoadf(GL_PATH_MODELVIEW_NV, matrix()->constData()); - nvpr.matrixLoadf(GL_PATH_PROJECTION_NV, state->projectionMatrix()->constData()); - } - - // Fill! - if (hasFill) { - if (!stencilClip) { - setupStencilForCover(false, 0); - renderFill(&d); - } else { - if (!m_fallbackBlitter.isCreated()) - m_fallbackBlitter.create(); - f->glStencilFunc(GL_EQUAL, sv, 0xFF); - f->glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); - QMatrix4x4 mv = *matrix(); - mv.translate(d.fallbackTopLeft.x(), d.fallbackTopLeft.y()); - m_fallbackBlitter.texturedQuad(d.fallbackFbo->texture(), d.fallbackFbo->size(), - *state->projectionMatrix(), mv, - inheritedOpacity()); - } - } - - // Stroke! - if (hasStroke) { - const int strokeStencilValue = 0x80; - const int writeMask = 0x80; - - setupStencilForCover(stencilClip, sv); - if (stencilClip) { - // for the stencil step (eff. read mask == 0xFF & ~writeMask) - nvpr.pathStencilFunc(GL_EQUAL, sv, 0xFF); - // With stencilCLip == true the read mask for the stencil test before the stencil step is 0x7F. - // This assumes the clip stencil value is <= 127. - if (sv >= strokeStencilValue) - qWarning("PathItem/NVPR: stencil clip ref value %d too large; expect rendering errors", sv); - } - - renderStroke(&d, strokeStencilValue, writeMask); - } - - if (stencilClip) - nvpr.pathStencilFunc(GL_ALWAYS, 0, ~0); - - d.dirty = 0; - } - - f->glBindProgramPipeline(0); -} - -QSGRenderNode::StateFlags QQuickPathItemNvprRenderNode::changedStates() const -{ - return BlendState | StencilState | DepthState | ScissorState; -} - -QSGRenderNode::RenderingFlags QQuickPathItemNvprRenderNode::flags() const -{ - return DepthAwareRendering; // avoid hitting the less optimal no-opaque-batch path in the renderer -} - -bool QQuickPathItemNvprRenderNode::isSupported() -{ - static const bool nvprDisabled = qEnvironmentVariableIntValue("QT_NO_NVPR") != 0; - return !nvprDisabled && QQuickNvprFunctions::isSupported(); -} - -bool QQuickNvprBlitter::create() -{ - if (isCreated()) - destroy(); - - m_program = new QOpenGLShaderProgram; - if (QOpenGLContext::currentContext()->format().profile() == QSurfaceFormat::CoreProfile) { - m_program->addCacheableShaderFromSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/qt-project.org/items/shaders/shadereffect_core.vert")); - m_program->addCacheableShaderFromSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/qt-project.org/items/shaders/shadereffect_core.frag")); - } else { - m_program->addCacheableShaderFromSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/qt-project.org/items/shaders/shadereffect.vert")); - m_program->addCacheableShaderFromSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/qt-project.org/items/shaders/shadereffect.frag")); - } - m_program->bindAttributeLocation("qt_Vertex", 0); - m_program->bindAttributeLocation("qt_MultiTexCoord0", 1); - if (!m_program->link()) - return false; - - m_matrixLoc = m_program->uniformLocation("qt_Matrix"); - m_opacityLoc = m_program->uniformLocation("qt_Opacity"); - - m_buffer = new QOpenGLBuffer; - if (!m_buffer->create()) - return false; - m_buffer->bind(); - m_buffer->allocate(4 * sizeof(GLfloat) * 6); - m_buffer->release(); - - return true; -} - -void QQuickNvprBlitter::destroy() -{ - if (m_program) { - delete m_program; - m_program = nullptr; - } - if (m_buffer) { - delete m_buffer; - m_buffer = nullptr; - } -} - -void QQuickNvprBlitter::texturedQuad(GLuint textureId, const QSize &size, - const QMatrix4x4 &proj, const QMatrix4x4 &modelview, - float opacity) -{ - QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions(); - - m_program->bind(); - - QMatrix4x4 m = proj * modelview; - m_program->setUniformValue(m_matrixLoc, m); - m_program->setUniformValue(m_opacityLoc, opacity); - - m_buffer->bind(); - - if (size != m_prevSize) { - m_prevSize = size; - - QPointF p0(size.width() - 1, size.height() - 1); - QPointF p1(0, 0); - QPointF p2(0, size.height() - 1); - QPointF p3(size.width() - 1, 0); - - GLfloat vertices[6 * 4] = { - GLfloat(p0.x()), GLfloat(p0.y()), 1, 0, - GLfloat(p1.x()), GLfloat(p1.y()), 0, 1, - GLfloat(p2.x()), GLfloat(p2.y()), 0, 0, - - GLfloat(p0.x()), GLfloat(p0.y()), 1, 0, - GLfloat(p3.x()), GLfloat(p3.y()), 1, 1, - GLfloat(p1.x()), GLfloat(p1.y()), 0, 1, - }; - - m_buffer->write(0, vertices, sizeof(vertices)); - } - - m_program->enableAttributeArray(0); - m_program->enableAttributeArray(1); - f->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0); - f->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (const void *) (2 * sizeof(GLfloat))); - - f->glBindTexture(GL_TEXTURE_2D, textureId); - - f->glDrawArrays(GL_TRIANGLES, 0, 6); - - f->glBindTexture(GL_TEXTURE_2D, 0); - m_buffer->release(); - m_program->release(); -} - -QT_END_NAMESPACE diff --git a/src/quick/items/qquickpathitemnvprrenderer_p.h b/src/quick/items/qquickpathitemnvprrenderer_p.h deleted file mode 100644 index deab9cf7f9..0000000000 --- a/src/quick/items/qquickpathitemnvprrenderer_p.h +++ /dev/null @@ -1,237 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKPATHITEMNVPRRENDERER_P_H -#define QQUICKPATHITEMNVPRRENDERER_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 "qquickpathitem_p_p.h" -#include -#include -#include -#include -#include - -#ifndef QT_NO_OPENGL - -QT_BEGIN_NAMESPACE - -class QQuickPathItemNvprRenderNode; -class QOpenGLFramebufferObject; -class QOpenGLBuffer; -class QOpenGLExtraFunctions; - -class QQuickPathItemNvprRenderer : public QQuickAbstractPathRenderer -{ -public: - enum Dirty { - DirtyPath = 0x01, - DirtyStyle = 0x02, - DirtyFillRule = 0x04, - DirtyDash = 0x08, - DirtyFillGradient = 0x10, - DirtyList = 0x20 - }; - - void beginSync(int totalCount) override; - void setPath(int index, const QQuickPath *path) override; - void setJSPath(int index, const QQuickPathItemPath &path) override; - 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, QQuickVisualPath::FillRule fillRule) override; - void setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) override; - void setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) override; - void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, - qreal dashOffset, const QVector &dashPattern) override; - void setFillGradient(int index, QQuickPathGradient *gradient) override; - void endSync(bool async) override; - - void updateNode() override; - - void setNode(QQuickPathItemNvprRenderNode *node); - - struct NvprPath { - QVector cmd; - QVector coord; - QByteArray str; - }; - -private: - struct VisualPathGuiData { - int dirty = 0; - NvprPath path; - qreal strokeWidth; - QColor strokeColor; - QColor fillColor; - QQuickVisualPath::JoinStyle joinStyle; - int miterLimit; - QQuickVisualPath::CapStyle capStyle; - QQuickVisualPath::FillRule fillRule; - bool dashActive; - qreal dashOffset; - QVector dashPattern; - bool fillGradientActive; - QQuickPathItemGradientCache::GradientDesc fillGradient; - }; - - void convertPath(const QQuickPath *path, VisualPathGuiData *d); - void convertJSPath(const QQuickPathItemPath &path, VisualPathGuiData *d); - - QQuickPathItemNvprRenderNode *m_node = nullptr; - int m_accDirty = 0; - - QVector m_vp; -}; - -QDebug operator<<(QDebug debug, const QQuickPathItemNvprRenderer::NvprPath &path); - -class QQuickNvprMaterialManager -{ -public: - enum Material { - MatSolid, - MatLinearGradient, - - NMaterials - }; - - struct MaterialDesc { - GLuint ppl = 0; - GLuint prg = 0; - int uniLoc[4]; - }; - - void create(QQuickNvprFunctions *nvpr); - MaterialDesc *activateMaterial(Material m); - void releaseResources(); - -private: - QQuickNvprFunctions *m_nvpr; - MaterialDesc m_materials[NMaterials]; -}; - -class QQuickNvprBlitter -{ -public: - bool create(); - void destroy(); - bool isCreated() const { return m_program != nullptr; } - void texturedQuad(GLuint textureId, const QSize &size, - const QMatrix4x4 &proj, const QMatrix4x4 &modelview, - float opacity); - -private: - QOpenGLShaderProgram *m_program = nullptr; - QOpenGLBuffer *m_buffer = nullptr; - int m_matrixLoc; - int m_opacityLoc; - QSize m_prevSize; -}; - -class QQuickPathItemNvprRenderNode : public QSGRenderNode -{ -public: - ~QQuickPathItemNvprRenderNode(); - - void render(const RenderState *state) override; - void releaseResources() override; - StateFlags changedStates() const override; - RenderingFlags flags() const override; - - static bool isSupported(); - -private: - struct VisualPathRenderData { - GLuint path = 0; - int dirty = 0; - QQuickPathItemNvprRenderer::NvprPath source; - GLfloat strokeWidth; - QVector4D strokeColor; - QVector4D fillColor; - GLenum joinStyle; - GLint miterLimit; - GLenum capStyle; - GLenum fillRule; - GLfloat dashOffset; - QVector dashPattern; - bool fillGradientActive; - QQuickPathItemGradientCache::GradientDesc fillGradient; - QOpenGLFramebufferObject *fallbackFbo = nullptr; - bool fallbackValid = false; - QSize fallbackSize; - QPointF fallbackTopLeft; - - bool hasFill() const { return !qFuzzyIsNull(fillColor.w()) || fillGradientActive; } - bool hasStroke() const { return strokeWidth >= 0.0f && !qFuzzyIsNull(strokeColor.w()); } - }; - - void updatePath(VisualPathRenderData *d); - void renderStroke(VisualPathRenderData *d, int strokeStencilValue, int writeMask); - void renderFill(VisualPathRenderData *d); - void renderOffscreenFill(VisualPathRenderData *d); - void setupStencilForCover(bool stencilClip, int sv); - - static bool nvprInited; - static QQuickNvprFunctions nvpr; - static QQuickNvprMaterialManager mtlmgr; - - QQuickNvprBlitter m_fallbackBlitter; - QOpenGLExtraFunctions *f = nullptr; - - QVector m_vp; - - friend class QQuickPathItemNvprRenderer; -}; - -QT_END_NAMESPACE - -#endif // QT_NO_OPENGL - -#endif // QQUICKPATHITEMNVPRRENDERER_P_H diff --git a/src/quick/items/qquickpathitemsoftwarerenderer.cpp b/src/quick/items/qquickpathitemsoftwarerenderer.cpp deleted file mode 100644 index b7aa93bf65..0000000000 --- a/src/quick/items/qquickpathitemsoftwarerenderer.cpp +++ /dev/null @@ -1,277 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qquickpathitemsoftwarerenderer_p.h" -#include - -QT_BEGIN_NAMESPACE - -void QQuickPathItemSoftwareRenderer::beginSync(int totalCount) -{ - if (m_vp.count() != totalCount) { - m_vp.resize(totalCount); - m_accDirty |= DirtyList; - } -} - -void QQuickPathItemSoftwareRenderer::setPath(int index, const QQuickPath *path) -{ - VisualPathGuiData &d(m_vp[index]); - d.path = path ? path->path() : QPainterPath(); - d.dirty |= DirtyPath; - m_accDirty |= DirtyPath; -} - -void QQuickPathItemSoftwareRenderer::setJSPath(int index, const QQuickPathItemPath &path) -{ - VisualPathGuiData &d(m_vp[index]); - d.path = path.toPainterPath(); - d.dirty |= DirtyPath; - m_accDirty |= DirtyPath; -} - -void QQuickPathItemSoftwareRenderer::setStrokeColor(int index, const QColor &color) -{ - VisualPathGuiData &d(m_vp[index]); - d.pen.setColor(color); - d.dirty |= DirtyPen; - m_accDirty |= DirtyPen; -} - -void QQuickPathItemSoftwareRenderer::setStrokeWidth(int index, qreal w) -{ - VisualPathGuiData &d(m_vp[index]); - d.strokeWidth = w; - if (w >= 0.0f) - d.pen.setWidthF(w); - d.dirty |= DirtyPen; - m_accDirty |= DirtyPen; -} - -void QQuickPathItemSoftwareRenderer::setFillColor(int index, const QColor &color) -{ - VisualPathGuiData &d(m_vp[index]); - d.fillColor = color; - d.brush.setColor(color); - d.dirty |= DirtyBrush; - m_accDirty |= DirtyBrush; -} - -void QQuickPathItemSoftwareRenderer::setFillRule(int index, QQuickVisualPath::FillRule fillRule) -{ - VisualPathGuiData &d(m_vp[index]); - d.fillRule = Qt::FillRule(fillRule); - d.dirty |= DirtyFillRule; - m_accDirty |= DirtyFillRule; -} - -void QQuickPathItemSoftwareRenderer::setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) -{ - VisualPathGuiData &d(m_vp[index]); - d.pen.setJoinStyle(Qt::PenJoinStyle(joinStyle)); - d.pen.setMiterLimit(miterLimit); - d.dirty |= DirtyPen; - m_accDirty |= DirtyPen; -} - -void QQuickPathItemSoftwareRenderer::setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) -{ - VisualPathGuiData &d(m_vp[index]); - d.pen.setCapStyle(Qt::PenCapStyle(capStyle)); - d.dirty |= DirtyPen; - m_accDirty |= DirtyPen; -} - -void QQuickPathItemSoftwareRenderer::setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, - qreal dashOffset, const QVector &dashPattern) -{ - VisualPathGuiData &d(m_vp[index]); - switch (strokeStyle) { - case QQuickVisualPath::SolidLine: - d.pen.setStyle(Qt::SolidLine); - break; - case QQuickVisualPath::DashLine: - d.pen.setStyle(Qt::CustomDashLine); - d.pen.setDashPattern(dashPattern); - d.pen.setDashOffset(dashOffset); - break; - default: - break; - } - d.dirty |= DirtyPen; - m_accDirty |= DirtyPen; -} - -void QQuickPathItemSoftwareRenderer::setFillGradient(int index, QQuickPathGradient *gradient) -{ - VisualPathGuiData &d(m_vp[index]); - if (QQuickPathLinearGradient *linearGradient = qobject_cast(gradient)) { - QLinearGradient painterGradient(linearGradient->x1(), linearGradient->y1(), - linearGradient->x2(), linearGradient->y2()); - painterGradient.setStops(linearGradient->sortedGradientStops()); - switch (gradient->spread()) { - case QQuickPathGradient::PadSpread: - painterGradient.setSpread(QGradient::PadSpread); - break; - case QQuickPathGradient::RepeatSpread: - painterGradient.setSpread(QGradient::RepeatSpread); - break; - case QQuickPathGradient::ReflectSpread: - painterGradient.setSpread(QGradient::ReflectSpread); - break; - default: - break; - } - d.brush = QBrush(painterGradient); - } else { - d.brush = QBrush(d.fillColor); - } - d.dirty |= DirtyBrush; - m_accDirty |= DirtyBrush; -} - -void QQuickPathItemSoftwareRenderer::endSync(bool) -{ -} - -void QQuickPathItemSoftwareRenderer::setNode(QQuickPathItemSoftwareRenderNode *node) -{ - if (m_node != node) { - m_node = node; - m_accDirty |= DirtyList; - } -} - -void QQuickPathItemSoftwareRenderer::updateNode() -{ - if (!m_accDirty) - return; - - const int count = m_vp.count(); - const bool listChanged = m_accDirty & DirtyList; - if (listChanged) - m_node->m_vp.resize(count); - - m_node->m_boundingRect = QRectF(); - - for (int i = 0; i < count; ++i) { - VisualPathGuiData &src(m_vp[i]); - QQuickPathItemSoftwareRenderNode::VisualPathRenderData &dst(m_node->m_vp[i]); - - if (listChanged || (src.dirty & DirtyPath)) { - dst.path = src.path; - dst.path.setFillRule(src.fillRule); - } - - if (listChanged || (src.dirty & DirtyFillRule)) - dst.path.setFillRule(src.fillRule); - - if (listChanged || (src.dirty & DirtyPen)) { - dst.pen = src.pen; - dst.strokeWidth = src.strokeWidth; - } - - if (listChanged || (src.dirty & DirtyBrush)) - dst.brush = src.brush; - - src.dirty = 0; - - QRectF br = dst.path.boundingRect(); - const float sw = qMax(1.0f, dst.strokeWidth); - br.adjust(-sw, -sw, sw, sw); - m_node->m_boundingRect |= br; - } - - m_node->markDirty(QSGNode::DirtyMaterial); - m_accDirty = 0; -} - -QQuickPathItemSoftwareRenderNode::QQuickPathItemSoftwareRenderNode(QQuickPathItem *item) - : m_item(item) -{ -} - -QQuickPathItemSoftwareRenderNode::~QQuickPathItemSoftwareRenderNode() -{ - releaseResources(); -} - -void QQuickPathItemSoftwareRenderNode::releaseResources() -{ -} - -void QQuickPathItemSoftwareRenderNode::render(const RenderState *state) -{ - if (m_vp.isEmpty()) - return; - - QSGRendererInterface *rif = m_item->window()->rendererInterface(); - QPainter *p = static_cast(rif->getResource(m_item->window(), QSGRendererInterface::PainterResource)); - Q_ASSERT(p); - - const QRegion *clipRegion = state->clipRegion(); - if (clipRegion && !clipRegion->isEmpty()) - p->setClipRegion(*clipRegion, Qt::ReplaceClip); // must be done before setTransform - - p->setTransform(matrix()->toTransform()); - p->setOpacity(inheritedOpacity()); - - for (const VisualPathRenderData &d : qAsConst(m_vp)) { - p->setPen(d.strokeWidth >= 0.0f && d.pen.color() != Qt::transparent ? d.pen : Qt::NoPen); - p->setBrush(d.brush.color() != Qt::transparent ? d.brush : Qt::NoBrush); - p->drawPath(d.path); - } -} - -QSGRenderNode::StateFlags QQuickPathItemSoftwareRenderNode::changedStates() const -{ - return 0; -} - -QSGRenderNode::RenderingFlags QQuickPathItemSoftwareRenderNode::flags() const -{ - return BoundedRectRendering; // avoid fullscreen updates by saying we won't draw outside rect() -} - -QRectF QQuickPathItemSoftwareRenderNode::rect() const -{ - return m_boundingRect; -} - -QT_END_NAMESPACE diff --git a/src/quick/items/qquickpathitemsoftwarerenderer_p.h b/src/quick/items/qquickpathitemsoftwarerenderer_p.h deleted file mode 100644 index e76590bdfe..0000000000 --- a/src/quick/items/qquickpathitemsoftwarerenderer_p.h +++ /dev/null @@ -1,136 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKPATHITEMSOFTWARERENDERER_P_H -#define QQUICKPATHITEMSOFTWARERENDERER_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 "qquickpathitem_p_p.h" -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QQuickPathItemSoftwareRenderNode; - -class QQuickPathItemSoftwareRenderer : public QQuickAbstractPathRenderer -{ -public: - enum Dirty { - DirtyPath = 0x01, - DirtyPen = 0x02, - DirtyFillRule = 0x04, - DirtyBrush = 0x08, - DirtyList = 0x10 - }; - - void beginSync(int totalCount) override; - void setPath(int index, const QQuickPath *path) override; - void setJSPath(int index, const QQuickPathItemPath &path) override; - 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, QQuickVisualPath::FillRule fillRule) override; - void setJoinStyle(int index, QQuickVisualPath::JoinStyle joinStyle, int miterLimit) override; - void setCapStyle(int index, QQuickVisualPath::CapStyle capStyle) override; - void setStrokeStyle(int index, QQuickVisualPath::StrokeStyle strokeStyle, - qreal dashOffset, const QVector &dashPattern) override; - void setFillGradient(int index, QQuickPathGradient *gradient) override; - void endSync(bool async) override; - - void updateNode() override; - - void setNode(QQuickPathItemSoftwareRenderNode *node); - -private: - QQuickPathItemSoftwareRenderNode *m_node = nullptr; - int m_accDirty = 0; - struct VisualPathGuiData { - int dirty = 0; - QPainterPath path; - QPen pen; - float strokeWidth; - QColor fillColor; - QBrush brush; - Qt::FillRule fillRule; - }; - QVector m_vp; -}; - -class QQuickPathItemSoftwareRenderNode : public QSGRenderNode -{ -public: - QQuickPathItemSoftwareRenderNode(QQuickPathItem *item); - ~QQuickPathItemSoftwareRenderNode(); - - void render(const RenderState *state) override; - void releaseResources() override; - StateFlags changedStates() const override; - RenderingFlags flags() const override; - QRectF rect() const override; - -private: - QQuickPathItem *m_item; - - struct VisualPathRenderData { - QPainterPath path; - QPen pen; - float strokeWidth; - QBrush brush; - }; - QVector m_vp; - QRectF m_boundingRect; - - friend class QQuickPathItemSoftwareRenderer; -}; - -QT_END_NAMESPACE - -#endif // QQUICKPATHITEMSOFTWARERENDERER_P_H -- cgit v1.2.3