Merge "SvgLoader: Support to linear, radial gradient" into tizen
[platform/core/graphics/tizenvg.git] / test / testUpdate.cpp
1 #include "testCommon.h"
2
3 /************************************************************************/
4 /* Drawing Commands                                                     */
5 /************************************************************************/
6
7 void tvgDrawCmds(tvg::Canvas* canvas)
8 {
9     //Shape
10     auto shape = tvg::Shape::gen();
11     shape->appendRect(-100, -100, 200, 200, 0, 0);
12     shape->fill(255, 255, 255, 255);
13     canvas->push(move(shape));
14 }
15
16 void tvgUpdateCmds(tvg::Canvas* canvas, float progress)
17 {
18     //Explicitly clear all retained paint nodes.
19     if (canvas->clear() != tvg::Result::Success) return;
20
21     //Shape
22     auto shape = tvg::Shape::gen();
23     shape->appendRect(-100, -100, 200, 200, (100 * progress), (100 * progress));
24     shape->fill(rand()%255, rand()%255, rand()%255, 255);
25     shape->translate(800 * progress, 800 * progress);
26     shape->scale(1 - 0.75 * progress);
27     shape->rotate(360 * progress);
28
29     canvas->push(move(shape));
30 }
31
32
33 /************************************************************************/
34 /* Sw Engine Test Code                                                  */
35 /************************************************************************/
36
37 static unique_ptr<tvg::SwCanvas> swCanvas;
38
39 void tvgSwTest(uint32_t* buffer)
40 {
41     //Create a Canvas
42     swCanvas = tvg::SwCanvas::gen();
43     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT);
44
45     /* Push the shape into the Canvas drawing list
46        When this shape is into the canvas list, the shape could update & prepare
47        internal data asynchronously for coming rendering.
48        Canvas keeps this shape node unless user call canvas->clear() */
49     tvgDrawCmds(swCanvas.get());
50 }
51
52 void transitSwCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
53 {
54     tvgUpdateCmds(swCanvas.get(), progress);
55
56     //Update Efl Canvas
57     Eo* img = (Eo*) effect;
58     evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
59     evas_object_image_pixels_dirty_set(img, EINA_TRUE);
60 }
61
62 void drawSwView(void* data, Eo* obj)
63 {
64     if (swCanvas->draw() == tvg::Result::Success) {
65         swCanvas->sync();
66     }
67 }
68
69
70 /************************************************************************/
71 /* GL Engine Test Code                                                  */
72 /************************************************************************/
73
74 static unique_ptr<tvg::GlCanvas> glCanvas;
75
76 void initGLview(Evas_Object *obj)
77 {
78     static constexpr auto BPP = 4;
79
80     //Create a Canvas
81     glCanvas = tvg::GlCanvas::gen();
82     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
83
84     /* Push the shape into the Canvas drawing list
85        When this shape is into the canvas list, the shape could update & prepare
86        internal data asynchronously for coming rendering.
87        Canvas keeps this shape node unless user call canvas->clear() */
88     tvgDrawCmds(glCanvas.get());
89 }
90
91 void drawGLview(Evas_Object *obj)
92 {
93     auto gl = elm_glview_gl_api_get(obj);
94     int w, h;
95     elm_glview_size_get(obj, &w, &h);
96     gl->glViewport(0, 0, w, h);
97     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
98     gl->glClear(GL_COLOR_BUFFER_BIT);
99     gl->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
100     gl->glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
101     gl->glEnable(GL_BLEND);
102
103     if (glCanvas->draw() == tvg::Result::Success) {
104         glCanvas->sync();
105     }
106 }
107
108 void transitGlCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
109 {
110     tvgUpdateCmds(glCanvas.get(), progress);
111 }
112
113
114 /************************************************************************/
115 /* Main Code                                                            */
116 /************************************************************************/
117
118 int main(int argc, char **argv)
119 {
120     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
121
122     if (argc > 1) {
123         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
124     }
125
126     //Initialize ThorVG Engine
127     if (tvgEngine == tvg::CanvasEngine::Sw) {
128         cout << "tvg engine: software" << endl;
129     } else {
130         cout << "tvg engine: opengl" << endl;
131     }
132
133     //Initialize ThorVG Engine
134     if (tvg::Initializer::init(tvgEngine) == tvg::Result::Success) {
135
136         elm_init(argc, argv);
137
138         Elm_Transit *transit = elm_transit_add();
139
140         if (tvgEngine == tvg::CanvasEngine::Sw) {
141             auto view = createSwView();
142             elm_transit_effect_add(transit, transitSwCb, view, nullptr);
143         } else {
144             auto view = createGlView();
145             elm_transit_effect_add(transit, transitGlCb, view, nullptr);
146         }
147
148         elm_transit_duration_set(transit, 2);
149         elm_transit_repeat_times_set(transit, -1);
150         elm_transit_auto_reverse_set(transit, EINA_TRUE);
151         elm_transit_go(transit);
152
153         elm_run();
154         elm_shutdown();
155
156         //Terminate ThorVG Engine
157         tvg::Initializer::term(tvgEngine);
158
159     } else {
160         cout << "engine is not supported" << endl;
161     }
162     return 0;
163 }