Skip to content

Commit

Permalink
Add new invalidate method to LayerProperty's SubClass instead of onIn…
Browse files Browse the repository at this point in the history
…validate
  • Loading branch information
Hparty committed Oct 31, 2024
1 parent c74e06b commit f924f1a
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 33 deletions.
7 changes: 7 additions & 0 deletions include/tgfx/core/Surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ class Surface {
*/
bool readPixels(const ImageInfo& dstInfo, void* dstPixels, int srcX = 0, int srcY = 0);

/**
* Returns the unique ID of the Surface. The ID is unique among all Surfaces.
*/
uint32_t uniqueID() const {
return _uniqueID;
}

private:
/**
* Returns the version of the content. The version is changed when the content is changed.
Expand Down
1 change: 0 additions & 1 deletion include/tgfx/layers/DisplayList.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class DisplayList {
void render(Surface* surface, bool replaceAll = true);

private:
bool needRender(const Surface* surface, bool replaceAll) const;
std::shared_ptr<Layer> _root = nullptr;
uint32_t surfaceContentVersion = 0u;
uint32_t surfaceID = 0u;
Expand Down
8 changes: 2 additions & 6 deletions include/tgfx/layers/LayerProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ class LayerProperty {

protected:
/**
* Invalidate this property and notify the layer that the content needs to be updated.
* Called when the property is invalidated. This method will notify the layer that the content
* of the layer should be invalidated.
*/
void invalidate();

/**
* Called when the property is invalidated.
*/
virtual void onInvalidate();

private:
void attachToLayer(const Layer* layer);

Expand Down
5 changes: 4 additions & 1 deletion include/tgfx/layers/PathProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class PathProvider : public LayerProperty {

virtual Path onGeneratePath();

void onInvalidate() override;
/**
* Marks the path as dirty and invalidates the cached path.
*/
void invalidatePath();

private:
Path path = {};
Expand Down
5 changes: 4 additions & 1 deletion include/tgfx/layers/filters/LayerFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class LayerFilter : public LayerProperty {
*/
virtual std::shared_ptr<ImageFilter> onCreateImageFilter(float scale) = 0;

void onInvalidate() override;
/**
* Marks the filter as dirty and invalidates the cached filter.
*/
void invalidateFilter();

private:
bool dirty = true;
Expand Down
8 changes: 2 additions & 6 deletions src/layers/DisplayList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Layer* DisplayList::root() const {
}

void DisplayList::render(Surface* surface, bool replaceAll) {
if (!surface || !needRender(surface, replaceAll)) {
if (!surface || (replaceAll && surface->_uniqueID == surfaceID &&
surface->contentVersion() == surfaceContentVersion && !_root->bitFields.dirty)) {
return;
}
auto canvas = surface->getCanvas();
Expand All @@ -43,9 +44,4 @@ void DisplayList::render(Surface* surface, bool replaceAll) {
surfaceID = surface->_uniqueID;
}

bool DisplayList::needRender(const Surface* surface, bool replaceAll) const {
return !replaceAll || surface->_uniqueID != surfaceID ||
surface->contentVersion() != surfaceContentVersion || _root->bitFields.dirty;
}

} // namespace tgfx
4 changes: 0 additions & 4 deletions src/layers/LayerProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@
namespace tgfx {

void LayerProperty::invalidate() {
onInvalidate();
for (auto& owner : owners) {
if (auto layer = owner.lock()) {
layer->invalidateContent();
}
}
}

void LayerProperty::onInvalidate() {
}

void LayerProperty::attachToLayer(const Layer* layer) {
owners.push_back(layer->weakThis);
}
Expand Down
3 changes: 2 additions & 1 deletion src/layers/PathProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ Path PathProvider::onGeneratePath() {
return path;
}

void PathProvider::onInvalidate() {
void PathProvider::invalidatePath() {
dirty = true;
invalidate();
}

} // namespace tgfx
4 changes: 2 additions & 2 deletions src/layers/filters/BlendFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ void BlendFilter::setBlendMode(BlendMode mode) {
return;
}
_blendMode = mode;
invalidate();
invalidateFilter();
}

void BlendFilter::setColor(const Color& color) {
if (_color == color) {
return;
}
_color = color;
invalidate();
invalidateFilter();
}

std::shared_ptr<ImageFilter> BlendFilter::onCreateImageFilter(float) {
Expand Down
6 changes: 3 additions & 3 deletions src/layers/filters/BlurFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ void BlurFilter::setBlurrinessX(float blurrinessX) {
return;
}
_blurrinessX = blurrinessX;
invalidate();
invalidateFilter();
}

void BlurFilter::setBlurrinessY(float blurrinessY) {
if (_blurrinessY == blurrinessY) {
return;
}
_blurrinessY = blurrinessY;
invalidate();
invalidateFilter();
}

void BlurFilter::setTileMode(TileMode tileMode) {
if (_tileMode == tileMode) {
return;
}
_tileMode = tileMode;
invalidate();
invalidateFilter();
}

BlurFilter::BlurFilter(float blurrinessX, float blurrinessY, TileMode tileMode)
Expand Down
2 changes: 1 addition & 1 deletion src/layers/filters/ColorMatrixFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void ColorMatrixFilter::setMatrix(const std::array<float, 20>& matrix) {
return;
}
_matrix = std::move(matrix);
invalidate();
invalidateFilter();
}

std::shared_ptr<ImageFilter> ColorMatrixFilter::onCreateImageFilter(float) {
Expand Down
12 changes: 6 additions & 6 deletions src/layers/filters/DropShadowFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,47 @@ void DropShadowFilter::setOffsetX(float offsetX) {
return;
}
_offsetX = offsetX;
invalidate();
invalidateFilter();
}

void DropShadowFilter::setOffsetY(float offsetY) {
if (_offsetY == offsetY) {
return;
}
_offsetY = offsetY;
invalidate();
invalidateFilter();
}

void DropShadowFilter::setBlurrinessX(float blurrinessX) {
if (_blurrinessX == blurrinessX) {
return;
}
_blurrinessX = blurrinessX;
invalidate();
invalidateFilter();
}

void DropShadowFilter::setBlurrinessY(float blurrinessY) {
if (_blurrinessY == blurrinessY) {
return;
}
_blurrinessY = blurrinessY;
invalidate();
invalidateFilter();
}

void DropShadowFilter::setColor(const Color& color) {
if (_color == color) {
return;
}
_color = color;
invalidate();
invalidateFilter();
}

void DropShadowFilter::setDropsShadowOnly(bool value) {
if (_dropsShadowOnly == value) {
return;
}
_dropsShadowOnly = value;
invalidate();
invalidateFilter();
}

std::shared_ptr<ImageFilter> DropShadowFilter::onCreateImageFilter(float scale) {
Expand Down
3 changes: 2 additions & 1 deletion src/layers/filters/LayerFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ std::shared_ptr<ImageFilter> LayerFilter::getImageFilter(float filterScale) {
return lastFilter;
}

void LayerFilter::onInvalidate() {
void LayerFilter::invalidateFilter() {
dirty = true;
invalidate();
}

} // namespace tgfx
8 changes: 8 additions & 0 deletions test/src/LayerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,14 @@ TGFX_TEST(LayerTest, ContentVersion) {
contentVersion = surface->contentVersion();
displayList.render(surface.get());
EXPECT_NE(surface->contentVersion(), contentVersion);
contentVersion = surface->contentVersion();

auto surface2 = Surface::Make(context, 100, 100);
EXPECT_EQ(surface2->contentVersion(), 1u);
displayList.render(surface2.get());
EXPECT_NE(surface2->contentVersion(), 1u);
displayList.render(surface.get());
EXPECT_NE(surface->contentVersion(), contentVersion);
device->unlock();
}

Expand Down

0 comments on commit f924f1a

Please sign in to comment.