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