common: code refactoring

Replace individual array implementation with a common feature.
This commit is contained in:
Hermet Park 2023-07-03 13:41:48 +09:00 committed by Hermet Park
parent 5f64baf642
commit 1c824ed191
5 changed files with 55 additions and 84 deletions

View file

@ -269,11 +269,11 @@ static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ct
static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix* transform) static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix* transform)
{ {
const PathCommand* cmds = rshape->path.cmds; const PathCommand* cmds = rshape->path.cmds.data;
auto cmdCnt = rshape->path.cmdCnt; auto cmdCnt = rshape->path.cmds.count;
const Point* pts = rshape->path.pts; const Point* pts = rshape->path.pts.data;
auto ptsCnt = rshape->path.ptsCnt; auto ptsCnt = rshape->path.pts.count;
//No actual shape data //No actual shape data
if (cmdCnt == 0 || ptsCnt == 0) return nullptr; if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
@ -383,11 +383,11 @@ static bool _axisAlignedRect(const SwOutline* outline)
static bool _genOutline(SwShape* shape, const RenderShape* rshape, const Matrix* transform, SwMpool* mpool, unsigned tid, bool hasComposite) static bool _genOutline(SwShape* shape, const RenderShape* rshape, const Matrix* transform, SwMpool* mpool, unsigned tid, bool hasComposite)
{ {
const PathCommand* cmds = rshape->path.cmds; const PathCommand* cmds = rshape->path.cmds.data;
auto cmdCnt = rshape->path.cmdCnt; auto cmdCnt = rshape->path.cmds.count;
const Point* pts = rshape->path.pts; const Point* pts = rshape->path.pts.data;
auto ptsCnt = rshape->path.ptsCnt; auto ptsCnt = rshape->path.pts.count;
//No actual shape data //No actual shape data
if (cmdCnt == 0 || ptsCnt == 0) return false; if (cmdCnt == 0 || ptsCnt == 0) return false;

View file

@ -111,7 +111,7 @@ struct Array
~Array() ~Array()
{ {
if (data) free(data); free(data);
} }
}; };

View file

@ -153,13 +153,8 @@ struct RenderShape
{ {
struct struct
{ {
PathCommand* cmds = nullptr; Array<PathCommand> cmds;
uint32_t cmdCnt = 0; Array<Point> pts;
uint32_t reservedCmdCnt = 0;
Point *pts = nullptr;
uint32_t ptsCnt = 0;
uint32_t reservedPtsCnt = 0;
} path; } path;
Fill *fill = nullptr; Fill *fill = nullptr;
@ -169,9 +164,6 @@ struct RenderShape
~RenderShape() ~RenderShape()
{ {
free(path.cmds);
free(path.pts);
if (fill) delete(fill); if (fill) delete(fill);
if (stroke) delete(stroke); if (stroke) delete(stroke);
} }

View file

@ -59,7 +59,10 @@ uint32_t Shape::identifier() noexcept
Result Shape::reset() noexcept Result Shape::reset() noexcept
{ {
pImpl->reset(); pImpl->rs.path.cmds.clear();
pImpl->rs.path.pts.clear();
pImpl->flag = RenderUpdateFlag::Path;
return Result::Success; return Result::Success;
} }
@ -69,9 +72,8 @@ uint32_t Shape::pathCommands(const PathCommand** cmds) const noexcept
{ {
if (!cmds) return 0; if (!cmds) return 0;
*cmds = pImpl->rs.path.cmds; *cmds = pImpl->rs.path.cmds.data;
return pImpl->rs.path.cmds.count;
return pImpl->rs.path.cmdCnt;
} }
@ -79,9 +81,8 @@ uint32_t Shape::pathCoords(const Point** pts) const noexcept
{ {
if (!pts) return 0; if (!pts) return 0;
*pts = pImpl->rs.path.pts; *pts = pImpl->rs.path.pts.data;
return pImpl->rs.path.pts.count;
return pImpl->rs.path.ptsCnt;
} }

View file

@ -106,15 +106,16 @@ struct Shape::Impl
bool bounds(float* x, float* y, float* w, float* h) bool bounds(float* x, float* y, float* w, float* h)
{ {
//Path bounding size //Path bounding size
if (rs.path.ptsCnt > 0 ) { if (rs.path.pts.count > 0 ) {
Point min = { rs.path.pts[0].x, rs.path.pts[0].y }; auto pts = rs.path.pts.data;
Point max = { rs.path.pts[0].x, rs.path.pts[0].y }; Point min = { pts->x, pts->y };
Point max = { pts->x, pts->y };
for (uint32_t i = 1; i < rs.path.ptsCnt; ++i) { for (auto pts2 = pts + 1; pts2 < rs.path.pts.end(); ++pts2) {
if (rs.path.pts[i].x < min.x) min.x = rs.path.pts[i].x; if (pts2->x < min.x) min.x = pts2->x;
if (rs.path.pts[i].y < min.y) min.y = rs.path.pts[i].y; if (pts2->y < min.y) min.y = pts2->y;
if (rs.path.pts[i].x > max.x) max.x = rs.path.pts[i].x; if (pts2->x > max.x) max.x = pts2->x;
if (rs.path.pts[i].y > max.y) max.y = rs.path.pts[i].y; if (pts2->y > max.y) max.y = pts2->y;
} }
if (x) *x = min.x; if (x) *x = min.x;
@ -130,88 +131,67 @@ struct Shape::Impl
if (w) *w += rs.stroke->width; if (w) *w += rs.stroke->width;
if (h) *h += rs.stroke->width; if (h) *h += rs.stroke->width;
} }
return rs.path.ptsCnt > 0 ? true : false; return rs.path.pts.count > 0 ? true : false;
} }
void reserveCmd(uint32_t cmdCnt) void reserveCmd(uint32_t cmdCnt)
{ {
if (cmdCnt <= rs.path.reservedCmdCnt) return; rs.path.cmds.reserve(cmdCnt);
rs.path.reservedCmdCnt = cmdCnt;
rs.path.cmds = static_cast<PathCommand*>(realloc(rs.path.cmds, sizeof(PathCommand) * rs.path.reservedCmdCnt));
} }
void reservePts(uint32_t ptsCnt) void reservePts(uint32_t ptsCnt)
{ {
if (ptsCnt <= rs.path.reservedPtsCnt) return; rs.path.pts.reserve(ptsCnt);
rs.path.reservedPtsCnt = ptsCnt;
rs.path.pts = static_cast<Point*>(realloc(rs.path.pts, sizeof(Point) * rs.path.reservedPtsCnt));
} }
void grow(uint32_t cmdCnt, uint32_t ptsCnt) void grow(uint32_t cmdCnt, uint32_t ptsCnt)
{ {
reserveCmd(rs.path.cmdCnt + cmdCnt); rs.path.cmds.grow(cmdCnt);
reservePts(rs.path.ptsCnt + ptsCnt); rs.path.pts.grow(ptsCnt);
}
void reset()
{
rs.path.cmdCnt = 0;
rs.path.ptsCnt = 0;
flag = RenderUpdateFlag::Path;
} }
void append(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt) void append(const PathCommand* cmds, uint32_t cmdCnt, const Point* pts, uint32_t ptsCnt)
{ {
memcpy(rs.path.cmds + rs.path.cmdCnt, cmds, sizeof(PathCommand) * cmdCnt); memcpy(rs.path.cmds.end(), cmds, sizeof(PathCommand) * cmdCnt);
memcpy(rs.path.pts + rs.path.ptsCnt, pts, sizeof(Point) * ptsCnt); memcpy(rs.path.pts.end(), pts, sizeof(Point) * ptsCnt);
rs.path.cmdCnt += cmdCnt; rs.path.cmds.count += cmdCnt;
rs.path.ptsCnt += ptsCnt; rs.path.pts.count += ptsCnt;
flag |= RenderUpdateFlag::Path; flag |= RenderUpdateFlag::Path;
} }
void moveTo(float x, float y) void moveTo(float x, float y)
{ {
if (rs.path.cmdCnt + 1 > rs.path.reservedCmdCnt) reserveCmd((rs.path.cmdCnt + 1) * 2); rs.path.cmds.push(PathCommand::MoveTo);
if (rs.path.ptsCnt + 2 > rs.path.reservedPtsCnt) reservePts((rs.path.ptsCnt + 2) * 2); rs.path.pts.push({x, y});
rs.path.cmds[rs.path.cmdCnt++] = PathCommand::MoveTo;
rs.path.pts[rs.path.ptsCnt++] = {x, y};
flag |= RenderUpdateFlag::Path; flag |= RenderUpdateFlag::Path;
} }
void lineTo(float x, float y) void lineTo(float x, float y)
{ {
if (rs.path.cmdCnt + 1 > rs.path.reservedCmdCnt) reserveCmd((rs.path.cmdCnt + 1) * 2); rs.path.cmds.push(PathCommand::LineTo);
if (rs.path.ptsCnt + 2 > rs.path.reservedPtsCnt) reservePts((rs.path.ptsCnt + 2) * 2); rs.path.pts.push({x, y});
rs.path.cmds[rs.path.cmdCnt++] = PathCommand::LineTo;
rs.path.pts[rs.path.ptsCnt++] = {x, y};
flag |= RenderUpdateFlag::Path; flag |= RenderUpdateFlag::Path;
} }
void cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) void cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y)
{ {
if (rs.path.cmdCnt + 1 > rs.path.reservedCmdCnt) reserveCmd((rs.path.cmdCnt + 1) * 2); rs.path.cmds.push(PathCommand::CubicTo);
if (rs.path.ptsCnt + 3 > rs.path.reservedPtsCnt) reservePts((rs.path.ptsCnt + 3) * 2); rs.path.pts.push({cx1, cy1});
rs.path.pts.push({cx2, cy2});
rs.path.cmds[rs.path.cmdCnt++] = PathCommand::CubicTo; rs.path.pts.push({x, y});
rs.path.pts[rs.path.ptsCnt++] = {cx1, cy1};
rs.path.pts[rs.path.ptsCnt++] = {cx2, cy2};
rs.path.pts[rs.path.ptsCnt++] = {x, y};
flag |= RenderUpdateFlag::Path; flag |= RenderUpdateFlag::Path;
} }
void close() void close()
{ {
if (rs.path.cmdCnt > 0 && rs.path.cmds[rs.path.cmdCnt - 1] == PathCommand::Close) return; //Don't close multiple times.
if (rs.path.cmds.count > 0 && rs.path.cmds.last() == PathCommand::Close) return;
if (rs.path.cmdCnt + 1 > rs.path.reservedCmdCnt) reserveCmd((rs.path.cmdCnt + 1) * 2); rs.path.cmds.push(PathCommand::Close);
rs.path.cmds[rs.path.cmdCnt++] = PathCommand::Close;
flag |= RenderUpdateFlag::Path; flag |= RenderUpdateFlag::Path;
} }
@ -335,17 +315,15 @@ struct Shape::Impl
dup->flag = RenderUpdateFlag::Color; dup->flag = RenderUpdateFlag::Color;
//Path //Path
if (rs.path.cmdCnt > 0 && rs.path.ptsCnt > 0) { if (rs.path.cmds.count > 0 && rs.path.pts.count > 0) {
dup->rs.path.cmdCnt = rs.path.cmdCnt; dup->rs.path.cmds.reserve(rs.path.cmds.reserved);
dup->rs.path.reservedCmdCnt = rs.path.reservedCmdCnt; dup->rs.path.pts.reserve(rs.path.pts.reserved);
dup->rs.path.ptsCnt = rs.path.ptsCnt;
dup->rs.path.reservedPtsCnt = rs.path.reservedPtsCnt;
dup->rs.path.cmds = static_cast<PathCommand*>(malloc(sizeof(PathCommand) * dup->rs.path.reservedCmdCnt)); dup->rs.path.cmds.count = rs.path.cmds.count;
if (dup->rs.path.cmds) memcpy(dup->rs.path.cmds, rs.path.cmds, sizeof(PathCommand) * dup->rs.path.cmdCnt); memcpy(dup->rs.path.cmds.data, rs.path.cmds.data, sizeof(PathCommand) * dup->rs.path.cmds.count);
dup->rs.path.pts = static_cast<Point*>(malloc(sizeof(Point) * dup->rs.path.reservedPtsCnt)); dup->rs.path.pts.count = rs.path.pts.count;
if (dup->rs.path.pts) memcpy(dup->rs.path.pts, rs.path.pts, sizeof(Point) * dup->rs.path.ptsCnt); memcpy(dup->rs.path.pts.data, rs.path.pts.data, sizeof(Point) * dup->rs.path.pts.count);
} }
dup->flag |= RenderUpdateFlag::Path; dup->flag |= RenderUpdateFlag::Path;