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>
(cherry picked from commit 84873c898cb4cc433f05d3161cad1e458d26c2e9)
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Laszlo Agocs 2023-07-19 11:18:57 +02:00 committed by Tor Arne Vestbø
parent a40ca88b63
commit de230ea9bd
3 changed files with 45 additions and 0 deletions

View File

@ -8272,6 +8272,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
@ -8286,6 +8313,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

@ -1867,6 +1867,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

@ -178,6 +178,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);
@ -238,6 +248,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;