Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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::optional< StrokeParametersGetStroke () const
 Return an optional StrokeParameters if this Paint is a stroked Paint, otherwise return a nullopt.
 
std::shared_ptr< ContentsWithFilters (const ContentContext &renderer, std::shared_ptr< Contents > input) const
 Wrap this paint's configured filters to the given contents.
 
std::shared_ptr< ContentsWithFiltersForSubpassTarget (const ContentContext &renderer, std::shared_ptr< Contents > input, const Matrix &effect_transform=Matrix()) const
 Wrap this paint's configured filters to the given contents of subpass target.
 
bool HasColorFilter () const
 Whether this paint has a color filter that can apply opacity.
 
std::shared_ptr< ColorSourceContentsCreateContents (const ContentContext &renderer, const Geometry *geometry) const
 
std::shared_ptr< ContentsWithMaskBlur (std::shared_ptr< Contents > input, bool is_solid_color, const Matrix &ctm) const
 
std::shared_ptr< FilterContentsWithImageFilter (const ContentContext &renderer, 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.
 
static 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 stops monotonically increase from 0.0 to 1.0.
 

Public Attributes

Color color = Color::Black()
 
const flutter::DlColorSourcecolor_source = nullptr
 
const flutter::DlColorFiltercolor_filter = nullptr
 
const flutter::DlImageFilterimage_filter = nullptr
 
StrokeParameters stroke
 
Style style = Style::kFill
 
BlendMode blend_mode = BlendMode::kSrcOver
 
bool invert_colors = false
 
bool anti_alias = true
 
std::optional< MaskBlurDescriptormask_blur_descriptor
 

Detailed Description

Definition at line 29 of file paint.h.

Member Typedef Documentation

◆ ColorSourceProc

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

Definition at line 38 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

enum class impeller::Paint::Style
strong
Enumerator
kFill 
kStroke 

Definition at line 49 of file paint.h.

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 42 of file paint.h.

42 {
43 return paint.blend_mode == BlendMode::kSrcOver &&
44 paint.invert_colors == false &&
45 !paint.mask_blur_descriptor.has_value() &&
46 paint.image_filter == nullptr && paint.color_filter == nullptr;
47 }

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

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

◆ ConvertStops()

void impeller::Paint::ConvertStops ( const flutter::DlGradientColorSourceBase gradient,
std::vector< Color > &  colors,
std::vector< float > &  stops 
)
static

Convert display list colors + stops into impeller colors and stops, taking care to ensure that the stops monotonically increase from 0.0 to 1.0.

The general process is:

  • Ensure that the first gradient stop value is 0.0. If not, insert a new stop with a value of 0.0 and use the first gradient color as this new stops color.
  • Ensure the last gradient stop value is 1.0. If not, insert a new stop with a value of 1.0 and use the last gradient color as this stops color.
  • Clamp all gradient values between the values of 0.0 and 1.0.
  • For all stop values, ensure that the values are monotonically increasing by clamping each value to a minimum of the previous stop value and itself. For example, with stop values of 0.0, 0.5, 0.4, 1.0, we would clamp such that the values were 0.0, 0.5, 0.5, 1.0.

Definition at line 39 of file paint.cc.

41 {
42 FML_DCHECK(gradient->stop_count() >= 2)
43 << "stop_count:" << gradient->stop_count();
44
45 auto* dl_colors = gradient->colors();
46 auto* dl_stops = gradient->stops();
47 if (dl_stops[0] != 0.0) {
48 colors.emplace_back(skia_conversions::ToColor(dl_colors[0]));
49 stops.emplace_back(0);
50 }
51 for (auto i = 0; i < gradient->stop_count(); i++) {
52 colors.emplace_back(skia_conversions::ToColor(dl_colors[i]));
53 stops.emplace_back(std::clamp(dl_stops[i], 0.0f, 1.0f));
54 }
55 if (dl_stops[gradient->stop_count() - 1] != 1.0) {
56 colors.emplace_back(colors.back());
57 stops.emplace_back(1.0);
58 }
59 for (auto i = 1; i < gradient->stop_count(); i++) {
60 stops[i] = std::clamp(stops[i], stops[i - 1], stops[i]);
61 }
62}
#define FML_DCHECK(condition)
Definition logging.h:122
Color ToColor(const flutter::DlColor &color)

References flutter::DlGradientColorSourceBase::colors(), FML_DCHECK, i, flutter::DlGradientColorSourceBase::stop_count(), flutter::DlGradientColorSourceBase::stops(), and impeller::skia_conversions::ToColor().

Referenced by CreateContents(), impeller::testing::TEST(), impeller::testing::TEST(), impeller::testing::TEST(), impeller::testing::TEST(), and impeller::testing::TEST().

◆ CreateContents()

std::shared_ptr< ColorSourceContents > impeller::Paint::CreateContents ( const ContentContext renderer,
const Geometry geometry 
) const

Definition at line 64 of file paint.cc.

66 {
67 if (color_source == nullptr) {
68 auto contents = std::make_shared<SolidColorContents>(geometry);
69 contents->SetColor(color);
70 return contents;
71 }
72
73 switch (color_source->type()) {
77 FML_DCHECK(linear);
78 auto start_point = linear->start_point();
79 auto end_point = linear->end_point();
80 std::vector<Color> colors;
81 std::vector<float> stops;
82 ConvertStops(linear, colors, stops);
83
84 auto tile_mode = static_cast<Entity::TileMode>(linear->tile_mode());
85 auto effect_transform = linear->matrix();
86
87 auto contents = std::make_shared<LinearGradientContents>(geometry);
88 contents->SetOpacityFactor(color.alpha);
89 contents->SetColors(std::move(colors));
90 contents->SetStops(std::move(stops));
91 contents->SetEndPoints(start_point, end_point);
92 contents->SetTileMode(tile_mode);
93 contents->SetEffectTransform(effect_transform);
94
95 std::array<Point, 2> bounds{start_point, end_point};
96 auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
97 if (intrinsic_size.has_value()) {
98 contents->SetColorSourceSize(intrinsic_size->GetSize().Max({1, 1}));
99 }
100 return contents;
101 }
103 const flutter::DlRadialGradientColorSource* radialGradient =
105 FML_DCHECK(radialGradient);
106 auto center = radialGradient->center();
107 auto radius = radialGradient->radius();
108 std::vector<Color> colors;
109 std::vector<float> stops;
110 ConvertStops(radialGradient, colors, stops);
111
112 auto tile_mode =
113 static_cast<Entity::TileMode>(radialGradient->tile_mode());
114 auto effect_transform = radialGradient->matrix();
115
116 auto contents = std::make_shared<RadialGradientContents>(geometry);
117 contents->SetOpacityFactor(color.alpha);
118 contents->SetColors(std::move(colors));
119 contents->SetStops(std::move(stops));
120 contents->SetCenterAndRadius(center, radius);
121 contents->SetTileMode(tile_mode);
122 contents->SetEffectTransform(effect_transform);
123
124 auto intrinsic_size = Rect::MakeCircleBounds(center, std::abs(radius));
125 contents->SetColorSourceSize(intrinsic_size.GetSize().Max({1, 1}));
126 return contents;
127 }
129 const flutter::DlConicalGradientColorSource* conical_gradient =
131 FML_DCHECK(conical_gradient);
132 Point center = conical_gradient->end_center();
133 DlScalar radius = conical_gradient->end_radius();
134 Point focus_center = conical_gradient->start_center();
135 DlScalar focus_radius = conical_gradient->start_radius();
136 std::vector<Color> colors;
137 std::vector<float> stops;
138 ConvertStops(conical_gradient, colors, stops);
139
140 auto tile_mode =
141 static_cast<Entity::TileMode>(conical_gradient->tile_mode());
142 auto effect_transform = conical_gradient->matrix();
143
144 auto contents = std::make_shared<ConicalGradientContents>(geometry);
145 contents->SetOpacityFactor(color.alpha);
146 contents->SetColors(std::move(colors));
147 contents->SetStops(std::move(stops));
148 contents->SetCenterAndRadius(center, radius);
149 contents->SetTileMode(tile_mode);
150 contents->SetEffectTransform(effect_transform);
151 contents->SetFocus(focus_center, focus_radius);
152
153 auto intrinsic_size = Rect::MakeCircleBounds(center, std::abs(radius));
154 contents->SetColorSourceSize(intrinsic_size.GetSize().Max({1, 1}));
155 return contents;
156 }
158 const flutter::DlSweepGradientColorSource* sweepGradient =
160 FML_DCHECK(sweepGradient);
161
162 auto center = sweepGradient->center();
163 auto start_angle = Degrees(sweepGradient->start());
164 auto end_angle = Degrees(sweepGradient->end());
165 std::vector<Color> colors;
166 std::vector<float> stops;
167 ConvertStops(sweepGradient, colors, stops);
168
169 auto tile_mode =
170 static_cast<Entity::TileMode>(sweepGradient->tile_mode());
171 auto effect_transform = sweepGradient->matrix();
172
173 auto contents = std::make_shared<SweepGradientContents>(geometry);
174 contents->SetOpacityFactor(color.alpha);
175 contents->SetCenterAndAngles(center, start_angle, end_angle);
176 contents->SetColors(std::move(colors));
177 contents->SetStops(std::move(stops));
178 contents->SetTileMode(tile_mode);
179 contents->SetEffectTransform(effect_transform);
180
181 return contents;
182 }
184 const flutter::DlImageColorSource* image_color_source =
186 FML_DCHECK(image_color_source);
187 auto texture =
188 image_color_source->image()->asImpellerImage()->GetCachedTexture(
189 renderer);
191 auto x_tile_mode = static_cast<Entity::TileMode>(
192 image_color_source->horizontal_tile_mode());
193 auto y_tile_mode = static_cast<Entity::TileMode>(
194 image_color_source->vertical_tile_mode());
195 auto sampler_descriptor =
196 skia_conversions::ToSamplerDescriptor(image_color_source->sampling());
197 // See https://github.com/flutter/flutter/issues/165205
198 flutter::DlMatrix effect_transform = image_color_source->matrix().To3x3();
199
200 auto contents = std::make_shared<TiledTextureContents>(geometry);
201 contents->SetOpacityFactor(color.alpha);
202 contents->SetTexture(texture);
203 contents->SetTileModes(x_tile_mode, y_tile_mode);
204 contents->SetSamplerDescriptor(sampler_descriptor);
205 contents->SetEffectTransform(effect_transform);
211 std::shared_ptr<FilterContents> color_filter_output =
216 FilterInput::Make(color_filter_output),
218 }
219 if (color_filter) {
223 }
226 };
227 contents->SetColorFilter(filter_proc);
228 }
229 contents->SetColorSourceSize(Size::Ceil(texture->GetSize()));
230 return contents;
231 }
233 const flutter::DlRuntimeEffectColorSource* runtime_effect_color_source =
235 auto runtime_stage =
236 runtime_effect_color_source->runtime_effect()->runtime_stage();
237 auto uniform_data = runtime_effect_color_source->uniform_data();
238 auto samplers = runtime_effect_color_source->samplers();
239
240 std::vector<RuntimeEffectContents::TextureInput> texture_inputs;
241
242 for (auto& sampler : samplers) {
243 if (sampler == nullptr) {
244 VALIDATION_LOG << "Runtime effect sampler is null";
245 auto contents = std::make_shared<SolidColorContents>(geometry);
246 contents->SetColor(Color::BlackTransparent());
247 return contents;
248 }
249 auto* image = sampler->asImage();
250 if (!sampler->asImage()) {
251 VALIDATION_LOG << "Runtime effect sampler is not an image";
252 auto contents = std::make_shared<SolidColorContents>(geometry);
253 contents->SetColor(Color::BlackTransparent());
254 return contents;
255 }
256 auto texture =
257 image->image()->asImpellerImage()->GetCachedTexture(renderer);
259 texture_inputs.push_back({
260 .sampler_descriptor =
262 .texture = std::move(texture),
263 });
264 }
265
266 auto contents = std::make_shared<RuntimeEffectContents>(geometry);
267 contents->SetOpacityFactor(color.alpha);
268 contents->SetRuntimeStage(std::move(runtime_stage));
269 contents->SetUniformData(std::move(uniform_data));
270 contents->SetTextureInputs(std::move(texture_inputs));
271 return contents;
272 }
273 }
275}
virtual T type() const =0
virtual const DlRuntimeEffectColorSource * asRuntimeEffect() const
virtual const DlRadialGradientColorSource * asRadialGradient() const
virtual const DlLinearGradientColorSource * asLinearGradient() const
virtual const DlImageColorSource * asImage() const
virtual const DlSweepGradientColorSource * asSweepGradient() const
virtual const DlConicalGradientColorSource * asConicalGradient() const
DlImageSampling sampling() const
DlTileMode horizontal_tile_mode() const
sk_sp< const DlImage > image() const
const std::shared_ptr< std::vector< uint8_t > > uniform_data() const
const sk_sp< DlRuntimeEffect > runtime_effect() const
const std::vector< std::shared_ptr< DlColorSource > > samplers() const
std::shared_ptr< FilterInput > Ref
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
std::function< std::shared_ptr< ColorFilterContents >(FilterInput::Ref)> ColorFilterProc
static int input(yyscan_t yyscanner)
FlutterVulkanImage * image
#define FML_UNREACHABLE()
Definition logging.h:128
FlTexture * texture
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)
std::shared_ptr< ColorFilterContents > WrapWithInvertColors(const std::shared_ptr< FilterInput > &input, ColorFilterContents::AbsorbOpacity absorb_opacity)
TPoint< Scalar > Point
Definition point.h:426
flutter::DlScalar DlScalar
FlutterVulkanImageHandle image
Definition embedder.h:938
static constexpr Color BlackTransparent()
Definition color.h:275
Scalar alpha
Definition color.h:143
A 4x4 matrix using column-major storage.
Definition matrix.h:37
constexpr Matrix To3x3() const
Definition matrix.h:252
const flutter::DlColorFilter * color_filter
Definition paint.h:81
const flutter::DlColorSource * color_source
Definition paint.h:80
static 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 paint.cc:39
bool invert_colors
Definition paint.h:87
Color color
Definition paint.h:79
static constexpr TRect MakeCircleBounds(const TPoint< Type > &center, Type radius)
Definition rect.h:156
static constexpr std::optional< TRect > MakePointBounds(const U &value)
Definition rect.h:189
constexpr TSize Ceil() const
Definition size.h:114
#define VALIDATION_LOG
Definition validation.h:91

