test: unify test code for supporting gl engine from all test cases.
[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     //Shape1
11     auto shape = tvg::Shape::gen();
12
13     /* Acquire shape pointer to access it again.
14        instead, you should consider not to interrupt this pointer life-cycle. */
15     pShape = shape.get();
16
17     shape->moveTo(0, -114.5);
18     shape->lineTo(54, -5.5);
19     shape->lineTo(175, 11.5);
20     shape->lineTo(88, 95.5);
21     shape->lineTo(108, 216.5);
22     shape->lineTo(0, 160.5);
23     shape->lineTo(-102, 216.5);
24     shape->lineTo(-87, 96.5);
25     shape->lineTo(-173, 12.5);
26     shape->lineTo(-53, -5.5);
27     shape->close();
28     shape->fill(0, 0, 255, 255);
29     canvas->push(move(shape));
30 }
31
32 void tvgUpdateCmds(tvg::Canvas* canvas, float progress)
33 {
34     /* Update shape directly.
35        You can update only necessary properties of this shape,
36        while retaining other properties. */
37
38     //Transform Matrix
39     tvg::Matrix m = {1, 0, 0, 0, 1, 0, 0, 0, 1};
40
41     //scale x
42     m.e11 = 1 - (progress * 0.5f);
43
44     //scale y
45     m.e22 = 1 + (progress * 2.0f);
46
47     //rotation
48     constexpr auto PI = 3.141592f;
49     auto degree = 45.0f;
50     auto radian = degree / 180.0f * PI;
51     auto cosVal = cosf(radian);
52     auto sinVal = sinf(radian);
53
54     auto t11 = m.e11 * cosVal + m.e12 * sinVal;
55     auto t12 = m.e11 * -sinVal + m.e12 * cosVal;
56     auto t21 = m.e21 * cosVal + m.e22 * sinVal;
57     auto t22 = m.e21 * -sinVal + m.e22 * cosVal;
58     auto t31 = m.e31 * cosVal + m.e32 * sinVal;
59     auto t32 = m.e31 * -sinVal + m.e32 * cosVal;
60
61     m.e11 = t11;
62     m.e12 = t12;
63     m.e21 = t21;
64     m.e22 = t22;
65     m.e31 = t31;
66     m.e32 = t32;
67
68     //translate
69     m.e31 = progress * 300.0f + 300.0f;
70     m.e32 = progress * -100.0f + 300.0f;
71
72     pShape->transform(m);
73
74     //Update shape for drawing (this may work asynchronously)
75     canvas->update(pShape);
76 }
77
78
79 /************************************************************************/
80 /* Sw Engine Test Code                                                  */
81 /************************************************************************/
82
83 static unique_ptr<tvg::SwCanvas> swCanvas;
84
85 void tvgSwTest(uint32_t* buffer)
86 {
87     //Create a Canvas
88     swCanvas = tvg::SwCanvas::gen();
89     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT);
90
91     /* Push the shape into the Canvas drawing list
92        When this shape is into the canvas list, the shape could update & prepare
93        internal data asynchronously for coming rendering.
94        Canvas keeps this shape node unless user call canvas->clear() */
95     tvgDrawCmds(swCanvas.get());
96 }
97
98 void transitSwCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
99 {
100     tvgUpdateCmds(swCanvas.get(), progress);
101
102     //Update Efl Canvas
103     Eo* img = (Eo*) effect;
104     evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
105     evas_object_image_pixels_dirty_set(img, EINA_TRUE);
106 }
107
108 void drawSwView(void* data, Eo* obj)
109 {
110     swCanvas->draw();
111     swCanvas->sync();
112 }
113
114
115 /************************************************************************/
116 /* GL Engine Test Code                                                  */
117 /************************************************************************/
118
119 static unique_ptr<tvg::GlCanvas> glCanvas;
120
121 void initGLview(Evas_Object *obj)
122 {
123     static constexpr auto BPP = 4;
124
125     //Create a Canvas
126     glCanvas = tvg::GlCanvas::gen();
127     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
128
129     /* Push the shape into the Canvas drawing list
130        When this shape is into the canvas list, the shape could update & prepare
131        internal data asynchronously for coming rendering.
132        Canvas keeps this shape node unless user call canvas->clear() */
133     tvgDrawCmds(glCanvas.get());
134 }
135
136 void drawGLview(Evas_Object *obj)
137 {
138     auto gl = elm_glview_gl_api_get(obj);
139     int w, h;
140     elm_glview_size_get(obj, &w, &h);
141     gl->glViewport(0, 0, w, h);
142     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
143     gl->glClear(GL_COLOR_BUFFER_BIT);
144     gl->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
145     gl->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
146     gl->glEnable(GL_BLEND);
147
148     glCanvas->draw();
149     glCanvas->sync();
150 }
151
152 void transitGlCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
153 {
154     tvgUpdateCmds(glCanvas.get(), progress);
155 }
156
157
158 /************************************************************************/
159 /* Main Code                                                            */
160 /************************************************************************/
161
162 int main(int argc, char **argv)
163 {
164     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
165
166     if (argc > 1) {
167         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
168     }
169
170     //Initialize ThorVG Engine
171     if (tvgEngine == tvg::CanvasEngine::Sw) {
172         cout << "tvg engine: software" << endl;
173     } else {
174         cout << "tvg engine: opengl" << endl;
175     }
176
177     //Initialize ThorVG Engine
178     tvg::Initializer::init(tvgEngine);
179
180     elm_init(argc, argv);
181
182     elm_config_accel_preference_set("gl");
183
184     Elm_Transit *transit = elm_transit_add();
185
186     if (tvgEngine == tvg::CanvasEngine::Sw) {
187         auto view = createSwView();
188         elm_transit_effect_add(transit, transitSwCb, view, nullptr);
189     } else {
190         auto view = createGlView();
191         elm_transit_effect_add(transit, transitGlCb, view, nullptr);
192     }
193
194     elm_transit_duration_set(transit, 2);
195     elm_transit_repeat_times_set(transit, -1);
196     elm_transit_auto_reverse_set(transit, EINA_TRUE);
197     elm_transit_go(transit);
198
199     elm_run();
200     elm_shutdown();
201
202     //Terminate ThorVG Engine
203     tvg::Initializer::term(tvgEngine);
204 }