| // Copyright (c) 2018 The Chromium Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "gpu/command_buffer/service/webgpu_decoder.h" | |
| #include "base/macros.h" | |
| #include "gpu/command_buffer/common/webgpu_cmd_format.h" | |
| #include "gpu/command_buffer/common/webgpu_cmd_ids.h" | |
| #include "gpu/command_buffer/service/command_buffer_service.h" | |
| namespace gpu { | |
| namespace webgpu { | |
| class WebGPUDecoderImpl final : public WebGPUDecoder { | |
| public: | |
| WebGPUDecoderImpl(DecoderClient* client, | |
| CommandBufferServiceBase* command_buffer_service, | |
| gles2::Outputter* outputter); | |
| ~WebGPUDecoderImpl() override; | |
| // DecoderContext implementation. | |
| base::WeakPtr<DecoderContext> AsWeakPtr() override { | |
| NOTIMPLEMENTED(); | |
| return nullptr; | |
| } | |
| ContextResult Initialize( | |
| const scoped_refptr<gl::GLSurface>& surface, | |
| const scoped_refptr<gl::GLContext>& context, | |
| bool offscreen, | |
| const gles2::DisallowedFeatures& disallowed_features, | |
| const ContextCreationAttribs& attrib_helper) override { | |
| return ContextResult::kSuccess; | |
| } | |
| const gles2::ContextState* GetContextState() override { | |
| NOTREACHED(); | |
| return nullptr; | |
| } | |
| void Destroy(bool have_context) override {} | |
| bool MakeCurrent() override { return true; } | |
| gl::GLContext* GetGLContext() override { return nullptr; } | |
| gl::GLSurface* GetGLSurface() override { | |
| NOTREACHED(); | |
| return nullptr; | |
| } | |
| const gles2::FeatureInfo* GetFeatureInfo() const override { | |
| NOTREACHED(); | |
| return nullptr; | |
| } | |
| Capabilities GetCapabilities() override { return {}; } | |
| void RestoreGlobalState() const override { NOTREACHED(); } | |
| void ClearAllAttributes() const override { NOTREACHED(); } | |
| void RestoreAllAttributes() const override { NOTREACHED(); } | |
| void RestoreState(const gles2::ContextState* prev_state) override { | |
| NOTREACHED(); | |
| } | |
| void RestoreActiveTexture() const override { NOTREACHED(); } | |
| void RestoreAllTextureUnitAndSamplerBindings( | |
| const gles2::ContextState* prev_state) const override { | |
| NOTREACHED(); | |
| } | |
| void RestoreActiveTextureUnitBinding(unsigned int target) const override { | |
| NOTREACHED(); | |
| } | |
| void RestoreBufferBinding(unsigned int target) override { NOTREACHED(); } | |
| void RestoreBufferBindings() const override { NOTREACHED(); } | |
| void RestoreFramebufferBindings() const override { NOTREACHED(); } | |
| void RestoreRenderbufferBindings() override { NOTREACHED(); } | |
| void RestoreProgramBindings() const override { NOTREACHED(); } | |
| void RestoreTextureState(unsigned service_id) const override { NOTREACHED(); } | |
| void RestoreTextureUnitBindings(unsigned unit) const override { | |
| NOTREACHED(); | |
| } | |
| void RestoreVertexAttribArray(unsigned index) override { NOTREACHED(); } | |
| void RestoreAllExternalTextureBindingsIfNeeded() override { NOTREACHED(); } | |
| QueryManager* GetQueryManager() override { | |
| NOTREACHED(); | |
| return nullptr; | |
| } | |
| void SetQueryCallback(unsigned int query_client_id, | |
| base::OnceClosure callback) override { | |
| NOTREACHED(); | |
| } | |
| gles2::GpuFenceManager* GetGpuFenceManager() override { | |
| NOTREACHED(); | |
| return nullptr; | |
| } | |
| bool HasPendingQueries() const override { return false; } | |
| void ProcessPendingQueries(bool did_finish) override {} | |
| bool HasMoreIdleWork() const override { return false; } | |
| void PerformIdleWork() override { NOTREACHED(); } | |
| bool HasPollingWork() const override { return false; } | |
| void PerformPollingWork() override { NOTREACHED(); } | |
| TextureBase* GetTextureBase(uint32_t client_id) override { | |
| NOTREACHED(); | |
| return nullptr; | |
| } | |
| void SetLevelInfo(uint32_t client_id, | |
| int level, | |
| unsigned internal_format, | |
| unsigned width, | |
| unsigned height, | |
| unsigned depth, | |
| unsigned format, | |
| unsigned type, | |
| const gfx::Rect& cleared_rect) override { | |
| NOTREACHED(); | |
| } | |
| bool WasContextLost() const override { | |
| NOTIMPLEMENTED(); | |
| return false; | |
| } | |
| bool WasContextLostByRobustnessExtension() const override { | |
| NOTREACHED(); | |
| return false; | |
| } | |
| void MarkContextLost(error::ContextLostReason reason) override { | |
| NOTIMPLEMENTED(); | |
| } | |
| bool CheckResetStatus() override { | |
| NOTREACHED(); | |
| return false; | |
| } | |
| void BeginDecoding() override {} | |
| void EndDecoding() override {} | |
| const char* GetCommandName(unsigned int command_id) const; | |
| error::Error DoCommands(unsigned int num_commands, | |
| const volatile void* buffer, | |
| int num_entries, | |
| int* entries_processed) override; | |
| base::StringPiece GetLogPrefix() override { | |
| NOTIMPLEMENTED(); | |
| return ""; | |
| } | |
| void BindImage(uint32_t client_texture_id, | |
| uint32_t texture_target, | |
| gl::GLImage* image, | |
| bool can_bind_to_sampler) override { | |
| NOTREACHED(); | |
| } | |
| gles2::ContextGroup* GetContextGroup() override { | |
| NOTREACHED(); | |
| return nullptr; | |
| } | |
| gles2::ErrorState* GetErrorState() override { | |
| NOTREACHED(); | |
| return nullptr; | |
| } | |
| std::unique_ptr<gles2::AbstractTexture> CreateAbstractTexture( | |
| GLenum target, | |
| GLenum internal_format, | |
| GLsizei width, | |
| GLsizei height, | |
| GLsizei depth, | |
| GLint border, | |
| GLenum format, | |
| GLenum type) override { | |
| NOTREACHED(); | |
| return nullptr; | |
| } | |
| bool IsCompressedTextureFormat(unsigned format) override { | |
| NOTREACHED(); | |
| return false; | |
| } | |
| bool ClearLevel(gles2::Texture* texture, | |
| unsigned target, | |
| int level, | |
| unsigned format, | |
| unsigned type, | |
| int xoffset, | |
| int yoffset, | |
| int width, | |
| int height) override { | |
| NOTREACHED(); | |
| return false; | |
| } | |
| bool ClearCompressedTextureLevel(gles2::Texture* texture, | |
| unsigned target, | |
| int level, | |
| unsigned format, | |
| int width, | |
| int height) override { | |
| NOTREACHED(); | |
| return false; | |
| } | |
| bool ClearLevel3D(gles2::Texture* texture, | |
| unsigned target, | |
| int level, | |
| unsigned format, | |
| unsigned type, | |
| int width, | |
| int height, | |
| int depth) override { | |
| NOTREACHED(); | |
| return false; | |
| } | |
| bool initialized() const override { return true; } | |
| void SetLogCommands(bool log_commands) override { NOTIMPLEMENTED(); } | |
| gles2::Outputter* outputter() const override { | |
| NOTIMPLEMENTED(); | |
| return nullptr; | |
| } | |
| private: | |
| typedef error::Error (WebGPUDecoderImpl::*CmdHandler)( | |
| uint32_t immediate_data_size, | |
| const volatile void* data); | |
| // A struct to hold info about each command. | |
| struct CommandInfo { | |
| CmdHandler cmd_handler; | |
| uint8_t arg_flags; // How to handle the arguments for this command | |
| uint8_t cmd_flags; // How to handle this command | |
| uint16_t arg_count; // How many arguments are expected for this command. | |
| }; | |
| // A table of CommandInfo for all the commands. | |
| static const CommandInfo command_info[kNumCommands - kFirstWebGPUCommand]; | |
| // Generate a member function prototype for each command in an automated and | |
| // typesafe way. | |
| #define WEBGPU_CMD_OP(name) \ | |
| Error Handle##name(uint32_t immediate_data_size, const volatile void* data); | |
| WEBGPU_COMMAND_LIST(WEBGPU_CMD_OP) | |
| #undef WEBGPU_CMD_OP | |
| // The current decoder error communicates the decoder error through command | |
| // processing functions that do not return the error value. Should be set | |
| // only if not returning an error. | |
| error::Error current_decoder_error_ = error::kNoError; | |
| DISALLOW_COPY_AND_ASSIGN(WebGPUDecoderImpl); | |
| }; | |
| constexpr WebGPUDecoderImpl::CommandInfo WebGPUDecoderImpl::command_info[] = { | |
| #define WEBGPU_CMD_OP(name) \ | |
| { \ | |
| &WebGPUDecoderImpl::Handle##name, cmds::name::kArgFlags, \ | |
| cmds::name::cmd_flags, \ | |
| sizeof(cmds::name) / sizeof(CommandBufferEntry) - 1, \ | |
| }, /* NOLINT */ | |
| WEBGPU_COMMAND_LIST(WEBGPU_CMD_OP) | |
| #undef WEBGPU_CMD_OP | |
| }; | |
| // static | |
| WebGPUDecoder* WebGPUDecoder::Create( | |
| DecoderClient* client, | |
| CommandBufferServiceBase* command_buffer_service, | |
| gles2::Outputter* outputter) { | |
| return new WebGPUDecoderImpl(client, command_buffer_service, outputter); | |
| } | |
| WebGPUDecoder::WebGPUDecoder(DecoderClient* client, | |
| CommandBufferServiceBase* command_buffer_service, | |
| gles2::Outputter* outputter) | |
| : CommonDecoder(command_buffer_service) {} | |
| WebGPUDecoder::~WebGPUDecoder() {} | |
| WebGPUDecoderImpl::WebGPUDecoderImpl( | |
| DecoderClient* client, | |
| CommandBufferServiceBase* command_buffer_service, | |
| gles2::Outputter* outputter) | |
| : WebGPUDecoder(client, command_buffer_service, outputter) {} | |
| WebGPUDecoderImpl::~WebGPUDecoderImpl() {} | |
| const char* WebGPUDecoderImpl::GetCommandName(unsigned int command_id) const { | |
| if (command_id >= kFirstWebGPUCommand && command_id < kNumCommands) { | |
| return webgpu::GetCommandName(static_cast<CommandId>(command_id)); | |
| } | |
| return GetCommonCommandName(static_cast<cmd::CommandId>(command_id)); | |
| } | |
| error::Error WebGPUDecoderImpl::DoCommands(unsigned int num_commands, | |
| const volatile void* buffer, | |
| int num_entries, | |
| int* entries_processed) { | |
| DCHECK(entries_processed); | |
| int commands_to_process = num_commands; | |
| error::Error result = error::kNoError; | |
| const volatile CommandBufferEntry* cmd_data = | |
| static_cast<const volatile CommandBufferEntry*>(buffer); | |
| int process_pos = 0; | |
| CommandId command = static_cast<CommandId>(0); | |
| while (process_pos < num_entries && result == error::kNoError && | |
| commands_to_process--) { | |
| const unsigned int size = cmd_data->value_header.size; | |
| command = static_cast<CommandId>(cmd_data->value_header.command); | |
| if (size == 0) { | |
| result = error::kInvalidSize; | |
| break; | |
| } | |
| if (static_cast<int>(size) + process_pos > num_entries) { | |
| result = error::kOutOfBounds; | |
| break; | |
| } | |
| const unsigned int arg_count = size - 1; | |
| unsigned int command_index = command - kFirstWebGPUCommand; | |
| if (command_index < base::size(command_info)) { | |
| const CommandInfo& info = command_info[command_index]; | |
| unsigned int info_arg_count = static_cast<unsigned int>(info.arg_count); | |
| if ((info.arg_flags == cmd::kFixed && arg_count == info_arg_count) || | |
| (info.arg_flags == cmd::kAtLeastN && arg_count >= info_arg_count)) { | |
| uint32_t immediate_data_size = (arg_count - info_arg_count) * | |
| sizeof(CommandBufferEntry); // NOLINT | |
| result = (this->*info.cmd_handler)(immediate_data_size, cmd_data); | |
| } else { | |
| result = error::kInvalidArguments; | |
| } | |
| } else { | |
| result = DoCommonCommand(command, arg_count, cmd_data); | |
| } | |
| if (result == error::kNoError && | |
| current_decoder_error_ != error::kNoError) { | |
| result = current_decoder_error_; | |
| current_decoder_error_ = error::kNoError; | |
| } | |
| if (result != error::kDeferCommandUntilLater) { | |
| process_pos += size; | |
| cmd_data += size; | |
| } | |
| } | |
| *entries_processed = process_pos; | |
| if (error::IsError(result)) { | |
| LOG(ERROR) << "Error: " << result << " for Command " | |
| << GetCommandName(command); | |
| } | |
| return result; | |
| } | |
| error::Error WebGPUDecoderImpl::HandleDummy(uint32_t immediate_data_size, | |
| const volatile void* cmd_data) { | |
| DLOG(ERROR) << "WebGPUDecoderImpl::HandleDummy"; | |
| return error::kNoError; | |
| } | |
| } // namespace webgpu | |
| } // namespace gpu |