[Impeller] drawAtlas performance improvements (flutter/engine#39865)

[Impeller] drawAtlas performance improvements
This commit is contained in:
Jason Simmons
2023-02-27 09:01:41 -08:00
committed by GitHub
parent 083ab1488a
commit ad789d08f8
4 changed files with 32 additions and 26 deletions

View File

@@ -35,10 +35,12 @@ std::shared_ptr<Texture> AtlasContents::GetTexture() const {
void AtlasContents::SetTransforms(std::vector<Matrix> transforms) {
transforms_ = std::move(transforms);
bounding_box_cache_.reset();
}
void AtlasContents::SetTextureCoordinates(std::vector<Rect> texture_coords) {
texture_coords_ = std::move(texture_coords);
bounding_box_cache_.reset();
}
void AtlasContents::SetColors(std::vector<Color> colors) {
@@ -65,14 +67,17 @@ std::optional<Rect> 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<VS::PerVertexData> vertex_builder;
vertex_builder.Reserve(texture_coords.size() * 6);
constexpr size_t indices[6] = {0, 1, 2, 1, 2, 3};

View File

@@ -66,6 +66,7 @@ class AtlasContents final : public Contents {
std::optional<Rect> cull_rect_;
Scalar alpha_ = 1.0;
SamplerDescriptor sampler_descriptor_ = {};
mutable std::optional<Rect> bounding_box_cache_;
FML_DISALLOW_COPY_AND_ASSIGN(AtlasContents);
};

View File

@@ -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<Point> 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<Rect> r = Rect::MakePointBounds({});
std::vector<Point> points;
std::optional<Rect> r = Rect::MakePointBounds(points.begin(), points.end());
ASSERT_FALSE(r.has_value());
}
}

View File

@@ -53,22 +53,21 @@ struct TRect {
return TRect(0.0, 0.0, size.width, size.height);
}
constexpr static std::optional<TRect> MakePointBounds(
const std::vector<TPoint<Type>>& points) {
if (points.empty()) {
template <typename PointIter>
constexpr static std::optional<TRect> 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 {