Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0325408

Browse files
committed
Refactor color source resolution to use explicit factory types
1 parent 21a572e commit 0325408

15 files changed

+1001
-582
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,8 @@ FILE: ../../../flutter/impeller/aiks/aiks_playground.h
10301030
FILE: ../../../flutter/impeller/aiks/aiks_unittests.cc
10311031
FILE: ../../../flutter/impeller/aiks/canvas.cc
10321032
FILE: ../../../flutter/impeller/aiks/canvas.h
1033+
FILE: ../../../flutter/impeller/aiks/color_source_factory.cc
1034+
FILE: ../../../flutter/impeller/aiks/color_source_factory.h
10331035
FILE: ../../../flutter/impeller/aiks/image.cc
10341036
FILE: ../../../flutter/impeller/aiks/image.h
10351037
FILE: ../../../flutter/impeller/aiks/paint.cc
@@ -1129,6 +1131,10 @@ FILE: ../../../flutter/impeller/compiler/types.cc
11291131
FILE: ../../../flutter/impeller/compiler/types.h
11301132
FILE: ../../../flutter/impeller/compiler/utilities.cc
11311133
FILE: ../../../flutter/impeller/compiler/utilities.h
1134+
FILE: ../../../flutter/impeller/display_list/conversion_utilities.cc
1135+
FILE: ../../../flutter/impeller/display_list/conversion_utilities.h
1136+
FILE: ../../../flutter/impeller/display_list/display_list_color_source_factory.cc
1137+
FILE: ../../../flutter/impeller/display_list/display_list_color_source_factory.h
11321138
FILE: ../../../flutter/impeller/display_list/display_list_dispatcher.cc
11331139
FILE: ../../../flutter/impeller/display_list/display_list_dispatcher.h
11341140
FILE: ../../../flutter/impeller/display_list/display_list_image_impeller.cc

