Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bounds cache for the Path class. #161

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions src/core/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,7 @@ bool Path::isLine(Point line[2]) const {
}

Rect Path::getBounds() const {
// Internally, SkPath lazily computes bounds. Use this function instead of path.getBounds()
// for thread safety.
const auto& path = pathRef->path;
auto count = path.countPoints();
auto points = new SkPoint[static_cast<size_t>(count)];
path.getPoints(points, count);
auto rect = SkRect::MakeEmpty();
rect.setBounds(points, count);
delete[] points;
return {rect.fLeft, rect.fTop, rect.fRight, rect.fBottom};
return pathRef->getBounds();
}

bool Path::isEmpty() const {
Expand Down Expand Up @@ -308,14 +299,14 @@ void Path::addRoundRect(const Rect& rect, float radiusX, float radiusY, bool rev
}

void Path::addPath(const Path& src, PathOp op) {
if (isEmpty()) {
*this = src;
return;
}
auto& path = writableRef()->path;
const auto& newPath = src.pathRef->path;
if (op == PathOp::Append) {
if (path.isEmpty()) {
path = newPath;
} else {
path.addPath(newPath);
}
path.addPath(newPath);
return;
}
SkPathOp pathOp;
Expand Down Expand Up @@ -401,6 +392,7 @@ PathRef* Path::writableRef() {
pathRef = std::make_shared<PathRef>(pathRef->path);
} else {
pathRef->uniqueKey.reset();
pathRef->resetBounds();
}
return pathRef.get();
}
Expand Down
30 changes: 30 additions & 0 deletions src/core/PathRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,34 @@ SkPath& PathRef::WriteAccess(Path& path) {
UniqueKey PathRef::GetUniqueKey(const Path& path) {
return path.pathRef->uniqueKey.get();
}

PathRef::~PathRef() {
resetBounds();
}

Rect PathRef::getBounds() {
auto cacheBounds = bounds.load(std::memory_order_acquire);
if (cacheBounds == nullptr) {
// Internally, SkPath lazily computes bounds. Use this function instead of path.getBounds()
// for thread safety.
auto count = path.countPoints();
auto points = new SkPoint[static_cast<size_t>(count)];
path.getPoints(points, count);
auto rect = SkRect::MakeEmpty();
rect.setBounds(points, count);
delete[] points;
auto newBounds = new Rect{rect.fLeft, rect.fTop, rect.fRight, rect.fBottom};
if (bounds.compare_exchange_strong(cacheBounds, newBounds, std::memory_order_acq_rel)) {
cacheBounds = newBounds;
} else {
delete newBounds;
}
}
return *cacheBounds;
}

void PathRef::resetBounds() {
auto oldBounds = bounds.exchange(nullptr, std::memory_order_acq_rel);
delete oldBounds;
}
} // namespace tgfx
7 changes: 7 additions & 0 deletions src/core/PathRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@ class PathRef {
explicit PathRef(const pk::SkPath& path) : path(path) {
}

~PathRef();

Rect getBounds();

private:
LazyUniqueKey uniqueKey = {};
std::atomic<Rect*> bounds = {nullptr};
pk::SkPath path = {};

void resetBounds();

friend bool operator==(const Path& a, const Path& b);
friend bool operator!=(const Path& a, const Path& b);
friend class Path;
Expand Down
Loading