rhi: Enable registering cleanup callbacks with a key

And the ability to deregister.

Going to be required by QRhiWidget.

Change-Id: If185cbed2faa042098ac1f6bb1d6daaffd834377
Reviewed-by: Andy Nichols <andy.nichols@qt.io>
This commit is contained in:
Laszlo Agocs 2023-07-19 11:18:57 +02:00
parent f9d473ac72
commit 84873c898c
3 changed files with 45 additions and 0 deletions

View File

@ -8445,6 +8445,33 @@ void QRhi::addCleanupCallback(const CleanupCallback &callback)
d->addCleanupCallback(callback);
}
/*!
\overload
Registers \a callback to be invoked either when the QRhi is destroyed or
when runCleanup() is called. This overload takes an opaque pointer, \a key,
that is used to ensure that a given callback is registered (and so called)
only once.
\sa removeCleanupCallback()
*/
void QRhi::addCleanupCallback(const void *key, const CleanupCallback &callback)
{
d->addCleanupCallback(key, callback);
}
/*!
Deregisters the callback with \a key. If no cleanup callback was registered
with \a key, the function does nothing. Callbacks registered without a key
cannot be removed.
\sa addCleanupCallback()
*/
void QRhi::removeCleanupCallback(const void *key)
{
d->removeCleanupCallback(key);
}
/*!
Invokes all registered cleanup functions. The list of cleanup callbacks it
then cleared. Normally destroying the QRhi does this automatically, but
@ -8459,6 +8486,11 @@ void QRhi::runCleanup()
f(this);
d->cleanupCallbacks.clear();
for (auto it = d->keyedCleanupCallbacks.cbegin(), end = d->keyedCleanupCallbacks.cend(); it != end; ++it)
it.value()(this);
d->keyedCleanupCallbacks.clear();
}
/*!

View File

@ -1876,6 +1876,8 @@ public:
using CleanupCallback = std::function<void(QRhi *)>;
void addCleanupCallback(const CleanupCallback &callback);
void addCleanupCallback(const void *key, const CleanupCallback &callback);
void removeCleanupCallback(const void *key);
void runCleanup();
QRhiGraphicsPipeline *newGraphicsPipeline();

View File

@ -179,6 +179,16 @@ public:
cleanupCallbacks.append(callback);
}
void addCleanupCallback(const void *key, const QRhi::CleanupCallback &callback)
{
keyedCleanupCallbacks[key] = callback;
}
void removeCleanupCallback(const void *key)
{
keyedCleanupCallbacks.remove(key);
}
bool sanityCheckGraphicsPipeline(QRhiGraphicsPipeline *ps);
bool sanityCheckShaderResourceBindings(QRhiShaderResourceBindings *srb);
void updateLayoutDesc(QRhiShaderResourceBindings *srb);
@ -239,6 +249,7 @@ private:
QHash<QRhiResource *, bool> resources;
QSet<QRhiResource *> pendingDeleteResources;
QVarLengthArray<QRhi::CleanupCallback, 4> cleanupCallbacks;
QHash<const void *, QRhi::CleanupCallback> keyedCleanupCallbacks;
QElapsedTimer pipelineCreationTimer;
qint64 accumulatedPipelineCreationTime = 0;