References impeller::Color::alpha, flutter::DlColorSource::asConicalGradient(), flutter::DlColorSource::asImage(), flutter::DlColorSource::asLinearGradient(), flutter::DlColorSource::asRadialGradient(), flutter::DlColorSource::asRuntimeEffect(), flutter::DlColorSource::asSweepGradient(), impeller::Color::BlackTransparent(), impeller::TSize< Scalar >::Ceil(), flutter::DlRadialGradientColorSource::center(), flutter::DlSweepGradientColorSource::center(), color, color_filter, color_source, ConvertStops(), flutter::DlSweepGradientColorSource::end(), flutter::DlConicalGradientColorSource::end_center(), flutter::DlLinearGradientColorSource::end_point(), flutter::DlConicalGradientColorSource::end_radius(), FML_DCHECK, FML_UNREACHABLE, flutter::DlImageColorSource::horizontal_tile_mode(), flutter::DlImageColorSource::image(), FlutterVulkanImage::image, image, input(), invert_colors, flutter::kConicalGradient, flutter::kImage, flutter::kLinearGradient, impeller::ColorFilterContents::kNo, flutter::kRadialGradient, flutter::kRuntimeEffect, flutter::kSweepGradient, impeller::FilterInput::Make(), impeller::TRect< Scalar >::MakeCircleBounds(), impeller::TRect< Scalar >::MakePointBounds(), flutter::DlMatrixColorSourceBase::matrix(), flutter::DlRadialGradientColorSource::radius(), flutter::DlRuntimeEffectColorSource::runtime_effect(), flutter::DlRuntimeEffectColorSource::samplers(), flutter::DlImageColorSource::sampling(), flutter::DlSweepGradientColorSource::start(), flutter::DlConicalGradientColorSource::start_center(), flutter::DlLinearGradientColorSource::start_point(), flutter::DlConicalGradientColorSource::start_radius(), texture, flutter::DlGradientColorSourceBase::tile_mode(), impeller::Matrix::To3x3(), impeller::skia_conversions::ToSamplerDescriptor(), flutter::DlAttribute< D, T >::type(), flutter::DlRuntimeEffectColorSource::uniform_data(), VALIDATION_LOG, flutter::DlImageColorSource::vertical_tile_mode(), impeller::WrapWithGPUColorFilter(), and impeller::WrapWithInvertColors().

