Refactoring

This commit is contained in:
Isaac 2024-05-19 12:27:24 +04:00
parent 62d58b3bdd
commit 1f1509a64c
25 changed files with 522 additions and 349 deletions

View file

@ -81,7 +81,7 @@ public:
virtual void setAlpha(float alpha) = 0;
virtual void concatenate(lottie::Transform3D const &transform) = 0;
virtual void concatenate(lottie::Transform2D const &transform) = 0;
virtual void draw(std::shared_ptr<Canvas> const &other, lottie::CGRect const &rect) = 0;
};

View file

@ -40,7 +40,7 @@ public:
virtual void fill(lottie::CGRect const &rect, lottie::Color const &fillColor) override;
virtual void setBlendMode(BlendMode blendMode) override;
virtual void setAlpha(float alpha) override;
virtual void concatenate(lottie::Transform3D const &transform) override;
virtual void concatenate(lottie::Transform2D const &transform) override;
virtual std::shared_ptr<Image> makeImage() const;
virtual void draw(std::shared_ptr<Canvas> const &other, lottie::CGRect const &rect) override;

View file

@ -509,7 +509,7 @@ void CanvasImpl::setAlpha(float alpha) {
CGContextSetAlpha(_context, alpha);
}
void CanvasImpl::concatenate(lottie::Transform3D const &transform) {
void CanvasImpl::concatenate(lottie::Transform2D const &transform) {
CGContextConcatCTM(_context, CATransform3DGetAffineTransform(nativeTransform(transform)));
}

View file

@ -30,7 +30,7 @@ public:
virtual void setAlpha(float alpha) override;
virtual void concatenate(lottie::Transform3D const &transform) override;
virtual void concatenate(lottie::Transform2D const &transform) override;
virtual void draw(std::shared_ptr<Canvas> const &other, lottie::CGRect const &rect) override;
@ -39,7 +39,7 @@ public:
private:
float _width = 0.0f;
float _height = 0.0f;
lottie::Transform3D _transform;
lottie::Transform2D _transform;
};
}

View file

