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 
40 
41 void TiledTextureContents::SetTexture(std::shared_ptr<Texture> texture) {
42  texture_ = std::move(texture);
43 }
44 
46  Entity::TileMode y_tile_mode) {
47  x_tile_mode_ = x_tile_mode;
48  y_tile_mode_ = y_tile_mode;
49 }
50 
52  sampler_descriptor_ = desc;
53 }
54 
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  /*options=*/
69  {.coverage_limit = std::nullopt,
70  .sampler_descriptor = std::nullopt,
71  .msaa_enabled = true,
72  .mip_count = 1,
73  .label = "TiledTextureContents Snapshot"});
74  if (snapshot.has_value()) {
75  return snapshot.value().texture;
76  }
77  return nullptr;
78 }
79 
80 SamplerDescriptor TiledTextureContents::CreateSamplerDescriptor(
81  const Capabilities& capabilities) const {
82  SamplerDescriptor descriptor = sampler_descriptor_;
83  auto width_mode = TileModeToAddressMode(x_tile_mode_, capabilities);
84  auto height_mode = TileModeToAddressMode(y_tile_mode_, capabilities);
85  if (width_mode.has_value()) {
86  descriptor.width_address_mode = width_mode.value();
87  }
88  if (height_mode.has_value()) {
89  descriptor.height_address_mode = height_mode.value();
90  }
91  return descriptor;
92 }
93 
94 bool TiledTextureContents::UsesEmulatedTileMode(
95  const Capabilities& capabilities) const {
96  return !TileModeToAddressMode(x_tile_mode_, capabilities).has_value() ||
97  !TileModeToAddressMode(y_tile_mode_, capabilities).has_value();
98 }
99 
100 // |Contents|
102  if (GetOpacityFactor() < 1 || x_tile_mode_ == Entity::TileMode::kDecal ||
103  y_tile_mode_ == Entity::TileMode::kDecal) {
104  return false;
105  }
106  if (color_filter_) {
107  return false;
108  }
109  return texture_->IsOpaque() && !AppliesAlphaForStrokeCoverage(transform);
110 }
111 
113  const Entity& entity,
114  RenderPass& pass) const {
115  if (texture_ == nullptr) {
116  return true;
117  }
118 
119  using VS = TextureUvFillVertexShader;
120  using FS = TiledTextureFillFragmentShader;
121 
122  const auto texture_size = texture_->GetSize();
123  if (texture_size.IsEmpty()) {
124  return true;
125  }
126 
127  VS::FrameInfo frame_info;
128  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
129  frame_info.uv_transform =
130  Rect::MakeSize(texture_size).GetNormalizingTransform() *
132 
133 #if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_EMSCRIPTEN)
134  using FSExternal = TiledTextureFillExternalFragmentShader;
135  if (texture_->GetTextureDescriptor().type ==
137  return ColorSourceContents::DrawGeometry<VS>(
138  renderer, entity, pass,
139  [&renderer](ContentContextOptions options) {
140  return renderer.GetTiledTextureUvExternalPipeline(options);
141  },
142  frame_info,
143  [this, &renderer](RenderPass& pass) {
144  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
145 #ifdef IMPELLER_DEBUG
146  pass.SetCommandLabel("TextureFill External");
147 #endif // IMPELLER_DEBUG
148 
149  FML_DCHECK(!color_filter_);
150  FSExternal::FragInfo frag_info;
151  frag_info.x_tile_mode =
152  static_cast<Scalar>(sampler_descriptor_.width_address_mode);
153  frag_info.y_tile_mode =
154  static_cast<Scalar>(sampler_descriptor_.height_address_mode);
155  frag_info.alpha = GetOpacityFactor();
156  FSExternal::BindFragInfo(pass,
157  data_host_buffer.EmplaceUniform(frag_info));
158 
159  SamplerDescriptor sampler_desc;
160  // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid,
161  // so we emulate all other tile modes here by remapping the texture
162  // coordinates.
165  sampler_desc.min_filter = sampler_descriptor_.min_filter;
166  sampler_desc.mag_filter = sampler_descriptor_.mag_filter;
167  sampler_desc.mip_filter = MipFilter::kBase;
168 
169  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
170  pass, texture_,
171  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
172  sampler_desc));
173  return true;
174  });
175  }
176 #endif // IMPELLER_ENABLE_OPENGLES
177 
178  PipelineBuilderCallback pipeline_callback =
179  [&renderer](ContentContextOptions options) {
180  return renderer.GetTiledTexturePipeline(options);
181  };
182  return ColorSourceContents::DrawGeometry<VS>(
183  renderer, entity, pass, pipeline_callback, frame_info,
184  [this, &renderer, &entity](RenderPass& pass) {
185  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
186 #ifdef IMPELLER_DEBUG
187  pass.SetCommandLabel("TextureFill");
188 #endif // IMPELLER_DEBUG
189 
190  FS::FragInfo frag_info;
191  frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
192  frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
193  frag_info.alpha =
194  GetOpacityFactor() *
196  FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
197 
198  if (color_filter_) {
199  auto filtered_texture = CreateFilterTexture(renderer);
200  if (!filtered_texture) {
201  return false;
202  }
203  FS::BindTextureSampler(
204  pass, filtered_texture,
205  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
206  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
207  } else {
208  FS::BindTextureSampler(
209  pass, texture_,
210  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
211  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
212  }
213 
214  return true;
215  });
216 }
217 
219  const ContentContext& renderer,
220  const Entity& entity,
221  const SnapshotOptions& options) const {
222  std::optional<Rect> geometry_coverage = GetGeometry()->GetCoverage({});
223  if (GetInverseEffectTransform().IsIdentity() &&
224  GetGeometry()->IsAxisAlignedRect() &&
225  (!geometry_coverage.has_value() ||
226  Rect::MakeSize(texture_->GetSize())
227  .Contains(geometry_coverage.value()))) {
228  auto coverage = GetCoverage(entity);
229  if (!coverage.has_value()) {
230  return std::nullopt;
231  }
232  auto scale = Vector2(coverage->GetSize() / Size(texture_->GetSize()));
233 
234  return Snapshot{
235  .texture = texture_,
236  .transform = Matrix::MakeTranslation(coverage->GetOrigin()) *
237  Matrix::MakeScale(scale),
238  .sampler_descriptor =
239  options.sampler_descriptor.value_or(sampler_descriptor_),
240  .opacity = GetOpacityFactor(),
241  };
242  }
243 
245  renderer, entity,
246  {.coverage_limit = std::nullopt,
247  .sampler_descriptor =
248  options.sampler_descriptor.value_or(sampler_descriptor_),
249  .msaa_enabled = true,
250  .mip_count = 1,
251  .label = options.label});
252 }
253 
254 } // namespace impeller
virtual bool SupportsDecalSamplerAddressMode() const =0
Whether the context backend supports SamplerAddressMode::Decal.
const Geometry * GetGeometry() const
Get the geometry that this contents will use to render.
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
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)
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:429
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