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 **************************************************************************/
41 #include "tdm_private.h"
44 typedef struct _tdm_buffer_func_info
46 tdm_buffer_release_handler func;
49 struct list_head link;
50 } tdm_buffer_func_info;
52 typedef struct _tdm_buffer_info
56 /* frontend ref_count */
59 /* backend ref_count */
60 int backend_ref_count;
62 struct list_head release_funcs;
63 struct list_head link;
66 static int buffer_list_init;
67 static struct list_head buffer_list;
70 tdm_buffer_create(tbm_surface_h buffer, tdm_error *error)
72 tdm_buffer_info *buf_info;
74 if (!buffer_list_init)
76 LIST_INITHEAD(&buffer_list);
83 *error = TDM_ERROR_INVALID_PARAMETER;
85 TDM_ERR("'buffer != NULL' failed");
90 buf_info = calloc(1, sizeof(tdm_buffer_info));
94 *error = TDM_ERROR_OUT_OF_MEMORY;
96 TDM_ERR("'buf_info != NULL' failed");
101 buf_info->ref_count = 1;
103 tbm_surface_internal_ref(buffer);
104 buf_info->buffer = buffer;
106 LIST_INITHEAD(&buf_info->release_funcs);
107 LIST_ADDTAIL(&buf_info->link, &buffer_list);
110 *error = TDM_ERROR_NONE;
112 return (tdm_buffer*)buf_info;
116 tdm_buffer_ref(tdm_buffer *buffer, tdm_error *error)
118 tdm_buffer_info *buf_info;
123 *error = TDM_ERROR_INVALID_PARAMETER;
125 TDM_ERR("'buffer != NULL' failed");
131 buf_info->ref_count++;
134 *error = TDM_ERROR_NONE;
140 tdm_buffer_unref(tdm_buffer *buffer)
142 tdm_buffer_info *buf_info;
143 tdm_buffer_func_info *func_info = NULL, *next = NULL;
149 buf_info->ref_count--;
151 if (buf_info->ref_count > 0)
154 /* Before ref_count become 0, all backend reference should be removed */
155 TDM_WARNING_IF_FAIL(buf_info->backend_ref_count == 0);
157 LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link)
159 LIST_DEL(&func_info->link);
163 LIST_DEL(&buf_info->link);
165 tbm_surface_internal_unref(buf_info->buffer);
171 tdm_buffer_add_release_handler(tdm_buffer *buffer,
172 tdm_buffer_release_handler func, void *user_data)
174 tdm_buffer_info *buf_info;
175 tdm_buffer_func_info *func_info;
177 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
178 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
180 func_info = calloc(1, sizeof(tdm_buffer_func_info));
181 TDM_RETURN_VAL_IF_FAIL(func_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
183 func_info->func = func;
184 func_info->user_data = user_data;
187 LIST_ADD(&func_info->link, &buf_info->release_funcs);
189 return TDM_ERROR_NONE;
193 tdm_buffer_remove_release_handler(tdm_buffer *buffer, tdm_buffer_release_handler func, void *user_data)
195 tdm_buffer_info *buf_info;
196 tdm_buffer_func_info *func_info = NULL, *next = NULL;
198 TDM_RETURN_IF_FAIL(buffer != NULL);
199 TDM_RETURN_IF_FAIL(func != NULL);
202 LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link)
204 if (func_info->func != func || func_info->user_data != user_data)
207 LIST_DEL(&func_info->link);
216 tdm_buffer_ref_backend(tdm_buffer *buffer)
218 tdm_buffer_info *buf_info;
220 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
223 buf_info->backend_ref_count++;
229 tdm_buffer_unref_backend(tdm_buffer *buffer)
231 tdm_buffer_info *buf_info;
232 tdm_buffer_func_info *func_info = NULL, *next = NULL;
234 TDM_RETURN_IF_FAIL(buffer != NULL);
237 buf_info->backend_ref_count--;
239 if (buf_info->backend_ref_count > 0)
242 LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link)
243 func_info->func(buffer, func_info->user_data);
247 tdm_buffer_get_surface(tdm_buffer *buffer)
249 tdm_buffer_info *buf_info = buffer;
251 TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, NULL);
253 return buf_info->buffer;
257 tdm_buffer_get(tbm_surface_h buffer)
259 tdm_buffer_info *found;
261 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
263 if (!buffer_list_init)
265 LIST_INITHEAD(&buffer_list);
266 buffer_list_init = 1;
269 LIST_FOR_EACH_ENTRY(found, &buffer_list, link)
271 if (found->buffer == buffer)