Flutter Impeller
render_target_cache_unittests.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 <memory>
6 
7 #include "flutter/testing/testing.h"
14 #include "impeller/renderer/testing/mocks.h"
15 
16 namespace impeller {
17 namespace testing {
18 
21 
22 class TestAllocator : public Allocator {
23  public:
24  TestAllocator() = default;
25 
26  ~TestAllocator() = default;
27 
28  ISize GetMaxTextureSizeSupported() const override {
29  return ISize(1024, 1024);
30  };
31 
32  std::shared_ptr<DeviceBuffer> OnCreateBuffer(
33  const DeviceBufferDescriptor& desc) override {
34  if (should_fail) {
35  return nullptr;
36  }
37  return std::make_shared<MockDeviceBuffer>(desc);
38  };
39 
40  virtual std::shared_ptr<Texture> OnCreateTexture(
41  const TextureDescriptor& desc) override {
42  if (should_fail) {
43  return nullptr;
44  }
45  return std::make_shared<MockTexture>(desc);
46  };
47 
48  bool should_fail = false;
49 };
50 
51 TEST_P(RenderTargetCacheTest, CachesUsedTexturesAcrossFrames) {
52  auto render_target_cache =
53  RenderTargetCache(GetContext()->GetResourceAllocator());
54 
55  render_target_cache.Start();
56  // Create two render targets of the same exact size/shape. Both should be
57  // marked as used this frame, so the cached data set will contain two.
58  render_target_cache.CreateOffscreen(*GetContext(), {100, 100}, 1);
59  render_target_cache.CreateOffscreen(*GetContext(), {100, 100}, 1);
60 
61  EXPECT_EQ(render_target_cache.CachedTextureCount(), 2u);
62 
63  render_target_cache.End();
64  render_target_cache.Start();
65 
66  // Next frame, only create one texture. The set will still contain two,
67  // but one will be removed at the end of the frame.
68  render_target_cache.CreateOffscreen(*GetContext(), {100, 100}, 1);
69  EXPECT_EQ(render_target_cache.CachedTextureCount(), 2u);
70 
71  render_target_cache.End();
72  EXPECT_EQ(render_target_cache.CachedTextureCount(), 1u);
73 }
74 
75 TEST_P(RenderTargetCacheTest, DoesNotPersistFailedAllocations) {
77  auto allocator = std::make_shared<TestAllocator>();
78  auto render_target_cache = RenderTargetCache(allocator);
79 
80  render_target_cache.Start();
81  allocator->should_fail = true;
82 
83  auto render_target =
84  render_target_cache.CreateOffscreen(*GetContext(), {100, 100}, 1);
85 
86  EXPECT_FALSE(render_target.IsValid());
87  EXPECT_EQ(render_target_cache.CachedTextureCount(), 0u);
88 }
89 
90 TEST_P(RenderTargetCacheTest, CachedTextureGetsNewAttachmentConfig) {
91  auto render_target_cache =
92  RenderTargetCache(GetContext()->GetResourceAllocator());
93 
94  render_target_cache.Start();
95  RenderTarget::AttachmentConfig color_attachment_config =
97  RenderTarget target1 = render_target_cache.CreateOffscreen(
98  *GetContext(), {100, 100}, 1, "Offscreen1", color_attachment_config);
99  render_target_cache.End();
100 
101  render_target_cache.Start();
102  color_attachment_config.clear_color = Color::Red();
103  RenderTarget target2 = render_target_cache.CreateOffscreen(
104  *GetContext(), {100, 100}, 1, "Offscreen2", color_attachment_config);
105  render_target_cache.End();
106 
107  auto color1 = target1.GetColorAttachments().find(0)->second;
108  auto color2 = target2.GetColorAttachments().find(0)->second;
109  // The second color attachment should reuse the first attachment's texture
110  // but with attributes from the second AttachmentConfig.
111  EXPECT_EQ(color2.texture, color1.texture);
112  EXPECT_EQ(color2.clear_color, Color::Red());
113 }
114 
115 TEST_P(RenderTargetCacheTest, CreateWithEmptySize) {
116  auto render_target_cache =
117  RenderTargetCache(GetContext()->GetResourceAllocator());
118 
119  render_target_cache.Start();
120  RenderTarget empty_target =
121  render_target_cache.CreateOffscreen(*GetContext(), {100, 0}, 1);
122  RenderTarget empty_target_msaa =
123  render_target_cache.CreateOffscreenMSAA(*GetContext(), {0, 0}, 1);
124  render_target_cache.End();
125 
126  {
127  ScopedValidationDisable disable_validation;
128  EXPECT_FALSE(empty_target.IsValid());
129  EXPECT_FALSE(empty_target_msaa.IsValid());
130  }
131 }
132 
133 } // namespace testing
134 } // namespace impeller
impeller::RenderTarget::kDefaultColorAttachmentConfig
static constexpr AttachmentConfig kDefaultColorAttachmentConfig
Definition: render_target.h:55
impeller::Color::Red
static constexpr Color Red()
Definition: color.h:264
impeller::DeviceBufferDescriptor
Definition: device_buffer_descriptor.h:14
impeller::testing::TestAllocator::GetMaxTextureSizeSupported
ISize GetMaxTextureSizeSupported() const override
Definition: render_target_cache_unittests.cc:28
texture_descriptor.h
impeller::RenderTarget::GetColorAttachments
const std::map< size_t, ColorAttachment > & GetColorAttachments() const
Definition: render_target.cc:198
validation.h
impeller::RenderTarget::AttachmentConfig
Definition: render_target.h:40
impeller::testing::INSTANTIATE_PLAYGROUND_SUITE
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
impeller::TSize< int64_t >
impeller::testing::TestAllocator::OnCreateTexture
virtual std::shared_ptr< Texture > OnCreateTexture(const TextureDescriptor &desc) override
Definition: render_target_cache_unittests.cc:40
impeller::Allocator
An object that allocates device memory.
Definition: allocator.h:22
impeller::RenderTarget
Definition: render_target.h:38
impeller::RenderTarget::IsValid
bool IsValid() const
Definition: render_target.cc:23
impeller::RenderTarget::AttachmentConfig::clear_color
Color clear_color
Definition: render_target.h:44
impeller::ISize
TSize< int64_t > ISize
Definition: size.h:138
impeller::testing::TestAllocator::TestAllocator
TestAllocator()=default
allocator.h
impeller::EntityPlayground
Definition: entity_playground.h:18
impeller::testing::TestAllocator::should_fail
bool should_fail
Definition: render_target_cache_unittests.cc:48
impeller::testing::TEST_P
TEST_P(AiksTest, CanRenderMaskBlurHugeSigma)
Definition: aiks_blur_unittests.cc:23
impeller::RenderTargetCache
An implementation of the [RenderTargetAllocator] that caches all allocated texture data for one frame...
Definition: render_target_cache.h:16
impeller::testing::TestAllocator::OnCreateBuffer
std::shared_ptr< DeviceBuffer > OnCreateBuffer(const DeviceBufferDescriptor &desc) override
Definition: render_target_cache_unittests.cc:32
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:37
impeller::testing::TestAllocator::~TestAllocator
~TestAllocator()=default
render_target_cache.h
impeller::ScopedValidationDisable
Definition: validation.h:38
impeller
Definition: aiks_blur_unittests.cc:20
playground_test.h
impeller::testing::TestAllocator
Definition: render_target_cache_unittests.cc:22
entity_playground.h