macro: re-ordered
[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_NEVER_GET_HERE() TDM_WRN("** NEVER GET HERE **")
78
79 #define TDM_SNPRINTF(p, len, fmt, ARG...)  \
80         do { \
81                 if (p && len && *len > 0) { \
82                         int s = snprintf(p, *len, fmt, ##ARG); \
83                         p += s; \
84                         *len -= s; \
85                 } \
86         } while (0)
87
88 /* dump directory ***********************************************************/
89 #define TDM_DUMP_DIR    "/tmp"
90
91 /* message length ***********************************************************/
92 #define TDM_SERVER_REPLY_MSG_LEN        8192
93 #define TDM_DEBUG_REPLY_MSG_LEN         2048
94
95 /* DPMS *********************************************************************/
96 /* can't export VSYNC macro because we can't define the exact meaning of vsync off
97  * at this time. Does committing in standy mode work? Doesn't committing in suspend mode work?
98  */
99 #define TDM_OUTPUT_DPMS_DEFAULT_MASK         0xF
100 #define TDM_OUTPUT_DPMS_VSYNC_OFF_MASK       0x2
101 #define TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms)   ((dpms) & TDM_OUTPUT_DPMS_VSYNC_OFF_MASK)
102
103 /* strtostr *****************************************************************/
104 static inline char*
105 strtostr(char *buf, int len, char *str, char *delim)
106 {
107         char *end;
108         end = strpbrk(str, delim);
109         if (end)
110                 len = ((end - str + 1) < len) ? (end - str + 1) : len;
111         else {
112                 int l = strlen(str);
113                 len = ((l + 1) < len) ? (l + 1) : len;
114         }
115         snprintf(buf, len, "%s", str);
116         return str + len - 1;
117 }
118
119 /* EXTERN, INTERN, DEPRECATED ***********************************************/
120 #undef EXTERN
121 #undef DEPRECATED
122 #undef INTERN
123
124 #if defined(__GNUC__) && __GNUC__ >= 4
125 #define EXTERN __attribute__ ((visibility("default")))
126 #else
127 #define EXTERN
128 #endif
129
130 #if defined(__GNUC__) && __GNUC__ >= 4
131 #define INTERN __attribute__ ((visibility("hidden")))
132 #else
133 #define INTERN
134 #endif
135
136 #if defined(__GNUC__) && __GNUC__ >= 4
137 #define DEPRECATED __attribute__ ((deprecated))
138 #else
139 #define DEPRECATED
140 #endif
141
142 /* type to string ***********************************************************/
143 struct tdm_type_name {
144         int type;
145         const char *name;
146 };
147
148 #define TDM_TYPE_NAME_FN(res) \
149 static inline const char * tdm_##res##_str(int type)    \
150 {                       \
151         unsigned int i;                                 \
152         for (i = 0; i < TDM_ARRAY_SIZE(tdm_##res##_names); i++) { \
153                 if (tdm_##res##_names[i].type == type)  \
154                         return tdm_##res##_names[i].name;       \
155         }                                               \
156         return "(invalid)";                             \
157 }
158
159 static struct tdm_type_name tdm_dpms_names[] = {
160         { TDM_OUTPUT_DPMS_ON, "on" },
161         { TDM_OUTPUT_DPMS_STANDBY, "standby" },
162         { TDM_OUTPUT_DPMS_SUSPEND, "suspend" },
163         { TDM_OUTPUT_DPMS_OFF, "off" },
164         { TDM_OUTPUT_DPMS_AOD, "aod" },
165 };
166 TDM_TYPE_NAME_FN(dpms)
167
168 static struct tdm_type_name tdm_status_names[] = {
169         { TDM_OUTPUT_CONN_STATUS_DISCONNECTED, "disconnected" },
170         { TDM_OUTPUT_CONN_STATUS_CONNECTED, "connected" },
171         { TDM_OUTPUT_CONN_STATUS_MODE_SETTED, "mode_setted" },
172 };
173 TDM_TYPE_NAME_FN(status)
174
175 static struct tdm_type_name tdm_conn_names[] = {
176         { TDM_OUTPUT_TYPE_Unknown, "Unknown" },
177         { TDM_OUTPUT_TYPE_VGA, "VGA" },
178         { TDM_OUTPUT_TYPE_DVII, "DVII" },
179         { TDM_OUTPUT_TYPE_DVID, "DVID" },
180         { TDM_OUTPUT_TYPE_DVIA, "DVIA" },
181         { TDM_OUTPUT_TYPE_Composite, "Composite" },
182         { TDM_OUTPUT_TYPE_SVIDEO, "SVIDEO" },
183         { TDM_OUTPUT_TYPE_LVDS, "LVDS" },
184         { TDM_OUTPUT_TYPE_Component, "Component" },
185         { TDM_OUTPUT_TYPE_9PinDIN, "9PinDIN" },
186         { TDM_OUTPUT_TYPE_DisplayPort, "DisplayPort" },
187         { TDM_OUTPUT_TYPE_HDMIA, "HDMIA" },
188         { TDM_OUTPUT_TYPE_HDMIB, "HDMIB" },
189         { TDM_OUTPUT_TYPE_TV, "TV" },
190         { TDM_OUTPUT_TYPE_eDP, "eDP" },
191         { TDM_OUTPUT_TYPE_VIRTUAL, "VIRTUAL" },
192         { TDM_OUTPUT_TYPE_DSI, "DSI" },
193 };
194 TDM_TYPE_NAME_FN(conn)
195
196 static struct tdm_type_name tdm_transform_names[] = {
197         { TDM_TRANSFORM_NORMAL, "none" },
198         { TDM_TRANSFORM_90, "90" },
199         { TDM_TRANSFORM_180, "180" },
200         { TDM_TRANSFORM_270, "270" },
201         { TDM_TRANSFORM_FLIPPED, "flipped" },
202         { TDM_TRANSFORM_FLIPPED_90, "90,flipped" },
203         { TDM_TRANSFORM_FLIPPED_180, "180,flipped" },
204         { TDM_TRANSFORM_FLIPPED_270, "270,flipped" },
205 };
206 TDM_TYPE_NAME_FN(transform)
207
208 static struct tdm_type_name tdm_value_type_names[] = {
209         { TDM_VALUE_TYPE_UNKNOWN, "unknown" },
210         { TDM_VALUE_TYPE_PTR, "ptr" },
211         { TDM_VALUE_TYPE_INT32, "int32" },
212         { TDM_VALUE_TYPE_UINT32, "uint32" },
213         { TDM_VALUE_TYPE_INT64, "int64" },
214         { TDM_VALUE_TYPE_UINT64, "uint64" },
215 };
216 TDM_TYPE_NAME_FN(value_type)
217
218 #define TDM_BIT_NAME_FB(res)                                    \
219 static inline const char * tdm_##res##_str(int type, char **reply, int *len)    \
220 {                       \
221         unsigned int i;                                         \
222         const char *sep = "";                                   \
223         if (type == 0) {        \
224                 TDM_SNPRINTF(*reply, len, "none");      \
225                 return NULL;    \
226         }       \
227         for (i = 0; i < TDM_ARRAY_SIZE(tdm_##res##_names); i++) {               \
228                 if (type & (1 << i)) {                          \
229                         TDM_SNPRINTF(*reply, len, "%s%s", sep, tdm_##res##_names[i]);   \
230                         sep = ",";                              \
231                 }                                               \
232         }                                                       \
233         return NULL;                                            \
234 }
235
236 static const char *tdm_mode_type_names[] = {
237         "builtin",
238         "clock_c",
239         "crtc_c",
240         "preferred",
241         "default",
242         "userdef",
243         "driver",
244 };
245 TDM_BIT_NAME_FB(mode_type)
246
247 static const char *tdm_mode_flag_names[] = {
248         "phsync",
249         "nhsync",
250         "pvsync",
251         "nvsync",
252         "interlace",
253         "dblscan",
254         "csync",
255         "pcsync",
256         "ncsync",
257         "hskew",
258         "bcast",
259         "pixmux",
260         "dblclk",
261         "clkdiv2"
262 };
263 TDM_BIT_NAME_FB(mode_flag)
264
265 static const char *tdm_layer_caps_names[] = {
266         "cursor",
267         "primary",
268         "overlay",
269         "",
270         "graphic",
271         "video",
272         "",
273         "",
274         "scale",
275         "transform",
276         "scanout",
277         "reserved",
278         "no_crop",
279 };
280 TDM_BIT_NAME_FB(layer_caps)
281
282 static const char *tdm_pp_caps_names[] = {
283         "sync",
284         "async",
285         "scale",
286         "transform",
287 };
288 TDM_BIT_NAME_FB(pp_caps)
289
290 static const char *tdm_capture_caps_names[] = {
291         "output",
292         "layer",
293         "scale",
294         "transform",
295 };
296 TDM_BIT_NAME_FB(capture_caps)
297
298 /* check condition **********************************************************/
299 #define TDM_RETURN_IF_FAIL(cond) { \
300         if (!(cond))  { \
301                 TDM_ERR("'%s' failed", #cond); \
302                 return; \
303         } \
304 }
305 #define TDM_RETURN_VAL_IF_FAIL(cond, val) { \
306         if (!(cond)) { \
307                 TDM_ERR("'%s' failed", #cond); \
308                 return val; \
309         } \
310 }
311 #define TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(cond, error_v, val) { \
312         if (!(cond)) { \
313                 TDM_ERR("'%s' failed", #cond); \
314                 ret = error_v; \
315                 if (error) *error = ret; \
316                 return val; \
317         } \
318 }
319 #define TDM_WARNING_IF_FAIL(cond)  { \
320         if (!(cond)) \
321                 TDM_WRN("'%s' failed", #cond); \
322 }
323 #define TDM_GOTO_IF_FAIL(cond, dst) { \
324         if (!(cond)) { \
325                 TDM_ERR("'%s' failed", #cond); \
326                 goto dst; \
327         } \
328 }
329 #define TDM_EXIT_IF_FAIL(cond) { \
330         if (!(cond)) { \
331                 TDM_ERR("'%s' failed", #cond); \
332                 exit(0); \
333         } \
334 }
335 #define TDM_DBG_RETURN_IF_FAIL(cond) { \
336         if (!(cond))  { \
337                 TDM_SNPRINTF(reply, len, "[%s %d] '%s' failed\n", __func__, __LINE__, #cond); \
338                 return; \
339         } \
340 }
341 #define TDM_DBG_GOTO_IF_FAIL(cond, dst) { \
342         if (!(cond))  { \
343                 TDM_SNPRINTF(reply, len, "[%s %d] '%s' failed\n", __func__, __LINE__, #cond); \
344                 goto dst; \
345         } \
346 }
347
348 /* trace ********************************************************************/
349 #ifdef HAVE_TTRACE
350 #include <ttrace.h>
351 #define TDM_TRACE_BEGIN(fmt, ...) traceBegin(TTRACE_TAG_GRAPHICS, fmt, ##__VA_ARGS__)
352 #define TDM_TRACE_END() traceEnd(TTRACE_TAG_GRAPHICS)
353 #define TDM_TRACE_ASYNC_BEGIN(key, name,...) traceAsyncBegin(TTRACE_TAG_GRAPHICS, key, name, ##__VA_ARGS__)
354 #define TDM_TRACE_ASYNC_END(key, name,...) traceAsyncEnd(TTRACE_TAG_GRAPHICS, key, name, ##__VA_ARGS__)
355 #define TDM_TRACE_COUNT(count, fmt, ...) traceCounter(TTRACE_TAG_GRAPHICS, count, fmt, ##__VA_ARGS__)
356 #define TDM_TRACE_MARK(fmt, ...) traceMark(TTRACE_TAG_GRAPHICS, fmt, ##__VA_ARGS__)
357 #else
358 #define TDM_TRACE_BEGIN(fmt, ...)
359 #define TDM_TRACE_END()
360 #define TDM_TRACE_ASYNC_BEGIN(key, name,...)
361 #define TDM_TRACE_ASYNC_END(key, name,...)
362 #define TDM_TRACE_COUNT(count, fmt, ...)
363 #define TDM_TRACE_MARK(fmt, ...)
364 #endif
365
366 /* display mutex ************************************************************/
367 extern pthread_mutex_t tdm_mutex_check_lock;
368 extern int tdm_mutex_locked;
369 extern const char *tdm_mutex_lock_func;
370 extern int tdm_mutex_lock_line;
371 extern const char *tdm_mutex_unlock_func;
372 extern int tdm_mutex_unlock_line;
373
374 #define _pthread_mutex_lock(l) \
375         do { \
376                 if (tdm_debug_module & TDM_DEBUG_MUTEX) \
377                         TDM_INFO("mutex lock"); \
378                 pthread_mutex_lock(l); \
379                 pthread_mutex_lock(&tdm_mutex_check_lock); \
380                 tdm_mutex_locked = 1; \
381                 tdm_mutex_lock_func = __FUNCTION__; \
382                 tdm_mutex_lock_line = __LINE__; \
383                 tdm_mutex_unlock_func = NULL; \
384                 tdm_mutex_unlock_line = 0; \
385                 pthread_mutex_unlock(&tdm_mutex_check_lock); \
386         } while (0)
387
388 #define _pthread_mutex_unlock(l) \
389         do { \
390                 if (tdm_debug_module & TDM_DEBUG_MUTEX) \
391                         TDM_INFO("mutex unlock"); \
392                 pthread_mutex_lock(&tdm_mutex_check_lock); \
393                 tdm_mutex_locked = 0; \
394                 tdm_mutex_lock_func = NULL; \
395                 tdm_mutex_lock_line = 0; \
396                 tdm_mutex_unlock_func = __FUNCTION__; \
397                 tdm_mutex_unlock_line = __LINE__; \
398                 pthread_mutex_unlock(&tdm_mutex_check_lock); \
399                 pthread_mutex_unlock(l); \
400         } while (0)
401
402 static inline int TDM_MUTEX_IS_LOCKED(void)
403 {
404         int ret;
405         /* if thread is not running, we don't need to consider mutex things. */
406         if (!tdm_thread_is_running())
407                 return 1;
408         pthread_mutex_lock(&tdm_mutex_check_lock);
409         ret = (tdm_mutex_locked == 1);
410         pthread_mutex_unlock(&tdm_mutex_check_lock);
411         return ret;
412 }
413
414 #define tdm_display_lock(dpy)   _pthread_mutex_lock(&((tdm_private_display *)dpy)->lock)
415 #define tdm_display_unlock(dpy)   _pthread_mutex_unlock(&((tdm_private_display *)dpy)->lock)
416
417
418 #ifdef __cplusplus
419 }
420 #endif
421
422 #endif /* _TDM_MACRO_H_ */