Flutter Impeller
color_source.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <memory>
8 #include <vector>
9 
10 #include "impeller/aiks/paint.h"
24 
25 #if IMPELLER_ENABLE_3D
26 #include "impeller/entity/contents/scene_contents.h" // nogncheck
27 #include "impeller/scene/node.h" // nogncheck
28 #endif // IMPELLER_ENABLE_3D
29 
30 namespace impeller {
31 
33  : proc_([](const Paint& paint) -> std::shared_ptr<ColorSourceContents> {
34  auto contents = std::make_shared<SolidColorContents>();
35  contents->SetColor(paint.color);
36  return contents;
37  }){};
38 
39 ColorSource::~ColorSource() = default;
40 
42  return {};
43 }
44 
46  Point end_point,
47  std::vector<Color> colors,
48  std::vector<Scalar> stops,
49  Entity::TileMode tile_mode,
50  Matrix effect_transform) {
51  ColorSource result;
52  result.type_ = Type::kLinearGradient;
53  result.proc_ = [start_point, end_point, colors = std::move(colors),
54  stops = std::move(stops), tile_mode,
55  effect_transform](const Paint& paint) {
56  auto contents = std::make_shared<LinearGradientContents>();
57  contents->SetOpacityFactor(paint.color.alpha);
58  contents->SetColors(colors);
59  contents->SetStops(stops);
60  contents->SetEndPoints(start_point, end_point);
61  contents->SetTileMode(tile_mode);
62  contents->SetEffectTransform(effect_transform);
63 
64  std::vector<Point> bounds{start_point, end_point};
65  auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
66  if (intrinsic_size.has_value()) {
67  contents->SetColorSourceSize(intrinsic_size->GetSize());
68  }
69  return contents;
70  };
71  return result;
72 }
73 
75  Scalar radius,
76  std::vector<Color> colors,
77  std::vector<Scalar> stops,
78  Point focus_center,
79  Scalar focus_radius,
80  Entity::TileMode tile_mode,
81  Matrix effect_transform) {
82  ColorSource result;
83  result.type_ = Type::kConicalGradient;
84  result.proc_ = [center, radius, colors = std::move(colors),
85  stops = std::move(stops), focus_center, focus_radius,
86  tile_mode, effect_transform](const Paint& paint) {
87  std::shared_ptr<ConicalGradientContents> contents =
88  std::make_shared<ConicalGradientContents>();
89  contents->SetOpacityFactor(paint.color.alpha);
90  contents->SetColors(colors);
91  contents->SetStops(stops);
92  contents->SetCenterAndRadius(center, radius);
93  contents->SetTileMode(tile_mode);
94  contents->SetEffectTransform(effect_transform);
95  contents->SetFocus(focus_center, focus_radius);
96 
97  auto radius_pt = Point(radius, radius);
98  std::vector<Point> bounds{center + radius_pt, center - radius_pt};
99  auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
100  if (intrinsic_size.has_value()) {
101  contents->SetColorSourceSize(intrinsic_size->GetSize());
102  }
103  return contents;
104  };
105  return result;
106 }
107 
109  Scalar radius,
110  std::vector<Color> colors,
111  std::vector<Scalar> stops,
112  Entity::TileMode tile_mode,
113  Matrix effect_transform) {
114  ColorSource result;
115  result.type_ = Type::kRadialGradient;
116  result.proc_ = [center, radius, colors = std::move(colors),
117  stops = std::move(stops), tile_mode,
118  effect_transform](const Paint& paint) {
119  auto contents = std::make_shared<RadialGradientContents>();
120  contents->SetOpacityFactor(paint.color.alpha);
121  contents->SetColors(colors);
122  contents->SetStops(stops);
123  contents->SetCenterAndRadius(center, radius);
124  contents->SetTileMode(tile_mode);
125  contents->SetEffectTransform(effect_transform);
126 
127  auto radius_pt = Point(radius, radius);
128  std::vector<Point> bounds{center + radius_pt, center - radius_pt};
129  auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
130  if (intrinsic_size.has_value()) {
131  contents->SetColorSourceSize(intrinsic_size->GetSize());
132  }
133  return contents;
134  };
135  return result;
136 }
137 
139  Degrees start_angle,
140  Degrees end_angle,
141  std::vector<Color> colors,
142  std::vector<Scalar> stops,
143  Entity::TileMode tile_mode,
144  Matrix effect_transform) {
145  ColorSource result;
146  result.type_ = Type::kSweepGradient;
147  result.proc_ = [center, start_angle, end_angle, colors = std::move(colors),
148  stops = std::move(stops), tile_mode,
149  effect_transform](const Paint& paint) {
150  auto contents = std::make_shared<SweepGradientContents>();
151  contents->SetOpacityFactor(paint.color.alpha);
152  contents->SetCenterAndAngles(center, start_angle, end_angle);
153  contents->SetColors(colors);
154  contents->SetStops(stops);
155  contents->SetTileMode(tile_mode);
156  contents->SetEffectTransform(effect_transform);
157 
158  return contents;
159  };
160  return result;
161 }
162 
163 ColorSource ColorSource::MakeImage(std::shared_ptr<Texture> texture,
164  Entity::TileMode x_tile_mode,
165  Entity::TileMode y_tile_mode,
166  SamplerDescriptor sampler_descriptor,
167  Matrix effect_transform) {
168  ColorSource result;
169  result.type_ = Type::kImage;
170  result.proc_ = [texture = std::move(texture), x_tile_mode, y_tile_mode,
171  sampler_descriptor = std::move(sampler_descriptor),
172  effect_transform](const Paint& paint) {
173  auto contents = std::make_shared<TiledTextureContents>();
174  contents->SetOpacityFactor(paint.color.alpha);
175  contents->SetTexture(texture);
176  contents->SetTileModes(x_tile_mode, y_tile_mode);
177  contents->SetSamplerDescriptor(sampler_descriptor);
178  contents->SetEffectTransform(effect_transform);
179  if (paint.color_filter) {
181  [color_filter = paint.color_filter](FilterInput::Ref input) {
182  return color_filter->WrapWithGPUColorFilter(
183  std::move(input), ColorFilterContents::AbsorbOpacity::kNo);
184  };
185  contents->SetColorFilter(filter_proc);
186  }
187  contents->SetColorSourceSize(Size::Ceil(texture->GetSize()));
188  return contents;
189  };
190  return result;
191 }
192 
194  std::shared_ptr<RuntimeStage> runtime_stage,
195  std::shared_ptr<std::vector<uint8_t>> uniform_data,
196  std::vector<RuntimeEffectContents::TextureInput> texture_inputs) {
197  ColorSource result;
198  result.type_ = Type::kRuntimeEffect;
199  result.proc_ = [runtime_stage = std::move(runtime_stage),
200  uniform_data = std::move(uniform_data),
201  texture_inputs =
202  std::move(texture_inputs)](const Paint& paint) {
203  auto contents = std::make_shared<RuntimeEffectContents>();
204  contents->SetOpacityFactor(paint.color.alpha);
205  contents->SetRuntimeStage(runtime_stage);
206  contents->SetUniformData(uniform_data);
207  contents->SetTextureInputs(texture_inputs);
208  return contents;
209  };
210  return result;
211 }
212 
213 #if IMPELLER_ENABLE_3D
214 ColorSource ColorSource::MakeScene(std::shared_ptr<scene::Node> scene_node,
215  Matrix camera_transform) {
216  ColorSource result;
217  result.type_ = Type::kScene;
218  result.proc_ = [scene_node = std::move(scene_node),
219  camera_transform](const Paint& paint) {
220  auto contents = std::make_shared<SceneContents>();
221  contents->SetOpacityFactor(paint.color.alpha);
222  contents->SetNode(scene_node);
223  contents->SetCameraTransform(camera_transform);
224  return contents;
225  };
226  return result;
227 }
228 #endif // IMPELLER_ENABLE_3D
229 
231  return type_;
232 }
233 
234 std::shared_ptr<ColorSourceContents> ColorSource::GetContents(
235  const Paint& paint) const {
236  return proc_(paint);
237 }
238 
239 } // namespace impeller
impeller::ColorSource::Type::kScene
@ kScene
impeller::ColorSource::Type::kLinearGradient
@ kLinearGradient
impeller::ColorSource::GetContents
std::shared_ptr< ColorSourceContents > GetContents(const Paint &paint) const
Definition: color_source.cc:234
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::ColorSource::Type::kRadialGradient
@ kRadialGradient
impeller::ColorSource::MakeLinearGradient
static ColorSource MakeLinearGradient(Point start_point, Point end_point, std::vector< Color > colors, std::vector< Scalar > stops, Entity::TileMode tile_mode, Matrix effect_transform)
Definition: color_source.cc:45
impeller::Paint
Definition: paint.h:23
solid_color_contents.h
impeller::FilterInput::Ref
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:32
tiled_texture_contents.h
impeller::TSize< Scalar >::Ceil
constexpr TSize Ceil() const
Definition: size.h:96
impeller::ColorFilterContents::AbsorbOpacity::kNo
@ kNo
impeller::ColorSource::MakeSweepGradient
static ColorSource MakeSweepGradient(Point center, Degrees start_angle, Degrees end_angle, std::vector< Color > colors, std::vector< Scalar > stops, Entity::TileMode tile_mode, Matrix effect_transform)
Definition: color_source.cc:138
impeller::ColorSource::MakeColor
static ColorSource MakeColor()
Definition: color_source.cc:41
impeller::ColorSource::MakeImage
static ColorSource MakeImage(std::shared_ptr< Texture > texture, Entity::TileMode x_tile_mode, Entity::TileMode y_tile_mode, SamplerDescriptor sampler_descriptor, Matrix effect_transform)
Definition: color_source.cc:163
sweep_gradient_contents.h
impeller::TiledTextureContents::ColorFilterProc
std::function< std::shared_ptr< ColorFilterContents >(FilterInput::Ref)> ColorFilterProc
Definition: tiled_texture_contents.h:29
impeller::TRect< Scalar >::MakePointBounds
constexpr static std::optional< TRect > MakePointBounds(const U &value)
Definition: rect.h:151
runtime_effect_contents.h
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
matrix.h
impeller::Point
TPoint< Scalar > Point
Definition: point.h:316
impeller::ColorSource
Definition: color_source.h:28
color_source.h
runtime_stage.h
scene_contents.h
node.h
impeller::ColorSource::MakeConicalGradient
static ColorSource MakeConicalGradient(Point center, Scalar radius, std::vector< Color > colors, std::vector< Scalar > stops, Point focus_center, Scalar focus_radius, Entity::TileMode tile_mode, Matrix effect_transform)
Definition: color_source.cc:74
impeller::ColorSource::Type::kRuntimeEffect
@ kRuntimeEffect
conical_gradient_contents.h
impeller::ColorSource::ColorSource
ColorSource() noexcept
Definition: color_source.cc:32
color_filter_contents.h
impeller::Entity::TileMode
TileMode
Definition: entity.h:42
impeller::ColorSource::MakeRadialGradient
static ColorSource MakeRadialGradient(Point center, Scalar radius, std::vector< Color > colors, std::vector< Scalar > stops, Entity::TileMode tile_mode, Matrix effect_transform)
Definition: color_source.cc:108
scalar.h
sampler_descriptor.h
impeller::ColorSource::Type::kImage
@ kImage
impeller::ColorSource::MakeRuntimeEffect
static ColorSource MakeRuntimeEffect(std::shared_ptr< RuntimeStage > runtime_stage, std::shared_ptr< std::vector< uint8_t >> uniform_data, std::vector< RuntimeEffectContents::TextureInput > texture_inputs)
Definition: color_source.cc:193
impeller::ColorSource::Type::kConicalGradient
@ kConicalGradient
std
Definition: comparable.h:95
impeller::ColorSource::Type
Type
Definition: color_source.h:30
impeller::TPoint< Scalar >
impeller::ColorSource::~ColorSource
~ColorSource()
paint.h
impeller::Degrees
Definition: scalar.h:46
color.h
radial_gradient_contents.h
impeller::ColorSourceContents
Definition: color_source_contents.h:36
impeller::ColorSource::Type::kSweepGradient
@ kSweepGradient
impeller
Definition: aiks_blur_unittests.cc:20
impeller::ColorSource::GetType
Type GetType() const
Definition: color_source.cc:230
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
linear_gradient_contents.h