1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
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>
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:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
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.
34 **************************************************************************/
42 #include <sys/types.h>
44 #include <tbm_surface.h>
46 #include "tdm_private_types.h"
47 #include "tdm_thread.h"
53 /* not-categorized **********************************************************/
54 #define TDM_NOT_DEFINED_VALUE (-1)
55 #define TDM_FRONT_VALUE(n) (((n) > 0) ? (n) : TDM_NOT_DEFINED_VALUE)
57 #define TDM_MAX(x, y) (((x) > (y)) ? (x) : (y))
58 #define TDM_MIN(x, y) (((x) < (y)) ? (x) : (y))
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)
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)
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]))
77 #define TDM_NEVER_GET_HERE() TDM_WRN("** NEVER GET HERE **")
79 #define TDM_SNPRINTF(p, len, fmt, ARG...) \
81 if (p && len && *len > 0) { \
82 int s = snprintf(p, *len, fmt, ##ARG); \
88 /* dump directory ***********************************************************/
89 #define TDM_DUMP_DIR "/tmp"
91 /* message length ***********************************************************/
92 #define TDM_SERVER_REPLY_MSG_LEN 8192
93 #define TDM_DEBUG_REPLY_MSG_LEN 2048
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?
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)
103 /* strtostr *****************************************************************/
105 strtostr(char *buf, int len, char *str, char *delim)
108 end = strpbrk(str, delim);
110 len = ((end - str + 1) < len) ? (end - str + 1) : len;
113 len = ((l + 1) < len) ? (l + 1) : len;
115 snprintf(buf, len, "%s", str);
116 return str + len - 1;
119 /* EXTERN, INTERN, DEPRECATED ***********************************************/
124 #if defined(__GNUC__) && __GNUC__ >= 4
125 #define EXTERN __attribute__ ((visibility("default")))
130 #if defined(__GNUC__) && __GNUC__ >= 4
131 #define INTERN __attribute__ ((visibility("hidden")))
136 #if defined(__GNUC__) && __GNUC__ >= 4
137 #define DEPRECATED __attribute__ ((deprecated))
142 /* type to string ***********************************************************/
143 struct tdm_type_name {
148 #define TDM_TYPE_NAME_FN(res) \
149 static inline const char * tdm_##res##_str(int type) \
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; \
156 return "(invalid)"; \
159 static struct tdm_type_name tdm_error_names[] = {
160 { TDM_ERROR_NONE, "Success" },
161 { TDM_ERROR_BAD_REQUEST, "bad request" },
162 { TDM_ERROR_OPERATION_FAILED, "operaion failed" },
163 { TDM_ERROR_INVALID_PARAMETER, "wrong input parameter" },
164 { TDM_ERROR_PERMISSION_DENIED, "access denied" },
165 { TDM_ERROR_BUSY, "hardware resource busy" },
166 { TDM_ERROR_OUT_OF_MEMORY, "no free memory" },
167 { TDM_ERROR_BAD_MODULE, "bad backend module" },
168 { TDM_ERROR_NOT_IMPLEMENTED, "not implemented" },
169 { TDM_ERROR_NO_CAPABILITY, "no capability" },
170 { TDM_ERROR_DPMS_OFF, "dpms off" },
171 { TDM_ERROR_OUTPUT_DISCONNECTED, "output disconnected" },
172 { TDM_ERROR_PROTOCOL_ERROR, "protocol error" },
174 TDM_TYPE_NAME_FN(error)
176 static struct tdm_type_name tdm_dpms_names[] = {
177 { TDM_OUTPUT_DPMS_ON, "on" },
178 { TDM_OUTPUT_DPMS_STANDBY, "standby" },
179 { TDM_OUTPUT_DPMS_SUSPEND, "suspend" },
180 { TDM_OUTPUT_DPMS_OFF, "off" },
181 { TDM_OUTPUT_DPMS_AOD, "aod" },
183 TDM_TYPE_NAME_FN(dpms)
185 static struct tdm_type_name tdm_status_names[] = {
186 { TDM_OUTPUT_CONN_STATUS_DISCONNECTED, "disconnected" },
187 { TDM_OUTPUT_CONN_STATUS_CONNECTED, "connected" },
188 { TDM_OUTPUT_CONN_STATUS_MODE_SETTED, "mode_setted" },
190 TDM_TYPE_NAME_FN(status)
192 static struct tdm_type_name tdm_conn_names[] = {
193 { TDM_OUTPUT_TYPE_Unknown, "Unknown" },
194 { TDM_OUTPUT_TYPE_VGA, "VGA" },
195 { TDM_OUTPUT_TYPE_DVII, "DVII" },
196 { TDM_OUTPUT_TYPE_DVID, "DVID" },
197 { TDM_OUTPUT_TYPE_DVIA, "DVIA" },
198 { TDM_OUTPUT_TYPE_Composite, "Composite" },
199 { TDM_OUTPUT_TYPE_SVIDEO, "SVIDEO" },
200 { TDM_OUTPUT_TYPE_LVDS, "LVDS" },
201 { TDM_OUTPUT_TYPE_Component, "Component" },
202 { TDM_OUTPUT_TYPE_9PinDIN, "9PinDIN" },
203 { TDM_OUTPUT_TYPE_DisplayPort, "DisplayPort" },
204 { TDM_OUTPUT_TYPE_HDMIA, "HDMIA" },
205 { TDM_OUTPUT_TYPE_HDMIB, "HDMIB" },
206 { TDM_OUTPUT_TYPE_TV, "TV" },
207 { TDM_OUTPUT_TYPE_eDP, "eDP" },
208 { TDM_OUTPUT_TYPE_VIRTUAL, "VIRTUAL" },
209 { TDM_OUTPUT_TYPE_DSI, "DSI" },
211 TDM_TYPE_NAME_FN(conn)
213 static struct tdm_type_name tdm_transform_names[] = {
214 { TDM_TRANSFORM_NORMAL, "none" },
215 { TDM_TRANSFORM_90, "90" },
216 { TDM_TRANSFORM_180, "180" },
217 { TDM_TRANSFORM_270, "270" },
218 { TDM_TRANSFORM_FLIPPED, "flipped" },
219 { TDM_TRANSFORM_FLIPPED_90, "90,flipped" },
220 { TDM_TRANSFORM_FLIPPED_180, "180,flipped" },
221 { TDM_TRANSFORM_FLIPPED_270, "270,flipped" },
223 TDM_TYPE_NAME_FN(transform)
225 static struct tdm_type_name tdm_value_type_names[] = {
226 { TDM_VALUE_TYPE_UNKNOWN, "unknown" },
227 { TDM_VALUE_TYPE_PTR, "ptr" },
228 { TDM_VALUE_TYPE_INT32, "int32" },
229 { TDM_VALUE_TYPE_UINT32, "uint32" },
230 { TDM_VALUE_TYPE_INT64, "int64" },
231 { TDM_VALUE_TYPE_UINT64, "uint64" },
233 TDM_TYPE_NAME_FN(value_type)
235 #define TDM_BIT_NAME_FB(res) \
236 static inline const char * tdm_##res##_str(int type, char **reply, int *len) \
239 const char *sep = ""; \
241 TDM_SNPRINTF(*reply, len, "none"); \
244 for (i = 0; i < TDM_ARRAY_SIZE(tdm_##res##_names); i++) { \
245 if (type & (1 << i)) { \
246 TDM_SNPRINTF(*reply, len, "%s%s", sep, tdm_##res##_names[i]); \
253 static const char *tdm_mode_type_names[] = {
262 TDM_BIT_NAME_FB(mode_type)
264 static const char *tdm_mode_flag_names[] = {
280 TDM_BIT_NAME_FB(mode_flag)
282 static const char *tdm_layer_caps_names[] = {
297 TDM_BIT_NAME_FB(layer_caps)
299 static const char *tdm_pp_caps_names[] = {
305 TDM_BIT_NAME_FB(pp_caps)
307 static const char *tdm_capture_caps_names[] = {
313 TDM_BIT_NAME_FB(capture_caps)
315 /* check condition **********************************************************/
316 #define TDM_RETURN_IF_FAIL(cond) { \
318 TDM_ERR("'%s' failed", #cond); \
322 #define TDM_RETURN_VAL_IF_FAIL(cond, val) { \
324 TDM_ERR("'%s' failed", #cond); \
328 #define TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(cond, error_v, val) { \
330 TDM_ERR("'%s' failed", #cond); \
332 if (error) *error = ret; \
336 #define TDM_WARNING_IF_FAIL(cond) { \
338 TDM_WRN("'%s' failed", #cond); \
340 #define TDM_GOTO_IF_FAIL(cond, dst) { \
342 TDM_ERR("'%s' failed", #cond); \
346 #define TDM_EXIT_IF_FAIL(cond) { \
348 TDM_ERR("'%s' failed", #cond); \
352 #define TDM_DBG_RETURN_IF_FAIL(cond) { \
354 TDM_SNPRINTF(reply, len, "[%s %d] '%s' failed\n", __func__, __LINE__, #cond); \
358 #define TDM_DBG_GOTO_IF_FAIL(cond, dst) { \
360 TDM_SNPRINTF(reply, len, "[%s %d] '%s' failed\n", __func__, __LINE__, #cond); \
365 /* trace ********************************************************************/
368 #define TDM_TRACE_BEGIN(fmt, ...) traceBegin(TTRACE_TAG_GRAPHICS, fmt, ##__VA_ARGS__)
369 #define TDM_TRACE_END() traceEnd(TTRACE_TAG_GRAPHICS)
370 #define TDM_TRACE_ASYNC_BEGIN(key, name,...) traceAsyncBegin(TTRACE_TAG_GRAPHICS, key, name, ##__VA_ARGS__)
371 #define TDM_TRACE_ASYNC_END(key, name,...) traceAsyncEnd(TTRACE_TAG_GRAPHICS, key, name, ##__VA_ARGS__)
372 #define TDM_TRACE_COUNT(count, fmt, ...) traceCounter(TTRACE_TAG_GRAPHICS, count, fmt, ##__VA_ARGS__)
373 #define TDM_TRACE_MARK(fmt, ...) traceMark(TTRACE_TAG_GRAPHICS, fmt, ##__VA_ARGS__)
375 #define TDM_TRACE_BEGIN(fmt, ...)
376 #define TDM_TRACE_END()
377 #define TDM_TRACE_ASYNC_BEGIN(key, name,...)
378 #define TDM_TRACE_ASYNC_END(key, name,...)
379 #define TDM_TRACE_COUNT(count, fmt, ...)
380 #define TDM_TRACE_MARK(fmt, ...)
383 /* display mutex ************************************************************/
384 extern pthread_mutex_t tdm_mutex_check_lock;
385 extern int tdm_mutex_locked;
386 extern const char *tdm_mutex_lock_func;
387 extern int tdm_mutex_lock_line;
388 extern const char *tdm_mutex_unlock_func;
389 extern int tdm_mutex_unlock_line;
390 extern int tdm_debug_module;
392 #define _pthread_mutex_lock(l) \
394 if (tdm_debug_module & TDM_DEBUG_MUTEX) \
395 TDM_INFO("mutex lock"); \
396 pthread_mutex_lock(l); \
397 pthread_mutex_lock(&tdm_mutex_check_lock); \
398 tdm_mutex_locked = 1; \
399 tdm_mutex_lock_func = __FUNCTION__; \
400 tdm_mutex_lock_line = __LINE__; \
401 tdm_mutex_unlock_func = NULL; \
402 tdm_mutex_unlock_line = 0; \
403 pthread_mutex_unlock(&tdm_mutex_check_lock); \
406 #define _pthread_mutex_unlock(l) \
408 if (tdm_debug_module & TDM_DEBUG_MUTEX) \
409 TDM_INFO("mutex unlock"); \
410 pthread_mutex_lock(&tdm_mutex_check_lock); \
411 tdm_mutex_locked = 0; \
412 tdm_mutex_lock_func = NULL; \
413 tdm_mutex_lock_line = 0; \
414 tdm_mutex_unlock_func = __FUNCTION__; \
415 tdm_mutex_unlock_line = __LINE__; \
416 pthread_mutex_unlock(&tdm_mutex_check_lock); \
417 pthread_mutex_unlock(l); \
420 static inline int TDM_MUTEX_IS_LOCKED(void)
423 /* if thread is not running, we don't need to consider mutex things. */
424 if (!tdm_thread_is_running())
426 pthread_mutex_lock(&tdm_mutex_check_lock);
427 ret = (tdm_mutex_locked == 1);
428 pthread_mutex_unlock(&tdm_mutex_check_lock);
432 #define tdm_display_lock(dpy) _pthread_mutex_lock(&((tdm_private_display *)dpy)->lock)
433 #define tdm_display_unlock(dpy) _pthread_mutex_unlock(&((tdm_private_display *)dpy)->lock)
440 #endif /* _TDM_MACRO_H_ */