Referenced by impeller::Paint::MaskBlurDescriptor::CreateMaskBlur().

◆ GetStroke()

std::optional< StrokeParameters > impeller::Paint::GetStroke ( ) const
inline

Return an optional StrokeParameters if this Paint is a stroked Paint, otherwise return a nullopt.

Returns
An optional set of StrokeParameters

Definition at line 95 of file paint.h.

95 {
96 return (style == Style::kStroke) ? std::optional(stroke) : std::nullopt;
97 }
Definition ref_ptr.h:261
Style style
Definition paint.h:85
StrokeParameters stroke
Definition paint.h:84

References kStroke, stroke, and style.

Referenced by impeller::Canvas::DrawCircle(), impeller::Canvas::DrawRect(), impeller::Canvas::DrawRoundSuperellipse(), impeller::FirstPassDispatcher::drawText(), impeller::Canvas::DrawTextFrame(), impeller::testing::TEST(), and impeller::testing::TEST().

◆ HasColorFilter()

bool impeller::Paint::HasColorFilter ( ) const

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

Definition at line 471 of file paint.cc.

471 {
472 return color_filter || invert_colors;
473}

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

◆ WithFilters()

std::shared_ptr< Contents > impeller::Paint::WithFilters ( const ContentContext renderer,
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 277 of file paint.cc.

279 {
281 auto image_filter = WithImageFilter(renderer, input, Matrix(),
283 if (image_filter) {
285 }
286 return input;
287}
std::shared_ptr< FilterContents > WithImageFilter(const ContentContext &renderer, const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
Definition paint.cc:313
const flutter::DlImageFilter * image_filter
Definition paint.h:82

References image_filter, input(), 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 ( const ContentContext renderer,
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 289 of file paint.cc.

292 {
293 auto image_filter =
294 WithImageFilter(renderer, input, effect_transform,
296 if (image_filter) {
298 }
300 return input;
301}

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

◆ WithImageFilter()

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

Definition at line 313 of file paint.cc.

317 {
318 if (!image_filter) {
319 return nullptr;
320 }
321 auto filter = WrapInput(renderer, image_filter, FilterInput::Make(input));
322 filter->SetRenderingMode(rendering_mode);
323 filter->SetEffectTransform(effect_transform);
324 return filter;
325}
std::shared_ptr< FilterContents > WrapInput(const ContentContext &renderer, const flutter::DlImageFilter *filter, const FilterInput::Ref &input)
Generate a new FilterContents using this filter's configuration.

References image_filter, input(), 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 303 of file paint.cc.

305 {
306 if (mask_blur_descriptor.has_value()) {
308 is_solid_color, ctm);
309 }
310 return input;
311}
std::optional< MaskBlurDescriptor > mask_blur_descriptor
Definition paint.h:90

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

Member Data Documentation

◆ anti_alias

bool impeller::Paint::anti_alias = true

◆ blend_mode

◆ color

◆ color_filter

◆ color_source

◆ image_filter

◆ invert_colors

◆ mask_blur_descriptor

◆ stroke

◆ style


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