Flutter Impeller
formats.h
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 #ifndef FLUTTER_IMPELLER_TOOLKIT_INTEROP_FORMATS_H_
6 #define FLUTTER_IMPELLER_TOOLKIT_INTEROP_FORMATS_H_
7 
8 #include <vector>
9 
10 #include "flutter/display_list/dl_builder.h"
11 #include "flutter/display_list/dl_color.h"
12 #include "flutter/display_list/geometry/dl_path_builder.h"
13 #include "flutter/txt/src/txt/font_style.h"
14 #include "flutter/txt/src/txt/font_weight.h"
15 #include "flutter/txt/src/txt/paragraph_style.h"
16 #include "impeller/entity/entity.h"
20 #include "impeller/geometry/rect.h"
21 #include "impeller/geometry/size.h"
23 
24 #include "flutter/third_party/skia/include/core/SkM44.h"
25 #include "flutter/third_party/skia/include/core/SkPath.h"
26 #include "flutter/third_party/skia/include/core/SkRRect.h"
27 
28 namespace impeller::interop {
29 
30 constexpr std::optional<SkRect> ToSkiaType(const ImpellerRect* rect) {
31  if (!rect) {
32  return std::nullopt;
33  }
34  return SkRect::MakeXYWH(rect->x, rect->y, rect->width, rect->height);
35 }
36 
37 constexpr SkPoint ToSkiaType(const Point& point) {
38  return SkPoint::Make(point.x, point.y);
39 }
40 
41 constexpr SkColor ToSkiaType(const ImpellerColor& color) {
42  return SkColorSetARGB(color.alpha * 255, //
43  color.red * 255, //
44  color.green * 255, //
45  color.blue * 255 //
46  );
47 }
48 
49 constexpr SkVector ToSkiaVector(const Size& point) {
50  return SkVector::Make(point.width, point.height);
51 }
52 
53 constexpr SkRect ToSkiaType(const Rect& rect) {
54  return SkRect::MakeXYWH(rect.GetX(), //
55  rect.GetY(), //
56  rect.GetWidth(), //
57  rect.GetHeight() //
58  );
59 }
60 
61 constexpr SkPathFillType ToSkiaType(FillType type) {
62  switch (type) {
63  case FillType::kNonZero:
64  return SkPathFillType::kWinding;
65  case FillType::kOdd:
66  return SkPathFillType::kEvenOdd;
67  }
68  return SkPathFillType::kWinding;
69 }
70 
71 constexpr SkIRect ToSkiaType(IRect rect) {
72  return SkIRect::MakeXYWH(rect.GetX(), //
73  rect.GetY(), //
74  rect.GetWidth(), //
75  rect.GetHeight() //
76  );
77 }
78 
79 template <class SkiaType, class OtherType>
80 std::vector<SkiaType> ToSkiaType(const std::vector<OtherType>& other_vec) {
81  std::vector<SkiaType> skia_vec;
82  skia_vec.reserve(other_vec.size());
83  for (const auto& other : other_vec) {
84  skia_vec.emplace_back(ToSkiaType(other));
85  }
86  return skia_vec;
87 }
88 
89 constexpr flutter::DlColor ToDisplayListType(Color color) {
90  return flutter::DlColor::RGBA(color.red, //
91  color.green, //
92  color.blue, //
93  color.alpha //
94  );
95 }
96 
97 inline SkMatrix ToSkMatrix(const Matrix& matrix) {
98  return SkM44::ColMajor(matrix.m).asM33();
99 }
100 
101 template <class DlType, class OtherType>
102 std::vector<DlType> ToDisplayListType(const std::vector<OtherType>& other_vec) {
103  std::vector<DlType> dl_vec;
104  dl_vec.reserve(other_vec.size());
105  for (const auto& other : other_vec) {
106  dl_vec.emplace_back(ToDisplayListType(other));
107  }
108  return dl_vec;
109 }
110 
111 constexpr flutter::DlImageSampling ToDisplayListType(
112  ImpellerTextureSampling sampling) {
113  switch (sampling) {
115  return flutter::DlImageSampling::kNearestNeighbor;
117  return flutter::DlImageSampling::kLinear;
118  }
119  return flutter::DlImageSampling::kLinear;
120 }
121 
122 constexpr flutter::DlBlurStyle ToDisplayListType(ImpellerBlurStyle style) {
123  switch (style) {
125  return flutter::DlBlurStyle::kNormal;
127  return flutter::DlBlurStyle::kSolid;
129  return flutter::DlBlurStyle::kOuter;
131  return flutter::DlBlurStyle::kInner;
132  }
133  return flutter::DlBlurStyle::kNormal;
134 }
135 
136 constexpr flutter::DlBlendMode ToDisplayListType(BlendMode mode) {
137  using Mode = flutter::DlBlendMode;
138  switch (mode) {
139  case BlendMode::kClear:
140  return Mode::kClear;
141  case BlendMode::kSrc:
142  return Mode::kSrc;
143  case BlendMode::kDst:
144  return Mode::kDst;
145  case BlendMode::kSrcOver:
146  return Mode::kSrcOver;
147  case BlendMode::kDstOver:
148  return Mode::kDstOver;
149  case BlendMode::kSrcIn:
150  return Mode::kSrcIn;
151  case BlendMode::kDstIn:
152  return Mode::kDstIn;
153  case BlendMode::kSrcOut:
154  return Mode::kSrcOut;
155  case BlendMode::kDstOut:
156  return Mode::kDstOut;
157  case BlendMode::kSrcATop:
158  return Mode::kSrcATop;
159  case BlendMode::kDstATop:
160  return Mode::kDstATop;
161  case BlendMode::kXor:
162  return Mode::kXor;
163  case BlendMode::kPlus:
164  return Mode::kPlus;
166  return Mode::kModulate;
167  case BlendMode::kScreen:
168  return Mode::kScreen;
169  case BlendMode::kOverlay:
170  return Mode::kOverlay;
171  case BlendMode::kDarken:
172  return Mode::kDarken;
173  case BlendMode::kLighten:
174  return Mode::kLighten;
176  return Mode::kColorDodge;
178  return Mode::kColorBurn;
180  return Mode::kHardLight;
182  return Mode::kSoftLight;
184  return Mode::kDifference;
186  return Mode::kExclusion;
188  return Mode::kMultiply;
189  case BlendMode::kHue:
190  return Mode::kHue;
192  return Mode::kSaturation;
193  case BlendMode::kColor:
194  return Mode::kColor;
196  return Mode::kLuminosity;
197  }
198  return Mode::kSrcOver;
199 }
200 
201 inline SkRRect ToSkiaType(const Rect& rect, const RoundingRadii& radii) {
202  using Corner = SkRRect::Corner;
203  SkVector sk_radii[4];
204  sk_radii[Corner::kUpperLeft_Corner] = ToSkiaVector(radii.top_left);
205  sk_radii[Corner::kUpperRight_Corner] = ToSkiaVector(radii.top_right);
206  sk_radii[Corner::kLowerRight_Corner] = ToSkiaVector(radii.bottom_right);
207  sk_radii[Corner::kLowerLeft_Corner] = ToSkiaVector(radii.bottom_left);
208  SkRRect result;
209  result.setRectRadii(ToSkiaType(rect), sk_radii);
210  return result;
211 }
212 
213 constexpr Matrix ToImpellerType(const ImpellerMatrix& m) {
214  return Matrix(m.m[0], m.m[1], m.m[2], m.m[3], //
215  m.m[4], m.m[5], m.m[6], m.m[7], //
216  m.m[8], m.m[9], m.m[10], m.m[11], //
217  m.m[12], m.m[13], m.m[14], m.m[15] //
218  );
219 }
220 
221 constexpr void FromImpellerType(const Matrix& from, ImpellerMatrix& to) {
222  to.m[0] = from.m[0];
223  to.m[1] = from.m[1];
224  to.m[2] = from.m[2];
225  to.m[3] = from.m[3];
226  to.m[4] = from.m[4];
227  to.m[5] = from.m[5];
228  to.m[6] = from.m[6];
229  to.m[7] = from.m[7];
230  to.m[8] = from.m[8];
231  to.m[9] = from.m[9];
232  to.m[10] = from.m[10];
233  to.m[11] = from.m[11];
234  to.m[12] = from.m[12];
235  to.m[13] = from.m[13];
236  to.m[14] = from.m[14];
237  to.m[15] = from.m[15];
238 }
239 
240 constexpr Size ToImpellerType(const ImpellerSize& size) {
241  return Size{size.width, size.height};
242 }
243 
244 constexpr Point ToImpellerType(const ImpellerPoint& point) {
245  return Point{point.x, point.y};
246 }
247 
248 constexpr Size ToImpellerSize(const ImpellerPoint& point) {
249  return Size{point.x, point.y};
250 }
251 
252 constexpr Rect ToImpellerType(const ImpellerRect& rect) {
253  return Rect::MakeXYWH(rect.x, rect.y, rect.width, rect.height);
254 }
255 
256 constexpr flutter::DlTileMode ToDisplayListType(ImpellerTileMode mode) {
257  switch (mode) {
259  return flutter::DlTileMode::kClamp;
261  return flutter::DlTileMode::kRepeat;
263  return flutter::DlTileMode::kMirror;
265  return flutter::DlTileMode::kDecal;
266  }
267  return flutter::DlTileMode::kClamp;
268 }
269 
271  auto result = RoundingRadii{};
272  result.top_left = ToImpellerSize(radii.top_left);
273  result.bottom_left = ToImpellerSize(radii.bottom_left);
274  result.top_right = ToImpellerSize(radii.top_right);
275  result.bottom_right = ToImpellerSize(radii.bottom_right);
276  return result;
277 }
278 
280  switch (type) {
282  return FillType::kNonZero;
284  return FillType::kOdd;
285  }
286  return FillType::kNonZero;
287 }
288 
289 constexpr flutter::DlClipOp ToImpellerType(ImpellerClipOperation op) {
290  switch (op) {
292  return flutter::DlClipOp::kDifference;
294  return flutter::DlClipOp::kIntersect;
295  }
296  return flutter::DlClipOp::kDifference;
297 }
298 
299 constexpr Color ToImpellerType(const ImpellerColor& color) {
300  Color result;
301  result.red = color.red;
302  result.green = color.green;
303  result.blue = color.blue;
304  result.alpha = color.alpha;
305  return result;
306 }
307 
309  switch (mode) {
311  return BlendMode::kClear;
313  return BlendMode::kSrc;
315  return BlendMode::kDst;
317  return BlendMode::kSrcOver;
319  return BlendMode::kDstOver;
321  return BlendMode::kSrcIn;
323  return BlendMode::kDstIn;
325  return BlendMode::kSrcOut;
327  return BlendMode::kDstOut;
329  return BlendMode::kSrcATop;
331  return BlendMode::kDstATop;
333  return BlendMode::kXor;
335  return BlendMode::kPlus;
337  return BlendMode::kModulate;
339  return BlendMode::kScreen;
341  return BlendMode::kOverlay;
343  return BlendMode::kDarken;
345  return BlendMode::kLighten;
347  return BlendMode::kColorDodge;
349  return BlendMode::kColorBurn;
351  return BlendMode::kHardLight;
353  return BlendMode::kSoftLight;
355  return BlendMode::kDifference;
357  return BlendMode::kExclusion;
359  return BlendMode::kMultiply;
361  return BlendMode::kHue;
363  return BlendMode::kSaturation;
365  return BlendMode::kColor;
367  return BlendMode::kLuminosity;
368  }
369  return BlendMode::kSrcOver;
370 }
371 
372 constexpr flutter::DlDrawStyle ToDisplayListType(ImpellerDrawStyle style) {
373  switch (style) {
375  return flutter::DlDrawStyle::kFill;
377  return flutter::DlDrawStyle::kStroke;
379  return flutter::DlDrawStyle::kStrokeAndFill;
380  }
381  return flutter::DlDrawStyle::kFill;
382 }
383 
384 constexpr flutter::DlStrokeCap ToDisplayListType(ImpellerStrokeCap cap) {
385  switch (cap) {
387  return flutter::DlStrokeCap::kButt;
389  return flutter::DlStrokeCap::kRound;
391  return flutter::DlStrokeCap::kSquare;
392  }
393  return flutter::DlStrokeCap::kButt;
394 }
395 
396 constexpr flutter::DlStrokeJoin ToDisplayListType(ImpellerStrokeJoin join) {
397  switch (join) {
399  return flutter::DlStrokeJoin::kMiter;
401  return flutter::DlStrokeJoin::kRound;
403  return flutter::DlStrokeJoin::kBevel;
404  }
405  return flutter::DlStrokeJoin::kMiter;
406 }
407 
409  switch (format) {
412  }
414 }
415 
416 constexpr ISize ToImpellerType(const ImpellerISize& size) {
417  return ISize::MakeWH(size.width, size.height);
418 }
419 
420 constexpr flutter::DlColorSpace ToDisplayListType(
421  ImpellerColorSpace color_space) {
422  switch (color_space) {
424  return flutter::DlColorSpace::kSRGB;
426  return flutter::DlColorSpace::kExtendedSRGB;
428  return flutter::DlColorSpace::kDisplayP3;
429  }
430  return flutter::DlColorSpace::kSRGB;
431 }
432 
433 constexpr flutter::DlColor ToDisplayListType(ImpellerColor color) {
434  return flutter::DlColor(color.alpha, //
435  color.red, //
436  color.green, //
437  color.blue, //
439  );
440 }
441 
442 constexpr txt::TextDecorationStyle ToTxtType(
444  switch (style) {
446  return txt::TextDecorationStyle::kSolid;
448  return txt::TextDecorationStyle::kDouble;
450  return txt::TextDecorationStyle::kDotted;
452  return txt::TextDecorationStyle::kDashed;
454  return txt::TextDecorationStyle::kWavy;
455  }
456  return txt::TextDecorationStyle::kSolid;
457 }
458 
459 constexpr txt::FontWeight ToTxtType(ImpellerFontWeight weight) {
460  switch (weight) {
462  return txt::FontWeight::w100;
464  return txt::FontWeight::w200;
466  return txt::FontWeight::w300;
468  return txt::FontWeight::w400;
470  return txt::FontWeight::w500;
472  return txt::FontWeight::w600;
474  return txt::FontWeight::w700;
476  return txt::FontWeight::w800;
478  return txt::FontWeight::w900;
479  }
480  return txt::FontWeight::w400;
481 }
482 
483 constexpr txt::FontStyle ToTxtType(ImpellerFontStyle style) {
484  switch (style) {
486  return txt::FontStyle::normal;
488  return txt::FontStyle::italic;
489  }
490  return txt::FontStyle::normal;
491 }
492 
493 constexpr txt::TextAlign ToTxtType(ImpellerTextAlignment align) {
494  switch (align) {
496  return txt::TextAlign::left;
498  return txt::TextAlign::right;
500  return txt::TextAlign::center;
502  return txt::TextAlign::justify;
504  return txt::TextAlign::start;
506  return txt::TextAlign::end;
507  }
508  return txt::TextAlign::left;
509 }
510 
511 constexpr txt::TextDirection ToTxtType(ImpellerTextDirection direction) {
512  switch (direction) {
514  return txt::TextDirection::rtl;
516  return txt::TextDirection::ltr;
517  }
518  return txt::TextDirection::ltr;
519 }
520 
521 } // namespace impeller::interop
522 
523 #endif // FLUTTER_IMPELLER_TOOLKIT_INTEROP_FORMATS_H_
GLenum type
ImpellerFillType
Definition: impeller.h:364
@ kImpellerFillTypeOdd
Definition: impeller.h:366
@ kImpellerFillTypeNonZero
Definition: impeller.h:365
ImpellerTextDirection
Definition: impeller.h:479
@ kImpellerTextDirectionLTR
Definition: impeller.h:481
@ kImpellerTextDirectionRTL
Definition: impeller.h:480
ImpellerTextureSampling
Definition: impeller.h:428
@ kImpellerTextureSamplingNearestNeighbor
Definition: impeller.h:429
@ kImpellerTextureSamplingLinear
Definition: impeller.h:430
ImpellerTextDecorationStyle
Definition: impeller.h:491
@ kImpellerTextDecorationStyleSolid
Definition: impeller.h:492
@ kImpellerTextDecorationStyleWavy
Definition: impeller.h:496
@ kImpellerTextDecorationStyleDouble
Definition: impeller.h:493
@ kImpellerTextDecorationStyleDotted
Definition: impeller.h:494
@ kImpellerTextDecorationStyleDashed
Definition: impeller.h:495
ImpellerStrokeJoin
Definition: impeller.h:418
@ kImpellerStrokeJoinRound
Definition: impeller.h:420
@ kImpellerStrokeJoinBevel
Definition: impeller.h:421
@ kImpellerStrokeJoinMiter
Definition: impeller.h:419
ImpellerBlendMode
Definition: impeller.h:374
@ kImpellerBlendModeSaturation
Definition: impeller.h:401
@ kImpellerBlendModeSoftLight
Definition: impeller.h:396
@ kImpellerBlendModeHardLight
Definition: impeller.h:395
@ kImpellerBlendModeLuminosity
Definition: impeller.h:403
@ kImpellerBlendModeLighten
Definition: impeller.h:392
@ kImpellerBlendModeModulate
Definition: impeller.h:388
@ kImpellerBlendModeSourceIn
Definition: impeller.h:380
@ kImpellerBlendModeDifference
Definition: impeller.h:397
@ kImpellerBlendModeClear
Definition: impeller.h:375
@ kImpellerBlendModeColor
Definition: impeller.h:402
@ kImpellerBlendModeMultiply
Definition: impeller.h:399
@ kImpellerBlendModeSourceATop
Definition: impeller.h:384
@ kImpellerBlendModeDestinationOut
Definition: impeller.h:383
@ kImpellerBlendModeScreen
Definition: impeller.h:389
@ kImpellerBlendModeExclusion
Definition: impeller.h:398
@ kImpellerBlendModeColorBurn
Definition: impeller.h:394
@ kImpellerBlendModeDarken
Definition: impeller.h:391
@ kImpellerBlendModePlus
Definition: impeller.h:387
@ kImpellerBlendModeOverlay
Definition: impeller.h:390
@ kImpellerBlendModeDestinationIn
Definition: impeller.h:381
@ kImpellerBlendModeDestinationATop
Definition: impeller.h:385
@ kImpellerBlendModeDestination
Definition: impeller.h:377
@ kImpellerBlendModeSourceOver
Definition: impeller.h:378
@ kImpellerBlendModeXor
Definition: impeller.h:386
@ kImpellerBlendModeColorDodge
Definition: impeller.h:393
@ kImpellerBlendModeDestinationOver
Definition: impeller.h:379
@ kImpellerBlendModeSource
Definition: impeller.h:376
@ kImpellerBlendModeSourceOut
Definition: impeller.h:382
@ kImpellerBlendModeHue
Definition: impeller.h:400
ImpellerFontWeight
Definition: impeller.h:453
@ kImpellerFontWeight400
Definition: impeller.h:457
@ kImpellerFontWeight500
Definition: impeller.h:458
@ kImpellerFontWeight700
Definition: impeller.h:460
@ kImpellerFontWeight200
Definition: impeller.h:455
@ kImpellerFontWeight300
Definition: impeller.h:456
@ kImpellerFontWeight900
Definition: impeller.h:462
@ kImpellerFontWeight800
Definition: impeller.h:461
@ kImpellerFontWeight600
Definition: impeller.h:459
@ kImpellerFontWeight100
Definition: impeller.h:454
ImpellerStrokeCap
Definition: impeller.h:412
@ kImpellerStrokeCapButt
Definition: impeller.h:413
@ kImpellerStrokeCapRound
Definition: impeller.h:414
@ kImpellerStrokeCapSquare
Definition: impeller.h:415
ImpellerDrawStyle
Definition: impeller.h:406
@ kImpellerDrawStyleStroke
Definition: impeller.h:408
@ kImpellerDrawStyleFill
Definition: impeller.h:407
@ kImpellerDrawStyleStrokeAndFill
Definition: impeller.h:409
ImpellerColorSpace
Definition: impeller.h:447
@ kImpellerColorSpaceExtendedSRGB
Definition: impeller.h:449
@ kImpellerColorSpaceSRGB
Definition: impeller.h:448
@ kImpellerColorSpaceDisplayP3
Definition: impeller.h:450
ImpellerTileMode
Definition: impeller.h:433
@ kImpellerTileModeMirror
Definition: impeller.h:436
@ kImpellerTileModeClamp
Definition: impeller.h:434
@ kImpellerTileModeRepeat
Definition: impeller.h:435
@ kImpellerTileModeDecal
Definition: impeller.h:437
ImpellerTextAlignment
Definition: impeller.h:470
@ kImpellerTextAlignmentJustify
Definition: impeller.h:474
@ kImpellerTextAlignmentLeft
Definition: impeller.h:471
@ kImpellerTextAlignmentCenter
Definition: impeller.h:473
@ kImpellerTextAlignmentRight
Definition: impeller.h:472
@ kImpellerTextAlignmentStart
Definition: impeller.h:475
@ kImpellerTextAlignmentEnd
Definition: impeller.h:476
ImpellerFontStyle
Definition: impeller.h:465
@ kImpellerFontStyleItalic
Definition: impeller.h:467
@ kImpellerFontStyleNormal
Definition: impeller.h:466
ImpellerClipOperation
Definition: impeller.h:369
@ kImpellerClipOperationIntersect
Definition: impeller.h:371
@ kImpellerClipOperationDifference
Definition: impeller.h:370
ImpellerBlurStyle
Definition: impeller.h:440
@ kImpellerBlurStyleNormal
Definition: impeller.h:441
@ kImpellerBlurStyleOuter
Definition: impeller.h:443
@ kImpellerBlurStyleInner
Definition: impeller.h:444
@ kImpellerBlurStyleSolid
Definition: impeller.h:442
ImpellerPixelFormat
Definition: impeller.h:424
@ kImpellerPixelFormatRGBA8888
Definition: impeller.h:425
constexpr SkVector ToSkiaVector(const Size &point)
Definition: formats.h:49
constexpr txt::TextDecorationStyle ToTxtType(ImpellerTextDecorationStyle style)
Definition: formats.h:442
SkMatrix ToSkMatrix(const Matrix &matrix)
Definition: formats.h:97
constexpr Matrix ToImpellerType(const ImpellerMatrix &m)
Definition: formats.h:213
constexpr Size ToImpellerSize(const ImpellerPoint &point)
Definition: formats.h:248
constexpr void FromImpellerType(const Matrix &from, ImpellerMatrix &to)
Definition: formats.h:221
constexpr flutter::DlColor ToDisplayListType(Color color)
Definition: formats.h:89
constexpr std::optional< SkRect > ToSkiaType(const ImpellerRect *rect)
Definition: formats.h:30
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
BlendMode
Definition: color.h:58
float blue
Definition: impeller.h:615
float alpha
Definition: impeller.h:616
float green
Definition: impeller.h:614
ImpellerColorSpace color_space
Definition: impeller.h:617
int64_t height
Definition: impeller.h:521
int64_t width
Definition: impeller.h:520
float m[16]
Definition: impeller.h:540
float width
Definition: impeller.h:505
float height
Definition: impeller.h:506
ImpellerPoint top_left
Definition: impeller.h:606
ImpellerPoint top_right
Definition: impeller.h:608
ImpellerPoint bottom_left
Definition: impeller.h:607
ImpellerPoint bottom_right
Definition: impeller.h:609
float height
Definition: impeller.h:516
float width
Definition: impeller.h:515
Scalar blue
Definition: color.h:138
Scalar alpha
Definition: color.h:143
Scalar red
Definition: color.h:128
Scalar green
Definition: color.h:133
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
Scalar m[16]
Definition: matrix.h:39
constexpr Type GetY() const
Returns the Y coordinate of the upper left corner, equivalent to |GetOrigin().y|.
Definition: rect.h:341
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition: rect.h:351
constexpr Type GetX() const
Returns the X coordinate of the upper left corner, equivalent to |GetOrigin().x|.
Definition: rect.h:337
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:136
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition: rect.h:345
Type height
Definition: size.h:29
Type width
Definition: size.h:28
static constexpr TSize MakeWH(Type width, Type height)
Definition: size.h:43
const size_t start
const size_t end