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/dl_color_source.h"
13 #include "display_list/effects/dl_image_filter.h"
14 #include "flutter/fml/logging.h"
15 #include "flutter/fml/trace_event.h"
17 #include "impeller/core/formats.h"
47 
48 namespace impeller {
49 
50 namespace {
51 
52 static bool UseColorSourceContents(
53  const std::shared_ptr<VerticesGeometry>& vertices,
54  const Paint& paint) {
55  // If there are no vertex color or texture coordinates. Or if there
56  // are vertex coordinates but its just a color.
57  if (vertices->HasVertexColors()) {
58  return false;
59  }
60  if (vertices->HasTextureCoordinates() && !paint.color_source) {
61  return true;
62  }
63  return !vertices->HasTextureCoordinates();
64 }
65 
66 static void SetClipScissor(std::optional<Rect> clip_coverage,
67  RenderPass& pass,
68  Point global_pass_position) {
69  // Set the scissor to the clip coverage area. We do this prior to rendering
70  // the clip itself and all its contents.
71  IRect scissor;
72  if (clip_coverage.has_value()) {
73  clip_coverage = clip_coverage->Shift(-global_pass_position);
74  scissor = IRect::RoundOut(clip_coverage.value());
75  // The scissor rect must not exceed the size of the render target.
76  scissor = scissor.Intersection(IRect::MakeSize(pass.GetRenderTargetSize()))
77  .value_or(IRect());
78  }
79  pass.SetScissor(scissor);
80 }
81 
82 static void ApplyFramebufferBlend(Entity& entity) {
83  auto src_contents = entity.GetContents();
84  auto contents = std::make_shared<FramebufferBlendContents>();
85  contents->SetChildContents(src_contents);
86  contents->SetBlendMode(entity.GetBlendMode());
87  entity.SetContents(std::move(contents));
88  entity.SetBlendMode(BlendMode::kSource);
89 }
90 
91 /// @brief Create the subpass restore contents, appling any filters or opacity
92 /// from the provided paint object.
93 static std::shared_ptr<Contents> CreateContentsForSubpassTarget(
94  const Paint& paint,
95  const std::shared_ptr<Texture>& target,
96  const Matrix& effect_transform) {
97  auto contents = TextureContents::MakeRect(Rect::MakeSize(target->GetSize()));
98  contents->SetTexture(target);
99  contents->SetLabel("Subpass");
100  contents->SetSourceRect(Rect::MakeSize(target->GetSize()));
101  contents->SetOpacity(paint.color.alpha);
102  contents->SetDeferApplyingOpacity(true);
103 
104  return paint.WithFiltersForSubpassTarget(std::move(contents),
105  effect_transform);
106 }
107 
108 static const constexpr RenderTarget::AttachmentConfig kDefaultStencilConfig =
109  RenderTarget::AttachmentConfig{
110  .storage_mode = StorageMode::kDeviceTransient,
111  .load_action = LoadAction::kDontCare,
112  .store_action = StoreAction::kDontCare,
113  };
114 
115 static std::unique_ptr<EntityPassTarget> CreateRenderTarget(
116  ContentContext& renderer,
117  ISize size,
118  const Color& clear_color) {
119  const std::shared_ptr<Context>& context = renderer.GetContext();
120 
121  /// All of the load/store actions are managed by `InlinePassContext` when
122  /// `RenderPasses` are created, so we just set them to `kDontCare` here.
123  /// What's important is the `StorageMode` of the textures, which cannot be
124  /// changed for the lifetime of the textures.
125 
126  RenderTarget target;
127  if (context->GetCapabilities()->SupportsOffscreenMSAA()) {
128  target = renderer.GetRenderTargetCache()->CreateOffscreenMSAA(
129  /*context=*/*context,
130  /*size=*/size,
131  /*mip_count=*/1,
132  /*label=*/"EntityPass",
133  /*color_attachment_config=*/
134  RenderTarget::AttachmentConfigMSAA{
135  .storage_mode = StorageMode::kDeviceTransient,
136  .resolve_storage_mode = StorageMode::kDevicePrivate,
137  .load_action = LoadAction::kDontCare,
138  .store_action = StoreAction::kMultisampleResolve,
139  .clear_color = clear_color},
140  /*stencil_attachment_config=*/kDefaultStencilConfig);
141  } else {
142  target = renderer.GetRenderTargetCache()->CreateOffscreen(
143  *context, // context
144  size, // size
145  /*mip_count=*/1,
146  "EntityPass", // label
147  RenderTarget::AttachmentConfig{
148  .storage_mode = StorageMode::kDevicePrivate,
149  .load_action = LoadAction::kDontCare,
150  .store_action = StoreAction::kDontCare,
151  .clear_color = clear_color,
152  }, // color_attachment_config
153  kDefaultStencilConfig //
154  );
155  }
156 
157  return std::make_unique<EntityPassTarget>(
158  target, renderer.GetDeviceCapabilities().SupportsReadFromResolve(),
159  renderer.GetDeviceCapabilities().SupportsImplicitResolvingMSAA());
160 }
161 
162 } // namespace
163 
165  const RenderTarget& render_target,
166  bool is_onscreen,
167  bool requires_readback)
168  : renderer_(renderer),
169  render_target_(render_target),
170  is_onscreen_(is_onscreen),
171  requires_readback_(requires_readback),
172  clip_coverage_stack_(EntityPassClipStack(
173  Rect::MakeSize(render_target.GetRenderTargetSize()))) {
174  Initialize(std::nullopt);
175  SetupRenderPass();
176 }
177 
179  const RenderTarget& render_target,
180  bool is_onscreen,
181  bool requires_readback,
182  Rect cull_rect)
183  : renderer_(renderer),
184  render_target_(render_target),
185  is_onscreen_(is_onscreen),
186  requires_readback_(requires_readback),
187  clip_coverage_stack_(EntityPassClipStack(
188  Rect::MakeSize(render_target.GetRenderTargetSize()))) {
189  Initialize(cull_rect);
190  SetupRenderPass();
191 }
192 
194  const RenderTarget& render_target,
195  bool is_onscreen,
196  bool requires_readback,
197  IRect cull_rect)
198  : renderer_(renderer),
199  render_target_(render_target),
200  is_onscreen_(is_onscreen),
201  requires_readback_(requires_readback),
202  clip_coverage_stack_(EntityPassClipStack(
203  Rect::MakeSize(render_target.GetRenderTargetSize()))) {
204  Initialize(Rect::MakeLTRB(cull_rect.GetLeft(), cull_rect.GetTop(),
205  cull_rect.GetRight(), cull_rect.GetBottom()));
206  SetupRenderPass();
207 }
208 
209 void Canvas::Initialize(std::optional<Rect> cull_rect) {
210  initial_cull_rect_ = cull_rect;
211  transform_stack_.emplace_back(CanvasStackEntry{
213  });
214  FML_DCHECK(GetSaveCount() == 1u);
215 }
216 
217 void Canvas::Reset() {
218  current_depth_ = 0u;
219  transform_stack_ = {};
220 }
221 
223  transform_stack_.back().transform = GetCurrentTransform() * transform;
224 }
225 
227  transform_stack_.back().transform = transform * GetCurrentTransform();
228 }
229 
231  transform_stack_.back().transform = {};
232 }
233 
235  Concat(transform);
236 }
237 
239  return transform_stack_.back().transform;
240 }
241 
244 }
245 
248 }
249 
252 }
253 
254 void Canvas::Skew(Scalar sx, Scalar sy) {
255  Concat(Matrix::MakeSkew(sx, sy));
256 }
257 
258 void Canvas::Rotate(Radians radians) {
259  Concat(Matrix::MakeRotationZ(radians));
260 }
261 
262 Point Canvas::GetGlobalPassPosition() const {
263  if (save_layer_state_.empty()) {
264  return Point(0, 0);
265  }
266  return save_layer_state_.back().coverage.GetOrigin();
267 }
268 
269 // clip depth of the previous save or 0.
270 size_t Canvas::GetClipHeightFloor() const {
271  if (transform_stack_.size() > 1) {
272  return transform_stack_[transform_stack_.size() - 2].clip_height;
273  }
274  return 0;
275 }
276 
277 size_t Canvas::GetSaveCount() const {
278  return transform_stack_.size();
279 }
280 
281 bool Canvas::IsSkipping() const {
282  return transform_stack_.back().skipping;
283 }
284 
285 void Canvas::RestoreToCount(size_t count) {
286  while (GetSaveCount() > count) {
287  if (!Restore()) {
288  return;
289  }
290  }
291 }
292 
293 void Canvas::DrawPath(const Path& path, const Paint& paint) {
294  Entity entity;
296  entity.SetBlendMode(paint.blend_mode);
297 
298  if (paint.style == Paint::Style::kFill) {
299  FillPathGeometry geom(path);
300  AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
301  } else {
302  StrokePathGeometry geom(path, paint.stroke_width, paint.stroke_miter,
303  paint.stroke_cap, paint.stroke_join);
304  AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
305  }
306 }
307 
308 void Canvas::DrawPaint(const Paint& paint) {
309  Entity entity;
311  entity.SetBlendMode(paint.blend_mode);
312 
313  CoverGeometry geom;
314  AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
315 }
316 
317 bool Canvas::AttemptDrawBlurredRRect(const Rect& rect,
318  Size corner_radii,
319  const Paint& paint) {
320  if (paint.style != Paint::Style::kFill) {
321  return false;
322  }
323 
324  if (paint.color_source) {
325  return false;
326  }
327 
328  if (!paint.mask_blur_descriptor.has_value()) {
329  return false;
330  }
331 
332  // A blur sigma that is not positive enough should not result in a blur.
333  if (paint.mask_blur_descriptor->sigma.sigma <= kEhCloseEnough) {
334  return false;
335  }
336 
337  // The current rrect blur math doesn't work on ovals.
338  if (fabsf(corner_radii.width - corner_radii.height) > kEhCloseEnough) {
339  return false;
340  }
341 
342  // For symmetrically mask blurred solid RRects, absorb the mask blur and use
343  // a faster SDF approximation.
344  Color rrect_color = paint.color;
345  if (paint.invert_colors) {
346  rrect_color = rrect_color.ApplyColorMatrix(kColorInversion);
347  }
348  if (paint.color_filter) {
349  rrect_color = GetCPUColorFilterProc(paint.color_filter)(rrect_color);
350  }
351 
352  Paint rrect_paint = {.mask_blur_descriptor = paint.mask_blur_descriptor};
353 
354  // In some cases, we need to render the mask blur to a separate layer.
355  //
356  // 1. If the blur style is normal, we'll be drawing using one draw call and
357  // no clips. And so we can just wrap the RRect contents with the
358  // ImageFilter, which will get applied to the result as per usual.
359  //
360  // 2. If the blur style is solid, we combine the non-blurred RRect with the
361  // blurred RRect via two separate draw calls, and so we need to defer any
362  // fancy blending, translucency, or image filtering until after these two
363  // draws have been combined in a separate layer.
364  //
365  // 3. If the blur style is outer or inner, we apply the blur style via a
366  // clip. The ImageFilter needs to be applied to the mask blurred result.
367  // And so if there's an ImageFilter, we need to defer applying it until
368  // after the clipped RRect blur has been drawn to a separate texture.
369  // However, since there's only one draw call that produces color, we
370  // don't need to worry about the blend mode or translucency (unlike with
371  // BlurStyle::kSolid).
372  //
373  if ((paint.mask_blur_descriptor->style !=
375  paint.image_filter) ||
377  (!rrect_color.IsOpaque() ||
378  paint.blend_mode != BlendMode::kSourceOver))) {
379  Rect render_bounds = rect;
380  if (paint.mask_blur_descriptor->style !=
382  render_bounds =
383  render_bounds.Expand(paint.mask_blur_descriptor->sigma.sigma * 4.0);
384  }
385  // Defer the alpha, blend mode, and image filter to a separate layer.
386  SaveLayer(
387  Paint{
388  .color = Color::White().WithAlpha(rrect_color.alpha),
389  .image_filter = paint.image_filter,
390  .blend_mode = paint.blend_mode,
391  },
392  render_bounds, nullptr, ContentBoundsPromise::kContainsContents, 1u);
393  rrect_paint.color = rrect_color.WithAlpha(1);
394  } else {
395  rrect_paint.color = rrect_color;
396  rrect_paint.blend_mode = paint.blend_mode;
397  rrect_paint.image_filter = paint.image_filter;
398  Save(1u);
399  }
400 
401  auto draw_blurred_rrect = [this, &rect, &corner_radii, &rrect_paint]() {
402  auto contents = std::make_shared<SolidRRectBlurContents>();
403 
404  contents->SetColor(rrect_paint.color);
405  contents->SetSigma(rrect_paint.mask_blur_descriptor->sigma);
406  contents->SetRRect(rect, corner_radii);
407 
408  Entity blurred_rrect_entity;
409  blurred_rrect_entity.SetTransform(GetCurrentTransform());
410  blurred_rrect_entity.SetBlendMode(rrect_paint.blend_mode);
411 
412  rrect_paint.mask_blur_descriptor = std::nullopt;
413  blurred_rrect_entity.SetContents(
414  rrect_paint.WithFilters(std::move(contents)));
415  AddRenderEntityToCurrentPass(blurred_rrect_entity);
416  };
417 
418  switch (rrect_paint.mask_blur_descriptor->style) {
420  draw_blurred_rrect();
421  break;
422  }
424  // First, draw the blurred RRect.
425  draw_blurred_rrect();
426  // Then, draw the non-blurred RRect on top.
427  Entity entity;
428  entity.SetTransform(GetCurrentTransform());
429  entity.SetBlendMode(rrect_paint.blend_mode);
430 
431  RoundRectGeometry geom(rect, corner_radii);
432  AddRenderEntityWithFiltersToCurrentPass(entity, &geom, rrect_paint,
433  /*reuse_depth=*/true);
434  break;
435  }
437  RoundRectGeometry geom(rect, corner_radii);
439  draw_blurred_rrect();
440  break;
441  }
443  RoundRectGeometry geom(rect, corner_radii);
445  draw_blurred_rrect();
446  break;
447  }
448  }
449 
450  Restore();
451 
452  return true;
453 }
454 
455 void Canvas::DrawLine(const Point& p0,
456  const Point& p1,
457  const Paint& paint,
458  bool reuse_depth) {
459  Entity entity;
461  entity.SetBlendMode(paint.blend_mode);
462 
463  LineGeometry geom(p0, p1, paint.stroke_width, paint.stroke_cap);
464  AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint,
465  /*reuse_depth=*/reuse_depth);
466 }
467 
468 void Canvas::DrawRect(const Rect& rect, const Paint& paint) {
469  if (paint.style == Paint::Style::kStroke) {
470  DrawPath(PathBuilder{}.AddRect(rect).TakePath(), paint);
471  return;
472  }
473 
474  if (AttemptDrawBlurredRRect(rect, {}, paint)) {
475  return;
476  }
477 
478  Entity entity;
480  entity.SetBlendMode(paint.blend_mode);
481 
482  RectGeometry geom(rect);
483  AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
484 }
485 
486 void Canvas::DrawOval(const Rect& rect, const Paint& paint) {
487  // TODO(jonahwilliams): This additional condition avoids an assert in the
488  // stroke circle geometry generator. I need to verify the condition that this
489  // assert prevents.
490  if (rect.IsSquare() && (paint.style == Paint::Style::kFill ||
491  (paint.style == Paint::Style::kStroke &&
492  paint.stroke_width < rect.GetWidth()))) {
493  // Circles have slightly less overhead and can do stroking
494  DrawCircle(rect.GetCenter(), rect.GetWidth() * 0.5f, paint);
495  return;
496  }
497 
498  if (paint.style == Paint::Style::kStroke) {
499  // No stroked ellipses yet
500  DrawPath(PathBuilder{}.AddOval(rect).TakePath(), paint);
501  return;
502  }
503 
504  if (AttemptDrawBlurredRRect(rect, rect.GetSize() * 0.5f, paint)) {
505  return;
506  }
507 
508  Entity entity;
510  entity.SetBlendMode(paint.blend_mode);
511 
512  EllipseGeometry geom(rect);
513  AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
514 }
515 
516 void Canvas::DrawRoundRect(const RoundRect& round_rect, const Paint& paint) {
517  auto& rect = round_rect.GetBounds();
518  auto& radii = round_rect.GetRadii();
519  if (radii.AreAllCornersSame()) {
520  if (AttemptDrawBlurredRRect(rect, radii.top_left, paint)) {
521  return;
522  }
523 
524  if (paint.style == Paint::Style::kFill) {
525  Entity entity;
527  entity.SetBlendMode(paint.blend_mode);
528 
529  RoundRectGeometry geom(rect, radii.top_left);
530  AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
531  return;
532  }
533  }
534 
535  auto path = PathBuilder{}
537  .AddRoundRect(round_rect)
538  .SetBounds(rect)
539  .TakePath();
540  DrawPath(path, paint);
541 }
542 
543 void Canvas::DrawCircle(const Point& center,
544  Scalar radius,
545  const Paint& paint) {
546  Size half_size(radius, radius);
547  if (AttemptDrawBlurredRRect(
548  Rect::MakeOriginSize(center - half_size, half_size * 2),
549  {radius, radius}, paint)) {
550  return;
551  }
552 
553  Entity entity;
555  entity.SetBlendMode(paint.blend_mode);
556 
557  if (paint.style == Paint::Style::kStroke) {
558  CircleGeometry geom(center, radius, paint.stroke_width);
559  AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
560  } else {
561  CircleGeometry geom(center, radius);
562  AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
563  }
564 }
565 
566 void Canvas::ClipGeometry(const Geometry& geometry,
567  Entity::ClipOperation clip_op,
568  bool is_aa) {
569  if (IsSkipping()) {
570  return;
571  }
572 
573  // Ideally the clip depth would be greater than the current rendering
574  // depth because any rendering calls that follow this clip operation will
575  // pre-increment the depth and then be rendering above our clip depth,
576  // but that case will be caught by the CHECK in AddRenderEntity above.
577  // In practice we sometimes have a clip set with no rendering after it
578  // and in such cases the current depth will equal the clip depth.
579  // Eventually the DisplayList should optimize these out, but it is hard
580  // to know if a clip will actually be used in advance of storing it in
581  // the DisplayList buffer.
582  // See https://github.com/flutter/flutter/issues/147021
583  FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
584  << current_depth_ << " <=? " << transform_stack_.back().clip_depth;
585  uint32_t clip_depth = transform_stack_.back().clip_depth;
586 
587  const Matrix clip_transform =
588  Matrix::MakeTranslation(Vector3(-GetGlobalPassPosition())) *
590 
591  std::optional<Rect> clip_coverage = geometry.GetCoverage(clip_transform);
592  if (!clip_coverage.has_value()) {
593  return;
594  }
595 
596  ClipContents clip_contents(
597  clip_coverage.value(),
598  /*is_axis_aligned_rect=*/geometry.IsAxisAlignedRect() &&
599  GetCurrentTransform().IsTranslationScaleOnly());
600  clip_contents.SetClipOperation(clip_op);
601 
602  EntityPassClipStack::ClipStateResult clip_state_result =
603  clip_coverage_stack_.RecordClip(
604  clip_contents, //
605  /*transform=*/clip_transform, //
606  /*global_pass_position=*/GetGlobalPassPosition(), //
607  /*clip_depth=*/clip_depth, //
608  /*clip_height_floor=*/GetClipHeightFloor(), //
609  /*is_aa=*/is_aa);
610 
611  if (clip_state_result.clip_did_change) {
612  // We only need to update the pass scissor if the clip state has changed.
613  SetClipScissor(clip_coverage_stack_.CurrentClipCoverage(),
614  *render_passes_.back().inline_pass_context->GetRenderPass(),
615  GetGlobalPassPosition());
616  }
617 
618  ++transform_stack_.back().clip_height;
619  ++transform_stack_.back().num_clips;
620 
621  if (!clip_state_result.should_render) {
622  return;
623  }
624 
625  // Note: this is a bit of a hack. Its not possible to construct a geometry
626  // result without begninning the render pass. We should refactor the geometry
627  // objects so that they only need a reference to the render pass size and/or
628  // orthographic transform.
629  Entity entity;
630  entity.SetTransform(clip_transform);
631  entity.SetClipDepth(clip_depth);
632 
633  GeometryResult geometry_result = geometry.GetPositionBuffer(
634  renderer_, //
635  entity, //
636  *render_passes_.back().inline_pass_context->GetRenderPass() //
637  );
638  clip_contents.SetGeometry(geometry_result);
639  clip_coverage_stack_.GetLastReplayResult().clip_contents.SetGeometry(
640  geometry_result);
641 
642  clip_contents.Render(
643  renderer_, *render_passes_.back().inline_pass_context->GetRenderPass(),
644  clip_depth);
645 }
646 
647 void Canvas::DrawPoints(const Point points[],
648  uint32_t count,
649  Scalar radius,
650  const Paint& paint,
651  PointStyle point_style) {
652  if (radius <= 0) {
653  return;
654  }
655 
656  Entity entity;
658  entity.SetBlendMode(paint.blend_mode);
659 
660  PointFieldGeometry geom(points, count, radius,
661  /*round=*/point_style == PointStyle::kRound);
662  AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
663 }
664 
665 void Canvas::DrawImage(const std::shared_ptr<Texture>& image,
666  Point offset,
667  const Paint& paint,
668  const SamplerDescriptor& sampler) {
669  if (!image) {
670  return;
671  }
672 
673  const auto source = Rect::MakeSize(image->GetSize());
674  const auto dest = source.Shift(offset);
675 
676  DrawImageRect(image, source, dest, paint, sampler);
677 }
678 
679 void Canvas::DrawImageRect(const std::shared_ptr<Texture>& image,
680  Rect source,
681  Rect dest,
682  const Paint& paint,
683  const SamplerDescriptor& sampler,
684  SourceRectConstraint src_rect_constraint) {
685  if (!image || source.IsEmpty() || dest.IsEmpty()) {
686  return;
687  }
688 
689  auto size = image->GetSize();
690 
691  if (size.IsEmpty()) {
692  return;
693  }
694 
695  std::optional<Rect> clipped_source =
696  source.Intersection(Rect::MakeSize(size));
697  if (!clipped_source) {
698  return;
699  }
700  if (*clipped_source != source) {
701  Scalar sx = dest.GetWidth() / source.GetWidth();
702  Scalar sy = dest.GetHeight() / source.GetHeight();
703  Scalar tx = dest.GetLeft() - source.GetLeft() * sx;
704  Scalar ty = dest.GetTop() - source.GetTop() * sy;
705  Matrix src_to_dest = Matrix::MakeTranslateScale({sx, sy, 1}, {tx, ty, 0});
706  dest = clipped_source->TransformBounds(src_to_dest);
707  }
708 
709  auto texture_contents = TextureContents::MakeRect(dest);
710  texture_contents->SetTexture(image);
711  texture_contents->SetSourceRect(*clipped_source);
712  texture_contents->SetStrictSourceRect(src_rect_constraint ==
714  texture_contents->SetSamplerDescriptor(sampler);
715  texture_contents->SetOpacity(paint.color.alpha);
716  texture_contents->SetDeferApplyingOpacity(paint.HasColorFilter());
717 
718  Entity entity;
719  entity.SetBlendMode(paint.blend_mode);
721 
722  if (!paint.mask_blur_descriptor.has_value()) {
723  entity.SetContents(paint.WithFilters(std::move(texture_contents)));
724  AddRenderEntityToCurrentPass(entity);
725  return;
726  }
727 
728  RectGeometry out_rect(Rect{});
729 
730  entity.SetContents(paint.WithFilters(
731  paint.mask_blur_descriptor->CreateMaskBlur(texture_contents, &out_rect)));
732  AddRenderEntityToCurrentPass(entity);
733 }
734 
735 size_t Canvas::GetClipHeight() const {
736  return transform_stack_.back().clip_height;
737 }
738 
739 void Canvas::DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
740  BlendMode blend_mode,
741  const Paint& paint) {
742  // Override the blend mode with kDestination in order to match the behavior
743  // of Skia's SK_LEGACY_IGNORE_DRAW_VERTICES_BLEND_WITH_NO_SHADER flag, which
744  // is enabled when the Flutter engine builds Skia.
745  if (!paint.color_source) {
746  blend_mode = BlendMode::kDestination;
747  }
748 
749  Entity entity;
751  entity.SetBlendMode(paint.blend_mode);
752 
753  // If there are no vertex colors.
754  if (UseColorSourceContents(vertices, paint)) {
755  AddRenderEntityWithFiltersToCurrentPass(entity, vertices.get(), paint);
756  return;
757  }
758 
759  // If the blend mode is destination don't bother to bind or create a texture.
760  if (blend_mode == BlendMode::kDestination) {
761  auto contents = std::make_shared<VerticesSimpleBlendContents>();
762  contents->SetBlendMode(blend_mode);
763  contents->SetAlpha(paint.color.alpha);
764  contents->SetGeometry(vertices);
765  entity.SetContents(paint.WithFilters(std::move(contents)));
766  AddRenderEntityToCurrentPass(entity);
767  return;
768  }
769 
770  // If there is a texture, use this directly. Otherwise render the color
771  // source to a texture.
772  if (paint.color_source &&
773  paint.color_source->type() == flutter::DlColorSourceType::kImage) {
774  const flutter::DlImageColorSource* image_color_source =
775  paint.color_source->asImage();
776  FML_DCHECK(image_color_source &&
777  image_color_source->image()->impeller_texture());
778  auto texture = image_color_source->image()->impeller_texture();
779  auto x_tile_mode = static_cast<Entity::TileMode>(
780  image_color_source->horizontal_tile_mode());
781  auto y_tile_mode =
782  static_cast<Entity::TileMode>(image_color_source->vertical_tile_mode());
783  auto sampler_descriptor =
784  skia_conversions::ToSamplerDescriptor(image_color_source->sampling());
785  auto effect_transform = image_color_source->matrix();
786 
787  auto contents = std::make_shared<VerticesSimpleBlendContents>();
788  contents->SetBlendMode(blend_mode);
789  contents->SetAlpha(paint.color.alpha);
790  contents->SetGeometry(vertices);
791  contents->SetEffectTransform(effect_transform);
792  contents->SetTexture(texture);
793  contents->SetTileMode(x_tile_mode, y_tile_mode);
794  contents->SetSamplerDescriptor(sampler_descriptor);
795 
796  entity.SetContents(paint.WithFilters(std::move(contents)));
797  AddRenderEntityToCurrentPass(entity);
798  return;
799  }
800 
801  auto src_paint = paint;
802  src_paint.color = paint.color.WithAlpha(1.0);
803 
804  std::shared_ptr<ColorSourceContents> src_contents =
805  src_paint.CreateContents();
806  src_contents->SetGeometry(vertices.get());
807 
808  // If the color source has an intrinsic size, then we use that to
809  // create the src contents as a simplification. Otherwise we use
810  // the extent of the texture coordinates to determine how large
811  // the src contents should be. If neither has a value we fall back
812  // to using the geometry coverage data.
813  Rect src_coverage;
814  auto size = src_contents->GetColorSourceSize();
815  if (size.has_value()) {
816  src_coverage = Rect::MakeXYWH(0, 0, size->width, size->height);
817  } else {
818  auto cvg = vertices->GetCoverage(Matrix{});
819  FML_CHECK(cvg.has_value());
820  auto texture_coverage = vertices->GetTextureCoordinateCoverge();
821  if (texture_coverage.has_value()) {
822  src_coverage =
823  Rect::MakeOriginSize(texture_coverage->GetOrigin(),
824  texture_coverage->GetSize().Max({1, 1}));
825  } else {
826  src_coverage = cvg.value();
827  }
828  }
829  src_contents = src_paint.CreateContents();
830 
831  clip_geometry_.push_back(Geometry::MakeRect(Rect::Round(src_coverage)));
832  src_contents->SetGeometry(clip_geometry_.back().get());
833 
834  auto contents = std::make_shared<VerticesSimpleBlendContents>();
835  contents->SetBlendMode(blend_mode);
836  contents->SetAlpha(paint.color.alpha);
837  contents->SetGeometry(vertices);
838  contents->SetLazyTextureCoverage(src_coverage);
839  contents->SetLazyTexture([src_contents,
840  src_coverage](const ContentContext& renderer) {
841  // Applying the src coverage as the coverage limit prevents the 1px
842  // coverage pad from adding a border that is picked up by developer
843  // specified UVs.
844  auto snapshot =
845  src_contents->RenderToSnapshot(renderer, {}, Rect::Round(src_coverage));
846  return snapshot.has_value() ? snapshot->texture : nullptr;
847  });
848  entity.SetContents(paint.WithFilters(std::move(contents)));
849  AddRenderEntityToCurrentPass(entity);
850 }
851 
852 void Canvas::DrawAtlas(const std::shared_ptr<AtlasContents>& atlas_contents,
853  const Paint& paint) {
854  atlas_contents->SetAlpha(paint.color.alpha);
855 
856  Entity entity;
858  entity.SetBlendMode(paint.blend_mode);
859  entity.SetContents(paint.WithFilters(atlas_contents));
860 
861  AddRenderEntityToCurrentPass(entity);
862 }
863 
864 /// Compositor Functionality
865 /////////////////////////////////////////
866 
867 void Canvas::SetupRenderPass() {
868  renderer_.GetRenderTargetCache()->Start();
869  ColorAttachment color0 = render_target_.GetColorAttachment(0);
870 
871  auto& stencil_attachment = render_target_.GetStencilAttachment();
872  auto& depth_attachment = render_target_.GetDepthAttachment();
873  if (!stencil_attachment.has_value() || !depth_attachment.has_value()) {
874  // Setup a new root stencil with an optimal configuration if one wasn't
875  // provided by the caller.
876  render_target_.SetupDepthStencilAttachments(
877  *renderer_.GetContext(),
878  *renderer_.GetContext()->GetResourceAllocator(),
879  color0.texture->GetSize(),
880  renderer_.GetContext()->GetCapabilities()->SupportsOffscreenMSAA(),
881  "ImpellerOnscreen", kDefaultStencilConfig);
882  }
883 
884  // Set up the clear color of the root pass.
886  render_target_.SetColorAttachment(color0, 0);
887 
888  // If requires_readback is true, then there is a backdrop filter or emulated
889  // advanced blend in the first save layer. This requires a readback, which
890  // isn't supported by onscreen textures. To support this, we immediately begin
891  // a second save layer with the same dimensions as the onscreen. When
892  // rendering is completed, we must blit this saveLayer to the onscreen.
893  if (requires_readback_) {
894  auto entity_pass_target =
895  CreateRenderTarget(renderer_, //
896  color0.texture->GetSize(), //
897  /*clear_color=*/Color::BlackTransparent());
898  render_passes_.push_back(
899  LazyRenderingConfig(renderer_, std::move(entity_pass_target)));
900  } else {
901  auto entity_pass_target = std::make_unique<EntityPassTarget>(
902  render_target_, //
905  );
906  render_passes_.push_back(
907  LazyRenderingConfig(renderer_, std::move(entity_pass_target)));
908  }
909 }
910 
911 void Canvas::SkipUntilMatchingRestore(size_t total_content_depth) {
912  auto entry = CanvasStackEntry{};
913  entry.skipping = true;
914  entry.clip_depth = current_depth_ + total_content_depth;
915  transform_stack_.push_back(entry);
916 }
917 
918 void Canvas::Save(uint32_t total_content_depth) {
919  if (IsSkipping()) {
920  return SkipUntilMatchingRestore(total_content_depth);
921  }
922 
923  auto entry = CanvasStackEntry{};
924  entry.transform = transform_stack_.back().transform;
925  entry.clip_depth = current_depth_ + total_content_depth;
926  entry.distributed_opacity = transform_stack_.back().distributed_opacity;
927  FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
928  << entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
929  << " after allocating " << total_content_depth;
930  entry.clip_height = transform_stack_.back().clip_height;
931  entry.rendering_mode = Entity::RenderingMode::kDirect;
932  transform_stack_.push_back(entry);
933 }
934 
935 std::optional<Rect> Canvas::GetLocalCoverageLimit() const {
936  if (!clip_coverage_stack_.HasCoverage()) {
937  // The current clip is empty. This means the pass texture won't be
938  // visible, so skip it.
939  return std::nullopt;
940  }
941 
942  auto maybe_current_clip_coverage = clip_coverage_stack_.CurrentClipCoverage();
943  if (!maybe_current_clip_coverage.has_value()) {
944  return std::nullopt;
945  }
946 
947  auto current_clip_coverage = maybe_current_clip_coverage.value();
948 
949  // The maximum coverage of the subpass. Subpasses textures should never
950  // extend outside the parent pass texture or the current clip coverage.
951  std::optional<Rect> maybe_coverage_limit =
952  Rect::MakeOriginSize(GetGlobalPassPosition(),
953  Size(render_passes_.back()
954  .inline_pass_context->GetTexture()
955  ->GetSize()))
956  .Intersection(current_clip_coverage);
957 
958  if (!maybe_coverage_limit.has_value() || maybe_coverage_limit->IsEmpty()) {
959  return std::nullopt;
960  }
961 
962  return maybe_coverage_limit->Intersection(
963  Rect::MakeSize(render_target_.GetRenderTargetSize()));
964 }
965 
966 void Canvas::SaveLayer(const Paint& paint,
967  std::optional<Rect> bounds,
968  const flutter::DlImageFilter* backdrop_filter,
969  ContentBoundsPromise bounds_promise,
970  uint32_t total_content_depth,
971  bool can_distribute_opacity,
972  std::optional<int64_t> backdrop_id) {
973  TRACE_EVENT0("flutter", "Canvas::saveLayer");
974  if (IsSkipping()) {
975  return SkipUntilMatchingRestore(total_content_depth);
976  }
977 
978  auto maybe_coverage_limit = GetLocalCoverageLimit();
979  if (!maybe_coverage_limit.has_value()) {
980  return SkipUntilMatchingRestore(total_content_depth);
981  }
982  auto coverage_limit = maybe_coverage_limit.value();
983 
984  if (can_distribute_opacity && !backdrop_filter &&
986  bounds_promise != ContentBoundsPromise::kMayClipContents) {
987  Save(total_content_depth);
988  transform_stack_.back().distributed_opacity *= paint.color.alpha;
989  return;
990  }
991 
992  std::shared_ptr<FilterContents> filter_contents = paint.WithImageFilter(
993  Rect(), transform_stack_.back().transform,
995 
996  std::optional<Rect> maybe_subpass_coverage = ComputeSaveLayerCoverage(
997  bounds.value_or(Rect::MakeMaximum()),
998  transform_stack_.back().transform, //
999  coverage_limit, //
1000  filter_contents, //
1001  /*flood_output_coverage=*/
1003  /*flood_input_coverage=*/!!backdrop_filter ||
1004  (paint.color_filter &&
1005  paint.color_filter->modifies_transparent_black()) //
1006  );
1007 
1008  if (!maybe_subpass_coverage.has_value()) {
1009  return SkipUntilMatchingRestore(total_content_depth);
1010  }
1011 
1012  auto subpass_coverage = maybe_subpass_coverage.value();
1013 
1014  // When an image filter is present, clamp to avoid flicking due to nearest
1015  // sampled image. For other cases, round out to ensure than any geometry is
1016  // not cut off.
1017  //
1018  // See also this bug: https://github.com/flutter/flutter/issues/144213
1019  //
1020  // TODO(jonahwilliams): this could still round out for filters that use decal
1021  // sampling mode.
1023  bool did_round_out = false;
1024  Point coverage_origin_adjustment = Point{0, 0};
1025  if (paint.image_filter) {
1026  subpass_size = ISize(subpass_coverage.GetSize());
1027  } else {
1028  did_round_out = true;
1029  subpass_size =
1030  static_cast<ISize>(IRect::RoundOut(subpass_coverage).GetSize());
1031  // If rounding out, adjust the coverage to account for the subpixel shift.
1032  coverage_origin_adjustment =
1033  Point(subpass_coverage.GetLeftTop().x -
1034  std::floor(subpass_coverage.GetLeftTop().x),
1035  subpass_coverage.GetLeftTop().y -
1036  std::floor(subpass_coverage.GetLeftTop().y));
1037  }
1038  if (subpass_size.IsEmpty()) {
1039  return SkipUntilMatchingRestore(total_content_depth);
1040  }
1041 
1042  // When there are scaling filters present, these contents may exceed the
1043  // maximum texture size. Perform a clamp here, which may cause rendering
1044  // artifacts.
1045  subpass_size = subpass_size.Min(renderer_.GetContext()
1046  ->GetCapabilities()
1047  ->GetMaximumRenderPassAttachmentSize());
1048 
1049  // Backdrop filter state, ignored if there is no BDF.
1050  std::shared_ptr<FilterContents> backdrop_filter_contents;
1051  Point local_position = Point(0, 0);
1052  if (backdrop_filter) {
1053  local_position = subpass_coverage.GetOrigin() - GetGlobalPassPosition();
1054  Canvas::BackdropFilterProc backdrop_filter_proc =
1055  [backdrop_filter = backdrop_filter](
1056  const FilterInput::Ref& input, const Matrix& effect_transform,
1057  Entity::RenderingMode rendering_mode) {
1058  auto filter = WrapInput(backdrop_filter, input);
1059  filter->SetEffectTransform(effect_transform);
1060  filter->SetRenderingMode(rendering_mode);
1061  return filter;
1062  };
1063 
1064  std::shared_ptr<Texture> input_texture;
1065 
1066  // If the backdrop ID is not nullopt and there is more than one usage
1067  // of it in the current scene, cache the backdrop texture and remove it from
1068  // the current entity pass flip.
1069  bool will_cache_backdrop_texture = false;
1070  BackdropData* backdrop_data = nullptr;
1071  // If we've reached this point, there is at least one backdrop filter. But
1072  // potentially more if there is a backdrop id. We may conditionally set this
1073  // to a higher value in the if block below.
1074  size_t backdrop_count = 1;
1075  if (backdrop_id.has_value()) {
1076  std::unordered_map<int64_t, BackdropData>::iterator backdrop_data_it =
1077  backdrop_data_.find(backdrop_id.value());
1078  if (backdrop_data_it != backdrop_data_.end()) {
1079  backdrop_data = &backdrop_data_it->second;
1080  will_cache_backdrop_texture =
1081  backdrop_data_it->second.backdrop_count > 1;
1082  backdrop_count = backdrop_data_it->second.backdrop_count;
1083  }
1084  }
1085 
1086  if (!will_cache_backdrop_texture || !backdrop_data->texture_slot) {
1087  backdrop_count_ -= backdrop_count;
1088 
1089  // The onscreen texture can be flipped to if:
1090  // 1. The device supports framebuffer fetch
1091  // 2. There are no more backdrop filters
1092  // 3. The current render pass is for the onscreen pass.
1093  const bool should_use_onscreen =
1095  backdrop_count_ == 0 && render_passes_.size() == 1u;
1096  input_texture = FlipBackdrop(
1097  GetGlobalPassPosition(), //
1098  /*should_remove_texture=*/will_cache_backdrop_texture, //
1099  /*should_use_onscreen=*/should_use_onscreen //
1100  );
1101  if (!input_texture) {
1102  // Validation failures are logged in FlipBackdrop.
1103  return;
1104  }
1105 
1106  if (will_cache_backdrop_texture) {
1107  backdrop_data->texture_slot = input_texture;
1108  }
1109  } else {
1110  input_texture = backdrop_data->texture_slot;
1111  }
1112 
1113  backdrop_filter_contents = backdrop_filter_proc(
1114  FilterInput::Make(std::move(input_texture)),
1115  transform_stack_.back().transform.Basis(),
1116  // When the subpass has a translation that means the math with
1117  // the snapshot has to be different.
1118  transform_stack_.back().transform.HasTranslation()
1121 
1122  if (will_cache_backdrop_texture) {
1123  FML_DCHECK(backdrop_data);
1124  // If all filters on the shared backdrop layer are equal, process the
1125  // layer once.
1126  if (backdrop_data->all_filters_equal &&
1127  !backdrop_data->shared_filter_snapshot.has_value()) {
1128  // TODO(157110): compute minimum input hint.
1129  backdrop_data->shared_filter_snapshot =
1130  backdrop_filter_contents->RenderToSnapshot(renderer_, {});
1131  }
1132 
1133  std::optional<Snapshot> maybe_snapshot =
1134  backdrop_data->shared_filter_snapshot;
1135  if (maybe_snapshot.has_value()) {
1136  Snapshot snapshot = maybe_snapshot.value();
1137  std::shared_ptr<TextureContents> contents = TextureContents::MakeRect(
1138  subpass_coverage.Shift(-GetGlobalPassPosition()));
1139  auto scaled =
1140  subpass_coverage.TransformBounds(snapshot.transform.Invert());
1141  contents->SetTexture(snapshot.texture);
1142  contents->SetSourceRect(scaled);
1143  contents->SetSamplerDescriptor(snapshot.sampler_descriptor);
1144 
1145  // This backdrop entity sets a depth value as it is written to the newly
1146  // flipped backdrop and not into a new saveLayer.
1147  Entity backdrop_entity;
1148  backdrop_entity.SetContents(std::move(contents));
1149  backdrop_entity.SetClipDepth(++current_depth_);
1150  backdrop_entity.SetBlendMode(paint.blend_mode);
1151 
1152  backdrop_entity.Render(renderer_, GetCurrentRenderPass());
1153  Save(0);
1154  return;
1155  }
1156  }
1157  }
1158 
1159  // When applying a save layer, absorb any pending distributed opacity.
1160  Paint paint_copy = paint;
1161  paint_copy.color.alpha *= transform_stack_.back().distributed_opacity;
1162  transform_stack_.back().distributed_opacity = 1.0;
1163 
1164  render_passes_.push_back(
1165  LazyRenderingConfig(renderer_, //
1166  CreateRenderTarget(renderer_, //
1167  subpass_size, //
1169  )));
1170  save_layer_state_.push_back(SaveLayerState{
1171  paint_copy, subpass_coverage.Shift(-coverage_origin_adjustment)});
1172 
1173  CanvasStackEntry entry;
1174  entry.transform = transform_stack_.back().transform;
1175  entry.clip_depth = current_depth_ + total_content_depth;
1176  FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
1177  << entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
1178  << " after allocating " << total_content_depth;
1179  entry.clip_height = transform_stack_.back().clip_height;
1181  entry.did_round_out = did_round_out;
1182  transform_stack_.emplace_back(entry);
1183 
1184  // Start non-collapsed subpasses with a fresh clip coverage stack limited by
1185  // the subpass coverage. This is important because image filters applied to
1186  // save layers may transform the subpass texture after it's rendered,
1187  // causing parent clip coverage to get misaligned with the actual area that
1188  // the subpass will affect in the parent pass.
1189  clip_coverage_stack_.PushSubpass(subpass_coverage, GetClipHeight());
1190 
1191  if (!backdrop_filter_contents) {
1192  return;
1193  }
1194 
1195  // Render the backdrop entity.
1196  Entity backdrop_entity;
1197  backdrop_entity.SetContents(std::move(backdrop_filter_contents));
1198  backdrop_entity.SetTransform(
1199  Matrix::MakeTranslation(Vector3(-local_position)));
1200  backdrop_entity.SetClipDepth(std::numeric_limits<uint32_t>::max());
1201  backdrop_entity.Render(renderer_, GetCurrentRenderPass());
1202 }
1203 
1205  FML_DCHECK(transform_stack_.size() > 0);
1206  if (transform_stack_.size() == 1) {
1207  return false;
1208  }
1209 
1210  // This check is important to make sure we didn't exceed the depth
1211  // that the clips were rendered at while rendering any of the
1212  // rendering ops. It is OK for the current depth to equal the
1213  // outgoing clip depth because that means the clipping would have
1214  // been successful up through the last rendering op, but it cannot
1215  // be greater.
1216  // Also, we bump the current rendering depth to the outgoing clip
1217  // depth so that future rendering operations are not clipped by
1218  // any of the pixels set by the expiring clips. It is OK for the
1219  // estimates used to determine the clip depth in save/saveLayer
1220  // to be overly conservative, but we need to jump the depth to
1221  // the clip depth so that the next rendering op will get a
1222  // larger depth (it will pre-increment the current_depth_ value).
1223  FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
1224  << current_depth_ << " <=? " << transform_stack_.back().clip_depth;
1225  current_depth_ = transform_stack_.back().clip_depth;
1226 
1227  if (IsSkipping()) {
1228  transform_stack_.pop_back();
1229  return true;
1230  }
1231 
1232  if (transform_stack_.back().rendering_mode ==
1234  transform_stack_.back().rendering_mode ==
1236  auto lazy_render_pass = std::move(render_passes_.back());
1237  render_passes_.pop_back();
1238  // Force the render pass to be constructed if it never was.
1239  lazy_render_pass.inline_pass_context->GetRenderPass();
1240 
1241  SaveLayerState save_layer_state = save_layer_state_.back();
1242  save_layer_state_.pop_back();
1243  auto global_pass_position = GetGlobalPassPosition();
1244 
1245  std::shared_ptr<Contents> contents = CreateContentsForSubpassTarget(
1246  save_layer_state.paint, //
1247  lazy_render_pass.inline_pass_context->GetTexture(), //
1248  Matrix::MakeTranslation(Vector3{-global_pass_position}) * //
1249  transform_stack_.back().transform //
1250  );
1251 
1252  lazy_render_pass.inline_pass_context->EndPass();
1253 
1254  // Round the subpass texture position for pixel alignment with the parent
1255  // pass render target. By default, we draw subpass textures with nearest
1256  // sampling, so aligning here is important for avoiding visual nearest
1257  // sampling errors caused by limited floating point precision when
1258  // straddling a half pixel boundary.
1259  Point subpass_texture_position;
1260  if (transform_stack_.back().did_round_out) {
1261  // Subpass coverage was rounded out, origin potentially moved "down" by
1262  // as much as a pixel.
1263  subpass_texture_position =
1264  (save_layer_state.coverage.GetOrigin() - global_pass_position)
1265  .Floor();
1266  } else {
1267  // Subpass coverage was truncated. Pick the closest phyiscal pixel.
1268  subpass_texture_position =
1269  (save_layer_state.coverage.GetOrigin() - global_pass_position)
1270  .Round();
1271  }
1272 
1273  Entity element_entity;
1274  element_entity.SetClipDepth(++current_depth_);
1275  element_entity.SetContents(std::move(contents));
1276  element_entity.SetBlendMode(save_layer_state.paint.blend_mode);
1277  element_entity.SetTransform(
1278  Matrix::MakeTranslation(Vector3(subpass_texture_position)));
1279 
1280  if (element_entity.GetBlendMode() > Entity::kLastPipelineBlendMode) {
1281  if (renderer_.GetDeviceCapabilities().SupportsFramebufferFetch()) {
1282  ApplyFramebufferBlend(element_entity);
1283  } else {
1284  // End the active pass and flush the buffer before rendering "advanced"
1285  // blends. Advanced blends work by binding the current render target
1286  // texture as an input ("destination"), blending with a second texture
1287  // input ("source"), writing the result to an intermediate texture, and
1288  // finally copying the data from the intermediate texture back to the
1289  // render target texture. And so all of the commands that have written
1290  // to the render target texture so far need to execute before it's bound
1291  // for blending (otherwise the blend pass will end up executing before
1292  // all the previous commands in the active pass).
1293  auto input_texture = FlipBackdrop(GetGlobalPassPosition());
1294  if (!input_texture) {
1295  return false;
1296  }
1297 
1298  FilterInput::Vector inputs = {
1299  FilterInput::Make(input_texture,
1300  element_entity.GetTransform().Invert()),
1301  FilterInput::Make(element_entity.GetContents())};
1302  auto contents = ColorFilterContents::MakeBlend(
1303  element_entity.GetBlendMode(), inputs);
1304  contents->SetCoverageHint(element_entity.GetCoverage());
1305  element_entity.SetContents(std::move(contents));
1306  element_entity.SetBlendMode(BlendMode::kSource);
1307  }
1308  }
1309 
1310  element_entity.Render(
1311  renderer_, //
1312  *render_passes_.back().inline_pass_context->GetRenderPass() //
1313  );
1314  clip_coverage_stack_.PopSubpass();
1315  transform_stack_.pop_back();
1316 
1317  // We don't need to restore clips if a saveLayer was performed, as the clip
1318  // state is per render target, and no more rendering operations will be
1319  // performed as the render target workloaded is completed in the restore.
1320  return true;
1321  }
1322 
1323  size_t num_clips = transform_stack_.back().num_clips;
1324  transform_stack_.pop_back();
1325 
1326  if (num_clips > 0) {
1327  EntityPassClipStack::ClipStateResult clip_state_result =
1328  clip_coverage_stack_.RecordRestore(GetGlobalPassPosition(),
1329  GetClipHeight());
1330 
1331  // Clip restores are never required with depth based clipping.
1332  FML_DCHECK(!clip_state_result.should_render);
1333  if (clip_state_result.clip_did_change) {
1334  // We only need to update the pass scissor if the clip state has changed.
1335  SetClipScissor(
1336  clip_coverage_stack_.CurrentClipCoverage(), //
1337  *render_passes_.back().inline_pass_context->GetRenderPass(), //
1338  GetGlobalPassPosition() //
1339  );
1340  }
1341  }
1342 
1343  return true;
1344 }
1345 
1346 void Canvas::DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
1347  Point position,
1348  const Paint& paint) {
1349  Entity entity;
1350  entity.SetClipDepth(GetClipHeight());
1351  entity.SetBlendMode(paint.blend_mode);
1352 
1353  auto text_contents = std::make_shared<TextContents>();
1354  text_contents->SetTextFrame(text_frame);
1355  text_contents->SetForceTextColor(paint.mask_blur_descriptor.has_value());
1356  text_contents->SetScale(GetCurrentTransform().GetMaxBasisLengthXY());
1357  text_contents->SetColor(paint.color);
1358  text_contents->SetOffset(position);
1359  text_contents->SetTextProperties(paint.color, //
1360  paint.style == Paint::Style::kStroke, //
1361  paint.stroke_width, //
1362  paint.stroke_cap, //
1363  paint.stroke_join, //
1364  paint.stroke_miter //
1365  );
1366 
1368  Matrix::MakeTranslation(position));
1369 
1370  // TODO(bdero): This mask blur application is a hack. It will always wind up
1371  // doing a gaussian blur that affects the color source itself
1372  // instead of just the mask. The color filter text support
1373  // needs to be reworked in order to interact correctly with
1374  // mask filters.
1375  // https://github.com/flutter/flutter/issues/133297
1376  entity.SetContents(paint.WithFilters(paint.WithMaskBlur(
1377  std::move(text_contents), true, GetCurrentTransform())));
1378 
1379  AddRenderEntityToCurrentPass(entity, false);
1380 }
1381 
1382 void Canvas::AddRenderEntityWithFiltersToCurrentPass(Entity& entity,
1383  const Geometry* geometry,
1384  const Paint& paint,
1385  bool reuse_depth) {
1386  std::shared_ptr<ColorSourceContents> contents = paint.CreateContents();
1387  if (!paint.color_filter && !paint.invert_colors && !paint.image_filter &&
1388  !paint.mask_blur_descriptor.has_value()) {
1389  contents->SetGeometry(geometry);
1390  entity.SetContents(std::move(contents));
1391  AddRenderEntityToCurrentPass(entity, reuse_depth);
1392  return;
1393  }
1394 
1395  // Attempt to apply the color filter on the CPU first.
1396  // Note: This is not just an optimization; some color sources rely on
1397  // CPU-applied color filters to behave properly.
1398  bool needs_color_filter = paint.color_filter || paint.invert_colors;
1399  if (needs_color_filter &&
1400  contents->ApplyColorFilter([&](Color color) -> Color {
1401  if (paint.color_filter) {
1402  color = GetCPUColorFilterProc(paint.color_filter)(color);
1403  }
1404  if (paint.invert_colors) {
1405  color = color.ApplyColorMatrix(kColorInversion);
1406  }
1407  return color;
1408  })) {
1409  needs_color_filter = false;
1410  }
1411 
1412  bool can_apply_mask_filter = geometry->CanApplyMaskFilter();
1413  contents->SetGeometry(geometry);
1414 
1415  if (can_apply_mask_filter && paint.mask_blur_descriptor.has_value()) {
1416  // If there's a mask blur and we need to apply the color filter on the GPU,
1417  // we need to be careful to only apply the color filter to the source
1418  // colors. CreateMaskBlur is able to handle this case.
1419  RectGeometry out_rect(Rect{});
1420  auto filter_contents = paint.mask_blur_descriptor->CreateMaskBlur(
1421  contents, needs_color_filter ? paint.color_filter : nullptr,
1422  needs_color_filter ? paint.invert_colors : false, &out_rect);
1423  entity.SetContents(std::move(filter_contents));
1424  AddRenderEntityToCurrentPass(entity, reuse_depth);
1425  return;
1426  }
1427 
1428  std::shared_ptr<Contents> contents_copy = std::move(contents);
1429 
1430  // Image input types will directly set their color filter,
1431  // if any. See `TiledTextureContents.SetColorFilter`.
1432  if (needs_color_filter &&
1433  (!paint.color_source ||
1434  paint.color_source->type() != flutter::DlColorSourceType::kImage)) {
1435  if (paint.color_filter) {
1436  contents_copy = WrapWithGPUColorFilter(
1437  paint.color_filter, FilterInput::Make(std::move(contents_copy)),
1439  }
1440  if (paint.invert_colors) {
1441  contents_copy =
1442  WrapWithInvertColors(FilterInput::Make(std::move(contents_copy)),
1444  }
1445  }
1446 
1447  if (paint.image_filter) {
1448  std::shared_ptr<FilterContents> filter = WrapInput(
1449  paint.image_filter, FilterInput::Make(std::move(contents_copy)));
1450  filter->SetRenderingMode(Entity::RenderingMode::kDirect);
1451  entity.SetContents(filter);
1452  AddRenderEntityToCurrentPass(entity, reuse_depth);
1453  return;
1454  }
1455 
1456  entity.SetContents(std::move(contents_copy));
1457  AddRenderEntityToCurrentPass(entity, reuse_depth);
1458 }
1459 
1460 void Canvas::AddRenderEntityToCurrentPass(Entity& entity, bool reuse_depth) {
1461  if (IsSkipping()) {
1462  return;
1463  }
1464 
1465  entity.SetTransform(
1466  Matrix::MakeTranslation(Vector3(-GetGlobalPassPosition())) *
1467  entity.GetTransform());
1468  entity.SetInheritedOpacity(transform_stack_.back().distributed_opacity);
1469  if (entity.GetBlendMode() == BlendMode::kSourceOver &&
1470  entity.GetContents()->IsOpaque(entity.GetTransform())) {
1471  entity.SetBlendMode(BlendMode::kSource);
1472  }
1473 
1474  // If the entity covers the current render target and is a solid color, then
1475  // conditionally update the backdrop color to its solid color value blended
1476  // with the current backdrop.
1477  if (render_passes_.back().IsApplyingClearColor()) {
1478  std::optional<Color> maybe_color = entity.AsBackgroundColor(
1479  render_passes_.back().inline_pass_context->GetTexture()->GetSize());
1480  if (maybe_color.has_value()) {
1481  Color color = maybe_color.value();
1482  RenderTarget& render_target = render_passes_.back()
1483  .inline_pass_context->GetPassTarget()
1484  .GetRenderTarget();
1485  ColorAttachment attachment = render_target.GetColorAttachment(0);
1486  // Attachment.clear color needs to be premultiplied at all times, but the
1487  // Color::Blend function requires unpremultiplied colors.
1488  attachment.clear_color = attachment.clear_color.Unpremultiply()
1489  .Blend(color, entity.GetBlendMode())
1490  .Premultiply();
1491  render_target.SetColorAttachment(attachment, 0u);
1492  return;
1493  }
1494  }
1495 
1496  if (!reuse_depth) {
1497  ++current_depth_;
1498  }
1499  // We can render at a depth up to and including the depth of the currently
1500  // active clips and we will still be clipped out, but we cannot render at
1501  // a depth that is greater than the current clips or we will not be clipped.
1502  FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
1503  << current_depth_ << " <=? " << transform_stack_.back().clip_depth;
1504  entity.SetClipDepth(current_depth_);
1505 
1506  if (entity.GetBlendMode() > Entity::kLastPipelineBlendMode) {
1507  if (renderer_.GetDeviceCapabilities().SupportsFramebufferFetch()) {
1508  ApplyFramebufferBlend(entity);
1509  } else {
1510  // End the active pass and flush the buffer before rendering "advanced"
1511  // blends. Advanced blends work by binding the current render target
1512  // texture as an input ("destination"), blending with a second texture
1513  // input ("source"), writing the result to an intermediate texture, and
1514  // finally copying the data from the intermediate texture back to the
1515  // render target texture. And so all of the commands that have written
1516  // to the render target texture so far need to execute before it's bound
1517  // for blending (otherwise the blend pass will end up executing before
1518  // all the previous commands in the active pass).
1519  auto input_texture = FlipBackdrop(GetGlobalPassPosition());
1520  if (!input_texture) {
1521  return;
1522  }
1523 
1524  // The coverage hint tells the rendered Contents which portion of the
1525  // rendered output will actually be used, and so we set this to the
1526  // current clip coverage (which is the max clip bounds). The contents may
1527  // optionally use this hint to avoid unnecessary rendering work.
1528  auto element_coverage_hint = entity.GetContents()->GetCoverageHint();
1529  entity.GetContents()->SetCoverageHint(Rect::Intersection(
1530  element_coverage_hint, clip_coverage_stack_.CurrentClipCoverage()));
1531 
1532  FilterInput::Vector inputs = {
1533  FilterInput::Make(input_texture, entity.GetTransform().Invert()),
1534  FilterInput::Make(entity.GetContents())};
1535  auto contents =
1536  ColorFilterContents::MakeBlend(entity.GetBlendMode(), inputs);
1537  entity.SetContents(std::move(contents));
1538  entity.SetBlendMode(BlendMode::kSource);
1539  }
1540  }
1541 
1542  const std::shared_ptr<RenderPass>& result =
1543  render_passes_.back().inline_pass_context->GetRenderPass();
1544  if (!result) {
1545  // Failure to produce a render pass should be explained by specific errors
1546  // in `InlinePassContext::GetRenderPass()`, so avoid log spam and don't
1547  // append a validation log here.
1548  return;
1549  }
1550 
1551  entity.Render(renderer_, *result);
1552 }
1553 
1554 RenderPass& Canvas::GetCurrentRenderPass() const {
1555  return *render_passes_.back().inline_pass_context->GetRenderPass();
1556 }
1557 
1558 void Canvas::SetBackdropData(
1559  std::unordered_map<int64_t, BackdropData> backdrop_data,
1560  size_t backdrop_count) {
1561  backdrop_data_ = std::move(backdrop_data);
1562  backdrop_count_ = backdrop_count;
1563 }
1564 
1565 std::shared_ptr<Texture> Canvas::FlipBackdrop(Point global_pass_position,
1566  bool should_remove_texture,
1567  bool should_use_onscreen) {
1568  LazyRenderingConfig rendering_config = std::move(render_passes_.back());
1569  render_passes_.pop_back();
1570 
1571  // If the very first thing we render in this EntityPass is a subpass that
1572  // happens to have a backdrop filter or advanced blend, than that backdrop
1573  // filter/blend will sample from an uninitialized texture.
1574  //
1575  // By calling `pass_context.GetRenderPass` here, we force the texture to pass
1576  // through at least one RenderPass with the correct clear configuration before
1577  // any sampling occurs.
1578  //
1579  // In cases where there are no contents, we
1580  // could instead check the clear color and initialize a 1x2 CPU texture
1581  // instead of ending the pass.
1582  rendering_config.inline_pass_context->GetRenderPass();
1583  if (!rendering_config.inline_pass_context->EndPass()) {
1585  << "Failed to end the current render pass in order to read from "
1586  "the backdrop texture and apply an advanced blend or backdrop "
1587  "filter.";
1588  // Note: adding this render pass ensures there are no later crashes from
1589  // unbalanced save layers. Ideally, this method would return false and the
1590  // renderer could handle that by terminating dispatch.
1591  render_passes_.push_back(LazyRenderingConfig(
1592  renderer_, std::move(rendering_config.entity_pass_target),
1593  std::move(rendering_config.inline_pass_context)));
1594  return nullptr;
1595  }
1596 
1597  const std::shared_ptr<Texture>& input_texture =
1598  rendering_config.inline_pass_context->GetTexture();
1599 
1600  if (!input_texture) {
1601  VALIDATION_LOG << "Failed to fetch the color texture in order to "
1602  "apply an advanced blend or backdrop filter.";
1603 
1604  // Note: see above.
1605  render_passes_.push_back(LazyRenderingConfig(
1606  renderer_, std::move(rendering_config.entity_pass_target),
1607  std::move(rendering_config.inline_pass_context)));
1608  return nullptr;
1609  }
1610 
1611  if (should_use_onscreen) {
1612  ColorAttachment color0 = render_target_.GetColorAttachment(0);
1613  // When MSAA is being used, we end up overriding the entire backdrop by
1614  // drawing the previous pass texture, and so we don't have to clear it and
1615  // can use kDontCare.
1616  color0.load_action = color0.resolve_texture != nullptr
1617  ? LoadAction::kDontCare
1618  : LoadAction::kLoad;
1619  render_target_.SetColorAttachment(color0, 0);
1620 
1621  auto entity_pass_target = std::make_unique<EntityPassTarget>(
1622  render_target_, //
1623  renderer_.GetDeviceCapabilities().SupportsReadFromResolve(), //
1624  renderer_.GetDeviceCapabilities().SupportsImplicitResolvingMSAA() //
1625  );
1626  render_passes_.push_back(
1627  LazyRenderingConfig(renderer_, std::move(entity_pass_target)));
1628  requires_readback_ = false;
1629  } else {
1630  render_passes_.push_back(LazyRenderingConfig(
1631  renderer_, std::move(rendering_config.entity_pass_target),
1632  std::move(rendering_config.inline_pass_context)));
1633  // If the current texture is being cached for a BDF we need to ensure we
1634  // don't recycle it during recording; remove it from the entity pass target.
1635  if (should_remove_texture) {
1636  render_passes_.back().entity_pass_target->RemoveSecondary();
1637  }
1638  }
1639  RenderPass& current_render_pass =
1640  *render_passes_.back().inline_pass_context->GetRenderPass();
1641 
1642  // Eagerly restore the BDF contents.
1643 
1644  // If the pass context returns a backdrop texture, we need to draw it to the
1645  // current pass. We do this because it's faster and takes significantly less
1646  // memory than storing/loading large MSAA textures. Also, it's not possible
1647  // to blit the non-MSAA resolve texture of the previous pass to MSAA
1648  // textures (let alone a transient one).
1649  Rect size_rect = Rect::MakeSize(input_texture->GetSize());
1650  auto msaa_backdrop_contents = TextureContents::MakeRect(size_rect);
1651  msaa_backdrop_contents->SetStencilEnabled(false);
1652  msaa_backdrop_contents->SetLabel("MSAA backdrop");
1653  msaa_backdrop_contents->SetSourceRect(size_rect);
1654  msaa_backdrop_contents->SetTexture(input_texture);
1655 
1656  Entity msaa_backdrop_entity;
1657  msaa_backdrop_entity.SetContents(std::move(msaa_backdrop_contents));
1658  msaa_backdrop_entity.SetBlendMode(BlendMode::kSource);
1659  msaa_backdrop_entity.SetClipDepth(std::numeric_limits<uint32_t>::max());
1660  if (!msaa_backdrop_entity.Render(renderer_, current_render_pass)) {
1661  VALIDATION_LOG << "Failed to render MSAA backdrop entity.";
1662  return nullptr;
1663  }
1664 
1665  // Restore any clips that were recorded before the backdrop filter was
1666  // applied.
1667  auto& replay_entities = clip_coverage_stack_.GetReplayEntities();
1668  for (const auto& replay : replay_entities) {
1669  if (replay.clip_depth <= current_depth_) {
1670  continue;
1671  }
1672 
1673  SetClipScissor(replay.clip_coverage, current_render_pass,
1674  global_pass_position);
1675  if (!replay.clip_contents.Render(renderer_, current_render_pass,
1676  replay.clip_depth)) {
1677  VALIDATION_LOG << "Failed to render entity for clip restore.";
1678  }
1679  }
1680 
1681  return input_texture;
1682 }
1683 
1684 bool Canvas::SupportsBlitToOnscreen() const {
1685  return renderer_.GetContext()
1686  ->GetCapabilities()
1687  ->SupportsTextureToTextureBlits() &&
1688  renderer_.GetContext()->GetBackendType() !=
1689  Context::BackendType::kOpenGLES;
1690 }
1691 
1692 bool Canvas::BlitToOnscreen(bool is_onscreen) {
1693  auto command_buffer = renderer_.GetContext()->CreateCommandBuffer();
1694  command_buffer->SetLabel("EntityPass Root Command Buffer");
1695  auto offscreen_target = render_passes_.back()
1696  .inline_pass_context->GetPassTarget()
1697  .GetRenderTarget();
1698  if (SupportsBlitToOnscreen()) {
1699  auto blit_pass = command_buffer->CreateBlitPass();
1700  blit_pass->AddCopy(offscreen_target.GetRenderTargetTexture(),
1701  render_target_.GetRenderTargetTexture());
1702  if (!blit_pass->EncodeCommands()) {
1703  VALIDATION_LOG << "Failed to encode root pass blit command.";
1704  return false;
1705  }
1706  } else {
1707  auto render_pass = command_buffer->CreateRenderPass(render_target_);
1708  render_pass->SetLabel("EntityPass Root Render Pass");
1709 
1710  {
1711  auto size_rect = Rect::MakeSize(offscreen_target.GetRenderTargetSize());
1712  auto contents = TextureContents::MakeRect(size_rect);
1713  contents->SetTexture(offscreen_target.GetRenderTargetTexture());
1714  contents->SetSourceRect(size_rect);
1715  contents->SetLabel("Root pass blit");
1716 
1717  Entity entity;
1718  entity.SetContents(contents);
1719  entity.SetBlendMode(BlendMode::kSource);
1720 
1721  if (!entity.Render(renderer_, *render_pass)) {
1722  VALIDATION_LOG << "Failed to render EntityPass root blit.";
1723  return false;
1724  }
1725  }
1726 
1727  if (!render_pass->EncodeCommands()) {
1728  VALIDATION_LOG << "Failed to encode root pass command buffer.";
1729  return false;
1730  }
1731  }
1732 
1733  if (is_onscreen) {
1734  return renderer_.GetContext()->SubmitOnscreen(std::move(command_buffer));
1735  } else {
1736  return renderer_.GetContext()->EnqueueCommandBuffer(
1737  std::move(command_buffer));
1738  }
1739 }
1740 
1741 bool Canvas::EnsureFinalMipmapGeneration() const {
1742  if (!render_target_.GetRenderTargetTexture()->NeedsMipmapGeneration()) {
1743  return true;
1744  }
1745  std::shared_ptr<CommandBuffer> cmd_buffer =
1746  renderer_.GetContext()->CreateCommandBuffer();
1747  if (!cmd_buffer) {
1748  return false;
1749  }
1750  std::shared_ptr<BlitPass> blit_pass = cmd_buffer->CreateBlitPass();
1751  if (!blit_pass) {
1752  return false;
1753  }
1754  blit_pass->GenerateMipmap(render_target_.GetRenderTargetTexture());
1755  blit_pass->EncodeCommands();
1756  return renderer_.GetContext()->EnqueueCommandBuffer(std::move(cmd_buffer));
1757 }
1758 
1759 void Canvas::EndReplay() {
1760  FML_DCHECK(render_passes_.size() == 1u);
1761  render_passes_.back().inline_pass_context->GetRenderPass();
1762  render_passes_.back().inline_pass_context->EndPass(
1763  /*is_onscreen=*/!requires_readback_ && is_onscreen_);
1764  backdrop_data_.clear();
1765 
1766  // If requires_readback_ was true, then we rendered to an offscreen texture
1767  // instead of to the onscreen provided in the render target. Now we need to
1768  // draw or blit the offscreen back to the onscreen.
1769  if (requires_readback_) {
1770  BlitToOnscreen(/*is_onscreen_=*/is_onscreen_);
1771  }
1772  if (!EnsureFinalMipmapGeneration()) {
1773  VALIDATION_LOG << "Failed to generate onscreen mipmaps.";
1774  }
1775  if (!renderer_.GetContext()->FlushCommandBuffers()) {
1776  // Not much we can do.
1777  VALIDATION_LOG << "Failed to submit command buffers";
1778  }
1779  render_passes_.clear();
1780  renderer_.GetRenderTargetCache()->End();
1781  clip_geometry_.clear();
1782 
1783  Reset();
1784  Initialize(initial_cull_rect_);
1785 }
1786 
1787 } // namespace impeller
void ClipGeometry(const Geometry &geometry, Entity::ClipOperation clip_op, bool is_aa=true)
Definition: canvas.cc:566
static constexpr uint32_t kMaxDepth
Definition: canvas.h:117
Canvas(ContentContext &renderer, const RenderTarget &render_target, bool is_onscreen, bool requires_readback)
Definition: canvas.cc:164
std::optional< Rect > GetLocalCoverageLimit() const
Return the culling bounds of the current render target, or nullopt if there is no coverage.
Definition: canvas.cc:935
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:966
const Matrix & GetCurrentTransform() const
Definition: canvas.cc:238
void DrawVertices(const std::shared_ptr< VerticesGeometry > &vertices, BlendMode blend_mode, const Paint &paint)
Definition: canvas.cc:739
void DrawOval(const Rect &rect, const Paint &paint)
Definition: canvas.cc:486
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:679
void RestoreToCount(size_t count)
Definition: canvas.cc:285
bool Restore()
Definition: canvas.cc:1204
size_t GetSaveCount() const
Definition: canvas.cc:277
void Concat(const Matrix &transform)
Definition: canvas.cc:222
void Transform(const Matrix &transform)
Definition: canvas.cc:234
std::function< std::shared_ptr< FilterContents >(FilterInput::Ref, const Matrix &effect_transform, Entity::RenderingMode rendering_mode)> BackdropFilterProc
Definition: canvas.h:122
void PreConcat(const Matrix &transform)
Definition: canvas.cc:226
void Rotate(Radians radians)
Definition: canvas.cc:258
void DrawPoints(const Point points[], uint32_t count, Scalar radius, const Paint &paint, PointStyle point_style)
Definition: canvas.cc:647
void ResetTransform()
Definition: canvas.cc:230
void DrawTextFrame(const std::shared_ptr< TextFrame > &text_frame, Point position, const Paint &paint)
Definition: canvas.cc:1346
void DrawImage(const std::shared_ptr< Texture > &image, Point offset, const Paint &paint, const SamplerDescriptor &sampler={})
Definition: canvas.cc:665
void DrawPaint(const Paint &paint)
Definition: canvas.cc:308
void DrawRoundRect(const RoundRect &rect, const Paint &paint)
Definition: canvas.cc:516
void Skew(Scalar sx, Scalar sy)
Definition: canvas.cc:254
void Scale(const Vector2 &scale)
Definition: canvas.cc:246
void DrawPath(const Path &path, const Paint &paint)
Definition: canvas.cc:293
void Save(uint32_t total_content_depth=kMaxDepth)
Definition: canvas.cc:918
void DrawRect(const Rect &rect, const Paint &paint)
Definition: canvas.cc:468
void DrawAtlas(const std::shared_ptr< AtlasContents > &atlas_contents, const Paint &paint)
Definition: canvas.cc:852
void DrawLine(const Point &p0, const Point &p1, const Paint &paint, bool reuse_depth=false)
Definition: canvas.cc:455
void Translate(const Vector3 &offset)
Definition: canvas.cc:242
void DrawCircle(const Point &center, Scalar radius, const Paint &paint)
Definition: canvas.cc:543
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
std::shared_ptr< Context > GetContext() const
A geometry that implements "drawPaint" like behavior by covering the entire render pass area.
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:145
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
static constexpr BlendMode kLastPipelineBlendMode
Definition: entity.h:22
static bool IsBlendModeDestructive(BlendMode blend_mode)
Returns true if the blend mode is "destructive", meaning that even fully transparent source colors wo...
Definition: entity.cc:128
A class that tracks all clips that have been recorded in the current entity pass stencil.
std::optional< Rect > CurrentClipCoverage() const
void PushSubpass(std::optional< Rect > subpass_coverage, size_t clip_height)
ClipStateResult RecordClip(const ClipContents &clip_contents, Matrix transform, Point global_pass_position, uint32_t clip_depth, size_t clip_height_floor, bool is_aa)
ClipStateResult RecordRestore(Point global_pass_position, size_t restore_height)
A geometry that is created from a filled path object.
@ 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:82
virtual GeometryResult GetPositionBuffer(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
virtual bool CanApplyMaskFilter() const
Definition: geometry.cc:121
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
virtual bool IsAxisAlignedRect() const
Definition: geometry.cc:117
PathBuilder & AddRect(Rect rect)
Path TakePath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:28
PathBuilder & AddRoundRect(RoundRect rect)
PathBuilder & SetBounds(Rect bounds)
Set the bounding box that will be used by Path.GetBoundingBox in place of performing the computation.
PathBuilder & AddOval(const Rect &rect)
PathBuilder & SetConvexity(Convexity value)
Definition: path_builder.cc:97
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:53
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 that is created from a stroked path object.
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
A common case factory that marks the texture contents as having a destination rectangle....
ISize subpass_size
The output size of the down-sampling pass.
impeller::SamplerDescriptor ToSamplerDescriptor(const flutter::DlImageSampling options)
std::shared_ptr< ColorFilterContents > WrapWithGPUColorFilter(const flutter::DlColorFilter *filter, const std::shared_ptr< FilterInput > &input, ColorFilterContents::AbsorbOpacity absorb_opacity)
Definition: color_filter.cc:24
float Scalar
Definition: scalar.h:18
SourceRectConstraint
Controls the behavior of the source rectangle given to DrawImageRect.
Definition: canvas.h:69
@ 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:17
constexpr float kEhCloseEnough
Definition: constants.h:56
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:792
PointStyle
Definition: canvas.h:60
@ kRound
Points are drawn as squares.
TPoint< Scalar > Point
Definition: point.h:327
ColorFilterProc GetCPUColorFilterProc(const flutter::DlColorFilter *filter)
Definition: color_filter.cc:66
IRect64 IRect
Definition: rect.h:795
BlendMode
Definition: color.h:58
ContentBoundsPromise
Definition: canvas.h:79
@ 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:171
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:174
const Scalar scale
SeparatedVector2 offset
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:38
size_t backdrop_count
Definition: canvas.h:36
std::optional< Snapshot > shared_filter_snapshot
Definition: canvas.h:41
Definition: canvas.h:45
size_t clip_height
Definition: canvas.h:48
bool did_round_out
Definition: canvas.h:57
Entity::RenderingMode rendering_mode
Definition: canvas.h:52
Matrix transform
Definition: canvas.h:46
uint32_t clip_depth
Definition: canvas.h:47
static constexpr Color BlackTransparent()
Definition: color.h:269
Scalar alpha
Definition: color.h:142
static constexpr Color White()
Definition: color.h:263
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:277
Color ApplyColorMatrix(const ColorMatrix &color_matrix) const
A color filter that transforms colors through a 4x5 color matrix.
Definition: color.cc:301
std::unique_ptr< InlinePassContext > inline_pass_context
Definition: canvas.h:96
std::unique_ptr< EntityPassTarget > entity_pass_target
Definition: canvas.h:95
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:97
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
std::shared_ptr< Contents > WithFilters(std::shared_ptr< Contents > input) const
Wrap this paint's configured filters to the given contents.
Definition: paint.cc:246
const flutter::DlColorFilter * color_filter
Definition: paint.h:76
const flutter::DlColorSource * color_source
Definition: paint.h:75
const flutter::DlImageFilter * image_filter
Definition: paint.h:77
Cap stroke_cap
Definition: paint.h:80
Join stroke_join
Definition: paint.h:81
Scalar stroke_miter
Definition: paint.h:82
Style style
Definition: paint.h:83
std::shared_ptr< Contents > WithMaskBlur(std::shared_ptr< Contents > input, bool is_solid_color, const Matrix &ctm) const
Definition: paint.cc:270
bool invert_colors
Definition: paint.h:85
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:39
std::optional< MaskBlurDescriptor > mask_blur_descriptor
Definition: paint.h:87
Color color
Definition: paint.h:74
BlendMode blend_mode
Definition: paint.h:84
std::shared_ptr< FilterContents > WithImageFilter(const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
Definition: paint.cc:280
std::shared_ptr< ColorSourceContents > CreateContents() const
Definition: paint.cc:37
bool HasColorFilter() const
Whether this paint has a color filter that can apply opacity.
Definition: paint.cc:437
Scalar stroke_width
Definition: paint.h:79
constexpr const RoundingRadii & GetRadii() const
Definition: round_rect.h:44
constexpr const Rect & GetBounds() const
Definition: round_rect.h:43
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 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:622
constexpr auto GetBottom() const
Definition: rect.h:361
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:476
constexpr auto GetTop() const
Definition: rect.h:357
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition: rect.h:351
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:324
constexpr std::optional< TRect > Intersection(const TRect &o) const
Definition: rect.h:532
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:301
constexpr static TRect MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
Definition: rect.h:144
constexpr auto GetLeft() const
Definition: rect.h:355
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:331
Round(const TRect< U > &r)
Definition: rect.h:699
RoundOut(const TRect< U > &r)
Definition: rect.h:683
constexpr auto GetRight() const
Definition: rect.h:359
constexpr bool IsSquare() const
Returns true if width and height are equal and neither is NaN.
Definition: rect.h:308
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:606
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:345
constexpr Point GetCenter() const
Get the center point as a |Point|.
Definition: rect.h:386
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
constexpr static TRect MakeMaximum()
Definition: rect.h:188
Type height
Definition: size.h:29
Type width
Definition: size.h:28
#define VALIDATION_LOG
Definition: validation.h:91