| Flutter Impeller |

Classes | Public Member Functions | List of all members
impeller::RenderTargetCache Class Reference

An implementation of the [RenderTargetAllocator] that caches all allocated texture data for one frame. More...

`#include <render_target_cache.h>`

Inheritance diagram for impeller::RenderTargetCache:

## Public Member Functions
| RenderTargetCache (std::shared_ptr< Allocator > allocator, uint32_t keep_alive_frame_count=4)
| ~RenderTargetCache ()=default
void | Start () override
| Mark the beginning of a frame workload. More...
void | End () override
| Mark the end of a frame workload. More...
void | DisableCache () override
| Disable any caching until the next call to `EnabledCache`. More...
void | EnableCache () override
| Re-enable any caching if disabled. More...
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) override
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) override
size_t | CachedTextureCount () const
std::vector< RenderTargetData >::const_iterator | GetRenderTargetDataBegin () const
| Visible for testing. More...
std::vector< RenderTargetData >::const_iterator | GetRenderTargetDataEnd () const
| Visible for testing. More...
Public Member Functions inherited from impeller::RenderTargetAllocator
| RenderTargetAllocator (std::shared_ptr< Allocator > allocator)
virtual | ~RenderTargetAllocator ()=default

## Detailed Description

An implementation of the [RenderTargetAllocator] that caches all allocated texture data for one frame.

Any textures unused after a frame are immediately discarded.

Definition at line 18 of file render_target_cache.h.

## Constructor & Destructor Documentation

## ◆ RenderTargetCache()

impeller::RenderTargetCache::RenderTargetCache | ( | std::shared_ptr< Allocator > | allocator,
|  | uint32_t | keep_alive_frame_count = `4`
| ) |  |
| explicit

Definition at line 11 of file render_target_cache.cc.

13 : RenderTargetAllocator(std::move(allocator)),
14 keep_alive_frame_count_(keep_alive_frame_count) {}
impeller::RenderTargetAllocator::RenderTargetAllocator
RenderTargetAllocator(std::shared_ptr< Allocator > allocator)
Definition: render_target.cc:314

## ◆ ~RenderTargetCache()

impeller::RenderTargetCache::~RenderTargetCache | ( |  | ) |
| default

## Member Function Documentation

## ◆ CachedTextureCount()

size_t impeller::RenderTargetCache::CachedTextureCount | ( |  | ) | const

Definition at line 168 of file render_target_cache.cc.

168 {
169 return render_target_data_.size();
170 }

## ◆ CreateOffscreen()

RenderTarget impeller::RenderTargetCache::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`
| ) |  |
| override virtual

Reimplemented from impeller::RenderTargetAllocator.

Definition at line 54 of file render_target_cache.cc.

62 {
63 if (size.IsEmpty()) {
64 return {};
65 }
66
67 FML_DCHECK(existing_color_texture == nullptr &&
68 existing_depth_stencil_texture == nullptr);
69 auto config = RenderTargetConfig{
70 .size = size,
71 .mip_count = static_cast<size_t>(mip_count),
72 .has_msaa = false,
73 .has_depth_stencil = stencil_attachment_config.has_value(),
74 };
75
76 if (CacheEnabled()) {
77 for (RenderTargetData& render_target_data : render_target_data_) {
78 const RenderTargetConfig other_config = render_target_data.config;
79 if (!render_target_data.used_this_frame && other_config == config) {
80 render_target_data.used_this_frame = true;
81 render_target_data.keep_alive_frame_count = keep_alive_frame_count_;
82 ColorAttachment color0 =
83 render_target_data.render_target.GetColorAttachment(0);
84 std::optional<DepthAttachment> depth =
85 render_target_data.render_target.GetDepthAttachment();
86 std::shared_ptr<Texture> depth_tex = depth ? depth->texture : nullptr;
87 return RenderTargetAllocator::CreateOffscreen(
88 context, size, mip_count, label, color_attachment_config,
89 stencil_attachment_config, color0.texture, depth_tex);
90 }
91 }
92 }
93 RenderTarget created_target = RenderTargetAllocator::CreateOffscreen(
94 context, size, mip_count, label, color_attachment_config,
95 stencil_attachment_config);
96 if (!created_target.IsValid()) {
97 return created_target;
98 }
99 if (CacheEnabled()) {
100 render_target_data_.push_back(RenderTargetData{
101 .used_this_frame = true, //
102 .keep_alive_frame_count = keep_alive_frame_count_, //
103 .config = config, //
104 .render_target = created_target //
105 });
106 }
107 return created_target;
108 }
impeller::RenderTargetAllocator::CreateOffscreen
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)
Definition: render_target.cc:322

References impeller::RenderTargetAllocator::CreateOffscreen(), impeller::TSize< T >::IsEmpty(), impeller::RenderTarget::IsValid(), impeller::RenderTargetConfig::size, and impeller::Attachment::texture.

## ◆ CreateOffscreenMSAA()

RenderTarget impeller::RenderTargetCache::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`
| ) |  |
| override virtual

