Add run-time check in EVENT() to disable debug markers

To avoid a large performance impact from adding Vulkan debug-util
markers for every GLES entrypoint, the EVENT() macro needs a run-time
check that avoids constructing a ScopedPerfEventHelper object.

Test: angle_perftests -v --local-output --gtest_filter="TracePerfTest.Run/*nba*"
Bug: b/170249632
Change-Id: I422111cdf6f6f713800e7ac587e66582bd00359f
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2491009
Commit-Queue: Ian Elliott <ianelliott@google.com>
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/common/debug.cpp b/src/common/debug.cpp
index a4545bf..575ea50 100644
--- a/src/common/debug.cpp
+++ b/src/common/debug.cpp
@@ -120,19 +120,22 @@
     return *g_debugMutex;
 }
 
-ScopedPerfEventHelper::ScopedPerfEventHelper(gl::Context *context,
-                                             gl::EntryPoint entryPoint,
-                                             const char *format,
-                                             ...)
-    : mContext(context), mEntryPoint(entryPoint), mFunctionName(GetEntryPointName(entryPoint))
+ScopedPerfEventHelper::ScopedPerfEventHelper(gl::Context *context, gl::EntryPoint entryPoint)
+    : mContext(context), mEntryPoint(entryPoint), mFunctionName(nullptr)
+{}
+
+ScopedPerfEventHelper::~ScopedPerfEventHelper()
 {
-    bool dbgTrace = DebugAnnotationsActive();
-#if !defined(ANGLE_ENABLE_DEBUG_TRACE)
-    if (!dbgTrace)
+    // EGL_Terminate() can set g_debugAnnotator to nullptr; must call DebugAnnotationsActive() here
+    if (mFunctionName && DebugAnnotationsActive())
     {
-        return;
+        g_debugAnnotator->endEvent(mContext, mFunctionName, mEntryPoint);
     }
-#endif  // !ANGLE_ENABLE_DEBUG_TRACE
+}
+
+void ScopedPerfEventHelper::begin(const char *format, ...)
+{
+    mFunctionName = GetEntryPointName(mEntryPoint);
 
     va_list vararg;
     va_start(vararg, format);
@@ -142,18 +145,8 @@
     va_end(vararg);
 
     ANGLE_LOG(EVENT) << std::string(&buffer[0], len);
-    if (dbgTrace)
-    {
-        g_debugAnnotator->beginEvent(context, entryPoint, mFunctionName, buffer.data());
-    }
-}
-
-ScopedPerfEventHelper::~ScopedPerfEventHelper()
-{
-    if (DebugAnnotationsActive())
-    {
-        g_debugAnnotator->endEvent(mContext, mFunctionName, mEntryPoint);
-    }
+    // Do not need to call DebugAnnotationsActive() here, because it was called in EVENT()
+    g_debugAnnotator->beginEvent(mContext, mEntryPoint, mFunctionName, buffer.data());
 }
 
 LogMessage::LogMessage(const char *file, const char *function, int line, LogSeverity severity)
diff --git a/src/common/debug.h b/src/common/debug.h
index 532bff1..3a46f50 100644
--- a/src/common/debug.h
+++ b/src/common/debug.h
@@ -35,9 +35,10 @@
 class ScopedPerfEventHelper : angle::NonCopyable
 {
   public:
-    ANGLE_FORMAT_PRINTF(4, 5)
-    ScopedPerfEventHelper(gl::Context *context, gl::EntryPoint entryPoint, const char *format, ...);
+    ScopedPerfEventHelper(gl::Context *context, gl::EntryPoint entryPoint);
     ~ScopedPerfEventHelper();
+    ANGLE_FORMAT_PRINTF(2, 3)
+    void begin(const char *format, ...);
 
   private:
     gl::Context *mContext;
@@ -254,13 +255,26 @@
 // A macro to log a performance event around a scope.
 #if defined(ANGLE_TRACE_ENABLED)
 #    if defined(_MSC_VER)
-#        define EVENT(context, entryPoint, function, message, ...)     \
-            gl::ScopedPerfEventHelper scopedPerfEventHelper##__LINE__( \
-                context, entryPoint, "%s(" message ")", function, __VA_ARGS__)
+#        define EVENT(context, entryPoint, function, message, ...)                          \
+            gl::ScopedPerfEventHelper scopedPerfEventHelper##__LINE__(context, entryPoint); \
+            do                                                                              \
+            {                                                                               \
+                if (gl::DebugAnnotationsActive())                                           \
+                {                                                                           \
+                    scopedPerfEventHelper##__LINE__.begin("%s(" message ")", function,      \
+                                                          __VA_ARGS__);                     \
+                }                                                                           \
+            } while (0)
 #    else
-#        define EVENT(context, entryPoint, function, message, ...) \
-            gl::ScopedPerfEventHelper scopedPerfEventHelper(       \
-                context, entryPoint, "%s(" message ")", function, ##__VA_ARGS__)
+#        define EVENT(context, entryPoint, function, message, ...)                           \
+            gl::ScopedPerfEventHelper scopedPerfEventHelper(context, entryPoint);            \
+            do                                                                               \
+            {                                                                                \
+                if (gl::DebugAnnotationsActive())                                            \
+                {                                                                            \
+                    scopedPerfEventHelper.begin("%s(" message ")", function, ##__VA_ARGS__); \
+                }                                                                            \
+            } while (0)
 #    endif  // _MSC_VER
 #else
 #    define EVENT(message, ...) (void(0))