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 #ifndef ROSEN_TRACE_DISABLE
21 #include "platform/common/rs_system_properties.h"
22 #define RS_OPTIONAL_TRACE_BEGIN(name)                            \
23     do {                                                         \
24         if (Rosen::RSSystemProperties::GetDebugTraceEnabled()) { \
25             RS_TRACE_BEGIN(name);                                \
26         }                                                        \
27     } while (0)
28 
29 #define RS_OPTIONAL_TRACE_END()                                  \
30     do {                                                         \
31         if (Rosen::RSSystemProperties::GetDebugTraceEnabled()) { \
32             RS_TRACE_END();                                      \
33         }                                                        \
34     } while (0)
35 
36 #define RS_OPTIONAL_TRACE_NAME_FMT(fmt, ...)                                \
37     do {                                                                    \
38         if (Rosen::RSSystemProperties::GetDebugTraceEnabled()) {            \
39             HITRACE_METER_FMT(HITRACE_TAG_GRAPHIC_AGP, fmt, ##__VA_ARGS__); \
40         }                                                                   \
41     } while (0)
42 
43 #define RS_OPTIONAL_TRACE_NAME_FMT_LEVEL(Level, fmt, ...)                   \
44     do {                                                                    \
45         if (Rosen::RSSystemProperties::GetDebugTraceLevel() >= Level) {     \
46             HITRACE_METER_FMT(HITRACE_TAG_GRAPHIC_AGP, fmt, ##__VA_ARGS__); \
47         }                                                                   \
48     } while (0)
49 
50 #define RS_APPOINTED_TRACE_BEGIN(node, name)                         \
51     do {                                                             \
52         if (Rosen::RSSystemProperties::GetDebugTraceEnabled() ||     \
53             Rosen::RSSystemProperties::FindNodeInTargetList(node)) { \
54             RS_TRACE_BEGIN(name);                                    \
55         }                                                            \
56     } while (0)
57 
58 #define RS_APPOINTED_TRACE_END(node)                                 \
59     do {                                                             \
60         if (Rosen::RSSystemProperties::GetDebugTraceEnabled() ||     \
61             Rosen::RSSystemProperties::FindNodeInTargetList(node)) { \
62             RS_TRACE_END();                                          \
63         }                                                            \
64     } while (0)
65 
66 #define RS_OPTIONAL_TRACE_NAME(name) RSOptionalTrace optionalTrace(name)
67 
68 #define RS_OPTIONAL_TRACE_FUNC() RSOptionalTrace optionalTrace(__func__)
69 
70 #define RS_PROCESS_TRACE(forceEnable, name) RSProcessTrace processTrace(forceEnable, name)
71 
72 class RSOptionalTrace {
73 public:
RSOptionalTrace(const std::string & traceStr)74     RSOptionalTrace(const std::string& traceStr)
75     {
76         debugTraceEnable_ = OHOS::Rosen::RSSystemProperties::GetDebugTraceEnabled();
77         if (debugTraceEnable_) {
78             RS_TRACE_BEGIN(traceStr);
79         }
80     }
~RSOptionalTrace()81     ~RSOptionalTrace()
82     {
83         if (debugTraceEnable_) {
84             RS_TRACE_END();
85         }
86     }
87 
88 private:
89     bool debugTraceEnable_ = false;
90 };
91 
92 class RSProcessTrace {
93 public:
RSProcessTrace(bool forceEnable,const std::string & traceStr)94     RSProcessTrace(bool forceEnable, const std::string& traceStr)
95     {
96         debugTraceEnable_ = OHOS::Rosen::RSSystemProperties::GetDebugTraceEnabled();
97         forceEnable_ = forceEnable;
98         if (debugTraceEnable_ || forceEnable_) {
99             RS_TRACE_BEGIN(traceStr);
100         }
101     }
~RSProcessTrace()102     ~RSProcessTrace()
103     {
104         if (debugTraceEnable_ || forceEnable_) {
105             RS_TRACE_END();
106         }
107     }
108 private:
109     bool debugTraceEnable_ = false;
110     bool forceEnable_ = false;
111 };
112 #else
113 #define RS_OPTIONAL_TRACE_BEGIN(name)
114 #define RS_OPTIONAL_TRACE_END()
115 #define RS_OPTIONAL_TRACE_NAME_FMT(fmt, ...)
116 #define RS_APPOINTED_TRACE_BEGIN(node, name)
117 #define RS_OPTIONAL_TRACE_NAME(name)
118 #define RS_OPTIONAL_TRACE_FUNC()
119 #define RS_PROCESS_TRACE(forceEnable, name)
120 #define RS_OPTIONAL_TRACE_NAME_FMT_LEVEL(Level, fmt, ...) \
121     do {                                                  \
122         (void)TRACE_LEVEL_TWO;                            \
123     } while (0)
124 #endif //ROSEN_TRACE_DISABLE
125 #endif // RENDER_SERVICE_BASE_COMMON_OPTIONAL_TRACE