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_external.frag.h"
20 
21 namespace impeller {
22 
24 
26 
27 std::shared_ptr<TextureContents> TextureContents::MakeRect(Rect destination) {
28  auto contents = std::make_shared<TextureContents>();
29  contents->destination_rect_ = destination;
30  return contents;
31 }
32 
33 void TextureContents::SetLabel(std::string label) {
34  label_ = std::move(label);
35 }
36 
38  destination_rect_ = rect;
39 }
40 
41 void TextureContents::SetTexture(std::shared_ptr<Texture> texture) {
42  texture_ = std::move(texture);
43 }
44 
45 std::shared_ptr<Texture> TextureContents::GetTexture() const {
46  return texture_;
47 }
48 
50  opacity_ = opacity;
51 }
52 
54  stencil_enabled_ = enabled;
55 }
56 
57 bool TextureContents::CanInheritOpacity(const Entity& entity) const {
58  return true;
59 }
60 
62  inherited_opacity_ = opacity;
63 }
64 
66  return opacity_ * inherited_opacity_;
67 }
68 
69 std::optional<Rect> TextureContents::GetCoverage(const Entity& entity) const {
70  if (GetOpacity() == 0) {
71  return std::nullopt;
72  }
73  return destination_rect_.TransformBounds(entity.GetTransform());
74 };
75 
76 std::optional<Snapshot> TextureContents::RenderToSnapshot(
77  const ContentContext& renderer,
78  const Entity& entity,
79  std::optional<Rect> coverage_limit,
80  const std::optional<SamplerDescriptor>& sampler_descriptor,
81  bool msaa_enabled,
82  int32_t mip_count,
83  const std::string& label) const {
84  // Passthrough textures that have simple rectangle paths and complete source
85  // rects.
86  auto bounds = destination_rect_;
87  auto opacity = GetOpacity();
88  if (source_rect_ == Rect::MakeSize(texture_->GetSize()) &&
89  (opacity >= 1 - kEhCloseEnough || defer_applying_opacity_)) {
90  auto scale = Vector2(bounds.GetSize() / Size(texture_->GetSize()));
91  return Snapshot{
92  .texture = texture_,
93  .transform = entity.GetTransform() *
94  Matrix::MakeTranslation(bounds.GetOrigin()) *
96  .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
97  .opacity = opacity};
98  }
100  renderer, // renderer
101  entity, // entity
102  std::nullopt, // coverage_limit
103  sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
104  true, // msaa_enabled
105  /*mip_count=*/mip_count,
106  label); // label
107 }
108 
110  const Entity& entity,
111  RenderPass& pass) const {
112  auto capture = entity.GetCapture().CreateChild("TextureContents");
113 
114  using VS = TextureFillVertexShader;
115  using FS = TextureFillFragmentShader;
116  using FSStrictSrc = TextureFillStrictSrcFragmentShader;
117  using FSExternal = TextureFillExternalFragmentShader;
118 
119  if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
120  texture_ == nullptr || texture_->GetSize().IsEmpty()) {
121  return true; // Nothing to render.
122  }
123 
124  bool is_external_texture =
125  texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
126 
127  auto source_rect = capture.AddRect("Source rect", source_rect_);
128  auto texture_coords =
129  Rect::MakeSize(texture_->GetSize()).Project(source_rect);
130 
132 
133  auto destination_rect =
134  capture.AddRect("Destination rect", destination_rect_);
135  vertex_builder.AddVertices({
136  {destination_rect.GetLeftTop(), texture_coords.GetLeftTop()},
137  {destination_rect.GetRightTop(), texture_coords.GetRightTop()},
138  {destination_rect.GetLeftBottom(), texture_coords.GetLeftBottom()},
139  {destination_rect.GetRightBottom(), texture_coords.GetRightBottom()},
140  });
141 
142  auto& host_buffer = renderer.GetTransientsBuffer();
143 
144  VS::FrameInfo frame_info;
145  frame_info.mvp = entity.GetShaderTransform(pass);
146  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
147  frame_info.alpha = capture.AddScalar("Alpha", GetOpacity());
148 
149 #ifdef IMPELLER_DEBUG
150  if (label_.empty()) {
151  pass.SetCommandLabel("Texture Fill");
152  } else {
153  pass.SetCommandLabel("Texture Fill: " + label_);
154  }
155 #endif // IMPELLER_DEBUG
156 
157  auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
158  if (!stencil_enabled_) {
159  pipeline_options.stencil_mode = ContentContextOptions::StencilMode::kIgnore;
160  }
161  pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
162 
163  std::shared_ptr<Pipeline<PipelineDescriptor>> pipeline;
164 #ifdef IMPELLER_ENABLE_OPENGLES
165  if (is_external_texture) {
166  pipeline = renderer.GetTextureExternalPipeline(pipeline_options);
167  }
168 #endif // IMPELLER_ENABLE_OPENGLES
169 
170  if (!pipeline) {
171  if (strict_source_rect_enabled_) {
172  pipeline = renderer.GetTextureStrictSrcPipeline(pipeline_options);
173  } else {
174  pipeline = renderer.GetTexturePipeline(pipeline_options);
175  }
176  }
177  pass.SetPipeline(pipeline);
178 
179  pass.SetStencilReference(entity.GetClipDepth());
180  pass.SetVertexBuffer(vertex_builder.CreateVertexBuffer(host_buffer));
181  VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
182  if (is_external_texture) {
183  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
184  pass, texture_,
185  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
186  sampler_descriptor_));
187  } else if (strict_source_rect_enabled_) {
188  // For a strict source rect, shrink the texture coordinate range by half a
189  // texel to ensure that linear filtering does not sample anything outside
190  // the source rect bounds.
191  auto strict_texture_coords =
192  Rect::MakeSize(texture_->GetSize()).Project(source_rect.Expand(-0.5));
193 
194  FSStrictSrc::FragInfo frag_info;
195  frag_info.source_rect = Vector4(strict_texture_coords.GetLTRB());
196  FSStrictSrc::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
197  FSStrictSrc::BindTextureSampler(
198  pass, texture_,
199  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
200  sampler_descriptor_));
201  } else {
202  FS::BindTextureSampler(
203  pass, texture_,
204  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
205  sampler_descriptor_));
206  }
207  return pass.Draw().ok();
208 }
209 
210 void TextureContents::SetSourceRect(const Rect& source_rect) {
211  source_rect_ = source_rect;
212 }
213 
215  return source_rect_;
216 }
217 
219  strict_source_rect_enabled_ = strict;
220 }
221 
223  return strict_source_rect_enabled_;
224 }
225 
227  sampler_descriptor_ = std::move(desc);
228 }
229 
231  return sampler_descriptor_;
232 }
233 
234 void TextureContents::SetDeferApplyingOpacity(bool defer_applying_opacity) {
235  defer_applying_opacity_ = defer_applying_opacity;
236 }
237 
238 } // namespace impeller
impeller::ContentContext::GetTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:485
impeller::ContentContextOptions::StencilMode::kIgnore
@ kIgnore
Turn the stencil test off. Used when drawing without stencil-then-cover.
impeller::ContentContext::GetTextureStrictSrcPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTextureStrictSrcPipeline(ContentContextOptions opts) const
Definition: content_context.h:490
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:53
impeller::TextureContents::SetSamplerDescriptor
void SetSamplerDescriptor(SamplerDescriptor desc)
Definition: texture_contents.cc:226
impeller::TextureContents::SetStrictSourceRect
void SetStrictSourceRect(bool strict)
Definition: texture_contents.cc:218
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::TextureContents::SetOpacity
void SetOpacity(Scalar opacity)
Definition: texture_contents.cc:49
impeller::Entity::GetCapture
Capture & GetCapture() const
Definition: entity.cc:206
texture_contents.h
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
entity.h
impeller::TextureContents::SetInheritedOpacity
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
Definition: texture_contents.cc:61
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:76
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:56
impeller::Vector4
Definition: vector.h:232
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:66
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:405
formats.h
impeller::TextureContents::GetStrictSourceRect
bool GetStrictSourceRect() const
Definition: texture_contents.cc:222
impeller::Vector2
Point Vector2
Definition: point.h:320
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:69
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:210
impeller::TextureContents::SetStencilEnabled
void SetStencilEnabled(bool enabled)
Definition: texture_contents.cc:53
impeller::VertexBufferBuilder::AddVertices
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
Definition: vertex_buffer_builder.h:70
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::TextureContents::GetOpacity
Scalar GetOpacity() const
Definition: texture_contents.cc:65
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:264
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:15
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:37
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:214
impeller::TextureContents::CanInheritOpacity
bool CanInheritOpacity(const Entity &entity) const override
Whether or not this contents can accept the opacity peephole optimization.
Definition: texture_contents.cc:57
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:564
impeller::VertexBufferBuilder
Definition: vertex_buffer_builder.h:24
impeller::TextureContents::SetLabel
void SetLabel(std::string label)
Definition: texture_contents.cc:33
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:25
impeller::TextureContents::GetTexture
std::shared_ptr< Texture > GetTexture() const
Definition: texture_contents.cc:45
impeller::RenderPass::SetStencilReference
virtual void SetStencilReference(uint32_t value)
Definition: render_pass.cc:103
impeller::VertexBufferBuilder::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
Definition: vertex_buffer_builder.h:84
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:105
impeller::TextureContents::SetDeferApplyingOpacity
void SetDeferApplyingOpacity(bool defer_applying_opacity)
Definition: texture_contents.cc:234
content_context.h
impeller::TextureContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: texture_contents.cc:109
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
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:598
scale
const Scalar scale
Definition: stroke_path_geometry.cc:297
impeller::TextureContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: texture_contents.cc:41
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:26
impeller::Capture::CreateChild
Capture CreateChild(std::string_view label)
Definition: capture.h:239
impeller::TextureContents::SetDestinationRect
void SetDestinationRect(Rect rect)
Definition: texture_contents.cc:37
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:27
impeller::TextureContents::GetSamplerDescriptor
const SamplerDescriptor & GetSamplerDescriptor() const
Definition: texture_contents.cc:230
impeller
Definition: aiks_blur_unittests.cc:20
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::ContentContext
Definition: content_context.h:392
impeller::TRect< Scalar >
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:833
vertex_buffer_builder.h