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