Skip to content

Commit

Permalink
add surface id
Browse files Browse the repository at this point in the history
  • Loading branch information
Hparty committed Oct 30, 2024
1 parent f191431 commit eb09113
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/tgfx/core/Surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class Surface {
*/
uint32_t contentVersion() const;

uint32_t _uniqueID = 0;
std::shared_ptr<RenderTargetProxy> renderTargetProxy = nullptr;
uint32_t _renderFlags = 0;
RenderContext* renderContext = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions include/tgfx/layers/DisplayList.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class DisplayList {
void render(Surface* surface, bool replaceAll = true);

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

protected:
/**
* Mark the property as dirty and notify the layer that the content needs to be updated.
* Invalidate this property and notify the layer that the content needs to be updated.
*/
void invalidate();

bool dirty = true;
/**
* Called when the property is invalidated.
*/
virtual void onInvalidate();

private:
void attachToLayer(const Layer* layer);
Expand Down
4 changes: 4 additions & 0 deletions include/tgfx/layers/PathProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ class PathProvider : public LayerProperty {

virtual Path onGeneratePath();

void onInvalidate() override;

private:
Path path = {};

bool dirty = true;
};
} // namespace tgfx
4 changes: 4 additions & 0 deletions include/tgfx/layers/filters/LayerFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ class LayerFilter : public LayerProperty {
*/
virtual std::shared_ptr<ImageFilter> onCreateImageFilter(float scale) = 0;

void onInvalidate() override;

private:
bool dirty = true;

float lastScale = 1.0f;

std::unique_ptr<Rect> _clipBounds = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/gpu/Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::shared_ptr<Surface> Surface::MakeFrom(std::shared_ptr<RenderTargetProxy> re
}

Surface::Surface(std::shared_ptr<RenderTargetProxy> proxy, uint32_t renderFlags)
: renderTargetProxy(std::move(proxy)), _renderFlags(renderFlags) {
: _uniqueID(UniqueID::Next()), renderTargetProxy(std::move(proxy)), _renderFlags(renderFlags) {
DEBUG_ASSERT(this->renderTargetProxy != nullptr);
}

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

void DisplayList::render(Surface* surface, bool replaceAll) {
if (!surface || (surface->contentVersion() == surfaceContentVersion && !_root->bitFields.dirty &&
replaceAll)) {
if (!surface || !needRender(surface)) {
return;
}
auto canvas = surface->getCanvas();
Expand All @@ -41,5 +40,12 @@ void DisplayList::render(Surface* surface, bool replaceAll) {
DrawArgs args(surface->getContext(), surface->renderFlags(), true);
_root->drawLayer(args, canvas, 1.0f, BlendMode::SrcOver);
surfaceContentVersion = surface->contentVersion();
surfaceID = surface->_uniqueID;
}

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

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

void LayerProperty::invalidate() {
dirty = true;
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
5 changes: 5 additions & 0 deletions src/layers/PathProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ Path PathProvider::getPath() {
Path PathProvider::onGeneratePath() {
return path;
}

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

} // namespace tgfx
4 changes: 4 additions & 0 deletions src/layers/filters/LayerFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ std::shared_ptr<ImageFilter> LayerFilter::getImageFilter(float filterScale) {
return lastFilter;
}

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

} // namespace tgfx

0 comments on commit eb09113

Please sign in to comment.