Flutter Impeller
impeller::Paint Struct Reference

#include <paint.h>

Classes

struct  MaskBlurDescriptor
 

Public Types

enum  Style {
  Style::kFill,
  Style::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 30 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 34 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 = skia_conversions::ToPoint(linear->start_point());
50  auto end_point = skia_conversions::ToPoint(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 = skia_conversions::ToMatrix(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());
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 = skia_conversions::ToPoint(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 =
86  skia_conversions::ToMatrix(radialGradient->matrix());
87 
88  auto contents = std::make_shared<RadialGradientContents>();
89  contents->SetOpacityFactor(color.alpha);
90  contents->SetColors(std::move(colors));
91  contents->SetStops(std::move(stops));
92  contents->SetCenterAndRadius(center, radius);
93  contents->SetTileMode(tile_mode);
94  contents->SetEffectTransform(effect_transform);
95 
96  auto radius_pt = Point(radius, radius);
97  std::array<Point, 2> bounds{center + radius_pt, center - radius_pt};
98  auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
99  if (intrinsic_size.has_value()) {
100  contents->SetColorSourceSize(intrinsic_size->GetSize());
101  }
102  return contents;
103  }
104  case flutter::DlColorSourceType::kConicalGradient: {
105  const flutter::DlConicalGradientColorSource* conical_gradient =
106  color_source->asConicalGradient();
107  FML_DCHECK(conical_gradient);
108  Point center = skia_conversions::ToPoint(conical_gradient->end_center());
109  DlScalar radius = conical_gradient->end_radius();
110  Point focus_center =
111  skia_conversions::ToPoint(conical_gradient->start_center());
112  DlScalar focus_radius = conical_gradient->start_radius();
113  std::vector<Color> colors;
114  std::vector<float> stops;
115  skia_conversions::ConvertStops(conical_gradient, colors, stops);
116 
117  auto tile_mode =
118  static_cast<Entity::TileMode>(conical_gradient->tile_mode());
119  auto effect_transform =
120  skia_conversions::ToMatrix(conical_gradient->matrix());
121 
122  std::shared_ptr<ConicalGradientContents> contents =
123  std::make_shared<ConicalGradientContents>();
124  contents->SetOpacityFactor(color.alpha);
125  contents->SetColors(std::move(colors));
126  contents->SetStops(std::move(stops));
127  contents->SetCenterAndRadius(center, radius);
128  contents->SetTileMode(tile_mode);
129  contents->SetEffectTransform(effect_transform);
130  contents->SetFocus(focus_center, focus_radius);
131 
132  auto radius_pt = Point(radius, radius);
133  std::array<Point, 2> bounds{center + radius_pt, center - radius_pt};
134  auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
135  if (intrinsic_size.has_value()) {
136  contents->SetColorSourceSize(intrinsic_size->GetSize());
137  }
138  return contents;
139  }
140  case flutter::DlColorSourceType::kSweepGradient: {
141  const flutter::DlSweepGradientColorSource* sweepGradient =
142  color_source->asSweepGradient();
143  FML_DCHECK(sweepGradient);
144 
145  auto center = skia_conversions::ToPoint(sweepGradient->center());
146  auto start_angle = Degrees(sweepGradient->start());
147  auto end_angle = Degrees(sweepGradient->end());
148  std::vector<Color> colors;
149  std::vector<float> stops;
150  skia_conversions::ConvertStops(sweepGradient, colors, stops);
151 
152  auto tile_mode =
153  static_cast<Entity::TileMode>(sweepGradient->tile_mode());
154  auto effect_transform =
155  skia_conversions::ToMatrix(sweepGradient->matrix());
156 
157  auto contents = std::make_shared<SweepGradientContents>();
158  contents->SetOpacityFactor(color.alpha);
159  contents->SetCenterAndAngles(center, start_angle, end_angle);
160  contents->SetColors(std::move(colors));
161  contents->SetStops(std::move(stops));
162  contents->SetTileMode(tile_mode);
163  contents->SetEffectTransform(effect_transform);
164 
165  return contents;
166  }
167  case flutter::DlColorSourceType::kImage: {
168  const flutter::DlImageColorSource* image_color_source =
169  color_source->asImage();
170  FML_DCHECK(image_color_source &&
171  image_color_source->image()->impeller_texture());
172  auto texture = image_color_source->image()->impeller_texture();
173  auto x_tile_mode = static_cast<Entity::TileMode>(
174  image_color_source->horizontal_tile_mode());
175  auto y_tile_mode = static_cast<Entity::TileMode>(
176  image_color_source->vertical_tile_mode());
177  auto sampler_descriptor =
178  skia_conversions::ToSamplerDescriptor(image_color_source->sampling());
179  auto effect_transform =
180  skia_conversions::ToMatrix(image_color_source->matrix());
181 
182  auto contents = std::make_shared<TiledTextureContents>();
183  contents->SetOpacityFactor(color.alpha);
184  contents->SetTexture(texture);
185  contents->SetTileModes(x_tile_mode, y_tile_mode);
186  contents->SetSamplerDescriptor(sampler_descriptor);
187  contents->SetEffectTransform(effect_transform);
188  if (color_filter || invert_colors) {
191  invert_colors = invert_colors](const FilterInput::Ref& input) {
192  if (invert_colors && color_filter) {
193  std::shared_ptr<FilterContents> color_filter_output =
195  color_filter, input,
197  return WrapWithInvertColors(
198  FilterInput::Make(color_filter_output),
200  }
201  if (color_filter) {
202  return WrapWithGPUColorFilter(
203  color_filter, input,
205  }
206  return WrapWithInvertColors(
208  };
209  contents->SetColorFilter(filter_proc);
210  }
211  contents->SetColorSourceSize(Size::Ceil(texture->GetSize()));
212  return contents;
213  }
214  case flutter::DlColorSourceType::kRuntimeEffect: {
215  const flutter::DlRuntimeEffectColorSource* runtime_effect_color_source =
216  color_source->asRuntimeEffect();
217  auto runtime_stage =
218  runtime_effect_color_source->runtime_effect()->runtime_stage();
219  auto uniform_data = runtime_effect_color_source->uniform_data();
220  auto samplers = runtime_effect_color_source->samplers();
221 
222  std::vector<RuntimeEffectContents::TextureInput> texture_inputs;
223 
224  for (auto& sampler : samplers) {
225  if (sampler == nullptr) {
226  return nullptr;
227  }
228  auto* image = sampler->asImage();
229  if (!sampler->asImage()) {
230  return nullptr;
231  }
232  FML_DCHECK(image->image()->impeller_texture());
233  texture_inputs.push_back({
234  .sampler_descriptor =
235  skia_conversions::ToSamplerDescriptor(image->sampling()),
236  .texture = image->image()->impeller_texture(),
237  });
238  }
239 
240  auto contents = std::make_shared<RuntimeEffectContents>();
241  contents->SetOpacityFactor(color.alpha);
242  contents->SetRuntimeStage(std::move(runtime_stage));
243  contents->SetUniformData(std::move(uniform_data));
244  contents->SetTextureInputs(std::move(texture_inputs));
245  return contents;
246  }
247  case flutter::DlColorSourceType::kColor: {
248  auto contents = std::make_shared<SolidColorContents>();
249  contents->SetColor(color);
250  return contents;
251  }
252  }
253  FML_UNREACHABLE();
254 }

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::ToMatrix(), impeller::skia_conversions::ToPoint(), 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 447 of file paint.cc.

447  {
448  return color_filter || invert_colors;
449 }

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 256 of file paint.cc.

257  {
258  input = WithColorFilter(input, ColorFilterContents::AbsorbOpacity::kYes);
259  auto image_filter =
261  if (image_filter) {
262  input = image_filter;
263  }
264  return input;
265 }

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 267 of file paint.cc.

269  {
270  auto image_filter =
271  WithImageFilter(input, effect_transform,
273  if (image_filter) {
274  input = image_filter;
275  }
276  input = WithColorFilter(input, ColorFilterContents::AbsorbOpacity::kYes);
277  return input;
278 }

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 290 of file paint.cc.

293  {
294  if (!image_filter) {
295  return nullptr;
296  }
297  auto filter = WrapInput(image_filter, FilterInput::Make(input));
298  filter->SetRenderingMode(rendering_mode);
299  filter->SetEffectTransform(effect_transform);
300  return filter;
301 }

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 280 of file paint.cc.

282  {
283  if (mask_blur_descriptor.has_value()) {
284  input = mask_blur_descriptor->CreateMaskBlur(FilterInput::Make(input),
285  is_solid_color, ctm);
286  }
287  return input;
288 }

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:
impeller::skia_conversions::ConvertStops
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...
Definition: skia_conversions.cc:144
impeller::skia_conversions::ToSamplerDescriptor
impeller::SamplerDescriptor ToSamplerDescriptor(const flutter::DlImageSampling options)
Definition: skia_conversions.cc:169
impeller::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
impeller::Paint::color
Color color
Definition: paint.h:74
impeller::WrapWithInvertColors
std::shared_ptr< ColorFilterContents > WrapWithInvertColors(const std::shared_ptr< FilterInput > &input, ColorFilterContents::AbsorbOpacity absorb_opacity)
Definition: color_filter.cc:16
impeller::FilterInput::Ref
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:32
impeller::TSize< Scalar >::Ceil
constexpr TSize Ceil() const
Definition: size.h:96
impeller::skia_conversions::ToMatrix
Matrix ToMatrix(const SkMatrix &m)
Definition: skia_conversions.cc:193
impeller::ColorFilterContents::AbsorbOpacity::kNo
@ kNo
impeller::Color::alpha
Scalar alpha
Definition: color.h:142
impeller::Paint::color_filter
const flutter::DlColorFilter * color_filter
Definition: paint.h:76
impeller::TiledTextureContents::ColorFilterProc
std::function< std::shared_ptr< ColorFilterContents >(FilterInput::Ref)> ColorFilterProc
Definition: tiled_texture_contents.h:26
impeller::PolygonMode::kFill
@ kFill
impeller::TRect< Scalar >::MakePointBounds
constexpr static std::optional< TRect > MakePointBounds(const U &value)
Definition: rect.h:155
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
impeller::WrapWithGPUColorFilter
std::shared_ptr< ColorFilterContents > WrapWithGPUColorFilter(const flutter::DlColorFilter *filter, const std::shared_ptr< FilterInput > &input, ColorFilterContents::AbsorbOpacity absorb_opacity)
Definition: color_filter.cc:24
impeller::Paint::color_source
const flutter::DlColorSource * color_source
Definition: paint.h:75
impeller::Entity::RenderingMode::kDirect
@ kDirect
impeller::Paint::image_filter
const flutter::DlImageFilter * image_filter
Definition: paint.h:77
impeller::skia_conversions::ToPoint
Point ToPoint(const SkPoint &point)
Definition: skia_conversions.cc:98
impeller::Entity::TileMode
TileMode
Definition: entity.h:42
impeller::WrapInput
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:15
impeller::Paint::WithImageFilter
std::shared_ptr< FilterContents > WithImageFilter(const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
Definition: paint.cc:290
impeller::Entity::RenderingMode::kSubpassPrependSnapshotTransform
@ kSubpassPrependSnapshotTransform
impeller::Paint::invert_colors
bool invert_colors
Definition: paint.h:85
impeller::ColorFilterContents::AbsorbOpacity::kYes
@ kYes
impeller::DlScalar
flutter::DlScalar DlScalar
Definition: dl_dispatcher.h:21
impeller::Paint::mask_blur_descriptor
std::optional< MaskBlurDescriptor > mask_blur_descriptor
Definition: paint.h:87
impeller::BlendMode::kSourceOver
@ kSourceOver