diff --git a/engine/src/flutter/impeller/entity/contents/atlas_contents.cc b/engine/src/flutter/impeller/entity/contents/atlas_contents.cc index f3d31fbece..c3f3661fb2 100644 --- a/engine/src/flutter/impeller/entity/contents/atlas_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/atlas_contents.cc @@ -35,10 +35,12 @@ std::shared_ptr AtlasContents::GetTexture() const { void AtlasContents::SetTransforms(std::vector transforms) { transforms_ = std::move(transforms); + bounding_box_cache_.reset(); } void AtlasContents::SetTextureCoordinates(std::vector texture_coords) { texture_coords_ = std::move(texture_coords); + bounding_box_cache_.reset(); } void AtlasContents::SetColors(std::vector colors) { @@ -65,14 +67,17 @@ std::optional AtlasContents::GetCoverage(const Entity& entity) const { } Rect AtlasContents::ComputeBoundingBox() const { - Rect bounding_box = {}; - for (size_t i = 0; i < texture_coords_.size(); i++) { - auto matrix = transforms_[i]; - auto sample_rect = texture_coords_[i]; - auto bounds = Rect::MakeSize(sample_rect.size).TransformBounds(matrix); - bounding_box = bounds.Union(bounding_box); + if (!bounding_box_cache_.has_value()) { + Rect bounding_box = {}; + for (size_t i = 0; i < texture_coords_.size(); i++) { + auto matrix = transforms_[i]; + auto sample_rect = texture_coords_[i]; + auto bounds = Rect::MakeSize(sample_rect.size).TransformBounds(matrix); + bounding_box = bounds.Union(bounding_box); + } + bounding_box_cache_ = bounding_box; } - return bounding_box; + return bounding_box_cache_.value(); } void AtlasContents::SetSamplerDescriptor(SamplerDescriptor desc) { @@ -164,7 +169,7 @@ bool AtlasTextureContents::Render(const ContentContext& renderer, auto texture_coords = parent_.GetTextureCoordinates(); auto transforms = parent_.GetTransforms(); - const auto texture_size = texture->GetSize(); + const Size texture_size(texture->GetSize()); VertexBufferBuilder vertex_builder; vertex_builder.Reserve(texture_coords.size() * 6); constexpr size_t indices[6] = {0, 1, 2, 1, 2, 3}; diff --git a/engine/src/flutter/impeller/entity/contents/atlas_contents.h b/engine/src/flutter/impeller/entity/contents/atlas_contents.h index 2313f3402f..ba451d20f2 100644 --- a/engine/src/flutter/impeller/entity/contents/atlas_contents.h +++ b/engine/src/flutter/impeller/entity/contents/atlas_contents.h @@ -66,6 +66,7 @@ class AtlasContents final : public Contents { std::optional cull_rect_; Scalar alpha_ = 1.0; SamplerDescriptor sampler_descriptor_ = {}; + mutable std::optional bounding_box_cache_; FML_DISALLOW_COPY_AND_ASSIGN(AtlasContents); }; diff --git a/engine/src/flutter/impeller/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/geometry/geometry_unittests.cc index a98b922bc1..8edada62ea 100644 --- a/engine/src/flutter/impeller/geometry/geometry_unittests.cc +++ b/engine/src/flutter/impeller/geometry/geometry_unittests.cc @@ -1553,13 +1553,14 @@ TEST(GeometryTest, RectGetTransformedPoints) { TEST(GeometryTest, RectMakePointBounds) { { - Rect r = - Rect::MakePointBounds({Point(1, 5), Point(4, -1), Point(0, 6)}).value(); + std::vector points{{1, 5}, {4, -1}, {0, 6}}; + Rect r = Rect::MakePointBounds(points.begin(), points.end()).value(); auto expected = Rect(0, -1, 4, 7); ASSERT_RECT_NEAR(r, expected); } { - std::optional r = Rect::MakePointBounds({}); + std::vector points; + std::optional r = Rect::MakePointBounds(points.begin(), points.end()); ASSERT_FALSE(r.has_value()); } } diff --git a/engine/src/flutter/impeller/geometry/rect.h b/engine/src/flutter/impeller/geometry/rect.h index 94a5b6d6f9..4dd4107107 100644 --- a/engine/src/flutter/impeller/geometry/rect.h +++ b/engine/src/flutter/impeller/geometry/rect.h @@ -53,22 +53,21 @@ struct TRect { return TRect(0.0, 0.0, size.width, size.height); } - constexpr static std::optional MakePointBounds( - const std::vector>& points) { - if (points.empty()) { + template + constexpr static std::optional MakePointBounds(const PointIter first, + const PointIter last) { + if (first == last) { return std::nullopt; } - auto left = points[0].x; - auto top = points[0].y; - auto right = points[0].x; - auto bottom = points[0].y; - if (points.size() > 1) { - for (size_t i = 1; i < points.size(); i++) { - left = std::min(left, points[i].x); - top = std::min(top, points[i].y); - right = std::max(right, points[i].x); - bottom = std::max(bottom, points[i].y); - } + auto left = first->x; + auto top = first->y; + auto right = first->x; + auto bottom = first->y; + for (auto it = first + 1; it < last; ++it) { + left = std::min(left, it->x); + top = std::min(top, it->y); + right = std::max(right, it->x); + bottom = std::max(bottom, it->y); } return TRect::MakeLTRB(left, top, right, bottom); } @@ -175,7 +174,7 @@ struct TRect { /// rectangle. constexpr TRect TransformBounds(const Matrix& transform) const { auto points = GetTransformedPoints(transform); - return TRect::MakePointBounds({points.begin(), points.end()}).value(); + return TRect::MakePointBounds(points.begin(), points.end()).value(); } constexpr TRect Union(const TRect& o) const {