Debug: Add Systrace Markers

Update ScopedPerfEventHelper class to add systrace markers by default.
This change unifies ANGLE EVENT* macro system so that at the base level
in LoggingAnnotator class, systrace markers will be added by default.

Modify the base DebugLogger to use char* by default and move any
conversions to wchar_t to the Windows specializations where wchar is
used. This limits type conversions to only where they're needed.

This change also includes some new TRACE_EVENT() calls in the VK
backend which will result in systrace markers for those calls on the
Android platform.

The new build flag "angle_enable_trace" is added to enable the tracing
calls.

Bug: angleproject:2528
Change-Id: Icefc197d4407e1cd31338710e37865abae6a0b15
Reviewed-on: https://chromium-review.googlesource.com/c/1042785
Commit-Queue: Tobin Ehlis <tobine@google.com>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Yuly Novikov <ynovikov@chromium.org>
diff --git a/src/libANGLE/renderer/d3d/d3d11/Context11.cpp b/src/libANGLE/renderer/d3d/d3d11/Context11.cpp
index 3ee92d7..41edebf 100644
--- a/src/libANGLE/renderer/d3d/d3d11/Context11.cpp
+++ b/src/libANGLE/renderer/d3d/d3d11/Context11.cpp
@@ -402,25 +402,24 @@
 
 void Context11::insertEventMarker(GLsizei length, const char *marker)
 {
-    auto optionalString = angle::WidenString(static_cast<size_t>(length), marker);
-    if (optionalString.valid())
-    {
-        mRenderer->getAnnotator()->setMarker(optionalString.value().data());
-    }
+    mRenderer->getAnnotator()->setMarker(marker);
 }
 
 void Context11::pushGroupMarker(GLsizei length, const char *marker)
 {
-    auto optionalString = angle::WidenString(static_cast<size_t>(length), marker);
-    if (optionalString.valid())
-    {
-        mRenderer->getAnnotator()->beginEvent(optionalString.value().data());
-    }
+    mRenderer->getAnnotator()->beginEvent(marker, marker);
+    mMarkerStack.push(std::string(marker));
 }
 
 void Context11::popGroupMarker()
 {
-    mRenderer->getAnnotator()->endEvent();
+    const char *marker = nullptr;
+    if (!mMarkerStack.empty())
+    {
+        marker = mMarkerStack.top().c_str();
+        mMarkerStack.pop();
+        mRenderer->getAnnotator()->endEvent(marker);
+    }
 }
 
 void Context11::pushDebugGroup(GLenum source, GLuint id, GLsizei length, const char *message)
diff --git a/src/libANGLE/renderer/d3d/d3d11/Context11.h b/src/libANGLE/renderer/d3d/d3d11/Context11.h
index d51da68..e2bde41 100644
--- a/src/libANGLE/renderer/d3d/d3d11/Context11.h
+++ b/src/libANGLE/renderer/d3d/d3d11/Context11.h
@@ -10,8 +10,8 @@
 #ifndef LIBANGLE_RENDERER_D3D_D3D11_CONTEXT11_H_
 #define LIBANGLE_RENDERER_D3D_D3D11_CONTEXT11_H_
 
+#include <stack>
 #include "libANGLE/renderer/ContextImpl.h"
-
 #include "libANGLE/renderer/d3d/ContextD3D.h"
 
 namespace rx
@@ -177,6 +177,7 @@
 
     Renderer11 *mRenderer;
     IncompleteTextureSet mIncompleteTextures;
+    std::stack<std::string> mMarkerStack;
 };
 }  // namespace rx
 
diff --git a/src/libANGLE/renderer/d3d/d3d11/DebugAnnotator11.cpp b/src/libANGLE/renderer/d3d/d3d11/DebugAnnotator11.cpp
index 1e70363..e7e3425 100644
--- a/src/libANGLE/renderer/d3d/d3d11/DebugAnnotator11.cpp
+++ b/src/libANGLE/renderer/d3d/d3d11/DebugAnnotator11.cpp
@@ -35,33 +35,40 @@
     }
 }
 
-void DebugAnnotator11::beginEvent(const wchar_t *eventName)
+void DebugAnnotator11::beginEvent(const char *eventName, const char *eventMessage)
 {
     initializeDevice();
 
+    angle::LoggingAnnotator::beginEvent(eventName, eventMessage);
     if (mUserDefinedAnnotation != nullptr)
     {
-        mUserDefinedAnnotation->BeginEvent(eventName);
+        std::mbstate_t state = std::mbstate_t();
+        std::mbsrtowcs(mWCharMessage, &eventMessage, kMaxMessageLength, &state);
+        mUserDefinedAnnotation->BeginEvent(mWCharMessage);
     }
 }
 
