[Impeller] Insert implied leading and trailing stops if missing, correct off by one in gradient calculation (flutter/engine#36227)

This commit is contained in:
Jonah Williams
2022-09-28 00:03:08 -07:00
committed by GitHub
parent e59ef6d79c
commit 034e72b49a
6 changed files with 74 additions and 54 deletions

View File

@@ -290,6 +290,30 @@ static std::vector<Matrix> 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 <typename T>
static void ConvertStops(T* gradient,
std::vector<Color>* colors,
std::vector<float>* 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<Color> colors;
std::vector<float> 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<Color> colors;
std::vector<float> 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<Color> colors;
std::vector<Scalar> 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<float> 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,

View File

@@ -19,18 +19,16 @@ std::shared_ptr<Texture> CreateGradientTexture(
const std::vector<Color>& colors,
const std::vector<Scalar>& stops,
std::shared_ptr<impeller::Context> 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<Texture> CreateGradientTexture(
return nullptr;
}
auto mapping = std::make_shared<fml::DataMapping>(color_stop_channels);
auto mapping = std::make_shared<fml::DataMapping>(gradient_data.color_bytes);
if (!texture->SetContents(mapping)) {
FML_DLOG(ERROR) << "Could not copy contents into Impeller texture.";
return nullptr;

View File

@@ -42,6 +42,8 @@ impeller_component("geometry") {
"vertices.cc",
"vertices.h",
]
deps = [ "//flutter/fml" ]
}
impeller_component("geometry_unittests") {

View File

@@ -1650,12 +1650,11 @@ TEST(GeometryTest, Gradient) {
// values.
std::vector<Color> colors = {Color::Red(), Color::Blue()};
std::vector<Scalar> 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<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);
auto gradient = CreateGradientBuffer(colors, stops);
ASSERT_EQ(gradient.texture_size, 5u);
}
{
@@ -1675,21 +1673,19 @@ TEST(GeometryTest, Gradient) {
std::vector<Color> colors = {Color::Red(), Color::Blue(), Color::Green(),
Color::White()};
std::vector<Scalar> 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<Color> colors = {Color::Red(), Color::Blue(), Color::Green()};
std::vector<Scalar> 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<Color> 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);
}
}

View File

@@ -4,6 +4,7 @@
#include <algorithm>
#include "flutter/fml/logging.h"
#include "impeller/geometry/gradient.h"
namespace impeller {
@@ -16,14 +17,13 @@ static void AppendColor(const Color& color, std::vector<uint8_t>* colors) {
colors->push_back(converted[3]);
}
std::vector<uint8_t> CreateGradientBuffer(const std::vector<Color>& colors,
const std::vector<Scalar>& stops,
uint32_t* out_texture_size) {
GradientData CreateGradientBuffer(const std::vector<Color>& colors,
const std::vector<Scalar>& 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<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);
@@ -60,7 +59,7 @@ std::vector<uint8_t> CreateGradientBuffer(const std::vector<Color>& 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<uint8_t> CreateGradientBuffer(const std::vector<Color>& 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

View File

@@ -13,16 +13,21 @@
namespace impeller {
// If texture_size is 0 then the gradient is invalid.
struct GradientData {
std::vector<uint8_t> 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<u_int8_t>
* @return GradientData
*/
std::vector<uint8_t> CreateGradientBuffer(const std::vector<Color>& colors,
const std::vector<Scalar>& stops,
uint32_t* out_texture_size);
GradientData CreateGradientBuffer(const std::vector<Color>& colors,
const std::vector<Scalar>& stops);
} // namespace impeller