Merge "SvgLoader: Inherit parent opacity" into tizen
[platform/core/graphics/tizenvg.git] / test / testSceneTransform.cpp
1 #include "testCommon.h"
2
3 /************************************************************************/
4 /* Drawing Commands                                                     */
5 /************************************************************************/
6 tvg::Scene* pScene1 = nullptr;
7 tvg::Scene* pScene2 = nullptr;
8
9 void tvgDrawCmds(tvg::Canvas* canvas)
10 {
11     //Create a Scene1
12     auto scene = tvg::Scene::gen();
13     pScene1 = scene.get();
14     scene->reserve(3);   //reserve 3 shape nodes (optional)
15
16     //Prepare Round Rectangle (Scene1)
17     auto shape1 = tvg::Shape::gen();
18     shape1->appendRect(-235, -250, 400, 400, 50, 50);  //x, y, w, h, rx, ry
19     shape1->fill(0, 255, 0, 255);                      //r, g, b, a
20     shape1->stroke(5);                                 //width
21     shape1->stroke(255, 255, 255, 255);                //r, g, b, a
22     scene->push(move(shape1));
23
24     //Prepare Circle (Scene1)
25     auto shape2 = tvg::Shape::gen();
26     shape2->appendCircle(-165, -150, 200, 200);    //cx, cy, radiusW, radiusH
27     shape2->fill(255, 255, 0, 255);                //r, g, b, a
28     scene->push(move(shape2));
29
30     //Prepare Ellipse (Scene1)
31     auto shape3 = tvg::Shape::gen();
32     shape3->appendCircle(265, 250, 150, 100);      //cx, cy, radiusW, radiusH
33     shape3->fill(0, 255, 255, 255);                //r, g, b, a
34     scene->push(move(shape3));
35
36     scene->translate(350, 350);
37     scene->scale(0.5);
38
39     //Create Scene2
40     auto scene2 = tvg::Scene::gen();
41     pScene2 = scene2.get();
42     scene2->reserve(2);   //reserve 2 shape nodes (optional)
43
44     //Star (Scene2)
45     auto shape4 = tvg::Shape::gen();
46
47     //Appends Paths
48     shape4->moveTo(0, -114.5);
49     shape4->lineTo(54, -5.5);
50     shape4->lineTo(175, 11.5);
51     shape4->lineTo(88, 95.5);
52     shape4->lineTo(108, 216.5);
53     shape4->lineTo(0, 160.5);
54     shape4->lineTo(-102, 216.5);
55     shape4->lineTo(-87, 96.5);
56     shape4->lineTo(-173, 12.5);
57     shape4->lineTo(-53, -5.5);
58     shape4->close();
59     shape4->fill(0, 0, 127, 127);
60     shape4->stroke(3);                             //width
61     shape4->stroke(0, 0, 255, 255);                //r, g, b, a
62     scene2->push(move(shape4));
63
64     //Circle (Scene2)
65     auto shape5 = tvg::Shape::gen();
66
67     auto cx = -150.0f;
68     auto cy = -150.0f;
69     auto radius = 100.0f;
70     auto halfRadius = radius * 0.552284f;
71
72     //Append Paths
73     shape5->moveTo(cx, cy - radius);
74     shape5->cubicTo(cx + halfRadius, cy - radius, cx + radius, cy - halfRadius, cx + radius, cy);
75     shape5->cubicTo(cx + radius, cy + halfRadius, cx + halfRadius, cy + radius, cx, cy+ radius);
76     shape5->cubicTo(cx - halfRadius, cy + radius, cx - radius, cy + halfRadius, cx - radius, cy);
77     shape5->cubicTo(cx - radius, cy - halfRadius, cx - halfRadius, cy - radius, cx, cy - radius);
78     shape5->close();
79     shape5->fill(127, 0, 0, 127);
80     scene2->push(move(shape5));
81
82     scene2->translate(500, 350);
83
84     //Push scene2 onto the scene
85     scene->push(move(scene2));
86
87     //Draw the Scene onto the Canvas
88     canvas->push(move(scene));
89 }
90
91 void tvgUpdateCmds(tvg::Canvas* canvas, float progress)
92 {
93     /* Update scene directly.
94        You can update only necessary properties of this scene,
95        while retaining other properties. */
96
97     pScene1->rotate(360 * progress);
98     pScene2->rotate(360 * progress);
99
100     //Update shape for drawing (this may work asynchronously)
101     canvas->update(pScene1);
102 }
103
104
105 /************************************************************************/
106 /* Sw Engine Test Code                                                  */
107 /************************************************************************/
108
109 static unique_ptr<tvg::SwCanvas> swCanvas;
110
111 void tvgSwTest(uint32_t* buffer)
112 {
113     //Create a Canvas
114     swCanvas = tvg::SwCanvas::gen();
115     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT);
116
117     /* Push the shape into the Canvas drawing list
118        When this shape is into the canvas list, the shape could update & prepare
119        internal data asynchronously for coming rendering.
120        Canvas keeps this shape node unless user call canvas->clear() */
121     tvgDrawCmds(swCanvas.get());
122 }
123
124 void transitSwCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
125 {
126     tvgUpdateCmds(swCanvas.get(), progress);
127
128     //Update Efl Canvas
129     Eo* img = (Eo*) effect;
130     evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
131     evas_object_image_pixels_dirty_set(img, EINA_TRUE);
132 }
133
134 void drawSwView(void* data, Eo* obj)
135 {
136     if (swCanvas->draw() == tvg::Result::Success) {
137         swCanvas->sync();
138     }
139 }
140
141
142 /************************************************************************/
143 /* GL Engine Test Code                                                  */
144 /************************************************************************/
145
146 static unique_ptr<tvg::GlCanvas> glCanvas;
147
148 void initGLview(Evas_Object *obj)
149 {
150     static constexpr auto BPP = 4;
151
152     //Create a Canvas
153     glCanvas = tvg::GlCanvas::gen();
154     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
155
156     /* Push the shape into the Canvas drawing list
157        When this shape is into the canvas list, the shape could update & prepare
158        internal data asynchronously for coming rendering.
159        Canvas keeps this shape node unless user call canvas->clear() */
160     tvgDrawCmds(glCanvas.get());
161 }
162
163 void drawGLview(Evas_Object *obj)
164 {
165     auto gl = elm_glview_gl_api_get(obj);
166     int w, h;
167     elm_glview_size_get(obj, &w, &h);
168     gl->glViewport(0, 0, w, h);
169     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
170     gl->glClear(GL_COLOR_BUFFER_BIT);
171     gl->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
172     gl->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
173     gl->glEnable(GL_BLEND);
174
175     if (glCanvas->draw() == tvg::Result::Success) {
176         glCanvas->sync();
177     }
178 }
179
180 void transitGlCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
181 {
182     tvgUpdateCmds(glCanvas.get(), progress);
183 }
184
185
186 /************************************************************************/
187 /* Main Code                                                            */
188 /************************************************************************/
189
190 int main(int argc, char **argv)
191 {
192     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
193
194     if (argc > 1) {
195         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
196     }
197
198     //Initialize ThorVG Engine
199     if (tvgEngine == tvg::CanvasEngine::Sw) {
200         cout << "tvg engine: software" << endl;
201     } else {
202         cout << "tvg engine: opengl" << endl;
203     }
204
205     //Initialize ThorVG Engine
206     if (tvg::Initializer::init(tvgEngine) == tvg::Result::Success) {
207
208         elm_init(argc, argv);
209
210         Elm_Transit *transit = elm_transit_add();
211
212         if (tvgEngine == tvg::CanvasEngine::Sw) {
213             auto view = createSwView();
214             elm_transit_effect_add(transit, transitSwCb, view, nullptr);
215         } else {
216             auto view = createGlView();
217             elm_transit_effect_add(transit, transitGlCb, view, nullptr);
218         }
219
220         elm_transit_duration_set(transit, 2);
221         elm_transit_repeat_times_set(transit, -1);
222         elm_transit_auto_reverse_set(transit, EINA_TRUE);
223         elm_transit_go(transit);
224
225         elm_run();
226         elm_shutdown();
227
228         //Terminate ThorVG Engine
229         tvg::Initializer::term(tvgEngine);
230
231     } else {
232         cout << "engine is not supported" << endl;
233     }
234     return 0;
235 }