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