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