lottie: code refactoring

- _draw() returns boolean whether merging shape is allowed or not.
- renamed some internal variables.
This commit is contained in:
Hermet Park 2024-07-08 17:27:27 +09:00 committed by Hermet Park
parent 49e58eecc0
commit e0116b90c4
2 changed files with 36 additions and 36 deletions

View file

@ -58,7 +58,7 @@ struct RenderContext
{
INLIST_ITEM(RenderContext);
Shape* propagator = nullptr;
Shape* propagator = nullptr; //for propagating the shape properties excluding paths
Shape* merging = nullptr; //merging shapes if possible (if shapes have same properties)
LottieObject** begin = nullptr; //iteration entry point
Array<RenderRepeater> repeaters;
@ -100,7 +100,7 @@ struct RenderContext
static void _updateChildren(LottieGroup* parent, float frameNo, Inlist<RenderContext>& contexts, LottieExpressions* exps);
static void _updateLayer(LottieLayer* root, LottieLayer* layer, float frameNo, LottieExpressions* exps);
static bool _buildComposition(LottieComposition* comp, LottieLayer* parent);
static Shape* _draw(LottieGroup* parent, RenderContext* ctx);
static bool _draw(LottieGroup* parent, RenderContext* ctx);
static void _rotateX(Matrix* m, float degree)
{
@ -377,15 +377,15 @@ static void _updateGradientFill(TVG_UNUSED LottieGroup* parent, LottieObject** c
}
static Shape* _draw(LottieGroup* parent, RenderContext* ctx)
static bool _draw(LottieGroup* parent, RenderContext* ctx)
{
if (ctx->merging) return ctx->merging;
if (ctx->merging) return false;
auto shape = cast<Shape>(ctx->propagator->duplicate());
ctx->merging = shape.get();
parent->scene->push(std::move(shape));
return ctx->merging;
return true;
}
@ -522,8 +522,8 @@ static void _updateRect(LottieGroup* parent, LottieObject** child, float frameNo
_appendRect(path.get(), position.x - size.x * 0.5f, position.y - size.y * 0.5f, size.x, size.y, roundness, ctx->transform);
_repeat(parent, std::move(path), ctx);
} else {
auto merging = _draw(parent, ctx);
_appendRect(merging, position.x - size.x * 0.5f, position.y - size.y * 0.5f, size.x, size.y, roundness, ctx->transform);
_draw(parent, ctx);
_appendRect(ctx->merging, position.x - size.x * 0.5f, position.y - size.y * 0.5f, size.x, size.y, roundness, ctx->transform);
}
}
@ -570,8 +570,8 @@ static void _updateEllipse(LottieGroup* parent, LottieObject** child, float fram
_appendCircle(path.get(), position.x, position.y, size.x * 0.5f, size.y * 0.5f, ctx->transform);
_repeat(parent, std::move(path), ctx);
} else {
auto merging = _draw(parent, ctx);
_appendCircle(merging, position.x, position.y, size.x * 0.5f, size.y * 0.5f, ctx->transform);
_draw(parent, ctx);
_appendCircle(ctx->merging, position.x, position.y, size.x * 0.5f, size.y * 0.5f, ctx->transform);
}
}
@ -585,9 +585,9 @@ static void _updatePath(LottieGroup* parent, LottieObject** child, float frameNo
path->pathset(frameNo, P(p)->rs.path.cmds, P(p)->rs.path.pts, ctx->transform, ctx->roundness, exps);
_repeat(parent, std::move(p), ctx);
} else {
auto merging = _draw(parent, ctx);
if (path->pathset(frameNo, P(merging)->rs.path.cmds, P(merging)->rs.path.pts, ctx->transform, ctx->roundness, exps)) {
P(merging)->update(RenderUpdateFlag::Path);
_draw(parent, ctx);
if (path->pathset(frameNo, P(ctx->merging)->rs.path.cmds, P(ctx->merging)->rs.path.pts, ctx->transform, ctx->roundness, exps)) {
P(ctx->merging)->update(RenderUpdateFlag::Path);
}
}
}
@ -863,10 +863,10 @@ static void _updatePolystar(LottieGroup* parent, LottieObject** child, float fra
else _updatePolygon(parent, star, identity ? nullptr : &matrix, frameNo, p.get(), exps);
_repeat(parent, std::move(p), ctx);
} else {
auto merging = _draw(parent, ctx);
if (star->type == LottiePolyStar::Star) _updateStar(parent, star, identity ? nullptr : &matrix, ctx->roundness, frameNo, merging, exps);
else _updatePolygon(parent, star, identity ? nullptr : &matrix, frameNo, merging, exps);
P(merging)->update(RenderUpdateFlag::Path);
_draw(parent, ctx);
if (star->type == LottiePolyStar::Star) _updateStar(parent, star, identity ? nullptr : &matrix, ctx->roundness, frameNo, ctx->merging, exps);
else _updatePolygon(parent, star, identity ? nullptr : &matrix, frameNo, ctx->merging, exps);
P(ctx->merging)->update(RenderUpdateFlag::Path);
}
}

