• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RENDER_SERVICE_BASE_COMMON_OPTIONAL_TRACE
17 #define RENDER_SERVICE_BASE_COMMON_OPTIONAL_TRACE
18 
19 #include "foundation/graphic/graphic_2d/utils/log/rs_trace.h"
20 #include "securec.h"
21 #ifndef ROSEN_TRACE_DISABLE
22 #include "platform/common/rs_system_properties.h"
23 static inline int g_debugLevel = OHOS::Rosen::RSSystemProperties::GetDebugTraceLevel();
24 
25 #define RS_OPTIONAL_TRACE_BEGIN_LEVEL(Level, fmt, ...)           \
26     do {                                                         \
27         if (UNLIKELY(g_debugLevel >= (Level))) {                 \
28             RenderTrace::OptionalTraceStart(fmt, ##__VA_ARGS__); \
29         }                                                        \
30     } while (0)
31 
32 #define RS_OPTIONAL_TRACE_END_LEVEL(Level)                       \
33     do {                                                         \
34         if (UNLIKELY(g_debugLevel >= (Level))) {                 \
35             FinishTrace(HITRACE_TAG_GRAPHIC_AGP);                \
36         }                                                        \
37     } while (0)
38 
39 #define RS_OPTIONAL_TRACE_BEGIN(name)                            \
40     do {                                                         \
41         if (Rosen::RSSystemProperties::GetDebugTraceEnabled()) { \
42             RS_TRACE_BEGIN(name);                                \
43         }                                                        \
44     } while (0)
45 
46 #define RS_OPTIONAL_TRACE_END()                                  \
47     do {                                                         \
48         if (Rosen::RSSystemProperties::GetDebugTraceEnabled()) { \
49             RS_TRACE_END();                                      \
50         }                                                        \
51     } while (0)
52 
53 #define RS_OPTIONAL_TRACE_NAME_FMT(fmt, ...)                                \
54     do {                                                                    \
55         if (Rosen::RSSystemProperties::GetDebugTraceEnabled()) {            \
56             HITRACE_METER_FMT(HITRACE_TAG_GRAPHIC_AGP, fmt, ##__VA_ARGS__); \
57         }                                                                   \
58     } while (0)
59 
60 #define RS_OPTIONAL_TRACE_NAME_FMT_LEVEL(Level, fmt, ...)                   \
61     do {                                                                    \
62         if (Rosen::RSSystemProperties::GetDebugTraceLevel() >= Level) {     \
63             HITRACE_METER_FMT(HITRACE_TAG_GRAPHIC_AGP, fmt, ##__VA_ARGS__); \
64         }                                                                   \
65     } while (0)
66 
67 #define RS_APPOINTED_TRACE_BEGIN(node, name)                         \
68     do {                                                             \
69         if (Rosen::RSSystemProperties::GetDebugTraceEnabled() ||     \
70             Rosen::RSSystemProperties::FindNodeInTargetList(node)) { \
71             RS_TRACE_BEGIN(name);                                    \
72         }                                                            \
73     } while (0)
74 
75 #define RS_APPOINTED_TRACE_END(node)                                 \
76     do {                                                             \
77         if (Rosen::RSSystemProperties::GetDebugTraceEnabled() ||     \
78             Rosen::RSSystemProperties::FindNodeInTargetList(node)) { \
79             RS_TRACE_END();                                          \
80         }                                                            \
81     } while (0)
82 
83 #define RS_OPTIONAL_TRACE_NAME(name) RSOptionalTrace optionalTrace(name)
84 
85 #define RS_OPTIONAL_TRACE_FUNC() RSOptionalTrace optionalTrace(__func__)
86 
87 #define RS_PROCESS_TRACE(forceEnable, name) RSProcessTrace processTrace(forceEnable, name)
88 
89 #define RS_OPTIONAL_TRACE_FMT(fmt, ...)                                                         \
90     auto optionalFmtTrace = (UNLIKELY(Rosen::RSSystemProperties::GetDebugFmtTraceEnabled())) ?  \
91                                 std::make_unique<RSOptionalFmtTrace>(fmt, ##__VA_ARGS__) :      \
92                                 nullptr
93 
94 class RenderTrace {
95 public:
OptionalTraceStart(const char * fmt,...)96     static void OptionalTraceStart(const char* fmt, ...)
97     {
98         va_list vaList;
99         char buf[maxSize_];
100         va_start(vaList, fmt);
101         if (vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, fmt, vaList) < 0) {
102             va_end(vaList);
103             StartTrace(HITRACE_TAG_GRAPHIC_AGP, "length > 256, error");
104             return;
105         }
106         va_end(vaList);
107         StartTrace(HITRACE_TAG_GRAPHIC_AGP, buf);
108     }
109 private:
110     static const int maxSize_ = 256; // 256 Maximum length of a character string to be printed
111 };
112 
113 class RSOptionalFmtTrace {
114 public:
RSOptionalFmtTrace(const char * fmt,...)115     RSOptionalFmtTrace(const char* fmt, ...)
116     {
117         va_list vaList;
118         char buf[maxSize_];
119         va_start(vaList, fmt);
120         if (vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, fmt, vaList) < 0) {
121             va_end(vaList);
122             StartTrace(HITRACE_TAG_GRAPHIC_AGP, "length > 256, error");
123             return;
124         }
125         va_end(vaList);
126         StartTrace(HITRACE_TAG_GRAPHIC_AGP, buf);
127     }
~RSOptionalFmtTrace()128     ~RSOptionalFmtTrace()
129     {
130         FinishTrace(HITRACE_TAG_GRAPHIC_AGP); // 256 Maximum length of a character string to be printed
131     }
132 
133 private:
134     const int maxSize_ = 256;
135 };
136 
137 class RSOptionalTrace {
138 public:
RSOptionalTrace(const std::string & traceStr)139     RSOptionalTrace(const std::string& traceStr)
140     {
141         debugTraceEnable_ = OHOS::Rosen::RSSystemProperties::GetDebugTraceEnabled();
142         if (debugTraceEnable_) {
143             RS_TRACE_BEGIN(traceStr);
144         }
145     }
~RSOptionalTrace()146     ~RSOptionalTrace()
147     {
148         if (debugTraceEnable_) {
149             RS_TRACE_END();
150         }
151     }
152 
153 private:
154     bool debugTraceEnable_ = false;
155 };
156 
157 class RSProcessTrace {
158 public:
RSProcessTrace(bool forceEnable,const std::string & traceStr)159     RSProcessTrace(bool forceEnable, const std::string& traceStr)
160     {
161         debugTraceEnable_ = OHOS::Rosen::RSSystemProperties::GetDebugTraceEnabled();
162         forceEnable_ = forceEnable;
163         if (debugTraceEnable_ || forceEnable_) {
164             RS_TRACE_BEGIN(traceStr);
165         }
166     }
~RSProcessTrace()167     ~RSProcessTrace()
168     {
169         if (debugTraceEnable_ || forceEnable_) {
170             RS_TRACE_END();
171         }
172     }
173 private:
174     bool debugTraceEnable_ = false;
175     bool forceEnable_ = false;
176 };
177 #else
178 #define RS_OPTIONAL_TRACE_BEGIN_LEVEL(Level, fmt, ...)
179 #define RS_OPTIONAL_TRACE_END_LEVEL(Level)
180 #define RS_OPTIONAL_TRACE_BEGIN(name)
181 #define RS_OPTIONAL_TRACE_END()
182 #define RS_OPTIONAL_TRACE_NAME_FMT(fmt, ...)
183 #define RS_OPTIONAL_TRACE_FMT(fmt, ...)
184 #define RS_APPOINTED_TRACE_BEGIN(node, name)
185 #define RS_OPTIONAL_TRACE_NAME(name)
186 #define RS_OPTIONAL_TRACE_FUNC()
187 #define RS_PROCESS_TRACE(forceEnable, name)
188 #define RS_OPTIONAL_TRACE_NAME_FMT_LEVEL(Level, fmt, ...) \
189     do {                                                  \
190         (void)TRACE_LEVEL_TWO;                            \
191     } while (0)
192 #endif // ROSEN_TRACE_DISABLE
193 #endif // RENDER_SERVICE_BASE_COMMON_OPTIONAL_TRACE