updated copyright.
[platform/core/graphics/tizenvg.git] / src / examples / PictureTvg.cpp
1 /*
2  * Copyright (c) 2021 - 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
30 void tvgDrawCmds(tvg::Canvas* canvas)
31 {
32     if (!canvas) return;
33
34     //load the tvg file
35     auto picture = tvg::Picture::gen();
36     if (picture->load(EXAMPLE_DIR"/test.tvg") != tvg::Result::Success) {
37         cout << "TVG is not supported. Did you enable TVG Loader?" << endl;
38         return;
39     }
40
41     float w, h;
42     picture->size(&w, &h);
43     cout << "default tvg view size = " << w << " x " << h << endl;
44
45     picture->translate(w * 0.1f, h * 0.1f);
46     picture->size(w * 0.8f, h * 0.8f);
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
134     //Initialize ThorVG Engine
135     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
136
137         elm_init(argc, argv);
138
139         if (tvgEngine == tvg::CanvasEngine::Sw) {
140             createSwView();
141         } else {
142             createGlView();
143         }
144
145         elm_run();
146         elm_shutdown();
147
148         //Terminate ThorVG Engine
149         tvg::Initializer::term(tvgEngine);
150
151     } else {
152         cout << "engine is not supported" << endl;
153     }
154     return 0;
155 }