common taskscheduler: revise functionalities.
[platform/core/graphics/tizenvg.git] / test / testCustomTransform.cpp
1 #include "testCommon.h"
2
3 /************************************************************************/
4 /* Drawing Commands                                                     */
5 /************************************************************************/
6 tvg::Shape* pShape = nullptr;
7
8 void tvgDrawCmds(tvg::Canvas* canvas)
9 {
10     if (!canvas) return;
11
12     //Shape1
13     auto shape = tvg::Shape::gen();
14
15     /* Acquire shape pointer to access it again.
16        instead, you should consider not to interrupt this pointer life-cycle. */
17     pShape = shape.get();
18
19     shape->moveTo(0, -114.5);
20     shape->lineTo(54, -5.5);
21     shape->lineTo(175, 11.5);
22     shape->lineTo(88, 95.5);
23     shape->lineTo(108, 216.5);
24     shape->lineTo(0, 160.5);
25     shape->lineTo(-102, 216.5);
26     shape->lineTo(-87, 96.5);
27     shape->lineTo(-173, 12.5);
28     shape->lineTo(-53, -5.5);
29     shape->close();
30     shape->fill(0, 0, 255, 255);
31     shape->stroke(3);
32     shape->stroke(255, 255, 255, 255);
33     if (canvas->push(move(shape)) != tvg::Result::Success) return;
34 }
35
36 void tvgUpdateCmds(tvg::Canvas* canvas, float progress)
37 {
38     if (!canvas) return;
39
40     /* Update shape directly.
41        You can update only necessary properties of this shape,
42        while retaining other properties. */
43
44     //Transform Matrix
45     tvg::Matrix m = {1, 0, 0, 0, 1, 0, 0, 0, 1};
46
47     //scale x
48     m.e11 = 1 - (progress * 0.5f);
49
50     //scale y
51     m.e22 = 1 + (progress * 2.0f);
52
53     //rotation
54     constexpr auto PI = 3.141592f;
55     auto degree = 45.0f;
56     auto radian = degree / 180.0f * PI;
57     auto cosVal = cosf(radian);
58     auto sinVal = sinf(radian);
59
60     auto t11 = m.e11 * cosVal + m.e12 * sinVal;
61     auto t12 = m.e11 * -sinVal + m.e12 * cosVal;
62     auto t21 = m.e21 * cosVal + m.e22 * sinVal;
63     auto t22 = m.e21 * -sinVal + m.e22 * cosVal;
64     auto t13 = m.e31 * cosVal + m.e32 * sinVal;
65     auto t23 = m.e31 * -sinVal + m.e32 * cosVal;
66
67     m.e11 = t11;
68     m.e12 = t12;
69     m.e21 = t21;
70     m.e22 = t22;
71     m.e13 = t13;
72     m.e23 = t23;
73
74     //translate
75     m.e13 = progress * 300.0f + 300.0f;
76     m.e23 = progress * -100.0f + 300.0f;
77
78     pShape->transform(m);
79
80     //Update shape for drawing (this may work asynchronously)
81     canvas->update(pShape);
82 }
83
84
85 /************************************************************************/
86 /* Sw Engine Test Code                                                  */
87 /************************************************************************/
88
89 static unique_ptr<tvg::SwCanvas> swCanvas;
90
91 void tvgSwTest(uint32_t* buffer)
92 {
93     //Create a Canvas
94     swCanvas = tvg::SwCanvas::gen();
95     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
96
97     /* Push the shape into the Canvas drawing list
98        When this shape is into the canvas list, the shape could update & prepare
99        internal data asynchronously for coming rendering.
100        Canvas keeps this shape node unless user call canvas->clear() */
101     tvgDrawCmds(swCanvas.get());
102 }
103
104 void transitSwCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
105 {
106     tvgUpdateCmds(swCanvas.get(), progress);
107
108     //Update Efl Canvas
109     Eo* img = (Eo*) effect;
110     evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
111     evas_object_image_pixels_dirty_set(img, EINA_TRUE);
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 void transitGlCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
155 {
156     tvgUpdateCmds(glCanvas.get(), progress);
157 }
158
159
160 /************************************************************************/
161 /* Main Code                                                            */
162 /************************************************************************/
163
164 int main(int argc, char **argv)
165 {
166     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
167
168     if (argc > 1) {
169         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
170     }
171
172     //Initialize ThorVG Engine
173     if (tvgEngine == tvg::CanvasEngine::Sw) {
174         cout << "tvg engine: software" << endl;
175     } else {
176         cout << "tvg engine: opengl" << endl;
177     }
178
179     //Threads Count
180     auto threads = std::thread::hardware_concurrency();
181
182     //Initialize ThorVG Engine
183     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
184
185         elm_init(argc, argv);
186
187         Elm_Transit *transit = elm_transit_add();
188
189         if (tvgEngine == tvg::CanvasEngine::Sw) {
190             auto view = createSwView();
191             elm_transit_effect_add(transit, transitSwCb, view, nullptr);
192         } else {
193             auto view = createGlView();
194             elm_transit_effect_add(transit, transitGlCb, view, nullptr);
195         }
196
197         elm_transit_duration_set(transit, 2);
198         elm_transit_repeat_times_set(transit, -1);
199         elm_transit_auto_reverse_set(transit, EINA_TRUE);
200         elm_transit_go(transit);
201
202         elm_run();
203         elm_shutdown();
204
205         //Terminate ThorVG Engine
206         tvg::Initializer::term(tvgEngine);
207
208     } else {
209         cout << "engine is not supported" << endl;
210     }
211     return 0;
212 }