common: code refactoring. accepted/tizen/unified/20200923.024103 submit/tizen/20200920.213436
authorHermet Park <chuneon.park@samsung.com>
Fri, 18 Sep 2020 03:14:01 +0000 (12:14 +0900)
committerHermet Park <chuneon.park@samsung.com>
Fri, 18 Sep 2020 07:11:41 +0000 (16:11 +0900)
revise duplicate() approach with stategy pattern.

Change-Id: Id829e6ed5bad481ed317f036ee82991a01faa1c1

inc/thorvg.h
src/lib/tvgLinearGradient.cpp
src/lib/tvgPaint.cpp
src/lib/tvgPaint.h
src/lib/tvgPicture.cpp
src/lib/tvgPictureImpl.h
src/lib/tvgScene.cpp
src/lib/tvgSceneImpl.h
src/lib/tvgShape.cpp
src/lib/tvgShapeImpl.h
src/lib/tvgShapePath.h

index c6fb037..efeaab1 100644 (file)
@@ -92,7 +92,7 @@ public:
     Result transform(const Matrix& m) noexcept;
     Result bounds(float* x, float* y, float* w, float* h) const noexcept;
 
-    virtual std::unique_ptr<Paint> duplicate() const noexcept = 0;
+    std::unique_ptr<Paint> duplicate() const noexcept;
 
     _TVG_DECLARE_ACCESSOR();
     _TVG_DECLARE_PRIVATE(Paint);
@@ -250,7 +250,6 @@ public:
     StrokeJoin strokeJoin() const noexcept;
 
     static std::unique_ptr<Shape> gen() noexcept;
-    std::unique_ptr<Paint> duplicate() const noexcept override;
 
     _TVG_DECLARE_PRIVATE(Shape);
 };
@@ -274,7 +273,6 @@ public:
     Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
 
     static std::unique_ptr<Picture> gen() noexcept;
-    std::unique_ptr<Paint> duplicate() const noexcept override;
 
     _TVG_DECLARE_PRIVATE(Picture);
 };
@@ -297,7 +295,6 @@ public:
     Result reserve(uint32_t size) noexcept;
 
     static std::unique_ptr<Scene> gen() noexcept;
-    std::unique_ptr<Paint> duplicate() const noexcept override;
 
     _TVG_DECLARE_PRIVATE(Scene);
 };
index a76239e..29837c4 100644 (file)
@@ -30,7 +30,6 @@ struct LinearGradient::Impl
     float x1, y1, x2, y2;
 };
 
-
 /************************************************************************/
 /* External Class Implementation                                        */
 /************************************************************************/
index fbbad59..43edcc7 100644 (file)
@@ -72,3 +72,8 @@ Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept
     if (IMPL->bounds(x, y, w, h)) return Result::Success;
     return Result::InsufficientCondition;
 }
+
+unique_ptr<Paint> Paint::duplicate() const noexcept
+{
+    return IMPL->duplicate();
+}
\ No newline at end of file
index 709a99a..0fd3a79 100644 (file)
@@ -32,6 +32,7 @@ namespace tvg
         virtual bool update(RenderMethod& renderer, const RenderTransform* transform, RenderUpdateFlag pFlag) = 0;
         virtual bool render(RenderMethod& renderer) = 0;
         virtual bool bounds(float* x, float* y, float* w, float* h) const = 0;
+        virtual unique_ptr<Paint> duplicate() = 0;
     };
 
     struct Paint::Impl
@@ -144,6 +145,11 @@ namespace tvg
         {
             return smethod->render(renderer);
         }
+
+        unique_ptr<Paint> duplicate()
+        {
+            return smethod->duplicate();
+        }
     };
 
 
@@ -174,6 +180,11 @@ namespace tvg
         {
             return inst->render(renderer);
         }
+
+        unique_ptr<Paint> duplicate() override
+        {
+            return inst->duplicate();
+        }
     };
 }
 
index a3676e0..7c20a37 100644 (file)
@@ -62,11 +62,4 @@ Result Picture::viewbox(float* x, float* y, float* w, float* h) const noexcept
 {
     if (IMPL->viewbox(x, y, w, h)) return Result::Success;
     return Result::InsufficientCondition;
-}
-
-
-std::unique_ptr<Paint> Picture::duplicate() const noexcept
-{
-    //TODO: implement
-    return nullptr;
-}
+}
\ No newline at end of file
index d430de4..e98c6b7 100644 (file)
@@ -104,6 +104,12 @@ struct Picture::Impl
         if (!loader->read()) return Result::Unknown;
         return Result::Success;
     }
+
+    unique_ptr<Paint> duplicate()
+    {
+        //TODO:
+        return nullptr;
+    }
 };
 
 #endif //_TVG_PICTURE_IMPL_H_
\ No newline at end of file
index 11ccebb..df7362f 100644 (file)
@@ -57,11 +57,4 @@ Result Scene::reserve(uint32_t size) noexcept
     IMPL->paints.reserve(size);
 
     return Result::Success;
