diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index be11f22006..99b64efc1a 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -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 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(); diff --git a/engine/src/flutter/impeller/aiks/paint.cc b/engine/src/flutter/impeller/aiks/paint.cc index 1702cb9f72..8b5487e3c2 100644 --- a/engine/src/flutter/impeller/aiks/paint.cc +++ b/engine/src/flutter/impeller/aiks/paint.cc @@ -14,6 +14,7 @@ std::shared_ptr Paint::CreateContentsForEntity(Path path, auto& source = color_source.value(); auto contents = source(); contents->SetPath(std::move(path)); + contents->SetAlpha(color.alpha); return contents; } diff --git a/engine/src/flutter/impeller/entity/contents/color_source_contents.cc b/engine/src/flutter/impeller/entity/contents/color_source_contents.cc index 3a8f26a29e..69add3ea15 100644 --- a/engine/src/flutter/impeller/entity/contents/color_source_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/color_source_contents.cc @@ -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(); } diff --git a/engine/src/flutter/impeller/entity/contents/color_source_contents.h b/engine/src/flutter/impeller/entity/contents/color_source_contents.h index 94386bd111..3f2f4268df 100644 --- a/engine/src/flutter/impeller/entity/contents/color_source_contents.h +++ b/engine/src/flutter/impeller/entity/contents/color_source_contents.h @@ -21,6 +21,8 @@ class ColorSourceContents : public Contents { void SetMatrix(Matrix matrix); + void SetAlpha(Scalar alpha); + // |Contents| std::optional 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); }; diff --git a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc index 9144410225..bf639ac839 100644 --- a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc @@ -80,6 +80,7 @@ bool LinearGradientContents::Render(const ContentContext& renderer, gradient_info.tile_mode = static_cast(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()) * diff --git a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc index f8bffbdbb4..15fcb20271 100644 --- a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc @@ -78,6 +78,7 @@ bool RadialGradientContents::Render(const ContentContext& renderer, gradient_info.tile_mode = static_cast(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()) * diff --git a/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc index 5e4e9f1a06..7be8162ed0 100644 --- a/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc @@ -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(tile_mode_); + gradient_info.alpha = GetAlpha(); VS::FrameInfo frame_info; frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * diff --git a/engine/src/flutter/impeller/entity/shaders/linear_gradient_fill.frag b/engine/src/flutter/impeller/entity/shaders/linear_gradient_fill.frag index 77c03fbd28..8067182ab6 100644 --- a/engine/src/flutter/impeller/entity/shaders/linear_gradient_fill.frag +++ b/engine/src/flutter/impeller/entity/shaders/linear_gradient_fill.frag @@ -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; } diff --git a/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.frag b/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.frag index c400fef17a..541b3a6b09 100644 --- a/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.frag +++ b/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.frag @@ -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; } diff --git a/engine/src/flutter/impeller/entity/shaders/sweep_gradient_fill.frag b/engine/src/flutter/impeller/entity/shaders/sweep_gradient_fill.frag index 08733dc3f6..c6d9ed38a3 100644 --- a/engine/src/flutter/impeller/entity/shaders/sweep_gradient_fill.frag +++ b/engine/src/flutter/impeller/entity/shaders/sweep_gradient_fill.frag @@ -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; } diff --git a/engine/src/flutter/impeller/geometry/gradient.cc b/engine/src/flutter/impeller/geometry/gradient.cc index 6859fa0ab1..9978df0e1f 100644 --- a/engine/src/flutter/impeller/geometry/gradient.cc +++ b/engine/src/flutter/impeller/geometry/gradient.cc @@ -59,10 +59,9 @@ 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; + 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 CreateGradientBuffer(const std::vector& 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.