-void DebugAnnotator11::endEvent()
+void DebugAnnotator11::endEvent(const char *eventName)
 {
     initializeDevice();
 
+    angle::LoggingAnnotator::endEvent(eventName);
     if (mUserDefinedAnnotation != nullptr)
     {
         mUserDefinedAnnotation->EndEvent();
     }
 }
 
-void DebugAnnotator11::setMarker(const wchar_t *markerName)
+void DebugAnnotator11::setMarker(const char *markerName)
 {
     initializeDevice();
 
+    angle::LoggingAnnotator::setMarker(markerName);
     if (mUserDefinedAnnotation != nullptr)
     {
-        mUserDefinedAnnotation->SetMarker(markerName);
+        std::mbstate_t state = std::mbstate_t();
+        std::mbsrtowcs(mWCharMessage, &markerName, kMaxMessageLength, &state);
+        mUserDefinedAnnotation->SetMarker(mWCharMessage);
     }
 }
 
diff --git a/src/libANGLE/renderer/d3d/d3d11/DebugAnnotator11.h b/src/libANGLE/renderer/d3d/d3d11/DebugAnnotator11.h
index 62662c4..a918984 100644
--- a/src/libANGLE/renderer/d3d/d3d11/DebugAnnotator11.h
+++ b/src/libANGLE/renderer/d3d/d3d11/DebugAnnotator11.h
@@ -19,9 +19,9 @@
   public:
     DebugAnnotator11();
     ~DebugAnnotator11() override;
-    void beginEvent(const wchar_t *eventName) override;
-    void endEvent() override;
-    void setMarker(const wchar_t *markerName) override;
+    void beginEvent(const char *eventName, const char *eventMessage) override;
+    void endEvent(const char *eventName) override;
+    void setMarker(const char *markerName) override;
     bool getStatus() override;
 
   private:
@@ -30,6 +30,8 @@
     bool mInitialized;
     HMODULE mD3d11Module;
     ID3DUserDefinedAnnotation *mUserDefinedAnnotation;
+    static constexpr size_t kMaxMessageLength = 256;
+    wchar_t mWCharMessage[kMaxMessageLength];
 };
 
 }
diff --git a/src/libANGLE/renderer/d3d/d3d9/Context9.cpp b/src/libANGLE/renderer/d3d/d3d9/Context9.cpp
index 0528190..ddf74fc 100644
--- a/src/libANGLE/renderer/d3d/d3d9/Context9.cpp
+++ b/src/libANGLE/renderer/d3d/d3d9/Context9.cpp
@@ -227,25 +227,24 @@
 
 void Context9::insertEventMarker(GLsizei length, const char *marker)
 {
-    auto optionalString = angle::WidenString(static_cast<size_t>(length), marker);
-    if (optionalString.valid())
-    {
-        mRenderer->getAnnotator()->setMarker(optionalString.value().data());
-    }
+    mRenderer->getAnnotator()->setMarker(marker);
 }
 
 void Context9::pushGroupMarker(GLsizei length, const char *marker)
 {
-    auto optionalString = angle::WidenString(static_cast<size_t>(length), marker);
-    if (optionalString.valid())
-    {
-        mRenderer->getAnnotator()->beginEvent(optionalString.value().data());
-    }
+    mRenderer->getAnnotator()->beginEvent(marker, marker);
+    mMarkerStack.push(std::string(marker));
 }
 
 void Context9::popGroupMarker()
 {
-    mRenderer->getAnnotator()->endEvent();
+    const char *marker = nullptr;
+    if (!mMarkerStack.empty())
+    {
+        marker = mMarkerStack.top().c_str();
+        mMarkerStack.pop();
+        mRenderer->getAnnotator()->endEvent(marker);
+    }
 }
 
 void Context9::pushDebugGroup(GLenum source, GLuint id, GLsizei length, const char *message)
diff --git a/src/libANGLE/renderer/d3d/d3d9/Context9.h b/src/libANGLE/renderer/d3d/d3d9/Context9.h
index ee3855e..f9f1456 100644
--- a/src/libANGLE/renderer/d3d/d3d9/Context9.h
+++ b/src/libANGLE/renderer/d3d/d3d9/Context9.h
@@ -10,6 +10,7 @@
 #ifndef LIBANGLE_RENDERER_D3D_D3D9_CONTEXT9_H_
 #define LIBANGLE_RENDERER_D3D_D3D9_CONTEXT9_H_
 
+#include <stack>
 #include "libANGLE/renderer/d3d/ContextD3D.h"
 
 namespace rx
@@ -162,6 +163,7 @@
   private:
     Renderer9 *mRenderer;
     IncompleteTextureSet mIncompleteTextures;
+    std::stack<std::string> mMarkerStack;
 };
 
 }  // namespace rx
diff --git a/src/libANGLE/renderer/d3d/d3d9/DebugAnnotator9.cpp b/src/libANGLE/renderer/d3d/d3d9/DebugAnnotator9.cpp
index 6ec35e1..b699603 100644
--- a/src/libANGLE/renderer/d3d/d3d9/DebugAnnotator9.cpp
+++ b/src/libANGLE/renderer/d3d/d3d9/DebugAnnotator9.cpp
@@ -13,19 +13,26 @@
 namespace rx
 {
 
-void DebugAnnotator9::beginEvent(const wchar_t *eventName)
+void DebugAnnotator9::beginEvent(const char *eventName, const char *eventMessage)
 {
-    D3DPERF_BeginEvent(0, eventName);
+    angle::LoggingAnnotator::beginEvent(eventName, eventMessage);
+    std::mbstate_t state = std::mbstate_t();
+    std::mbsrtowcs(mWCharMessage, &eventMessage, kMaxMessageLength, &state);
+    D3DPERF_BeginEvent(0, mWCharMessage);
 }
 
-void DebugAnnotator9::endEvent()
+void DebugAnnotator9::endEvent(const char *eventName)
 {
+    angle::LoggingAnnotator::endEvent(eventName);
     D3DPERF_EndEvent();
 }
 
-void DebugAnnotator9::setMarker(const wchar_t *markerName)
+void DebugAnnotator9::setMarker(const char *markerName)
 {
-    D3DPERF_SetMarker(0, markerName);
+    angle::LoggingAnnotator::setMarker(markerName);
+    std::mbstate_t state = std::mbstate_t();
+    std::mbsrtowcs(mWCharMessage, &markerName, kMaxMessageLength, &state);
+    D3DPERF_SetMarker(0, mWCharMessage);
 }
 
 bool DebugAnnotator9::getStatus()
diff --git a/src/libANGLE/renderer/d3d/d3d9/DebugAnnotator9.h b/src/libANGLE/renderer/d3d/d3d9/DebugAnnotator9.h
index b280083..4b6d12f 100644
--- a/src/libANGLE/renderer/d3d/d3d9/DebugAnnotator9.h
+++ b/src/libANGLE/renderer/d3d/d3d9/DebugAnnotator9.h
@@ -18,10 +18,14 @@
 {
   public:
     DebugAnnotator9() {}
-    void beginEvent(const wchar_t *eventName) override;
-    void endEvent() override;
-    void setMarker(const wchar_t *markerName) override;
+    void beginEvent(const char *eventName, const char *eventMessage) override;
+    void endEvent(const char *eventName) override;
+    void setMarker(const char *markerName) override;
     bool getStatus() override;
+
+  private:
+    static constexpr size_t kMaxMessageLength = 256;
+    wchar_t mWCharMessage[kMaxMessageLength];
 };
 
 }
diff --git a/src/libANGLE/renderer/vulkan/ContextVk.cpp b/src/libANGLE/renderer/vulkan/ContextVk.cpp
index da3496b..c08a251 100644
--- a/src/libANGLE/renderer/vulkan/ContextVk.cpp
+++ b/src/libANGLE/renderer/vulkan/ContextVk.cpp
@@ -32,6 +32,8 @@
 #include "libANGLE/renderer/vulkan/TransformFeedbackVk.h"
 #include "libANGLE/renderer/vulkan/VertexArrayVk.h"
 
+#include "third_party/trace_event/trace_event.h"
+
 namespace rx
 {
 
@@ -122,6 +124,7 @@
       mViewport{},
       mScissor{}
 {
+    TRACE_EVENT0("gpu.angle", "ContextVk::ContextVk");
     memset(&mClearColorValue, 0, sizeof(mClearColorValue));
     memset(&mClearDepthStencilValue, 0, sizeof(mClearDepthStencilValue));
 
@@ -189,6 +192,7 @@
 
 angle::Result ContextVk::initialize()
 {
+    TRACE_EVENT0("gpu.angle", "ContextVk::initialize");
     // Note that this may reserve more sets than strictly necessary for a particular layout.
     ANGLE_TRY(mDynamicDescriptorPools[kUniformsDescriptorSetIndex].init(
         this, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, GetUniformBufferDescriptorCount()));
diff --git a/src/libANGLE/renderer/vulkan/RendererVk.cpp b/src/libANGLE/renderer/vulkan/RendererVk.cpp
index 6d85f90..f148613 100644
--- a/src/libANGLE/renderer/vulkan/RendererVk.cpp
+++ b/src/libANGLE/renderer/vulkan/RendererVk.cpp
@@ -1020,6 +1020,7 @@
                                       const VkSubmitInfo &submitInfo,
                                       vk::CommandBuffer &&commandBuffer)
 {
+    TRACE_EVENT0("gpu.angle", "RendererVk::submitFrame");
     VkFenceCreateInfo fenceInfo = {};
     fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
     fenceInfo.flags = 0;