diff --git a/src/quick/items/qquickrendertarget.cpp b/src/quick/items/qquickrendertarget.cpp index a6f6aeef5e..e8a2257626 100644 --- a/src/quick/items/qquickrendertarget.cpp +++ b/src/quick/items/qquickrendertarget.cpp @@ -64,6 +64,7 @@ QQuickRenderTargetPrivate::QQuickRenderTargetPrivate(const QQuickRenderTargetPri : ref(1), type(other->type), pixelSize(other->pixelSize), + devicePixelRatio(other->devicePixelRatio), sampleCount(other->sampleCount), u(other->u) { @@ -122,6 +123,42 @@ bool QQuickRenderTarget::isNull() const return d->type == QQuickRenderTargetPrivate::Type::Null; } +/*! + \return the device pixel ratio for the render target. This is the ratio + between \e{device pixels} and \e{device independent pixels}. + + The default device pixel ratio is 1.0. + + \since 6.3 + + \sa setDevicePixelRatio() +*/ +qreal QQuickRenderTarget::devicePixelRatio() const +{ + return d->devicePixelRatio; +} + +/*! + Sets the device pixel ratio for this render target to \a ratio. This is + the ratio between \e{device pixels} and \e{device independent pixels}. + + Note that the specified device pixel ratio value will be ignored if + QQuickRenderControl::renderWindow() is re-implemented to return a valid + QWindow. + + \since 6.3 + + \sa devicePixelRatio() +*/ +void QQuickRenderTarget::setDevicePixelRatio(qreal ratio) +{ + if (d->devicePixelRatio == ratio) + return; + + detach(); + d->devicePixelRatio = ratio; +} + /*! \return a new QQuickRenderTarget referencing an OpenGL texture object specified by \a textureId. @@ -384,6 +421,7 @@ bool QQuickRenderTarget::isEqual(const QQuickRenderTarget &other) const noexcept { if (d->type != other.d->type || d->pixelSize != other.d->pixelSize + || d->devicePixelRatio != other.d->devicePixelRatio || d->sampleCount != other.d->sampleCount) { return false; diff --git a/src/quick/items/qquickrendertarget.h b/src/quick/items/qquickrendertarget.h index c170025ca6..b43bd81003 100644 --- a/src/quick/items/qquickrendertarget.h +++ b/src/quick/items/qquickrendertarget.h @@ -66,6 +66,9 @@ public: bool isNull() const; + qreal devicePixelRatio() const; + void setDevicePixelRatio(qreal ratio); + #if QT_CONFIG(opengl) || defined(Q_CLANG_QDOC) static QQuickRenderTarget fromOpenGLTexture(uint textureId, const QSize &pixelSize, int sampleCount = 1); static QQuickRenderTarget fromOpenGLRenderBuffer(uint renderbufferId, const QSize &pixelSize, int sampleCount = 1); diff --git a/src/quick/items/qquickrendertarget_p.h b/src/quick/items/qquickrendertarget_p.h index 37d3f58ff5..2f355199eb 100644 --- a/src/quick/items/qquickrendertarget_p.h +++ b/src/quick/items/qquickrendertarget_p.h @@ -79,6 +79,7 @@ public: QAtomicInt ref; Type type = Type::Null; QSize pixelSize; + qreal devicePixelRatio = 1.0; int sampleCount = 1; struct NativeTexture { quint64 object; diff --git a/src/quick/items/qquickwindow.cpp b/src/quick/items/qquickwindow.cpp index 7388a7bada..5cffd599c4 100644 --- a/src/quick/items/qquickwindow.cpp +++ b/src/quick/items/qquickwindow.cpp @@ -515,6 +515,7 @@ void QQuickWindowPrivate::ensureCustomRenderTarget() redirect.renderTargetDirty = false; redirect.rt.reset(rhi, renderer); + redirect.devicePixelRatio = customRenderTarget.devicePixelRatio(); // a default constructed QQuickRenderTarget means no redirection if (customRenderTarget.isNull()) @@ -538,7 +539,7 @@ void QQuickWindowPrivate::syncSceneGraph() // Calculate the dpr the same way renderSceneGraph() will. qreal devicePixelRatio = q->effectiveDevicePixelRatio(); if (redirect.rt.renderTarget && !QQuickRenderControl::renderWindowFor(q)) - devicePixelRatio = 1; + devicePixelRatio = redirect.devicePixelRatio; QRhiCommandBuffer *cb = nullptr; if (rhi) { @@ -655,8 +656,10 @@ void QQuickWindowPrivate::renderSceneGraph(const QSize &size, const QSize &surfa renderer->setProjectionMatrixToRect(QRect(QPoint(0, 0), size), matrixFlags); renderer->setDevicePixelRatio(devicePixelRatio); } else { - renderer->setProjectionMatrixToRect(QRect(QPoint(0, 0), rect.size()), matrixFlags); - renderer->setDevicePixelRatio(1); + const QSizeF logicalSize = + redirect.rt.renderTarget->pixelSize() / redirect.devicePixelRatio; + renderer->setProjectionMatrixToRect(QRectF(QPointF(0, 0), logicalSize), matrixFlags); + renderer->setDevicePixelRatio(redirect.devicePixelRatio); } } else { QSize pixelSize; diff --git a/src/quick/items/qquickwindow_p.h b/src/quick/items/qquickwindow_p.h index c713291891..1bed7ae320 100644 --- a/src/quick/items/qquickwindow_p.h +++ b/src/quick/items/qquickwindow_p.h @@ -228,6 +228,7 @@ public: struct Redirect { QRhiCommandBuffer *commandBuffer = nullptr; QQuickWindowRenderTarget rt; + qreal devicePixelRatio = 1.0; bool renderTargetDirty = false; } redirect;