Skip to content

Commit

Permalink
Revert "Imported fixes from Qt to remediate issues with RHI."
Browse files Browse the repository at this point in the history
This reverts commit 6d86ab7.
  • Loading branch information
zhaojz2308 committed Jul 1, 2024
1 parent 7cbf0ae commit d7a8c65
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 390 deletions.
7 changes: 0 additions & 7 deletions src/gui/painting/qbackingstorerhisupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include <QtGui/private/qvulkandefaultinstance_p.h>
#endif

#include <QtGui/private/qrhinull_p.h>

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(lcQpaBackingStore)
Expand Down Expand Up @@ -68,11 +66,6 @@ bool QBackingStoreRhiSupport::create()
QOffscreenSurface *surface = nullptr;
QRhi::Flags flags;

if (m_config.api() == QPlatformBackingStoreRhiConfig::Null) {
QRhiNullInitParams params;
rhi = QRhi::create(QRhi::Null, &params, flags);
}

#if QT_CONFIG(opengl)
if (!rhi && m_config.api() == QPlatformBackingStoreRhiConfig::OpenGL) {
surface = QRhiGles2InitParams::newFallbackSurface(m_format);
Expand Down
84 changes: 24 additions & 60 deletions src/gui/painting/qplatformbackingstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#include <QtCore/private/qobject_p.h>

#include <unordered_map>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcQpaBackingStore, "qt.qpa.backingstore", QtWarningMsg);
Expand All @@ -28,14 +26,11 @@ class QPlatformBackingStorePrivate
QWindow *window;
QBackingStore *backingStore;

struct SurfaceSupport {
// The order matters. if it needs to be rearranged in the future, call
// reset() explicitly from the dtor in the correct order.
// (first the compositor, then the rhiSupport)
QBackingStoreRhiSupport rhiSupport;
QBackingStoreDefaultCompositor compositor;
};
std::unordered_map<QSurface::SurfaceType, SurfaceSupport> surfaceSupport;
// The order matters. if it needs to be rearranged in the future, call
// reset() explicitly from the dtor in the correct order.
// (first the compositor, then the rhiSupport)
QBackingStoreRhiSupport rhiSupport;
QBackingStoreDefaultCompositor compositor;
};

struct QBackingstoreTextureInfo
Expand Down Expand Up @@ -215,12 +210,8 @@ QPlatformBackingStore::FlushResult QPlatformBackingStore::rhiFlush(QWindow *wind
QPlatformTextureList *textures,
bool translucentBackground)
{
auto &surfaceSupport = d_ptr->surfaceSupport[window->surfaceType()];
return surfaceSupport.compositor.flush(this,
surfaceSupport.rhiSupport.rhi(),
surfaceSupport.rhiSupport.swapChainForWindow(window),
window, sourceDevicePixelRatio, region, offset, textures,
translucentBackground);
return d_ptr->compositor.flush(this, d_ptr->rhiSupport.rhi(), d_ptr->rhiSupport.swapChainForWindow(window),
window, sourceDevicePixelRatio, region, offset, textures, translucentBackground);
}

