test_gpu_service_holder.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_VIZ_TEST_TEST_GPU_SERVICE_HOLDER_H_
  5. #define COMPONENTS_VIZ_TEST_TEST_GPU_SERVICE_HOLDER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/feature_list.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/threading/thread.h"
  11. #include "build/build_config.h"
  12. #include "gpu/ipc/gpu_in_process_thread_service.h"
  13. #include "gpu/vulkan/buildflags.h"
  14. #if defined(USE_OZONE) && !BUILDFLAG(IS_FUCHSIA)
  15. #include "mojo/public/cpp/bindings/binder_map.h"
  16. #endif
  17. // START forward declarations for ScopedAllowRacyFeatureListOverrides.
  18. namespace ash {
  19. class AshScopedAllowRacyFeatureListOverrides;
  20. } // namespace ash
  21. class ChromeShelfControllerTest;
  22. class ShelfContextMenuTest;
  23. // END forward declarations for ScopedAllowRacyFeatureListOverrides.
  24. namespace gpu {
  25. class CommandBufferTaskExecutor;
  26. class SingleTaskSequence;
  27. #if BUILDFLAG(ENABLE_VULKAN)
  28. class VulkanImplementation;
  29. #endif
  30. struct GpuPreferences;
  31. } // namespace gpu
  32. namespace viz {
  33. class GpuServiceImpl;
  34. // Starts GPU Main and IO threads, and creates a GpuServiceImpl that can be used
  35. // to create a SkiaOutputSurfaceImpl. This isn't a full GPU service
  36. // implementation and should only be used in tests.
  37. class TestGpuServiceHolder : public gpu::GpuInProcessThreadServiceDelegate {
  38. public:
  39. class ScopedResetter {
  40. public:
  41. ~ScopedResetter() { TestGpuServiceHolder::ResetInstance(); }
  42. };
  43. // Don't instantiate FeatureList::ScopedDisallowOverrides when the GPU thread
  44. // is started. This shouldn't be required but there are existing tests that
  45. // initialize ScopedFeatureList after TestGpuServiceHolder.
  46. // TODO(crbug.com/1241161): Fix racy tests and remove this.
  47. class ScopedAllowRacyFeatureListOverrides {
  48. public:
  49. ~ScopedAllowRacyFeatureListOverrides();
  50. private:
  51. // Existing allowlisted failures. DO NOT ADD ANYTHING TO THIS LIST! Instead,
  52. // the test should change so the initialization of ScopedFeatureList happens
  53. // before TestGpuServiceHolder is created.
  54. friend class ::ChromeShelfControllerTest;
  55. friend class ::ShelfContextMenuTest;
  56. friend class ash::AshScopedAllowRacyFeatureListOverrides;
  57. ScopedAllowRacyFeatureListOverrides();
  58. };
  59. // Exposes a singleton to allow easy sharing of the GpuServiceImpl by
  60. // different clients (e.g. to share SharedImages via a common
  61. // SharedImageManager).
  62. //
  63. // The instance will parse GpuPreferences from the command line when it is
  64. // first created (e.g. to allow entire test suite with --use-vulkan).
  65. //
  66. // If specific feature flags or GpuPreferences are needed for a specific test,
  67. // a separate instance of this class can be created.
  68. //
  69. // By default the instance created by GetInstance() is destroyed after each
  70. // gtest completes -- it only applies to gtest because it uses gtest hooks. If
  71. // this isn't desired call DoNotResetOnTestExit() before first use.
  72. static TestGpuServiceHolder* GetInstance();
  73. // Resets the singleton instance, joining the GL thread. This is useful for
  74. // tests that individually initialize and tear down GL.
  75. static void ResetInstance();
  76. // Don't reset global instance on gtest exit. Must be called before
  77. // GetInstance().
  78. static void DoNotResetOnTestExit();
  79. explicit TestGpuServiceHolder(const gpu::GpuPreferences& preferences);
  80. TestGpuServiceHolder(const TestGpuServiceHolder&) = delete;
  81. TestGpuServiceHolder& operator=(const TestGpuServiceHolder&) = delete;
  82. ~TestGpuServiceHolder() override;
  83. scoped_refptr<base::SingleThreadTaskRunner> gpu_thread_task_runner() {
  84. return gpu_thread_.task_runner();
  85. }
  86. // Most of |gpu_service_| is not safe to use off of the GPU thread, be careful
  87. // when accessing this.
  88. GpuServiceImpl* gpu_service() { return gpu_service_.get(); }
  89. gpu::CommandBufferTaskExecutor* task_executor() {
  90. return task_executor_.get();
  91. }
  92. void ScheduleGpuTask(base::OnceClosure callback);
  93. bool is_vulkan_enabled() {
  94. #if BUILDFLAG(ENABLE_VULKAN)
  95. return !!vulkan_implementation_;
  96. #else
  97. return false;
  98. #endif
  99. }
  100. // gpu::GpuInProcessThreadServiceDelegate implementation:
  101. scoped_refptr<gpu::SharedContextState> GetSharedContextState() override;
  102. scoped_refptr<gl::GLShareGroup> GetShareGroup() override;
  103. private:
  104. void InitializeOnGpuThread(const gpu::GpuPreferences& preferences,
  105. base::WaitableEvent* completion);
  106. void DeleteOnGpuThread();
  107. // TODO(crbug.com/1267788): Fuchsia crashes. See details in the crbug.
  108. #if defined(USE_OZONE) && !BUILDFLAG(IS_FUCHSIA)
  109. void BindInterface(const std::string& interface_name,
  110. mojo::ScopedMessagePipeHandle interface_pipe);
  111. void BindInterfaceOnGpuThread(const std::string& interface_name,
  112. mojo::ScopedMessagePipeHandle interface_pipe);
  113. #endif
  114. absl::optional<base::FeatureList::ScopedDisallowOverrides>
  115. disallow_feature_overrides_;
  116. base::Thread gpu_thread_;
  117. base::Thread io_thread_;
  118. // These should only be created and deleted on the gpu thread.
  119. std::unique_ptr<GpuServiceImpl> gpu_service_;
  120. std::unique_ptr<gpu::CommandBufferTaskExecutor> task_executor_;
  121. // This is used to schedule gpu tasks in sequence.
  122. std::unique_ptr<gpu::SingleTaskSequence> gpu_task_sequence_;
  123. #if BUILDFLAG(ENABLE_VULKAN)
  124. std::unique_ptr<gpu::VulkanImplementation> vulkan_implementation_;
  125. #endif
  126. #if defined(USE_OZONE) && !BUILDFLAG(IS_FUCHSIA)
  127. // Bound interfaces.
  128. mojo::BinderMap binders_;
  129. #endif
  130. };
  131. } // namespace viz
  132. #endif // COMPONENTS_VIZ_TEST_TEST_GPU_SERVICE_HOLDER_H_