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