Flutter Engine
The Flutter Engine
dl_sk_conversions.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 "flutter/display_list/skia/dl_sk_conversions.h"
6
10
11namespace flutter {
12
13// clang-format off
14constexpr float kInvertColorMatrix[20] = {
15 -1.0, 0, 0, 1.0, 0,
16 0, -1.0, 0, 1.0, 0,
17 0, 0, -1.0, 1.0, 0,
18 1.0, 1.0, 1.0, 1.0, 0
19};
20// clang-format on
21
23 SkPaint sk_paint;
24
25 sk_paint.setAntiAlias(paint.isAntiAlias());
26 sk_paint.setColor(ToSk(paint.getColor()));
27 sk_paint.setBlendMode(ToSk(paint.getBlendMode()));
28 sk_paint.setStyle(ToSk(paint.getDrawStyle()));
29 sk_paint.setStrokeWidth(paint.getStrokeWidth());
30 sk_paint.setStrokeMiter(paint.getStrokeMiter());
31 sk_paint.setStrokeCap(ToSk(paint.getStrokeCap()));
32 sk_paint.setStrokeJoin(ToSk(paint.getStrokeJoin()));
33 sk_paint.setImageFilter(ToSk(paint.getImageFilterPtr()));
34 auto color_filter = ToSk(paint.getColorFilterPtr());
35 if (paint.isInvertColors()) {
36 auto invert_filter = SkColorFilters::Matrix(kInvertColorMatrix);
37 if (color_filter) {
38 invert_filter = invert_filter->makeComposed(color_filter);
39 }
40 color_filter = invert_filter;
41 }
43
44 auto color_source = paint.getColorSourcePtr();
45 if (color_source) {
46 // Unconditionally set dither to true for gradient shaders.
47 sk_paint.setDither(color_source->isGradient());
48 sk_paint.setShader(ToSk(color_source));
49 }
50
51 sk_paint.setMaskFilter(ToSk(paint.getMaskFilterPtr()));
52
53 return sk_paint;
54}
55
57 DlPaint stroked_paint = paint;
59 return ToSk(stroked_paint);
60}
61
63 DlPaint non_shader_paint = paint;
64 non_shader_paint.setColorSource(nullptr);
65 return ToSk(non_shader_paint);
66}
67
69 if (!source) {
70 return nullptr;
71 }
72 static auto ToSkColors = [](const DlGradientColorSourceBase* gradient) {
73 return reinterpret_cast<const SkColor*>(gradient->colors());
74 };
75 switch (source->type()) {
77 const DlColorColorSource* color_source = source->asColor();
78 FML_DCHECK(color_source != nullptr);
79 return SkShaders::Color(ToSk(color_source->color()));
80 }
82 const DlImageColorSource* image_source = source->asImage();
83 FML_DCHECK(image_source != nullptr);
84 auto image = image_source->image();
85 if (!image || !image->skia_image()) {
86 return nullptr;
87 }
88 return image->skia_image()->makeShader(
89 ToSk(image_source->horizontal_tile_mode()),
90 ToSk(image_source->vertical_tile_mode()),
91 ToSk(image_source->sampling()), image_source->matrix_ptr());
92 }
94 const DlLinearGradientColorSource* linear_source =
95 source->asLinearGradient();
96 FML_DCHECK(linear_source != nullptr);
97 SkPoint pts[] = {linear_source->start_point(),
98 linear_source->end_point()};
100 pts, ToSkColors(linear_source), linear_source->stops(),
101 linear_source->stop_count(), ToSk(linear_source->tile_mode()), 0,
102 linear_source->matrix_ptr());
103 }
105 const DlRadialGradientColorSource* radial_source =
106 source->asRadialGradient();
107 FML_DCHECK(radial_source != nullptr);
109 radial_source->center(), radial_source->radius(),
110 ToSkColors(radial_source), radial_source->stops(),
111 radial_source->stop_count(), ToSk(radial_source->tile_mode()), 0,
112 radial_source->matrix_ptr());
113 }
115 const DlConicalGradientColorSource* conical_source =
116 source->asConicalGradient();
117 FML_DCHECK(conical_source != nullptr);
119 conical_source->start_center(), conical_source->start_radius(),
120 conical_source->end_center(), conical_source->end_radius(),
121 ToSkColors(conical_source), conical_source->stops(),
122 conical_source->stop_count(), ToSk(conical_source->tile_mode()), 0,
123 conical_source->matrix_ptr());
124 }
126 const DlSweepGradientColorSource* sweep_source =
127 source->asSweepGradient();
128 FML_DCHECK(sweep_source != nullptr);
130 sweep_source->center().x(), sweep_source->center().y(),
131 ToSkColors(sweep_source), sweep_source->stops(),
132 sweep_source->stop_count(), ToSk(sweep_source->tile_mode()),
133 sweep_source->start(), sweep_source->end(), 0,
134 sweep_source->matrix_ptr());
135 }
137 const DlRuntimeEffectColorSource* runtime_source =
138 source->asRuntimeEffect();
139 FML_DCHECK(runtime_source != nullptr);
140 auto runtime_effect = runtime_source->runtime_effect();
141 if (!runtime_effect || !runtime_effect->skia_runtime_effect()) {
142 return nullptr;
143 }
144
145 auto samplers = runtime_source->samplers();
146 std::vector<sk_sp<SkShader>> sk_samplers(samplers.size());
147 for (size_t i = 0; i < samplers.size(); i++) {
148 auto sampler = samplers[i];
149 if (sampler == nullptr) {
150 return nullptr;
151 }
152 sk_samplers[i] = ToSk(sampler);
153 }
154
155 auto uniform_data = runtime_source->uniform_data();
156 auto ref = new std::shared_ptr<std::vector<uint8_t>>(uniform_data);
157 auto sk_uniform_data = SkData::MakeWithProc(
158 uniform_data->data(), uniform_data->size(),
159 [](const void* ptr, void* context) {
160 delete reinterpret_cast<std::shared_ptr<std::vector<uint8_t>>*>(
161 context);
162 },
163 ref);
164
165 return runtime_effect->skia_runtime_effect()->makeShader(
166 sk_uniform_data, sk_samplers.data(), sk_samplers.size());
167 }
168#ifdef IMPELLER_ENABLE_3D
169 case DlColorSourceType::kScene: {
170 return nullptr;
171 }
172#endif // IMPELLER_ENABLE_3D
173 }
174}
175
177 if (!filter) {
178 return nullptr;
179 }
180 switch (filter->type()) {
182 const DlBlurImageFilter* blur_filter = filter->asBlur();
183 FML_DCHECK(blur_filter != nullptr);
184 return SkImageFilters::Blur(blur_filter->sigma_x(),
185 blur_filter->sigma_y(),
186 ToSk(blur_filter->tile_mode()), nullptr);
187 }
189 const DlDilateImageFilter* dilate_filter = filter->asDilate();
190 FML_DCHECK(dilate_filter != nullptr);
191 return SkImageFilters::Dilate(dilate_filter->radius_x(),
192 dilate_filter->radius_y(), nullptr);
193 }
195 const DlErodeImageFilter* erode_filter = filter->asErode();
196 FML_DCHECK(erode_filter != nullptr);
197 return SkImageFilters::Erode(erode_filter->radius_x(),
198 erode_filter->radius_y(), nullptr);
199 }
201 const DlMatrixImageFilter* matrix_filter = filter->asMatrix();
202 FML_DCHECK(matrix_filter != nullptr);
204 matrix_filter->matrix(), ToSk(matrix_filter->sampling()), nullptr);
205 }
207 const DlComposeImageFilter* compose_filter = filter->asCompose();
208 FML_DCHECK(compose_filter != nullptr);
209 return SkImageFilters::Compose(ToSk(compose_filter->outer()),
210 ToSk(compose_filter->inner()));
211 }
213 const DlColorFilterImageFilter* cf_filter = filter->asColorFilter();
214 FML_DCHECK(cf_filter != nullptr);
215 return SkImageFilters::ColorFilter(ToSk(cf_filter->color_filter()),
216 nullptr);
217 }
219 const DlLocalMatrixImageFilter* lm_filter = filter->asLocalMatrix();
220 FML_DCHECK(lm_filter != nullptr);
221 sk_sp<SkImageFilter> skia_filter = ToSk(lm_filter->image_filter());
222 // The image_filter property itself might have been null, or the
223 // construction of the SkImageFilter might be optimized to null
224 // for any number of reasons. In any case, if the filter is null
225 // or optimizaed away, let's then optimize away this local matrix
226 // case by returning null.
227 if (!skia_filter) {
228 return nullptr;
229 }
230 return skia_filter->makeWithLocalMatrix(lm_filter->matrix());
231 }
232 }
233}
234
236 if (!filter) {
237 return nullptr;
238 }
239 switch (filter->type()) {
241 const DlBlendColorFilter* blend_filter = filter->asBlend();
242 FML_DCHECK(blend_filter != nullptr);
243 return SkColorFilters::Blend(ToSk(blend_filter->color()),
244 ToSk(blend_filter->mode()));
245 }
247 const DlMatrixColorFilter* matrix_filter = filter->asMatrix();
248 FML_DCHECK(matrix_filter != nullptr);
249 float matrix[20];
250 matrix_filter->get_matrix(matrix);
252 }
255 }
258 }
259 }
260}
261
263 if (!filter) {
264 return nullptr;
265 }
266 switch (filter->type()) {
268 const DlBlurMaskFilter* blur_filter = filter->asBlur();
269 FML_DCHECK(blur_filter != nullptr);
270 return SkMaskFilter::MakeBlur(ToSk(blur_filter->style()),
271 blur_filter->sigma(),
272 blur_filter->respectCTM());
273 }
274 }
275}
276
278 const SkColor* sk_colors =
279 reinterpret_cast<const SkColor*>(vertices->colors());
280 return SkVertices::MakeCopy(ToSk(vertices->mode()), vertices->vertex_count(),
281 vertices->vertices(),
282 vertices->texture_coordinates(), sk_colors,
283 vertices->index_count(), vertices->indices());
284}
285
286} // namespace flutter
uint32_t SkColor
Definition: SkColor.h:37
static sk_sp< SkImage > color_filter(const SkImage *image, SkColorFilter *colorFilter)
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
static sk_sp< SkColorFilter > Blend(const SkColor4f &c, sk_sp< SkColorSpace >, SkBlendMode mode)
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
static sk_sp< SkColorFilter > SRGBToLinearGamma()
static sk_sp< SkColorFilter > LinearToSRGBGamma()
static sk_sp< SkData > MakeWithProc(const void *ptr, size_t length, ReleaseProc proc, void *ctx)
Definition: SkData.cpp:128
static sk_sp< SkShader > MakeTwoPointConical(const SkPoint &start, SkScalar startRadius, const SkPoint &end, SkScalar endRadius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
sk_sp< SkImageFilter > makeWithLocalMatrix(const SkMatrix &matrix) const
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Erode(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Compose(sk_sp< SkImageFilter > outer, sk_sp< SkImageFilter > inner)
static sk_sp< SkImageFilter > MatrixTransform(const SkMatrix &matrix, const SkSamplingOptions &sampling, sk_sp< SkImageFilter > input)
static sk_sp< SkImageFilter > Dilate(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
static sk_sp< SkMaskFilter > MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM=true)
void setStyle(Style style)
Definition: SkPaint.cpp:105
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setStrokeMiter(SkScalar miter)
Definition: SkPaint.cpp:169
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
void setDither(bool dither)
Definition: SkPaint.h:182
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
void setStrokeCap(Cap cap)
Definition: SkPaint.cpp:179
void setStrokeJoin(Join join)
Definition: SkPaint.cpp:189
void setMaskFilter(sk_sp< SkMaskFilter > maskFilter)
void setShader(sk_sp< SkShader > shader)
void setBlendMode(SkBlendMode mode)
Definition: SkPaint.cpp:151
void setColorFilter(sk_sp< SkColorFilter > colorFilter)
void setStrokeWidth(SkScalar width)
Definition: SkPaint.cpp:159
static sk_sp< SkVertices > MakeCopy(VertexMode mode, int vertexCount, const SkPoint positions[], const SkPoint texs[], const SkColor colors[], int indexCount, const uint16_t indices[])
Definition: SkVertices.cpp:200
virtual T type() const =0
DlBlendMode mode() const
DlTileMode tile_mode() const
DlBlurStyle style() const
SkScalar sigma() const
const std::shared_ptr< const DlColorFilter > color_filter() const
virtual const DlBlendColorFilter * asBlend() const
virtual const DlMatrixColorFilter * asMatrix() const
std::shared_ptr< const DlImageFilter > outer() const
std::shared_ptr< const DlImageFilter > inner() const
DlImageSampling sampling() const
DlTileMode vertical_tile_mode() const
DlTileMode horizontal_tile_mode() const
sk_sp< const DlImage > image() const
virtual const DlLocalMatrixImageFilter * asLocalMatrix() const
virtual const DlColorFilterImageFilter * asColorFilter() const
virtual const DlMatrixImageFilter * asMatrix() const
virtual const DlComposeImageFilter * asCompose() const
virtual const DlBlurImageFilter * asBlur() const
virtual const DlDilateImageFilter * asDilate() const
virtual const DlErodeImageFilter * asErode() const
const SkPoint & start_point() const
const SkMatrix & matrix() const
const std::shared_ptr< DlImageFilter > image_filter() const
virtual const DlBlurMaskFilter * asBlur() const
void get_matrix(float matrix[20]) const
const SkMatrix * matrix_ptr() const
const SkMatrix & matrix() const
DlImageSampling sampling() const
DlPaint & setDrawStyle(DlDrawStyle style)
Definition: dl_paint.h:94
DlPaint & setColorSource(std::shared_ptr< const DlColorSource > source)
Definition: dl_paint.h:131
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
Holds all of the data (both required and optional) for a DisplayList drawVertices call.
Definition: dl_vertices.h:71
const DlColor * colors() const
Definition: dl_vertices.h:217
int vertex_count() const
Definition: dl_vertices.h:202
DlVertexMode mode() const
Definition: dl_vertices.h:198
int index_count() const
Definition: dl_vertices.h:223
const SkPoint * vertices() const
Returns a pointer to the vertex information. Should be non-null.
Definition: dl_vertices.h:205
const uint16_t * indices() const
Definition: dl_vertices.h:227
const SkPoint * texture_coordinates() const
Definition: dl_vertices.h:211
const Paint & paint
Definition: color_source.cc:38
SkBitmap source
Definition: examples.cpp:28
#define FML_DCHECK(condition)
Definition: logging.h:103
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkShader > Color(SkColor)
SkPaint ToSk(const DlPaint &paint)
SkPaint ToStrokedSk(const DlPaint &paint)
SkPaint ToNonShaderSk(const DlPaint &paint)
@ kStroke
strokes boundary of shapes
constexpr float kInvertColorMatrix[20]
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181