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"
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 
58 bool TextureContents::CanInheritOpacity(const Entity& entity) const {
59  return true;
60 }
61 
63  inherited_opacity_ = opacity;
64 }
65 
67  return opacity_ * inherited_opacity_;
68 }
69 
70 std::optional<Rect> TextureContents::GetCoverage(const Entity& entity) const {
71  if (GetOpacity() == 0) {
72  return std::nullopt;
73  }
74  return destination_rect_.TransformBounds(entity.GetTransformation());
75 };
76 
77 std::optional<Snapshot> TextureContents::RenderToSnapshot(
78  const ContentContext& renderer,
79  const Entity& entity,
80  std::optional<Rect> coverage_limit,
81  const std::optional<SamplerDescriptor>& sampler_descriptor,
82  bool msaa_enabled,
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.size / Size(texture_->GetSize()));
91  return Snapshot{
92  .texture = texture_,
93  .transform = entity.GetTransformation() *
94  Matrix::MakeTranslation(bounds.origin) *
95  Matrix::MakeScale(scale),
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  label); // label
106 }
107 
109  const Entity& entity,
110  RenderPass& pass) const {
111  auto capture = entity.GetCapture().CreateChild("TextureContents");
112 
113  using VS = TextureFillVertexShader;
114  using FS = TextureFillFragmentShader;
115  using FSExternal = TextureFillExternalFragmentShader;
116 
117  if (destination_rect_.size.IsEmpty() || source_rect_.IsEmpty() ||
118  texture_ == nullptr || texture_->GetSize().IsEmpty()) {
119  return true; // Nothing to render.
120  }
121 
122  bool is_external_texture =
123  texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
124 
125  // Expand the source rect by half a texel, which aligns sampled texels to the
126  // pixel grid if the source rect is the same size as the destination rect.
127  auto texture_coords =
128  Rect::MakeSize(texture_->GetSize())
129  .Project(capture.AddRect("Source rect", source_rect_).Expand(0.5));
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 = pass.GetTransientsBuffer();
143 
144  VS::FrameInfo frame_info;
145  frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
146  capture.AddMatrix("Transform", entity.GetTransformation());
147  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
148  frame_info.alpha = capture.AddScalar("Alpha", GetOpacity());
149 
150  Command cmd;
151  if (label_.empty()) {
152  DEBUG_COMMAND_INFO(cmd, "Texture Fill");
153  } else {
154  DEBUG_COMMAND_INFO(cmd, "Texture Fill: " + label_);
155  }
156 
157  auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
158  if (!stencil_enabled_) {
159  pipeline_options.stencil_compare = CompareFunction::kAlways;
160  }
161  pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
162 
163 #ifdef IMPELLER_ENABLE_OPENGLES
164  if (is_external_texture) {
165  cmd.pipeline = renderer.GetTextureExternalPipeline(pipeline_options);
166  } else {
167  cmd.pipeline = renderer.GetTexturePipeline(pipeline_options);
168  }
169 #else
170  cmd.pipeline = renderer.GetTexturePipeline(pipeline_options);
171 #endif // IMPELLER_ENABLE_OPENGLES
172 
173  cmd.stencil_reference = entity.GetStencilDepth();
174  cmd.BindVertices(vertex_builder.CreateVertexBuffer(host_buffer));
175  VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
176  if (is_external_texture) {
177  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
178  cmd, texture_,
179  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
180  sampler_descriptor_));
181  } else {
182  FS::BindTextureSampler(
183  cmd, texture_,
184  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
185  sampler_descriptor_));
186  }
187  pass.AddCommand(std::move(cmd));
188 
189  return true;
190 }
191 
192 void TextureContents::SetSourceRect(const Rect& source_rect) {
193  source_rect_ = source_rect;
194 }
195 
197  return source_rect_;
198 }
199 
201  sampler_descriptor_ = std::move(desc);
202 }
203 
205  return sampler_descriptor_;
206 }
207 
208 void TextureContents::SetDeferApplyingOpacity(bool defer_applying_opacity) {
209  defer_applying_opacity_ = defer_applying_opacity;
210 }
211 
212 } // namespace impeller
impeller::ContentContext::GetTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:427
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::TextureContents::SetSamplerDescriptor
void SetSamplerDescriptor(SamplerDescriptor desc)
Definition: texture_contents.cc:200
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:99
impeller::Capture::CreateChild
Capture CreateChild(const std::string &label)
Definition: capture.h:234
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:31
impeller::Entity::GetStencilDepth
uint32_t GetStencilDepth() const
Definition: entity.cc:89
impeller::TRect::size
TSize< Type > size
Definition: rect.h:24
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::TextureContents::SetOpacity
void SetOpacity(Scalar opacity)
Definition: texture_contents.cc:50
impeller::Entity::GetCapture
Capture & GetCapture() const
Definition: entity.cc:173
texture_contents.h
sampler_library.h
entity.h
impeller::TextureContents::SetInheritedOpacity
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
Definition: texture_contents.cc:62
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:55
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:204
formats.h
impeller::Vector2
Point Vector2
Definition: point.h:310
impeller::TextureContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the screen space bounding rectangle that this contents affects.
Definition: texture_contents.cc:70
impeller::TextureContents::SetSourceRect
void SetSourceRect(const Rect &source_rect)
Definition: texture_contents.cc:192
impeller::TextureContents::SetStencilEnabled
void SetStencilEnabled(bool enabled)
Definition: texture_contents.cc:54
impeller::VertexBufferBuilder::AddVertices
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
Definition: vertex_buffer_builder.h:64
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:94
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::TextureContents::GetOpacity
Scalar GetOpacity() const
Definition: texture_contents.cc:66
impeller::Entity::GetTransformation
const Matrix & GetTransformation() const
Definition: entity.cc:49
impeller::TRect::IsEmpty
constexpr bool IsEmpty() const
Definition: rect.h:133
path_builder.h
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:18
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:30
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:30
impeller::TSize
Definition: size.h:18
impeller::TextureContents::~TextureContents
~TextureContents() override
render_pass.h
impeller::TextureContents::GetSourceRect
const Rect & GetSourceRect() const
Definition: texture_contents.cc:196
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:58
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:440
impeller::VertexBufferBuilder
Definition: vertex_buffer_builder.h:23
impeller::TextureContents::SetLabel
void SetLabel(std::string label)
Definition: texture_contents.cc:34
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:24
impeller::TextureContents::GetTexture
std::shared_ptr< Texture > GetTexture() const
Definition: texture_contents.cc:46
impeller::VertexBufferBuilder::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
Definition: vertex_buffer_builder.h:78
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:27
impeller::TextureContents::SetDeferApplyingOpacity
void SetDeferApplyingOpacity(bool defer_applying_opacity)
Definition: texture_contents.cc:208
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:152
content_context.h
impeller::TextureContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: texture_contents.cc:108
impeller::CompareFunction::kAlways
@ kAlways
Comparison test passes always passes.
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:52
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:299
impeller::Matrix::MakeOrthographic
static constexpr Matrix MakeOrthographic(TSize< T > size)
Definition: matrix.h:448
impeller::TextureContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: texture_contents.cc:42
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Definition: size.h:105
impeller::TextureContents::TextureContents
TextureContents()
impeller::Command::BindVertices
bool BindVertices(const VertexBuffer &buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:25
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:103
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:204
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, const std::string &label="Texture Snapshot") const override
Render this contents to a snapshot, respecting the entity's transform, path, stencil depth,...
Definition: texture_contents.cc:77
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:46
impeller
Definition: aiks_context.cc:10
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:103
impeller::ContentContext
Definition: content_context.h:344
impeller::TRect< Scalar >
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:34
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, stencil depth,...
Definition: contents.cc:59