common: code refactoring. 23/231923/2
authorHermet Park <chuneon.park@samsung.com>
Sun, 26 Apr 2020 16:45:52 +0000 (01:45 +0900)
committerHermet Park <chuneon.park@samsung.com>
Sun, 26 Apr 2020 16:48:48 +0000 (01:48 +0900)
introduce Canvas class to replace the CanvasBase.
now, SwCanvas, GlCanvas inherits this Canvas for polymorphism
and remove duplicated interfaces.

Change-Id: I65a87e3aa2289d04997930a54aeccd14f57dd73a

inc/tizenvg.h
src/lib/meson.build
src/lib/tvgCanvas.cpp [new file with mode: 0644]
src/lib/tvgCanvasBase.h [deleted file]
src/lib/tvgGlCanvas.cpp
src/lib/tvgSwCanvas.cpp

index e8974396921b4f2587c60f92e3dcbfa67029bb02..babca5d4ba2b3ec9f18bc8161625fe9959df493d 100644 (file)
@@ -72,7 +72,33 @@ class TIZENVG_EXPORT PaintNode
 {
 public:
     virtual ~PaintNode() {}
-    virtual int update(RenderMethod* engine) = 0;
+    virtual int update(RenderMethod*) = 0;
+};
+
+
+/**
+ * @class Canvas
+ *
+ * @ingroup TizenVG
+ *
+ * @brief description...
+ *
+ */
+class TIZENVG_EXPORT Canvas
+{
+public:
+    Canvas(RenderMethod*);
+    virtual ~Canvas();
+
+    virtual int push(std::unique_ptr<PaintNode> paint) noexcept;
+    virtual int clear() noexcept;
+    virtual int update() noexcept;
+    virtual int draw(bool async = true) noexcept;
+    virtual int sync() = 0;
+
+    RenderMethod* engine() noexcept;
+
+    _TIZENVG_DECLARE_PRIVATE(Canvas);
 };
 
 
@@ -141,21 +167,13 @@ public:
   @brief description...
  *
  */
