diff --git a/impeller/geometry/geometry_unittests.cc b/impeller/geometry/geometry_unittests.cc index 9b64c1d834bc3..8f35c4f87b3ce 100644 --- a/impeller/geometry/geometry_unittests.cc +++ b/impeller/geometry/geometry_unittests.cc @@ -1451,6 +1451,17 @@ TEST(GeometryTest, Gradient) { ASSERT_EQ(texture_size, 2u); } + { + // Gradient with duplicate stops does not create an empty texture. + std::vector colors = {Color::Red(), Color::Yellow(), Color::Black(), + Color::Blue()}; + std::vector stops = {0.0, 0.25, 0.25, 1.0}; + uint32_t texture_size; + + auto gradient = CreateGradientBuffer(colors, stops, &texture_size); + ASSERT_EQ(texture_size, 5u); + } + { // Simple N color gradient produces color buffer containing exactly those // values. @@ -1488,9 +1499,9 @@ TEST(GeometryTest, Gradient) { // Gradient size is capped at 1024. std::vector colors = {}; std::vector stops = {}; - for (auto i = 0u; i < 2000; i++) { + for (auto i = 0u; i < 1025; i++) { colors.push_back(Color::Blue()); - stops.push_back(i / 2000.0); + stops.push_back(i / 1025.0); } stops[1999] = 1.0; diff --git a/impeller/geometry/gradient.cc b/impeller/geometry/gradient.cc index 9978df0e1f8e1..8be11718b631e 100644 --- a/impeller/geometry/gradient.cc +++ b/impeller/geometry/gradient.cc @@ -22,14 +22,16 @@ std::vector CreateGradientBuffer(const std::vector& colors, uint32_t texture_size; // TODO(jonahwilliams): we should add a display list flag to check if the // stops were provided or not, then we can skip this step. - // TODO(jonahwilliams): Skia has a check for stop sizes below a certain - // threshold, we should make sure that we behave reasonably with them. if (stops.size() == 2) { texture_size = 2; } else { auto minimum_delta = 1.0; for (size_t i = 1; i < stops.size(); i++) { auto value = stops[i] - stops[i - 1]; + // Smaller than kEhCloseEnough + if (value < 0.0001) { + continue; + } if (value < minimum_delta) { minimum_delta = value; } @@ -41,7 +43,6 @@ std::vector CreateGradientBuffer(const std::vector& colors, texture_size = std::min((uint32_t)std::round(1.0 / minimum_delta) + 1, 1024u); } - *out_texture_size = texture_size; std::vector color_stop_channels; color_stop_channels.reserve(texture_size * 4);