| // Copyright 2026 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "gpu/webgpu/dawn_testing_command_serializer.h" | |
| #include <optional> | |
| #include <utility> | |
| #include "base/containers/span.h" | |
| namespace gpu::webgpu { | |
| DawnTestingCommandSerializer::DawnTestingCommandSerializer() = default; | |
| DawnTestingCommandSerializer::~DawnTestingCommandSerializer() = default; | |
| void DawnTestingCommandSerializer::SetHandler( | |
| dawn::wire::CommandHandler* handler) { | |
| handler_ = handler; | |
| } | |
| size_t DawnTestingCommandSerializer::GetMaximumAllocationSize() const { | |
| return buffer_.size(); | |
| } | |
| DawnTestingCommandSerializer::CommandSpace | |
| DawnTestingCommandSerializer::GetCommandSpace(size_t size) { | |
| if (size > buffer_.size()) { | |
| return std::nullopt; | |
| } | |
| if (buffer_.size() - size < offset_) { | |
| if (!Flush()) { | |
| return std::nullopt; | |
| } | |
| return GetCommandSpace(size); | |
| } | |
| base::span<std::byte> result = base::span(buffer_).subspan(offset_, size); | |
| offset_ += size; | |
| return CommandSpace(std::in_place, result.data(), result.size()); | |
| } | |
| bool DawnTestingCommandSerializer::Flush() { | |
| if (!handler_) { | |
| return false; | |
| } | |
| if (last_flushed_offset_ == offset_) { | |
| return true; | |
| } | |
| base::span<const std::byte> flush_range = base::span(buffer_).subspan( | |
| last_flushed_offset_, offset_ - last_flushed_offset_); | |
| last_flushed_offset_ = offset_; | |
| bool success = | |
| handler_->HandleCommands({flush_range.data(), flush_range.size()}); | |
| if (last_flushed_offset_ == offset_) { | |
| offset_ = 0; | |
| last_flushed_offset_ = 0; | |
| } | |
| return success; | |
| } | |
| } // namespace gpu::webgpu |