common taskscheduler: revise functionalities.
[platform/core/graphics/tizenvg.git] / test / testAsync.cpp
1 #include "testCommon.h"
2
3 /************************************************************************/
4 /* Drawing Commands                                                     */
5 /************************************************************************/
6 #define COUNT 50
7
8 static double t1, t2, t3, t4;
9 static unsigned cnt = 0;
10
11 bool tvgUpdateCmds(tvg::Canvas* canvas)
12 {
13    if (!canvas) return false;
14
15     auto t = ecore_time_get();
16
17     //Explicitly clear all retained paint nodes.
18     if (canvas->clear() != tvg::Result::Success) {
19         //Logically wrong! Probably, you missed to call sync() before.
20         return false;
21     }
22
23     t1 = t;
24     t2 = ecore_time_get();
25
26     for (int i = 0; i < COUNT; i++) {
27         auto shape = tvg::Shape::gen();
28
29         float x = rand() % (WIDTH/2);
30         float y = rand() % (HEIGHT/2);
31         float w = 1 + rand() % (int)(WIDTH * 1.3 / 2);
32         float h = 1 + rand() %  (int)(HEIGHT * 1.3 / 2);
33
34         shape->appendRect(x, y, w, h, 0, 0);
35
36         //LinearGradient
37         auto fill = tvg::LinearGradient::gen();
38         fill->linear(x, y, x + w, y + h);
39
40         //Gradient Color Stops
41         tvg::Fill::ColorStop colorStops[3];
42         colorStops[0] = {0, uint8_t(rand() % 255), uint8_t(rand() % 255), uint8_t(rand() % 255), 255};
43         colorStops[1] = {1, uint8_t(rand() % 255), uint8_t(rand() % 255), uint8_t(rand() % 255), 255};
44         colorStops[2] = {2, uint8_t(rand() % 255), uint8_t(rand() % 255), uint8_t(rand() % 255), 255};
45
46         fill->colorStops(colorStops, 3);
47         shape->fill(move(fill));
48
49         if (canvas->push(move(shape)) != tvg::Result::Success) {
50             //Did you call clear()? Make it sure if canvas is on rendering
51             break;
52         }
53     }
54
55     t3 = ecore_time_get();
56
57     return true;
58 }
59
60
61 /************************************************************************/
62 /* Sw Engine Test Code                                                  */
63 /************************************************************************/
64
65 static unique_ptr<tvg::SwCanvas> swCanvas;
66
67 void tvgSwTest(uint32_t* buffer)
68 {
69     //Create a Canvas
70     swCanvas = tvg::SwCanvas::gen();
71     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
72 }
73
74 Eina_Bool animSwCb(void* data)
75 {
76     if (!tvgUpdateCmds(swCanvas.get())) return ECORE_CALLBACK_RENEW;
77
78     //Drawing task can be performed asynchronously.
79     if (swCanvas->draw() != tvg::Result::Success) return false;
80
81     //Update Efl Canvas
82     Eo* img = (Eo*) data;
83     evas_object_image_pixels_dirty_set(img, EINA_TRUE);
84     evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
85
86     return ECORE_CALLBACK_RENEW;
87 }
88
89 void drawSwView(void* data, Eo* obj)
90 {
91     //Make it guarantee finishing drawing task.
92     swCanvas->sync();
93
94     t4 = ecore_time_get();
95
96     printf("[%5d]: total[%fms] = clear[%fms], update[%fms], render[%fms]\n", ++cnt, t4 - t1, t2 - t1, t3 - t2, t4 - t3);
97 }
98
99
100 /************************************************************************/
101 /* GL Engine Test Code                                                  */
102 /************************************************************************/
103
104 static unique_ptr<tvg::GlCanvas> glCanvas;
105
106 void initGLview(Evas_Object *obj)
107 {
108     static constexpr auto BPP = 4;
109
110     //Create a Canvas
111     glCanvas = tvg::GlCanvas::gen();
112     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
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     glCanvas->sync();
122 }
123
124 Eina_Bool animGlCb(void* data)
125 {
126     if (!tvgUpdateCmds(glCanvas.get())) return ECORE_CALLBACK_RENEW;
127
128     //Drawing task can be performed asynchronously.
129     glCanvas->draw();
130
131     return ECORE_CALLBACK_RENEW;
132 }
133
134
135 /************************************************************************/
136 /* Main Code                                                            */
137 /************************************************************************/
138
139 int main(int argc, char **argv)
140 {
141     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
142
143     if (argc > 1) {
144         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
145     }
146
147     //Initialize ThorVG Engine
148     if (tvgEngine == tvg::CanvasEngine::Sw) {
149         cout << "tvg engine: software" << endl;
150     } else {
151         cout << "tvg engine: opengl" << endl;
152     }
153
154     //Threads Count
155     auto threads = std::thread::hardware_concurrency();
156
157     //Initialize ThorVG Engine
158     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
159
160         elm_init(argc, argv);
161
162         if (tvgEngine == tvg::CanvasEngine::Sw) {
163             auto view = createSwView();
164             evas_object_image_pixels_get_callback_set(view, drawSwView, nullptr);
165             ecore_animator_add(animSwCb, view);
166         } else {
167             auto view = createGlView();
168             ecore_animator_add(animGlCb, view);
169         }
170
171         elm_run();
172         elm_shutdown();
173
174         //Terminate ThorVG Engine
175         tvg::Initializer::term(tvgEngine);
176
177     } else {
178         cout << "engine is not supported" << endl;
179     }
180     return 0;
181 }