-}
-
-
-std::unique_ptr<Paint> Scene::duplicate() const noexcept
-{
-    //TODO: implement
-    return nullptr;
-}
+}
\ No newline at end of file
index 68939c3..4d30592 100644 (file)
@@ -88,6 +88,12 @@ struct Scene::Impl
 
         return true;
     }
+
+    unique_ptr<Paint> duplicate()
+    {
+        //TODO:
+        return nullptr;
+    }
 };
 
 #endif //_TVG_SCENE_IMPL_H_
\ No newline at end of file
index 9f5aa0d..92da1d5 100644 (file)
@@ -49,14 +49,6 @@ unique_ptr<Shape> Shape::gen() noexcept
 }
 
 
-unique_ptr<Paint> Shape::duplicate() const noexcept
-{
-    auto shape = Shape::gen();
-    if (!shape->pImpl->duplicate(IMPL)) return nullptr;
-    return shape;
-}
-
-
 Result Shape::reset() noexcept
 {
     IMPL->path->reset();
index d7133c3..2281573 100644 (file)
@@ -38,6 +38,19 @@ struct ShapeStroke
     StrokeCap cap = StrokeCap::Square;
     StrokeJoin join = StrokeJoin::Bevel;
 
+    ShapeStroke() {}
+
+    ShapeStroke(const ShapeStroke* src)
+    {
+        width = src->width;
+        dashCnt = src->dashCnt;
+        cap = src->cap;
+        join = src->join;
+        memcpy(color, src->color, sizeof(color));
+        dashPattern = static_cast<float*>(malloc(sizeof(float) * dashCnt));
+        memcpy(dashPattern, src->dashPattern, sizeof(float) * dashCnt);
+    }
+
     ~ShapeStroke()
     {
         if (dashPattern) free(dashPattern);
@@ -163,42 +176,32 @@ struct Shape::Impl
         return true;
     }
 
-    bool duplicate(Shape::Impl *src)
+    unique_ptr<Paint> duplicate()
     {
-        //Color
-        memcpy(color, src->color, sizeof(color));
-        flag = RenderUpdateFlag::Color;
-
-        //Copy Path
-        if (src->path) {
-            path = new ShapePath();
-            if (!path) return false;
-            *path = *src->path;
+        auto ret = Shape::gen();
+        if (!ret) return nullptr;
 
-            path->cmds = static_cast<PathCommand*>(malloc(sizeof(PathCommand) * path->reservedCmdCnt));
-            if (!path->cmds) return false;
-            memcpy(path->cmds, src->path->cmds, sizeof(PathCommand) * path->cmdCnt);
+        auto dup = ret.get()->pImpl.get();
 
-            path->pts = static_cast<Point*>(malloc(sizeof(Point) * path->reservedPtsCnt));
-            if (!path->pts) return false;
-            memcpy(path->pts, src->path->pts, sizeof(Point) * path->ptsCnt);
+        //Color
+        memcpy(dup->color, color, sizeof(color));
+        dup->flag = RenderUpdateFlag::Color;
 
-            flag |= RenderUpdateFlag::Path;
+        //Path
+        if (path) {
+            dup->path = new ShapePath(path);
+            dup->flag |= RenderUpdateFlag::Path;
         }
 
-        //Copy Stroke
-        if (src->stroke) {
-            stroke = new ShapeStroke();
-            if (!stroke) return false;
-            *stroke = *src->stroke;
-            stroke->dashPattern = static_cast<float*>(malloc(sizeof(float) * stroke->dashCnt));
-            memcpy(stroke->dashPattern,  src->stroke->dashPattern, sizeof(float) * stroke->dashCnt);
-            flag |= RenderUpdateFlag::Stroke;
+        //Stroke
+        if (stroke) {
+            dup->stroke = new ShapeStroke(stroke);
+            dup->flag |= RenderUpdateFlag::Stroke;
         }
 
-        //TODO: Copy Fill
+        //TODO: Fill
 
-        return true;
+        return ret;
     }
 };
 
index 19bbe2f..f3b1f6e 100644 (file)
@@ -44,6 +44,27 @@ struct ShapePath
         if (pts) free(pts);
     }
 
+    ShapePath() {}
+
+    ShapePath(const ShapePath* src)
+    {
+        cmdCnt = src->cmdCnt;
+        reservedCmdCnt = src->reservedCmdCnt;
+        ptsCnt = src->ptsCnt;
+        reservedPtsCnt = src->reservedPtsCnt;
+
+        cmds = static_cast<PathCommand*>(malloc(sizeof(PathCommand) * reservedCmdCnt));
+        if (!cmds) return;
+        memcpy(cmds, src->cmds, sizeof(PathCommand) * cmdCnt);
+
+        pts = static_cast<Point*>(malloc(sizeof(Point) * reservedPtsCnt));
+        if (!pts) {
+            free(cmds);
+            return;
+        }
+        memcpy(pts, src->pts, sizeof(Point) * ptsCnt);
+    }
+
     void reserveCmd(uint32_t cmdCnt)
     {
         if (cmdCnt <= reservedCmdCnt) return;