diff options
author | Laszlo Agocs <laszlo.agocs@qt.io> | 2017-01-11 17:05:14 +0100 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@qt.io> | 2017-01-23 15:44:17 +0000 |
commit | 5daaec1e193bc69f55d4ddbfef8911ce9810ea28 (patch) | |
tree | 56956c277c33aec1ed4d05149b7853e5c367182b | |
parent | b9f060f6fae5734216d7088afd0e3b52165551c5 (diff) |
Improve stencil clipping with NVPR
So that it actually performs when clipping the tiger. Add an example.
Change-Id: I7c1c6244710febdb6b02852ebca094665adec417
Reviewed-by: Andy Nichols <andy.nichols@qt.io>
-rw-r--r-- | examples/quick/pathitem/content/item17.qml | 1 | ||||
-rw-r--r-- | examples/quick/pathitem/content/pathitem.qml | 1 | ||||
-rw-r--r-- | examples/quick/pathitem/content/pathitemtigers.qml | 137 | ||||
-rw-r--r-- | examples/quick/pathitem/content/tiger.qml | 1 | ||||
-rw-r--r-- | examples/quick/pathitem/pathitem.pro | 1 | ||||
-rw-r--r-- | examples/quick/pathitem/pathitem.qrc | 1 | ||||
-rw-r--r-- | src/quick/items/qquickpathitem.cpp | 2 | ||||
-rw-r--r-- | src/quick/items/qquickpathitemnvprrenderer.cpp | 54 | ||||
-rw-r--r-- | src/quick/items/qquickpathitemnvprrenderer_p.h | 9 |
9 files changed, 177 insertions, 30 deletions
diff --git a/examples/quick/pathitem/content/item17.qml b/examples/quick/pathitem/content/item17.qml index 0197aa5fb6..7f3f06cfb8 100644 --- a/examples/quick/pathitem/content/item17.qml +++ b/examples/quick/pathitem/content/item17.qml @@ -70,6 +70,7 @@ Rectangle { source: "tiger.qml" asynchronous: true visible: status == Loader.Ready + scale: 0.4 } } } diff --git a/examples/quick/pathitem/content/pathitem.qml b/examples/quick/pathitem/content/pathitem.qml index e3a28b2bbe..c4a86ff8b1 100644 --- a/examples/quick/pathitem/content/pathitem.qml +++ b/examples/quick/pathitem/content/pathitem.qml @@ -58,6 +58,7 @@ Item { Component.onCompleted: { addExample("PathItem Gallery", "Simple path rendering examples", Qt.resolvedUrl("pathitemgallery.qml")) addExample("Interactive paths", "Dynamic path examples", Qt.resolvedUrl("pathiteminteract.qml")) + addExample("Clip My Tiger!", "Clip examples, a.k.a. What Not To Do", Qt.resolvedUrl("pathitemtigers.qml")) } } } diff --git a/examples/quick/pathitem/content/pathitemtigers.qml b/examples/quick/pathitem/content/pathitemtigers.qml new file mode 100644 index 0000000000..6c8e3a19ce --- /dev/null +++ b/examples/quick/pathitem/content/pathitemtigers.qml @@ -0,0 +1,137 @@ +/**************************************************************************** +** +** 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:BSD$ +** 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. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.9 // to get PathItem + +Rectangle { + id: root + width: 1024 + height: 768 + + property color col: "lightsteelblue" + gradient: Gradient { + GradientStop { position: 0.0; color: Qt.tint(root.col, "#20FFFFFF") } + GradientStop { position: 0.1; color: Qt.tint(root.col, "#20AAAAAA") } + GradientStop { position: 0.9; color: Qt.tint(root.col, "#20666666") } + GradientStop { position: 1.0; color: Qt.tint(root.col, "#20000000") } + } + + Rectangle { + id: scissorRect + width: 200 + height: 200 + x: 150 + property real centerY: parent.height / 2 - height / 2 + property real dy: 0 + y: centerY + dy + clip: true + + Loader { + id: loader1 + width: parent.width + height: parent.height + y: 25 - scissorRect.dy + source: "tiger.qml" + asynchronous: true + visible: status === Loader.Ready + } + + SequentialAnimation on dy { + loops: Animation.Infinite + running: loader1.status === Loader.Ready && loader1.item.status === PathItem.Ready + NumberAnimation { + from: 0 + to: -scissorRect.centerY + duration: 2000 + } + NumberAnimation { + from: -scissorRect.centerY + to: scissorRect.centerY + duration: 4000 + } + NumberAnimation { + from: scissorRect.centerY + to: 0 + duration: 2000 + } + } + } + + // With a more complex transformation (like rotation), stenciling is used + // instead of scissoring, this is more expensive. It may also trigger a + // slower code path for PathItems, depending on the path rendering backend + // in use, and may affect rendering quality as well. + // So in short: do not do this. + Rectangle { + id: stencilRect + width: 300 + height: 200 + anchors.right: parent.right + anchors.rightMargin: 100 + anchors.verticalCenter: parent.verticalCenter + clip: true // NB! still clips to bounding rect (not shape) + + Loader { + id: loader2 + width: parent.width + height: parent.height + source: "tiger.qml" + asynchronous: true + visible: status === Loader.Ready + } + + NumberAnimation on rotation { + from: 0 + to: 360 + duration: 5000 + loops: Animation.Infinite + } + } +} diff --git a/examples/quick/pathitem/content/tiger.qml b/examples/quick/pathitem/content/tiger.qml index 7d275ba22f..a9f4abe46a 100644 --- a/examples/quick/pathitem/content/tiger.qml +++ b/examples/quick/pathitem/content/tiger.qml @@ -56,7 +56,6 @@ PathItem { asynchronous: true anchors.fill: parent - scale: 0.4 VisualPath { fillColor: "#ffffff" diff --git a/examples/quick/pathitem/pathitem.pro b/examples/quick/pathitem/pathitem.pro index 8b606a3c50..3c12f35fd2 100644 --- a/examples/quick/pathitem/pathitem.pro +++ b/examples/quick/pathitem/pathitem.pro @@ -6,6 +6,7 @@ RESOURCES += pathitem.qrc OTHER_FILES += content/pathitem.qml \ content/pathitemgallery.qml \ content/pathiteminteract.qml \ + content/pathitemtigers.qml \ content/tiger.qml \ content/item1.qml \ content/item2.qml \ diff --git a/examples/quick/pathitem/pathitem.qrc b/examples/quick/pathitem/pathitem.qrc index 5b38d90c0c..d67c918faf 100644 --- a/examples/quick/pathitem/pathitem.qrc +++ b/examples/quick/pathitem/pathitem.qrc @@ -7,6 +7,7 @@ <file alias="pathitem.qml">content/pathitem.qml</file> <file alias="pathitemgallery.qml">content/pathitemgallery.qml</file> <file alias="pathiteminteract.qml">content/pathiteminteract.qml</file> + <file alias="pathitemtigers.qml">content/pathitemtigers.qml</file> <file alias="tiger.qml">content/tiger.qml</file> <file alias="item1.qml">content/item1.qml</file> <file alias="item2.qml">content/item2.qml</file> 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<QQuickPathItemNvprRenderer *>(renderer)->setNode( static_cast<QQuickPathItemNvprRenderNode *>(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; |