Skip to content

Commit e95fb62

Browse files
authored
Handle linear and radial gradients in UberSDF (flutter#190874)
Adds direct support for linear and radial gradient color sources to UberSDF, enabling single-pass rendering of these gradients without the multi-pass blending that is currently used. - Adds a color_source_sampler input to UberSDF, which is used to sample a texture representing the gradient color ramp for a linear or radial gradient. This sampler may also be extended in the future for image-based color sources, but that is outside the scope of this PR. - Adds properties to UberSDFParameters and to UberSDF's FragInfo for specifying gradient properties. - Factors out code in `linear_gradient_fill.frag` and `radial_gradient_fill.frag` that samples a texture to get a color for linear and radial gradients. This logic is moved to functions in `gradient.glsl`, to share with `uber_sdf.frag`. - Updates `Canvas::AddRenderSDFEntityToCurrentPass` to fill in the gradient texture and gradient related properties when `paint.color_source` is a gradient supported by UberSDF. This supports only linear and radial gradients. It does not support conical and sweep gradients. According to Gemini, linear and radial gradients make up more than 99% of gradient usage. Supporting conical and sweep gradients would require additional complexity to UberSDF, so it may not be worth it. We can revisit optimizing these other gradients in the future if we see performance related to them in real world use cases. This change by itself speeds up gradient rendering significantly, according to the results below. This can be further improved in a follow up PR by adding caching for gradient textures, similar to TextShadowCache in ContentContext. Addresses flutter#190401. Results from the demo app in flutter#190401, run on my Windows VM. Note that my Windows VM may not necessarily reflect real-world performance, but these numbers still show significant improvements with this PR. Also, my Windows VM reports a refresh rate of 500 hz, so almost all frame render times are considered "over budget". So ignore the "over budget" percentages and just look at the raw timing numbers. before: ``` === Windows linear-gradient comparison === mode=profile speed=1400px/s Solid color | frames=953 fps=238.00 | ui p50/p95=0.669/1.231ms | raster p50/p90/p95/p99/max=4.131/5.154/5.519/6.192/6.830ms | raster_over_budget=949/953 (99.6%) Linear gradient | frames=204 fps=50.75 | ui p50/p95=1.231/1.658ms | raster p50/p90/p95/p99/max=19.094/23.040/23.982/26.812/32.019ms | raster_over_budget=204/204 (100.0%) ================================================ ``` after: ``` === Windows linear-gradient comparison === mode=profile speed=1400px/s Solid color | frames=833 fps=208.00 | ui p50/p95=0.635/1.033ms | raster p50/p90/p95/p99/max=4.752/5.570/5.732/6.134/7.504ms | raster_over_budget=833/833 (100.0%) Linear gradient | frames=687 fps=171.50 | ui p50/p95=1.187/1.804ms | raster p50/p90/p95/p99/max=5.665/7.414/7.925/8.951/12.153ms | raster_over_budget=687/687 (100.0%) ================================================ ``` Skia: ``` === Windows linear-gradient comparison === mode=profile speed=1400px/s Solid color | frames=1375 fps=343.50 | ui p50/p95=0.495/0.845ms | raster p50/p90/p95/p99/max=2.679/3.514/3.851/4.631/5.777ms | raster_over_budget=1160/1375 (84.4%) Linear gradient | frames=1234 fps=308.25 | ui p50/p95=1.087/1.645ms | raster p50/p90/p95/p99/max=1.731/2.251/2.445/2.960/3.903ms | raster_over_budget=319/1234 (25.9%) ================================================ ``` ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 2394af7 commit e95fb62

13 files changed

Lines changed: 508 additions & 182 deletions

File tree

‎engine/src/flutter/impeller/compiler/shader_lib/impeller/gradient.glsl‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,36 @@ vec3 IPComputeFixedGradientValues(float t, float colors_length) {
149149
return vec3(lower_index, upper_index, scale);
150150
}
151151

152+
/// Samples a linear gradient from a 1D gradient ramp texture.
153+
vec4 IPSampleLinearGradient(sampler2D tex,
154+
vec2 start_point,
155+
vec2 end_point,
156+
vec2 pos,
157+
vec2 half_texel,
158+
float tile_mode,
159+
vec4 decal_border_color) {
160+
vec2 start_to_end = end_point - start_point;
161+
vec2 start_to_position = pos - start_point;
162+
float start_to_end_squared = dot(start_to_end, start_to_end);
163+
164+
float t = start_to_end_squared == 0.0
165+
? 0.0
166+
: dot(start_to_position, start_to_end) / start_to_end_squared;
167+
return IPSampleLinearWithTileMode(tex, vec2(t, 0.5), half_texel, tile_mode,
168+
decal_border_color);
169+
}
170+
171+
/// Samples a radial gradient from a 1D gradient ramp texture.
172+
vec4 IPSampleRadialGradient(sampler2D tex,
173+
vec2 center,
174+
float radius,
175+
vec2 pos,
176+
vec2 half_texel,
177+
float tile_mode,
178+
vec4 decal_border_color) {
179+
float t = radius == 0.0 ? 0.0 : length(pos - center) / radius;
180+
return IPSampleLinearWithTileMode(tex, vec2(t, 0.5), half_texel, tile_mode,
181+
decal_border_color);
182+
}
183+
152184
#endif

‎engine/src/flutter/impeller/compiler/shader_lib/impeller/texture.glsl‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,12 @@ vec4 IPSampleLinearWithTileMode(sampler2D tex,
9898
float x_tile_mode,
9999
float y_tile_mode,
100100
vec4 decal_border_color) {
101-
if (x_tile_mode == kTileModeDecal && (coords.x < 0 || coords.x >= 1) ||
102-
y_tile_mode == kTileModeDecal && (coords.y < 0 || coords.y >= 1)) {
101+
// Unlike 2D image sampling which uses a half-open interval [0, 1) where 1.0
102+
// is outside the texture boundary, linear sampling uses a closed interval
103+
// [0, 1]. Therefore, decal mode checks the ending coordinate using > 1 rather
104+
// than >= 1.
105+
if (x_tile_mode == kTileModeDecal && (coords.x < 0.0 || coords.x > 1.0) ||
106+
y_tile_mode == kTileModeDecal && (coords.y < 0.0 || coords.y > 1.0)) {
103107
return decal_border_color;
104108
}
105109

‎engine/src/flutter/impeller/display_list/canvas.cc‎

Lines changed: 99 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "impeller/entity/contents/content_context.h"
3535
#include "impeller/entity/contents/filters/filter_contents.h"
3636
#include "impeller/entity/contents/framebuffer_blend_contents.h"
37+
#include "impeller/entity/contents/gradient_generator.h"
3738
#include "impeller/entity/contents/shadow_vertices_contents.h"
3839
#include "impeller/entity/contents/solid_color_contents.h"
3940
#include "impeller/entity/contents/solid_rrect_blur_contents.h"
@@ -59,6 +60,7 @@
5960
#include "impeller/entity/save_layer_utils.h"
6061
#include "impeller/geometry/color.h"
6162
#include "impeller/geometry/constants.h"
63+
#include "impeller/geometry/gradient.h"
6264
#include "impeller/geometry/round_superellipse_param.h"
6365
#include "impeller/geometry/rounding_radii.h"
6466
#include "impeller/geometry/rstransform.h"
@@ -266,6 +268,82 @@ static std::pair<Rect, Color> ExpandRectToPixelMinimum(const Rect& rect,
266268
return {expanded.value(), color.WithAlpha(color.alpha * alpha_scaling)};
267269
}
268270

271+
/// @brief Attempts to create UberSDF GradientParameters from a DlColorSource,
272+
/// if supported by UberSDF (e.g. for linear and radial gradients).
273+
///
274+
/// @return The GradientParameters if the color source is a supported gradient,
275+
/// otherwise std::nullopt.
276+
static std::optional<UberSDFParameters::GradientParameters>
277+
CreateUberSDFGradientParameters(const ContentContext& renderer,
278+
const flutter::DlColorSource& color_source,
279+
const std::optional<Matrix>& shape_transform) {
280+
if (!color_source.isGradient()) {
281+
// Color source not supported by UberSDF.
282+
return std::nullopt;
283+
}
284+
285+
UberSDFParameters::GradientParameters gradient;
286+
287+
std::vector<Color> colors;
288+
std::vector<float> stops;
289+
290+
// When a shape transform is applied to transform the SDF shape from local
291+
// space into canvas space, the gradient parameters must be mapped from
292+
// canvas space into local space via the inverse shape transform.
293+
Matrix inverted_shape_transform =
294+
shape_transform.has_value() ? shape_transform->Invert() : Matrix();
295+
296+
if (color_source.type() == flutter::DlColorSourceType::kLinearGradient) {
297+
const auto* linear = color_source.asLinearGradient();
298+
FML_DCHECK(linear);
299+
Matrix gradient_transform = inverted_shape_transform * linear->matrix();
300+
if (!gradient_transform.IsAffine()) {
301+
// Non-affine matrix transformation not supported by UberSDF.
302+
return std::nullopt;
303+
}
304+
Paint::ConvertStops(linear, colors, stops);
305+
gradient.type = UberSDFParameters::GradientParameters::Type::kLinear;
306+
gradient.start = gradient_transform * linear->start_point();
307+
gradient.end = gradient_transform * linear->end_point();
308+
gradient.tile_mode = static_cast<Entity::TileMode>(linear->tile_mode());
309+
} else if (color_source.type() ==
310+
flutter::DlColorSourceType::kRadialGradient) {
311+
const auto* radial = color_source.asRadialGradient();
312+
FML_DCHECK(radial);
313+
Matrix gradient_transform = inverted_shape_transform * radial->matrix();
314+
if (!gradient_transform.IsAffine()) {
315+
// Non-affine matrix transformation not supported by UberSDF.
316+
return std::nullopt;
317+
}
318+
auto scales = gradient_transform.GetScales2D();
319+
if (!scales.has_value() ||
320+
!ScalarNearlyEqual(scales->first, scales->second)) {
321+
// Non-uniform scaling on a radial gradient creates an ellipse, which is
322+
// not supported by UberSDF.
323+
return std::nullopt;
324+
}
325+
Paint::ConvertStops(radial, colors, stops);
326+
gradient.type = UberSDFParameters::GradientParameters::Type::kRadial;
327+
gradient.start = gradient_transform * radial->center();
328+
// For radial gradients, gradient.end.x stores the radius.
329+
gradient.end = Point(radial->radius() * scales->first, 0.0f);
330+
gradient.tile_mode = static_cast<Entity::TileMode>(radial->tile_mode());
331+
} else {
332+
// Gradient type not supported by UberSDF.
333+
return std::nullopt;
334+
}
335+
336+
GradientData gradient_data = CreateGradientBuffer(colors, stops);
337+
std::shared_ptr<Texture> texture =
338+
CreateGradientTexture(gradient_data, renderer.GetContext());
339+
if (!texture) {
340+
return std::nullopt;
341+
}
342+
343+
gradient.texture = std::move(texture);
344+
return gradient;
345+
}
346+
269347
} // namespace
270348

271349
class Canvas::RRectBlurShape : public BlurShape {
@@ -2216,22 +2294,32 @@ void Canvas::AddRenderSDFEntityToCurrentPass(
22162294
entity.SetBlendMode(paint.blend_mode);
22172295

22182296
if (paint.color_source) {
2219-
// Since we are going to use BlendMode::kSrcIn to implement the color_source
2220-
// the SDF portion of the blend should just be solid white to get the
2221-
// correct color from the color_source.
2222-
params.color = Color::White();
2297+
params.gradient = CreateUberSDFGradientParameters(
2298+
renderer_, *paint.color_source, shape_transform);
22232299
}
2224-
auto geometry = std::make_unique<UberSDFGeometry>(params);
2225-
auto contents = UberSDFContents::Make(params, std::move(geometry));
2226-
const Geometry* geom = contents->GetGeometry();
22272300

2228-
if (paint.color_source) {
2229-
// UberSDF doesn't perform things like gradients so we blend the SDF
2230-
// with the color source.
2301+
if (!paint.color_source || params.gradient.has_value()) {
2302+
// No color source (solid paint color), or a supported gradient color
2303+
// source.
2304+
auto geometry = std::make_unique<UberSDFGeometry>(params);
2305+
auto contents = UberSDFContents::Make(params, std::move(geometry));
2306+
const Geometry* geom = contents->GetGeometry();
2307+
AddRenderEntityWithFiltersToCurrentPass(entity, geom, paint, reuse_depth,
2308+
/*override_contents=*/
2309+
std::move(contents));
2310+
} else {
2311+
// Color source not directly supported by UberSDF (e.g. image, runtime
2312+
// effect, or an unsupported gradient type). Render a solid white mask with
2313+
// UberSDF and blend with ColorSourceContents.
2314+
params.color = Color::White();
2315+
auto geometry = std::make_unique<UberSDFGeometry>(params);
2316+
auto uber_sdf_contents = UberSDFContents::Make(params, std::move(geometry));
2317+
const Geometry* geom = uber_sdf_contents->GetGeometry();
2318+
22312319
std::shared_ptr<ColorSourceContents> color_source_contents =
22322320
paint.CreateContents(renderer_, geom, shape_transform);
22332321
std::shared_ptr<Contents> final_contents = ColorFilterContents::MakeBlend(
2234-
BlendMode::kSrcIn, {FilterInput::Make(std::move(contents)),
2322+
BlendMode::kSrcIn, {FilterInput::Make(std::move(uber_sdf_contents)),
22352323
FilterInput::Make(color_source_contents)});
22362324

22372325
Paint new_paint = paint;
@@ -2240,10 +2328,6 @@ void Canvas::AddRenderSDFEntityToCurrentPass(
22402328
reuse_depth,
22412329
/*override_contents=*/
22422330
std::move(final_contents));
2243-
} else {
2244-
AddRenderEntityWithFiltersToCurrentPass(entity, geom, paint, reuse_depth,
2245-
/*override_contents=*/
2246-
std::move(contents));
22472331
}
22482332
}
22492333

‎engine/src/flutter/impeller/entity/contents/uber_sdf_contents.cc‎

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "impeller/entity/contents/uber_sdf_contents.h"
66

7+
#include "fml/logging.h"
78
#include "impeller/entity/contents/color_source_contents.h"
89
#include "impeller/entity/contents/content_context.h"
910
#include "impeller/entity/contents/pipelines.h"
@@ -46,6 +47,47 @@ Scalar ToShaderStrokeJoin(Join join) {
4647
}
4748
}
4849

50+
Scalar ToShaderColorSourceType(const UberSDFParameters& params) {
51+
if (!params.gradient.has_value()) {
52+
return 0.0f;
53+
}
54+
switch (params.gradient->type) {
55+
case UberSDFParameters::GradientParameters::Type::kLinear:
56+
return 1.0f;
57+
case UberSDFParameters::GradientParameters::Type::kRadial:
58+
return 2.0f;
59+
}
60+
}
61+
62+
struct SamplerBinding {
63+
std::shared_ptr<Texture> texture;
64+
raw_ptr<const Sampler> sampler;
65+
};
66+
67+
/// @brief Populates the gradient uniform fields in `frag_info` and returns
68+
/// the texture and sampler for the gradient.
69+
SamplerBinding SetupGradientParameters(
70+
const UberSDFParameters::GradientParameters& gradient,
71+
const ContentContext& renderer,
72+
FS::FragInfo& frag_info) {
73+
FML_DCHECK(gradient.texture);
74+
frag_info.gradient_start = gradient.start;
75+
frag_info.gradient_end = gradient.end;
76+
frag_info.tile_mode = static_cast<Scalar>(gradient.tile_mode);
77+
auto texture_size = gradient.texture->GetSize();
78+
FML_DCHECK(!texture_size.IsEmpty());
79+
frag_info.half_texel =
80+
Point(0.5f, 0.5f) / Point(texture_size.width, texture_size.height);
81+
82+
SamplerDescriptor sampler_desc;
83+
sampler_desc.min_filter = MinMagFilter::kLinear;
84+
sampler_desc.mag_filter = MinMagFilter::kLinear;
85+
raw_ptr<const Sampler> sampler =
86+
renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc);
87+
88+
return {gradient.texture, sampler};
89+
}
90+
4991
} // namespace
5092

5193
std::unique_ptr<UberSDFContents> UberSDFContents::Make(
@@ -69,6 +111,7 @@ bool UberSDFContents::Render(const ContentContext& renderer,
69111
VS::FrameInfo frame_info;
70112
FS::FragInfo frag_info;
71113
frag_info.type = ToShaderType(params_.type);
114+
frag_info.color_source_type = ToShaderColorSourceType(params_);
72115
frag_info.color =
73116
params_.color.WithAlpha(params_.color.alpha * GetOpacityFactor());
74117
frag_info.center = params_.center;
@@ -84,6 +127,16 @@ bool UberSDFContents::Render(const ContentContext& renderer,
84127
frag_info.circle_center_right = params_.circle_center_right;
85128
frag_info.radii = params_.radii;
86129

130+
SamplerBinding sampler_binding;
131+
if (params_.gradient) {
132+
sampler_binding =
133+
SetupGradientParameters(params_.gradient.value(), renderer, frag_info);
134+
} else {
135+
sampler_binding.texture = renderer.GetEmptyTexture();
136+
sampler_binding.sampler =
137+
renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
138+
}
139+
87140
auto geometry_result =
88141
GetGeometry()->GetPositionBuffer(renderer, entity, pass);
89142

@@ -96,7 +149,10 @@ bool UberSDFContents::Render(const ContentContext& renderer,
96149
this, GetGeometry(), renderer, entity, pass, pipeline_callback,
97150
frame_info,
98151
/*bind_fragment_callback=*/
99-
[&frag_info, &data_host_buffer](RenderPass& pass) {
152+
[&frag_info, &data_host_buffer,
153+
sampler_binding = std::move(sampler_binding)](RenderPass& pass) {
154+
FS::BindColorSourceSampler(pass, sampler_binding.texture,
155+
sampler_binding.sampler);
100156
FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
101157
pass.SetCommandLabel("UberSDF");
102158
return true;
@@ -123,14 +179,18 @@ Color UberSDFContents::GetColor() const {
123179

124180
bool UberSDFContents::ApplyColorFilter(
125181
const ColorFilterProc& color_filter_proc) {
182+
if (params_.gradient.has_value()) {
183+
return false;
184+
}
126185
params_.color = color_filter_proc(params_.color);
127186
return true;
128187
}
129188

130189
std::optional<Color> UberSDFContents::AsBackgroundColor(
131190
const Entity& entity,
132191
ISize target_size) const {
133-
if (params_.type != UberSDFParameters::Type::kRect) {
192+
if (params_.type != UberSDFParameters::Type::kRect ||
193+
params_.gradient.has_value()) {
134194
return std::nullopt;
135195
}
136196
const Geometry* geometry = GetGeometry();

‎engine/src/flutter/impeller/entity/contents/uber_sdf_contents_unittests.cc‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ TEST(UberSDFContentsTest, ApplyColorFilter) {
2929
EXPECT_EQ(contents->GetColor(), Color::Blue());
3030
}
3131

32+
TEST(UberSDFContentsTest, ApplyColorFilterWithGradient) {
33+
auto rect = Rect::MakeXYWH(100, 100, 200, 200);
34+
auto params =
35+
UberSDFParameters::MakeRect(Color::Red(), rect, /*stroke=*/std::nullopt);
36+
UberSDFParameters::GradientParameters gradient;
37+
gradient.type = UberSDFParameters::GradientParameters::Type::kLinear;
38+
gradient.start = Point(0, 0);
39+
gradient.end = Point(200, 200);
40+
params.gradient = gradient;
41+
42+
auto geometry = std::make_unique<UberSDFGeometry>(params);
43+
auto contents = UberSDFContents::Make(params, std::move(geometry));
44+
45+
// Color filters cannot be applied on CPU to pre-baked gradients in
46+
// UberSDFContents, so ApplyColorFilter must return false to trigger GPU
47+
// filter wrapping.
48+
bool result =
49+
contents->ApplyColorFilter([](Color color) { return Color::Blue(); });
50+
51+
EXPECT_FALSE(result);
52+
// The color should remain unchanged.
53+
EXPECT_EQ(contents->GetColor(), Color::Red());
54+
}
55+
3256
TEST(UberSDFContentsTest, AsBackgroundColor) {
3357
auto rect = Rect::MakeXYWH(-2, -2, 504, 504);
3458
auto params =
@@ -100,5 +124,29 @@ TEST(UberSDFContentsTest, AsBackgroundColorStrokedRect) {
100124
EXPECT_FALSE(bg_color.has_value());
101125
}
102126

127+
TEST(UberSDFContentsTest, AsBackgroundColorGradientReturnsNullopt) {
128+
auto rect = Rect::MakeXYWH(-2, -2, 504, 504);
129+
auto params =
130+
UberSDFParameters::MakeRect(Color::Red(), rect, /*stroke=*/std::nullopt);
131+
132+
UberSDFParameters::GradientParameters gradient;
133+
gradient.type = UberSDFParameters::GradientParameters::Type::kLinear;
134+
gradient.start = Point(0, 0);
135+
gradient.end = Point(500, 500);
136+
gradient.tile_mode = Entity::TileMode::kClamp;
137+
params.gradient = gradient;
138+
139+
auto geometry = std::make_unique<UberSDFGeometry>(params);
140+
auto contents = UberSDFContents::Make(params, std::move(geometry));
141+
142+
Entity entity;
143+
entity.SetTransform(Matrix());
144+
145+
// Even though the rect covers the entire area, a gradient is not a single
146+
// solid background color, so AsBackgroundColor must return nullopt.
147+
auto bg_color = contents->AsBackgroundColor(entity, ISize(500, 500));
148+
EXPECT_FALSE(bg_color.has_value());
149+
}
150+
103151
} // namespace testing
104152
} // namespace impeller

0 commit comments

Comments
 (0)