From 034e72b49af1d1ae79bb46844254b7c859235516 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 28 Sep 2022 00:03:08 -0700 Subject: [PATCH] [Impeller] Insert implied leading and trailing stops if missing, correct off by one in gradient calculation (flutter/engine#36227) --- .../display_list/display_list_dispatcher.cc | 44 +++++++++++++------ .../entity/contents/gradient_generator.cc | 18 ++++---- engine/src/flutter/impeller/geometry/BUILD.gn | 2 + .../impeller/geometry/geometry_unittests.cc | 31 ++++++------- .../src/flutter/impeller/geometry/gradient.cc | 20 +++++---- .../src/flutter/impeller/geometry/gradient.h | 13 ++++-- 6 files changed, 74 insertions(+), 54 deletions(-) diff --git a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc index ad2eb4286d..1da0db438c 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -290,6 +290,30 @@ static std::vector ToRSXForms(const SkRSXform xform[], int count) { return result; } +// Convert display list colors + stops into impeller colors and stops, taking +// care to ensure that the stops always start with 0.0 and end with 1.0. +template +static void ConvertStops(T* gradient, + std::vector* colors, + std::vector* stops) { + FML_DCHECK(gradient->stop_count() >= 2); + + auto* dl_colors = gradient->colors(); + auto* dl_stops = gradient->stops(); + if (dl_stops[0] != 0.0) { + colors->emplace_back(ToColor(dl_colors[0])); + stops->emplace_back(0); + } + for (auto i = 0; i < gradient->stop_count(); i++) { + colors->emplace_back(ToColor(dl_colors[i])); + stops->emplace_back(dl_stops[i]); + } + if (stops->back() != 1.0) { + colors->emplace_back(colors->back()); + stops->emplace_back(1.0); + } +} + // |flutter::Dispatcher| void DisplayListDispatcher::setColorSource( const flutter::DlColorSource* source) { @@ -314,10 +338,8 @@ void DisplayListDispatcher::setColorSource( auto end_point = ToPoint(linear->end_point()); std::vector colors; std::vector stops; - for (auto i = 0; i < linear->stop_count(); i++) { - colors.emplace_back(ToColor(linear->colors()[i])); - stops.emplace_back(linear->stops()[i]); - } + ConvertStops(linear, &colors, &stops); + auto tile_mode = ToTileMode(linear->tile_mode()); auto matrix = ToMatrix(linear->matrix()); paint_.color_source = [start_point, end_point, colors = std::move(colors), @@ -340,10 +362,8 @@ void DisplayListDispatcher::setColorSource( auto radius = radialGradient->radius(); std::vector colors; std::vector stops; - for (auto i = 0; i < radialGradient->stop_count(); i++) { - colors.emplace_back(ToColor(radialGradient->colors()[i])); - stops.emplace_back(radialGradient->stops()[i]); - } + ConvertStops(radialGradient, &colors, &stops); + auto tile_mode = ToTileMode(radialGradient->tile_mode()); auto matrix = ToMatrix(radialGradient->matrix()); paint_.color_source = [center, radius, colors = std::move(colors), @@ -367,11 +387,9 @@ void DisplayListDispatcher::setColorSource( auto start_angle = Degrees(sweepGradient->start()); auto end_angle = Degrees(sweepGradient->end()); std::vector colors; - std::vector stops; - for (auto i = 0; i < sweepGradient->stop_count(); i++) { - colors.emplace_back(ToColor(sweepGradient->colors()[i])); - stops.emplace_back(sweepGradient->stops()[i]); - } + std::vector stops; + ConvertStops(sweepGradient, &colors, &stops); + auto tile_mode = ToTileMode(sweepGradient->tile_mode()); auto matrix = ToMatrix(sweepGradient->matrix()); paint_.color_source = [center, start_angle, end_angle, diff --git a/engine/src/flutter/impeller/entity/contents/gradient_generator.cc b/engine/src/flutter/impeller/entity/contents/gradient_generator.cc index bfb0afff25..c66afc3a9d 100644 --- a/engine/src/flutter/impeller/entity/contents/gradient_generator.cc +++ b/engine/src/flutter/impeller/entity/contents/gradient_generator.cc @@ -19,18 +19,16 @@ std::shared_ptr CreateGradientTexture( const std::vector& colors, const std::vector& stops, std::shared_ptr context) { - // If the computed scale is nearly the same as the color length, then the - // stops are evenly spaced and we can lerp entirely in the gradient shader. - // Thus we only need to populate a texture with all of the colors in order. - // For other cases, we may have more colors than we can fit in the texture, - // or we may have very small stop values. For these gradients the lerped - // values are computed here and then populated in a texture. - uint32_t texture_size; - auto color_stop_channels = CreateGradientBuffer(colors, stops, &texture_size); + auto gradient_data = CreateGradientBuffer(colors, stops); + if (gradient_data.texture_size == 0) { + FML_DLOG(ERROR) << "Invalid gradient data."; + return nullptr; + } + impeller::TextureDescriptor texture_descriptor; texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible; texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt; - texture_descriptor.size = {texture_size, 1}; + texture_descriptor.size = {gradient_data.texture_size, 1}; auto texture = context->GetResourceAllocator()->CreateTexture(texture_descriptor); if (!texture) { @@ -38,7 +36,7 @@ std::shared_ptr CreateGradientTexture( return nullptr; } - auto mapping = std::make_shared(color_stop_channels); + auto mapping = std::make_shared(gradient_data.color_bytes); if (!texture->SetContents(mapping)) { FML_DLOG(ERROR) << "Could not copy contents into Impeller texture."; return nullptr; diff --git a/engine/src/flutter/impeller/geometry/BUILD.gn b/engine/src/flutter/impeller/geometry/BUILD.gn index 03865753d2..f34e00ad15 100644 --- a/engine/src/flutter/impeller/geometry/BUILD.gn +++ b/engine/src/flutter/impeller/geometry/BUILD.gn @@ -42,6 +42,8 @@ impeller_component("geometry") { "vertices.cc", "vertices.h", ] + + deps = [ "//flutter/fml" ] } impeller_component("geometry_unittests") { diff --git a/engine/src/flutter/impeller/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/geometry/geometry_unittests.cc index f2fed24cf4..a199c689d7 100644 --- a/engine/src/flutter/impeller/geometry/geometry_unittests.cc +++ b/engine/src/flutter/impeller/geometry/geometry_unittests.cc @@ -1650,12 +1650,11 @@ TEST(GeometryTest, Gradient) { // values. std::vector colors = {Color::Red(), Color::Blue()}; std::vector stops = {0.0, 1.0}; - uint32_t texture_size; - auto gradient = CreateGradientBuffer(colors, stops, &texture_size); + auto gradient = CreateGradientBuffer(colors, stops); - ASSERT_COLOR_BUFFER_NEAR(gradient, colors); - ASSERT_EQ(texture_size, 2u); + ASSERT_COLOR_BUFFER_NEAR(gradient.color_bytes, colors); + ASSERT_EQ(gradient.texture_size, 2u); } { @@ -1663,10 +1662,9 @@ TEST(GeometryTest, Gradient) { 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); + auto gradient = CreateGradientBuffer(colors, stops); + ASSERT_EQ(gradient.texture_size, 5u); } { @@ -1675,21 +1673,19 @@ TEST(GeometryTest, Gradient) { std::vector colors = {Color::Red(), Color::Blue(), Color::Green(), Color::White()}; std::vector stops = {0.0, 0.33, 0.66, 1.0}; - uint32_t texture_size; - auto gradient = CreateGradientBuffer(colors, stops, &texture_size); + auto gradient = CreateGradientBuffer(colors, stops); - ASSERT_COLOR_BUFFER_NEAR(gradient, colors); - ASSERT_EQ(texture_size, 4u); + ASSERT_COLOR_BUFFER_NEAR(gradient.color_bytes, colors); + ASSERT_EQ(gradient.texture_size, 4u); } { // Gradient with color stops will lerp and scale buffer. std::vector colors = {Color::Red(), Color::Blue(), Color::Green()}; std::vector stops = {0.0, 0.25, 1.0}; - uint32_t texture_size; - auto gradient = CreateGradientBuffer(colors, stops, &texture_size); + auto gradient = CreateGradientBuffer(colors, stops); std::vector lerped_colors = { Color::Red(), @@ -1698,8 +1694,8 @@ TEST(GeometryTest, Gradient) { Color::lerp(Color::Blue(), Color::Green(), 0.6666), Color::Green(), }; - ASSERT_COLOR_BUFFER_NEAR(gradient, lerped_colors); - ASSERT_EQ(texture_size, 5u); + ASSERT_COLOR_BUFFER_NEAR(gradient.color_bytes, lerped_colors); + ASSERT_EQ(gradient.texture_size, 5u); } { @@ -1711,10 +1707,9 @@ TEST(GeometryTest, Gradient) { stops.push_back(i / 1025.0); } - uint32_t texture_size; - auto gradient = CreateGradientBuffer(colors, stops, &texture_size); + auto gradient = CreateGradientBuffer(colors, stops); - ASSERT_EQ(texture_size, 1024u); + ASSERT_EQ(gradient.texture_size, 1024u); } } diff --git a/engine/src/flutter/impeller/geometry/gradient.cc b/engine/src/flutter/impeller/geometry/gradient.cc index 8be11718b6..fc0f52b957 100644 --- a/engine/src/flutter/impeller/geometry/gradient.cc +++ b/engine/src/flutter/impeller/geometry/gradient.cc @@ -4,6 +4,7 @@ #include +#include "flutter/fml/logging.h" #include "impeller/geometry/gradient.h" namespace impeller { @@ -16,14 +17,13 @@ static void AppendColor(const Color& color, std::vector* colors) { colors->push_back(converted[3]); } -std::vector CreateGradientBuffer(const std::vector& colors, - const std::vector& stops, - uint32_t* out_texture_size) { +GradientData CreateGradientBuffer(const std::vector& colors, + const std::vector& stops) { + FML_DCHECK(stops.size() == colors.size()); + 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. if (stops.size() == 2) { - texture_size = 2; + texture_size = colors.size(); } else { auto minimum_delta = 1.0; for (size_t i = 1; i < stops.size(); i++) { @@ -43,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); @@ -60,7 +59,7 @@ std::vector CreateGradientBuffer(const std::vector& colors, AppendColor(previous_color, &color_stop_channels); for (auto i = 1u; i < texture_size - 1; i++) { - auto scaled_i = i / (texture_size * 1.0); + auto scaled_i = i / (texture_size - 1.0); Color next_color = colors[previous_color_index + 1]; auto next_stop = stops[previous_color_index + 1]; // We're almost exactly equal to the next stop. @@ -93,7 +92,10 @@ std::vector CreateGradientBuffer(const std::vector& colors, // The last index is always equal to the last color, exactly. AppendColor(colors.back(), &color_stop_channels); } - return color_stop_channels; + return GradientData{ + .color_bytes = std::move(color_stop_channels), + .texture_size = texture_size, + }; } } // namespace impeller diff --git a/engine/src/flutter/impeller/geometry/gradient.h b/engine/src/flutter/impeller/geometry/gradient.h index 2e99e89ba0..d9f943de77 100644 --- a/engine/src/flutter/impeller/geometry/gradient.h +++ b/engine/src/flutter/impeller/geometry/gradient.h @@ -13,16 +13,21 @@ namespace impeller { +// If texture_size is 0 then the gradient is invalid. +struct GradientData { + std::vector color_bytes; + uint32_t texture_size; +}; + /** * @brief Populate a vector with the interpolated colors for the linear gradient * described colors and stops. * * @param colors * @param stops - * @return std::vector + * @return GradientData */ -std::vector CreateGradientBuffer(const std::vector& colors, - const std::vector& stops, - uint32_t* out_texture_size); +GradientData CreateGradientBuffer(const std::vector& colors, + const std::vector& stops); } // namespace impeller