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

Define the PathProvider interface to create Shape objects for deferred construction of Path objects. #404

Merged
merged 14 commits into from
Dec 26, 2024

Conversation

shlzxjp
Copy link
Collaborator

@shlzxjp shlzxjp commented Dec 26, 2024

定义PathProvider接口,通过PathProvider创建Shape,用于延迟构建Path对象。

* Note: This method may be called multiple times. If constructing the Path object is
* time-consuming, consider caching the Path object after it is created.
*/
virtual Path getPath() = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方法需要标记const,确保调用的时候没有写入操作才能保证线程安全。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方法也要增加一个resolutionScale精度参数,传递下去,以防外部生成Path的过程是需要使用Stroke::applyToPath(Path* path, float resolutionScale = 1.0f)的。


Path ExternalShape::getPath(float resolutionScale) const {
auto finalPath = getPathInternal();
finalPath.transform(Matrix::MakeScale(resolutionScale));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个参数的含义理解错了,看一下注释。它不改变Path的大小。这是个精度缩放值。在Stroke相关的操作里才会用到。

Comment on lines 75 to 79
if (path.isEmpty()) {
const auto tmpPath = const_cast<Path*>(&path);
*tmpPath = provider->getPath();
}
return path;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

标记了const的方法又进行数据写入,会导致线程安全问题。我们的Shape::getPath()方法已经加过注释了,每次调用都会生成一个新的Path,并不缓存。去掉这块的特殊逻辑。

}

UniqueKey ExternalShape::getUniqueKey() const {
return PathRef::GetUniqueKey(getPathInternal());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里直接像EffectShape一样,定义一个自身独立的UniqueKey,不能从Path上获取,整个Shape对象都是immutable的才能保证线程安全,Path不能缓存

Comment on lines 59 to 61
auto finalPath = getPathInternal();
finalPath.transform(Matrix::MakeScale(resolutionScale));
return finalPath.getBounds();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该给PathProvider定义一个getBounds方法。避免要生成Path才能计算bounds,这就违背了延迟计算的初衷了。getBounds是没有渲染的时候就要调用的。

#pragma once

namespace tgfx {
class ExternalShape : public Shape {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

重命名为ProviderShape

Comment on lines 34 to 35
* Note: This method may be called multiple times. If constructing the Path object is
* time-consuming, consider caching the Path object after it is created.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不能缓存

Update comment
Remove default parameter values for getPath and getBounds methods.
@domchen domchen merged commit 643d386 into main Dec 26, 2024
8 checks passed
@domchen domchen deleted the feature/xjp_path_provider branch December 26, 2024 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants