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