From 296b2f368a5a8f72c1066459d7cae9d5a6ffca71 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Mon, 5 Dec 2022 13:05:13 +0100 Subject: [PATCH] code simplified/harmonized --- src/nodes/QskBoxFillNode.cpp | 78 +++++++++++++++----------------- src/nodes/QskBoxFillNode.h | 4 -- src/nodes/QskShapeNode.cpp | 88 +++++++++++++----------------------- src/nodes/QskShapeNode.h | 3 -- 4 files changed, 69 insertions(+), 104 deletions(-) diff --git a/src/nodes/QskBoxFillNode.cpp b/src/nodes/QskBoxFillNode.cpp index 6650193a..fbb3db7f 100644 --- a/src/nodes/QskBoxFillNode.cpp +++ b/src/nodes/QskBoxFillNode.cpp @@ -60,7 +60,6 @@ class QskBoxFillNodePrivate final : public QSGGeometryNodePrivate } QskHashValue metricsHash = 0; - QskHashValue gradientHash = 0; QRectF rect; QSGGeometry geometry; @@ -83,60 +82,57 @@ void QskBoxFillNode::updateNode( { Q_D( QskBoxFillNode ); - const auto effectiveGradient = qskEffectiveGradient( gradient ); - - const auto metricsHash = qskMetricsHash( shapeMetrics, borderMetrics ); - const auto gradientHash = effectiveGradient.hash( 17321 ); - - const bool dirtyGeometry = ( metricsHash != d->metricsHash ) || ( rect == d->rect ); - const bool dirtyMaterial = gradientHash != d->gradientHash; - - if ( !( dirtyGeometry || dirtyMaterial ) ) - return; - - d->metricsHash = metricsHash; - d->gradientHash = gradientHash; - d->rect = rect; - - if ( rect.isEmpty() || !effectiveGradient.isVisible() ) + if ( rect.isEmpty() || !gradient.isVisible() ) { + d->rect = QRectF(); + d->metricsHash = 0; qskResetGeometry( this ); + return; } + const auto metricsHash = qskMetricsHash( shapeMetrics, borderMetrics ); + const bool dirtyGeometry = ( metricsHash != d->metricsHash ) || ( rect == d->rect ); + + d->metricsHash = metricsHash; + d->rect = rect; + if ( dirtyGeometry ) { QskBoxRenderer().renderFill( rect, shapeMetrics, borderMetrics, d->geometry ); markDirty( QSGNode::DirtyGeometry ); } - if ( dirtyMaterial ) + if ( gradient.isMonochrome() ) { - if ( effectiveGradient.isMonochrome() ) + if ( material() == nullptr || d->gradientType >= 0 ) { - if ( material() == nullptr || d->gradientType >= 0 ) - { - setMaterial( new QSGFlatColorMaterial() ); - d->gradientType = -1; - } - - auto colorMaterial = static_cast< QSGFlatColorMaterial* >( material() ); - colorMaterial->setColor( effectiveGradient.startColor().toRgb() ); - } - else - { - const auto gradientType = effectiveGradient.type(); - - if ( ( material() == nullptr ) || ( gradientType != d->gradientType ) ) - { - setMaterial( QskGradientMaterial::createMaterial( gradientType ) ); - d->gradientType = gradientType; - } - - auto gradientMaterial = static_cast< QskGradientMaterial* >( material() ); - gradientMaterial->updateGradient( rect, effectiveGradient ); + setMaterial( new QSGFlatColorMaterial() ); + d->gradientType = -1; } - markDirty( QSGNode::DirtyMaterial ); + const auto color = gradient.startColor().toRgb(); + + auto mat = static_cast< QSGFlatColorMaterial* >( material() ); + if ( mat->color() != color ) + { + mat->setColor( color ); + markDirty( QSGNode::DirtyMaterial ); + } + } + else + { + const auto effectiveGradient = qskEffectiveGradient( gradient ); + const auto gradientType = effectiveGradient.type(); + + if ( ( material() == nullptr ) || ( gradientType != d->gradientType ) ) + { + setMaterial( QskGradientMaterial::createMaterial( gradientType ) ); + d->gradientType = gradientType; + } + + auto mat = static_cast< QskGradientMaterial* >( material() ); + if ( mat->updateGradient( rect, effectiveGradient ) ) + markDirty( QSGNode::DirtyMaterial ); } } diff --git a/src/nodes/QskBoxFillNode.h b/src/nodes/QskBoxFillNode.h index 5c069f9c..3a5c9e56 100644 --- a/src/nodes/QskBoxFillNode.h +++ b/src/nodes/QskBoxFillNode.h @@ -24,10 +24,6 @@ class QSK_EXPORT QskBoxFillNode : public QSGGeometryNode const QskBoxShapeMetrics&, const QskBoxBorderMetrics&, const QskGradient& ); - void updateNode( const QRectF&, - const QskBoxShapeMetrics&, const QskBoxBorderMetrics&, - const QColor& ); - private: Q_DECLARE_PRIVATE( QskBoxFillNode ) }; diff --git a/src/nodes/QskShapeNode.cpp b/src/nodes/QskShapeNode.cpp index 79f37009..a95101a1 100644 --- a/src/nodes/QskShapeNode.cpp +++ b/src/nodes/QskShapeNode.cpp @@ -18,8 +18,9 @@ QSK_QT_PRIVATE_END static inline QskGradient qskEffectiveGradient( const QskGradient& gradient ) { - if ( gradient.type() == QskGradient::Stops ) + if ( gradient.type() == QskGradient::Stops || gradient.isMonochrome() ) { + // the shader for linear gradients is the fastest QskGradient g; g.setLinearDirection( Qt::Vertical ); g.setStops( gradient.stops() ); @@ -112,44 +113,6 @@ QskShapeNode::QskShapeNode() setFlag( QSGNode::OwnsMaterial, true ); } -void QskShapeNode::updateNode( const QPainterPath& path, - const QTransform& transform, const QColor& color ) -{ - Q_D( QskShapeNode ); - - if ( path.isEmpty() || !color.isValid() || color.alpha() == 0 ) - { - d->path = QPainterPath(); - qskResetGeometry( this ); - - return; - } - - if ( ( transform != d->transform ) || ( path != d->path ) ) - { - d->path = path; - d->transform = transform; - - qskUpdateGeometry( path, transform, d->geometry ); - markDirty( QSGNode::DirtyGeometry ); - } - - if ( material() == nullptr || d->gradientType >= 0 ) - { - setMaterial( new QSGFlatColorMaterial() ); - d->gradientType = -1; - } - - const auto c = color.toRgb(); - - auto colorMaterial = static_cast< QSGFlatColorMaterial* >( material() ); - if ( colorMaterial->color() != c ) - { - colorMaterial->setColor( c ); - markDirty( QSGNode::DirtyMaterial ); - } -} - void QskShapeNode::updateNode( const QPainterPath& path, const QTransform& transform, const QRectF& rect, const QskGradient& gradient ) { @@ -158,17 +121,12 @@ void QskShapeNode::updateNode( const QPainterPath& path, if ( path.isEmpty() || !gradient.isVisible() ) { d->path = QPainterPath(); + d->transform = QTransform(); qskResetGeometry( this ); return; } - if ( gradient.isMonochrome() ) - { - updateNode( path, transform, gradient.startColor() ); - return; - } - if ( ( transform != d->transform ) || ( path != d->path ) ) { d->path = path; @@ -178,18 +136,36 @@ void QskShapeNode::updateNode( const QPainterPath& path, markDirty( QSGNode::DirtyGeometry ); } - const auto effectiveGradient = qskEffectiveGradient( gradient ); - - const auto gradientType = effectiveGradient.type(); - - if ( ( material() == nullptr ) || ( gradientType != d->gradientType ) ) + if ( gradient.isMonochrome() ) { - setMaterial( QskGradientMaterial::createMaterial( gradientType ) ); - d->gradientType = gradientType; + if ( material() == nullptr || d->gradientType >= 0 ) + { + setMaterial( new QSGFlatColorMaterial() ); + d->gradientType = -1; + } + + const auto color = gradient.startColor().toRgb(); + + auto mat = static_cast< QSGFlatColorMaterial* >( material() ); + if ( mat->color() != color ) + { + mat->setColor( color ); + markDirty( QSGNode::DirtyMaterial ); + } } + else + { + const auto effectiveGradient = qskEffectiveGradient( gradient ); + const auto gradientType = effectiveGradient.type(); - auto gradientMaterial = static_cast< QskGradientMaterial* >( material() ); - if ( gradientMaterial->updateGradient( rect, effectiveGradient ) ) - markDirty( QSGNode::DirtyMaterial ); + if ( ( material() == nullptr ) || ( gradientType != d->gradientType ) ) + { + setMaterial( QskGradientMaterial::createMaterial( gradientType ) ); + d->gradientType = gradientType; + } + + auto mat = static_cast< QskGradientMaterial* >( material() ); + if ( mat->updateGradient( rect, effectiveGradient ) ) + markDirty( QSGNode::DirtyMaterial ); + } } - diff --git a/src/nodes/QskShapeNode.h b/src/nodes/QskShapeNode.h index 3652b204..7f23e144 100644 --- a/src/nodes/QskShapeNode.h +++ b/src/nodes/QskShapeNode.h @@ -23,9 +23,6 @@ class QSK_EXPORT QskShapeNode : public QSGGeometryNode void updateNode( const QPainterPath&, const QTransform&, const QRectF&, const QskGradient& ); - void updateNode( const QPainterPath&, - const QTransform&, const QColor& ); - private: Q_DECLARE_PRIVATE( QskShapeNode ) };