Flutter Engine
The Flutter Engine
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 <variant>
9#include <vector>
10
11#include "impeller/aiks/paint.h"
25
26#if IMPELLER_ENABLE_3D
28#include "impeller/scene/node.h" // nogncheck
29#endif // IMPELLER_ENABLE_3D
30
31namespace impeller {
32
33namespace {
34
35struct 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) {
120 [color_filter = paint.color_filter](FilterInput::Ref input) {
121 return color_filter->WrapWithGPUColorFilter(
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
158ColorSource::ColorSource() noexcept : color_source_data_(std::monostate()) {}
159
160ColorSource::~ColorSource() = default;
161
163 return {};
164}
165
167 Point end_point,
168 std::vector<Color> colors,
169 std::vector<Scalar> stops,
170 Entity::TileMode tile_mode,
171 Matrix effect_transform) {
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
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) {
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
197 Scalar radius,
198 std::vector<Color> colors,
199 std::vector<Scalar> stops,
200 Entity::TileMode tile_mode,
201 Matrix effect_transform) {
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
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) {
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
226 Entity::TileMode x_tile_mode,
227 Entity::TileMode y_tile_mode,
228 SamplerDescriptor sampler_descriptor,
229 Matrix effect_transform) {
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
239 std::shared_ptr<RuntimeStage> runtime_stage,
240 std::shared_ptr<std::vector<uint8_t>> uniform_data,
241 std::vector<RuntimeEffectContents::TextureInput> texture_inputs) {
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
251ColorSource ColorSource::MakeScene(std::shared_ptr<scene::Node> scene_node,
252 Matrix camera_transform) {
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
261 return type_;
262}
263
264std::shared_ptr<ColorSourceContents> ColorSource::GetContents(
265 const Paint& paint) const {
266 return std::visit(CreateContentsVisitor{paint}, color_source_data_);
267}
268
270 return color_source_data_;
271}
272
273} // namespace impeller
static sk_sp< SkImage > color_filter(const SkImage *image, SkColorFilter *colorFilter)
const ColorSourceData & GetData() const
static ColorSource MakeLinearGradient(Point start_point, Point end_point, std::vector< Color > colors, std::vector< Scalar > stops, Entity::TileMode tile_mode, Matrix effect_transform)
static ColorSource MakeColor()
static ColorSource MakeRadialGradient(Point center, Scalar radius, std::vector< Color > colors, std::vector< Scalar > stops, Entity::TileMode tile_mode, Matrix effect_transform)
static ColorSource MakeImage(std::shared_ptr< Texture > texture, Entity::TileMode x_tile_mode, Entity::TileMode y_tile_mode, SamplerDescriptor sampler_descriptor, Matrix effect_transform)
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)
std::shared_ptr< ColorSourceContents > GetContents(const Paint &paint) const
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)
static ColorSource MakeRuntimeEffect(std::shared_ptr< RuntimeStage > runtime_stage, std::shared_ptr< std::vector< uint8_t > > uniform_data, std::vector< RuntimeEffectContents::TextureInput > texture_inputs)
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:32
std::function< std::shared_ptr< ColorFilterContents >(FilterInput::Ref)> ColorFilterProc
const Paint & paint
Definition: color_source.cc:38
GAsyncResult * result
FlTexture * texture
Optional< SkRect > bounds
Definition: SkRecords.h:189
PODArray< SkColor > colors
Definition: SkRecords.h:276
float Scalar
Definition: scalar.h:18
TPoint< Scalar > Point
Definition: point.h:322
std::variant< LinearGradientData, RadialGradientData, ConicalGradientData, SweepGradientData, ImageData, RuntimeEffectData, std::monostate > ColorSourceData
Definition: color_source.h:97
Definition: ref_ptr.h:256
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr std::optional< TRect > MakePointBounds(const U &value)
Definition: rect.h:151
constexpr TSize Ceil() const
Definition: size.h:96
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63