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 
5 #ifndef GPU_COMMAND_BUFFER_TESTS_WEBGPU_TEST_H_
6 #define GPU_COMMAND_BUFFER_TESTS_WEBGPU_TEST_H_
7 
8 #include <dawn/webgpu_cpp.h>
9 
10 #include <memory>
11 
12 #include "build/build_config.h"
13 #include "gpu/command_buffer/client/shared_memory_limits.h"
14 #include "gpu/command_buffer/common/webgpu_cmd_ids.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 
17 #if defined(OS_MAC)
18 #include "gpu/ipc/service/gpu_memory_buffer_factory_io_surface.h"
19 #endif
20 
21 namespace viz {
22 class TestGpuServiceHolder;
23 }
24 
25 namespace gpu {
26 
27 class SharedImageInterface;
28 class WebGPUInProcessContext;
29 
30 void OnRequestDeviceCallback(bool is_request_device_success,
31                              webgpu::DawnDeviceClientID device_client_id);
32 
33 namespace webgpu {
34 
35 class WebGPUCmdHelper;
36 class WebGPUDecoder;
37 class WebGPUImplementation;
38 
39 }  // namespace webgpu
40 
41 class WebGPUTest : public testing::Test {
42  protected:
43   struct Options {
44     Options();
45 
46     // Shared memory limits
47     SharedMemoryLimits shared_memory_limits =
48         SharedMemoryLimits::ForWebGPUContext();
49   };
50 
51   WebGPUTest();
52   ~WebGPUTest() override;
53 
54   bool WebGPUSupported() const;
55   bool WebGPUSharedImageSupported() const;
56   void SetUp() override;
57   void TearDown() override;
58 
59   void Initialize(const Options& options);
60 
61   webgpu::WebGPUImplementation* webgpu() const;
62   webgpu::WebGPUCmdHelper* webgpu_cmds() const;
63   SharedImageInterface* GetSharedImageInterface() const;
64   webgpu::WebGPUDecoder* GetDecoder() const;
65 
66   void RunPendingTasks();
67   void WaitForCompletion(wgpu::Device device);
68 
69   struct DeviceAndClientID {
70     wgpu::Device device;
71     webgpu::DawnDeviceClientID client_id;
72   };
73   DeviceAndClientID GetNewDeviceAndClientID();
74 
GetGpuServiceHolder()75   viz::TestGpuServiceHolder* GetGpuServiceHolder() {
76     return gpu_service_holder_.get();
77   }
78 
79   const uint32_t kAdapterServiceID = 0u;
80 
81  private:
82   std::unique_ptr<viz::TestGpuServiceHolder> gpu_service_holder_;
83   std::unique_ptr<WebGPUInProcessContext> context_;
84   std::unique_ptr<webgpu::WebGPUCmdHelper> cmd_helper_;
85 #if defined(OS_MAC)
86   // SharedImages on macOS require a valid image factory.
87   GpuMemoryBufferFactoryIOSurface image_factory_;
88 #endif
89 
90   webgpu::DawnDeviceClientID next_device_client_id_ = 1;
91 };
92 
93 }  // namespace gpu
94 
95 #endif  // GPU_COMMAND_BUFFER_TESTS_WEBGPU_TEST_H_
96