From 2604d38c3d492ed26e096c6c018661869bd9b4db Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 3 Aug 2022 13:27:04 -0700 Subject: [PATCH] [impeller] use cullRect for drawAtlas coverage (flutter/engine#35133) --- engine/src/flutter/impeller/aiks/canvas.cc | 2 +- .../entity/contents/atlas_contents.cc | 8 ++++ .../impeller/entity/contents/atlas_contents.h | 3 ++ .../impeller/entity/entity_unittests.cc | 40 ++++++++++++++++++- 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index 8aee04ec32..8b48264cf8 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -346,8 +346,8 @@ void Canvas::DrawAtlas(std::shared_ptr atlas, contents->SetTexture(atlas->GetTexture()); contents->SetSamplerDescriptor(std::move(sampler)); contents->SetBlendMode(blend_mode); + contents->SetCullRect(cull_rect); contents->SetAlpha(paint.color.alpha); - // TODO(jonahwilliams): set cull rect. Entity entity; entity.SetTransformation(GetCurrentTransformation()); diff --git a/engine/src/flutter/impeller/entity/contents/atlas_contents.cc b/engine/src/flutter/impeller/entity/contents/atlas_contents.cc index 721fe5e7e4..8e82a568c1 100644 --- a/engine/src/flutter/impeller/entity/contents/atlas_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/atlas_contents.cc @@ -50,7 +50,15 @@ void AtlasContents::SetBlendMode(Entity::BlendMode blend_mode) { blend_mode_ = blend_mode; } +void AtlasContents::SetCullRect(std::optional cull_rect) { + cull_rect_ = cull_rect; +} + std::optional AtlasContents::GetCoverage(const Entity& entity) const { + if (cull_rect_.has_value()) { + return cull_rect_.value().TransformBounds(entity.GetTransformation()); + } + Rect bounding_box = {}; for (size_t i = 0; i < texture_coords_.size(); i++) { auto matrix = transforms_[i]; diff --git a/engine/src/flutter/impeller/entity/contents/atlas_contents.h b/engine/src/flutter/impeller/entity/contents/atlas_contents.h index e5dd3185ba..2ff284c1a5 100644 --- a/engine/src/flutter/impeller/entity/contents/atlas_contents.h +++ b/engine/src/flutter/impeller/entity/contents/atlas_contents.h @@ -33,6 +33,8 @@ class AtlasContents final : public Contents { void SetColors(std::vector colors); + void SetCullRect(std::optional cull_rect); + void SetSamplerDescriptor(SamplerDescriptor desc); void SetAlpha(Scalar alpha); @@ -53,6 +55,7 @@ class AtlasContents final : public Contents { std::vector colors_; std::vector transforms_; Entity::BlendMode blend_mode_; + std::optional cull_rect_; Scalar alpha_ = 1.0; SamplerDescriptor sampler_descriptor_ = {}; diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index d4784161db..a38b6bb974 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -1159,6 +1159,44 @@ TEST_P(EntityTest, DrawAtlasWithColor) { ASSERT_TRUE(OpenPlaygroundHere(e)); } +TEST_P(EntityTest, DrawAtlasUsesProvidedCullRectForCoverage) { + auto atlas = CreateTextureForFixture("bay_bridge.jpg"); + auto size = atlas->GetSize(); + + Scalar half_width = size.width / 2; + Scalar half_height = size.height / 2; + std::vector texture_coordinates = { + Rect::MakeLTRB(0, 0, half_width, half_height), + Rect::MakeLTRB(half_width, 0, size.width, half_height), + Rect::MakeLTRB(0, half_height, half_width, size.height), + Rect::MakeLTRB(half_width, half_height, size.width, size.height)}; + std::vector transforms = { + Matrix::MakeTranslation({0, 0, 0}), + Matrix::MakeTranslation({half_width, 0, 0}), + Matrix::MakeTranslation({0, half_height, 0}), + Matrix::MakeTranslation({half_width, half_height, 0})}; + + std::shared_ptr contents = std::make_shared(); + + contents->SetTransforms(std::move(transforms)); + contents->SetTextureCoordinates(std::move(texture_coordinates)); + contents->SetTexture(atlas); + contents->SetBlendMode(Entity::BlendMode::kSource); + + auto transform = Matrix::MakeScale(GetContentScale()); + Entity e; + e.SetTransformation(transform); + e.SetContents(contents); + + ASSERT_EQ(contents->GetCoverage(e).value(), + Rect::MakeSize(size).TransformBounds(transform)); + + contents->SetCullRect(Rect::MakeLTRB(0, 0, 10, 10)); + + ASSERT_EQ(contents->GetCoverage(e).value(), + Rect::MakeLTRB(0, 0, 10, 10).TransformBounds(transform)); +} + TEST_P(EntityTest, DrawAtlasWithOpacity) { // Draws the image as four squares stiched together slightly // opaque @@ -1184,8 +1222,8 @@ TEST_P(EntityTest, DrawAtlasWithOpacity) { contents->SetTransforms(std::move(transforms)); contents->SetTextureCoordinates(std::move(texture_coordinates)); contents->SetTexture(atlas); - contents->SetAlpha(0.5); contents->SetBlendMode(Entity::BlendMode::kSource); + contents->SetAlpha(0.5); Entity e; e.SetTransformation(Matrix::MakeScale(GetContentScale()));