[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
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user