test: ++code safety
[platform/core/graphics/tizenvg.git] / test / testShape.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 a Shape (Rectangle + Rectangle + Circle + Circle)
12     auto shape1 = tvg::Shape::gen();
13     shape1->appendRect(0, 0, 200, 200, 0, 0);          //x, y, w, h, rx, ry
14     shape1->appendRect(100, 100, 300, 300, 100, 100);  //x, y, w, h, rx, ry
15     shape1->appendCircle(400, 400, 100, 100);          //cx, cy, radiusW, radiusH
16     shape1->appendCircle(400, 500, 170, 100);          //cx, cy, radiusW, radiusH
17     shape1->fill(255, 255, 0, 255);                    //r, g, b, a
18
19     canvas->push(move(shape1));
20 }
21
22
23 /************************************************************************/
24 /* Sw Engine Test Code                                                  */
25 /************************************************************************/
26
27 static unique_ptr<tvg::SwCanvas> swCanvas;
28
29 void tvgSwTest(uint32_t* buffer)
30 {
31     //Create a Canvas
32     swCanvas = tvg::SwCanvas::gen();
33     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT);
34
35     /* Push the shape into the Canvas drawing list
36        When this shape is into the canvas list, the shape could update & prepare
37        internal data asynchronously for coming rendering.
38        Canvas keeps this shape node unless user call canvas->clear() */
39     tvgDrawCmds(swCanvas.get());
40 }
41
42 void drawSwView(void* data, Eo* obj)
43 {
44     if (swCanvas->draw() == tvg::Result::Success) {
45         swCanvas->sync();
46     }
47 }
48
49
50 /************************************************************************/
51 /* GL Engine Test Code                                                  */
52 /************************************************************************/
53
54 static unique_ptr<tvg::GlCanvas> glCanvas;
55
56 void initGLview(Evas_Object *obj)
57 {
58     static constexpr auto BPP = 4;
59
60     //Create a Canvas
61     glCanvas = tvg::GlCanvas::gen();
62     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
63
64     /* Push the shape into the Canvas drawing list
65        When this shape is into the canvas list, the shape could update & prepare
66        internal data asynchronously for coming rendering.
67        Canvas keeps this shape node unless user call canvas->clear() */
68     tvgDrawCmds(glCanvas.get());
69 }
70
71 void drawGLview(Evas_Object *obj)
72 {
73     auto gl = elm_glview_gl_api_get(obj);
74     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
75     gl->glClear(GL_COLOR_BUFFER_BIT);
76
77     if (glCanvas->draw() == tvg::Result::Success) {
78         glCanvas->sync();
79     }
80 }
81
82
83 /************************************************************************/
84 /* Main Code                                                            */
85 /************************************************************************/
86
87 int main(int argc, char **argv)
88 {
89     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
90
91     if (argc > 1) {
92         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
93     }
94
95     //Initialize ThorVG Engine
96     if (tvgEngine == tvg::CanvasEngine::Sw) {
97         cout << "tvg engine: software" << endl;
98     } else {
99         cout << "tvg engine: opengl" << endl;
100     }
101
102     //Initialize ThorVG Engine
103     if (tvg::Initializer::init(tvgEngine) == tvg::Result::Success) {
104
105         elm_init(argc, argv);
106
107         if (tvgEngine == tvg::CanvasEngine::Sw) {
108             createSwView();
109         } else {
110             createGlView();
111         }
112
113         elm_run();
114         elm_shutdown();
115
116         //Terminate ThorVG Engine
117         tvg::Initializer::term(tvgEngine);
118
119     } else {
120         cout << "engine is not supported" << endl;
121     }
122     return 0;
123 }