Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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
6
7#include <memory>
8#include <optional>
9#include <unordered_map>
10#include <utility>
11
19#include "flutter/fml/logging.h"
68
69namespace impeller {
70
71namespace {
72
73constexpr Scalar kAntialiasPadding = 1.0f;
74
75bool IsPipelineBlendOrMatrixFilter(const flutter::DlColorFilter* filter) {
76 return filter->type() == flutter::DlColorFilterType::kMatrix ||
79}
80
81static bool UseColorSourceContents(
82 const std::shared_ptr<VerticesGeometry>& vertices,
83 const Paint& paint) {
84 // If there are no vertex color or texture coordinates. Or if there
85 // are vertex coordinates but its just a color.
86 if (vertices->HasVertexColors()) {
87 return false;
88 }
89 if (vertices->HasTextureCoordinates() && !paint.color_source) {
90 return true;
91 }
92 return !vertices->HasTextureCoordinates();
93}
94
95static void SetClipScissor(std::optional<Rect> clip_coverage,
96 RenderPass& pass,
97 Point global_pass_position) {
98 // Set the scissor to the clip coverage area. We do this prior to rendering
99 // the clip itself and all its contents.
100 IRect32 scissor;
101 if (clip_coverage.has_value()) {
102 clip_coverage = clip_coverage->Shift(-global_pass_position);
103 scissor = IRect32::RoundOut(clip_coverage.value());
104 // The scissor rect must not exceed the size of the render target.
105 scissor =
106 scissor.Intersection(IRect32::MakeSize(pass.GetRenderTargetSize()))
107 .value_or(IRect32());
108 }
109 pass.SetScissor(scissor);
110}
111
112static void ApplyFramebufferBlend(Entity& entity) {
113 auto src_contents = entity.GetContents();
114 auto contents = std::make_shared<FramebufferBlendContents>();
115 contents->SetChildContents(src_contents);
116 contents->SetBlendMode(entity.GetBlendMode());
117 entity.SetContents(std::move(contents));
118 entity.SetBlendMode(BlendMode::kSrc);
119}
120
121/// @brief Create the subpass restore contents, appling any filters or opacity
122/// from the provided paint object.
123static std::shared_ptr<Contents> CreateContentsForSubpassTarget(
124 const ContentContext& renderer,
125 const Paint& paint,
126 const std::shared_ptr<Texture>& target,
127 const Matrix& effect_transform) {
128 auto contents = TextureContents::MakeRect(Rect::MakeSize(target->GetSize()));
129 contents->SetTexture(target);
130 contents->SetLabel("Subpass");
131 contents->SetSourceRect(Rect::MakeSize(target->GetSize()));
132 contents->SetOpacity(paint.color.alpha);
133 contents->SetDeferApplyingOpacity(true);
134
135 return paint.WithFiltersForSubpassTarget(renderer, std::move(contents),
136 effect_transform);
137}
138
139static const constexpr RenderTarget::AttachmentConfig kDefaultStencilConfig =
140 RenderTarget::AttachmentConfig{
141 .storage_mode = StorageMode::kDeviceTransient,
142 .load_action = LoadAction::kDontCare,
143 .store_action = StoreAction::kDontCare,
144 };
145
146static std::unique_ptr<EntityPassTarget> CreateRenderTarget(
147 ContentContext& renderer,
148 ISize size,
149 const Color& clear_color) {
150 const std::shared_ptr<Context>& context = renderer.GetContext();
151
152 /// All of the load/store actions are managed by `InlinePassContext` when
153 /// `RenderPasses` are created, so we just set them to `kDontCare` here.
154 /// What's important is the `StorageMode` of the textures, which cannot be
155 /// changed for the lifetime of the textures.
156
157 RenderTarget target;
158 if (context->GetCapabilities()->SupportsOffscreenMSAA()) {
159 target = renderer.GetRenderTargetCache()->CreateOffscreenMSAA(
160 /*context=*/*context,
161 /*size=*/size,
162 /*mip_count=*/1,
163 /*label=*/"EntityPass",
164 /*color_attachment_config=*/
165 RenderTarget::AttachmentConfigMSAA{
166 .storage_mode = StorageMode::kDeviceTransient,
167 .resolve_storage_mode = StorageMode::kDevicePrivate,
168 .load_action = LoadAction::kDontCare,
169 .store_action = StoreAction::kMultisampleResolve,
170 .clear_color = clear_color},
171 /*stencil_attachment_config=*/kDefaultStencilConfig);
172 } else {
173 target = renderer.GetRenderTargetCache()->CreateOffscreen(
174 *context, // context
175 size, // size
176 /*mip_count=*/1,
177 "EntityPass", // label
178 RenderTarget::AttachmentConfig{
179 .storage_mode = StorageMode::kDevicePrivate,
180 .load_action = LoadAction::kDontCare,
181 .store_action = StoreAction::kDontCare,
182 .clear_color = clear_color,
183 }, // color_attachment_config
184 kDefaultStencilConfig //
185 );
186 }
187
188 return std::make_unique<EntityPassTarget>(
189 target, //
190 renderer.GetDeviceCapabilities().SupportsReadFromResolve(), //
191 renderer.GetDeviceCapabilities().SupportsImplicitResolvingMSAA() //
192 );
193}
194
195} // namespace
196
197class Canvas::RRectBlurShape : public BlurShape {
198 public:
199 RRectBlurShape(const Rect& rect, Scalar corner_radius)
200 : rect_(rect), corner_radius_(corner_radius) {}
201
202 Rect GetBounds() const override { return rect_; }
203
204 std::shared_ptr<SolidBlurContents> BuildBlurContent(Sigma sigma) override {
205 auto contents = std::make_shared<SolidRRectBlurContents>();
206 contents->SetSigma(sigma);
207 contents->SetShape(rect_, corner_radius_);
208 return contents;
209 }
210
211 const Geometry& BuildDrawGeometry() override {
212 return geom_.emplace(rect_, Size(corner_radius_));
213 }
214
215 private:
216 const Rect rect_;
217 const Scalar corner_radius_;
218
219 std::optional<RoundRectGeometry> geom_; // optional stack allocation
220};
221
222class Canvas::RSuperellipseBlurShape : public BlurShape {
223 public:
224 RSuperellipseBlurShape(const Rect& rect, Scalar corner_radius)
225 : rect_(rect), corner_radius_(corner_radius) {}
226
227 Rect GetBounds() const override { return rect_; }
228
229 std::shared_ptr<SolidBlurContents> BuildBlurContent(Sigma sigma) override {
230 auto contents = std::make_shared<SolidRSuperellipseBlurContents>();
231 contents->SetSigma(sigma);
232 contents->SetShape(rect_, corner_radius_);
233 return contents;
234 }
235
236 const Geometry& BuildDrawGeometry() override {
237 return geom_.emplace(rect_, corner_radius_);
238 }
239
240 private:
241 const Rect rect_;
242 const Scalar corner_radius_;
243
244 std::optional<RoundSuperellipseGeometry> geom_; // optional stack allocation
245};
246
247class Canvas::PathBlurShape : public BlurShape {
248 public:
249 /// Construct a PathBlurShape from a path source, a set of shadow vertices
250 /// (typically produced by ShadowPathGeometry) and the sigma that was used
251 /// to generate the vertex mesh.
252 ///
253 /// The sigma was already used to generate the shadow vertices, so it is
254 /// provided here only to make sure it matches the sigma we will see in
255 /// our BuildBlurContent method.
256 ///
257 /// The source was used to generate the mesh and it might be used again
258 /// for the SOLID mask operation so we save it here in case the mask
259 /// rendering code calls our BuildDrawGeometry method. Its lifetime
260 /// must survive the lifetime of this object, typically because the
261 /// source object was stack allocated not long before this object is
262 /// also being stack allocated.
263 PathBlurShape(const PathSource& source [[clang::lifetimebound]],
264 std::shared_ptr<ShadowVertices> shadow_vertices,
265 Sigma sigma)
266 : sigma_(sigma),
267 source_(source),
268 shadow_vertices_(std::move(shadow_vertices)) {}
269
270 Rect GetBounds() const override {
271 return shadow_vertices_->GetBounds().value_or(Rect());
272 }
273
274 std::shared_ptr<SolidBlurContents> BuildBlurContent(Sigma sigma) override {
275 // We have to use the sigma to generate the mesh up front in order to
276 // even know if we can perform the operation, but then the method that
277 // actually uses our contents informs us of the sigma, but it's too
278 // late to make use of it. Instead we remember what sigma we used and
279 // make sure they match.
280 FML_DCHECK(sigma_.sigma == sigma.sigma);
281 return ShadowVerticesContents::Make(shadow_vertices_);
282 }
283
284 const Geometry& BuildDrawGeometry() override {
285 return source_geometry_.emplace(source_);
286 }
287
288 private:
289 const Sigma sigma_;
290 const PathSource& source_;
291 const std::shared_ptr<ShadowVertices> shadow_vertices_;
292
293 // optional stack allocation - for BuildGeometry
294 std::optional<FillPathFromSourceGeometry> source_geometry_;
295};
296
298 const RenderTarget& render_target,
299 bool is_onscreen,
300 bool requires_readback)
301 : renderer_(renderer),
302 render_target_(render_target),
303 is_onscreen_(is_onscreen),
304 requires_readback_(requires_readback),
305 clip_coverage_stack_(EntityPassClipStack(
306 Rect::MakeSize(render_target.GetRenderTargetSize()))) {
307 Initialize(std::nullopt);
308 SetupRenderPass();
309}
310
312 const RenderTarget& render_target,
313 bool is_onscreen,
314 bool requires_readback,
315 Rect cull_rect)
316 : renderer_(renderer),
317 render_target_(render_target),
318 is_onscreen_(is_onscreen),
319 requires_readback_(requires_readback),
320 clip_coverage_stack_(EntityPassClipStack(
321 Rect::MakeSize(render_target.GetRenderTargetSize()))) {
322 Initialize(cull_rect);
323 SetupRenderPass();
324}
325
327 const RenderTarget& render_target,
328 bool is_onscreen,
329 bool requires_readback,
330 IRect32 cull_rect)
331 : renderer_(renderer),
332 render_target_(render_target),
333 is_onscreen_(is_onscreen),
334 requires_readback_(requires_readback),
335 clip_coverage_stack_(EntityPassClipStack(
336 Rect::MakeSize(render_target.GetRenderTargetSize()))) {
337 Initialize(Rect::MakeLTRB(cull_rect.GetLeft(), cull_rect.GetTop(),
338 cull_rect.GetRight(), cull_rect.GetBottom()));
339 SetupRenderPass();
340}
341
342void Canvas::Initialize(std::optional<Rect> cull_rect) {
343 initial_cull_rect_ = cull_rect;
344 transform_stack_.emplace_back(CanvasStackEntry{
346 });
347 FML_DCHECK(GetSaveCount() == 1u);
348}
349
350void Canvas::Reset() {
351 current_depth_ = 0u;
352 transform_stack_ = {};
353}
354
356 transform_stack_.back().transform = GetCurrentTransform() * transform;
357}
358
360 transform_stack_.back().transform = transform * GetCurrentTransform();
361}
362
364 transform_stack_.back().transform = {};
365}
366
370
372 return transform_stack_.back().transform;
373}
374
375void Canvas::Translate(const Vector3& offset) {
377}
378
379void Canvas::Scale(const Vector2& scale) {
381}
382
383void Canvas::Scale(const Vector3& scale) {
385}
386
388 Concat(Matrix::MakeSkew(sx, sy));
389}
390
391void Canvas::Rotate(Radians radians) {
393}
394
395Point Canvas::GetGlobalPassPosition() const {
396 if (save_layer_state_.empty()) {
397 return Point(0, 0);
398 }
399 return save_layer_state_.back().coverage.GetOrigin();
400}
401
402// clip depth of the previous save or 0.
403size_t Canvas::GetClipHeightFloor() const {
404 if (transform_stack_.size() > 1) {
405 return transform_stack_[transform_stack_.size() - 2].clip_height;
406 }
407 return 0;
408}
409
410size_t Canvas::GetSaveCount() const {
411 return transform_stack_.size();
412}
413
414bool Canvas::IsSkipping() const {
415 return transform_stack_.back().skipping;
416}
417
418void Canvas::RestoreToCount(size_t count) {
419 while (GetSaveCount() > count) {
420 if (!Restore()) {
421 return;
422 }
423 }
424}
425
426void Canvas::DrawPath(const flutter::DlPath& path, const Paint& paint) {
427 if (IsShadowBlurDrawOperation(paint)) {
428 if (AttemptDrawBlurredPathSource(path, paint)) {
429 return;
430 }
431 }
432
433 Entity entity;
435 entity.SetBlendMode(paint.blend_mode);
436
437 if (paint.style == Paint::Style::kFill) {
438 FillPathGeometry geom(path);
439 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
440 } else {
441 StrokePathGeometry geom(path, paint.stroke);
442 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
443 }
444}
445
446void Canvas::DrawPaint(const Paint& paint) {
447 Entity entity;
449 entity.SetBlendMode(paint.blend_mode);
450
451 CoverGeometry geom;
452 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
453}
454
455// Optimization: if the texture has a color filter that is a simple
456// porter-duff blend or matrix filter, then instead of performing a save layer
457// we should swap out the shader for the porter duff blend shader and avoid a
458// saveLayer. This optimization is important for Flame.
459bool Canvas::AttemptColorFilterOptimization(
460 const std::shared_ptr<Texture>& image,
461 Rect source,
462 Rect dest,
463 const Paint& paint,
464 const SamplerDescriptor& sampler,
465 SourceRectConstraint src_rect_constraint) {
466 if (!paint.color_filter || //
467 paint.image_filter != nullptr || //
468 paint.invert_colors || //
469 paint.mask_blur_descriptor.has_value() || //
470 !IsPipelineBlendOrMatrixFilter(paint.color_filter)) {
471 return false;
472 }
473
475 const flutter::DlBlendColorFilter* blend_filter =
476 paint.color_filter->asBlend();
477 DrawImageRectAtlasGeometry geometry = DrawImageRectAtlasGeometry(
478 /*texture=*/image,
479 /*source=*/source,
480 /*destination=*/dest,
481 /*color=*/skia_conversions::ToColor(blend_filter->color()),
482 /*blend_mode=*/blend_filter->mode(),
483 /*desc=*/sampler,
484 /*use_strict_src_rect=*/src_rect_constraint ==
486
487 auto atlas_contents = std::make_shared<AtlasContents>();
488 atlas_contents->SetGeometry(&geometry);
489 atlas_contents->SetAlpha(paint.color.alpha);
490
491 Entity entity;
492 entity.SetTransform(GetCurrentTransform());
493 entity.SetBlendMode(paint.blend_mode);
494 entity.SetContents(atlas_contents);
495
496 AddRenderEntityToCurrentPass(entity);
497 } else {
498 // src_rect_constraint is only supported in the porter-duff mode
499 // for now.
500 if (src_rect_constraint == SourceRectConstraint::kStrict) {
501 return false;
502 }
503
504 const flutter::DlMatrixColorFilter* matrix_filter =
505 paint.color_filter->asMatrix();
506
507 DrawImageRectAtlasGeometry geometry = DrawImageRectAtlasGeometry(
508 /*texture=*/image,
509 /*source=*/source,
510 /*destination=*/dest,
511 /*color=*/Color::Khaki(), // ignored
512 /*blend_mode=*/BlendMode::kSrcOver, // ignored
513 /*desc=*/sampler,
514 /*use_strict_src_rect=*/src_rect_constraint ==
516
517 auto atlas_contents = std::make_shared<ColorFilterAtlasContents>();
518 atlas_contents->SetGeometry(&geometry);
519 atlas_contents->SetAlpha(paint.color.alpha);
520 impeller::ColorMatrix color_matrix;
521 matrix_filter->get_matrix(color_matrix.array);
522 atlas_contents->SetMatrix(color_matrix);
523
524 Entity entity;
525 entity.SetTransform(GetCurrentTransform());
526 entity.SetBlendMode(paint.blend_mode);
527 entity.SetContents(atlas_contents);
528
529 AddRenderEntityToCurrentPass(entity);
530 }
531 return true;
532}
533
534bool Canvas::AttemptDrawAntialiasedCircle(const Point& center,
535 Scalar radius,
536 const Paint& paint) {
537 if (paint.HasColorFilter() || paint.image_filter || paint.invert_colors ||
538 paint.color_source || paint.mask_blur_descriptor.has_value()) {
539 return false;
540 }
541
542 Entity entity;
543 entity.SetTransform(GetCurrentTransform());
544 entity.SetBlendMode(paint.blend_mode);
545
546 const bool is_stroked = paint.style == Paint::Style::kStroke;
547 std::unique_ptr<CircleGeometry> geom;
548 if (is_stroked) {
549 geom = std::make_unique<CircleGeometry>(center, radius, paint.stroke.width);
550 } else {
551 geom = std::make_unique<CircleGeometry>(center, radius);
552 }
553 geom->SetAntialiasPadding(kAntialiasPadding);
554
555 auto contents =
556 CircleContents::Make(std::move(geom), paint.color, is_stroked);
557 entity.SetContents(std::move(contents));
558 AddRenderEntityToCurrentPass(entity);
559
560 return true;
561}
562
563bool Canvas::IsShadowBlurDrawOperation(const Paint& paint) {
564 if (paint.style != Paint::Style::kFill) {
565 return false;
566 }
567
568 if (paint.color_source) {
569 return false;
570 }
571
572 if (!paint.mask_blur_descriptor.has_value()) {
573 return false;
574 }
575
576 // A blur sigma that is not positive enough should not result in a blur.
577 // We test both the sigma value and the converted radius value as the
578 // algorithms might use either and either indicates the blur is too small
579 // to be noticeable.
580 if (paint.mask_blur_descriptor->sigma.sigma <= kEhCloseEnough) {
581 return false;
582 }
583 Radius radius = paint.mask_blur_descriptor->sigma;
584 if (radius.radius <= kEhCloseEnough) {
585 return false;
586 }
587
588 return true;
589}
590
591bool Canvas::AttemptDrawBlurredPathSource(const PathSource& source,
592 const Paint& paint) {
593 FML_DCHECK(IsShadowBlurDrawOperation);
594
595 // This has_value() test should always succeed as it is checked by the
596 // IsShadowBlurDrawOperation method which should have been called before
597 // this method, but we check again here to avoid warnings from the
598 // following code.
599 if (paint.mask_blur_descriptor.has_value()) {
600 // This value was determined by empirical eyesight tests so that the
601 // shadow mesh results will match the results of the shape-specific
602 // optimized shadow shaders.
603 static constexpr Scalar kSigmaScale = 2.8f;
604
605 Sigma sigma = paint.mask_blur_descriptor->sigma;
606 const Matrix& matrix = GetCurrentTransform();
607 Scalar basis_scale = matrix.GetMaxBasisLengthXY();
608 Scalar device_radius = sigma.sigma * kSigmaScale * basis_scale;
609 std::shared_ptr<ShadowVertices> shadow_vertices =
611 renderer_.GetTessellator(), source, device_radius, matrix);
612 if (shadow_vertices) {
613 PathBlurShape shape(source, std::move(shadow_vertices), sigma);
614 return AttemptDrawBlur(shape, paint);
615 }
616 }
617 return false;
618}
619
620Scalar Canvas::GetCommonRRectLikeRadius(const RoundingRadii& radii) {
621 if (!radii.AreAllCornersSame()) {
622 return -1;
623 }
624 const Size& corner_radii = radii.top_left;
625 if (ScalarNearlyEqual(corner_radii.width, corner_radii.height)) {
626 return corner_radii.width;
627 }
628 return -1;
629}
630
631bool Canvas::AttemptDrawBlurredRRect(const RoundRect& round_rect,
632 const Paint& paint) {
633 Scalar radius = GetCommonRRectLikeRadius(round_rect.GetRadii());
634 if (radius < 0) {
635 RoundRectPathSource source(round_rect);
636 return AttemptDrawBlurredPathSource(source, paint);
637 }
638 RRectBlurShape shape(round_rect.GetBounds(), radius);
639 return AttemptDrawBlur(shape, paint);
640}
641
642bool Canvas::AttemptDrawBlurredRSuperellipse(const RoundSuperellipse& rse,
643 const Paint& paint) {
644 Scalar radius = GetCommonRRectLikeRadius(rse.GetRadii());
645 if (radius < 0) {
646 RoundSuperellipsePathSource source(rse);
647 return AttemptDrawBlurredPathSource(source, paint);
648 }
649 RSuperellipseBlurShape shape(rse.GetBounds(), radius);
650 return AttemptDrawBlur(shape, paint);
651}
652
653bool Canvas::AttemptDrawBlur(BlurShape& shape, const Paint& paint) {
654 FML_DCHECK(IsShadowBlurDrawOperation(paint));
655
656 // For symmetrically mask blurred solid RRects, absorb the mask blur and use
657 // a faster SDF approximation.
658 Color rrect_color = paint.color;
659 if (paint.invert_colors) {
660 rrect_color = rrect_color.ApplyColorMatrix(kColorInversion);
661 }
662 if (paint.color_filter) {
663 rrect_color = GetCPUColorFilterProc(paint.color_filter)(rrect_color);
664 }
665
666 Paint rrect_paint = {.mask_blur_descriptor = paint.mask_blur_descriptor};
667
668 if (!rrect_paint.mask_blur_descriptor.has_value()) {
669 // This should never happen in practice because the caller would have
670 // first called |IsShadowBlurDrawOperation| on the paint object, but
671 // we test anyway to make the compiler happy about the dereferences
672 // below.
673 return false;
674 }
675
676 // In some cases, we need to render the mask blur to a separate layer.
677 //
678 // 1. If the blur style is normal, we'll be drawing using one draw call and
679 // no clips. And so we can just wrap the RRect contents with the
680 // ImageFilter, which will get applied to the result as per usual.
681 //
682 // 2. If the blur style is solid, we combine the non-blurred RRect with the
683 // blurred RRect via two separate draw calls, and so we need to defer any
684 // fancy blending, translucency, or image filtering until after these two
685 // draws have been combined in a separate layer.
686 //
687 // 3. If the blur style is outer or inner, we apply the blur style via a
688 // clip. The ImageFilter needs to be applied to the mask blurred result.
689 // And so if there's an ImageFilter, we need to defer applying it until
690 // after the clipped RRect blur has been drawn to a separate texture.
691 // However, since there's only one draw call that produces color, we
692 // don't need to worry about the blend mode or translucency (unlike with
693 // BlurStyle::kSolid).
694 //
695 if ((paint.mask_blur_descriptor->style !=
697 paint.image_filter) ||
698 (paint.mask_blur_descriptor->style == FilterContents::BlurStyle::kSolid &&
699 (!rrect_color.IsOpaque() || paint.blend_mode != BlendMode::kSrcOver))) {
700 Rect render_bounds = shape.GetBounds();
701 if (paint.mask_blur_descriptor->style !=
703 render_bounds =
704 render_bounds.Expand(paint.mask_blur_descriptor->sigma.sigma * 4.0);
705 }
706 // Defer the alpha, blend mode, and image filter to a separate layer.
707 SaveLayer(
708 Paint{
709 .color = Color::White().WithAlpha(rrect_color.alpha),
710 .image_filter = paint.image_filter,
711 .blend_mode = paint.blend_mode,
712 },
713 render_bounds, nullptr, ContentBoundsPromise::kContainsContents, 1u);
714 rrect_paint.color = rrect_color.WithAlpha(1);
715 } else {
716 rrect_paint.color = rrect_color;
717 rrect_paint.blend_mode = paint.blend_mode;
718 rrect_paint.image_filter = paint.image_filter;
719 Save(1u);
720 }
721
722 auto draw_blurred_rrect = [this, &rrect_paint, &shape]() {
723 std::shared_ptr<SolidBlurContents> contents =
724 shape.BuildBlurContent(rrect_paint.mask_blur_descriptor->sigma);
725 FML_DCHECK(contents);
726
727 contents->SetColor(rrect_paint.color);
728
729 Entity blurred_rrect_entity;
730 blurred_rrect_entity.SetTransform(GetCurrentTransform());
731 blurred_rrect_entity.SetBlendMode(rrect_paint.blend_mode);
732
733 rrect_paint.mask_blur_descriptor = std::nullopt;
734 blurred_rrect_entity.SetContents(
735 rrect_paint.WithFilters(renderer_, std::move(contents)));
736 AddRenderEntityToCurrentPass(blurred_rrect_entity);
737 };
738
739 switch (rrect_paint.mask_blur_descriptor->style) {
741 draw_blurred_rrect();
742 break;
743 }
745 // First, draw the blurred RRect.
746 draw_blurred_rrect();
747 // Then, draw the non-blurred RRect on top.
748 Entity entity;
749 entity.SetTransform(GetCurrentTransform());
750 entity.SetBlendMode(rrect_paint.blend_mode);
751
752 const Geometry& geom = shape.BuildDrawGeometry();
753 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, rrect_paint,
754 /*reuse_depth=*/true);
755 break;
756 }
758 const Geometry& geom = shape.BuildDrawGeometry();
760 draw_blurred_rrect();
761 break;
762 }
764 const Geometry& geom = shape.BuildDrawGeometry();
766 draw_blurred_rrect();
767 break;
768 }
769 }
770
771 Restore();
772
773 return true;
774}
775
776void Canvas::DrawLine(const Point& p0,
777 const Point& p1,
778 const Paint& paint,
779 bool reuse_depth) {
780 Entity entity;
782 entity.SetBlendMode(paint.blend_mode);
783
784 auto geometry = std::make_unique<LineGeometry>(p0, p1, paint.stroke);
785
786 if ((renderer_.GetContext()->GetFlags().antialiased_lines ||
787 renderer_.GetContext()->GetFlags().use_sdfs) &&
788 !paint.color_filter && !paint.invert_colors && !paint.image_filter &&
789 !paint.mask_blur_descriptor.has_value() && !paint.color_source) {
790 auto contents = LineContents::Make(std::move(geometry), paint.color);
791 entity.SetContents(std::move(contents));
792 AddRenderEntityToCurrentPass(entity, reuse_depth);
793 } else {
794 AddRenderEntityWithFiltersToCurrentPass(entity, geometry.get(), paint,
795 /*reuse_depth=*/reuse_depth);
796 }
797}
798
800 const Point& p1,
801 Scalar on_length,
802 Scalar off_length,
803 const Paint& paint) {
804 // Reasons to defer to regular DrawLine:
805 // - performance for degenerate and "regular line" cases
806 // - length is non-positive - DrawLine will draw appropriate "dot"
807 // - off_length is non-positive - no gaps, DrawLine will draw it solid
808 // - on_length is negative - invalid dashing
809 //
810 // Note that a 0 length "on" dash will draw "dot"s every "off" distance
811 // apart so we proceed with the dashing process in that case.
813 if (length > 0.0f && on_length >= 0.0f && off_length > 0.0f) {
814 Entity entity;
816 entity.SetBlendMode(paint.blend_mode);
817
818 StrokeDashedLineGeometry geom(p0, p1, on_length, off_length, paint.stroke);
819 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
820 } else {
821 DrawLine(p0, p1, paint);
822 }
823}
824
825void Canvas::DrawRect(const Rect& rect, const Paint& paint) {
826 if (IsShadowBlurDrawOperation(paint)) {
827 RRectBlurShape shape(rect, 0.0f);
828 if (AttemptDrawBlur(shape, paint)) {
829 return;
830 }
831 }
832
833 if (renderer_.GetContext()->GetFlags().use_sdfs &&
836 /*color=*/paint.color,
837 /*rect=*/rect,
838 /*stroke=*/paint.GetStroke());
839 AddRenderSDFEntityToCurrentPass(paint, params);
840 return;
841 }
842
843 Entity entity;
845 entity.SetBlendMode(paint.blend_mode);
846
847 if (paint.style == Paint::Style::kStroke) {
848 StrokeRectGeometry geom(rect, paint.stroke);
849 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
850 } else {
851 FillRectGeometry geom(rect);
852 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
853 }
854}
855
856void Canvas::DrawOval(const Rect& rect, const Paint& paint) {
857 // TODO(jonahwilliams): This additional condition avoids an assert in the
858 // stroke circle geometry generator. I need to verify the condition that this
859 // assert prevents.
860 if (rect.IsSquare() && (paint.style == Paint::Style::kFill ||
861 (paint.style == Paint::Style::kStroke &&
862 paint.stroke.width < rect.GetWidth()))) {
863 // Circles have slightly less overhead and can do stroking
864 DrawCircle(rect.GetCenter(), rect.GetWidth() * 0.5f, paint);
865 return;
866 }
867
868 if (IsShadowBlurDrawOperation(paint)) {
869 if (rect.IsSquare()) {
870 // RRectBlurShape takes the corner radii which are half of the
871 // overall width and height of the DrawOval bounds rect.
872 RRectBlurShape shape(rect, rect.GetWidth() * 0.5f);
873 if (AttemptDrawBlur(shape, paint)) {
874 return;
875 }
876 } else {
877 EllipsePathSource source(rect);
878 if (AttemptDrawBlurredPathSource(source, paint)) {
879 return;
880 }
881 }
882 }
883
884 Entity entity;
886 entity.SetBlendMode(paint.blend_mode);
887
888 if (renderer_.GetContext()->GetFlags().use_sdfs &&
891
892 if (paint.style == Paint::Style::kStroke) {
893 params = UberSDFParameters::MakeOval(paint.color, rect, paint.stroke);
894 } else {
895 params = UberSDFParameters::MakeOval(paint.color, rect, std::nullopt);
896 }
897
898 AddRenderSDFEntityToCurrentPass(paint, params);
899 return;
900 }
901
902 if (paint.style == Paint::Style::kStroke) {
903 StrokeEllipseGeometry geom(rect, paint.stroke);
904 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
905 } else {
906 EllipseGeometry geom(rect);
907 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
908 }
909}
910
911void Canvas::DrawArc(const Arc& arc, const Paint& paint) {
912 Entity entity;
914 entity.SetBlendMode(paint.blend_mode);
915
916 if (paint.style == Paint::Style::kFill) {
917 ArcGeometry geom(arc);
918 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
919 return;
920 }
921
922 const Rect& oval_bounds = arc.GetOvalBounds();
923 if (paint.stroke.width > oval_bounds.GetSize().MaxDimension()) {
924 // This is a special case for rendering arcs whose stroke width is so large
925 // you are effectively drawing a sector of a circle.
926 // https://github.com/flutter/flutter/issues/158567
927 Arc expanded_arc(oval_bounds.Expand(Size(paint.stroke.width * 0.5f)),
928 arc.GetStart(), arc.GetSweep(), true);
929
930 ArcGeometry geom(expanded_arc);
931 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
932 return;
933 }
934
935 // IncludeCenter incurs lots of extra work for stroking an arc, including:
936 // - It introduces segments to/from the center point (not too hard).
937 // - It introduces joins on those segments (a bit more complicated).
938 // - Even if the sweep is >=360 degrees, we still draw the segment to
939 // the center and it basically looks like a pie cut into the complete
940 // boundary circle, as if the slice were cut, but not extracted
941 // (hard to express as a continuous kTriangleStrip).
942 if (!arc.IncludeCenter()) {
943 if (arc.IsFullCircle()) {
944 return DrawOval(oval_bounds, paint);
945 }
946
947 // Our fast stroking code only works for circular bounds as it assumes
948 // that the inner and outer radii can be scaled along each angular step
949 // of the arc - which is not true for elliptical arcs where the inner
950 // and outer samples are perpendicular to the traveling direction of the
951 // elliptical curve which may not line up with the center of the bounds.
952 if (oval_bounds.IsSquare()) {
953 ArcGeometry geom(arc, paint.stroke);
954 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
955 return;
956 }
957 }
958
959 ArcStrokeGeometry geom(arc, paint.stroke);
960 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
961}
962
963void Canvas::DrawRoundRect(const RoundRect& round_rect, const Paint& paint) {
964 if (IsShadowBlurDrawOperation(paint)) {
965 if (AttemptDrawBlurredRRect(round_rect, paint)) {
966 return;
967 }
968 }
969
970 const RoundingRadii& radii = round_rect.GetRadii();
971
972 if (renderer_.GetContext()->GetFlags().use_sdfs &&
975 /*color=*/paint.color,
976 /*rect=*/round_rect.GetBounds(),
977 /*radii=*/radii,
978 /*stroke=*/paint.style == Paint::Style::kStroke
979 ? std::make_optional(paint.stroke)
980 : std::nullopt);
981 AddRenderSDFEntityToCurrentPass(paint, params);
982 return;
983 }
984
985 if (round_rect.GetRadii().AreAllCornersSame() &&
986 paint.style == Paint::Style::kFill) {
987 Entity entity;
989 entity.SetBlendMode(paint.blend_mode);
990
991 RoundRectGeometry geom(round_rect.GetBounds(),
992 round_rect.GetRadii().top_left);
993 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
994 return;
995 }
996
997 Entity entity;
999 entity.SetBlendMode(paint.blend_mode);
1000
1001 if (paint.style == Paint::Style::kFill) {
1002 FillRoundRectGeometry geom(round_rect);
1003 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
1004 } else {
1005 StrokeRoundRectGeometry geom(round_rect, paint.stroke);
1006 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
1007 }
1008}
1009
1011 const RoundRect& inner,
1012 const Paint& paint) {
1013 Entity entity;
1015 entity.SetBlendMode(paint.blend_mode);
1016
1017 if (paint.style == Paint::Style::kFill) {
1018 FillDiffRoundRectGeometry geom(outer, inner);
1019 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
1020 } else {
1021 StrokeDiffRoundRectGeometry geom(outer, inner, paint.stroke);
1022 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
1023 }
1024}
1025
1027 const Paint& paint) {
1028 if (IsShadowBlurDrawOperation(paint)) {
1029 if (AttemptDrawBlurredRSuperellipse(round_superellipse, paint)) {
1030 return;
1031 }
1032 }
1033
1034 Entity entity;
1036 entity.SetBlendMode(paint.blend_mode);
1037
1038 if (renderer_.GetContext()->GetFlags().use_sdfs &&
1040 round_superellipse.GetRadii().AreAllCornersSame()) {
1041 auto round_superellipse_params = RoundSuperellipseParam::MakeBoundsRadii(
1042 round_superellipse.GetBounds(), round_superellipse.GetRadii());
1043
1045 round_superellipse_params.top_right.top;
1046 RoundSuperellipseParam::Octant octant_right =
1047 round_superellipse_params.top_right.right;
1048
1049 auto adjusted_radii = RoundingRadii::MakeRadii(
1050 Size(octant_top.circle_radius, octant_right.circle_radius));
1051
1053 /*color=*/paint.color,
1054 /*bounds=*/round_superellipse.GetBounds(),
1055 /*superellipse_degree=*/Point(octant_top.se_n, octant_right.se_n),
1056 /*superellipse_a=*/Point(octant_top.se_a, octant_right.se_a),
1057 /*radii=*/adjusted_radii,
1058 /*corner_angle_span=*/
1059 Point(octant_top.circle_max_angle.radians,
1060 octant_right.circle_max_angle.radians),
1061 /*corner_circle_center_top=*/octant_top.circle_center,
1062 /*corner_circle_center_right=*/octant_right.circle_center,
1063 /*superellipse_c=*/octant_top.se_a - octant_right.se_a,
1064 /*superellipse_scale=*/
1065 Point(round_superellipse_params.top_right.signed_scale.Abs()),
1066 /*stroke=*/paint.GetStroke());
1067
1068 AddRenderSDFEntityToCurrentPass(paint, params);
1069
1070 return;
1071 }
1072
1073 if (paint.style == Paint::Style::kFill) {
1074 RoundSuperellipseGeometry geom(round_superellipse.GetBounds(),
1075 round_superellipse.GetRadii());
1076 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
1077 } else {
1078 StrokeRoundSuperellipseGeometry geom(round_superellipse, paint.stroke);
1079 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
1080 }
1081}
1082
1083void Canvas::DrawCircle(const Point& center,
1084 Scalar radius,
1085 const Paint& paint) {
1086 if (IsShadowBlurDrawOperation(paint)) {
1087 Rect bounds = Rect::MakeCircleBounds(center, radius);
1088 RRectBlurShape shape(bounds, radius);
1089 if (AttemptDrawBlur(shape, paint)) {
1090 return;
1091 }
1092 }
1093
1094 if (renderer_.GetContext()->GetFlags().use_sdfs &&
1097 /*color=*/paint.color, /*center=*/center, /*radius=*/radius,
1098 /*stroke=*/paint.GetStroke());
1099 AddRenderSDFEntityToCurrentPass(paint, params);
1100 return;
1101 }
1102
1103 if (AttemptDrawAntialiasedCircle(center, radius, paint)) {
1104 return;
1105 }
1106
1107 Entity entity;
1109 entity.SetBlendMode(paint.blend_mode);
1110
1111 if (paint.style == Paint::Style::kStroke) {
1112 CircleGeometry geom(center, radius, paint.stroke.width);
1113 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
1114 } else {
1115 CircleGeometry geom(center, radius);
1116 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
1117 }
1118}
1119
1120void Canvas::ClipGeometry(const Geometry& geometry,
1121 Entity::ClipOperation clip_op,
1122 bool is_aa) {
1123 if (IsSkipping()) {
1124 return;
1125 }
1126
1127 // Ideally the clip depth would be greater than the current rendering
1128 // depth because any rendering calls that follow this clip operation will
1129 // pre-increment the depth and then be rendering above our clip depth,
1130 // but that case will be caught by the CHECK in AddRenderEntity above.
1131 // In practice we sometimes have a clip set with no rendering after it
1132 // and in such cases the current depth will equal the clip depth.
1133 // Eventually the DisplayList should optimize these out, but it is hard
1134 // to know if a clip will actually be used in advance of storing it in
1135 // the DisplayList buffer.
1136 // See https://github.com/flutter/flutter/issues/147021
1137 FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
1138 << current_depth_ << " <=? " << transform_stack_.back().clip_depth;
1139 uint32_t clip_depth = transform_stack_.back().clip_depth;
1140
1141 const Matrix clip_transform =
1142 Matrix::MakeTranslation(Vector3(-GetGlobalPassPosition())) *
1144
1145 std::optional<Rect> clip_coverage = geometry.GetCoverage(clip_transform);
1146 if (!clip_coverage.has_value()) {
1147 return;
1148 }
1149
1150 ClipContents clip_contents(
1151 clip_coverage.value(),
1152 /*is_axis_aligned_rect=*/geometry.IsAxisAlignedRect() &&
1153 GetCurrentTransform().IsTranslationScaleOnly());
1154 clip_contents.SetClipOperation(clip_op);
1155
1156 EntityPassClipStack::ClipStateResult clip_state_result =
1157 clip_coverage_stack_.RecordClip(
1158 clip_contents, //
1159 /*transform=*/clip_transform, //
1160 /*global_pass_position=*/GetGlobalPassPosition(), //
1161 /*clip_depth=*/clip_depth, //
1162 /*clip_height_floor=*/GetClipHeightFloor(), //
1163 /*is_aa=*/is_aa);
1164
1165 if (clip_state_result.clip_did_change) {
1166 // We only need to update the pass scissor if the clip state has changed.
1167 SetClipScissor(
1168 clip_coverage_stack_.CurrentClipCoverage(),
1169 *render_passes_.back().GetInlinePassContext()->GetRenderPass(),
1170 GetGlobalPassPosition());
1171 }
1172
1173 ++transform_stack_.back().clip_height;
1174 ++transform_stack_.back().num_clips;
1175
1176 if (!clip_state_result.should_render) {
1177 return;
1178 }
1179
1180 // Note: this is a bit of a hack. Its not possible to construct a geometry
1181 // result without begninning the render pass. We should refactor the geometry
1182 // objects so that they only need a reference to the render pass size and/or
1183 // orthographic transform.
1184 Entity entity;
1185 entity.SetTransform(clip_transform);
1186 entity.SetClipDepth(clip_depth);
1187
1188 GeometryResult geometry_result = geometry.GetPositionBuffer(
1189 renderer_, //
1190 entity, //
1191 *render_passes_.back().GetInlinePassContext()->GetRenderPass() //
1192 );
1193 clip_contents.SetGeometry(geometry_result);
1194 clip_coverage_stack_.GetLastReplayResult().clip_contents.SetGeometry(
1195 geometry_result);
1196
1197 clip_contents.Render(
1198 renderer_, *render_passes_.back().GetInlinePassContext()->GetRenderPass(),
1199 clip_depth);
1200}
1201
1203 uint32_t count,
1204 Scalar radius,
1205 const Paint& paint,
1206 PointStyle point_style) {
1207 if (radius <= 0) {
1208 return;
1209 }
1210
1211 Entity entity;
1213 entity.SetBlendMode(paint.blend_mode);
1214
1215 PointFieldGeometry geom(points, count, radius,
1216 /*round=*/point_style == PointStyle::kRound);
1217 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
1218}
1219
1220void Canvas::DrawImage(const std::shared_ptr<Texture>& image,
1221 Point offset,
1222 const Paint& paint,
1223 const SamplerDescriptor& sampler) {
1224 if (!image) {
1225 return;
1226 }
1227
1228 const Rect source = Rect::MakeSize(image->GetSize());
1229 const Rect dest = source.Shift(offset);
1230
1231 DrawImageRect(image, source, dest, paint, sampler);
1232}
1233
1234void Canvas::DrawImageRect(const std::shared_ptr<Texture>& image,
1235 Rect source,
1236 Rect dest,
1237 const Paint& paint,
1238 const SamplerDescriptor& sampler,
1239 SourceRectConstraint src_rect_constraint) {
1240 if (!image || source.IsEmpty() || dest.IsEmpty()) {
1241 return;
1242 }
1243
1244 ISize size = image->GetSize();
1245 if (size.IsEmpty()) {
1246 return;
1247 }
1248
1249 std::optional<Rect> clipped_source =
1250 source.Intersection(Rect::MakeSize(size));
1251 if (!clipped_source) {
1252 return;
1253 }
1254
1255 if (AttemptColorFilterOptimization(image, source, dest, paint, sampler,
1256 src_rect_constraint)) {
1257 return;
1258 }
1259
1260 if (*clipped_source != source) {
1261 Scalar sx = dest.GetWidth() / source.GetWidth();
1262 Scalar sy = dest.GetHeight() / source.GetHeight();
1263 Scalar tx = dest.GetLeft() - source.GetLeft() * sx;
1264 Scalar ty = dest.GetTop() - source.GetTop() * sy;
1265 Matrix src_to_dest = Matrix::MakeTranslateScale({sx, sy, 1}, {tx, ty, 0});
1266 dest = clipped_source->TransformBounds(src_to_dest);
1267 }
1268
1269 auto texture_contents = TextureContents::MakeRect(dest);
1270 texture_contents->SetTexture(image);
1271 texture_contents->SetSourceRect(*clipped_source);
1272 texture_contents->SetStrictSourceRect(src_rect_constraint ==
1274 texture_contents->SetSamplerDescriptor(sampler);
1275 texture_contents->SetOpacity(paint.color.alpha);
1276 texture_contents->SetDeferApplyingOpacity(paint.HasColorFilter());
1277
1278 Entity entity;
1279 entity.SetBlendMode(paint.blend_mode);
1281
1282 if (!paint.mask_blur_descriptor.has_value()) {
1283 entity.SetContents(
1284 paint.WithFilters(renderer_, std::move(texture_contents)));
1285 AddRenderEntityToCurrentPass(entity);
1286 return;
1287 }
1288
1289 FillRectGeometry out_rect(Rect{});
1290
1291 entity.SetContents(paint.WithFilters(
1292 renderer_,
1293 paint.mask_blur_descriptor->CreateMaskBlur(texture_contents, &out_rect)));
1294 AddRenderEntityToCurrentPass(entity);
1295}
1296
1297size_t Canvas::GetClipHeight() const {
1298 return transform_stack_.back().clip_height;
1299}
1300
1301void Canvas::DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
1302 BlendMode blend_mode,
1303 const Paint& paint) {
1304 // Override the blend mode with kDestination in order to match the behavior
1305 // of Skia's SK_LEGACY_IGNORE_DRAW_VERTICES_BLEND_WITH_NO_SHADER flag, which
1306 // is enabled when the Flutter engine builds Skia.
1307 if (!paint.color_source) {
1308 blend_mode = BlendMode::kDst;
1309 }
1310
1311 Entity entity;
1313 entity.SetBlendMode(paint.blend_mode);
1314
1315 // If there are no vertex colors.
1316 if (UseColorSourceContents(vertices, paint)) {
1317 AddRenderEntityWithFiltersToCurrentPass(entity, vertices.get(), paint);
1318 return;
1319 }
1320
1321 // If the blend mode is destination don't bother to bind or create a texture.
1322 if (blend_mode == BlendMode::kDst) {
1323 auto contents = std::make_shared<VerticesSimpleBlendContents>();
1324 contents->SetBlendMode(blend_mode);
1325 contents->SetAlpha(paint.color.alpha);
1326 contents->SetGeometry(vertices);
1327 entity.SetContents(paint.WithFilters(renderer_, std::move(contents)));
1328 AddRenderEntityToCurrentPass(entity);
1329 return;
1330 }
1331
1332 // If there is a texture, use this directly. Otherwise render the color
1333 // source to a texture.
1334 if (paint.color_source &&
1336 const flutter::DlImageColorSource* image_color_source =
1337 paint.color_source->asImage();
1338 FML_DCHECK(image_color_source);
1339 auto texture =
1340 image_color_source->image()->asImpellerImage()->GetCachedTexture(
1341 renderer_);
1343 auto x_tile_mode = static_cast<Entity::TileMode>(
1344 image_color_source->horizontal_tile_mode());
1345 auto y_tile_mode =
1346 static_cast<Entity::TileMode>(image_color_source->vertical_tile_mode());
1347 auto sampler_descriptor =
1348 skia_conversions::ToSamplerDescriptor(image_color_source->sampling());
1349 auto effect_transform = image_color_source->matrix();
1350
1351 auto contents = std::make_shared<VerticesSimpleBlendContents>();
1352 contents->SetBlendMode(blend_mode);
1353 contents->SetAlpha(paint.color.alpha);
1354 contents->SetGeometry(vertices);
1355 contents->SetEffectTransform(effect_transform);
1356 contents->SetTexture(texture);
1357 contents->SetTileMode(x_tile_mode, y_tile_mode);
1358 contents->SetSamplerDescriptor(sampler_descriptor);
1359
1360 entity.SetContents(paint.WithFilters(renderer_, std::move(contents)));
1361 AddRenderEntityToCurrentPass(entity);
1362 return;
1363 }
1364
1365 auto src_paint = paint;
1366 src_paint.color = paint.color.WithAlpha(1.0);
1367
1368 std::shared_ptr<ColorSourceContents> src_contents =
1369 src_paint.CreateContents(renderer_, vertices.get());
1370
1371 // If the color source has an intrinsic size, then we use that to
1372 // create the src contents as a simplification. Otherwise we use
1373 // the extent of the texture coordinates to determine how large
1374 // the src contents should be. If neither has a value we fall back
1375 // to using the geometry coverage data.
1376 Rect src_coverage;
1377 auto size = src_contents->GetColorSourceSize();
1378 if (size.has_value()) {
1379 src_coverage = Rect::MakeXYWH(0, 0, size->width, size->height);
1380 } else {
1381 auto cvg = vertices->GetCoverage(Matrix{});
1382 FML_CHECK(cvg.has_value());
1383 auto texture_coverage = vertices->GetTextureCoordinateCoverage();
1384 if (texture_coverage.has_value()) {
1385 src_coverage =
1386 Rect::MakeOriginSize(texture_coverage->GetOrigin(),
1387 texture_coverage->GetSize().Max({1, 1}));
1388 } else {
1389 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1390 src_coverage = cvg.value();
1391 }
1392 }
1393 clip_geometry_.push_back(Geometry::MakeRect(Rect::Round(src_coverage)));
1394 src_contents =
1395 src_paint.CreateContents(renderer_, clip_geometry_.back().get());
1396
1397 auto contents = std::make_shared<VerticesSimpleBlendContents>();
1398 contents->SetBlendMode(blend_mode);
1399 contents->SetAlpha(paint.color.alpha);
1400 contents->SetGeometry(vertices);
1401 contents->SetLazyTextureCoverage(src_coverage);
1402 contents->SetLazyTexture(
1403 [src_contents, src_coverage](
1404 const ContentContext& renderer) -> std::shared_ptr<Texture> {
1405 // Applying the src coverage as the coverage limit prevents the 1px
1406 // coverage pad from adding a border that is picked up by developer
1407 // specified UVs.
1408 auto snapshot = src_contents->RenderToSnapshot(
1409 renderer, {}, {.coverage_limit = Rect::Round(src_coverage)});
1410 return snapshot.has_value() ? snapshot->texture : nullptr;
1411 });
1412 entity.SetContents(paint.WithFilters(renderer_, std::move(contents)));
1413 AddRenderEntityToCurrentPass(entity);
1414}
1415
1416void Canvas::DrawAtlas(const std::shared_ptr<AtlasContents>& atlas_contents,
1417 const Paint& paint) {
1418 atlas_contents->SetAlpha(paint.color.alpha);
1419
1420 Entity entity;
1422 entity.SetBlendMode(paint.blend_mode);
1423 entity.SetContents(paint.WithFilters(renderer_, atlas_contents));
1424
1425 AddRenderEntityToCurrentPass(entity);
1426}
1427
1428/// Compositor Functionality
1429/////////////////////////////////////////
1430
1431void Canvas::SetupRenderPass() {
1432 renderer_.GetRenderTargetCache()->Start();
1433 ColorAttachment color0 = render_target_.GetColorAttachment(0);
1434
1435 auto& stencil_attachment = render_target_.GetStencilAttachment();
1436 auto& depth_attachment = render_target_.GetDepthAttachment();
1437 if (!stencil_attachment.has_value() || !depth_attachment.has_value()) {
1438 // Setup a new root stencil with an optimal configuration if one wasn't
1439 // provided by the caller.
1440 render_target_.SetupDepthStencilAttachments(
1441 *renderer_.GetContext(),
1442 *renderer_.GetContext()->GetResourceAllocator(),
1443 color0.texture->GetSize(),
1444 renderer_.GetContext()->GetCapabilities()->SupportsOffscreenMSAA() &&
1445 color0.texture->GetTextureDescriptor().sample_count >
1447 "ImpellerOnscreen", kDefaultStencilConfig);
1448 }
1449
1450 // Set up the clear color of the root pass.
1452 render_target_.SetColorAttachment(color0, 0);
1453
1454 // If requires_readback is true, then there is a backdrop filter or emulated
1455 // advanced blend in the first save layer. This requires a readback, which
1456 // isn't supported by onscreen textures. To support this, we immediately begin
1457 // a second save layer with the same dimensions as the onscreen. When
1458 // rendering is completed, we must blit this saveLayer to the onscreen.
1459 if (requires_readback_) {
1460 auto entity_pass_target =
1461 CreateRenderTarget(renderer_, //
1462 color0.texture->GetSize(), //
1463 /*clear_color=*/Color::BlackTransparent());
1464 render_passes_.push_back(
1465 LazyRenderingConfig(renderer_, std::move(entity_pass_target)));
1466 } else {
1467 auto entity_pass_target = std::make_unique<EntityPassTarget>(
1468 render_target_, //
1471 );
1472 render_passes_.push_back(
1473 LazyRenderingConfig(renderer_, std::move(entity_pass_target)));
1474 }
1475}
1476
1477void Canvas::SkipUntilMatchingRestore(size_t total_content_depth) {
1478 auto entry = CanvasStackEntry{};
1479 entry.skipping = true;
1480 entry.clip_depth = current_depth_ + total_content_depth;
1481 transform_stack_.push_back(entry);
1482}
1483
1484void Canvas::Save(uint32_t total_content_depth) {
1485 if (IsSkipping()) {
1486 return SkipUntilMatchingRestore(total_content_depth);
1487 }
1488
1489 auto entry = CanvasStackEntry{};
1490 entry.transform = transform_stack_.back().transform;
1491 entry.clip_depth = current_depth_ + total_content_depth;
1492 entry.distributed_opacity = transform_stack_.back().distributed_opacity;
1493 FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
1494 << entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
1495 << " after allocating " << total_content_depth;
1496 entry.clip_height = transform_stack_.back().clip_height;
1497 entry.rendering_mode = Entity::RenderingMode::kDirect;
1498 transform_stack_.push_back(entry);
1499}
1500
1501std::optional<Rect> Canvas::GetLocalCoverageLimit() const {
1502 if (!clip_coverage_stack_.HasCoverage()) {
1503 // The current clip is empty. This means the pass texture won't be
1504 // visible, so skip it.
1505 return std::nullopt;
1506 }
1507
1508 std::optional<Rect> maybe_current_clip_coverage =
1509 clip_coverage_stack_.CurrentClipCoverage();
1510 if (!maybe_current_clip_coverage.has_value()) {
1511 return std::nullopt;
1512 }
1513
1514 Rect current_clip_coverage = maybe_current_clip_coverage.value();
1515
1516 FML_CHECK(!render_passes_.empty());
1517 const LazyRenderingConfig& back_render_pass = render_passes_.back();
1518 std::shared_ptr<Texture> back_texture =
1519 back_render_pass.GetInlinePassContext()->GetTexture();
1520 FML_CHECK(back_texture) << "Context is valid:"
1521 << back_render_pass.GetInlinePassContext()->IsValid();
1522
1523 // The maximum coverage of the subpass. Subpasses textures should never
1524 // extend outside the parent pass texture or the current clip coverage.
1525 std::optional<Rect> maybe_coverage_limit =
1526 Rect::MakeOriginSize(GetGlobalPassPosition(),
1527 Size(back_texture->GetSize()))
1528 .Intersection(current_clip_coverage);
1529
1530 if (!maybe_coverage_limit.has_value() || maybe_coverage_limit->IsEmpty()) {
1531 return std::nullopt;
1532 }
1533
1534 return maybe_coverage_limit->Intersection(
1535 Rect::MakeSize(render_target_.GetRenderTargetSize()));
1536}
1537
1538void Canvas::SaveLayer(const Paint& paint,
1539 std::optional<Rect> bounds,
1540 const flutter::DlImageFilter* backdrop_filter,
1541 ContentBoundsPromise bounds_promise,
1542 uint32_t total_content_depth,
1543 bool can_distribute_opacity,
1544 std::optional<int64_t> backdrop_id) {
1545 TRACE_EVENT0("flutter", "Canvas::saveLayer");
1546 if (IsSkipping()) {
1547 return SkipUntilMatchingRestore(total_content_depth);
1548 }
1549
1550 auto maybe_coverage_limit = GetLocalCoverageLimit();
1551 if (!maybe_coverage_limit.has_value()) {
1552 return SkipUntilMatchingRestore(total_content_depth);
1553 }
1554 auto coverage_limit = maybe_coverage_limit.value();
1555
1556 if (can_distribute_opacity && !backdrop_filter &&
1558 bounds_promise != ContentBoundsPromise::kMayClipContents) {
1559 Save(total_content_depth);
1560 transform_stack_.back().distributed_opacity *= paint.color.alpha;
1561 return;
1562 }
1563
1564 std::shared_ptr<FilterContents> filter_contents = paint.WithImageFilter(
1565 renderer_, Rect(), transform_stack_.back().transform,
1567
1568 std::optional<Rect> maybe_subpass_coverage = ComputeSaveLayerCoverage(
1569 bounds.value_or(Rect::MakeMaximum()),
1570 transform_stack_.back().transform, //
1571 coverage_limit, //
1572 filter_contents, //
1573 /*flood_output_coverage=*/
1575 /*flood_input_coverage=*/!!backdrop_filter ||
1576 (paint.color_filter &&
1578 );
1579
1580 if (!maybe_subpass_coverage.has_value()) {
1581 return SkipUntilMatchingRestore(total_content_depth);
1582 }
1583
1584 auto subpass_coverage = maybe_subpass_coverage.value();
1585
1586 // When an image filter is present, clamp to avoid flicking due to nearest
1587 // sampled image. For other cases, round out to ensure than any geometry is
1588 // not cut off.
1589 //
1590 // See also this bug: https://github.com/flutter/flutter/issues/144213
1591 //
1592 // TODO(jonahwilliams): this could still round out for filters that use decal
1593 // sampling mode.
1595 bool did_round_out = false;
1596 Point coverage_origin_adjustment = Point{0, 0};
1597 if (paint.image_filter) {
1598 subpass_size = ISize(subpass_coverage.GetSize());
1599 } else {
1600 did_round_out = true;
1601 subpass_size =
1602 static_cast<ISize>(IRect::RoundOut(subpass_coverage).GetSize());
1603 // If rounding out, adjust the coverage to account for the subpixel shift.
1604 coverage_origin_adjustment =
1605 Point(subpass_coverage.GetLeftTop().x -
1606 std::floor(subpass_coverage.GetLeftTop().x),
1607 subpass_coverage.GetLeftTop().y -
1608 std::floor(subpass_coverage.GetLeftTop().y));
1609 }
1610 if (subpass_size.IsEmpty()) {
1611 return SkipUntilMatchingRestore(total_content_depth);
1612 }
1613
1614 // When there are scaling filters present, these contents may exceed the
1615 // maximum texture size. Perform a clamp here, which may cause rendering
1616 // artifacts.
1617 subpass_size = subpass_size.Min(renderer_.GetContext()
1618 ->GetCapabilities()
1619 ->GetMaximumRenderPassAttachmentSize());
1620
1621 // Backdrop filter state, ignored if there is no BDF.
1622 std::shared_ptr<FilterContents> backdrop_filter_contents;
1623 Point local_position = Point(0, 0);
1624 if (backdrop_filter) {
1625 local_position = subpass_coverage.GetOrigin() - GetGlobalPassPosition();
1626
1627 std::shared_ptr<Texture> input_texture;
1628
1629 // If the backdrop ID is not nullopt and there is more than one usage
1630 // of it in the current scene, cache the backdrop texture and remove it from
1631 // the current entity pass flip.
1632 bool will_cache_backdrop_texture = false;
1633 BackdropData* backdrop_data = nullptr;
1634 // If we've reached this point, there is at least one backdrop filter. But
1635 // potentially more if there is a backdrop id. We may conditionally set this
1636 // to a higher value in the if block below.
1637 size_t backdrop_count = 1;
1638 if (backdrop_id.has_value()) {
1639 std::unordered_map<int64_t, BackdropData>::iterator backdrop_data_it =
1640 backdrop_data_.find(backdrop_id.value());
1641 if (backdrop_data_it != backdrop_data_.end()) {
1642 backdrop_data = &backdrop_data_it->second;
1643 will_cache_backdrop_texture =
1644 backdrop_data_it->second.backdrop_count > 1;
1645 backdrop_count = backdrop_data_it->second.backdrop_count;
1646 }
1647 }
1648
1649 if (!will_cache_backdrop_texture || !backdrop_data->texture_slot) {
1650 backdrop_count_ -= backdrop_count;
1651
1652 // The onscreen texture can be flipped to if:
1653 // 1. The device supports framebuffer fetch
1654 // 2. There are no more backdrop filters
1655 // 3. The current render pass is for the onscreen pass.
1656 const bool should_use_onscreen =
1658 backdrop_count_ == 0 && render_passes_.size() == 1u;
1659 input_texture = FlipBackdrop(
1660 GetGlobalPassPosition(), //
1661 /*should_remove_texture=*/will_cache_backdrop_texture, //
1662 /*should_use_onscreen=*/should_use_onscreen //
1663 );
1664 if (!input_texture) {
1665 // Validation failures are logged in FlipBackdrop.
1666 return;
1667 }
1668
1669 if (will_cache_backdrop_texture) {
1670 backdrop_data->texture_slot = input_texture;
1671 }
1672 } else {
1673 input_texture = backdrop_data->texture_slot;
1674 }
1675
1676 backdrop_filter_contents =
1677 WrapInput(renderer_, backdrop_filter,
1678 FilterInput::Make(std::move(input_texture)));
1679 backdrop_filter_contents->SetEffectTransform(
1680 transform_stack_.back().transform.Basis());
1681 backdrop_filter_contents->SetRenderingMode(
1682 transform_stack_.back().transform.HasTranslation()
1685
1686 if (will_cache_backdrop_texture) {
1687 FML_DCHECK(backdrop_data);
1688 // If all filters on the shared backdrop layer are equal, process the
1689 // layer once.
1690 if (backdrop_data->all_filters_equal &&
1691 !backdrop_data->shared_filter_snapshot.has_value()) {
1692 // TODO(157110): compute minimum input hint.
1693 backdrop_data->shared_filter_snapshot =
1694 backdrop_filter_contents->RenderToSnapshot(renderer_, {}, {});
1695 }
1696
1697 std::optional<Snapshot> maybe_snapshot =
1698 backdrop_data->shared_filter_snapshot;
1699 if (maybe_snapshot.has_value()) {
1700 const Snapshot& snapshot = maybe_snapshot.value();
1701 std::shared_ptr<TextureContents> contents = TextureContents::MakeRect(
1702 subpass_coverage.Shift(-GetGlobalPassPosition()));
1703 auto scaled =
1704 subpass_coverage.TransformBounds(snapshot.transform.Invert());
1705 contents->SetTexture(snapshot.texture);
1706 contents->SetSourceRect(scaled);
1707 contents->SetSamplerDescriptor(snapshot.sampler_descriptor);
1708
1709 // This backdrop entity sets a depth value as it is written to the newly
1710 // flipped backdrop and not into a new saveLayer.
1711 Entity backdrop_entity;
1712 backdrop_entity.SetContents(std::move(contents));
1713 backdrop_entity.SetClipDepth(++current_depth_);
1714 backdrop_entity.SetBlendMode(paint.blend_mode);
1715
1716 backdrop_entity.Render(renderer_, GetCurrentRenderPass());
1717 Save(0);
1718 return;
1719 }
1720 }
1721 }
1722
1723 // When applying a save layer, absorb any pending distributed opacity.
1724 Paint paint_copy = paint;
1725 paint_copy.color.alpha *= transform_stack_.back().distributed_opacity;
1726 transform_stack_.back().distributed_opacity = 1.0;
1727
1728 render_passes_.push_back(
1729 LazyRenderingConfig(renderer_, //
1730 CreateRenderTarget(renderer_, //
1731 subpass_size, //
1733 )));
1734 save_layer_state_.push_back(SaveLayerState{
1735 paint_copy, subpass_coverage.Shift(-coverage_origin_adjustment)});
1736
1737 CanvasStackEntry entry;
1738 entry.transform = transform_stack_.back().transform;
1739 entry.clip_depth = current_depth_ + total_content_depth;
1740 FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
1741 << entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
1742 << " after allocating " << total_content_depth;
1743 entry.clip_height = transform_stack_.back().clip_height;
1745 entry.did_round_out = did_round_out;
1746 transform_stack_.emplace_back(entry);
1747
1748 // Start non-collapsed subpasses with a fresh clip coverage stack limited by
1749 // the subpass coverage. This is important because image filters applied to
1750 // save layers may transform the subpass texture after it's rendered,
1751 // causing parent clip coverage to get misaligned with the actual area that
1752 // the subpass will affect in the parent pass.
1753 clip_coverage_stack_.PushSubpass(subpass_coverage, GetClipHeight());
1754
1755 if (!backdrop_filter_contents) {
1756 return;
1757 }
1758
1759 // Render the backdrop entity.
1760 Entity backdrop_entity;
1761 backdrop_entity.SetContents(std::move(backdrop_filter_contents));
1762 backdrop_entity.SetTransform(
1763 Matrix::MakeTranslation(Vector3(-local_position)));
1764 backdrop_entity.SetClipDepth(std::numeric_limits<uint32_t>::max());
1765 backdrop_entity.Render(renderer_, GetCurrentRenderPass());
1766}
1767
1769 FML_DCHECK(transform_stack_.size() > 0);
1770 if (transform_stack_.size() == 1) {
1771 return false;
1772 }
1773
1774 // This check is important to make sure we didn't exceed the depth
1775 // that the clips were rendered at while rendering any of the
1776 // rendering ops. It is OK for the current depth to equal the
1777 // outgoing clip depth because that means the clipping would have
1778 // been successful up through the last rendering op, but it cannot
1779 // be greater.
1780 // Also, we bump the current rendering depth to the outgoing clip
1781 // depth so that future rendering operations are not clipped by
1782 // any of the pixels set by the expiring clips. It is OK for the
1783 // estimates used to determine the clip depth in save/saveLayer
1784 // to be overly conservative, but we need to jump the depth to
1785 // the clip depth so that the next rendering op will get a
1786 // larger depth (it will pre-increment the current_depth_ value).
1787 FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
1788 << current_depth_ << " <=? " << transform_stack_.back().clip_depth;
1789 current_depth_ = transform_stack_.back().clip_depth;
1790
1791 if (IsSkipping()) {
1792 transform_stack_.pop_back();
1793 return true;
1794 }
1795
1796 if (transform_stack_.back().rendering_mode ==
1798 transform_stack_.back().rendering_mode ==
1800 auto lazy_render_pass = std::move(render_passes_.back());
1801 render_passes_.pop_back();
1802 // Force the render pass to be constructed if it never was.
1803 lazy_render_pass.GetInlinePassContext()->GetRenderPass();
1804
1805 SaveLayerState save_layer_state = save_layer_state_.back();
1806 save_layer_state_.pop_back();
1807 auto global_pass_position = GetGlobalPassPosition();
1808
1809 std::shared_ptr<Contents> contents = CreateContentsForSubpassTarget(
1810 renderer_, save_layer_state.paint, //
1811 lazy_render_pass.GetInlinePassContext()->GetTexture(), //
1812 Matrix::MakeTranslation(Vector3{-global_pass_position}) * //
1813 transform_stack_.back().transform //
1814 );
1815
1816 lazy_render_pass.GetInlinePassContext()->EndPass();
1817
1818 // Round the subpass texture position for pixel alignment with the parent
1819 // pass render target. By default, we draw subpass textures with nearest
1820 // sampling, so aligning here is important for avoiding visual nearest
1821 // sampling errors caused by limited floating point precision when
1822 // straddling a half pixel boundary.
1823 Point subpass_texture_position;
1824 if (transform_stack_.back().did_round_out) {
1825 // Subpass coverage was rounded out, origin potentially moved "down" by
1826 // as much as a pixel.
1827 subpass_texture_position =
1828 (save_layer_state.coverage.GetOrigin() - global_pass_position)
1829 .Floor();
1830 } else {
1831 // Subpass coverage was truncated. Pick the closest phyiscal pixel.
1832 subpass_texture_position =
1833 (save_layer_state.coverage.GetOrigin() - global_pass_position)
1834 .Round();
1835 }
1836
1837 Entity element_entity;
1838 element_entity.SetClipDepth(++current_depth_);
1839 element_entity.SetContents(std::move(contents));
1840 element_entity.SetBlendMode(save_layer_state.paint.blend_mode);
1841 element_entity.SetTransform(
1842 Matrix::MakeTranslation(Vector3(subpass_texture_position)));
1843
1844 if (element_entity.GetBlendMode() > Entity::kLastPipelineBlendMode) {
1846 ApplyFramebufferBlend(element_entity);
1847 } else {
1848 // End the active pass and flush the buffer before rendering "advanced"
1849 // blends. Advanced blends work by binding the current render target
1850 // texture as an input ("destination"), blending with a second texture
1851 // input ("source"), writing the result to an intermediate texture, and
1852 // finally copying the data from the intermediate texture back to the
1853 // render target texture. And so all of the commands that have written
1854 // to the render target texture so far need to execute before it's bound
1855 // for blending (otherwise the blend pass will end up executing before
1856 // all the previous commands in the active pass).
1857 auto input_texture = FlipBackdrop(GetGlobalPassPosition());
1858 if (!input_texture) {
1859 return false;
1860 }
1861
1862 FilterInput::Vector inputs = {
1863 FilterInput::Make(input_texture,
1864 element_entity.GetTransform().Invert()),
1865 FilterInput::Make(element_entity.GetContents())};
1866 auto contents = ColorFilterContents::MakeBlend(
1867 element_entity.GetBlendMode(), inputs);
1868 contents->SetCoverageHint(element_entity.GetCoverage());
1869 element_entity.SetContents(std::move(contents));
1870 element_entity.SetBlendMode(BlendMode::kSrc);
1871 }
1872 }
1873
1874 element_entity.Render(
1875 renderer_, //
1876 *render_passes_.back().GetInlinePassContext()->GetRenderPass() //
1877 );
1878 clip_coverage_stack_.PopSubpass();
1879 transform_stack_.pop_back();
1880
1881 // We don't need to restore clips if a saveLayer was performed, as the clip
1882 // state is per render target, and no more rendering operations will be
1883 // performed as the render target workloaded is completed in the restore.
1884 return true;
1885 }
1886
1887 size_t num_clips = transform_stack_.back().num_clips;
1888 transform_stack_.pop_back();
1889
1890 if (num_clips > 0) {
1891 EntityPassClipStack::ClipStateResult clip_state_result =
1892 clip_coverage_stack_.RecordRestore(GetGlobalPassPosition(),
1893 GetClipHeight());
1894
1895 // Clip restores are never required with depth based clipping.
1896 FML_DCHECK(!clip_state_result.should_render);
1897 if (clip_state_result.clip_did_change) {
1898 // We only need to update the pass scissor if the clip state has changed.
1899 SetClipScissor(
1900 clip_coverage_stack_.CurrentClipCoverage(), //
1901 *render_passes_.back().GetInlinePassContext()->GetRenderPass(), //
1902 GetGlobalPassPosition() //
1903 );
1904 }
1905 }
1906
1907 return true;
1908}
1909
1910bool Canvas::AttemptBlurredTextOptimization(
1911 const std::shared_ptr<TextFrame>& text_frame,
1912 const std::shared_ptr<TextContents>& text_contents,
1913 Entity& entity,
1914 const Paint& paint) {
1915 if (!paint.mask_blur_descriptor.has_value() || //
1916 paint.image_filter != nullptr || //
1917 paint.color_filter != nullptr || //
1918 paint.invert_colors) {
1919 return false;
1920 }
1921
1922 // TODO(bdero): This mask blur application is a hack. It will always wind up
1923 // doing a gaussian blur that affects the color source itself
1924 // instead of just the mask. The color filter text support
1925 // needs to be reworked in order to interact correctly with
1926 // mask filters.
1927 // https://github.com/flutter/flutter/issues/133297
1928 std::shared_ptr<FilterContents> filter =
1929 paint.mask_blur_descriptor->CreateMaskBlur(
1930 FilterInput::Make(text_contents),
1931 /*is_solid_color=*/true, GetCurrentTransform());
1932
1933 std::optional<Glyph> maybe_glyph = text_frame->AsSingleGlyph();
1934 int64_t identifier = maybe_glyph.has_value()
1935 ? maybe_glyph.value().index
1936 : reinterpret_cast<int64_t>(text_frame.get());
1937 TextShadowCache::TextShadowCacheKey cache_key(
1938 /*p_max_basis=*/entity.GetTransform().GetMaxBasisLengthXY(),
1939 /*p_identifier=*/identifier,
1940 /*p_is_single_glyph=*/maybe_glyph.has_value(),
1941 /*p_font=*/text_frame->GetFont(),
1942 /*p_sigma=*/paint.mask_blur_descriptor->sigma,
1943 /*p_color=*/paint.color);
1944
1945 std::optional<Entity> result = renderer_.GetTextShadowCache().Lookup(
1946 renderer_, entity, filter, cache_key);
1947 if (result.has_value()) {
1948 AddRenderEntityToCurrentPass(result.value(), /*reuse_depth=*/false);
1949 return true;
1950 } else {
1951 return false;
1952 }
1953}
1954
1955// If the text point size * max basis XY is larger than this value,
1956// render the text as paths (if available) for faster and higher
1957// fidelity rendering. This is a somewhat arbitrary cutoff
1958static constexpr Scalar kMaxTextScale = 250;
1959
1960void Canvas::DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
1961 Point position,
1962 const Paint& paint) {
1964 if (max_scale * text_frame->GetFont().GetMetrics().point_size >
1965 kMaxTextScale) {
1966 fml::StatusOr<flutter::DlPath> path = text_frame->GetPath();
1967 if (path.ok()) {
1968 Save(1);
1970 DrawPath(path.value(), paint);
1971 Restore();
1972 return;
1973 }
1974 }
1975
1976 Entity entity;
1977 entity.SetClipDepth(GetClipHeight());
1978 entity.SetBlendMode(paint.blend_mode);
1979
1980 auto text_contents = std::make_shared<TextContents>();
1981 text_contents->SetTextFrame(text_frame);
1982 text_contents->SetPosition(position);
1983 text_contents->SetScreenTransform(GetCurrentTransform());
1984 text_contents->SetForceTextColor(paint.mask_blur_descriptor.has_value());
1985 text_contents->SetColor(paint.color);
1986 text_contents->SetTextProperties(paint.color, paint.GetStroke());
1987
1989
1990 if (AttemptBlurredTextOptimization(text_frame, text_contents, entity,
1991 paint)) {
1992 return;
1993 }
1994
1995 entity.SetContents(paint.WithFilters(renderer_, std::move(text_contents)));
1996 AddRenderEntityToCurrentPass(entity, false);
1997}
1998
1999void Canvas::AddRenderSDFEntityToCurrentPass(const Paint& paint,
2001 Entity entity;
2003 entity.SetBlendMode(paint.blend_mode);
2004
2005 if (paint.color_source) {
2006 // Since we are going to use BlendMode::kSrcIn to implement the color_source
2007 // the SDF portion of the blend should just be solid white to get the
2008 // correct color from the color_source.
2009 params.color = Color::White();
2010 }
2011 auto geometry = std::make_unique<UberSDFGeometry>(params);
2012 auto contents = UberSDFContents::Make(params, std::move(geometry));
2013 const Geometry* geom = contents->GetGeometry();
2014
2015 if (paint.color_source) {
2016 // UberSDF doesn't perform things like gradients so we blend the SDF
2017 // with the color source.
2018 std::shared_ptr<Contents> color_source_contents =
2019 paint.CreateContents(renderer_, geom);
2020 std::shared_ptr<Contents> final_contents = ColorFilterContents::MakeBlend(
2021 BlendMode::kSrcIn, {FilterInput::Make(std::move(contents)),
2022 FilterInput::Make(color_source_contents)});
2023
2024 Paint new_paint = paint;
2025 new_paint.color_source = nullptr;
2026 AddRenderEntityWithFiltersToCurrentPass(entity, geom, new_paint,
2027 /*reuse_depth=*/false,
2028 /*override_contents=*/
2029 std::move(final_contents));
2030 } else {
2031 AddRenderEntityWithFiltersToCurrentPass(entity, geom, paint,
2032 /*reuse_depth=*/false,
2033 /*override_contents=*/
2034 std::move(contents));
2035 }
2036}
2037
2038void Canvas::AddRenderEntityWithFiltersToCurrentPass(
2039 Entity& entity,
2040 const Geometry* geometry,
2041 const Paint& paint,
2042 bool reuse_depth,
2043 std::shared_ptr<Contents> override_contents) {
2044 std::shared_ptr<ColorSourceContents> color_source_contents;
2045 std::shared_ptr<Contents> contents;
2046 if (override_contents) {
2047 contents = std::move(override_contents);
2048 } else {
2049 color_source_contents = paint.CreateContents(renderer_, geometry);
2050 contents = color_source_contents;
2051 }
2052
2053 if (!paint.color_filter && !paint.invert_colors && !paint.image_filter &&
2054 !paint.mask_blur_descriptor.has_value()) {
2055 entity.SetContents(std::move(contents));
2056 AddRenderEntityToCurrentPass(entity, reuse_depth);
2057 return;
2058 }
2059
2060 // Attempt to apply the color filter on the CPU first.
2061 // Note: This is not just an optimization; some color sources rely on
2062 // CPU-applied color filters to behave properly.
2063 bool needs_color_filter = paint.color_filter || paint.invert_colors;
2064 if (needs_color_filter &&
2065 contents->ApplyColorFilter([&](Color color) -> Color {
2066 if (paint.color_filter) {
2067 color = GetCPUColorFilterProc(paint.color_filter)(color);
2068 }
2069 if (paint.invert_colors) {
2070 color = color.ApplyColorMatrix(kColorInversion);
2071 }
2072 return color;
2073 })) {
2074 needs_color_filter = false;
2075 }
2076
2077 bool can_apply_mask_filter = geometry->CanApplyMaskFilter();
2078
2079 if (can_apply_mask_filter && paint.mask_blur_descriptor.has_value()) {
2080 // If there's a mask blur and we need to apply the color filter on the GPU,
2081 // we need to be careful to only apply the color filter to the source
2082 // colors. CreateMaskBlur is able to handle this case.
2083 FML_DCHECK(color_source_contents) << "Mask blur is only supported when no "
2084 "override contents are provided.";
2085 FillRectGeometry out_rect(Rect{});
2086 auto filter = paint.mask_blur_descriptor->CreateMaskBlur(
2087 paint, renderer_, geometry, color_source_contents, needs_color_filter,
2088 &out_rect);
2089 entity.SetContents(std::move(filter));
2090 AddRenderEntityToCurrentPass(entity, reuse_depth);
2091 return;
2092 }
2093
2094 std::shared_ptr<Contents> contents_copy = std::move(contents);
2095
2096 // Image input types will directly set their color filter,
2097 // if any. See `TiledTextureContents.SetColorFilter`.
2098 if (needs_color_filter &&
2099 (!paint.color_source ||
2100 paint.color_source->type() != flutter::DlColorSourceType::kImage)) {
2101 if (paint.color_filter) {
2102 contents_copy = WrapWithGPUColorFilter(
2103 paint.color_filter, FilterInput::Make(std::move(contents_copy)),
2105 }
2106 if (paint.invert_colors) {
2107 contents_copy =
2108 WrapWithInvertColors(FilterInput::Make(std::move(contents_copy)),
2110 }
2111 }
2112
2113 if (paint.image_filter) {
2114 std::shared_ptr<FilterContents> filter =
2115 WrapInput(renderer_, paint.image_filter,
2116 FilterInput::Make(std::move(contents_copy)));
2117 filter->SetRenderingMode(Entity::RenderingMode::kDirect);
2118 entity.SetContents(filter);
2119 AddRenderEntityToCurrentPass(entity, reuse_depth);
2120 return;
2121 }
2122
2123 entity.SetContents(std::move(contents_copy));
2124 AddRenderEntityToCurrentPass(entity, reuse_depth);
2125}
2126
2127void Canvas::AddRenderEntityToCurrentPass(Entity& entity, bool reuse_depth) {
2128 if (IsSkipping()) {
2129 return;
2130 }
2131
2132 entity.SetTransform(
2133 Matrix::MakeTranslation(Vector3(-GetGlobalPassPosition())) *
2134 entity.GetTransform());
2135 entity.SetInheritedOpacity(transform_stack_.back().distributed_opacity);
2136 if (entity.GetBlendMode() == BlendMode::kSrcOver &&
2137 entity.GetContents()->IsOpaque(entity.GetTransform())) {
2138 entity.SetBlendMode(BlendMode::kSrc);
2139 }
2140
2141 // If the entity covers the current render target and is a solid color, then
2142 // conditionally update the backdrop color to its solid color value blended
2143 // with the current backdrop.
2144 if (render_passes_.back().IsApplyingClearColor()) {
2145 std::optional<Color> maybe_color = entity.AsBackgroundColor(
2146 render_passes_.back().GetInlinePassContext()->GetTexture()->GetSize());
2147 if (maybe_color.has_value()) {
2148 Color color = maybe_color.value();
2149 RenderTarget& render_target = render_passes_.back()
2150 .GetInlinePassContext()
2151 ->GetPassTarget()
2152 .GetRenderTarget();
2153 ColorAttachment attachment = render_target.GetColorAttachment(0);
2154 // Attachment.clear color needs to be premultiplied at all times, but the
2155 // Color::Blend function requires unpremultiplied colors.
2156 attachment.clear_color = attachment.clear_color.Unpremultiply()
2157 .Blend(color, entity.GetBlendMode())
2158 .Premultiply();
2159 render_target.SetColorAttachment(attachment, 0u);
2160 return;
2161 }
2162 }
2163 if (!reuse_depth) {
2164 ++current_depth_;
2165 }
2166
2167 // We can render at a depth up to and including the depth of the currently
2168 // active clips and we will still be clipped out, but we cannot render at
2169 // a depth that is greater than the current clips or we will not be clipped.
2170 FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
2171 << current_depth_ << " <=? " << transform_stack_.back().clip_depth;
2172 entity.SetClipDepth(current_depth_);
2173
2174 if (entity.GetBlendMode() > Entity::kLastPipelineBlendMode) {
2175 if (renderer_.GetDeviceCapabilities().SupportsFramebufferFetch()) {
2176 ApplyFramebufferBlend(entity);
2177 } else {
2178 // End the active pass and flush the buffer before rendering "advanced"
2179 // blends. Advanced blends work by binding the current render target
2180 // texture as an input ("destination"), blending with a second texture
2181 // input ("source"), writing the result to an intermediate texture, and
2182 // finally copying the data from the intermediate texture back to the
2183 // render target texture. And so all of the commands that have written
2184 // to the render target texture so far need to execute before it's bound
2185 // for blending (otherwise the blend pass will end up executing before
2186 // all the previous commands in the active pass).
2187 auto input_texture = FlipBackdrop(GetGlobalPassPosition(), //
2188 /*should_remove_texture=*/false,
2189 /*should_use_onscreen=*/false,
2190 /*post_depth_increment=*/true);
2191 if (!input_texture) {
2192 return;
2193 }
2194
2195 // The coverage hint tells the rendered Contents which portion of the
2196 // rendered output will actually be used, and so we set this to the
2197 // current clip coverage (which is the max clip bounds). The contents may
2198 // optionally use this hint to avoid unnecessary rendering work.
2199 auto element_coverage_hint = entity.GetContents()->GetCoverageHint();
2200 entity.GetContents()->SetCoverageHint(Rect::Intersection(
2201 element_coverage_hint, clip_coverage_stack_.CurrentClipCoverage()));
2202
2203 FilterInput::Vector inputs = {
2204 FilterInput::Make(input_texture, entity.GetTransform().Invert()),
2205 FilterInput::Make(entity.GetContents())};
2206 auto contents =
2207 ColorFilterContents::MakeBlend(entity.GetBlendMode(), inputs);
2208 entity.SetContents(std::move(contents));
2209 entity.SetBlendMode(BlendMode::kSrc);
2210 }
2211 }
2212
2213 const std::shared_ptr<RenderPass>& result =
2214 render_passes_.back().GetInlinePassContext()->GetRenderPass();
2215 if (!result) {
2216 // Failure to produce a render pass should be explained by specific errors
2217 // in `InlinePassContext::GetRenderPass()`, so avoid log spam and don't
2218 // append a validation log here.
2219 return;
2220 }
2221
2222 entity.Render(renderer_, *result);
2223}
2224
2225RenderPass& Canvas::GetCurrentRenderPass() const {
2226 return *render_passes_.back().GetInlinePassContext()->GetRenderPass();
2227}
2228
2229void Canvas::SetBackdropData(
2230 std::unordered_map<int64_t, BackdropData> backdrop_data,
2231 size_t backdrop_count) {
2232 backdrop_data_ = std::move(backdrop_data);
2233 backdrop_count_ = backdrop_count;
2234}
2235
2236std::shared_ptr<Texture> Canvas::FlipBackdrop(Point global_pass_position,
2237 bool should_remove_texture,
2238 bool should_use_onscreen,
2239 bool post_depth_increment) {
2240 LazyRenderingConfig rendering_config = std::move(render_passes_.back());
2241 render_passes_.pop_back();
2242
2243 // If the very first thing we render in this EntityPass is a subpass that
2244 // happens to have a backdrop filter or advanced blend, than that backdrop
2245 // filter/blend will sample from an uninitialized texture.
2246 //
2247 // By calling `pass_context.GetRenderPass` here, we force the texture to pass
2248 // through at least one RenderPass with the correct clear configuration before
2249 // any sampling occurs.
2250 //
2251 // In cases where there are no contents, we
2252 // could instead check the clear color and initialize a 1x2 CPU texture
2253 // instead of ending the pass.
2254 rendering_config.GetInlinePassContext()->GetRenderPass();
2255 if (!rendering_config.GetInlinePassContext()->EndPass()) {
2257 << "Failed to end the current render pass in order to read from "
2258 "the backdrop texture and apply an advanced blend or backdrop "
2259 "filter.";
2260 // Note: adding this render pass ensures there are no later crashes from
2261 // unbalanced save layers. Ideally, this method would return false and the
2262 // renderer could handle that by terminating dispatch.
2263 render_passes_.emplace_back(std::move(rendering_config));
2264 return nullptr;
2265 }
2266
2267 const std::shared_ptr<Texture>& input_texture =
2268 rendering_config.GetInlinePassContext()->GetTexture();
2269
2270 if (!input_texture) {
2271 VALIDATION_LOG << "Failed to fetch the color texture in order to "
2272 "apply an advanced blend or backdrop filter.";
2273
2274 // Note: see above.
2275 render_passes_.emplace_back(std::move(rendering_config));
2276 return nullptr;
2277 }
2278
2279 if (should_use_onscreen) {
2280 ColorAttachment color0 = render_target_.GetColorAttachment(0);
2281 // When MSAA is being used, we end up overriding the entire backdrop by
2282 // drawing the previous pass texture, and so we don't have to clear it and
2283 // can use kDontCare.
2284 color0.load_action = color0.resolve_texture != nullptr
2285 ? LoadAction::kDontCare
2286 : LoadAction::kLoad;
2287 render_target_.SetColorAttachment(color0, 0);
2288
2289 auto entity_pass_target = std::make_unique<EntityPassTarget>(
2290 render_target_, //
2291 renderer_.GetDeviceCapabilities().SupportsReadFromResolve(), //
2292 renderer_.GetDeviceCapabilities().SupportsImplicitResolvingMSAA() //
2293 );
2294 render_passes_.push_back(
2295 LazyRenderingConfig(renderer_, std::move(entity_pass_target)));
2296 requires_readback_ = false;
2297 } else {
2298 render_passes_.emplace_back(std::move(rendering_config));
2299 // If the current texture is being cached for a BDF we need to ensure we
2300 // don't recycle it during recording; remove it from the entity pass target.
2301 if (should_remove_texture) {
2302 render_passes_.back().GetEntityPassTarget()->RemoveSecondary();
2303 }
2304 }
2305 RenderPass& current_render_pass =
2306 *render_passes_.back().GetInlinePassContext()->GetRenderPass();
2307
2308 // Eagerly restore the BDF contents.
2309
2310 // If the pass context returns a backdrop texture, we need to draw it to the
2311 // current pass. We do this because it's faster and takes significantly less
2312 // memory than storing/loading large MSAA textures. Also, it's not possible
2313 // to blit the non-MSAA resolve texture of the previous pass to MSAA
2314 // textures (let alone a transient one).
2315 Rect size_rect = Rect::MakeSize(input_texture->GetSize());
2316 auto msaa_backdrop_contents = TextureContents::MakeRect(size_rect);
2317 msaa_backdrop_contents->SetStencilEnabled(false);
2318 msaa_backdrop_contents->SetLabel("MSAA backdrop");
2319 msaa_backdrop_contents->SetSourceRect(size_rect);
2320 msaa_backdrop_contents->SetTexture(input_texture);
2321
2322 Entity msaa_backdrop_entity;
2323 msaa_backdrop_entity.SetContents(std::move(msaa_backdrop_contents));
2324 msaa_backdrop_entity.SetBlendMode(BlendMode::kSrc);
2325 msaa_backdrop_entity.SetClipDepth(std::numeric_limits<uint32_t>::max());
2326 if (!msaa_backdrop_entity.Render(renderer_, current_render_pass)) {
2327 VALIDATION_LOG << "Failed to render MSAA backdrop entity.";
2328 return nullptr;
2329 }
2330
2331 // Restore any clips that were recorded before the backdrop filter was
2332 // applied.
2333 auto& replay_entities = clip_coverage_stack_.GetReplayEntities();
2334 uint64_t current_depth =
2335 post_depth_increment ? current_depth_ - 1 : current_depth_;
2336 for (const auto& replay : replay_entities) {
2337 if (replay.clip_depth <= current_depth) {
2338 continue;
2339 }
2340
2341 SetClipScissor(replay.clip_coverage, current_render_pass,
2342 global_pass_position);
2343 if (!replay.clip_contents.Render(renderer_, current_render_pass,
2344 replay.clip_depth)) {
2345 VALIDATION_LOG << "Failed to render entity for clip restore.";
2346 }
2347 }
2348
2349 return input_texture;
2350}
2351
2352bool Canvas::SupportsBlitToOnscreen() const {
2353 return renderer_.GetContext()
2354 ->GetCapabilities()
2355 ->SupportsTextureToTextureBlits() &&
2356 renderer_.GetContext()->GetBackendType() ==
2357 Context::BackendType::kMetal;
2358}
2359
2360bool Canvas::BlitToOnscreen(bool is_onscreen) {
2361 auto command_buffer = renderer_.GetContext()->CreateCommandBuffer();
2362 command_buffer->SetLabel("EntityPass Root Command Buffer");
2363 auto offscreen_target = render_passes_.back()
2364 .GetInlinePassContext()
2365 ->GetPassTarget()
2366 .GetRenderTarget();
2367 if (SupportsBlitToOnscreen()) {
2368 auto blit_pass = command_buffer->CreateBlitPass();
2369 blit_pass->AddCopy(offscreen_target.GetRenderTargetTexture(),
2370 render_target_.GetRenderTargetTexture());
2371 if (!blit_pass->EncodeCommands()) {
2372 VALIDATION_LOG << "Failed to encode root pass blit command.";
2373 return false;
2374 }
2375 } else {
2376 auto render_pass = command_buffer->CreateRenderPass(render_target_);
2377 render_pass->SetLabel("EntityPass Root Render Pass");
2378
2379 {
2380 auto size_rect = Rect::MakeSize(offscreen_target.GetRenderTargetSize());
2381 auto contents = TextureContents::MakeRect(size_rect);
2382 contents->SetTexture(offscreen_target.GetRenderTargetTexture());
2383 contents->SetSourceRect(size_rect);
2384 contents->SetLabel("Root pass blit");
2385
2386 Entity entity;
2387 entity.SetContents(contents);
2388 entity.SetBlendMode(BlendMode::kSrc);
2389
2390 if (!entity.Render(renderer_, *render_pass)) {
2391 VALIDATION_LOG << "Failed to render EntityPass root blit.";
2392 return false;
2393 }
2394 }
2395
2396 if (!render_pass->EncodeCommands()) {
2397 VALIDATION_LOG << "Failed to encode root pass command buffer.";
2398 return false;
2399 }
2400 }
2401
2402 if (is_onscreen) {
2403 return renderer_.GetContext()->SubmitOnscreen(std::move(command_buffer));
2404 } else {
2405 return renderer_.GetContext()->EnqueueCommandBuffer(
2406 std::move(command_buffer));
2407 }
2408}
2409
2410bool Canvas::EnsureFinalMipmapGeneration() const {
2411 if (!render_target_.GetRenderTargetTexture()->NeedsMipmapGeneration()) {
2412 return true;
2413 }
2414 std::shared_ptr<CommandBuffer> cmd_buffer =
2415 renderer_.GetContext()->CreateCommandBuffer();
2416 if (!cmd_buffer) {
2417 return false;
2418 }
2419 std::shared_ptr<BlitPass> blit_pass = cmd_buffer->CreateBlitPass();
2420 if (!blit_pass) {
2421 return false;
2422 }
2423 blit_pass->GenerateMipmap(render_target_.GetRenderTargetTexture());
2424 blit_pass->EncodeCommands();
2425 return renderer_.GetContext()->EnqueueCommandBuffer(std::move(cmd_buffer));
2426}
2427
2428void Canvas::EndReplay() {
2429 FML_DCHECK(render_passes_.size() == 1u);
2430 render_passes_.back().GetInlinePassContext()->GetRenderPass();
2431 render_passes_.back().GetInlinePassContext()->EndPass(
2432 /*is_onscreen=*/!requires_readback_ && is_onscreen_);
2433 backdrop_data_.clear();
2434
2435 // If requires_readback_ was true, then we rendered to an offscreen texture
2436 // instead of to the onscreen provided in the render target. Now we need to
2437 // draw or blit the offscreen back to the onscreen.
2438 if (requires_readback_) {
2439 BlitToOnscreen(/*is_onscreen_=*/is_onscreen_);
2440 }
2441 if (!EnsureFinalMipmapGeneration()) {
2442 VALIDATION_LOG << "Failed to generate onscreen mipmaps.";
2443 }
2444 if (!renderer_.GetContext()->FlushCommandBuffers()) {
2445 // Not much we can do.
2446 VALIDATION_LOG << "Failed to submit command buffers";
2447 }
2448 render_passes_.clear();
2449 renderer_.GetRenderTargetCache()->End();
2450 clip_geometry_.clear();
2451
2452 Reset();
2453 Initialize(initial_cull_rect_);
2454}
2455
2456bool Canvas::IsCompatibleWithSDFRendering(const Paint& paint) {
2457 if (!paint.anti_alias) {
2458 return false;
2459 }
2460 if (paint.mask_blur_descriptor.has_value()) {
2461 return false;
2462 }
2463 switch (paint.blend_mode) {
2464 // Incompatible blend modes:
2465 case BlendMode::kClear:
2466 case BlendMode::kSrc:
2467 case BlendMode::kSrcIn:
2468 case BlendMode::kDstIn:
2469 case BlendMode::kSrcOut:
2470 case BlendMode::kDstATop:
2471 case BlendMode::kPlus:
2472 case BlendMode::kModulate:
2473 return false;
2474 // Compatible blend modes:
2475 case BlendMode::kDst:
2476 case BlendMode::kSrcOver:
2477 case BlendMode::kDstOver:
2478 case BlendMode::kDstOut:
2479 case BlendMode::kSrcATop:
2480 case BlendMode::kXor:
2481 case BlendMode::kScreen:
2482 case BlendMode::kOverlay:
2483 case BlendMode::kDarken:
2484 case BlendMode::kLighten:
2485 case BlendMode::kColorDodge:
2486 case BlendMode::kColorBurn:
2487 case BlendMode::kHardLight:
2488 case BlendMode::kSoftLight:
2489 case BlendMode::kDifference:
2490 case BlendMode::kExclusion:
2491 case BlendMode::kMultiply:
2492 case BlendMode::kHue:
2493 case BlendMode::kSaturation:
2494 case BlendMode::kColor:
2495 case BlendMode::kLuminosity:
2496 return true;
2497 }
2498}
2499
2500LazyRenderingConfig::LazyRenderingConfig(
2501 ContentContext& renderer,
2502 std::unique_ptr<EntityPassTarget> p_entity_pass_target)
2503 : entity_pass_target_(std::move(p_entity_pass_target)) {
2504 inline_pass_context_ =
2505 std::make_unique<InlinePassContext>(renderer, *entity_pass_target_);
2506}
2507
2509 return !inline_pass_context_->IsActive();
2510}
2511
2513 return entity_pass_target_.get();
2514}
2515
2517 return inline_pass_context_.get();
2518}
2519
2520} // namespace impeller
virtual T type() const =0
virtual const DlBlendColorFilter * asBlend() const
virtual const DlMatrixColorFilter * asMatrix() const
virtual bool modifies_transparent_black() const =0
virtual const DlImageColorSource * asImage() const
DlImageSampling sampling() const
DlTileMode horizontal_tile_mode() const
sk_sp< const DlImage > image() const
void get_matrix(float matrix[20]) const
A Geometry that produces fillable vertices representing the stroked outline of an |Arc| object using ...
const Geometry & BuildDrawGeometry() override
Definition canvas.cc:284
PathBlurShape(const PathSource &source, std::shared_ptr< ShadowVertices > shadow_vertices, Sigma sigma)
Definition canvas.cc:263
Rect GetBounds() const override
Definition canvas.cc:270
std::shared_ptr< SolidBlurContents > BuildBlurContent(Sigma sigma) override
Definition canvas.cc:274
Rect GetBounds() const override
Definition canvas.cc:202
RRectBlurShape(const Rect &rect, Scalar corner_radius)
Definition canvas.cc:199
const Geometry & BuildDrawGeometry() override
Definition canvas.cc:211
std::shared_ptr< SolidBlurContents > BuildBlurContent(Sigma sigma) override
Definition canvas.cc:204
std::shared_ptr< SolidBlurContents > BuildBlurContent(Sigma sigma) override
Definition canvas.cc:229
const Geometry & BuildDrawGeometry() override
Definition canvas.cc:236
RSuperellipseBlurShape(const Rect &rect, Scalar corner_radius)
Definition canvas.cc:224
void ClipGeometry(const Geometry &geometry, Entity::ClipOperation clip_op, bool is_aa=true)
Definition canvas.cc:1120
static constexpr uint32_t kMaxDepth
Definition canvas.h:121
Canvas(ContentContext &renderer, const RenderTarget &render_target, bool is_onscreen, bool requires_readback)
Definition canvas.cc:297
void DrawRoundSuperellipse(const RoundSuperellipse &rse, const Paint &paint)
Definition canvas.cc:1026
std::optional< Rect > GetLocalCoverageLimit() const
Return the culling bounds of the current render target, or nullopt if there is no coverage.
Definition canvas.cc:1501
void SaveLayer(const Paint &paint, std::optional< Rect > bounds=std::nullopt, const flutter::DlImageFilter *backdrop_filter=nullptr, ContentBoundsPromise bounds_promise=ContentBoundsPromise::kUnknown, uint32_t total_content_depth=kMaxDepth, bool can_distribute_opacity=false, std::optional< int64_t > backdrop_id=std::nullopt)
Definition canvas.cc:1538
const Matrix & GetCurrentTransform() const
Definition canvas.cc:371
void DrawVertices(const std::shared_ptr< VerticesGeometry > &vertices, BlendMode blend_mode, const Paint &paint)
Definition canvas.cc:1301
void DrawOval(const Rect &rect, const Paint &paint)
Definition canvas.cc:856
void DrawImageRect(const std::shared_ptr< Texture > &image, Rect source, Rect dest, const Paint &paint, const SamplerDescriptor &sampler={}, SourceRectConstraint src_rect_constraint=SourceRectConstraint::kFast)
Definition canvas.cc:1234
void RestoreToCount(size_t count)
Definition canvas.cc:418
static bool IsCompatibleWithSDFRendering(const Paint &paint)
Definition canvas.cc:2456
size_t GetSaveCount() const
Definition canvas.cc:410
void Concat(const Matrix &transform)
Definition canvas.cc:355
void Transform(const Matrix &transform)
Definition canvas.cc:367
void DrawDashedLine(const Point &p0, const Point &p1, Scalar on_length, Scalar off_length, const Paint &paint)
Definition canvas.cc:799
void DrawDiffRoundRect(const RoundRect &outer, const RoundRect &inner, const Paint &paint)
Definition canvas.cc:1010
void DrawPath(const flutter::DlPath &path, const Paint &paint)
Definition canvas.cc:426
void PreConcat(const Matrix &transform)
Definition canvas.cc:359
void Rotate(Radians radians)
Definition canvas.cc:391
void DrawPoints(const Point points[], uint32_t count, Scalar radius, const Paint &paint, PointStyle point_style)
Definition canvas.cc:1202
void ResetTransform()
Definition canvas.cc:363
void DrawTextFrame(const std::shared_ptr< TextFrame > &text_frame, Point position, const Paint &paint)
Definition canvas.cc:1960
void DrawImage(const std::shared_ptr< Texture > &image, Point offset, const Paint &paint, const SamplerDescriptor &sampler={})
Definition canvas.cc:1220
void DrawPaint(const Paint &paint)
Definition canvas.cc:446
void DrawRoundRect(const RoundRect &rect, const Paint &paint)
Definition canvas.cc:963
void Skew(Scalar sx, Scalar sy)
Definition canvas.cc:387
void Scale(const Vector2 &scale)
Definition canvas.cc:379
void Save(uint32_t total_content_depth=kMaxDepth)
Definition canvas.cc:1484
void DrawRect(const Rect &rect, const Paint &paint)
Definition canvas.cc:825
void DrawAtlas(const std::shared_ptr< AtlasContents > &atlas_contents, const Paint &paint)
Definition canvas.cc:1416
void DrawLine(const Point &p0, const Point &p1, const Paint &paint, bool reuse_depth=false)
Definition canvas.cc:776
void Translate(const Vector3 &offset)
Definition canvas.cc:375
void DrawCircle(const Point &center, Scalar radius, const Paint &paint)
Definition canvas.cc:1083
void DrawArc(const Arc &arc, const Paint &paint)
Definition canvas.cc:911
virtual bool SupportsImplicitResolvingMSAA() const =0
Whether the context backend supports multisampled rendering to the on-screen surface without requirin...
virtual bool SupportsFramebufferFetch() const =0
Whether the context backend is able to support pipelines with shaders that read from the framebuffer ...
virtual bool SupportsReadFromResolve() const =0
Whether the context backend supports binding the current RenderPass attachments. This is supported if...
static std::unique_ptr< CircleContents > Make(std::unique_ptr< CircleGeometry > geometry, Color color, bool stroked)
void SetGeometry(GeometryResult geometry)
Set the pre-tessellated clip geometry.
void SetClipOperation(Entity::ClipOperation clip_op)
bool Render(const ContentContext &renderer, RenderPass &pass, uint32_t clip_depth) const
static std::shared_ptr< ColorFilterContents > MakeBlend(BlendMode blend_mode, FilterInput::Vector inputs, std::optional< Color > foreground_color=std::nullopt)
the [inputs] are expected to be in the order of dst, src.
const std::shared_ptr< RenderTargetAllocator > & GetRenderTargetCache() const
const Capabilities & GetDeviceCapabilities() const
TextShadowCache & GetTextShadowCache() const
Tessellator & GetTessellator() const
std::shared_ptr< Context > GetContext() const
A geometry that implements "drawPaint" like behavior by covering the entire render pass area.
A Geometry class that can directly generate vertices (with or without texture coordinates) for filled...
A PathSource object that provides path iteration for any ellipse inscribed within a Rect bounds.
Definition path_source.h:90
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition entity.cc:62
std::optional< Rect > GetCoverage() const
Definition entity.cc:66
const std::shared_ptr< Contents > & GetContents() const
Definition entity.cc:78
void SetClipDepth(uint32_t clip_depth)
Definition entity.cc:82
BlendMode GetBlendMode() const
Definition entity.cc:102
void SetContents(std::shared_ptr< Contents > contents)
Definition entity.cc:74
void SetBlendMode(BlendMode blend_mode)
Definition entity.cc:98
bool Render(const ContentContext &renderer, RenderPass &parent_pass) const
Definition entity.cc:145
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition entity.cc:46
static constexpr BlendMode kLastPipelineBlendMode
Definition entity.h:28
static bool IsBlendModeDestructive(BlendMode blend_mode)
Returns true if the blend mode is "destructive", meaning that even fully transparent source colors wo...
Definition entity.cc:128
A class that tracks all clips that have been recorded in the current entity pass stencil.
std::optional< Rect > CurrentClipCoverage() const
void PushSubpass(std::optional< Rect > subpass_coverage, size_t clip_height)
ClipStateResult RecordClip(const ClipContents &clip_contents, Matrix transform, Point global_pass_position, uint32_t clip_depth, size_t clip_height_floor, bool is_aa)
ClipStateResult RecordRestore(Point global_pass_position, size_t restore_height)
A Geometry that produces fillable vertices for the gap between a pair of |RoundRect| objects using th...
A Geometry that produces fillable vertices from a |DlPath| object using the |FillPathSourceGeometry| ...
A Geometry class that produces fillable vertices from any |RoundRect| object regardless of radii unif...
@ kNormal
Blurred inside and outside.
@ kOuter
Nothing inside, blurred outside.
@ kInner
Blurred inside, nothing outside.
@ kSolid
Solid inside, blurred outside.
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
std::vector< FilterInput::Ref > Vector
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
The coverage rectangle of this geometry, transformed by the transform argument.
static std::unique_ptr< Geometry > MakeRect(const Rect &rect)
Definition geometry.cc:83
virtual GeometryResult GetPositionBuffer(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
virtual bool IsAxisAlignedRect() const
Definition geometry.cc:140
bool EndPass(bool is_onscreen=false)
std::shared_ptr< Texture > GetTexture()
const std::shared_ptr< RenderPass > & GetRenderPass()
bool IsApplyingClearColor() const
Whether or not the clear color texture can still be updated.
Definition canvas.cc:2508
EntityPassTarget * GetEntityPassTarget() const
Definition canvas.cc:2512
InlinePassContext * GetInlinePassContext() const
Definition canvas.cc:2516
static std::unique_ptr< LineContents > Make(std::unique_ptr< LineGeometry > geometry, Color color)
A geometry class specialized for Canvas::DrawPoints.
ColorAttachment GetColorAttachment(size_t index) const
Get the color attachment at [index].
RenderTarget & SetColorAttachment(const ColorAttachment &attachment, size_t index)
ISize GetRenderTargetSize() const
const std::optional< DepthAttachment > & GetDepthAttachment() const
const std::optional< StencilAttachment > & GetStencilAttachment() const
void SetupDepthStencilAttachments(const Context &context, Allocator &allocator, ISize size, bool msaa, std::string_view label="Offscreen", RenderTarget::AttachmentConfig stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &depth_stencil_texture=nullptr)
A Geometry class that generates fillable vertices (with or without texture coordinates) directly from...
A Geometry class that generates fillable vertices (with or without texture coordinates) directly from...
static std::shared_ptr< ShadowVertices > MakeAmbientShadowVertices(Tessellator &tessellator, const PathSource &source, Scalar occluder_height, const Matrix &matrix)
static std::shared_ptr< ShadowVerticesContents > Make(const std::shared_ptr< ShadowVertices > &geometry)
A Geometry that produces fillable vertices representing the stroked outline of a |DlPath| object usin...
A Geometry that produces fillable vertices representing the stroked outline of a pair of nested |Roun...
A Geometry class that produces fillable vertices representing the stroked outline of an ellipse with ...
A Geometry that produces fillable vertices representing the stroked outline of a |DlPath| object usin...
A Geometry class that produces fillable vertices representing the stroked outline of any |Roundrect| ...
A Geometry class that produces fillable vertices representing the stroked outline of any |RoundSupere...
std::optional< Entity > Lookup(const ContentContext &renderer, const Entity &entity, const std::shared_ptr< FilterContents > &contents, const TextShadowCacheKey &)
Lookup the entity in the cache with the given filter/text contents, returning the new entity to rende...
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
static std::unique_ptr< UberSDFContents > Make(const UberSDFParameters &params, std::unique_ptr< Geometry > geometry)
const EmbeddedViewParams * params
FlutterVulkanImage * image
if(engine==nullptr)
uint32_t * target
#define FML_CHECK(condition)
Definition logging.h:104
#define FML_DCHECK(condition)
Definition logging.h:122
ISize subpass_size
The output size of the down-sampling pass.
size_t length
FlTexture * texture
impeller::SamplerDescriptor ToSamplerDescriptor(const flutter::DlImageSampling options)
Color ToColor(const flutter::DlColor &color)
static constexpr Scalar kMaxTextScale
Definition canvas.cc:1958
std::shared_ptr< ColorFilterContents > WrapWithGPUColorFilter(const flutter::DlColorFilter *filter, const std::shared_ptr< FilterInput > &input, ColorFilterContents::AbsorbOpacity absorb_opacity)
TRect< int32_t > IRect32
Definition rect.h:823
float Scalar
Definition scalar.h:19
SourceRectConstraint
Controls the behavior of the source rectangle given to DrawImageRect.
Definition canvas.h:75
@ kStrict
Sample only within the source rectangle. May be slower.
constexpr float kEhCloseEnough
Definition constants.h:57
std::shared_ptr< ColorFilterContents > WrapWithInvertColors(const std::shared_ptr< FilterInput > &input, ColorFilterContents::AbsorbOpacity absorb_opacity)
TRect< Scalar > Rect
Definition rect.h:822
PointStyle
Definition canvas.h:66
@ kRound
Points are drawn as squares.
TPoint< Scalar > Point
Definition point.h:426
ColorFilterProc GetCPUColorFilterProc(const flutter::DlColorFilter *filter)
std::shared_ptr< FilterContents > WrapInput(const ContentContext &renderer, const flutter::DlImageFilter *filter, const FilterInput::Ref &input)
Generate a new FilterContents using this filter's configuration.
BlendMode
Definition color.h:58
ContentBoundsPromise
Definition canvas.h:85
@ kMayClipContents
The caller claims the bounds are a subset of an estimate of the reasonably tight bounds but likely cl...
@ kContainsContents
The caller claims the bounds are a reasonably tight estimate of the coverage of the contents and shou...
TSize< Scalar > Size
Definition size.h:159
std::optional< Rect > ComputeSaveLayerCoverage(const Rect &content_coverage, const Matrix &effect_transform, const Rect &coverage_limit, const std::shared_ptr< FilterContents > &image_filter, bool flood_output_coverage, bool flood_input_coverage)
Compute the coverage of a subpass in the global coordinate space.
ISize64 ISize
Definition size.h:162
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition scalar.h:36
static const constexpr ColorMatrix kColorInversion
A color matrix which inverts colors.
Definition ref_ptr.h:261
constexpr bool IncludeCenter() const
Definition arc.h:110
constexpr bool IsFullCircle() const
Definition arc.h:114
constexpr Degrees GetSweep() const
Definition arc.h:108
constexpr Degrees GetStart() const
Definition arc.h:106
const Rect & GetOvalBounds() const
Return the bounds of the oval in which this arc is inscribed.
Definition arc.h:94
std::shared_ptr< Texture > texture
Definition formats.h:661
std::shared_ptr< Texture > texture_slot
Definition canvas.h:44
std::optional< Snapshot > shared_filter_snapshot
Definition canvas.h:47
Definition canvas.h:51
size_t clip_height
Definition canvas.h:54
bool did_round_out
Definition canvas.h:63
Entity::RenderingMode rendering_mode
Definition canvas.h:58
Matrix transform
Definition canvas.h:52
uint32_t clip_depth
Definition canvas.h:53
static constexpr Color BlackTransparent()
Definition color.h:275
static constexpr Color Khaki()
Definition color.h:523
Scalar alpha
Definition color.h:143
static constexpr Color White()
Definition color.h:269
constexpr Color WithAlpha(Scalar new_alpha) const
Definition color.h:283
Scalar array[20]
Definition color.h:118
A 4x4 matrix using column-major storage.
Definition matrix.h:37
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition matrix.h:95
Matrix Invert() const
Definition matrix.cc:99
static constexpr Matrix MakeSkew(Scalar sx, Scalar sy)
Definition matrix.h:127
static constexpr Matrix MakeTranslateScale(const Vector3 &s, const Vector3 &t)
Definition matrix.h:113
static Matrix MakeRotationZ(Radians r)
Definition matrix.h:223
static constexpr Matrix MakeScale(const Vector3 &s)
Definition matrix.h:104
Scalar GetMaxBasisLengthXY() const
Return the maximum scale applied specifically to either the X axis or Y axis unit vectors (the bases)...
Definition matrix.h:328
std::shared_ptr< FilterContents > WithImageFilter(const ContentContext &renderer, const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
Definition paint.cc:313
const flutter::DlColorFilter * color_filter
Definition paint.h:81
const flutter::DlColorSource * color_source
Definition paint.h:80
bool anti_alias
Definition paint.h:88
const flutter::DlImageFilter * image_filter
Definition paint.h:82
std::shared_ptr< Contents > WithFilters(const ContentContext &renderer, std::shared_ptr< Contents > input) const
Wrap this paint's configured filters to the given contents.
Definition paint.cc:277
Style style
Definition paint.h:85
bool invert_colors
Definition paint.h:87
static bool CanApplyOpacityPeephole(const Paint &paint)
Whether or not a save layer with the provided paint can perform the opacity peephole optimization.
Definition paint.h:42
std::optional< StrokeParameters > GetStroke() const
Return an optional StrokeParameters if this Paint is a stroked Paint, otherwise return a nullopt.
Definition paint.h:95
std::optional< MaskBlurDescriptor > mask_blur_descriptor
Definition paint.h:90
Color color
Definition paint.h:79
std::shared_ptr< ColorSourceContents > CreateContents(const ContentContext &renderer, const Geometry *geometry) const
Definition paint.cc:64
BlendMode blend_mode
Definition paint.h:86
StrokeParameters stroke
Definition paint.h:84
bool HasColorFilter() const
Whether this paint has a color filter that can apply opacity.
Definition paint.cc:471
Scalar radians
Definition scalar.h:45
constexpr const RoundingRadii & GetRadii() const
Definition round_rect.h:55
constexpr const Rect & GetBounds() const
Definition round_rect.h:53
constexpr const Rect & GetBounds() const
constexpr const RoundingRadii & GetRadii() const
static RoundSuperellipseParam MakeBoundsRadii(const Rect &bounds, const RoundingRadii &radii)
static constexpr RoundingRadii MakeRadii(Size radii)
constexpr bool AreAllCornersCircular() const
constexpr bool AreAllCornersSame(Scalar tolerance=kEhCloseEnough) const
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition sigma.h:32
Scalar sigma
Definition sigma.h:33
Represents a texture and its intended draw transform/sampler configuration.
Definition snapshot.h:24
Matrix transform
The transform that should be applied to this texture for rendering.
Definition snapshot.h:27
std::shared_ptr< Texture > texture
Definition snapshot.h:25
SamplerDescriptor sampler_descriptor
Definition snapshot.h:29
constexpr Type GetDistance(const TPoint &p) const
Definition point.h:201
constexpr auto GetBottom() const
Definition rect.h:391
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition rect.h:506
constexpr auto GetTop() const
Definition rect.h:387
constexpr std::optional< TRect > Intersection(const TRect &o) const
Definition rect.h:562
constexpr TSize< Type > GetSize() const
Returns the size of the rectangle which may be negative in either width or height and may have been c...
Definition rect.h:361
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition rect.h:381
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition rect.h:331
constexpr auto GetLeft() const
Definition rect.h:385
Round(const TRect< U > &r)
Definition rect.h:729
RoundOut(const TRect< U > &r)
Definition rect.h:713
static constexpr TRect MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
Definition rect.h:144
constexpr auto GetRight() const
Definition rect.h:389
constexpr bool IsSquare() const
Returns true if width and height are equal and neither is NaN.
Definition rect.h:338
static constexpr TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition rect.h:136
static constexpr TRect MakeCircleBounds(const TPoint< Type > &center, Type radius)
Definition rect.h:156
static constexpr TRect MakeSize(const TSize< U > &size)
Definition rect.h:150
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition rect.h:375
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:652
static constexpr TRect MakeMaximum()
Definition rect.h:212
constexpr Point GetCenter() const
Get the center point as a |Point|.
Definition rect.h:416
constexpr TRect< T > Shift(T dx, T dy) const
Returns a new rectangle translated by the given offset.
Definition rect.h:636
constexpr TPoint< Type > GetOrigin() const
Returns the upper left corner of the rectangle as specified by the left/top or x/y values when it was...
Definition rect.h:354
static constexpr TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition rect.h:129
Type width
Definition size.h:28
Parameters for rendering shapes using the UberSDF shader.
static UberSDFParameters MakeCircle(Color color, const Point &center, Scalar radius, std::optional< StrokeParameters > stroke)
Creates UberSDFParameters for a circle.
static UberSDFParameters MakeOval(Color color, const Rect &bounds, std::optional< StrokeParameters > stroke)
Creates UberSDFParameters for an Oval.
static UberSDFParameters MakeRoundedRect(Color color, const Rect &rect, const RoundingRadii &radii, std::optional< StrokeParameters > stroke)
Creates UberSDFParameters for a rounded rectangle.
static UberSDFParameters MakeRect(Color color, const Rect &rect, std::optional< StrokeParameters > stroke)
Creates UberSDFParameters for a rectangle.
static UberSDFParameters MakeRoundedSuperellipse(Color color, Rect bounds, Point superellipse_degree, Point superellipse_a, RoundingRadii radii, Point corner_angle_span, Point corner_circle_center_top, Point corner_circle_center_right, Scalar superellipse_c, Point superellipse_scale, std::optional< StrokeParameters > stroke)
Creates UberSDFParameters for a symmetric round superellipse.
std::vector< Point > points
#define TRACE_EVENT0(category_group, name)
#define VALIDATION_LOG
Definition validation.h:91