Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
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"
20
21namespace impeller {
22
24
26
27std::shared_ptr<TextureContents> TextureContents::MakeRect(Rect destination) {
28 auto contents = std::make_shared<TextureContents>();
29 contents->destination_rect_ = destination;
30 return contents;
31}
32
33void TextureContents::SetLabel(std::string label) {
34 label_ = std::move(label);
35}
36
38 destination_rect_ = rect;
39}
40
41void TextureContents::SetTexture(std::shared_ptr<Texture> texture) {
42 texture_ = std::move(texture);
43}
44
45std::shared_ptr<Texture> TextureContents::GetTexture() const {
46 return texture_;
47}
48
50 opacity_ = opacity;
51}
52
54 stencil_enabled_ = enabled;
55}
56
57bool TextureContents::CanInheritOpacity(const Entity& entity) const {
58 return true;
59}
60
62 inherited_opacity_ = opacity;
63}
64
66 return opacity_ * inherited_opacity_;
67}
68
69std::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
76std::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 FSStrict = TextureFillStrictSrcFragmentShader;
117
118 if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
119 texture_ == nullptr || texture_->GetSize().IsEmpty()) {
120 return true; // Nothing to render.
121 }
122
123 [[maybe_unused]] bool is_external_texture =
124 texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
125 FML_DCHECK(!is_external_texture);
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 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 = renderer.GetTransientsBuffer();
142
143 VS::FrameInfo frame_info;
144 frame_info.mvp = entity.GetShaderTransform(pass);
145 frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
146
147#ifdef IMPELLER_DEBUG
148 if (label_.empty()) {
149 pass.SetCommandLabel("Texture Fill");
150 } else {
151 pass.SetCommandLabel("Texture Fill: " + label_);
152 }
153#endif // IMPELLER_DEBUG
154
155 auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
156 if (!stencil_enabled_) {
157 pipeline_options.stencil_mode = ContentContextOptions::StencilMode::kIgnore;
158 }
159 pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
160
161 pass.SetPipeline(strict_source_rect_enabled_
162 ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
163 : renderer.GetTexturePipeline(pipeline_options));
164
165 pass.SetVertexBuffer(vertex_builder.CreateVertexBuffer(host_buffer));
166 VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
167
168 if (strict_source_rect_enabled_) {
169 // For a strict source rect, shrink the texture coordinate range by half a
170 // texel to ensure that linear filtering does not sample anything outside
171 // the source rect bounds.
172 auto strict_texture_coords =
173 Rect::MakeSize(texture_->GetSize()).Project(source_rect.Expand(-0.5));
174
175 FSStrict::FragInfo frag_info;
176 frag_info.source_rect = Vector4(strict_texture_coords.GetLTRB());
177 frag_info.alpha = capture.AddScalar("Alpha", GetOpacity());
178 FSStrict::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
179 FSStrict::BindTextureSampler(
180 pass, texture_,
181 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
182 sampler_descriptor_));
183 } else {
184 FS::FragInfo frag_info;
185 frag_info.alpha = capture.AddScalar("Alpha", GetOpacity());
186 FS::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
187 FS::BindTextureSampler(
188 pass, texture_,
189 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
190 sampler_descriptor_));
191 }
192 return pass.Draw().ok();
193}
194
195void TextureContents::SetSourceRect(const Rect& source_rect) {
196 source_rect_ = source_rect;
197}
198
200 return source_rect_;
201}
202
204 strict_source_rect_enabled_ = strict;
205}
206
208 return strict_source_rect_enabled_;
209}
210
212 sampler_descriptor_ = std::move(desc);
213}
214
216 return sampler_descriptor_;
217}
218
219void TextureContents::SetDeferApplyingOpacity(bool defer_applying_opacity) {
220 defer_applying_opacity_ = defer_applying_opacity;
221}
222
223} // namespace impeller
bool ok() const
Definition status.h:71
Capture CreateChild(std::string_view label)
Definition capture.h:239
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:64
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition entity.cc:50
Capture & GetCapture() const
Definition entity.cc:191
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition entity.cc:46
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition render_pass.h:33
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
virtual void SetPipeline(const std::shared_ptr< Pipeline< PipelineDescriptor > > &pipeline)
The pipeline to use for this command.
virtual fml::Status Draw()
Record the currently pending command.
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
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 SetStrictSourceRect(bool strict)
void SetLabel(std::string label)
void SetDeferApplyingOpacity(bool defer_applying_opacity)
const SamplerDescriptor & GetSamplerDescriptor() const
void SetOpacity(Scalar opacity)
void SetSamplerDescriptor(SamplerDescriptor desc)
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
A common case factory that marks the texture contents as having a destination rectangle....
const Rect & GetSourceRect() const
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
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,...
bool CanInheritOpacity(const Entity &entity) const override
Whether or not this contents can accept the opacity peephole optimization.
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 SetStencilEnabled(bool enabled)
void SetDestinationRect(Rect rect)
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
#define FML_DCHECK(condition)
Definition logging.h:103
FlTexture * texture
Point Vector2
Definition point.h:320
float Scalar
Definition scalar.h:18
constexpr float kEhCloseEnough
Definition constants.h:56
SolidFillVertexShader VS
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition contents.cc:35
const Scalar scale
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition matrix.h:95
static constexpr Matrix MakeScale(const Vector3 &s)
Definition matrix.h:104
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 TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition rect.h:440
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:633
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition rect.h:264
static constexpr TRect MakeSize(const TSize< U > &size)
Definition rect.h:146