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