examples: move tests to src/examples
[platform/core/graphics/tizenvg.git] / src / examples / testDuplicate.cpp
1 #include "testCommon.h"
2
3 /************************************************************************/
4 /* Drawing Commands                                                     */
5 /************************************************************************/
6
7 void tvgDrawCmds(tvg::Canvas* canvas)
8 {
9     if (!canvas) return;
10
11     //Prepare first shape, which will not be drawn
12     auto shape1 = tvg::Shape::gen();
13     shape1->appendRect(10, 10, 200, 200, 0, 0);
14     shape1->appendRect(220, 10, 100, 100, 0, 0);
15
16     shape1->stroke(3);
17     shape1->stroke(0, 255, 0, 255);
18
19     float dashPattern[2] = {4, 4};
20     shape1->stroke(dashPattern, 2);
21     shape1->fill(255, 0, 0, 255);
22
23     //Create second shape and duplicate parameters
24     auto shape2 = shape1->duplicate();
25
26     //Create third shape, duplicate from first and add some new parameters
27     auto shape3 = shape1->duplicate();
28
29     //Get access to valid derived class type
30     tvg::Shape* shapePtr = reinterpret_cast<tvg::Shape*>(shape3.get());
31
32     //move shape3 to new postion to don't cover second shape
33     shape3->translate(0, 220);
34
35     //append new paths
36     shapePtr->appendRect(340, 10, 100, 100, 0, 0);
37     shapePtr->fill(0, 0, 255, 255);
38
39     //TODO: test gradient
40     canvas->push(move(shape2));
41     canvas->push(move(shape3));
42 }
43
44
45 /************************************************************************/
46 /* Sw Engine Test Code                                                  */
47 /************************************************************************/
48
49 static unique_ptr<tvg::SwCanvas> swCanvas;
50
51 void tvgSwTest(uint32_t* buffer)
52 {
53     //Create a Canvas
54     swCanvas = tvg::SwCanvas::gen();
55     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
56
57     /* Push the shape into the Canvas drawing list
58        When this shape is into the canvas list, the shape could update & prepare
59        internal data asynchronously for coming rendering.
60        Canvas keeps this shape node unless user call canvas->clear() */
61     tvgDrawCmds(swCanvas.get());
62 }
63
64 void drawSwView(void* data, Eo* obj)
65 {
66     if (swCanvas->draw() == tvg::Result::Success) {
67         swCanvas->sync();
68     }
69 }
70
71
72 /************************************************************************/
73 /* GL Engine Test Code                                                  */
74 /************************************************************************/
75
76 static unique_ptr<tvg::GlCanvas> glCanvas;
77
78 void initGLview(Evas_Object *obj)
79 {
80     static constexpr auto BPP = 4;
81
82     //Create a Canvas
83     glCanvas = tvg::GlCanvas::gen();
84     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
85
86     /* Push the shape into the Canvas drawing list
87        When this shape is into the canvas list, the shape could update & prepare
88        internal data asynchronously for coming rendering.
89        Canvas keeps this shape node unless user call canvas->clear() */
90     tvgDrawCmds(glCanvas.get());
91 }
92
93 void drawGLview(Evas_Object *obj)
94 {
95     auto gl = elm_glview_gl_api_get(obj);
96     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
97     gl->glClear(GL_COLOR_BUFFER_BIT);
98
99     if (glCanvas->draw() == tvg::Result::Success) {
100         glCanvas->sync();
101     }
102 }
103
104
105 /************************************************************************/
106 /* Main Code                                                            */
107 /************************************************************************/
108
109 int main(int argc, char **argv)
110 {
111     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
112
113     if (argc > 1) {
114         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
115     }
116
117     //Initialize ThorVG Engine
118     if (tvgEngine == tvg::CanvasEngine::Sw) {
119         cout << "tvg engine: software" << endl;
120     } else {
121         cout << "tvg engine: opengl" << endl;
122     }
123
124     //Threads Count
125     auto threads = std::thread::hardware_concurrency();
126
127     //Initialize ThorVG Engine
128     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
129
130         elm_init(argc, argv);
131
132         if (tvgEngine == tvg::CanvasEngine::Sw) {
133             createSwView();
134         } else {
135             createGlView();
136         }
137
138         elm_run();
139         elm_shutdown();
140
141         //Terminate ThorVG Engine
142         tvg::Initializer::term(tvgEngine);
143
144     } else {
145         cout << "engine is not supported" << endl;
146     }
147     return 0;
148 }