thread: remove caller_tid information when adding and removing thread_cb
[platform/core/uifw/libtdm.git] / src / tdm_buffer.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <sc1.lim@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "tdm_private.h"
41
42 static int tdm_buffer_key;
43 #define TDM_BUFFER_KEY ((unsigned long)&tdm_buffer_key)
44
45 typedef struct _tdm_buffer_func_info {
46         tdm_buffer_release_handler release_func;
47         tdm_buffer_destroy_handler destroy_func;
48         void *user_data;
49
50         struct list_head link;
51 } tdm_buffer_func_info;
52
53 static void
54 _tdm_buffer_destroy_info(void *user_data)
55 {
56         tdm_buffer_info *buf_info = (tdm_buffer_info *)user_data;
57         tdm_buffer_func_info *func_info = NULL, *next = NULL;
58
59         if (buf_info->backend_ref_count > 0) {
60                 TDM_NEVER_GET_HERE();
61                 if (tdm_debug_module & TDM_DEBUG_BUFFER)
62                         TDM_INFO("%p", buf_info->buffer);
63         }
64
65         LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link) {
66                 LIST_DEL(&func_info->link);
67                 free(func_info);
68         }
69
70         LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->destroy_funcs, link)
71         func_info->destroy_func(buf_info->buffer, func_info->user_data);
72
73         LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->destroy_funcs, link) {
74                 LIST_DEL(&func_info->link);
75                 free(func_info);
76         }
77
78         if (tdm_debug_module & TDM_DEBUG_BUFFER)
79                 TDM_INFO("%p destroyed", buf_info->buffer);
80
81         free(buf_info);
82 }
83
84 INTERN tdm_buffer_info *
85 tdm_buffer_get_info(tbm_surface_h buffer)
86 {
87         tdm_buffer_info *buf_info = NULL;
88
89         if (!tbm_surface_internal_get_user_data(buffer, TDM_BUFFER_KEY, (void **)&buf_info)) {
90                 buf_info = calloc(1, sizeof(tdm_buffer_info));
91                 TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, NULL);
92
93                 buf_info->buffer = buffer;
94
95                 LIST_INITHEAD(&buf_info->release_funcs);
96                 LIST_INITHEAD(&buf_info->destroy_funcs);
97                 LIST_INITHEAD(&buf_info->link);
98
99                 if (!tbm_surface_internal_add_user_data(buffer, TDM_BUFFER_KEY, _tdm_buffer_destroy_info)) {
100                         TDM_ERR("FAIL to create user_data for surface %p", buffer);
101                         free(buf_info);
102                         return NULL;
103                 }
104                 if (!tbm_surface_internal_set_user_data(buffer, TDM_BUFFER_KEY, buf_info)) {
105                         TDM_ERR("FAIL to set user_data for surface %p", buffer);
106                         tbm_surface_internal_delete_user_data(buffer, TDM_BUFFER_KEY);
107                         free(buf_info);
108                         return NULL;
109                 }
110
111                 if (tdm_debug_module & TDM_DEBUG_BUFFER)
112                         TDM_INFO("%p created", buf_info->buffer);
113         }
114
115         return buf_info;
116 }
117
118 EXTERN tdm_error
119 tdm_buffer_add_release_handler(tbm_surface_h buffer,
120                                                            tdm_buffer_release_handler func, void *user_data)
121 {
122         tdm_buffer_info *buf_info;
123         tdm_buffer_func_info *func_info = NULL;
124
125         TDM_RETURN_VAL_IF_FAIL(buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
126         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
127
128         buf_info = tdm_buffer_get_info(buffer);
129         TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
130
131         LIST_FOR_EACH_ENTRY(func_info, &buf_info->release_funcs, link) {
132                 if (func_info->release_func == func && func_info->user_data == user_data) {
133                         TDM_ERR("can't add twice");
134                         return TDM_ERROR_BAD_REQUEST;
135                 }
136         }
137
138         func_info = calloc(1, sizeof(tdm_buffer_func_info));
139         TDM_RETURN_VAL_IF_FAIL(func_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
140
141         func_info->release_func = func;
142         func_info->user_data = user_data;
143
144         LIST_ADDTAIL(&func_info->link, &buf_info->release_funcs);
145
146         return TDM_ERROR_NONE;
147 }
148
149 EXTERN void
150 tdm_buffer_remove_release_handler(tbm_surface_h buffer,
151                                                                   tdm_buffer_release_handler func, void *user_data)
152 {
153         tdm_buffer_info *buf_info;
154         tdm_buffer_func_info *func_info = NULL, *next = NULL;
155
156         TDM_RETURN_IF_FAIL(buffer != NULL);
157         TDM_RETURN_IF_FAIL(func != NULL);
158
159         buf_info = tdm_buffer_get_info(buffer);
160         TDM_RETURN_IF_FAIL(buf_info != NULL);
161
162         LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link) {
163                 if (func_info->release_func != func || func_info->user_data != user_data)
164                         continue;
165
166                 LIST_DEL(&func_info->link);
167                 free(func_info);
168
169                 return;
170         }
171 }
172
173 INTERN void
174 tdm_buffer_remove_release_handler_internal(tbm_surface_h buffer)
175 {
176         tdm_buffer_info *buf_info;
177         tdm_buffer_func_info *func_info = NULL, *next = NULL;
178
179         TDM_RETURN_IF_FAIL(buffer != NULL);
180
181         buf_info = tdm_buffer_get_info(buffer);
182         TDM_RETURN_IF_FAIL(buf_info != NULL);
183
184         LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link) {
185
186                 LIST_DEL(&func_info->link);
187                 free(func_info);
188
189                 return;
190         }
191 }
192
193 EXTERN tbm_surface_h
194 tdm_buffer_ref_backend(tbm_surface_h buffer)
195 {
196         tdm_buffer_info *buf_info;
197
198         TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
199
200         buf_info = tdm_buffer_get_info(buffer);
201         TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, NULL);
202
203         buf_info->backend_ref_count++;
204         tbm_surface_internal_ref(buffer);
205
206         return buffer;
207 }
208
209 EXTERN void
210 tdm_buffer_unref_backend(tbm_surface_h buffer)
211 {
212         tdm_buffer_info *buf_info;
213         tdm_buffer_func_info *func_info = NULL, *next = NULL;
214
215         TDM_RETURN_IF_FAIL(buffer != NULL);
216
217         buf_info = tdm_buffer_get_info(buffer);
218         TDM_RETURN_IF_FAIL(buf_info != NULL);
219
220         buf_info->backend_ref_count--;
221         if (buf_info->backend_ref_count > 0) {
222                 tbm_surface_internal_unref(buffer);
223                 return;
224         }
225
226 //      if (!tdm_thread_in_display_thread(syscall(SYS_gettid)))
227 //              TDM_NEVER_GET_HERE();
228
229         tbm_surface_internal_ref(buffer);
230         LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link)
231         func_info->release_func(buffer, func_info->user_data);
232         tbm_surface_internal_unref(buffer);
233
234         tbm_surface_internal_unref(buffer);
235 }
236
237 EXTERN tdm_error
238 tdm_buffer_add_destroy_handler(tbm_surface_h buffer,
239                                                            tdm_buffer_destroy_handler func, void *user_data)
240 {
241         tdm_buffer_info *buf_info;
242         tdm_buffer_func_info *func_info = NULL;
243
244         TDM_RETURN_VAL_IF_FAIL(buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
245         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
246
247         buf_info = tdm_buffer_get_info(buffer);
248         TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
249
250         LIST_FOR_EACH_ENTRY(func_info, &buf_info->destroy_funcs, link) {
251                 if (func_info->destroy_func == func && func_info->user_data == user_data) {
252                         TDM_ERR("can't add twice");
253                         return TDM_ERROR_BAD_REQUEST;
254                 }
255         }
256
257         func_info = calloc(1, sizeof(tdm_buffer_func_info));
258         TDM_RETURN_VAL_IF_FAIL(func_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
259
260         func_info->destroy_func = func;
261         func_info->user_data = user_data;
262
263         LIST_ADDTAIL(&func_info->link, &buf_info->destroy_funcs);
264
265         return TDM_ERROR_NONE;
266 }
267
268 EXTERN void
269 tdm_buffer_remove_destroy_handler(tbm_surface_h buffer,
270                                                                   tdm_buffer_destroy_handler func, void *user_data)
271 {
272         tdm_buffer_info *buf_info;
273         tdm_buffer_func_info *func_info = NULL, *next = NULL;
274
275         TDM_RETURN_IF_FAIL(buffer != NULL);
276         TDM_RETURN_IF_FAIL(func != NULL);
277
278         buf_info = tdm_buffer_get_info(buffer);
279         TDM_RETURN_IF_FAIL(buf_info != NULL);
280
281         LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->destroy_funcs, link) {
282                 if (func_info->destroy_func != func || func_info->user_data != user_data)
283                         continue;
284
285                 LIST_DEL(&func_info->link);
286                 free(func_info);
287
288                 return;
289         }
290 }
291