Flutter Impeller
render_target_cache.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 
8 
9 namespace impeller {
10 
11 RenderTargetCache::RenderTargetCache(std::shared_ptr<Allocator> allocator,
12  uint32_t keep_alive_frame_count)
13  : RenderTargetAllocator(std::move(allocator)),
14  keep_alive_frame_count_(keep_alive_frame_count) {}
15 
17  cache_disabled_count_ = 0;
18  for (auto& td : render_target_data_) {
19  td.used_this_frame = false;
20  }
21 }
22 
24  cache_disabled_count_ = 0;
25  std::vector<RenderTargetData> retain;
26 
27  for (RenderTargetData& td : render_target_data_) {
28  if (td.used_this_frame) {
29  retain.push_back(td);
30  } else if (td.keep_alive_frame_count > 0) {
31  td.keep_alive_frame_count--;
32  retain.push_back(td);
33  }
34  }
35  render_target_data_.swap(retain);
36 }
37 
39  cache_disabled_count_++;
40 }
41 
42 bool RenderTargetCache::CacheEnabled() const {
43  return cache_disabled_count_ == 0;
44 }
45 
47  FML_DCHECK(cache_disabled_count_ > 0);
48  if (cache_disabled_count_ == 0) {
49  return;
50  }
51  cache_disabled_count_--;
52 }
53 
55  const Context& context,
56  ISize size,
57  int mip_count,
58  std::string_view label,
59  RenderTarget::AttachmentConfig color_attachment_config,
60  std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
61  const std::shared_ptr<Texture>& existing_color_texture,
62  const std::shared_ptr<Texture>& existing_depth_stencil_texture,
63  std::optional<PixelFormat> target_pixel_format) {
64  if (size.IsEmpty()) {
65  return {};
66  }
67 
68  FML_DCHECK(existing_color_texture == nullptr &&
69  existing_depth_stencil_texture == nullptr);
70  auto config = RenderTargetConfig{
71  .size = size,
72  .mip_count = static_cast<size_t>(mip_count),
73  .has_msaa = false,
74  .has_depth_stencil = stencil_attachment_config.has_value(),
75  };
76 
77  if (CacheEnabled()) {
78  for (RenderTargetData& render_target_data : render_target_data_) {
79  const RenderTargetConfig other_config = render_target_data.config;
80  if (!render_target_data.used_this_frame && other_config == config) {
81  render_target_data.used_this_frame = true;
82  render_target_data.keep_alive_frame_count = keep_alive_frame_count_;
83  ColorAttachment color0 =
84  render_target_data.render_target.GetColorAttachment(0);
85  std::optional<DepthAttachment> depth =
86  render_target_data.render_target.GetDepthAttachment();
87  std::shared_ptr<Texture> depth_tex = depth ? depth->texture : nullptr;
89  context, size, mip_count, label, color_attachment_config,
90  stencil_attachment_config, color0.texture, depth_tex,
91  target_pixel_format);
92  }
93  }
94  }
96  context, size, mip_count, label, color_attachment_config,
97  stencil_attachment_config, nullptr, nullptr, target_pixel_format);
98  if (!created_target.IsValid()) {
99  return created_target;
100  }
101  if (CacheEnabled()) {
102  render_target_data_.push_back(RenderTargetData{
103  .used_this_frame = true, //
104  .keep_alive_frame_count = keep_alive_frame_count_, //
105  .config = config, //
106  .render_target = created_target //
107  });
108  }
109  return created_target;
110 }
111 
113  const Context& context,
114  ISize size,
115  int mip_count,
116  std::string_view label,
117  RenderTarget::AttachmentConfigMSAA color_attachment_config,
118  std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
119  const std::shared_ptr<Texture>& existing_color_msaa_texture,
120  const std::shared_ptr<Texture>& existing_color_resolve_texture,
121  const std::shared_ptr<Texture>& existing_depth_stencil_texture,
122  std::optional<PixelFormat> target_pixel_format) {
123  if (size.IsEmpty()) {
124  return {};
125  }
126 
127  FML_DCHECK(existing_color_msaa_texture == nullptr &&
128  existing_color_resolve_texture == nullptr &&
129  existing_depth_stencil_texture == nullptr);
130  auto config = RenderTargetConfig{
131  .size = size,
132  .mip_count = static_cast<size_t>(mip_count),
133  .has_msaa = true,
134  .has_depth_stencil = stencil_attachment_config.has_value(),
135  };
136  if (CacheEnabled()) {
137  for (RenderTargetData& render_target_data : render_target_data_) {
138  const RenderTargetConfig other_config = render_target_data.config;
139  if (!render_target_data.used_this_frame && other_config == config) {
140  render_target_data.used_this_frame = true;
141  render_target_data.keep_alive_frame_count = keep_alive_frame_count_;
142  ColorAttachment color0 =
143  render_target_data.render_target.GetColorAttachment(0);
144  std::optional<DepthAttachment> depth =
145  render_target_data.render_target.GetDepthAttachment();
146  std::shared_ptr<Texture> depth_tex = depth ? depth->texture : nullptr;
148  context, size, mip_count, label, color_attachment_config,
149  stencil_attachment_config, color0.texture, color0.resolve_texture,
150  depth_tex, target_pixel_format);
151  }
152  }
153  }
155  context, size, mip_count, label, color_attachment_config,
156  stencil_attachment_config, nullptr, nullptr, nullptr,
157  target_pixel_format);
158  if (!created_target.IsValid()) {
159  return created_target;
160  }
161  if (CacheEnabled()) {
162  render_target_data_.push_back(RenderTargetData{
163  .used_this_frame = true, //
164  .keep_alive_frame_count = keep_alive_frame_count_, //
165  .config = config, //
166  .render_target = created_target //
167  });
168  }
169  return created_target;
170 }
171 
173  return render_target_data_.size();
174 }
175 
176 } // namespace impeller
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...
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, std::optional< PixelFormat > target_pixel_format=std::nullopt)
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, std::optional< PixelFormat > target_pixel_format=std::nullopt)
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, std::optional< PixelFormat > target_pixel_format=std::nullopt) override
void DisableCache() override
Disable any caching until the next call to EnabledCache.
void EnableCache() override
Re-enable any caching if disabled.
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, std::optional< PixelFormat > target_pixel_format=std::nullopt) override
RenderTargetCache(std::shared_ptr< Allocator > allocator, uint32_t keep_alive_frame_count=4)
void End() override
Mark the end of a frame workload.
void Start() override
Mark the beginning of a frame workload.
Definition: comparable.h:95
std::shared_ptr< Texture > resolve_texture
Definition: formats.h:662
std::shared_ptr< Texture > texture
Definition: formats.h:661
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: size.h:123