Refactoring

This commit is contained in:
Isaac 2024-05-19 02:36:08 +04:00
parent 59d8d311d4
commit 62d58b3bdd
9 changed files with 51 additions and 121 deletions

View File

@ -134,9 +134,7 @@ static std::optional<CGRect> getRenderNodeGlobalRect(std::shared_ptr<RenderTreeN
} }
auto currentTransform = parentTransform; auto currentTransform = parentTransform;
Vector2D localTranslation(node->position().x + -node->bounds().x, node->position().y + -node->bounds().y);
Transform3D localTransform = node->transform(); Transform3D localTransform = node->transform();
localTransform = localTransform.translated(localTranslation);
currentTransform = localTransform * currentTransform; currentTransform = localTransform * currentTransform;
std::optional<CGRect> globalRect; std::optional<CGRect> globalRect;
@ -145,7 +143,7 @@ static std::optional<CGRect> getRenderNodeGlobalRect(std::shared_ptr<RenderTreeN
} }
if (isInvertedMatte) { if (isInvertedMatte) {
CGRect globalBounds = node->bounds().applyingTransform(currentTransform); CGRect globalBounds = CGRect(0.0f, 0.0f, node->size().x, node->size().y).applyingTransform(currentTransform);
if (globalRect) { if (globalRect) {
globalRect = globalRect->unionWith(globalBounds); globalRect = globalRect->unionWith(globalBounds);
} else { } else {
@ -467,9 +465,7 @@ static void renderLottieRenderNode(std::shared_ptr<lottie::RenderTreeNode> node,
} }
auto currentTransform = parentTransform; auto currentTransform = parentTransform;
lottie::Vector2D localTranslation(node->position().x + -node->bounds().x, node->position().y + -node->bounds().y);
lottie::Transform3D localTransform = node->transform(); lottie::Transform3D localTransform = node->transform();
localTransform = localTransform.translated(localTranslation);
currentTransform = localTransform * currentTransform; currentTransform = localTransform * currentTransform;
std::shared_ptr<lottieRendering::Canvas> maskContext; std::shared_ptr<lottieRendering::Canvas> maskContext;
@ -478,7 +474,7 @@ static void renderLottieRenderNode(std::shared_ptr<lottie::RenderTreeNode> node,
bool masksToBounds = node->masksToBounds(); bool masksToBounds = node->masksToBounds();
if (masksToBounds) { if (masksToBounds) {
lottie::CGRect effectiveGlobalBounds = node->bounds().applyingTransform(currentTransform); lottie::CGRect effectiveGlobalBounds = lottie::CGRect(0.0f, 0.0f, node->size().x, node->size().y).applyingTransform(currentTransform);
if (effectiveGlobalBounds.width <= 0.0f || effectiveGlobalBounds.height <= 0.0f) { if (effectiveGlobalBounds.width <= 0.0f || effectiveGlobalBounds.height <= 0.0f) {
return; return;
} }
@ -515,7 +511,7 @@ static void renderLottieRenderNode(std::shared_ptr<lottie::RenderTreeNode> node,
maskBackingStorage->concatenate(currentTransform); maskBackingStorage->concatenate(currentTransform);
if (masksToBounds) { if (masksToBounds) {
maskBackingStorage->fill(lottie::CGRect(node->bounds().x, node->bounds().y, node->bounds().width, node->bounds().height), lottie::Color(1.0, 1.0, 1.0, 1.0)); maskBackingStorage->fill(lottie::CGRect(0.0f, 0.0f, node->size().x, node->size().y), lottie::Color(1.0f, 1.0f, 1.0f, 1.0f));
} }
if (node->mask() && !node->mask()->isHidden() && node->mask()->alpha() >= minVisibleAlpha) { if (node->mask() && !node->mask()->isHidden() && node->mask()->alpha() >= minVisibleAlpha) {
renderLottieRenderNode(node->mask(), maskBackingStorage, globalSize, currentTransform, 1.0, node->invertMask(), bezierPathsBoundingBoxContext); renderLottieRenderNode(node->mask(), maskBackingStorage, globalSize, currentTransform, 1.0, node->invertMask(), bezierPathsBoundingBoxContext);
@ -536,13 +532,11 @@ static void renderLottieRenderNode(std::shared_ptr<lottie::RenderTreeNode> node,
currentContext = parentContext; currentContext = parentContext;
} }
parentContext->concatenate(lottie::Transform3D::identity().translated(lottie::Vector2D(node->position().x, node->position().y)));
parentContext->concatenate(lottie::Transform3D::identity().translated(lottie::Vector2D(-node->bounds().x, -node->bounds().y)));
parentContext->concatenate(node->transform()); parentContext->concatenate(node->transform());
float renderAlpha = 1.0; float renderAlpha = 1.0f;
if (tempContext) { if (tempContext) {
renderAlpha = 1.0; renderAlpha = 1.0f;
} else { } else {
renderAlpha = layerAlpha; renderAlpha = layerAlpha;
} }
@ -552,7 +546,7 @@ static void renderLottieRenderNode(std::shared_ptr<lottie::RenderTreeNode> node,
} }
if (isInvertedMatte) { if (isInvertedMatte) {
currentContext->fill(lottie::CGRect(node->bounds().x, node->bounds().y, node->bounds().width, node->bounds().height), lottie::Color(0.0, 0.0, 0.0, 1.0)); currentContext->fill(lottie::CGRect(0.0f, 0.0f, node->size().x, node->size().y), lottie::Color(0.0f, 0.0f, 0.0f, 1.0f));
currentContext->setBlendMode(lottieRendering::BlendMode::DestinationOut); currentContext->setBlendMode(lottieRendering::BlendMode::DestinationOut);
} }

View File

@ -391,8 +391,7 @@ public:
class RenderTreeNode { class RenderTreeNode {
public: public:
RenderTreeNode( RenderTreeNode(
CGRect bounds_, Vector2D size_,
Vector2D position_,
Transform3D transform_, Transform3D transform_,
float alpha_, float alpha_,
bool masksToBounds_, bool masksToBounds_,
@ -401,8 +400,7 @@ public:
std::shared_ptr<RenderTreeNode> mask_, std::shared_ptr<RenderTreeNode> mask_,
bool invertMask_ bool invertMask_
) : ) :
_bounds(bounds_), _size(size_),
_position(position_),
_transform(transform_), _transform(transform_),
_alpha(alpha_), _alpha(alpha_),
_masksToBounds(masksToBounds_), _masksToBounds(masksToBounds_),
@ -419,12 +417,8 @@ public:
} }
public: public:
CGRect const &bounds() const { Vector2D const &size() const {
return _bounds; return _size;
}
Vector2D const &position() const {
return _position;
} }
Transform3D const &transform() const { Transform3D const &transform() const {
@ -456,8 +450,7 @@ public:
} }
public: public:
CGRect _bounds; Vector2D _size;
Vector2D _position;
Transform3D _transform = Transform3D::identity(); Transform3D _transform = Transform3D::identity();
float _alpha = 1.0f; float _alpha = 1.0f;
bool _masksToBounds = false; bool _masksToBounds = false;

View File

@ -4,7 +4,7 @@ namespace lottie {
InvertedMatteLayer::InvertedMatteLayer(std::shared_ptr<CompositionLayer> inputMatte) : InvertedMatteLayer::InvertedMatteLayer(std::shared_ptr<CompositionLayer> inputMatte) :
_inputMatte(inputMatte) { _inputMatte(inputMatte) {
setBounds(inputMatte->bounds()); setSize(inputMatte->size());
addSublayer(_inputMatte); addSublayer(_inputMatte);
} }

View File

@ -57,7 +57,7 @@ public:
_childKeypaths.push_back(_transformNode->transformProperties()); _childKeypaths.push_back(_transformNode->transformProperties());
_contentsLayer->setBounds(CGRect(0.0, 0.0, size.x, size.y)); _contentsLayer->setSize(size);
if (layer->blendMode.has_value() && layer->blendMode.value() != BlendMode::Normal) { if (layer->blendMode.has_value() && layer->blendMode.value() != BlendMode::Normal) {
setCompositingFilter(layer->blendMode); setCompositingFilter(layer->blendMode);

View File

@ -30,9 +30,9 @@ public:
} }
_frameRate = frameRate; _frameRate = frameRate;
setBounds(CGRect(0.0, 0.0, precomp->width, precomp->height)); setSize(Vector2D(precomp->width, precomp->height));
contentsLayer()->setMasksToBounds(true); contentsLayer()->setMasksToBounds(true);
contentsLayer()->setBounds(bounds()); contentsLayer()->setSize(size());
auto layers = initializeCompositionLayers( auto layers = initializeCompositionLayers(
asset.layers, asset.layers,
@ -49,7 +49,7 @@ public:
for (auto layerIt = layers.rbegin(); layerIt != layers.rend(); layerIt++) { for (auto layerIt = layers.rbegin(); layerIt != layers.rend(); layerIt++) {
std::shared_ptr<CompositionLayer> layer = *layerIt; std::shared_ptr<CompositionLayer> layer = *layerIt;
layer->setBounds(bounds()); layer->setSize(size());
_animationLayers.push_back(layer); _animationLayers.push_back(layer);
if (layer->isImageCompositionLayer()) { if (layer->isImageCompositionLayer()) {
@ -121,7 +121,6 @@ public:
std::vector<std::shared_ptr<RenderTreeNode>> renderTreeValue; std::vector<std::shared_ptr<RenderTreeNode>> renderTreeValue;
auto renderTreeContentItem = std::make_shared<RenderTreeNode>( auto renderTreeContentItem = std::make_shared<RenderTreeNode>(
CGRect(0.0, 0.0, 0.0, 0.0),
Vector2D(0.0, 0.0), Vector2D(0.0, 0.0),
Transform3D::identity(), Transform3D::identity(),
1.0, 1.0,
@ -136,7 +135,6 @@ public:
} }
_contentsTreeNode = std::make_shared<RenderTreeNode>( _contentsTreeNode = std::make_shared<RenderTreeNode>(
CGRect(0.0, 0.0, 0.0, 0.0),
Vector2D(0.0, 0.0), Vector2D(0.0, 0.0),
Transform3D::identity(), Transform3D::identity(),
1.0, 1.0,
@ -160,7 +158,6 @@ public:
} }
_renderTreeNode = std::make_shared<RenderTreeNode>( _renderTreeNode = std::make_shared<RenderTreeNode>(
CGRect(0.0, 0.0, 0.0, 0.0),
Vector2D(0.0, 0.0), Vector2D(0.0, 0.0),
Transform3D::identity(), Transform3D::identity(),
1.0, 1.0,
@ -172,12 +169,10 @@ public:
); );
} }
_contentsTreeNode->_bounds = _contentsLayer->bounds(); _contentsTreeNode->_size = _contentsLayer->size();
_contentsTreeNode->_position = _contentsLayer->position();
_contentsTreeNode->_masksToBounds = _contentsLayer->masksToBounds(); _contentsTreeNode->_masksToBounds = _contentsLayer->masksToBounds();
_renderTreeNode->_bounds = bounds(); _renderTreeNode->_size = size();
_renderTreeNode->_position = position();
_renderTreeNode->_transform = transform(); _renderTreeNode->_transform = transform();
_renderTreeNode->_alpha = opacity(); _renderTreeNode->_alpha = opacity();
_renderTreeNode->_masksToBounds = masksToBounds(); _renderTreeNode->_masksToBounds = masksToBounds();

View File

@ -1323,7 +1323,6 @@ std::shared_ptr<RenderTreeNode> ShapeCompositionLayer::renderTreeNode(BezierPath
if (!_renderTreeNode) { if (!_renderTreeNode) {
_contentRenderTreeNode = std::make_shared<RenderTreeNode>( _contentRenderTreeNode = std::make_shared<RenderTreeNode>(
CGRect(0.0, 0.0, 0.0, 0.0),
Vector2D(0.0, 0.0), Vector2D(0.0, 0.0),
Transform3D::identity(), Transform3D::identity(),
1.0, 1.0,
@ -1349,7 +1348,6 @@ std::shared_ptr<RenderTreeNode> ShapeCompositionLayer::renderTreeNode(BezierPath
} }
_renderTreeNode = std::make_shared<RenderTreeNode>( _renderTreeNode = std::make_shared<RenderTreeNode>(
CGRect(0.0, 0.0, 0.0, 0.0),
Vector2D(0.0, 0.0), Vector2D(0.0, 0.0),
Transform3D::identity(), Transform3D::identity(),
1.0, 1.0,
@ -1361,14 +1359,12 @@ std::shared_ptr<RenderTreeNode> ShapeCompositionLayer::renderTreeNode(BezierPath
); );
} }
_contentRenderTreeNode->_bounds = _contentsLayer->bounds(); _contentRenderTreeNode->_size = _contentsLayer->size();
_contentRenderTreeNode->_position = _contentsLayer->position();
_contentRenderTreeNode->_masksToBounds = _contentsLayer->masksToBounds(); _contentRenderTreeNode->_masksToBounds = _contentsLayer->masksToBounds();
_renderTreeNode->_masksToBounds = masksToBounds(); _renderTreeNode->_masksToBounds = masksToBounds();
_renderTreeNode->_bounds = bounds(); _renderTreeNode->_size = size();
_renderTreeNode->_position = position();
return _renderTreeNode; return _renderTreeNode;
} }

View File

@ -42,7 +42,7 @@ public:
_layerTextProvider = std::make_shared<LayerTextProvider>(textProvider); _layerTextProvider = std::make_shared<LayerTextProvider>(textProvider);
_layerFontProvider = std::make_shared<LayerFontProvider>(fontProvider); _layerFontProvider = std::make_shared<LayerFontProvider>(fontProvider);
setBounds(CGRect(0.0, 0.0, animation.width, animation.height)); setSize(Vector2D(animation.width, animation.height));
auto layers = initializeCompositionLayers( auto layers = initializeCompositionLayers(
animation.layers, animation.layers,
@ -60,7 +60,7 @@ public:
for (auto layerIt = layers.rbegin(); layerIt != layers.rend(); layerIt++) { for (auto layerIt = layers.rbegin(); layerIt != layers.rend(); layerIt++) {
std::shared_ptr<CompositionLayer> const &layer = *layerIt; std::shared_ptr<CompositionLayer> const &layer = *layerIt;
layer->setBounds(bounds()); layer->setSize(size());
_animationLayers.push_back(layer); _animationLayers.push_back(layer);
if (layer->isImageCompositionLayer()) { if (layer->isImageCompositionLayer()) {
@ -237,8 +237,7 @@ public:
} }
} }
_renderTreeNode = std::make_shared<RenderTreeNode>( _renderTreeNode = std::make_shared<RenderTreeNode>(
bounds(), size(),
position(),
Transform3D::identity(), Transform3D::identity(),
1.0, 1.0,
false, false,

View File

@ -24,27 +24,13 @@ public:
virtual ~CALayer() = default; virtual ~CALayer() = default;
void addSublayer(std::shared_ptr<CALayer> layer) { void addSublayer(std::shared_ptr<CALayer> layer) {
if (layer->_superlayer) {
layer->_superlayer->removeSublayer(layer.get());
}
layer->_superlayer = this;
_sublayers.push_back(layer); _sublayers.push_back(layer);
} }
void insertSublayer(std::shared_ptr<CALayer> layer, int index) { void insertSublayer(std::shared_ptr<CALayer> layer, int index) {
if (layer->_superlayer) {
layer->_superlayer->removeSublayer(layer.get());
}
layer->_superlayer = this;
_sublayers.insert(_sublayers.begin() + index, layer); _sublayers.insert(_sublayers.begin() + index, layer);
} }
void removeFromSuperlayer() {
if (_superlayer) {
_superlayer->removeSublayer(this);
}
}
virtual bool implementsDraw() const { virtual bool implementsDraw() const {
return false; return false;
} }
@ -71,22 +57,11 @@ public:
_opacity = opacity; _opacity = opacity;
} }
Vector2D const &position() const { Vector2D const &size() const {
return _position; return _size;
} }
void setPosition(Vector2D const &position) { void setSize(Vector2D const &size) {
_position = position; _size = size;
}
CGRect const &bounds() const {
return _bounds;
}
void setBounds(CGRect const &bounds) {
_bounds = bounds;
}
virtual CGRect effectiveBounds() const {
return bounds();
} }
Transform3D const &transform() const { Transform3D const &transform() const {
@ -131,7 +106,6 @@ private:
void removeSublayer(CALayer *layer) { void removeSublayer(CALayer *layer) {
for (auto it = _sublayers.begin(); it != _sublayers.end(); it++) { for (auto it = _sublayers.begin(); it != _sublayers.end(); it++) {
if (it->get() == layer) { if (it->get() == layer) {
layer->_superlayer = nullptr;
_sublayers.erase(it); _sublayers.erase(it);
break; break;
} }
@ -139,12 +113,10 @@ private:
} }
private: private:
CALayer *_superlayer = nullptr;
std::vector<std::shared_ptr<CALayer>> _sublayers; std::vector<std::shared_ptr<CALayer>> _sublayers;
bool _isHidden = false; bool _isHidden = false;
float _opacity = 1.0; float _opacity = 1.0;
Vector2D _position = Vector2D(0.0, 0.0); Vector2D _size = Vector2D(0.0, 0.0);
CGRect _bounds = CGRect(0.0, 0.0, 0.0, 0.0);
Transform3D _transform = Transform3D::identity(); Transform3D _transform = Transform3D::identity();
std::shared_ptr<CALayer> _mask; std::shared_ptr<CALayer> _mask;
bool _masksToBounds = false; bool _masksToBounds = false;
@ -221,21 +193,6 @@ public:
_dashPattern = dashPattern; _dashPattern = dashPattern;
} }
virtual CGRect effectiveBounds() const override {
if (_path) {
CGRect boundingBox = _path->boundingBox();
if (_strokeColor) {
boundingBox.x -= _lineWidth / 2.0;
boundingBox.y -= _lineWidth / 2.0;
boundingBox.width += _lineWidth;
boundingBox.height += _lineWidth;
}
return boundingBox;
} else {
return CGRect(0.0, 0.0, 0.0, 0.0);
}
}
std::shared_ptr<RenderableItem> renderableItem() override; std::shared_ptr<RenderableItem> renderableItem() override;
private: private:

View File

@ -468,10 +468,8 @@ Transform3D fromNativeTransform(::CATransform3D const &value) {
} }
Transform3D Transform3D::makeRotation(float radians, float x, float y, float z) { Transform3D Transform3D::makeRotation(float radians, float x, float y, float z) {
return fromNativeTransform(CATransform3DMakeRotation(radians, x, y, z)); if (std::abs(radians) <= FLT_EPSILON || (x == 0.0 && y == 0.0 && z == 0.0)) {
return Transform3D::identity();
/*if (x == 0.0 && y == 0.0 && z == 0.0) {
return CATransform3D::identity();
} }
float s = sin(radians); float s = sin(radians);
@ -480,43 +478,41 @@ Transform3D Transform3D::makeRotation(float radians, float x, float y, float z)
float len = sqrt(x*x + y*y + z*z); float len = sqrt(x*x + y*y + z*z);
x /= len; y /= len; z /= len; x /= len; y /= len; z /= len;
CATransform3D returnValue = CATransform3D::identity(); Transform3D returnValue = Transform3D::identity();
returnValue.m11 = c + (1-c) * x*x; returnValue.m11 = c + (1.0f - c) * x * x;
returnValue.m12 = (1-c) * x*y + s*z; returnValue.m12 = (1.0f - c) * x*y + s * z;
returnValue.m13 = (1-c) * x*z - s*y; returnValue.m13 = (1.0f - c) * x*z - s * y;
returnValue.m14 = 0; returnValue.m14 = 0.0f;
returnValue.m21 = (1-c) * y*x - s*z; returnValue.m21 = (1.0f - c) * y * x - s * z;
returnValue.m22 = c + (1-c) * y*y; returnValue.m22 = c + (1.0f - c) * y * y;
returnValue.m23 = (1-c) * y*z + s*x; returnValue.m23 = (1.0f - c) * y * z + s * x;
returnValue.m24 = 0; returnValue.m24 = 0.0f;
returnValue.m31 = (1-c) * z*x + s*y; returnValue.m31 = (1.0f - c) * z * x + s * y;
returnValue.m32 = (1-c) * y*z - s*x; returnValue.m32 = (1.0f - c) * y * z - s * x;
returnValue.m33 = c + (1-c) * z*z; returnValue.m33 = c + (1.0f - c) * z * z;
returnValue.m34 = 0; returnValue.m34 = 0.0f;
returnValue.m41 = 0; returnValue.m41 = 0.0f;
returnValue.m42 = 0; returnValue.m42 = 0.0f;
returnValue.m43 = 0; returnValue.m43 = 0.0f;
returnValue.m44 = 1; returnValue.m44 = 1.0f;
return returnValue;*/ return returnValue;
} }
Transform3D Transform3D::rotated(float degrees) const { Transform3D Transform3D::rotated(float degrees) const {
return fromNativeTransform(CATransform3DRotate(nativeTransform(*this), degreesToRadians(degrees), 0.0, 0.0, 1.0)); return Transform3D::makeRotation(degreesToRadians(degrees), 0.0, 0.0, 1.0) * (*this);
//return CATransform3D::makeRotation(degreesToRadians(degrees), 0.0, 0.0, 1.0) * (*this);
} }
Transform3D Transform3D::translated(Vector2D const &translation) const { Transform3D Transform3D::translated(Vector2D const &translation) const {
return fromNativeTransform(CATransform3DTranslate(nativeTransform(*this), translation.x, translation.y, 0.0)); return Transform3D::makeTranslation(translation.x, translation.y, 0.0f) * (*this);
} }
Transform3D Transform3D::scaled(Vector2D const &scale) const { Transform3D Transform3D::scaled(Vector2D const &scale) const {
return fromNativeTransform(CATransform3DScale(nativeTransform(*this), scale.x, scale.y, 1.0)); return Transform3D::makeScale(scale.x, scale.y, 1.0) * (*this);
//return CATransform3D::makeScale(scale.x, scale.y, 1.0) * (*this);
} }
Transform3D Transform3D::operator*(Transform3D const &b) const { Transform3D Transform3D::operator*(Transform3D const &b) const {