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_view label) {
35  label_ = 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  const SnapshotOptions& options) const {
77  // Passthrough textures that have simple rectangle paths and complete source
78  // rects.
79  auto bounds = destination_rect_;
80  auto opacity = GetOpacity();
81  if (source_rect_ == Rect::MakeSize(texture_->GetSize()) &&
82  (opacity >= 1 - kEhCloseEnough || defer_applying_opacity_)) {
83  auto scale = Vector2(bounds.GetSize() / Size(texture_->GetSize()));
84  return Snapshot{.texture = texture_,
85  .transform = entity.GetTransform() *
86  Matrix::MakeTranslation(bounds.GetOrigin()) *
87  Matrix::MakeScale(scale),
88  .sampler_descriptor = options.sampler_descriptor.value_or(
89  sampler_descriptor_),
90  .opacity = opacity};
91  }
93  renderer, entity,
94  {.coverage_limit = std::nullopt,
95  .sampler_descriptor =
96  options.sampler_descriptor.value_or(sampler_descriptor_),
97  .msaa_enabled = true,
98  .mip_count = options.mip_count,
99  .label = options.label,
100  .coverage_expansion = options.coverage_expansion});
101 }
102 
104  const Entity& entity,
105  RenderPass& pass) const {
106  using VS = TextureFillVertexShader;
107  using FS = TextureFillFragmentShader;
108  using FSStrict = TextureFillStrictSrcFragmentShader;
109 
110  if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
111  texture_ == nullptr || texture_->GetSize().IsEmpty()) {
112  return true; // Nothing to render.
113  }
114 
115 #ifdef IMPELLER_ENABLE_OPENGLES
116  using FSExternal = TiledTextureFillExternalFragmentShader;
117  bool is_external_texture =
118  texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
119 #endif // IMPELLER_ENABLE_OPENGLES
120 
121  auto texture_coords =
122  Rect::MakeSize(texture_->GetSize()).Project(source_rect_);
123  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
124 
125  std::array<VS::PerVertexData, 4> vertices = {
126  VS::PerVertexData{destination_rect_.GetLeftTop(),
127  texture_coords.GetLeftTop()},
128  VS::PerVertexData{destination_rect_.GetRightTop(),
129  texture_coords.GetRightTop()},
130  VS::PerVertexData{destination_rect_.GetLeftBottom(),
131  texture_coords.GetLeftBottom()},
132  VS::PerVertexData{destination_rect_.GetRightBottom(),
133  texture_coords.GetRightBottom()},
134  };
135  auto vertex_buffer = CreateVertexBuffer(vertices, data_host_buffer);
136 
137  VS::FrameInfo frame_info;
138  frame_info.mvp = entity.GetShaderTransform(pass);
139  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
140 
141 #ifdef IMPELLER_DEBUG
142  if (label_.empty()) {
143  pass.SetCommandLabel("Texture Fill");
144  } else {
145  pass.SetCommandLabel("Texture Fill: " + label_);
146  }
147 #endif // IMPELLER_DEBUG
148 
149  auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
150  if (!stencil_enabled_) {
151  pipeline_options.stencil_mode = ContentContextOptions::StencilMode::kIgnore;
152  }
153  pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
154 
155  pipeline_options.depth_write_enabled =
156  stencil_enabled_ && pipeline_options.blend_mode == BlendMode::kSrc;
157 
158 #ifdef IMPELLER_ENABLE_OPENGLES
159  if (is_external_texture) {
160  pass.SetPipeline(
161  renderer.GetTiledTextureExternalPipeline(pipeline_options));
162  } else {
163  pass.SetPipeline(
164  strict_source_rect_enabled_
165  ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
166  : renderer.GetTexturePipeline(pipeline_options));
167  }
168 #else
169  pass.SetPipeline(strict_source_rect_enabled_
170  ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
171  : renderer.GetTexturePipeline(pipeline_options));
172 #endif // IMPELLER_ENABLE_OPENGLES
173 
174  pass.SetVertexBuffer(vertex_buffer);
175  VS::BindFrameInfo(pass, data_host_buffer.EmplaceUniform(frame_info));
176 
177  if (strict_source_rect_enabled_) {
178  // For a strict source rect, shrink the texture coordinate range by half a
179  // texel to ensure that linear filtering does not sample anything outside
180  // the source rect bounds.
181  auto strict_texture_coords =
182  Rect::MakeSize(texture_->GetSize()).Project(source_rect_.Expand(-0.5));
183 
184  FSStrict::FragInfo frag_info;
185  frag_info.source_rect = Vector4(strict_texture_coords.GetLTRB());
186  frag_info.alpha = GetOpacity();
187  FSStrict::BindFragInfo(pass, data_host_buffer.EmplaceUniform((frag_info)));
188  FSStrict::BindTextureSampler(
189  pass, texture_,
190  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
191  sampler_descriptor_));
192 #ifdef IMPELLER_ENABLE_OPENGLES
193  } else if (is_external_texture) {
194  FSExternal::FragInfo frag_info;
195  frag_info.x_tile_mode =
196  static_cast<Scalar>(sampler_descriptor_.width_address_mode);
197  frag_info.y_tile_mode =
198  static_cast<Scalar>(sampler_descriptor_.height_address_mode);
199  frag_info.alpha = GetOpacity();
200  FSExternal::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
201 
202  SamplerDescriptor sampler_desc;
203  // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid, so
204  // we emulate all other tile modes here by remapping the texture
205  // coordinates.
208  sampler_desc.min_filter = sampler_descriptor_.min_filter;
209  sampler_desc.mag_filter = sampler_descriptor_.mag_filter;
210  sampler_desc.mip_filter = MipFilter::kBase;
211 
212  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
213  pass, texture_,
214  renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
215 #endif // IMPELLER_ENABLE_OPENGLES
216  } else {
217  FS::FragInfo frag_info;
218  frag_info.alpha = GetOpacity();
219  FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform((frag_info)));
220  FS::BindTextureSampler(
221  pass, texture_,
222  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
223  sampler_descriptor_));
224  }
225  return pass.Draw().ok();
226 }
227 
228 void TextureContents::SetSourceRect(const Rect& source_rect) {
229  source_rect_ = source_rect;
230 }
231 
233  return source_rect_;
234 }
235 
237  strict_source_rect_enabled_ = strict;
238 }
239 
241  return strict_source_rect_enabled_;
242 }
243 
245  sampler_descriptor_ = desc;
246 }
247 
249  return sampler_descriptor_;
250 }
251 
252 void TextureContents::SetDeferApplyingOpacity(bool defer_applying_opacity) {
253  defer_applying_opacity_ = defer_applying_opacity;
254 }
255 
256 } // namespace impeller
PipelineRef GetTextureStrictSrcPipeline(ContentContextOptions opts) const
PipelineRef GetTexturePipeline(ContentContextOptions opts) 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
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:48
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: render_pass.cc:127
virtual void SetPipeline(PipelineRef pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:86
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:208
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
std::shared_ptr< Texture > GetTexture() const
void SetSourceRect(const Rect &source_rect)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
void SetSamplerDescriptor(const SamplerDescriptor &desc)
void SetStrictSourceRect(bool strict)
void SetDeferApplyingOpacity(bool defer_applying_opacity)
const SamplerDescriptor & GetSamplerDescriptor() const
void SetOpacity(Scalar opacity)
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
const Rect & GetSourceRect() const
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
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.
void SetTexture(std::shared_ptr< Texture > texture)
void SetLabel(std::string_view label)
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,...
void SetStencilEnabled(bool enabled)
void SetDestinationRect(Rect rect)
Point Vector2
Definition: point.h:331
float Scalar
Definition: scalar.h:19
constexpr float kEhCloseEnough
Definition: constants.h:57
LinePipeline::FragmentShader FS
VertexBuffer CreateVertexBuffer(std::array< VertexType, size > input, HostBuffer &data_host_buffer)
Create an index-less vertex buffer from a fixed size array.
@ kBase
The texture is sampled as if it only had a single mipmap level.
LinePipeline::VertexShader VS
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
const std::optional< SamplerDescriptor > & sampler_descriptor
Definition: contents.h:87
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 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:618
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:472
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:669
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:297
constexpr TPoint< T > GetLeftBottom() const
Definition: rect.h:367
constexpr TPoint< T > GetRightTop() const
Definition: rect.h:363
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
constexpr TPoint< T > GetRightBottom() const
Definition: rect.h:371
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:359