| 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 <vector>
9
10 #include "impeller/aiks/paint.h"
11 #include "impeller/core/sampler_descriptor.h"
12 #include "impeller/entity/contents/conical_gradient_contents.h"
13 #include "impeller/entity/contents/filters/color_filter_contents.h"
14 #include "impeller/entity/contents/linear_gradient_contents.h"
15 #include "impeller/entity/contents/radial_gradient_contents.h"
16 #include "impeller/entity/contents/runtime_effect_contents.h"
17 #include "impeller/entity/contents/solid_color_contents.h"
18 #include "impeller/entity/contents/sweep_gradient_contents.h"
19 #include "impeller/entity/contents/tiled_texture_contents.h"
20 #include "impeller/geometry/color.h"
21 #include "impeller/geometry/matrix.h"
22 #include "impeller/geometry/scalar.h"
23 #include "impeller/runtime_stage/runtime_stage.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
32 ColorSource::ColorSource() noexcept
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
41 ColorSource ColorSource::MakeColor() {
42 return {};
43 }
44
45 ColorSource ColorSource::MakeLinearGradient(Point start_point,
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->SetDither(paint.dither);
63 contents->SetEffectTransform(effect_transform);
64
65 std::vector<Point> bounds{start_point, end_point};
66 auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
67 if (intrinsic_size.has_value()) {
68 contents->SetColorSourceSize(intrinsic_size->size);
69 }
70 return contents;
71 };
72 return result;
73 }
74
75 ColorSource ColorSource::MakeConicalGradient(Point center,
76 Scalar radius,
77 std::vector<Color> colors,
78 std::vector<Scalar> stops,
79 Point focus_center,
80 Scalar focus_radius,
81 Entity::TileMode tile_mode,
82 Matrix effect_transform) {
83 ColorSource result;
84 result.type_ = Type::kConicalGradient;
85 result.proc_ = [center, radius, colors = std::move(colors),
86 stops = std::move(stops), focus_center, focus_radius,
87 tile_mode, effect_transform](const Paint& paint) {
88 std::shared_ptr<ConicalGradientContents> contents =
89 std::make_shared<ConicalGradientContents>();
90 contents->SetOpacityFactor(paint.color.alpha);
91 contents->SetColors(colors);
92 contents->SetStops(stops);
93 contents->SetCenterAndRadius(center, radius);
94 contents->SetTileMode(tile_mode);
95 contents->SetDither(paint.dither);
96 contents->SetEffectTransform(effect_transform);
97 contents->SetFocus(focus_center, focus_radius);
98
99 auto radius_pt = Point(radius, radius);
100 std::vector<Point> bounds{center + radius_pt, center - radius_pt};
101 auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
102 if (intrinsic_size.has_value()) {
103 contents->SetColorSourceSize(intrinsic_size->size);
104 }
105 return contents;
106 };
107 return result;
108 }
109
110 ColorSource ColorSource::MakeRadialGradient(Point center,
111 Scalar radius,
112 std::vector<Color> colors,
113 std::vector<Scalar> stops,
114 Entity::TileMode tile_mode,
115 Matrix effect_transform) {
116 ColorSource result;
117 result.type_ = Type::kRadialGradient;
118 result.proc_ = [center, radius, colors = std::move(colors),
119 stops = std::move(stops), tile_mode,
120 effect_transform](const Paint& paint) {
121 auto contents = std::make_shared<RadialGradientContents>();
122 contents->SetOpacityFactor(paint.color.alpha);
123 contents->SetColors(colors);
124 contents->SetStops(stops);
125 contents->SetCenterAndRadius(center, radius);
126 contents->SetTileMode(tile_mode);
127 contents->SetDither(paint.dither);
128 contents->SetEffectTransform(effect_transform);
129
130 auto radius_pt = Point(radius, radius);
131 std::vector<Point> bounds{center + radius_pt, center - radius_pt};
132 auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
133 if (intrinsic_size.has_value()) {
134 contents->SetColorSourceSize(intrinsic_size->size);
135 }
136 return contents;
137 };
138 return result;
139 }
140
141 ColorSource ColorSource::MakeSweepGradient(Point center,
142 Degrees start_angle,
143 Degrees end_angle,
144 std::vector<Color> colors,
145 std::vector<Scalar> stops,
146 Entity::TileMode tile_mode,
147 Matrix effect_transform) {
148 ColorSource result;
149 result.type_ = Type::kSweepGradient;
150 result.proc_ = [center, start_angle, end_angle, colors = std::move(colors),
151 stops = std::move(stops), tile_mode,
152 effect_transform](const Paint& paint) {
153 auto contents = std::make_shared<SweepGradientContents>();
154 contents->SetOpacityFactor(paint.color.alpha);
155 contents->SetCenterAndAngles(center, start_angle, end_angle);
156 contents->SetColors(colors);
157 contents->SetStops(stops);
158 contents->SetTileMode(tile_mode);
159 contents->SetDither(paint.dither);
160 contents->SetEffectTransform(effect_transform);
161
162 return contents;
163 };
164 return result;
165 }
166
167 ColorSource ColorSource::MakeImage(std::shared_ptr<Texture> texture,
168 Entity::TileMode x_tile_mode,
169 Entity::TileMode y_tile_mode,
170 SamplerDescriptor sampler_descriptor,
171 Matrix effect_transform) {
172 ColorSource result;
173 result.type_ = Type::kImage;
174 result.proc_ = [texture = std::move(texture), x_tile_mode, y_tile_mode,
175 sampler_descriptor = std::move(sampler_descriptor),
176 effect_transform](const Paint& paint) {
177 auto contents = std::make_shared<TiledTextureContents>();
178 contents->SetOpacityFactor(paint.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 (paint.color_filter) {
184 TiledTextureContents::ColorFilterProc filter_proc =
185 [color_filter = paint.color_filter](FilterInput::Ref input) {
186 return color_filter->WrapWithGPUColorFilter(
187 std::move(input), ColorFilterContents::AbsorbOpacity::kNo);
188 };
189 contents->SetColorFilter(filter_proc);
190 }
191 contents->SetColorSourceSize(Size::Ceil(texture->GetSize()));
192 return contents;
193 };
194 return result;
195 }
196
197 ColorSource ColorSource::MakeRuntimeEffect(
198 std::shared_ptr<RuntimeStage> runtime_stage,
199 std::shared_ptr<std::vector<uint8_t>> uniform_data,
200 std::vector<RuntimeEffectContents::TextureInput> texture_inputs) {
201 ColorSource result;
202 result.type_ = Type::kRuntimeEffect;
203 result.proc_ = [runtime_stage = std::move(runtime_stage),
204 uniform_data = std::move(uniform_data),
205 texture_inputs =
206 std::move(texture_inputs)](const Paint& paint) {
207 auto contents = std::make_shared<RuntimeEffectContents>();
208 contents->SetOpacityFactor(paint.color.alpha);
209 contents->SetRuntimeStage(runtime_stage);
210 contents->SetUniformData(uniform_data);
211 contents->SetTextureInputs(texture_inputs);
212 return contents;
213 };
214 return result;
215 }
216
217 #if IMPELLER_ENABLE_3D
218 ColorSource ColorSource::MakeScene(std::shared_ptr<scene::Node> scene_node,
219 Matrix camera_transform) {
220 ColorSource result;
221 result.type_ = Type::kScene;
222 result.proc_ = [scene_node = std::move(scene_node),
223 camera_transform](const Paint& paint) {
224 auto contents = std::make_shared<SceneContents>();
225 contents->SetOpacityFactor(paint.color.alpha);
226 contents->SetNode(scene_node);
227 contents->SetCameraTransform(camera_transform);
228 return contents;
229 };
230 return result;
231 }
232 #endif // IMPELLER_ENABLE_3D
233
234 ColorSource::Type ColorSource::GetType() const {
235 return type_;
236 }
237
238 std::shared_ptr<ColorSourceContents> ColorSource::GetContents(
239 const Paint& paint) const {
240 return proc_(paint);
241 }
242
243 } // 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:238
impeller::Scalar
float Scalar
Definition: scalar.h:15
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:25
solid_color_contents.h
impeller::FilterInput::Ref
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:31
tiled_texture_contents.h
impeller::TSize::Ceil
constexpr TSize Ceil() const
Definition: size.h:91
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:141
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:167
sweep_gradient_contents.h
impeller::TiledTextureContents::ColorFilterProc
std::function< std::shared_ptr< ColorFilterContents >(FilterInput::Ref)> ColorFilterProc
Definition: tiled_texture_contents.h:28
runtime_effect_contents.h
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:18
matrix.h
impeller::Point
TPoint< Scalar > Point
Definition: point.h:306
impeller::ColorSource
Definition: color_source.h:27
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:75
impeller::ColorSource::Type::kRuntimeEffect
@ kRuntimeEffect
conical_gradient_contents.h
impeller::TRect< Scalar >::MakePointBounds
constexpr static std::optional< TRect > MakePointBounds(const PointIter first, const PointIter last)
Definition: rect.h:57
impeller::ColorSource::ColorSource
ColorSource() noexcept
Definition: color_source.cc:32
color_filter_contents.h
impeller::Entity::TileMode
TileMode
Definition: entity.h:40
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:110
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:197
impeller::ColorSource::Type::kConicalGradient
@ kConicalGradient
std
Definition: comparable.h:98
impeller::ColorSource::Type
Type
Definition: color_source.h:29
impeller::TPoint< Scalar >
impeller::ColorSource::~ColorSource
~ColorSource()
paint.h
impeller::Degrees
Definition: scalar.h:43
color.h
radial_gradient_contents.h
impeller::ColorSourceContents
Definition: color_source_contents.h:33
impeller::ColorSource::Type::kSweepGradient
@ kSweepGradient
impeller
Definition: aiks_context.cc:10
impeller::ColorSource::GetType
Type GetType() const
Definition: color_source.cc:234
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
linear_gradient_contents.h