1 /**************************************************************************
2 *
3 * Copyright (C) 2019 Chromium.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 **************************************************************************/
24
25 #ifndef VIRGL_UTIL_H
26 #define VIRGL_UTIL_H
27
28 #include <stdarg.h>
29 #include <stdbool.h>
30 #include <stdint.h>
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "util/macros.h"
37
38 #include "virglrenderer.h"
39
40 #define TRACE_WITH_PERFETTO 1
41 #define TRACE_WITH_STDERR 2
42 #define TRACE_WITH_PERCETTO 3
43
44 #define BIT(n) (UINT32_C(1) << (n))
45
has_bit(uint32_t mask,uint32_t bit)46 static inline bool has_bit(uint32_t mask, uint32_t bit)
47 {
48 return !!(mask & bit);
49 }
50
has_bits(uint32_t mask,uint32_t bits)51 static inline bool has_bits(uint32_t mask, uint32_t bits)
52 {
53 return !!((mask & bits) == bits);
54 }
55
is_only_bit(uint32_t mask,uint32_t bit)56 static inline bool is_only_bit(uint32_t mask, uint32_t bit)
57 {
58 return (mask == bit);
59 }
60
61 uint32_t hash_func_u32(const void *key);
62
63 bool equal_func(const void *key1, const void *key2);
64
65 bool has_eventfd(void);
66 int create_eventfd(unsigned int initval);
67 int write_eventfd(int fd, uint64_t val);
68 void flush_eventfd(int fd);
69
70 virgl_debug_callback_type virgl_log_set_logger(virgl_debug_callback_type logger);
71 void virgl_logv(const char *fmt, va_list va);
72
virgl_log(const char * fmt,...)73 static inline void PRINTFLIKE(1, 2) virgl_log(const char *fmt, ...)
74 {
75 va_list va;
76 va_start(va, fmt);
77 virgl_logv(fmt, va);
78 va_end(va);
79 }
80
81 #ifdef ENABLE_TRACING
82 void trace_init(void);
83
84 #define TRACE_INIT() trace_init()
85 #define TRACE_FUNC() TRACE_SCOPE(__func__)
86
87 #if ENABLE_TRACING == TRACE_WITH_PERCETTO
88
89 #include <percetto.h>
90
91 #define VIRGL_PERCETTO_CATEGORIES(C, G) \
92 C(virgl, "virglrenderer") \
93 C(virgls, "virglrenderer detailed events", "slow")
94
95 PERCETTO_CATEGORY_DECLARE(VIRGL_PERCETTO_CATEGORIES)
96
97 #define TRACE_SCOPE(SCOPE) TRACE_EVENT(virgl, SCOPE)
98 /* Trace high frequency events (tracing may impact performance). */
99 #define TRACE_SCOPE_SLOW(SCOPE) TRACE_EVENT(virgls, SCOPE)
100
101 #define TRACE_SCOPE_BEGIN(SCOPE) TRACE_EVENT_BEGIN(virgl, SCOPE)
102 #define TRACE_SCOPE_END(SCOPE) do { TRACE_EVENT_END(virgl); (void)SCOPE; } while (0)
103
104 #else
105
106 const char *trace_begin(const char *scope);
107 void trace_end(const char **scope);
108
109 #define TRACE_SCOPE(SCOPE) \
110 const char *trace_dummy __attribute__((cleanup (trace_end), unused)) = \
111 trace_begin(SCOPE)
112
113 #define TRACE_SCOPE_SLOW(SCOPE) TRACE_SCOPE(SCOPE)
114
115 #define TRACE_SCOPE_BEGIN(SCOPE) trace_begin(SCOPE);
116 #define TRACE_SCOPE_END(SCOPE) trace_end(&SCOPE);
117
118 #endif /* ENABLE_TRACING == TRACE_WITH_PERCETTO */
119
120 #else
121 #define TRACE_INIT()
122 #define TRACE_FUNC()
123 #define TRACE_SCOPE(SCOPE)
124 #define TRACE_SCOPE_SLOW(SCOPE)
125 #define TRACE_SCOPE_BEGIN(SCOPE)
126 #define TRACE_SCOPE_END(VAR)
127 #endif /* ENABLE_TRACING */
128
129 #endif /* VIRGL_UTIL_H */
130