-class TIZENVG_EXPORT SwCanvas final
+class TIZENVG_EXPORT SwCanvas final : public Canvas
 {
 public:
     ~SwCanvas();
 
-    int push(std::unique_ptr<PaintNode> paint) noexcept;
-    int clear() noexcept;
-
-    int update() noexcept;
-    int draw(bool async = true) noexcept;
-    int sync() noexcept;
-    RenderMethod* engine() noexcept;
-
     int target(uint32_t* buffer, size_t stride, size_t height) noexcept;
-
+    int sync() noexcept override;
     static std::unique_ptr<SwCanvas> gen(uint32_t* buffer = nullptr, size_t stride = 0, size_t height = 0) noexcept;
 
     _TIZENVG_DECLARE_PRIVATE(SwCanvas);
@@ -170,20 +188,14 @@ public:
  * @brief description...
  *
  */
-class TIZENVG_EXPORT GlCanvas final
+class TIZENVG_EXPORT GlCanvas final : public Canvas
 {
 public:
     ~GlCanvas();
 
-    int push(std::unique_ptr<PaintNode> paint) noexcept;
-    int clear() noexcept;
-
     //TODO: Gl Specific methods. Need gl backend configuration methods as well.
-    int update() noexcept;
-    int draw(bool async = true) noexcept;
-    int sync() noexcept { return 0; }
-    RenderMethod* engine() noexcept;
 
+    int sync() noexcept override;
     static std::unique_ptr<GlCanvas> gen() noexcept;
 
     _TIZENVG_DECLARE_PRIVATE(GlCanvas);
index ffecb2a52194f636781dd08e7a126ec5cf2f8ced..976aa5eeeec6886ea7de7b955dc464867ab32225 100644 (file)
@@ -5,8 +5,8 @@ source_file = [
    'tvgCommon.h',
    'tvgRenderCommon.h',
    'tvgEngine.cpp',
-   'tvgCanvasBase.h',
    'tvgShapePath.h',
+   'tvgCanvas.cpp',
    'tvgSwCanvas.cpp',
    'tvgGlCanvas.cpp',
    'tvgSceneNode.cpp',
diff --git a/src/lib/tvgCanvas.cpp b/src/lib/tvgCanvas.cpp
new file mode 100644 (file)
index 0000000..3bde24f
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *               http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+#ifndef _TVG_CANVAS_CPP_
+#define _TVG_CANVAS_CPP_
+
+#include "tvgCommon.h"
+
+
+/************************************************************************/
+/* Internal Class Implementation                                        */
+/************************************************************************/
+
+struct Canvas::Impl
+{
+    vector<PaintNode*> nodes;
+    RenderMethod*      renderer;
+
+    Impl(RenderMethod *pRenderer):renderer(pRenderer)
+    {
+        renderer->ref();
+    }
+
+    ~Impl()
+    {
+        clear();
+        renderer->unref();
+    }
+
+    int reserve(size_t n)
+    {
+        nodes.reserve(n);
+
+        return 0;
+    }
+
+    int push(unique_ptr<PaintNode> paint)
+    {
+        PaintNode *node = paint.release();
+        assert(node);
+        nodes.push_back(node);
+        return node->update(renderer);
+    }
+
+    int clear()
+    {
+        assert(renderer);
+
+        auto ret = 0;
+
+        for (auto node : nodes) {
+            if (SceneNode *scene = dynamic_cast<SceneNode *>(node)) {
+                cout << "TODO: " <<  scene << endl;
+            } else if (ShapeNode *shape = dynamic_cast<ShapeNode *>(node)) {
+                ret |= renderer->dispose(*shape, shape->engine());
+            }
+            delete(node);
+        }
+        nodes.clear();
+
+        return ret;
+    }
+
+    int update()
+    {
+        assert(renderer);
+
+        auto ret = 0;
+
+        for(auto node: nodes) {
+            ret |= node->update(renderer);
+        }
+
+        return ret;
+    }
+
+    int draw()
+    {
+        assert(renderer);
+
+        auto ret = 0;
+
+        for(auto node: nodes) {
+            if (SceneNode *scene = dynamic_cast<SceneNode *>(node)) {
+                cout << "TODO: " <<  scene << endl;
+            } else if (ShapeNode *shape = dynamic_cast<ShapeNode *>(node)) {
+                ret |= renderer->render(*shape, shape->engine());
+            }
+        }
+        return ret;
+    }
+
+};
+
+
+/************************************************************************/
+/* External Class Implementation                                        */
+/************************************************************************/
+
+Canvas::Canvas(RenderMethod *pRenderer):pImpl(make_unique<Impl>(pRenderer))
+{
+}
+
+
+Canvas::~Canvas()
+{
+}
+
+
+int Canvas::push(unique_ptr<PaintNode> paint) noexcept
+{
+    auto impl = pImpl.get();
+    assert(impl);
+
+    return impl->push(move(paint));
+}
+
+
+int Canvas::clear() noexcept
+{
+    auto impl = pImpl.get();
+    assert(impl);
+    return impl->clear();
+}
+
+
+int Canvas::draw(bool async) noexcept
+{
+    auto impl = pImpl.get();
+    assert(impl);
+    return impl->draw();
+}
+
+
+int Canvas::update() noexcept
+{
+    auto impl = pImpl.get();
+    assert(impl);
+    return impl->update();
+}
+
+
+RenderMethod* Canvas::engine() noexcept
+{
+    auto impl = pImpl.get();
+    assert(impl);
+    return impl->renderer;
+}
+
+#endif /* _TVG_CANVAS_CPP_ */
diff --git a/src/lib/tvgCanvasBase.h b/src/lib/tvgCanvasBase.h
deleted file mode 100644 (file)
index a2ed34b..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *               http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- */
-#ifndef _TVG_CANVAS_BASE_CPP_
-#define _TVG_CANVAS_BASE_CPP_
-
-#include "tvgCommon.h"
-
-
-struct CanvasBase
-{
-    vector<PaintNode*> nodes;
-    RenderMethod*      renderer;
-
-    CanvasBase(RenderMethod *pRenderer):renderer(pRenderer)
-    {
-        renderer->ref();
-    }
-
-    ~CanvasBase()
-    {
-       clear();
-       renderer->unref();
-    }
-
-    int reserve(size_t n)
-    {
-        nodes.reserve(n);
-
-        return 0;
-    }
-
-    int clear()
-    {
-        assert(renderer);
-
-        auto ret = 0;
-
-        for (auto node : nodes) {
-            if (SceneNode *scene = dynamic_cast<SceneNode *>(node)) {
-                cout << "TODO: " <<  scene << endl;
-            } else if (ShapeNode *shape = dynamic_cast<ShapeNode *>(node)) {
-                ret |= renderer->dispose(*shape, shape->engine());
-            }
-            delete(node);
-        }
-        nodes.clear();
-
-        return ret;
-    }
-
-    int update()
-    {
-        assert(renderer);
-
-        auto ret = 0;
-
-        for(auto node: nodes) {
-            ret |= node->update(renderer);
-        }
-
-        return ret;
-    }
-
-    int push(unique_ptr<PaintNode> paint)
-    {
-        PaintNode *node = paint.release();
-        assert(node);
-        nodes.push_back(node);
-        return node->update(renderer);
-    }
-
-    int draw()
-    {
-        assert(renderer);
-
-        auto ret = 0;
-
-        for(auto node: nodes) {
-            if (SceneNode *scene = dynamic_cast<SceneNode *>(node)) {
-                cout << "TODO: " <<  scene << endl;
-            } else if (ShapeNode *shape = dynamic_cast<ShapeNode *>(node)) {
-                ret |= renderer->render(*shape, shape->engine());
-            }
-        }
-        return ret;
-    }
-
-};
-
-#endif /* _TVG_CANVAS_BASE_CPP_ */
index 386778d7d9bfd7da13fdf4b8ecaf832862f80e30..a7719d30eb78c62e6a102bdaa13d9f28fee1db3c 100644 (file)
 #define _TVG_GLCANVAS_CPP_
 
 #include "tvgCommon.h"
-#include "tvgCanvasBase.h"
 #include "tvgGlRenderer.h"
 
 /************************************************************************/
 /* Internal Class Implementation                                        */
 /************************************************************************/
 
-struct GlCanvas::Impl : CanvasBase
+struct GlCanvas::Impl
 {
-    Impl() : CanvasBase(GlRenderer::inst()) {}
+    Impl() {}
 };
 
 
@@ -35,7 +34,7 @@ struct GlCanvas::Impl : CanvasBase
 /* External Class Implementation                                        */
 /************************************************************************/
 
-GlCanvas::GlCanvas() : pImpl(make_unique<Impl>())
+GlCanvas::GlCanvas() : Canvas(GlRenderer::inst()), pImpl(make_unique<Impl>())
 {
 }
 
@@ -45,51 +44,19 @@ GlCanvas::~GlCanvas()
 }
 
 
-unique_ptr<GlCanvas> GlCanvas::gen() noexcept
-{
-    auto canvas = unique_ptr<GlCanvas>(new GlCanvas);
-    assert(canvas);
-
-    return canvas;
-}
-
-
-int GlCanvas::push(unique_ptr<PaintNode> paint) noexcept
-{
-    auto impl = pImpl.get();
-    assert(impl);
-    return impl->push(move(paint));
-}
-
-int GlCanvas::clear() noexcept
+int GlCanvas::sync() noexcept
 {
-    auto impl = pImpl.get();
-    assert(impl);
-    return impl->clear();
+    return 0;
 }
 
 
-int GlCanvas::update() noexcept
+unique_ptr<GlCanvas> GlCanvas::gen() noexcept
 {
-    auto impl = pImpl.get();
-    assert(impl);
-    return impl->update();
-}
-
+    auto canvas = unique_ptr<GlCanvas>(new GlCanvas);
+    assert(canvas);
 
-RenderMethod* GlCanvas::engine() noexcept
-{
-    auto impl = pImpl.get();
-    assert(impl);
-    return impl->renderer;
+    return canvas;
 }
 
 
-int GlCanvas::draw(bool async) noexcept
-{
-    auto impl = pImpl.get();
-    assert(impl);
-    return impl->draw();
-}
-
 #endif /* _TVG_GLCANVAS_CPP_ */
index 442b0d7ca5a1cdca49bccc406fe6848d0d9593df..04902fc65a00115eaa166e8543237ca81ca751fc 100644 (file)
@@ -18,7 +18,6 @@
 #define _TVG_SWCANVAS_CPP_
 
 #include "tvgCommon.h"
-#include "tvgCanvasBase.h"
 #include "tvgSwRenderer.h"
 
 
@@ -26,9 +25,9 @@
 /* Internal Class Implementation                                        */
 /************************************************************************/
 
-struct SwCanvas::Impl : CanvasBase
+struct SwCanvas::Impl
 {
-    Impl() : CanvasBase(SwRenderer::inst()) {}
+    Impl() {}
 };
 
 
@@ -36,55 +35,30 @@ struct SwCanvas::Impl : CanvasBase
 /* External Class Implementation                                        */
 /************************************************************************/
 
-int SwCanvas::target(uint32_t* buffer, size_t stride, size_t height) noexcept
-{
-    auto impl = pImpl.get();
-    assert(impl);
-
-    dynamic_cast<SwRenderer*>(impl->renderer)->target(buffer, stride, height);
-
-    return 0;
-}
-
-
-int SwCanvas::draw(bool async) noexcept
+SwCanvas::SwCanvas() : Canvas(SwRenderer::inst()), pImpl(make_unique<Impl>())
 {
-    auto impl = pImpl.get();
-    assert(impl);
-    return impl->draw();
 }
 
 
-int SwCanvas::sync() noexcept
-{
-    return 0;
-}
-
-
-int SwCanvas::push(unique_ptr<PaintNode> paint) noexcept
+SwCanvas::~SwCanvas()
 {
-    auto impl = pImpl.get();
-    assert(impl);
-
-    return impl->push(move(paint));
 }
 
 
-int SwCanvas::clear() noexcept
+int SwCanvas::target(uint32_t* buffer, size_t stride, size_t height) noexcept
 {
-    auto impl = pImpl.get();
-    assert(impl);
-    return impl->clear();
-}
+    auto renderer = dynamic_cast<SwRenderer*>(engine());
+    assert(renderer);
 
+    if (!renderer->target(buffer, stride, height)) return -1;
 
-SwCanvas::SwCanvas() : pImpl(make_unique<Impl>())
-{
+    return 0;
 }
 
 
-SwCanvas::~SwCanvas()
+int SwCanvas::sync() noexcept
 {
+    return 0;
 }
 
 
@@ -99,20 +73,4 @@ unique_ptr<SwCanvas> SwCanvas::gen(uint32_t* buffer, size_t stride, size_t heigh
    return canvas;
 }
 
-
-int SwCanvas::update() noexcept
-{
-    auto impl = pImpl.get();
-    assert(impl);
-    return impl->update();
-}
-
-
-RenderMethod* SwCanvas::engine() noexcept
-{
-    auto impl = pImpl.get();
-    assert(impl);
-    return impl->renderer;
-}
-
 #endif /* _TVG_SWCANVAS_CPP_ */