From 9900922ec2c0bac54f9b0204e62b2b63c131e19a Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Thu, 11 Aug 2022 18:14:17 +0800 Subject: [PATCH] [impeller] Add 'ColorSourceProc' for creating color source contents (flutter/engine#35299) --- .../flutter/impeller/aiks/aiks_unittests.cc | 75 +++++++++++++------ engine/src/flutter/impeller/aiks/paint.cc | 6 +- engine/src/flutter/impeller/aiks/paint.h | 3 +- .../display_list/display_list_dispatcher.cc | 58 ++++++++------ 4 files changed, 97 insertions(+), 45 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index d4fbf8c636..5fc29d01bb 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -214,13 +214,16 @@ TEST_P(AiksTest, CanRenderLinearGradient) { for (int i = 0; i < 4; i++) { canvas.Save(); canvas.Translate(offsets[i]); - auto contents = std::make_shared(); - contents->SetEndPoints({0, 0}, {100, 100}); - std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, - Color{0.1294, 0.5882, 0.9529, 1.0}}; - contents->SetColors(std::move(colors)); - contents->SetTileMode(tile_modes[i]); - paint.contents = contents; + Entity::TileMode tile_mode = tile_modes[i]; + paint.color_source = [tile_mode]() { + auto contents = std::make_shared(); + contents->SetEndPoints({0, 0}, {100, 100}); + std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, + Color{0.1294, 0.5882, 0.9529, 1.0}}; + contents->SetColors(std::move(colors)); + contents->SetTileMode(tile_mode); + return contents; + }; canvas.DrawRect({0, 0, 200, 200}, paint); canvas.Restore(); } @@ -238,13 +241,16 @@ TEST_P(AiksTest, CanRenderRadialGradient) { for (int i = 0; i < 4; i++) { canvas.Save(); canvas.Translate(offsets[i]); - auto contents = std::make_shared(); - contents->SetCenterAndRadius({50, 50}, 50); - std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, - Color{0.1294, 0.5882, 0.9529, 1.0}}; - contents->SetColors(std::move(colors)); - contents->SetTileMode(tile_modes[i]); - paint.contents = contents; + Entity::TileMode tile_mode = tile_modes[i]; + paint.color_source = [tile_mode]() { + auto contents = std::make_shared(); + contents->SetCenterAndRadius({50, 50}, 50); + std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, + Color{0.1294, 0.5882, 0.9529, 1.0}}; + contents->SetColors(std::move(colors)); + contents->SetTileMode(tile_mode); + return contents; + }; canvas.DrawRect({0, 0, 200, 200}, paint); canvas.Restore(); } @@ -263,13 +269,16 @@ TEST_P(AiksTest, CanRenderSweepGradient) { for (int i = 0; i < 4; i++) { canvas.Save(); canvas.Translate(offsets[i]); - auto contents = std::make_shared(); - contents->SetCenterAndAngles({50, 50}, Degrees(45), Degrees(135)); - std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, - Color{0.1294, 0.5882, 0.9529, 1.0}}; - contents->SetColors(std::move(colors)); - contents->SetTileMode(tile_modes[i]); - paint.contents = contents; + Entity::TileMode tile_mode = tile_modes[i]; + paint.color_source = [tile_mode]() { + auto contents = std::make_shared(); + contents->SetCenterAndAngles({50, 50}, Degrees(45), Degrees(135)); + std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, + Color{0.1294, 0.5882, 0.9529, 1.0}}; + contents->SetColors(std::move(colors)); + contents->SetTileMode(tile_mode); + return contents; + }; canvas.DrawRect({0, 0, 200, 200}, paint); canvas.Restore(); } @@ -277,6 +286,30 @@ TEST_P(AiksTest, CanRenderSweepGradient) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } +TEST_P(AiksTest, CanRenderDifferentShapesWithSameColorSource) { + Canvas canvas; + Paint paint; + paint.color_source = []() { + auto contents = std::make_shared(); + contents->SetEndPoints({0, 0}, {100, 100}); + std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, + Color{0.1294, 0.5882, 0.9529, 1.0}}; + contents->SetColors(std::move(colors)); + contents->SetTileMode(Entity::TileMode::kRepeat); + return contents; + }; + canvas.Save(); + canvas.Translate({100, 100, 0}); + canvas.DrawRect({0, 0, 200, 200}, paint); + canvas.Restore(); + + canvas.Save(); + canvas.Translate({100, 400, 0}); + canvas.DrawCircle({100, 100}, 100, paint); + canvas.Restore(); + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + TEST_P(AiksTest, BlendModeShouldCoverWholeScreen) { Canvas canvas; Paint paint; diff --git a/engine/src/flutter/impeller/aiks/paint.cc b/engine/src/flutter/impeller/aiks/paint.cc index 2d49996f3c..0a6704de91 100644 --- a/engine/src/flutter/impeller/aiks/paint.cc +++ b/engine/src/flutter/impeller/aiks/paint.cc @@ -10,7 +10,9 @@ namespace impeller { std::shared_ptr Paint::CreateContentsForEntity(Path path, bool cover) const { - if (contents) { + if (color_source.has_value()) { + auto& source = color_source.value(); + auto contents = source(); contents->SetPath(std::move(path)); return contents; } @@ -41,7 +43,7 @@ std::shared_ptr Paint::CreateContentsForEntity(Path path, std::shared_ptr Paint::WithFilters( std::shared_ptr input, std::optional is_solid_color) const { - bool is_solid_color_val = is_solid_color.value_or(!contents); + bool is_solid_color_val = is_solid_color.value_or(!color_source); if (mask_blur_descriptor.has_value()) { input = mask_blur_descriptor->CreateMaskBlur(FilterInput::Make(input), diff --git a/engine/src/flutter/impeller/aiks/paint.h b/engine/src/flutter/impeller/aiks/paint.h index 8a70d3428e..b150c868e3 100644 --- a/engine/src/flutter/impeller/aiks/paint.h +++ b/engine/src/flutter/impeller/aiks/paint.h @@ -25,6 +25,7 @@ struct Paint { using MaskFilterProc = std::function(FilterInput::Ref, bool is_solid_color)>; + using ColorSourceProc = std::function()>; enum class Style { kFill, @@ -40,7 +41,7 @@ struct Paint { }; Color color = Color::Black(); - std::shared_ptr contents; + std::optional color_source; Scalar stroke_width = 0.0; SolidStrokeContents::Cap stroke_cap = SolidStrokeContents::Cap::kButt; 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 5525e23de0..e8dd2f38fc 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -239,14 +239,14 @@ static std::vector ToRSXForms(const SkRSXform xform[], int count) { void DisplayListDispatcher::setColorSource( const flutter::DlColorSource* source) { if (!source) { - paint_.contents = nullptr; + paint_.color_source = std::nullopt; return; } switch (source->type()) { case flutter::DlColorSourceType::kColor: { const flutter::DlColorColorSource* color = source->asColor(); - paint_.contents = nullptr; + paint_.color_source = std::nullopt; setColor(color->color()); FML_DCHECK(color); return; @@ -255,49 +255,65 @@ void DisplayListDispatcher::setColorSource( const flutter::DlLinearGradientColorSource* linear = source->asLinearGradient(); FML_DCHECK(linear); - auto contents = std::make_shared(); - contents->SetEndPoints(ToPoint(linear->start_point()), - ToPoint(linear->end_point())); + auto start_point = ToPoint(linear->start_point()); + auto end_point = ToPoint(linear->end_point()); std::vector colors; for (auto i = 0; i < linear->stop_count(); i++) { colors.emplace_back(ToColor(linear->colors()[i])); } - contents->SetColors(std::move(colors)); - contents->SetTileMode(ToTileMode(linear->tile_mode())); - paint_.contents = std::move(contents); + auto tile_mode = ToTileMode(linear->tile_mode()); + paint_.color_source = [start_point, end_point, colors = std::move(colors), + tile_mode]() { + auto contents = std::make_shared(); + contents->SetEndPoints(start_point, end_point); + contents->SetColors(std::move(colors)); + contents->SetTileMode(tile_mode); + return contents; + }; return; } case flutter::DlColorSourceType::kRadialGradient: { const flutter::DlRadialGradientColorSource* radialGradient = source->asRadialGradient(); FML_DCHECK(radialGradient); - auto contents = std::make_shared(); - contents->SetCenterAndRadius(ToPoint(radialGradient->center()), - radialGradient->radius()); + auto center = ToPoint(radialGradient->center()); + auto radius = radialGradient->radius(); std::vector colors; for (auto i = 0; i < radialGradient->stop_count(); i++) { colors.emplace_back(ToColor(radialGradient->colors()[i])); } - contents->SetColors(std::move(colors)); - contents->SetTileMode(ToTileMode(radialGradient->tile_mode())); - paint_.contents = std::move(contents); + auto tile_mode = ToTileMode(radialGradient->tile_mode()); + paint_.color_source = [center, radius, colors = std::move(colors), + tile_mode]() { + auto contents = std::make_shared(); + contents->SetCenterAndRadius(center, radius), + contents->SetColors(std::move(colors)); + contents->SetTileMode(tile_mode); + return contents; + }; return; } case flutter::DlColorSourceType::kSweepGradient: { const flutter::DlSweepGradientColorSource* sweepGradient = source->asSweepGradient(); FML_DCHECK(sweepGradient); - auto contents = std::make_shared(); - contents->SetCenterAndAngles(ToPoint(sweepGradient->center()), - Degrees(sweepGradient->start()), - Degrees(sweepGradient->end())); + + auto center = ToPoint(sweepGradient->center()); + auto start_angle = Degrees(sweepGradient->start()); + auto end_angle = Degrees(sweepGradient->end()); std::vector colors; for (auto i = 0; i < sweepGradient->stop_count(); i++) { colors.emplace_back(ToColor(sweepGradient->colors()[i])); } - contents->SetColors(std::move(colors)); - contents->SetTileMode(ToTileMode(sweepGradient->tile_mode())); - paint_.contents = std::move(contents); + auto tile_mode = ToTileMode(sweepGradient->tile_mode()); + paint_.color_source = [center, start_angle, end_angle, + colors = std::move(colors), tile_mode]() { + auto contents = std::make_shared(); + contents->SetCenterAndAngles(center, start_angle, end_angle); + contents->SetColors(std::move(colors)); + contents->SetTileMode(tile_mode); + return contents; + }; return; } case flutter::DlColorSourceType::kImage: