| Flutter Impeller |

framebuffer_blend_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 "framebuffer_blend_contents.h"
6
7 #include "impeller/entity/contents/content_context.h"
8 #include "impeller/renderer/render_pass.h"
9
10 namespace impeller {
11
12 FramebufferBlendContents::FramebufferBlendContents() = default;
13
14 FramebufferBlendContents::~FramebufferBlendContents() = default;
15
16 void FramebufferBlendContents::SetBlendMode(BlendMode blend_mode) {
17 blend_mode_ = blend_mode;
18 }
19
20 void FramebufferBlendContents::SetChildContents(
21 std::shared_ptr<Contents> child_contents) {
22 child_contents_ = std::move(child_contents);
23 }
24
25 // |Contents|
26 std::optional<Rect> FramebufferBlendContents::GetCoverage(
27 const Entity& entity) const {
28 return child_contents_->GetCoverage(entity);
29 }
30
31 bool FramebufferBlendContents::Render(const ContentContext& renderer,
32 const Entity& entity,
33 RenderPass& pass) const {
34 if (!renderer.GetDeviceCapabilities().SupportsFramebufferFetch()) {
35 return false;
36 }
37
38 using VS = FramebufferBlendScreenPipeline::VertexShader;
39 using FS = FramebufferBlendScreenPipeline::FragmentShader;
40
41 auto& host_buffer = pass.GetTransientsBuffer();
42
43 auto src_snapshot = child_contents_->RenderToSnapshot(
44 renderer, // renderer
45 entity, // entity
46 Rect::MakeSize(pass.GetRenderTargetSize()), // coverage_limit
47 std::nullopt, // sampler_descriptor
48 true, // msaa_enabled
49 "FramebufferBlendContents Snapshot"); // label
50
51 if (!src_snapshot.has_value()) {
52 return true;
53 }
54 auto coverage = src_snapshot->GetCoverage();
55 if (!coverage.has_value()) {
56 return true;
57 }
58 Rect src_coverage = coverage.value();
59
60 auto size = src_coverage.GetSize();
61 VertexBufferBuilder<VS::PerVertexData> vtx_builder;
62 vtx_builder.AddVertices({
63 {Point(0, 0), Point(0, 0)},
64 {Point(size.width, 0), Point(1, 0)},
65 {Point(0, size.height), Point(0, 1)},
66 {Point(size.width, size.height), Point(1, 1)},
67 });
68
69 auto options = OptionsFromPass(pass);
70 options.blend_mode = BlendMode::kSource;
71 options.primitive_type = PrimitiveType::kTriangleStrip;
72
73 Command cmd;
74 DEBUG_COMMAND_INFO(cmd, "Framebuffer Advanced Blend Filter");
75 cmd.BindVertices(vtx_builder.CreateVertexBuffer(host_buffer));
76 cmd.stencil_reference = entity.GetClipDepth();
77
78 switch (blend_mode_) {
79 case BlendMode::kScreen:
80 cmd.pipeline = renderer.GetFramebufferBlendScreenPipeline(options);
81 break;
82 case BlendMode::kOverlay:
83 cmd.pipeline = renderer.GetFramebufferBlendOverlayPipeline(options);
84 break;
85 case BlendMode::kDarken:
86 cmd.pipeline = renderer.GetFramebufferBlendDarkenPipeline(options);
87 break;
88 case BlendMode::kLighten:
89 cmd.pipeline = renderer.GetFramebufferBlendLightenPipeline(options);
90 break;
91 case BlendMode::kColorDodge:
92 cmd.pipeline = renderer.GetFramebufferBlendColorDodgePipeline(options);
93 break;
94 case BlendMode::kColorBurn:
95 cmd.pipeline = renderer.GetFramebufferBlendColorBurnPipeline(options);
96 break;
97 case BlendMode::kHardLight:
98 cmd.pipeline = renderer.GetFramebufferBlendHardLightPipeline(options);
99 break;
100 case BlendMode::kSoftLight:
101 cmd.pipeline = renderer.GetFramebufferBlendSoftLightPipeline(options);
102 break;
103 case BlendMode::kDifference:
104 cmd.pipeline = renderer.GetFramebufferBlendDifferencePipeline(options);
105 break;
106 case BlendMode::kExclusion:
107 cmd.pipeline = renderer.GetFramebufferBlendExclusionPipeline(options);
108 break;
109 case BlendMode::kMultiply:
110 cmd.pipeline = renderer.GetFramebufferBlendMultiplyPipeline(options);
111 break;
112 case BlendMode::kHue:
113 cmd.pipeline = renderer.GetFramebufferBlendHuePipeline(options);
114 break;
115 case BlendMode::kSaturation:
116 cmd.pipeline = renderer.GetFramebufferBlendSaturationPipeline(options);
117 break;
118 case BlendMode::kColor:
119 cmd.pipeline = renderer.GetFramebufferBlendColorPipeline(options);
120 break;
121 case BlendMode::kLuminosity:
122 cmd.pipeline = renderer.GetFramebufferBlendLuminosityPipeline(options);
123 break;
124 default:
125 return false;
126 }
127
128 VS::FrameInfo frame_info;
129 FS::FragInfo frag_info;
130
131 auto src_sampler_descriptor = src_snapshot->sampler_descriptor;
132 if (renderer.GetDeviceCapabilities().SupportsDecalSamplerAddressMode()) {
133 src_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
134 src_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
135 }
136 auto src_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
137 src_sampler_descriptor);
138 FS::BindTextureSamplerSrc(cmd, src_snapshot->texture, src_sampler);
139
140 frame_info.mvp = pass.GetOrthographicTransform() * src_snapshot->transform;
141 frame_info.src_y_coord_scale = src_snapshot->texture->GetYCoordScale();
142 VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
143
144 frag_info.src_input_alpha = src_snapshot->opacity;
145 FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
146
147 return pass.AddCommand(std::move(cmd));
148 }
149
150 } // namespace impeller
impeller::OptionsFromPass
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:20
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::BlendMode
BlendMode
Definition: color.h:59
impeller::BlendMode::kLuminosity
@ kLuminosity
impeller::BlendMode::kSource
@ kSource
impeller::BlendMode::kColorDodge
@ kColorDodge
impeller::BlendMode::kDarken
@ kDarken
impeller::BlendMode::kColor
@ kColor
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:93
impeller::BlendMode::kOverlay
@ kOverlay
framebuffer_blend_contents.h
impeller::FramebufferBlendContents::~FramebufferBlendContents
~FramebufferBlendContents() override
impeller::BlendMode::kSaturation
@ kSaturation
impeller::BlendMode::kDifference
@ kDifference
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:308
render_pass.h
impeller::BlendMode::kColorBurn
@ kColorBurn
impeller::BlendMode::kHardLight
@ kHardLight
impeller::FramebufferBlendContents::FramebufferBlendContents
FramebufferBlendContents()
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:488
impeller::FramebufferBlendContents::SetChildContents
void SetChildContents(std::shared_ptr< Contents > child_contents)
Definition: framebuffer_blend_contents.cc:20
impeller::BlendMode::kExclusion
@ kExclusion
content_context.h
impeller::TRect::GetSize
constexpr TSize< Type > GetSize() const
Returns the size of the rectangle as specified when it was constructed and which may be negative in e...
Definition: rect.h:159
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:44
impeller::FramebufferBlendContents::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: framebuffer_blend_contents.cc:16
impeller::BlendMode::kScreen
@ kScreen
impeller::BlendMode::kHue
@ kHue
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:92
impeller
Definition: aiks_context.cc:10
impeller::BlendMode::kMultiply
@ kMultiply
impeller::SamplerAddressMode::kDecal
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...