2300f21780e1d0d4fd454e1eb746e1f7593963b7
[platform/core/graphics/tizenvg.git] / test / testSvg2.cpp
1 #include "testCommon.h"
2
3 /************************************************************************/
4 /* Drawing Commands                                                     */
5 /************************************************************************/
6
7 static const char* svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" stroke-linejoin=\"round\" viewBox=\"50 -100 500 500\"><path fill=\"none\" stroke=\"black\" stroke-width=\"10\" d=\"M 212,220 C 197,171 156,153 123,221 109,157 120,109  159,63.6 190,114  234,115  254,89.8 260,82.3 268,69.6 270,60.3 273,66.5 275,71.6 280,75.6 286,79.5 294,79.8 300,79.8 306,79.8 314,79.5 320,75.6 325,71.6 327,66.5 330,60.3 332,69.6 340,82.3 346,89.8 366,115  410,114  441,63.6 480,109  491,157 477,221 444,153 403,171 388,220 366,188 316,200 300,248 284,200 234,188 212,220 Z\"/></svg>";
8
9
10 void tvgDrawCmds(tvg::Canvas* canvas)
11 {
12     if (!canvas) return;
13
14     //Background
15     auto shape = tvg::Shape::gen();
16     shape->appendRect(0, 0, WIDTH, HEIGHT, 0, 0);    //x, y, w, h, rx, ry
17     shape->fill(255, 255, 255, 255);                 //r, g, b, a
18
19     if (canvas->push(move(shape)) != tvg::Result::Success) return;
20
21     auto picture = tvg::Picture::gen();
22     if (picture->load(svg, strlen(svg)) != tvg::Result::Success) return;
23
24     float x, y, w, h;
25     picture->viewbox(&x, &y, &w, &h);
26
27     float rate = (WIDTH/(w > h ? w : h));
28     picture->scale(rate);
29
30     x *= rate;
31     y *= rate;
32     w *= rate;
33     h *= rate;
34
35     //Center Align ?
36     if (w > h) {
37          y -= (WIDTH - h) * 0.5f;
38     } else {
39          x -= (WIDTH - w) * 0.5f;
40     }
41
42     picture->translate(-x, -y);
43
44     canvas->push(move(picture));
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, tvg::SwCanvas::ARGB8888);
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     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
100     gl->glClear(GL_COLOR_BUFFER_BIT);
101
102     if (glCanvas->draw() == tvg::Result::Success) {
103         glCanvas->sync();
104     }
105 }
106
107
108 /************************************************************************/
109 /* Main Code                                                            */
110 /************************************************************************/
111
112 int main(int argc, char **argv)
113 {
114     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
115
116     if (argc > 1) {
117         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
118     }
119
120     //Initialize ThorVG Engine
121     if (tvgEngine == tvg::CanvasEngine::Sw) {
122         cout << "tvg engine: software" << endl;
123     } else {
124         cout << "tvg engine: opengl" << endl;
125     }
126
127     //Initialize ThorVG Engine
128     if (tvg::Initializer::init(tvgEngine) == 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(tvg::CanvasEngine::Sw);
143
144     } else {
145         cout << "engine is not supported" << endl;
146     }
147     return 0;
148 }