11 #if defined(__GNUC__) && __GNUC__ >= 4
12 # define TPL_API __attribute__ ((visibility("default")))
16 #define TPL_ASSERT(expr) assert(expr)
17 #define TPL_INLINE __inline__
18 #define TPL_IGNORE(x) (void)x
20 #ifdef ARM_ATOMIC_OPERATION
21 #define TPL_DMB() __asm__ volatile("dmb sy" : : : "memory")
22 #else /* ARM_ATOMIC_OPERATION */
23 #define TPL_DMB() __asm__ volatile("" : : : "memory")
24 #endif /* ARM_ATOMIC_OPERATION */
28 #define TRACE_BEGIN(name,...) traceBegin(TTRACE_TAG_GRAPHICS, name, ##__VA_ARGS__)
29 #define TRACE_END() traceEnd(TTRACE_TAG_GRAPHICS)
30 #define TRACE_ASYNC_BEGIN(key, name,...) traceAsyncBegin(TTRACE_TAG_GRAPHICS, key, name, ##__VA_ARGS__)
31 #define TRACE_ASYNC_END(key, name,...) traceAsyncEnd(TTRACE_TAG_GRAPHICS, key, name, ##__VA_ARGS__)
32 #define TRACE_COUNTER(value, name,...) traceCounter(TTRACE_TAG_GRAPHICS, value, name, ##__VA_ARGS__)
33 #define TRACE_MARK(name,...) traceMark(TTRACE_TAG_GRAPHICS, name, ##__VA_ARGS__)
34 #else /* TTRACE_ENABLE */
35 #define TRACE_BEGIN(name,...)
37 #define TRACE_ASYNC_BEGIN(key, name,...)
38 #define TRACE_ASYNC_END(key, name,...)
39 #define TRACE_COUNTER(value, name,...)
40 #define TRACE_MARK(name,...)
41 #endif /* TTRACE_ENABLE */
46 #include <sys/syscall.h>
47 #include <sys/types.h>
51 #include <tbm_surface.h>
52 #include <tbm_surface_internal.h>
54 /* 0:uninitialized, 1:initialized,no log, 2:user log */
55 extern unsigned int tpl_log_lvl;
56 extern unsigned int tpl_log_initialized;
57 extern unsigned int tpl_getenv_enabled;
58 extern unsigned int tpl_dump_lvl;
60 inline char *tpl_getenv(const char *name)
62 if (tpl_getenv_enabled)
69 #define FONT_DEFAULT "\033[0m" /* for reset to default color */
70 #define FONT_RED "\033[31m" /* for error logs */
71 #define FONT_YELLOW "\033[33m" /* for warning logs */
72 #define FONT_GREEN "\033[32m" /* for frontend API logs */
73 #define FONT_BLUE "\033[34m" /* for backend logs */
74 #define FONT_MAGENTA "\033[35m" /* for debug logs */
75 #define FONT_CYAN "\033[36m" /* for thread logs */
76 #define BG_COLOR_GREEN "\033[0;42m"
78 #ifdef DLOG_DEFAULT_ENABLE
83 #ifdef DLOG_DEFAULT_ENABLE
84 #define tpl_log_f(t, f, x...) LOGI(FONT_GREEN t FONT_DEFAULT " " f, ##x)
85 #define tpl_log_b(t, f, x...) LOGI(FONT_BLUE t FONT_DEFAULT " " f, ##x)
86 #define tpl_log_d(t, f, x...) LOGI(FONT_MAGENTA t FONT_DEFAULT " " f, ##x)
87 #define tpl_log_t(t, f, x...) LOGI(FONT_CYAN t FONT_DEFAULT " " f, ##x)
88 #define tpl_log_i(t, f, x...) LOGI(BG_COLOR_GREEN t FONT_DEFAULT " " f, ##x)
89 #define tpl_log_e(t, f, x...) LOGE(FONT_RED t " " f FONT_DEFAULT, ##x)
90 #define tpl_log_w(t, f, x...) LOGW(FONT_YELLOW t " " f FONT_DEFAULT, ##x)
91 #else /* DLOG_DEFAULT_ENABLE */
92 #define tpl_log_f(t, f, x...) \
93 fprintf(stderr, FONT_GREEN t FONT_DEFAULT "[(tid:%li)(%s)] " f "\n", \
94 syscall(SYS_gettid), __func__, ##x)
95 #define tpl_log_b(t, f, x...) \
96 fprintf(stderr, FONT_BLUE t FONT_DEFAULT "[(tid:%li)(%s)] " f "\n", \
97 syscall(SYS_gettid), __func__, ##x)
98 #define tpl_log_d(t, f, x...) \
99 fprintf(stderr, FONT_MAGENTA t FONT_DEFAULT "[(tid:%li)(%s)] " f "\n",\
100 syscall(SYS_gettid), __func__, ##x)
101 #define tpl_log_t(t, f, x...) \
102 fprintf(stderr, FONT_CYAN t FONT_DEFAULT "[(tid:%li)(%s)]" f "\n", \
103 syscall(SYS_gettid), __func__, ##x)
104 #define tpl_log_i(t, f, x...) \
105 fprintf(stderr, BG_COLOR_GREEN t FONT_DEFAULT "[(tid:%li)(%s)]" f "\n", \
106 syscall(SYS_gettid), __func__, ##x)
107 #define tpl_log_e(t, f, x...) \
108 fprintf(stderr, FONT_RED t "[(tid:%li)(%s)] " f FONT_DEFAULT "\n", \
109 syscall(SYS_gettid), __func__, ##x)
110 #define tpl_log_w(t, f, x...) \
111 fprintf(stderr, FONT_YELLOW t "[(tid:%li)(%s)] " f FONT_DEFAULT "\n",\
112 syscall(SYS_gettid), __func__, ##x)
113 #endif /* DLOG_DEFAULT_ENABLE */
115 #define TPL_INFO(tag, f, x...) tpl_log_i(tag, f, ##x)
116 #define TPL_ERR(f, x...) tpl_log_e("[TPL_ERROR]", f, ##x)
117 #define TPL_WARN(f, x...) tpl_log_w("[TPL_WARNING]", f, ##x)
119 #ifdef LOG_DEFAULT_ENABLE
120 #define TPL_LOG_F(f, x...) tpl_log_f("[TPL_F]", f, ##x)
121 #define TPL_LOG_B(b, f, x...) tpl_log_b("[TPL_" b "]", f, ##x)
122 #define TPL_DEBUG(f, x...) tpl_log_d("[TPL_DEBUG]", f, ##x)
123 #define TPL_LOG_T(b, f, x...) \
125 if ((int)getpid() != (int)syscall(SYS_gettid)) \
126 tpl_log_t("[TPL_" b "_T]", f, ##x); \
128 tpl_log_b("[TPL_" b "]", f, ##x); \
130 #else /* LOG_DEFAULT_ENABLE */
133 * export TPL_LOG_LEVEL=[lvl]
135 * 0: initialized but do not print any log.
136 * 1: enable only frontend API logs. TPL_LOG_F
137 * 2: enable also backend API logs. TPL_LOG_B (detail info about window system)
138 * 3: enable also debug logs. TPL_DEBUG
139 * 4: enable only debug logs. TPL_DEBUG
143 if (!tpl_log_initialized) \
145 char *env = tpl_getenv("TPL_LOG_LEVEL"); \
149 tpl_log_lvl = atoi(env); \
150 tpl_log_initialized = 1; \
154 #define TPL_LOG_F(f, x...) \
157 if (tpl_log_lvl > 0 && tpl_log_lvl < 4) \
158 tpl_log_f("[TPL_F]", f, ##x); \
161 #define TPL_LOG_B(b, f, x...) \
164 if (tpl_log_lvl > 1 && tpl_log_lvl < 4) \
165 tpl_log_b("[TPL_" b "]", f, ##x); \
168 #define TPL_LOG_T(b, f, x...) \
171 if (tpl_log_lvl > 1 && tpl_log_lvl < 4) { \
172 if ((int)getpid() != (int)syscall(SYS_gettid)) \
173 tpl_log_t("[TPL_" b "_T]", f, ##x); \
175 tpl_log_b("[TPL_" b "]", f, ##x); \
179 #define TPL_DEBUG(f, x...) \
182 if (tpl_log_lvl > 2) \
183 tpl_log_d("[TPL_DEBUG]", f, ##x); \
185 #endif /* LOG_DEFAULT_ENABLE */
187 #define TPL_LOG_F(f, x...)
188 #define TPL_LOG_B(b, f, x...)
189 #define TPL_DEBUG(f, x...)
190 #define TPL_ERR(f, x...)
191 #define TPL_WARN(f, x...)
195 #define TPL_CHECK_ON_NULL_RETURN(exp) \
199 TPL_ERR("%s", "check failed: " # exp " == NULL"); \
204 #define TPL_CHECK_ON_NULL_RETURN_VAL(exp, val) \
208 TPL_ERR("%s", "check failed: " # exp " == NULL"); \
213 #define TPL_CHECK_ON_NULL_GOTO(exp, label) \
217 TPL_ERR("%s", "check failed: " # exp " == NULL"); \
222 #define TPL_CHECK_ON_TRUE_RETURN(exp) \
226 TPL_ERR("%s", "check failed: " # exp " is true"); \
231 #define TPL_CHECK_ON_TRUE_RETURN_VAL(exp, val) \
235 TPL_ERR("%s", "check failed: " # exp " is true"); \
240 #define TPL_CHECK_ON_TRUE_GOTO(exp, label) \
244 TPL_ERR("%s", "check failed: " # exp " is true"); \
249 #define TPL_CHECK_ON_FALSE_RETURN(exp) \
253 TPL_ERR("%s", "check failed: " # exp " is false"); \
258 #define TPL_CHECK_ON_FALSE_RETURN_VAL(exp, val) \
262 TPL_ERR("%s", "check failed: " # exp " is false"); \
267 #define TPL_CHECK_ON_FALSE_GOTO(exp, label) \
271 TPL_ERR("%s", "check failed: " # exp " is false"); \
276 #define TPL_CHECK_ON_FALSE_ASSERT_FAIL(exp, mesg) \
280 TPL_ERR("%s", mesg); \
285 #ifdef DEFAULT_DUMP_ENABLE
286 #define TPL_IMAGE_DUMP __tpl_util_image_dump
288 #define TPL_IMAGE_DUMP(data, width, height) \
290 if (tpl_dump_lvl != 0) \
292 __tpl_util_image_dump(data, width, height); \
296 char *env = tpl_getenv("TPL_DUMP_LEVEL"); \
300 tpl_dump_lvl = atoi(env); \
302 if (tpl_dump_lvl != 0) \
303 __tpl_util_image_dump(data, width, height); \
308 typedef struct _tpl_list_node tpl_list_node_t;
309 typedef struct _tpl_list tpl_list_t;
310 typedef struct tpl_util_map_entry tpl_util_map_entry_t;
311 typedef struct tpl_util_map tpl_util_map_t;
312 typedef struct tpl_util_key tpl_util_key_t;
314 typedef int (*tpl_util_hash_func_t)(const tpl_util_key_t key, int key_length);
315 typedef int (*tpl_util_key_length_func_t)(const tpl_util_key_t key);
316 typedef int (*tpl_util_key_compare_func_t)(const tpl_util_key_t key0,
318 const tpl_util_key_t key1,
321 enum _tpl_occurrence {
327 struct tpl_util_key {
328 void *ptr; /*pointer key or user defined key(string)*/
331 struct _tpl_list_node {
332 tpl_list_node_t *prev;
333 tpl_list_node_t *next;
339 tpl_list_node_t head;
340 tpl_list_node_t tail;
344 struct tpl_util_map {
345 tpl_util_hash_func_t hash_func;
346 tpl_util_key_length_func_t key_length_func;
347 tpl_util_key_compare_func_t key_compare_func;
351 tpl_util_map_entry_t **buckets;
354 void tpl_util_map_init(tpl_util_map_t *map, int bucket_bits,
355 tpl_util_hash_func_t hash_func,
356 tpl_util_key_length_func_t key_length_func,
357 tpl_util_key_compare_func_t key_compare_func,
360 void tpl_util_map_pointer_init(tpl_util_map_t *map, int bucket_bits,
363 void tpl_util_map_fini(tpl_util_map_t *map);
366 tpl_util_map_create(int bucket_bits, tpl_util_hash_func_t hash_func,
367 tpl_util_key_length_func_t key_length_func,
368 tpl_util_key_compare_func_t key_compare_func);
370 tpl_util_map_t *tpl_util_map_pointer_create(int bucket_bits);
372 void tpl_util_map_destroy(tpl_util_map_t *map);
374 void tpl_util_map_clear(tpl_util_map_t *map);
376 void *tpl_util_map_get(tpl_util_map_t *map, const tpl_util_key_t key);
379 tpl_util_map_set(tpl_util_map_t *map, const tpl_util_key_t key, void *data,
380 tpl_free_func_t free_func);
382 static TPL_INLINE int
383 __tpl_list_get_count(const tpl_list_t *list)
390 static TPL_INLINE tpl_bool_t
391 __tpl_list_is_empty(const tpl_list_t *list)
395 return list->count == 0;
398 static TPL_INLINE void
399 __tpl_list_init(tpl_list_t *list)
403 list->head.list = list;
404 list->tail.list = list;
406 list->head.prev = NULL;
407 list->head.next = &list->tail;
408 list->tail.prev = &list->head;
409 list->tail.next = NULL;
414 static TPL_INLINE void
415 __tpl_list_fini(tpl_list_t *list, tpl_free_func_t func)
417 tpl_list_node_t *node;
421 node = list->head.next;
423 while (node != &list->tail) {
424 tpl_list_node_t *free_node = node;
427 TPL_ASSERT(free_node);
429 if (func) func(free_node->data);
434 __tpl_list_init(list);
437 static TPL_INLINE tpl_list_t *
442 list = (tpl_list_t *) malloc(sizeof(tpl_list_t));
443 if (!list) return NULL;
445 __tpl_list_init(list);
450 static TPL_INLINE void
451 __tpl_list_free(tpl_list_t *list, tpl_free_func_t func)
455 __tpl_list_fini(list, func);
459 static TPL_INLINE void *
460 __tpl_list_node_get_data(const tpl_list_node_t *node)
467 static TPL_INLINE tpl_list_node_t *
468 __tpl_list_get_front_node(tpl_list_t *list)
472 if (__tpl_list_is_empty(list)) return NULL;
474 return list->head.next;
477 static TPL_INLINE tpl_list_node_t *
478 __tpl_list_get_back_node(tpl_list_t *list)
482 if (__tpl_list_is_empty(list)) return NULL;
484 return list->tail.prev;
487 static TPL_INLINE tpl_list_node_t *
488 __tpl_list_node_prev(tpl_list_node_t *node)
491 TPL_ASSERT(node->list);
493 if (node->prev != &node->list->head)
494 return (tpl_list_node_t *)node->prev;
499 static TPL_INLINE tpl_list_node_t *
500 __tpl_list_node_next(tpl_list_node_t *node)
503 TPL_ASSERT(node->list);
505 if (node->next != &node->list->tail)
511 static TPL_INLINE void *
512 __tpl_list_get_front(const tpl_list_t *list)
516 if (__tpl_list_is_empty(list))
519 TPL_ASSERT(list->head.next);
521 return list->head.next->data;
524 static TPL_INLINE void *
525 __tpl_list_get_back(const tpl_list_t *list)
529 if (__tpl_list_is_empty(list)) return NULL;
531 TPL_ASSERT(list->tail.prev);
533 return list->tail.prev->data;
536 static TPL_INLINE void
537 __tpl_list_remove(tpl_list_node_t *node, tpl_free_func_t func)
540 TPL_ASSERT(node->prev);
541 TPL_ASSERT(node->next);
543 node->prev->next = node->next;
544 node->next->prev = node->prev;
546 if (func) func(node->data);
552 static TPL_INLINE tpl_result_t
553 __tpl_list_insert(tpl_list_node_t *pos, void *data)
555 tpl_list_node_t *node = (tpl_list_node_t *)malloc(sizeof(tpl_list_node_t));
557 TPL_ERR("Failed to allocate new tpl_list_node_t.");
558 return TPL_ERROR_OUT_OF_MEMORY;
562 node->list = pos->list;
564 pos->next->prev = node;
565 node->next = pos->next;
572 return TPL_ERROR_NONE;
575 static TPL_INLINE tpl_list_node_t *
576 __tpl_list_find_node(tpl_list_t *list, void *data, int occurrence,
577 tpl_free_func_t func)
579 tpl_list_node_t *node = NULL;
580 tpl_list_node_t *res_node = NULL;
584 if (occurrence == TPL_FIRST) {
585 node = list->head.next;
587 while (node != &list->tail) {
588 tpl_list_node_t *curr;
596 if (curr->data == data) {
601 } else if (occurrence == TPL_LAST) {
602 node = list->tail.prev;
604 while (node != &list->head) {
605 tpl_list_node_t *curr;
613 if (curr->data == data) {
623 static TPL_INLINE void
624 __tpl_list_remove_data(tpl_list_t *list, void *data, int occurrence,
625 tpl_free_func_t func)
627 tpl_list_node_t *node;
631 if (occurrence == TPL_FIRST) {
632 node = list->head.next;
634 while (node != &list->tail) {
635 tpl_list_node_t *curr;
643 if (curr->data == data) {
644 __tpl_list_remove(curr, func);
648 } else if (occurrence == TPL_LAST) {
649 node = list->tail.prev;
651 while (node != &list->head) {
652 tpl_list_node_t *curr;
660 if (curr->data == data) {
661 __tpl_list_remove(curr, func);
665 } else if (occurrence == TPL_ALL) {
666 node = list->head.next;
668 while (node != &list->tail) {
669 tpl_list_node_t *curr;
677 if (curr->data == data)
678 __tpl_list_remove(curr, func);
683 static TPL_INLINE void
684 __tpl_list_push_front(tpl_list_t *list, void *data)
688 __tpl_list_insert(&list->head, data);
691 static TPL_INLINE tpl_result_t
692 __tpl_list_push_back(tpl_list_t *list, void *data)
696 return __tpl_list_insert(list->tail.prev, data);
699 static TPL_INLINE void *
700 __tpl_list_pop_front(tpl_list_t *list, tpl_free_func_t func)
706 if (__tpl_list_is_empty(list)) return NULL;
708 data = list->head.next->data;
709 __tpl_list_remove(list->head.next, func);
714 static TPL_INLINE void *
715 tpl_list_pop_back(tpl_list_t *list, tpl_free_func_t func)
721 if (__tpl_list_is_empty(list)) return NULL;
723 data = list->tail.prev->data;
724 __tpl_list_remove(list->tail.prev, func);
729 static TPL_INLINE void
730 __tpl_util_image_dump(void *data, int width, int height)
732 char path_name[20] = "/tmp";
734 tbm_surface_internal_dump_start(path_name, width, height, 1);
735 tbm_surface_internal_dump_buffer((tbm_surface_h)data, "png");
736 tbm_surface_internal_dump_end();
738 #endif /* TPL_UTILS_H */