| Flutter Impeller |

canvas.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/canvas.h"
6
7 #include <algorithm>
8 #include <optional>
9 #include <utility>
10
11 #include "flutter/fml/logging.h"
12 #include "impeller/aiks/image_filter.h"
13 #include "impeller/aiks/paint_pass_delegate.h"
14 #include "impeller/entity/contents/atlas_contents.h"
15 #include "impeller/entity/contents/clip_contents.h"
16 #include "impeller/entity/contents/solid_rrect_blur_contents.h"
17 #include "impeller/entity/contents/text_contents.h"
18 #include "impeller/entity/contents/texture_contents.h"
19 #include "impeller/entity/contents/vertices_contents.h"
20 #include "impeller/entity/geometry/geometry.h"
21 #include "impeller/geometry/path_builder.h"
22
23 namespace impeller {
24
25 Canvas::Canvas() {
26 Initialize(std::nullopt);
27 }
28
29 Canvas::Canvas(Rect cull_rect) {
30 Initialize(cull_rect);
31 }
32
33 Canvas::Canvas(IRect cull_rect) {
34 Initialize(Rect::MakeLTRB(cull_rect.GetLeft(), cull_rect.GetTop(),
35 cull_rect.GetRight(), cull_rect.GetBottom()));
36 }
37
38 Canvas::~Canvas() = default;
39
40 void Canvas::Initialize(std::optional<Rect> cull_rect) {
41 initial_cull_rect_ = cull_rect;
42 base_pass_ = std::make_unique<EntityPass>();
43 current_pass_ = base_pass_.get();
44 xformation_stack_.emplace_back(CanvasStackEntry{.cull_rect = cull_rect});
45 FML_DCHECK(GetSaveCount() == 1u);
46 FML_DCHECK(base_pass_->GetSubpassesDepth() == 1u);
47 }
48
49 void Canvas::Reset() {
50 base_pass_ = nullptr;
51 current_pass_ = nullptr;
52 xformation_stack_ = {};
53 }
54
55 void Canvas::Save() {
56 Save(false);
57 }
58
59 void Canvas::Save(bool create_subpass,
60 BlendMode blend_mode,
61 const std::shared_ptr<ImageFilter>& backdrop_filter) {
62 auto entry = CanvasStackEntry{};
63 entry.xformation = xformation_stack_.back().xformation;
64 entry.cull_rect = xformation_stack_.back().cull_rect;
65 entry.stencil_depth = xformation_stack_.back().stencil_depth;
66 if (create_subpass) {
67 entry.rendering_mode = Entity::RenderingMode::kSubpass;
68 auto subpass = std::make_unique<EntityPass>();
69 subpass->SetEnableOffscreenCheckerboard(
70 debug_options.offscreen_texture_checkerboard);
71 if (backdrop_filter) {
72 EntityPass::BackdropFilterProc backdrop_filter_proc =
73 [backdrop_filter = backdrop_filter->Clone()](
74 const FilterInput::Ref& input, const Matrix& effect_transform,
75 Entity::RenderingMode rendering_mode) {
76 auto filter = backdrop_filter->WrapInput(input);
77 filter->SetEffectTransform(effect_transform);
78 filter->SetRenderingMode(rendering_mode);
79 return filter;
80 };
81 subpass->SetBackdropFilter(backdrop_filter_proc);
82 }
83 subpass->SetBlendMode(blend_mode);
84 current_pass_ = GetCurrentPass().AddSubpass(std::move(subpass));
85 current_pass_->SetTransformation(xformation_stack_.back().xformation);
86 current_pass_->SetStencilDepth(xformation_stack_.back().stencil_depth);
87 }
88 xformation_stack_.emplace_back(entry);
89 }
90
91 bool Canvas::Restore() {
92 FML_DCHECK(xformation_stack_.size() > 0);
93 if (xformation_stack_.size() == 1) {
94 return false;
95 }
96 if (xformation_stack_.back().rendering_mode ==
97 Entity::RenderingMode::kSubpass) {
98 current_pass_ = GetCurrentPass().GetSuperpass();
99 FML_DCHECK(current_pass_);
100 }
101
102 bool contains_clips = xformation_stack_.back().contains_clips;
103 xformation_stack_.pop_back();
104
105 if (contains_clips) {
106 RestoreClip();
107 }
108
109 return true;
110 }
111
112 void Canvas::Concat(const Matrix& xformation) {
113 xformation_stack_.back().xformation = GetCurrentTransformation() * xformation;
114 }
115
116 void Canvas::PreConcat(const Matrix& xformation) {
117 xformation_stack_.back().xformation = xformation * GetCurrentTransformation();
118 }
119
120 void Canvas::ResetTransform() {
121 xformation_stack_.back().xformation = {};
122 }
123
124 void Canvas::Transform(const Matrix& xformation) {
125 Concat(xformation);
126 }
127
128 const Matrix& Canvas::GetCurrentTransformation() const {
129 return xformation_stack_.back().xformation;
130 }
131
132 const std::optional<Rect> Canvas::GetCurrentLocalCullingBounds() const {
133 auto cull_rect = xformation_stack_.back().cull_rect;
134 if (cull_rect.has_value()) {
135 Matrix inverse = xformation_stack_.back().xformation.Invert();
136 cull_rect = cull_rect.value().TransformBounds(inverse);
137 }
138 return cull_rect;
139 }
140
141 void Canvas::Translate(const Vector3& offset) {
142 Concat(Matrix::MakeTranslation(offset));
143 }
144
145 void Canvas::Scale(const Vector2& scale) {
146 Concat(Matrix::MakeScale(scale));
147 }
148
149 void Canvas::Scale(const Vector3& scale) {
150 Concat(Matrix::MakeScale(scale));
151 }
152
153 void Canvas::Skew(Scalar sx, Scalar sy) {
154 Concat(Matrix::MakeSkew(sx, sy));
155 }
156
157 void Canvas::Rotate(Radians radians) {
158 Concat(Matrix::MakeRotationZ(radians));
159 }
160
161 size_t Canvas::GetSaveCount() const {
162 return xformation_stack_.size();
163 }
164
165 void Canvas::RestoreToCount(size_t count) {
166 while (GetSaveCount() > count) {
167 if (!Restore()) {
168 return;
169 }
170 }
171 }
172
173 void Canvas::DrawPath(const Path& path, const Paint& paint) {
174 Entity entity;
175 entity.SetTransformation(GetCurrentTransformation());
176 entity.SetStencilDepth(GetStencilDepth());
177 entity.SetBlendMode(paint.blend_mode);
178 entity.SetContents(paint.WithFilters(paint.CreateContentsForEntity(path)));
179
180 GetCurrentPass().AddEntity(entity);
181 }
182
183 void Canvas::DrawPaint(const Paint& paint) {
184 Entity entity;
185 entity.SetTransformation(GetCurrentTransformation());
186 entity.SetStencilDepth(GetStencilDepth());
187 entity.SetBlendMode(paint.blend_mode);
188 entity.SetContents(paint.CreateContentsForEntity({}, true));
189
190 GetCurrentPass().AddEntity(entity);
191 }
192
193 bool Canvas::AttemptDrawBlurredRRect(const Rect& rect,
194 Scalar corner_radius,
195 const Paint& paint) {
196 Paint new_paint = paint;
197 if (new_paint.color_source.GetType() != ColorSource::Type::kColor ||
198 new_paint.style != Paint::Style::kFill) {
199 return false;
200 }
201
202 if (!new_paint.mask_blur_descriptor.has_value() ||
203 new_paint.mask_blur_descriptor->style !=
204 FilterContents::BlurStyle::kNormal) {
205 return false;
206 }
207
208 // For symmetrically mask blurred solid RRects, absorb the mask blur and use
209 // a faster SDF approximation.
210
211 auto contents = std::make_shared<SolidRRectBlurContents>();
212 contents->SetColor(new_paint.color);
213 contents->SetSigma(new_paint.mask_blur_descriptor->sigma);
214 contents->SetRRect(rect, corner_radius);
215
216 new_paint.mask_blur_descriptor = std::nullopt;
217
218 Entity entity;
219 entity.SetTransformation(GetCurrentTransformation());
220 entity.SetStencilDepth(GetStencilDepth());
221 entity.SetBlendMode(new_paint.blend_mode);
222 entity.SetContents(new_paint.WithFilters(std::move(contents)));
223
224 GetCurrentPass().AddEntity(entity);
225
226 return true;
227 }
228
229 void Canvas::DrawRect(Rect rect, const Paint& paint) {
230 if (paint.style == Paint::Style::kStroke) {
231 DrawPath(PathBuilder{}.AddRect(rect).TakePath(), paint);
232 return;
233 }
234
235 if (AttemptDrawBlurredRRect(rect, 0, paint)) {
236 return;
237 }
238
239 Entity entity;
240 entity.SetTransformation(GetCurrentTransformation());
241 entity.SetStencilDepth(GetStencilDepth());
242 entity.SetBlendMode(paint.blend_mode);
243 entity.SetContents(paint.WithFilters(
244 paint.CreateContentsForGeometry(Geometry::MakeRect(rect))));
245
246 GetCurrentPass().AddEntity(entity);
247 }
248
249 void Canvas::DrawRRect(Rect rect, Scalar corner_radius, const Paint& paint) {
250 if (AttemptDrawBlurredRRect(rect, corner_radius, paint)) {
251 return;
252 }
253 auto path = PathBuilder{}
254 .SetConvexity(Convexity::kConvex)
255 .AddRoundedRect(rect, corner_radius)
256 .TakePath();
257 if (paint.style == Paint::Style::kFill) {
258 Entity entity;
259 entity.SetTransformation(GetCurrentTransformation());
260 entity.SetStencilDepth(GetStencilDepth());
261 entity.SetBlendMode(paint.blend_mode);
262 entity.SetContents(paint.WithFilters(
263 paint.CreateContentsForGeometry(Geometry::MakeFillPath(path))));
264
265 GetCurrentPass().AddEntity(entity);
266 return;
267 }
268 DrawPath(path, paint);
269 }
270
271 void Canvas::DrawCircle(Point center, Scalar radius, const Paint& paint) {
272 Size half_size(radius, radius);
273 if (AttemptDrawBlurredRRect(Rect(center - half_size, half_size * 2), radius,
274 paint)) {
275 return;
276 }
277 auto circle_path = PathBuilder{}
278 .AddCircle(center, radius)
279 .SetConvexity(Convexity::kConvex)
280 .TakePath();
281 DrawPath(circle_path, paint);
282 }
283
284 void Canvas::ClipPath(const Path& path, Entity::ClipOperation clip_op) {
285 ClipGeometry(Geometry::MakeFillPath(path), clip_op);
286 if (clip_op == Entity::ClipOperation::kIntersect) {
287 auto bounds = path.GetBoundingBox();
288 if (bounds.has_value()) {
289 IntersectCulling(bounds.value());
290 }
291 }
292 }
293
294 void Canvas::ClipRect(const Rect& rect, Entity::ClipOperation clip_op) {
295 auto geometry = Geometry::MakeRect(rect);
296 auto& cull_rect = xformation_stack_.back().cull_rect;
297 if (clip_op == Entity::ClipOperation::kIntersect && //
298 cull_rect.has_value() && //
299 geometry->CoversArea(xformation_stack_.back().xformation, *cull_rect) //
300 ) {
301 return; // This clip will do nothing, so skip it.
302 }
303
304 ClipGeometry(std::move(geometry), clip_op);
305 switch (clip_op) {
306 case Entity::ClipOperation::kIntersect:
307 IntersectCulling(rect);
308 break;
309 case Entity::ClipOperation::kDifference:
310 SubtractCulling(rect);
311 break;
312 }
313 }
314
315 void Canvas::ClipRRect(const Rect& rect,
316 Scalar corner_radius,
317 Entity::ClipOperation clip_op) {
318 auto path = PathBuilder{}
319 .SetConvexity(Convexity::kConvex)
320 .AddRoundedRect(rect, corner_radius)
321 .TakePath();
322
323 std::optional<Rect> inner_rect = (corner_radius * 2 < rect.size.width &&
324 corner_radius * 2 < rect.size.height)
325 ? rect.Expand(-corner_radius)
326 : std::make_optional<Rect>();
327 auto geometry = Geometry::MakeFillPath(path, inner_rect);
328 auto& cull_rect = xformation_stack_.back().cull_rect;
329 if (clip_op == Entity::ClipOperation::kIntersect && //
330 cull_rect.has_value() && //
331 geometry->CoversArea(xformation_stack_.back().xformation, *cull_rect) //
332 ) {
333 return; // This clip will do nothing, so skip it.
334 }
335
336 ClipGeometry(std::move(geometry), clip_op);
337 switch (clip_op) {
338 case Entity::ClipOperation::kIntersect:
339 IntersectCulling(rect);
340 break;
341 case Entity::ClipOperation::kDifference:
342 if (corner_radius <= 0) {
343 SubtractCulling(rect);
344 } else {
345 // We subtract the inner "tall" and "wide" rectangle pieces
346 // that fit inside the corners which cover the greatest area
347 // without involving the curved corners
348 // Since this is a subtract operation, we can subtract each
349 // rectangle piece individually without fear of interference.
350 if (corner_radius * 2 < rect.size.width) {
351 SubtractCulling(Rect::MakeLTRB(
352 rect.GetLeft() + corner_radius, rect.GetTop(),
353 rect.GetRight() - corner_radius, rect.GetBottom()));
354 }
355 if (corner_radius * 2 < rect.size.height) {
356 SubtractCulling(Rect::MakeLTRB(
357 rect.GetLeft(), rect.GetTop() + corner_radius, //
358 rect.GetRight(), rect.GetBottom() - corner_radius));
359 }
360 }
361 break;
362 }
363 }
364
365 void Canvas::ClipGeometry(std::unique_ptr<Geometry> geometry,
366 Entity::ClipOperation clip_op) {
367 auto contents = std::make_shared<ClipContents>();
368 contents->SetGeometry(std::move(geometry));
369 contents->SetClipOperation(clip_op);
370
371 Entity entity;
372 entity.SetTransformation(GetCurrentTransformation());
373 entity.SetContents(std::move(contents));
374 entity.SetStencilDepth(GetStencilDepth());
375
376 GetCurrentPass().AddEntity(entity);
377
378 ++xformation_stack_.back().stencil_depth;
379 xformation_stack_.back().contains_clips = true;
380 }
381
382 void Canvas::IntersectCulling(Rect clip_rect) {
383 clip_rect = clip_rect.TransformBounds(GetCurrentTransformation());
384 std::optional<Rect>& cull_rect = xformation_stack_.back().cull_rect;
385 if (cull_rect.has_value()) {
386 cull_rect = cull_rect
387 .value() //
388 .Intersection(clip_rect) //
389 .value_or(Rect{});
390 } else {
391 cull_rect = clip_rect;
392 }
393 }
394
395 void Canvas::SubtractCulling(Rect clip_rect) {
396 std::optional<Rect>& cull_rect = xformation_stack_.back().cull_rect;
397 if (cull_rect.has_value()) {
398 clip_rect = clip_rect.TransformBounds(GetCurrentTransformation());
399 cull_rect = cull_rect
400 .value() //
401 .Cutout(clip_rect) //
402 .value_or(Rect{});
403 }
404 // else (no cull) diff (any clip) is non-rectangular
405 }
406
407 void Canvas::RestoreClip() {
408 Entity entity;
409 entity.SetTransformation(GetCurrentTransformation());
410 // This path is empty because ClipRestoreContents just generates a quad that
411 // takes up the full render target.
412 entity.SetContents(std::make_shared<ClipRestoreContents>());
413 entity.SetStencilDepth(GetStencilDepth());
414
415 GetCurrentPass().AddEntity(entity);
416 }
417
418 void Canvas::DrawPoints(std::vector<Point> points,
419 Scalar radius,
420 const Paint& paint,
421 PointStyle point_style) {
422 if (radius <= 0) {
423 return;
424 }
425
426 Entity entity;
427 entity.SetTransformation(GetCurrentTransformation());
428 entity.SetStencilDepth(GetStencilDepth());
429 entity.SetBlendMode(paint.blend_mode);
430 entity.SetContents(paint.WithFilters(paint.CreateContentsForGeometry(
431 Geometry::MakePointField(std::move(points), radius,
432 /*round=*/point_style == PointStyle::kRound))));
433
434 GetCurrentPass().AddEntity(entity);
435 }
436
437 void Canvas::DrawPicture(const Picture& picture) {
438 if (!picture.pass) {
439 return;
440 }
441
442 // Clone the base pass and account for the CTM updates.
443 auto pass = picture.pass->Clone();
444
445 pass->IterateAllElements([&](auto& element) -> bool {
446 if (auto entity = std::get_if<Entity>(&element)) {
447 entity->IncrementStencilDepth(GetStencilDepth());
448 entity->SetTransformation(GetCurrentTransformation() *
449 entity->GetTransformation());
450 return true;
451 }
452
453 if (auto subpass = std::get_if<std::unique_ptr<EntityPass>>(&element)) {
454 subpass->get()->SetStencilDepth(subpass->get()->GetStencilDepth() +
455 GetStencilDepth());
456 return true;
457 }
458
459 FML_UNREACHABLE();
460 });
461
462 GetCurrentPass().AddSubpassInline(std::move(pass));
463
464 RestoreClip();
465 }
466
467 void Canvas::DrawImage(const std::shared_ptr<Image>& image,
468 Point offset,
469 const Paint& paint,
470 SamplerDescriptor sampler) {
471 if (!image) {
472 return;
473 }
474
475 const auto source = Rect::MakeSize(image->GetSize());
476 const auto dest =
477 Rect::MakeXYWH(offset.x, offset.y, source.size.width, source.size.height);
478
479 DrawImageRect(image, source, dest, paint, std::move(sampler));
480 }
481
482 void Canvas::DrawImageRect(const std::shared_ptr<Image>& image,
483 Rect source,
484 Rect dest,
485 const Paint& paint,
486 SamplerDescriptor sampler) {
487 if (!image || source.size.IsEmpty() || dest.size.IsEmpty()) {
488 return;
489 }
490
491 auto size = image->GetSize();
492
493 if (size.IsEmpty()) {
494 return;
495 }
496
497 auto contents = TextureContents::MakeRect(dest);
498 contents->SetTexture(image->GetTexture());
499 contents->SetSourceRect(source);
500 contents->SetSamplerDescriptor(std::move(sampler));
501 contents->SetOpacity(paint.color.alpha);
502 contents->SetDeferApplyingOpacity(paint.HasColorFilter());
503
504 Entity entity;
505 entity.SetBlendMode(paint.blend_mode);
506 entity.SetStencilDepth(GetStencilDepth());
507 entity.SetContents(paint.WithFilters(contents));
508 entity.SetTransformation(GetCurrentTransformation());
509
510 GetCurrentPass().AddEntity(entity);
511 }
512
513 Picture Canvas::EndRecordingAsPicture() {
514 Picture picture;
515 picture.pass = std::move(base_pass_);
516
517 Reset();
518 Initialize(initial_cull_rect_);
519
520 return picture;
521 }
522
523 EntityPass& Canvas::GetCurrentPass() {
524 FML_DCHECK(current_pass_ != nullptr);
525 return *current_pass_;
526 }
527
528 size_t Canvas::GetStencilDepth() const {
529 return xformation_stack_.back().stencil_depth;
530 }
531
532 void Canvas::SaveLayer(const Paint& paint,
533 std::optional<Rect> bounds,
534 const std::shared_ptr<ImageFilter>& backdrop_filter) {
535 Save(true, paint.blend_mode, backdrop_filter);
536
537 auto& new_layer_pass = GetCurrentPass();
538 new_layer_pass.SetBoundsLimit(bounds);
539
540 // Only apply opacity peephole on default blending.
541 if (paint.blend_mode == BlendMode::kSourceOver) {
542 new_layer_pass.SetDelegate(
543 std::make_shared<OpacityPeepholePassDelegate>(paint));
544 } else {
545 new_layer_pass.SetDelegate(std::make_shared<PaintPassDelegate>(paint));
546 }
547 }
548
549 void Canvas::DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
550 Point position,
551 const Paint& paint) {
552 Entity entity;
553 entity.SetStencilDepth(GetStencilDepth());
554 entity.SetBlendMode(paint.blend_mode);
555
556 auto text_contents = std::make_shared<TextContents>();
557 text_contents->SetTextFrame(text_frame);
558 text_contents->SetColor(paint.color);
559
560 entity.SetTransformation(GetCurrentTransformation() *
561 Matrix::MakeTranslation(position));
562
563 // TODO(bdero): This mask blur application is a hack. It will always wind up
564 // doing a gaussian blur that affects the color source itself
565 // instead of just the mask. The color filter text support
566 // needs to be reworked in order to interact correctly with
567 // mask filters.
568 // https://github.com/flutter/flutter/issues/133297
569 entity.SetContents(
570 paint.WithFilters(paint.WithMaskBlur(std::move(text_contents), true)));
571
572 GetCurrentPass().AddEntity(entity);
573 }
574
575 static bool UseColorSourceContents(
576 const std::shared_ptr<VerticesGeometry>& vertices,
577 const Paint& paint) {
578 // If there are no vertex color or texture coordinates. Or if there
579 // are vertex coordinates then only if the contents are an image or
580 // a solid color.
581 if (vertices->HasVertexColors()) {
582 return false;
583 }
584 if (vertices->HasTextureCoordinates() &&
585 (paint.color_source.GetType() == ColorSource::Type::kImage ||
586 paint.color_source.GetType() == ColorSource::Type::kColor)) {
587 return true;
588 }
589 return !vertices->HasTextureCoordinates();
590 }
591
592 void Canvas::DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
593 BlendMode blend_mode,
594 const Paint& paint) {
595 // Override the blend mode with kDestination in order to match the behavior
596 // of Skia's SK_LEGACY_IGNORE_DRAW_VERTICES_BLEND_WITH_NO_SHADER flag, which
597 // is enabled when the Flutter engine builds Skia.
598 if (paint.color_source.GetType() == ColorSource::Type::kColor) {
599 blend_mode = BlendMode::kDestination;
600 }
601
602 Entity entity;
603 entity.SetTransformation(GetCurrentTransformation());
604 entity.SetStencilDepth(GetStencilDepth());
605 entity.SetBlendMode(paint.blend_mode);
606
607 // If there are no vertex color or texture coordinates. Or if there
608 // are vertex coordinates then only if the contents are an image.
609 if (UseColorSourceContents(vertices, paint)) {
610 auto contents = paint.CreateContentsForGeometry(vertices);
611 entity.SetContents(paint.WithFilters(std::move(contents)));
612 GetCurrentPass().AddEntity(entity);
613 return;
614 }
615
616 auto src_paint = paint;
617 src_paint.color = paint.color.WithAlpha(1.0);
618
619 std::shared_ptr<Contents> src_contents =
620 src_paint.CreateContentsForGeometry(vertices);
621 if (vertices->HasTextureCoordinates()) {
622 // If the color source has an intrinsic size, then we use that to
623 // create the src contents as a simplification. Otherwise we use
624 // the extent of the texture coordinates to determine how large
625 // the src contents should be. If neither has a value we fall back
626 // to using the geometry coverage data.
627 Rect src_coverage;
628 auto size = src_contents->GetColorSourceSize();
629 if (size.has_value()) {
630 src_coverage = Rect::MakeXYWH(0, 0, size->width, size->height);
631 } else {
632 auto cvg = vertices->GetCoverage(Matrix{});
633 FML_CHECK(cvg.has_value());
634 src_coverage =
635 // Covered by FML_CHECK.
636 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
637 vertices->GetTextureCoordinateCoverge().value_or(cvg.value());
638 }
639 src_contents =
640 src_paint.CreateContentsForGeometry(Geometry::MakeRect(src_coverage));
641 }
642
643 auto contents = std::make_shared<VerticesContents>();
644 contents->SetAlpha(paint.color.alpha);
645 contents->SetBlendMode(blend_mode);
646 contents->SetGeometry(vertices);
647 contents->SetSourceContents(std::move(src_contents));
648 entity.SetContents(paint.WithFilters(std::move(contents)));
649
650 GetCurrentPass().AddEntity(entity);
651 }
652
653 void Canvas::DrawAtlas(const std::shared_ptr<Image>& atlas,
654 std::vector<Matrix> transforms,
655 std::vector<Rect> texture_coordinates,
656 std::vector<Color> colors,
657 BlendMode blend_mode,
658 SamplerDescriptor sampler,
659 std::optional<Rect> cull_rect,
660 const Paint& paint) {
661 if (!atlas) {
662 return;
663 }
664
665 std::shared_ptr<AtlasContents> contents = std::make_shared<AtlasContents>();
666 contents->SetColors(std::move(colors));
667 contents->SetTransforms(std::move(transforms));
668 contents->SetTextureCoordinates(std::move(texture_coordinates));
669 contents->SetTexture(atlas->GetTexture());
670 contents->SetSamplerDescriptor(std::move(sampler));
671 contents->SetBlendMode(blend_mode);
672 contents->SetCullRect(cull_rect);
673 contents->SetAlpha(paint.color.alpha);
674
675 Entity entity;
676 entity.SetTransformation(GetCurrentTransformation());
677 entity.SetStencilDepth(GetStencilDepth());
678 entity.SetBlendMode(paint.blend_mode);
679 entity.SetContents(paint.WithFilters(contents));
680
681 GetCurrentPass().AddEntity(entity);
682 }
683
684 } // namespace impeller
text_contents.h
impeller::Matrix::MakeSkew
static constexpr Matrix MakeSkew(Scalar sx, Scalar sy)
Definition: matrix.h:116
impeller::EntityPass::AddSubpassInline
void AddSubpassInline(std::unique_ptr< EntityPass > pass)
Merges a given pass into this pass. Useful for drawing pre-recorded pictures that don't require rende...
Definition: entity_pass.cc:245
impeller::BlendMode
BlendMode
Definition: color.h:57
impeller::Entity::ClipOperation::kIntersect
@ kIntersect
impeller::Canvas::EndRecordingAsPicture
Picture EndRecordingAsPicture()
Definition: canvas.cc:513
impeller::Picture::pass
std::unique_ptr< EntityPass > pass
Definition: picture.h:20
impeller::Entity::SetTransformation
void SetTransformation(const Matrix &transformation)
Definition: entity.cc:53
impeller::EntityPass::SetTransformation
void SetTransformation(Matrix xformation)
Definition: entity_pass.cc:1126
impeller::Paint::WithMaskBlur
std::shared_ptr< Contents > WithMaskBlur(std::shared_ptr< Contents > input, bool is_solid_color) const
Definition: paint.cc:83
impeller::TRect::size
TSize< Type > size
Definition: rect.h:24
impeller::EntityPass::BackdropFilterProc
std::function< std::shared_ptr< FilterContents >(FilterInput::Ref, const Matrix &effect_transform, Entity::RenderingMode rendering_mode)> BackdropFilterProc
Definition: entity_pass.h:44
impeller::TPoint::y
Type y
Definition: point.h:24
impeller::Entity::ClipOperation::kDifference
@ kDifference
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::Canvas::RestoreToCount
void RestoreToCount(size_t count)
Definition: canvas.cc:165
image_filter.h
impeller::Entity::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:97
texture_contents.h
impeller::Paint::Style::kStroke
@ kStroke
impeller::CanvasStackEntry
Definition: canvas.h:33
impeller::Paint
Definition: paint.h:25
impeller::CanvasStackEntry::cull_rect
std::optional< Rect > cull_rect
Definition: canvas.h:36
impeller::TRect< Scalar >::MakeXYWH
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:47
impeller::Canvas::Skew
void Skew(Scalar sx, Scalar sy)
Definition: canvas.cc:153
impeller::Paint::CreateContentsForEntity
std::shared_ptr< Contents > CreateContentsForEntity(const Path &path={}, bool cover=false) const
Definition: paint.cc:17
impeller::PointStyle
PointStyle
Definition: canvas.h:42
impeller::Paint::color
Color color
Definition: paint.h:54
paint_pass_delegate.h
impeller::EntityPass::AddSubpass
EntityPass * AddSubpass(std::unique_ptr< EntityPass > pass)
Appends a given pass as a subpass.
Definition: entity_pass.cc:226
impeller::Canvas::DrawTextFrame
void DrawTextFrame(const std::shared_ptr< TextFrame > &text_frame, Point position, const Paint &paint)
Definition: canvas.cc:549
impeller::PathBuilder
Definition: path_builder.h:13
impeller::PointStyle::kRound
@ kRound
Points are drawn as squares.
impeller::FilterInput::Ref
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:31
impeller::Canvas::ResetTransform
void ResetTransform()
Definition: canvas.cc:120
impeller::Canvas::DrawVertices
void DrawVertices(const std::shared_ptr< VerticesGeometry > &vertices, BlendMode blend_mode, const Paint &paint)
Definition: canvas.cc:592
impeller::Path::GetBoundingBox
std::optional< Rect > GetBoundingBox() const
Definition: path.cc:399
impeller::Geometry::MakeRect
static std::unique_ptr< Geometry > MakeRect(Rect rect)
Definition: geometry.cc:142
impeller::Color::alpha
Scalar alpha
Definition: color.h:141
impeller::Canvas::DebugOptions::offscreen_texture_checkerboard
bool offscreen_texture_checkerboard
Definition: canvas.h:57
impeller::PathBuilder::AddRoundedRect
PathBuilder & AddRoundedRect(Rect rect, RoundingRadii radii)
Definition: path_builder.cc:207
impeller::Canvas::debug_options
struct impeller::Canvas::DebugOptions debug_options
impeller::FilterContents::BlurStyle::kNormal
@ kNormal
Blurred inside and outside.
impeller::PathBuilder::SetConvexity
PathBuilder & SetConvexity(Convexity value)
Definition: path_builder.cc:111
impeller::Canvas::SaveLayer
void SaveLayer(const Paint &paint, std::optional< Rect > bounds=std::nullopt, const std::shared_ptr< ImageFilter > &backdrop_filter=nullptr)
Definition: canvas.cc:532
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:94
impeller::Paint::color_source
ColorSource color_source
Definition: paint.h:55
impeller::EntityPass::SetStencilDepth
void SetStencilDepth(size_t stencil_depth)
Definition: entity_pass.cc:1130
impeller::PathBuilder::AddRect
PathBuilder & AddRect(Rect rect)
Definition: path_builder.cc:181
impeller::UseColorSourceContents
static bool UseColorSourceContents(const std::shared_ptr< VerticesGeometry > &vertices, const Paint &paint)
Definition: canvas.cc:575
impeller::Canvas::DrawImage
void DrawImage(const std::shared_ptr< Image > &image, Point offset, const Paint &paint, SamplerDescriptor sampler={})
Definition: canvas.cc:467
impeller::Canvas::DrawPicture
void DrawPicture(const Picture &picture)
Definition: canvas.cc:437
impeller::Entity::SetContents
void SetContents(std::shared_ptr< Contents > contents)
Definition: entity.cc:77
vertices_contents.h
path_builder.h
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:18
impeller::Entity
Definition: entity.h:21
impeller::Picture
Definition: picture.h:19
impeller::TSize
Definition: size.h:18
impeller::Canvas::DrawImageRect
void DrawImageRect(const std::shared_ptr< Image > &image, Rect source, Rect dest, const Paint &paint, SamplerDescriptor sampler={})
Definition: canvas.cc:482
impeller::BlendMode::kSourceOver
@ kSourceOver
impeller::TRect::GetLeft
constexpr auto GetLeft() const
Definition: rect.h:137
impeller::Canvas::Scale
void Scale(const Vector2 &scale)
Definition: canvas.cc:145
impeller::EntityPass::GetSuperpass
EntityPass * GetSuperpass() const
Definition: entity_pass.cc:222
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:54
impeller::BlendMode::kDestination
@ kDestination
impeller::Canvas::Save
void Save()
Definition: canvas.cc:55
impeller::EntityPass
Definition: entity_pass.h:26
impeller::Paint::style
Style style
Definition: paint.h:62
impeller::Canvas::DrawCircle
void DrawCircle(Point center, Scalar radius, const Paint &paint)
Definition: canvas.cc:271
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:268
impeller::Paint::Style::kFill
@ kFill
impeller::Canvas::DrawRRect
void DrawRRect(Rect rect, Scalar corner_radius, const Paint &paint)
Definition: canvas.cc:249
geometry.h
impeller::Canvas::Restore
bool Restore()
Definition: canvas.cc:91
impeller::Radians
Definition: scalar.h:35
impeller::Canvas::DrawAtlas
void DrawAtlas(const std::shared_ptr< Image > &atlas, std::vector< Matrix > transforms, std::vector< Rect > texture_coordinates, std::vector< Color > colors, BlendMode blend_mode, SamplerDescriptor sampler, std::optional< Rect > cull_rect, const Paint &paint)
Definition: canvas.cc:653
canvas.h
clip_contents.h
impeller::Canvas::DrawPath
void DrawPath(const Path &path, const Paint &paint)
Definition: canvas.cc:173
impeller::PathBuilder::TakePath
Path TakePath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:21
impeller::Canvas::DrawPaint
void DrawPaint(const Paint &paint)
Definition: canvas.cc:183
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:306
impeller::Canvas::GetSaveCount
size_t GetSaveCount() const
Definition: canvas.cc:161
impeller::Canvas::PreConcat
void PreConcat(const Matrix &xformation)
Definition: canvas.cc:116
impeller::Canvas::Canvas
Canvas()
Definition: canvas.cc:25
impeller::TSize::width
Type width
Definition: size.h:21
impeller::Entity::RenderingMode::kSubpass
@ kSubpass
impeller::TPoint::x
Type x
Definition: point.h:23
impeller::Canvas::Transform
void Transform(const Matrix &xformation)
Definition: canvas.cc:124
atlas_contents.h
impeller::ColorSource::Type::kImage
@ kImage
impeller::Canvas::ClipRRect
void ClipRRect(const Rect &rect, Scalar corner_radius, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:315
impeller::TRect::GetRight
constexpr auto GetRight() const
Definition: rect.h:151
impeller::Matrix::MakeRotationZ
static Matrix MakeRotationZ(Radians r)
Definition: matrix.h:208
impeller::Canvas::Rotate
void Rotate(Radians radians)
Definition: canvas.cc:157
impeller::Geometry::MakePointField
static std::unique_ptr< Geometry > MakePointField(std::vector< Point > points, Scalar radius, bool round)
Definition: geometry.cc:119
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:52
impeller::Canvas::GetCurrentTransformation
const Matrix & GetCurrentTransformation() const
Definition: canvas.cc:128
impeller::TPoint< Scalar >
impeller::Geometry::MakeFillPath
static std::unique_ptr< Geometry > MakeFillPath(const Path &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:113
impeller::Paint::CreateContentsForGeometry
std::shared_ptr< Contents > CreateContentsForGeometry(std::shared_ptr< Geometry > geometry) const
Definition: paint.cc:34
impeller::Canvas::GetCurrentLocalCullingBounds
const std::optional< Rect > GetCurrentLocalCullingBounds() const
Definition: canvas.cc:132
impeller::Canvas::Concat
void Concat(const Matrix &xformation)
Definition: canvas.cc:112
impeller::Entity::ClipOperation
ClipOperation
Definition: entity.h:59
impeller::Canvas::DrawRect
void DrawRect(Rect rect, const Paint &paint)
Definition: canvas.cc:229
impeller::TRect::GetBottom
constexpr auto GetBottom() const
Definition: rect.h:158
impeller::Paint::HasColorFilter
bool HasColorFilter() const
Whether this paint has a color filter that can apply opacity.
Definition: paint.cc:211
impeller::Entity::SetStencilDepth
void SetStencilDepth(uint32_t stencil_depth)
Definition: entity.cc:85
impeller::PathBuilder::AddCircle
PathBuilder & AddCircle(const Point &center, Scalar radius)
Definition: path_builder.cc:198
impeller::Canvas::DrawPoints
void DrawPoints(std::vector< Point >, Scalar radius, const Paint &paint, PointStyle point_style)
Definition: canvas.cc:418
impeller::TSize::height
Type height
Definition: size.h:22
impeller::Canvas::ClipRect
void ClipRect(const Rect &rect, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:294
impeller::TRect< Scalar >::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:40
impeller::Entity::RenderingMode
RenderingMode
Definition: entity.h:26
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Definition: size.h:105
impeller::Convexity::kConvex
@ kConvex
impeller::TextureContents::MakeRect
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
A common case factory that marks the texture contents as having a destination rectangle....
Definition: texture_contents.cc:28
impeller
Definition: aiks_context.cc:10
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:103
impeller::Paint::mask_blur_descriptor
std::optional< MaskBlurDescriptor > mask_blur_descriptor
Definition: paint.h:68
solid_rrect_blur_contents.h
impeller::TRect::GetTop
constexpr auto GetTop() const
Definition: rect.h:144
impeller::ColorSource::Type::kColor
@ kColor
impeller::Paint::WithFilters
std::shared_ptr< Contents > WithFilters(std::shared_ptr< Contents > input) const
Wrap this paint's configured filters to the given contents.
Definition: paint.cc:59
impeller::ColorSource::GetType
Type GetType() const
Definition: color_source.cc:234
impeller::TRect
Definition: rect.h:20
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
impeller::Canvas::~Canvas
~Canvas()
impeller::Vector3
Definition: vector.h:17
impeller::Paint::blend_mode
BlendMode blend_mode
Definition: paint.h:63
impeller::TRect::Expand
constexpr TRect< T > Expand(T left, T top, T right, T bottom) const
Returns a rectangle with expanded edges. Negative expansion results in shrinking.
Definition: rect.h:279
impeller::EntityPass::AddEntity
void AddEntity(Entity entity)
Definition: entity_pass.cc:77
impeller::CanvasStackEntry::xformation
Matrix xformation
Definition: canvas.h:34
impeller::Canvas::Translate
void Translate(const Vector3 &offset)
Definition: canvas.cc:141
impeller::Canvas::ClipPath
void ClipPath(const Path &path, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:284