From eef7620bc86f394f3cce157bc58a8724da471678 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Fri, 18 Sep 2020 16:34:12 +0900 Subject: [PATCH] common: binary optimization. (#65) removed unique_ptr usages from pImpl pattern. that increased binary size. 2204082 -> 2045672 --- inc/thorvg.h | 3 +- src/lib/tvgCanvas.cpp | 15 +-- src/lib/tvgCanvasImpl.h | 8 +- src/lib/tvgCommon.h | 2 - src/lib/tvgFill.cpp | 29 +++-- src/lib/tvgGlCanvas.cpp | 8 +- src/lib/tvgLinearGradient.cpp | 24 +++-- src/lib/tvgPaint.cpp | 15 +-- src/lib/tvgPicture.cpp | 11 +- src/lib/tvgPictureImpl.h | 8 +- src/lib/tvgRadialGradient.cpp | 19 ++-- src/lib/tvgScene.cpp | 9 +- src/lib/tvgSceneImpl.h | 8 +- src/lib/tvgShape.cpp | 193 ++++++++++++++++------------------ src/lib/tvgShapeImpl.h | 2 +- src/lib/tvgSwCanvas.cpp | 8 +- 16 files changed, 178 insertions(+), 184 deletions(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index efeaab1a..7d5062c0 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -23,7 +23,7 @@ extern "C" { #define _TVG_DECLARE_PRIVATE(A) \ protected: \ struct Impl; \ - std::unique_ptr pImpl; \ + Impl* pImpl; \ A(const A&) = delete; \ const A& operator=(const A&) = delete; \ A() @@ -91,7 +91,6 @@ public: Result translate(float x, float y) noexcept; Result transform(const Matrix& m) noexcept; Result bounds(float* x, float* y, float* w, float* h) const noexcept; - std::unique_ptr duplicate() const noexcept; _TVG_DECLARE_ACCESSOR(); diff --git a/src/lib/tvgCanvas.cpp b/src/lib/tvgCanvas.cpp index 957940ca..0720e048 100644 --- a/src/lib/tvgCanvas.cpp +++ b/src/lib/tvgCanvas.cpp @@ -26,50 +26,51 @@ /* External Class Implementation */ /************************************************************************/ -Canvas::Canvas(RenderMethod *pRenderer):pImpl(make_unique(pRenderer)) +Canvas::Canvas(RenderMethod *pRenderer):pImpl(new Impl(pRenderer)) { } Canvas::~Canvas() { + delete(pImpl); } Result Canvas::reserve(uint32_t n) noexcept { - IMPL->paints.reserve(n); + pImpl->paints.reserve(n); return Result::Success; } Result Canvas::push(unique_ptr paint) noexcept { - return IMPL->push(move(paint)); + return pImpl->push(move(paint)); } Result Canvas::clear() noexcept { - return IMPL->clear(); + return pImpl->clear(); } Result Canvas::draw() noexcept { - return IMPL->draw(); + return pImpl->draw(); } Result Canvas::update(Paint* paint) noexcept { - return IMPL->update(paint); + return pImpl->update(paint); } Result Canvas::sync() noexcept { - if (IMPL->renderer->flush()) return Result::Success; + if (pImpl->renderer->flush()) return Result::Success; return Result::InsufficientCondition; } \ No newline at end of file diff --git a/src/lib/tvgCanvasImpl.h b/src/lib/tvgCanvasImpl.h index d5b1b767..ebac74db 100644 --- a/src/lib/tvgCanvasImpl.h +++ b/src/lib/tvgCanvasImpl.h @@ -60,7 +60,7 @@ struct Canvas::Impl if (!renderer->clear()) return Result::InsufficientCondition; for (auto paint : paints) { - paint->IMPL->dispose(*renderer); + paint->pImpl->dispose(*renderer); delete(paint); } paints.clear(); @@ -74,13 +74,13 @@ struct Canvas::Impl //Update single paint node if (paint) { - if (!paint->IMPL->update(*renderer, nullptr, RenderUpdateFlag::None)) { + if (!paint->pImpl->update(*renderer, nullptr, RenderUpdateFlag::None)) { return Result::InsufficientCondition; } //Update retained all paint nodes } else { for(auto paint: paints) { - if (!paint->IMPL->update(*renderer, nullptr, RenderUpdateFlag::None)) { + if (!paint->pImpl->update(*renderer, nullptr, RenderUpdateFlag::None)) { return Result::InsufficientCondition; } } @@ -95,7 +95,7 @@ struct Canvas::Impl if (!renderer->preRender()) return Result::InsufficientCondition; for(auto paint: paints) { - if(!paint->IMPL->render(*renderer)) return Result::InsufficientCondition; + if(!paint->pImpl->render(*renderer)) return Result::InsufficientCondition; } if (!renderer->postRender()) return Result::InsufficientCondition; diff --git a/src/lib/tvgCommon.h b/src/lib/tvgCommon.h index c07a4cab..3ed4544d 100644 --- a/src/lib/tvgCommon.h +++ b/src/lib/tvgCommon.h @@ -37,8 +37,6 @@ using namespace std; using namespace tvg; -#define IMPL pImpl.get() - #define FILL_ID_LINEAR 0 #define FILL_ID_RADIAL 1 diff --git a/src/lib/tvgFill.cpp b/src/lib/tvgFill.cpp index cc1a13bf..4594e846 100644 --- a/src/lib/tvgFill.cpp +++ b/src/lib/tvgFill.cpp @@ -43,35 +43,34 @@ struct Fill::Impl /* External Class Implementation */ /************************************************************************/ -Fill::Fill():pImpl(make_unique()) +Fill::Fill():pImpl(new Impl()) { } Fill::~Fill() { + delete(pImpl); } Result Fill::colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept { - auto impl = pImpl.get(); - if (cnt == 0) { - if (impl->colorStops) { - free(impl->colorStops); - impl->colorStops = nullptr; - impl->cnt = cnt; + if (pImpl->colorStops) { + free(pImpl->colorStops); + pImpl->colorStops = nullptr; + pImpl->cnt = cnt; } return Result::Success; } - if (impl->cnt != cnt) { - impl->colorStops = static_cast(realloc(impl->colorStops, cnt * sizeof(ColorStop))); + if (pImpl->cnt != cnt) { + pImpl->colorStops = static_cast(realloc(pImpl->colorStops, cnt * sizeof(ColorStop))); } - impl->cnt = cnt; - memcpy(impl->colorStops, colorStops, cnt * sizeof(ColorStop)); + pImpl->cnt = cnt; + memcpy(pImpl->colorStops, colorStops, cnt * sizeof(ColorStop)); return Result::Success; } @@ -79,15 +78,15 @@ Result Fill::colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept uint32_t Fill::colorStops(const ColorStop** colorStops) const noexcept { - if (colorStops) *colorStops = IMPL->colorStops; + if (colorStops) *colorStops = pImpl->colorStops; - return IMPL->cnt; + return pImpl->cnt; } Result Fill::spread(FillSpread s) noexcept { - IMPL->spread = s; + pImpl->spread = s; return Result::Success; } @@ -95,5 +94,5 @@ Result Fill::spread(FillSpread s) noexcept FillSpread Fill::spread() const noexcept { - return IMPL->spread; + return pImpl->spread; } \ No newline at end of file diff --git a/src/lib/tvgGlCanvas.cpp b/src/lib/tvgGlCanvas.cpp index b425da0e..85bcd1d4 100644 --- a/src/lib/tvgGlCanvas.cpp +++ b/src/lib/tvgGlCanvas.cpp @@ -37,7 +37,6 @@ struct GlCanvas::Impl { - Impl() {} }; @@ -46,9 +45,9 @@ struct GlCanvas::Impl /************************************************************************/ #ifdef THORVG_GL_RASTER_SUPPORT -GlCanvas::GlCanvas() : Canvas(GlRenderer::gen()), pImpl(make_unique()) +GlCanvas::GlCanvas() : Canvas(GlRenderer::gen()), pImpl(new Impl) #else -GlCanvas::GlCanvas() : Canvas(nullptr), pImpl(make_unique()) +GlCanvas::GlCanvas() : Canvas(nullptr), pImpl(new Impl) #endif { } @@ -57,6 +56,7 @@ GlCanvas::GlCanvas() : Canvas(nullptr), pImpl(make_unique()) GlCanvas::~GlCanvas() { + delete(pImpl); } @@ -64,7 +64,7 @@ Result GlCanvas::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t { #ifdef THORVG_GL_RASTER_SUPPORT //We know renderer type, avoid dynamic_cast for performance. - auto renderer = static_cast(Canvas::pImpl.get()->renderer); + auto renderer = static_cast(Canvas::pImpl->renderer); if (!renderer) return Result::MemoryCorruption; if (!renderer->target(buffer, stride, w, h)) return Result::Unknown; diff --git a/src/lib/tvgLinearGradient.cpp b/src/lib/tvgLinearGradient.cpp index 29837c47..1d67c17d 100644 --- a/src/lib/tvgLinearGradient.cpp +++ b/src/lib/tvgLinearGradient.cpp @@ -27,14 +27,17 @@ struct LinearGradient::Impl { - float x1, y1, x2, y2; + float x1 = 0; + float y1 = 0; + float x2 = 0; + float y2 = 0; }; /************************************************************************/ /* External Class Implementation */ /************************************************************************/ -LinearGradient::LinearGradient():pImpl(make_unique()) +LinearGradient::LinearGradient():pImpl(new Impl()) { _id = FILL_ID_LINEAR; } @@ -42,6 +45,7 @@ LinearGradient::LinearGradient():pImpl(make_unique()) LinearGradient::~LinearGradient() { + delete(pImpl); } @@ -50,10 +54,10 @@ Result LinearGradient::linear(float x1, float y1, float x2, float y2) noexcept if (fabsf(x2 - x1) < FLT_EPSILON && fabsf(y2 - y1) < FLT_EPSILON) return Result::InvalidArguments; - IMPL->x1 = x1; - IMPL->y1 = y1; - IMPL->x2 = x2; - IMPL->y2 = y2; + pImpl->x1 = x1; + pImpl->y1 = y1; + pImpl->x2 = x2; + pImpl->y2 = y2; return Result::Success; } @@ -61,10 +65,10 @@ Result LinearGradient::linear(float x1, float y1, float x2, float y2) noexcept Result LinearGradient::linear(float* x1, float* y1, float* x2, float* y2) const noexcept { - if (x1) *x1 = IMPL->x1; - if (x2) *x2 = IMPL->x2; - if (y1) *y1 = IMPL->y1; - if (y2) *y2 = IMPL->y2; + if (x1) *x1 = pImpl->x1; + if (x2) *x2 = pImpl->x2; + if (y1) *y1 = pImpl->y1; + if (y2) *y2 = pImpl->y2; return Result::Success; } diff --git a/src/lib/tvgPaint.cpp b/src/lib/tvgPaint.cpp index 43edcc73..55e0d754 100644 --- a/src/lib/tvgPaint.cpp +++ b/src/lib/tvgPaint.cpp @@ -25,13 +25,14 @@ /* Internal Class Implementation */ /************************************************************************/ -Paint :: Paint() : pImpl(make_unique()) +Paint :: Paint() : pImpl(new Impl()) { } Paint :: ~Paint() { + delete(pImpl); } @@ -41,39 +42,39 @@ Paint :: ~Paint() Result Paint::rotate(float degree) noexcept { - if (IMPL->rotate(degree)) return Result::Success; + if (pImpl->rotate(degree)) return Result::Success; return Result::FailedAllocation; } Result Paint::scale(float factor) noexcept { - if (IMPL->scale(factor)) return Result::Success; + if (pImpl->scale(factor)) return Result::Success; return Result::FailedAllocation; } Result Paint::translate(float x, float y) noexcept { - if (IMPL->translate(x, y)) return Result::Success; + if (pImpl->translate(x, y)) return Result::Success; return Result::FailedAllocation; } Result Paint::transform(const Matrix& m) noexcept { - if (IMPL->transform(m)) return Result::Success; + if (pImpl->transform(m)) return Result::Success; return Result::FailedAllocation; } Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept { - if (IMPL->bounds(x, y, w, h)) return Result::Success; + if (pImpl->bounds(x, y, w, h)) return Result::Success; return Result::InsufficientCondition; } unique_ptr Paint::duplicate() const noexcept { - return IMPL->duplicate(); + return pImpl->duplicate(); } \ No newline at end of file diff --git a/src/lib/tvgPicture.cpp b/src/lib/tvgPicture.cpp index 7c20a372..a4ac3ee5 100644 --- a/src/lib/tvgPicture.cpp +++ b/src/lib/tvgPicture.cpp @@ -25,14 +25,15 @@ /* External Class Implementation */ /************************************************************************/ -Picture::Picture() : pImpl(make_unique()) +Picture::Picture() : pImpl(new Impl()) { - Paint::IMPL->method(new PaintMethod(IMPL)); + Paint::pImpl->method(new PaintMethod(pImpl)); } Picture::~Picture() { + delete(pImpl); } @@ -46,7 +47,7 @@ Result Picture::load(const std::string& path) noexcept { if (path.empty()) return Result::InvalidArguments; - return IMPL->load(path); + return pImpl->load(path); } @@ -54,12 +55,12 @@ Result Picture::load(const char* data, uint32_t size) noexcept { if (!data || size <= 0) return Result::InvalidArguments; - return IMPL->load(data, size); + return pImpl->load(data, size); } Result Picture::viewbox(float* x, float* y, float* w, float* h) const noexcept { - if (IMPL->viewbox(x, y, w, h)) return Result::Success; + if (pImpl->viewbox(x, y, w, h)) return Result::Success; return Result::InsufficientCondition; } \ No newline at end of file diff --git a/src/lib/tvgPictureImpl.h b/src/lib/tvgPictureImpl.h index e98c6b7b..e96384d7 100644 --- a/src/lib/tvgPictureImpl.h +++ b/src/lib/tvgPictureImpl.h @@ -37,7 +37,7 @@ struct Picture::Impl { if (!paint) return false; - paint->IMPL->dispose(renderer); + paint->pImpl->dispose(renderer); delete(paint); return true; @@ -56,13 +56,13 @@ struct Picture::Impl if (!paint) return false; - return paint->IMPL->update(renderer, transform, flag); + return paint->pImpl->update(renderer, transform, flag); } bool render(RenderMethod &renderer) { if (!paint) return false; - return paint->IMPL->render(renderer); + return paint->pImpl->render(renderer); } bool viewbox(float* x, float* y, float* w, float* h) @@ -78,7 +78,7 @@ struct Picture::Impl bool bounds(float* x, float* y, float* w, float* h) { if (!paint) return false; - return paint->IMPL->bounds(x, y, w, h); + return paint->pImpl->bounds(x, y, w, h); } Result load(const string& path) diff --git a/src/lib/tvgRadialGradient.cpp b/src/lib/tvgRadialGradient.cpp index 92e934ee..d91ebba3 100644 --- a/src/lib/tvgRadialGradient.cpp +++ b/src/lib/tvgRadialGradient.cpp @@ -27,7 +27,9 @@ struct RadialGradient::Impl { - float cx, cy, radius; + float cx = 0; + float cy = 0; + float radius = 0; }; @@ -35,7 +37,7 @@ struct RadialGradient::Impl /* External Class Implementation */ /************************************************************************/ -RadialGradient::RadialGradient():pImpl(make_unique()) +RadialGradient::RadialGradient():pImpl(new Impl()) { _id = FILL_ID_RADIAL; } @@ -43,6 +45,7 @@ RadialGradient::RadialGradient():pImpl(make_unique()) RadialGradient::~RadialGradient() { + delete(pImpl); } @@ -50,9 +53,9 @@ Result RadialGradient::radial(float cx, float cy, float radius) noexcept { if (radius < FLT_EPSILON) return Result::InvalidArguments; - IMPL->cx = cx; - IMPL->cy = cy; - IMPL->radius = radius; + pImpl->cx = cx; + pImpl->cy = cy; + pImpl->radius = radius; return Result::Success; } @@ -60,9 +63,9 @@ Result RadialGradient::radial(float cx, float cy, float radius) noexcept Result RadialGradient::radial(float* cx, float* cy, float* radius) const noexcept { - if (cx) *cx = IMPL->cx; - if (cy) *cy = IMPL->cy; - if (radius) *radius = IMPL->radius; + if (cx) *cx = pImpl->cx; + if (cy) *cy = pImpl->cy; + if (radius) *radius = pImpl->radius; return Result::Success; } diff --git a/src/lib/tvgScene.cpp b/src/lib/tvgScene.cpp index df7362fb..cf982a58 100644 --- a/src/lib/tvgScene.cpp +++ b/src/lib/tvgScene.cpp @@ -25,14 +25,15 @@ /* External Class Implementation */ /************************************************************************/ -Scene::Scene() : pImpl(make_unique()) +Scene::Scene() : pImpl(new Impl()) { - Paint::IMPL->method(new PaintMethod(IMPL)); + Paint::pImpl->method(new PaintMethod(pImpl)); } Scene::~Scene() { + delete(pImpl); } @@ -46,7 +47,7 @@ Result Scene::push(unique_ptr paint) noexcept { auto p = paint.release(); if (!p) return Result::MemoryCorruption; - IMPL->paints.push_back(p); + pImpl->paints.push_back(p); return Result::Success; } @@ -54,7 +55,7 @@ Result Scene::push(unique_ptr paint) noexcept Result Scene::reserve(uint32_t size) noexcept { - IMPL->paints.reserve(size); + pImpl->paints.reserve(size); return Result::Success; } \ No newline at end of file diff --git a/src/lib/tvgSceneImpl.h b/src/lib/tvgSceneImpl.h index 4d305926..b1b28981 100644 --- a/src/lib/tvgSceneImpl.h +++ b/src/lib/tvgSceneImpl.h @@ -35,7 +35,7 @@ struct Scene::Impl bool dispose(RenderMethod& renderer) { for (auto paint : paints) { - paint->IMPL->dispose(renderer); + paint->pImpl->dispose(renderer); delete(paint); } paints.clear(); @@ -46,7 +46,7 @@ struct Scene::Impl bool update(RenderMethod &renderer, const RenderTransform* transform, RenderUpdateFlag flag) { for(auto paint: paints) { - if (!paint->IMPL->update(renderer, transform, static_cast(flag))) return false; + if (!paint->pImpl->update(renderer, transform, static_cast(flag))) return false; } return true; } @@ -54,7 +54,7 @@ struct Scene::Impl bool render(RenderMethod &renderer) { for(auto paint: paints) { - if(!paint->IMPL->render(renderer)) return false; + if(!paint->pImpl->render(renderer)) return false; } return true; } @@ -72,7 +72,7 @@ struct Scene::Impl auto w2 = 0.0f; auto h2 = 0.0f; - if (paint->IMPL->bounds(&x2, &y2, &w2, &h2)) return false; + if (paint->pImpl->bounds(&x2, &y2, &w2, &h2)) return false; //Merge regions if (x2 < x) x = x2; diff --git a/src/lib/tvgShape.cpp b/src/lib/tvgShape.cpp index 92da1d5e..a68c0851 100644 --- a/src/lib/tvgShape.cpp +++ b/src/lib/tvgShape.cpp @@ -32,14 +32,15 @@ constexpr auto PATH_KAPPA = 0.552284f; /* External Class Implementation */ /************************************************************************/ -Shape :: Shape() : pImpl(make_unique(this)) +Shape :: Shape() : pImpl(new Impl(this)) { - Paint::IMPL->method(new PaintMethod(IMPL)); + Paint::pImpl->method(new PaintMethod(pImpl)); } Shape :: ~Shape() { + delete(pImpl); } @@ -51,9 +52,9 @@ unique_ptr Shape::gen() noexcept Result Shape::reset() noexcept { - IMPL->path->reset(); + pImpl->path->reset(); - IMPL->flag |= RenderUpdateFlag::Path; + pImpl->flag |= RenderUpdateFlag::Path; return Result::Success; } @@ -63,9 +64,9 @@ uint32_t Shape::pathCommands(const PathCommand** cmds) const noexcept { if (!cmds) return 0; - *cmds = IMPL->path->cmds; + *cmds = pImpl->path->cmds; - return IMPL->path->cmdCnt; + return pImpl->path->cmdCnt; } @@ -73,9 +74,9 @@ uint32_t Shape::pathCoords(const Point** pts) const noexcept { if (!pts) return 0; - *pts = IMPL->path->pts; + *pts = pImpl->path->pts; - return IMPL->path->ptsCnt; + return pImpl->path->ptsCnt; } @@ -83,10 +84,10 @@ Result Shape::appendPath(const PathCommand *cmds, uint32_t cmdCnt, const Point* { if (cmdCnt == 0 || ptsCnt == 0 || !pts || !ptsCnt) return Result::InvalidArguments; - IMPL->path->grow(cmdCnt, ptsCnt); - IMPL->path->append(cmds, cmdCnt, pts, ptsCnt); + pImpl->path->grow(cmdCnt, ptsCnt); + pImpl->path->append(cmds, cmdCnt, pts, ptsCnt); - IMPL->flag |= RenderUpdateFlag::Path; + pImpl->flag |= RenderUpdateFlag::Path; return Result::Success; } @@ -94,9 +95,9 @@ Result Shape::appendPath(const PathCommand *cmds, uint32_t cmdCnt, const Point* Result Shape::moveTo(float x, float y) noexcept { - IMPL->path->moveTo(x, y); + pImpl->path->moveTo(x, y); - IMPL->flag |= RenderUpdateFlag::Path; + pImpl->flag |= RenderUpdateFlag::Path; return Result::Success; } @@ -104,9 +105,9 @@ Result Shape::moveTo(float x, float y) noexcept Result Shape::lineTo(float x, float y) noexcept { - IMPL->path->lineTo(x, y); + pImpl->path->lineTo(x, y); - IMPL->flag |= RenderUpdateFlag::Path; + pImpl->flag |= RenderUpdateFlag::Path; return Result::Success; } @@ -114,9 +115,9 @@ Result Shape::lineTo(float x, float y) noexcept Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) noexcept { - IMPL->path->cubicTo(cx1, cy1, cx2, cy2, x, y); + pImpl->path->cubicTo(cx1, cy1, cx2, cy2, x, y); - IMPL->flag |= RenderUpdateFlag::Path; + pImpl->flag |= RenderUpdateFlag::Path; return Result::Success; } @@ -124,9 +125,9 @@ Result Shape::cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float Result Shape::close() noexcept { - IMPL->path->close(); + pImpl->path->close(); - IMPL->flag |= RenderUpdateFlag::Path; + pImpl->flag |= RenderUpdateFlag::Path; return Result::Success; } @@ -134,20 +135,18 @@ Result Shape::close() noexcept Result Shape::appendCircle(float cx, float cy, float rx, float ry) noexcept { - auto impl = pImpl.get(); - auto rxKappa = rx * PATH_KAPPA; auto ryKappa = ry * PATH_KAPPA; - impl->path->grow(6, 13); - impl->path->moveTo(cx, cy - ry); - impl->path->cubicTo(cx + rxKappa, cy - ry, cx + rx, cy - ryKappa, cx + rx, cy); - impl->path->cubicTo(cx + rx, cy + ryKappa, cx + rxKappa, cy + ry, cx, cy + ry); - impl->path->cubicTo(cx - rxKappa, cy + ry, cx - rx, cy + ryKappa, cx - rx, cy); - impl->path->cubicTo(cx - rx, cy - ryKappa, cx - rxKappa, cy - ry, cx, cy - ry); - impl->path->close(); + pImpl->path->grow(6, 13); + pImpl->path->moveTo(cx, cy - ry); + pImpl->path->cubicTo(cx + rxKappa, cy - ry, cx + rx, cy - ryKappa, cx + rx, cy); + pImpl->path->cubicTo(cx + rx, cy + ryKappa, cx + rxKappa, cy + ry, cx, cy + ry); + pImpl->path->cubicTo(cx - rxKappa, cy + ry, cx - rx, cy + ryKappa, cx - rx, cy); + pImpl->path->cubicTo(cx - rx, cy - ryKappa, cx - rxKappa, cy - ry, cx, cy - ry); + pImpl->path->close(); - impl->flag |= RenderUpdateFlag::Path; + pImpl->flag |= RenderUpdateFlag::Path; return Result::Success; } @@ -159,8 +158,6 @@ Result Shape::appendArc(float cx, float cy, float radius, float startAngle, floa //just circle if (sweep >= 360) return appendCircle(cx, cy, radius, radius); - auto impl = pImpl.get(); - startAngle = (startAngle * M_PI) / 180; sweep = sweep * M_PI / 180; @@ -172,10 +169,10 @@ Result Shape::appendArc(float cx, float cy, float radius, float startAngle, floa Point start = {radius * cos(startAngle), radius * sin(startAngle)}; if (pie) { - impl->path->moveTo(cx, cy); - impl->path->lineTo(start.x + cx, start.y + cy); + pImpl->path->moveTo(cx, cy); + pImpl->path->lineTo(start.x + cx, start.y + cy); }else { - impl->path->moveTo(start.x + cx, start.y + cy); + pImpl->path->moveTo(start.x + cx, start.y + cy); } for (int i = 0; i < nCurves; ++i) { @@ -203,17 +200,17 @@ Result Shape::appendArc(float cx, float cy, float radius, float startAngle, floa Point ctrl1 = {ax - k2 * ay + cx, ay + k2 * ax + cy}; Point ctrl2 = {bx + k2 * by + cx, by - k2 * bx + cy}; - impl->path->cubicTo(ctrl1.x, ctrl1.y, ctrl2.x, ctrl2.y, end.x, end.y); + pImpl->path->cubicTo(ctrl1.x, ctrl1.y, ctrl2.x, ctrl2.y, end.x, end.y); startAngle = endAngle; } if (pie) { - impl->path->moveTo(cx, cy); - impl->path->close(); + pImpl->path->moveTo(cx, cy); + pImpl->path->close(); } - IMPL->flag |= RenderUpdateFlag::Path; + pImpl->flag |= RenderUpdateFlag::Path; return Result::Success; } @@ -221,8 +218,6 @@ Result Shape::appendArc(float cx, float cy, float radius, float startAngle, floa Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) noexcept { - auto impl = pImpl.get(); - auto halfW = w * 0.5f; auto halfH = h * 0.5f; @@ -232,32 +227,32 @@ Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) //rectangle if (rx == 0 && ry == 0) { - impl->path->grow(5, 4); - impl->path->moveTo(x, y); - impl->path->lineTo(x + w, y); - impl->path->lineTo(x + w, y + h); - impl->path->lineTo(x, y + h); - impl->path->close(); + pImpl->path->grow(5, 4); + pImpl->path->moveTo(x, y); + pImpl->path->lineTo(x + w, y); + pImpl->path->lineTo(x + w, y + h); + pImpl->path->lineTo(x, y + h); + pImpl->path->close(); //circle } else if (fabsf(rx - halfW) < FLT_EPSILON && fabsf(ry - halfH) < FLT_EPSILON) { return appendCircle(x + (w * 0.5f), y + (h * 0.5f), rx, ry); } else { auto hrx = rx * 0.5f; auto hry = ry * 0.5f; - impl->path->grow(10, 17); - impl->path->moveTo(x + rx, y); - impl->path->lineTo(x + w - rx, y); - impl->path->cubicTo(x + w - rx + hrx, y, x + w, y + ry - hry, x + w, y + ry); - impl->path->lineTo(x + w, y + h - ry); - impl->path->cubicTo(x + w, y + h - ry + hry, x + w - rx + hrx, y + h, x + w - rx, y + h); - impl->path->lineTo(x + rx, y + h); - impl->path->cubicTo(x + rx - hrx, y + h, x, y + h - ry + hry, x, y + h - ry); - impl->path->lineTo(x, y + ry); - impl->path->cubicTo(x, y + ry - hry, x + rx - hrx, y, x + rx, y); - impl->path->close(); + pImpl->path->grow(10, 17); + pImpl->path->moveTo(x + rx, y); + pImpl->path->lineTo(x + w - rx, y); + pImpl->path->cubicTo(x + w - rx + hrx, y, x + w, y + ry - hry, x + w, y + ry); + pImpl->path->lineTo(x + w, y + h - ry); + pImpl->path->cubicTo(x + w, y + h - ry + hry, x + w - rx + hrx, y + h, x + w - rx, y + h); + pImpl->path->lineTo(x + rx, y + h); + pImpl->path->cubicTo(x + rx - hrx, y + h, x, y + h - ry + hry, x, y + h - ry); + pImpl->path->lineTo(x, y + ry); + pImpl->path->cubicTo(x, y + ry - hry, x + rx - hrx, y, x + rx, y); + pImpl->path->close(); } - impl->flag |= RenderUpdateFlag::Path; + pImpl->flag |= RenderUpdateFlag::Path; return Result::Success; } @@ -265,18 +260,16 @@ Result Shape::appendRect(float x, float y, float w, float h, float rx, float ry) Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept { - auto impl = pImpl.get(); + pImpl->color[0] = r; + pImpl->color[1] = g; + pImpl->color[2] = b; + pImpl->color[3] = a; + pImpl->flag |= RenderUpdateFlag::Color; - impl->color[0] = r; - impl->color[1] = g; - impl->color[2] = b; - impl->color[3] = a; - impl->flag |= RenderUpdateFlag::Color; - - if (impl->fill) { - delete(impl->fill); - impl->fill = nullptr; - impl->flag |= RenderUpdateFlag::Gradient; + if (pImpl->fill) { + delete(pImpl->fill); + pImpl->fill = nullptr; + pImpl->flag |= RenderUpdateFlag::Gradient; } return Result::Success; @@ -285,14 +278,12 @@ Result Shape::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept Result Shape::fill(unique_ptr f) noexcept { - auto impl = pImpl.get(); - auto p = f.release(); if (!p) return Result::MemoryCorruption; - if (impl->fill && impl->fill != p) delete(impl->fill); - impl->fill = p; - impl->flag |= RenderUpdateFlag::Gradient; + if (pImpl->fill && pImpl->fill != p) delete(pImpl->fill); + pImpl->fill = p; + pImpl->flag |= RenderUpdateFlag::Gradient; return Result::Success; } @@ -300,25 +291,23 @@ Result Shape::fill(unique_ptr f) noexcept Result Shape::fill(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept { - auto impl = pImpl.get(); - - if (r) *r = impl->color[0]; - if (g) *g = impl->color[1]; - if (b) *b = impl->color[2]; - if (a) *a = impl->color[3]; + if (r) *r = pImpl->color[0]; + if (g) *g = pImpl->color[1]; + if (b) *b = pImpl->color[2]; + if (a) *a = pImpl->color[3]; return Result::Success; } const Fill* Shape::fill() const noexcept { - return IMPL->fill; + return pImpl->fill; } Result Shape::stroke(float width) noexcept { - if (!IMPL->strokeWidth(width)) return Result::FailedAllocation; + if (!pImpl->strokeWidth(width)) return Result::FailedAllocation; return Result::Success; } @@ -326,14 +315,14 @@ Result Shape::stroke(float width) noexcept float Shape::strokeWidth() const noexcept { - if (!IMPL->stroke) return 0; - return IMPL->stroke->width; + if (!pImpl->stroke) return 0; + return pImpl->stroke->width; } Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept { - if (!IMPL->strokeColor(r, g, b, a)) return Result::FailedAllocation; + if (!pImpl->strokeColor(r, g, b, a)) return Result::FailedAllocation; return Result::Success; } @@ -341,14 +330,12 @@ Result Shape::stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept Result Shape::strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept { - auto impl = pImpl.get(); + if (!pImpl->stroke) return Result::InsufficientCondition; - if (!impl->stroke) return Result::InsufficientCondition; - - if (r) *r = impl->stroke->color[0]; - if (g) *g = impl->stroke->color[1]; - if (b) *b = impl->stroke->color[2]; - if (a) *a = impl->stroke->color[3]; + if (r) *r = pImpl->stroke->color[0]; + if (g) *g = pImpl->stroke->color[1]; + if (b) *b = pImpl->stroke->color[2]; + if (a) *a = pImpl->stroke->color[3]; return Result::Success; } @@ -358,7 +345,7 @@ Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept { if (cnt < 2 || !dashPattern) return Result::InvalidArguments; - if (!IMPL->strokeDash(dashPattern, cnt)) return Result::FailedAllocation; + if (!pImpl->strokeDash(dashPattern, cnt)) return Result::FailedAllocation; return Result::Success; } @@ -366,16 +353,16 @@ Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept uint32_t Shape::strokeDash(const float** dashPattern) const noexcept { - if (!IMPL->stroke) return 0; + if (!pImpl->stroke) return 0; - if (dashPattern) *dashPattern = IMPL->stroke->dashPattern; - return IMPL->stroke->dashCnt; + if (dashPattern) *dashPattern = pImpl->stroke->dashPattern; + return pImpl->stroke->dashCnt; } Result Shape::stroke(StrokeCap cap) noexcept { - if (!IMPL->strokeCap(cap)) return Result::FailedAllocation; + if (!pImpl->strokeCap(cap)) return Result::FailedAllocation; return Result::Success; } @@ -383,7 +370,7 @@ Result Shape::stroke(StrokeCap cap) noexcept Result Shape::stroke(StrokeJoin join) noexcept { - if (!IMPL->strokeJoin(join)) return Result::FailedAllocation; + if (!pImpl->strokeJoin(join)) return Result::FailedAllocation; return Result::Success; } @@ -391,15 +378,15 @@ Result Shape::stroke(StrokeJoin join) noexcept StrokeCap Shape::strokeCap() const noexcept { - if (!IMPL->stroke) return StrokeCap::Square; + if (!pImpl->stroke) return StrokeCap::Square; - return IMPL->stroke->cap; + return pImpl->stroke->cap; } StrokeJoin Shape::strokeJoin() const noexcept { - if (!IMPL->stroke) return StrokeJoin::Bevel; + if (!pImpl->stroke) return StrokeJoin::Bevel; - return IMPL->stroke->join; -} + return pImpl->stroke->join; +} \ No newline at end of file diff --git a/src/lib/tvgShapeImpl.h b/src/lib/tvgShapeImpl.h index 2281573d..d9481a27 100644 --- a/src/lib/tvgShapeImpl.h +++ b/src/lib/tvgShapeImpl.h @@ -181,7 +181,7 @@ struct Shape::Impl auto ret = Shape::gen(); if (!ret) return nullptr; - auto dup = ret.get()->pImpl.get(); + auto dup = ret.get()->pImpl; //Color memcpy(dup->color, color, sizeof(color)); diff --git a/src/lib/tvgSwCanvas.cpp b/src/lib/tvgSwCanvas.cpp index 5e6af449..1a2261a2 100644 --- a/src/lib/tvgSwCanvas.cpp +++ b/src/lib/tvgSwCanvas.cpp @@ -37,7 +37,6 @@ struct SwCanvas::Impl { - Impl() {} }; @@ -46,9 +45,9 @@ struct SwCanvas::Impl /************************************************************************/ #ifdef THORVG_SW_RASTER_SUPPORT -SwCanvas::SwCanvas() : Canvas(SwRenderer::gen()), pImpl(make_unique()) +SwCanvas::SwCanvas() : Canvas(SwRenderer::gen()), pImpl(new Impl) #else -SwCanvas::SwCanvas() : Canvas(nullptr), pImpl(make_unique()) +SwCanvas::SwCanvas() : Canvas(nullptr), pImpl(new Impl) #endif { } @@ -56,6 +55,7 @@ SwCanvas::SwCanvas() : Canvas(nullptr), pImpl(make_unique()) SwCanvas::~SwCanvas() { + delete(pImpl); } @@ -63,7 +63,7 @@ Result SwCanvas::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t { #ifdef THORVG_SW_RASTER_SUPPORT //We know renderer type, avoid dynamic_cast for performance. - auto renderer = static_cast(Canvas::pImpl.get()->renderer); + auto renderer = static_cast(Canvas::pImpl->renderer); if (!renderer) return Result::MemoryCorruption; if (!renderer->target(buffer, stride, w, h, cs)) return Result::InvalidArguments;