-
Notifications
You must be signed in to change notification settings - Fork 72
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
Conversation
include/tgfx/core/PathProvider.h
Outdated
* 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个方法需要标记const,确保调用的时候没有写入操作才能保证线程安全。
There was a problem hiding this comment.
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)的。
src/core/shapes/ExternalShape.cpp
Outdated
|
||
Path ExternalShape::getPath(float resolutionScale) const { | ||
auto finalPath = getPathInternal(); | ||
finalPath.transform(Matrix::MakeScale(resolutionScale)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个参数的含义理解错了,看一下注释。它不改变Path的大小。这是个精度缩放值。在Stroke相关的操作里才会用到。
src/core/shapes/ExternalShape.cpp
Outdated
if (path.isEmpty()) { | ||
const auto tmpPath = const_cast<Path*>(&path); | ||
*tmpPath = provider->getPath(); | ||
} | ||
return path; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
标记了const的方法又进行数据写入,会导致线程安全问题。我们的Shape::getPath()方法已经加过注释了,每次调用都会生成一个新的Path,并不缓存。去掉这块的特殊逻辑。
src/core/shapes/ExternalShape.cpp
Outdated
} | ||
|
||
UniqueKey ExternalShape::getUniqueKey() const { | ||
return PathRef::GetUniqueKey(getPathInternal()); |
There was a problem hiding this comment.
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不能缓存
src/core/shapes/ExternalShape.cpp
Outdated
auto finalPath = getPathInternal(); | ||
finalPath.transform(Matrix::MakeScale(resolutionScale)); | ||
return finalPath.getBounds(); |
There was a problem hiding this comment.
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是没有渲染的时候就要调用的。
src/core/shapes/ExternalShape.h
Outdated
#pragma once | ||
|
||
namespace tgfx { | ||
class ExternalShape : public Shape { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
重命名为ProviderShape
include/tgfx/core/PathProvider.h
Outdated
* 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. |
There was a problem hiding this comment.
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.
定义PathProvider接口,通过PathProvider创建Shape,用于延迟构建Path对象。