• 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 "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 class RenderTrace {
90 public:
OptionalTraceStart(const char * fmt,...)91     static void OptionalTraceStart(const char* fmt, ...)
92     {
93         va_list vaList;
94         char buf[maxSize_];
95         va_start(vaList, fmt);
96         if (vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, fmt, vaList) < 0) {
97             va_end(vaList);
98             StartTrace(HITRACE_TAG_GRAPHIC_AGP, "length > 256, error");
99             return;
100         }
101         va_end(vaList);
102         StartTrace(HITRACE_TAG_GRAPHIC_AGP, buf);
103     }
104 private:
105     static const int maxSize_ = 256; // 256 Maximum length of a character string to be printed
106 };
107 
108 class RSOptionalTrace {
109 public:
RSOptionalTrace(const std::string & traceStr)110     RSOptionalTrace(const std::string& traceStr)
111     {
112         debugTraceEnable_ = OHOS::Rosen::RSSystemProperties::GetDebugTraceEnabled();
113         if (debugTraceEnable_) {
114             RS_TRACE_BEGIN(traceStr);
115         }
116     }
~RSOptionalTrace()117     ~RSOptionalTrace()
118     {
119         if (debugTraceEnable_) {
120             RS_TRACE_END();
121         }
122     }
123 
124 private:
125     bool debugTraceEnable_ = false;
126 };
127 
128 class RSProcessTrace {
129 public:
RSProcessTrace(bool forceEnable,const std::string & traceStr)130     RSProcessTrace(bool forceEnable, const std::string& traceStr)
131     {
132         debugTraceEnable_ = OHOS::Rosen::RSSystemProperties::GetDebugTraceEnabled();
133         forceEnable_ = forceEnable;
134         if (debugTraceEnable_ || forceEnable_) {
135             RS_TRACE_BEGIN(traceStr);
136         }
137     }
~RSProcessTrace()138     ~RSProcessTrace()
139     {
140         if (debugTraceEnable_ || forceEnable_) {
141             RS_TRACE_END();
142         }
143     }
144 private:
145     bool debugTraceEnable_ = false;
146     bool forceEnable_ = false;
147 };
148 #else
149 #define RS_OPTIONAL_TRACE_BEGIN_LEVEL(Level, fmt, ...)
150 #define RS_OPTIONAL_TRACE_END_LEVEL(Level)
151 #define RS_OPTIONAL_TRACE_BEGIN(name)
152 #define RS_OPTIONAL_TRACE_END()
153 #define RS_OPTIONAL_TRACE_NAME_FMT(fmt, ...)
154 #define RS_APPOINTED_TRACE_BEGIN(node, name)
155 #define RS_OPTIONAL_TRACE_NAME(name)
156 #define RS_OPTIONAL_TRACE_FUNC()
157 #define RS_PROCESS_TRACE(forceEnable, name)
158 #define RS_OPTIONAL_TRACE_NAME_FMT_LEVEL(Level, fmt, ...) \
159     do {                                                  \
160         (void)TRACE_LEVEL_TWO;                            \
161     } while (0)
162 #endif //ROSEN_TRACE_DISABLE
163 #endif // RENDER_SERVICE_BASE_COMMON_OPTIONAL_TRACE