From 78cb32794a03f124374b2bc7ef8f3297bd6020c0 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Tue, 5 Sep 2023 10:58:45 +0900 Subject: [PATCH] sw_engine: utilize the memory pool for dash stroking. --- src/renderer/sw_engine/tvgSwCommon.h | 3 + src/renderer/sw_engine/tvgSwMemPool.cpp | 73 +++++++++++-------------- src/renderer/sw_engine/tvgSwShape.cpp | 27 ++++----- 3 files changed, 45 insertions(+), 58 deletions(-) diff --git a/src/renderer/sw_engine/tvgSwCommon.h b/src/renderer/sw_engine/tvgSwCommon.h index 7d246688..a43e3dc5 100644 --- a/src/renderer/sw_engine/tvgSwCommon.h +++ b/src/renderer/sw_engine/tvgSwCommon.h @@ -271,6 +271,7 @@ struct SwMpool { SwOutline* outline; SwOutline* strokeOutline; + SwOutline* dashOutline; unsigned allocSize; }; @@ -542,6 +543,8 @@ SwOutline* mpoolReqOutline(SwMpool* mpool, unsigned idx); void mpoolRetOutline(SwMpool* mpool, unsigned idx); SwOutline* mpoolReqStrokeOutline(SwMpool* mpool, unsigned idx); void mpoolRetStrokeOutline(SwMpool* mpool, unsigned idx); +SwOutline* mpoolReqDashOutline(SwMpool* mpool, unsigned idx); +void mpoolRetDashOutline(SwMpool* mpool, unsigned idx); bool rasterCompositor(SwSurface* surface); bool rasterGradientShape(SwSurface* surface, SwShape* shape, unsigned id); diff --git a/src/renderer/sw_engine/tvgSwMemPool.cpp b/src/renderer/sw_engine/tvgSwMemPool.cpp index 936d9cbd..54ae594b 100644 --- a/src/renderer/sw_engine/tvgSwMemPool.cpp +++ b/src/renderer/sw_engine/tvgSwMemPool.cpp @@ -62,54 +62,52 @@ void mpoolRetStrokeOutline(SwMpool* mpool, unsigned idx) } +SwOutline* mpoolReqDashOutline(SwMpool* mpool, unsigned idx) +{ + return &mpool->dashOutline[idx]; +} + + +void mpoolRetDashOutline(SwMpool* mpool, unsigned idx) +{ + mpool->dashOutline[idx].pts.clear(); + mpool->dashOutline[idx].cntrs.clear(); + mpool->dashOutline[idx].types.clear(); + mpool->dashOutline[idx].closed.clear(); +} + + SwMpool* mpoolInit(unsigned threads) { auto allocSize = threads + 1; auto mpool = static_cast(calloc(sizeof(SwMpool), 1)); mpool->outline = static_cast(calloc(1, sizeof(SwOutline) * allocSize)); - if (!mpool->outline) goto err; - mpool->strokeOutline = static_cast(calloc(1, sizeof(SwOutline) * allocSize)); - if (!mpool->strokeOutline) goto err; - + mpool->dashOutline = static_cast(calloc(1, sizeof(SwOutline) * allocSize)); mpool->allocSize = allocSize; return mpool; - -err: - if (mpool->outline) { - free(mpool->outline); - mpool->outline = nullptr; - } - - if (mpool->strokeOutline) { - free(mpool->strokeOutline); - mpool->strokeOutline = nullptr; - } - free(mpool); - return nullptr; } bool mpoolClear(SwMpool* mpool) { - SwOutline* p; - for (unsigned i = 0; i < mpool->allocSize; ++i) { - //Outline - p = &mpool->outline[i]; - p->pts.reset(); - p->cntrs.reset(); - p->types.reset(); - p->closed.reset(); + mpool->outline[i].pts.reset(); + mpool->outline[i].cntrs.reset(); + mpool->outline[i].types.reset(); + mpool->outline[i].closed.reset(); - //StrokeOutline - p = &mpool->strokeOutline[i]; - p->pts.reset(); - p->cntrs.reset(); - p->types.reset(); - p->closed.reset(); + mpool->strokeOutline[i].pts.reset(); + mpool->strokeOutline[i].cntrs.reset(); + mpool->strokeOutline[i].types.reset(); + mpool->strokeOutline[i].closed.reset(); + + mpool->dashOutline[i].pts.reset(); + mpool->dashOutline[i].cntrs.reset(); + mpool->dashOutline[i].types.reset(); + mpool->dashOutline[i].closed.reset(); } return true; @@ -122,16 +120,9 @@ bool mpoolTerm(SwMpool* mpool) mpoolClear(mpool); - if (mpool->outline) { - free(mpool->outline); - mpool->outline = nullptr; - } - - if (mpool->strokeOutline) { - free(mpool->strokeOutline); - mpool->strokeOutline = nullptr; - } - + free(mpool->outline); + free(mpool->strokeOutline); + free(mpool->dashOutline); free(mpool); return true; diff --git a/src/renderer/sw_engine/tvgSwShape.cpp b/src/renderer/sw_engine/tvgSwShape.cpp index df3592b1..75b4fbfd 100644 --- a/src/renderer/sw_engine/tvgSwShape.cpp +++ b/src/renderer/sw_engine/tvgSwShape.cpp @@ -204,7 +204,7 @@ static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ct } -static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix* transform, float length) +static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix* transform, float length, SwMpool* mpool, unsigned tid) { const PathCommand* cmds = rshape->path.cmds.data; auto cmdCnt = rshape->path.cmds.count; @@ -275,8 +275,7 @@ static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix* trans } } - //OPTMIZE ME: Use mempool??? - dash.outline = static_cast(calloc(1, sizeof(SwOutline))); + dash.outline = mpoolReqDashOutline(mpool, tid); //smart reservation auto closeCnt = 0; @@ -559,16 +558,16 @@ bool shapeGenStrokeRle(SwShape* shape, const RenderShape* rshape, const Matrix* { SwOutline* shapeOutline = nullptr; SwOutline* strokeOutline = nullptr; - bool freeOutline = false; - bool ret = true; + auto dashStroking = false; + auto ret = true; auto length = rshape->strokeTrim() ? _outlineLength(rshape) : 0.0f; //Dash style (+trimming) if (rshape->stroke->dashCnt > 0 || length > 0) { - shapeOutline = _genDashOutline(rshape, transform, length); + shapeOutline = _genDashOutline(rshape, transform, length, mpool, tid); if (!shapeOutline) return false; - freeOutline = true; + dashStroking = true; //Normal style } else { if (!shape->outline) { @@ -579,26 +578,20 @@ bool shapeGenStrokeRle(SwShape* shape, const RenderShape* rshape, const Matrix* if (!strokeParseOutline(shape->stroke, *shapeOutline)) { ret = false; - goto fail; + goto clear; } strokeOutline = strokeExportOutline(shape->stroke, mpool, tid); if (!mathUpdateOutlineBBox(strokeOutline, clipRegion, renderRegion, false)) { ret = false; - goto fail; + goto clear; } shape->strokeRle = rleRender(shape->strokeRle, strokeOutline, renderRegion, true); -fail: - if (freeOutline) { - free(shapeOutline->cntrs.data); - free(shapeOutline->pts.data); - free(shapeOutline->types.data); - free(shapeOutline->closed.data); - free(shapeOutline); - } +clear: + if (dashStroking) mpoolRetDashOutline(mpool, tid); mpoolRetStrokeOutline(mpool, tid); return ret;