Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions impeller/geometry/geometry_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,17 @@ TEST(GeometryTest, Gradient) {
ASSERT_EQ(texture_size, 2u);
}

{
// Gradient with duplicate stops does not create an empty texture.
std::vector<Color> colors = {Color::Red(), Color::Yellow(), Color::Black(),
Color::Blue()};
std::vector<Scalar> 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.
Expand Down Expand Up @@ -1488,9 +1499,9 @@ TEST(GeometryTest, Gradient) {
// Gradient size is capped at 1024.
std::vector<Color> colors = {};
std::vector<Scalar> 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;

Expand Down
7 changes: 4 additions & 3 deletions impeller/geometry/gradient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ std::vector<uint8_t> CreateGradientBuffer(const std::vector<Color>& 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;
}
Expand All @@ -41,7 +43,6 @@ std::vector<uint8_t> CreateGradientBuffer(const std::vector<Color>& colors,
texture_size =
std::min((uint32_t)std::round(1.0 / minimum_delta) + 1, 1024u);
}

*out_texture_size = texture_size;
std::vector<uint8_t> color_stop_channels;
color_stop_channels.reserve(texture_size * 4);
Expand Down