Skip to content

Commit

Permalink
Fix the issue where the Layer dirty property sometimes errors out
Browse files Browse the repository at this point in the history
  • Loading branch information
Hparty committed Nov 21, 2024
1 parent d77d3a9 commit 9cbc644
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
1 change: 0 additions & 1 deletion include/tgfx/layers/Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,6 @@ class Layer {
std::unique_ptr<LayerContent> rasterizedContent = nullptr;
std::vector<std::shared_ptr<Layer>> _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;
Expand Down
5 changes: 3 additions & 2 deletions src/layers/DisplayList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 0 additions & 7 deletions src/layers/Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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 {
Expand Down
44 changes: 44 additions & 0 deletions test/src/LayerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DisplayList>();

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

0 comments on commit 9cbc644

Please sign in to comment.