Flutter Impeller
render_target.h
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 #ifndef FLUTTER_IMPELLER_RENDERER_RENDER_TARGET_H_
6 #define FLUTTER_IMPELLER_RENDERER_RENDER_TARGET_H_
7 
8 #include <functional>
9 #include <map>
10 #include <optional>
11 
12 #include "flutter/fml/hash_combine.h"
14 #include "impeller/core/formats.h"
15 #include "impeller/geometry/size.h"
16 
17 namespace impeller {
18 
19 class Context;
20 
22  ISize size = ISize{0, 0};
23  size_t mip_count = 0;
24  bool has_msaa = false;
25  bool has_depth_stencil = false;
26 
27  constexpr bool operator==(const RenderTargetConfig& o) const {
28  return size == o.size && mip_count == o.mip_count &&
30  }
31 
32  constexpr size_t Hash() const {
33  return fml::HashCombine(size.width, size.height, mip_count, has_msaa,
35  }
36 };
37 
38 class RenderTarget final {
39  public:
45  };
46 
53  };
54 
57  .load_action = LoadAction::kClear,
58  .store_action = StoreAction::kStore,
59  .clear_color = Color::BlackTransparent()};
60 
63  .resolve_storage_mode = StorageMode::kDevicePrivate,
64  .load_action = LoadAction::kClear,
65  .store_action = StoreAction::kMultisampleResolve,
66  .clear_color = Color::BlackTransparent()};
67 
70  .load_action = LoadAction::kClear,
71  .store_action = StoreAction::kDontCare,
72  .clear_color = Color::BlackTransparent()};
73 
75 
77 
78  bool IsValid() const;
79 
81  const Context& context,
82  Allocator& allocator,
83  ISize size,
84  bool msaa,
85  std::string_view label = "Offscreen",
86  RenderTarget::AttachmentConfig stencil_attachment_config =
88  const std::shared_ptr<Texture>& depth_stencil_texture = nullptr);
89 
91 
92  bool HasColorAttachment(size_t index) const;
93 
94  ISize GetRenderTargetSize() const;
95 
96  std::shared_ptr<Texture> GetRenderTargetTexture() const;
97 
99 
100  std::optional<ISize> GetColorAttachmentSize(size_t index) const;
101 
103  size_t index);
104 
105  /// @brief Get the color attachment at [index].
106  ///
107  /// This function does not validate whether or not the attachment was
108  /// previously defined and will return a default constructed attachment
109  /// if none is set.
110  ColorAttachment GetColorAttachment(size_t index) const;
111 
112  RenderTarget& SetDepthAttachment(std::optional<DepthAttachment> attachment);
113 
115  std::optional<StencilAttachment> attachment);
116 
117  size_t GetMaxColorAttacmentBindIndex() const;
118 
119  const std::optional<DepthAttachment>& GetDepthAttachment() const;
120 
121  const std::optional<StencilAttachment>& GetStencilAttachment() const;
122 
123  size_t GetTotalAttachmentCount() const;
124 
126  const std::function<bool(size_t index,
127  const ColorAttachment& attachment)>& iterator)
128  const;
129 
131  const std::function<bool(const Attachment& attachment)>& iterator) const;
132 
133  std::string ToString() const;
134 
136 
137  private:
138  std::optional<ColorAttachment> color0_;
139  std::optional<DepthAttachment> depth_;
140  std::optional<StencilAttachment> stencil_;
141  // Note: color0 is stored in the color0_ field above and not in this map,
142  // to avoid heap allocations for the commonly created render target formats
143  // in Flutter.
144  std::map<size_t, ColorAttachment> colors_;
145 };
146 
147 /// @brief a wrapper around the impeller [Allocator] instance that can be used
148 /// to provide caching of allocated render target textures.
150  public:
151  explicit RenderTargetAllocator(std::shared_ptr<Allocator> allocator);
152 
153  virtual ~RenderTargetAllocator() = default;
154 
156  const Context& context,
157  ISize size,
158  int mip_count,
159  std::string_view label = "Offscreen",
160  RenderTarget::AttachmentConfig color_attachment_config =
162  std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
164  const std::shared_ptr<Texture>& existing_color_texture = nullptr,
165  const std::shared_ptr<Texture>& existing_depth_stencil_texture = nullptr);
166 
168  const Context& context,
169  ISize size,
170  int mip_count,
171  std::string_view label = "Offscreen MSAA",
172  RenderTarget::AttachmentConfigMSAA color_attachment_config =
174  std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
176  const std::shared_ptr<Texture>& existing_color_msaa_texture = nullptr,
177  const std::shared_ptr<Texture>& existing_color_resolve_texture = nullptr,
178  const std::shared_ptr<Texture>& existing_depth_stencil_texture = nullptr);
179 
180  /// @brief Disable any caching until the next call to `EnabledCache`.
181  virtual void DisableCache() {}
182 
183  /// @brief Re-enable any caching if disabled.
184  virtual void EnableCache() {}
185 
186  /// @brief Mark the beginning of a frame workload.
187  ///
188  /// This may be used to reset any tracking state on whether or not a
189  /// particular texture instance is still in use.
190  virtual void Start();
191 
192  /// @brief Mark the end of a frame workload.
193  ///
194  /// This may be used to deallocate any unused textures.
195  virtual void End();
196 
197  private:
198  std::shared_ptr<Allocator> allocator_;
199 };
200 
201 } // namespace impeller
202 
203 #endif // FLUTTER_IMPELLER_RENDERER_RENDER_TARGET_H_
An object that allocates device memory.
Definition: allocator.h:24
To do anything rendering related with Impeller, you need a context.
Definition: context.h:65
a wrapper around the impeller [Allocator] instance that can be used to provide caching of allocated r...
RenderTargetAllocator(std::shared_ptr< Allocator > allocator)
virtual ~RenderTargetAllocator()=default
virtual void EnableCache()
Re-enable any caching if disabled.
virtual RenderTarget CreateOffscreenMSAA(const Context &context, ISize size, int mip_count, std::string_view label="Offscreen MSAA", RenderTarget::AttachmentConfigMSAA color_attachment_config=RenderTarget::kDefaultColorAttachmentConfigMSAA, std::optional< RenderTarget::AttachmentConfig > stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &existing_color_msaa_texture=nullptr, const std::shared_ptr< Texture > &existing_color_resolve_texture=nullptr, const std::shared_ptr< Texture > &existing_depth_stencil_texture=nullptr)
virtual RenderTarget CreateOffscreen(const Context &context, ISize size, int mip_count, std::string_view label="Offscreen", RenderTarget::AttachmentConfig color_attachment_config=RenderTarget::kDefaultColorAttachmentConfig, std::optional< RenderTarget::AttachmentConfig > stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &existing_color_texture=nullptr, const std::shared_ptr< Texture > &existing_depth_stencil_texture=nullptr)
virtual void Start()
Mark the beginning of a frame workload.
virtual void DisableCache()
Disable any caching until the next call to EnabledCache.
virtual void End()
Mark the end of a frame workload.
ColorAttachment GetColorAttachment(size_t index) const
Get the color attachment at [index].
std::shared_ptr< Texture > GetRenderTargetTexture() const
static constexpr AttachmentConfig kDefaultColorAttachmentConfig
Definition: render_target.h:55
SampleCount GetSampleCount() const
static constexpr AttachmentConfigMSAA kDefaultColorAttachmentConfigMSAA
Definition: render_target.h:61
void IterateAllAttachments(const std::function< bool(const Attachment &attachment)> &iterator) const
RenderTarget & SetColorAttachment(const ColorAttachment &attachment, size_t index)
bool HasColorAttachment(size_t index) const
size_t GetMaxColorAttacmentBindIndex() const
static constexpr AttachmentConfig kDefaultStencilAttachmentConfig
Definition: render_target.h:68
std::string ToString() const
RenderTarget & SetDepthAttachment(std::optional< DepthAttachment > attachment)
PixelFormat GetRenderTargetPixelFormat() const
size_t GetTotalAttachmentCount() const
ISize GetRenderTargetSize() const
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
bool IterateAllColorAttachments(const std::function< bool(size_t index, const ColorAttachment &attachment)> &iterator) const
std::optional< ISize > GetColorAttachmentSize(size_t index) const
const std::optional< DepthAttachment > & GetDepthAttachment() const
const std::optional< StencilAttachment > & GetStencilAttachment() const
void SetupDepthStencilAttachments(const Context &context, Allocator &allocator, ISize size, bool msaa, std::string_view label="Offscreen", RenderTarget::AttachmentConfig stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &depth_stencil_texture=nullptr)
RenderTargetConfig ToConfig() const
StorageMode
Specified where the allocation resides and how it is used.
Definition: formats.h:32
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
LoadAction
Definition: formats.h:202
StoreAction
Definition: formats.h:208
SampleCount
Definition: formats.h:295
static constexpr Color BlackTransparent()
Definition: color.h:270
constexpr size_t Hash() const
Definition: render_target.h:32
constexpr bool operator==(const RenderTargetConfig &o) const
Definition: render_target.h:27
Type height
Definition: size.h:29
Type width
Definition: size.h:28