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