| Flutter Impeller |

vertices_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 "vertices_contents.h"
6
7 #include "impeller/entity/contents/content_context.h"
8 #include "impeller/entity/contents/filters/color_filter_contents.h"
9 #include "impeller/entity/geometry/geometry.h"
10 #include "impeller/entity/geometry/vertices_geometry.h"
11 #include "impeller/entity/position_color.vert.h"
12 #include "impeller/entity/vertices.frag.h"
13 #include "impeller/geometry/color.h"
14 #include "impeller/renderer/render_pass.h"
15
16 namespace impeller {
17
18 VerticesContents::VerticesContents() = default;
19
20 VerticesContents::~VerticesContents() = default;
21
22 std::optional<Rect> VerticesContents::GetCoverage(const Entity& entity) const {
23 return geometry_->GetCoverage(entity.GetTransform());
24 };
25
26 void VerticesContents::SetGeometry(std::shared_ptr<VerticesGeometry> geometry) {
27 geometry_ = std::move(geometry);
28 }
29
30 void VerticesContents::SetSourceContents(std::shared_ptr<Contents> contents) {
31 src_contents_ = std::move(contents);
32 }
33
34 std::shared_ptr<VerticesGeometry> VerticesContents::GetGeometry() const {
35 return geometry_;
36 }
37
38 void VerticesContents::SetAlpha(Scalar alpha) {
39 alpha_ = alpha;
40 }
41
42 void VerticesContents::SetBlendMode(BlendMode blend_mode) {
43 blend_mode_ = blend_mode;
44 }
45
46 const std::shared_ptr<Contents>& VerticesContents::GetSourceContents() const {
47 return src_contents_;
48 }
49
50 bool VerticesContents::Render(const ContentContext& renderer,
51 const Entity& entity,
52 RenderPass& pass) const {
53 if (blend_mode_ == BlendMode::kClear) {
54 return true;
55 }
56 std::shared_ptr<Contents> src_contents = src_contents_;
57 src_contents->SetCoverageHint(GetCoverageHint());
58 if (geometry_->HasTextureCoordinates()) {
59 auto contents = std::make_shared<VerticesUVContents>(*this);
60 contents->SetCoverageHint(GetCoverageHint());
61 if (!geometry_->HasVertexColors()) {
62 contents->SetAlpha(alpha_);
63 return contents->Render(renderer, entity, pass);
64 }
65 src_contents = contents;
66 }
67
68 auto dst_contents = std::make_shared<VerticesColorContents>(*this);
69 dst_contents->SetCoverageHint(GetCoverageHint());
70
71 std::shared_ptr<Contents> contents;
72 if (blend_mode_ == BlendMode::kDestination) {
73 dst_contents->SetAlpha(alpha_);
74 contents = dst_contents;
75 } else {
76 auto color_filter_contents = ColorFilterContents::MakeBlend(
77 blend_mode_, {FilterInput::Make(dst_contents, false),
78 FilterInput::Make(src_contents, false)});
79 color_filter_contents->SetAlpha(alpha_);
80 color_filter_contents->SetCoverageHint(GetCoverageHint());
81 contents = color_filter_contents;
82 }
83
84 FML_DCHECK(contents->GetCoverageHint() == GetCoverageHint());
85 return contents->Render(renderer, entity, pass);
86 }
87
88 //------------------------------------------------------
89 // VerticesUVContents
90
91 VerticesUVContents::VerticesUVContents(const VerticesContents& parent)
92 : parent_(parent) {}
93
94 VerticesUVContents::~VerticesUVContents() {}
95
96 std::optional<Rect> VerticesUVContents::GetCoverage(
97 const Entity& entity) const {
98 return parent_.GetCoverage(entity);
99 }
100
101 void VerticesUVContents::SetAlpha(Scalar alpha) {
102 alpha_ = alpha;
103 }
104
105 bool VerticesUVContents::Render(const ContentContext& renderer,
106 const Entity& entity,
107 RenderPass& pass) const {
108 using VS = TexturePipeline::VertexShader;
109 using FS = TexturePipeline::FragmentShader;
110
111 auto src_contents = parent_.GetSourceContents();
112
113 auto snapshot =
114 src_contents->RenderToSnapshot(renderer, // renderer
115 entity, // entity
116 GetCoverageHint(), // coverage_limit
117 std::nullopt, // sampler_descriptor
118 true, // msaa_enabled
119 /*mip_count=*/1,
120 "VerticesUVContents Snapshot"); // label
121 if (!snapshot.has_value()) {
122 return false;
123 }
124
125 pass.SetCommandLabel("VerticesUV");
126 auto& host_buffer = renderer.GetTransientsBuffer();
127 const std::shared_ptr<Geometry>& geometry = parent_.GetGeometry();
128
129 auto coverage = src_contents->GetCoverage(Entity{});
130 if (!coverage.has_value()) {
131 return false;
132 }
133 auto geometry_result = geometry->GetPositionUVBuffer(
134 coverage.value(), Matrix(), renderer, entity, pass);
135 auto opts = OptionsFromPassAndEntity(pass, entity);
136 opts.primitive_type = geometry_result.type;
137 pass.SetPipeline(renderer.GetTexturePipeline(opts));
138 pass.SetStencilReference(entity.GetClipDepth());
139 pass.SetVertexBuffer(std::move(geometry_result.vertex_buffer));
140
141 VS::FrameInfo frame_info;
142 frame_info.mvp = geometry_result.transform;
143 frame_info.texture_sampler_y_coord_scale =
144 snapshot->texture->GetYCoordScale();
145 frame_info.alpha = alpha_ * snapshot->opacity;
146 VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
147
148 FS::BindTextureSampler(pass, snapshot->texture,
149 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
150 snapshot->sampler_descriptor));
151
152 return pass.Draw().ok();
153 }
154
155 //------------------------------------------------------
156 // VerticesColorContents
157
158 VerticesColorContents::VerticesColorContents(const VerticesContents& parent)
159 : parent_(parent) {}
160
161 VerticesColorContents::~VerticesColorContents() {}
162
163 std::optional<Rect> VerticesColorContents::GetCoverage(
164 const Entity& entity) const {
165 return parent_.GetCoverage(entity);
166 }
167
168 void VerticesColorContents::SetAlpha(Scalar alpha) {
169 alpha_ = alpha;
170 }
171
172 bool VerticesColorContents::Render(const ContentContext& renderer,
173 const Entity& entity,
174 RenderPass& pass) const {
175 using VS = GeometryColorPipeline::VertexShader;
176 using FS = GeometryColorPipeline::FragmentShader;
177
178 pass.SetCommandLabel("VerticesColors");
179 auto& host_buffer = renderer.GetTransientsBuffer();
180 const std::shared_ptr<VerticesGeometry>& geometry = parent_.GetGeometry();
181
182 auto geometry_result =
183 geometry->GetPositionColorBuffer(renderer, entity, pass);
184 auto opts = OptionsFromPassAndEntity(pass, entity);
185 opts.primitive_type = geometry_result.type;
186 pass.SetPipeline(renderer.GetGeometryColorPipeline(opts));
187 pass.SetStencilReference(entity.GetClipDepth());
188 pass.SetVertexBuffer(std::move(geometry_result.vertex_buffer));
189
190 VS::FrameInfo frame_info;
191 frame_info.mvp = geometry_result.transform;
192 VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
193
194 FS::FragInfo frag_info;
195 frag_info.alpha = alpha_;
196 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
197
198 return pass.Draw().ok();
199 }
200
201 } // namespace impeller
impeller::ContentContext::GetTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:485
impeller::VerticesContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: vertices_contents.cc:50
impeller::VerticesColorContents::~VerticesColorContents
~VerticesColorContents() override
Definition: vertices_contents.cc:161
impeller::VerticesContents::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: vertices_contents.cc:42
impeller::VerticesUVContents::SetAlpha
void SetAlpha(Scalar alpha)
Definition: vertices_contents.cc:101
impeller::VerticesColorContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: vertices_contents.cc:172
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
impeller::BlendMode
BlendMode
Definition: color.h:59
impeller::VerticesUVContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: vertices_contents.cc:105
impeller::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
impeller::VerticesColorContents::VerticesColorContents
VerticesColorContents(const VerticesContents &parent)
Definition: vertices_contents.cc:158
impeller::BlendMode::kDestination
@ kDestination
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:94
impeller::RenderPass::SetVertexBuffer
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: render_pass.cc:123
impeller::VerticesContents::SetGeometry
void SetGeometry(std::shared_ptr< VerticesGeometry > geometry)
Definition: vertices_contents.cc:26
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::RenderPass::Draw
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:127
vertices_contents.h
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:15
impeller::VerticesContents::SetAlpha
void SetAlpha(Scalar alpha)
Definition: vertices_contents.cc:38
impeller::VerticesContents
Definition: vertices_contents.h:24
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:37
impeller::Contents::GetCoverageHint
const std::optional< Rect > & GetCoverageHint() const
Definition: contents.cc:164
impeller::VerticesContents::~VerticesContents
~VerticesContents() override
render_pass.h
impeller::VerticesUVContents::VerticesUVContents
VerticesUVContents(const VerticesContents &parent)
Definition: vertices_contents.cc:91
impeller::BlendMode::kClear
@ kClear
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:564
impeller::VerticesContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
Definition: vertices_contents.cc:22
geometry.h
impeller::VerticesContents::SetSourceContents
void SetSourceContents(std::shared_ptr< Contents > contents)
Definition: vertices_contents.cc:30
impeller::RenderPass::SetStencilReference
virtual void SetStencilReference(uint32_t value)
Definition: render_pass.cc:103
color_filter_contents.h
impeller::VerticesColorContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
Definition: vertices_contents.cc:163
vertices_geometry.h
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:105
content_context.h
impeller::VerticesContents::VerticesContents
VerticesContents()
impeller::VerticesUVContents::~VerticesUVContents
~VerticesUVContents() override
Definition: vertices_contents.cc:94
color.h
impeller::RenderPass::SetPipeline
virtual void SetPipeline(const std::shared_ptr< Pipeline< PipelineDescriptor >> &pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:92
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:93
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
Definition: aiks_blur_unittests.cc:20
impeller::VerticesContents::GetSourceContents
const std::shared_ptr< Contents > & GetSourceContents() const
Definition: vertices_contents.cc:46
impeller::VerticesContents::GetGeometry
std::shared_ptr< VerticesGeometry > GetGeometry() const
Definition: vertices_contents.cc:34
impeller::ContentContext
Definition: content_context.h:392
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::ContentContext::GetGeometryColorPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetGeometryColorPipeline(ContentContextOptions opts) const
Definition: content_context.h:581
impeller::VerticesColorContents::SetAlpha
void SetAlpha(Scalar alpha)
Definition: vertices_contents.cc:168
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:833
impeller::VerticesUVContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
Definition: vertices_contents.cc:96