Flutter Engine
The Flutter Engine
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"
15
16namespace impeller {
17namespace testing {
18
21
22class TestAllocator : public Allocator {
23 public:
24 TestAllocator() = default;
25
26 ~TestAllocator() = default;
27
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
51TEST_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
75TEST_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
90TEST_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
115TEST_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
An object that allocates device memory.
Definition: allocator.h:22
An implementation of the [RenderTargetAllocator] that caches all allocated texture data for one frame...
static constexpr AttachmentConfig kDefaultColorAttachmentConfig
Definition: render_target.h:55
const std::map< size_t, ColorAttachment > & GetColorAttachments() const
virtual std::shared_ptr< Texture > OnCreateTexture(const TextureDescriptor &desc) override
std::shared_ptr< DeviceBuffer > OnCreateBuffer(const DeviceBufferDescriptor &desc) override
SK_API GrDirectContext * GetContext(const SkImage *src)
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
TEST_P(AiksTest, CanRenderAdvancedBlendColorFilterWithSaveLayer)
ISize64 ISize
Definition: size.h:140
static constexpr Color Red()
Definition: color.h:274
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...