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"
11 #include "impeller/entity/texture_fill.vert.h"
12 #include "impeller/entity/tiled_texture_fill.frag.h"
13 #include "impeller/entity/tiled_texture_fill_external.frag.h"
15 
16 namespace impeller {
17 
18 static std::optional<SamplerAddressMode> TileModeToAddressMode(
19  Entity::TileMode tile_mode,
20  const Capabilities& capabilities) {
21  switch (tile_mode) {
24  break;
27  break;
30  break;
32  if (capabilities.SupportsDecalSamplerAddressMode()) {
34  }
35  return std::nullopt;
36  }
37 }
38 
40 
42 
43 void TiledTextureContents::SetTexture(std::shared_ptr<Texture> texture) {
44  texture_ = std::move(texture);
45 }
46 
48  Entity::TileMode y_tile_mode) {
49  x_tile_mode_ = x_tile_mode;
50  y_tile_mode_ = y_tile_mode;
51 }
52 
54  sampler_descriptor_ = std::move(desc);
55 }
56 
58  color_filter_ = std::move(color_filter);
59 }
60 
61 std::shared_ptr<Texture> TiledTextureContents::CreateFilterTexture(
62  const ContentContext& renderer) const {
63  if (!color_filter_) {
64  return nullptr;
65  }
66  auto color_filter_contents = color_filter_(FilterInput::Make(texture_));
67  auto snapshot = color_filter_contents->RenderToSnapshot(
68  renderer, // renderer
69  Entity(), // entity
70  std::nullopt, // coverage_limit
71  std::nullopt, // sampler_descriptor
72  true, // msaa_enabled
73  "TiledTextureContents Snapshot"); // label
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();
110 }
111 
113  const Entity& entity,
114  RenderPass& pass) const {
115  if (texture_ == nullptr) {
116  return true;
117  }
118 
119  using VS = TextureFillVertexShader;
120  using FS = TiledTextureFillFragmentShader;
121  using FSExternal = TiledTextureFillExternalFragmentShader;
122 
123  const auto texture_size = texture_->GetSize();
124  if (texture_size.IsEmpty()) {
125  return true;
126  }
127 
128  bool is_external_texture =
129  texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
130 
131  auto& host_buffer = pass.GetTransientsBuffer();
132 
133  auto geometry_result = GetGeometry()->GetPositionUVBuffer(
134  Rect::MakeSize(texture_size), GetInverseEffectTransform(), renderer,
135  entity, pass);
136  bool uses_emulated_tile_mode =
137  UsesEmulatedTileMode(renderer.GetDeviceCapabilities());
138 
139  VS::FrameInfo frame_info;
140  frame_info.mvp = geometry_result.transform;
141  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
142  frame_info.alpha = GetOpacityFactor();
143 
144  Command cmd;
145  if (uses_emulated_tile_mode) {
146  DEBUG_COMMAND_INFO(cmd, "TiledTextureFill");
147  } else {
148  DEBUG_COMMAND_INFO(cmd, "TextureFill");
149  }
150 
151  cmd.stencil_reference = entity.GetClipDepth();
152 
153  auto options = OptionsFromPassAndEntity(pass, entity);
154  if (geometry_result.prevent_overdraw) {
155  options.stencil_compare = CompareFunction::kEqual;
156  options.stencil_operation = StencilOperation::kIncrementClamp;
157  }
158  options.primitive_type = geometry_result.type;
159 
160 #ifdef IMPELLER_ENABLE_OPENGLES
161  if (is_external_texture) {
162  cmd.pipeline = renderer.GetTiledTextureExternalPipeline(options);
163  } else {
164  cmd.pipeline = uses_emulated_tile_mode
165  ? renderer.GetTiledTexturePipeline(options)
166  : renderer.GetTexturePipeline(options);
167  }
168 #else
169  cmd.pipeline = uses_emulated_tile_mode
170  ? renderer.GetTiledTexturePipeline(options)
171  : renderer.GetTexturePipeline(options);
172 #endif // IMPELLER_ENABLE_OPENGLES
173 
174  cmd.BindVertices(std::move(geometry_result.vertex_buffer));
175  VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
176 
177  if (is_external_texture) {
178  FSExternal::FragInfo frag_info;
179  frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
180  frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
181  FSExternal::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
182  } else if (uses_emulated_tile_mode) {
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  FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
187  }
188 
189  if (is_external_texture) {
190  SamplerDescriptor sampler_desc;
191  // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid, so we
192  // emulate all other tile modes here by remapping the texture coordinates.
195 
196  // Also, external textures cannot be bound to color filters, so ignore this
197  // case for now.
198  FML_DCHECK(!color_filter_)
199  << "Color filters are not currently supported for external textures.";
200 
201  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
202  cmd, texture_,
203  renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
204  } else {
205  if (color_filter_) {
206  auto filtered_texture = CreateFilterTexture(renderer);
207  if (!filtered_texture) {
208  return false;
209  }
210  FS::BindTextureSampler(
211  cmd, filtered_texture,
212  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
213  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
214  } else {
215  FS::BindTextureSampler(
216  cmd, texture_,
217  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
218  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
219  }
220  }
221 
222  if (!pass.AddCommand(std::move(cmd))) {
223  return false;
224  }
225 
226  if (geometry_result.prevent_overdraw) {
227  auto restore = ClipRestoreContents();
228  restore.SetRestoreCoverage(GetCoverage(entity));
229  return restore.Render(renderer, entity, pass);
230  }
231  return true;
232 }
233 
235  const ContentContext& renderer,
236  const Entity& entity,
237  std::optional<Rect> coverage_limit,
238  const std::optional<SamplerDescriptor>& sampler_descriptor,
239  bool msaa_enabled,
240  const std::string& label) const {
241  if (GetInverseEffectTransform().IsIdentity() &&
242  GetGeometry()->IsAxisAlignedRect()) {
243  auto coverage = GetCoverage(entity);
244  if (!coverage.has_value()) {
245  return std::nullopt;
246  }
247  auto scale = Vector2(coverage->GetSize() / Size(texture_->GetSize()));
248 
249  return Snapshot{
250  .texture = texture_,
251  .transform = Matrix::MakeTranslation(coverage->GetOrigin()) *
252  Matrix::MakeScale(scale),
253  .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
254  .opacity = GetOpacityFactor(),
255  };
256  }
257 
259  renderer, // renderer
260  entity, // entity
261  std::nullopt, // coverage_limit
262  sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
263  true, // msaa_enabled
264  label); // label
265 }
266 
267 } // namespace impeller
impeller::ContentContext::GetTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:415
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::TiledTextureContents::IsOpaque
bool IsOpaque() const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: tiled_texture_contents.cc:101
impeller::Entity::TileMode::kClamp
@ kClamp
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:29
impeller::Command
An object used to specify work to the GPU along with references to resources the GPU will used when d...
Definition: command.h:92
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::TiledTextureContents::TiledTextureContents
TiledTextureContents()
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
impeller::Entity::TileMode::kDecal
@ kDecal
tiled_texture_contents.h
impeller::Vector2
Point Vector2
Definition: point.h:312
impeller::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
impeller::ColorSourceContents::GetGeometry
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:21
impeller::TiledTextureContents::SetSamplerDescriptor
void SetSamplerDescriptor(SamplerDescriptor desc)
Definition: tiled_texture_contents.cc:53
impeller::ContentContext::GetTiledTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTiledTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:441
impeller::SamplerAddressMode::kClampToEdge
@ kClampToEdge
impeller::Entity::TileMode::kRepeat
@ kRepeat
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::Entity::TileMode::kMirror
@ kMirror
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::Capabilities
Definition: capabilities.h:15
impeller::TSize< Scalar >
render_pass.h
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:479
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:38
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:25
geometry.h
impeller::Command::BindVertices
bool BindVertices(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::SamplerDescriptor::width_address_mode
SamplerAddressMode width_address_mode
Definition: sampler_descriptor.h:20
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, const std::string &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:234
clip_contents.h
impeller::ClipRestoreContents
Definition: clip_contents.h:60
impeller::Entity::TileMode
TileMode
Definition: entity.h:40
impeller::TiledTextureContents::SetColorFilter
void SetColorFilter(ColorFilterProc color_filter)
Set a color filter to apply directly to this tiled texture.
Definition: tiled_texture_contents.cc:57
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:29
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:93
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:122
content_context.h
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:483
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:45
impeller::SamplerAddressMode::kMirror
@ kMirror
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:44
impeller::TiledTextureContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: tiled_texture_contents.cc:112
impeller::TiledTextureContents::SetTileModes
void SetTileModes(Entity::TileMode x_tile_mode, Entity::TileMode y_tile_mode)
Definition: tiled_texture_contents.cc:47
impeller::TiledTextureContents::~TiledTextureContents
~TiledTextureContents() override
impeller::TileModeToAddressMode
static std::optional< SamplerAddressMode > TileModeToAddressMode(Entity::TileMode tile_mode, const Capabilities &capabilities)
Definition: tiled_texture_contents.cc:18
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:26
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:96
impeller::Capabilities::SupportsDecalSamplerAddressMode
virtual bool SupportsDecalSamplerAddressMode() const =0
Whether the context backend supports SamplerAddressMode::Decal.
impeller::SamplerDescriptor::height_address_mode
SamplerAddressMode height_address_mode
Definition: sampler_descriptor.h:21
impeller::RenderPass::AddCommand
bool AddCommand(Command &&command)
Record a command for subsequent encoding to the underlying command buffer. No work is encoded into th...
Definition: render_pass.cc:67
impeller
Definition: aiks_context.cc:10
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::ContentContext
Definition: content_context.h:332
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:55
impeller::ColorSourceContents::GetInverseEffectTransform
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
Definition: color_source_contents.cc:37
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, const std::string &label="Snapshot") const
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition: contents.cc:57
impeller::TiledTextureContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: tiled_texture_contents.cc:43
impeller::SamplerAddressMode::kRepeat
@ kRepeat
impeller::SamplerAddressMode::kDecal
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...