From ac991b9a6ed00978ebc1e4c706a35c196145f8b9 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 29 Oct 2024 10:10:31 -0700 Subject: [PATCH] [Impeller] Clip the DrawImageRect source rect to the bounds of the image (flutter/engine#56183) The destination rect will then be updated to a mapping of the clipped source rect. This matches the behavior of Skia's SkCanvas::drawImageRect. Fixes https://github.com/flutter/flutter/issues/157167 --- .../display_list/aiks_dl_basic_unittests.cc | 20 ++++++++++++++++++ .../flutter/impeller/display_list/canvas.cc | 21 ++++++++++++++++++- .../testing/impeller_golden_tests_output.txt | 3 +++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/impeller/display_list/aiks_dl_basic_unittests.cc b/engine/src/flutter/impeller/display_list/aiks_dl_basic_unittests.cc index cf9fcf0183..bb813e29a3 100644 --- a/engine/src/flutter/impeller/display_list/aiks_dl_basic_unittests.cc +++ b/engine/src/flutter/impeller/display_list/aiks_dl_basic_unittests.cc @@ -233,6 +233,26 @@ TEST_P(AiksTest, CanRenderImageRect) { ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); } +TEST_P(AiksTest, DrawImageRectSrcOutsideBounds) { + DisplayListBuilder builder; + auto image = DlImageImpeller::Make(CreateTextureForFixture("kalimba.jpg")); + + // Use a source rect that is partially outside the bounds of the image. + auto source_rect = SkRect::MakeXYWH( + image->dimensions().fWidth * 0.25f, image->dimensions().fHeight * 0.4f, + image->dimensions().fWidth, image->dimensions().fHeight); + + auto dest_rect = SkRect::MakeXYWH(100, 100, 600, 600); + + DlPaint paint; + paint.setColor(DlColor::kMidGrey()); + builder.DrawRect(dest_rect, paint); + + builder.DrawImageRect(image, source_rect, dest_rect, + DlImageSampling::kNearestNeighbor); + ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); +} + TEST_P(AiksTest, CanRenderSimpleClips) { DisplayListBuilder builder; builder.Scale(GetContentScale().x, GetContentScale().y); diff --git a/engine/src/flutter/impeller/display_list/canvas.cc b/engine/src/flutter/impeller/display_list/canvas.cc index b4aa89af7b..1132333296 100644 --- a/engine/src/flutter/impeller/display_list/canvas.cc +++ b/engine/src/flutter/impeller/display_list/canvas.cc @@ -679,9 +679,28 @@ void Canvas::DrawImageRect(const std::shared_ptr& image, return; } + std::optional clipped_source = + source.Intersection(Rect::MakeSize(size)); + if (!clipped_source) { + return; + } + if (*clipped_source != source) { + Scalar sx = dest.GetWidth() / source.GetWidth(); + Scalar sy = dest.GetHeight() / source.GetHeight(); + Scalar tx = dest.GetLeft() - source.GetLeft() * sx; + Scalar ty = dest.GetTop() - source.GetTop() * sy; + // clang-format off + Matrix src_to_dest( sx, 0.0f, 0.0f, 0.0f, + 0.0f, sy, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + tx, ty, 0.0f, 1.0f); + // clang-format on + dest = clipped_source->TransformBounds(src_to_dest); + } + auto texture_contents = TextureContents::MakeRect(dest); texture_contents->SetTexture(image); - texture_contents->SetSourceRect(source); + texture_contents->SetSourceRect(*clipped_source); texture_contents->SetStrictSourceRect(src_rect_constraint == SourceRectConstraint::kStrict); texture_contents->SetSamplerDescriptor(std::move(sampler)); diff --git a/engine/src/flutter/testing/impeller_golden_tests_output.txt b/engine/src/flutter/testing/impeller_golden_tests_output.txt index e2fd3037a2..7c92b9b133 100644 --- a/engine/src/flutter/testing/impeller_golden_tests_output.txt +++ b/engine/src/flutter/testing/impeller_golden_tests_output.txt @@ -588,6 +588,9 @@ impeller_Play_AiksTest_DrawAtlasWithColorSimple_Vulkan.png impeller_Play_AiksTest_DrawAtlasWithOpacity_Metal.png impeller_Play_AiksTest_DrawAtlasWithOpacity_OpenGLES.png impeller_Play_AiksTest_DrawAtlasWithOpacity_Vulkan.png +impeller_Play_AiksTest_DrawImageRectSrcOutsideBounds_Metal.png +impeller_Play_AiksTest_DrawImageRectSrcOutsideBounds_OpenGLES.png +impeller_Play_AiksTest_DrawImageRectSrcOutsideBounds_Vulkan.png impeller_Play_AiksTest_DrawLinesRenderCorrectly_Metal.png impeller_Play_AiksTest_DrawLinesRenderCorrectly_OpenGLES.png impeller_Play_AiksTest_DrawLinesRenderCorrectly_Vulkan.png