bindings/capi: fix c compatibility warnings.
[platform/core/graphics/tizenvg.git] / test / testDirectUpdate.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     if (!canvas) return;
11
12     //Shape (for BG)
13     auto bg = tvg::Shape::gen();
14     bg->appendRect(0, 0, WIDTH, HEIGHT, 0, 0);
15
16     //fill property will be retained
17     bg->fill(255, 255, 255, 255);
18
19     if (canvas->push(move(bg)) != tvg::Result::Success) return;
20
21     //Shape
22     auto shape = tvg::Shape::gen();
23
24     /* Acquire shape pointer to access it again.
25        instead, you should consider not to interrupt this pointer life-cycle. */
26     pShape = shape.get();
27
28     shape->appendRect(-100, -100, 200, 200, 0, 0);
29
30     //fill property will be retained
31     shape->fill(127, 255, 255, 255);
32     shape->stroke(0, 0, 255, 255);
33     shape->stroke(1);
34
35     if (canvas->push(move(shape)) != tvg::Result::Success) return;
36 }
37
38 void tvgUpdateCmds(tvg::Canvas* canvas, float progress)
39 {
40     if (!canvas) return;
41
42     /* Update shape directly.
43        You can update only necessary properties of this shape,
44        while retaining other properties. */
45
46     //Reset Shape
47     if (pShape->reset() == tvg::Result::Success) {
48         pShape->appendRect(-100 + (800 * progress), -100 + (800 * progress), 200, 200, (100 * progress), (100 * progress));
49         pShape->stroke(30 * progress);
50
51         //Update shape for drawing (this may work asynchronously)
52         canvas->update(pShape);
53     }
54 }
55
56
57 /************************************************************************/
58 /* Sw Engine Test Code                                                  */
59 /************************************************************************/
60
61 static unique_ptr<tvg::SwCanvas> swCanvas;
62
63 void tvgSwTest(uint32_t* buffer)
64 {
65     //Create a Canvas
66     swCanvas = tvg::SwCanvas::gen();
67     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
68
69     /* Push the shape into the Canvas drawing list
70        When this shape is into the canvas list, the shape could update & prepare
71        internal data asynchronously for coming rendering.
72        Canvas keeps this shape node unless user call canvas->clear() */
73     tvgDrawCmds(swCanvas.get());
74 }
75
76 void transitSwCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
77 {
78     tvgUpdateCmds(swCanvas.get(), progress);
79
80     //Update Efl Canvas
81     Eo* img = (Eo*) effect;
82     evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
83     evas_object_image_pixels_dirty_set(img, EINA_TRUE);
84 }
85
86 void drawSwView(void* data, Eo* obj)
87 {
88     if (swCanvas->draw() == tvg::Result::Success) {
89         swCanvas->sync();
90     }
91 }
92
93
94 /************************************************************************/
95 /* GL Engine Test Code                                                  */
96 /************************************************************************/
97
98 static unique_ptr<tvg::GlCanvas> glCanvas;
99
100 void initGLview(Evas_Object *obj)
101 {
102     static constexpr auto BPP = 4;
103
104     //Create a Canvas
105     glCanvas = tvg::GlCanvas::gen();
106     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
107
108     /* Push the shape into the Canvas drawing list
109        When this shape is into the canvas list, the shape could update & prepare
110        internal data asynchronously for coming rendering.
111        Canvas keeps this shape node unless user call canvas->clear() */
112     tvgDrawCmds(glCanvas.get());
113 }
114
115 void drawGLview(Evas_Object *obj)
116 {
117     auto gl = elm_glview_gl_api_get(obj);
118     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
119     gl->glClear(GL_COLOR_BUFFER_BIT);
120
121     if (glCanvas->draw() == tvg::Result::Success) {
122         glCanvas->sync();
123     }
124 }
125
126 void transitGlCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
127 {
128     tvgUpdateCmds(glCanvas.get(), progress);
129 }
130
131
132 /************************************************************************/
133 /* Main Code                                                            */
134 /************************************************************************/
135
136 int main(int argc, char **argv)
137 {
138     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
139
140     if (argc > 1) {
141         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
142     }
143
144     //Initialize ThorVG Engine
145     if (tvgEngine == tvg::CanvasEngine::Sw) {
146         cout << "tvg engine: software" << endl;
147     } else {
148         cout << "tvg engine: opengl" << endl;
149     }
150
151     //Threads Count
152     auto threads = std::thread::hardware_concurrency();
153
154     //Initialize ThorVG Engine
155     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
156
157         elm_init(argc, argv);
158
159         Elm_Transit *transit = elm_transit_add();
160
161         if (tvgEngine == tvg::CanvasEngine::Sw) {
162             auto view = createSwView();
163             elm_transit_effect_add(transit, transitSwCb, view, nullptr);
164         } else {
165             auto view = createGlView();
166             elm_transit_effect_add(transit, transitGlCb, view, nullptr);
167         }
168
169         elm_transit_duration_set(transit, 2);
170         elm_transit_repeat_times_set(transit, -1);
171         elm_transit_auto_reverse_set(transit, EINA_TRUE);
172         elm_transit_go(transit);
173
174         elm_run();
175         elm_shutdown();
176
177         //Terminate ThorVG Engine
178         tvg::Initializer::term(tvgEngine);
179
180     } else {
181         cout << "engine is not supported" << endl;
182     }
183     return 0;
184 }