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"
3837#include " impeller/entity/contents/shadow_vertices_contents.h"
3938#include " impeller/entity/contents/solid_color_contents.h"
4039#include " impeller/entity/contents/solid_rrect_blur_contents.h"
6059#include " impeller/entity/save_layer_utils.h"
6160#include " impeller/geometry/color.h"
6261#include " impeller/geometry/constants.h"
63- #include " impeller/geometry/gradient.h"
6462#include " impeller/geometry/round_superellipse_param.h"
6563#include " impeller/geometry/rounding_radii.h"
6664#include " impeller/geometry/rstransform.h"
@@ -268,82 +266,6 @@ static std::pair<Rect, Color> ExpandRectToPixelMinimum(const Rect& rect,
268266 return {expanded.value (), color.WithAlpha (color.alpha * alpha_scaling)};
269267}
270268
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-
347269} // namespace
348270
349271class Canvas ::RRectBlurShape : public BlurShape {
@@ -2294,32 +2216,22 @@ void Canvas::AddRenderSDFEntityToCurrentPass(
22942216 entity.SetBlendMode (paint.blend_mode );
22952217
22962218 if (paint.color_source ) {
2297- params.gradient = CreateUberSDFGradientParameters (
2298- renderer_, *paint.color_source , shape_transform);
2299- }
2300-
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.
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.
23142222 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 ();
2223+ }
2224+ auto geometry = std::make_unique<UberSDFGeometry>(params);
2225+ auto contents = UberSDFContents::Make (params, std::move (geometry));
2226+ const Geometry* geom = contents->GetGeometry ();
23182227
2228+ if (paint.color_source ) {
2229+ // UberSDF doesn't perform things like gradients so we blend the SDF
2230+ // with the color source.
23192231 std::shared_ptr<ColorSourceContents> color_source_contents =
23202232 paint.CreateContents (renderer_, geom, shape_transform);
23212233 std::shared_ptr<Contents> final_contents = ColorFilterContents::MakeBlend (
2322- BlendMode::kSrcIn , {FilterInput::Make (std::move (uber_sdf_contents )),
2234+ BlendMode::kSrcIn , {FilterInput::Make (std::move (contents )),
23232235 FilterInput::Make (color_source_contents)});
23242236
23252237 Paint new_paint = paint;
@@ -2328,6 +2240,10 @@ void Canvas::AddRenderSDFEntityToCurrentPass(
23282240 reuse_depth,
23292241 /* override_contents=*/
23302242 std::move (final_contents));
2243+ } else {
2244+ AddRenderEntityWithFiltersToCurrentPass (entity, geom, paint, reuse_depth,
2245+ /* override_contents=*/
2246+ std::move (contents));
23312247 }
23322248}
23332249
0 commit comments