diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index bdc8533b04..b214c91d02 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -20,6 +20,7 @@ #include "impeller/aiks/image_filter.h" #include "impeller/aiks/paint_pass_delegate.h" #include "impeller/aiks/testing/context_spy.h" +#include "impeller/core/device_buffer.h" #include "impeller/entity/contents/solid_color_contents.h" #include "impeller/geometry/color.h" #include "impeller/geometry/constants.h" @@ -3151,6 +3152,39 @@ TEST_P(AiksTest, CanRenderTextWithLargePerspectiveTransform) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } +TEST_P(AiksTest, SetContentsWithRegion) { + auto bridge = CreateTextureForFixture("bay_bridge.jpg"); + + // Replace part of the texture with a red rectangle. + std::vector bytes(100 * 100 * 4); + for (auto i = 0u; i < bytes.size(); i += 4) { + bytes[i] = 255; + bytes[i + 1] = 0; + bytes[i + 2] = 0; + bytes[i + 3] = 255; + } + auto mapping = + std::make_shared(bytes.data(), bytes.size()); + auto device_buffer = + GetContext()->GetResourceAllocator()->CreateBufferWithCopy(*mapping); + auto cmd_buffer = GetContext()->CreateCommandBuffer(); + auto blit_pass = cmd_buffer->CreateBlitPass(); + blit_pass->AddCopy(DeviceBuffer::AsBufferView(device_buffer), bridge, + IRect::MakeLTRB(50, 50, 150, 150)); + + auto did_submit = + blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()) && + GetContext()->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok(); + ASSERT_TRUE(did_submit); + + auto image = std::make_shared(bridge); + + Canvas canvas; + canvas.DrawImage(image, {0, 0}, {}); + + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc index 93753abbdb..2d104449a0 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc @@ -219,7 +219,9 @@ bool BlitCopyBufferToTextureCommandGLES::Encode( } if (!tex_descriptor.IsValid() || - source.range.length < tex_descriptor.GetByteSizeOfBaseMipLevel()) { + source.range.length != + BytesPerPixelForPixelFormat(tex_descriptor.format) * + destination_region.Area()) { return false; } @@ -263,9 +265,9 @@ bool BlitCopyBufferToTextureCommandGLES::Encode( const GLvoid* tex_data = data.buffer_view.buffer->OnGetContents() + data.buffer_view.range.offset; - { - TRACE_EVENT1("impeller", "TexImage2DUpload", "Bytes", - std::to_string(data.buffer_view.range.length).c_str()); + // GL_INVALID_OPERATION if the texture array has not been + // defined by a previous glTexImage2D operation. + if (!texture_gles.IsSliceInitialized(slice)) { gl.TexImage2D(texture_target, // target 0u, // LOD level data.internal_format, // internal format @@ -276,8 +278,24 @@ bool BlitCopyBufferToTextureCommandGLES::Encode( data.type, // type tex_data // data ); + texture_gles.MarkSliceInitialized(slice); + } + + { + TRACE_EVENT1("impeller", "TexImage2DUpload", "Bytes", + std::to_string(data.buffer_view.range.length).c_str()); + gl.TexSubImage2D(texture_target, // target + 0u, // LOD level + destination_region.GetX(), // xoffset + destination_region.GetY(), // yoffset + destination_region.GetWidth(), // width + destination_region.GetHeight(), // height + data.external_format, // external format + data.type, // type + tex_data // data + + ); } - texture_gles.MarkContentsInitialized(); return true; } diff --git a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc index 100259a490..363400df99 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc @@ -130,14 +130,14 @@ bool BlitPassGLES::OnCopyTextureToBufferCommand( bool BlitPassGLES::OnCopyBufferToTextureCommand( BufferView source, std::shared_ptr destination, - IPoint destination_origin, + IRect destination_region, std::string label, uint32_t slice) { auto command = std::make_unique(); command->label = label; command->source = std::move(source); command->destination = std::move(destination); - command->destination_origin = destination_origin; + command->destination_region = destination_region; command->label = label; command->slice = slice; diff --git a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h index 58eaed6e8e..1b5385f874 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.h @@ -57,7 +57,7 @@ class BlitPassGLES final : public BlitPass, // |BlitPass| bool OnCopyBufferToTextureCommand(BufferView source, std::shared_ptr destination, - IPoint destination_origin, + IRect destination_region, std::string label, uint32_t slice) override; diff --git a/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h index 88397c5d68..0ea7ba2a12 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h @@ -170,6 +170,7 @@ struct GLProc { PROC(StencilMaskSeparate); \ PROC(StencilOpSeparate); \ PROC(TexImage2D); \ + PROC(TexSubImage2D); \ PROC(TexParameteri); \ PROC(TexParameterfv); \ PROC(Uniform1fv); \ diff --git a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc index af745e23fd..96939b0676 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc @@ -304,8 +304,8 @@ bool TextureGLES::OnSetContents(std::shared_ptr mapping, } }; - contents_initialized_ = reactor_->AddOperation(texture_upload); - return contents_initialized_; + slices_initialized_ = reactor_->AddOperation(texture_upload); + return slices_initialized_[0]; } // |Texture| @@ -343,13 +343,10 @@ static std::optional ToRenderBufferFormat(PixelFormat format) { } void TextureGLES::InitializeContentsIfNecessary() const { - if (!IsValid()) { + if (!IsValid() || slices_initialized_[0]) { return; } - if (contents_initialized_) { - return; - } - contents_initialized_ = true; + slices_initialized_[0] = true; if (is_wrapped_) { return; @@ -455,8 +452,12 @@ bool TextureGLES::Bind() const { return true; } -void TextureGLES::MarkContentsInitialized() const { - contents_initialized_ = true; +void TextureGLES::MarkSliceInitialized(size_t slice) const { + slices_initialized_[slice] = true; +} + +bool TextureGLES::IsSliceInitialized(size_t slice) const { + return slices_initialized_[slice]; } bool TextureGLES::GenerateMipmap() { diff --git a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.h index a1827876d8..86c8b82235 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.h @@ -5,6 +5,8 @@ #ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_TEXTURE_GLES_H_ #define FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_TEXTURE_GLES_H_ +#include + #include "impeller/base/backend_cast.h" #include "impeller/core/texture.h" #include "impeller/renderer/backend/gles/handle_gles.h" @@ -60,13 +62,18 @@ class TextureGLES final : public Texture, std::optional GetFBO() const { return wrapped_fbo_; } - void MarkContentsInitialized() const; + // For non cubemap textures, 0 indicates uninitialized and 1 indicates + // initialized. For cubemap textures, each face is initialized separately with + // each bit tracking the initialization of the corresponding slice. + void MarkSliceInitialized(size_t slice) const; + + bool IsSliceInitialized(size_t slice) const; private: ReactorGLES::Ref reactor_; const Type type_; HandleGLES handle_; - mutable bool contents_initialized_ = false; + mutable std::bitset<6> slices_initialized_ = 0; const bool is_wrapped_; const std::optional wrapped_fbo_; bool is_valid_ = false; 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 index f07c8d09bd..ca73897cdb 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm @@ -114,22 +114,21 @@ bool BlitCopyBufferToTextureCommandMTL::Encode( } auto destination_origin_mtl = - MTLOriginMake(destination_origin.x, destination_origin.y, 0); - - auto image_size = destination->GetTextureDescriptor().size; - auto source_size_mtl = MTLSizeMake(image_size.width, image_size.height, 1); + 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 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; + destination_region.GetWidth() * destination_bytes_per_pixel; [encoder copyFromBuffer:source_mtl sourceOffset:source.range.offset sourceBytesPerRow:destination_bytes_per_row - sourceBytesPerImage:destination_bytes_per_image + 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 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 6ddfe3d55a..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 @@ -55,7 +55,7 @@ class BlitPassMTL final : public BlitPass { // |BlitPass| bool OnCopyBufferToTextureCommand(BufferView source, std::shared_ptr destination, - IPoint destination_origin, + IRect destination_region, std::string label, uint32_t slice) override; 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 ce58225feb..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 @@ -127,14 +127,14 @@ bool BlitPassMTL::OnCopyTextureToBufferCommand( bool BlitPassMTL::OnCopyBufferToTextureCommand( BufferView source, std::shared_ptr destination, - IPoint destination_origin, + IRect destination_region, std::string label, uint32_t slice) { auto command = std::make_unique(); command->label = std::move(label); command->source = std::move(source); command->destination = std::move(destination); - command->destination_origin = destination_origin; + command->destination_region = destination_region; command->slice = slice; commands_.emplace_back(std::move(command)); diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_command_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_command_vk.cc index 922c2d2848..bc1c219478 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_command_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_command_vk.cc @@ -246,10 +246,12 @@ bool BlitCopyBufferToTextureCommandVK::Encode(CommandEncoderVK& encoder) const { image_copy.setBufferImageHeight(0); image_copy.setImageSubresource( vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, 0, 0, 1)); - image_copy.setImageOffset( - vk::Offset3D(destination_origin.x, destination_origin.y, 0)); - image_copy.setImageExtent(vk::Extent3D(destination->GetSize().width, - destination->GetSize().height, 1)); + image_copy.imageOffset.x = destination_region.GetX(); + image_copy.imageOffset.y = destination_region.GetY(); + image_copy.imageOffset.z = 0u; + image_copy.imageExtent.width = destination_region.GetWidth(); + image_copy.imageExtent.height = destination_region.GetHeight(); + image_copy.imageExtent.depth = 1u; if (!dst.SetLayout(dst_barrier)) { VALIDATION_LOG << "Could not encode layout transition."; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc index 1dfb570fd7..814ebd1efe 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.cc @@ -96,14 +96,14 @@ bool BlitPassVK::OnCopyTextureToBufferCommand( bool BlitPassVK::OnCopyBufferToTextureCommand( BufferView source, std::shared_ptr destination, - IPoint destination_origin, + IRect destination_region, std::string label, uint32_t slice) { auto command = std::make_unique(); command->source = std::move(source); command->destination = std::move(destination); - command->destination_origin = destination_origin; + command->destination_region = destination_region; command->label = std::move(label); command->slice = slice; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h index f693023ff6..1d336c27a4 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/blit_pass_vk.h @@ -6,6 +6,7 @@ #define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_BLIT_PASS_VK_H_ #include "flutter/impeller/base/config.h" +#include "impeller/geometry/rect.h" #include "impeller/renderer/backend/vulkan/blit_command_vk.h" #include "impeller/renderer/blit_pass.h" @@ -55,7 +56,7 @@ class BlitPassVK final : public BlitPass { // |BlitPass| bool OnCopyBufferToTextureCommand(BufferView source, std::shared_ptr destination, - IPoint destination_origin, + IRect destination_region, std::string label, uint32_t slice) override; // |BlitPass| diff --git a/engine/src/flutter/impeller/renderer/blit_command.h b/engine/src/flutter/impeller/renderer/blit_command.h index 07c951e470..0bae872ab0 100644 --- a/engine/src/flutter/impeller/renderer/blit_command.h +++ b/engine/src/flutter/impeller/renderer/blit_command.h @@ -32,7 +32,7 @@ struct BlitCopyTextureToBufferCommand : public BlitCommand { struct BlitCopyBufferToTextureCommand : public BlitCommand { BufferView source; std::shared_ptr destination; - IPoint destination_origin; + IRect destination_region; uint32_t slice = 0; }; diff --git a/engine/src/flutter/impeller/renderer/blit_pass.cc b/engine/src/flutter/impeller/renderer/blit_pass.cc index 56e919b7ac..cd9511d56a 100644 --- a/engine/src/flutter/impeller/renderer/blit_pass.cc +++ b/engine/src/flutter/impeller/renderer/blit_pass.cc @@ -122,28 +122,41 @@ bool BlitPass::AddCopy(std::shared_ptr source, bool BlitPass::AddCopy(BufferView source, std::shared_ptr destination, - IPoint destination_origin, + std::optional destination_region, std::string label, uint32_t slice) { if (!destination) { VALIDATION_LOG << "Attempted to add a texture blit with no destination."; return false; } + ISize destination_size = destination->GetSize(); + IRect destination_region_value = + destination_region.value_or(IRect::MakeSize(destination_size)); + if (destination_region_value.GetX() < 0 || + destination_region_value.GetY() < 0 || + destination_region_value.GetRight() > destination_size.width || + destination_region_value.GetBottom() > destination_size.height) { + VALIDATION_LOG << "Blit region cannot be larger than destination texture."; + return false; + } auto bytes_per_pixel = BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format); - auto bytes_per_image = - destination->GetTextureDescriptor().size.Area() * bytes_per_pixel; + auto bytes_per_image = destination_region_value.Area() * bytes_per_pixel; if (source.range.length != bytes_per_image) { VALIDATION_LOG << "Attempted to add a texture blit with out of bounds access."; return false; } + if (slice > 5) { + VALIDATION_LOG << "Invalid value for slice: " << slice; + return false; + } return OnCopyBufferToTextureCommand(std::move(source), std::move(destination), - destination_origin, std::move(label), - slice); + destination_region_value, + std::move(label), slice); } bool BlitPass::GenerateMipmap(std::shared_ptr texture, diff --git a/engine/src/flutter/impeller/renderer/blit_pass.h b/engine/src/flutter/impeller/renderer/blit_pass.h index 3ea790c5a4..5b2f56b33b 100644 --- a/engine/src/flutter/impeller/renderer/blit_pass.h +++ b/engine/src/flutter/impeller/renderer/blit_pass.h @@ -89,8 +89,9 @@ class BlitPass { /// @param[in] source The buffer view to read for copying. /// @param[in] destination The texture to overwrite using the source /// contents. - /// @param[in] destination_offset The offset to start writing to in the - /// destination buffer. + /// @param[in] destination_region The offset to start writing to in the + /// destination texture. If not provided, this + /// defaults to the entire texture. /// @param[in] label The optional debug label to give the /// command. /// @param[in] slice For cubemap textures, the slice to write @@ -98,9 +99,17 @@ class BlitPass { /// /// @return If the command was valid for subsequent commitment. /// + /// If a region smaller than the texture size is provided, the + /// contents are treated as containing tightly packed pixel data of + /// that region. Only the portion of the texture in this region is + /// replaced and existing data is preserved. + /// + /// For example, to replace the top left 10 x 10 region of a larger + /// 100 x 100 texture, the region is {0, 0, 10, 10} and the expected + /// buffer size in bytes is 100 x bpp. bool AddCopy(BufferView source, std::shared_ptr destination, - IPoint destination_origin = {}, + std::optional destination_region = std::nullopt, std::string label = "", uint32_t slice = 0); @@ -148,7 +157,7 @@ class BlitPass { virtual bool OnCopyBufferToTextureCommand( BufferView source, std::shared_ptr destination, - IPoint destination_origin, + IRect destination_region, std::string label, uint32_t slice) = 0; diff --git a/engine/src/flutter/impeller/renderer/blit_pass_unittests.cc b/engine/src/flutter/impeller/renderer/blit_pass_unittests.cc index dbf534d1de..732af99108 100644 --- a/engine/src/flutter/impeller/renderer/blit_pass_unittests.cc +++ b/engine/src/flutter/impeller/renderer/blit_pass_unittests.cc @@ -4,6 +4,7 @@ #include "gtest/gtest.h" #include "impeller/base/validation.h" +#include "impeller/core/device_buffer_descriptor.h" #include "impeller/core/formats.h" #include "impeller/core/texture_descriptor.h" #include "impeller/playground/playground_test.h" @@ -75,5 +76,33 @@ TEST_P(BlitPassTest, BlitPassesForMatchingFormats) { EXPECT_TRUE(blit_pass->AddCopy(src, dst)); } +TEST_P(BlitPassTest, ChecksInvalidSliceParameters) { + ScopedValidationDisable scope; // avoid noise in output. + auto context = GetContext(); + auto cmd_buffer = context->CreateCommandBuffer(); + auto blit_pass = cmd_buffer->CreateBlitPass(); + + TextureDescriptor dst_format; + dst_format.storage_mode = StorageMode::kDevicePrivate; + dst_format.format = PixelFormat::kR8G8B8A8UNormInt; + dst_format.size = {100, 100}; + auto dst = context->GetResourceAllocator()->CreateTexture(dst_format); + + DeviceBufferDescriptor src_format; + src_format.size = 40000; + src_format.storage_mode = StorageMode::kHostVisible; + auto src = context->GetResourceAllocator()->CreateBuffer(src_format); + + ASSERT_TRUE(dst); + ASSERT_TRUE(src); + + EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst, + std::nullopt, "", /*slice=*/25)); + EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst, + std::nullopt, "", /*slice=*/6)); + EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst, + std::nullopt, "", /*slice=*/0)); +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/testing/mocks.h b/engine/src/flutter/impeller/renderer/testing/mocks.h index 848a811393..c333ac9624 100644 --- a/engine/src/flutter/impeller/renderer/testing/mocks.h +++ b/engine/src/flutter/impeller/renderer/testing/mocks.h @@ -82,7 +82,7 @@ class MockBlitPass : public BlitPass { OnCopyBufferToTextureCommand, (BufferView source, std::shared_ptr destination, - IPoint destination_origin, + IRect destination_rect, std::string label, uint32_t slice), (override)); diff --git a/engine/src/flutter/testing/impeller_golden_tests_output.txt b/engine/src/flutter/testing/impeller_golden_tests_output.txt index 4c37c79c51..f267f1d575 100644 --- a/engine/src/flutter/testing/impeller_golden_tests_output.txt +++ b/engine/src/flutter/testing/impeller_golden_tests_output.txt @@ -702,6 +702,9 @@ impeller_Play_AiksTest_SaveLayerDrawsBehindSubsequentEntities_Vulkan.png impeller_Play_AiksTest_SaveLayerFiltersScaleWithTransform_Metal.png impeller_Play_AiksTest_SaveLayerFiltersScaleWithTransform_OpenGLES.png impeller_Play_AiksTest_SaveLayerFiltersScaleWithTransform_Vulkan.png +impeller_Play_AiksTest_SetContentsWithRegion_Metal.png +impeller_Play_AiksTest_SetContentsWithRegion_OpenGLES.png +impeller_Play_AiksTest_SetContentsWithRegion_Vulkan.png impeller_Play_AiksTest_SiblingSaveLayerBoundsAreRespected_Metal.png impeller_Play_AiksTest_SiblingSaveLayerBoundsAreRespected_OpenGLES.png impeller_Play_AiksTest_SiblingSaveLayerBoundsAreRespected_Vulkan.png