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