Flutter Impeller
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 <memory>
8 #include <optional>
9 #include <utility>
10 
11 #include "impeller/core/formats.h"
13 #include "impeller/entity/entity.h"
14 #include "impeller/entity/texture_fill.frag.h"
15 #include "impeller/entity/texture_fill.vert.h"
16 #include "impeller/entity/texture_fill_strict_src.frag.h"
17 #include "impeller/entity/tiled_texture_fill_external.frag.h"
21 
22 namespace impeller {
23 
25 
27 
28 std::shared_ptr<TextureContents> TextureContents::MakeRect(Rect destination) {
29  auto contents = std::make_shared<TextureContents>();
30  contents->destination_rect_ = destination;
31  return contents;
32 }
33 
34 void TextureContents::SetLabel(std::string label) {
35  label_ = std::move(label);
36 }
37 
39  destination_rect_ = rect;
40 }
41 
42 void TextureContents::SetTexture(std::shared_ptr<Texture> texture) {
43  texture_ = std::move(texture);
44 }
45 
46 std::shared_ptr<Texture> TextureContents::GetTexture() const {
47  return texture_;
48 }
49 
51  opacity_ = opacity;
52 }
53 
55  stencil_enabled_ = enabled;
56 }
57 
59  inherited_opacity_ = opacity;
60 }
61 
63  return opacity_ * inherited_opacity_;
64 }
65 
66 std::optional<Rect> TextureContents::GetCoverage(const Entity& entity) const {
67  if (GetOpacity() == 0) {
68  return std::nullopt;
69  }
70  return destination_rect_.TransformBounds(entity.GetTransform());
71 };
72 
73 std::optional<Snapshot> TextureContents::RenderToSnapshot(
74  const ContentContext& renderer,
75  const Entity& entity,
76  std::optional<Rect> coverage_limit,
77  const std::optional<SamplerDescriptor>& sampler_descriptor,
78  bool msaa_enabled,
79  int32_t mip_count,
80  const std::string& label) const {
81  // Passthrough textures that have simple rectangle paths and complete source
82  // rects.
83  auto bounds = destination_rect_;
84  auto opacity = GetOpacity();
85  if (source_rect_ == Rect::MakeSize(texture_->GetSize()) &&
86  (opacity >= 1 - kEhCloseEnough || defer_applying_opacity_)) {
87  auto scale = Vector2(bounds.GetSize() / Size(texture_->GetSize()));
88  return Snapshot{
89  .texture = texture_,
90  .transform = entity.GetTransform() *
91  Matrix::MakeTranslation(bounds.GetOrigin()) *
93  .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
94  .opacity = opacity};
95  }
97  renderer, // renderer
98  entity, // entity
99  std::nullopt, // coverage_limit
100  sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
101  true, // msaa_enabled
102  /*mip_count=*/mip_count,
103  label); // label
104 }
105 
107  const Entity& entity,
108  RenderPass& pass) const {
109  using VS = TextureFillVertexShader;
110  using FS = TextureFillFragmentShader;
111  using FSStrict = TextureFillStrictSrcFragmentShader;
112 
113  if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
114  texture_ == nullptr || texture_->GetSize().IsEmpty()) {
115  return true; // Nothing to render.
116  }
117 
118 #ifdef IMPELLER_ENABLE_OPENGLES
119  using FSExternal = TiledTextureFillExternalFragmentShader;
120  bool is_external_texture =
121  texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
122 #endif // IMPELLER_ENABLE_OPENGLES
123 
124  auto texture_coords =
125  Rect::MakeSize(texture_->GetSize()).Project(source_rect_);
126  auto& host_buffer = renderer.GetTransientsBuffer();
127 
128  std::array<VS::PerVertexData, 4> vertices = {
129  VS::PerVertexData{destination_rect_.GetLeftTop(),
130  texture_coords.GetLeftTop()},
131  VS::PerVertexData{destination_rect_.GetRightTop(),
132  texture_coords.GetRightTop()},
133  VS::PerVertexData{destination_rect_.GetLeftBottom(),
134  texture_coords.GetLeftBottom()},
135  VS::PerVertexData{destination_rect_.GetRightBottom(),
136  texture_coords.GetRightBottom()},
137  };
138  auto vertex_buffer = CreateVertexBuffer(vertices, host_buffer);
139 
140  VS::FrameInfo frame_info;
141  frame_info.mvp = entity.GetShaderTransform(pass);
142  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
143 
144 #ifdef IMPELLER_DEBUG
145  if (label_.empty()) {
146  pass.SetCommandLabel("Texture Fill");
147  } else {
148  pass.SetCommandLabel("Texture Fill: " + label_);
149  }
150 #endif // IMPELLER_DEBUG
151 
152  auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
153  if (!stencil_enabled_) {
154  pipeline_options.stencil_mode = ContentContextOptions::StencilMode::kIgnore;
155  }
156  pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
157 
158  pipeline_options.depth_write_enabled =
159  stencil_enabled_ && pipeline_options.blend_mode == BlendMode::kSource;
160 
161 #ifdef IMPELLER_ENABLE_OPENGLES
162  if (is_external_texture) {
163  pass.SetPipeline(
164  renderer.GetTiledTextureExternalPipeline(pipeline_options));
165  } else {
166  pass.SetPipeline(
167  strict_source_rect_enabled_
168  ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
169  : renderer.GetTexturePipeline(pipeline_options));
170  }
171 #else
172  pass.SetPipeline(strict_source_rect_enabled_
173  ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
174  : renderer.GetTexturePipeline(pipeline_options));
175 #endif // IMPELLER_ENABLE_OPENGLES
176 
177  pass.SetVertexBuffer(vertex_buffer);
178  VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
179 
180  if (strict_source_rect_enabled_) {
181  // For a strict source rect, shrink the texture coordinate range by half a
182  // texel to ensure that linear filtering does not sample anything outside
183  // the source rect bounds.
184  auto strict_texture_coords =
185  Rect::MakeSize(texture_->GetSize()).Project(source_rect_.Expand(-0.5));
186 
187  FSStrict::FragInfo frag_info;
188  frag_info.source_rect = Vector4(strict_texture_coords.GetLTRB());
189  frag_info.alpha = GetOpacity();
190  FSStrict::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
191  FSStrict::BindTextureSampler(
192  pass, texture_,
193  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
194  sampler_descriptor_));
195 #ifdef IMPELLER_ENABLE_OPENGLES
196  } else if (is_external_texture) {
197  FSExternal::FragInfo frag_info;
198  frag_info.x_tile_mode =
199  static_cast<Scalar>(sampler_descriptor_.width_address_mode);
200  frag_info.y_tile_mode =
201  static_cast<Scalar>(sampler_descriptor_.height_address_mode);
202  frag_info.alpha = GetOpacity();
203  FSExternal::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
204 
205  SamplerDescriptor sampler_desc;
206  // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid, so
207  // we emulate all other tile modes here by remapping the texture
208  // coordinates.
211  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
212  pass, texture_,
213  renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
214 #endif // IMPELLER_ENABLE_OPENGLES
215  } else {
216  FS::FragInfo frag_info;
217  frag_info.alpha = GetOpacity();
218  FS::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
219  FS::BindTextureSampler(
220  pass, texture_,
221  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
222  sampler_descriptor_));
223  }
224  return pass.Draw().ok();
225 }
226 
227 void TextureContents::SetSourceRect(const Rect& source_rect) {
228  source_rect_ = source_rect;
229 }
230 
232  return source_rect_;
233 }
234 
236  strict_source_rect_enabled_ = strict;
237 }
238 
240  return strict_source_rect_enabled_;
241 }
242 
244  sampler_descriptor_ = std::move(desc);
245 }
246 
248  return sampler_descriptor_;
249 }
250 
251 void TextureContents::SetDeferApplyingOpacity(bool defer_applying_opacity) {
252  defer_applying_opacity_ = defer_applying_opacity;
253 }
254 
255 } // namespace impeller
impeller::ContentContext::GetTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:438
impeller::ContentContextOptions::StencilMode::kIgnore
@ kIgnore
impeller::ContentContext::GetTextureStrictSrcPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTextureStrictSrcPipeline(ContentContextOptions opts) const
Definition: content_context.h:443
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:51
impeller::TextureContents::SetSamplerDescriptor
void SetSamplerDescriptor(SamplerDescriptor desc)
Definition: texture_contents.cc:243
impeller::TextureContents::SetStrictSourceRect
void SetStrictSourceRect(bool strict)
Definition: texture_contents.cc:235
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::TextureContents::SetOpacity
void SetOpacity(Scalar opacity)
Definition: texture_contents.cc:50
texture_contents.h
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:47
entity.h
impeller::TextureContents::SetInheritedOpacity
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
Definition: texture_contents.cc:58
impeller::TextureContents::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, int32_t mip_count=1, const std::string &label="Texture Snapshot") const override
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition: texture_contents.cc:73
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:56
impeller::Vector4
Definition: vector.h:232
impeller::BlendMode::kSource
@ kSource
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, 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
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:466
impeller::TRect::GetLeftTop
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:353
impeller::TextureContents::GetStrictSourceRect
bool GetStrictSourceRect() const
Definition: texture_contents.cc:239
impeller::Vector2
Point Vector2
Definition: point.h:331
impeller::TextureContents::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: texture_contents.cc:66
formats.h
impeller::RenderPass::SetVertexBuffer
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: render_pass.cc:123
impeller::TextureContents::SetSourceRect
void SetSourceRect(const Rect &source_rect)
Definition: texture_contents.cc:227
impeller::TextureContents::SetStencilEnabled
void SetStencilEnabled(bool enabled)
Definition: texture_contents.cc:54
impeller::SamplerAddressMode::kClampToEdge
@ kClampToEdge
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::TRect::GetRightTop
constexpr TPoint< T > GetRightTop() const
Definition: rect.h:357
impeller::TextureContents::GetOpacity
Scalar GetOpacity() const
Definition: texture_contents.cc:62
impeller::RenderPass::Draw
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:127
impeller::TRect::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:291
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
impeller::Entity
Definition: entity.h:20
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::TSize< Scalar >
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::TextureContents::~TextureContents
~TextureContents() override
render_pass.h
impeller::TextureContents::GetSourceRect
const Rect & GetSourceRect() const
Definition: texture_contents.cc:231
impeller::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(std::array< VertexType, size > input, HostBuffer &host_buffer)
Create an index-less vertex buffer from a fixed size array.
Definition: vertex_buffer_builder.h:24
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:550
impeller::TextureContents::SetLabel
void SetLabel(std::string label)
Definition: texture_contents.cc:34
impeller::TRect::GetLeftBottom
constexpr TPoint< T > GetLeftBottom() const
Definition: rect.h:361
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:24
impeller::SamplerDescriptor::width_address_mode
SamplerAddressMode width_address_mode
Definition: sampler_descriptor.h:20
impeller::TextureContents::GetTexture
std::shared_ptr< Texture > GetTexture() const
Definition: texture_contents.cc:46
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
impeller::TextureContents::SetDeferApplyingOpacity
void SetDeferApplyingOpacity(bool defer_applying_opacity)
Definition: texture_contents.cc:251
content_context.h
impeller::TextureContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: texture_contents.cc:106
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
constants.h
impeller::TRect::Project
constexpr TRect< T > Project(TRect< T > source) const
Returns a new rectangle that represents the projection of the source rectangle onto this rectangle....
Definition: rect.h:663
scale
const Scalar scale
Definition: stroke_path_geometry.cc:301
impeller::TextureContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: texture_contents.cc:42
impeller::RenderPass::SetPipeline
virtual void SetPipeline(const std::shared_ptr< Pipeline< PipelineDescriptor >> &pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:92
impeller::TextureContents::TextureContents
TextureContents()
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:25
impeller::TRect::GetRightBottom
constexpr TPoint< T > GetRightBottom() const
Definition: rect.h:365
impeller::TextureContents::SetDestinationRect
void SetDestinationRect(Rect rect)
Definition: texture_contents.cc:38
impeller::TextureContents::MakeRect
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
A common case factory that marks the texture contents as having a destination rectangle....
Definition: texture_contents.cc:28
impeller::TextureContents::GetSamplerDescriptor
const SamplerDescriptor & GetSamplerDescriptor() const
Definition: texture_contents.cc:247
impeller::SamplerDescriptor::height_address_mode
SamplerAddressMode height_address_mode
Definition: sampler_descriptor.h:21
impeller
Definition: allocation.cc:12
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::ContentContext
Definition: content_context.h:366
impeller::TRect< Scalar >
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:753
impeller::TRect::Expand
constexpr TRect< T > Expand(T left, T top, T right, T bottom) const
Returns a rectangle with expanded edges. Negative expansion results in shrinking.
Definition: rect.h:612
vertex_buffer_builder.h