From 3a3a9b0b70a0e3dd907a5532e066379cce8197f1 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Tue, 30 Mar 2021 20:30:25 +0900 Subject: [PATCH] renderer: code refactoring Keep unique_ptr shape, as tvg recommended to use. --- src/renderer/tvg_renderer.cpp | 64 ++++++++++++++++--------------------------- src/renderer/tvg_renderer.hpp | 19 ++++++------- 2 files changed, 32 insertions(+), 51 deletions(-) diff --git a/src/renderer/tvg_renderer.cpp b/src/renderer/tvg_renderer.cpp index b682f99..9cbaf84 100644 --- a/src/renderer/tvg_renderer.cpp +++ b/src/renderer/tvg_renderer.cpp @@ -4,25 +4,16 @@ using namespace rive; -TvgRenderPath::TvgRenderPath() -{ - this->m_Shape = tvg::Shape::gen().release(); -} - -TvgRenderPath::~TvgRenderPath() -{ - delete(m_Shape); -} void TvgRenderPath::fillRule(FillRule value) { switch (value) { case FillRule::evenOdd: - m_Shape->fill(tvg::FillRule::EvenOdd); + tvgShape->fill(tvg::FillRule::EvenOdd); break; case FillRule::nonZero: - m_Shape->fill(tvg::FillRule::Winding); + tvgShape->fill(tvg::FillRule::Winding); break; } } @@ -42,30 +33,28 @@ Point transformCoord(const Point pt, const Mat2D &transform) void TvgRenderPath::reset() { - m_Shape->reset(); + tvgShape->reset(); } void TvgRenderPath::addRenderPath(RenderPath* path, const Mat2D& transform) { - auto srcShape = static_cast(path)->shape(); - const Point* pts; - auto ptsCnt = srcShape->pathCoords(&pts); + auto ptsCnt = static_cast(path)->tvgShape->pathCoords(&pts); if (!pts) return; const PathCommand* cmds; - auto cmdCnt = srcShape->pathCommands(&cmds); + auto cmdCnt = static_cast(path)->tvgShape->pathCommands(&cmds); if (!cmds) return; //Capture the last coordinates Point* pts2; - auto ptsCnt2 = m_Shape->pathCoords(const_cast(&pts2)); + auto ptsCnt2 = tvgShape->pathCoords(const_cast(&pts2)); - m_Shape->appendPath(cmds, cmdCnt, pts, ptsCnt); + tvgShape->appendPath(cmds, cmdCnt, pts, ptsCnt); //Immediate Transform for the newly appended Point* pts3; - auto ptsCnt3 = m_Shape->pathCoords(const_cast(&pts3)); + auto ptsCnt3 = tvgShape->pathCoords(const_cast(&pts3)); for (unsigned i = ptsCnt2; i < ptsCnt3; ++i) { @@ -75,27 +64,22 @@ void TvgRenderPath::addRenderPath(RenderPath* path, const Mat2D& transform) void TvgRenderPath::moveTo(float x, float y) { - m_Shape->moveTo(x, y); + tvgShape->moveTo(x, y); } void TvgRenderPath::lineTo(float x, float y) { - m_Shape->lineTo(x, y); + tvgShape->lineTo(x, y); } void TvgRenderPath::cubicTo(float ox, float oy, float ix, float iy, float x, float y) { - m_Shape->cubicTo(ox, oy, ix, iy, x, y); + tvgShape->cubicTo(ox, oy, ix, iy, x, y); } void TvgRenderPath::close() { - m_Shape->close(); -} - -TvgRenderPaint::TvgRenderPaint() -{ - + tvgShape->close(); } void TvgRenderPaint::style(RenderPaintStyle style) @@ -238,7 +222,7 @@ void TvgRenderer::transform(const Mat2D& transform) void TvgRenderer::drawPath(RenderPath* path, RenderPaint* paint) { - auto shape = static_cast(path)->shape(); + auto tvgShape = static_cast(path)->tvgShape.get(); auto tvgPaint = static_cast(paint)->paint(); /* OPTIMIZE ME: Stroke / Fill Paints required to draw separately. @@ -247,29 +231,29 @@ void TvgRenderer::drawPath(RenderPath* path, RenderPaint* paint) if (tvgPaint->style == RenderPaintStyle::fill) { if (!tvgPaint->isGradient) - shape->fill(tvgPaint->color[0], tvgPaint->color[1], tvgPaint->color[2], tvgPaint->color[3]); + tvgShape->fill(tvgPaint->color[0], tvgPaint->color[1], tvgPaint->color[2], tvgPaint->color[3]); else { if (!tvgPaint->gradientApplied) { - shape->fill(unique_ptr(tvgPaint->gradientFill)); + tvgShape->fill(unique_ptr(tvgPaint->gradientFill)); tvgPaint->gradientApplied = true; } } } else if (tvgPaint->style == RenderPaintStyle::stroke) { - shape->stroke(tvgPaint->cap); - shape->stroke(tvgPaint->join); - shape->stroke(tvgPaint->thickness); + tvgShape->stroke(tvgPaint->cap); + tvgShape->stroke(tvgPaint->join); + tvgShape->stroke(tvgPaint->thickness); if (!tvgPaint->isGradient) - shape->stroke(tvgPaint->color[0], tvgPaint->color[1], tvgPaint->color[2], tvgPaint->color[3]); + tvgShape->stroke(tvgPaint->color[0], tvgPaint->color[1], tvgPaint->color[2], tvgPaint->color[3]); else { if (!tvgPaint->gradientApplied) { - shape->stroke(unique_ptr(tvgPaint->gradientFill)); + tvgShape->stroke(unique_ptr(tvgPaint->gradientFill)); tvgPaint->gradientApplied = true; } } @@ -278,19 +262,19 @@ void TvgRenderer::drawPath(RenderPath* path, RenderPaint* paint) if (m_ClipPath) { m_ClipPath->fill(255, 255, 255, 255); - shape->composite(unique_ptr(static_cast(m_ClipPath->duplicate())), tvg::CompositeMethod::ClipPath); + tvgShape->composite(unique_ptr(static_cast(m_ClipPath->duplicate())), tvg::CompositeMethod::ClipPath); m_ClipPath = nullptr; } - shape->transform({m_Transform[0], m_Transform[2], m_Transform[4], m_Transform[1], m_Transform[3], m_Transform[5], 0, 0, 1}); - m_Canvas->push(unique_ptr(shape->duplicate())); + tvgShape->transform({m_Transform[0], m_Transform[2], m_Transform[4], m_Transform[1], m_Transform[3], m_Transform[5], 0, 0, 1}); + m_Canvas->push(unique_ptr(tvgShape->duplicate())); } void TvgRenderer::clipPath(RenderPath* path) { //Note: ClipPath transform matrix is calculated by transfrom matrix in addRenderPath function - m_ClipPath = static_cast(path)->shape(); + m_ClipPath = static_cast(path)->tvgShape.get(); m_ClipPath->transform({m_Transform[0], m_Transform[2], m_Transform[4], m_Transform[1], m_Transform[3], m_Transform[5], 0, 0, 1}); } diff --git a/src/renderer/tvg_renderer.hpp b/src/renderer/tvg_renderer.hpp index 3a2aa59..973eca7 100644 --- a/src/renderer/tvg_renderer.hpp +++ b/src/renderer/tvg_renderer.hpp @@ -23,15 +23,12 @@ namespace rive TvgPaint() : isGradient(false), gradientApplied(false) {} }; - class TvgRenderPath : public RenderPath + struct TvgRenderPath : public RenderPath { - private: - Shape *m_Shape; + unique_ptr tvgShape; + + TvgRenderPath() : tvgShape(tvg::Shape::gen()) {} - public: - TvgRenderPath(); - ~TvgRenderPath(); - Shape* shape() { return m_Shape; } void buildShape(); void reset() override; void addRenderPath(RenderPath* path, const Mat2D& transform) override; @@ -39,7 +36,7 @@ namespace rive void moveTo(float x, float y) override; void lineTo(float x, float y) override; void cubicTo(float ox, float oy, float ix, float iy, float x, float y) override; - virtual void close() override; + void close() override; }; struct GradientStop @@ -93,7 +90,6 @@ namespace rive public: TvgPaint* paint() { return &m_Paint; } - TvgRenderPaint(); void style(RenderPaintStyle style) override; void color(unsigned int value) override; void thickness(float value) override; @@ -123,5 +119,6 @@ namespace rive void drawPath(RenderPath* path, RenderPaint* paint) override; void clipPath(RenderPath* path) override; }; -} // namespace rive -#endif +} + +#endif \ No newline at end of file -- 2.7.4