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"
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
271349class 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
0 commit comments