| 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
5 #include "impeller/aiks/color_source.h"
6
7 #include <memory>
8 #include <variant>
9 #include <vector>
10
11 #include "impeller/aiks/paint.h"
12 #include "impeller/core/sampler_descriptor.h"
13 #include "impeller/entity/contents/conical_gradient_contents.h"
14 #include "impeller/entity/contents/filters/color_filter_contents.h"
15 #include "impeller/entity/contents/linear_gradient_contents.h"
16 #include "impeller/entity/contents/radial_gradient_contents.h"
17 #include "impeller/entity/contents/runtime_effect_contents.h"
18 #include "impeller/entity/contents/solid_color_contents.h"
19 #include "impeller/entity/contents/sweep_gradient_contents.h"
20 #include "impeller/entity/contents/tiled_texture_contents.h"
21 #include "impeller/geometry/color.h"
22 #include "impeller/geometry/matrix.h"
23 #include "impeller/geometry/scalar.h"
24 #include "impeller/runtime_stage/runtime_stage.h"
25
26 #if IMPELLER_ENABLE_3D
27 #include "impeller/entity/contents/scene_contents.h" // nogncheck
28 #include "impeller/scene/node.h" // nogncheck
29 #endif // IMPELLER_ENABLE_3D
30
31 namespace impeller {
32
33 namespace {
34
35 struct CreateContentsVisitor {
36 explicit CreateContentsVisitor(const Paint& p_paint) : paint(p_paint) {}
37
38 const Paint& paint;
39
40 std::shared_ptr<ColorSourceContents> operator()(
41 const LinearGradientData& data) {
42 auto contents = std::make_shared<LinearGradientContents>();
43 contents->SetOpacityFactor(paint.color.alpha);
44 contents->SetColors(data.colors);
45 contents->SetStops(data.stops);
46 contents->SetEndPoints(data.start_point, data.end_point);
47 contents->SetTileMode(data.tile_mode);
48 contents->SetEffectTransform(data.effect_transform);
49
50 std::vector<Point> bounds{data.start_point, data.end_point};
51 auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
52 if (intrinsic_size.has_value()) {
53 contents->SetColorSourceSize(intrinsic_size->GetSize());
54 }
55 return contents;
56 }
57
58 std::shared_ptr<ColorSourceContents> operator()(
59 const RadialGradientData& data) {
60 auto contents = std::make_shared<RadialGradientContents>();
61 contents->SetOpacityFactor(paint.color.alpha);
62 contents->SetColors(data.colors);
63 contents->SetStops(data.stops);
64 contents->SetCenterAndRadius(data.center, data.radius);
65 contents->SetTileMode(data.tile_mode);
66 contents->SetEffectTransform(data.effect_transform);
67
68 auto radius_pt = Point(data.radius, data.radius);
69 std::vector<Point> bounds{data.center + radius_pt, data.center - radius_pt};
70 auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
71 if (intrinsic_size.has_value()) {
72 contents->SetColorSourceSize(intrinsic_size->GetSize());
73 }
74 return contents;
75 }
76
77 std::shared_ptr<ColorSourceContents> operator()(
78 const ConicalGradientData& data) {
79 std::shared_ptr<ConicalGradientContents> contents =
80 std::make_shared<ConicalGradientContents>();
81 contents->SetOpacityFactor(paint.color.alpha);
82 contents->SetColors(data.colors);
83 contents->SetStops(data.stops);
84 contents->SetCenterAndRadius(data.center, data.radius);
85 contents->SetTileMode(data.tile_mode);
86 contents->SetEffectTransform(data.effect_transform);
87 contents->SetFocus(data.focus_center, data.focus_radius);
88
89 auto radius_pt = Point(data.radius, data.radius);
90 std::vector<Point> bounds{data.center + radius_pt, data.center - radius_pt};
91 auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
92 if (intrinsic_size.has_value()) {
93 contents->SetColorSourceSize(intrinsic_size->GetSize());
94 }
95 return contents;
96 }
97
98 std::shared_ptr<ColorSourceContents> operator()(
99 const SweepGradientData& data) {
100 auto contents = std::make_shared<SweepGradientContents>();
101 contents->SetOpacityFactor(paint.color.alpha);
102 contents->SetCenterAndAngles(data.center, data.start_angle, data.end_angle);
103 contents->SetColors(data.colors);
104 contents->SetStops(data.stops);
105 contents->SetTileMode(data.tile_mode);
106 contents->SetEffectTransform(data.effect_transform);
107
108 return contents;
109 }
110
111 std::shared_ptr<ColorSourceContents> operator()(const ImageData& data) {
112 auto contents = std::make_shared<TiledTextureContents>();
113 contents->SetOpacityFactor(paint.color.alpha);
114 contents->SetTexture(data.texture);
115 contents->SetTileModes(data.x_tile_mode, data.y_tile_mode);
116 contents->SetSamplerDescriptor(data.sampler_descriptor);
117 contents->SetEffectTransform(data.effect_transform);
118 if (paint.color_filter) {
119 TiledTextureContents::ColorFilterProc filter_proc =
120 [color_filter = paint.color_filter](FilterInput::Ref input) {
121 return color_filter->WrapWithGPUColorFilter(
122 std::move(input), ColorFilterContents::AbsorbOpacity::kNo);
123 };
124 contents->SetColorFilter(filter_proc);
125 }
126 contents->SetColorSourceSize(Size::Ceil(data.texture->GetSize()));
127 return contents;
128 }
129
130 std::shared_ptr<ColorSourceContents> operator()(
131 const RuntimeEffectData& data) {
132 auto contents = std::make_shared<RuntimeEffectContents>();
133 contents->SetOpacityFactor(paint.color.alpha);
134 contents->SetRuntimeStage(data.runtime_stage);
135 contents->SetUniformData(data.uniform_data);
136 contents->SetTextureInputs(data.texture_inputs);
137 return contents;
138 }
139
140 std::shared_ptr<ColorSourceContents> operator()(const std::monostate& data) {
141 auto contents = std::make_shared<SolidColorContents>();
142 contents->SetColor(paint.color);
143 return contents;
144 }
145
146 #if IMPELLER_ENABLE_3D
147 std::shared_ptr<ColorSourceContents> operator()(const SceneData& data) {
148 auto contents = std::make_shared<SceneContents>();
149 contents->SetOpacityFactor(paint.color.alpha);
150 contents->SetNode(data.scene_node);
151 contents->SetCameraTransform(data.camera_transform);
152 return contents;
153 }
154 #endif // IMPELLER_ENABLE_3D
155 };
156 } // namespace
157
158 ColorSource::ColorSource() noexcept : color_source_data_(std::monostate()) {}
159
160 ColorSource::~ColorSource() = default;
161
162 ColorSource ColorSource::MakeColor() {
163 return {};
164 }
165
166 ColorSource ColorSource::MakeLinearGradient(Point start_point,
167 Point end_point,
168 std::vector<Color> colors,
169 std::vector<Scalar> stops,
170 Entity::TileMode tile_mode,
171 Matrix effect_transform) {
172 ColorSource result;
173 result.type_ = Type::kLinearGradient;
174 result.color_source_data_ =
175 LinearGradientData{start_point, end_point, std::move(colors),
176 std::move(stops), tile_mode, effect_transform};
177 return result;
178 }
179
180 ColorSource ColorSource::MakeConicalGradient(Point center,
181 Scalar radius,
182 std::vector<Color> colors,
183 std::vector<Scalar> stops,
184 Point focus_center,
185 Scalar focus_radius,
186 Entity::TileMode tile_mode,
187 Matrix effect_transform) {
188 ColorSource result;
189 result.type_ = Type::kConicalGradient;
190 result.color_source_data_ = ConicalGradientData{
191 center, radius, std::move(colors), std::move(stops),
192 focus_center, focus_radius, tile_mode, effect_transform};
193 return result;
194 }
195
196 ColorSource ColorSource::MakeRadialGradient(Point center,
197 Scalar radius,
198 std::vector<Color> colors,
199 std::vector<Scalar> stops,
200 Entity::TileMode tile_mode,
201 Matrix effect_transform) {
202 ColorSource result;
203 result.type_ = Type::kRadialGradient;
204 result.color_source_data_ =
205 RadialGradientData{center, radius, std::move(colors),
206 std::move(stops), tile_mode, effect_transform};
207 return result;
208 }
209
210 ColorSource ColorSource::MakeSweepGradient(Point center,
211 Degrees start_angle,
212 Degrees end_angle,
213 std::vector<Color> colors,
214 std::vector<Scalar> stops,
215 Entity::TileMode tile_mode,
216 Matrix effect_transform) {
217 ColorSource result;
218 result.type_ = Type::kSweepGradient;
219 result.color_source_data_ = SweepGradientData{
220 center, start_angle, end_angle, std::move(colors),
221 std::move(stops), tile_mode, effect_transform};
222 return result;
223 }
224
225 ColorSource ColorSource::MakeImage(std::shared_ptr<Texture> texture,
226 Entity::TileMode x_tile_mode,
227 Entity::TileMode y_tile_mode,
228 SamplerDescriptor sampler_descriptor,
229 Matrix effect_transform) {
230 ColorSource result;
231 result.type_ = Type::kImage;
232 result.color_source_data_ =
233 ImageData{std::move(texture), x_tile_mode, y_tile_mode,
234 std::move(sampler_descriptor), effect_transform};
235 return result;
236 }
237
238 ColorSource ColorSource::MakeRuntimeEffect(
239 std::shared_ptr<RuntimeStage> runtime_stage,
240 std::shared_ptr<std::vector<uint8_t>> uniform_data,
241 std::vector<RuntimeEffectContents::TextureInput> texture_inputs) {
242 ColorSource result;
243 result.type_ = Type::kRuntimeEffect;
244 result.color_source_data_ =
245 RuntimeEffectData{std::move(runtime_stage), std::move(uniform_data),
246 std::move(texture_inputs)};
247 return result;
248 }
249
250 #if IMPELLER_ENABLE_3D
251 ColorSource ColorSource::MakeScene(std::shared_ptr<scene::Node> scene_node,
252 Matrix camera_transform) {
253 ColorSource result;
254 result.type_ = Type::kScene;
255 result.color_source_data_ = SceneData{scene_node, camera_transform};
256 return result;
257 }
258 #endif // IMPELLER_ENABLE_3D
259
260 ColorSource::Type ColorSource::GetType() const {
261 return type_;
262 }
263
264 std::shared_ptr<ColorSourceContents> ColorSource::GetContents(
265 const Paint& paint) const {
266 return std::visit(CreateContentsVisitor{paint}, color_source_data_);
267 }
268
269 const ColorSourceData& ColorSource::GetData() const {
270 return color_source_data_;
271 }
272
273 } // 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:264
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::ColorSource::Type::kRadialGradient
@ kRadialGradient
impeller::RuntimeEffectData
Definition: color_source.h:75
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:166
impeller::Paint
Definition: paint.h:23
solid_color_contents.h
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
impeller::LinearGradientData
Definition: color_source.h:28
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:210
impeller::ColorSource::MakeColor
static ColorSource MakeColor()
Definition: color_source.cc:162
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:225
sweep_gradient_contents.h
impeller::TiledTextureContents::ColorFilterProc
std::function< std::shared_ptr< ColorFilterContents >(FilterInput::Ref)> ColorFilterProc
Definition: tiled_texture_contents.h:28
impeller::TRect< Scalar >::MakePointBounds
constexpr static std::optional< TRect > MakePointBounds(const U &value)
Definition: rect.h:151
impeller::ColorSourceData
std::variant< LinearGradientData, RadialGradientData, ConicalGradientData, SweepGradientData, ImageData, RuntimeEffectData, std::monostate > ColorSourceData
Definition: color_source.h:97
runtime_effect_contents.h
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
matrix.h
impeller::Point
TPoint< Scalar > Point
Definition: point.h:322
impeller::ColorSource
Definition: color_source.h:99
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:180
impeller::ColorSource::Type::kRuntimeEffect
@ kRuntimeEffect
conical_gradient_contents.h
impeller::ImageData
Definition: color_source.h:67
impeller::ConicalGradientData
Definition: color_source.h:46
impeller::SweepGradientData
Definition: color_source.h:57
impeller::ColorSource::ColorSource
ColorSource() noexcept
Definition: color_source.cc:158
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:196
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:238
impeller::ColorSource::Type::kConicalGradient
@ kConicalGradient
std
Definition: comparable.h:95
impeller::ColorSource::Type
Type
Definition: color_source.h:101
impeller::RadialGradientData
Definition: color_source.h:37
impeller::TPoint< Scalar >
impeller::ColorSource::~ColorSource
~ColorSource()
impeller::ColorSource::GetData
const ColorSourceData & GetData() const
Definition: color_source.cc:269
paint
const Paint & paint
Definition: color_source.cc:38
paint.h
impeller::Degrees
Definition: scalar.h:46
color.h
radial_gradient_contents.h
impeller::ColorSource::Type::kSweepGradient
@ kSweepGradient
impeller
Definition: aiks_blend_unittests.cc:18
impeller::ColorSource::GetType
Type GetType() const
Definition: color_source.cc:260
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
linear_gradient_contents.h