| Flutter Impeller |

tiled_texture_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/tiled_texture_contents.h"
6
7 #include "fml/logging.h"
8 #include "impeller/core/formats.h"
9 #include "impeller/entity/contents/content_context.h"
10 #include "impeller/entity/tiled_texture_fill.frag.h"
11 #include "impeller/entity/tiled_texture_fill_external.frag.h"
12 #include "impeller/renderer/render_pass.h"
13
14 namespace impeller {
15
16 static std::optional<SamplerAddressMode> TileModeToAddressMode(
17 Entity::TileMode tile_mode,
18 const Capabilities& capabilities) {
19 switch (tile_mode) {
20 case Entity::TileMode::kClamp:
21 return SamplerAddressMode::kClampToEdge;
22 break;
23 case Entity::TileMode::kMirror:
24 return SamplerAddressMode::kMirror;
25 break;
26 case Entity::TileMode::kRepeat:
27 return SamplerAddressMode::kRepeat;
28 break;
29 case Entity::TileMode::kDecal:
30 if (capabilities.SupportsDecalSamplerAddressMode()) {
31 return SamplerAddressMode::kDecal;
32 }
33 return std::nullopt;
34 }
35 }
36
37 TiledTextureContents::TiledTextureContents() = default;
38
39 TiledTextureContents::~TiledTextureContents() = default;
40
41 void TiledTextureContents::SetTexture(std::shared_ptr<Texture> texture) {
42 texture_ = std::move(texture);
43 }
44
45 void TiledTextureContents::SetTileModes(Entity::TileMode x_tile_mode,
46 Entity::TileMode y_tile_mode) {
47 x_tile_mode_ = x_tile_mode;
48 y_tile_mode_ = y_tile_mode;
49 }
50
51 void TiledTextureContents::SetSamplerDescriptor(const SamplerDescriptor& desc) {
52 sampler_descriptor_ = desc;
53 }
54
55 void TiledTextureContents::SetColorFilter(ColorFilterProc color_filter) {
56 color_filter_ = std::move(color_filter);
57 }
58
59 std::shared_ptr<Texture> TiledTextureContents::CreateFilterTexture(
60 const ContentContext& renderer) const {
61 if (!color_filter_) {
62 return nullptr;
63 }
64 auto color_filter_contents = color_filter_(FilterInput::Make(texture_));
65 auto snapshot = color_filter_contents->RenderToSnapshot(
66 renderer, // renderer
67 Entity(), // entity
68 std::nullopt, // coverage_limit
69 std::nullopt, // sampler_descriptor
70 true, // msaa_enabled
71 /*mip_count=*/1,
72 "TiledTextureContents Snapshot"); // label
73 if (snapshot.has_value()) {
74 return snapshot.value().texture;
75 }
76 return nullptr;
77 }
78
79 SamplerDescriptor TiledTextureContents::CreateSamplerDescriptor(
80 const Capabilities& capabilities) const {
81 SamplerDescriptor descriptor = sampler_descriptor_;
82 auto width_mode = TileModeToAddressMode(x_tile_mode_, capabilities);
83 auto height_mode = TileModeToAddressMode(y_tile_mode_, capabilities);
84 if (width_mode.has_value()) {
85 descriptor.width_address_mode = width_mode.value();
86 }
87 if (height_mode.has_value()) {
88 descriptor.height_address_mode = height_mode.value();
89 }
90 return descriptor;
91 }
92
93 bool TiledTextureContents::UsesEmulatedTileMode(
94 const Capabilities& capabilities) const {
95 return !TileModeToAddressMode(x_tile_mode_, capabilities).has_value() ||
96 !TileModeToAddressMode(y_tile_mode_, capabilities).has_value();
97 }
98
99 // |Contents|
100 bool TiledTextureContents::IsOpaque(const Matrix& transform) const {
101 if (GetOpacityFactor() < 1 || x_tile_mode_ == Entity::TileMode::kDecal ||
102 y_tile_mode_ == Entity::TileMode::kDecal) {
103 return false;
104 }
105 if (color_filter_) {
106 return false;
107 }
108 return texture_->IsOpaque() && !AppliesAlphaForStrokeCoverage(transform);
109 }
110
111 bool TiledTextureContents::Render(const ContentContext& renderer,
112 const Entity& entity,
113 RenderPass& pass) const {
114 if (texture_ == nullptr) {
115 return true;
116 }
117
118 using VS = TextureUvFillVertexShader;
119 using FS = TiledTextureFillFragmentShader;
120
121 const auto texture_size = texture_->GetSize();
122 if (texture_size.IsEmpty()) {
123 return true;
124 }
125
126 VS::FrameInfo frame_info;
127 frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
128 frame_info.uv_transform =
129 Rect::MakeSize(texture_size).GetNormalizingTransform() *
130 GetInverseEffectTransform();
131
132 #ifdef IMPELLER_ENABLE_OPENGLES
133 using FSExternal = TiledTextureFillExternalFragmentShader;
134 if (texture_->GetTextureDescriptor().type ==
135 TextureType::kTextureExternalOES) {
136 return ColorSourceContents::DrawGeometry<VS>(
137 renderer, entity, pass,
138 [&renderer](ContentContextOptions options) {
139 return renderer.GetTiledTextureUvExternalPipeline(options);
140 },
141 frame_info,
142 [this, &renderer](RenderPass& pass) {
143 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
144 #ifdef IMPELLER_DEBUG
145 pass.SetCommandLabel("TextureFill External");
146 #endif // IMPELLER_DEBUG
147
148 FML_DCHECK(!color_filter_);
149 FSExternal::FragInfo frag_info;
150 frag_info.x_tile_mode =
151 static_cast<Scalar>(sampler_descriptor_.width_address_mode);
152 frag_info.y_tile_mode =
153 static_cast<Scalar>(sampler_descriptor_.height_address_mode);
154 frag_info.alpha = GetOpacityFactor();
155 FSExternal::BindFragInfo(pass,
156 data_host_buffer.EmplaceUniform(frag_info));
157
158 SamplerDescriptor sampler_desc;
159 // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid,
160 // so we emulate all other tile modes here by remapping the texture
161 // coordinates.
162 sampler_desc.width_address_mode = SamplerAddressMode::kClampToEdge;
163 sampler_desc.height_address_mode = SamplerAddressMode::kClampToEdge;
164 sampler_desc.min_filter = sampler_descriptor_.min_filter;
165 sampler_desc.mag_filter = sampler_descriptor_.mag_filter;
166 sampler_desc.mip_filter = MipFilter::kBase;
167
168 FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
169 pass, texture_,
170 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
171 sampler_desc));
172 return true;
173 });
174 }
175 #endif // IMPELLER_ENABLE_OPENGLES
176
177 PipelineBuilderCallback pipeline_callback =
178 [&renderer](ContentContextOptions options) {
179 return renderer.GetTiledTexturePipeline(options);
180 };
181 return ColorSourceContents::DrawGeometry<VS>(
182 renderer, entity, pass, pipeline_callback, frame_info,
183 [this, &renderer, &entity](RenderPass& pass) {
184 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
185 #ifdef IMPELLER_DEBUG
186 pass.SetCommandLabel("TextureFill");
187 #endif // IMPELLER_DEBUG
188
189 FS::FragInfo frag_info;
190 frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
191 frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
192 frag_info.alpha =
193 GetOpacityFactor() *
194 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
195 FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
196
197 if (color_filter_) {
198 auto filtered_texture = CreateFilterTexture(renderer);
199 if (!filtered_texture) {
200 return false;
201 }
202 FS::BindTextureSampler(
203 pass, filtered_texture,
204 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
205 CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
206 } else {
207 FS::BindTextureSampler(
208 pass, texture_,
209 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
210 CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
211 }
212
213 return true;
214 });
215 }
216
217 std::optional<Snapshot> TiledTextureContents::RenderToSnapshot(
218 const ContentContext& renderer,
219 const Entity& entity,
220 std::optional<Rect> coverage_limit,
221 const std::optional<SamplerDescriptor>& sampler_descriptor,
222 bool msaa_enabled,
223 int32_t mip_count,
224 std::string_view label) const {
225 std::optional<Rect> geometry_coverage = GetGeometry()->GetCoverage({});
226 if (GetInverseEffectTransform().IsIdentity() &&
227 GetGeometry()->IsAxisAlignedRect() &&
228 (!geometry_coverage.has_value() ||
229 Rect::MakeSize(texture_->GetSize())
230 .Contains(geometry_coverage.value()))) {
231 auto coverage = GetCoverage(entity);
232 if (!coverage.has_value()) {
233 return std::nullopt;
234 }
235 auto scale = Vector2(coverage->GetSize() / Size(texture_->GetSize()));
236
237 return Snapshot{
238 .texture = texture_,
239 .transform = Matrix::MakeTranslation(coverage->GetOrigin()) *
240 Matrix::MakeScale(scale),
241 .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
242 .opacity = GetOpacityFactor(),
243 };
244 }
245
246 return Contents::RenderToSnapshot(
247 renderer, // renderer
248 entity, // entity
249 std::nullopt, // coverage_limit
250 sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
251 true, // msaa_enabled
252 /*mip_count=*/1,
253 label); // label
254 }
255
256 } // namespace impeller
impeller::Capabilities
Definition: capabilities.h:14
impeller::Capabilities::SupportsDecalSamplerAddressMode
virtual bool SupportsDecalSamplerAddressMode() const =0
Whether the context backend supports SamplerAddressMode::Decal.
impeller::ColorSourceContents::GetGeometry
const Geometry * GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:20
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:28
impeller::ColorSourceContents::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: color_source_contents.cc:44
impeller::ColorSourceContents::AppliesAlphaForStrokeCoverage
bool AppliesAlphaForStrokeCoverage(const Matrix &transform) const
Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverag...
Definition: color_source_contents.cc:53
impeller::ColorSourceContents::GetInverseEffectTransform
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
Definition: color_source_contents.cc:36
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:42
impeller::ContentContext
Definition: content_context.h:138
impeller::ContentContext::GetTiledTexturePipeline
PipelineRef GetTiledTexturePipeline(ContentContextOptions opts) const
Definition: content_context.cc:1140
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:954
impeller::ContentContext::GetTransientsDataBuffer
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
Definition: content_context.h:310
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:950
impeller::Contents::RenderToSnapshot
virtual std::optional< Snapshot > RenderToSnapshot(const ContentContext &renderer, const Entity &entity, std::optional< Rect > coverage_limit=std::nullopt, const std::optional< SamplerDescriptor > &sampler_descriptor=std::nullopt, bool msaa_enabled=true, int32_t mip_count=1, std::string_view label="Snapshot") const
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition: contents.cc:56
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
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::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
impeller::Geometry::ComputeAlphaCoverage
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:125
impeller::Geometry::GetCoverage
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::TiledTextureContents::~TiledTextureContents
~TiledTextureContents() override
impeller::TiledTextureContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: tiled_texture_contents.cc:111
impeller::TiledTextureContents::TiledTextureContents
TiledTextureContents()
impeller::TiledTextureContents::IsOpaque
bool IsOpaque(const Matrix &transform) const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: tiled_texture_contents.cc:100
impeller::TiledTextureContents::SetSamplerDescriptor
void SetSamplerDescriptor(const SamplerDescriptor &desc)
Definition: tiled_texture_contents.cc:51
impeller::TiledTextureContents::SetColorFilter
void SetColorFilter(ColorFilterProc color_filter)
Set a color filter to apply directly to this tiled texture.
Definition: tiled_texture_contents.cc:55
impeller::TiledTextureContents::SetTileModes
void SetTileModes(Entity::TileMode x_tile_mode, Entity::TileMode y_tile_mode)
Definition: tiled_texture_contents.cc:45
impeller::TiledTextureContents::RenderToSnapshot
std::optional< Snapshot > RenderToSnapshot(const ContentContext &renderer, const Entity &entity, std::optional< Rect > coverage_limit=std::nullopt, const std::optional< SamplerDescriptor > &sampler_descriptor=std::nullopt, bool msaa_enabled=true, int32_t mip_count=1, std::string_view label="Tiled Texture Snapshot") const override
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition: tiled_texture_contents.cc:217
impeller::TiledTextureContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: tiled_texture_contents.cc:41
content_context.h
formats.h
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
impeller
Definition: allocation.cc:12
impeller::Vector2
Point Vector2
Definition: point.h:331
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::FS
LinePipeline::FragmentShader FS
Definition: line_contents.cc:14
impeller::MipFilter::kBase
@ kBase
The texture is sampled as if it only had a single mipmap level.
impeller::TileModeToAddressMode
static std::optional< SamplerAddressMode > TileModeToAddressMode(Entity::TileMode tile_mode, const Capabilities &capabilities)
Definition: tiled_texture_contents.cc:16
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::VS
LinePipeline::VertexShader VS
Definition: line_contents.cc:13
render_pass.h
impeller::ContentContextOptions
Definition: content_context.h:40
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:14
impeller::SamplerDescriptor::mip_filter
MipFilter mip_filter
Definition: sampler_descriptor.h:17
impeller::SamplerDescriptor::width_address_mode
SamplerAddressMode width_address_mode
Definition: sampler_descriptor.h:19
impeller::SamplerDescriptor::height_address_mode
SamplerAddressMode height_address_mode
Definition: sampler_descriptor.h:20
impeller::SamplerDescriptor::mag_filter
MinMagFilter mag_filter
Definition: sampler_descriptor.h:16
impeller::SamplerDescriptor::min_filter
MinMagFilter min_filter
Definition: sampler_descriptor.h:15
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:24
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:25
impeller::TRect::Contains
constexpr bool Contains(const TPoint< Type > &p) const
Returns true iff the provided point |p| is inside the half-open interior of this rectangle.
Definition: rect.h:235
impeller::TRect::GetNormalizingTransform
constexpr Matrix GetNormalizingTransform() const
Constructs a Matrix that will map all points in the coordinate space of the rectangle into a new norm...
Definition: rect.h:495
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
impeller::TSize< Scalar >
tiled_texture_contents.h