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