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