14c0f7ad013da2132a3920bdc0a808646f5ec41e
[platform/core/uifw/libtdm.git] / src / tdm_macro.h
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
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>
13  *
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:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
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.
33  *
34 **************************************************************************/
35
36 #ifndef _TDM_MACRO_H_
37 #define _TDM_MACRO_H_
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <sys/types.h>
43
44 #include <tbm_surface.h>
45
46 #include "tdm_private_types.h"
47 #include "tdm_thread.h"
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 /* not-categorized **********************************************************/
54 #define TDM_NOT_DEFINED_VALUE       (-1)
55 #define TDM_FRONT_VALUE(n)          (((n) > 0) ? (n) : TDM_NOT_DEFINED_VALUE)
56
57 #define TDM_MAX(x, y) (((x) > (y)) ? (x) : (y))
58 #define TDM_MIN(x, y) (((x) < (y)) ? (x) : (y))
59
60 #define TDM_TIME(sec, usec)   ((double)(sec) + ((double)(usec)) / 1000000.0)
61 #define TDM_TIME_SEC(time)    ((unsigned int)(time))
62 #define TDM_TIME_USEC(time)   (unsigned int)(((time) - (unsigned int)(time)) * 1000000.0)
63
64 #define C(b, m)             (((b) >> (m)) & 0xFF)
65 #define B(c, s)             ((((unsigned int)(c)) & 0xff) << (s))
66 #define FOURCC(a, b, c, d)  (B(d, 24) | B(c, 16) | B(b, 8) | B(a, 0))
67 #define FOURCC_STR(id)      C(id, 0), C(id, 8), C(id, 16), C(id, 24)
68 #define FOURCC_ID(str)      FOURCC(((char*)str)[0], ((char*)str)[1], ((char*)str)[2], ((char*)str)[3])
69 #define IS_RGB(f)           ((f) == TBM_FORMAT_XRGB8888 || (f) == TBM_FORMAT_ARGB8888)
70
71 /* don't using !,$,# */
72 #define TDM_DELIM           "@^&*+-|,:~"
73 #define TDM_ALIGN(a, b)     (((a) + ((b) - 1)) & ~((b) - 1))
74 #define TDM_SWAP(a, b)      ({ int t; t = a; a = b; b = t; })
75 #define TDM_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
76
77 #define TDM_SNPRINTF(p, len, fmt, ARG...)  \
78         do { \
79                 if (p && len && *len > 0) { \
80                         int s = snprintf(p, *len, fmt, ##ARG); \
81                         p += s; \
82                         *len -= s; \
83                 } \
84         } while (0)
85
86 /* common backend names *****************************************************/
87 #define TDM_DEFAULT_MODULE        "libtdm-default.so"
88 #define TDM_DUMMY_MODULE          "libtdm-dummy.so"
89
90 /* dump directory ***********************************************************/
91 #define TDM_DUMP_DIR    "/tmp"
92
93 /* message length ***********************************************************/
94 #define TDM_SERVER_REPLY_MSG_LEN        8192
95 #define TDM_DEBUG_REPLY_MSG_LEN         2048
96
97 /* DPMS *********************************************************************/
98 /* can't export VSYNC macro because we can't define the exact meaning of vsync off
99  * at this time. Does committing in standy mode work? Doesn't committing in suspend mode work?
100  */
101 #define TDM_OUTPUT_DPMS_DEFAULT_MASK         0xF
102 #define TDM_OUTPUT_DPMS_VSYNC_OFF_MASK       0x2
103 #define TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms)   ((dpms) & TDM_OUTPUT_DPMS_VSYNC_OFF_MASK)
104
105 /* strtostr *****************************************************************/
106 static inline char*
107 strtostr(char *buf, int len, char *str, char *delim)
108 {
109         char *end;
110         end = strpbrk(str, delim);
111         if (end)
112                 len = ((end - str + 1) < len) ? (end - str + 1) : len;
113         else {
114                 int l = strlen(str);
115                 len = ((l + 1) < len) ? (l + 1) : len;
116         }
117         snprintf(buf, len, "%s", str);
118         return str + len - 1;
119 }
120
121 /* EXTERN, INTERN, DEPRECATED ***********************************************/
122 #undef EXTERN
123 #undef DEPRECATED
124 #undef INTERN
125
126 #if defined(__GNUC__) && __GNUC__ >= 4
127 #define EXTERN __attribute__ ((visibility("default")))
128 #else
129 #define EXTERN
130 #endif
131
132 #if defined(__GNUC__) && __GNUC__ >= 4
133 #define INTERN __attribute__ ((visibility("hidden")))
134 #else
135 #define INTERN
136 #endif
137
138 #if defined(__GNUC__) && __GNUC__ >= 4
139 #define DEPRECATED __attribute__ ((deprecated))
140 #else
141 #define DEPRECATED
142 #endif
143
144 /* type to string ***********************************************************/
145 struct tdm_type_name {
146         int type;
147         const char *name;
148 };
149
150 #define TDM_TYPE_NAME_FN(res) \
151 static inline const char * tdm_##res##_str(int type)    \
152 {                       \
153         unsigned int i;                                 \
154         for (i = 0; i < TDM_ARRAY_SIZE(tdm_##res##_names); i++) { \
155                 if (tdm_##res##_names[i].type == type)  \
156                         return tdm_##res##_names[i].name;       \
157         }                                               \
158         return "(invalid)";                             \
159 }
160
161 static struct tdm_type_name tdm_error_names[] = {
162         { TDM_ERROR_NONE, "Success" },
163         { TDM_ERROR_BAD_REQUEST, "bad request" },
164         { TDM_ERROR_OPERATION_FAILED, "operaion failed" },
165         { TDM_ERROR_INVALID_PARAMETER, "wrong input parameter" },
166         { TDM_ERROR_PERMISSION_DENIED, "access denied" },
167         { TDM_ERROR_BUSY, "hardware resource busy" },
168         { TDM_ERROR_OUT_OF_MEMORY, "no free memory" },
169         { TDM_ERROR_BAD_MODULE, "bad backend module" },
170         { TDM_ERROR_NOT_IMPLEMENTED, "not implemented" },
171         { TDM_ERROR_NO_CAPABILITY, "no capability" },
172         { TDM_ERROR_DPMS_OFF, "dpms off" },
173         { TDM_ERROR_OUTPUT_DISCONNECTED, "output disconnected" },
174         { TDM_ERROR_PROTOCOL_ERROR, "protocol error" },
175 };
176 TDM_TYPE_NAME_FN(error)
177
178 static struct tdm_type_name tdm_dpms_names[] = {
179         { TDM_OUTPUT_DPMS_ON, "on" },
180         { TDM_OUTPUT_DPMS_STANDBY, "standby" },
181         { TDM_OUTPUT_DPMS_SUSPEND, "suspend" },
182         { TDM_OUTPUT_DPMS_OFF, "off" },
183         { TDM_OUTPUT_DPMS_AOD, "aod" },
184 };
185 TDM_TYPE_NAME_FN(dpms)
186
187 static struct tdm_type_name tdm_status_names[] = {
188         { TDM_OUTPUT_CONN_STATUS_DISCONNECTED, "disconnected" },
189         { TDM_OUTPUT_CONN_STATUS_CONNECTED, "connected" },
190         { TDM_OUTPUT_CONN_STATUS_MODE_SETTED, "mode_setted" },
191 };
192 TDM_TYPE_NAME_FN(status)
193
194 static struct tdm_type_name tdm_conn_names[] = {
195         { TDM_OUTPUT_TYPE_Unknown, "Unknown" },
196         { TDM_OUTPUT_TYPE_VGA, "VGA" },
197         { TDM_OUTPUT_TYPE_DVII, "DVII" },
198         { TDM_OUTPUT_TYPE_DVID, "DVID" },
199         { TDM_OUTPUT_TYPE_DVIA, "DVIA" },
200         { TDM_OUTPUT_TYPE_Composite, "Composite" },
201         { TDM_OUTPUT_TYPE_SVIDEO, "SVIDEO" },
202         { TDM_OUTPUT_TYPE_LVDS, "LVDS" },
203         { TDM_OUTPUT_TYPE_Component, "Component" },
204         { TDM_OUTPUT_TYPE_9PinDIN, "9PinDIN" },
205         { TDM_OUTPUT_TYPE_DisplayPort, "DisplayPort" },
206         { TDM_OUTPUT_TYPE_HDMIA, "HDMIA" },
207         { TDM_OUTPUT_TYPE_HDMIB, "HDMIB" },
208         { TDM_OUTPUT_TYPE_TV, "TV" },
209         { TDM_OUTPUT_TYPE_eDP, "eDP" },
210         { TDM_OUTPUT_TYPE_VIRTUAL, "VIRTUAL" },
211         { TDM_OUTPUT_TYPE_DSI, "DSI" },
212 };
213 TDM_TYPE_NAME_FN(conn)
214
215 static struct tdm_type_name tdm_capture_type_names[] = {
216         { TDM_CAPTURE_TYPE_ONESHOT, "none" },
217         { TDM_CAPTURE_TYPE_STREAM, "90" },
218 };
219 TDM_TYPE_NAME_FN(capture_type)
220
221 static struct tdm_type_name tdm_transform_names[] = {
222         { TDM_TRANSFORM_NORMAL, "none" },
223         { TDM_TRANSFORM_90, "90" },
224         { TDM_TRANSFORM_180, "180" },
225         { TDM_TRANSFORM_270, "270" },
226         { TDM_TRANSFORM_FLIPPED, "flipped" },
227         { TDM_TRANSFORM_FLIPPED_90, "90,flipped" },
228         { TDM_TRANSFORM_FLIPPED_180, "180,flipped" },
229         { TDM_TRANSFORM_FLIPPED_270, "270,flipped" },
230 };
231 TDM_TYPE_NAME_FN(transform)
232
233 static struct tdm_type_name tdm_value_type_names[] = {
234         { TDM_VALUE_TYPE_UNKNOWN, "unknown" },
235         { TDM_VALUE_TYPE_PTR, "ptr" },
236         { TDM_VALUE_TYPE_INT32, "int32" },
237         { TDM_VALUE_TYPE_UINT32, "uint32" },
238         { TDM_VALUE_TYPE_INT64, "int64" },
239         { TDM_VALUE_TYPE_UINT64, "uint64" },
240 };
241 TDM_TYPE_NAME_FN(value_type)
242
243 static struct tdm_type_name tdm_cb_type_names[] = {
244         { TDM_THREAD_CB_NONE, "none" },
245         { TDM_THREAD_CB_EXIT, "exit" },
246         { TDM_THREAD_CB_OUTPUT_COMMIT, "output-commit" },
247         { TDM_THREAD_CB_OUTPUT_VBLANK, "output-vblank" },
248         { TDM_THREAD_CB_OUTPUT_STATUS, "output-status" },
249         { TDM_THREAD_CB_OUTPUT_DPMS, "output-dpms" },
250         { TDM_THREAD_CB_PP_DONE, "pp-done" },
251         { TDM_THREAD_CB_CAPTURE_DONE, "capture-done" },
252         { TDM_THREAD_CB_VBLANK_SW, "vblank-sw" },
253         { TDM_THREAD_CB_VBLANK_CREATE, "vblank-create" },
254         { TDM_THREAD_CB_NEED_VALIDATE, "need-validate" },
255 };
256 TDM_TYPE_NAME_FN(cb_type)
257
258 #define TDM_BIT_NAME_FB(res)                                    \
259 static inline const char * tdm_##res##_str(int type, char **reply, int *len)    \
260 {                       \
261         unsigned int i;                                         \
262         const char *sep = "";                                   \
263         if (type == 0) {        \
264                 TDM_SNPRINTF(*reply, len, "none");      \
265                 return NULL;    \
266         }       \
267         for (i = 0; i < TDM_ARRAY_SIZE(tdm_##res##_names); i++) {               \
268                 if (type & (1 << i)) {                          \
269                         TDM_SNPRINTF(*reply, len, "%s%s", sep, tdm_##res##_names[i]);   \
270                         sep = ",";                              \
271                 }                                               \
272         }                                                       \
273         return NULL;                                            \
274 }
275
276 static const char *tdm_mode_type_names[] = {
277         "builtin",
278         "clock_c",
279         "crtc_c",
280         "preferred",
281         "default",
282         "userdef",
283         "driver",
284 };
285 TDM_BIT_NAME_FB(mode_type)
286
287 static const char *tdm_mode_flag_names[] = {
288         "phsync",
289         "nhsync",
290         "pvsync",
291         "nvsync",
292         "interlace",
293         "dblscan",
294         "csync",
295         "pcsync",
296         "ncsync",
297         "hskew",
298         "bcast",
299         "pixmux",
300         "dblclk",
301         "clkdiv2"
302 };
303 TDM_BIT_NAME_FB(mode_flag)
304
305 static const char *tdm_layer_caps_names[] = {
306         "cursor",
307         "primary",
308         "overlay",
309         "",
310         "graphic",
311         "video",
312         "",
313         "",
314         "scale",
315         "transform",
316         "scanout",
317         "reserved",
318         "no_crop",
319 };
320 TDM_BIT_NAME_FB(layer_caps)
321
322 static const char *tdm_pp_caps_names[] = {
323         "sync",
324         "async",
325         "scale",
326         "transform",
327 };
328 TDM_BIT_NAME_FB(pp_caps)
329
330 static const char *tdm_capture_caps_names[] = {
331         "output",
332         "layer",
333         "scale",
334         "transform",
335 };
336 TDM_BIT_NAME_FB(capture_caps)
337
338 /* check condition **********************************************************/
339 #define TDM_RETURN_IF_FAIL(cond) { \
340         if (!(cond))  { \
341                 TDM_ERR("'%s' failed", #cond); \
342                 return; \
343         } \
344 }
345 #define TDM_RETURN_VAL_IF_FAIL(cond, val) { \
346         if (!(cond)) { \
347                 TDM_ERR("'%s' failed", #cond); \
348                 return val; \
349         } \
350 }
351 #define TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(cond, error_v, val) { \
352         if (!(cond)) { \
353                 TDM_ERR("'%s' failed", #cond); \
354                 ret = error_v; \
355                 if (error) *error = ret; \
356                 return val; \
357         } \
358 }
359 #define TDM_WARNING_IF_FAIL(cond)  { \
360         if (!(cond)) \
361                 TDM_WRN("'%s' failed", #cond); \
362 }
363 #define TDM_GOTO_IF_FAIL(cond, dst) { \
364         if (!(cond)) { \
365                 TDM_ERR("'%s' failed", #cond); \
366                 goto dst; \
367         } \
368 }
369 #define TDM_EXIT_IF_FAIL(cond) { \
370         if (!(cond)) { \
371                 TDM_ERR("'%s' failed", #cond); \
372                 exit(0); \
373         } \
374 }
375 #define TDM_DBG_RETURN_IF_FAIL(cond) { \
376         if (!(cond))  { \
377                 TDM_SNPRINTF(reply, len, "[%s %d] '%s' failed\n", __func__, __LINE__, #cond); \
378                 return; \
379         } \
380 }
381 #define TDM_DBG_RETURN_VAL_IF_FAIL(cond, val) { \
382         if (!(cond))  { \
383                 TDM_SNPRINTF(reply, len, "[%s %d] '%s' failed\n", __func__, __LINE__, #cond); \
384                 return val; \
385         } \
386 }
387 #define TDM_DBG_GOTO_IF_FAIL(cond, dst) { \
388         if (!(cond))  { \
389                 TDM_SNPRINTF(reply, len, "[%s %d] '%s' failed\n", __func__, __LINE__, #cond); \
390                 goto dst; \
391         } \
392 }
393
394 /* trace ********************************************************************/
395 #ifdef HAVE_TTRACE
396 #include <ttrace.h>
397 #define TDM_TRACE_BEGIN(fmt, ...) traceBegin(TTRACE_TAG_GRAPHICS, fmt, ##__VA_ARGS__)
398 #define TDM_TRACE_END() traceEnd(TTRACE_TAG_GRAPHICS)
399 #define TDM_TRACE_ASYNC_BEGIN(key, name,...) traceAsyncBegin(TTRACE_TAG_GRAPHICS, key, name, ##__VA_ARGS__)
400 #define TDM_TRACE_ASYNC_END(key, name,...) traceAsyncEnd(TTRACE_TAG_GRAPHICS, key, name, ##__VA_ARGS__)
401 #define TDM_TRACE_COUNT(count, fmt, ...) traceCounter(TTRACE_TAG_GRAPHICS, count, fmt, ##__VA_ARGS__)
402 #define TDM_TRACE_MARK(fmt, ...) traceMark(TTRACE_TAG_GRAPHICS, fmt, ##__VA_ARGS__)
403 #else
404 #define TDM_TRACE_BEGIN(fmt, ...)
405 #define TDM_TRACE_END()
406 #define TDM_TRACE_ASYNC_BEGIN(key, name,...)
407 #define TDM_TRACE_ASYNC_END(key, name,...)
408 #define TDM_TRACE_COUNT(count, fmt, ...)
409 #define TDM_TRACE_MARK(fmt, ...)
410 #endif
411
412 /* display mutex ************************************************************/
413 extern pthread_mutex_t tdm_mutex_check_lock;
414 extern int tdm_mutex_locked;
415 extern const char *tdm_mutex_lock_func;
416 extern int tdm_mutex_lock_line;
417 extern const char *tdm_mutex_unlock_func;
418 extern int tdm_mutex_unlock_line;
419 extern int tdm_debug_module;
420
421 #define _pthread_mutex_lock(l) \
422         do { \
423                 if (tdm_debug_module & TDM_DEBUG_MUTEX) \
424                         TDM_INFO("mutex lock"); \
425                 pthread_mutex_lock(l); \
426                 pthread_mutex_lock(&tdm_mutex_check_lock); \
427                 tdm_mutex_locked = 1; \
428                 tdm_mutex_lock_func = __FUNCTION__; \
429                 tdm_mutex_lock_line = __LINE__; \
430                 tdm_mutex_unlock_func = NULL; \
431                 tdm_mutex_unlock_line = 0; \
432                 pthread_mutex_unlock(&tdm_mutex_check_lock); \
433         } while (0)
434
435 #define _pthread_mutex_unlock(l) \
436         do { \
437                 if (tdm_debug_module & TDM_DEBUG_MUTEX) \
438                         TDM_INFO("mutex unlock"); \
439                 pthread_mutex_lock(&tdm_mutex_check_lock); \
440                 tdm_mutex_locked = 0; \
441                 tdm_mutex_lock_func = NULL; \
442                 tdm_mutex_lock_line = 0; \
443                 tdm_mutex_unlock_func = __FUNCTION__; \
444                 tdm_mutex_unlock_line = __LINE__; \
445                 pthread_mutex_unlock(&tdm_mutex_check_lock); \
446                 pthread_mutex_unlock(l); \
447         } while (0)
448
449 static inline int TDM_MUTEX_IS_LOCKED(void)
450 {
451         int ret;
452         /* if thread is not running, we don't need to consider mutex things. */
453         if (!tdm_thread_is_running())
454                 return 1;
455         pthread_mutex_lock(&tdm_mutex_check_lock);
456         ret = (tdm_mutex_locked == 1);
457         pthread_mutex_unlock(&tdm_mutex_check_lock);
458         return ret;
459 }
460
461 #define tdm_display_lock(dpy)   _pthread_mutex_lock(&((tdm_private_display *)dpy)->lock)
462 #define tdm_display_unlock(dpy)   _pthread_mutex_unlock(&((tdm_private_display *)dpy)->lock)
463
464
465 /* debugging mutex ************************************************************/
466 extern pthread_mutex_t tdm_debug_mutex_check_lock;
467 extern const char *tdm_debug_mutex_lock_func;
468 extern int tdm_debug_mutex_lock_line;
469
470 #define _debug_pthread_mutex_lock(l) \
471         do { \
472                 pthread_mutex_lock(l); \
473                 pthread_mutex_lock(&tdm_debug_mutex_check_lock); \
474                 tdm_debug_mutex_lock_func = __FUNCTION__; \
475                 tdm_debug_mutex_lock_line = __LINE__; \
476                 pthread_mutex_unlock(&tdm_debug_mutex_check_lock); \
477         } while (0)
478
479 #define _debug_pthread_mutex_unlock(l) \
480         do { \
481                 pthread_mutex_lock(&tdm_debug_mutex_check_lock); \
482                 tdm_debug_mutex_lock_func = NULL; \
483                 tdm_debug_mutex_lock_line = 0; \
484                 pthread_mutex_unlock(&tdm_debug_mutex_check_lock); \
485                 pthread_mutex_unlock(l); \
486         } while (0)
487
488 #ifdef __cplusplus
489 }
490 #endif
491
492 #endif /* _TDM_MACRO_H_ */