View file

@ -263,44 +263,44 @@ static void _roundCorner(Array<PathCommand>& cmds, Array<Point>& pts, const Poin
}
static bool _modifier(Point* inputPts, uint32_t inputPtsCnt, PathCommand* inputCmds, uint32_t inputCmdsCnt, Array<PathCommand>& cmds, Array<Point>& pts, Matrix* transform, float roundness)
static bool _modifier(Point* inPts, uint32_t inPtsCnt, PathCommand* inCmds, uint32_t inCmdsCnt, Array<PathCommand>& cmds, Array<Point>& pts, Matrix* transform, float roundness)
{
cmds.reserve(inputCmdsCnt * 2);
pts.reserve((uint16_t)(inputPtsCnt * 1.5));
cmds.reserve(inCmdsCnt * 2);
pts.reserve((uint16_t)(inPtsCnt * 1.5));
auto ptsCnt = pts.count;
auto startIndex = 0;
for (uint32_t iCmds = 0, iPts = 0; iCmds < inputCmdsCnt; ++iCmds) {
switch (inputCmds[iCmds]) {
for (uint32_t iCmds = 0, iPts = 0; iCmds < inCmdsCnt; ++iCmds) {
switch (inCmds[iCmds]) {
case PathCommand::MoveTo: {
startIndex = pts.count;
cmds.push(PathCommand::MoveTo);
pts.push(inputPts[iPts++]);
pts.push(inPts[iPts++]);
break;
}
case PathCommand::CubicTo: {
auto& prev = inputPts[iPts - 1];
auto& curr = inputPts[iPts + 2];
if (iCmds < inputCmdsCnt - 1 &&
mathZero(inputPts[iPts - 1] - inputPts[iPts]) &&
mathZero(inputPts[iPts + 1] - inputPts[iPts + 2])) {
if (inputCmds[iCmds + 1] == PathCommand::CubicTo &&
mathZero(inputPts[iPts + 2] - inputPts[iPts + 3]) &&
mathZero(inputPts[iPts + 4] - inputPts[iPts + 5])) {
_roundCorner(cmds, pts, prev, curr, inputPts[iPts + 5], roundness);
auto& prev = inPts[iPts - 1];
auto& curr = inPts[iPts + 2];
if (iCmds < inCmdsCnt - 1 &&
mathZero(inPts[iPts - 1] - inPts[iPts]) &&
mathZero(inPts[iPts + 1] - inPts[iPts + 2])) {
if (inCmds[iCmds + 1] == PathCommand::CubicTo &&
mathZero(inPts[iPts + 2] - inPts[iPts + 3]) &&
mathZero(inPts[iPts + 4] - inPts[iPts + 5])) {
_roundCorner(cmds, pts, prev, curr, inPts[iPts + 5], roundness);
iPts += 3;
break;
} else if (inputCmds[iCmds + 1] == PathCommand::Close) {
_roundCorner(cmds, pts, prev, curr, inputPts[2], roundness);
} else if (inCmds[iCmds + 1] == PathCommand::Close) {
_roundCorner(cmds, pts, prev, curr, inPts[2], roundness);
pts[startIndex] = pts.last();
iPts += 3;
break;
}
}
cmds.push(PathCommand::CubicTo);
pts.push(inputPts[iPts++]);
pts.push(inputPts[iPts++]);
pts.push(inputPts[iPts++]);
pts.push(inPts[iPts++]);
pts.push(inPts[iPts++]);
pts.push(inPts[iPts++]);
break;
}
case PathCommand::Close: {