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