1 // Copyright 2018 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 #include "gpu/ipc/webgpu_in_process_context.h"
6
7 #include <utility>
8
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/test/test_simple_task_runner.h"
12 #include "gpu/command_buffer/client/shared_memory_limits.h"
13 #include "gpu/command_buffer/client/transfer_buffer.h"
14 #include "gpu/command_buffer/client/webgpu_cmd_helper.h"
15 #include "gpu/command_buffer/client/webgpu_implementation.h"
16 #include "gpu/command_buffer/common/command_buffer.h"
17 #include "gpu/command_buffer/common/constants.h"
18 #include "gpu/command_buffer/common/context_creation_attribs.h"
19 #include "gpu/config/gpu_feature_info.h"
20 #include "gpu/config/gpu_switches.h"
21 #include "gpu/ipc/common/surface_handle.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace gpu {
25
26 WebGPUInProcessContext::WebGPUInProcessContext() = default;
27
~WebGPUInProcessContext()28 WebGPUInProcessContext::~WebGPUInProcessContext() {
29 // Trigger any pending lost contexts. First do a full sync between client
30 // and service threads. Then execute any pending tasks.
31 if (webgpu_implementation_) {
32 // TODO(crbug.com/868192): do the equivalent of a glFinish here?
33 client_task_runner_->RunUntilIdle();
34 webgpu_implementation_.reset();
35 }
36 transfer_buffer_.reset();
37 helper_.reset();
38 command_buffer_.reset();
39 }
40
Initialize(CommandBufferTaskExecutor * task_executor,const ContextCreationAttribs & attribs,const SharedMemoryLimits & memory_limits,GpuMemoryBufferManager * gpu_memory_buffer_manager,ImageFactory * image_factory,GpuChannelManagerDelegate * gpu_channel_manager_delegate)41 ContextResult WebGPUInProcessContext::Initialize(
42 CommandBufferTaskExecutor* task_executor,
43 const ContextCreationAttribs& attribs,
44 const SharedMemoryLimits& memory_limits,
45 GpuMemoryBufferManager* gpu_memory_buffer_manager,
46 ImageFactory* image_factory,
47 GpuChannelManagerDelegate* gpu_channel_manager_delegate) {
48 DCHECK(attribs.context_type == CONTEXT_TYPE_WEBGPU);
49
50 if (attribs.context_type != CONTEXT_TYPE_WEBGPU ||
51 attribs.enable_raster_interface || attribs.enable_gles2_interface) {
52 return ContextResult::kFatalFailure;
53 }
54
55 client_task_runner_ = base::MakeRefCounted<base::TestSimpleTaskRunner>();
56 command_buffer_ =
57 std::make_unique<InProcessCommandBuffer>(task_executor, GURL());
58
59 static const scoped_refptr<gl::GLSurface> surface = nullptr;
60 static constexpr bool is_offscreen = true;
61 auto result = command_buffer_->Initialize(
62 surface, is_offscreen, kNullSurfaceHandle, attribs,
63 gpu_memory_buffer_manager, image_factory, gpu_channel_manager_delegate,
64 client_task_runner_, nullptr /* task_sequence */,
65 nullptr /* display_compositor_memory_and_task_controller_on_gpu */,
66 nullptr, nullptr);
67 if (result != ContextResult::kSuccess) {
68 DLOG(ERROR) << "Failed to initialize InProcessCommmandBuffer";
69 return result;
70 }
71
72 // Check for consistency.
73 DCHECK(!attribs.bind_generates_resource);
74
75 // Create the WebGPUCmdHelper, which writes the command buffer protocol.
76 auto webgpu_helper =
77 std::make_unique<webgpu::WebGPUCmdHelper>(command_buffer_.get());
78 result = webgpu_helper->Initialize(memory_limits.command_buffer_size);
79 if (result != ContextResult::kSuccess) {
80 LOG(ERROR) << "Failed to initialize WebGPUCmdHelper";
81 return result;
82 }
83 transfer_buffer_ = std::make_unique<TransferBuffer>(webgpu_helper.get());
84
85 webgpu_implementation_ = std::make_unique<webgpu::WebGPUImplementation>(
86 webgpu_helper.get(), transfer_buffer_.get(), command_buffer_.get());
87 helper_ = std::move(webgpu_helper);
88 webgpu_implementation_->Initialize(memory_limits);
89 return result;
90 }
91
GetCapabilities() const92 const Capabilities& WebGPUInProcessContext::GetCapabilities() const {
93 return command_buffer_->GetCapabilities();
94 }
95
GetGpuFeatureInfo() const96 const GpuFeatureInfo& WebGPUInProcessContext::GetGpuFeatureInfo() const {
97 return command_buffer_->GetGpuFeatureInfo();
98 }
99
GetImplementation()100 webgpu::WebGPUImplementation* WebGPUInProcessContext::GetImplementation() {
101 return webgpu_implementation_.get();
102 }
103
GetTaskRunner()104 base::TestSimpleTaskRunner* WebGPUInProcessContext::GetTaskRunner() {
105 return client_task_runner_.get();
106 }
107
GetTransferCacheForTest() const108 ServiceTransferCache* WebGPUInProcessContext::GetTransferCacheForTest() const {
109 return command_buffer_->GetTransferCacheForTest();
110 }
111
GetCommandBufferForTest() const112 InProcessCommandBuffer* WebGPUInProcessContext::GetCommandBufferForTest()
113 const {
114 return command_buffer_.get();
115 }
116
117 } // namespace gpu
118