Fix graphprinting example crash

We exceed maximum texture size on certain platforms. Added a check
for it.

Pick-to: 6.9.1
Fixes: QTBUG-135384
Fixes: QTBUG-135386
Change-Id: I0a09337c9c6b831f801df35b5756327f45507237
Reviewed-by: Owais Akhtar <owais.akhtar@qt.io>
Reviewed-by: Tomi Korpipää <tomi.korpipaa@qt.io>
(cherry picked from commit 713a6e0b72)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tomi Korpipaa 2025-04-28 14:48:49 +03:00 committed by Qt Cherry-pick Bot
parent ed3893c5b1
commit 46c4bd0784
4 changed files with 64 additions and 3 deletions

View File

@ -40,6 +40,7 @@ target_link_libraries(graphprinting PRIVATE
Qt::Quick3D
Qt::Graphs
Qt::PrintSupport
Qt::GuiPrivate
)
qt6_add_qml_module(graphprinting

View File

@ -1,8 +1,13 @@
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "graphprinter.h"
#include <QtGui/qtguiglobal.h>
#include <QtGui/qtransform.h>
#include <QtPrintSupport/QtPrintSupport>
#if QT_CONFIG(opengl)
#include <QtGui/qoffscreensurface.h>
#endif
#include <rhi/qrhi.h>
GraphPrinter::GraphPrinter(QObject *parent)
: QObject(parent)
@ -10,6 +15,45 @@ GraphPrinter::GraphPrinter(QObject *parent)
GraphPrinter::~GraphPrinter() {}
static qreal s_maxTextureSize = 0.;
qreal GraphPrinter::maxTextureSize()
{
// Query maximum texture size only once
if (!s_maxTextureSize) {
std::unique_ptr<QRhi> rhi;
#if defined(Q_OS_WIN)
QRhiD3D12InitParams params;
rhi.reset(QRhi::create(QRhi::D3D12, &params));
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS)
QRhiMetalInitParams params;
rhi.reset(QRhi::create(QRhi::Metal, &params));
#elif QT_CONFIG(opengl)
QRhiGles2InitParams params;
params.fallbackSurface = QRhiGles2InitParams::newFallbackSurface();
rhi.reset(QRhi::create(QRhi::OpenGLES2, &params));
#elif QT_CONFIG(vulkan)
if (!qEnvironmentVariable("QSG_RHI_BACKEND").compare("vulkan")) {
QVulkanInstance inst;
inst.setExtensions(QRhiVulkanInitParams::preferredInstanceExtensions());
if (inst.create()) {
QRhiVulkanInitParams params;
params.inst = &inst;
rhi.reset(QRhi::create(QRhi::Vulkan, &params));
} else {
qWarning("Failed to create Vulkan instance");
}
}
#endif
if (rhi)
s_maxTextureSize = qreal(rhi->resourceLimit(QRhi::TextureSizeMax));
else
s_maxTextureSize = 4096.; // Use 4096 as the minimum
}
return s_maxTextureSize;
}
QString GraphPrinter::generatePDF(const QUrl &path, const QImage &image)
{
//! [0]

View File

@ -19,6 +19,7 @@ public:
Q_INVOKABLE QString generatePDF(const QUrl &path, const QImage &image);
Q_INVOKABLE QString print(const QImage &image, const QString printerName);
Q_INVOKABLE QStringList getPrinters();
Q_INVOKABLE qreal maxTextureSize();
};
#endif // GRAPHPRINTER_H

View File

@ -146,6 +146,8 @@ Rectangle {
}
MessageDialog {
id: message
onButtonClicked: mainView.cleanAfterPrint()
}
//! [1.1]
@ -257,7 +259,20 @@ Rectangle {
//! [3.3]
function prepareForPrint() {
if (stackLayout.currentIndex === 1) {
outputsize = Qt.size(bargraph.width * 4, bargraph.height * 4)
var newsize = Qt.size(bargraph.width * 4, bargraph.height * 4)
// check that we do not exceed maximum texture size
if (newsize.width * Screen.devicePixelRatio > graphPrinter.maxTextureSize() ) {
// scale to 25% under max texture size to be on the safe side; some GPUs seem
// to glitch when using the abosulute max
var ratio = (newsize.width * Screen.devicePixelRatio * 1.25)
/ graphPrinter.maxTextureSize()
newsize.width /= ratio
newsize.height /= ratio
}
outputsize.width = Math.round(newsize.width)
outputsize.height = Math.round(newsize.height)
// resize the bar graph to match the PDF output size
item.width = outputsize.width
item.height = outputsize.height
@ -269,8 +284,8 @@ Rectangle {
function cleanAfterPrint() {
if (stackLayout.currentIndex === 1) {
// resize the bar graph back to the actual visual size
item.width = mainView.width
item.height = mainView.height
item.width = stackLayout.width
item.height = stackLayout.height
}
}
//! [3.3]