de6ef60ea946c9381db9959a1b9b853dfd4cf22e
[platform/core/graphics/tizenvg.git] / test / testPath.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     //Star
12     auto shape1 = tvg::Shape::gen();
13
14     //Appends Paths
15     shape1->moveTo(199, 34);
16     shape1->lineTo(253, 143);
17     shape1->lineTo(374, 160);
18     shape1->lineTo(287, 244);
19     shape1->lineTo(307, 365);
20     shape1->lineTo(199, 309);
21     shape1->lineTo(97, 365);
22     shape1->lineTo(112, 245);
23     shape1->lineTo(26, 161);
24     shape1->lineTo(146, 143);
25     shape1->close();
26     shape1->fill(0, 0, 255, 255);
27     if (canvas->push(move(shape1)) != tvg::Result::Success) return;
28
29
30     //Circle
31     auto shape2 = tvg::Shape::gen();
32
33     auto cx = 550.0f;
34     auto cy = 550.0f;
35     auto radius = 125.0f;
36     auto halfRadius = radius * 0.552284f;
37
38     //Append Paths
39     shape2->moveTo(cx, cy - radius);
40     shape2->cubicTo(cx + halfRadius, cy - radius, cx + radius, cy - halfRadius, cx + radius, cy);
41     shape2->cubicTo(cx + radius, cy + halfRadius, cx + halfRadius, cy + radius, cx, cy+ radius);
42     shape2->cubicTo(cx - halfRadius, cy + radius, cx - radius, cy + halfRadius, cx - radius, cy);
43     shape2->cubicTo(cx - radius, cy - halfRadius, cx - halfRadius, cy - radius, cx, cy - radius);
44     shape2->close();
45     shape2->fill(255, 0, 0, 255);
46     if (canvas->push(move(shape2)) != tvg::Result::Success) return;
47
48 }
49
50 /************************************************************************/
51 /* Sw Engine Test Code                                                  */
52 /************************************************************************/
53
54 static unique_ptr<tvg::SwCanvas> swCanvas;
55
56 void tvgSwTest(uint32_t* buffer)
57 {
58     //Create a Canvas
59     swCanvas = tvg::SwCanvas::gen();
60     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
61
62     /* Push the shape into the Canvas drawing list
63        When this shape is into the canvas list, the shape could update & prepare
64        internal data asynchronously for coming rendering.
65        Canvas keeps this shape node unless user call canvas->clear() */
66     tvgDrawCmds(swCanvas.get());
67 }
68
69 void drawSwView(void* data, Eo* obj)
70 {
71     if (swCanvas->draw() == tvg::Result::Success) {
72         swCanvas->sync();
73     }
74 }
75
76
77 /************************************************************************/
78 /* GL Engine Test Code                                                  */
79 /************************************************************************/
80
81 static unique_ptr<tvg::GlCanvas> glCanvas;
82
83 void initGLview(Evas_Object *obj)
84 {
85     static constexpr auto BPP = 4;
86
87     //Create a Canvas
88     glCanvas = tvg::GlCanvas::gen();
89     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
90
91     /* Push the shape into the Canvas drawing list
92        When this shape is into the canvas list, the shape could update & prepare
93        internal data asynchronously for coming rendering.
94        Canvas keeps this shape node unless user call canvas->clear() */
95     tvgDrawCmds(glCanvas.get());
96 }
97
98 void drawGLview(Evas_Object *obj)
99 {
100     auto gl = elm_glview_gl_api_get(obj);
101     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
102     gl->glClear(GL_COLOR_BUFFER_BIT);
103
104     if (glCanvas->draw() == tvg::Result::Success) {
105         glCanvas->sync();
106     }
107 }
108
109
110 /************************************************************************/
111 /* Main Code                                                            */
112 /************************************************************************/
113
114 int main(int argc, char **argv)
115 {
116     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
117
118     if (argc > 1) {
119         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
120     }
121
122     //Initialize ThorVG Engine
123     if (tvgEngine == tvg::CanvasEngine::Sw) {
124         cout << "tvg engine: software" << endl;
125     } else {
126         cout << "tvg engine: opengl" << endl;
127     }
128
129     //Initialize ThorVG Engine
130     if (tvg::Initializer::init(tvgEngine) == tvg::Result::Success) {
131
132         elm_init(argc, argv);
133
134         if (tvgEngine == tvg::CanvasEngine::Sw) {
135             createSwView();
136         } else {
137             createGlView();
138         }
139
140         elm_run();
141         elm_shutdown();
142
143         //Terminate ThorVG Engine
144         tvg::Initializer::term(tvgEngine);
145
146     } else {
147         cout << "engine is not supported" << endl;
148     }
149     return 0;
150 }