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