b0a0a68832803964f04e1ef59447ab6f33f76ad4
[platform/core/graphics/tizenvg.git] / src / examples / Svg2.cpp
1 /*
2  * Copyright (c) 2020 - 2023 the ThorVG project. 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 "Common.h"
24
25 /************************************************************************/
26 /* Drawing Commands                                                     */
27 /************************************************************************/
28
29 static const char* svg = "<svg height=\"1000\" viewBox=\"0 0 1000 1000\" width=\"1000\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M.10681413.09784845 1000.0527.01592069V1000.0851L.06005738 999.9983Z\" fill=\"#ffffff\" stroke-width=\"3.910218\"/><g fill=\"#252f35\"><g stroke-width=\"3.864492\"><path d=\"M256.61221 100.51736H752.8963V386.99554H256.61221Z\"/><path d=\"M201.875 100.51736H238.366478V386.99554H201.875Z\"/><path d=\"M771.14203 100.51736H807.633508V386.99554H771.14203Z\"/></g><path d=\"M420.82388 380H588.68467V422.805317H420.82388Z\" stroke-width=\"3.227\"/><path d=\"m420.82403 440.7101v63.94623l167.86079 25.5782V440.7101Z\"/><path d=\"M420.82403 523.07258V673.47362L588.68482 612.59701V548.13942Z\"/></g><g fill=\"#222f35\"><path d=\"M420.82403 691.37851 588.68482 630.5019 589 834H421Z\"/><path d=\"m420.82403 852.52249h167.86079v28.64782H420.82403v-28.64782 0 0\"/><path d=\"m439.06977 879.17031c0 0-14.90282 8.49429-18.24574 15.8161-4.3792 9.59153 0 31.63185 0 31.63185h167.86079c0 0 4.3792-22.04032 0-31.63185-3.34292-7.32181-18.24574-15.8161-18.24574-15.8161z\"/></g><g fill=\"#ffffff\"><path d=\"m280 140h15v55l8 10 8-10v-55h15v60l-23 25-23-25z\"/><path d=\"m335 140v80h45v-50h-25v10h10v30h-15v-57h18v-13z\"/></g></svg>";
30
31
32 void tvgDrawCmds(tvg::Canvas* canvas)
33 {
34     if (!canvas) return;
35
36     //Background
37     auto shape = tvg::Shape::gen();
38     shape->appendRect(0, 0, WIDTH, HEIGHT, 0, 0);    //x, y, w, h, rx, ry
39     shape->fill(255, 255, 255, 255);                 //r, g, b, a
40
41     if (canvas->push(move(shape)) != tvg::Result::Success) return;
42
43     auto picture = tvg::Picture::gen();
44     if (picture->load(svg, strlen(svg), "svg") != tvg::Result::Success) return;
45
46     picture->size(WIDTH, HEIGHT);
47
48     canvas->push(move(picture));
49 }
50
51
52 /************************************************************************/
53 /* Sw Engine Test Code                                                  */
54 /************************************************************************/
55
56 static unique_ptr<tvg::SwCanvas> swCanvas;
57
58 void tvgSwTest(uint32_t* buffer)
59 {
60     //Create a Canvas
61     swCanvas = tvg::SwCanvas::gen();
62     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
63
64     /* Push the shape into the Canvas drawing list
65        When this shape is into the canvas list, the shape could update & prepare
66        internal data asynchronously for coming rendering.
67        Canvas keeps this shape node unless user call canvas->clear() */
68     tvgDrawCmds(swCanvas.get());
69 }
70
71 void drawSwView(void* data, Eo* obj)
72 {
73     if (swCanvas->draw() == tvg::Result::Success) {
74         swCanvas->sync();
75     }
76 }
77
78
79 /************************************************************************/
80 /* GL Engine Test Code                                                  */
81 /************************************************************************/
82
83 static unique_ptr<tvg::GlCanvas> glCanvas;
84
85 void initGLview(Evas_Object *obj)
86 {
87     static constexpr auto BPP = 4;
88
89     //Create a Canvas
90     glCanvas = tvg::GlCanvas::gen();
91     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
92
93     /* Push the shape into the Canvas drawing list
94        When this shape is into the canvas list, the shape could update & prepare
95        internal data asynchronously for coming rendering.
96        Canvas keeps this shape node unless user call canvas->clear() */
97     tvgDrawCmds(glCanvas.get());
98 }
99
100 void drawGLview(Evas_Object *obj)
101 {
102     auto gl = elm_glview_gl_api_get(obj);
103     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
104     gl->glClear(GL_COLOR_BUFFER_BIT);
105
106     if (glCanvas->draw() == tvg::Result::Success) {
107         glCanvas->sync();
108     }
109 }
110
111
112 /************************************************************************/
113 /* Main Code                                                            */
114 /************************************************************************/
115
116 int main(int argc, char **argv)
117 {
118     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
119
120     if (argc > 1) {
121         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
122     }
123
124     //Initialize ThorVG Engine
125     if (tvgEngine == tvg::CanvasEngine::Sw) {
126         cout << "tvg engine: software" << endl;
127     } else {
128         cout << "tvg engine: opengl" << endl;
129     }
130
131     //Threads Count
132     auto threads = std::thread::hardware_concurrency();
133     if (threads > 0) --threads;    //Allow the designated main thread capacity
134
135     //Initialize ThorVG Engine
136     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
137
138         elm_init(argc, argv);
139
140         if (tvgEngine == tvg::CanvasEngine::Sw) {
141             createSwView();
142         } else {
143             createGlView();
144         }
145
146         elm_run();
147         elm_shutdown();
148
149         //Terminate ThorVG Engine
150         tvg::Initializer::term(tvgEngine);
151
152     } else {
153         cout << "engine is not supported" << endl;
154     }
155     return 0;
156 }