#include "tdm_private.h"
#include "tdm_list.h"
-typedef struct _tdm_buffer_func_info
-{
- tdm_buffer_release_handler func;
- void *user_data;
+static int tdm_buffer_key;
+#define TDM_BUFFER_KEY ((unsigned long)&tdm_buffer_key)
+
+typedef struct _tdm_buffer_func_info {
+ tdm_buffer_release_handler release_func;
+ tdm_buffer_destroy_handler destroy_func;
+ void *user_data;
- struct list_head link;
+ struct list_head link;
} tdm_buffer_func_info;
-typedef struct _tdm_buffer_info
+static void
+_tdm_buffer_destroy_info(void *user_data)
{
- tbm_surface_h buffer;
-
- /* frontend ref_count */
- int ref_count;
+ tdm_buffer_info *buf_info = (tdm_buffer_info *)user_data;
+ tdm_buffer_func_info *func_info = NULL, *next = NULL;
- /* backend ref_count */
- int backend_ref_count;
+ if (buf_info->backend_ref_count > 0) {
+ TDM_NEVER_GET_HERE();
+ if (tdm_debug_buffer)
+ TDM_INFO("%p", buf_info->buffer);
+ }
- struct list_head release_funcs;
- struct list_head link;
-} tdm_buffer_info;
+ LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link) {
+ LIST_DEL(&func_info->link);
+ free(func_info);
+ }
-static int buffer_list_init;
-static struct list_head buffer_list;
-
-EXTERN tdm_buffer*
-tdm_buffer_create(tbm_surface_h buffer, tdm_error *error)
-{
- tdm_buffer_info *buf_info;
+ LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->destroy_funcs, link)
+ func_info->destroy_func(buf_info->buffer, func_info->user_data);
- if (!buffer_list_init)
- {
- LIST_INITHEAD(&buffer_list);
- buffer_list_init = 1;
- }
+ LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->destroy_funcs, link) {
+ LIST_DEL(&func_info->link);
+ free(func_info);
+ }
- if (!buffer)
- {
- if (error)
- *error = TDM_ERROR_INVALID_PARAMETER;
+ if (tdm_debug_buffer)
+ TDM_INFO("%p destroyed", buf_info->buffer);
- TDM_ERR("'buffer != NULL' failed");
-
- return NULL;
- }
-
- buf_info = calloc(1, sizeof(tdm_buffer_info));
- if (!buf_info)
- {
- if (error)
- *error = TDM_ERROR_OUT_OF_MEMORY;
+ free(buf_info);
+}
- TDM_ERR("'buf_info != NULL' failed");
+INTERN tdm_buffer_info *
+tdm_buffer_get_info(tbm_surface_h buffer)
+{
+ tdm_buffer_info *buf_info = NULL;
- return NULL;
- }
+ if (!tbm_surface_internal_get_user_data(buffer, TDM_BUFFER_KEY,(void **)&buf_info)) {
+ buf_info = calloc(1, sizeof(tdm_buffer_info));
+ TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, NULL);
- buf_info->ref_count = 1;
+ buf_info->buffer = buffer;
- tbm_surface_internal_ref(buffer);
- buf_info->buffer = buffer;
+ LIST_INITHEAD(&buf_info->release_funcs);
+ LIST_INITHEAD(&buf_info->destroy_funcs);
+ LIST_INITHEAD(&buf_info->link);
- LIST_INITHEAD(&buf_info->release_funcs);
- LIST_ADDTAIL(&buf_info->link, &buffer_list);
+ if (!tbm_surface_internal_add_user_data(buffer, TDM_BUFFER_KEY, _tdm_buffer_destroy_info)) {
+ TDM_ERR("FAIL to create user_data for surface %p", buffer);
+ return NULL;
+ }
+ if (!tbm_surface_internal_set_user_data(buffer, TDM_BUFFER_KEY, buf_info)) {
+ TDM_ERR("FAIL to set user_data for surface %p", buffer);
+ return NULL;
+ }
- if (error)
- *error = TDM_ERROR_NONE;
+ if (tdm_debug_buffer)
+ TDM_INFO("%p created", buf_info->buffer);
+ }
- return (tdm_buffer*)buf_info;
+ return buf_info;
}
-EXTERN tdm_buffer*
-tdm_buffer_ref(tdm_buffer *buffer, tdm_error *error)
+EXTERN tdm_error
+tdm_buffer_add_release_handler(tbm_surface_h buffer,
+ tdm_buffer_release_handler func, void *user_data)
{
- tdm_buffer_info *buf_info;
+ tdm_buffer_info *buf_info;
+ tdm_buffer_func_info *func_info;
- if (!buffer)
- {
- if (error)
- *error = TDM_ERROR_INVALID_PARAMETER;
+ TDM_RETURN_VAL_IF_FAIL(buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
+ TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
- TDM_ERR("'buffer != NULL' failed");
+ buf_info = tdm_buffer_get_info(buffer);
+ TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
- return NULL;
- }
+ func_info = calloc(1, sizeof(tdm_buffer_func_info));
+ TDM_RETURN_VAL_IF_FAIL(func_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
- buf_info = buffer;
- buf_info->ref_count++;
+ func_info->release_func = func;
+ func_info->user_data = user_data;
- if (error)
- *error = TDM_ERROR_NONE;
+ LIST_ADD(&func_info->link, &buf_info->release_funcs);
- return buffer;
+ return TDM_ERROR_NONE;
}
EXTERN void
-tdm_buffer_unref(tdm_buffer *buffer)
+tdm_buffer_remove_release_handler(tbm_surface_h buffer,
+ tdm_buffer_release_handler func, void *user_data)
{
- tdm_buffer_info *buf_info;
- tdm_buffer_func_info *func_info = NULL, *next = NULL;
-
- if (!buffer)
- return;
-
- buf_info = buffer;
- buf_info->ref_count--;
+ tdm_buffer_info *buf_info;
+ tdm_buffer_func_info *func_info = NULL, *next = NULL;
- if (buf_info->ref_count > 0)
- return;
+ TDM_RETURN_IF_FAIL(buffer != NULL);
+ TDM_RETURN_IF_FAIL(func != NULL);
- /* Before ref_count become 0, all backend reference should be removed */
- TDM_WARNING_IF_FAIL(buf_info->backend_ref_count == 0);
+ buf_info = tdm_buffer_get_info(buffer);
+ TDM_RETURN_IF_FAIL(buf_info != NULL);
- LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link)
- {
- LIST_DEL(&func_info->link);
- free(func_info);
- }
+ LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link) {
+ if (func_info->release_func != func || func_info->user_data != user_data)
+ continue;
- LIST_DEL(&buf_info->link);
+ LIST_DEL(&func_info->link);
+ free(func_info);
- free(buf_info);
+ return;
+ }
}
-EXTERN tdm_error
-tdm_buffer_add_release_handler(tdm_buffer *buffer,
- tdm_buffer_release_handler func, void *user_data)
-{
- tdm_buffer_info *buf_info;
- tdm_buffer_func_info *func_info;
- TDM_RETURN_VAL_IF_FAIL(buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
- TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
+EXTERN tbm_surface_h
+tdm_buffer_ref_backend(tbm_surface_h buffer)
+{
+ tdm_buffer_info *buf_info;
- func_info = calloc(1, sizeof(tdm_buffer_func_info));
- TDM_RETURN_VAL_IF_FAIL(func_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
+ TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
- func_info->func = func;
- func_info->user_data = user_data;
+ buf_info = tdm_buffer_get_info(buffer);
+ TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, NULL);
- buf_info = buffer;
- LIST_ADD(&func_info->link, &buf_info->release_funcs);
+ buf_info->backend_ref_count++;
+ tbm_surface_internal_ref(buffer);
- return TDM_ERROR_NONE;
+ return buffer;
}
EXTERN void
-tdm_buffer_remove_release_handler(tdm_buffer *buffer, tdm_buffer_release_handler func, void *user_data)
+tdm_buffer_unref_backend(tbm_surface_h buffer)
{
- tdm_buffer_info *buf_info;
- tdm_buffer_func_info *func_info = NULL, *next = NULL;
+ tdm_buffer_info *buf_info;
+ tdm_buffer_func_info *func_info = NULL, *next = NULL;
- TDM_RETURN_IF_FAIL(buffer != NULL);
- TDM_RETURN_IF_FAIL(func != NULL);
+ TDM_RETURN_IF_FAIL(buffer != NULL);
- buf_info = buffer;
- LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link)
- {
- if (func_info->func != func || func_info->user_data != user_data)
- continue;
+ buf_info = tdm_buffer_get_info(buffer);
+ TDM_RETURN_IF_FAIL(buf_info != NULL);
- LIST_DEL(&func_info->link);
- free(func_info);
+ buf_info->backend_ref_count--;
+ if (buf_info->backend_ref_count > 0) {
+ tbm_surface_internal_unref(buffer);
+ return;
+ }
- return;
- }
-}
+ if (!tdm_thread_in_display_thread(syscall(SYS_gettid)))
+ TDM_NEVER_GET_HERE();
+
+ LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link) {
+ tbm_surface_internal_ref(buffer);
+ func_info->release_func(buffer, func_info->user_data);
+ tbm_surface_internal_unref(buffer);
+ }
+ tbm_surface_internal_unref(buffer);
+}
-INTERN tdm_buffer*
-tdm_buffer_ref_backend(tdm_buffer *buffer)
+EXTERN tdm_error
+tdm_buffer_add_destroy_handler(tbm_surface_h buffer,
+ tdm_buffer_destroy_handler func, void *user_data)
{
- tdm_buffer_info *buf_info;
+ tdm_buffer_info *buf_info;
+ tdm_buffer_func_info *func_info;
- TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
+ TDM_RETURN_VAL_IF_FAIL(buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
+ TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
- buf_info = buffer;
- buf_info->backend_ref_count++;
+ buf_info = tdm_buffer_get_info(buffer);
+ TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
- return buffer;
+ func_info = calloc(1, sizeof(tdm_buffer_func_info));
+ TDM_RETURN_VAL_IF_FAIL(func_info != NULL, TDM_ERROR_OUT_OF_MEMORY);
+
+ func_info->destroy_func = func;
+ func_info->user_data = user_data;
+
+ LIST_ADD(&func_info->link, &buf_info->destroy_funcs);
+
+ return TDM_ERROR_NONE;
}
-INTERN void
-tdm_buffer_unref_backend(tdm_buffer *buffer)
+EXTERN void
+tdm_buffer_remove_destroy_handler(tbm_surface_h buffer,
+ tdm_buffer_destroy_handler func, void *user_data)
{
- tdm_buffer_info *buf_info;
- tdm_buffer_func_info *func_info = NULL, *next = NULL;
+ tdm_buffer_info *buf_info;
+ tdm_buffer_func_info *func_info = NULL, *next = NULL;
+
+ TDM_RETURN_IF_FAIL(buffer != NULL);
+ TDM_RETURN_IF_FAIL(func != NULL);
- TDM_RETURN_IF_FAIL(buffer != NULL);
+ buf_info = tdm_buffer_get_info(buffer);
+ TDM_RETURN_IF_FAIL(buf_info != NULL);
- buf_info = buffer;
- buf_info->backend_ref_count--;
+ LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->destroy_funcs, link) {
+ if (func_info->destroy_func != func || func_info->user_data != user_data)
+ continue;
- if (buf_info->backend_ref_count > 0)
- return;
+ LIST_DEL(&func_info->link);
+ free(func_info);
- LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info->release_funcs, link)
- func_info->func(buffer, func_info->user_data);
+ return;
+ }
}
INTERN tbm_surface_h
-tdm_buffer_get_surface(tdm_buffer *buffer)
+tdm_buffer_list_get_first_entry(struct list_head *list)
{
- tdm_buffer_info *buf_info = buffer;
+ tdm_buffer_info *buf_info = NULL;
- TDM_RETURN_VAL_IF_FAIL(buf_info != NULL, NULL);
+ TDM_RETURN_VAL_IF_FAIL(list != NULL, NULL);
- return buf_info->buffer;
+ buf_info = container_of((list)->next, buf_info, link);
+
+ return buf_info->buffer;
}
-INTERN tdm_buffer*
-tdm_buffer_get(tbm_surface_h buffer)
+INTERN void
+tdm_buffer_list_dump(struct list_head *list)
{
- tdm_buffer_info *found;
-
- TDM_RETURN_VAL_IF_FAIL(buffer != NULL, NULL);
-
- if (!buffer_list_init)
- {
- LIST_INITHEAD(&buffer_list);
- buffer_list_init = 1;
- }
-
- LIST_FOR_EACH_ENTRY(found, &buffer_list, link)
- {
- if (found->buffer == buffer)
- return found;
- }
-
- return NULL;
+ tdm_buffer_info *buf_info = NULL;
+ char str[256], *p;
+ int len = sizeof (str);
+
+ TDM_RETURN_IF_FAIL(list != NULL);
+
+ p = str;
+ LIST_FOR_EACH_ENTRY(buf_info, list, link) {
+ if (len > 0) {
+ int l = snprintf(p, len, " %p", buf_info->buffer);
+ p += l;
+ len -= l;
+ }
+ else
+ break;
+ }
+
+ TDM_INFO("\t %s", str);
}