webgpu_command_buffer_stub.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (c) 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. #include "gpu/ipc/service/webgpu_command_buffer_stub.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/memory/unsafe_shared_memory_region.h"
  8. #include "base/trace_event/trace_event.h"
  9. #include "build/build_config.h"
  10. #include "gpu/command_buffer/common/constants.h"
  11. #include "gpu/command_buffer/common/gpu_memory_buffer_support.h"
  12. #include "gpu/command_buffer/common/mailbox.h"
  13. #include "gpu/command_buffer/service/gl_context_virtual.h"
  14. #include "gpu/command_buffer/service/gl_state_restorer_impl.h"
  15. #include "gpu/command_buffer/service/logger.h"
  16. #include "gpu/command_buffer/service/mailbox_manager.h"
  17. #include "gpu/command_buffer/service/memory_tracking.h"
  18. #include "gpu/command_buffer/service/service_utils.h"
  19. #include "gpu/command_buffer/service/sync_point_manager.h"
  20. #include "gpu/command_buffer/service/transfer_buffer_manager.h"
  21. #include "gpu/command_buffer/service/webgpu_decoder.h"
  22. #include "gpu/config/gpu_crash_keys.h"
  23. #include "gpu/ipc/service/gpu_channel.h"
  24. #include "gpu/ipc/service/gpu_channel_manager.h"
  25. #include "gpu/ipc/service/gpu_channel_manager_delegate.h"
  26. #include "gpu/ipc/service/gpu_memory_buffer_factory.h"
  27. #include "gpu/ipc/service/gpu_watchdog_thread.h"
  28. #include "ui/gl/gl_bindings.h"
  29. #include "ui/gl/gl_context.h"
  30. #include "ui/gl/gl_implementation.h"
  31. #include "ui/gl/gl_switches.h"
  32. #include "ui/gl/gl_workarounds.h"
  33. #include "ui/gl/init/gl_factory.h"
  34. #if BUILDFLAG(IS_WIN)
  35. #include "base/win/win_util.h"
  36. #endif
  37. #if BUILDFLAG(IS_ANDROID)
  38. #include "gpu/ipc/service/stream_texture_android.h"
  39. #endif
  40. namespace gpu {
  41. WebGPUCommandBufferStub::WebGPUCommandBufferStub(
  42. GpuChannel* channel,
  43. const mojom::CreateCommandBufferParams& init_params,
  44. CommandBufferId command_buffer_id,
  45. SequenceId sequence_id,
  46. int32_t stream_id,
  47. int32_t route_id)
  48. : CommandBufferStub(channel,
  49. init_params,
  50. command_buffer_id,
  51. sequence_id,
  52. stream_id,
  53. route_id) {}
  54. WebGPUCommandBufferStub::~WebGPUCommandBufferStub() {
  55. // Must run before memory_tracker_ is destroyed.
  56. decoder_context()->Destroy(false);
  57. memory_tracker_ = nullptr;
  58. }
  59. gpu::ContextResult WebGPUCommandBufferStub::Initialize(
  60. CommandBufferStub* share_command_buffer_stub,
  61. const mojom::CreateCommandBufferParams& init_params,
  62. base::UnsafeSharedMemoryRegion shared_state_shm) {
  63. #if BUILDFLAG(IS_FUCHSIA)
  64. // TODO(crbug.com/707031): Implement this.
  65. NOTIMPLEMENTED();
  66. LOG(ERROR) << "ContextResult::kFatalFailure: no fuchsia support";
  67. return gpu::ContextResult::kFatalFailure;
  68. #else
  69. TRACE_EVENT0("gpu", "WebGPUBufferStub::Initialize");
  70. UpdateActiveUrl();
  71. GpuChannelManager* manager = channel_->gpu_channel_manager();
  72. DCHECK(manager);
  73. if (share_command_buffer_stub) {
  74. LOG(ERROR) << "Using a share group is not supported with WebGPUDecoder";
  75. return ContextResult::kFatalFailure;
  76. }
  77. if (surface_handle_ != kNullSurfaceHandle) {
  78. LOG(ERROR) << "ContextResult::kFatalFailure: "
  79. "WebGPUInterface clients must render offscreen.";
  80. return ContextResult::kFatalFailure;
  81. }
  82. if (init_params.attribs.context_type != CONTEXT_TYPE_WEBGPU) {
  83. LOG(ERROR) << "ContextResult::kFatalFailure: Incompatible creation attribs "
  84. "used with WebGPUDecoder";
  85. return ContextResult::kFatalFailure;
  86. }
  87. ContextResult result;
  88. scoped_refptr<SharedContextState> shared_context_state =
  89. manager->GetSharedContextState(&result);
  90. if (!shared_context_state) {
  91. LOG(ERROR) << "ContextResult::kFatalFailure: "
  92. "Failed to create WebGPU decoder state.";
  93. DCHECK_NE(result, gpu::ContextResult::kSuccess);
  94. return result;
  95. }
  96. share_group_ = manager->share_group();
  97. use_virtualized_gl_context_ = false;
  98. memory_tracker_ = CreateMemoryTracker();
  99. command_buffer_ =
  100. std::make_unique<CommandBufferService>(this, memory_tracker_.get());
  101. std::unique_ptr<webgpu::WebGPUDecoder> decoder(webgpu::WebGPUDecoder::Create(
  102. this, command_buffer_.get(), manager->shared_image_manager(),
  103. memory_tracker_.get(), manager->outputter(), manager->gpu_preferences(),
  104. std::move(shared_context_state)));
  105. sync_point_client_state_ =
  106. channel_->sync_point_manager()->CreateSyncPointClientState(
  107. CommandBufferNamespace::GPU_IO, command_buffer_id_, sequence_id_);
  108. result = decoder->Initialize(manager->gpu_feature_info());
  109. if (result != gpu::ContextResult::kSuccess) {
  110. DLOG(ERROR) << "Failed to initialize decoder.";
  111. return result;
  112. }
  113. if (manager->gpu_preferences().enable_gpu_service_logging) {
  114. decoder->SetLogCommands(true);
  115. }
  116. set_decoder_context(std::move(decoder));
  117. const size_t kSharedStateSize = sizeof(CommandBufferSharedState);
  118. base::WritableSharedMemoryMapping shared_state_mapping =
  119. shared_state_shm.MapAt(0, kSharedStateSize);
  120. if (!shared_state_mapping.IsValid()) {
  121. LOG(ERROR) << "ContextResult::kFatalFailure: "
  122. "Failed to map shared state buffer.";
  123. return gpu::ContextResult::kFatalFailure;
  124. }
  125. command_buffer_->SetSharedStateBuffer(MakeBackingFromSharedMemory(
  126. std::move(shared_state_shm), std::move(shared_state_mapping)));
  127. if (!active_url_.is_empty())
  128. manager->delegate()->DidCreateOffscreenContext(active_url_.url());
  129. manager->delegate()->DidCreateContextSuccessfully();
  130. initialized_ = true;
  131. return gpu::ContextResult::kSuccess;
  132. #endif // BUILDFLAG(IS_FUCHSIA)
  133. }
  134. MemoryTracker* WebGPUCommandBufferStub::GetContextGroupMemoryTracker() const {
  135. return nullptr;
  136. }
  137. void WebGPUCommandBufferStub::OnSwapBuffers(uint64_t swap_id, uint32_t flags) {}
  138. } // namespace gpu