/*!
Expand Down Expand Up @@ -270,10 +261,7 @@ QRhiTexture *QPlatformBackingStore::toTexture(QRhiResourceUpdateBatch *resourceU
const QRegion &dirtyRegion,
TextureFlags *flags) const
{
auto &surfaceSupport = d_ptr->surfaceSupport[window()->surfaceType()];
return surfaceSupport.compositor.toTexture(this,
surfaceSupport.rhiSupport.rhi(), resourceUpdates,
dirtyRegion, flags);
return d_ptr->compositor.toTexture(this, d_ptr->rhiSupport.rhi(), resourceUpdates, dirtyRegion, flags);
}

/*!
Expand Down Expand Up @@ -368,56 +356,32 @@ bool QPlatformBackingStore::scroll(const QRegion &area, int dx, int dy)
return false;
}

void QPlatformBackingStore::createRhi(QWindow *window, QPlatformBackingStoreRhiConfig config)
{
auto &support = d_ptr->surfaceSupport[window->surfaceType()];
if (!support.rhiSupport.rhi()) {
qCDebug(lcQpaBackingStore) << "Setting up RHI support in" << this
<< "for" << window << "with" << window->surfaceType()
<< "and requested API" << config.api();
if (config.api() == QPlatformBackingStoreRhiConfig::Null) {
// Auto detect based on window's surface type
switch (window->surfaceType()) {
case QSurface::OpenGLSurface:
config.setApi(QPlatformBackingStoreRhiConfig::OpenGL);
break;
case QSurface::MetalSurface:
config.setApi(QPlatformBackingStoreRhiConfig::Metal);
break;
case QSurface::Direct3DSurface:
config.setApi(QPlatformBackingStoreRhiConfig::D3D11);
break;
case QSurface::VulkanSurface:
config.setApi(QPlatformBackingStoreRhiConfig::Vulkan);
break;
default:
; // Default null-configuration
}
}
support.rhiSupport.setConfig(config);
support.rhiSupport.setWindow(window);
support.rhiSupport.setFormat(window->format());
support.rhiSupport.create();
}
void QPlatformBackingStore::setRhiConfig(const QPlatformBackingStoreRhiConfig &config)
{
if (!config.isEnabled())
return;

d_ptr->rhiSupport.setConfig(config);
d_ptr->rhiSupport.setWindow(d_ptr->window);
d_ptr->rhiSupport.setFormat(d_ptr->window->format());
d_ptr->rhiSupport.create();
}

QRhi *QPlatformBackingStore::rhi(QWindow *window) const
QRhi *QPlatformBackingStore::rhi() const
{
// Returning null is valid, and means this is not a QRhi-capable backingstore.
return d_ptr->surfaceSupport[window->surfaceType()].rhiSupport.rhi();
return d_ptr->rhiSupport.rhi();
}

void QPlatformBackingStore::graphicsDeviceReportedLost(QWindow *window)
void QPlatformBackingStore::graphicsDeviceReportedLost()
{
auto &surfaceSupport = d_ptr->surfaceSupport[window->surfaceType()];
if (!surfaceSupport.rhiSupport.rhi())
if (!d_ptr->rhiSupport.rhi())
return;

qWarning("Rhi backingstore: graphics device lost, attempting to reinitialize");
surfaceSupport.compositor.reset();
surfaceSupport.rhiSupport.reset();
surfaceSupport.rhiSupport.create();
if (!surfaceSupport.rhiSupport.rhi())
d_ptr->rhiSupport.reset();
d_ptr->rhiSupport.create();
if (!d_ptr->rhiSupport.rhi())
qWarning("Rhi backingstore: failed to reinitialize after losing the device");
}

Expand Down
11 changes: 4 additions & 7 deletions src/gui/painting/qplatformbackingstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,13 @@ class QRhiSwapChain;

struct Q_GUI_EXPORT QPlatformBackingStoreRhiConfig
{
Q_GADGET
public:
enum Api {
OpenGL,
Metal,
Vulkan,
D3D11,
Null
};
Q_ENUM(Api)

QPlatformBackingStoreRhiConfig()
: m_enable(false)
Expand Down Expand Up @@ -173,11 +170,11 @@ class Q_GUI_EXPORT QPlatformBackingStore
virtual void beginPaint(const QRegion &);
virtual void endPaint();

void createRhi(QWindow *window, QPlatformBackingStoreRhiConfig config = {});
QRhi *rhi(QWindow *window) const;

void setRhiConfig(const QPlatformBackingStoreRhiConfig &config);
QRhi *rhi() const;
QRhiSwapChain *rhiSwapChain() const;
void surfaceAboutToBeDestroyed();
void graphicsDeviceReportedLost(QWindow *window);
void graphicsDeviceReportedLost();

private:
QPlatformBackingStorePrivate *d_ptr;
Expand Down
20 changes: 18 additions & 2 deletions src/gui/painting/qrhibackingstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,24 @@ void QRhiBackingStore::flush(QWindow *window, const QRegion &region, const QPoin
Q_UNUSED(region);
Q_UNUSED(offset);

if (!rhi(window))
createRhi(window);
if (window != this->window())
return;

if (!rhi()) {
QPlatformBackingStoreRhiConfig rhiConfig;
switch (window->surfaceType()) {
case QSurface::OpenGLSurface:
rhiConfig.setApi(QPlatformBackingStoreRhiConfig::OpenGL);
break;
case QSurface::MetalSurface:
rhiConfig.setApi(QPlatformBackingStoreRhiConfig::Metal);
break;
default:
Q_UNREACHABLE();
}
rhiConfig.setEnabled(true);
setRhiConfig(rhiConfig);
}

static QPlatformTextureList emptyTextureList;
bool translucentBackground = m_image.hasAlphaChannel();
Expand Down
12 changes: 10 additions & 2 deletions src/opengl/qopenglcompositorbackingstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ void QOpenGLCompositorBackingStore::flush(QWindow *window, const QRegion &region
Q_UNUSED(region);
Q_UNUSED(offset);

m_rhi = rhi(window);
m_rhi = rhi();
if (!m_rhi) {
setRhiConfig(QPlatformBackingStoreRhiConfig(QPlatformBackingStoreRhiConfig::OpenGL));
m_rhi = rhi();
}
Q_ASSERT(m_rhi);

QOpenGLCompositor *compositor = QOpenGLCompositor::instance();
Expand Down Expand Up @@ -177,7 +181,11 @@ QPlatformBackingStore::FlushResult QOpenGLCompositorBackingStore::rhiFlush(QWind
Q_UNUSED(translucentBackground);
Q_UNUSED(sourceDevicePixelRatio);

m_rhi = rhi(window);
m_rhi = rhi();
if (!m_rhi) {
setRhiConfig(QPlatformBackingStoreRhiConfig(QPlatformBackingStoreRhiConfig::OpenGL));
m_rhi = rhi();
}
Q_ASSERT(m_rhi);

QOpenGLCompositor *compositor = QOpenGLCompositor::instance();
Expand Down
13 changes: 9 additions & 4 deletions src/openglwidgets/qopenglwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,9 @@ void QOpenGLWidgetPrivate::ensureRhiDependentResources()
{
Q_Q(QOpenGLWidget);

QRhi *rhi = QWidgetPrivate::rhi();
QRhi *rhi = nullptr;
if (QWidgetRepaintManager *repaintManager = QWidgetPrivate::get(q->window())->maybeRepaintManager())
rhi = repaintManager->rhi();

// If there is no rhi, because we are completely offscreen, then there's no wrapperTexture either
if (rhi && rhi->backend() == QRhi::OpenGLES2) {
Expand Down Expand Up @@ -826,6 +828,7 @@ void QOpenGLWidgetPrivate::initialize()
// If no global shared context get our toplevel's context with which we
// will share in order to make the texture usable by the underlying window's backingstore.
QWidget *tlw = q->window();
QWidgetPrivate *tlwd = get(tlw);

// Do not include the sample count. Requesting a multisampled context is not necessary
// since we render into an FBO, never to an actual surface. What's more, attempting to
Expand All @@ -834,7 +837,9 @@ void QOpenGLWidgetPrivate::initialize()
requestedSamples = requestedFormat.samples();
requestedFormat.setSamples(0);

QRhi *rhi = QWidgetPrivate::rhi();
QRhi *rhi = nullptr;
if (QWidgetRepaintManager *repaintManager = tlwd->maybeRepaintManager())
rhi = repaintManager->rhi();

// Could be that something else already initialized the window with some
// other graphics API for the QRhi, that's not good.
Expand Down Expand Up @@ -1682,8 +1687,8 @@ bool QOpenGLWidget::event(QEvent *e)
if (!QCoreApplication::testAttribute(Qt::AA_ShareOpenGLContexts))
d->reset();
}
if (d->rhi()) {
if (!d->initialized && !size().isEmpty()) {
if (QWidgetRepaintManager *repaintManager = QWidgetPrivate::get(window())->maybeRepaintManager()) {
if (!d->initialized && !size().isEmpty() && repaintManager->rhi()) {
d->initialize();
if (d->initialized) {
d->recreateFbos();
Expand Down
Loading

0 comments on commit d7a8c65

Please sign in to comment.