| Flutter Impeller |

render_target_cache.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_ENTITY_RENDER_TARGET_CACHE_H_
6 #define FLUTTER_IMPELLER_ENTITY_RENDER_TARGET_CACHE_H_
7
8 #include <string_view>
9
10 #include "impeller/renderer/render_target.h"
11
12 namespace impeller {
13
14 /// @brief An implementation of the [RenderTargetAllocator] that caches all
15 /// allocated texture data for one frame.
16 ///
17 /// Any textures unused after a frame are immediately discarded.
18 class RenderTargetCache : public RenderTargetAllocator {
19 public:
20 explicit RenderTargetCache(std::shared_ptr<Allocator> allocator,
21 uint32_t keep_alive_frame_count = 4);
22
23 ~RenderTargetCache() = default;
24
25 // |RenderTargetAllocator|
26 void Start() override;
27
28 // |RenderTargetAllocator|
29 void End() override;
30
31 // |RenderTargetAllocator|
32 void DisableCache() override;
33
34 // |RenderTargetAllocator|
35 void EnableCache() override;
36
37 RenderTarget CreateOffscreen(
38 const Context& context,
39 ISize size,
40 int mip_count,
41 std::string_view label = "Offscreen",
42 RenderTarget::AttachmentConfig color_attachment_config =
43 RenderTarget::kDefaultColorAttachmentConfig,
44 std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
45 RenderTarget::kDefaultStencilAttachmentConfig,
46 const std::shared_ptr<Texture>& existing_color_texture = nullptr,
47 const std::shared_ptr<Texture>& existing_depth_stencil_texture =
48 nullptr) override;
49
50 RenderTarget CreateOffscreenMSAA(
51 const Context& context,
52 ISize size,
53 int mip_count,
54 std::string_view label = "Offscreen MSAA",
55 RenderTarget::AttachmentConfigMSAA color_attachment_config =
56 RenderTarget::kDefaultColorAttachmentConfigMSAA,
57 std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config =
58 RenderTarget::kDefaultStencilAttachmentConfig,
59 const std::shared_ptr<Texture>& existing_color_msaa_texture = nullptr,
60 const std::shared_ptr<Texture>& existing_color_resolve_texture = nullptr,
61 const std::shared_ptr<Texture>& existing_depth_stencil_texture =
62 nullptr) override;
63
64 // visible for testing.
65 size_t CachedTextureCount() const;
66
67 private:
68 struct RenderTargetData {
69 bool used_this_frame;
70 uint32_t keep_alive_frame_count;
71 RenderTargetConfig config;
72 RenderTarget render_target;
73 };
74
75 bool CacheEnabled() const;
76
77 std::vector<RenderTargetData> render_target_data_;
78 uint32_t keep_alive_frame_count_;
79 uint32_t cache_disabled_count_ = 0;
80
81 RenderTargetCache(const RenderTargetCache&) = delete;
82
83 RenderTargetCache& operator=(const RenderTargetCache&) = delete;
84
85 public:
86 /// Visible for testing.
87 std::vector<RenderTargetData>::const_iterator GetRenderTargetDataBegin()
88 const {
89 return render_target_data_.begin();
90 }
91
92 /// Visible for testing.
93 std::vector<RenderTargetData>::const_iterator GetRenderTargetDataEnd() const {
94 return render_target_data_.end();
95 }
96 };
97
98 } // namespace impeller
99
100 #endif // FLUTTER_IMPELLER_ENTITY_RENDER_TARGET_CACHE_H_
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:47
impeller::RenderTargetAllocator
a wrapper around the impeller [Allocator] instance that can be used to provide caching of allocated r...
Definition: render_target.h:149
impeller::RenderTargetCache
An implementation of the [RenderTargetAllocator] that caches all allocated texture data for one frame...
Definition: render_target_cache.h:18
impeller::RenderTargetCache::CreateOffscreen
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
Definition: render_target_cache.cc:54
impeller::RenderTargetCache::CachedTextureCount
size_t CachedTextureCount() const
Definition: render_target_cache.cc:164
impeller::RenderTargetCache::DisableCache
void DisableCache() override
Disable any caching until the next call to EnabledCache.
Definition: render_target_cache.cc:38
impeller::RenderTargetCache::EnableCache
void EnableCache() override
Re-enable any caching if disabled.
Definition: render_target_cache.cc:46
impeller::RenderTargetCache::~RenderTargetCache
~RenderTargetCache()=default
impeller::RenderTargetCache::RenderTargetCache
RenderTargetCache(std::shared_ptr< Allocator > allocator, uint32_t keep_alive_frame_count=4)
Definition: render_target_cache.cc:11
impeller::RenderTargetCache::GetRenderTargetDataBegin
std::vector< RenderTargetData >::const_iterator GetRenderTargetDataBegin() const
Visible for testing.
Definition: render_target_cache.h:87
impeller::RenderTargetCache::GetRenderTargetDataEnd
std::vector< RenderTargetData >::const_iterator GetRenderTargetDataEnd() const
Visible for testing.
Definition: render_target_cache.h:93
impeller::RenderTargetCache::End
void End() override
Mark the end of a frame workload.
Definition: render_target_cache.cc:23
impeller::RenderTargetCache::Start
void Start() override
Mark the beginning of a frame workload.
Definition: render_target_cache.cc:16
impeller::RenderTargetCache::CreateOffscreenMSAA
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
Definition: render_target_cache.cc:108
impeller::RenderTarget
Definition: render_target.h:38
impeller::RenderTarget::kDefaultColorAttachmentConfig
static constexpr AttachmentConfig kDefaultColorAttachmentConfig
Definition: render_target.h:55
impeller::RenderTarget::kDefaultColorAttachmentConfigMSAA
static constexpr AttachmentConfigMSAA kDefaultColorAttachmentConfigMSAA
Definition: render_target.h:61
impeller::RenderTarget::kDefaultStencilAttachmentConfig
static constexpr AttachmentConfig kDefaultStencilAttachmentConfig
Definition: render_target.h:68
impeller
Definition: allocation.cc:12
render_target.h
impeller::RenderTarget::AttachmentConfig
Definition: render_target.h:40
impeller::RenderTarget::AttachmentConfigMSAA
Definition: render_target.h:47
impeller::RenderTargetConfig
Definition: render_target.h:21
impeller::TSize
Definition: size.h:25