diff --git a/impeller/entity/render_target_cache.cc b/impeller/entity/render_target_cache.cc index a26551768975b..89e16289092a8 100644 --- a/impeller/entity/render_target_cache.cc +++ b/impeller/entity/render_target_cache.cc @@ -39,12 +39,16 @@ std::shared_ptr RenderTargetCache::CreateTexture( for (auto& td : texture_data_) { const auto other_desc = td.texture->GetTextureDescriptor(); + FML_DCHECK(td.texture != nullptr); if (!td.used_this_frame && desc == other_desc) { td.used_this_frame = true; return td.texture; } } auto result = RenderTargetAllocator::CreateTexture(desc); + if (result == nullptr) { + return result; + } texture_data_.push_back( TextureData{.used_this_frame = true, .texture = result}); return result; diff --git a/impeller/entity/render_target_cache_unittests.cc b/impeller/entity/render_target_cache_unittests.cc index d8086b32aec44..fc402f87fa9f5 100644 --- a/impeller/entity/render_target_cache_unittests.cc +++ b/impeller/entity/render_target_cache_unittests.cc @@ -25,13 +25,21 @@ class TestAllocator : public Allocator { std::shared_ptr OnCreateBuffer( const DeviceBufferDescriptor& desc) override { + if (should_fail) { + return nullptr; + } return std::make_shared(desc); }; virtual std::shared_ptr OnCreateTexture( const TextureDescriptor& desc) override { + if (should_fail) { + return nullptr; + } return std::make_shared(desc); }; + + bool should_fail = false; }; TEST(RenderTargetCacheTest, CachesUsedTexturesAcrossFrames) { @@ -62,5 +70,20 @@ TEST(RenderTargetCacheTest, CachesUsedTexturesAcrossFrames) { ASSERT_EQ(render_target_cache.CachedTextureCount(), 1u); } +TEST(RenderTargetCacheTest, DoesNotPersistFailedAllocations) { + auto allocator = std::make_shared(); + auto render_target_cache = RenderTargetCache(allocator); + auto desc = TextureDescriptor{ + .format = PixelFormat::kR8G8B8A8UNormInt, + .size = ISize(100, 100), + .usage = static_cast(TextureUsage::kRenderTarget)}; + + render_target_cache.Start(); + allocator->should_fail = true; + + ASSERT_EQ(render_target_cache.CreateTexture(desc), nullptr); + ASSERT_EQ(render_target_cache.CachedTextureCount(), 0u); +} + } // namespace testing } // namespace impeller