diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2021-01-18 22:29:34 +0100 |
---|---|---|
committer | bstoeger <32835590+bstoeger@users.noreply.github.com> | 2021-01-20 08:47:18 +0100 |
commit | db69c38245cc260d19e189716e73dbbbedd56273 (patch) | |
tree | 67c08a8e929ab69f5f23b769cd25dc6c280e87cc /stats/chartitem.cpp | |
parent | 9d3de1801e8d14f5143f95042f3b842551d4c4cd (diff) | |
download | subsurface-db69c38245cc260d19e189716e73dbbbedd56273.tar.gz |
statistics: refactor QSG memory management
The code was wrong, because it deleted the ChartItems in the
main UI thread, not the render thread. This would delete the
QSG nodes in the UI thread and then crash on mobile.
Therefore refactor this part of the code by adding the
items to be deleted to a list that will be deleted by the
render thread.
As a drop in replacement of std::unique_ptr, implement
a silly ChartItemPtr class, which auto-initializes to null.
This turns the deterministic and easily controlled memory
management into a steaming pile of insanity. Obviously,
this can be made much more elegant, but this has to do for now.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'stats/chartitem.cpp')
-rw-r--r-- | stats/chartitem.cpp | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/stats/chartitem.cpp b/stats/chartitem.cpp index be2b3d635..6a69b6973 100644 --- a/stats/chartitem.cpp +++ b/stats/chartitem.cpp @@ -16,15 +16,16 @@ static int round_up(double f) } ChartItem::ChartItem(StatsView &v, ChartZValue z) : - dirty(false), dirtyPrev(nullptr), dirtyNext(nullptr), + dirty(false), prev(nullptr), next(nullptr), zValue(z), view(v) { + // Register before the derived constructors run, so that the + // derived classes can mark the item as dirty in the constructor. + v.registerChartItem(*this); } ChartItem::~ChartItem() { - if (dirty) - view.unregisterDirtyChartItem(*this); } QSizeF ChartItem::sceneSize() const @@ -32,6 +33,11 @@ QSizeF ChartItem::sceneSize() const return view.size(); } +void ChartItem::markDirty() +{ + view.registerDirtyChartItem(*this); +} + ChartPixmapItem::ChartPixmapItem(StatsView &v, ChartZValue z) : HideableChartItem(v, z), positionDirty(false), textureDirty(false) { @@ -45,13 +51,13 @@ ChartPixmapItem::~ChartPixmapItem() void ChartPixmapItem::setTextureDirty() { textureDirty = true; - view.registerDirtyChartItem(*this); + markDirty(); } void ChartPixmapItem::setPositionDirty() { positionDirty = true; - view.registerDirtyChartItem(*this); + markDirty(); } void ChartPixmapItem::render() @@ -60,6 +66,8 @@ void ChartPixmapItem::render() createNode(view.w()->createImageNode()); view.addQSGNode(node.get(), zValue); } + updateVisible(); + if (!img) { resize(QSizeF(1,1)); img->fill(Qt::transparent); @@ -141,6 +149,7 @@ void ChartScatterItem::render() view.addQSGNode(node.get(), zValue); textureDirty = positionDirty = true; } + updateVisible(); if (textureDirty) { node->node->setTexture(highlighted ? scatterItemHighlightedTexture.get() : scatterItemTexture.get()); textureDirty = false; @@ -156,7 +165,7 @@ void ChartScatterItem::setPos(QPointF pos) pos -= QPointF(scatterItemDiameter / 2.0, scatterItemDiameter / 2.0); rect.moveTopLeft(pos); positionDirty = true; - view.registerDirtyChartItem(*this); + markDirty(); } static double squareDist(const QPointF &p1, const QPointF &p2) @@ -176,7 +185,7 @@ void ChartScatterItem::setHighlight(bool highlightedIn) return; highlighted = highlightedIn; textureDirty = true; - view.registerDirtyChartItem(*this); + markDirty(); } QRectF ChartScatterItem::getRect() const @@ -297,6 +306,7 @@ void ChartLineItem::render() view.addQSGNode(node.get(), zValue); positionDirty = materialDirty = true; } + updateVisible(); if (positionDirty) { // Attention: width is a geometry property and therefore handled by position dirty! @@ -320,7 +330,7 @@ void ChartLineItem::setLine(QPointF fromIn, QPointF toIn) from = fromIn; to = toIn; positionDirty = true; - view.registerDirtyChartItem(*this); + markDirty(); } ChartBarItem::ChartBarItem(StatsView &v, ChartZValue z, double borderWidth, bool horizontal) : HideableChartItem(v, z), @@ -350,6 +360,7 @@ void ChartBarItem::render() view.addQSGNode(node.get(), zValue); positionDirty = colorDirty = true; } + updateVisible(); if (colorDirty) { node->node->setColor(color); @@ -384,14 +395,14 @@ void ChartBarItem::setColor(QColor colorIn, QColor borderColorIn) color = colorIn; borderColor = borderColorIn; colorDirty = true; - view.registerDirtyChartItem(*this); + markDirty(); } void ChartBarItem::setRect(const QRectF &rectIn) { rect = rectIn; positionDirty = true; - view.registerDirtyChartItem(*this); + markDirty(); } QRectF ChartBarItem::getRect() const |