updated copyright date.
[platform/core/graphics/tizenvg.git] / src / examples / LinearGradient.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     canvas->reserve(3);                          //reserve 3 shape nodes (optional)
34
35     //Prepare Round Rectangle
36     auto shape1 = tvg::Shape::gen();
37     shape1->appendRect(0, 0, 400, 400, 0, 0);    //x, y, w, h, rx, ry
38
39     //LinearGradient
40     auto fill = tvg::LinearGradient::gen();
41     fill->linear(0, 0, 400, 400);
42
43     //Gradient Color Stops
44     tvg::Fill::ColorStop colorStops[2];
45     colorStops[0] = {0, 0, 0, 0, 255};
46     colorStops[1] = {1, 255, 255, 255, 255};
47
48     fill->colorStops(colorStops, 2);
49
50     shape1->fill(move(fill));
51     if (canvas->push(move(shape1)) != tvg::Result::Success) return;
52
53     //Prepare Circle
54     auto shape2 = tvg::Shape::gen();
55     shape2->appendCircle(400, 400, 200, 200);    //cx, cy, radiusW, radiusH
56
57     //LinearGradient
58     auto fill2 = tvg::LinearGradient::gen();
59     fill2->linear(400, 200, 400, 600);
60
61     //Gradient Color Stops
62     tvg::Fill::ColorStop colorStops2[3];
63     colorStops2[0] = {0, 255, 0, 0, 255};
64     colorStops2[1] = {0.5, 255, 255, 0, 255};
65     colorStops2[2] = {1, 255, 255, 255, 255};
66
67     fill2->colorStops(colorStops2, 3);
68
69     shape2->fill(move(fill2));
70     if (canvas->push(move(shape2)) != tvg::Result::Success) return;
71
72
73     //Prepare Ellipse
74     auto shape3 = tvg::Shape::gen();
75     shape3->appendCircle(600, 600, 150, 100);    //cx, cy, radiusW, radiusH
76
77     //LinearGradient
78     auto fill3 = tvg::LinearGradient::gen();
79     fill3->linear(450, 600, 750, 600);
80
81     //Gradient Color Stops
82     tvg::Fill::ColorStop colorStops3[4];
83     colorStops3[0] = {0, 0, 127, 0, 127};
84     colorStops3[1] = {0.25, 0, 170, 170, 170};
85     colorStops3[2] = {0.5, 200, 0, 200, 200};
86     colorStops3[3] = {1, 255, 255, 255, 255};
87
88     fill3->colorStops(colorStops3, 4);
89
90     shape3->fill(move(fill3));
91     if (canvas->push(move(shape3)) != tvg::Result::Success) return;
92 }
93
94
95 /************************************************************************/
96 /* Sw Engine Test Code                                                  */
97 /************************************************************************/
98
99 static unique_ptr<tvg::SwCanvas> swCanvas;
100
101 void tvgSwTest(uint32_t* buffer)
102 {
103     //Create a Canvas
104     swCanvas = tvg::SwCanvas::gen();
105     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
106
107     /* Push the shape into the Canvas drawing list
108        When this shape is into the canvas list, the shape could update & prepare
109        internal data asynchronously for coming rendering.
110        Canvas keeps this shape node unless user call canvas->clear() */
111     tvgDrawCmds(swCanvas.get());
112 }
113
114 void drawSwView(void* data, Eo* obj)
115 {
116     if (swCanvas->draw() == tvg::Result::Success) {
117         swCanvas->sync();
118     }
119 }
120
121
122 /************************************************************************/
123 /* GL Engine Test Code                                                  */
124 /************************************************************************/
125
126 static unique_ptr<tvg::GlCanvas> glCanvas;
127
128 void initGLview(Evas_Object *obj)
129 {
130     static constexpr auto BPP = 4;
131
132     //Create a Canvas
133     glCanvas = tvg::GlCanvas::gen();
134     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
135
136     /* Push the shape into the Canvas drawing list
137        When this shape is into the canvas list, the shape could update & prepare
138        internal data asynchronously for coming rendering.
139        Canvas keeps this shape node unless user call canvas->clear() */
140     tvgDrawCmds(glCanvas.get());
141 }
142
143 void drawGLview(Evas_Object *obj)
144 {
145     auto gl = elm_glview_gl_api_get(obj);
146     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
147     gl->glClear(GL_COLOR_BUFFER_BIT);
148
149     if (glCanvas->draw() == tvg::Result::Success) {
150         glCanvas->sync();
151     }
152 }
153
154
155 /************************************************************************/
156 /* Main Code                                                            */
157 /************************************************************************/
158
159 int main(int argc, char **argv)
160 {
161     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
162
163     if (argc > 1) {
164         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
165     }
166
167     //Initialize ThorVG Engine
168     if (tvgEngine == tvg::CanvasEngine::Sw) {
169         cout << "tvg engine: software" << endl;
170     } else {
171         cout << "tvg engine: opengl" << endl;
172     }
173
174     //Threads Count
175     auto threads = std::thread::hardware_concurrency();
176     if (threads > 0) --threads;    //Allow the designated main thread capacity
177
178     //Initialize ThorVG Engine
179     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
180
181         elm_init(argc, argv);
182
183         if (tvgEngine == tvg::CanvasEngine::Sw) {
184             createSwView();
185         } else {
186             createGlView();
187         }
188
189         elm_run();
190         elm_shutdown();
191
192         //Terminate ThorVG Engine
193         tvg::Initializer::term(tvgEngine);
194
195     } else {
196         cout << "engine is not supported" << endl;
197     }
198     return 0;
199 }