Skip to content

Commit

Permalink
QTBUG-119760: rhi: Enable registering cleanup callbacks with a key
Browse files Browse the repository at this point in the history
(cherry picked from commit 075dc6d6f93729f4d72efad467d0df1115cccfc6)

And the ability to deregister.
Going to be required by QRhiWidget.

Task-number: QTBUG-119760
Change-Id: If185cbed2faa042098ac1f6bb1d6daaffd834377
Reviewed-by: Andy Nichols <[email protected]>
(cherry picked from commit 84873c8)
Reviewed-by: Christian Strømme <[email protected]>
  • Loading branch information
jpek42 authored and GitHub Enterprise committed Dec 29, 2023
2 parents 2c45f75 + a1d63a2 commit 2f1a8c1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/gui/rhi/qrhi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5826,6 +5826,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
Expand All @@ -5840,6 +5867,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();
}

/*!
Expand Down
2 changes: 2 additions & 0 deletions src/gui/rhi/qrhi_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,8 @@ class Q_GUI_EXPORT QRhi

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();

using GpuFrameTimeCallback = std::function<void(float t)>;
Expand Down
11 changes: 11 additions & 0 deletions src/gui/rhi/qrhi_p_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ class QRhiImplementation
cleanupCallbacks.append(callback);
}

void addCleanupCallback(const void *key, const QRhi::CleanupCallback &callback)
{
keyedCleanupCallbacks[key] = callback;
}

void removeCleanupCallback(const void *key)
{
keyedCleanupCallbacks.remove(key);
}

void addGpuFrameTimeCallback(const QRhi::GpuFrameTimeCallback &callback)
{
gpuFrameTimeCallbacks.append(callback);
Expand Down Expand Up @@ -236,6 +246,7 @@ class QRhiImplementation
QHash<QRhiResource *, bool> resources;
QSet<QRhiResource *> pendingDeleteResources;
QVarLengthArray<QRhi::CleanupCallback, 4> cleanupCallbacks;
QHash<const void *, QRhi::CleanupCallback> keyedCleanupCallbacks;
QVarLengthArray<QRhi::GpuFrameTimeCallback, 4> gpuFrameTimeCallbacks;
QElapsedTimer pipelineCreationTimer;
qint64 accumulatedPipelineCreationTime = 0;
Expand Down

0 comments on commit 2f1a8c1

Please sign in to comment.