2 * Copyright (c) 2020-2021 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 /************************************************************************/
30 #define NUM_PER_LINE 6
31 #define SIZE (WIDTH/NUM_PER_LINE)
35 static std::vector<unique_ptr<tvg::Picture>> pictures;
37 void svgDirCallback(const char* name, const char* path, void* data)
40 const char *ext = name + strlen(name) - 3;
41 if (strcmp(ext, "svg")) return;
43 auto picture = tvg::Picture::gen();
46 snprintf(buf, sizeof(buf), "/%s/%s", path, name);
48 if (picture->load(buf) != tvg::Result::Success) return;
50 picture->size(SIZE, SIZE);
51 picture->translate((count % NUM_PER_LINE) * SIZE, SIZE * (count / NUM_PER_LINE));
53 pictures.push_back(move(picture));
55 cout << "SVG: " << buf << endl;
60 void tvgDrawCmds(tvg::Canvas* canvas)
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
69 if (canvas->push(move(shape)) != tvg::Result::Success) return;
71 eina_file_dir_list(EXAMPLE_DIR, EINA_TRUE, svgDirCallback, canvas);
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));
85 /************************************************************************/
86 /* Sw Engine Test Code */
87 /************************************************************************/
89 static unique_ptr<tvg::SwCanvas> swCanvas;
91 void tvgSwTest(uint32_t* buffer)
94 swCanvas = tvg::SwCanvas::gen();
95 swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
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());
104 void drawSwView(void* data, Eo* obj)
106 if (swCanvas->draw() == tvg::Result::Success) {
112 /************************************************************************/
113 /* GL Engine Test Code */
114 /************************************************************************/
116 static unique_ptr<tvg::GlCanvas> glCanvas;
118 void initGLview(Evas_Object *obj)
120 static constexpr auto BPP = 4;
123 glCanvas = tvg::GlCanvas::gen();
124 glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
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());
133 void drawGLview(Evas_Object *obj)
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);
139 if (glCanvas->draw() == tvg::Result::Success) {
145 /************************************************************************/
147 /************************************************************************/
149 int main(int argc, char **argv)
151 tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
154 if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
157 //Initialize ThorVG Engine
158 if (tvgEngine == tvg::CanvasEngine::Sw) {
159 cout << "tvg engine: software" << endl;
161 cout << "tvg engine: opengl" << endl;
165 auto threads = std::thread::hardware_concurrency();
166 if (threads > 0) --threads; //Allow the designated main thread capacity
168 //Initialize ThorVG Engine
169 if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
171 elm_init(argc, argv);
173 if (tvgEngine == tvg::CanvasEngine::Sw) {
182 //Terminate ThorVG Engine
183 tvg::Initializer::term(tvg::CanvasEngine::Sw);
186 cout << "engine is not supported" << endl;