From 043b6b9f4f3e937f78771c28e01295a58dfcfa2b Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Mon, 19 Feb 2024 13:41:12 +0900 Subject: [PATCH] common/array: code refactoring Make the array interface pair begin()/end() for consistency. --- src/common/tvgArray.h | 10 ++++++++++ src/loaders/lottie/tvgLottieBuilder.cpp | 24 ++++++++++++------------ src/loaders/lottie/tvgLottieModel.cpp | 10 +++++----- src/loaders/lottie/tvgLottieModel.h | 8 ++++---- src/loaders/lottie/tvgLottieParser.cpp | 8 ++++---- src/loaders/lottie/tvgLottieProperty.h | 8 ++++---- src/loaders/svg/tvgSvgLoader.cpp | 4 ++-- src/loaders/svg/tvgSvgSceneBuilder.cpp | 4 ++-- src/renderer/sw_engine/tvgSwMath.cpp | 4 ++-- src/renderer/sw_engine/tvgSwRenderer.cpp | 18 +++++++++--------- src/renderer/sw_engine/tvgSwRle.cpp | 2 +- src/renderer/sw_engine/tvgSwStroke.cpp | 2 +- src/renderer/tvgShape.h | 2 +- src/renderer/tvgTaskScheduler.cpp | 6 +++--- 14 files changed, 60 insertions(+), 50 deletions(-) diff --git a/src/common/tvgArray.h b/src/common/tvgArray.h index acb3a41b..d95df404 100644 --- a/src/common/tvgArray.h +++ b/src/common/tvgArray.h @@ -90,6 +90,16 @@ struct Array return data[idx]; } + const T* begin() const + { + return data; + } + + T* begin() + { + return data; + } + T* end() { return data + count; diff --git a/src/loaders/lottie/tvgLottieBuilder.cpp b/src/loaders/lottie/tvgLottieBuilder.cpp index 291a43b4..e3aa6a14 100644 --- a/src/loaders/lottie/tvgLottieBuilder.cpp +++ b/src/loaders/lottie/tvgLottieBuilder.cpp @@ -360,11 +360,11 @@ static void _repeat(LottieGroup* parent, unique_ptr path, RenderContext* //push repeat shapes in order. if (repeater->inorder) { - for (auto shape = shapes.data; shape < shapes.end(); ++shape) { + for (auto shape = shapes.begin(); shape < shapes.end(); ++shape) { parent->scene->push(cast(*shape)); } } else { - for (auto shape = shapes.end() - 1; shape >= shapes.data; --shape) { + for (auto shape = shapes.end() - 1; shape >= shapes.begin(); --shape) { parent->scene->push(cast(*shape)); } } @@ -454,15 +454,15 @@ static void _updateText(LottieGroup* parent, LottieObject** child, float frameNo //text string while (*p != '\0') { //find the glyph - for (auto g = text->font->chars.data; g < text->font->chars.end(); ++g) { + for (auto g = text->font->chars.begin(); g < text->font->chars.end(); ++g) { auto glyph = *g; //draw matched glyphs if (!strncmp(glyph->code, p, glyph->len)) { //TODO: caching? auto shape = Shape::gen(); - for (auto g = glyph->children.data; g < glyph->children.end(); ++g) { + for (auto g = glyph->children.begin(); g < glyph->children.end(); ++g) { auto group = static_cast(*g); - for (auto p = group->children.data; p < group->children.end(); ++p) { + for (auto p = group->children.begin(); p < group->children.end(); ++p) { if (static_cast(*p)->pathset(frameNo, P(shape)->rs.path.cmds, P(shape)->rs.path.pts)) { P(shape)->update(RenderUpdateFlag::Path); } @@ -889,7 +889,7 @@ static void _updatePrecomp(LottieLayer* precomp, float frameNo) frameNo = precomp->remap(frameNo); - for (auto child = precomp->children.end() - 1; child >= precomp->children.data; --child) { + for (auto child = precomp->children.end() - 1; child >= precomp->children.begin(); --child) { _updateLayer(precomp, static_cast(*child), frameNo); } @@ -925,7 +925,7 @@ static void _updateMaskings(LottieLayer* layer, float frameNo) Shape* pmask = nullptr; auto pmethod = CompositeMethod::AlphaMask; - for (auto m = layer->masks.data; m < layer->masks.end(); ++m) { + for (auto m = layer->masks.begin(); m < layer->masks.end(); ++m) { auto mask = static_cast(*m); auto shape = Shape::gen().release(); shape->fill(255, 255, 255, mask->opacity(frameNo)); @@ -1029,7 +1029,7 @@ static void _updateLayer(LottieLayer* root, LottieLayer* layer, float frameNo) static void _buildReference(LottieComposition* comp, LottieLayer* layer) { - for (auto asset = comp->assets.data; asset < comp->assets.end(); ++asset) { + for (auto asset = comp->assets.begin(); asset < comp->assets.end(); ++asset) { if (strcmp(layer->refId, (*asset)->name)) continue; if (layer->type == LottieLayer::Precomp) { auto assetLayer = static_cast(*asset); @@ -1055,7 +1055,7 @@ static void _bulidHierarchy(LottieGroup* parent, LottieLayer* child) return; } - for (auto p = parent->children.data; p < parent->children.end(); ++p) { + for (auto p = parent->children.begin(); p < parent->children.end(); ++p) { auto parent = static_cast(*p); if (child == parent) continue; if (child->pid == parent->id) { @@ -1073,7 +1073,7 @@ static void _bulidHierarchy(LottieGroup* parent, LottieLayer* child) static void _attachFont(LottieComposition* comp, LottieLayer* parent) { //TODO: Consider to migrate this attachment to the frame update time. - for (auto c = parent->children.data; c < parent->children.end(); ++c) { + for (auto c = parent->children.begin(); c < parent->children.end(); ++c) { auto text = static_cast(*c); auto& doc = text->doc(0); if (!doc.name) continue; @@ -1096,7 +1096,7 @@ static bool _buildComposition(LottieComposition* comp, LottieGroup* parent) if (parent->buildDone) return true; parent->buildDone = true; - for (auto c = parent->children.data; c < parent->children.end(); ++c) { + for (auto c = parent->children.begin(); c < parent->children.end(); ++c) { auto child = static_cast(*c); //attach the precomp layer. @@ -1135,7 +1135,7 @@ bool LottieBuilder::update(LottieComposition* comp, float frameNo) auto root = comp->root; root->scene->clear(); - for (auto child = root->children.end() - 1; child >= root->children.data; --child) { + for (auto child = root->children.end() - 1; child >= root->children.begin(); --child) { _updateLayer(root, static_cast(*child), frameNo); } return true; diff --git a/src/loaders/lottie/tvgLottieModel.cpp b/src/loaders/lottie/tvgLottieModel.cpp index c7f10613..42d1ece2 100644 --- a/src/loaders/lottie/tvgLottieModel.cpp +++ b/src/loaders/lottie/tvgLottieModel.cpp @@ -141,7 +141,7 @@ void LottieGroup::prepare(LottieObject::Type type) size_t strokeCnt = 0; size_t fillCnt = 0; - for (auto c = children.end() - 1; c >= children.data; --c) { + for (auto c = children.end() - 1; c >= children.begin(); --c) { if (reqFragment && !statical) break; auto child = static_cast(*c); if (statical) statical &= child->statical; @@ -170,7 +170,7 @@ LottieLayer::~LottieLayer() free(refId); } - for (auto m = masks.data; m < masks.end(); ++m) { + for (auto m = masks.begin(); m < masks.end(); ++m) { delete(*m); } @@ -214,18 +214,18 @@ LottieComposition::~LottieComposition() free(name); //delete interpolators - for (auto i = interpolators.data; i < interpolators.end(); ++i) { + for (auto i = interpolators.begin(); i < interpolators.end(); ++i) { free((*i)->key); free(*i); } //delete assets - for (auto a = assets.data; a < assets.end(); ++a) { + for (auto a = assets.begin(); a < assets.end(); ++a) { delete(*a); } //delete fonts - for (auto f = fonts.data; f < fonts.end(); ++f) { + for (auto f = fonts.begin(); f < fonts.end(); ++f) { delete(*f); } } \ No newline at end of file diff --git a/src/loaders/lottie/tvgLottieModel.h b/src/loaders/lottie/tvgLottieModel.h index 3b624009..5b928655 100644 --- a/src/loaders/lottie/tvgLottieModel.h +++ b/src/loaders/lottie/tvgLottieModel.h @@ -164,7 +164,7 @@ struct LottieGradient bool prepare() { if (colorStops.frames) { - for (auto v = colorStops.frames->data; v < colorStops.frames->end(); ++v) { + for (auto v = colorStops.frames->begin(); v < colorStops.frames->end(); ++v) { colorStops.count = populate(v->value); } } else { @@ -253,7 +253,7 @@ struct LottieGlyph ~LottieGlyph() { - for (auto p = children.data; p < children.end(); ++p) delete(*p); + for (auto p = children.begin(); p < children.end(); ++p) delete(*p); free(code); } }; @@ -265,7 +265,7 @@ struct LottieFont ~LottieFont() { - for (auto c = chars.data; c < chars.end(); ++c) delete(*c); + for (auto c = chars.begin(); c < chars.end(); ++c) delete(*c); free(style); free(family); free(name); @@ -524,7 +524,7 @@ struct LottieGroup : LottieObject { virtual ~LottieGroup() { - for (auto p = children.data; p < children.end(); ++p) delete(*p); + for (auto p = children.begin(); p < children.end(); ++p) delete(*p); } void prepare(LottieObject::Type type = LottieObject::Group); diff --git a/src/loaders/lottie/tvgLottieParser.cpp b/src/loaders/lottie/tvgLottieParser.cpp index 85b91631..d58d7788 100644 --- a/src/loaders/lottie/tvgLottieParser.cpp +++ b/src/loaders/lottie/tvgLottieParser.cpp @@ -209,9 +209,9 @@ void LottieParser::getValue(PathSet& path) if (ins.count != outs.count || outs.count != pts.count) return; //convert path - auto out = outs.data; - auto in = ins.data; - auto pt = pts.data; + auto out = outs.begin(); + auto in = ins.begin(); + auto pt = pts.begin(); //Store manipulated results Array outPts; @@ -379,7 +379,7 @@ LottieInterpolator* LottieParser::getInterpolator(const char* key, Point& in, Po LottieInterpolator* interpolator = nullptr; //get a cached interpolator if it has any. - for (auto i = comp->interpolators.data; i < comp->interpolators.end(); ++i) { + for (auto i = comp->interpolators.begin(); i < comp->interpolators.end(); ++i) { if (!strncmp((*i)->key, key, sizeof(buf))) interpolator = *i; } diff --git a/src/loaders/lottie/tvgLottieProperty.h b/src/loaders/lottie/tvgLottieProperty.h index 72f6f843..0a7f4bc6 100644 --- a/src/loaders/lottie/tvgLottieProperty.h +++ b/src/loaders/lottie/tvgLottieProperty.h @@ -263,7 +263,7 @@ struct LottiePathSet free(value.pts); if (!frames) return; - for (auto p = frames->data; p < frames->end(); ++p) { + for (auto p = frames->begin(); p < frames->end(); ++p) { free((*p).value.cmds); free((*p).value.pts); } @@ -353,7 +353,7 @@ struct LottieColorStop { free(value.data); if (!frames) return; - for (auto p = frames->data; p < frames->end(); ++p) { + for (auto p = frames->begin(); p < frames->end(); ++p) { free((*p).value.data); } free(frames->data); @@ -486,7 +486,7 @@ struct LottiePosition void prepare() { if (!frames || frames->count < 2) return; - for (auto frame = frames->data + 1; frame < frames->end(); ++frame) { + for (auto frame = frames->begin() + 1; frame < frames->end(); ++frame) { (frame - 1)->prepare(frame); } } @@ -504,7 +504,7 @@ struct LottieTextDoc free(value.name); if (!frames) return; - for (auto p = frames->data; p < frames->end(); ++p) { + for (auto p = frames->begin(); p < frames->end(); ++p) { free((*p).value.text); free((*p).value.name); } diff --git a/src/loaders/svg/tvgSvgLoader.cpp b/src/loaders/svg/tvgSvgLoader.cpp index 2235be85..e590f8c4 100644 --- a/src/loaders/svg/tvgSvgLoader.cpp +++ b/src/loaders/svg/tvgSvgLoader.cpp @@ -3495,7 +3495,7 @@ void SvgLoader::clear(bool all) free(loaderData.svgParse); loaderData.svgParse = nullptr; - for (auto gradient = loaderData.gradients.data; gradient < loaderData.gradients.end(); ++gradient) { + for (auto gradient = loaderData.gradients.begin(); gradient < loaderData.gradients.end(); ++gradient) { (*gradient)->clear(); free(*gradient); } @@ -3507,7 +3507,7 @@ void SvgLoader::clear(bool all) if (!all) return; - for (auto p = loaderData.images.data; p < loaderData.images.end(); ++p) { + for (auto p = loaderData.images.begin(); p < loaderData.images.end(); ++p) { free(*p); } loaderData.images.reset(); diff --git a/src/loaders/svg/tvgSvgSceneBuilder.cpp b/src/loaders/svg/tvgSvgSceneBuilder.cpp index 51c7ea46..e6ee4d3d 100644 --- a/src/loaders/svg/tvgSvgSceneBuilder.cpp +++ b/src/loaders/svg/tvgSvgSceneBuilder.cpp @@ -409,7 +409,7 @@ static bool _recognizeShape(SvgNode* node, Shape* shape) } case SvgNodeType::Polygon: { if (node->node.polygon.pts.count < 2) break; - auto pts = node->node.polygon.pts.data; + auto pts = node->node.polygon.pts.begin(); shape->moveTo(pts[0], pts[1]); for (pts += 2; pts < node->node.polygon.pts.end(); pts += 2) { shape->lineTo(pts[0], pts[1]); @@ -419,7 +419,7 @@ static bool _recognizeShape(SvgNode* node, Shape* shape) } case SvgNodeType::Polyline: { if (node->node.polyline.pts.count < 2) break; - auto pts = node->node.polyline.pts.data; + auto pts = node->node.polyline.pts.begin(); shape->moveTo(pts[0], pts[1]); for (pts += 2; pts < node->node.polyline.pts.end(); pts += 2) { shape->lineTo(pts[0], pts[1]); diff --git a/src/renderer/sw_engine/tvgSwMath.cpp b/src/renderer/sw_engine/tvgSwMath.cpp index 42e40519..4dae3122 100644 --- a/src/renderer/sw_engine/tvgSwMath.cpp +++ b/src/renderer/sw_engine/tvgSwMath.cpp @@ -293,13 +293,13 @@ bool mathUpdateOutlineBBox(const SwOutline* outline, const SwBBox& clipRegion, S { if (!outline) return false; - auto pt = outline->pts.data; - if (outline->pts.empty() || outline->cntrs.empty()) { renderRegion.reset(); return false; } + auto pt = outline->pts.begin(); + auto xMin = pt->x; auto xMax = pt->x; auto yMin = pt->y; diff --git a/src/renderer/sw_engine/tvgSwRenderer.cpp b/src/renderer/sw_engine/tvgSwRenderer.cpp index af6f0192..d8a16fc3 100644 --- a/src/renderer/sw_engine/tvgSwRenderer.cpp +++ b/src/renderer/sw_engine/tvgSwRenderer.cpp @@ -182,7 +182,7 @@ struct SwShapeTask : SwTask shapeDelOutline(&shape, mpool, tid); //Clip Path - for (auto clip = clips.data; clip < clips.end(); ++clip) { + for (auto clip = clips.begin(); clip < clips.end(); ++clip) { auto clipper = static_cast(*clip); //Clip shape rle if (shape.rle && !clipper->clip(shape.rle)) goto err; @@ -242,7 +242,7 @@ struct SwSceneTask : SwTask rleMerge(sceneRle, clipper1->rle(), clipper2->rle()); //Unify the remained clippers - for (auto rd = scene.data + 2; rd < scene.end(); ++rd) { + for (auto rd = scene.begin() + 2; rd < scene.end(); ++rd) { auto clipper = static_cast(*rd); rleMerge(sceneRle, sceneRle, clipper->rle()); } @@ -301,7 +301,7 @@ struct SwImageTask : SwTask if (image.rle) { //Clear current task memorypool here if the clippers would use the same memory pool imageDelOutline(&image, mpool, tid); - for (auto clip = clips.data; clip < clips.end(); ++clip) { + for (auto clip = clips.begin(); clip < clips.end(); ++clip) { auto clipper = static_cast(*clip); if (!clipper->clip(image.rle)) goto err; } @@ -384,7 +384,7 @@ bool SwRenderer::clear() bool SwRenderer::sync() { - for (auto task = tasks.data; task < tasks.end(); ++task) { + for (auto task = tasks.begin(); task < tasks.end(); ++task) { if ((*task)->disposed) { delete(*task); } else { @@ -452,7 +452,7 @@ bool SwRenderer::preRender() void SwRenderer::clearCompositors() { //Free Composite Caches - for (auto comp = compositors.data; comp < compositors.end(); ++comp) { + for (auto comp = compositors.begin(); comp < compositors.end(); ++comp) { free((*comp)->compositor->image.data); delete((*comp)->compositor); delete(*comp); @@ -468,7 +468,7 @@ bool SwRenderer::postRender() rasterUnpremultiply(surface); } - for (auto task = tasks.data; task < tasks.end(); ++task) { + for (auto task = tasks.begin(); task < tasks.end(); ++task) { if ((*task)->disposed) delete(*task); else (*task)->pushed = false; } @@ -625,7 +625,7 @@ Compositor* SwRenderer::target(const RenderRegion& region, ColorSpace cs) auto reqChannelSize = CHANNEL_SIZE(cs); //Use cached data - for (auto p = compositors.data; p < compositors.end(); ++p) { + for (auto p = compositors.begin(); p < compositors.end(); ++p) { if ((*p)->compositor->valid && (*p)->compositor->image.channelSize == reqChannelSize) { cmp = *p; break; @@ -724,7 +724,7 @@ void* SwRenderer::prepareCommon(SwTask* task, const RenderTransform* transform, //TODO: Failed threading them. It would be better if it's possible. //See: https://github.com/thorvg/thorvg/issues/1409 //Guarantee composition targets get ready. - for (auto clip = clips.data; clip < clips.end(); ++clip) { + for (auto clip = clips.begin(); clip < clips.end(); ++clip) { static_cast(*clip)->done(); } @@ -785,7 +785,7 @@ RenderData SwRenderer::prepare(const Array& scene, RenderData data, //TODO: Failed threading them. It would be better if it's possible. //See: https://github.com/thorvg/thorvg/issues/1409 //Guarantee composition targets get ready. - for (auto task = scene.data; task < scene.end(); ++task) { + for (auto task = scene.begin(); task < scene.end(); ++task) { static_cast(*task)->done(); } return prepareCommon(task, transform, clips, opacity, flags); diff --git a/src/renderer/sw_engine/tvgSwRle.cpp b/src/renderer/sw_engine/tvgSwRle.cpp index c87f9c85..3af7e1b5 100644 --- a/src/renderer/sw_engine/tvgSwRle.cpp +++ b/src/renderer/sw_engine/tvgSwRle.cpp @@ -713,7 +713,7 @@ static void _decomposeOutline(RleWorker& rw) auto outline = rw.outline; auto first = 0; //index of first point in contour - for (auto cntr = outline->cntrs.data; cntr < outline->cntrs.end(); ++cntr) { + for (auto cntr = outline->cntrs.begin(); cntr < outline->cntrs.end(); ++cntr) { auto last = *cntr; auto limit = outline->pts.data + last; auto start = UPSCALE(outline->pts[first]); diff --git a/src/renderer/sw_engine/tvgSwStroke.cpp b/src/renderer/sw_engine/tvgSwStroke.cpp index 8f44cf36..eff63858 100644 --- a/src/renderer/sw_engine/tvgSwStroke.cpp +++ b/src/renderer/sw_engine/tvgSwStroke.cpp @@ -835,7 +835,7 @@ bool strokeParseOutline(SwStroke* stroke, const SwOutline& outline) uint32_t first = 0; uint32_t i = 0; - for (auto cntr = outline.cntrs.data; cntr < outline.cntrs.end(); ++cntr, ++i) { + for (auto cntr = outline.cntrs.begin(); cntr < outline.cntrs.end(); ++cntr, ++i) { auto last = *cntr; //index of last point in contour auto limit = outline.pts.data + last; diff --git a/src/renderer/tvgShape.h b/src/renderer/tvgShape.h index 54d7353b..09117f8e 100644 --- a/src/renderer/tvgShape.h +++ b/src/renderer/tvgShape.h @@ -106,7 +106,7 @@ struct Shape::Impl { //Path bounding size if (rs.path.pts.count > 0 ) { - auto pts = rs.path.pts.data; + auto pts = rs.path.pts.begin(); Point min = { pts->x, pts->y }; Point max = { pts->x, pts->y }; diff --git a/src/renderer/tvgTaskScheduler.cpp b/src/renderer/tvgTaskScheduler.cpp index ac683b4e..d4932772 100644 --- a/src/renderer/tvgTaskScheduler.cpp +++ b/src/renderer/tvgTaskScheduler.cpp @@ -123,14 +123,14 @@ struct TaskSchedulerImpl ~TaskSchedulerImpl() { - for (auto tq = taskQueues.data; tq < taskQueues.end(); ++tq) { + for (auto tq = taskQueues.begin(); tq < taskQueues.end(); ++tq) { (*tq)->complete(); } - for (auto thread = threads.data; thread < threads.end(); ++thread) { + for (auto thread = threads.begin(); thread < threads.end(); ++thread) { (*thread)->join(); delete(*thread); } - for (auto tq = taskQueues.data; tq < taskQueues.end(); ++tq) { + for (auto tq = taskQueues.begin(); tq < taskQueues.end(); ++tq) { delete(*tq); } }