diff --git a/engine/src/flutter/impeller/aiks/aiks_gradient_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_gradient_unittests.cc index 8fa05e75db..ce0e8d31a7 100644 --- a/engine/src/flutter/impeller/aiks/aiks_gradient_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_gradient_unittests.cc @@ -818,5 +818,27 @@ TEST_P(AiksTest, FastGradientTestVerticalReversed) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } +TEST_P(AiksTest, VerifyNonOptimizedGradient) { + Canvas canvas; + Paint paint; + canvas.Translate({100.0f, 0, 0}); + + std::vector colors = {Color::Red(), Color::Blue(), Color::Green()}; + std::vector stops = {0.0, 0.1, 1.0}; + + // Inset the start and end point to verify that we do not apply + // the fast gradient condition. + paint.color_source = ColorSource::MakeLinearGradient( + {0, 150}, {0, 100}, std::move(colors), std::move(stops), + Entity::TileMode::kRepeat, {}); + + paint.color = Color(1.0, 1.0, 1.0, 1.0); + canvas.DrawRect(Rect::MakeXYWH(0, 0, 300, 300), paint); + canvas.Translate({400, 0, 0}); + canvas.DrawRRect(Rect::MakeXYWH(0, 0, 300, 300), Size(4, 4), paint); + + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc index 1389da6fe0..44cd3c1c46 100644 --- a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc @@ -8,6 +8,7 @@ #include "impeller/entity/contents/content_context.h" #include "impeller/entity/contents/gradient_generator.h" #include "impeller/entity/entity.h" +#include "impeller/geometry/scalar.h" #include "impeller/renderer/render_pass.h" #include "impeller/renderer/vertex_buffer_builder.h" @@ -54,6 +55,46 @@ bool LinearGradientContents::IsOpaque() const { return true; } +bool LinearGradientContents::CanApplyFastGradient() const { + if (!GetGeometry()->IsAxisAlignedRect() || + !GetInverseEffectTransform().IsIdentity()) { + return false; + } + std::optional maybe_rect = GetGeometry()->GetCoverage(Matrix()); + if (!maybe_rect.has_value()) { + return false; + } + Rect rect = maybe_rect.value(); + + if (ScalarNearlyEqual(start_point_.x, end_point_.x)) { + // Sort start and end to make on-rect comparisons easier. + Point start = (start_point_.y < end_point_.y) ? start_point_ : end_point_; + Point end = (start_point_.y < end_point_.y) ? end_point_ : start_point_; + // The exact x positon doesn't matter for a vertical gradient, but the y + // position must be nearly on the rectangle. + if (ScalarNearlyEqual(start.y, rect.GetTop()) && + ScalarNearlyEqual(end.y, rect.GetBottom())) { + return true; + } + return false; + } + + if (ScalarNearlyEqual(start_point_.y, end_point_.y)) { + // Sort start and end to make on-rect comparisons easier. + Point start = (start_point_.x < end_point_.x) ? start_point_ : end_point_; + Point end = (start_point_.x < end_point_.x) ? end_point_ : start_point_; + // The exact y positon doesn't matter for a horizontal gradient, but the x + // position must be nearly on the rectangle. + if (ScalarNearlyEqual(start.x, rect.GetLeft()) && + ScalarNearlyEqual(end.x, rect.GetRight())) { + return true; + } + return false; + } + + return false; +} + // A much faster (in terms of ALU) linear gradient that uses vertex // interpolation to perform all color computation. Requires that the geometry of // the gradient is divided into regions based on the stop values. @@ -137,9 +178,7 @@ bool LinearGradientContents::Render(const ContentContext& renderer, // TODO(148651): The fast path is overly restrictive, following the design in // https://github.com/flutter/flutter/issues/148651 support for more cases can // be gradually added. - if (GetGeometry()->IsAxisAlignedRect() && - (start_point_.x == end_point_.x || start_point_.y == end_point_.y) && - GetInverseEffectTransform().IsIdentity()) { + if (CanApplyFastGradient()) { return FastLinearGradient(renderer, entity, pass); } if (renderer.GetDeviceCapabilities().SupportsSSBO()) { diff --git a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h index c1b94c6db4..e39d9aae8e 100644 --- a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h +++ b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h @@ -57,6 +57,8 @@ class LinearGradientContents final : public ColorSourceContents { const Entity& entity, RenderPass& pass) const; + bool CanApplyFastGradient() const; + Point start_point_; Point end_point_; std::vector colors_; diff --git a/engine/src/flutter/testing/impeller_golden_tests_output.txt b/engine/src/flutter/testing/impeller_golden_tests_output.txt index 3f5e0434d1..c3c1caba83 100644 --- a/engine/src/flutter/testing/impeller_golden_tests_output.txt +++ b/engine/src/flutter/testing/impeller_golden_tests_output.txt @@ -789,6 +789,9 @@ impeller_Play_AiksTest_TranslucentSaveLayerWithColorMatrixColorFilterDrawsCorrec impeller_Play_AiksTest_TranslucentSaveLayerWithColorMatrixImageFilterDrawsCorrectly_Metal.png impeller_Play_AiksTest_TranslucentSaveLayerWithColorMatrixImageFilterDrawsCorrectly_OpenGLES.png impeller_Play_AiksTest_TranslucentSaveLayerWithColorMatrixImageFilterDrawsCorrectly_Vulkan.png +impeller_Play_AiksTest_VerifyNonOptimizedGradient_Metal.png +impeller_Play_AiksTest_VerifyNonOptimizedGradient_OpenGLES.png +impeller_Play_AiksTest_VerifyNonOptimizedGradient_Vulkan.png impeller_Play_AiksTest_VerticesGeometryColorUVPositionDataAdvancedBlend_Metal.png impeller_Play_AiksTest_VerticesGeometryColorUVPositionDataAdvancedBlend_OpenGLES.png impeller_Play_AiksTest_VerticesGeometryColorUVPositionDataAdvancedBlend_Vulkan.png