| 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
5 #include "impeller/entity/contents/texture_contents.h"
6
7 #include <memory>
8 #include <optional>
9 #include <utility>
10
11 #include "impeller/core/formats.h"
12 #include "impeller/entity/contents/content_context.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"
17 #include "impeller/geometry/constants.h"
18 #include "impeller/renderer/render_pass.h"
19 #include "impeller/renderer/vertex_buffer_builder.h"
20
21 namespace impeller {
22
23 TextureContents::TextureContents() = default;
24
25 TextureContents::~TextureContents() = default;
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
37 void TextureContents::SetDestinationRect(Rect rect) {
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
49 void TextureContents::SetOpacity(Scalar opacity) {
50 opacity_ = opacity;
51 }
52
53 void TextureContents::SetStencilEnabled(bool enabled) {
54 stencil_enabled_ = enabled;
55 }
56
57 bool TextureContents::CanInheritOpacity(const Entity& entity) const {
58 return true;
59 }
60
61 void TextureContents::SetInheritedOpacity(Scalar opacity) {
62 inherited_opacity_ = opacity;
63 }
64
65 Scalar TextureContents::GetOpacity() const {
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 const std::string& label) const {
83 // Passthrough textures that have simple rectangle paths and complete source
84 // rects.
85 auto bounds = destination_rect_;
86 auto opacity = GetOpacity();
87 if (source_rect_ == Rect::MakeSize(texture_->GetSize()) &&
88 (opacity >= 1 - kEhCloseEnough || defer_applying_opacity_)) {
89 auto scale = Vector2(bounds.GetSize() / Size(texture_->GetSize()));
90 return Snapshot{
91 .texture = texture_,
92 .transform = entity.GetTransform() *
93 Matrix::MakeTranslation(bounds.GetOrigin()) *
94 Matrix::MakeScale(scale),
95 .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
96 .opacity = opacity};
97 }
98 return Contents::RenderToSnapshot(
99 renderer, // renderer
100 entity, // entity
101 std::nullopt, // coverage_limit
102 sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
103 true, // msaa_enabled
104 label); // label
105 }
106
107 bool TextureContents::Render(const ContentContext& renderer,
108 const Entity& entity,
109 RenderPass& pass) const {
110 auto capture = entity.GetCapture().CreateChild("TextureContents");
111
112 using VS = TextureFillVertexShader;
113 using FS = TextureFillFragmentShader;
114 using FSExternal = TextureFillExternalFragmentShader;
115
116 if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
117 texture_ == nullptr || texture_->GetSize().IsEmpty()) {
118 return true; // Nothing to render.
119 }
120
121 bool is_external_texture =
122 texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
123
124 // Expand the source rect by half a texel, which aligns sampled texels to the
125 // pixel grid if the source rect is the same size as the destination rect.
126 auto texture_coords =
127 Rect::MakeSize(texture_->GetSize())
128 .Project(capture.AddRect("Source rect", source_rect_).Expand(0.5));
129
130 VertexBufferBuilder<VS::PerVertexData> vertex_builder;
131
132 auto destination_rect =
133 capture.AddRect("Destination rect", destination_rect_);
134 vertex_builder.AddVertices({
135 {destination_rect.GetLeftTop(), texture_coords.GetLeftTop()},
136 {destination_rect.GetRightTop(), texture_coords.GetRightTop()},
137 {destination_rect.GetLeftBottom(), texture_coords.GetLeftBottom()},
138 {destination_rect.GetRightBottom(), texture_coords.GetRightBottom()},
139 });
140
141 auto& host_buffer = pass.GetTransientsBuffer();
142
143 VS::FrameInfo frame_info;
144 frame_info.mvp = pass.GetOrthographicTransform() *
145 capture.AddMatrix("Transform", entity.GetTransform());
146 frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
147 frame_info.alpha = capture.AddScalar("Alpha", GetOpacity());
148
149 Command cmd;
150 if (label_.empty()) {
151 DEBUG_COMMAND_INFO(cmd, "Texture Fill");
152 } else {
153 DEBUG_COMMAND_INFO(cmd, "Texture Fill: " + label_);
154 }
155
156 auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
157 if (!stencil_enabled_) {
158 pipeline_options.stencil_compare = CompareFunction::kAlways;
159 }
160 pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
161
162 #ifdef IMPELLER_ENABLE_OPENGLES
163 if (is_external_texture) {
164 cmd.pipeline = renderer.GetTextureExternalPipeline(pipeline_options);
165 } else {
166 cmd.pipeline = renderer.GetTexturePipeline(pipeline_options);
167 }
168 #else
169 cmd.pipeline = renderer.GetTexturePipeline(pipeline_options);
170 #endif // IMPELLER_ENABLE_OPENGLES
171
172 cmd.stencil_reference = entity.GetClipDepth();
173 cmd.BindVertices(vertex_builder.CreateVertexBuffer(host_buffer));
174 VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
175 if (is_external_texture) {
176 FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
177 cmd, texture_,
178 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
179 sampler_descriptor_));
180 } else {
181 FS::BindTextureSampler(
182 cmd, texture_,
183 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
184 sampler_descriptor_));
185 }
186 pass.AddCommand(std::move(cmd));
187
188 return true;
189 }
190
191 void TextureContents::SetSourceRect(const Rect& source_rect) {
192 source_rect_ = source_rect;
193 }
194
195 const Rect& TextureContents::GetSourceRect() const {
196 return source_rect_;
197 }
198
199 void TextureContents::SetSamplerDescriptor(SamplerDescriptor desc) {
200 sampler_descriptor_ = std::move(desc);
201 }
202
203 const SamplerDescriptor& TextureContents::GetSamplerDescriptor() const {
204 return sampler_descriptor_;
205 }
206
207 void TextureContents::SetDeferApplyingOpacity(bool defer_applying_opacity) {
208 defer_applying_opacity_ = defer_applying_opacity;
209 }
210
211 } // namespace impeller
impeller::ContentContext::GetTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:415
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::TextureContents::SetSamplerDescriptor
void SetSamplerDescriptor(SamplerDescriptor desc)
Definition: texture_contents.cc:199
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::TextureContents::SetOpacity
void SetOpacity(Scalar opacity)
Definition: texture_contents.cc:49
impeller::Entity::GetCapture
Capture & GetCapture() const
Definition: entity.cc:177
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::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:56
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:264
formats.h
impeller::Vector2
Point Vector2
Definition: point.h:312
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::TextureContents::SetSourceRect
void SetSourceRect(const Rect &source_rect)
Definition: texture_contents.cc:191
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::GetOrthographicTransform
const Matrix & GetOrthographicTransform() const
Definition: render_pass.cc:51
impeller::TextureContents::GetOpacity
Scalar GetOpacity() const
Definition: texture_contents.cc:65
impeller::TRect::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:138
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::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:195
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:479
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::Command::BindVertices
bool BindVertices(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::TextureContents::GetTexture
std::shared_ptr< Texture > GetTexture() const
Definition: texture_contents.cc:45
impeller::CompareFunction::kAlways
@ kAlways
Comparison test passes always passes.
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:29
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:93
impeller::TextureContents::SetDeferApplyingOpacity
void SetDeferApplyingOpacity(bool defer_applying_opacity)
Definition: texture_contents.cc:207
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:122
content_context.h
impeller::TextureContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: texture_contents.cc:107
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:44
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:424
impeller::TextureContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: texture_contents.cc:41
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::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:96
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:203
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, clip depth,...
Definition: texture_contents.cc:76
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::TRect< Scalar >
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:55
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
vertex_buffer_builder.h