| 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 "fml/logging.h"
8 #include "impeller/base/validation.h"
9 #include "impeller/core/formats.h"
10 #include "impeller/entity/contents/content_context.h"
11 #include "impeller/entity/contents/contents.h"
12 #include "impeller/entity/contents/filters/blend_filter_contents.h"
13 #include "impeller/entity/contents/pipelines.h"
14 #include "impeller/entity/geometry/geometry.h"
15 #include "impeller/entity/geometry/vertices_geometry.h"
16 #include "impeller/geometry/color.h"
17 #include "impeller/renderer/render_pass.h"
18
19 namespace impeller {
20
21 namespace {
22 static std::optional<SamplerAddressMode> TileModeToAddressMode(
23 Entity::TileMode tile_mode,
24 const Capabilities& capabilities) {
25 switch (tile_mode) {
26 case Entity::TileMode::kClamp:
27 return SamplerAddressMode::kClampToEdge;
28 break;
29 case Entity::TileMode::kMirror:
30 return SamplerAddressMode::kMirror;
31 break;
32 case Entity::TileMode::kRepeat:
33 return SamplerAddressMode::kRepeat;
34 break;
35 case Entity::TileMode::kDecal:
36 if (capabilities.SupportsDecalSamplerAddressMode()) {
37 return SamplerAddressMode::kDecal;
38 }
39 return std::nullopt;
40 }
41 }
42 } // namespace
43
44 //------------------------------------------------------
45 // VerticesSimpleBlendContents
46
47 VerticesSimpleBlendContents::VerticesSimpleBlendContents() {}
48
49 VerticesSimpleBlendContents::~VerticesSimpleBlendContents() {}
50
51 void VerticesSimpleBlendContents::SetGeometry(
52 std::shared_ptr<VerticesGeometry> geometry) {
53 geometry_ = std::move(geometry);
54 }
55
56 void VerticesSimpleBlendContents::SetAlpha(Scalar alpha) {
57 alpha_ = alpha;
58 }
59
60 void VerticesSimpleBlendContents::SetBlendMode(BlendMode blend_mode) {
61 blend_mode_ = blend_mode;
62 }
63
64 void VerticesSimpleBlendContents::SetTexture(std::shared_ptr<Texture> texture) {
65 texture_ = std::move(texture);
66 }
67
68 std::optional<Rect> VerticesSimpleBlendContents::GetCoverage(
69 const Entity& entity) const {
70 return geometry_->GetCoverage(entity.GetTransform());
71 }
72
73 void VerticesSimpleBlendContents::SetSamplerDescriptor(
74 const SamplerDescriptor& descriptor) {
75 descriptor_ = descriptor;
76 }
77
78 void VerticesSimpleBlendContents::SetTileMode(Entity::TileMode tile_mode_x,
79 Entity::TileMode tile_mode_y) {
80 tile_mode_x_ = tile_mode_x;
81 tile_mode_y_ = tile_mode_y;
82 }
83
84 void VerticesSimpleBlendContents::SetEffectTransform(Matrix transform) {
85 inverse_matrix_ = transform.Invert();
86 }
87
88 void VerticesSimpleBlendContents::SetLazyTexture(
89 const LazyTexture& lazy_texture) {
90 lazy_texture_ = lazy_texture;
91 }
92
93 void VerticesSimpleBlendContents::SetLazyTextureCoverage(Rect rect) {
94 lazy_texture_coverage_ = rect;
95 }
96
97 bool VerticesSimpleBlendContents::Render(const ContentContext& renderer,
98 const Entity& entity,
99 RenderPass& pass) const {
100 FML_DCHECK(texture_ || lazy_texture_ || blend_mode_ == BlendMode::kDst);
101 BlendMode blend_mode = blend_mode_;
102 if (!geometry_->HasVertexColors()) {
103 blend_mode = BlendMode::kSrc;
104 }
105
106 std::shared_ptr<Texture> texture;
107 if (blend_mode != BlendMode::kDst) {
108 if (!texture_) {
109 texture = lazy_texture_(renderer);
110 } else {
111 texture = texture_;
112 }
113 } else {
114 texture = renderer.GetEmptyTexture();
115 }
116 if (!texture) {
117 VALIDATION_LOG << "Missing texture for VerticesSimpleBlendContents";
118 return false;
119 }
120
121 auto dst_sampler_descriptor = descriptor_;
122 dst_sampler_descriptor.width_address_mode =
123 TileModeToAddressMode(tile_mode_x_, renderer.GetDeviceCapabilities())
124 .value_or(SamplerAddressMode::kClampToEdge);
125 dst_sampler_descriptor.height_address_mode =
126 TileModeToAddressMode(tile_mode_y_, renderer.GetDeviceCapabilities())
127 .value_or(SamplerAddressMode::kClampToEdge);
128
129 raw_ptr<const Sampler> dst_sampler =
130 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
131 dst_sampler_descriptor);
132
133 GeometryResult geometry_result = geometry_->GetPositionUVColorBuffer(
134 lazy_texture_coverage_.has_value() ? lazy_texture_coverage_.value()
135 : Rect::MakeSize(texture->GetSize()),
136 inverse_matrix_, renderer, entity, pass);
137 if (geometry_result.vertex_buffer.vertex_count == 0) {
138 return true;
139 }
140 FML_DCHECK(geometry_result.mode == GeometryResult::Mode::kNormal);
141
142 if (blend_mode <= Entity::kLastPipelineBlendMode) {
143 using VS = PorterDuffBlendPipeline::VertexShader;
144 using FS = PorterDuffBlendPipeline::FragmentShader;
145
146 #ifdef IMPELLER_DEBUG
147 pass.SetCommandLabel(SPrintF("DrawVertices Porterduff Blend (%s)",
148 BlendModeToString(blend_mode)));
149 #endif // IMPELLER_DEBUG
150 pass.SetVertexBuffer(std::move(geometry_result.vertex_buffer));
151
152 auto options = OptionsFromPassAndEntity(pass, entity);
153 options.primitive_type = geometry_result.type;
154 auto inverted_blend_mode =
155 InvertPorterDuffBlend(blend_mode).value_or(BlendMode::kSrc);
156 pass.SetPipeline(
157 renderer.GetPorterDuffPipeline(inverted_blend_mode, options));
158
159 FS::BindTextureSamplerDst(pass, texture, dst_sampler);
160
161 VS::FrameInfo frame_info;
162 FS::FragInfo frag_info;
163
164 frame_info.texture_sampler_y_coord_scale = texture->GetYCoordScale();
165 frame_info.mvp = geometry_result.transform;
166
167 frag_info.input_alpha_output_alpha_tmx_tmy =
168 Vector4(1, alpha_, static_cast<int>(tile_mode_x_),
169 static_cast<int>(tile_mode_y_));
170 frag_info.use_strict_source_rect = 0.0;
171
172 auto& host_buffer = renderer.GetTransientsBuffer();
173 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
174 VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
175
176 return pass.Draw().ok();
177 }
178
179 using VS = VerticesUber1Shader::VertexShader;
180 using FS = VerticesUber1Shader::FragmentShader;
181
182 #ifdef IMPELLER_DEBUG
183 pass.SetCommandLabel(SPrintF("DrawVertices Advanced Blend (%s)",
184 BlendModeToString(blend_mode)));
185 #endif // IMPELLER_DEBUG
186 pass.SetVertexBuffer(std::move(geometry_result.vertex_buffer));
187
188 auto options = OptionsFromPassAndEntity(pass, entity);
189 options.primitive_type = geometry_result.type;
190 pass.SetPipeline(renderer.GetDrawVerticesUberPipeline(blend_mode, options));
191
192 FS::BindTextureSampler(pass, texture, dst_sampler);
193
194 VS::FrameInfo frame_info;
195 FS::FragInfo frag_info;
196
197 frame_info.texture_sampler_y_coord_scale = texture->GetYCoordScale();
198 frame_info.mvp = geometry_result.transform;
199 frag_info.alpha = alpha_;
200 frag_info.blend_mode = static_cast<int>(blend_mode);
201
202 // These values are ignored if the platform supports native decal mode.
203 frag_info.tmx = static_cast<int>(tile_mode_x_);
204 frag_info.tmy = static_cast<int>(tile_mode_y_);
205
206 auto& host_buffer = renderer.GetTransientsBuffer();
207 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
208 VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
209
210 return pass.Draw().ok();
211 }
212
213 } // namespace impeller
blend_filter_contents.h
impeller::ContentContext
Definition: content_context.h:138
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:297
impeller::ContentContext::GetPorterDuffPipeline
PipelineRef GetPorterDuffPipeline(BlendMode mode, ContentContextOptions opts) const
Definition: content_context.cc:1167
impeller::ContentContext::GetEmptyTexture
std::shared_ptr< Texture > GetEmptyTexture() const
Definition: content_context.cc:863
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:943
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:939
impeller::ContentContext::GetDrawVerticesUberPipeline
PipelineRef GetDrawVerticesUberPipeline(BlendMode blend_mode, ContentContextOptions opts) const
Definition: content_context.cc:1467
impeller::Entity
Definition: entity.h:26
impeller::Entity::TileMode
TileMode
Definition: entity.h:48
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::Entity::TileMode::kRepeat
@ kRepeat
impeller::Entity::TileMode::kClamp
@ kClamp
impeller::Entity::TileMode::kMirror
@ kMirror
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
impeller::Entity::kLastPipelineBlendMode
static constexpr BlendMode kLastPipelineBlendMode
Definition: entity.h:28
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::RenderPass::SetVertexBuffer
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: render_pass.cc:127
impeller::RenderPass::SetPipeline
virtual void SetPipeline(PipelineRef pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:86
impeller::RenderPass::Draw
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:208
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:163
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:164
impeller::VerticesSimpleBlendContents::SetSamplerDescriptor
void SetSamplerDescriptor(const SamplerDescriptor &descriptor)
Definition: vertices_contents.cc:73
impeller::VerticesSimpleBlendContents::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:68
impeller::VerticesSimpleBlendContents::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: vertices_contents.cc:60
impeller::VerticesSimpleBlendContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: vertices_contents.cc:97
impeller::VerticesSimpleBlendContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode_x, Entity::TileMode tile_mode_y)
Definition: vertices_contents.cc:78
impeller::VerticesSimpleBlendContents::SetLazyTexture
void SetLazyTexture(const LazyTexture &lazy_texture)
Definition: vertices_contents.cc:88
impeller::VerticesSimpleBlendContents::SetLazyTextureCoverage
void SetLazyTextureCoverage(Rect rect)
Definition: vertices_contents.cc:93
impeller::VerticesSimpleBlendContents::~VerticesSimpleBlendContents
~VerticesSimpleBlendContents() override
Definition: vertices_contents.cc:49
impeller::VerticesSimpleBlendContents::LazyTexture
std::function< std::shared_ptr< Texture >(const ContentContext &renderer)> LazyTexture
Definition: vertices_contents.h:27
impeller::VerticesSimpleBlendContents::SetEffectTransform
void SetEffectTransform(Matrix transform)
Definition: vertices_contents.cc:84
impeller::VerticesSimpleBlendContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: vertices_contents.cc:64
impeller::VerticesSimpleBlendContents::SetGeometry
void SetGeometry(std::shared_ptr< VerticesGeometry > geometry)
Definition: vertices_contents.cc:51
impeller::VerticesSimpleBlendContents::SetAlpha
void SetAlpha(Scalar alpha)
Definition: vertices_contents.cc:56
impeller::VerticesSimpleBlendContents::VerticesSimpleBlendContents
VerticesSimpleBlendContents()
Definition: vertices_contents.cc:47
impeller::raw_ptr
A wrapper around a raw ptr that adds additional unopt mode only checks.
Definition: raw_ptr.h:15
color.h
content_context.h
contents.h
formats.h
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
geometry.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::SamplerAddressMode::kDecal
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
impeller::SamplerAddressMode::kRepeat
@ kRepeat
impeller::SamplerAddressMode::kMirror
@ kMirror
impeller::SamplerAddressMode::kClampToEdge
@ kClampToEdge
impeller::BlendModeToString
const char * BlendModeToString(BlendMode blend_mode)
Definition: color.cc:47
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::FS
LinePipeline::FragmentShader FS
Definition: line_contents.cc:14
impeller::TileModeToAddressMode
static std::optional< SamplerAddressMode > TileModeToAddressMode(Entity::TileMode tile_mode, const Capabilities &capabilities)
Definition: tiled_texture_contents.cc:16
impeller::InvertPorterDuffBlend
std::optional< BlendMode > InvertPorterDuffBlend(BlendMode blend_mode)
Definition: blend_filter_contents.cc:51
impeller::BlendMode
BlendMode
Definition: color.h:58
impeller::BlendMode::kDst
@ kDst
impeller::BlendMode::kSrc
@ kSrc
impeller::VS
LinePipeline::VertexShader VS
Definition: line_contents.cc:13
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
pipelines.h
render_pass.h
impeller::GeometryResult
Definition: geometry.h:21
impeller::GeometryResult::type
PrimitiveType type
Definition: geometry.h:37
impeller::GeometryResult::Mode::kNormal
@ kNormal
The geometry has no overlapping triangles.
impeller::GeometryResult::transform
Matrix transform
Definition: geometry.h:39
impeller::GeometryResult::vertex_buffer
VertexBuffer vertex_buffer
Definition: geometry.h:38
impeller::GeometryResult::mode
Mode mode
Definition: geometry.h:40
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:14
impeller::SamplerDescriptor::width_address_mode
SamplerAddressMode width_address_mode
Definition: sampler_descriptor.h:19
impeller::TRect< Scalar >
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
impeller::Vector4
Definition: vector.h:232
impeller::VertexBuffer::vertex_count
size_t vertex_count
Definition: vertex_buffer.h:25
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
vertices_contents.h
vertices_geometry.h