Skip to content

Commit

Permalink
Correct issues when dynamically creating and destroying rivequickitem…
Browse files Browse the repository at this point in the history
…s in a scene
  • Loading branch information
jebos committed Jul 7, 2023
1 parent 1cd6ab1 commit 9ede9cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/RiveQtQuickItem/riveqsgrendernode.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class RiveQSGBaseNode
public:
RiveQSGBaseNode(std::weak_ptr<rive::ArtboardInstance> artboardInstance, RiveQtQuickItem *item);

virtual void detach() { m_item = nullptr; }
virtual void renderOffscreen() { }
virtual void setRect(const QRectF &bounds);
virtual QPointF topLeft() const;
Expand Down
11 changes: 11 additions & 0 deletions src/RiveQtQuickItem/riveqsgrhirendernode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ RiveQSGRHIRenderNode::~RiveQSGRHIRenderNode()
if (m_item->m_renderNode == this) {
m_item->m_renderNode = nullptr;
}
} else {
// resources are already destroyed by qt
delete m_renderer;
return;
}

while (!m_cleanupList.empty()) {
Expand Down Expand Up @@ -184,6 +188,10 @@ QSGRenderNode::StateFlags RiveQSGRHIRenderNode::changedStates() const

void RiveQSGRHIRenderNode::prepare()
{
if (!m_item) {
return;
}

QSGRendererInterface *renderInterface = m_window->rendererInterface();
QRhiSwapChain *swapChain =
static_cast<QRhiSwapChain *>(renderInterface->getResource(m_window, QSGRendererInterface::RhiSwapchainResource));
Expand Down Expand Up @@ -227,6 +235,9 @@ void RiveQSGRHIRenderNode::prepare()
{ // update projection matrix
QMatrix4x4 projMatrix = *projectionMatrix();

if (!m_item) {
return;
}
const auto window2itemScaleX = m_item->window()->width() / m_item->width();
const auto window2itemScaleY = m_item->window()->height() / m_item->height();

Expand Down
15 changes: 12 additions & 3 deletions src/RiveQtQuickItem/riveqtquickitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ RiveQtQuickItem::RiveQtQuickItem(QQuickItem *parent)
update();
}

RiveQtQuickItem::~RiveQtQuickItem() { }
RiveQtQuickItem::~RiveQtQuickItem()
{
if (m_renderNode) {
m_renderNode->detach();
}
}

void RiveQtQuickItem::triggerAnimation(int id)
{
Expand Down Expand Up @@ -346,7 +351,7 @@ int RiveQtQuickItem::currentAnimationIndex() const

void RiveQtQuickItem::loadRiveFile(const QString &source)
{
if (m_loadingStatus != Idle && m_loadingStatus != Unloading) {
if (m_loadingStatus != Idle && m_loadingStatus != Unloading && m_loadingStatus != Loading) {
// clear the data
m_artboardInfoList.clear();
emit artboardsChanged();
Expand Down Expand Up @@ -377,7 +382,7 @@ void RiveQtQuickItem::loadRiveFile(const QString &source)

if (!currentWindow) {
qCWarning(rqqpItem) << "No window found";
m_loadingStatus = Error;
m_loadingStatus = Loading;
emit loadingStatusChanged();
return;
}
Expand Down Expand Up @@ -423,6 +428,8 @@ void RiveQtQuickItem::loadRiveFile(const QString &source)
ArtBoardInfo info;
info.id = i;
info.name = QString::fromStdString(m_riveFile->artboardNameAt(i));
qCDebug(rqqpInspection) << "ArtBoardInfo" << i << "found.\tName:" << info.name;

m_artboardInfoList.append(info);
}
}
Expand Down Expand Up @@ -483,6 +490,8 @@ void RiveQtQuickItem::updateStateMachines()
info.id = i;
info.name = QString::fromStdString(stateMachine->name());

qCDebug(rqqpInspection) << "StateMachineInfo" << i << "found.\tName:" << info.name;

m_stateMachineList.append(info);
}
}
Expand Down

0 comments on commit 9ede9cd

Please sign in to comment.