From dfcb85dcb8c39b192f31da1a6004ad3bd6349f04 Mon Sep 17 00:00:00 2001 From: Hparty <420024556@qq.com> Date: Thu, 21 Nov 2024 18:49:42 +0800 Subject: [PATCH] Fix the issue where the Layer dirty property sometimes errors out (#336) --- include/tgfx/layers/Layer.h | 1 - src/layers/DisplayList.cpp | 5 +++-- src/layers/Layer.cpp | 7 ------ test/src/LayerTest.cpp | 44 +++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/include/tgfx/layers/Layer.h b/include/tgfx/layers/Layer.h index 8447ed72..082e23ee 100644 --- a/include/tgfx/layers/Layer.h +++ b/include/tgfx/layers/Layer.h @@ -541,7 +541,6 @@ class Layer { std::unique_ptr rasterizedContent = nullptr; std::vector> _children = {}; struct { - bool dirty : 1; // need to redraw the layer bool contentDirty : 1; // need to update content bool childrenDirty : 1; // need to redraw child layers bool visible : 1; diff --git a/src/layers/DisplayList.cpp b/src/layers/DisplayList.cpp index 717f742f..503edee1 100644 --- a/src/layers/DisplayList.cpp +++ b/src/layers/DisplayList.cpp @@ -30,8 +30,9 @@ Layer* DisplayList::root() const { } bool DisplayList::render(Surface* surface, bool replaceAll) { - if (!surface || (replaceAll && surface->_uniqueID == surfaceID && - surface->contentVersion() == surfaceContentVersion && !_root->bitFields.dirty)) { + if (!surface || + (replaceAll && surface->_uniqueID == surfaceID && + surface->contentVersion() == surfaceContentVersion && !_root->bitFields.childrenDirty)) { return false; } auto canvas = surface->getCanvas(); diff --git a/src/layers/Layer.cpp b/src/layers/Layer.cpp index 59b74680..aba1ea8a 100644 --- a/src/layers/Layer.cpp +++ b/src/layers/Layer.cpp @@ -420,10 +420,6 @@ void Layer::draw(Canvas* canvas, float alpha, BlendMode blendMode) { } void Layer::invalidate() { - if (bitFields.dirty) { - return; - } - bitFields.dirty = true; if (_parent) { _parent->invalidateChildren(); } @@ -627,9 +623,6 @@ void Layer::drawLayer(const DrawArgs& args, Canvas* canvas, float alpha, BlendMo // draw directly drawContents(args, canvas, alpha); } - if (args.cleanDirtyFlags) { - bitFields.dirty = false; - } } Matrix Layer::getRelativeMatrix(const Layer* targetCoordinateSpace) const { diff --git a/test/src/LayerTest.cpp b/test/src/LayerTest.cpp index 7b417f44..6f5bc070 100644 --- a/test/src/LayerTest.cpp +++ b/test/src/LayerTest.cpp @@ -1732,4 +1732,48 @@ TGFX_TEST(LayerTest, InnerShadowFilter) { EXPECT_TRUE(Baseline::Compare(surface, "LayerTest/innerShadow")); device->unlock(); } + +TGFX_TEST(LayerTest, DirtyFlag) { + auto device = DevicePool::Make(); + auto displayList = std::make_unique(); + + EXPECT_TRUE(device != nullptr); + auto context = device->lockContext(); + auto surface = Surface::Make(context, 100, 100); + auto child = ImageLayer::Make(); + auto image = MakeImage("resources/apitest/imageReplacement.png"); + EXPECT_TRUE(image != nullptr); + child->setImage(image); + displayList->root()->addChild(child); + + auto grandChild = ImageLayer::Make(); + grandChild->setImage(image); + grandChild->setMatrix(Matrix::MakeTrans(10, 10)); + grandChild->setVisible(false); + child->addChild(grandChild); + + displayList->render(surface.get()); + + auto root = displayList->root(); + EXPECT_TRUE(!grandChild->bitFields.childrenDirty && grandChild->bitFields.contentDirty); + EXPECT_TRUE(!child->bitFields.childrenDirty && !child->bitFields.contentDirty); + EXPECT_TRUE(!root->bitFields.childrenDirty && !root->bitFields.contentDirty); + + grandChild->setVisible(true); + EXPECT_TRUE(!grandChild->bitFields.childrenDirty && grandChild->bitFields.contentDirty); + EXPECT_TRUE(child->bitFields.childrenDirty); + EXPECT_TRUE(root->bitFields.childrenDirty); + displayList->render(surface.get()); + + EXPECT_TRUE(!grandChild->bitFields.childrenDirty && !grandChild->bitFields.contentDirty); + EXPECT_TRUE(!child->bitFields.childrenDirty && !child->bitFields.contentDirty); + EXPECT_TRUE(!root->bitFields.childrenDirty && !root->bitFields.contentDirty); + + child->setVisible(false); + EXPECT_TRUE(!grandChild->bitFields.childrenDirty && !grandChild->bitFields.contentDirty); + EXPECT_TRUE(!child->bitFields.childrenDirty && !child->bitFields.contentDirty); + EXPECT_TRUE(root->bitFields.childrenDirty && !root->bitFields.contentDirty); + + device->unlock(); +} } // namespace tgfx