diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index f6761efd07..02dd25cd71 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -42656,6 +42656,8 @@ ORIGIN: ../../../flutter/impeller/renderer/backend/gles/texture_gles.cc + ../../ ORIGIN: ../../../flutter/impeller/renderer/backend/gles/texture_gles.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/backend/metal/allocator_mtl.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/backend/metal/allocator_mtl.mm + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/renderer/backend/metal/blit_command_mtl.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/impeller/renderer/backend/metal/blit_command_mtl.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/backend/metal/blit_pass_mtl.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/renderer/backend/metal/command_buffer_mtl.h + ../../../flutter/LICENSE @@ -45529,6 +45531,8 @@ FILE: ../../../flutter/impeller/renderer/backend/gles/texture_gles.cc FILE: ../../../flutter/impeller/renderer/backend/gles/texture_gles.h FILE: ../../../flutter/impeller/renderer/backend/metal/allocator_mtl.h FILE: ../../../flutter/impeller/renderer/backend/metal/allocator_mtl.mm +FILE: ../../../flutter/impeller/renderer/backend/metal/blit_command_mtl.h +FILE: ../../../flutter/impeller/renderer/backend/metal/blit_command_mtl.mm FILE: ../../../flutter/impeller/renderer/backend/metal/blit_pass_mtl.h FILE: ../../../flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm FILE: ../../../flutter/impeller/renderer/backend/metal/command_buffer_mtl.h diff --git a/engine/src/flutter/impeller/renderer/backend/metal/BUILD.gn b/engine/src/flutter/impeller/renderer/backend/metal/BUILD.gn index e681b7bfee..2c9e0f2e71 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/BUILD.gn +++ b/engine/src/flutter/impeller/renderer/backend/metal/BUILD.gn @@ -8,6 +8,8 @@ impeller_component("metal") { sources = [ "allocator_mtl.h", "allocator_mtl.mm", + "blit_command_mtl.h", + "blit_command_mtl.mm", "blit_pass_mtl.h", "blit_pass_mtl.mm", "command_buffer_mtl.h", diff --git a/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.h b/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.h new file mode 100644 index 0000000000..90dad19560 --- /dev/null +++ b/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.h @@ -0,0 +1,66 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_BLIT_COMMAND_MTL_H_ +#define FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_BLIT_COMMAND_MTL_H_ + +#include + +#include "impeller/base/backend_cast.h" +#include "impeller/renderer/blit_command.h" + +namespace impeller { + +/// Mixin for dispatching Metal commands. +struct BlitEncodeMTL : BackendCast { + virtual ~BlitEncodeMTL(); + + virtual std::string GetLabel() const = 0; + + [[nodiscard]] virtual bool Encode( + id encoder) const = 0; +}; + +struct BlitCopyTextureToTextureCommandMTL + : public BlitCopyTextureToTextureCommand, + public BlitEncodeMTL { + ~BlitCopyTextureToTextureCommandMTL() override; + + std::string GetLabel() const override; + + [[nodiscard]] bool Encode(id encoder) const override; +}; + +struct BlitCopyTextureToBufferCommandMTL + : public BlitCopyTextureToBufferCommand, + public BlitEncodeMTL { + ~BlitCopyTextureToBufferCommandMTL() override; + + std::string GetLabel() const override; + + [[nodiscard]] bool Encode(id encoder) const override; +}; + +struct BlitGenerateMipmapCommandMTL : public BlitGenerateMipmapCommand, + public BlitEncodeMTL { + ~BlitGenerateMipmapCommandMTL() override; + + std::string GetLabel() const override; + + [[nodiscard]] bool Encode(id encoder) const override; +}; + +struct BlitCopyBufferToTextureCommandMTL + : public BlitCopyBufferToTextureCommand, + public BlitEncodeMTL { + ~BlitCopyBufferToTextureCommandMTL() override; + + std::string GetLabel() const override; + + [[nodiscard]] bool Encode(id encoder) const override; +}; + +} // namespace impeller + +#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_BLIT_COMMAND_MTL_H_ diff --git a/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm new file mode 100644 index 0000000000..16ebf4441d --- /dev/null +++ b/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm @@ -0,0 +1,152 @@ +// Copyright 2013 The Flutter 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 "impeller/renderer/backend/metal/blit_command_mtl.h" + +#include "impeller/renderer/backend/metal/device_buffer_mtl.h" +#include "impeller/renderer/backend/metal/texture_mtl.h" + +namespace impeller { + +BlitEncodeMTL::~BlitEncodeMTL() = default; + +BlitCopyTextureToTextureCommandMTL::~BlitCopyTextureToTextureCommandMTL() = + default; + +std::string BlitCopyTextureToTextureCommandMTL::GetLabel() const { + return label; +} + +bool BlitCopyTextureToTextureCommandMTL::Encode( + id encoder) const { + auto source_mtl = TextureMTL::Cast(*source).GetMTLTexture(); + if (!source_mtl) { + return false; + } + + auto destination_mtl = TextureMTL::Cast(*destination).GetMTLTexture(); + if (!destination_mtl) { + return false; + } + + auto source_origin_mtl = + MTLOriginMake(source_region.GetX(), source_region.GetY(), 0); + auto source_size_mtl = + MTLSizeMake(source_region.GetWidth(), source_region.GetHeight(), 1); + auto destination_origin_mtl = + MTLOriginMake(destination_origin.x, destination_origin.y, 0); + + [encoder copyFromTexture:source_mtl + sourceSlice:0 + sourceLevel:0 + sourceOrigin:source_origin_mtl + sourceSize:source_size_mtl + toTexture:destination_mtl + destinationSlice:0 + destinationLevel:0 + destinationOrigin:destination_origin_mtl]; + + return true; +}; + +BlitCopyTextureToBufferCommandMTL::~BlitCopyTextureToBufferCommandMTL() = + default; + +std::string BlitCopyTextureToBufferCommandMTL::GetLabel() const { + return label; +} + +bool BlitCopyTextureToBufferCommandMTL::Encode( + id encoder) const { + auto source_mtl = TextureMTL::Cast(*source).GetMTLTexture(); + if (!source_mtl) { + return false; + } + + auto destination_mtl = DeviceBufferMTL::Cast(*destination).GetMTLBuffer(); + if (!destination_mtl) { + return false; + } + + auto source_origin_mtl = + MTLOriginMake(source_region.GetX(), source_region.GetY(), 0); + auto source_size_mtl = + MTLSizeMake(source_region.GetWidth(), source_region.GetHeight(), 1); + + auto destination_bytes_per_pixel = + BytesPerPixelForPixelFormat(source->GetTextureDescriptor().format); + auto destination_bytes_per_row = + source_size_mtl.width * destination_bytes_per_pixel; + auto destination_bytes_per_image = + source_size_mtl.height * destination_bytes_per_row; + + [encoder copyFromTexture:source_mtl + sourceSlice:0 + sourceLevel:0 + sourceOrigin:source_origin_mtl + sourceSize:source_size_mtl + toBuffer:destination_mtl + destinationOffset:destination_offset + destinationBytesPerRow:destination_bytes_per_row + destinationBytesPerImage:destination_bytes_per_image]; + + return true; +}; + +BlitCopyBufferToTextureCommandMTL::~BlitCopyBufferToTextureCommandMTL() = + default; + +std::string BlitCopyBufferToTextureCommandMTL::GetLabel() const { + return label; +} + +bool BlitCopyBufferToTextureCommandMTL::Encode( + id encoder) const { + auto source_mtl = DeviceBufferMTL::Cast(*source.buffer).GetMTLBuffer(); + if (!source_mtl) { + return false; + } + + auto destination_mtl = TextureMTL::Cast(*destination).GetMTLTexture(); + if (!destination_mtl) { + return false; + } + + auto destination_origin_mtl = + MTLOriginMake(destination_region.GetX(), destination_region.GetY(), 0); + auto source_size_mtl = MTLSizeMake(destination_region.GetWidth(), + destination_region.GetHeight(), 1); + + auto destination_bytes_per_pixel = + BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format); + auto source_bytes_per_row = + destination_region.GetWidth() * destination_bytes_per_pixel; + + [encoder copyFromBuffer:source_mtl + sourceOffset:source.range.offset + sourceBytesPerRow:source_bytes_per_row + sourceBytesPerImage: + 0 // 0 for 2D textures according to + // https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400752-copyfrombuffer + sourceSize:source_size_mtl + toTexture:destination_mtl + destinationSlice:slice + destinationLevel:0 + destinationOrigin:destination_origin_mtl]; + + return true; +}; + +BlitGenerateMipmapCommandMTL::~BlitGenerateMipmapCommandMTL() = default; + +std::string BlitGenerateMipmapCommandMTL::GetLabel() const { + return label; +} + +bool BlitGenerateMipmapCommandMTL::Encode( + id encoder) const { + return TextureMTL::Cast(*texture).GenerateMipmap(encoder); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h index 3363eb969f..b21a04e20a 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h +++ b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.h @@ -7,6 +7,7 @@ #include +#include "impeller/renderer/backend/metal/blit_command_mtl.h" #include "impeller/renderer/blit_pass.h" namespace impeller { @@ -19,14 +20,10 @@ class BlitPassMTL final : public BlitPass { private: friend class CommandBufferMTL; - id encoder_ = nil; + std::vector> commands_; id buffer_ = nil; + std::string label_; bool is_valid_ = false; - bool is_metal_trace_active_ = false; - // Many parts of the codebase will start writing to a render pass but - // never submit them. This boolean is used to track if a submit happened - // so that in the dtor we can always ensure the render pass is finished. - mutable bool did_finish_encoding_ = false; explicit BlitPassMTL(id buffer); @@ -40,6 +37,8 @@ class BlitPassMTL final : public BlitPass { bool EncodeCommands( const std::shared_ptr& transients_allocator) const override; + bool EncodeCommands(id pass) const; + // |BlitPass| bool OnCopyTextureToTextureCommand(std::shared_ptr source, std::shared_ptr destination, diff --git a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm index 9caa449cdf..172a36212d 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm @@ -15,12 +15,12 @@ #include "impeller/core/formats.h" #include "impeller/core/host_buffer.h" #include "impeller/core/shader_types.h" +#include "impeller/renderer/backend/metal/blit_command_mtl.h" #include "impeller/renderer/backend/metal/device_buffer_mtl.h" #include "impeller/renderer/backend/metal/formats_mtl.h" #include "impeller/renderer/backend/metal/pipeline_mtl.h" #include "impeller/renderer/backend/metal/sampler_mtl.h" #include "impeller/renderer/backend/metal/texture_mtl.h" - #include "impeller/renderer/blit_command.h" namespace impeller { @@ -29,19 +29,10 @@ BlitPassMTL::BlitPassMTL(id buffer) : buffer_(buffer) { if (!buffer_) { return; } - encoder_ = [buffer_ blitCommandEncoder]; -#ifdef IMPELLER_DEBUG - is_metal_trace_active_ = - [[MTLCaptureManager sharedCaptureManager] isCapturing]; -#endif // IMPELLER_DEBUG is_valid_ = true; } -BlitPassMTL::~BlitPassMTL() { - if (!did_finish_encoding_) { - [encoder_ endEncoding]; - } -} +BlitPassMTL::~BlitPassMTL() = default; bool BlitPassMTL::IsValid() const { return is_valid_; @@ -51,13 +42,49 @@ void BlitPassMTL::OnSetLabel(std::string label) { if (label.empty()) { return; } - [encoder_ setLabel:@(label.c_str())]; + label_ = std::move(label); } bool BlitPassMTL::EncodeCommands( const std::shared_ptr& transients_allocator) const { - [encoder_ endEncoding]; - did_finish_encoding_ = true; + TRACE_EVENT0("impeller", "BlitPassMTL::EncodeCommands"); + if (!IsValid()) { + return false; + } + + auto blit_command_encoder = [buffer_ blitCommandEncoder]; + + if (!blit_command_encoder) { + return false; + } + + if (!label_.empty()) { + [blit_command_encoder setLabel:@(label_.c_str())]; + } + + // Success or failure, the pass must end. The buffer can only process one pass + // at a time. + fml::ScopedCleanupClosure auto_end( + [blit_command_encoder]() { [blit_command_encoder endEncoding]; }); + + return EncodeCommands(blit_command_encoder); +} + +bool BlitPassMTL::EncodeCommands(id encoder) const { + fml::closure pop_debug_marker = [encoder]() { [encoder popDebugGroup]; }; + for (const auto& command : commands_) { + fml::ScopedCleanupClosure auto_pop_debug_marker(pop_debug_marker); + auto label = command->GetLabel(); + if (!label.empty()) { + [encoder pushDebugGroup:@(label.c_str())]; + } else { + auto_pop_debug_marker.Release(); + } + + if (!command->Encode(encoder)) { + return false; + } + } return true; } @@ -68,44 +95,14 @@ bool BlitPassMTL::OnCopyTextureToTextureCommand( IRect source_region, IPoint destination_origin, std::string label) { - auto source_mtl = TextureMTL::Cast(*source).GetMTLTexture(); - if (!source_mtl) { - return false; - } - - auto destination_mtl = TextureMTL::Cast(*destination).GetMTLTexture(); - if (!destination_mtl) { - return false; - } - - auto source_origin_mtl = - MTLOriginMake(source_region.GetX(), source_region.GetY(), 0); - auto source_size_mtl = - MTLSizeMake(source_region.GetWidth(), source_region.GetHeight(), 1); - auto destination_origin_mtl = - MTLOriginMake(destination_origin.x, destination_origin.y, 0); - -#ifdef IMPELLER_DEBUG - if (is_metal_trace_active_) { - [encoder_ pushDebugGroup:@(label.c_str())]; - } -#endif // IMPELLER_DEBUG - [encoder_ copyFromTexture:source_mtl - sourceSlice:0 - sourceLevel:0 - sourceOrigin:source_origin_mtl - sourceSize:source_size_mtl - toTexture:destination_mtl - destinationSlice:0 - destinationLevel:0 - destinationOrigin:destination_origin_mtl]; - -#ifdef IMPELLER_DEBUG - if (is_metal_trace_active_) { - [encoder_ popDebugGroup]; - } -#endif // IMPELLER_DEBUG + auto command = std::make_unique(); + command->label = label; + command->source = std::move(source); + command->destination = std::move(destination); + command->source_region = source_region; + command->destination_origin = destination_origin; + commands_.emplace_back(std::move(command)); return true; } @@ -116,48 +113,14 @@ bool BlitPassMTL::OnCopyTextureToBufferCommand( IRect source_region, size_t destination_offset, std::string label) { - auto source_mtl = TextureMTL::Cast(*source).GetMTLTexture(); - if (!source_mtl) { - return false; - } + auto command = std::make_unique(); + command->label = label; + command->source = std::move(source); + command->destination = std::move(destination); + command->source_region = source_region; + command->destination_offset = destination_offset; - auto destination_mtl = DeviceBufferMTL::Cast(*destination).GetMTLBuffer(); - if (!destination_mtl) { - return false; - } - - auto source_origin_mtl = - MTLOriginMake(source_region.GetX(), source_region.GetY(), 0); - auto source_size_mtl = - MTLSizeMake(source_region.GetWidth(), source_region.GetHeight(), 1); - - auto destination_bytes_per_pixel = - BytesPerPixelForPixelFormat(source->GetTextureDescriptor().format); - auto destination_bytes_per_row = - source_size_mtl.width * destination_bytes_per_pixel; - auto destination_bytes_per_image = - source_size_mtl.height * destination_bytes_per_row; - -#ifdef IMPELLER_DEBUG - if (is_metal_trace_active_) { - [encoder_ pushDebugGroup:@(label.c_str())]; - } -#endif // IMPELLER_DEBUG - [encoder_ copyFromTexture:source_mtl - sourceSlice:0 - sourceLevel:0 - sourceOrigin:source_origin_mtl - sourceSize:source_size_mtl - toBuffer:destination_mtl - destinationOffset:destination_offset - destinationBytesPerRow:destination_bytes_per_row - destinationBytesPerImage:destination_bytes_per_image]; - -#ifdef IMPELLER_DEBUG - if (is_metal_trace_active_) { - [encoder_ popDebugGroup]; - } -#endif // IMPELLER_DEBUG + commands_.emplace_back(std::move(command)); return true; } @@ -167,67 +130,26 @@ bool BlitPassMTL::OnCopyBufferToTextureCommand( IRect destination_region, std::string label, uint32_t slice) { - auto source_mtl = DeviceBufferMTL::Cast(*source.buffer).GetMTLBuffer(); - if (!source_mtl) { - return false; - } + auto command = std::make_unique(); + command->label = std::move(label); + command->source = std::move(source); + command->destination = std::move(destination); + command->destination_region = destination_region; + command->slice = slice; - auto destination_mtl = TextureMTL::Cast(*destination).GetMTLTexture(); - if (!destination_mtl) { - return false; - } - - auto destination_origin_mtl = - MTLOriginMake(destination_region.GetX(), destination_region.GetY(), 0); - auto source_size_mtl = MTLSizeMake(destination_region.GetWidth(), - destination_region.GetHeight(), 1); - - auto destination_bytes_per_pixel = - BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format); - auto source_bytes_per_row = - destination_region.GetWidth() * destination_bytes_per_pixel; - -#ifdef IMPELLER_DEBUG - if (is_metal_trace_active_) { - [encoder_ pushDebugGroup:@(label.c_str())]; - } -#endif // IMPELLER_DEBUG - [encoder_ - copyFromBuffer:source_mtl - sourceOffset:source.range.offset - sourceBytesPerRow:source_bytes_per_row - sourceBytesPerImage: - 0 // 0 for 2D textures according to - // https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400752-copyfrombuffer - sourceSize:source_size_mtl - toTexture:destination_mtl - destinationSlice:slice - destinationLevel:0 - destinationOrigin:destination_origin_mtl]; - -#ifdef IMPELLER_DEBUG - if (is_metal_trace_active_) { - [encoder_ popDebugGroup]; - } -#endif // IMPELLER_DEBUG + commands_.emplace_back(std::move(command)); return true; } // |BlitPass| bool BlitPassMTL::OnGenerateMipmapCommand(std::shared_ptr texture, std::string label) { -#ifdef IMPELLER_DEBUG - if (is_metal_trace_active_) { - [encoder_ pushDebugGroup:@(label.c_str())]; - } -#endif // IMPELLER_DEBUG - auto result = TextureMTL::Cast(*texture).GenerateMipmap(encoder_); -#ifdef IMPELLER_DEBUG - if (is_metal_trace_active_) { - [encoder_ popDebugGroup]; - } -#endif // IMPELLER_DEBUG - return result; + auto command = std::make_unique(); + command->label = label; + command->texture = std::move(texture); + + commands_.emplace_back(std::move(command)); + return true; } } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/blit_pass_unittests.cc b/engine/src/flutter/impeller/renderer/blit_pass_unittests.cc index b99a3d0aff..e30d89fb04 100644 --- a/engine/src/flutter/impeller/renderer/blit_pass_unittests.cc +++ b/engine/src/flutter/impeller/renderer/blit_pass_unittests.cc @@ -66,13 +66,11 @@ TEST_P(BlitPassTest, BlitPassesForMatchingFormats) { TextureDescriptor src_desc; src_desc.format = PixelFormat::kR8G8B8A8UNormInt; src_desc.size = {100, 100}; - src_desc.storage_mode = StorageMode::kHostVisible; auto src = context->GetResourceAllocator()->CreateTexture(src_desc); TextureDescriptor dst_format; dst_format.format = PixelFormat::kR8G8B8A8UNormInt; dst_format.size = {100, 100}; - dst_format.storage_mode = StorageMode::kHostVisible; auto dst = context->GetResourceAllocator()->CreateTexture(dst_format); EXPECT_TRUE(blit_pass->AddCopy(src, dst));