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