forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_target_cache_unittests.cc
134 lines (106 loc) · 4.34 KB
/
render_target_cache_unittests.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "flutter/testing/testing.h"
#include "impeller/base/validation.h"
#include "impeller/core/allocator.h"
#include "impeller/core/texture_descriptor.h"
#include "impeller/entity/entity_playground.h"
#include "impeller/entity/render_target_cache.h"
#include "impeller/playground/playground_test.h"
#include "impeller/renderer/testing/mocks.h"
namespace impeller {
namespace testing {
using RenderTargetCacheTest = EntityPlayground;
INSTANTIATE_PLAYGROUND_SUITE(RenderTargetCacheTest);
class TestAllocator : public Allocator {
public:
TestAllocator() = default;
~TestAllocator() = default;
ISize GetMaxTextureSizeSupported() const override {
return ISize(1024, 1024);
};
std::shared_ptr<DeviceBuffer> OnCreateBuffer(
const DeviceBufferDescriptor& desc) override {
if (should_fail) {
return nullptr;
}
return std::make_shared<MockDeviceBuffer>(desc);
};
virtual std::shared_ptr<Texture> OnCreateTexture(
const TextureDescriptor& desc) override {
if (should_fail) {
return nullptr;
}
return std::make_shared<MockTexture>(desc);
};
bool should_fail = false;
};
TEST_P(RenderTargetCacheTest, CachesUsedTexturesAcrossFrames) {
auto render_target_cache =
RenderTargetCache(GetContext()->GetResourceAllocator());
render_target_cache.Start();
// Create two render targets of the same exact size/shape. Both should be
// marked as used this frame, so the cached data set will contain two.
render_target_cache.CreateOffscreen(*GetContext(), {100, 100}, 1);
render_target_cache.CreateOffscreen(*GetContext(), {100, 100}, 1);
EXPECT_EQ(render_target_cache.CachedTextureCount(), 2u);
render_target_cache.End();
render_target_cache.Start();
// Next frame, only create one texture. The set will still contain two,
// but one will be removed at the end of the frame.
render_target_cache.CreateOffscreen(*GetContext(), {100, 100}, 1);
EXPECT_EQ(render_target_cache.CachedTextureCount(), 2u);
render_target_cache.End();
EXPECT_EQ(render_target_cache.CachedTextureCount(), 1u);
}
TEST_P(RenderTargetCacheTest, DoesNotPersistFailedAllocations) {
ScopedValidationDisable disable;
auto allocator = std::make_shared<TestAllocator>();
auto render_target_cache = RenderTargetCache(allocator);
render_target_cache.Start();
allocator->should_fail = true;
auto render_target =
render_target_cache.CreateOffscreen(*GetContext(), {100, 100}, 1);
EXPECT_FALSE(render_target.IsValid());
EXPECT_EQ(render_target_cache.CachedTextureCount(), 0u);
}
TEST_P(RenderTargetCacheTest, CachedTextureGetsNewAttachmentConfig) {
auto render_target_cache =
RenderTargetCache(GetContext()->GetResourceAllocator());
render_target_cache.Start();
RenderTarget::AttachmentConfig color_attachment_config =
RenderTarget::kDefaultColorAttachmentConfig;
RenderTarget target1 = render_target_cache.CreateOffscreen(
*GetContext(), {100, 100}, 1, "Offscreen1", color_attachment_config);
render_target_cache.End();
render_target_cache.Start();
color_attachment_config.clear_color = Color::Red();
RenderTarget target2 = render_target_cache.CreateOffscreen(
*GetContext(), {100, 100}, 1, "Offscreen2", color_attachment_config);
render_target_cache.End();
auto color1 = target1.GetColorAttachments().find(0)->second;
auto color2 = target2.GetColorAttachments().find(0)->second;
// The second color attachment should reuse the first attachment's texture
// but with attributes from the second AttachmentConfig.
EXPECT_EQ(color2.texture, color1.texture);
EXPECT_EQ(color2.clear_color, Color::Red());
}
TEST_P(RenderTargetCacheTest, CreateWithEmptySize) {
auto render_target_cache =
RenderTargetCache(GetContext()->GetResourceAllocator());
render_target_cache.Start();
RenderTarget empty_target =
render_target_cache.CreateOffscreen(*GetContext(), {100, 0}, 1);
RenderTarget empty_target_msaa =
render_target_cache.CreateOffscreenMSAA(*GetContext(), {0, 0}, 1);
render_target_cache.End();
{
ScopedValidationDisable disable_validation;
EXPECT_FALSE(empty_target.IsValid());
EXPECT_FALSE(empty_target_msaa.IsValid());
}
}
} // namespace testing
} // namespace impeller