[Impeller] tighten fast gradient condition. (flutter/engine#53195)

If the start/end point are _inside_ the rectangle, we can't apply the fast gradient condition.
This commit is contained in:
Jonah Williams
2024-06-04 13:15:02 -07:00
committed by GitHub
parent 8829c96265
commit aad95e521e
4 changed files with 69 additions and 3 deletions

View File

@@ -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<Color> colors = {Color::Red(), Color::Blue(), Color::Green()};
std::vector<Scalar> 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

View File

@@ -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<Rect> 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()) {

View File

@@ -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<Color> colors_;

View File

@@ -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