Flutter Engine
The Flutter Engine
skia_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
8
9namespace impeller {
10namespace skia_conversions {
11
13 auto [a, b] = rr.radii(SkRRect::kUpperLeft_Corner);
14 auto [c, d] = rr.radii(SkRRect::kLowerLeft_Corner);
16 auto [g, h] = rr.radii(SkRRect::kLowerRight_Corner);
24}
25
27 return Rect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
28}
29
30std::optional<Rect> ToRect(const SkRect* rect) {
31 if (rect == nullptr) {
32 return std::nullopt;
33 }
34 return Rect::MakeLTRB(rect->fLeft, rect->fTop, rect->fRight, rect->fBottom);
35}
36
37std::vector<Rect> ToRects(const SkRect tex[], int count) {
38 auto result = std::vector<Rect>();
39 for (int i = 0; i < count; i++) {
40 result.push_back(ToRect(tex[i]));
41 }
42 return result;
43}
44
45std::vector<Point> ToPoints(const SkPoint points[], int count) {
46 std::vector<Point> result(count);
47 for (auto i = 0; i < count; i++) {
48 result[i] = ToPoint(points[i]);
49 }
50 return result;
51}
52
54 using Corner = SkRRect::Corner;
56 radii.bottom_left = ToPoint(rrect.radii(Corner::kLowerLeft_Corner));
57 radii.bottom_right = ToPoint(rrect.radii(Corner::kLowerRight_Corner));
58 radii.top_left = ToPoint(rrect.radii(Corner::kUpperLeft_Corner));
59 radii.top_right = ToPoint(rrect.radii(Corner::kUpperRight_Corner));
60 return radii;
61}
62
63Path ToPath(const SkPath& path, Point shift) {
64 auto iterator = SkPath::Iter(path, false);
65
66 struct PathData {
67 union {
68 SkPoint points[4];
69 };
70 };
71
73 PathData data;
74 // Reserve a path size with some arbitrarily additional padding.
75 builder.Reserve(path.countPoints() + 8, path.countVerbs() + 8);
76 auto verb = SkPath::Verb::kDone_Verb;
77 do {
78 verb = iterator.next(data.points);
79 switch (verb) {
81 builder.MoveTo(ToPoint(data.points[0]));
82 break;
84 builder.LineTo(ToPoint(data.points[1]));
85 break;
87 builder.QuadraticCurveTo(ToPoint(data.points[1]),
88 ToPoint(data.points[2]));
89 break;
91 constexpr auto kPow2 = 1; // Only works for sweeps up to 90 degrees.
92 constexpr auto kQuadCount = 1 + (2 * (1 << kPow2));
93 SkPoint points[kQuadCount];
94 const auto curve_count =
96 data.points[1], //
97 data.points[2], //
98 iterator.conicWeight(), //
99 points, //
100 kPow2 //
101 );
102
103 for (int curve_index = 0, point_index = 0; //
104 curve_index < curve_count; //
105 curve_index++, point_index += 2 //
106 ) {
107 builder.QuadraticCurveTo(ToPoint(points[point_index + 1]),
108 ToPoint(points[point_index + 2]));
109 }
110 } break;
112 builder.CubicCurveTo(ToPoint(data.points[1]), ToPoint(data.points[2]),
113 ToPoint(data.points[3]));
114 break;
116 builder.Close();
117 break;
119 break;
120 }
121 } while (verb != SkPath::Verb::kDone_Verb);
122
123 FillType fill_type;
124 switch (path.getFillType()) {
126 fill_type = FillType::kNonZero;
127 break;
129 fill_type = FillType::kOdd;
130 break;
133 // Flutter doesn't expose these path fill types. These are only visible
134 // via the receiver interface. We should never get here.
135 fill_type = FillType::kNonZero;
136 break;
137 }
138 builder.SetConvexity(path.isConvex() ? Convexity::kConvex
140 builder.Shift(shift);
141 auto sk_bounds = path.getBounds().makeOutset(shift.x, shift.y);
142 builder.SetBounds(ToRect(sk_bounds));
143 return builder.TakePath(fill_type);
144}
145
147 return PathBuilder{}
151 .TakePath();
152}
153
154Point ToPoint(const SkPoint& point) {
155 return Point::MakeXY(point.fX, point.fY);
156}
157
158Size ToSize(const SkPoint& point) {
159 return Size(point.fX, point.fY);
160}
161
163 return {
164 static_cast<Scalar>(color.getRedF()), //
165 static_cast<Scalar>(color.getGreenF()), //
166 static_cast<Scalar>(color.getBlueF()), //
167 static_cast<Scalar>(color.getAlphaF()) //
168 };
169}
170
171std::vector<Matrix> ToRSXForms(const SkRSXform xform[], int count) {
172 auto result = std::vector<Matrix>();
173 for (int i = 0; i < count; i++) {
174 auto form = xform[i];
175 // clang-format off
176 auto matrix = Matrix{
177 form.fSCos, form.fSSin, 0, 0,
178 -form.fSSin, form.fSCos, 0, 0,
179 0, 0, 1, 0,
180 form.fTx, form.fTy, 0, 1
181 };
182 // clang-format on
183 result.push_back(matrix);
184 }
185 return result;
186}
187
189 if (!blob) {
190 return {};
191 }
192
193 return ToPath(skia::textlayout::Paragraph::GetPath(blob.get()), shift);
194}
195
196std::optional<impeller::PixelFormat> ToPixelFormat(SkColorType type) {
197 switch (type) {
206 default:
207 return std::nullopt;
208 }
209 return std::nullopt;
210}
211
213 std::vector<Color>& colors,
214 std::vector<float>& stops) {
215 FML_DCHECK(gradient->stop_count() >= 2);
216
217 auto* dl_colors = gradient->colors();
218 auto* dl_stops = gradient->stops();
219 if (dl_stops[0] != 0.0) {
220 colors.emplace_back(skia_conversions::ToColor(dl_colors[0]));
221 stops.emplace_back(0);
222 }
223 for (auto i = 0; i < gradient->stop_count(); i++) {
224 colors.emplace_back(skia_conversions::ToColor(dl_colors[i]));
225 stops.emplace_back(std::clamp(dl_stops[i], 0.0f, 1.0f));
226 }
227 if (dl_stops[gradient->stop_count() - 1] != 1.0) {
228 colors.emplace_back(colors.back());
229 stops.emplace_back(1.0);
230 }
231 for (auto i = 1; i < gradient->stop_count(); i++) {
232 stops[i] = std::clamp(stops[i], stops[i - 1], stops[i]);
233 }
234}
235
236} // namespace skia_conversions
237} // namespace impeller
int count
Definition: FontMgrTest.cpp:50
static const int points[]
static unsigned clamp(SkFixed fx, int max)
SkColorType
Definition: SkColorType.h:19
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kBGR_101010x_XR_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
Definition: SkColorType.h:31
static bool SkScalarNearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:107
GLenum type
Definition: SkPath.h:59
friend class Iter
Definition: SkPath.h:1840
static int ConvertConicToQuads(const SkPoint &p0, const SkPoint &p1, const SkPoint &p2, SkScalar w, SkPoint pts[], int pow2)
Definition: SkPath.cpp:3238
@ kClose_Verb
Definition: SkPath.h:1471
@ kMove_Verb
Definition: SkPath.h:1466
@ kConic_Verb
Definition: SkPath.h:1469
@ kDone_Verb
Definition: SkPath.h:1472
@ kCubic_Verb
Definition: SkPath.h:1470
@ kQuad_Verb
Definition: SkPath.h:1468
@ kLine_Verb
Definition: SkPath.h:1467
SkVector radii(Corner corner) const
Definition: SkRRect.h:271
@ kUpperLeft_Corner
index of top-left corner radii
Definition: SkRRect.h:252
@ kLowerRight_Corner
index of bottom-right corner radii
Definition: SkRRect.h:254
@ kUpperRight_Corner
index of top-right corner radii
Definition: SkRRect.h:253
@ kLowerLeft_Corner
index of bottom-left corner radii
Definition: SkRRect.h:255
const SkRect & getBounds() const
Definition: SkRRect.h:279
const DlColor * colors() const
Path TakePath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:22
PathBuilder & SetBounds(Rect bounds)
Set the bounding box that will be used by Path.GetBoundingBox in place of performing the computation.
PathBuilder & AddRoundedRect(Rect rect, RoundingRadii radii)
PathBuilder & SetConvexity(Convexity value)
Definition: path_builder.cc:85
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:52
T * get() const
Definition: SkRefCnt.h:303
static SkPath GetPath(SkTextBlob *textBlob)
DlColor color
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
static bool b
struct MyStruct a[10]
GAsyncResult * result
#define FML_DCHECK(condition)
Definition: logging.h:103
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
SkRRect rrect
Definition: SkRecords.h:232
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkColor > colors
Definition: SkRecords.h:276
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
std::optional< impeller::PixelFormat > ToPixelFormat(SkColorType type)
Path PathDataFromTextBlob(const sk_sp< SkTextBlob > &blob, Point shift)
std::vector< Point > ToPoints(const SkPoint points[], int count)
Path ToPath(const SkPath &path, Point shift)
void ConvertStops(const flutter::DlGradientColorSourceBase *gradient, std::vector< Color > &colors, std::vector< float > &stops)
Convert display list colors + stops into impeller colors and stops, taking care to ensure that the st...
Point ToPoint(const SkPoint &point)
Size ToSize(const SkPoint &point)
std::vector< Rect > ToRects(const SkRect tex[], int count)
PathBuilder::RoundingRadii ToRoundingRadii(const SkRRect &rrect)
std::vector< Matrix > ToRSXForms(const SkRSXform xform[], int count)
bool IsNearlySimpleRRect(const SkRRect &rr)
Like SkRRect.isSimple, but allows the corners to differ by kEhCloseEnough.
Rect ToRect(const SkRect &rect)
Color ToColor(const flutter::DlColor &color)
float Scalar
Definition: scalar.h:18
constexpr float kEhCloseEnough
Definition: constants.h:56
FillType
Definition: path.h:30
TSize< Scalar > Size
Definition: size.h:137
SkScalar h
float fX
x-axis value
Definition: SkPoint_impl.h:164
float fY
y-axis value
Definition: SkPoint_impl.h:165
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr TPoint< Type > MakeXY(Type x, Type y)
Definition: point.h:46
static constexpr TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63