| 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/tiled_texture_fill.frag.h"
10 #include "impeller/entity/tiled_texture_fill_external.frag.h"
11 #include "impeller/renderer/render_pass.h"
12
13 namespace impeller {
14
15 static std::optional<SamplerAddressMode> TileModeToAddressMode(
16 Entity::TileMode tile_mode,
17 const Capabilities& capabilities) {
18 switch (tile_mode) {
19 case Entity::TileMode::kClamp:
20 return SamplerAddressMode::kClampToEdge;
21 break;
22 case Entity::TileMode::kMirror:
23 return SamplerAddressMode::kMirror;
24 break;
25 case Entity::TileMode::kRepeat:
26 return SamplerAddressMode::kRepeat;
27 break;
28 case Entity::TileMode::kDecal:
29 if (capabilities.SupportsDecalSamplerAddressMode()) {
30 return SamplerAddressMode::kDecal;
31 }
32 return std::nullopt;
33 }
34 }
35
36 TiledTextureContents::TiledTextureContents() = default;
37
38 TiledTextureContents::~TiledTextureContents() = default;
39
40 void TiledTextureContents::SetTexture(std::shared_ptr<Texture> texture) {
41 texture_ = std::move(texture);
42 }
43
44 void TiledTextureContents::SetTileModes(Entity::TileMode x_tile_mode,
45 Entity::TileMode y_tile_mode) {
46 x_tile_mode_ = x_tile_mode;
47 y_tile_mode_ = y_tile_mode;
48 }
49
50 void TiledTextureContents::SetSamplerDescriptor(const SamplerDescriptor& desc) {
51 sampler_descriptor_ = desc;
52 }
53
54 void TiledTextureContents::SetColorFilter(ColorFilterProc color_filter) {
55 color_filter_ = std::move(color_filter);
56 }
57
58 std::shared_ptr<Texture> TiledTextureContents::CreateFilterTexture(
59 const ContentContext& renderer) const {
60 if (!color_filter_) {
61 return nullptr;
62 }
63 auto color_filter_contents = color_filter_(FilterInput::Make(texture_));
64 auto snapshot = color_filter_contents->RenderToSnapshot(
65 renderer, // renderer
66 Entity(), // entity
67 std::nullopt, // coverage_limit
68 std::nullopt, // sampler_descriptor
69 true, // msaa_enabled
70 /*mip_count=*/1,
71 "TiledTextureContents Snapshot"); // label
72 if (snapshot.has_value()) {
73 return snapshot.value().texture;
74 }
75 return nullptr;
76 }
77
78 SamplerDescriptor TiledTextureContents::CreateSamplerDescriptor(
79 const Capabilities& capabilities) const {
80 SamplerDescriptor descriptor = sampler_descriptor_;
81 auto width_mode = TileModeToAddressMode(x_tile_mode_, capabilities);
82 auto height_mode = TileModeToAddressMode(y_tile_mode_, capabilities);
83 if (width_mode.has_value()) {
84 descriptor.width_address_mode = width_mode.value();
85 }
86 if (height_mode.has_value()) {
87 descriptor.height_address_mode = height_mode.value();
88 }
89 return descriptor;
90 }
91
92 bool TiledTextureContents::UsesEmulatedTileMode(
93 const Capabilities& capabilities) const {
94 return !TileModeToAddressMode(x_tile_mode_, capabilities).has_value() ||
95 !TileModeToAddressMode(y_tile_mode_, capabilities).has_value();
96 }
97
98 // |Contents|
99 bool TiledTextureContents::IsOpaque(const Matrix& transform) const {
100 if (GetOpacityFactor() < 1 || x_tile_mode_ == Entity::TileMode::kDecal ||
101 y_tile_mode_ == Entity::TileMode::kDecal) {
102 return false;
103 }
104 if (color_filter_) {
105 return false;
106 }
107 return texture_->IsOpaque() && !AppliesAlphaForStrokeCoverage(transform);
108 }
109
110 bool TiledTextureContents::Render(const ContentContext& renderer,
111 const Entity& entity,
112 RenderPass& pass) const {
113 if (texture_ == nullptr) {
114 return true;
115 }
116
117 using VS = TextureUvFillVertexShader;
118 using FS = TiledTextureFillFragmentShader;
119
120 const auto texture_size = texture_->GetSize();
121 if (texture_size.IsEmpty()) {
122 return true;
123 }
124
125 VS::FrameInfo frame_info;
126 frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
127 frame_info.uv_transform =
128 Rect::MakeSize(texture_size).GetNormalizingTransform() *
129 GetInverseEffectTransform();
130
131 #ifdef IMPELLER_ENABLE_OPENGLES
132 using FSExternal = TiledTextureFillExternalFragmentShader;
133 if (texture_->GetTextureDescriptor().type ==
134 TextureType::kTextureExternalOES) {
135 return ColorSourceContents::DrawGeometry<VS>(
136 renderer, entity, pass,
137 [&renderer](ContentContextOptions options) {
138 return renderer.GetTiledTextureUvExternalPipeline(options);
139 },
140 frame_info,
141 [this, &renderer](RenderPass& pass) {
142 auto& host_buffer = renderer.GetTransientsBuffer();
143 #ifdef IMPELLER_DEBUG
144 pass.SetCommandLabel("TextureFill External");
145 #endif // IMPELLER_DEBUG
146
147 FML_DCHECK(!color_filter_);
148 FSExternal::FragInfo frag_info;
149 frag_info.x_tile_mode =
150 static_cast<Scalar>(sampler_descriptor_.width_address_mode);
151 frag_info.y_tile_mode =
152 static_cast<Scalar>(sampler_descriptor_.height_address_mode);
153 frag_info.alpha = GetOpacityFactor();
154 FSExternal::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
155
156 SamplerDescriptor sampler_desc;
157 // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid,
158 // so we emulate all other tile modes here by remapping the texture
159 // coordinates.
160 sampler_desc.width_address_mode = SamplerAddressMode::kClampToEdge;
161 sampler_desc.height_address_mode = SamplerAddressMode::kClampToEdge;
162 FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
163 pass, texture_,
164 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
165 sampler_desc));
166 return true;
167 });
168 }
169 #endif // IMPELLER_ENABLE_OPENGLES
170
171 PipelineBuilderCallback pipeline_callback =
172 [&renderer](ContentContextOptions options) {
173 return renderer.GetTiledTexturePipeline(options);
174 };
175 return ColorSourceContents::DrawGeometry<VS>(
176 renderer, entity, pass, pipeline_callback, frame_info,
177 [this, &renderer, &entity](RenderPass& pass) {
178 auto& host_buffer = renderer.GetTransientsBuffer();
179 #ifdef IMPELLER_DEBUG
180 pass.SetCommandLabel("TextureFill");
181 #endif // IMPELLER_DEBUG
182
183 FS::FragInfo frag_info;
184 frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
185 frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
186 frag_info.alpha =
187 GetOpacityFactor() *
188 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
189 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
190
191 if (color_filter_) {
192 auto filtered_texture = CreateFilterTexture(renderer);
193 if (!filtered_texture) {
194 return false;
195 }
196 FS::BindTextureSampler(
197 pass, filtered_texture,
198 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
199 CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
200 } else {
201 FS::BindTextureSampler(
202 pass, texture_,
203 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
204 CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
205 }
206
207 return true;
208 });
209 }
210
211 std::optional<Snapshot> TiledTextureContents::RenderToSnapshot(
212 const ContentContext& renderer,
213 const Entity& entity,
214 std::optional<Rect> coverage_limit,
215 const std::optional<SamplerDescriptor>& sampler_descriptor,
216 bool msaa_enabled,
217 int32_t mip_count,
218 std::string_view label) const {
219 std::optional<Rect> geometry_coverage = GetGeometry()->GetCoverage({});
220 if (GetInverseEffectTransform().IsIdentity() &&
221 GetGeometry()->IsAxisAlignedRect() &&
222 (!geometry_coverage.has_value() ||
223 Rect::MakeSize(texture_->GetSize())
224 .Contains(geometry_coverage.value()))) {
225 auto coverage = GetCoverage(entity);
226 if (!coverage.has_value()) {
227 return std::nullopt;
228 }
229 auto scale = Vector2(coverage->GetSize() / Size(texture_->GetSize()));
230
231 return Snapshot{
232 .texture = texture_,
233 .transform = Matrix::MakeTranslation(coverage->GetOrigin()) *
234 Matrix::MakeScale(scale),
235 .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
236 .opacity = GetOpacityFactor(),
237 };
238 }
239
240 return Contents::RenderToSnapshot(
241 renderer, // renderer
242 entity, // entity
243 std::nullopt, // coverage_limit
244 sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
245 true, // msaa_enabled
246 /*mip_count=*/1,
247 label); // label
248 }
249
250 } // 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:105
impeller::ContentContext
Definition: content_context.h:371
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:866
impeller::ContentContext::GetTiledTexturePipeline
PipelineRef GetTiledTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:485
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:617
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:613
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:20
impeller::Entity::TileMode
TileMode
Definition: entity.h:42
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:115
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:110
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:99
impeller::TiledTextureContents::SetSamplerDescriptor
void SetSamplerDescriptor(const SamplerDescriptor &desc)
Definition: tiled_texture_contents.cc:50
impeller::TiledTextureContents::SetColorFilter
void SetColorFilter(ColorFilterProc color_filter)
Set a color filter to apply directly to this tiled texture.
Definition: tiled_texture_contents.cc:54
impeller::TiledTextureContents::SetTileModes
void SetTileModes(Entity::TileMode x_tile_mode, Entity::TileMode y_tile_mode)
Definition: tiled_texture_contents.cc:44
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:211
impeller::TiledTextureContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: tiled_texture_contents.cc:40
content_context.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:18
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::VS
GlyphAtlasPipeline::VertexShader VS
Definition: text_contents.cc:25
impeller::TileModeToAddressMode
static std::optional< SamplerAddressMode > TileModeToAddressMode(Entity::TileMode tile_mode, const Capabilities &capabilities)
Definition: tiled_texture_contents.cc:15
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::FS
GlyphAtlasPipeline::FragmentShader FS
Definition: text_contents.cc:26
render_pass.h
scale
const Scalar scale
Definition: stroke_path_geometry.cc:316
impeller::ContentContextOptions
Definition: content_context.h:279
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::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::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