test_context_provider.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 2013 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_CONTEXT_PROVIDER_H_
  5. #define COMPONENTS_VIZ_TEST_TEST_CONTEXT_PROVIDER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/callback.h"
  12. #include "base/containers/flat_set.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/observer_list.h"
  15. #include "base/synchronization/lock.h"
  16. #include "base/threading/thread_checker.h"
  17. #include "build/build_config.h"
  18. #include "components/viz/common/gpu/context_provider.h"
  19. #include "components/viz/common/gpu/raster_context_provider.h"
  20. #include "components/viz/test/test_context_support.h"
  21. #include "gpu/command_buffer/client/gles2_interface_stub.h"
  22. #include "gpu/command_buffer/client/shared_image_interface.h"
  23. #include "gpu/config/gpu_feature_info.h"
  24. #include "testing/gmock/include/gmock/gmock.h"
  25. #include "third_party/skia/include/core/SkRefCnt.h"
  26. namespace skia_bindings {
  27. class GrContextForGLES2Interface;
  28. }
  29. namespace viz {
  30. class TestGLES2Interface;
  31. class TestRasterInterface;
  32. class TestSharedImageInterface : public gpu::SharedImageInterface {
  33. public:
  34. TestSharedImageInterface();
  35. ~TestSharedImageInterface() override;
  36. gpu::Mailbox CreateSharedImage(ResourceFormat format,
  37. const gfx::Size& size,
  38. const gfx::ColorSpace& color_space,
  39. GrSurfaceOrigin surface_origin,
  40. SkAlphaType alpha_type,
  41. uint32_t usage,
  42. gpu::SurfaceHandle surface_handle) override;
  43. gpu::Mailbox CreateSharedImage(ResourceFormat format,
  44. const gfx::Size& size,
  45. const gfx::ColorSpace& color_space,
  46. GrSurfaceOrigin surface_origin,
  47. SkAlphaType alpha_type,
  48. uint32_t usage,
  49. base::span<const uint8_t> pixel_data) override;
  50. gpu::Mailbox CreateSharedImage(
  51. gfx::GpuMemoryBuffer* gpu_memory_buffer,
  52. gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
  53. gfx::BufferPlane plane,
  54. const gfx::ColorSpace& color_space,
  55. GrSurfaceOrigin surface_origin,
  56. SkAlphaType alpha_type,
  57. uint32_t usage) override;
  58. std::vector<gpu::Mailbox> CreateSharedImageVideoPlanes(
  59. gfx::GpuMemoryBuffer* gpu_memory_buffer,
  60. gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
  61. uint32_t usage) override;
  62. void UpdateSharedImage(const gpu::SyncToken& sync_token,
  63. const gpu::Mailbox& mailbox) override;
  64. void UpdateSharedImage(const gpu::SyncToken& sync_token,
  65. std::unique_ptr<gfx::GpuFence> acquire_fence,
  66. const gpu::Mailbox& mailbox) override;
  67. void DestroySharedImage(const gpu::SyncToken& sync_token,
  68. const gpu::Mailbox& mailbox) override;
  69. SwapChainMailboxes CreateSwapChain(ResourceFormat format,
  70. const gfx::Size& size,
  71. const gfx::ColorSpace& color_space,
  72. GrSurfaceOrigin surface_origin,
  73. SkAlphaType alpha_type,
  74. uint32_t usage) override;
  75. void PresentSwapChain(const gpu::SyncToken& sync_token,
  76. const gpu::Mailbox& mailbox) override;
  77. #if BUILDFLAG(IS_FUCHSIA)
  78. void RegisterSysmemBufferCollection(gfx::SysmemBufferCollectionId id,
  79. zx::channel token,
  80. gfx::BufferFormat format,
  81. gfx::BufferUsage usage,
  82. bool register_with_image_pipe) override;
  83. void ReleaseSysmemBufferCollection(gfx::SysmemBufferCollectionId id) override;
  84. #endif // BUILDFLAG(IS_FUCHSIA)
  85. gpu::SyncToken GenVerifiedSyncToken() override;
  86. gpu::SyncToken GenUnverifiedSyncToken() override;
  87. void WaitSyncToken(const gpu::SyncToken& sync_token) override;
  88. void Flush() override;
  89. scoped_refptr<gfx::NativePixmap> GetNativePixmap(
  90. const gpu::Mailbox& mailbox) override;
  91. size_t shared_image_count() const { return shared_images_.size(); }
  92. const gfx::Size& MostRecentSize() const { return most_recent_size_; }
  93. const gpu::SyncToken& MostRecentGeneratedToken() const {
  94. return most_recent_generated_token_;
  95. }
  96. const gpu::SyncToken& MostRecentDestroyToken() const {
  97. return most_recent_destroy_token_;
  98. }
  99. bool CheckSharedImageExists(const gpu::Mailbox& mailbox) const;
  100. private:
  101. mutable base::Lock lock_;
  102. uint64_t release_id_ = 0;
  103. gfx::Size most_recent_size_;
  104. gpu::SyncToken most_recent_generated_token_;
  105. gpu::SyncToken most_recent_destroy_token_;
  106. base::flat_set<gpu::Mailbox> shared_images_;
  107. };
  108. class TestContextProvider
  109. : public base::RefCountedThreadSafe<TestContextProvider>,
  110. public ContextProvider,
  111. public RasterContextProvider {
  112. public:
  113. static scoped_refptr<TestContextProvider> Create(
  114. std::string additional_extensions = std::string());
  115. // Creates a worker context provider that can be used on any thread. This is
  116. // equivalent to: Create(); BindToCurrentThread().
  117. static scoped_refptr<TestContextProvider> CreateWorker();
  118. static scoped_refptr<TestContextProvider> CreateWorker(
  119. std::unique_ptr<TestContextSupport> support);
  120. static scoped_refptr<TestContextProvider> Create(
  121. std::unique_ptr<TestGLES2Interface> gl);
  122. static scoped_refptr<TestContextProvider> Create(
  123. std::unique_ptr<TestSharedImageInterface> sii);
  124. static scoped_refptr<TestContextProvider> Create(
  125. std::unique_ptr<TestContextSupport> support);
  126. explicit TestContextProvider(std::unique_ptr<TestContextSupport> support,
  127. std::unique_ptr<TestRasterInterface> raster,
  128. bool support_locking);
  129. explicit TestContextProvider(
  130. std::unique_ptr<TestContextSupport> support,
  131. std::unique_ptr<TestGLES2Interface> gl,
  132. std::unique_ptr<gpu::raster::RasterInterface> raster,
  133. std::unique_ptr<TestSharedImageInterface> sii,
  134. bool support_locking);
  135. TestContextProvider(const TestContextProvider&) = delete;
  136. TestContextProvider& operator=(const TestContextProvider&) = delete;
  137. // ContextProvider / RasterContextProvider implementation.
  138. void AddRef() const override;
  139. void Release() const override;
  140. gpu::ContextResult BindToCurrentThread() override;
  141. const gpu::Capabilities& ContextCapabilities() const override;
  142. const gpu::GpuFeatureInfo& GetGpuFeatureInfo() const override;
  143. gpu::gles2::GLES2Interface* ContextGL() override;
  144. gpu::raster::RasterInterface* RasterInterface() override;
  145. gpu::ContextSupport* ContextSupport() override;
  146. class GrDirectContext* GrContext() override;
  147. TestSharedImageInterface* SharedImageInterface() override;
  148. ContextCacheController* CacheController() override;
  149. base::Lock* GetLock() override;
  150. void AddObserver(ContextLostObserver* obs) override;
  151. void RemoveObserver(ContextLostObserver* obs) override;
  152. TestGLES2Interface* TestContextGL();
  153. TestRasterInterface* GetTestRasterInterface();
  154. // This returns the TestGLES2Interface but is valid to call
  155. // before the context is bound to a thread. This is needed to set up
  156. // state on the test interface before binding.
  157. TestGLES2Interface* UnboundTestContextGL() { return context_gl_.get(); }
  158. TestRasterInterface* UnboundTestRasterInterface();
  159. TestContextSupport* support() { return support_.get(); }
  160. gpu::GpuFeatureInfo& GetWritableGpuFeatureInfo() { return gpu_feature_info_; }
  161. protected:
  162. friend class base::RefCountedThreadSafe<TestContextProvider>;
  163. ~TestContextProvider() override;
  164. private:
  165. void OnLostContext();
  166. void CheckValidThreadOrLockAcquired() const {
  167. #if DCHECK_IS_ON()
  168. if (support_locking_) {
  169. context_lock_.AssertAcquired();
  170. } else {
  171. DCHECK(context_thread_checker_.CalledOnValidThread());
  172. }
  173. #endif
  174. }
  175. std::unique_ptr<TestContextSupport> support_;
  176. // Used for GLES2 contexts.
  177. std::unique_ptr<TestGLES2Interface> context_gl_;
  178. std::unique_ptr<gpu::raster::RasterInterface> raster_interface_gles_;
  179. std::unique_ptr<skia_bindings::GrContextForGLES2Interface> gr_context_;
  180. // Used for raster contexts.
  181. std::unique_ptr<TestRasterInterface> raster_context_;
  182. std::unique_ptr<ContextCacheController> cache_controller_;
  183. std::unique_ptr<TestSharedImageInterface> shared_image_interface_;
  184. [[maybe_unused]] const bool support_locking_;
  185. bool bound_ = false;
  186. gpu::GpuFeatureInfo gpu_feature_info_;
  187. base::ThreadChecker main_thread_checker_;
  188. base::ThreadChecker context_thread_checker_;
  189. base::Lock context_lock_;
  190. base::ObserverList<ContextLostObserver>::Unchecked observers_;
  191. base::WeakPtrFactory<TestContextProvider> weak_ptr_factory_{this};
  192. };
  193. } // namespace viz
  194. #endif // COMPONENTS_VIZ_TEST_TEST_CONTEXT_PROVIDER_H_