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 
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 
14 namespace impeller {
15 
16 static 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 
46 void 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 
64 std::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 
85 SamplerDescriptor 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 
99 bool 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.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
134  frame_info.uv_transform =
135  Rect::MakeSize(texture_size).GetNormalizingTransform() *
137 
138 #if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_EMSCRIPTEN)
139  using FSExternal = TiledTextureFillExternalFragmentShader;
140  if (texture_->GetTextureDescriptor().type ==
142  return ColorSourceContents::DrawGeometry<VS>(
143  renderer, entity, pass,
144  [&renderer](ContentContextOptions options) {
145  return renderer.GetTiledTextureUvExternalPipeline(options);
146  },
147  frame_info,
148  [this, &renderer](RenderPass& pass) {
149  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
150 #ifdef IMPELLER_DEBUG
151  pass.SetCommandLabel("TextureFill External");
152 #endif // IMPELLER_DEBUG
153 
154  FML_DCHECK(!color_filter_);
155  FSExternal::FragInfo frag_info;
156  frag_info.x_tile_mode =
157  static_cast<Scalar>(sampler_descriptor_.width_address_mode);
158  frag_info.y_tile_mode =
159  static_cast<Scalar>(sampler_descriptor_.height_address_mode);
160  frag_info.alpha = GetOpacityFactor();
161  FSExternal::BindFragInfo(pass,
162  data_host_buffer.EmplaceUniform(frag_info));
163 
164  SamplerDescriptor sampler_desc;
165  // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid,
166  // so we emulate all other tile modes here by remapping the texture
167  // coordinates.
170  sampler_desc.min_filter = sampler_descriptor_.min_filter;
171  sampler_desc.mag_filter = sampler_descriptor_.mag_filter;
172  sampler_desc.mip_filter = MipFilter::kBase;
173 
174  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
175  pass, texture_,
176  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
177  sampler_desc));
178  return true;
179  });
180  }
181 #endif // IMPELLER_ENABLE_OPENGLES
182 
183  PipelineBuilderCallback pipeline_callback =
184  [&renderer](ContentContextOptions options) {
185  return renderer.GetTiledTexturePipeline(options);
186  };
187  return ColorSourceContents::DrawGeometry<VS>(
188  renderer, entity, pass, pipeline_callback, frame_info,
189  [this, &renderer, &entity](RenderPass& pass) {
190  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
191 #ifdef IMPELLER_DEBUG
192  pass.SetCommandLabel("TextureFill");
193 #endif // IMPELLER_DEBUG
194 
195  FS::FragInfo frag_info;
196  frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
197  frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
198  frag_info.alpha =
199  GetOpacityFactor() *
201  FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
202 
203  if (color_filter_) {
204  auto filtered_texture = CreateFilterTexture(renderer);
205  if (!filtered_texture) {
206  return false;
207  }
208  FS::BindTextureSampler(
209  pass, filtered_texture,
210  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
211  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
212  } else {
213  FS::BindTextureSampler(
214  pass, texture_,
215  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
216  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
217  }
218 
219  return true;
220  });
221 }
222 
224  const ContentContext& renderer,
225  const Entity& entity,
226  const SnapshotOptions& options) const {
227  std::optional<Rect> geometry_coverage = GetGeometry()->GetCoverage({});
228  if (GetInverseEffectTransform().IsIdentity() &&
229  GetGeometry()->IsAxisAlignedRect() &&
230  (!geometry_coverage.has_value() ||
231  Rect::MakeSize(texture_->GetSize())
232  .Contains(geometry_coverage.value()))) {
233  auto coverage = GetCoverage(entity);
234  if (!coverage.has_value()) {
235  return std::nullopt;
236  }
237  auto scale = Vector2(coverage->GetSize() / Size(texture_->GetSize()));
238 
239  return Snapshot{
240  .texture = texture_,
241  .transform = Matrix::MakeTranslation(coverage->GetOrigin()) *
242  Matrix::MakeScale(scale),
243  .sampler_descriptor =
244  options.sampler_descriptor.value_or(sampler_descriptor_),
245  .opacity = GetOpacityFactor(),
246  };
247  }
248 
250  renderer, entity,
251  {.coverage_limit = std::nullopt,
252  .sampler_descriptor =
253  options.sampler_descriptor.value_or(sampler_descriptor_),
254  .msaa_enabled = true,
255  .mip_count = 1,
256  .label = options.label});
257 }
258 
259 } // 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
const Capabilities & GetDeviceCapabilities() const
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
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
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
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)
Definition: filter_input.cc:19
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:125
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
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.
Definition: render_pass.cc:97
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)
void SetTexture(std::shared_ptr< Texture > 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:231
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:491
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150