[Impeller] Support alpha in Gradients, fix uneven stops. (flutter/engine#36100)
This commit is contained in:
@@ -313,7 +313,9 @@ TEST_P(AiksTest, CanRenderLinearGradient) {
|
||||
Entity::TileMode::kMirror, Entity::TileMode::kDecal};
|
||||
|
||||
static int selected_tile_mode = 0;
|
||||
static float alpha = 1;
|
||||
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
|
||||
ImGui::SliderFloat("Alpha", &alpha, 0, 1);
|
||||
ImGui::Combo("Tile mode", &selected_tile_mode, tile_mode_names,
|
||||
sizeof(tile_mode_names) / sizeof(char*));
|
||||
static Matrix matrix = {
|
||||
@@ -347,6 +349,7 @@ TEST_P(AiksTest, CanRenderLinearGradient) {
|
||||
contents->SetMatrix(matrix);
|
||||
return contents;
|
||||
};
|
||||
paint.color = Color(1.0, 1.0, 1.0, alpha);
|
||||
canvas.DrawRect({0, 0, 600, 600}, paint);
|
||||
return renderer.Render(canvas.EndRecordingAsPicture(), render_target);
|
||||
};
|
||||
@@ -368,7 +371,9 @@ TEST_P(AiksTest, CanRenderLinearGradientManyColors) {
|
||||
Entity::TileMode::kMirror, Entity::TileMode::kDecal};
|
||||
|
||||
static int selected_tile_mode = 0;
|
||||
static float alpha = 1;
|
||||
ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
|
||||
ImGui::SliderFloat("Alpha", &alpha, 0, 1);
|
||||
ImGui::Combo("Tile mode", &selected_tile_mode, tile_mode_names,
|
||||
sizeof(tile_mode_names) / sizeof(char*));
|
||||
static Matrix matrix = {
|
||||
@@ -417,6 +422,7 @@ TEST_P(AiksTest, CanRenderLinearGradientManyColors) {
|
||||
contents->SetMatrix(matrix);
|
||||
return contents;
|
||||
};
|
||||
paint.color = Color(1.0, 1.0, 1.0, alpha);
|
||||
canvas.DrawRect({0, 0, 600, 600}, paint);
|
||||
return renderer.Render(canvas.EndRecordingAsPicture(), render_target);
|
||||
};
|
||||
@@ -533,8 +539,8 @@ TEST_P(AiksTest, CanRenderLinearGradientManyColorsUnevenStops) {
|
||||
Color{0xf3 / 255.0, 0x90 / 255.0, 0x60 / 255.0, 1.0},
|
||||
Color{0xff / 255.0, 0xb5 / 255.0, 0x6b / 250.0, 1.0}};
|
||||
std::vector<Scalar> stops = {
|
||||
0.0 / 126.0, 2.0 / 126.0, 4.0 / 126.0, 8.0 / 126.0,
|
||||
16.0 / 126.0, 32.0 / 126.0, 1.0,
|
||||
0.0, 2.0 / 62.0, 4.0 / 62.0, 8.0 / 62.0,
|
||||
16.0 / 62.0, 32.0 / 62.0, 1.0,
|
||||
};
|
||||
|
||||
auto contents = std::make_shared<LinearGradientContents>();
|
||||
|
||||
@@ -14,6 +14,7 @@ std::shared_ptr<Contents> Paint::CreateContentsForEntity(Path path,
|
||||
auto& source = color_source.value();
|
||||
auto contents = source();
|
||||
contents->SetPath(std::move(path));
|
||||
contents->SetAlpha(color.alpha);
|
||||
return contents;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,14 @@ const Path& ColorSourceContents::GetPath() const {
|
||||
return path_;
|
||||
}
|
||||
|
||||
void ColorSourceContents::SetAlpha(Scalar alpha) {
|
||||
alpha_ = alpha;
|
||||
}
|
||||
|
||||
Scalar ColorSourceContents::GetAlpha() const {
|
||||
return alpha_;
|
||||
}
|
||||
|
||||
void ColorSourceContents::SetMatrix(Matrix matrix) {
|
||||
inverse_matrix_ = matrix.Invert();
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ class ColorSourceContents : public Contents {
|
||||
|
||||
void SetMatrix(Matrix matrix);
|
||||
|
||||
void SetAlpha(Scalar alpha);
|
||||
|
||||
// |Contents|
|
||||
std::optional<Rect> GetCoverage(const Entity& entity) const override;
|
||||
|
||||
@@ -29,9 +31,12 @@ class ColorSourceContents : public Contents {
|
||||
|
||||
const Matrix& GetInverseMatrix() const;
|
||||
|
||||
Scalar GetAlpha() const;
|
||||
|
||||
private:
|
||||
Path path_;
|
||||
Matrix inverse_matrix_;
|
||||
Scalar alpha_ = 1.0;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ColorSourceContents);
|
||||
};
|
||||
|
||||
@@ -80,6 +80,7 @@ bool LinearGradientContents::Render(const ContentContext& renderer,
|
||||
gradient_info.tile_mode = static_cast<Scalar>(tile_mode_);
|
||||
gradient_info.texture_sampler_y_coord_scale =
|
||||
gradient_texture->GetYCoordScale();
|
||||
gradient_info.alpha = GetAlpha();
|
||||
|
||||
VS::FrameInfo frame_info;
|
||||
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
|
||||
|
||||
@@ -78,6 +78,7 @@ bool RadialGradientContents::Render(const ContentContext& renderer,
|
||||
gradient_info.tile_mode = static_cast<Scalar>(tile_mode_);
|
||||
gradient_info.texture_sampler_y_coord_scale =
|
||||
gradient_texture->GetYCoordScale();
|
||||
gradient_info.alpha = GetAlpha();
|
||||
|
||||
VS::FrameInfo frame_info;
|
||||
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
|
||||
|
||||
@@ -86,6 +86,7 @@ bool SweepGradientContents::Render(const ContentContext& renderer,
|
||||
gradient_info.texture_sampler_y_coord_scale =
|
||||
gradient_texture->GetYCoordScale();
|
||||
gradient_info.tile_mode = static_cast<Scalar>(tile_mode_);
|
||||
gradient_info.alpha = GetAlpha();
|
||||
|
||||
VS::FrameInfo frame_info;
|
||||
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
|
||||
|
||||
@@ -11,6 +11,7 @@ uniform GradientInfo {
|
||||
vec2 end_point;
|
||||
float tile_mode;
|
||||
float texture_sampler_y_coord_scale;
|
||||
float alpha;
|
||||
} gradient_info;
|
||||
|
||||
in vec2 v_position;
|
||||
@@ -30,5 +31,5 @@ void main() {
|
||||
gradient_info.texture_sampler_y_coord_scale,
|
||||
gradient_info.tile_mode,
|
||||
gradient_info.tile_mode);
|
||||
frag_color = vec4(frag_color.xyz * frag_color.a, frag_color.a);
|
||||
frag_color = vec4(frag_color.xyz * frag_color.a, frag_color.a) * gradient_info.alpha;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ uniform GradientInfo {
|
||||
float radius;
|
||||
float tile_mode;
|
||||
float texture_sampler_y_coord_scale;
|
||||
float alpha;
|
||||
} gradient_info;
|
||||
|
||||
in vec2 v_position;
|
||||
@@ -26,5 +27,5 @@ void main() {
|
||||
gradient_info.texture_sampler_y_coord_scale,
|
||||
gradient_info.tile_mode,
|
||||
gradient_info.tile_mode);
|
||||
frag_color = vec4(frag_color.xyz * frag_color.a, frag_color.a);
|
||||
frag_color = vec4(frag_color.xyz * frag_color.a, frag_color.a) * gradient_info.alpha;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ uniform GradientInfo {
|
||||
float scale;
|
||||
float tile_mode;
|
||||
float texture_sampler_y_coord_scale;
|
||||
float alpha;
|
||||
} gradient_info;
|
||||
|
||||
in vec2 v_position;
|
||||
@@ -30,5 +31,5 @@ void main() {
|
||||
gradient_info.texture_sampler_y_coord_scale,
|
||||
gradient_info.tile_mode,
|
||||
gradient_info.tile_mode);
|
||||
frag_color = vec4(frag_color.xyz * frag_color.a, frag_color.a);
|
||||
frag_color = vec4(frag_color.xyz * frag_color.a, frag_color.a) * gradient_info.alpha;
|
||||
}
|
||||
|
||||
@@ -59,10 +59,9 @@ 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;
|
||||
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.
|
||||
if (ScalarNearlyEqual(scaled_i, next_stop)) {
|
||||
AppendColor(next_color, &color_stop_channels);
|
||||
@@ -77,16 +76,17 @@ std::vector<uint8_t> CreateGradientBuffer(const std::vector<Color>& colors,
|
||||
|
||||
AppendColor(mixed_color, &color_stop_channels);
|
||||
} else {
|
||||
// We've slightly overshot the next stop. In theory this only happens if
|
||||
// we have scaled our texture such that not every stop gets their own
|
||||
// index. For now I am simply ignoring the inbetween colors. Currently
|
||||
// this requires a gradient with either an absurd number of textures
|
||||
// or very small stops.
|
||||
AppendColor(next_color, &color_stop_channels);
|
||||
|
||||
// We've slightly overshot the previous stop.
|
||||
previous_color = next_color;
|
||||
previous_stop = next_stop;
|
||||
previous_color_index += 1;
|
||||
next_color = colors[previous_color_index + 1];
|
||||
auto next_stop = stops[previous_color_index + 1];
|
||||
|
||||
auto t = (scaled_i - previous_stop) / (next_stop - previous_stop);
|
||||
auto mixed_color = Color::lerp(previous_color, next_color, t);
|
||||
|
||||
AppendColor(mixed_color, &color_stop_channels);
|
||||
}
|
||||
}
|
||||
// The last index is always equal to the last color, exactly.
|
||||
|
||||
Reference in New Issue
Block a user