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