QRhiWidget: Register cleanup callback also on first RHI init

The first time ensureRhi() is called we don't have an RHI yet, and will
adopt the top level's repaint manager's RHI. We need to register a
cleanup callback for this RHI, so that if it goes away, we will
reset our RHI pointer to null, just like when we switch from one top
level to another (and get a new potential RHI).

Without the reset, we would be working with a stale QRhi pointer,
which we in most cases updated via a call to ensureRhi(), that
picked up the new QRhi, but for good measure we should reset it
if we can.

Pick-to: 6.7
Change-Id: Iac6d8787b636675bbcb4358e8f0baad26187b0e2
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
This commit is contained in:
Tor Arne Vestbø 2024-02-08 18:31:33 +01:00
parent 6fbbbef260
commit 986f8b361c

View File

@ -491,19 +491,21 @@ void QRhiWidgetPrivate::ensureRhi()
}
// NB the rhi member may be an invalid object, the pointer can be used, but no deref
if (currentRhi && rhi && rhi != currentRhi) {
// if previously we created our own but now get a QRhi from the
// top-level, then drop what we have and start using the top-level's
if (rhi == offscreenRenderer.rhi()) {
q->releaseResources(); // notify the user code about the early-release
releaseResources();
offscreenRenderer.reset();
} else {
// rhi resources created by us all belong to the old rhi, drop them;
// due to nulling out colorTexture this is also what ensures that
// initialize() is going to be called again eventually
resetRenderTargetObjects();
resetColorBufferObjects();
if (currentRhi && rhi != currentRhi) {
if (rhi) {
// if previously we created our own but now get a QRhi from the
// top-level, then drop what we have and start using the top-level's
if (rhi == offscreenRenderer.rhi()) {
q->releaseResources(); // notify the user code about the early-release
releaseResources();
offscreenRenderer.reset();
} else {
// rhi resources created by us all belong to the old rhi, drop them;
// due to nulling out colorTexture this is also what ensures that
// initialize() is going to be called again eventually
resetRenderTargetObjects();
resetColorBufferObjects();
}
}
// Normally the widget gets destroyed before the QRhi (which is managed by