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