12 #include "Ecore_Buffer.h"
13 #include "ecore_buffer_private.h"
15 typedef struct _Ecore_Buffer_Module Ecore_Buffer_Module;
16 typedef struct _Ecore_Buffer_Cb_Data Ecore_Buffer_Cb_Data;
18 struct _Ecore_Buffer_Module
20 Ecore_Buffer_Backend *be;
21 Ecore_Buffer_Module_Data data;
31 Ecore_Buffer_Data buffer_data;
32 Ecore_Buffer_Module *bm;
35 Eina_Inlist *free_callbacks;
38 struct _Ecore_Buffer_Cb_Data
45 static Eina_Hash *_backends;
46 static Eina_Array *_modules;
47 static int _ecore_buffer_init_count = 0;
48 static int _ecore_buffer_log_dom = -1;
53 #define ERR(...) EINA_LOG_DOM_ERR(_ecore_buffer_log_dom, __VA_ARGS__)
58 #define DBG(...) EINA_LOG_DOM_DBG(_ecore_buffer_log_dom, __VA_ARGS__)
60 #ifndef PACKAGE_LIB_DIR
61 #define PACKAGE_LIB_DIR ""
64 #define MODULE_ARCH ""
67 static Ecore_Buffer_Module *
68 _ecore_buffer_get_backend(const char *name)
70 Ecore_Buffer_Module *bm = NULL;
71 Eina_Iterator *backend_name_itr;
72 const char *backend_name = NULL;
76 if (backend_name == NULL)
78 backend_name = (const char*)getenv("ECORE_BUFFER_ENGINE");
81 backend_name_itr = eina_hash_iterator_data_new(_backends);
83 (eina_iterator_next(backend_name_itr, (void **)&bm)));
84 eina_iterator_free(backend_name_itr);
88 bm = eina_hash_find(_backends, backend_name);
90 if ((!bm) || (!bm->be) || (!bm->be->init))
93 bm->data = bm->be->init(NULL, NULL);
99 _ecore_buffer_backends_free(const Eina_Hash *hash EINA_UNUSED, const void *key EINA_UNUSED, void *data, void *fdata EINA_UNUSED)
101 Ecore_Buffer_Module *bm = data;
107 bm->be->shutdown(bm->data);
113 ecore_buffer_register(Ecore_Buffer_Backend *be)
115 Ecore_Buffer_Module *bm;
117 EINA_SAFETY_ON_NULL_RETURN_VAL(be, 0);
119 bm = calloc(1, sizeof(Ecore_Buffer_Module));
126 return eina_hash_add(_backends, be->name, bm);
130 ecore_buffer_unregister(Ecore_Buffer_Backend *be)
132 Ecore_Buffer_Module *bm;
134 EINA_SAFETY_ON_NULL_RETURN(be);
136 bm = eina_hash_find(_backends, be->name);
140 eina_hash_del(_backends, be->name, bm);
145 ecore_buffer_init(void)
149 if (++_ecore_buffer_init_count > 1)
152 _ecore_buffer_log_dom = eina_log_domain_register("ecore_buffer", EINA_COLOR_BLUE);
153 if (_ecore_buffer_log_dom < 0)
155 EINA_LOG_ERR("Could not register log domain: ecore_buffer");
159 _backends = eina_hash_string_superfast_new(NULL);
161 /* dynamic backends */
162 _modules = eina_module_arch_list_get(NULL,
163 PACKAGE_LIB_DIR "/ecore_buffer/modules",
166 path = eina_module_symbol_path_get((const void *)ecore_buffer_init,
167 "/ecore_buffer/modules");
169 _modules = eina_module_arch_list_get(_modules, path, MODULE_ARCH);
173 /* fallback using module where in build directory */
175 (eina_array_count(_modules) == 0))
177 ERR("No available module in library directy: %s",
178 PACKAGE_LIB_DIR "/ecore_buffer/modules");
179 ERR("Fallback to load module where in build directory :%s",
180 PACKAGE_BUILD_DIR "/src/modules/");
181 _modules = eina_module_list_get(NULL,
182 PACKAGE_BUILD_DIR "/src/modules/",
183 EINA_TRUE, NULL, NULL);
187 (eina_array_count(_modules) == 0))
189 ERR("no ecore_buffer modules able to be loaded.");
190 eina_hash_free(_backends);
191 eina_log_domain_unregister(_ecore_buffer_log_dom);
192 _ecore_buffer_log_dom = -1;
196 // XXX: MODFIX: do not list ALL modules and load them ALL! this is
197 // wrong. load the module we need WHEN we need it (by name etc. etc.
199 eina_module_list_load(_modules);
204 _ecore_buffer_init_count--;
209 ecore_buffer_shutdown(void)
211 if (_ecore_buffer_init_count < 1)
213 WARN("Ecore_Buffer shut down called without init");
217 if (--_ecore_buffer_init_count != 0)
220 /* dynamic backends */
221 eina_hash_foreach(_backends, _ecore_buffer_backends_free, NULL);
223 eina_module_list_free(_modules);
225 eina_array_free(_modules);
228 eina_hash_free(_backends);
230 eina_log_domain_unregister(_ecore_buffer_log_dom);
231 _ecore_buffer_log_dom = -1;
237 ecore_buffer_new(const char *engine, unsigned int width, unsigned int height, Ecore_Buffer_Format format, unsigned int flags)
239 Ecore_Buffer_Module *bm;
243 bm = _ecore_buffer_get_backend(engine);
246 ERR("Failed to get backend: %s", engine);
250 EINA_SAFETY_ON_NULL_RETURN_VAL(bm->be, NULL);
252 if (!bm->be->buffer_alloc)
254 ERR("Not supported create buffer");
258 bo = calloc(1, sizeof(Ecore_Buffer));
262 bo_data = bm->be->buffer_alloc(bm->data, width, height, format, flags);
274 bo->buffer_data = bo_data;
280 ecore_buffer_new_with_tbm_surface(const char *engine, void *tbm_surface, unsigned int flags)
282 Ecore_Buffer_Module *bm;
285 Ecore_Buffer_Format format = 0;
288 bm = _ecore_buffer_get_backend(engine);
291 ERR("Failed to get Backend: %s", engine);
295 EINA_SAFETY_ON_NULL_RETURN_VAL(bm->be, NULL);
297 if (!bm->be->buffer_alloc_with_tbm_surface)
299 ERR("Not supported create buffer with tbm_surface");
303 bo = calloc(1, sizeof(Ecore_Buffer));
307 bo_data = bm->be->buffer_alloc_with_tbm_surface(bm->data, tbm_surface,
308 &w, &h, &format, flags);
317 bo->buffer_data = bo_data;
326 ecore_buffer_free(Ecore_Buffer *buf)
328 Ecore_Buffer_Cb_Data *free_cb;
330 EINA_SAFETY_ON_NULL_RETURN(buf);
333 while (buf->free_callbacks)
335 free_cb = EINA_INLIST_CONTAINER_GET(buf->free_callbacks, Ecore_Buffer_Cb_Data);
336 buf->free_callbacks = eina_inlist_remove(buf->free_callbacks, buf->free_callbacks);
338 free_cb->cb(buf, free_cb->data);
342 EINA_SAFETY_ON_NULL_RETURN(buf->bm);
343 EINA_SAFETY_ON_NULL_RETURN(buf->bm->be);
344 EINA_SAFETY_ON_NULL_RETURN(buf->bm->be->buffer_free);
346 buf->bm->be->buffer_free(buf->bm->data, buf->buffer_data);
350 eina_hash_free(buf->data);
356 ecore_buffer_pixmap_get(Ecore_Buffer *buf)
358 EINA_SAFETY_ON_NULL_RETURN_VAL(buf, 0);
359 EINA_SAFETY_ON_NULL_RETURN_VAL(buf->bm, 0);
360 EINA_SAFETY_ON_NULL_RETURN_VAL(buf->bm->be, 0);
362 if (!buf->bm->be->pixmap_get)
365 return buf->bm->be->pixmap_get(buf->bm->data, buf->buffer_data);
369 ecore_buffer_tbm_surface_get(Ecore_Buffer *buf)
371 EINA_SAFETY_ON_NULL_RETURN_VAL(buf, NULL);
372 EINA_SAFETY_ON_NULL_RETURN_VAL(buf->bm, NULL);
373 EINA_SAFETY_ON_NULL_RETURN_VAL(buf->bm->be, NULL);
375 if (!buf->bm->be->tbm_surface_get)
377 ERR("TBM is not supported\n");
381 return buf->bm->be->tbm_surface_get(buf->bm->data, buf->buffer_data);
385 ecore_buffer_size_get(Ecore_Buffer *buf, unsigned int *width, unsigned int *height)
387 EINA_SAFETY_ON_NULL_RETURN_VAL(buf, EINA_FALSE);
389 if (width) *width = buf->width;
390 if (height) *height = buf->height;
396 ecore_buffer_format_get(Ecore_Buffer *buf)
398 EINA_SAFETY_ON_NULL_RETURN_VAL(buf, 0);
404 ecore_buffer_flags_get(Ecore_Buffer *buf)
406 EINA_SAFETY_ON_NULL_RETURN_VAL(buf, 0);
412 ecore_buffer_free_callback_add(Ecore_Buffer *buf, Ecore_Buffer_Cb func, void *data)
414 EINA_SAFETY_ON_NULL_RETURN(buf);
415 EINA_SAFETY_ON_NULL_RETURN(func);
417 Ecore_Buffer_Cb_Data *free_cb;
419 free_cb = calloc(sizeof(Ecore_Buffer_Cb_Data), 1);
424 free_cb->data = data;
425 buf->free_callbacks = eina_inlist_append(buf->free_callbacks, EINA_INLIST_GET(free_cb));
429 ecore_buffer_free_callback_remove(Ecore_Buffer *buf, Ecore_Buffer_Cb func, void *data)
431 Ecore_Buffer_Cb_Data *free_cb;
433 EINA_SAFETY_ON_NULL_RETURN(buf);
434 EINA_SAFETY_ON_NULL_RETURN(func);
436 if (buf->free_callbacks)
439 EINA_INLIST_FOREACH_SAFE(buf->free_callbacks, itrn, free_cb)
441 if (free_cb->cb == func && free_cb->data == data)
443 buf->free_callbacks =
444 eina_inlist_remove(buf->free_callbacks,
445 EINA_INLIST_GET(free_cb));
453 _ecore_buffer_engine_name_get(Ecore_Buffer *buf)
455 EINA_SAFETY_ON_NULL_RETURN_VAL(buf, 0);
456 EINA_SAFETY_ON_NULL_RETURN_VAL(buf->bm, 0);
457 EINA_SAFETY_ON_NULL_RETURN_VAL(buf->bm->be, 0);
459 return buf->bm->be->name;
463 _ecore_buffer_export(Ecore_Buffer *buf, int *id)
465 Ecore_Export_Type type = EXPORT_TYPE_INVALID;
468 EINA_SAFETY_ON_NULL_RETURN_VAL(buf, type);
469 EINA_SAFETY_ON_NULL_RETURN_VAL(buf->bm, type);
470 EINA_SAFETY_ON_NULL_RETURN_VAL(buf->bm->be, type);
472 if (!buf->bm->be->buffer_export)
475 type = buf->bm->be->buffer_export(buf->bm->data, buf->buffer_data, &ret_id);
477 if (id) *id = ret_id;
483 _ecore_buffer_import(const char *engine, int width, int height, Ecore_Buffer_Format format, Ecore_Export_Type type, int export_id, unsigned int flags)
485 Ecore_Buffer_Module *bm;
489 bm = _ecore_buffer_get_backend(engine);
492 ERR("Filed to get Backend: %s", engine);
496 EINA_SAFETY_ON_NULL_RETURN_VAL(bm->be, NULL);
498 if (!bm->be->buffer_import)
500 ERR("Not supported import buffer");
504 bo = calloc(1, sizeof(Ecore_Buffer));
508 bo_data = bm->be->buffer_import(bm->data, width, height, format, type, export_id, flags);
520 bo->buffer_data = bo_data;