1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
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>
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:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
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.
34 **************************************************************************/
40 #include "tdm_private.h"
42 static int tdm_buffer_key;
43 #define TDM_BUFFER_KEY ((unsigned long)&tdm_buffer_key)
45 typedef struct _tdm_buffer_func_info {
46 tdm_buffer_release_handler release_func;
47 tdm_buffer_destroy_handler destroy_func;
50 struct list_head link;
51 } tdm_buffer_func_info;
54 _tdm_buffer_destroy_info(void *user_data)
56 tdm_buffer_info *buf_info = (tdm_buffer_info *)user_data;
57 tdm_buffer_func_info *func_info = NULL, *next = NULL;
59 if (buf_info->backend_ref_count > 0) {
61 if (tdm_debug_module & TDM_DEBUG_BUFFER)
62 TDM_INFO("%p", buf_info->buffer);
65 LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link) {
66 LIST_DEL(&func_info->link);
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);
73 LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->destroy_funcs, link) {
74 LIST_DEL(&func_info->link);
78 if (tdm_debug_module & TDM_DEBUG_BUFFER)
79 TDM_INFO("%p destroyed", buf_info->buffer);
84 INTERN tdm_buffer_info *
85 tdm_buffer_get_info(tbm_surface_h buffer)
87 tdm_buffer_info *buf_info = NULL;
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);
93 buf_info->buffer = buffer;
95 LIST_INITHEAD(&buf_info->release_funcs);
96 LIST_INITHEAD(&buf_info->destroy_funcs);
97 LIST_INITHEAD(&buf_info->link);
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);
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);
111 if (tdm_debug_module & TDM_DEBUG_BUFFER)
112 TDM_INFO("%p created", buf_info->buffer);
119 tdm_buffer_add_release_handler(tbm_surface_h buffer,
120 tdm_buffer_release_handler func, void *user_data)
122 tdm_buffer_info *buf_info;
123 tdm_buffer_func_info *func_info;
125 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
126 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
128 buf_info = tdm_buffer_get_info(buffer);
129 TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
131 func_info = calloc(1, sizeof(tdm_buffer_func_info));
132 TDM_RETURN_VAL_IF_FAIL(func_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
134 func_info->release_func = func;
135 func_info->user_data = user_data;
137 LIST_ADDTAIL(&func_info->link, &buf_info->release_funcs);
139 return TDM_ERROR_NONE;
143 tdm_buffer_remove_release_handler(tbm_surface_h buffer,
144 tdm_buffer_release_handler func, void *user_data)
146 tdm_buffer_info *buf_info;
147 tdm_buffer_func_info *func_info = NULL, *next = NULL;
149 TDM_RETURN_IF_FAIL(buffer != NULL);
150 TDM_RETURN_IF_FAIL(func != NULL);
152 buf_info = tdm_buffer_get_info(buffer);
153 TDM_RETURN_IF_FAIL(buf_info != NULL);
155 LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link) {
156 if (func_info->release_func != func || func_info->user_data != user_data)
159 LIST_DEL(&func_info->link);
167 tdm_buffer_remove_release_handler_internal(tbm_surface_h buffer)
169 tdm_buffer_info *buf_info;
170 tdm_buffer_func_info *func_info = NULL, *next = NULL;
172 TDM_RETURN_IF_FAIL(buffer != NULL);
174 buf_info = tdm_buffer_get_info(buffer);
175 TDM_RETURN_IF_FAIL(buf_info != NULL);
177 LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link) {
179 LIST_DEL(&func_info->link);
187 tdm_buffer_ref_backend(tbm_surface_h buffer)
189 tdm_buffer_info *buf_info;
191 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
193 buf_info = tdm_buffer_get_info(buffer);
194 TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, NULL);
196 buf_info->backend_ref_count++;
197 tbm_surface_internal_ref(buffer);
203 tdm_buffer_unref_backend(tbm_surface_h buffer)
205 tdm_buffer_info *buf_info;
206 tdm_buffer_func_info *func_info = NULL, *next = NULL;
208 TDM_RETURN_IF_FAIL(buffer != NULL);
210 buf_info = tdm_buffer_get_info(buffer);
211 TDM_RETURN_IF_FAIL(buf_info != NULL);
213 buf_info->backend_ref_count--;
214 if (buf_info->backend_ref_count > 0) {
215 tbm_surface_internal_unref(buffer);
219 // if (!tdm_thread_in_display_thread(syscall(SYS_gettid)))
220 // TDM_NEVER_GET_HERE();
222 tbm_surface_internal_ref(buffer);
223 LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link)
224 func_info->release_func(buffer, func_info->user_data);
225 tbm_surface_internal_unref(buffer);
227 tbm_surface_internal_unref(buffer);
231 tdm_buffer_add_destroy_handler(tbm_surface_h buffer,
232 tdm_buffer_destroy_handler func, void *user_data)
234 tdm_buffer_info *buf_info;
235 tdm_buffer_func_info *func_info;
237 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
238 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
240 buf_info = tdm_buffer_get_info(buffer);
241 TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
243 func_info = calloc(1, sizeof(tdm_buffer_func_info));
244 TDM_RETURN_VAL_IF_FAIL(func_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
246 func_info->destroy_func = func;
247 func_info->user_data = user_data;
249 LIST_ADDTAIL(&func_info->link, &buf_info->destroy_funcs);
251 return TDM_ERROR_NONE;
255 tdm_buffer_remove_destroy_handler(tbm_surface_h buffer,
256 tdm_buffer_destroy_handler func, void *user_data)
258 tdm_buffer_info *buf_info;
259 tdm_buffer_func_info *func_info = NULL, *next = NULL;
261 TDM_RETURN_IF_FAIL(buffer != NULL);
262 TDM_RETURN_IF_FAIL(func != NULL);
264 buf_info = tdm_buffer_get_info(buffer);
265 TDM_RETURN_IF_FAIL(buf_info != NULL);
267 LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->destroy_funcs, link) {
268 if (func_info->destroy_func != func || func_info->user_data != user_data)
271 LIST_DEL(&func_info->link);