Flutter Engine
The Flutter Engine
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
6
7#include "fml/logging.h"
9#include "impeller/entity/tiled_texture_fill.frag.h"
10#include "impeller/entity/tiled_texture_fill_external.frag.h"
12
13namespace impeller {
14
15static std::optional<SamplerAddressMode> TileModeToAddressMode(
16 Entity::TileMode tile_mode,
17 const Capabilities& capabilities) {
18 switch (tile_mode) {
21 break;
24 break;
27 break;
29 if (capabilities.SupportsDecalSamplerAddressMode()) {
31 }
32 return std::nullopt;
33 }
34}
35
37
39
40void TiledTextureContents::SetTexture(std::shared_ptr<Texture> texture) {
41 texture_ = std::move(texture);
42}
43
45 Entity::TileMode y_tile_mode) {
46 x_tile_mode_ = x_tile_mode;
47 y_tile_mode_ = y_tile_mode;
48}
49
51 sampler_descriptor_ = std::move(desc);
52}
53
55 color_filter_ = std::move(color_filter);
56}
57
58std::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
78SamplerDescriptor 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
92bool 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|
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();
108}
109
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 using FSExternal = TiledTextureFillExternalFragmentShader;
120
121 const auto texture_size = texture_->GetSize();
122 if (texture_size.IsEmpty()) {
123 return true;
124 }
125
126 bool is_external_texture =
127 texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
128
129 VS::FrameInfo frame_info;
130 frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
131 frame_info.uv_transform =
134
135 PipelineBuilderMethod pipeline_method;
136
137#ifdef IMPELLER_ENABLE_OPENGLES
138 if (is_external_texture) {
139 pipeline_method = &ContentContext::GetTiledTextureExternalPipeline;
140 } else {
142 }
143#else
145#endif // IMPELLER_ENABLE_OPENGLES
146
147 PipelineBuilderCallback pipeline_callback =
148 [&renderer, &pipeline_method](ContentContextOptions options) {
149 return (renderer.*pipeline_method)(options);
150 };
151 return ColorSourceContents::DrawGeometry<VS>(
152 renderer, entity, pass, pipeline_callback, frame_info,
153 [this, &renderer, &is_external_texture, &entity](RenderPass& pass) {
154 auto& host_buffer = renderer.GetTransientsBuffer();
155
156 pass.SetCommandLabel("TextureFill");
157
158 if (is_external_texture) {
159 FSExternal::FragInfo frag_info;
160 frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
161 frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
162 frag_info.alpha =
163 GetOpacityFactor() * GetGeometry()->ComputeAlphaCoverage(entity);
164 FSExternal::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
165 } else {
166 FS::FragInfo frag_info;
167 frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
168 frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
169 frag_info.alpha =
170 GetOpacityFactor() * GetGeometry()->ComputeAlphaCoverage(entity);
171 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
172 }
173
174 if (is_external_texture) {
175 SamplerDescriptor sampler_desc;
176 // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid, so
177 // we emulate all other tile modes here by remapping the texture
178 // coordinates.
181
182 // Also, external textures cannot be bound to color filters, so ignore
183 // this case for now.
184 FML_DCHECK(!color_filter_) << "Color filters are not currently "
185 "supported for external textures.";
186
187 FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
188 pass, texture_,
189 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
190 sampler_desc));
191 } else {
192 if (color_filter_) {
193 auto filtered_texture = CreateFilterTexture(renderer);
194 if (!filtered_texture) {
195 return false;
196 }
197 FS::BindTextureSampler(
198 pass, filtered_texture,
199 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
200 CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
201 } else {
202 FS::BindTextureSampler(
203 pass, texture_,
204 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
205 CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
206 }
207 }
208
209 return true;
210 });
211}
212
215 const Entity& entity,
216 std::optional<Rect> coverage_limit,
217 const std::optional<SamplerDescriptor>& sampler_descriptor,
218 bool msaa_enabled,
219 int32_t mip_count,
220 const std::string& label) const {
221 std::optional<Rect> geometry_coverage = GetGeometry()->GetCoverage({});
222 if (GetInverseEffectTransform().IsIdentity() &&
223 GetGeometry()->IsAxisAlignedRect() &&
224 (!geometry_coverage.has_value() ||
225 Rect::MakeSize(texture_->GetSize())
226 .Contains(geometry_coverage.value()))) {
227 auto coverage = GetCoverage(entity);
228 if (!coverage.has_value()) {
229 return std::nullopt;
230 }
231 auto scale = Vector2(coverage->GetSize() / Size(texture_->GetSize()));
232
233 return Snapshot{
234 .texture = texture_,
235 .transform = Matrix::MakeTranslation(coverage->GetOrigin()) *
237 .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
238 .opacity = GetOpacityFactor(),
239 };
240 }
241
243 renderer, // renderer
244 entity, // entity
245 std::nullopt, // coverage_limit
246 sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
247 true, // msaa_enabled
248 /*mip_count=*/1,
249 label); // label
250}
251
252} // namespace impeller
const char * options
static sk_sp< SkImage > color_filter(const SkImage *image, SkColorFilter *colorFilter)
virtual bool SupportsDecalSamplerAddressMode() const =0
Whether the context backend supports SamplerAddressMode::Decal.
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
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.
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
std::shared_ptr< Pipeline< PipelineDescriptor > >(impeller::ContentContext::*)(ContentContextOptions) const PipelineBuilderMethod
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTiledTexturePipeline(ContentContextOptions opts) const
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:63
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
void SetSamplerDescriptor(SamplerDescriptor desc)
bool IsOpaque() const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
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,...
void SetColorFilter(ColorFilterProc color_filter)
Set a color filter to apply directly to this tiled texture.
void SetTileModes(Entity::TileMode x_tile_mode, Entity::TileMode y_tile_mode)
void SetTexture(std::shared_ptr< Texture > texture)
#define FML_DCHECK(condition)
Definition: logging.h:103
FlTexture * texture
Point Vector2
Definition: point.h:326
float Scalar
Definition: scalar.h:18
SolidFillVertexShader VS
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
static std::optional< SamplerAddressMode > TileModeToAddressMode(Entity::TileMode tile_mode, const Capabilities &capabilities)
const Scalar scale
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
SamplerAddressMode width_address_mode
SamplerAddressMode height_address_mode
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:24
std::shared_ptr< Texture > texture
Definition: snapshot.h:25
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:221
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:482
static constexpr TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146