@ -12,7 +12,7 @@ void addEnumeratedPath(CanvasPathEnumerator const &enumeratePath) {
}
NullCanvasImpl::NullCanvasImpl(int width, int height) :
_width(width), _height(height), _transform(lottie::Transform3D::identity()) {
_width(width), _height(height), _transform(lottie::Transform2D::identity()) {
}
NullCanvasImpl::~NullCanvasImpl() {
@ -69,7 +69,7 @@ void NullCanvasImpl::setBlendMode(BlendMode blendMode) {
void NullCanvasImpl::setAlpha(float alpha) {
}
void NullCanvasImpl::concatenate(lottie::Transform3D const &transform) {
void NullCanvasImpl::concatenate(lottie::Transform2D const &transform) {
}
void NullCanvasImpl::draw(std::shared_ptr<Canvas> const &other, lottie::CGRect const &rect) {

View file

@ -15,17 +15,17 @@ static constexpr float minGlobalRectCalculationSize = 200.0f;
struct TransformedPath {
lottie::BezierPath path;
lottie::Transform3D transform;
lottie::Transform2D transform;
TransformedPath(lottie::BezierPath const &path_, lottie::Transform3D const &transform_) :
TransformedPath(lottie::BezierPath const &path_, lottie::Transform2D const &transform_) :
path(path_),
transform(transform_) {
}
};
static lottie::CGRect collectPathBoundingBoxes(std::shared_ptr<lottie::RenderTreeNodeContentItem> item, size_t subItemLimit, lottie::Transform3D const &parentTransform, bool skipApplyTransform, lottie::BezierPathsBoundingBoxContext &bezierPathsBoundingBoxContext) {
static lottie::CGRect collectPathBoundingBoxes(std::shared_ptr<lottie::RenderTreeNodeContentItem> item, size_t subItemLimit, lottie::Transform2D const &parentTransform, bool skipApplyTransform, lottie::BezierPathsBoundingBoxContext &bezierPathsBoundingBoxContext) {
//TODO:remove skipApplyTransform
lottie::Transform3D effectiveTransform = parentTransform;
lottie::Transform2D effectiveTransform = parentTransform;
if (!skipApplyTransform && item->isGroup) {
effectiveTransform = item->transform * effectiveTransform;
}
@ -56,9 +56,9 @@ static lottie::CGRect collectPathBoundingBoxes(std::shared_ptr<lottie::RenderTre
return boundingBox;
}
static void enumeratePaths(std::shared_ptr<lottie::RenderTreeNodeContentItem> item, size_t subItemLimit, lottie::Transform3D const &parentTransform, bool skipApplyTransform, std::function<void(lottie::BezierPath const &path, lottie::Transform3D const &transform)> const &onPath) {
static void enumeratePaths(std::shared_ptr<lottie::RenderTreeNodeContentItem> item, size_t subItemLimit, lottie::Transform2D const &parentTransform, bool skipApplyTransform, std::function<void(lottie::BezierPath const &path, lottie::Transform2D const &transform)> const &onPath) {
//TODO:remove skipApplyTransform
lottie::Transform3D effectiveTransform = parentTransform;
lottie::Transform2D effectiveTransform = parentTransform;
if (!skipApplyTransform && item->isGroup) {
effectiveTransform = item->transform * effectiveTransform;
}
@ -80,14 +80,14 @@ static void enumeratePaths(std::shared_ptr<lottie::RenderTreeNodeContentItem> it
namespace lottie {
static std::optional<CGRect> getRenderContentItemGlobalRect(std::shared_ptr<RenderTreeNodeContentItem> const &contentItem, lottie::Vector2D const &globalSize, lottie::Transform3D const &parentTransform, BezierPathsBoundingBoxContext &bezierPathsBoundingBoxContext) {
static std::optional<CGRect> getRenderContentItemGlobalRect(std::shared_ptr<RenderTreeNodeContentItem> const &contentItem, lottie::Vector2D const &globalSize, lottie::Transform2D const &parentTransform, BezierPathsBoundingBoxContext &bezierPathsBoundingBoxContext) {
auto currentTransform = parentTransform;
Transform3D localTransform = contentItem->transform;
Transform2D localTransform = contentItem->transform;
currentTransform = localTransform * currentTransform;
std::optional<CGRect> globalRect;
for (const auto &shadingVariant : contentItem->shadings) {
lottie::CGRect shapeBounds = collectPathBoundingBoxes(contentItem, shadingVariant->subItemLimit, lottie::Transform3D::identity(), true, bezierPathsBoundingBoxContext);
lottie::CGRect shapeBounds = collectPathBoundingBoxes(contentItem, shadingVariant->subItemLimit, lottie::Transform2D::identity(), true, bezierPathsBoundingBoxContext);
if (shadingVariant->stroke) {
shapeBounds = shapeBounds.insetBy(-shadingVariant->stroke->lineWidth / 2.0, -shadingVariant->stroke->lineWidth / 2.0);
@ -128,13 +128,13 @@ static std::optional<CGRect> getRenderContentItemGlobalRect(std::shared_ptr<Rend
}
}
static std::optional<CGRect> getRenderNodeGlobalRect(std::shared_ptr<RenderTreeNode> const &node, lottie::Vector2D const &globalSize, lottie::Transform3D const &parentTransform, bool isInvertedMatte, BezierPathsBoundingBoxContext &bezierPathsBoundingBoxContext) {
static std::optional<CGRect> getRenderNodeGlobalRect(std::shared_ptr<RenderTreeNode> const &node, lottie::Vector2D const &globalSize, lottie::Transform2D const &parentTransform, bool isInvertedMatte, BezierPathsBoundingBoxContext &bezierPathsBoundingBoxContext) {
if (node->isHidden() || node->alpha() < minVisibleAlpha) {
return std::nullopt;
}
auto currentTransform = parentTransform;
Transform3D localTransform = node->transform();
Transform2D localTransform = node->transform();
currentTransform = localTransform * currentTransform;
std::optional<CGRect> globalRect;
@ -179,9 +179,9 @@ static std::optional<CGRect> getRenderNodeGlobalRect(std::shared_ptr<RenderTreeN
namespace {
static void drawLottieContentItem(std::shared_ptr<lottieRendering::Canvas> const &parentContext, std::shared_ptr<lottie::RenderTreeNodeContentItem> item, float parentAlpha, lottie::Vector2D const &globalSize, lottie::Transform3D const &parentTransform, lottie::BezierPathsBoundingBoxContext &bezierPathsBoundingBoxContext) {
static void drawLottieContentItem(std::shared_ptr<lottieRendering::Canvas> const &parentContext, std::shared_ptr<lottie::RenderTreeNodeContentItem> item, float parentAlpha, lottie::Vector2D const &globalSize, lottie::Transform2D const &parentTransform, lottie::BezierPathsBoundingBoxContext &bezierPathsBoundingBoxContext) {
auto currentTransform = parentTransform;
lottie::Transform3D localTransform = item->transform;
lottie::Transform2D localTransform = item->transform;
currentTransform = localTransform * currentTransform;
float normalizedOpacity = item->alpha;
@ -215,7 +215,7 @@ static void drawLottieContentItem(std::shared_ptr<lottieRendering::Canvas> const
tempContext = tempContextValue;
currentContext = &tempContext;
(*currentContext)->concatenate(lottie::Transform3D::identity().translated(lottie::Vector2D(-globalRect->x, -globalRect->y)));
(*currentContext)->concatenate(lottie::Transform2D::identity().translated(lottie::Vector2D(-globalRect->x, -globalRect->y)));
(*currentContext)->saveState();
(*currentContext)->concatenate(currentTransform);
@ -268,7 +268,7 @@ static void drawLottieContentItem(std::shared_ptr<lottieRendering::Canvas> const
};
} else {
iteratePaths = [&](std::function<void(lottieRendering::PathCommand const &)> iterate) {
enumeratePaths(item, shading->subItemLimit, lottie::Transform3D::identity(), true, [&](lottie::BezierPath const &sourcePath, lottie::Transform3D const &transform) {
enumeratePaths(item, shading->subItemLimit, lottie::Transform2D::identity(), true, [&](lottie::BezierPath const &sourcePath, lottie::Transform2D const &transform) {
auto path = sourcePath.copyUsingTransform(transform);
lottieRendering::PathCommand pathCommand;
@ -456,7 +456,7 @@ static void drawLottieContentItem(std::shared_ptr<lottieRendering::Canvas> const
parentContext->restoreState();
}
static void renderLottieRenderNode(std::shared_ptr<lottie::RenderTreeNode> node, std::shared_ptr<lottieRendering::Canvas> const &parentContext, lottie::Vector2D const &globalSize, lottie::Transform3D const &parentTransform, float parentAlpha, bool isInvertedMatte, lottie::BezierPathsBoundingBoxContext &bezierPathsBoundingBoxContext) {
static void renderLottieRenderNode(std::shared_ptr<lottie::RenderTreeNode> node, std::shared_ptr<lottieRendering::Canvas> const &parentContext, lottie::Vector2D const &globalSize, lottie::Transform2D const &parentTransform, float parentAlpha, bool isInvertedMatte, lottie::BezierPathsBoundingBoxContext &bezierPathsBoundingBoxContext) {
float normalizedOpacity = node->alpha();
float layerAlpha = ((float)normalizedOpacity) * parentAlpha;
@ -465,7 +465,7 @@ static void renderLottieRenderNode(std::shared_ptr<lottie::RenderTreeNode> node,
}
auto currentTransform = parentTransform;
lottie::Transform3D localTransform = node->transform();
lottie::Transform2D localTransform = node->transform();
currentTransform = localTransform * currentTransform;
std::shared_ptr<lottieRendering::Canvas> maskContext;
@ -507,7 +507,7 @@ static void renderLottieRenderNode(std::shared_ptr<lottie::RenderTreeNode> node,
if ((node->mask() && !node->mask()->isHidden() && node->mask()->alpha() >= minVisibleAlpha) || masksToBounds) {
auto maskBackingStorage = parentContext->makeLayer((int)(globalRect->width), (int)(globalRect->height));
maskBackingStorage->concatenate(lottie::Transform3D::identity().translated(lottie::Vector2D(-globalRect->x, -globalRect->y)));
maskBackingStorage->concatenate(lottie::Transform2D::identity().translated(lottie::Vector2D(-globalRect->x, -globalRect->y)));
maskBackingStorage->concatenate(currentTransform);
if (masksToBounds) {
@ -524,7 +524,7 @@ static void renderLottieRenderNode(std::shared_ptr<lottie::RenderTreeNode> node,
tempContext = tempContextValue;
currentContext = tempContextValue;
currentContext->concatenate(lottie::Transform3D::identity().translated(lottie::Vector2D(-globalRect->x, -globalRect->y)));
currentContext->concatenate(lottie::Transform2D::identity().translated(lottie::Vector2D(-globalRect->x, -globalRect->y)));
currentContext->saveState();
currentContext->concatenate(currentTransform);
@ -602,13 +602,13 @@ CGRect getPathNativeBoundingBox(CGPathRef _Nonnull path) {
return nil;
}
lottie::Transform3D rootTransform = lottie::Transform3D::identity().scaled(lottie::Vector2D(size.width / (float)animation.size.width, size.height / (float)animation.size.height));
lottie::Transform2D rootTransform = lottie::Transform2D::identity().scaled(lottie::Vector2D(size.width / (float)animation.size.width, size.height / (float)animation.size.height));
if (useReferenceRendering) {
auto context = std::make_shared<lottieRendering::CanvasImpl>((int)size.width, (int)size.height);
CGPoint scale = CGPointMake(size.width / (CGFloat)animation.size.width, size.height / (CGFloat)animation.size.height);
context->concatenate(lottie::Transform3D::makeScale(scale.x, scale.y, 1.0));
context->concatenate(lottie::Transform2D::makeScale(scale.x, scale.y));
renderLottieRenderNode(renderNode, context, lottie::Vector2D(context->width(), context->height()), rootTransform, 1.0, false, *_bezierPathsBoundingBoxContext.get());
@ -620,7 +620,7 @@ CGRect getPathNativeBoundingBox(CGPathRef _Nonnull path) {
auto context = std::make_shared<lottieRendering::NullCanvasImpl>((int)size.width, (int)size.height);
CGPoint scale = CGPointMake(size.width / (CGFloat)animation.size.width, size.height / (CGFloat)animation.size.height);
context->concatenate(lottie::Transform3D::makeScale(scale.x, scale.y, 1.0));
context->concatenate(lottie::Transform2D::makeScale(scale.x, scale.y));
renderLottieRenderNode(renderNode, context, lottie::Vector2D(context->width(), context->height()), rootTransform, 1.0, false, *_bezierPathsBoundingBoxContext.get());

View file

@ -32,7 +32,7 @@ public:
virtual void setAlpha(float alpha) override;
virtual void concatenate(lottie::Transform3D const &transform) override;
virtual void concatenate(lottie::Transform2D const &transform) override;
virtual void draw(std::shared_ptr<Canvas> const &other, lottie::CGRect const &rect) override;
@ -52,8 +52,8 @@ private:
std::unique_ptr<tvg::SwCanvas> _canvas;
float _alpha = 1.0;
lottie::Transform3D _transform;
std::vector<lottie::Transform3D> _stateStack;
lottie::Transform2D _transform;
std::vector<lottie::Transform2D> _stateStack;
int _bytesPerRow = 0;
uint32_t *_backingData = nullptr;
int _statsNumStrokes = 0;

View file

@ -27,7 +27,7 @@ void tvgPath(CanvasPathEnumerator const &enumeratePath, tvg::Shape *shape) {
});
}
tvg::Matrix tvgTransform(lottie::Transform3D const &transform) {
tvg::Matrix tvgTransform(lottie::Transform2D const &transform) {
CGAffineTransform affineTransform = CATransform3DGetAffineTransform(lottie::nativeTransform(transform));
tvg::Matrix result;
result.e11 = affineTransform.a;
@ -45,7 +45,7 @@ tvg::Matrix tvgTransform(lottie::Transform3D const &transform) {
}
ThorVGCanvasImpl::ThorVGCanvasImpl(int width, int height) :
_width(width), _height(height), _transform(lottie::Transform3D::identity()) {
_width(width), _height(height), _transform(lottie::Transform2D::identity()) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
tvg::Initializer::init(0);
@ -259,7 +259,7 @@ void ThorVGCanvasImpl::setAlpha(float alpha) {
_alpha = alpha;
}
void ThorVGCanvasImpl::concatenate(lottie::Transform3D const &transform) {
void ThorVGCanvasImpl::concatenate(lottie::Transform2D const &transform) {
_transform = transform * _transform;
/*_canvas->concat(SkM44(
transform.m11, transform.m21, transform.m31, transform.m41,