rhiwindow: Set DPR on image texture to ensure DPR-agnostic drawing

Otherwise the 20x20 margins will produce different layouts depending
on the DPR, which is not what we want.

Change-Id: I4153d0843ef51c8e0f60d7d5166b153d45201c95
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
(cherry picked from commit a78938e0f686263540e06d923f2949169f3219e4)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2024-04-10 14:51:44 +02:00 committed by Qt Cherry-pick Bot
parent edc004ae7f
commit ed27756274
2 changed files with 10 additions and 4 deletions

View File

@ -427,6 +427,11 @@
QRhiShaderResourceBindings, continue to be valid all the time. It is only QRhiShaderResourceBindings, continue to be valid all the time. It is only
the underlying native resources that come and go over time. the underlying native resources that come and go over time.
Note also that we set a device pixel ratio on the image that matches
the window that we're drawing into. This ensures that the drawing code
can be DPR-agnostic, producing the same layout regardless of the DPR,
while also taking advantage of the additional pixels for improved fidelity.
\snippet rhiwindow/rhiwindow.cpp ensure-texture \snippet rhiwindow/rhiwindow.cpp ensure-texture
Once a QImage is generated and the QPainter-based drawing into it has Once a QImage is generated and the QPainter-based drawing into it has

View File

@ -281,17 +281,18 @@ void HelloWindow::ensureFullscreenTexture(const QSize &pixelSize, QRhiResourceUp
m_texture->create(); m_texture->create();
QImage image(pixelSize, QImage::Format_RGBA8888_Premultiplied); QImage image(pixelSize, QImage::Format_RGBA8888_Premultiplied);
image.setDevicePixelRatio(devicePixelRatio());
//! [ensure-texture] //! [ensure-texture]
QPainter painter(&image); QPainter painter(&image);
painter.fillRect(QRectF(QPointF(0, 0), pixelSize), QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f)); painter.fillRect(QRectF(QPointF(0, 0), size()), QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f));
painter.setPen(Qt::transparent); painter.setPen(Qt::transparent);
painter.setBrush({ QGradient(QGradient::DeepBlue) }); painter.setBrush({ QGradient(QGradient::DeepBlue) });
painter.drawRoundedRect(QRectF(QPointF(20, 20), pixelSize - QSize(40, 40)), 16, 16); painter.drawRoundedRect(QRectF(QPointF(20, 20), size() - QSize(40, 40)), 16, 16);
painter.setPen(Qt::black); painter.setPen(Qt::black);
QFont font; QFont font;
font.setPixelSize(0.05 * qMin(pixelSize.width(), pixelSize.height())); font.setPixelSize(0.05 * qMin(width(), height()));
painter.setFont(font); painter.setFont(font);
painter.drawText(QRectF(QPointF(60, 60), pixelSize - QSize(120, 120)), 0, painter.drawText(QRectF(QPointF(60, 60), size() - QSize(120, 120)), 0,
QLatin1String("Rendering with QRhi to a resizable QWindow.\nThe 3D API is %1.\nUse the command-line options to choose a different API.") QLatin1String("Rendering with QRhi to a resizable QWindow.\nThe 3D API is %1.\nUse the command-line options to choose a different API.")
.arg(graphicsApiName())); .arg(graphicsApiName()));
painter.end(); painter.end();