| 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"
8 #include "impeller/core/allocator.h"
9 #include "impeller/core/texture_descriptor.h"
10 #include "impeller/entity/render_target_cache.h"
11 #include "impeller/renderer/testing/mocks.h"
12
13 namespace impeller {
14 namespace testing {
15
16 class TestAllocator : public Allocator {
17 public:
18 TestAllocator() = default;
19
20 ~TestAllocator() = default;
21
22 ISize GetMaxTextureSizeSupported() const override {
23 return ISize(1024, 1024);
24 };
25
26 std::shared_ptr<DeviceBuffer> OnCreateBuffer(
27 const DeviceBufferDescriptor& desc) override {
28 if (should_fail) {
29 return nullptr;
30 }
31 return std::make_shared<MockDeviceBuffer>(desc);
32 };
33
34 virtual std::shared_ptr<Texture> OnCreateTexture(
35 const TextureDescriptor& desc) override {
36 if (should_fail) {
37 return nullptr;
38 }
39 return std::make_shared<MockTexture>(desc);
40 };
41
42 bool should_fail = false;
43 };
44
45 TEST(RenderTargetCacheTest, CachesUsedTexturesAcrossFrames) {
46 auto allocator = std::make_shared<TestAllocator>();
47 auto render_target_cache = RenderTargetCache(allocator);
48 auto desc = TextureDescriptor{
49 .format = PixelFormat::kR8G8B8A8UNormInt,
50 .size = ISize(100, 100),
51 .usage = static_cast<TextureUsageMask>(TextureUsage::kRenderTarget)};
52
53 render_target_cache.Start();
54 // Create two textures of the same exact size/shape. Both should be marked
55 // as used this frame, so the cached data set will contain two.
56 render_target_cache.CreateTexture(desc);
57 render_target_cache.CreateTexture(desc);
58
59 ASSERT_EQ(render_target_cache.CachedTextureCount(), 2u);
60
61 render_target_cache.End();
62 render_target_cache.Start();
63
64 // Next frame, only create one texture. The set will still contain two,
65 // but one will be removed at the end of the frame.
66 render_target_cache.CreateTexture(desc);
67 ASSERT_EQ(render_target_cache.CachedTextureCount(), 2u);
68
69 render_target_cache.End();
70 ASSERT_EQ(render_target_cache.CachedTextureCount(), 1u);
71 }
72
73 TEST(RenderTargetCacheTest, DoesNotPersistFailedAllocations) {
74 auto allocator = std::make_shared<TestAllocator>();
75 auto render_target_cache = RenderTargetCache(allocator);
76 auto desc = TextureDescriptor{
77 .format = PixelFormat::kR8G8B8A8UNormInt,
78 .size = ISize(100, 100),
79 .usage = static_cast<TextureUsageMask>(TextureUsage::kRenderTarget)};
80
81 render_target_cache.Start();
82 allocator->should_fail = true;
83
84 ASSERT_EQ(render_target_cache.CreateTexture(desc), nullptr);
85 ASSERT_EQ(render_target_cache.CachedTextureCount(), 0u);
86 }
87
88 } // namespace testing
89 } // namespace impeller
impeller::TextureUsageMask
uint64_t TextureUsageMask
Definition: formats.h:295
impeller::DeviceBufferDescriptor
Definition: device_buffer_descriptor.h:14
impeller::testing::TestAllocator::GetMaxTextureSizeSupported
ISize GetMaxTextureSizeSupported() const override
Definition: render_target_cache_unittests.cc:22
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:40
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
texture_descriptor.h
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
impeller::TSize< int64_t >
impeller::testing::TEST
TEST(CanvasRecorder, Save)
Definition: canvas_recorder_unittests.cc:61
impeller::testing::TestAllocator::OnCreateTexture
virtual std::shared_ptr< Texture > OnCreateTexture(const TextureDescriptor &desc) override
Definition: render_target_cache_unittests.cc:34
impeller::Allocator
An object that allocates device memory.
Definition: allocator.h:22
impeller::ISize
TSize< int64_t > ISize
Definition: size.h:138
impeller::testing::TestAllocator::TestAllocator
TestAllocator()=default
allocator.h
impeller::testing::TestAllocator::should_fail
bool should_fail
Definition: render_target_cache_unittests.cc:42
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:26
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
Definition: aiks_context.cc:10
impeller::testing::TestAllocator
Definition: render_target_cache_unittests.cc:16