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