Flutter Impeller
impeller::Paint Struct Reference

#include <paint.h>

Classes

struct  MaskBlurDescriptor
 

Public Types

enum class  Style {
  kFill ,
  kStroke
}
 
using ImageFilterProc = std::function< std::shared_ptr< FilterContents >(FilterInput::Ref, const Matrix &effect_transform, Entity::RenderingMode rendering_mode)>
 
using MaskFilterProc = std::function< std::shared_ptr< FilterContents >(FilterInput::Ref, bool is_solid_color, const Matrix &effect_transform)>
 
using ColorSourceProc = std::function< std::shared_ptr< ColorSourceContents >()>
 

Public Member Functions

std::shared_ptr< ContentsWithFilters (std::shared_ptr< Contents > input) const
 Wrap this paint's configured filters to the given contents. More...
 
std::shared_ptr< ContentsWithFiltersForSubpassTarget (std::shared_ptr< Contents > input, const Matrix &effect_transform=Matrix()) const
 Wrap this paint's configured filters to the given contents of subpass target. More...
 
bool HasColorFilter () const
 Whether this paint has a color filter that can apply opacity. More...
 
std::shared_ptr< ColorSourceContentsCreateContents () const
 
std::shared_ptr< ContentsWithMaskBlur (std::shared_ptr< Contents > input, bool is_solid_color, const Matrix &ctm) const
 
