examples: move tests to src/examples
[platform/core/graphics/tizenvg.git] / src / examples / testSvg.cpp
1 #include <vector>
2 #include "testCommon.h"
3
4 /************************************************************************/
5 /* Drawing Commands                                                     */
6 /************************************************************************/
7
8 #define NUM_PER_LINE 4
9 #define SIZE 200
10
11 static int count = 0;
12
13 static std::vector<unique_ptr<tvg::Picture>> pictures;
14
15 void svgDirCallback(const char* name, const char* path, void* data)
16 {
17     auto picture = tvg::Picture::gen();
18
19     char buf[PATH_MAX];
20     sprintf(buf, "/%s/%s", path, name);
21
22     if (picture->load(buf) != tvg::Result::Success) return;
23
24     float x, y, w, h;
25     picture->viewbox(&x, &y, &w, &h);
26
27     float rate = (SIZE/(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 -= (SIZE - h) * 0.5f;
38     } else {
39          x -= (SIZE - w) * 0.5f;
40     }
41
42     picture->translate((count % NUM_PER_LINE) * SIZE - x, SIZE * (count / NUM_PER_LINE) - y);
43
44     pictures.push_back(move(picture));
45
46     cout << "SVG: " << buf << endl;
47
48     count++;
49 }
50
51 void tvgDrawCmds(tvg::Canvas* canvas)
52 {
53     if (!canvas) return;
54
55     //Background
56     auto shape = tvg::Shape::gen();
57     shape->appendRect(0, 0, WIDTH, HEIGHT, 0, 0);    //x, y, w, h, rx, ry
58     shape->fill(255, 255, 255, 255);                 //r, g, b, a
59
60     if (canvas->push(move(shape)) != tvg::Result::Success) return;
61
62     eina_file_dir_list(EXAMPLE_DIR, EINA_TRUE, svgDirCallback, canvas);
63
64     /* This showcase shows you asynchrounous loading of svg.
65        For this, pushing pictures at a certian sync time.
66        This means it earns the time to finish loading svg resources,
67        otherwise you can push pictures immediately. */
68     for (auto& paint : pictures) {
69         canvas->push(move(paint));
70     }
71
72     pictures.clear();
73 }
74
75
76 /************************************************************************/
77 /* Sw Engine Test Code                                                  */
78 /************************************************************************/
79
80 static unique_ptr<tvg::SwCanvas> swCanvas;
81
82 void tvgSwTest(uint32_t* buffer)
83 {
84     //Create a Canvas
85     swCanvas = tvg::SwCanvas::gen();
86     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
87
88     /* Push the shape into the Canvas drawing list
89        When this shape is into the canvas list, the shape could update & prepare
90        internal data asynchronously for coming rendering.
91        Canvas keeps this shape node unless user call canvas->clear() */
92     tvgDrawCmds(swCanvas.get());
93 }
94
95 void drawSwView(void* data, Eo* obj)
96 {
97     if (swCanvas->draw() == tvg::Result::Success) {
98         swCanvas->sync();
99     }
100 }
101
102
103 /************************************************************************/
104 /* GL Engine Test Code                                                  */
105 /************************************************************************/
106
107 static unique_ptr<tvg::GlCanvas> glCanvas;
108
109 void initGLview(Evas_Object *obj)
110 {
111     static constexpr auto BPP = 4;
112
113     //Create a Canvas
114     glCanvas = tvg::GlCanvas::gen();
115     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
116
117     /* Push the shape into the Canvas drawing list
118        When this shape is into the canvas list, the shape could update & prepare
119        internal data asynchronously for coming rendering.
120        Canvas keeps this shape node unless user call canvas->clear() */
121     tvgDrawCmds(glCanvas.get());
122 }
123
124 void drawGLview(Evas_Object *obj)
125 {
126     auto gl = elm_glview_gl_api_get(obj);
127     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
128     gl->glClear(GL_COLOR_BUFFER_BIT);
129
130     if (glCanvas->draw() == tvg::Result::Success) {
131         glCanvas->sync();
132     }
133 }
134
135
136 /************************************************************************/
137 /* Main Code                                                            */
138 /************************************************************************/
139
140 int main(int argc, char **argv)
141 {
142     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
143
144     if (argc > 1) {
145         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
146     }
147
148     //Initialize ThorVG Engine
149     if (tvgEngine == tvg::CanvasEngine::Sw) {
150         cout << "tvg engine: software" << endl;
151     } else {
152         cout << "tvg engine: opengl" << endl;
153     }
154
155     //Threads Count
156     auto threads = std::thread::hardware_concurrency();
157
158     //Initialize ThorVG Engine
159     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
160
161         elm_init(argc, argv);
162
163         if (tvgEngine == tvg::CanvasEngine::Sw) {
164             createSwView();
165         } else {
166             createGlView();
167         }
168
169         elm_run();
170         elm_shutdown();
171
172         //Terminate ThorVG Engine
173         tvg::Initializer::term(tvg::CanvasEngine::Sw);
174
175     } else {
176         cout << "engine is not supported" << endl;
177     }
178     return 0;
179 }