Flutter Engine
The Flutter Engine
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"
17
18namespace impeller {
19
20namespace {
21static std::optional<SamplerAddressMode> TileModeToAddressMode(
22 Entity::TileMode tile_mode,
23 const Capabilities& capabilities) {
24 switch (tile_mode) {
27 break;
30 break;
33 break;
35 if (capabilities.SupportsDecalSamplerAddressMode()) {
37 }
38 return std::nullopt;
39 }
40}
41} // namespace
42
43//------------------------------------------------------
44// VerticesSimpleBlendContents
45
47
49
51 std::shared_ptr<VerticesGeometry> geometry) {
52 geometry_ = std::move(geometry);
53}
54
56 alpha_ = alpha;
57}
58
60 blend_mode_ = blend_mode;
61}
62
63void VerticesSimpleBlendContents::SetTexture(std::shared_ptr<Texture> texture) {
64 texture_ = std::move(texture);
65}
66
68 const Entity& entity) const {
69 return geometry_->GetCoverage(entity.GetTransform());
70}
71
73 SamplerDescriptor descriptor) {
74 descriptor_ = std::move(descriptor);
75}
76
78 Entity::TileMode tile_mode_y) {
79 tile_mode_x_ = tile_mode_x;
80 tile_mode_y_ = tile_mode_y;
81}
82
84 inverse_matrix_ = transform.Invert();
85}
86
88 const LazyTexture& lazy_texture) {
89 lazy_texture_ = lazy_texture;
90}
91
93 const Entity& entity,
94 RenderPass& pass) const {
95 FML_DCHECK(texture_ || lazy_texture_ ||
96 blend_mode_ == BlendMode::kDestination);
97 BlendMode blend_mode = blend_mode_;
98 if (!geometry_->HasVertexColors()) {
99 blend_mode = BlendMode::kSource;
100 }
101
102 std::shared_ptr<Texture> texture;
103 if (blend_mode != BlendMode::kDestination) {
104 if (!texture_) {
105 texture = lazy_texture_(renderer);
106 } else {
107 texture = texture_;
108 }
109 } else {
110 texture = renderer.GetEmptyTexture();
111 }
112
113 auto dst_sampler_descriptor = descriptor_;
114 dst_sampler_descriptor.width_address_mode =
115 TileModeToAddressMode(tile_mode_x_, renderer.GetDeviceCapabilities())
117 dst_sampler_descriptor.height_address_mode =
118 TileModeToAddressMode(tile_mode_y_, renderer.GetDeviceCapabilities())
120
121 const std::unique_ptr<const Sampler>& dst_sampler =
122 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
123 dst_sampler_descriptor);
124
125 GeometryResult geometry_result = geometry_->GetPositionUVColorBuffer(
126 (!!texture) ? Rect::MakeSize(texture->GetSize())
127 : Rect::MakeSize(ISize{1, 1}),
128 inverse_matrix_, renderer, entity, pass);
129 if (geometry_result.vertex_buffer.vertex_count == 0) {
130 return true;
131 }
133
134 if (blend_mode <= Entity::kLastPipelineBlendMode) {
137
138#ifdef IMPELLER_DEBUG
139 pass.SetCommandLabel(SPrintF("DrawVertices Porterduff Blend (%s)",
140 BlendModeToString(blend_mode)));
141#endif // IMPELLER_DEBUG
142 pass.SetVertexBuffer(std::move(geometry_result.vertex_buffer));
143
144 auto options = OptionsFromPassAndEntity(pass, entity);
145 options.primitive_type = geometry_result.type;
146 pass.SetPipeline(renderer.GetPorterDuffBlendPipeline(options));
147
148 FS::BindTextureSamplerDst(pass, texture, dst_sampler);
149
150 VS::FrameInfo frame_info;
151 FS::FragInfo frag_info;
152
153 frame_info.texture_sampler_y_coord_scale = texture->GetYCoordScale();
154 frame_info.mvp = geometry_result.transform;
155
156 frag_info.output_alpha = alpha_;
157 frag_info.input_alpha = 1.0;
158
159 auto inverted_blend_mode =
160 InvertPorterDuffBlend(blend_mode).value_or(BlendMode::kSource);
161 auto blend_coefficients =
162 kPorterDuffCoefficients[static_cast<int>(inverted_blend_mode)];
163 frag_info.src_coeff = blend_coefficients[0];
164 frag_info.src_coeff_dst_alpha = blend_coefficients[1];
165 frag_info.dst_coeff = blend_coefficients[2];
166 frag_info.dst_coeff_src_alpha = blend_coefficients[3];
167 frag_info.dst_coeff_src_color = blend_coefficients[4];
168
169 // These values are ignored if the platform supports native decal mode.
170 frag_info.tmx = static_cast<int>(tile_mode_x_);
171 frag_info.tmy = static_cast<int>(tile_mode_y_);
172
173 auto& host_buffer = renderer.GetTransientsBuffer();
174 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
175 VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
176
177 return pass.Draw().ok();
178 }
179
182
183#ifdef IMPELLER_DEBUG
184 pass.SetCommandLabel(SPrintF("DrawVertices Advanced Blend (%s)",
185 BlendModeToString(blend_mode)));
186#endif // IMPELLER_DEBUG
187 pass.SetVertexBuffer(std::move(geometry_result.vertex_buffer));
188
189 auto options = OptionsFromPassAndEntity(pass, entity);
190 options.primitive_type = geometry_result.type;
191 pass.SetPipeline(renderer.GetDrawVerticesUberShader(options));
192
193 FS::BindTextureSampler(pass, texture, dst_sampler);
194
195 VS::FrameInfo frame_info;
196 FS::FragInfo frag_info;
197
198 frame_info.texture_sampler_y_coord_scale = texture->GetYCoordScale();
199 frame_info.mvp = geometry_result.transform;
200 frag_info.alpha = alpha_;
201 frag_info.blend_mode = static_cast<int>(blend_mode);
202
203 // These values are ignored if the platform supports native decal mode.
204 frag_info.tmx = static_cast<int>(tile_mode_x_);
205 frag_info.tmy = static_cast<int>(tile_mode_y_);
206
207 auto& host_buffer = renderer.GetTransientsBuffer();
208 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
209 VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
210
211 return pass.Draw().ok();
212}
213
214} // namespace impeller
const char * options
bool ok() const
Definition: status.h:71
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
static constexpr BlendMode kLastPipelineBlendMode
Definition: entity.h:22
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: render_pass.cc:123
virtual void SetPipeline(const std::shared_ptr< Pipeline< PipelineDescriptor > > &pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:92
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:127
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
FragmentShader_ FragmentShader
Definition: pipeline.h:107
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.
void SetBlendMode(BlendMode blend_mode)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
void SetTileMode(Entity::TileMode tile_mode_x, Entity::TileMode tile_mode_y)
void SetLazyTexture(const LazyTexture &lazy_texture)
std::function< std::shared_ptr< Texture >(const ContentContext &renderer)> LazyTexture
void SetTexture(std::shared_ptr< Texture > texture)
void SetGeometry(std::shared_ptr< VerticesGeometry > geometry)
void SetSamplerDescriptor(SamplerDescriptor descriptor)
#define FML_DCHECK(condition)
Definition: logging.h:103
FlTexture * texture
float Scalar
Definition: scalar.h:18
SolidFillVertexShader VS
const char * BlendModeToString(BlendMode blend_mode)
Definition: color.cc:47
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
constexpr std::array< std::array< Scalar, 5 >, 15 > kPorterDuffCoefficients
std::optional< BlendMode > InvertPorterDuffBlend(BlendMode blend_mode)
BlendMode
Definition: color.h:59
static std::optional< SamplerAddressMode > TileModeToAddressMode(Entity::TileMode tile_mode, const Capabilities &capabilities)
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition: p3.cpp:47
PrimitiveType type
Definition: geometry.h:35
@ kNormal
The geometry has no overlapping triangles.
VertexBuffer vertex_buffer
Definition: geometry.h:36
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
SamplerAddressMode width_address_mode
static constexpr TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146