a86e9b6e23901547b960abae629c212498754ef8
[platform/core/graphics/tizenvg.git] / src / examples / PathCopy.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
35     //Prepare Path Commands
36     tvg::PathCommand cmds[11];
37     cmds[0] = tvg::PathCommand::MoveTo;
38     cmds[1] = tvg::PathCommand::LineTo;
39     cmds[2] = tvg::PathCommand::LineTo;
40     cmds[3] = tvg::PathCommand::LineTo;
41     cmds[4] = tvg::PathCommand::LineTo;
42     cmds[5] = tvg::PathCommand::LineTo;
43     cmds[6] = tvg::PathCommand::LineTo;
44     cmds[7] = tvg::PathCommand::LineTo;
45     cmds[8] = tvg::PathCommand::LineTo;
46     cmds[9] = tvg::PathCommand::LineTo;
47     cmds[10] = tvg::PathCommand::Close;
48
49     //Prepare Path Points
50     tvg::Point pts[10];
51     pts[0] = {199, 34};    //MoveTo
52     pts[1] = {253, 143};   //LineTo
53     pts[2] = {374, 160};   //LineTo
54     pts[3] = {287, 244};   //LineTo
55     pts[4] = {307, 365};   //LineTo
56     pts[5] = {199, 309};   //LineTo
57     pts[6] = {97, 365};    //LineTo
58     pts[7] = {112, 245};   //LineTo
59     pts[8] = {26, 161};    //LineTo
60     pts[9] = {146, 143};   //LineTo
61
62     auto shape1 = tvg::Shape::gen();
63     shape1->appendPath(cmds, 11, pts, 10);     //copy path data
64     shape1->fill(0, 255, 0, 255);
65     if (canvas->push(move(shape1)) != tvg::Result::Success) return;
66
67     /* Circle */
68     auto cx = 550.0f;
69     auto cy = 550.0f;
70     auto radius = 125.0f;
71     auto halfRadius = radius * 0.552284f;
72
73     //Prepare Path Commands
74     tvg::PathCommand cmds2[6];
75     cmds2[0] = tvg::PathCommand::MoveTo;
76     cmds2[1] = tvg::PathCommand::CubicTo;
77     cmds2[2] = tvg::PathCommand::CubicTo;
78     cmds2[3] = tvg::PathCommand::CubicTo;
79     cmds2[4] = tvg::PathCommand::CubicTo;
80     cmds2[5] = tvg::PathCommand::Close;
81
82     //Prepare Path Points
83     tvg::Point pts2[13];
84     pts2[0] = {cx, cy - radius};    //MoveTo
85     //CubicTo 1
86     pts2[1] = {cx + halfRadius, cy - radius};      //Ctrl1
87     pts2[2] = {cx + radius, cy - halfRadius};      //Ctrl2
88     pts2[3] = {cx + radius, cy};                   //To
89     //CubicTo 2
90     pts2[4] = {cx + radius, cy + halfRadius};      //Ctrl1
91     pts2[5] = {cx + halfRadius, cy + radius};      //Ctrl2
92     pts2[6] = {cx, cy+ radius};                    //To
93     //CubicTo 3
94     pts2[7] = {cx - halfRadius, cy + radius};      //Ctrl1
95     pts2[8] = {cx - radius, cy + halfRadius};      //Ctrl2
96     pts2[9] = {cx - radius, cy};                   //To
97     //CubicTo 4
98     pts2[10] = {cx - radius, cy - halfRadius};     //Ctrl1
99     pts2[11] = {cx - halfRadius, cy - radius};     //Ctrl2
100     pts2[12] = {cx, cy - radius};                  //To
101
102     auto shape2 = tvg::Shape::gen();
103     shape2->appendPath(cmds2, 6, pts2, 13);     //copy path data
104     shape2->fill(255, 255, 0, 255);
105     if (canvas->push(move(shape2)) != tvg::Result::Success) return;
106
107 }
108
109 /************************************************************************/
110 /* Sw Engine Test Code                                                  */
111 /************************************************************************/
112
113 static unique_ptr<tvg::SwCanvas> swCanvas;
114
115 void tvgSwTest(uint32_t* buffer)
116 {
117     //Create a Canvas
118     swCanvas = tvg::SwCanvas::gen();
119     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
120
121     /* Push the shape into the Canvas drawing list
122        When this shape is into the canvas list, the shape could update & prepare
123        internal data asynchronously for coming rendering.
124        Canvas keeps this shape node unless user call canvas->clear() */
125     tvgDrawCmds(swCanvas.get());
126 }
127
128 void drawSwView(void* data, Eo* obj)
129 {
130     if (swCanvas->draw() == tvg::Result::Success) {
131         swCanvas->sync();
132     }
133 }
134
135
136 /************************************************************************/
137 /* GL Engine Test Code                                                  */
138 /************************************************************************/
139
140 static unique_ptr<tvg::GlCanvas> glCanvas;
141
142 void initGLview(Evas_Object *obj)
143 {
144     static constexpr auto BPP = 4;
145
146     //Create a Canvas
147     glCanvas = tvg::GlCanvas::gen();
148     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
149
150     /* Push the shape into the Canvas drawing list
151        When this shape is into the canvas list, the shape could update & prepare
152        internal data asynchronously for coming rendering.
153        Canvas keeps this shape node unless user call canvas->clear() */
154     tvgDrawCmds(glCanvas.get());
155 }
156
157 void drawGLview(Evas_Object *obj)
158 {
159     auto gl = elm_glview_gl_api_get(obj);
160     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
161     gl->glClear(GL_COLOR_BUFFER_BIT);
162
163     if (glCanvas->draw() == tvg::Result::Success) {
164         glCanvas->sync();
165     }
166 }
167
168
169 /************************************************************************/
170 /* Main Code                                                            */
171 /************************************************************************/
172
173 int main(int argc, char **argv)
174 {
175     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
176
177     if (argc > 1) {
178         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
179     }
180
181     //Initialize ThorVG Engine
182     if (tvgEngine == tvg::CanvasEngine::Sw) {
183         cout << "tvg engine: software" << endl;
184     } else {
185         cout << "tvg engine: opengl" << endl;
186     }
187
188     //Threads Count
189     auto threads = std::thread::hardware_concurrency();
190     if (threads > 0) --threads;    //Allow the designated main thread capacity
191
192     //Initialize ThorVG Engine
193     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
194
195         elm_init(argc, argv);
196
197         if (tvgEngine == tvg::CanvasEngine::Sw) {
198             createSwView();
199         } else {
200             createGlView();
201         }
202
203         elm_run();
204         elm_shutdown();
205
206         //Terminate ThorVG Engine
207         tvg::Initializer::term(tvgEngine);
208
209     } else {
210         cout << "engine is not supported" << endl;
211     }
212     return 0;
213 }