Package version up to 0.8.8
[platform/core/uifw/wayland-tbm.git] / test / tbm-leak-test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 #define WL_HIDE_DEPRECATED
7 #include <wayland-client.h>
8 #include <wayland-tbm-client.h>
9 #include <wayland-tbm-int.h>
10
11 #include <tbm_surface.h>
12 #include <tbm_surface_queue.h>
13 #include <tbm_surface_internal.h>
14
15 #include "wayland-tbm-test-client-protocol.h"
16
17 #define WL_APP_LOG(fmt, ...)   fprintf(stderr, "[CLIENT(%d):%s] " fmt, getpid(), __func__, ##__VA_ARGS__)
18 #define WL_APP_CHECK(cond) {\
19         if (!(cond)) {\
20                 WL_APP_LOG("[%s:%d] : '%s' failed.\n", __FUNCTION__, __LINE__, #cond);\
21         } \
22 }
23
24 typedef struct {
25         struct wl_display *dpy;
26         struct wayland_tbm_client *tbm_client;
27         struct wl_tbm_test *wl_tbm_test;
28         tbm_bufmgr bufmgr;
29 } AppInfoClient;
30
31 static AppInfoClient gApp;
32
33 static void
34 _wl_registry_global_cb(void *data,
35                        struct wl_registry *wl_registry,
36                        uint32_t name,
37                        const char *interface,
38                        uint32_t version)
39 {
40         AppInfoClient *app = (AppInfoClient *)data;
41
42         if (!strcmp(interface, "wl_tbm_test")) {
43                 WL_APP_LOG("bind %s", interface);
44                 app->wl_tbm_test = wl_registry_bind(wl_registry, name,
45                                                     &wl_tbm_test_interface, 1);
46         }
47 }
48
49 static void
50 _wl_registry_global_remove_cb(void *data,
51                               struct wl_registry *wl_registry,
52                               uint32_t name)
53 {
54 }
55
56 static const struct wl_registry_listener wl_registry_impl = {
57         _wl_registry_global_cb,
58         _wl_registry_global_remove_cb,
59 };
60
61 static int test_01(AppInfoClient* app);
62 static int test_02(AppInfoClient* app);
63 static int test_03(AppInfoClient* app);
64 static int test_04(AppInfoClient* app);
65 static int test_05(AppInfoClient* app);
66 static int test_06(AppInfoClient* app);
67
68 int
69 main(int argc, char *argv[])
70 {
71         const char *default_dpy_name = "queue";
72         struct wl_registry *registry;
73         const char *dpy_name = NULL;
74
75         if (argc > 1)
76                 dpy_name = argv[1];
77         else
78                 dpy_name = default_dpy_name;
79
80         gApp.dpy = wl_display_connect(dpy_name);
81         if (!gApp.dpy) {
82                 printf("[APP] failed to connect server\n");
83                 return -1;
84         }
85
86         registry = wl_display_get_registry(gApp.dpy);
87         wl_registry_add_listener(registry, &wl_registry_impl, &gApp);
88         wl_display_roundtrip(gApp.dpy);
89         if (gApp.wl_tbm_test == NULL) {
90                 WL_APP_LOG("fail to bind::wl_tbm_test");
91                 return 0;
92         }
93
94         gApp.tbm_client = wayland_tbm_client_init(gApp.dpy);
95         if (!gApp.tbm_client) {
96                 WL_APP_LOG("fail to wayland_tbm_client_init()\n");
97                 goto finish;
98         }
99         gApp.bufmgr = tbm_bufmgr_init(-1);
100         tbm_bufmgr_debug_show(gApp.bufmgr);
101
102         test_01(&gApp);
103         test_02(&gApp);
104         test_03(&gApp);
105         test_04(&gApp);
106         test_05(&gApp);
107         test_06(&gApp);
108
109
110 finish:
111         if (gApp.bufmgr) {
112                 tbm_bufmgr_debug_show(gApp.bufmgr);
113                 tbm_bufmgr_deinit(gApp.bufmgr);
114         }
115
116         return 1;
117 }
118 static int
119 print_mem_info(AppInfoClient* app, char *str)
120 {
121         WL_APP_LOG("## %s ##\n", str);
122         tbm_bufmgr_debug_show(app->bufmgr);
123         WL_APP_LOG("\n");
124
125         return 0;
126 }
127
128 static int
129 test_01(AppInfoClient* app)
130 {
131         struct wl_test_surface* surface;
132         tbm_surface_queue_h surface_queue;
133         tbm_surface_h buffer = NULL;
134
135         surface = wl_tbm_test_create_surface(app->wl_tbm_test);
136         surface_queue = wayland_tbm_client_create_surface_queue(app->tbm_client,
137                                         (struct wl_surface*)surface,
138                                         3, 100, 100, TBM_FORMAT_ABGR8888);
139
140         tbm_surface_queue_dequeue(surface_queue, &buffer);
141         WL_APP_CHECK(buffer != NULL);
142         tbm_surface_queue_enqueue(surface_queue, buffer);
143         tbm_surface_queue_dequeue(surface_queue, &buffer);
144         WL_APP_CHECK(buffer != NULL);
145         tbm_surface_queue_enqueue(surface_queue, buffer);
146
147         tbm_surface_queue_destroy(surface_queue);
148         wl_test_surface_destroy(surface);
149         wl_display_roundtrip(app->dpy);
150
151         print_mem_info(app, "TEST_01");
152
153         return 0;
154 }
155
156 static int
157 test_02(AppInfoClient* app)
158 {
159         struct wl_test_surface* surface;
160         tbm_surface_queue_h surface_queue;
161         tbm_surface_h buffer = NULL;
162
163         surface = wl_tbm_test_create_surface(app->wl_tbm_test);
164         surface_queue = wayland_tbm_client_create_surface_queue(app->tbm_client,
165                                         (struct wl_surface*)surface,
166                                         3, 100, 100, TBM_FORMAT_ABGR8888);
167
168         tbm_surface_queue_dequeue(surface_queue, &buffer);
169         WL_APP_CHECK(buffer != NULL);
170         tbm_surface_queue_dequeue(surface_queue, &buffer);
171         WL_APP_CHECK(buffer != NULL);
172
173         tbm_surface_queue_destroy(surface_queue);
174         wl_test_surface_destroy(surface);
175         wl_display_roundtrip(app->dpy);
176
177         print_mem_info(app, "TEST_02");
178
179         return 0;
180 }
181
182 static int
183 test_03(AppInfoClient* app)
184 {
185         struct wl_test_surface* surface;
186         tbm_surface_queue_h surface_queue;
187         tbm_surface_h buffer = NULL;
188
189         surface = wl_tbm_test_create_surface(app->wl_tbm_test);
190         surface_queue = wayland_tbm_client_create_surface_queue(app->tbm_client,
191                                         (struct wl_surface*)surface,
192                                         3, 100, 100, TBM_FORMAT_ABGR8888);
193
194         tbm_surface_queue_dequeue(surface_queue, &buffer);
195         WL_APP_CHECK(buffer != NULL);
196         tbm_surface_queue_dequeue(surface_queue, &buffer);
197         WL_APP_CHECK(buffer != NULL);
198
199         tbm_surface_queue_reset(surface_queue, 200, 200, TBM_FORMAT_ABGR8888);
200
201         tbm_surface_queue_destroy(surface_queue);
202         wl_test_surface_destroy(surface);
203         wl_display_roundtrip(app->dpy);
204
205         print_mem_info(app, "TEST_03");
206
207         return 0;
208 }
209
210 static int
211 test_04(AppInfoClient* app)
212 {
213         struct wl_test_surface* surface;
214         tbm_surface_queue_h surface_queue;
215         tbm_surface_h buffer = NULL;
216
217         surface = wl_tbm_test_create_surface(app->wl_tbm_test);
218         surface_queue = wayland_tbm_client_create_surface_queue(app->tbm_client,
219                                         (struct wl_surface*)surface,
220                                         3, 100, 100, TBM_FORMAT_ABGR8888);
221
222         tbm_surface_queue_dequeue(surface_queue, &buffer);
223         WL_APP_CHECK(buffer != NULL);
224         tbm_surface_queue_dequeue(surface_queue, &buffer);
225         WL_APP_CHECK(buffer != NULL);
226
227         tbm_surface_queue_reset(surface_queue, 200, 200, TBM_FORMAT_ABGR8888);
228         tbm_surface_queue_dequeue(surface_queue, &buffer);
229         WL_APP_CHECK(buffer != NULL);
230         tbm_surface_queue_enqueue(surface_queue, buffer);
231         tbm_surface_queue_dequeue(surface_queue, &buffer);
232         WL_APP_CHECK(buffer != NULL);
233         tbm_surface_internal_ref(buffer);
234
235         tbm_surface_queue_destroy(surface_queue);
236         wl_test_surface_destroy(surface);
237         wl_display_roundtrip(app->dpy);
238
239         print_mem_info(app, "TEST_04");
240         tbm_surface_internal_unref(buffer);
241         print_mem_info(app, "TEST_04_final");
242
243         return 0;
244 }
245
246 static int
247 test_05(AppInfoClient* app)
248 {
249         struct wl_test_surface* surface;
250         tbm_surface_queue_h surface_queue;
251         tbm_surface_h buffer = NULL;
252         struct wl_buffer* wl_buffer;
253
254         surface = wl_tbm_test_create_surface(app->wl_tbm_test);
255         surface_queue = wayland_tbm_client_create_surface_queue(app->tbm_client,
256                                         (struct wl_surface*)surface,
257                                         3, 100, 100, TBM_FORMAT_ABGR8888);
258
259         tbm_surface_queue_dequeue(surface_queue, &buffer);
260         WL_APP_CHECK(buffer != NULL);
261
262         wl_buffer = wayland_tbm_client_create_buffer(app->tbm_client, buffer);
263         wl_test_surface_attach(surface, wl_buffer);
264         wl_display_roundtrip(app->dpy);
265
266         tbm_surface_queue_destroy(surface_queue);
267         wl_test_surface_destroy(surface);
268         wl_display_roundtrip(app->dpy);
269
270         print_mem_info(app, "TEST_05");
271
272         return 0;
273 }
274
275 static int
276 test_06(AppInfoClient* app)
277 {
278         struct wl_test_surface* surface;
279         tbm_surface_queue_h surface_queue;
280
281         WL_APP_LOG("\n");
282         surface = wl_tbm_test_create_surface(app->wl_tbm_test);
283         surface_queue = wayland_tbm_client_create_surface_queue(app->tbm_client,
284                                         (struct wl_surface*)surface,
285                                         3, 100, 100, TBM_FORMAT_ABGR8888);
286
287         wl_tbm_test_set_active_queue(app->wl_tbm_test, surface);
288         wl_display_roundtrip(app->dpy);
289
290         tbm_surface_queue_destroy(surface_queue);
291         wl_test_surface_destroy(surface);
292         wl_display_roundtrip(app->dpy);
293
294         print_mem_info(app, "TEST_06");
295
296         return 0;
297 }
298