2 * Copyright (c) 2020 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
26 /************************************************************************/
27 /* Drawing Commands */
28 /************************************************************************/
32 #define SIZE (WIDTH/NUM_PER_ROW)
36 static std::vector<unique_ptr<tvg::Picture>> pictures;
38 void svgDirCallback(const char* name, const char* path, void* data)
41 const char *ext = name + strlen(name) - 3;
42 if (strcmp(ext, "svg")) return;
44 auto picture = tvg::Picture::gen();
47 snprintf(buf, sizeof(buf), "/%s/%s", path, name);
49 if (picture->load(buf) != tvg::Result::Success) return;
51 picture->size(SIZE, SIZE);
52 picture->translate((count % NUM_PER_ROW) * SIZE, (count / NUM_PER_ROW) * (HEIGHT / NUM_PER_COL));
54 pictures.push_back(move(picture));
56 cout << "SVG: " << buf << endl;
61 void tvgDrawCmds(tvg::Canvas* canvas)
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
70 if (canvas->push(move(shape)) != tvg::Result::Success) return;
72 eina_file_dir_list(EXAMPLE_DIR, EINA_TRUE, svgDirCallback, canvas);
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));
86 /************************************************************************/
87 /* Sw Engine Test Code */
88 /************************************************************************/
90 static unique_ptr<tvg::SwCanvas> swCanvas;
92 void tvgSwTest(uint32_t* buffer)
95 swCanvas = tvg::SwCanvas::gen();
96 swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
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());
105 void drawSwView(void* data, Eo* obj)
107 if (swCanvas->draw() == tvg::Result::Success) {
113 /************************************************************************/
114 /* GL Engine Test Code */
115 /************************************************************************/
117 static unique_ptr<tvg::GlCanvas> glCanvas;
119 void initGLview(Evas_Object *obj)
121 static constexpr auto BPP = 4;
124 glCanvas = tvg::GlCanvas::gen();
125 glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
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());
134 void drawGLview(Evas_Object *obj)
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);
140 if (glCanvas->draw() == tvg::Result::Success) {
146 /************************************************************************/
148 /************************************************************************/
150 int main(int argc, char **argv)
152 tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
155 if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
158 //Initialize ThorVG Engine
159 if (tvgEngine == tvg::CanvasEngine::Sw) {
160 cout << "tvg engine: software" << endl;
162 cout << "tvg engine: opengl" << endl;
166 auto threads = std::thread::hardware_concurrency();
167 if (threads > 0) --threads; //Allow the designated main thread capacity
169 //Initialize ThorVG Engine
170 if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
172 elm_init(argc, argv);
174 if (tvgEngine == tvg::CanvasEngine::Sw) {
183 //Terminate ThorVG Engine
184 tvg::Initializer::term(tvgEngine);
187 cout << "engine is not supported" << endl;