| Flutter Impeller |

blend_filter_contents.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/entity/contents/filters/blend_filter_contents.h"
6
7 #include <array>
8 #include <memory>
9 #include <optional>
10
11 #include "impeller/base/strings.h"
12 #include "impeller/core/formats.h"
13 #include "impeller/entity/contents/anonymous_contents.h"
14 #include "impeller/entity/contents/content_context.h"
15 #include "impeller/entity/contents/contents.h"
16 #include "impeller/entity/contents/filters/color_filter_contents.h"
17 #include "impeller/entity/contents/filters/inputs/filter_input.h"
18 #include "impeller/entity/contents/solid_color_contents.h"
19 #include "impeller/entity/entity.h"
20 #include "impeller/geometry/color.h"
21 #include "impeller/renderer/render_pass.h"
22 #include "impeller/renderer/snapshot.h"
23
24 namespace impeller {
25
26 std::optional<BlendMode> InvertPorterDuffBlend(BlendMode blend_mode) {
27 switch (blend_mode) {
28 case BlendMode::kClear:
29 return BlendMode::kClear;
30 case BlendMode::kSource:
31 return BlendMode::kDestination;
32 case BlendMode::kDestination:
33 return BlendMode::kSource;
34 case BlendMode::kSourceOver:
35 return BlendMode::kDestinationOver;
36 case BlendMode::kDestinationOver:
37 return BlendMode::kSourceOver;
38 case BlendMode::kSourceIn:
39 return BlendMode::kDestinationIn;
40 case BlendMode::kDestinationIn:
41 return BlendMode::kSourceIn;
42 case BlendMode::kSourceOut:
43 return BlendMode::kDestinationOut;
44 case BlendMode::kDestinationOut:
45 return BlendMode::kSourceOut;
46 case BlendMode::kSourceATop:
47 return BlendMode::kDestinationATop;
48 case BlendMode::kDestinationATop:
49 return BlendMode::kSourceATop;
50 case BlendMode::kXor:
51 return BlendMode::kXor;
52 case BlendMode::kPlus:
53 return BlendMode::kPlus;
54 case BlendMode::kModulate:
55 return BlendMode::kModulate;
56 default:
57 return std::nullopt;
58 }
59 }
60
61 BlendFilterContents::BlendFilterContents() {
62 SetBlendMode(BlendMode::kSourceOver);
63 }
64
65 BlendFilterContents::~BlendFilterContents() = default;
66
67 using PipelineProc = std::shared_ptr<Pipeline<PipelineDescriptor>> (
68 ContentContext::*)(ContentContextOptions) const;
69
70 template <typename TPipeline>
71 static std::optional<Entity> AdvancedBlend(
72 const FilterInput::Vector& inputs,
73 const ContentContext& renderer,
74 const Entity& entity,
75 const Rect& coverage,
76 BlendMode blend_mode,
77 std::optional<Color> foreground_color,
78 ColorFilterContents::AbsorbOpacity absorb_opacity,
79 PipelineProc pipeline_proc,
80 std::optional<Scalar> alpha) {
81 using VS = typename TPipeline::VertexShader;
82 using FS = typename TPipeline::FragmentShader;
83
84 //----------------------------------------------------------------------------
85 /// Handle inputs.
86 ///
87
88 const size_t total_inputs =
89 inputs.size() + (foreground_color.has_value() ? 1 : 0);
90 if (total_inputs < 2) {
91 return std::nullopt;
92 }
93
94 auto dst_snapshot =
95 inputs[0]->GetSnapshot("AdvancedBlend(Dst)", renderer, entity);
96 if (!dst_snapshot.has_value()) {
97 return std::nullopt;
98 }
99 auto maybe_dst_uvs = dst_snapshot->GetCoverageUVs(coverage);
100 if (!maybe_dst_uvs.has_value()) {
101 return std::nullopt;
102 }
103 auto dst_uvs = maybe_dst_uvs.value();
104
105 std::optional<Snapshot> src_snapshot;
106 std::array<Point, 4> src_uvs;
107 if (!foreground_color.has_value()) {
108 src_snapshot =
109 inputs[1]->GetSnapshot("AdvancedBlend(Src)", renderer, entity);
110 if (!src_snapshot.has_value()) {
111 if (!dst_snapshot.has_value()) {
112 return std::nullopt;
113 }
114 return Entity::FromSnapshot(dst_snapshot.value(), entity.GetBlendMode(),
115 entity.GetClipDepth());
116 }
117 auto maybe_src_uvs = src_snapshot->GetCoverageUVs(coverage);
118 if (!maybe_src_uvs.has_value()) {
119 if (!dst_snapshot.has_value()) {
120 return std::nullopt;
121 }
122 return Entity::FromSnapshot(dst_snapshot.value(), entity.GetBlendMode(),
123 entity.GetClipDepth());
124 }
125 src_uvs = maybe_src_uvs.value();
126 }
127
128 Rect subpass_coverage = coverage;
129 if (entity.GetContents()) {
130 auto coverage_hint = entity.GetContents()->GetCoverageHint();
131
132 if (coverage_hint.has_value()) {
133 auto maybe_subpass_coverage =
134 subpass_coverage.Intersection(*coverage_hint);
135 if (!maybe_subpass_coverage.has_value()) {
136 return std::nullopt; // Nothing to render.
137 }
138
139 subpass_coverage = *maybe_subpass_coverage;
140 }
141 }
142
143 //----------------------------------------------------------------------------
144 /// Render to texture.
145 ///
146
147 ContentContext::SubpassCallback callback = [&](const ContentContext& renderer,
148 RenderPass& pass) {
149 auto& host_buffer = renderer.GetTransientsBuffer();
150
151 auto size = pass.GetRenderTargetSize();
152 VertexBufferBuilder<typename VS::PerVertexData> vtx_builder;
153 vtx_builder.AddVertices({
154 {Point(0, 0), dst_uvs[0], src_uvs[0]},
155 {Point(size.width, 0), dst_uvs[1], src_uvs[1]},
156 {Point(0, size.height), dst_uvs[2], src_uvs[2]},
157 {Point(size.width, size.height), dst_uvs[3], src_uvs[3]},
158 });
159 auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
160
161 auto options = OptionsFromPass(pass);
162 options.primitive_type = PrimitiveType::kTriangleStrip;
163 options.blend_mode = BlendMode::kSource;
164 std::shared_ptr<Pipeline<PipelineDescriptor>> pipeline =
165 std::invoke(pipeline_proc, renderer, options);
166
167 #ifdef IMPELLER_DEBUG
168 pass.SetCommandLabel(
169 SPrintF("Advanced Blend Filter (%s)", BlendModeToString(blend_mode)));
170 #endif // IMPELLER_DEBUG
171 pass.SetVertexBuffer(std::move(vtx_buffer));
172 pass.SetPipeline(pipeline);
173
174 typename FS::BlendInfo blend_info;
175 typename VS::FrameInfo frame_info;
176
177 auto dst_sampler_descriptor = dst_snapshot->sampler_descriptor;
178 if (renderer.GetDeviceCapabilities().SupportsDecalSamplerAddressMode()) {
179 dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
180 dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
181 }
182 const std::unique_ptr<const Sampler>& dst_sampler =
183 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
184 dst_sampler_descriptor);
185 FS::BindTextureSamplerDst(pass, dst_snapshot->texture, dst_sampler);
186 frame_info.dst_y_coord_scale = dst_snapshot->texture->GetYCoordScale();
187 blend_info.dst_input_alpha =
188 absorb_opacity == ColorFilterContents::AbsorbOpacity::kYes
189 ? dst_snapshot->opacity
190 : 1.0;
191
192 if (foreground_color.has_value()) {
193 blend_info.color_factor = 1;
194 blend_info.color = foreground_color.value();
195 // This texture will not be sampled from due to the color factor. But
196 // this is present so that validation doesn't trip on a missing
197 // binding.
198 FS::BindTextureSamplerSrc(pass, dst_snapshot->texture, dst_sampler);
199 } else {
200 auto src_sampler_descriptor = src_snapshot->sampler_descriptor;
201 if (renderer.GetDeviceCapabilities().SupportsDecalSamplerAddressMode()) {
202 src_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
203 src_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
204 }
205 const std::unique_ptr<const Sampler>& src_sampler =
206 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
207 src_sampler_descriptor);
208 blend_info.color_factor = 0;
209 blend_info.src_input_alpha = src_snapshot->opacity;
210 FS::BindTextureSamplerSrc(pass, src_snapshot->texture, src_sampler);
211 frame_info.src_y_coord_scale = src_snapshot->texture->GetYCoordScale();
212 }
213 auto blend_uniform = host_buffer.EmplaceUniform(blend_info);
214 FS::BindBlendInfo(pass, blend_uniform);
215
216 frame_info.mvp = pass.GetOrthographicTransform() *
217 Matrix::MakeTranslation(coverage.GetOrigin() -
218 subpass_coverage.GetOrigin());
219
220 auto uniform_view = host_buffer.EmplaceUniform(frame_info);
221 VS::BindFrameInfo(pass, uniform_view);
222
223 return pass.Draw().ok();
224 };
225
226 fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
227 "Advanced Blend Filter", ISize(subpass_coverage.GetSize()), callback);
228 if (!render_target.ok()) {
229 return std::nullopt;
230 }
231
232 return Entity::FromSnapshot(
233 Snapshot{
234 .texture = render_target.value().GetRenderTargetTexture(),
235 .transform = Matrix::MakeTranslation(subpass_coverage.GetOrigin()),
236 // Since we absorbed the transform of the inputs and used the
237 // respective snapshot sampling modes when blending, pass on
238 // the default NN clamp sampler.
239 .sampler_descriptor = {},
240 .opacity = (absorb_opacity == ColorFilterContents::AbsorbOpacity::kYes
241 ? 1.0f
242 : dst_snapshot->opacity) *
243 alpha.value_or(1.0)},
244 entity.GetBlendMode(), entity.GetClipDepth());
245 }
246
247 std::optional<Entity> BlendFilterContents::CreateForegroundAdvancedBlend(
248 const std::shared_ptr<FilterInput>& input,
249 const ContentContext& renderer,
250 const Entity& entity,
251 const Rect& coverage,
252 Color foreground_color,
253 BlendMode blend_mode,
254 std::optional<Scalar> alpha,
255 ColorFilterContents::AbsorbOpacity absorb_opacity) const {
256 auto dst_snapshot =
257 input->GetSnapshot("ForegroundAdvancedBlend", renderer, entity);
258 if (!dst_snapshot.has_value()) {
259 return std::nullopt;
260 }
261
262 RenderProc render_proc = [foreground_color, coverage, dst_snapshot,
263 blend_mode, alpha, absorb_opacity](
264 const ContentContext& renderer,
265 const Entity& entity, RenderPass& pass) -> bool {
266 using VS = BlendScreenPipeline::VertexShader;
267 using FS = BlendScreenPipeline::FragmentShader;
268
269 auto& host_buffer = renderer.GetTransientsBuffer();
270
271 auto maybe_dst_uvs = dst_snapshot->GetCoverageUVs(coverage);
272 if (!maybe_dst_uvs.has_value()) {
273 return false;
274 }
275 auto dst_uvs = maybe_dst_uvs.value();
276
277 auto size = coverage.GetSize();
278 auto origin = coverage.GetOrigin();
279 VertexBufferBuilder<VS::PerVertexData> vtx_builder;
280 vtx_builder.AddVertices({
281 {origin, dst_uvs[0], dst_uvs[0]},
282 {Point(origin.x + size.width, origin.y), dst_uvs[1], dst_uvs[1]},
283 {Point(origin.x, origin.y + size.height), dst_uvs[2], dst_uvs[2]},
284 {Point(origin.x + size.width, origin.y + size.height), dst_uvs[3],
285 dst_uvs[3]},
286 });
287 auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
288
289 #ifdef IMPELLER_DEBUG
290 pass.SetCommandLabel(SPrintF("Foreground Advanced Blend Filter (%s)",
291 BlendModeToString(blend_mode)));
292 #endif // IMPELLER_DEBUG
293 pass.SetVertexBuffer(std::move(vtx_buffer));
294 pass.SetStencilReference(entity.GetClipDepth());
295 auto options = OptionsFromPass(pass);
296 options.primitive_type = PrimitiveType::kTriangleStrip;
297
298 switch (blend_mode) {
299 case BlendMode::kScreen:
300 pass.SetPipeline(renderer.GetBlendScreenPipeline(options));
301 break;
302 case BlendMode::kOverlay:
303 pass.SetPipeline(renderer.GetBlendOverlayPipeline(options));
304 break;
305 case BlendMode::kDarken:
306 pass.SetPipeline(renderer.GetBlendDarkenPipeline(options));
307 break;
308 case BlendMode::kLighten:
309 pass.SetPipeline(renderer.GetBlendLightenPipeline(options));
310 break;
311 case BlendMode::kColorDodge:
312 pass.SetPipeline(renderer.GetBlendColorDodgePipeline(options));
313 break;
314 case BlendMode::kColorBurn:
315 pass.SetPipeline(renderer.GetBlendColorBurnPipeline(options));
316 break;
317 case BlendMode::kHardLight:
318 pass.SetPipeline(renderer.GetBlendHardLightPipeline(options));
319 break;
320 case BlendMode::kSoftLight:
321 pass.SetPipeline(renderer.GetBlendSoftLightPipeline(options));
322 break;
323 case BlendMode::kDifference:
324 pass.SetPipeline(renderer.GetBlendDifferencePipeline(options));
325 break;
326 case BlendMode::kExclusion:
327 pass.SetPipeline(renderer.GetBlendExclusionPipeline(options));
328 break;
329 case BlendMode::kMultiply:
330 pass.SetPipeline(renderer.GetBlendMultiplyPipeline(options));
331 break;
332 case BlendMode::kHue:
333 pass.SetPipeline(renderer.GetBlendHuePipeline(options));
334 break;
335 case BlendMode::kSaturation:
336 pass.SetPipeline(renderer.GetBlendSaturationPipeline(options));
337 break;
338 case BlendMode::kColor:
339 pass.SetPipeline(renderer.GetBlendColorPipeline(options));
340 break;
341 case BlendMode::kLuminosity:
342 pass.SetPipeline(renderer.GetBlendLuminosityPipeline(options));
343 break;
344 default:
345 return false;
346 }
347
348 FS::BlendInfo blend_info;
349 VS::FrameInfo frame_info;
350
351 auto dst_sampler_descriptor = dst_snapshot->sampler_descriptor;
352 if (renderer.GetDeviceCapabilities().SupportsDecalSamplerAddressMode()) {
353 dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
354 dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
355 }
356 const std::unique_ptr<const Sampler>& dst_sampler =
357 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
358 dst_sampler_descriptor);
359 FS::BindTextureSamplerDst(pass, dst_snapshot->texture, dst_sampler);
360 frame_info.dst_y_coord_scale = dst_snapshot->texture->GetYCoordScale();
361 blend_info.dst_input_alpha =
362 absorb_opacity == ColorFilterContents::AbsorbOpacity::kYes
363 ? dst_snapshot->opacity * alpha.value_or(1.0)
364 : 1.0;
365
366 blend_info.color_factor = 1;
367 blend_info.color = foreground_color;
368 // This texture will not be sampled from due to the color factor. But
369 // this is present so that validation doesn't trip on a missing
370 // binding.
371 FS::BindTextureSamplerSrc(pass, dst_snapshot->texture, dst_sampler);
372
373 auto blend_uniform = host_buffer.EmplaceUniform(blend_info);
374 FS::BindBlendInfo(pass, blend_uniform);
375
376 frame_info.mvp = entity.GetShaderTransform(pass);
377
378 auto uniform_view = host_buffer.EmplaceUniform(frame_info);
379 VS::BindFrameInfo(pass, uniform_view);
380
381 return pass.Draw().ok();
382 };
383 CoverageProc coverage_proc =
384 [coverage](const Entity& entity) -> std::optional<Rect> {
385 return coverage.TransformBounds(entity.GetTransform());
386 };
387
388 auto contents = AnonymousContents::Make(render_proc, coverage_proc);
389
390 Entity sub_entity;
391 sub_entity.SetContents(std::move(contents));
392 sub_entity.SetClipDepth(entity.GetClipDepth());
393
394 return sub_entity;
395 }
396
397 static std::optional<Entity> PipelineBlend(
398 const FilterInput::Vector& inputs,
399 const ContentContext& renderer,
400 const Entity& entity,
401 const Rect& coverage,
402 BlendMode blend_mode,
403 std::optional<Color> foreground_color,
404 ColorFilterContents::AbsorbOpacity absorb_opacity,
405 std::optional<Scalar> alpha) {
406 using VS = BlendPipeline::VertexShader;
407 using FS = BlendPipeline::FragmentShader;
408
409 auto dst_snapshot =
410 inputs[0]->GetSnapshot("PipelineBlend(Dst)", renderer, entity);
411 if (!dst_snapshot.has_value()) {
412 return std::nullopt; // Nothing to render.
413 }
414
415 Rect subpass_coverage = coverage;
416 if (entity.GetContents()) {
417 auto coverage_hint = entity.GetContents()->GetCoverageHint();
418
419 if (coverage_hint.has_value()) {
420 auto maybe_subpass_coverage =
421 subpass_coverage.Intersection(*coverage_hint);
422 if (!maybe_subpass_coverage.has_value()) {
423 return std::nullopt; // Nothing to render.
424 }
425
426 subpass_coverage = *maybe_subpass_coverage;
427 }
428 }
429
430 ContentContext::SubpassCallback callback = [&](const ContentContext& renderer,
431 RenderPass& pass) {
432 auto& host_buffer = renderer.GetTransientsBuffer();
433
434 #ifdef IMPELLER_DEBUG
435 pass.SetCommandLabel(
436 SPrintF("Pipeline Blend Filter (%s)", BlendModeToString(blend_mode)));
437 #endif // IMPELLER_DEBUG
438 auto options = OptionsFromPass(pass);
439 options.primitive_type = PrimitiveType::kTriangleStrip;
440
441 auto add_blend_command = [&](std::optional<Snapshot> input) {
442 if (!input.has_value()) {
443 return false;
444 }
445 auto input_coverage = input->GetCoverage();
446 if (!input_coverage.has_value()) {
447 return false;
448 }
449
450 const std::unique_ptr<const Sampler>& sampler =
451 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
452 input->sampler_descriptor);
453 FS::BindTextureSamplerSrc(pass, input->texture, sampler);
454
455 auto size = input->texture->GetSize();
456 VertexBufferBuilder<VS::PerVertexData> vtx_builder;
457 vtx_builder.AddVertices({
458 {Point(0, 0), Point(0, 0)},
459 {Point(size.width, 0), Point(1, 0)},
460 {Point(0, size.height), Point(0, 1)},
461 {Point(size.width, size.height), Point(1, 1)},
462 });
463 pass.SetVertexBuffer(vtx_builder.CreateVertexBuffer(host_buffer));
464
465 VS::FrameInfo frame_info;
466 frame_info.mvp = pass.GetOrthographicTransform() *
467 Matrix::MakeTranslation(-subpass_coverage.GetOrigin()) *
468 input->transform;
469 frame_info.texture_sampler_y_coord_scale =
470 input->texture->GetYCoordScale();
471
472 FS::FragInfo frag_info;
473 frag_info.input_alpha =
474 absorb_opacity == ColorFilterContents::AbsorbOpacity::kYes
475 ? input->opacity
476 : 1.0;
477 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
478 VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
479
480 return pass.Draw().ok();
481 };
482
483 // Draw the first texture using kSource.
484 options.blend_mode = BlendMode::kSource;
485 pass.SetPipeline(renderer.GetBlendPipeline(options));
486 if (!add_blend_command(dst_snapshot)) {
487 return true;
488 }
489
490 // Write subsequent textures using the selected blend mode.
491
492 if (inputs.size() >= 2) {
493 options.blend_mode = blend_mode;
494 pass.SetPipeline(renderer.GetBlendPipeline(options));
495
496 for (auto texture_i = inputs.begin() + 1; texture_i < inputs.end();
497 texture_i++) {
498 auto src_input = texture_i->get()->GetSnapshot("PipelineBlend(Src)",
499 renderer, entity);
500 if (!add_blend_command(src_input)) {
501 return true;
502 }
503 }
504 }
505
506 // If a foreground color is set, blend it in.
507
508 if (foreground_color.has_value()) {
509 auto contents = std::make_shared<SolidColorContents>();
510 contents->SetGeometry(
511 Geometry::MakeRect(Rect::MakeSize(pass.GetRenderTargetSize())));
512 contents->SetColor(foreground_color.value());
513
514 Entity foreground_entity;
515 foreground_entity.SetBlendMode(blend_mode);
516 foreground_entity.SetContents(contents);
517 if (!foreground_entity.Render(renderer, pass)) {
518 return false;
519 }
520 }
521
522 return true;
523 };
524
525 fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
526 "Pipeline Blend Filter", ISize(subpass_coverage.GetSize()), callback);
527
528 if (!render_target.ok()) {
529 return std::nullopt;
530 }
531
532 return Entity::FromSnapshot(
533 Snapshot{
534 .texture = render_target.value().GetRenderTargetTexture(),
535 .transform = Matrix::MakeTranslation(subpass_coverage.GetOrigin()),
536 // Since we absorbed the transform of the inputs and used the
537 // respective snapshot sampling modes when blending, pass on
538 // the default NN clamp sampler.
539 .sampler_descriptor = {},
540 .opacity = (absorb_opacity == ColorFilterContents::AbsorbOpacity::kYes
541 ? 1.0f
542 : dst_snapshot->opacity) *
543 alpha.value_or(1.0)},
544 entity.GetBlendMode(), entity.GetClipDepth());
545 }
546
547 #define BLEND_CASE(mode) \
548 case BlendMode::k##mode: \
549 advanced_blend_proc_ = \
550 [](const FilterInput::Vector& inputs, const ContentContext& renderer, \
551 const Entity& entity, const Rect& coverage, BlendMode blend_mode, \
552 std::optional<Color> fg_color, \
553 ColorFilterContents::AbsorbOpacity absorb_opacity, \
554 std::optional<Scalar> alpha) { \
555 PipelineProc p = &ContentContext::GetBlend##mode##Pipeline; \
556 return AdvancedBlend<Blend##mode##Pipeline>( \
557 inputs, renderer, entity, coverage, blend_mode, fg_color, \
558 absorb_opacity, p, alpha); \
559 }; \
560 break;
561
562 void BlendFilterContents::SetBlendMode(BlendMode blend_mode) {
563 if (blend_mode > Entity::kLastAdvancedBlendMode) {
564 VALIDATION_LOG << "Invalid blend mode " << static_cast<int>(blend_mode)
565 << " assigned to BlendFilterContents.";
566 }
567
568 blend_mode_ = blend_mode;
569
570 if (blend_mode > Entity::kLastPipelineBlendMode) {
571 switch (blend_mode) {
572 BLEND_CASE(Screen)
573 BLEND_CASE(Overlay)
574 BLEND_CASE(Darken)
575 BLEND_CASE(Lighten)
576 BLEND_CASE(ColorDodge)
577 BLEND_CASE(ColorBurn)
578 BLEND_CASE(HardLight)
579 BLEND_CASE(SoftLight)
580 BLEND_CASE(Difference)
581 BLEND_CASE(Exclusion)
582 BLEND_CASE(Multiply)
583 BLEND_CASE(Hue)
584 BLEND_CASE(Saturation)
585 BLEND_CASE(Color)
586 BLEND_CASE(Luminosity)
587 default:
588 FML_UNREACHABLE();
589 }
590 }
591 }
592
593 void BlendFilterContents::SetForegroundColor(std::optional<Color> color) {
594 foreground_color_ = color;
595 }
596
597 std::optional<Entity> BlendFilterContents::RenderFilter(
598 const FilterInput::Vector& inputs,
599 const ContentContext& renderer,
600 const Entity& entity,
601 const Matrix& effect_transform,
602 const Rect& coverage,
603 const std::optional<Rect>& coverage_hint) const {
604 if (inputs.empty()) {
605 return std::nullopt;
606 }
607
608 if (inputs.size() == 1 && !foreground_color_.has_value()) {
609 // Nothing to blend.
610 return PipelineBlend(inputs, renderer, entity, coverage, BlendMode::kSource,
611 std::nullopt, GetAbsorbOpacity(), GetAlpha());
612 }
613
614 if (blend_mode_ <= Entity::kLastPipelineBlendMode) {
615 return PipelineBlend(inputs, renderer, entity, coverage, blend_mode_,
616 foreground_color_, GetAbsorbOpacity(), GetAlpha());
617 }
618
619 if (blend_mode_ <= Entity::kLastAdvancedBlendMode) {
620 if (inputs.size() == 1 && foreground_color_.has_value() &&
621 GetAbsorbOpacity() == ColorFilterContents::AbsorbOpacity::kYes) {
622 return CreateForegroundAdvancedBlend(
623 inputs[0], renderer, entity, coverage, foreground_color_.value(),
624 blend_mode_, GetAlpha(), GetAbsorbOpacity());
625 }
626 return advanced_blend_proc_(inputs, renderer, entity, coverage, blend_mode_,
627 foreground_color_, GetAbsorbOpacity(),
628 GetAlpha());
629 }
630
631 FML_UNREACHABLE();
632 }
633
634 } // namespace impeller
impeller::OptionsFromPass
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:20
BLEND_CASE
#define BLEND_CASE(mode)
Definition: blend_filter_contents.cc:547
impeller::Entity::kLastPipelineBlendMode
static constexpr BlendMode kLastPipelineBlendMode
Definition: entity.h:23
impeller::BlendModeToString
const char * BlendModeToString(BlendMode blend_mode)
Definition: color.cc:47
impeller::BlendMode::kDestinationATop
@ kDestinationATop
contents.h
impeller::BlendFilterContents::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: blend_filter_contents.cc:562
impeller::ContentContext::GetBlendPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetBlendPipeline(ContentContextOptions opts) const
Definition: content_context.h:480
impeller::Entity::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:130
impeller::BlendFilterContents::SetForegroundColor
void SetForegroundColor(std::optional< Color > color)
Sets a source color which is blended after all of the inputs have been blended.
Definition: blend_filter_contents.cc:593
entity.h
impeller::BlendMode
BlendMode
Definition: color.h:59
solid_color_contents.h
impeller::Color
Definition: color.h:124
impeller::BlendMode::kLuminosity
@ kLuminosity
impeller::BlendMode::kSource
@ kSource
impeller::BlendMode::kColorDodge
@ kColorDodge
impeller::BlendMode::kDestination
@ kDestination
impeller::BlendMode::kDarken
@ kDarken
formats.h
impeller::TRect::Intersection
constexpr std::optional< TRect > Intersection(const TRect &o) const
Definition: rect.h:461
impeller::InvertPorterDuffBlend
std::optional< BlendMode > InvertPorterDuffBlend(BlendMode blend_mode)
Definition: blend_filter_contents.cc:26
impeller::BlendMode::kColor
@ kColor
impeller::Entity::FromSnapshot
static Entity FromSnapshot(const Snapshot &snapshot, BlendMode blend_mode=BlendMode::kSourceOver, uint32_t clip_depth=0)
Create an entity that can be used to render a given snapshot.
Definition: entity.cc:22
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:94
impeller::BlendMode::kDestinationOver
@ kDestinationOver
impeller::BlendMode::kPlus
@ kPlus
impeller::BlendMode::kOverlay
@ kOverlay
impeller::VertexBufferBuilder::AddVertices
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
Definition: vertex_buffer_builder.h:70
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
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:287
impeller::BlendMode::kModulate
@ kModulate
impeller::AdvancedBlend
static std::optional< Entity > AdvancedBlend(const FilterInput::Vector &inputs, const ContentContext &renderer, const Entity &entity, const Rect &coverage, BlendMode blend_mode, std::optional< Color > foreground_color, ColorFilterContents::AbsorbOpacity absorb_opacity, PipelineProc pipeline_proc, std::optional< Scalar > alpha)
Definition: blend_filter_contents.cc:71
impeller::BlendMode::kSourceOut
@ kSourceOut
impeller::BlendMode::kSaturation
@ kSaturation
impeller::Entity::SetContents
void SetContents(std::shared_ptr< Contents > contents)
Definition: entity.cc:93
impeller::BlendMode::kDifference
@ kDifference
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:15
impeller::Entity
Definition: entity.h:21
impeller::BlendMode::kLighten
@ kLighten
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::BlendMode::kSoftLight
@ kSoftLight
impeller::Point
TPoint< Scalar > Point
Definition: point.h:316
render_pass.h
impeller::BlendMode::kColorBurn
@ kColorBurn
impeller::Entity::Render
bool Render(const ContentContext &renderer, RenderPass &parent_pass) const
Definition: entity.cc:188
impeller::BlendMode::kHardLight
@ kHardLight
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::BlendMode::kClear
@ kClear
impeller::ContentContext::MakeSubpass
fml::StatusOr< RenderTarget > MakeSubpass(const std::string &label, ISize texture_size, const SubpassCallback &subpass_callback, bool msaa_enabled=true, bool depth_stencil_enabled=false, int32_t mip_count=1) const
Creates a new texture of size texture_size and calls subpass_callback with a RenderPass for drawing t...
Definition: content_context.cc:478
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:564
impeller::VertexBufferBuilder
Definition: vertex_buffer_builder.h:24
impeller::Entity::kLastAdvancedBlendMode
static constexpr BlendMode kLastAdvancedBlendMode
Definition: entity.h:24
impeller::ColorFilterContents::GetAbsorbOpacity
AbsorbOpacity GetAbsorbOpacity() const
Definition: color_filter_contents.cc:89
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:25
impeller::Entity::GetContents
const std::shared_ptr< Contents > & GetContents() const
Definition: entity.cc:97
impeller::PipelineProc
std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContext::*)(ContentContextOptions) const PipelineProc
Definition: blend_filter_contents.cc:68
filter_input.h
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:661
impeller::Luminosity
static constexpr Scalar Luminosity(Vector3 color)
Definition: color.cc:140
strings.h
color_filter_contents.h
impeller::BlendFilterContents::~BlendFilterContents
~BlendFilterContents() override
anonymous_contents.h
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:134
impeller::VertexBufferBuilder::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
Definition: vertex_buffer_builder.h:84
impeller::ContentContext::SubpassCallback
std::function< bool(const ContentContext &, RenderPass &)> SubpassCallback
Definition: content_context.h:783
impeller::ISize
TSize< int64_t > ISize
Definition: size.h:138
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
impeller::Geometry::MakeRect
static std::shared_ptr< Geometry > MakeRect(const Rect &rect)
Definition: geometry.cc:204
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:105
impeller::BlendMode::kDestinationIn
@ kDestinationIn
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::BlendMode::kExclusion
@ kExclusion
content_context.h
impeller::BlendMode::kDestinationOut
@ kDestinationOut
impeller::Contents::RenderProc
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:49
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:294
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:568
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
snapshot.h
impeller::PipelineBlend
static std::optional< Entity > PipelineBlend(const FilterInput::Vector &inputs, const ContentContext &renderer, const Entity &entity, const Rect &coverage, BlendMode blend_mode, std::optional< Color > foreground_color, ColorFilterContents::AbsorbOpacity absorb_opacity, std::optional< Scalar > alpha)
Definition: blend_filter_contents.cc:397
impeller::BlendMode::kSourceIn
@ kSourceIn
impeller::BlendMode::kScreen
@ kScreen
color.h
impeller::BlendMode::kHue
@ kHue
blend_filter_contents.h
impeller::Contents::CoverageProc
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:50
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:26
impeller::ColorFilterContents::AbsorbOpacity::kYes
@ kYes
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:33
impeller::BlendMode::kXor
@ kXor
impeller::ColorFilterContents::GetAlpha
std::optional< Scalar > GetAlpha() const
Definition: color_filter_contents.cc:98
impeller::Capabilities::SupportsDecalSamplerAddressMode
virtual bool SupportsDecalSamplerAddressMode() const =0
Whether the context backend supports SamplerAddressMode::Decal.
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:93
impeller::ContentContextOptions
Definition: content_context.h:288
impeller::ColorFilterContents::AbsorbOpacity
AbsorbOpacity
Definition: color_filter_contents.h:14
impeller
Definition: aiks_blur_unittests.cc:20
impeller::BlendMode::kSourceATop
@ kSourceATop
impeller::BlendMode::kMultiply
@ kMultiply
impeller::ContentContext
Definition: content_context.h:392
impeller::BlendFilterContents::BlendFilterContents
BlendFilterContents()
Definition: blend_filter_contents.cc:61
impeller::TRect
Definition: rect.h:122
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::BlendMode::kSourceOver
@ kSourceOver
impeller::AnonymousContents::Make
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
Definition: anonymous_contents.cc:11
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:833
impeller::Saturation
static constexpr Scalar Saturation(Vector3 color)
Definition: color.cc:166
impeller::SamplerAddressMode::kDecal
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...