Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 5c80851

Browse files
Jonah Williamsharryterkelsen
authored andcommitted
[Impeller] dont cache failed render target allocations. (#45895)
Otherwise we'll null de-ref when trying to create a texture with the same descriptor later on.
1 parent 1216347 commit 5c80851

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

impeller/entity/render_target_cache.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ std::shared_ptr<Texture> RenderTargetCache::CreateTexture(
3939

4040
for (auto& td : texture_data_) {
4141
const auto other_desc = td.texture->GetTextureDescriptor();
42+
FML_DCHECK(td.texture != nullptr);
4243
if (!td.used_this_frame && desc == other_desc) {
4344
td.used_this_frame = true;
4445
return td.texture;
4546
}
4647
}
4748
auto result = RenderTargetAllocator::CreateTexture(desc);
49+
if (result == nullptr) {
50+
return result;
51+
}
4852
texture_data_.push_back(
4953
TextureData{.used_this_frame = true, .texture = result});
5054
return result;

impeller/entity/render_target_cache_unittests.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@ class TestAllocator : public Allocator {
2525

2626
std::shared_ptr<DeviceBuffer> OnCreateBuffer(
2727
const DeviceBufferDescriptor& desc) override {
28+
if (should_fail) {
29+
return nullptr;
30+
}
2831
return std::make_shared<MockDeviceBuffer>(desc);
2932
};
3033

3134
virtual std::shared_ptr<Texture> OnCreateTexture(
3235
const TextureDescriptor& desc) override {
36+
if (should_fail) {
37+
return nullptr;
38+
}
3339
return std::make_shared<MockTexture>(desc);
3440
};
41+
42+
bool should_fail = false;
3543
};
3644

3745
TEST(RenderTargetCacheTest, CachesUsedTexturesAcrossFrames) {
@@ -62,5 +70,20 @@ TEST(RenderTargetCacheTest, CachesUsedTexturesAcrossFrames) {
6270
ASSERT_EQ(render_target_cache.CachedTextureCount(), 1u);
6371
}
6472

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+
6588
} // namespace testing
6689
} // namespace impeller

0 commit comments

Comments
 (0)