Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] Clip the DrawImageRect source rect to the bounds of the image #56183

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions impeller/display_list/aiks_dl_basic_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 20 additions & 1 deletion impeller/display_list/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,28 @@ void Canvas::DrawImageRect(const std::shared_ptr<Texture>& image,
return;
}

std::optional<Rect> 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));
Expand Down
3 changes: 3 additions & 0 deletions testing/impeller_golden_tests_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down