| 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_strict_src.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 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()) *
95 Matrix::MakeScale(scale),
96 .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
97 .opacity = opacity};
98 }
99 return Contents::RenderToSnapshot(
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
109 bool TextureContents::Render(const ContentContext& renderer,
110 const Entity& entity,
111 RenderPass& pass) const {
112 using VS = TextureFillVertexShader;
113 using FS = TextureFillFragmentShader;
114 using FSStrict = TextureFillStrictSrcFragmentShader;
115
116 if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
117 texture_ == nullptr || texture_->GetSize().IsEmpty()) {
118 return true; // Nothing to render.
119 }
120
121 [[maybe_unused]] bool is_external_texture =
122 texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
123 FML_DCHECK(!is_external_texture);
124
125 auto texture_coords =
126 Rect::MakeSize(texture_->GetSize()).Project(source_rect_);
127
128 VertexBufferBuilder<VS::PerVertexData> vertex_builder;
129 vertex_builder.AddVertices({
130 {destination_rect_.GetLeftTop(), texture_coords.GetLeftTop()},
131 {destination_rect_.GetRightTop(), texture_coords.GetRightTop()},
132 {destination_rect_.GetLeftBottom(), texture_coords.GetLeftBottom()},
133 {destination_rect_.GetRightBottom(), texture_coords.GetRightBottom()},
134 });
135
136 auto& host_buffer = renderer.GetTransientsBuffer();
137
138 VS::FrameInfo frame_info;
139 frame_info.mvp = entity.GetShaderTransform(pass);
140 frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
141
142 #ifdef IMPELLER_DEBUG
143 if (label_.empty()) {
144 pass.SetCommandLabel("Texture Fill");
145 } else {
146 pass.SetCommandLabel("Texture Fill: " + label_);
147 }
148 #endif // IMPELLER_DEBUG
149
150 auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
151 if (!stencil_enabled_) {
152 pipeline_options.stencil_mode = ContentContextOptions::StencilMode::kIgnore;
153 }
154 pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
155
156 pass.SetPipeline(strict_source_rect_enabled_
157 ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
158 : renderer.GetTexturePipeline(pipeline_options));
159
160 pass.SetVertexBuffer(vertex_builder.CreateVertexBuffer(host_buffer));
161 VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
162
163 if (strict_source_rect_enabled_) {
164 // For a strict source rect, shrink the texture coordinate range by half a
165 // texel to ensure that linear filtering does not sample anything outside
166 // the source rect bounds.
167 auto strict_texture_coords =
168 Rect::MakeSize(texture_->GetSize()).Project(source_rect_.Expand(-0.5));
169
170 FSStrict::FragInfo frag_info;
171 frag_info.source_rect = Vector4(strict_texture_coords.GetLTRB());
172 frag_info.alpha = GetOpacity();
173 FSStrict::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
174 FSStrict::BindTextureSampler(
175 pass, texture_,
176 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
177 sampler_descriptor_));
178 } else {
179 FS::FragInfo frag_info;
180 frag_info.alpha = GetOpacity();
181 FS::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
182 FS::BindTextureSampler(
183 pass, texture_,
184 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
185 sampler_descriptor_));
186 }
187 return pass.Draw().ok();
188 }
189
190 void TextureContents::SetSourceRect(const Rect& source_rect) {
191 source_rect_ = source_rect;
192 }
193
194 const Rect& TextureContents::GetSourceRect() const {
195 return source_rect_;
196 }
197
198 void TextureContents::SetStrictSourceRect(bool strict) {
199 strict_source_rect_enabled_ = strict;
200 }
201
202 bool TextureContents::GetStrictSourceRect() const {
203 return strict_source_rect_enabled_;
204 }
205
206 void TextureContents::SetSamplerDescriptor(SamplerDescriptor desc) {
207 sampler_descriptor_ = std::move(desc);
208 }
209
210 const SamplerDescriptor& TextureContents::GetSamplerDescriptor() const {
211 return sampler_descriptor_;
212 }
213
214 void TextureContents::SetDeferApplyingOpacity(bool defer_applying_opacity) {
215 defer_applying_opacity_ = defer_applying_opacity;
216 }
217
218 } // namespace impeller
impeller::ContentContext::GetTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:442
impeller::ContentContextOptions::StencilMode::kIgnore
@ kIgnore
impeller::ContentContext::GetTextureStrictSrcPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTextureStrictSrcPipeline(ContentContextOptions opts) const
Definition: content_context.h:447
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:50
impeller::TextureContents::SetSamplerDescriptor
void SetSamplerDescriptor(SamplerDescriptor desc)
Definition: texture_contents.cc:206
impeller::TextureContents::SetStrictSourceRect
void SetStrictSourceRect(bool strict)
Definition: texture_contents.cc:198
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::TextureContents::SetOpacity
void SetOpacity(Scalar opacity)
Definition: texture_contents.cc:49
texture_contents.h
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
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:63
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:463
formats.h
impeller::TRect::GetLeftTop
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:349
impeller::TextureContents::GetStrictSourceRect
bool GetStrictSourceRect() const
Definition: texture_contents.cc:202
impeller::Vector2
Point Vector2
Definition: point.h:326
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:190
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:67
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:353
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:287
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:194
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:553
impeller::VertexBufferBuilder
Definition: vertex_buffer_builder.h:21
impeller::TextureContents::SetLabel
void SetLabel(std::string label)
Definition: texture_contents.cc:33
impeller::TRect::GetLeftBottom
constexpr TPoint< T > GetLeftBottom() const
Definition: rect.h:357
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:45
impeller::VertexBufferBuilder::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
Definition: vertex_buffer_builder.h:81
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:214
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:656
scale
const Scalar scale
Definition: stroke_path_geometry.cc:308
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:25
impeller::TRect::GetRightBottom
constexpr TPoint< T > GetRightBottom() const
Definition: rect.h:361
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:210
impeller
Definition: aiks_blend_unittests.cc:18
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:752
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:605
vertex_buffer_builder.h