example: Modify the number of svg image examples
[platform/core/graphics/tizenvg.git] / src / examples / Svg.cpp
1 /*
2  * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved.
3
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #include <vector>
24 #include "Common.h"
25
26 /************************************************************************/
27 /* Drawing Commands                                                     */
28 /************************************************************************/
29
30 #define NUM_PER_ROW 7
31 #define NUM_PER_COL 6
32 #define SIZE (WIDTH/NUM_PER_ROW)
33
34 static int count = 0;
35
36 static std::vector<unique_ptr<tvg::Picture>> pictures;
37
38 void svgDirCallback(const char* name, const char* path, void* data)
39 {
40     //ignore if not svgs.
41     const char *ext = name + strlen(name) - 3;
42     if (strcmp(ext, "svg")) return;
43
44     auto picture = tvg::Picture::gen();
45
46     char buf[PATH_MAX];
47     snprintf(buf, sizeof(buf), "/%s/%s", path, name);
48
49     if (picture->load(buf) != tvg::Result::Success) return;
50
51     picture->size(SIZE, SIZE);
52     picture->translate((count % NUM_PER_ROW) * SIZE, (count / NUM_PER_ROW) * (HEIGHT / NUM_PER_COL));
53
54     pictures.push_back(move(picture));
55
56     cout << "SVG: " << buf << endl;
57
58     count++;
59 }
60
61 void tvgDrawCmds(tvg::Canvas* canvas)
62 {
63     if (!canvas) return;
64
65     //Background
66     auto shape = tvg::Shape::gen();
67     shape->appendRect(0, 0, WIDTH, HEIGHT, 0, 0);    //x, y, w, h, rx, ry
68     shape->fill(255, 255, 255, 255);                 //r, g, b, a
69
70     if (canvas->push(move(shape)) != tvg::Result::Success) return;
71
72     eina_file_dir_list(EXAMPLE_DIR, EINA_TRUE, svgDirCallback, canvas);
73
74     /* This showcase shows you asynchrounous loading of svg.
75        For this, pushing pictures at a certian sync time.
76        This means it earns the time to finish loading svg resources,
77        otherwise you can push pictures immediately. */
78     for (auto& paint : pictures) {
79         canvas->push(move(paint));
80     }
81
82     pictures.clear();
83 }
84
85
86 /************************************************************************/
87 /* Sw Engine Test Code                                                  */
88 /************************************************************************/
89
90 static unique_ptr<tvg::SwCanvas> swCanvas;
91
92 void tvgSwTest(uint32_t* buffer)
93 {
94     //Create a Canvas
95     swCanvas = tvg::SwCanvas::gen();
96     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
97
98     /* Push the shape into the Canvas drawing list
99        When this shape is into the canvas list, the shape could update & prepare
100        internal data asynchronously for coming rendering.
101        Canvas keeps this shape node unless user call canvas->clear() */
102     tvgDrawCmds(swCanvas.get());
103 }
104
105 void drawSwView(void* data, Eo* obj)
106 {
107     if (swCanvas->draw() == tvg::Result::Success) {
108         swCanvas->sync();
109     }
110 }
111
112
113 /************************************************************************/
114 /* GL Engine Test Code                                                  */
115 /************************************************************************/
116
117 static unique_ptr<tvg::GlCanvas> glCanvas;
118
119 void initGLview(Evas_Object *obj)
120 {
121     static constexpr auto BPP = 4;
122
123     //Create a Canvas
124     glCanvas = tvg::GlCanvas::gen();
125     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
126
127     /* Push the shape into the Canvas drawing list
128        When this shape is into the canvas list, the shape could update & prepare
129        internal data asynchronously for coming rendering.
130        Canvas keeps this shape node unless user call canvas->clear() */
131     tvgDrawCmds(glCanvas.get());
132 }
133
134 void drawGLview(Evas_Object *obj)
135 {
136     auto gl = elm_glview_gl_api_get(obj);
137     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
138     gl->glClear(GL_COLOR_BUFFER_BIT);
139
140     if (glCanvas->draw() == tvg::Result::Success) {
141         glCanvas->sync();
142     }
143 }
144
145
146 /************************************************************************/
147 /* Main Code                                                            */
148 /************************************************************************/
149
150 int main(int argc, char **argv)
151 {
152     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
153
154     if (argc > 1) {
155         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
156     }
157
158     //Initialize ThorVG Engine
159     if (tvgEngine == tvg::CanvasEngine::Sw) {
160         cout << "tvg engine: software" << endl;
161     } else {
162         cout << "tvg engine: opengl" << endl;
163     }
164
165     //Threads Count
166     auto threads = std::thread::hardware_concurrency();
167     if (threads > 0) --threads;    //Allow the designated main thread capacity
168
169     //Initialize ThorVG Engine
170     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
171
172         elm_init(argc, argv);
173
174         if (tvgEngine == tvg::CanvasEngine::Sw) {
175             createSwView();
176         } else {
177             createGlView();
178         }
179
180         elm_run();
181         elm_shutdown();
182
183         //Terminate ThorVG Engine
184         tvg::Initializer::term(tvg::CanvasEngine::Sw);
185
186     } else {
187         cout << "engine is not supported" << endl;
188     }
189     return 0;
190 }