[impeller] Add 'ColorSourceProc' for creating color source contents (flutter/engine#35299)
This commit is contained in:
@@ -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<LinearGradientContents>();
|
||||
contents->SetEndPoints({0, 0}, {100, 100});
|
||||
std::vector<Color> 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<LinearGradientContents>();
|
||||
contents->SetEndPoints({0, 0}, {100, 100});
|
||||
std::vector<Color> 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<RadialGradientContents>();
|
||||
contents->SetCenterAndRadius({50, 50}, 50);
|
||||
std::vector<Color> 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<RadialGradientContents>();
|
||||
contents->SetCenterAndRadius({50, 50}, 50);
|
||||
std::vector<Color> 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<SweepGradientContents>();
|
||||
contents->SetCenterAndAngles({50, 50}, Degrees(45), Degrees(135));
|
||||
std::vector<Color> 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<SweepGradientContents>();
|
||||
contents->SetCenterAndAngles({50, 50}, Degrees(45), Degrees(135));
|
||||
std::vector<Color> 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<LinearGradientContents>();
|
||||
contents->SetEndPoints({0, 0}, {100, 100});
|
||||
std::vector<Color> 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;
|
||||
|
||||
@@ -10,7 +10,9 @@ namespace impeller {
|
||||
|
||||
std::shared_ptr<Contents> 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<Contents> Paint::CreateContentsForEntity(Path path,
|
||||
std::shared_ptr<Contents> Paint::WithFilters(
|
||||
std::shared_ptr<Contents> input,
|
||||
std::optional<bool> 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),
|
||||
|
||||
@@ -25,6 +25,7 @@ struct Paint {
|
||||
using MaskFilterProc =
|
||||
std::function<std::shared_ptr<FilterContents>(FilterInput::Ref,
|
||||
bool is_solid_color)>;
|
||||
using ColorSourceProc = std::function<std::shared_ptr<PathContents>()>;
|
||||
|
||||
enum class Style {
|
||||
kFill,
|
||||
@@ -40,7 +41,7 @@ struct Paint {
|
||||
};
|
||||
|
||||
Color color = Color::Black();
|
||||
std::shared_ptr<PathContents> contents;
|
||||
std::optional<ColorSourceProc> color_source;
|
||||
|
||||
Scalar stroke_width = 0.0;
|
||||
SolidStrokeContents::Cap stroke_cap = SolidStrokeContents::Cap::kButt;
|
||||
|
||||
@@ -239,14 +239,14 @@ static std::vector<Matrix> 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<LinearGradientContents>();
|
||||
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<Color> 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<LinearGradientContents>();
|
||||
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<RadialGradientContents>();
|
||||
contents->SetCenterAndRadius(ToPoint(radialGradient->center()),
|
||||
radialGradient->radius());
|
||||
auto center = ToPoint(radialGradient->center());
|
||||
auto radius = radialGradient->radius();
|
||||
std::vector<Color> 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<RadialGradientContents>();
|
||||
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<SweepGradientContents>();
|
||||
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<Color> 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<SweepGradientContents>();
|
||||
contents->SetCenterAndAngles(center, start_angle, end_angle);
|
||||
contents->SetColors(std::move(colors));
|
||||
contents->SetTileMode(tile_mode);
|
||||
return contents;
|
||||
};
|
||||
return;
|
||||
}
|
||||
case flutter::DlColorSourceType::kImage:
|
||||
|
||||
Reference in New Issue
Block a user