Add GPU_STRATUP_TRACE_EVENT calls [1/2]

Adding traces to basic stages of gpu_init:
- GpuInfo collection
- Sandboxing
- Watchdog startup
- Ozone init
- GL, Dawn, Vulkan init, etc.

Change-Id: If949d27e4fbc0b8ab4c2ec738d016468f43cb079
Bug: 380411640
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6091031
Reviewed-by: Zhenyao Mo <zmo@chromium.org>
Commit-Queue: Kramer Ge <fangzhoug@chromium.org>
Reviewed-by: Colin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1398029}
diff --git a/content/gpu/gpu_main.cc b/content/gpu/gpu_main.cc
index 47e3435..d4ba760 100644
--- a/content/gpu/gpu_main.cc
+++ b/content/gpu/gpu_main.cc
@@ -463,7 +463,6 @@
 bool StartSandboxLinux(gpu::GpuWatchdogThread* watchdog_thread,
                        const gpu::GPUInfo* gpu_info,
                        const gpu::GpuPreferences& gpu_prefs) {
-  TRACE_EVENT0("gpu,startup", "Initialize sandbox");
   GPU_STARTUP_TRACE_EVENT("Initialize sandbox");
 
   if (watchdog_thread) {
@@ -527,7 +526,6 @@
 
 #if BUILDFLAG(IS_WIN)
 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
-  TRACE_EVENT0("gpu,startup", "Lower token");
   GPU_STARTUP_TRACE_EVENT("Lower token");
 
   // For Windows, if the target_services interface is not zero, the process
diff --git a/gpu/config/gpu_info_collector.cc b/gpu/config/gpu_info_collector.cc
index 9e3245b..e0b6ce0c 100644
--- a/gpu/config/gpu_info_collector.cc
+++ b/gpu/config/gpu_info_collector.cc
@@ -41,6 +41,7 @@
 #include "ui/gl/gl_version_info.h"
 #include "ui/gl/init/create_gr_gl_interface.h"
 #include "ui/gl/init/gl_factory.h"
+#include "ui/gl/startup_trace.h"
 
 #if BUILDFLAG(IS_MAC)
 #include "base/apple/bundle_locations.h"
@@ -89,6 +90,7 @@
 }
 
 scoped_refptr<gl::GLContext> InitializeGLContext(gl::GLSurface* surface) {
+  GPU_STARTUP_TRACE_EVENT("gpu_info_collector::InitializeGLContext");
   gl::GLContextAttribs attribs;
   attribs.client_major_es_version = 2;
   scoped_refptr<gl::GLContext> context(
@@ -530,6 +532,7 @@
 
 bool CollectBasicGraphicsInfo(const base::CommandLine* command_line,
                               GPUInfo* gpu_info) {
+  GPU_STARTUP_TRACE_EVENT("gpu_info_collector::CollectBasicGraphicsInfo");
   // In the info-collection GPU process on Windows, we get the device info from
   // the browser.
   if (CollectGraphicsDeviceInfoFromCommandLine(command_line, gpu_info)) {
@@ -578,7 +581,7 @@
 }
 
 bool CollectGraphicsInfoGL(GPUInfo* gpu_info, gl::GLDisplay* display) {
-  TRACE_EVENT0("startup", "gpu_info_collector::CollectGraphicsInfoGL");
+  GPU_STARTUP_TRACE_EVENT("gpu_info_collector::CollectGraphicsInfoGL");
   DCHECK_NE(gl::GetGLImplementationParts(), gl::kGLImplementationNone);
   gl::GLDisplayEGL* egl_display = display->GetAs<gl::GLDisplayEGL>();
 
@@ -804,6 +807,7 @@
 
 bool CollectGpuExtraInfo(gfx::GpuExtraInfo* gpu_extra_info,
                          const GpuPreferences& prefs) {
+  GPU_STARTUP_TRACE_EVENT("gpu_info_collector::CollectGpuExtraInfo");
   // Populate the list of ANGLE features by querying the functions exposed by
   // EGL_ANGLE_feature_control if it's available.
   if (gl::g_driver_egl.client_ext.b_EGL_ANGLE_feature_control) {
@@ -836,6 +840,7 @@
 void CollectDawnInfo(const gpu::GpuPreferences& gpu_preferences,
                      bool collect_metrics,
                      std::vector<std::string>* dawn_info_list) {
+  GPU_STARTUP_TRACE_EVENT("gpu_info_collector::CollectDawnInfo");
 #if BUILDFLAG(USE_DAWN)
   DawnProcTable procs = dawn::native::GetProcs();
   dawnProcSetProcs(&procs);
diff --git a/gpu/config/gpu_util.cc b/gpu/config/gpu_util.cc
index b7dba3a..3921b6d 100644
--- a/gpu/config/gpu_util.cc
+++ b/gpu/config/gpu_util.cc
@@ -7,6 +7,7 @@
 #include <string_view>
 
 #include "build/build_config.h"
+#include "ui/gl/startup_trace.h"
 
 #if BUILDFLAG(IS_WIN)
 #include <windows.h>
@@ -531,6 +532,7 @@
                                      const GpuPreferences& gpu_preferences,
                                      base::CommandLine* command_line,
                                      bool* needs_more_info) {
+  GPU_STARTUP_TRACE_EVENT("gpu_util::ComputeGpuFeatureInfo");
   bool use_swift_shader = false;
   bool blocklist_needs_more_info = false;
 
diff --git a/gpu/ipc/service/gpu_init.cc b/gpu/ipc/service/gpu_init.cc
index ff3655c..43a3d70 100644
--- a/gpu/ipc/service/gpu_init.cc
+++ b/gpu/ipc/service/gpu_init.cc
@@ -49,6 +49,7 @@
 #include "ui/gl/gl_switches.h"
 #include "ui/gl/gl_utils.h"
 #include "ui/gl/init/gl_factory.h"
+#include "ui/gl/startup_trace.h"
 
 #if BUILDFLAG(IS_MAC)
 #include <GLES2/gl2.h>
@@ -101,7 +102,7 @@
 namespace {
 bool CollectGraphicsInfo(GPUInfo* gpu_info) {
   DCHECK(gpu_info);
-  TRACE_EVENT0("gpu,startup", "Collect Graphics Info");
+  GPU_STARTUP_TRACE_EVENT("Collect Graphics Info");
   bool success = CollectContextGraphicsInfo(gpu_info);
   if (!success)
     LOG(ERROR) << "CollectGraphicsInfo failed.";
@@ -110,6 +111,7 @@
 
 void InitializeDawnProcs() {
 #if BUILDFLAG(USE_DAWN) || BUILDFLAG(SKIA_USE_DAWN)
+  GPU_STARTUP_TRACE_EVENT("gpu_init::InitializeDawnProcs");
   // Setup the global procs table for GPU process.
   dawnProcSetProcs(&dawn::native::GetProcs());
 #endif  // BUILDFLAG(USE_DAWN) || BUILDFLAG(SKIA_USE_DAWN)
@@ -251,6 +253,7 @@
 // Returns the default GPU's system_device_id.
 void SetupGLDisplayManagerEGL(const GPUInfo& gpu_info,
                               const GpuFeatureInfo& gpu_feature_info) {
+  GPU_STARTUP_TRACE_EVENT("gpu_init::SetupGLDisplayManagerEGL");
   const GPUInfo::GPUDevice* gpu_high_perf =
       gpu_info.GetGpuByPreference(gl::GpuPreference::kHighPerformance);
   const GPUInfo::GPUDevice* gpu_low_power =
@@ -324,6 +327,7 @@
 
 bool GpuInit::InitializeAndStartSandbox(base::CommandLine* command_line,
                                         const GpuPreferences& gpu_preferences) {
+  GPU_STARTUP_TRACE_EVENT("gpu::GpuInit::InitializeAndStartSandbox");
 #if BUILDFLAG(IS_CHROMEOS_ASH)
   LOG(WARNING) << "Starting gpu initialization.";
 #endif
@@ -419,6 +423,7 @@
   // Start the GPU watchdog only after anything that is expected to be time
   // consuming has completed, otherwise the process is liable to be aborted.
   if (enable_watchdog && !delayed_watchdog_enable) {
+    GPU_STARTUP_TRACE_EVENT("Create GpuWatchdog");
     watchdog_thread_ =
         GpuWatchdogThread::Create(gpu_preferences_.watchdog_starts_backgrounded,
                                   gl_use_swiftshader_, "GpuWatchdog");
@@ -753,10 +758,14 @@
 #if BUILDFLAG(IS_OZONE)
   // We need to get supported formats before sandboxing to avoid an known
   // issue which breaks the camera preview. (b/166850715)
-  std::vector<gfx::BufferFormat> supported_buffer_formats_for_texturing =
-      ui::OzonePlatform::GetInstance()
-          ->GetSurfaceFactoryOzone()
-          ->GetSupportedFormatsForTexturing();
+  std::vector<gfx::BufferFormat> supported_buffer_formats_for_texturing;
+  {
+    GPU_STARTUP_TRACE_EVENT("ui::ozone::GetSupportedFormatsForTexturing");
+    supported_buffer_formats_for_texturing =
+        ui::OzonePlatform::GetInstance()
+            ->GetSurfaceFactoryOzone()
+            ->GetSupportedFormatsForTexturing();
+  }
   std::vector<gfx::BufferFormat>
       supported_buffer_formats_for_gl_native_pixmap_import =
           ui::OzonePlatform::GetInstance()
@@ -1157,6 +1166,7 @@
 
 bool GpuInit::InitializeDawn() {
 #if BUILDFLAG(SKIA_USE_DAWN)
+  GPU_STARTUP_TRACE_EVENT("gpu::GpuInit::InitializeDawn");
   if (gpu_feature_info_.status_values[GPU_FEATURE_TYPE_SKIA_GRAPHITE] !=
           kGpuFeatureStatusEnabled &&
       !gpu::DawnContextProvider::DefaultForceFallbackAdapter()) {
@@ -1216,6 +1226,7 @@
 
 bool GpuInit::InitializeVulkan() {
 #if BUILDFLAG(ENABLE_VULKAN)
+  GPU_STARTUP_TRACE_EVENT("gpu::GpuInit::InitializeVulkan");
   DCHECK_EQ(gpu_feature_info_.status_values[GPU_FEATURE_TYPE_VULKAN],
             kGpuFeatureStatusEnabled);
   DCHECK_NE(gpu_preferences_.use_vulkan, VulkanImplementationName::kNone);
diff --git a/ui/gl/init/gl_display_initializer.cc b/ui/gl/init/gl_display_initializer.cc
index 57b2933..9ba97c0 100644
--- a/ui/gl/init/gl_display_initializer.cc
+++ b/ui/gl/init/gl_display_initializer.cc
@@ -10,6 +10,7 @@
 #include "ui/gl/gl_implementation.h"
 #include "ui/gl/gl_utils.h"
 #include "ui/gl/init/gl_factory.h"
+#include "ui/gl/startup_trace.h"
 
 namespace gl::init {
 
@@ -32,6 +33,7 @@
                         bool supports_angle_metal,
                         const base::CommandLine* command_line,
                         std::vector<DisplayType>* init_displays) {
+  GPU_STARTUP_TRACE_EVENT("gl_display_initializer::GetEGLInitDisplays");
   // Check which experiment groups we're in. Check these early in the function
   // so that finch assigns a group before the final decision to use the API is
   // made. If we check too late, it will appear that some users are missing from
diff --git a/ui/gl/init/gl_factory.cc b/ui/gl/init/gl_factory.cc
index c461eb16..c5c479f 100644
--- a/ui/gl/init/gl_factory.cc
+++ b/ui/gl/init/gl_factory.cc
@@ -20,6 +20,7 @@
 #include "ui/gl/gl_surface.h"
 #include "ui/gl/gl_utils.h"
 #include "ui/gl/init/gl_initializer.h"
+#include "ui/gl/startup_trace.h"
 
 #if BUILDFLAG(IS_OZONE)
 #include "ui/base/ui_base_features.h"
@@ -109,6 +110,7 @@
                                             gl::GpuPreference gpu_preference) {
   TRACE_EVENT1("gpu,startup", "gl::init::InitializeGLOneOffPlatformHelper",
                "init_extensions", init_extensions);
+  GPU_STARTUP_TRACE_EVENT("gl::init::InitializeGLOneOffPlatformHelper");
 
   const base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
   bool disable_gl_drawing = cmd->HasSwitch(switches::kDisableGLDrawingForTests);
@@ -120,7 +122,7 @@
 }  // namespace
 
 GLDisplay* InitializeGLOneOff(gl::GpuPreference gpu_preference) {
-  TRACE_EVENT0("gpu,startup", "gl::init::InitializeOneOff");
+  GPU_STARTUP_TRACE_EVENT("gl::init::InitializeOneOff");
 
   if (!InitializeStaticGLBindingsOneOff())
     return nullptr;
@@ -135,6 +137,7 @@
                                           gl::GpuPreference gpu_preference) {
   TRACE_EVENT1("gpu,startup", "gl::init::InitializeNoExtensionsOneOff",
                "init_bindings", init_bindings);
+  GPU_STARTUP_TRACE_EVENT("gl::init::InitializeNoExtensionsOneOff");
   if (init_bindings) {
     if (!InitializeStaticGLBindingsOneOff())
       return nullptr;
@@ -148,6 +151,7 @@
 
 bool InitializeStaticGLBindingsOneOff() {
   DCHECK_EQ(kGLImplementationNone, GetGLImplementation());
+  GPU_STARTUP_TRACE_EVENT("gl::init::InitializeStaticGLBindingsOneOff");
 
   GLImplementationParts impl = GetRequestedGLImplementation();
   if (impl.gl == kGLImplementationDisabled) {
diff --git a/ui/gl/startup_trace.h b/ui/gl/startup_trace.h
index 64f12ff..3b5b554 100644
--- a/ui/gl/startup_trace.h
+++ b/ui/gl/startup_trace.h
@@ -9,6 +9,7 @@
 
 #include "base/memory/scoped_refptr.h"
 #include "base/time/time.h"
+#include "base/trace_event/trace_event.h"
 #include "ui/gl/gl_export.h"
 
 namespace base {
@@ -83,10 +84,13 @@
 #define GPU_STARTUP_TRACE_UID(prefix) \
   GPU_STARTUP_TRACE_INTERNAL_CONCAT(prefix, __LINE__)
 
-#define GPU_STARTUP_TRACE_EVENT(name)                                     \
-  gl::StartupTrace::ScopedStage GPU_STARTUP_TRACE_UID(scoped_gpu_trace) = \
-      gl::StartupTrace::IsEnabled()                                       \
-          ? gl::StartupTrace::GetInstance()->AddStage(name)               \
-          : gl::StartupTrace::ScopedStage(0);
+#define GPU_STARTUP_TRACE_EVENT(name)                                       \
+  gl::StartupTrace::ScopedStage GPU_STARTUP_TRACE_UID(scoped_gpu_trace){0}; \
+  if (gl::StartupTrace::IsEnabled()) {                                      \
+    GPU_STARTUP_TRACE_UID(scoped_gpu_trace) =                               \
+        gl::StartupTrace::GetInstance()->AddStage(name);                    \
+  } else {                                                                  \
+    TRACE_EVENT0("gpu,startup", name);                                      \
+  }
 
 #endif  // UI_GL_STARTUP_TRACE_H_
diff --git a/ui/ozone/public/ozone_platform.cc b/ui/ozone/public/ozone_platform.cc
index 4ec3564..5c9db91 100644
--- a/ui/ozone/public/ozone_platform.cc
+++ b/ui/ozone/public/ozone_platform.cc
@@ -12,6 +12,7 @@
 #include "base/task/single_thread_task_runner.h"
 #include "base/trace_event/trace_event.h"
 #include "ui/events/devices/device_data_manager.h"
+#include "ui/gl/startup_trace.h"
 #include "ui/ozone/platform_object.h"
 #include "ui/ozone/platform_selection.h"
 #include "ui/ozone/public/platform_global_shortcut_listener.h"
@@ -89,6 +90,7 @@
 
 // static
 void OzonePlatform::InitializeForGPU(const InitParams& args) {
+  GPU_STARTUP_TRACE_EVENT("ui::OzonePlatform::InitializeForGPU");
   EnsureInstance();
   if (g_instance->initialized_gpu_)
     return;