Flutter Impeller
atlas_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 <optional>
6 
12 #include "impeller/entity/entity.h"
13 #include "impeller/entity/texture_fill.frag.h"
14 #include "impeller/entity/texture_fill.vert.h"
17 
18 namespace impeller {
19 
21  std::shared_ptr<Texture> texture,
22  const Rect& source,
23  const Rect& destination,
24  const Color& color,
25  BlendMode blend_mode,
26  const SamplerDescriptor& desc,
27  bool use_strict_src_rect)
28  : texture_(std::move(texture)),
29  source_(source),
30  destination_(destination),
31  color_(color),
32  blend_mode_(blend_mode),
33  desc_(desc),
34  use_strict_src_rect_(use_strict_src_rect) {}
35 
37 
39  return true;
40 }
41 
43  return false;
44 }
45 
47  HostBuffer& host_buffer) const {
48  using VS = TextureFillVertexShader;
49  constexpr size_t indices[6] = {0, 1, 2, 1, 2, 3};
50 
51  BufferView buffer_view = host_buffer.Emplace(
52  sizeof(VS::PerVertexData) * 6, alignof(VS::PerVertexData),
53  [&](uint8_t* raw_data) {
54  VS::PerVertexData* data =
55  reinterpret_cast<VS::PerVertexData*>(raw_data);
56  int offset = 0;
57  std::array<TPoint<float>, 4> destination_points =
58  destination_.GetPoints();
59  std::array<TPoint<float>, 4> texture_coords =
60  Rect::MakeSize(texture_->GetSize()).Project(source_).GetPoints();
61  for (size_t j = 0; j < 6; j++) {
62  data[offset].position = destination_points[indices[j]];
63  data[offset].texture_coords = texture_coords[indices[j]];
64  offset++;
65  }
66  });
67 
68  return VertexBuffer{
70  .index_buffer = {},
71  .vertex_count = 6,
72  .index_type = IndexType::kNone,
73  };
74 }
75 
77  HostBuffer& host_buffer) const {
78  using VS = PorterDuffBlendVertexShader;
79  constexpr size_t indices[6] = {0, 1, 2, 1, 2, 3};
80 
81  BufferView buffer_view = host_buffer.Emplace(
82  sizeof(VS::PerVertexData) * 6, alignof(VS::PerVertexData),
83  [&](uint8_t* raw_data) {
84  VS::PerVertexData* data =
85  reinterpret_cast<VS::PerVertexData*>(raw_data);
86  int offset = 0;
87  std::array<TPoint<float>, 4> texture_coords =
88  Rect::MakeSize(texture_->GetSize()).Project(source_).GetPoints();
89  std::array<TPoint<float>, 4> destination_points =
90  destination_.GetPoints();
91  for (size_t j = 0; j < 6; j++) {
92  data[offset].vertices = destination_points[indices[j]];
93  data[offset].texture_coords = texture_coords[indices[j]];
94  data[offset].color = color_.Premultiply();
95  offset++;
96  }
97  });
98 
99  return VertexBuffer{
101  .index_buffer = {},
102  .vertex_count = 6,
103  .index_type = IndexType::kNone,
104  };
105 }
106 
108  return destination_;
109 }
110 
111 const std::shared_ptr<Texture>& DrawImageRectAtlasGeometry::GetAtlas() const {
112  return texture_;
113 }
114 
116  const {
117  return desc_;
118 }
119 
121  return blend_mode_;
122 }
123 
125  return false;
126 }
127 
128 std::optional<Rect> DrawImageRectAtlasGeometry::GetStrictSrcRect() const {
129  if (use_strict_src_rect_) {
130  return source_;
131  }
132  return std::nullopt;
133 }
134 
135 ////
136 
137 AtlasContents::AtlasContents() = default;
138 
140 
141 std::optional<Rect> AtlasContents::GetCoverage(const Entity& entity) const {
142  if (!geometry_) {
143  return std::nullopt;
144  }
145  return geometry_->ComputeBoundingBox().TransformBounds(entity.GetTransform());
146 }
147 
149  geometry_ = geometry;
150 }
151 
153  alpha_ = alpha;
154 }
155 
157  const Entity& entity,
158  RenderPass& pass) const {
159  if (geometry_->ShouldSkip() || alpha_ <= 0.0) {
160  return true;
161  }
162 
163  const SamplerDescriptor& dst_sampler_descriptor =
164  geometry_->GetSamplerDescriptor();
165  raw_ptr<const Sampler> dst_sampler =
166  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
167  dst_sampler_descriptor);
168 
169  auto& host_buffer = renderer.GetTransientsBuffer();
170  if (!geometry_->ShouldUseBlend()) {
171  using VS = TextureFillVertexShader;
172  using FS = TextureFillFragmentShader;
173 
174  raw_ptr<const Sampler> dst_sampler =
175  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
176  dst_sampler_descriptor);
177 
178  auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
179  pipeline_options.primitive_type = PrimitiveType::kTriangle;
180  pipeline_options.depth_write_enabled =
181  pipeline_options.blend_mode == BlendMode::kSrc;
182 
183  pass.SetPipeline(renderer.GetTexturePipeline(pipeline_options));
184  pass.SetVertexBuffer(geometry_->CreateSimpleVertexBuffer(host_buffer));
185 #ifdef IMPELLER_DEBUG
186  pass.SetCommandLabel("DrawAtlas");
187 #endif // IMPELLER_DEBUG
188 
189  VS::FrameInfo frame_info;
190  frame_info.mvp = entity.GetShaderTransform(pass);
191  frame_info.texture_sampler_y_coord_scale =
192  geometry_->GetAtlas()->GetYCoordScale();
193 
194  VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
195 
196  FS::FragInfo frag_info;
197  frag_info.alpha = alpha_;
198  FS::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
199  FS::BindTextureSampler(pass, geometry_->GetAtlas(), dst_sampler);
200  return pass.Draw().ok();
201  }
202 
203  BlendMode blend_mode = geometry_->GetBlendMode();
204 
205  if (blend_mode <= BlendMode::kModulate) {
208 
209 #ifdef IMPELLER_DEBUG
210  pass.SetCommandLabel("DrawAtlas Blend");
211 #endif // IMPELLER_DEBUG
212  pass.SetVertexBuffer(geometry_->CreateBlendVertexBuffer(host_buffer));
213  BlendMode inverted_blend_mode =
214  geometry_->ShouldInvertBlendMode()
215  ? (InvertPorterDuffBlend(blend_mode).value_or(BlendMode::kSrc))
216  : blend_mode;
217  pass.SetPipeline(renderer.GetPorterDuffPipeline(
218  inverted_blend_mode, OptionsFromPassAndEntity(pass, entity)));
219 
220  FS::FragInfo frag_info;
221  VS::FrameInfo frame_info;
222 
223  FS::BindTextureSamplerDst(pass, geometry_->GetAtlas(), dst_sampler);
224  frame_info.texture_sampler_y_coord_scale =
225  geometry_->GetAtlas()->GetYCoordScale();
226 
227  frag_info.input_alpha_output_alpha_tmx_tmy =
228  Vector4(1.0, alpha_, static_cast<int>(Entity::TileMode::kDecal),
229  static_cast<int>(Entity::TileMode::kDecal));
230  if (auto rect = geometry_->GetStrictSrcRect(); rect.has_value()) {
231  Rect src_rect = rect.value();
232  frag_info.source_rect =
233  Vector4(src_rect.GetX(), src_rect.GetY(), src_rect.GetBottom(),
234  src_rect.GetRight());
235  frag_info.use_strict_source_rect = 1.0;
236  } else {
237  frag_info.use_strict_source_rect = 0.0;
238  }
239 
240  FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
241 
242  frame_info.mvp = entity.GetShaderTransform(pass);
243 
244  auto uniform_view = host_buffer.EmplaceUniform(frame_info);
245  VS::BindFrameInfo(pass, uniform_view);
246 
247  return pass.Draw().ok();
248  }
249 
252 
253 #ifdef IMPELLER_DEBUG
254  pass.SetCommandLabel("DrawAtlas Advanced Blend");
255 #endif // IMPELLER_DEBUG
256  pass.SetVertexBuffer(geometry_->CreateBlendVertexBuffer(host_buffer));
257 
258  renderer.GetDrawVerticesUberPipeline(blend_mode,
259  OptionsFromPassAndEntity(pass, entity));
260  FS::BindTextureSampler(pass, geometry_->GetAtlas(), dst_sampler);
261 
262  VUS::FrameInfo frame_info;
263  FS::FragInfo frag_info;
264 
265  frame_info.texture_sampler_y_coord_scale =
266  geometry_->GetAtlas()->GetYCoordScale();
267  frame_info.mvp = entity.GetShaderTransform(pass);
268 
269  frag_info.alpha = alpha_;
270  frag_info.blend_mode = static_cast<int>(blend_mode);
271 
272  // These values are ignored on platforms that natively support decal.
273  frag_info.tmx = static_cast<int>(Entity::TileMode::kDecal);
274  frag_info.tmy = static_cast<int>(Entity::TileMode::kDecal);
275 
276  FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
277  VUS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
278 
279  return pass.Draw().ok();
280 }
281 
282 ///////////////
283 
285 
287 
289  const Entity& entity) const {
290  if (!geometry_) {
291  return std::nullopt;
292  }
293  return geometry_->ComputeBoundingBox().TransformBounds(entity.GetTransform());
294 }
295 
297  geometry_ = geometry;
298 }
299 
301  alpha_ = alpha;
302 }
303 
305  matrix_ = matrix;
306 }
307 
309  const Entity& entity,
310  RenderPass& pass) const {
311  if (geometry_->ShouldSkip() || alpha_ <= 0.0) {
312  return true;
313  }
314 
315  const SamplerDescriptor& dst_sampler_descriptor =
316  geometry_->GetSamplerDescriptor();
317 
318  raw_ptr<const Sampler> dst_sampler =
319  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
320  dst_sampler_descriptor);
321 
322  auto& host_buffer = renderer.GetTransientsBuffer();
323 
326 
327 #ifdef IMPELLER_DEBUG
328  pass.SetCommandLabel("Atlas ColorFilter");
329 #endif // IMPELLER_DEBUG
330  pass.SetVertexBuffer(geometry_->CreateSimpleVertexBuffer(host_buffer));
331  pass.SetPipeline(
333 
334  FS::FragInfo frag_info;
335  VS::FrameInfo frame_info;
336 
337  FS::BindInputTexture(pass, geometry_->GetAtlas(), dst_sampler);
338  frame_info.texture_sampler_y_coord_scale =
339  geometry_->GetAtlas()->GetYCoordScale();
340 
341  frag_info.input_alpha = 1;
342  frag_info.output_alpha = alpha_;
343  const float* matrix = matrix_.array;
344  frag_info.color_v = Vector4(matrix[4], matrix[9], matrix[14], matrix[19]);
345  frag_info.color_m = Matrix(matrix[0], matrix[5], matrix[10], matrix[15], //
346  matrix[1], matrix[6], matrix[11], matrix[16], //
347  matrix[2], matrix[7], matrix[12], matrix[17], //
348  matrix[3], matrix[8], matrix[13], matrix[18] //
349  );
350 
351  FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
352 
353  frame_info.mvp = entity.GetShaderTransform(pass);
354 
355  auto uniform_view = host_buffer.EmplaceUniform(frame_info);
356  VS::BindFrameInfo(pass, uniform_view);
357 
358  return pass.Draw().ok();
359 }
360 
361 } // namespace impeller
BufferView buffer_view
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
void SetGeometry(AtlasGeometry *geometry)
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 SetAlpha(Scalar alpha)
virtual std::optional< Rect > GetStrictSrcRect() const
The source rect of the draw if a strict source rect should be applied, or nullopt.
virtual VertexBuffer CreateSimpleVertexBuffer(HostBuffer &host_buffer) const =0
virtual Rect ComputeBoundingBox() const =0
virtual bool ShouldInvertBlendMode() const
virtual bool ShouldUseBlend() const =0
virtual const SamplerDescriptor & GetSamplerDescriptor() const =0
virtual bool ShouldSkip() const =0
virtual VertexBuffer CreateBlendVertexBuffer(HostBuffer &host_buffer) const =0
virtual BlendMode GetBlendMode() const =0
virtual const std::shared_ptr< Texture > & GetAtlas() const =0
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 SetGeometry(AtlasGeometry *geometry)
void SetMatrix(ColorMatrix matrix)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
PipelineRef GetPorterDuffPipeline(BlendMode mode, ContentContextOptions opts) const
PipelineRef GetTexturePipeline(ContentContextOptions opts) const
PipelineRef GetColorMatrixColorFilterPipeline(ContentContextOptions opts) const
std::shared_ptr< Context > GetContext() const
PipelineRef GetDrawVerticesUberPipeline(BlendMode blend_mode, ContentContextOptions opts) const
DrawImageRectAtlasGeometry(std::shared_ptr< Texture > texture, const Rect &source, const Rect &destination, const Color &color, BlendMode blend_mode, const SamplerDescriptor &desc, bool use_strict_src_rect=false)
Rect ComputeBoundingBox() const override
const SamplerDescriptor & GetSamplerDescriptor() const override
bool ShouldUseBlend() const override
std::optional< Rect > GetStrictSrcRect() const override
The source rect of the draw if a strict source rect should be applied, or nullopt.
BlendMode GetBlendMode() const override
bool ShouldInvertBlendMode() const override
VertexBuffer CreateBlendVertexBuffer(HostBuffer &host_buffer) const override
VertexBuffer CreateSimpleVertexBuffer(HostBuffer &host_buffer) const override
const std::shared_ptr< Texture > & GetAtlas() const override
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:48
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:92
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.
Definition: render_pass.cc:127
virtual void SetPipeline(PipelineRef pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:86
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:208
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
FragmentShader_ FragmentShader
Definition: pipeline.h:164
A wrapper around a raw ptr that adds additional unopt mode only checks.
Definition: raw_ptr.h:15
std::optional< PipelineDescriptor > desc_
@ kNone
Does not use the index buffer.
float Scalar
Definition: scalar.h:19
LinePipeline::FragmentShader FS
std::optional< BlendMode > InvertPorterDuffBlend(BlendMode blend_mode)
BlendMode
Definition: color.h:58
LinePipeline::VertexShader VS
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:19
Definition: comparable.h:95
constexpr Color Premultiply() const
Definition: color.h:212
Scalar array[20]
Definition: color.h:118
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
constexpr auto GetBottom() const
Definition: rect.h:361
constexpr Type GetY() const
Returns the Y coordinate of the upper left corner, equivalent to |GetOrigin().y|.
Definition: rect.h:341
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:476
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:673
constexpr std::array< TPoint< T >, 4 > GetPoints() const
Get the points that represent the 4 corners of this rectangle in a Z order that is compatible with tr...
Definition: rect.h:418
constexpr Type GetX() const
Returns the X coordinate of the upper left corner, equivalent to |GetOrigin().x|.
Definition: rect.h:337
constexpr auto GetRight() const
Definition: rect.h:359
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68