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