| 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
5 #include "impeller/entity/render_target_cache.h"
6 #include "impeller/renderer/render_target.h"
7
8 namespace impeller {
9
10 RenderTargetCache::RenderTargetCache(std::shared_ptr<Allocator> allocator)
11 : RenderTargetAllocator(std::move(allocator)) {}
12
13 void RenderTargetCache::Start() {
14 for (auto& td : render_target_data_) {
15 td.used_this_frame = false;
16 }
17 }
18
19 void RenderTargetCache::End() {
20 std::vector<RenderTargetData> retain;
21
22 for (const auto& td : render_target_data_) {
23 if (td.used_this_frame) {
24 retain.push_back(td);
25 }
26 }
27 render_target_data_.swap(retain);
28 }
29
30 RenderTarget RenderTargetCache::CreateOffscreen(
31 const Context& context,
32 ISize size,
33 int mip_count,
34 const std::string& label,
35 RenderTarget::AttachmentConfig color_attachment_config,
36 std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
37 const std::shared_ptr<Texture>& existing_color_texture,
38 const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
39 if (size.IsEmpty()) {
40 return {};
41 }
42
43 FML_DCHECK(existing_color_texture == nullptr &&
44 existing_depth_stencil_texture == nullptr);
45 auto config = RenderTargetConfig{
46 .size = size,
47 .mip_count = static_cast<size_t>(mip_count),
48 .has_msaa = false,
49 .has_depth_stencil = stencil_attachment_config.has_value(),
50 };
51 for (auto& render_target_data : render_target_data_) {
52 const auto other_config = render_target_data.config;
53 if (!render_target_data.used_this_frame && other_config == config) {
54 render_target_data.used_this_frame = true;
55 auto color0 = render_target_data.render_target.GetColorAttachments()
56 .find(0u)
57 ->second;
58 auto depth = render_target_data.render_target.GetDepthAttachment();
59 std::shared_ptr<Texture> depth_tex = depth ? depth->texture : nullptr;
60 return RenderTargetAllocator::CreateOffscreen(
61 context, size, mip_count, label, color_attachment_config,
62 stencil_attachment_config, color0.texture, depth_tex);
63 }
64 }
65 RenderTarget created_target = RenderTargetAllocator::CreateOffscreen(
66 context, size, mip_count, label, color_attachment_config,
67 stencil_attachment_config);
68 if (!created_target.IsValid()) {
69 return created_target;
70 }
71 render_target_data_.push_back(
72 RenderTargetData{.used_this_frame = true,
73 .config = config,
74 .render_target = created_target});
75 return created_target;
76 }
77
78 RenderTarget RenderTargetCache::CreateOffscreenMSAA(
79 const Context& context,
80 ISize size,
81 int mip_count,
82 const std::string& label,
83 RenderTarget::AttachmentConfigMSAA color_attachment_config,
84 std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
85 const std::shared_ptr<Texture>& existing_color_msaa_texture,
86 const std::shared_ptr<Texture>& existing_color_resolve_texture,
87 const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
88 if (size.IsEmpty()) {
89 return {};
90 }
91
92 FML_DCHECK(existing_color_msaa_texture == nullptr &&
93 existing_color_resolve_texture == nullptr &&
94 existing_depth_stencil_texture == nullptr);
95 auto config = RenderTargetConfig{
96 .size = size,
97 .mip_count = static_cast<size_t>(mip_count),
98 .has_msaa = true,
99 .has_depth_stencil = stencil_attachment_config.has_value(),
100 };
101 for (auto& render_target_data : render_target_data_) {
102 const auto other_config = render_target_data.config;
103 if (!render_target_data.used_this_frame && other_config == config) {
104 render_target_data.used_this_frame = true;
105 auto color0 = render_target_data.render_target.GetColorAttachments()
106 .find(0u)
107 ->second;
108 auto depth = render_target_data.render_target.GetDepthAttachment();
109 std::shared_ptr<Texture> depth_tex = depth ? depth->texture : nullptr;
110 return RenderTargetAllocator::CreateOffscreenMSAA(
111 context, size, mip_count, label, color_attachment_config,
112 stencil_attachment_config, color0.texture, color0.resolve_texture,
113 depth_tex);
114 }
115 }
116 RenderTarget created_target = RenderTargetAllocator::CreateOffscreenMSAA(
117 context, size, mip_count, label, color_attachment_config,
118 stencil_attachment_config);
119 if (!created_target.IsValid()) {
120 return created_target;
121 }
122 render_target_data_.push_back(
123 RenderTargetData{.used_this_frame = true,
124 .config = config,
125 .render_target = created_target});
126 return created_target;
127 }
128
129 size_t RenderTargetCache::CachedTextureCount() const {
130 return render_target_data_.size();
131 }
132
133 } // namespace impeller
impeller::RenderTargetAllocator::CreateOffscreen
virtual RenderTarget CreateOffscreen(const Context &context, ISize size, int mip_count, const std::string &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:259
impeller::RenderTargetCache::CreateOffscreenMSAA
RenderTarget CreateOffscreenMSAA(const Context &context, ISize size, int mip_count, const std::string &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
Definition: render_target_cache.cc:78
impeller::RenderTargetCache::End
void End() override
Mark the end of a frame workload.
Definition: render_target_cache.cc:19
impeller::RenderTarget::AttachmentConfigMSAA
Definition: render_target.h:47
impeller::RenderTarget::AttachmentConfig
Definition: render_target.h:40
impeller::TSize
Definition: size.h:19
impeller::RenderTargetCache::RenderTargetCache
RenderTargetCache(std::shared_ptr< Allocator > allocator)
Definition: render_target_cache.cc:10
impeller::RenderTargetConfig::size
ISize size
Definition: render_target.h:22
impeller::RenderTargetCache::CreateOffscreen
RenderTarget CreateOffscreen(const Context &context, ISize size, int mip_count, const std::string &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
Definition: render_target_cache.cc:30
impeller::RenderTarget
Definition: render_target.h:38
impeller::RenderTarget::IsValid
bool IsValid() const
Definition: render_target.cc:23
impeller::RenderTargetAllocator::CreateOffscreenMSAA
virtual RenderTarget CreateOffscreenMSAA(const Context &context, ISize size, int mip_count, const std::string &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:313
impeller::RenderTargetAllocator
a wrapper around the impeller [Allocator] instance that can be used to provide caching of allocated r...
Definition: render_target.h:142
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:46
std
Definition: comparable.h:95
impeller::RenderTargetCache::Start
void Start() override
Mark the beginning of a frame workload.
Definition: render_target_cache.cc:13
impeller::RenderTargetConfig
Definition: render_target.h:21
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: size.h:105
render_target.h
render_target_cache.h
impeller
Definition: allocation.cc:12
impeller::RenderTargetCache::CachedTextureCount
size_t CachedTextureCount() const
Definition: render_target_cache.cc:129