std::shared_ptr< FilterContentsWithImageFilter (const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
 

Static Public Member Functions

static bool CanApplyOpacityPeephole (const Paint &paint)
 Whether or not a save layer with the provided paint can perform the opacity peephole optimization. More...
 

Public Attributes

Color color = Color::Black()
 
const flutter::DlColorSource * color_source = nullptr
 
const flutter::DlColorFilter * color_filter = nullptr
 
const flutter::DlImageFilter * image_filter = nullptr
 
Scalar stroke_width = 0.0
 
Cap stroke_cap = Cap::kButt
 
Join stroke_join = Join::kMiter
 
Scalar stroke_miter = 4.0
 
Style style = Style::kFill
 
BlendMode blend_mode = BlendMode::kSourceOver
 
bool invert_colors = false
 
std::optional< MaskBlurDescriptormask_blur_descriptor
 

Detailed Description

Definition at line 26 of file paint.h.

Member Typedef Documentation

◆ ColorSourceProc

using impeller::Paint::ColorSourceProc = std::function<std::shared_ptr<ColorSourceContents>()>

Definition at line 35 of file paint.h.

◆ ImageFilterProc

using impeller::Paint::ImageFilterProc = std::function<std::shared_ptr<FilterContents>( FilterInput::Ref, const Matrix& effect_transform, Entity::RenderingMode rendering_mode)>

Definition at line 27 of file paint.h.

◆ MaskFilterProc

using impeller::Paint::MaskFilterProc = std::function<std::shared_ptr<FilterContents>( FilterInput::Ref, bool is_solid_color, const Matrix& effect_transform)>

Definition at line 31 of file paint.h.

Member Enumeration Documentation

◆ Style

Enumerator
kFill 
kStroke 

Definition at line 46 of file paint.h.

46  {
47  kFill,
48  kStroke,
49  };

Member Function Documentation

◆ CanApplyOpacityPeephole()

static bool impeller::Paint::CanApplyOpacityPeephole ( const Paint paint)
inlinestatic

Whether or not a save layer with the provided paint can perform the opacity peephole optimization.

Definition at line 39 of file paint.h.

39  {
40  return paint.blend_mode == BlendMode::kSourceOver &&
41  paint.invert_colors == false &&
42  !paint.mask_blur_descriptor.has_value() &&
43  paint.image_filter == nullptr && paint.color_filter == nullptr;
44  }

References blend_mode, color_filter, image_filter, invert_colors, impeller::kSourceOver, and mask_blur_descriptor.

Referenced by impeller::Canvas::SaveLayer().

◆ CreateContents()

std::shared_ptr< ColorSourceContents > impeller::Paint::CreateContents ( ) const

Definition at line 37 of file paint.cc.

37  {
38  if (color_source == nullptr) {
39  auto contents = std::make_shared<SolidColorContents>();
40  contents->SetColor(color);
41  return contents;
42  }
43 
44  switch (color_source->type()) {
45  case flutter::DlColorSourceType::kLinearGradient: {
46  const flutter::DlLinearGradientColorSource* linear =
47  color_source->asLinearGradient();
48  FML_DCHECK(linear);
49  auto start_point = linear->start_point();
50  auto end_point = linear->end_point();
51  std::vector<Color> colors;
52  std::vector<float> stops;
53  skia_conversions::ConvertStops(linear, colors, stops);
54 
55  auto tile_mode = static_cast<Entity::TileMode>(linear->tile_mode());
56  auto effect_transform = linear->matrix();
57 
58  auto contents = std::make_shared<LinearGradientContents>();
59  contents->SetOpacityFactor(color.alpha);
60  contents->SetColors(std::move(colors));
61  contents->SetStops(std::move(stops));
62  contents->SetEndPoints(start_point, end_point);
63  contents->SetTileMode(tile_mode);
64  contents->SetEffectTransform(effect_transform);
65 
66  std::array<Point, 2> bounds{start_point, end_point};
67  auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
68  if (intrinsic_size.has_value()) {
69  contents->SetColorSourceSize(intrinsic_size->GetSize().Max({1, 1}));
70  }
71  return contents;
72  }
73  case flutter::DlColorSourceType::kRadialGradient: {
74  const flutter::DlRadialGradientColorSource* radialGradient =
75  color_source->asRadialGradient();
76  FML_DCHECK(radialGradient);
77  auto center = radialGradient->center();
78  auto radius = radialGradient->radius();
79  std::vector<Color> colors;
80  std::vector<float> stops;
81  skia_conversions::ConvertStops(radialGradient, colors, stops);
82 
83  auto tile_mode =
84  static_cast<Entity::TileMode>(radialGradient->tile_mode());
85  auto effect_transform = radialGradient->matrix();
86 
87  auto contents = std::make_shared<RadialGradientContents>();
88  contents->SetOpacityFactor(color.alpha);
89  contents->SetColors(std::move(colors));
90  contents->SetStops(std::move(stops));
91  contents->SetCenterAndRadius(center, radius);
92  contents->SetTileMode(tile_mode);
93  contents->SetEffectTransform(effect_transform);
94 
95  auto radius_pt = Point(radius, radius);
96  std::array<Point, 2> bounds{center + radius_pt, center - radius_pt};
97  auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
98  if (intrinsic_size.has_value()) {
99  contents->SetColorSourceSize(intrinsic_size->GetSize().Max({1, 1}));
100  }
101  return contents;
102  }
103  case flutter::DlColorSourceType::kConicalGradient: {
104  const flutter::DlConicalGradientColorSource* conical_gradient =
105  color_source->asConicalGradient();
106  FML_DCHECK(conical_gradient);
107  Point center = conical_gradient->end_center();
108  DlScalar radius = conical_gradient->end_radius();
109  Point focus_center = conical_gradient->start_center();
110  DlScalar focus_radius = conical_gradient->start_radius();
111  std::vector<Color> colors;
112  std::vector<float> stops;
113  skia_conversions::ConvertStops(conical_gradient, colors, stops);
114 
115  auto tile_mode =
116  static_cast<Entity::TileMode>(conical_gradient->tile_mode());
117  auto effect_transform = conical_gradient->matrix();
118 
119  std::shared_ptr<ConicalGradientContents> contents =
120  std::make_shared<ConicalGradientContents>();
121  contents->SetOpacityFactor(color.alpha);
122  contents->SetColors(std::move(colors));
123  contents->SetStops(std::move(stops));
124  contents->SetCenterAndRadius(center, radius);
125  contents->SetTileMode(tile_mode);
126  contents->SetEffectTransform(effect_transform);
127  contents->SetFocus(focus_center, focus_radius);
128 
129  auto radius_pt = Point(radius, radius);
130  std::array<Point, 2> bounds{center + radius_pt, center - radius_pt};
131  auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
132  if (intrinsic_size.has_value()) {
133  contents->SetColorSourceSize(intrinsic_size->GetSize().Max({1, 1}));
134  }
135  return contents;
136  }
137  case flutter::DlColorSourceType::kSweepGradient: {
138  const flutter::DlSweepGradientColorSource* sweepGradient =
139  color_source->asSweepGradient();
140  FML_DCHECK(sweepGradient);
141 
142  auto center = sweepGradient->center();
143  auto start_angle = Degrees(sweepGradient->start());
144  auto end_angle = Degrees(sweepGradient->end());
145  std::vector<Color> colors;
146  std::vector<float> stops;
147  skia_conversions::ConvertStops(sweepGradient, colors, stops);
148 
149  auto tile_mode =
150  static_cast<Entity::TileMode>(sweepGradient->tile_mode());
151  auto effect_transform = sweepGradient->matrix();
152 
153  auto contents = std::make_shared<SweepGradientContents>();
154  contents->SetOpacityFactor(color.alpha);
155  contents->SetCenterAndAngles(center, start_angle, end_angle);
156  contents->SetColors(std::move(colors));
157  contents->SetStops(std::move(stops));
158  contents->SetTileMode(tile_mode);
159  contents->SetEffectTransform(effect_transform);
160 
161  return contents;
162  }
163  case flutter::DlColorSourceType::kImage: {
164  const flutter::DlImageColorSource* image_color_source =
165  color_source->asImage();
166  FML_DCHECK(image_color_source &&
167  image_color_source->image()->impeller_texture());
168  auto texture = image_color_source->image()->impeller_texture();
169  auto x_tile_mode = static_cast<Entity::TileMode>(
170  image_color_source->horizontal_tile_mode());
171  auto y_tile_mode = static_cast<Entity::TileMode>(
172  image_color_source->vertical_tile_mode());
173  auto sampler_descriptor =
174  skia_conversions::ToSamplerDescriptor(image_color_source->sampling());
175  auto effect_transform = image_color_source->matrix();
176 
177  auto contents = std::make_shared<TiledTextureContents>();
178  contents->SetOpacityFactor(color.alpha);
179  contents->SetTexture(texture);
180  contents->SetTileModes(x_tile_mode, y_tile_mode);
181  contents->SetSamplerDescriptor(sampler_descriptor);
182  contents->SetEffectTransform(effect_transform);
183  if (color_filter || invert_colors) {
186  invert_colors = invert_colors](const FilterInput::Ref& input) {
187  if (invert_colors && color_filter) {
188  std::shared_ptr<FilterContents> color_filter_output =
190  color_filter, input,
192  return WrapWithInvertColors(
193  FilterInput::Make(color_filter_output),
195  }
196  if (color_filter) {
197  return WrapWithGPUColorFilter(
198  color_filter, input,
200  }
201  return WrapWithInvertColors(
203  };
204  contents->SetColorFilter(filter_proc);
205  }
206  contents->SetColorSourceSize(Size::Ceil(texture->GetSize()));
207  return contents;
208  }
209  case flutter::DlColorSourceType::kRuntimeEffect: {
210  const flutter::DlRuntimeEffectColorSource* runtime_effect_color_source =
211  color_source->asRuntimeEffect();
212  auto runtime_stage =
213  runtime_effect_color_source->runtime_effect()->runtime_stage();
214  auto uniform_data = runtime_effect_color_source->uniform_data();
215  auto samplers = runtime_effect_color_source->samplers();
216 
217  std::vector<RuntimeEffectContents::TextureInput> texture_inputs;
218 
219  for (auto& sampler : samplers) {
220  if (sampler == nullptr) {
221  return nullptr;
222  }
223  auto* image = sampler->asImage();
224  if (!sampler->asImage()) {
225  return nullptr;
226  }
227  FML_DCHECK(image->image()->impeller_texture());
228  texture_inputs.push_back({
229  .sampler_descriptor =
230  skia_conversions::ToSamplerDescriptor(image->sampling()),
231  .texture = image->image()->impeller_texture(),
232  });
233  }
234 
235  auto contents = std::make_shared<RuntimeEffectContents>();
236  contents->SetOpacityFactor(color.alpha);
237  contents->SetRuntimeStage(std::move(runtime_stage));
238  contents->SetUniformData(std::move(uniform_data));
239  contents->SetTextureInputs(std::move(texture_inputs));
240  return contents;
241  }
242  }
243  FML_UNREACHABLE();
244 }
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:32
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
std::function< std::shared_ptr< ColorFilterContents >(FilterInput::Ref)> ColorFilterProc
void ConvertStops(const flutter::DlGradientColorSourceBase *gradient, std::vector< Color > &colors, std::vector< float > &stops)
Convert display list colors + stops into impeller colors and stops, taking care to ensure that the st...
impeller::SamplerDescriptor ToSamplerDescriptor(const flutter::DlImageSampling options)
std::shared_ptr< ColorFilterContents > WrapWithGPUColorFilter(const flutter::DlColorFilter *filter, const std::shared_ptr< FilterInput > &input, ColorFilterContents::AbsorbOpacity absorb_opacity)
Definition: color_filter.cc:24
std::shared_ptr< ColorFilterContents > WrapWithInvertColors(const std::shared_ptr< FilterInput > &input, ColorFilterContents::AbsorbOpacity absorb_opacity)
Definition: color_filter.cc:16
TPoint< Scalar > Point
Definition: point.h:327
flutter::DlScalar DlScalar
Definition: dl_dispatcher.h:23
Scalar alpha
Definition: color.h:142
const flutter::DlColorFilter * color_filter
Definition: paint.h:76
const flutter::DlColorSource * color_source
Definition: paint.h:75
bool invert_colors
Definition: paint.h:85
Color color
Definition: paint.h:74
constexpr static std::optional< TRect > MakePointBounds(const U &value)
Definition: rect.h:165
constexpr TSize Ceil() const
Definition: size.h:114

References impeller::Color::alpha, impeller::TSize< Scalar >::Ceil(), color, color_filter, color_source, impeller::skia_conversions::ConvertStops(), invert_colors, impeller::ColorFilterContents::kNo, impeller::FilterInput::Make(), impeller::TRect< Scalar >::MakePointBounds(), impeller::skia_conversions::ToSamplerDescriptor(), impeller::WrapWithGPUColorFilter(), and impeller::WrapWithInvertColors().

◆ HasColorFilter()

bool impeller::Paint::HasColorFilter ( ) const

Whether this paint has a color filter that can apply opacity.

Definition at line 437 of file paint.cc.

437  {
438  return color_filter || invert_colors;
439 }

Referenced by impeller::Canvas::DrawImageRect().

◆ WithFilters()

std::shared_ptr< Contents > impeller::Paint::WithFilters ( std::shared_ptr< Contents input) const

Wrap this paint's configured filters to the given contents.

Parameters
[in]inputThe contents to wrap with paint's filters.
Returns
The filter-wrapped contents. If there are no filters that need to be wrapped for the current paint configuration, the original contents is returned.

Definition at line 246 of file paint.cc.

247  {
248  input = WithColorFilter(input, ColorFilterContents::AbsorbOpacity::kYes);
249  auto image_filter =
251  if (image_filter) {
252  input = image_filter;
253  }
254  return input;
255 }
const flutter::DlImageFilter * image_filter
Definition: paint.h:77
std::shared_ptr< FilterContents > WithImageFilter(const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
Definition: paint.cc:280

References image_filter, impeller::Entity::kDirect, impeller::ColorFilterContents::kYes, and WithImageFilter().

Referenced by impeller::Canvas::DrawAtlas(), impeller::Canvas::DrawImageRect(), impeller::Canvas::DrawTextFrame(), and impeller::Canvas::DrawVertices().

◆ WithFiltersForSubpassTarget()

std::shared_ptr< Contents > impeller::Paint::WithFiltersForSubpassTarget ( std::shared_ptr< Contents input,
const Matrix effect_transform = Matrix() 
) const

Wrap this paint's configured filters to the given contents of subpass target.

Parameters
[in]inputThe contents of subpass target to wrap with paint's filters.
Returns
The filter-wrapped contents. If there are no filters that need to be wrapped for the current paint configuration, the original contents is returned.

Definition at line 257 of file paint.cc.

259  {
260  auto image_filter =
261  WithImageFilter(input, effect_transform,
263  if (image_filter) {
264  input = image_filter;
265  }
266  input = WithColorFilter(input, ColorFilterContents::AbsorbOpacity::kYes);
267  return input;
268 }

References image_filter, impeller::Entity::kSubpassPrependSnapshotTransform, impeller::ColorFilterContents::kYes, and WithImageFilter().

◆ WithImageFilter()

std::shared_ptr< FilterContents > impeller::Paint::WithImageFilter ( const FilterInput::Variant input,
const Matrix effect_transform,
Entity::RenderingMode  rendering_mode 
) const

Definition at line 280 of file paint.cc.

283  {
284  if (!image_filter) {
285  return nullptr;
286  }
287  auto filter = WrapInput(image_filter, FilterInput::Make(input));
288  filter->SetRenderingMode(rendering_mode);
289  filter->SetEffectTransform(effect_transform);
290  return filter;
291 }
std::shared_ptr< FilterContents > WrapInput(const flutter::DlImageFilter *filter, const FilterInput::Ref &input)
Generate a new FilterContents using this filter's configuration.
Definition: image_filter.cc:17

References image_filter, impeller::FilterInput::Make(), and impeller::WrapInput().

Referenced by impeller::Canvas::SaveLayer(), WithFilters(), and WithFiltersForSubpassTarget().

◆ WithMaskBlur()

std::shared_ptr< Contents > impeller::Paint::WithMaskBlur ( std::shared_ptr< Contents input,
bool  is_solid_color,
const Matrix ctm 
) const

Definition at line 270 of file paint.cc.

272  {
273  if (mask_blur_descriptor.has_value()) {
274  input = mask_blur_descriptor->CreateMaskBlur(FilterInput::Make(input),
275  is_solid_color, ctm);
276  }
277  return input;
278 }
std::optional< MaskBlurDescriptor > mask_blur_descriptor
Definition: paint.h:87

References impeller::FilterInput::Make(), and mask_blur_descriptor.

Referenced by impeller::Canvas::DrawTextFrame().

Member Data Documentation

◆ blend_mode

◆ color

◆ color_filter

const flutter::DlColorFilter* impeller::Paint::color_filter = nullptr

◆ color_source

const flutter::DlColorSource* impeller::Paint::color_source = nullptr

◆ image_filter

const flutter::DlImageFilter* impeller::Paint::image_filter = nullptr

◆ invert_colors

bool impeller::Paint::invert_colors = false

◆ mask_blur_descriptor

◆ stroke_cap

◆ stroke_join

◆ stroke_miter

◆ stroke_width

◆ style


The documentation for this struct was generated from the following files: