6df8533bb826ee5bcf90a0f1116f49c2e4774c7a
[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     //Threads Count
132     auto threads = std::thread::hardware_concurrency();
133
134     //Initialize ThorVG Engine
135     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
136
137         elm_init(argc, argv);
138
139         Elm_Transit *transit = elm_transit_add();
140
141         if (tvgEngine == tvg::CanvasEngine::Sw) {
142             auto view = createSwView();
143             elm_transit_effect_add(transit, transitSwCb, view, nullptr);
144         } else {
145             auto view = createGlView();
146             elm_transit_effect_add(transit, transitGlCb, view, nullptr);
147         }
148
149         elm_transit_duration_set(transit, 2);
150         elm_transit_repeat_times_set(transit, -1);
151         elm_transit_auto_reverse_set(transit, EINA_TRUE);
152         elm_transit_go(transit);
153
154         elm_run();
155         elm_shutdown();
156
157         //Terminate ThorVG Engine
158         tvg::Initializer::term(tvgEngine);
159
160     } else {
161         cout << "engine is not supported" << endl;
162     }
163     return 0;
164 }