| Flutter Impeller |

tiled_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/tiled_texture_contents.h"
6
7 #include "impeller/entity/contents/clip_contents.h"
8 #include "impeller/entity/contents/content_context.h"
9 #include "impeller/entity/geometry/geometry.h"
10 #include "impeller/entity/texture_fill.frag.h"
11 #include "impeller/entity/texture_fill.vert.h"
12 #include "impeller/entity/tiled_texture_fill.frag.h"
13 #include "impeller/geometry/path_builder.h"
14 #include "impeller/renderer/render_pass.h"
15 #include "impeller/renderer/sampler_library.h"
16
17 namespace impeller {
18
19 static std::optional<SamplerAddressMode> TileModeToAddressMode(
20 Entity::TileMode tile_mode,
21 const Capabilities& capabilities) {
22 switch (tile_mode) {
23 case Entity::TileMode::kClamp:
24 return SamplerAddressMode::kClampToEdge;
25 break;
26 case Entity::TileMode::kMirror:
27 return SamplerAddressMode::kMirror;
28 break;
29 case Entity::TileMode::kRepeat:
30 return SamplerAddressMode::kRepeat;
31 break;
32 case Entity::TileMode::kDecal:
33 if (capabilities.SupportsDecalSamplerAddressMode()) {
34 return SamplerAddressMode::kDecal;
35 }
36 return std::nullopt;
37 }
38 }
39
40 TiledTextureContents::TiledTextureContents() = default;
41
42 TiledTextureContents::~TiledTextureContents() = default;
43
44 void TiledTextureContents::SetTexture(std::shared_ptr<Texture> texture) {
45 texture_ = std::move(texture);
46 }
47
48 void TiledTextureContents::SetTileModes(Entity::TileMode x_tile_mode,
49 Entity::TileMode y_tile_mode) {
50 x_tile_mode_ = x_tile_mode;
51 y_tile_mode_ = y_tile_mode;
52 }
53
54 void TiledTextureContents::SetSamplerDescriptor(SamplerDescriptor desc) {
55 sampler_descriptor_ = std::move(desc);
56 }
57
58 void TiledTextureContents::SetColorFilter(ColorFilterProc color_filter) {
59 color_filter_ = std::move(color_filter);
60 }
61
62 std::shared_ptr<Texture> TiledTextureContents::CreateFilterTexture(
63 const ContentContext& renderer) const {
64 if (!color_filter_) {
65 return nullptr;
66 }
67 auto color_filter_contents = color_filter_(FilterInput::Make(texture_));
68 auto snapshot = color_filter_contents->RenderToSnapshot(
69 renderer, // renderer
70 Entity(), // entity
71 std::nullopt, // coverage_limit
72 std::nullopt, // sampler_descriptor
73 true, // msaa_enabled
74 "TiledTextureContents Snapshot"); // label
75 if (snapshot.has_value()) {
76 return snapshot.value().texture;
77 }
78 return nullptr;
79 }
80
81 SamplerDescriptor TiledTextureContents::CreateDescriptor(
82 const Capabilities& capabilities) const {
83 SamplerDescriptor descriptor = sampler_descriptor_;
84 auto width_mode = TileModeToAddressMode(x_tile_mode_, capabilities);
85 auto height_mode = TileModeToAddressMode(y_tile_mode_, capabilities);
86 if (width_mode.has_value()) {
87 descriptor.width_address_mode = width_mode.value();
88 }
89 if (height_mode.has_value()) {
90 descriptor.height_address_mode = height_mode.value();
91 }
92 return descriptor;
93 }
94
95 bool TiledTextureContents::UsesEmulatedTileMode(
96 const Capabilities& capabilities) const {
97 return !TileModeToAddressMode(x_tile_mode_, capabilities).has_value() ||
98 !TileModeToAddressMode(y_tile_mode_, capabilities).has_value();
99 }
100
101 // |Contents|
102 bool TiledTextureContents::IsOpaque() const {
103 if (GetOpacityFactor() < 1 || x_tile_mode_ == Entity::TileMode::kDecal ||
104 y_tile_mode_ == Entity::TileMode::kDecal) {
105 return false;
106 }
107 if (color_filter_) {
108 return false;
109 }
110 return texture_->IsOpaque();
111 }
112
113 bool TiledTextureContents::Render(const ContentContext& renderer,
114 const Entity& entity,
115 RenderPass& pass) const {
116 if (texture_ == nullptr) {
117 return true;
118 }
119
120 using VS = TextureFillVertexShader;
121 using FS = TiledTextureFillFragmentShader;
122
123 const auto texture_size = texture_->GetSize();
124 if (texture_size.IsEmpty()) {
125 return true;
126 }
127
128 auto& host_buffer = pass.GetTransientsBuffer();
129
130 auto geometry_result = GetGeometry()->GetPositionUVBuffer(
131 Rect({0, 0}, Size(texture_size)), GetInverseEffectTransform(), renderer,
132 entity, pass);
133 bool uses_emulated_tile_mode =
134 UsesEmulatedTileMode(renderer.GetDeviceCapabilities());
135
136 VS::FrameInfo frame_info;
137 frame_info.mvp = geometry_result.transform;
138 frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
139 frame_info.alpha = GetOpacityFactor();
140
141 Command cmd;
142 if (uses_emulated_tile_mode) {
143 DEBUG_COMMAND_INFO(cmd, "TiledTextureFill");
144 } else {
145 DEBUG_COMMAND_INFO(cmd, "TextureFill");
146 }
147
148 cmd.stencil_reference = entity.GetStencilDepth();
149
150 auto options = OptionsFromPassAndEntity(pass, entity);
151 if (geometry_result.prevent_overdraw) {
152 options.stencil_compare = CompareFunction::kEqual;
153 options.stencil_operation = StencilOperation::kIncrementClamp;
154 }
155 options.primitive_type = geometry_result.type;
156 cmd.pipeline = uses_emulated_tile_mode
157 ? renderer.GetTiledTexturePipeline(options)
158 : renderer.GetTexturePipeline(options);
159
160 cmd.BindVertices(geometry_result.vertex_buffer);
161 VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
162
163 if (uses_emulated_tile_mode) {
164 FS::FragInfo frag_info;
165 frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
166 frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
167 FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
168 }
169
170 if (color_filter_) {
171 auto filtered_texture = CreateFilterTexture(renderer);
172 if (!filtered_texture) {
173 return false;
174 }
175 FS::BindTextureSampler(
176 cmd, filtered_texture,
177 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
178 CreateDescriptor(renderer.GetDeviceCapabilities())));
179 } else {
180 FS::BindTextureSampler(
181 cmd, texture_,
182 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
183 CreateDescriptor(renderer.GetDeviceCapabilities())));
184 }
185
186 if (!pass.AddCommand(std::move(cmd))) {
187 return false;
188 }
189
190 if (geometry_result.prevent_overdraw) {
191 auto restore = ClipRestoreContents();
192 restore.SetRestoreCoverage(GetCoverage(entity));
193 return restore.Render(renderer, entity, pass);
194 }
195 return true;
196 }
197
198 std::optional<Snapshot> TiledTextureContents::RenderToSnapshot(
199 const ContentContext& renderer,
200 const Entity& entity,
201 std::optional<Rect> coverage_limit,
202 const std::optional<SamplerDescriptor>& sampler_descriptor,
203 bool msaa_enabled,
204 const std::string& label) const {
205 if (GetInverseEffectTransform().IsIdentity()) {
206 return Snapshot{
207 .texture = texture_,
208 .transform = entity.GetTransformation(),
209 .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
210 .opacity = GetOpacityFactor(),
211 };
212 }
213
214 return Contents::RenderToSnapshot(
215 renderer, // renderer
216 entity, // entity
217 std::nullopt, // coverage_limit
218 sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
219 true, // msaa_enabled
220 label); // label
221 }
222
223 } // namespace impeller
impeller::ContentContext::GetTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:427
impeller::TiledTextureContents::IsOpaque
bool IsOpaque() const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: tiled_texture_contents.cc:102
impeller::Entity::TileMode::kClamp
@ kClamp
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:29
impeller::Command
An object used to specify work to the GPU along with references to resources the GPU will used when d...
Definition: command.h:99
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:31
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::TiledTextureContents::TiledTextureContents
TiledTextureContents()
sampler_library.h
impeller::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
impeller::Entity::TileMode::kDecal
@ kDecal
tiled_texture_contents.h
impeller::ColorSourceContents::GetGeometry
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:21
impeller::TiledTextureContents::SetSamplerDescriptor
void SetSamplerDescriptor(SamplerDescriptor desc)
Definition: tiled_texture_contents.cc:54
impeller::ContentContext::GetTiledTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTiledTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:446
impeller::SamplerAddressMode::kClampToEdge
@ kClampToEdge
impeller::Size
TSize< Scalar > Size
Definition: size.h:135
impeller::Entity::TileMode::kRepeat
@ kRepeat
impeller::Entity::GetTransformation
const Matrix & GetTransformation() const
Definition: entity.cc:49
impeller::Entity::TileMode::kMirror
@ kMirror
path_builder.h
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:18
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:30
impeller::Capabilities
Definition: capabilities.h:14
render_pass.h
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:440
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:37
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:24
geometry.h
impeller::TiledTextureContents::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, const std::string &label="Tiled Texture Snapshot") const override
Render this contents to a snapshot, respecting the entity's transform, path, stencil depth,...
Definition: tiled_texture_contents.cc:198
clip_contents.h
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:306
impeller::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
impeller::ClipRestoreContents
Definition: clip_contents.h:57
impeller::Entity::TileMode
TileMode
Definition: entity.h:40
impeller::TiledTextureContents::SetColorFilter
void SetColorFilter(ColorFilterProc color_filter)
Set a color filter to apply directly to this tiled texture.
Definition: tiled_texture_contents.cc:58
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:27
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:152
content_context.h
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:444
impeller::ColorSourceContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the screen space bounding rectangle that this contents affects.
Definition: color_source_contents.cc:45
impeller::SamplerAddressMode::kMirror
@ kMirror
impeller::TiledTextureContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: tiled_texture_contents.cc:113
impeller::TiledTextureContents::SetTileModes
void SetTileModes(Entity::TileMode x_tile_mode, Entity::TileMode y_tile_mode)
Definition: tiled_texture_contents.cc:48
impeller::TiledTextureContents::~TiledTextureContents
~TiledTextureContents() override
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::TileModeToAddressMode
static std::optional< SamplerAddressMode > TileModeToAddressMode(Entity::TileMode tile_mode, const Capabilities &capabilities)
Definition: tiled_texture_contents.cc:19
impeller::Command::BindVertices
bool BindVertices(const VertexBuffer &buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:25
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:103
impeller::Capabilities::SupportsDecalSamplerAddressMode
virtual bool SupportsDecalSamplerAddressMode() const =0
Whether the context backend supports SamplerAddressMode::Decal.
impeller::RenderPass::AddCommand
bool AddCommand(Command &&command)
Record a command for subsequent encoding to the underlying command buffer. No work is encoded into th...
Definition: render_pass.cc:46
impeller
Definition: aiks_context.cc:10
impeller::ContentContext
Definition: content_context.h:344
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:34
impeller::ColorSourceContents::GetInverseEffectTransform
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
Definition: color_source_contents.cc:37
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, const std::string &label="Snapshot") const
Render this contents to a snapshot, respecting the entity's transform, path, stencil depth,...
Definition: contents.cc:59
impeller::TiledTextureContents::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Definition: tiled_texture_contents.cc:44
impeller::SamplerAddressMode::kRepeat
@ kRepeat
impeller::SamplerAddressMode::kDecal
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...