678816a80de7de607f75c69af43a8265f65d13ca
[platform/core/graphics/tizenvg.git] / src / examples / CustomTransform.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 tvgUpdateCmds(tvg::Canvas* canvas, float progress)
30 {
31     if (!canvas) return;
32
33     if (canvas->clear() != tvg::Result::Success) return;
34
35     //Shape
36     auto shape = tvg::Shape::gen();
37
38     shape->moveTo(0, -114.5);
39     shape->lineTo(54, -5.5);
40     shape->lineTo(175, 11.5);
41     shape->lineTo(88, 95.5);
42     shape->lineTo(108, 216.5);
43     shape->lineTo(0, 160.5);
44     shape->lineTo(-102, 216.5);
45     shape->lineTo(-87, 96.5);
46     shape->lineTo(-173, 12.5);
47     shape->lineTo(-53, -5.5);
48     shape->close();
49     shape->fill(0, 0, 255, 255);
50     shape->stroke(3);
51     shape->stroke(255, 255, 255, 255);
52
53     //Transform Matrix
54     tvg::Matrix m = {1, 0, 0, 0, 1, 0, 0, 0, 1};
55
56     //scale x
57     m.e11 = 1 - (progress * 0.5f);
58
59     //scale y
60     m.e22 = 1 + (progress * 2.0f);
61
62     //rotation
63     constexpr auto PI = 3.141592f;
64     auto degree = 45.0f;
65     auto radian = degree / 180.0f * PI;
66     auto cosVal = cosf(radian);
67     auto sinVal = sinf(radian);
68
69     auto t11 = m.e11 * cosVal + m.e12 * sinVal;
70     auto t12 = m.e11 * -sinVal + m.e12 * cosVal;
71     auto t21 = m.e21 * cosVal + m.e22 * sinVal;
72     auto t22 = m.e21 * -sinVal + m.e22 * cosVal;
73     auto t13 = m.e31 * cosVal + m.e32 * sinVal;
74     auto t23 = m.e31 * -sinVal + m.e32 * cosVal;
75
76     m.e11 = t11;
77     m.e12 = t12;
78     m.e21 = t21;
79     m.e22 = t22;
80     m.e13 = t13;
81     m.e23 = t23;
82
83     //translate
84     m.e13 = progress * 300.0f + 300.0f;
85     m.e23 = progress * -100.0f + 300.0f;
86
87     shape->transform(m);
88
89     canvas->push(move(shape));
90 }
91
92
93 /************************************************************************/
94 /* Sw Engine Test Code                                                  */
95 /************************************************************************/
96
97 static unique_ptr<tvg::SwCanvas> swCanvas;
98
99 void tvgSwTest(uint32_t* buffer)
100 {
101     //Create a Canvas
102     swCanvas = tvg::SwCanvas::gen();
103     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
104
105     /* Push the shape into the Canvas drawing list
106        When this shape is into the canvas list, the shape could update & prepare
107        internal data asynchronously for coming rendering.
108        Canvas keeps this shape node unless user call canvas->clear() */
109     tvgUpdateCmds(swCanvas.get(), 0);
110 }
111
112 void transitSwCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
113 {
114     tvgUpdateCmds(swCanvas.get(), progress);
115
116     //Update Efl Canvas
117     Eo* img = (Eo*) effect;
118     evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
119     evas_object_image_pixels_dirty_set(img, EINA_TRUE);
120 }
121
122 void drawSwView(void* data, Eo* obj)
123 {
124     if (swCanvas->draw() == tvg::Result::Success) {
125         swCanvas->sync();
126     }
127 }
128
129
130 /************************************************************************/
131 /* GL Engine Test Code                                                  */
132 /************************************************************************/
133
134 static unique_ptr<tvg::GlCanvas> glCanvas;
135
136 void initGLview(Evas_Object *obj)
137 {
138     static constexpr auto BPP = 4;
139
140     //Create a Canvas
141     glCanvas = tvg::GlCanvas::gen();
142     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
143
144     /* Push the shape into the Canvas drawing list
145        When this shape is into the canvas list, the shape could update & prepare
146        internal data asynchronously for coming rendering.
147        Canvas keeps this shape node unless user call canvas->clear() */
148     tvgUpdateCmds(glCanvas.get(), 0);
149 }
150
151 void drawGLview(Evas_Object *obj)
152 {
153     auto gl = elm_glview_gl_api_get(obj);
154     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
155     gl->glClear(GL_COLOR_BUFFER_BIT);
156
157     if (glCanvas->draw() == tvg::Result::Success) {
158         glCanvas->sync();
159     }
160 }
161
162 void transitGlCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
163 {
164     tvgUpdateCmds(glCanvas.get(), progress);
165     elm_glview_changed_set((Evas_Object*)effect);
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         Elm_Transit *transit = elm_transit_add();
198
199         if (tvgEngine == tvg::CanvasEngine::Sw) {
200             auto view = createSwView();
201             elm_transit_effect_add(transit, transitSwCb, view, nullptr);
202         } else {
203             auto view = createGlView();
204             elm_transit_effect_add(transit, transitGlCb, view, nullptr);
205         }
206
207         elm_transit_duration_set(transit, 2);
208         elm_transit_repeat_times_set(transit, -1);
209         elm_transit_auto_reverse_set(transit, EINA_TRUE);
210         elm_transit_go(transit);
211
212         elm_run();
213         elm_shutdown();
214
215         //Terminate ThorVG Engine
216         tvg::Initializer::term(tvgEngine);
217
218     } else {
219         cout << "engine is not supported" << endl;
220     }
221     return 0;
222 }