b2e0730f0200dd95568d5107360d67fe296e5d57
[platform/core/graphics/tizenvg.git] / src / examples / FillRule.cpp
1 /*
2  * Copyright (c) 2020 - 2022 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 "Common.h"
24
25 /************************************************************************/
26 /* Drawing Commands                                                     */
27 /************************************************************************/
28
29 void tvgDrawCmds(tvg::Canvas* canvas)
30 {
31     if (!canvas) return;
32
33     //Star
34     auto shape1 = tvg::Shape::gen();
35     shape1->moveTo(205, 35);
36     shape1->lineTo(330, 355);
37     shape1->lineTo(25, 150);
38     shape1->lineTo(385, 150);
39     shape1->lineTo(80, 355);
40     shape1->close();
41     shape1->fill(255, 255, 255, 255);
42     shape1->fill(tvg::FillRule::Winding);  //Fill all winding shapes
43
44     if (canvas->push(move(shape1)) != tvg::Result::Success) return;
45
46     //Star 2
47     auto shape2 = tvg::Shape::gen();
48     shape2->moveTo(535, 335);
49     shape2->lineTo(660, 655);
50     shape2->lineTo(355, 450);
51     shape2->lineTo(715, 450);
52     shape2->lineTo(410, 655);
53     shape2->close();
54     shape2->fill(255, 255, 255, 255);
55     shape2->fill(tvg::FillRule::EvenOdd); //Fill polygons with even odd pattern
56
57     if (canvas->push(move(shape2)) != tvg::Result::Success) return;
58 }
59
60 /************************************************************************/
61 /* Sw Engine Test Code                                                  */
62 /************************************************************************/
63
64 static unique_ptr<tvg::SwCanvas> swCanvas;
65
66 void tvgSwTest(uint32_t* buffer)
67 {
68     //Create a Canvas
69     swCanvas = tvg::SwCanvas::gen();
70     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
71
72     /* Push the shape into the Canvas drawing list
73        When this shape is into the canvas list, the shape could update & prepare
74        internal data asynchronously for coming rendering.
75        Canvas keeps this shape node unless user call canvas->clear() */
76     tvgDrawCmds(swCanvas.get());
77 }
78
79 void drawSwView(void* data, Eo* obj)
80 {
81     if (swCanvas->draw() == tvg::Result::Success) {
82         swCanvas->sync();
83     }
84 }
85
86
87 /************************************************************************/
88 /* GL Engine Test Code                                                  */
89 /************************************************************************/
90
91 static unique_ptr<tvg::GlCanvas> glCanvas;
92
93 void initGLview(Evas_Object *obj)
94 {
95     static constexpr auto BPP = 4;
96
97     //Create a Canvas
98     glCanvas = tvg::GlCanvas::gen();
99     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
100
101     /* Push the shape into the Canvas drawing list
102        When this shape is into the canvas list, the shape could update & prepare
103        internal data asynchronously for coming rendering.
104        Canvas keeps this shape node unless user call canvas->clear() */
105     tvgDrawCmds(glCanvas.get());
106 }
107
108 void drawGLview(Evas_Object *obj)
109 {
110     auto gl = elm_glview_gl_api_get(obj);
111     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
112     gl->glClear(GL_COLOR_BUFFER_BIT);
113
114     if (glCanvas->draw() == tvg::Result::Success) {
115         glCanvas->sync();
116     }
117 }
118
119
120 /************************************************************************/
121 /* Main Code                                                            */
122 /************************************************************************/
123
124 int main(int argc, char **argv)
125 {
126     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
127
128     if (argc > 1) {
129         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
130     }
131
132     //Initialize ThorVG Engine
133     if (tvgEngine == tvg::CanvasEngine::Sw) {
134         cout << "tvg engine: software" << endl;
135     } else {
136         cout << "tvg engine: opengl" << endl;
137     }
138
139     //Threads Count
140     auto threads = std::thread::hardware_concurrency();
141     if (threads > 0) --threads;    //Allow the designated main thread capacity
142
143     //Initialize ThorVG Engine
144     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
145
146         elm_init(argc, argv);
147
148         if (tvgEngine == tvg::CanvasEngine::Sw) {
149             createSwView();
150         } else {
151             createGlView();
152         }
153
154         elm_run();
155         elm_shutdown();
156
157         //Terminate ThorVG Engine
158         tvg::Initializer::term(tvgEngine);
159
160     } else {
161         cout << "engine is not supported" << endl;
162     }
163     return 0;
164 }