| 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/entity/contents/content_context.h"
9 #include "impeller/entity/texture_fill.vert.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(SamplerDescriptor desc) {
52 sampler_descriptor_ = std::move(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 {
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();
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 = TextureFillVertexShader;
119 using FS = TiledTextureFillFragmentShader;
120 using FSExternal = TiledTextureFillExternalFragmentShader;
121
122 const auto texture_size = texture_->GetSize();
123 if (texture_size.IsEmpty()) {
124 return true;
125 }
126
127 bool is_external_texture =
128 texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
129
130 bool uses_emulated_tile_mode =
131 UsesEmulatedTileMode(renderer.GetDeviceCapabilities());
132
133 VS::FrameInfo frame_info;
134 frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
135 frame_info.alpha = GetOpacityFactor();
136
137 PipelineBuilderMethod pipeline_method;
138
139 #ifdef IMPELLER_ENABLE_OPENGLES
140 if (is_external_texture) {
141 pipeline_method = &ContentContext::GetTiledTextureExternalPipeline;
142 } else {
143 pipeline_method = uses_emulated_tile_mode
144 ? &ContentContext::GetTiledTexturePipeline
145 : &ContentContext::GetTexturePipeline;
146 }
147 #else
148 pipeline_method = uses_emulated_tile_mode
149 ? &ContentContext::GetTiledTexturePipeline
150 : &ContentContext::GetTexturePipeline;
151 #endif // IMPELLER_ENABLE_OPENGLES
152
153 PipelineBuilderCallback pipeline_callback =
154 [&renderer, &pipeline_method](ContentContextOptions options) {
155 return (renderer.*pipeline_method)(options);
156 };
157 return ColorSourceContents::DrawGeometry<VS>(
158 renderer, entity, pass, pipeline_callback, frame_info,
159 [this, &renderer, &is_external_texture,
160 &uses_emulated_tile_mode](RenderPass& pass) {
161 auto& host_buffer = renderer.GetTransientsBuffer();
162
163 if (uses_emulated_tile_mode) {
164 pass.SetCommandLabel("TiledTextureFill");
165 } else {
166 pass.SetCommandLabel("TextureFill");
167 }
168
169 if (is_external_texture) {
170 FSExternal::FragInfo frag_info;
171 frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
172 frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
173 FSExternal::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
174 } else if (uses_emulated_tile_mode) {
175 FS::FragInfo frag_info;
176 frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
177 frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
178 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
179 }
180
181 if (is_external_texture) {
182 SamplerDescriptor sampler_desc;
183 // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid, so
184 // we emulate all other tile modes here by remapping the texture
185 // coordinates.
186 sampler_desc.width_address_mode = SamplerAddressMode::kClampToEdge;
187 sampler_desc.height_address_mode = SamplerAddressMode::kClampToEdge;
188
189 // Also, external textures cannot be bound to color filters, so ignore
190 // this case for now.
191 FML_DCHECK(!color_filter_) << "Color filters are not currently "
192 "supported for external textures.";
193
194 FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
195 pass, texture_,
196 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
197 sampler_desc));
198 } else {
199 if (color_filter_) {
200 auto filtered_texture = CreateFilterTexture(renderer);
201 if (!filtered_texture) {
202 return false;
203 }
204 FS::BindTextureSampler(
205 pass, filtered_texture,
206 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
207 CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
208 } else {
209 FS::BindTextureSampler(
210 pass, texture_,
211 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
212 CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
213 }
214 }
215
216 return true;
217 },
218 /*enable_uvs=*/true,
219 /*texture_coverage=*/Rect::MakeSize(texture_size),
220 /*effect_transform=*/GetInverseEffectTransform());
221 }
222
223 std::optional<Snapshot> TiledTextureContents::RenderToSnapshot(
224 const ContentContext& renderer,
225 const Entity& entity,
226 std::optional<Rect> coverage_limit,
227 const std::optional<SamplerDescriptor>& sampler_descriptor,
228 bool msaa_enabled,
229 int32_t mip_count,
230 const std::string& label) const {
231 std::optional<Rect> geometry_coverage = GetGeometry()->GetCoverage({});
232 if (GetInverseEffectTransform().IsIdentity() &&
233 GetGeometry()->IsAxisAlignedRect() &&
234 (!geometry_coverage.has_value() ||
235 Rect::MakeSize(texture_->GetSize())
236 .Contains(geometry_coverage.value()))) {
237 auto coverage = GetCoverage(entity);
238 if (!coverage.has_value()) {
239 return std::nullopt;
240 }
241 auto scale = Vector2(coverage->GetSize() / Size(texture_->GetSize()));
242
243 return Snapshot{
244 .texture = texture_,
245 .transform = Matrix::MakeTranslation(coverage->GetOrigin()) *
246 Matrix::MakeScale(scale),
247 .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
248 .opacity = GetOpacityFactor(),
249 };
250 }
251
252 return Contents::RenderToSnapshot(
253 renderer, // renderer
254 entity, // entity
255 std::nullopt, // coverage_limit
256 sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
257 true, // msaa_enabled
258 /*mip_count=*/1,
259 label); // label
260 }
261
262 } // namespace impeller
impeller::ContentContext::GetTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:485
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:113
impeller::TiledTextureContents::IsOpaque
bool IsOpaque() 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::Entity::TileMode::kClamp
@ kClamp
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:28
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::TiledTextureContents::TiledTextureContents
TiledTextureContents()
impeller::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
impeller::Entity::TileMode::kDecal
@ kDecal
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, const std::string &label="Snapshot") const
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition: contents.cc:66
tiled_texture_contents.h
impeller::Vector2
Point Vector2
Definition: point.h:320
impeller::ColorSourceContents::GetGeometry
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:20
impeller::TiledTextureContents::SetSamplerDescriptor
void SetSamplerDescriptor(SamplerDescriptor desc)
Definition: tiled_texture_contents.cc:51
impeller::ContentContext::GetTiledTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTiledTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:516
impeller::SamplerAddressMode::kClampToEdge
@ kClampToEdge
impeller::Entity::TileMode::kRepeat
@ kRepeat
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::Entity::TileMode::kMirror
@ kMirror
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:15
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
impeller::Entity
Definition: entity.h:21
impeller::Capabilities
Definition: capabilities.h:15
impeller::TSize< Scalar >
render_pass.h
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:564
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:38
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:25
impeller::SamplerDescriptor::width_address_mode
SamplerAddressMode width_address_mode
Definition: sampler_descriptor.h:20
impeller::Entity::TileMode
TileMode
Definition: entity.h:42
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::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:217
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
impeller::ColorSourceContents::PipelineBuilderMethod
std::shared_ptr< Pipeline< PipelineDescriptor > >(impeller::ContentContext::*)(ContentContextOptions) const PipelineBuilderMethod
Definition: color_source_contents.h:110
content_context.h
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:568
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::SamplerAddressMode::kMirror
@ kMirror
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
scale
const Scalar scale
Definition: stroke_path_geometry.cc:297
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, const std::string &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:223
impeller::TiledTextureContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: tiled_texture_contents.cc:111
impeller::TiledTextureContents::SetTileModes
void SetTileModes(Entity::TileMode x_tile_mode, Entity::TileMode y_tile_mode)
Definition: tiled_texture_contents.cc:45
impeller::TiledTextureContents::~TiledTextureContents
~TiledTextureContents() override
impeller::TileModeToAddressMode
static std::optional< SamplerAddressMode > TileModeToAddressMode(Entity::TileMode tile_mode, const Capabilities &capabilities)
Definition: tiled_texture_contents.cc:16
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:26
impeller::Capabilities::SupportsDecalSamplerAddressMode
virtual bool SupportsDecalSamplerAddressMode() const =0
Whether the context backend supports SamplerAddressMode::Decal.
impeller::ContentContextOptions
Definition: content_context.h:288
impeller::SamplerDescriptor::height_address_mode
SamplerAddressMode height_address_mode
Definition: sampler_descriptor.h:21
impeller
Definition: aiks_blur_unittests.cc:20
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::ContentContext
Definition: content_context.h:392
impeller::ColorSourceContents::GetInverseEffectTransform
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
Definition: color_source_contents.cc:36
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:833
impeller::TiledTextureContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: tiled_texture_contents.cc:41
impeller::SamplerAddressMode::kRepeat
@ kRepeat
impeller::SamplerAddressMode::kDecal
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...