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