| 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
5 #include "impeller/display_list/canvas.h"
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"
15 #include "impeller/display_list/color_filter.h"
16 #include "impeller/display_list/image_filter.h"
17 #include "impeller/display_list/skia_conversions.h"
18 #include "impeller/entity/contents/atlas_contents.h"
19 #include "impeller/entity/contents/clip_contents.h"
20 #include "impeller/entity/contents/color_source_contents.h"
21 #include "impeller/entity/contents/content_context.h"
22 #include "impeller/entity/contents/filters/filter_contents.h"
23 #include "impeller/entity/contents/framebuffer_blend_contents.h"
24 #include "impeller/entity/contents/solid_rrect_blur_contents.h"
25 #include "impeller/entity/contents/text_contents.h"
26 #include "impeller/entity/contents/texture_contents.h"
27 #include "impeller/entity/contents/vertices_contents.h"
28 #include "impeller/entity/geometry/circle_geometry.h"
29 #include "impeller/entity/geometry/cover_geometry.h"
30 #include "impeller/entity/geometry/ellipse_geometry.h"
31 #include "impeller/entity/geometry/fill_path_geometry.h"
32 #include "impeller/entity/geometry/geometry.h"
33 #include "impeller/entity/geometry/line_geometry.h"
34 #include "impeller/entity/geometry/point_field_geometry.h"
35 #include "impeller/entity/geometry/rect_geometry.h"
36 #include "impeller/entity/geometry/round_rect_geometry.h"
37 #include "impeller/entity/geometry/stroke_path_geometry.h"
38 #include "impeller/entity/save_layer_utils.h"
39 #include "impeller/geometry/color.h"
40 #include "impeller/geometry/constants.h"
41 #include "impeller/geometry/path_builder.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()) {
117 VALIDATION_LOG
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
263 Canvas::Canvas(ContentContext& renderer,
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
275 Canvas::Canvas(ContentContext& renderer,
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
288 Canvas::Canvas(ContentContext& renderer,
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{
305 .clip_depth = kMaxDepth,
306 });
307 FML_DCHECK(GetSaveCount() == 1u);
308 }
309
310 void Canvas::Reset() {
311 current_depth_ = 0u;
312 transform_stack_ = {};
313 }
314
315 void Canvas::Concat(const Matrix& transform) {
316 transform_stack_.back().transform = GetCurrentTransform() * transform;
317 }
318
319 void Canvas::PreConcat(const Matrix& transform) {
320 transform_stack_.back().transform = transform * GetCurrentTransform();
321 }
322
323 void Canvas::ResetTransform() {
324 transform_stack_.back().transform = {};
325 }
326
327 void Canvas::Transform(const Matrix& transform) {
328 Concat(transform);
329 }
330
331 const Matrix& Canvas::GetCurrentTransform() const {
332 return transform_stack_.back().transform;
333 }
334
335 void Canvas::Translate(const Vector3& offset) {
336 Concat(Matrix::MakeTranslation(offset));
337 }
338
339 void Canvas::Scale(const Vector2& scale) {
340 Concat(Matrix::MakeScale(scale));
341 }
342
343 void Canvas::Scale(const Vector3& scale) {
344 Concat(Matrix::MakeScale(scale));
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;
388 entity.SetTransform(GetCurrentTransform());
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;
403 entity.SetTransform(GetCurrentTransform());
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 !=
465 FilterContents::BlurStyle::kNormal &&
466 paint.image_filter) ||
467 (paint.mask_blur_descriptor->style == FilterContents::BlurStyle::kSolid &&
468 (!rrect_color.IsOpaque() ||
469 paint.blend_mode != BlendMode::kSourceOver))) {
470 Rect render_bounds = rect;
471 if (paint.mask_blur_descriptor->style !=
472 FilterContents::BlurStyle::kInner) {
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) {
510 case FilterContents::BlurStyle::kNormal: {
511 draw_blurred_rrect();
512 break;
513 }
514 case FilterContents::BlurStyle::kSolid: {
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 }
527 case FilterContents::BlurStyle::kOuter: {
528 ClipGeometry(Geometry::MakeRoundRect(rect, corner_radii),
529 Entity::ClipOperation::kDifference);
530 draw_blurred_rrect();
531 break;
532 }
533 case FilterContents::BlurStyle::kInner: {
534 ClipGeometry(Geometry::MakeRoundRect(rect, corner_radii),
535 Entity::ClipOperation::kIntersect);
536 draw_blurred_rrect();
537 break;
538 }
539 }
540
541 Restore();
542
543 return true;
544 }
545
546 void Canvas::DrawLine(const Point& p0, const Point& p1, const Paint& paint) {
547 Entity entity;
548 entity.SetTransform(GetCurrentTransform());
549 entity.SetBlendMode(paint.blend_mode);
550
551 LineGeometry geom(p0, p1, paint.stroke_width, paint.stroke_cap);
552 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
553 }
554
555 void Canvas::DrawRect(const Rect& rect, const Paint& paint) {
556 if (paint.style == Paint::Style::kStroke) {
557 DrawPath(PathBuilder{}.AddRect(rect).TakePath(), paint);
558 return;
559 }
560
561 if (AttemptDrawBlurredRRect(rect, {}, paint)) {
562 return;
563 }
564
565 Entity entity;
566 entity.SetTransform(GetCurrentTransform());
567 entity.SetBlendMode(paint.blend_mode);
568
569 RectGeometry geom(rect);
570 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
571 }
572
573 void Canvas::DrawOval(const Rect& rect, const Paint& paint) {
574 // TODO(jonahwilliams): This additional condition avoids an assert in the
575 // stroke circle geometry generator. I need to verify the condition that this
576 // assert prevents.
577 if (rect.IsSquare() && (paint.style == Paint::Style::kFill ||
578 (paint.style == Paint::Style::kStroke &&
579 paint.stroke_width < rect.GetWidth()))) {
580 // Circles have slightly less overhead and can do stroking
581 DrawCircle(rect.GetCenter(), rect.GetWidth() * 0.5f, paint);
582 return;
583 }
584
585 if (paint.style == Paint::Style::kStroke) {
586 // No stroked ellipses yet
587 DrawPath(PathBuilder{}.AddOval(rect).TakePath(), paint);
588 return;
589 }
590
591 if (AttemptDrawBlurredRRect(rect, rect.GetSize() * 0.5f, paint)) {
592 return;
593 }
594
595 Entity entity;
596 entity.SetTransform(GetCurrentTransform());
597 entity.SetBlendMode(paint.blend_mode);
598
599 EllipseGeometry geom(rect);
600 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
601 }
602
603 void Canvas::DrawRRect(const Rect& rect,
604 const Size& corner_radii,
605 const Paint& paint) {
606 if (AttemptDrawBlurredRRect(rect, corner_radii, paint)) {
607 return;
608 }
609
610 if (paint.style == Paint::Style::kFill) {
611 Entity entity;
612 entity.SetTransform(GetCurrentTransform());
613 entity.SetBlendMode(paint.blend_mode);
614
615 RoundRectGeometry geom(rect, corner_radii);
616 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
617 return;
618 }
619
620 auto path = PathBuilder{}
621 .SetConvexity(Convexity::kConvex)
622 .AddRoundedRect(rect, corner_radii)
623 .SetBounds(rect)
624 .TakePath();
625 DrawPath(path, paint);
626 }
627
628 void Canvas::DrawCircle(const Point& center,
629 Scalar radius,
630 const Paint& paint) {
631 Size half_size(radius, radius);
632 if (AttemptDrawBlurredRRect(
633 Rect::MakeOriginSize(center - half_size, half_size * 2),
634 {radius, radius}, paint)) {
635 return;
636 }
637
638 Entity entity;
639 entity.SetTransform(GetCurrentTransform());
640 entity.SetBlendMode(paint.blend_mode);
641
642 if (paint.style == Paint::Style::kStroke) {
643 CircleGeometry geom(center, radius, paint.stroke_width);
644 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
645 } else {
646 CircleGeometry geom(center, radius);
647 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
648 }
649 }
650
651 void Canvas::ClipGeometry(std::unique_ptr<Geometry> geometry,
652 Entity::ClipOperation clip_op) {
653 clip_geometry_.push_back(std::move(geometry));
654
655 auto contents = std::make_shared<ClipContents>();
656 contents->SetGeometry(clip_geometry_.back().get());
657 contents->SetClipOperation(clip_op);
658
659 Entity entity;
660 entity.SetTransform(GetCurrentTransform());
661 entity.SetContents(std::move(contents));
662
663 AddClipEntityToCurrentPass(entity);
664
665 ++transform_stack_.back().clip_height;
666 ++transform_stack_.back().num_clips;
667 }
668
669 void Canvas::RestoreClip() {
670 Entity entity;
671 entity.SetTransform(GetCurrentTransform());
672 // This path is empty because ClipRestoreContents just generates a quad that
673 // takes up the full render target.
674 auto clip_restore = std::make_shared<ClipRestoreContents>();
675 clip_restore->SetRestoreHeight(GetClipHeight());
676 entity.SetContents(std::move(clip_restore));
677
678 AddRenderEntityToCurrentPass(entity);
679 }
680
681 void Canvas::DrawPoints(std::vector<Point> points,
682 Scalar radius,
683 const Paint& paint,
684 PointStyle point_style) {
685 if (radius <= 0) {
686 return;
687 }
688
689 Entity entity;
690 entity.SetTransform(GetCurrentTransform());
691 entity.SetBlendMode(paint.blend_mode);
692
693 PointFieldGeometry geom(std::move(points), radius,
694 /*round=*/point_style == PointStyle::kRound);
695 AddRenderEntityWithFiltersToCurrentPass(entity, &geom, paint);
696 }
697
698 void Canvas::DrawImage(const std::shared_ptr<Texture>& image,
699 Point offset,
700 const Paint& paint,
701 SamplerDescriptor sampler) {
702 if (!image) {
703 return;
704 }
705
706 const auto source = Rect::MakeSize(image->GetSize());
707 const auto dest = source.Shift(offset);
708
709 DrawImageRect(image, source, dest, paint, std::move(sampler));
710 }
711
712 void Canvas::DrawImageRect(const std::shared_ptr<Texture>& image,
713 Rect source,
714 Rect dest,
715 const Paint& paint,
716 SamplerDescriptor sampler,
717 SourceRectConstraint src_rect_constraint) {
718 if (!image || source.IsEmpty() || dest.IsEmpty()) {
719 return;
720 }
721
722 auto size = image->GetSize();
723
724 if (size.IsEmpty()) {
725 return;
726 }
727
728 auto texture_contents = TextureContents::MakeRect(dest);
729 texture_contents->SetTexture(image);
730 texture_contents->SetSourceRect(source);
731 texture_contents->SetStrictSourceRect(src_rect_constraint ==
732 SourceRectConstraint::kStrict);
733 texture_contents->SetSamplerDescriptor(std::move(sampler));
734 texture_contents->SetOpacity(paint.color.alpha);
735 texture_contents->SetDeferApplyingOpacity(paint.HasColorFilter());
736
737 Entity entity;
738 entity.SetBlendMode(paint.blend_mode);
739 entity.SetTransform(GetCurrentTransform());
740
741 if (!paint.mask_blur_descriptor.has_value()) {
742 entity.SetContents(paint.WithFilters(std::move(texture_contents)));
743 AddRenderEntityToCurrentPass(entity);
744 return;
745 }
746
747 RectGeometry out_rect(Rect{});
748
749 entity.SetContents(paint.WithFilters(
750 paint.mask_blur_descriptor->CreateMaskBlur(texture_contents, &out_rect)));
751 AddRenderEntityToCurrentPass(entity);
752 }
753
754 size_t Canvas::GetClipHeight() const {
755 return transform_stack_.back().clip_height;
756 }
757
758 void Canvas::DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
759 BlendMode blend_mode,
760 const Paint& paint) {
761 // Override the blend mode with kDestination in order to match the behavior
762 // of Skia's SK_LEGACY_IGNORE_DRAW_VERTICES_BLEND_WITH_NO_SHADER flag, which
763 // is enabled when the Flutter engine builds Skia.
764 if (!paint.color_source ||
765 paint.color_source->type() == flutter::DlColorSourceType::kColor) {
766 blend_mode = BlendMode::kDestination;
767 }
768
769 Entity entity;
770 entity.SetTransform(GetCurrentTransform());
771 entity.SetBlendMode(paint.blend_mode);
772
773 // If there are no vertex colors.
774 if (UseColorSourceContents(vertices, paint)) {
775 AddRenderEntityWithFiltersToCurrentPass(entity, vertices.get(), paint);
776 return;
777 }
778
779 // If the blend mode is destination don't bother to bind or create a texture.
780 if (blend_mode == BlendMode::kDestination) {
781 auto contents = std::make_shared<VerticesSimpleBlendContents>();
782 contents->SetBlendMode(blend_mode);
783 contents->SetAlpha(paint.color.alpha);
784 contents->SetGeometry(vertices);
785 entity.SetContents(paint.WithFilters(std::move(contents)));
786 AddRenderEntityToCurrentPass(entity);
787 return;
788 }
789
790 // If there is a texture, use this directly. Otherwise render the color
791 // source to a texture.
792 if (paint.color_source &&
793 paint.color_source->type() == flutter::DlColorSourceType::kImage) {
794 const flutter::DlImageColorSource* image_color_source =
795 paint.color_source->asImage();
796 FML_DCHECK(image_color_source &&
797 image_color_source->image()->impeller_texture());
798 auto texture = image_color_source->image()->impeller_texture();
799 auto x_tile_mode = static_cast<Entity::TileMode>(
800 image_color_source->horizontal_tile_mode());
801 auto y_tile_mode =
802 static_cast<Entity::TileMode>(image_color_source->vertical_tile_mode());
803 auto sampler_descriptor =
804 skia_conversions::ToSamplerDescriptor(image_color_source->sampling());
805 auto effect_transform =
806 skia_conversions::ToMatrix(image_color_source->matrix());
807
808 auto contents = std::make_shared<VerticesSimpleBlendContents>();
809 contents->SetBlendMode(blend_mode);
810 contents->SetAlpha(paint.color.alpha);
811 contents->SetGeometry(vertices);
812 contents->SetEffectTransform(effect_transform);
813 contents->SetTexture(texture);
814 contents->SetTileMode(x_tile_mode, y_tile_mode);
815
816 entity.SetContents(paint.WithFilters(std::move(contents)));
817 AddRenderEntityToCurrentPass(entity);
818 return;
819 }
820
821 auto src_paint = paint;
822 src_paint.color = paint.color.WithAlpha(1.0);
823
824 std::shared_ptr<ColorSourceContents> src_contents =
825 src_paint.CreateContents();
826 src_contents->SetGeometry(vertices.get());
827
828 // If the color source has an intrinsic size, then we use that to
829 // create the src contents as a simplification. Otherwise we use
830 // the extent of the texture coordinates to determine how large
831 // the src contents should be. If neither has a value we fall back
832 // to using the geometry coverage data.
833 Rect src_coverage;
834 auto size = src_contents->GetColorSourceSize();
835 if (size.has_value()) {
836 src_coverage = Rect::MakeXYWH(0, 0, size->width, size->height);
837 } else {
838 auto cvg = vertices->GetCoverage(Matrix{});
839 FML_CHECK(cvg.has_value());
840 src_coverage =
841 // Covered by FML_CHECK.
842 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
843 vertices->GetTextureCoordinateCoverge().value_or(cvg.value());
844 }
845 src_contents = src_paint.CreateContents();
846
847 clip_geometry_.push_back(Geometry::MakeRect(Rect::Round(src_coverage)));
848 src_contents->SetGeometry(clip_geometry_.back().get());
849
850 auto contents = std::make_shared<VerticesSimpleBlendContents>();
851 contents->SetBlendMode(blend_mode);
852 contents->SetAlpha(paint.color.alpha);
853 contents->SetGeometry(vertices);
854 contents->SetLazyTextureCoverage(src_coverage);
855 contents->SetLazyTexture(
856 [src_contents, src_coverage](const ContentContext& renderer) {
857 // Applying the src coverage as the coverage limit prevents the 1px
858 // coverage pad from adding a border that is picked up by developer
859 // specified UVs.
860 return src_contents
861 ->RenderToSnapshot(renderer, {}, Rect::Round(src_coverage))
862 ->texture;
863 });
864 entity.SetContents(paint.WithFilters(std::move(contents)));
865 AddRenderEntityToCurrentPass(entity);
866 }
867
868 void Canvas::DrawAtlas(const std::shared_ptr<AtlasContents>& atlas_contents,
869 const Paint& paint) {
870 atlas_contents->SetAlpha(paint.color.alpha);
871
872 Entity entity;
873 entity.SetTransform(GetCurrentTransform());
874 entity.SetBlendMode(paint.blend_mode);
875 entity.SetContents(paint.WithFilters(atlas_contents));
876
877 AddRenderEntityToCurrentPass(entity);
878 }
879
880 /// Compositor Functionality
881 /////////////////////////////////////////
882
883 void Canvas::SetupRenderPass() {
884 renderer_.GetRenderTargetCache()->Start();
885 auto color0 = render_target_.GetColorAttachments().find(0u)->second;
886
887 auto& stencil_attachment = render_target_.GetStencilAttachment();
888 auto& depth_attachment = render_target_.GetDepthAttachment();
889 if (!stencil_attachment.has_value() || !depth_attachment.has_value()) {
890 // Setup a new root stencil with an optimal configuration if one wasn't
891 // provided by the caller.
892 render_target_.SetupDepthStencilAttachments(
893 *renderer_.GetContext(),
894 *renderer_.GetContext()->GetResourceAllocator(),
895 color0.texture->GetSize(),
896 renderer_.GetContext()->GetCapabilities()->SupportsOffscreenMSAA(),
897 "ImpellerOnscreen", kDefaultStencilConfig);
898 }
899
900 // Set up the clear color of the root pass.
901 color0.clear_color = Color::BlackTransparent();
902 render_target_.SetColorAttachment(color0, 0);
903
904 // If requires_readback is true, then there is a backdrop filter or emulated
905 // advanced blend in the first save layer. This requires a readback, which
906 // isn't supported by onscreen textures. To support this, we immediately begin
907 // a second save layer with the same dimensions as the onscreen. When
908 // rendering is completed, we must blit this saveLayer to the onscreen.
909 if (requires_readback_) {
910 auto entity_pass_target =
911 CreateRenderTarget(renderer_, //
912 color0.texture->GetSize(), //
913 /*clear_color=*/Color::BlackTransparent());
914 render_passes_.push_back(
915 LazyRenderingConfig(renderer_, std::move(entity_pass_target)));
916 } else {
917 auto entity_pass_target = std::make_unique<EntityPassTarget>(
918 render_target_, //
919 renderer_.GetDeviceCapabilities().SupportsReadFromResolve(), //
920 renderer_.GetDeviceCapabilities().SupportsImplicitResolvingMSAA() //
921 );
922 render_passes_.push_back(
923 LazyRenderingConfig(renderer_, std::move(entity_pass_target)));
924 }
925 }
926
927 void Canvas::SkipUntilMatchingRestore(size_t total_content_depth) {
928 auto entry = CanvasStackEntry{};
929 entry.skipping = true;
930 entry.clip_depth = current_depth_ + total_content_depth;
931 transform_stack_.push_back(entry);
932 }
933
934 void Canvas::Save(uint32_t total_content_depth) {
935 if (IsSkipping()) {
936 return SkipUntilMatchingRestore(total_content_depth);
937 }
938
939 auto entry = CanvasStackEntry{};
940 entry.transform = transform_stack_.back().transform;
941 entry.clip_depth = current_depth_ + total_content_depth;
942 entry.distributed_opacity = transform_stack_.back().distributed_opacity;
943 FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
944 << entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
945 << " after allocating " << total_content_depth;
946 entry.clip_height = transform_stack_.back().clip_height;
947 entry.rendering_mode = Entity::RenderingMode::kDirect;
948 transform_stack_.push_back(entry);
949 }
950
951 std::optional<Rect> Canvas::GetLocalCoverageLimit() const {
952 if (!clip_coverage_stack_.HasCoverage()) {
953 // The current clip is empty. This means the pass texture won't be
954 // visible, so skip it.
955 return std::nullopt;
956 }
957
958 auto maybe_current_clip_coverage = clip_coverage_stack_.CurrentClipCoverage();
959 if (!maybe_current_clip_coverage.has_value()) {
960 return std::nullopt;
961 }
962
963 auto current_clip_coverage = maybe_current_clip_coverage.value();
964
965 // The maximum coverage of the subpass. Subpasses textures should never
966 // extend outside the parent pass texture or the current clip coverage.
967 std::optional<Rect> maybe_coverage_limit =
968 Rect::MakeOriginSize(GetGlobalPassPosition(),
969 Size(render_passes_.back()
970 .inline_pass_context->GetTexture()
971 ->GetSize()))
972 .Intersection(current_clip_coverage);
973
974 if (!maybe_coverage_limit.has_value() || maybe_coverage_limit->IsEmpty()) {
975 return std::nullopt;
976 }
977
978 return maybe_coverage_limit->Intersection(
979 Rect::MakeSize(render_target_.GetRenderTargetSize()));
980 }
981
982 void Canvas::SaveLayer(const Paint& paint,
983 std::optional<Rect> bounds,
984 const flutter::DlImageFilter* backdrop_filter,
985 ContentBoundsPromise bounds_promise,
986 uint32_t total_content_depth,
987 bool can_distribute_opacity) {
988 TRACE_EVENT0("flutter", "Canvas::saveLayer");
989 if (IsSkipping()) {
990 return SkipUntilMatchingRestore(total_content_depth);
991 }
992
993 auto maybe_coverage_limit = GetLocalCoverageLimit();
994 if (!maybe_coverage_limit.has_value()) {
995 return SkipUntilMatchingRestore(total_content_depth);
996 }
997 auto coverage_limit = maybe_coverage_limit.value();
998
999 if (can_distribute_opacity && !backdrop_filter &&
1000 Paint::CanApplyOpacityPeephole(paint) &&
1001 bounds_promise != ContentBoundsPromise::kMayClipContents) {
1002 Save(total_content_depth);
1003 transform_stack_.back().distributed_opacity *= paint.color.alpha;
1004 return;
1005 }
1006
1007 std::shared_ptr<FilterContents> filter_contents = paint.WithImageFilter(
1008 Rect(), transform_stack_.back().transform,
1009 Entity::RenderingMode::kSubpassPrependSnapshotTransform);
1010
1011 std::optional<Rect> maybe_subpass_coverage = ComputeSaveLayerCoverage(
1012 bounds.value_or(Rect::MakeMaximum()),
1013 transform_stack_.back().transform, //
1014 coverage_limit, //
1015 filter_contents, //
1016 /*flood_output_coverage=*/
1017 Entity::IsBlendModeDestructive(paint.blend_mode), //
1018 /*flood_input_coverage=*/!!backdrop_filter //
1019 );
1020
1021 if (!maybe_subpass_coverage.has_value()) {
1022 return SkipUntilMatchingRestore(total_content_depth);
1023 }
1024
1025 auto subpass_coverage = maybe_subpass_coverage.value();
1026
1027 // When an image filter is present, clamp to avoid flicking due to nearest
1028 // sampled image. For other cases, round out to ensure than any geometry is
1029 // not cut off.
1030 //
1031 // See also this bug: https://github.com/flutter/flutter/issues/144213
1032 //
1033 // TODO(jonahwilliams): this could still round out for filters that use decal
1034 // sampling mode.
1035 ISize subpass_size;
1036 bool did_round_out = false;
1037 if (paint.image_filter) {
1038 subpass_size = ISize(subpass_coverage.GetSize());
1039 } else {
1040 did_round_out = true;
1041 subpass_size = ISize(IRect::RoundOut(subpass_coverage).GetSize());
1042 }
1043 if (subpass_size.IsEmpty()) {
1044 return SkipUntilMatchingRestore(total_content_depth);
1045 }
1046
1047 // When there are scaling filters present, these contents may exceed the
1048 // maximum texture size. Perform a clamp here, which may cause rendering
1049 // artifacts.
1050 subpass_size = subpass_size.Min(renderer_.GetContext()
1051 ->GetCapabilities()
1052 ->GetMaximumRenderPassAttachmentSize());
1053
1054 // Backdrop filter state, ignored if there is no BDF.
1055 std::shared_ptr<FilterContents> backdrop_filter_contents;
1056 Point local_position = {0, 0};
1057 if (backdrop_filter) {
1058 local_position = subpass_coverage.GetOrigin() - GetGlobalPassPosition();
1059 Canvas::BackdropFilterProc backdrop_filter_proc =
1060 [backdrop_filter = backdrop_filter](
1061 const FilterInput::Ref& input, const Matrix& effect_transform,
1062 Entity::RenderingMode rendering_mode) {
1063 auto filter = WrapInput(backdrop_filter, input);
1064 filter->SetEffectTransform(effect_transform);
1065 filter->SetRenderingMode(rendering_mode);
1066 return filter;
1067 };
1068
1069 auto input_texture = FlipBackdrop(render_passes_, //
1070 GetGlobalPassPosition(), //
1071 clip_coverage_stack_, //
1072 renderer_ //
1073 );
1074 if (!input_texture) {
1075 // Validation failures are logged in FlipBackdrop.
1076 return;
1077 }
1078
1079 backdrop_filter_contents = backdrop_filter_proc(
1080 FilterInput::Make(std::move(input_texture)),
1081 transform_stack_.back().transform.Basis(),
1082 // When the subpass has a translation that means the math with
1083 // the snapshot has to be different.
1084 transform_stack_.back().transform.HasTranslation()
1085 ? Entity::RenderingMode::kSubpassPrependSnapshotTransform
1086 : Entity::RenderingMode::kSubpassAppendSnapshotTransform);
1087 }
1088
1089 // When applying a save layer, absorb any pending distributed opacity.
1090 Paint paint_copy = paint;
1091 paint_copy.color.alpha *= transform_stack_.back().distributed_opacity;
1092 transform_stack_.back().distributed_opacity = 1.0;
1093
1094 render_passes_.push_back(
1095 LazyRenderingConfig(renderer_, //
1096 CreateRenderTarget(renderer_, //
1097 subpass_size, //
1098 Color::BlackTransparent() //
1099 )));
1100 save_layer_state_.push_back(SaveLayerState{paint_copy, subpass_coverage});
1101
1102 CanvasStackEntry entry;
1103 entry.transform = transform_stack_.back().transform;
1104 entry.clip_depth = current_depth_ + total_content_depth;
1105 FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
1106 << entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
1107 << " after allocating " << total_content_depth;
1108 entry.clip_height = transform_stack_.back().clip_height;
1109 entry.rendering_mode = Entity::RenderingMode::kSubpassAppendSnapshotTransform;
1110 entry.did_round_out = did_round_out;
1111 transform_stack_.emplace_back(entry);
1112
1113 // Start non-collapsed subpasses with a fresh clip coverage stack limited by
1114 // the subpass coverage. This is important because image filters applied to
1115 // save layers may transform the subpass texture after it's rendered,
1116 // causing parent clip coverage to get misaligned with the actual area that
1117 // the subpass will affect in the parent pass.
1118 clip_coverage_stack_.PushSubpass(subpass_coverage, GetClipHeight());
1119
1120 if (backdrop_filter_contents) {
1121 // Render the backdrop entity.
1122 Entity backdrop_entity;
1123 backdrop_entity.SetContents(std::move(backdrop_filter_contents));
1124 backdrop_entity.SetTransform(
1125 Matrix::MakeTranslation(Vector3(-local_position)));
1126 backdrop_entity.SetClipDepth(std::numeric_limits<uint32_t>::max());
1127
1128 backdrop_entity.Render(
1129 renderer_,
1130 *render_passes_.back().inline_pass_context->GetRenderPass(0).pass);
1131 }
1132 }
1133
1134 bool Canvas::Restore() {
1135 FML_DCHECK(transform_stack_.size() > 0);
1136 if (transform_stack_.size() == 1) {
1137 return false;
1138 }
1139
1140 // This check is important to make sure we didn't exceed the depth
1141 // that the clips were rendered at while rendering any of the
1142 // rendering ops. It is OK for the current depth to equal the
1143 // outgoing clip depth because that means the clipping would have
1144 // been successful up through the last rendering op, but it cannot
1145 // be greater.
1146 // Also, we bump the current rendering depth to the outgoing clip
1147 // depth so that future rendering operations are not clipped by
1148 // any of the pixels set by the expiring clips. It is OK for the
1149 // estimates used to determine the clip depth in save/saveLayer
1150 // to be overly conservative, but we need to jump the depth to
1151 // the clip depth so that the next rendering op will get a
1152 // larger depth (it will pre-increment the current_depth_ value).
1153 FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
1154 << current_depth_ << " <=? " << transform_stack_.back().clip_depth;
1155 current_depth_ = transform_stack_.back().clip_depth;
1156
1157 if (IsSkipping()) {
1158 transform_stack_.pop_back();
1159 return true;
1160 }
1161
1162 if (transform_stack_.back().rendering_mode ==
1163 Entity::RenderingMode::kSubpassAppendSnapshotTransform ||
1164 transform_stack_.back().rendering_mode ==
1165 Entity::RenderingMode::kSubpassPrependSnapshotTransform) {
1166 auto lazy_render_pass = std::move(render_passes_.back());
1167 render_passes_.pop_back();
1168 // Force the render pass to be constructed if it never was.
1169 lazy_render_pass.inline_pass_context->GetRenderPass(0);
1170
1171 SaveLayerState save_layer_state = save_layer_state_.back();
1172 save_layer_state_.pop_back();
1173 auto global_pass_position = GetGlobalPassPosition();
1174
1175 std::shared_ptr<Contents> contents = CreateContentsForSubpassTarget(
1176 save_layer_state.paint, //
1177 lazy_render_pass.inline_pass_context->GetTexture(), //
1178 Matrix::MakeTranslation(Vector3{-global_pass_position}) * //
1179 transform_stack_.back().transform //
1180 );
1181
1182 lazy_render_pass.inline_pass_context->EndPass();
1183
1184 // Round the subpass texture position for pixel alignment with the parent
1185 // pass render target. By default, we draw subpass textures with nearest
1186 // sampling, so aligning here is important for avoiding visual nearest
1187 // sampling errors caused by limited floating point precision when
1188 // straddling a half pixel boundary.
1189 Point subpass_texture_position;
1190 if (transform_stack_.back().did_round_out) {
1191 // Subpass coverage was rounded out, origin potentially moved "down" by
1192 // as much as a pixel.
1193 subpass_texture_position =
1194 (save_layer_state.coverage.GetOrigin() - global_pass_position)
1195 .Floor();
1196 } else {
1197 // Subpass coverage was truncated. Pick the closest phyiscal pixel.
1198 subpass_texture_position =
1199 (save_layer_state.coverage.GetOrigin() - global_pass_position)
1200 .Round();
1201 }
1202
1203 Entity element_entity;
1204 element_entity.SetClipDepth(++current_depth_);
1205 element_entity.SetContents(std::move(contents));
1206 element_entity.SetBlendMode(save_layer_state.paint.blend_mode);
1207 element_entity.SetTransform(
1208 Matrix::MakeTranslation(Vector3(subpass_texture_position)));
1209
1210 if (element_entity.GetBlendMode() > Entity::kLastPipelineBlendMode) {
1211 if (renderer_.GetDeviceCapabilities().SupportsFramebufferFetch()) {
1212 ApplyFramebufferBlend(element_entity);
1213 } else {
1214 // End the active pass and flush the buffer before rendering "advanced"
1215 // blends. Advanced blends work by binding the current render target
1216 // texture as an input ("destination"), blending with a second texture
1217 // input ("source"), writing the result to an intermediate texture, and
1218 // finally copying the data from the intermediate texture back to the
1219 // render target texture. And so all of the commands that have written
1220 // to the render target texture so far need to execute before it's bound
1221 // for blending (otherwise the blend pass will end up executing before
1222 // all the previous commands in the active pass).
1223 auto input_texture =
1224 FlipBackdrop(render_passes_, GetGlobalPassPosition(),
1225 clip_coverage_stack_, renderer_);
1226 if (!input_texture) {
1227 return false;
1228 }
1229
1230 FilterInput::Vector inputs = {
1231 FilterInput::Make(input_texture,
1232 element_entity.GetTransform().Invert()),
1233 FilterInput::Make(element_entity.GetContents())};
1234 auto contents = ColorFilterContents::MakeBlend(
1235 element_entity.GetBlendMode(), inputs);
1236 contents->SetCoverageHint(element_entity.GetCoverage());
1237 element_entity.SetContents(std::move(contents));
1238 element_entity.SetBlendMode(BlendMode::kSource);
1239 }
1240 }
1241
1242 element_entity.Render(
1243 renderer_, //
1244 *render_passes_.back().inline_pass_context->GetRenderPass(0).pass //
1245 );
1246 clip_coverage_stack_.PopSubpass();
1247 transform_stack_.pop_back();
1248
1249 // We don't need to restore clips if a saveLayer was performed, as the clip
1250 // state is per render target, and no more rendering operations will be
1251 // performed as the render target workloaded is completed in the restore.
1252 return true;
1253 }
1254
1255 size_t num_clips = transform_stack_.back().num_clips;
1256 transform_stack_.pop_back();
1257
1258 if (num_clips > 0) {
1259 Entity entity;
1260 entity.SetTransform(
1261 Matrix::MakeTranslation(Vector3(-GetGlobalPassPosition())) *
1262 GetCurrentTransform());
1263 // This path is empty because ClipRestoreContents just generates a quad that
1264 // takes up the full render target.
1265 auto clip_restore = std::make_shared<ClipRestoreContents>();
1266 clip_restore->SetRestoreHeight(GetClipHeight());
1267 entity.SetContents(std::move(clip_restore));
1268
1269 auto current_clip_coverage = clip_coverage_stack_.CurrentClipCoverage();
1270 if (current_clip_coverage.has_value()) {
1271 // Entity transforms are relative to the current pass position, so we need
1272 // to check clip coverage in the same space.
1273 current_clip_coverage =
1274 current_clip_coverage->Shift(-GetGlobalPassPosition());
1275 }
1276
1277 auto clip_coverage = entity.GetClipCoverage(current_clip_coverage);
1278 if (clip_coverage.coverage.has_value()) {
1279 clip_coverage.coverage =
1280 clip_coverage.coverage->Shift(GetGlobalPassPosition());
1281 }
1282
1283 EntityPassClipStack::ClipStateResult clip_state_result =
1284 clip_coverage_stack_.ApplyClipState(clip_coverage, entity,
1285 GetClipHeightFloor(),
1286 GetGlobalPassPosition());
1287
1288 if (clip_state_result.clip_did_change) {
1289 // We only need to update the pass scissor if the clip state has changed.
1290 SetClipScissor(
1291 clip_coverage_stack_.CurrentClipCoverage(), //
1292 *render_passes_.back().inline_pass_context->GetRenderPass(0).pass, //
1293 GetGlobalPassPosition() //
1294 );
1295 }
1296
1297 if (!clip_state_result.should_render) {
1298 return true;
1299 }
1300
1301 entity.Render(
1302 renderer_,
1303 *render_passes_.back().inline_pass_context->GetRenderPass(0).pass);
1304 }
1305
1306 return true;
1307 }
1308
1309 void Canvas::DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
1310 Point position,
1311 const Paint& paint) {
1312 Entity entity;
1313 entity.SetClipDepth(GetClipHeight());
1314 entity.SetBlendMode(paint.blend_mode);
1315
1316 auto text_contents = std::make_shared<TextContents>();
1317 text_contents->SetTextFrame(text_frame);
1318 text_contents->SetForceTextColor(paint.mask_blur_descriptor.has_value());
1319 text_contents->SetScale(GetCurrentTransform().GetMaxBasisLengthXY());
1320 text_contents->SetColor(paint.color);
1321 text_contents->SetOffset(position);
1322 text_contents->SetTextProperties(paint.color, //
1323 paint.style == Paint::Style::kStroke, //
1324 paint.stroke_width, //
1325 paint.stroke_cap, //
1326 paint.stroke_join, //
1327 paint.stroke_miter //
1328 );
1329
1330 entity.SetTransform(GetCurrentTransform() *
1331 Matrix::MakeTranslation(position));
1332
1333 // TODO(bdero): This mask blur application is a hack. It will always wind up
1334 // doing a gaussian blur that affects the color source itself
1335 // instead of just the mask. The color filter text support
1336 // needs to be reworked in order to interact correctly with
1337 // mask filters.
1338 // https://github.com/flutter/flutter/issues/133297
1339 entity.SetContents(paint.WithFilters(paint.WithMaskBlur(
1340 std::move(text_contents), true, GetCurrentTransform())));
1341
1342 AddRenderEntityToCurrentPass(entity, false);
1343 }
1344
1345 void Canvas::AddRenderEntityWithFiltersToCurrentPass(Entity& entity,
1346 const Geometry* geometry,
1347 const Paint& paint,
1348 bool reuse_depth) {
1349 std::shared_ptr<ColorSourceContents> contents = paint.CreateContents();
1350 if (!paint.color_filter && !paint.invert_colors && !paint.image_filter &&
1351 !paint.mask_blur_descriptor.has_value()) {
1352 contents->SetGeometry(geometry);
1353 entity.SetContents(std::move(contents));
1354 AddRenderEntityToCurrentPass(entity, reuse_depth);
1355 return;
1356 }
1357
1358 // Attempt to apply the color filter on the CPU first.
1359 // Note: This is not just an optimization; some color sources rely on
1360 // CPU-applied color filters to behave properly.
1361 bool needs_color_filter = paint.color_filter || paint.invert_colors;
1362 if (needs_color_filter &&
1363 contents->ApplyColorFilter([&](Color color) -> Color {
1364 if (paint.color_filter) {
1365 color = GetCPUColorFilterProc(paint.color_filter)(color);
1366 }
1367 if (paint.invert_colors) {
1368 color = color.ApplyColorMatrix(kColorInversion);
1369 }
1370 return color;
1371 })) {
1372 needs_color_filter = false;
1373 }
1374
1375 bool can_apply_mask_filter = geometry->CanApplyMaskFilter();
1376 contents->SetGeometry(geometry);
1377
1378 if (can_apply_mask_filter && paint.mask_blur_descriptor.has_value()) {
1379 // If there's a mask blur and we need to apply the color filter on the GPU,
1380 // we need to be careful to only apply the color filter to the source
1381 // colors. CreateMaskBlur is able to handle this case.
1382 RectGeometry out_rect(Rect{});
1383 auto filter_contents = paint.mask_blur_descriptor->CreateMaskBlur(
1384 contents, needs_color_filter ? paint.color_filter : nullptr,
1385 needs_color_filter ? paint.invert_colors : false, &out_rect);
1386 entity.SetContents(std::move(filter_contents));
1387 AddRenderEntityToCurrentPass(entity, reuse_depth);
1388 return;
1389 }
1390
1391 std::shared_ptr<Contents> contents_copy = std::move(contents);
1392
1393 // Image input types will directly set their color filter,
1394 // if any. See `TiledTextureContents.SetColorFilter`.
1395 if (needs_color_filter &&
1396 (!paint.color_source ||
1397 paint.color_source->type() != flutter::DlColorSourceType::kImage)) {
1398 if (paint.color_filter) {
1399 contents_copy = WrapWithGPUColorFilter(
1400 paint.color_filter, FilterInput::Make(std::move(contents_copy)),
1401 ColorFilterContents::AbsorbOpacity::kYes);
1402 }
1403 if (paint.invert_colors) {
1404 contents_copy =
1405 WrapWithInvertColors(FilterInput::Make(std::move(contents_copy)),
1406 ColorFilterContents::AbsorbOpacity::kYes);
1407 }
1408 }
1409
1410 if (paint.image_filter) {
1411 std::shared_ptr<FilterContents> filter = WrapInput(
1412 paint.image_filter, FilterInput::Make(std::move(contents_copy)));
1413 filter->SetRenderingMode(Entity::RenderingMode::kDirect);
1414 entity.SetContents(filter);
1415 AddRenderEntityToCurrentPass(entity, reuse_depth);
1416 return;
1417 }
1418
1419 entity.SetContents(std::move(contents_copy));
1420 AddRenderEntityToCurrentPass(entity, reuse_depth);
1421 }
1422
1423 void Canvas::AddRenderEntityToCurrentPass(Entity& entity, bool reuse_depth) {
1424 if (IsSkipping()) {
1425 return;
1426 }
1427
1428 entity.SetTransform(
1429 Matrix::MakeTranslation(Vector3(-GetGlobalPassPosition())) *
1430 entity.GetTransform());
1431 entity.SetInheritedOpacity(transform_stack_.back().distributed_opacity);
1432 if (entity.GetBlendMode() == BlendMode::kSourceOver &&
1433 entity.GetContents()->IsOpaque(entity.GetTransform())) {
1434 entity.SetBlendMode(BlendMode::kSource);
1435 }
1436
1437 // If the entity covers the current render target and is a solid color, then
1438 // conditionally update the backdrop color to its solid color value blended
1439 // with the current backdrop.
1440 if (render_passes_.back().IsApplyingClearColor()) {
1441 std::optional<Color> maybe_color = entity.AsBackgroundColor(
1442 render_passes_.back().inline_pass_context->GetTexture()->GetSize());
1443 if (maybe_color.has_value()) {
1444 Color color = maybe_color.value();
1445 RenderTarget& render_target = render_passes_.back()
1446 .inline_pass_context->GetPassTarget()
1447 .GetRenderTarget();
1448 ColorAttachment attachment =
1449 render_target.GetColorAttachments().find(0u)->second;
1450 // Attachment.clear color needs to be premultiplied at all times, but the
1451 // Color::Blend function requires unpremultiplied colors.
1452 attachment.clear_color = attachment.clear_color.Unpremultiply()
1453 .Blend(color, entity.GetBlendMode())
1454 .Premultiply();
1455 render_target.SetColorAttachment(attachment, 0u);
1456 return;
1457 }
1458 }
1459
1460 if (!reuse_depth) {
1461 ++current_depth_;
1462 }
1463 // We can render at a depth up to and including the depth of the currently
1464 // active clips and we will still be clipped out, but we cannot render at
1465 // a depth that is greater than the current clips or we will not be clipped.
1466 FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
1467 << current_depth_ << " <=? " << transform_stack_.back().clip_depth;
1468 entity.SetClipDepth(current_depth_);
1469
1470 if (entity.GetBlendMode() > Entity::kLastPipelineBlendMode) {
1471 if (renderer_.GetDeviceCapabilities().SupportsFramebufferFetch()) {
1472 ApplyFramebufferBlend(entity);
1473 } else {
1474 // End the active pass and flush the buffer before rendering "advanced"
1475 // blends. Advanced blends work by binding the current render target
1476 // texture as an input ("destination"), blending with a second texture
1477 // input ("source"), writing the result to an intermediate texture, and
1478 // finally copying the data from the intermediate texture back to the
1479 // render target texture. And so all of the commands that have written
1480 // to the render target texture so far need to execute before it's bound
1481 // for blending (otherwise the blend pass will end up executing before
1482 // all the previous commands in the active pass).
1483 auto input_texture = FlipBackdrop(render_passes_, //
1484 GetGlobalPassPosition(), //
1485 clip_coverage_stack_, //
1486 renderer_ //
1487 );
1488 if (!input_texture) {
1489 return;
1490 }
1491
1492 // The coverage hint tells the rendered Contents which portion of the
1493 // rendered output will actually be used, and so we set this to the
1494 // current clip coverage (which is the max clip bounds). The contents may
1495 // optionally use this hint to avoid unnecessary rendering work.
1496 auto element_coverage_hint = entity.GetContents()->GetCoverageHint();
1497 entity.GetContents()->SetCoverageHint(Rect::Intersection(
1498 element_coverage_hint, clip_coverage_stack_.CurrentClipCoverage()));
1499
1500 FilterInput::Vector inputs = {
1501 FilterInput::Make(input_texture, entity.GetTransform().Invert()),
1502 FilterInput::Make(entity.GetContents())};
1503 auto contents =
1504 ColorFilterContents::MakeBlend(entity.GetBlendMode(), inputs);
1505 entity.SetContents(std::move(contents));
1506 entity.SetBlendMode(BlendMode::kSource);
1507 }
1508 }
1509
1510 InlinePassContext::RenderPassResult result =
1511 render_passes_.back().inline_pass_context->GetRenderPass(0);
1512 if (!result.pass) {
1513 // Failure to produce a render pass should be explained by specific errors
1514 // in `InlinePassContext::GetRenderPass()`, so avoid log spam and don't
1515 // append a validation log here.
1516 return;
1517 }
1518
1519 entity.Render(renderer_, *result.pass);
1520 }
1521
1522 void Canvas::AddClipEntityToCurrentPass(Entity& entity) {
1523 if (IsSkipping()) {
1524 return;
1525 }
1526
1527 auto transform = entity.GetTransform();
1528 entity.SetTransform(
1529 Matrix::MakeTranslation(Vector3(-GetGlobalPassPosition())) * transform);
1530
1531 // Ideally the clip depth would be greater than the current rendering
1532 // depth because any rendering calls that follow this clip operation will
1533 // pre-increment the depth and then be rendering above our clip depth,
1534 // but that case will be caught by the CHECK in AddRenderEntity above.
1535 // In practice we sometimes have a clip set with no rendering after it
1536 // and in such cases the current depth will equal the clip depth.
1537 // Eventually the DisplayList should optimize these out, but it is hard
1538 // to know if a clip will actually be used in advance of storing it in
1539 // the DisplayList buffer.
1540 // See https://github.com/flutter/flutter/issues/147021
1541 FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
1542 << current_depth_ << " <=? " << transform_stack_.back().clip_depth;
1543 entity.SetClipDepth(transform_stack_.back().clip_depth);
1544
1545 auto current_clip_coverage = clip_coverage_stack_.CurrentClipCoverage();
1546 if (current_clip_coverage.has_value()) {
1547 // Entity transforms are relative to the current pass position, so we need
1548 // to check clip coverage in the same space.
1549 current_clip_coverage =
1550 current_clip_coverage->Shift(-GetGlobalPassPosition());
1551 }
1552
1553 auto clip_coverage = entity.GetClipCoverage(current_clip_coverage);
1554 if (clip_coverage.coverage.has_value()) {
1555 clip_coverage.coverage =
1556 clip_coverage.coverage->Shift(GetGlobalPassPosition());
1557 }
1558
1559 EntityPassClipStack::ClipStateResult clip_state_result =
1560 clip_coverage_stack_.ApplyClipState(
1561 clip_coverage, entity, GetClipHeightFloor(), GetGlobalPassPosition());
1562
1563 if (clip_state_result.clip_did_change) {
1564 // We only need to update the pass scissor if the clip state has changed.
1565 SetClipScissor(
1566 clip_coverage_stack_.CurrentClipCoverage(),
1567 *render_passes_.back().inline_pass_context->GetRenderPass(0).pass,
1568 GetGlobalPassPosition());
1569 }
1570
1571 if (!clip_state_result.should_render) {
1572 return;
1573 }
1574
1575 entity.Render(
1576 renderer_,
1577 *render_passes_.back().inline_pass_context->GetRenderPass(0).pass);
1578 }
1579
1580 bool Canvas::BlitToOnscreen() {
1581 auto command_buffer = renderer_.GetContext()->CreateCommandBuffer();
1582 command_buffer->SetLabel("EntityPass Root Command Buffer");
1583 auto offscreen_target = render_passes_.back()
1584 .inline_pass_context->GetPassTarget()
1585 .GetRenderTarget();
1586
1587 if (renderer_.GetContext()
1588 ->GetCapabilities()
1589 ->SupportsTextureToTextureBlits()) {
1590 auto blit_pass = command_buffer->CreateBlitPass();
1591 blit_pass->AddCopy(offscreen_target.GetRenderTargetTexture(),
1592 render_target_.GetRenderTargetTexture());
1593 if (!blit_pass->EncodeCommands(
1594 renderer_.GetContext()->GetResourceAllocator())) {
1595 VALIDATION_LOG << "Failed to encode root pass blit command.";
1596 return false;
1597 }
1598 if (!renderer_.GetContext()
1599 ->GetCommandQueue()
1600 ->Submit({command_buffer})
1601 .ok()) {
1602 return false;
1603 }
1604 } else {
1605 auto render_pass = command_buffer->CreateRenderPass(render_target_);
1606 render_pass->SetLabel("EntityPass Root Render Pass");
1607
1608 {
1609 auto size_rect = Rect::MakeSize(offscreen_target.GetRenderTargetSize());
1610 auto contents = TextureContents::MakeRect(size_rect);
1611 contents->SetTexture(offscreen_target.GetRenderTargetTexture());
1612 contents->SetSourceRect(size_rect);
1613 contents->SetLabel("Root pass blit");
1614
1615 Entity entity;
1616 entity.SetContents(contents);
1617 entity.SetBlendMode(BlendMode::kSource);
1618
1619 if (!entity.Render(renderer_, *render_pass)) {
1620 VALIDATION_LOG << "Failed to render EntityPass root blit.";
1621 return false;
1622 }
1623 }
1624
1625 if (!render_pass->EncodeCommands()) {
1626 VALIDATION_LOG << "Failed to encode root pass command buffer.";
1627 return false;
1628 }
1629 if (!renderer_.GetContext()
1630 ->GetCommandQueue()
1631 ->Submit({command_buffer})
1632 .ok()) {
1633 return false;
1634 }
1635 }
1636 return true;
1637 }
1638
1639 void Canvas::EndReplay() {
1640 FML_DCHECK(render_passes_.size() == 1u);
1641 render_passes_.back().inline_pass_context->GetRenderPass(0);
1642 render_passes_.back().inline_pass_context->EndPass();
1643
1644 // If requires_readback_ was true, then we rendered to an offscreen texture
1645 // instead of to the onscreen provided in the render target. Now we need to
1646 // draw or blit the offscreen back to the onscreen.
1647 if (requires_readback_) {
1648 BlitToOnscreen();
1649 }
1650
1651 render_passes_.clear();
1652 renderer_.GetRenderTargetCache()->End();
1653 clip_geometry_.clear();
1654
1655 Reset();
1656 Initialize(initial_cull_rect_);
1657 }
1658
1659 } // 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:681
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:603
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:982
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:1309
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:758
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::Canvas::DrawLine
void DrawLine(const Point &p0, const Point &p1, const Paint &paint)
Definition: canvas.cc:546
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:555
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:220
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:951
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:222
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:628
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:1134
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:712
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:868
impeller::Canvas::ClipGeometry
void ClipGeometry(std::unique_ptr< Geometry > geometry, Entity::ClipOperation clip_op)
Definition: canvas.cc:651
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:698
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:573
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:221
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::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:934
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