| 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/entity/tiled_texture_fill_external.frag.h"
18 #include "impeller/geometry/constants.h"
19 #include "impeller/renderer/render_pass.h"
20 #include "impeller/renderer/vertex_buffer_builder.h"
21
22 namespace impeller {
23
24 TextureContents::TextureContents() = default;
25
26 TextureContents::~TextureContents() = default;
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_view label) {
35 label_ = label;
36 }
37
38 void TextureContents::SetDestinationRect(Rect rect) {
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
50 void TextureContents::SetOpacity(Scalar opacity) {
51 opacity_ = opacity;
52 }
53
54 void TextureContents::SetStencilEnabled(bool enabled) {
55 stencil_enabled_ = enabled;
56 }
57
58 void TextureContents::SetInheritedOpacity(Scalar opacity) {
59 inherited_opacity_ = opacity;
60 }
61
62 Scalar TextureContents::GetOpacity() const {
63 return opacity_ * inherited_opacity_;
64 }
65
66 std::optional<Rect> TextureContents::GetCoverage(const Entity& entity) const {
67 if (GetOpacity() == 0) {
68 return std::nullopt;
69 }
70 return destination_rect_.TransformBounds(entity.GetTransform());
71 };
72
73 std::optional<Snapshot> TextureContents::RenderToSnapshot(
74 const ContentContext& renderer,
75 const Entity& entity,
76 const SnapshotOptions& options) const {
77 // Passthrough textures that have simple rectangle paths and complete source
78 // rects.
79 auto bounds = destination_rect_;
80 auto opacity = GetOpacity();
81 if (source_rect_ == Rect::MakeSize(texture_->GetSize()) &&
82 (opacity >= 1 - kEhCloseEnough || defer_applying_opacity_)) {
83 auto scale = Vector2(bounds.GetSize() / Size(texture_->GetSize()));
84 return Snapshot{.texture = texture_,
85 .transform = entity.GetTransform() *
86 Matrix::MakeTranslation(bounds.GetOrigin()) *
87 Matrix::MakeScale(scale),
88 .sampler_descriptor = options.sampler_descriptor.value_or(
89 sampler_descriptor_),
90 .opacity = opacity,
91 .needs_rasterization_for_runtime_effects =
92 snapshots_need_rasterization_for_runtime_effects_};
93 }
94 return Contents::RenderToSnapshot(
95 renderer, entity,
96 {.coverage_limit = std::nullopt,
97 .sampler_descriptor =
98 options.sampler_descriptor.value_or(sampler_descriptor_),
99 .msaa_enabled = true,
100 .mip_count = options.mip_count,
101 .label = options.label,
102 .coverage_expansion = options.coverage_expansion});
103 }
104
105 bool TextureContents::Render(const ContentContext& renderer,
106 const Entity& entity,
107 RenderPass& pass) const {
108 using VS = TextureFillVertexShader;
109 using FS = TextureFillFragmentShader;
110 using FSStrict = TextureFillStrictSrcFragmentShader;
111
112 if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
113 texture_ == nullptr || texture_->GetSize().IsEmpty()) {
114 return true; // Nothing to render.
115 }
116
117 #if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_EMSCRIPTEN)
118 using FSExternal = TiledTextureFillExternalFragmentShader;
119 bool is_external_texture =
120 texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
121 #endif // IMPELLER_ENABLE_OPENGLES
122
123 auto texture_coords =
124 Rect::MakeSize(texture_->GetSize()).Project(source_rect_);
125 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
126
127 std::array<VS::PerVertexData, 4> vertices = {
128 VS::PerVertexData{destination_rect_.GetLeftTop(),
129 texture_coords.GetLeftTop()},
130 VS::PerVertexData{destination_rect_.GetRightTop(),
131 texture_coords.GetRightTop()},
132 VS::PerVertexData{destination_rect_.GetLeftBottom(),
133 texture_coords.GetLeftBottom()},
134 VS::PerVertexData{destination_rect_.GetRightBottom(),
135 texture_coords.GetRightBottom()},
136 };
137 auto vertex_buffer = CreateVertexBuffer(vertices, data_host_buffer);
138
139 VS::FrameInfo frame_info;
140 frame_info.mvp = entity.GetShaderTransform(pass);
141 frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
142
143 #ifdef IMPELLER_DEBUG
144 if (label_.empty()) {
145 pass.SetCommandLabel("Texture Fill");
146 } else {
147 pass.SetCommandLabel("Texture Fill: " + label_);
148 }
149 #endif // IMPELLER_DEBUG
150
151 auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
152 if (!stencil_enabled_) {
153 pipeline_options.stencil_mode = ContentContextOptions::StencilMode::kIgnore;
154 }
155 pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
156
157 pipeline_options.depth_write_enabled =
158 stencil_enabled_ && pipeline_options.blend_mode == BlendMode::kSrc;
159
160 #if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_EMSCRIPTEN)
161 if (is_external_texture) {
162 pass.SetPipeline(
163 renderer.GetTiledTextureExternalPipeline(pipeline_options));
164 } else {
165 pass.SetPipeline(
166 strict_source_rect_enabled_
167 ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
168 : renderer.GetTexturePipeline(pipeline_options));
169 }
170 #else
171 pass.SetPipeline(strict_source_rect_enabled_
172 ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
173 : renderer.GetTexturePipeline(pipeline_options));
174 #endif // IMPELLER_ENABLE_OPENGLES
175
176 pass.SetVertexBuffer(vertex_buffer);
177 VS::BindFrameInfo(pass, data_host_buffer.EmplaceUniform(frame_info));
178
179 if (strict_source_rect_enabled_) {
180 // For a strict source rect, shrink the texture coordinate range by half a
181 // texel to ensure that linear filtering does not sample anything outside
182 // the source rect bounds.
183 auto strict_texture_coords =
184 Rect::MakeSize(texture_->GetSize()).Project(source_rect_.Expand(-0.5));
185
186 FSStrict::FragInfo frag_info;
187 frag_info.source_rect = Vector4(strict_texture_coords.GetLTRB());
188 frag_info.alpha = GetOpacity();
189 FSStrict::BindFragInfo(pass, data_host_buffer.EmplaceUniform((frag_info)));
190 FSStrict::BindTextureSampler(
191 pass, texture_,
192 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
193 sampler_descriptor_));
194 #if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_EMSCRIPTEN)
195 } else if (is_external_texture) {
196 FSExternal::FragInfo frag_info;
197 frag_info.x_tile_mode =
198 static_cast<Scalar>(sampler_descriptor_.width_address_mode);
199 frag_info.y_tile_mode =
200 static_cast<Scalar>(sampler_descriptor_.height_address_mode);
201 frag_info.alpha = GetOpacity();
202 FSExternal::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
203
204 SamplerDescriptor sampler_desc;
205 // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid, so
206 // we emulate all other tile modes here by remapping the texture
207 // coordinates.
208 sampler_desc.width_address_mode = SamplerAddressMode::kClampToEdge;
209 sampler_desc.height_address_mode = SamplerAddressMode::kClampToEdge;
210 sampler_desc.min_filter = sampler_descriptor_.min_filter;
211 sampler_desc.mag_filter = sampler_descriptor_.mag_filter;
212 sampler_desc.mip_filter = MipFilter::kBase;
213
214 FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
215 pass, texture_,
216 renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
217 #endif // IMPELLER_ENABLE_OPENGLES
218 } else {
219 FS::FragInfo frag_info;
220 frag_info.alpha = GetOpacity();
221 FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform((frag_info)));
222 FS::BindTextureSampler(
223 pass, texture_,
224 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
225 sampler_descriptor_));
226 }
227 return pass.Draw().ok();
228 }
229
230 void TextureContents::SetSourceRect(const Rect& source_rect) {
231 source_rect_ = source_rect;
232 }
233
234 const Rect& TextureContents::GetSourceRect() const {
235 return source_rect_;
236 }
237
238 void TextureContents::SetStrictSourceRect(bool strict) {
239 strict_source_rect_enabled_ = strict;
240 }
241
242 bool TextureContents::GetStrictSourceRect() const {
243 return strict_source_rect_enabled_;
244 }
245
246 void TextureContents::SetSamplerDescriptor(const SamplerDescriptor& desc) {
247 sampler_descriptor_ = desc;
248 }
249
250 const SamplerDescriptor& TextureContents::GetSamplerDescriptor() const {
251 return sampler_descriptor_;
252 }
253
254 void TextureContents::SetDeferApplyingOpacity(bool defer_applying_opacity) {
255 defer_applying_opacity_ = defer_applying_opacity;
256 }
257
258 void TextureContents::SetNeedsRasterizationForRuntimeEffects(bool value) {
259 snapshots_need_rasterization_for_runtime_effects_ = value;
260 }
261
262 } // namespace impeller
impeller::ContentContext
Definition: content_context.h:122
impeller::ContentContext::GetTextureStrictSrcPipeline
PipelineRef GetTextureStrictSrcPipeline(ContentContextOptions opts) const
Definition: content_context.cc:1146
impeller::ContentContext::GetTexturePipeline
PipelineRef GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.cc:1141
impeller::ContentContext::GetTransientsDataBuffer
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
Definition: content_context.h:299
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:964
impeller::Contents::RenderToSnapshot
virtual std::optional< Snapshot > RenderToSnapshot(const ContentContext &renderer, const Entity &entity, const SnapshotOptions &options) const
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition: contents.cc:56
impeller::Entity
Definition: entity.h:26
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:50
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::RenderPass::SetVertexBuffer
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: render_pass.cc:127
impeller::RenderPass::SetPipeline
virtual void SetPipeline(PipelineRef pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:86
impeller::RenderPass::Draw
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:208
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::TextureContents::GetTexture
std::shared_ptr< Texture > GetTexture() const
Definition: texture_contents.cc:46
impeller::TextureContents::SetSourceRect
void SetSourceRect(const Rect &source_rect)
Definition: texture_contents.cc:230
impeller::TextureContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: texture_contents.cc:105
impeller::TextureContents::SetSamplerDescriptor
void SetSamplerDescriptor(const SamplerDescriptor &desc)
Definition: texture_contents.cc:246
impeller::TextureContents::GetOpacity
Scalar GetOpacity() const
Definition: texture_contents.cc:62
impeller::TextureContents::SetStrictSourceRect
void SetStrictSourceRect(bool strict)
Definition: texture_contents.cc:238
impeller::TextureContents::SetDeferApplyingOpacity
void SetDeferApplyingOpacity(bool defer_applying_opacity)
Definition: texture_contents.cc:254
impeller::TextureContents::GetSamplerDescriptor
const SamplerDescriptor & GetSamplerDescriptor() const
Definition: texture_contents.cc:250
impeller::TextureContents::SetOpacity
void SetOpacity(Scalar opacity)
Definition: texture_contents.cc:50
impeller::TextureContents::GetStrictSourceRect
bool GetStrictSourceRect() const
Definition: texture_contents.cc:242
impeller::TextureContents::MakeRect
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
Definition: texture_contents.cc:28
impeller::TextureContents::GetSourceRect
const Rect & GetSourceRect() const
Definition: texture_contents.cc:234
impeller::TextureContents::SetInheritedOpacity
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
Definition: texture_contents.cc:58
impeller::TextureContents::TextureContents
TextureContents()
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:66
impeller::TextureContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: texture_contents.cc:42
impeller::TextureContents::~TextureContents
~TextureContents() override
impeller::TextureContents::SetLabel
void SetLabel(std::string_view label)
Definition: texture_contents.cc:34
impeller::TextureContents::RenderToSnapshot
std::optional< Snapshot > RenderToSnapshot(const ContentContext &renderer, const Entity &entity, const SnapshotOptions &options) const override
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition: texture_contents.cc:73
impeller::TextureContents::SetStencilEnabled
void SetStencilEnabled(bool enabled)
Definition: texture_contents.cc:54
impeller::TextureContents::SetDestinationRect
void SetDestinationRect(Rect rect)
Definition: texture_contents.cc:38
impeller::TextureContents::SetNeedsRasterizationForRuntimeEffects
void SetNeedsRasterizationForRuntimeEffects(bool value)
Definition: texture_contents.cc:258
content_context.h
formats.h
value
int32_t value
Definition: dl_golden_unittests.cc:459
entity.h
constants.h
impeller
Definition: allocation.cc:12
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::Vector2
Point Vector2
Definition: point.h:429
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::SamplerAddressMode::kClampToEdge
@ kClampToEdge
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:57
impeller::FS
LinePipeline::FragmentShader FS
Definition: line_contents.cc:14
impeller::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(std::array< VertexType, size > input, HostBuffer &data_host_buffer)
Create an index-less vertex buffer from a fixed size array.
Definition: vertex_buffer_builder.h:24
impeller::MipFilter::kBase
@ kBase
The texture is sampled as if it only had a single mipmap level.
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::BlendMode::kSrc
@ kSrc
impeller::VS
LinePipeline::VertexShader VS
Definition: line_contents.cc:13
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
render_pass.h
impeller::ContentContextOptions::StencilMode::kIgnore
@ kIgnore
Turn the stencil test off. Used when drawing without stencil-then-cover.
impeller::Contents::SnapshotOptions
Definition: contents.h:85
impeller::Contents::SnapshotOptions::label
std::string_view label
Definition: contents.h:90
impeller::Contents::SnapshotOptions::mip_count
int32_t mip_count
Definition: contents.h:89
impeller::Contents::SnapshotOptions::sampler_descriptor
const std::optional< SamplerDescriptor > & sampler_descriptor
Definition: contents.h:87
impeller::Contents::SnapshotOptions::coverage_expansion
int32_t coverage_expansion
Definition: contents.h:91
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:14
impeller::SamplerDescriptor::mip_filter
MipFilter mip_filter
Definition: sampler_descriptor.h:17
impeller::SamplerDescriptor::width_address_mode
SamplerAddressMode width_address_mode
Definition: sampler_descriptor.h:19
impeller::SamplerDescriptor::height_address_mode
SamplerAddressMode height_address_mode
Definition: sampler_descriptor.h:20
impeller::SamplerDescriptor::mag_filter
MinMagFilter mag_filter
Definition: sampler_descriptor.h:16
impeller::SamplerDescriptor::min_filter
MinMagFilter min_filter
Definition: sampler_descriptor.h:15
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:24
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:25
impeller::TRect< Scalar >
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:618
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:472
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:669
impeller::TRect::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:297
impeller::TRect::GetLeftBottom
constexpr TPoint< T > GetLeftBottom() const
Definition: rect.h:367
impeller::TRect::GetRightTop
constexpr TPoint< T > GetRightTop() const
Definition: rect.h:363
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
impeller::TRect::GetRightBottom
constexpr TPoint< T > GetRightBottom() const
Definition: rect.h:371
impeller::TRect::GetLeftTop
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:359
impeller::TSize< Scalar >
impeller::Vector4
Definition: vector.h:232
texture_contents.h
vertex_buffer_builder.h