From 1f5996236040597dd5bbd1a3e8b73c6a0a8514c3 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Sun, 27 Oct 2019 08:11:47 +0100 Subject: [PATCH] devicePixelRatio detection moved to QskTextureRenderer --- src/controls/QskSkinlet.cpp | 25 +++++++++----------- src/nodes/QskTextureNode.cpp | 39 +++----------------------------- src/nodes/QskTextureRenderer.cpp | 38 +++++++++++++++++++++++++++++++ src/nodes/QskTextureRenderer.h | 3 +++ 4 files changed, 55 insertions(+), 50 deletions(-) diff --git a/src/controls/QskSkinlet.cpp b/src/controls/QskSkinlet.cpp index f6f6c1f1..6f27cdf0 100644 --- a/src/controls/QskSkinlet.cpp +++ b/src/controls/QskSkinlet.cpp @@ -90,21 +90,18 @@ static inline QSGNode* qskUpdateGraphicNode( if ( control->testControlFlag( QskControl::PreferRasterForTextures ) ) mode = QskTextureRenderer::Raster; - if ( auto window = control->window() ) - { - /* - Aligning the rect according to scene coordinates, so that - we don't run into rounding issues downstream, where values - will be floored/ceiled ending up with a slightly different - aspect ratio. - */ - const QRectF sceneRect( - control->mapToScene( r.topLeft() ), - r.size() * window->effectiveDevicePixelRatio() ); + /* + Aligning the rect according to scene coordinates, so that + we don't run into rounding issues downstream, where values + will be floored/ceiled ending up with a slightly different + aspect ratio. + */ + const QRectF sceneRect( + control->mapToScene( r.topLeft() ), + r.size() * QskTextureRenderer::devicePixelRatio() ); - r = qskInnerRect( sceneRect ); - r.moveTopLeft( control->mapFromScene( r.topLeft() ) ); - } + r = qskInnerRect( sceneRect ); + r.moveTopLeft( control->mapFromScene( r.topLeft() ) ); } graphicNode->setGraphic( graphic, colorFilter, mode, r ); diff --git a/src/nodes/QskTextureNode.cpp b/src/nodes/QskTextureNode.cpp index 81142dee..4e8f062e 100644 --- a/src/nodes/QskTextureNode.cpp +++ b/src/nodes/QskTextureNode.cpp @@ -1,4 +1,5 @@ #include "QskTextureNode.h" +#include "QskTextureRenderer.h" #include #include @@ -6,41 +7,6 @@ #include -#if 1 - -#include -#include -#include -#include - -static inline qreal qskDevicePixelRatio() -{ - qreal ratio = 1.0; - - const auto context = QOpenGLContext::currentContext(); - - if ( context->surface()->surfaceClass() == QSurface::Window ) - { - auto* window = static_cast< QWindow* >( context->surface() ); - - if ( auto* quickWindow = qobject_cast< QQuickWindow* >( window ) ) - ratio = quickWindow->effectiveDevicePixelRatio(); - else - ratio = window->devicePixelRatio(); - } - else - { - if ( context->screen() ) - ratio = context->screen()->devicePixelRatio(); - else - ratio = qGuiApp->devicePixelRatio(); - } - - return ratio; -} - -#endif - namespace { class MaterialShader final : public QSGMaterialShader @@ -316,7 +282,8 @@ void QskTextureNode::updateTexture() r.setBottom( 0 ); } - const qreal ratio = qskDevicePixelRatio(); + const qreal ratio = QskTextureRenderer::devicePixelRatio(); + const QRectF rect( d->rect.x(), d->rect.y(), d->rect.width() / ratio, d->rect.height() / ratio ); diff --git a/src/nodes/QskTextureRenderer.cpp b/src/nodes/QskTextureRenderer.cpp index 4978ec53..f0f6471d 100644 --- a/src/nodes/QskTextureRenderer.cpp +++ b/src/nodes/QskTextureRenderer.cpp @@ -18,6 +18,11 @@ #include #include +#include +#include +#include +#include + static uint qskCreateTextureOpenGL( const QSize& size, QskTextureRenderer::PaintHelper* helper ) { @@ -173,3 +178,36 @@ uint QskTextureRenderer::createTextureFromGraphic( PaintHelper helper( graphic, colorFilter, aspectRatioMode ); return createTexture( renderMode, size, &helper ); } + +static inline qreal qskOffscreenBufferRatio( const QOpenGLContext* context ) +{ + if ( context->screen() ) + return context->screen()->devicePixelRatio(); + + return qGuiApp->devicePixelRatio(); +} + +qreal QskTextureRenderer::devicePixelRatio( const QOpenGLContext* context ) +{ + if ( context == nullptr ) + context = QOpenGLContext::currentContext(); + + qreal ratio = 1.0; + + if ( context->surface()->surfaceClass() == QSurface::Window ) + { + auto* window = static_cast< QWindow* >( context->surface() ); + + if ( auto* quickWindow = qobject_cast< QQuickWindow* >( window ) ) + ratio = quickWindow->effectiveDevicePixelRatio(); + else + ratio = window->devicePixelRatio(); + } + else + { + ratio = qskOffscreenBufferRatio( context ); + } + + return ratio; +} + diff --git a/src/nodes/QskTextureRenderer.h b/src/nodes/QskTextureRenderer.h index 4bd715eb..876ab739 100644 --- a/src/nodes/QskTextureRenderer.h +++ b/src/nodes/QskTextureRenderer.h @@ -14,6 +14,7 @@ class QskColorFilter; class QPainter; class QSize; +class QOpenGLContext; namespace QskTextureRenderer { @@ -46,6 +47,8 @@ namespace QskTextureRenderer QSK_EXPORT uint createTextureFromGraphic( RenderMode, const QSize&, const QskGraphic&, const QskColorFilter&, Qt::AspectRatioMode ); + + QSK_EXPORT qreal devicePixelRatio( const QOpenGLContext* = nullptr ); } #endif