Reimplemented from impeller::RenderTargetAllocator.

Definition at line 110 of file render_target_cache.cc.

119 {
120 if (size.IsEmpty()) {
121 return {};
122 }
123
124 FML_DCHECK(existing_color_msaa_texture == nullptr &&
125 existing_color_resolve_texture == nullptr &&
126 existing_depth_stencil_texture == nullptr);
127 auto config = RenderTargetConfig{
128 .size = size,
129 .mip_count = static_cast<size_t>(mip_count),
130 .has_msaa = true,
131 .has_depth_stencil = stencil_attachment_config.has_value(),
132 };
133 if (CacheEnabled()) {
134 for (RenderTargetData& render_target_data : render_target_data_) {
135 const RenderTargetConfig other_config = render_target_data.config;
136 if (!render_target_data.used_this_frame && other_config == config) {
137 render_target_data.used_this_frame = true;
138 render_target_data.keep_alive_frame_count = keep_alive_frame_count_;
139 ColorAttachment color0 =
140 render_target_data.render_target.GetColorAttachment(0);
141 std::optional<DepthAttachment> depth =
142 render_target_data.render_target.GetDepthAttachment();
143 std::shared_ptr<Texture> depth_tex = depth ? depth->texture : nullptr;
144 return RenderTargetAllocator::CreateOffscreenMSAA(
145 context, size, mip_count, label, color_attachment_config,
146 stencil_attachment_config, color0.texture, color0.resolve_texture,
147 depth_tex);
148 }
149 }
150 }
151 RenderTarget created_target = RenderTargetAllocator::CreateOffscreenMSAA(
152 context, size, mip_count, label, color_attachment_config,
153 stencil_attachment_config);
154 if (!created_target.IsValid()) {
155 return created_target;
156 }
157 if (CacheEnabled()) {
158 render_target_data_.push_back(RenderTargetData{
159 .used_this_frame = true, //
160 .keep_alive_frame_count = keep_alive_frame_count_, //
161 .config = config, //
162 .render_target = created_target //
163 });
164 }
165 return created_target;
166 }
impeller::RenderTargetAllocator::CreateOffscreenMSAA
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)
Definition: render_target.cc:376

References impeller::RenderTargetAllocator::CreateOffscreenMSAA(), impeller::TSize< T >::IsEmpty(), impeller::RenderTarget::IsValid(), impeller::Attachment::resolve_texture, impeller::RenderTargetConfig::size, and impeller::Attachment::texture.

## ◆ DisableCache()

void impeller::RenderTargetCache::DisableCache | ( |  | ) |
| override virtual

Disable any caching until the next call to `EnabledCache`.

Reimplemented from impeller::RenderTargetAllocator.

Definition at line 38 of file render_target_cache.cc.

38 {
39 cache_disabled_count_++;
40 }

## ◆ EnableCache()

void impeller::RenderTargetCache::EnableCache | ( |  | ) |
| override virtual

Re-enable any caching if disabled.

Reimplemented from impeller::RenderTargetAllocator.

Definition at line 46 of file render_target_cache.cc.

46 {
47 FML_DCHECK(cache_disabled_count_ > 0);
48 if (cache_disabled_count_ == 0) {
49 return;
50 }
51 cache_disabled_count_--;
52 }

## ◆ End()

void impeller::RenderTargetCache::End | ( |  | ) |
| override virtual

Mark the end of a frame workload.

       This may be used to deallocate any unused textures.

Reimplemented from impeller::RenderTargetAllocator.

Definition at line 23 of file render_target_cache.cc.

23 {
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 }

## ◆ GetRenderTargetDataBegin()

std::vector<RenderTargetData>::const_iterator impeller::RenderTargetCache::GetRenderTargetDataBegin | ( |  | ) | const
| inline

Visible for testing.

Definition at line 87 of file render_target_cache.h.

88 {
89 return render_target_data_.begin();
90 }

## ◆ GetRenderTargetDataEnd()

std::vector<RenderTargetData>::const_iterator impeller::RenderTargetCache::GetRenderTargetDataEnd | ( |  | ) | const
| inline

Visible for testing.

Definition at line 93 of file render_target_cache.h.

93 {
94 return render_target_data_.end();
95 }

## ◆ Start()

void impeller::RenderTargetCache::Start | ( |  | ) |
| override virtual

Mark the beginning of a frame workload.

      This may be used to reset any tracking state on whether or not a
      particular texture instance is still in use.

Reimplemented from impeller::RenderTargetAllocator.

Definition at line 16 of file render_target_cache.cc.

16 {
17 cache_disabled_count_ = 0;
18 for (auto& td : render_target_data_) {
19 td.used_this_frame = false;
20 }
21 }

The documentation for this class was generated from the following files:

- impeller/entity/render_target_cache.h
- impeller/entity/render_target_cache.cc