Flutter Engine
The Flutter Engine
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(
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 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
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
190void TextureContents::SetSourceRect(const Rect& source_rect) {
191 source_rect_ = source_rect;
192}
193
195 return source_rect_;
196}
197
199 strict_source_rect_enabled_ = strict;
200}
201
203 return strict_source_rect_enabled_;
204}
205
207 sampler_descriptor_ = std::move(desc);
208}
209
211 return sampler_descriptor_;
212}
213
214void TextureContents::SetDeferApplyingOpacity(bool defer_applying_opacity) {
215 defer_applying_opacity_ = defer_applying_opacity;
216}
217
218} // namespace impeller
bool ok() const
Definition: status.h:71
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
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:50
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.
Definition: render_pass.cc:123
virtual void SetPipeline(const std::shared_ptr< Pipeline< PipelineDescriptor > > &pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:92
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:127
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
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
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
Point Vector2
Definition: point.h:326
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:34
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:463
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
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:287
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:349
constexpr TPoint< T > GetRightBottom() const
Definition: rect.h:361
constexpr TPoint< T > GetLeftBottom() const
Definition: rect.h:357
static constexpr TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
constexpr TPoint< T > GetRightTop() const
Definition: rect.h:353
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