impeller/aiks/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ impeller_component("aiks") {
1010
"aiks_context.h",
1111
"canvas.cc",
1212
"canvas.h",
13+
"color_source_factory.cc",
14+
"color_source_factory.h",
1315
"image.cc",
1416
"image.h",
1517
"paint.cc",

impeller/aiks/aiks_unittests.cc

Lines changed: 234 additions & 191 deletions
Large diffs are not rendered by default.

impeller/aiks/canvas.cc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ void Canvas::DrawPaint(const Paint& paint) {
160160
bool Canvas::AttemptDrawBlurredRRect(const Rect& rect,
161161
Scalar corner_radius,
162162
const Paint& paint) {
163-
if (paint.color_source == nullptr ||
164-
paint.color_source_type != Paint::ColorSourceType::kColor ||
165-
paint.style != Paint::Style::kFill) {
163+
if (!paint.color_source || paint.style != Paint::Style::kFill) {
166164
return false;
167165
}
168166

@@ -375,17 +373,16 @@ void Canvas::DrawTextFrame(const TextFrame& text_frame,
375373

376374
void Canvas::DrawVertices(const Vertices& vertices,
377375
BlendMode blend_mode,
378-
Paint paint) {
376+
const Paint& paint) {
379377
auto geometry = Geometry::MakeVertices(vertices);
380378

381379
Entity entity;
382380
entity.SetTransformation(GetCurrentTransformation());
383381
entity.SetStencilDepth(GetStencilDepth());
384382
entity.SetBlendMode(paint.blend_mode);
385383

386-
if (paint.color_source.has_value()) {
387-
auto& source = paint.color_source.value();
388-
auto contents = source();
384+
if (paint.color_source) {
385+
auto contents = paint.color_source->MakeContents();
389386
contents->SetGeometry(std::move(geometry));
390387
contents->SetAlpha(paint.color.alpha);
391388
entity.SetContents(paint.WithFilters(std::move(contents), true));

impeller/aiks/canvas.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Canvas {
9999

100100
void DrawVertices(const Vertices& vertices,
101101
BlendMode blend_mode,
102-
Paint paint);
102+
const Paint& paint);
103103

104104
void DrawAtlas(const std::shared_ptr<Image>& atlas,
105105
std::vector<Matrix> transforms,

impeller/aiks/color_source_factory.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "impeller/aiks/color_source_factory.h"
6+
7+
namespace impeller {
8+
9+
ColorSourceFactory::~ColorSourceFactory() = default;
10+
11+
} // namespace impeller

impeller/aiks/color_source_factory.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include <memory>
8+
9+
#include "impeller/entity/contents/color_source_contents.h"
10+
11+
namespace impeller {
12+
13+
class ColorSourceFactory {
14+
public:
15+
enum class ColorSourceType {
16+
kColor,
17+
kImage,
18+
kLinearGradient,
19+
kRadialGradient,
20+
kConicalGradient,
21+
kSweepGradient,
22+
kRuntimeEffect,
23+
};
24+
25+
virtual ~ColorSourceFactory();
26+
27+
virtual std::shared_ptr<ColorSourceContents> MakeContents() = 0;
28+
29+
virtual ColorSourceType GetType() = 0;
30+
};
31+
32+
} // namespace impeller

impeller/aiks/paint.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ std::shared_ptr<Contents> Paint::CreateContentsForEntity(const Path& path,
2727

2828
std::shared_ptr<Contents> Paint::CreateContentsForGeometry(
2929
std::unique_ptr<Geometry> geometry) const {
30-
if (color_source.has_value()) {
31-
auto& source = color_source.value();
32-
auto contents = source();
30+
if (color_source) {
31+
auto contents = color_source->MakeContents();
3332
contents->SetGeometry(std::move(geometry));
3433
contents->SetAlpha(color.alpha);
3534
return contents;

impeller/aiks/paint.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <memory>
88

99
#include "flutter/fml/macros.h"
10+
#include "impeller/aiks/color_source_factory.h"
1011
#include "impeller/entity/contents/contents.h"
1112
#include "impeller/entity/contents/filters/color_filter_contents.h"
1213
#include "impeller/entity/contents/filters/filter_contents.h"
@@ -29,23 +30,12 @@ struct Paint {
2930
FilterInput::Ref,
3031
bool is_solid_color,
3132
const Matrix& effect_transform)>;
32-
using ColorSourceProc = std::function<std::shared_ptr<ColorSourceContents>()>;
3333

3434
enum class Style {
3535
kFill,
3636
kStroke,
3737
};
3838

39-
enum class ColorSourceType {
40-
kColor,
41-
kImage,
42-
kLinearGradient,
43-
kRadialGradient,
44-
kConicalGradient,
45-
kSweepGradient,
46-
kRuntimeEffect,
47-
};
48-
4939
struct MaskBlurDescriptor {
5040
FilterContents::BlurStyle style;
5141
Sigma sigma;
@@ -56,10 +46,9 @@ struct Paint {
5646
const Matrix& effect_matrix) const;
5747
};
5848

59-
Color color = Color::Black();
60-
std::optional<ColorSourceProc> color_source;
61-
ColorSourceType color_source_type = ColorSourceType::kColor;
49+
std::shared_ptr<ColorSourceFactory> color_source;
6250

51+
Color color = Color::Black();
6352
Scalar stroke_width = 0.0;
6453
Cap stroke_cap = Cap::kButt;
6554
Join stroke_join = Join::kMiter;

impeller/display_list/BUILD.gn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import("//flutter/impeller/tools/impeller.gni")
66

77
impeller_component("display_list") {
88
sources = [
9+
"conversion_utilities.cc",
10+
"conversion_utilities.h",
11+
"display_list_color_source_factory.cc",
12+
"display_list_color_source_factory.h",
913
"display_list_dispatcher.cc",
1014
"display_list_dispatcher.h",
1115
"display_list_image_impeller.cc",
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "impeller/display_list/conversion_utilities.h"
6+
7+
namespace impeller {
8+
9+
BlendMode ToBlendMode(flutter::DlBlendMode mode) {
10+
switch (mode) {
11+
case flutter::DlBlendMode::kClear:
12+
return BlendMode::kClear;
13+
case flutter::DlBlendMode::kSrc:
14+
return BlendMode::kSource;
15+
case flutter::DlBlendMode::kDst:
16+
return BlendMode::kDestination;
17+
case flutter::DlBlendMode::kSrcOver:
18+
return BlendMode::kSourceOver;
19+
case flutter::DlBlendMode::kDstOver:
20+
return BlendMode::kDestinationOver;
21+
case flutter::DlBlendMode::kSrcIn:
22+
return BlendMode::kSourceIn;
23+
case flutter::DlBlendMode::kDstIn:
24+
return BlendMode::kDestinationIn;
25+
case flutter::DlBlendMode::kSrcOut:
26+
return BlendMode::kSourceOut;
27+
case flutter::DlBlendMode::kDstOut:
28+
return BlendMode::kDestinationOut;
29+
case flutter::DlBlendMode::kSrcATop:
30+
return BlendMode::kSourceATop;
31+
case flutter::DlBlendMode::kDstATop:
32+
return BlendMode::kDestinationATop;
33+
case flutter::DlBlendMode::kXor:
34+
return BlendMode::kXor;
35+
case flutter::DlBlendMode::kPlus:
36+
return BlendMode::kPlus;
37+
case flutter::DlBlendMode::kModulate:
38+
return BlendMode::kModulate;
39+
case flutter::DlBlendMode::kScreen:
40+
return BlendMode::kScreen;
41+
case flutter::DlBlendMode::kOverlay:
42+
return BlendMode::kOverlay;
43+
case flutter::DlBlendMode::kDarken:
44+
return BlendMode::kDarken;
45+
case flutter::DlBlendMode::kLighten:
46+
return BlendMode::kLighten;
47+
case flutter::DlBlendMode::kColorDodge:
48+
return BlendMode::kColorDodge;
49+
case flutter::DlBlendMode::kColorBurn:
50+
return BlendMode::kColorBurn;
51+
case flutter::DlBlendMode::kHardLight:
52+
return BlendMode::kHardLight;
53+
case flutter::DlBlendMode::kSoftLight:
54+
return BlendMode::kSoftLight;
55+
case flutter::DlBlendMode::kDifference:
56+
return BlendMode::kDifference;
57+
case flutter::DlBlendMode::kExclusion:
58+
return BlendMode::kExclusion;
59+
case flutter::DlBlendMode::kMultiply:
60+
return BlendMode::kMultiply;
61+
case flutter::DlBlendMode::kHue:
62+
return BlendMode::kHue;
63+
case flutter::DlBlendMode::kSaturation:
64+
return BlendMode::kSaturation;
65+
case flutter::DlBlendMode::kColor:
66+
return BlendMode::kColor;
67+
case flutter::DlBlendMode::kLuminosity:
68+
return BlendMode::kLuminosity;
69+
}
70+
FML_UNREACHABLE();
71+
}
72+
73+
Entity::TileMode ToTileMode(flutter::DlTileMode tile_mode) {
74+
switch (tile_mode) {
75+
case flutter::DlTileMode::kClamp:
76+
return Entity::TileMode::kClamp;
77+
case flutter::DlTileMode::kRepeat:
78+
return Entity::TileMode::kRepeat;
79+
case flutter::DlTileMode::kMirror:
80+
return Entity::TileMode::kMirror;
81+
case flutter::DlTileMode::kDecal:
82+
return Entity::TileMode::kDecal;
83+
}
84+
}
85+
86+
impeller::SamplerDescriptor ToSamplerDescriptor(
87+
const flutter::DlImageSampling options) {
88+
impeller::SamplerDescriptor desc;
89+
switch (options) {
90+
case flutter::DlImageSampling::kNearestNeighbor:
91+
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kNearest;
92+
desc.label = "Nearest Sampler";
93+
break;
94+
case flutter::DlImageSampling::kLinear:
95+
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
96+
desc.label = "Linear Sampler";
97+
break;
98+
case flutter::DlImageSampling::kMipmapLinear:
99+
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
100+
desc.mip_filter = impeller::MipFilter::kLinear;
101+
desc.label = "Mipmap Linear Sampler";
102+
break;
103+
default:
104+
break;
105+
}
106+
return desc;
107+
}
108+
109+
impeller::SamplerDescriptor ToSamplerDescriptor(
110+
const flutter::DlFilterMode options) {
111+
impeller::SamplerDescriptor desc;
112+
switch (options) {
113+
case flutter::DlFilterMode::kNearest:
114+
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kNearest;
115+
desc.label = "Nearest Sampler";
116+
break;
117+
case flutter::DlFilterMode::kLinear:
118+
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
119+
desc.label = "Linear Sampler";
120+
break;
121+
default:
122+
break;
123+
}
124+
return desc;
125+
}
126+
127+
Matrix ToMatrix(const SkMatrix& m) {
128+
return Matrix{
129+
// clang-format off
130+
m[0], m[3], 0, m[6],
131+
m[1], m[4], 0, m[7],
132+
0, 0, 1, 0,
133+
m[2], m[5], 0, m[8],
134+
// clang-format on
135+
};
136+
}
137+
138+
Point ToPoint(const SkPoint& point) {
139+
return Point::MakeXY(point.fX, point.fY);
140+
}
141+
142+
Color ToColor(const SkColor& color) {
143+
return {
144+
static_cast<Scalar>(SkColorGetR(color) / 255.0), //
145+
static_cast<Scalar>(SkColorGetG(color) / 255.0), //
146+
static_cast<Scalar>(SkColorGetB(color) / 255.0), //
147+
static_cast<Scalar>(SkColorGetA(color) / 255.0) //
148+
};
149+
}
150+
151+
std::vector<Color> ToColors(const flutter::DlColor colors[], int count) {
152+
auto result = std::vector<Color>();
153+
if (colors == nullptr) {
154+
return result;
155+
}
156+
for (int i = 0; i < count; i++) {
157+
result.push_back(ToColor(colors[i]));
158+
}
159+
return result;
160+
}
161+
162+
std::vector<Matrix> ToRSXForms(const SkRSXform xform[], int count) {
163+
auto result = std::vector<Matrix>();
164+
for (int i = 0; i < count; i++) {
165+
auto form = xform[i];
166+
// clang-format off
167+
auto matrix = Matrix{
168+
form.fSCos, form.fSSin, 0, 0,
169+
-form.fSSin, form.fSCos, 0, 0,
170+
0, 0, 1, 0,
171+
form.fTx, form.fTy, 0, 1
172+
};
173+
// clang-format on
174+
result.push_back(matrix);
175+
}
176+
return result;
177+
}
178+
179+
} // namespace impeller

0 commit comments

Comments
 (0)