From 08c9653756f23c6d4eb3d1b83ef8cf771a5ac2b1 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Fri, 22 Mar 2024 10:53:57 -0700 Subject: [PATCH] [Impeller] Check for empty sizes when creating render targets in RenderTargetCache (flutter/engine#51597) Using an empty size will result in an invalid RenderTarget. Calling IsValid on that target can produce Impeller validation warnings. --- .../impeller/entity/render_target_cache.cc | 8 ++++++++ .../entity/render_target_cache_unittests.cc | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/engine/src/flutter/impeller/entity/render_target_cache.cc b/engine/src/flutter/impeller/entity/render_target_cache.cc index 4a574758e2..c96dc6a065 100644 --- a/engine/src/flutter/impeller/entity/render_target_cache.cc +++ b/engine/src/flutter/impeller/entity/render_target_cache.cc @@ -36,6 +36,10 @@ RenderTarget RenderTargetCache::CreateOffscreen( std::optional stencil_attachment_config, const std::shared_ptr& existing_color_texture, const std::shared_ptr& existing_depth_stencil_texture) { + if (size.IsEmpty()) { + return {}; + } + FML_DCHECK(existing_color_texture == nullptr && existing_depth_stencil_texture == nullptr); auto config = RenderTargetConfig{ @@ -81,6 +85,10 @@ RenderTarget RenderTargetCache::CreateOffscreenMSAA( const std::shared_ptr& existing_color_msaa_texture, const std::shared_ptr& existing_color_resolve_texture, const std::shared_ptr& existing_depth_stencil_texture) { + if (size.IsEmpty()) { + return {}; + } + FML_DCHECK(existing_color_msaa_texture == nullptr && existing_color_resolve_texture == nullptr && existing_depth_stencil_texture == nullptr); diff --git a/engine/src/flutter/impeller/entity/render_target_cache_unittests.cc b/engine/src/flutter/impeller/entity/render_target_cache_unittests.cc index c39d10ae11..73493e7641 100644 --- a/engine/src/flutter/impeller/entity/render_target_cache_unittests.cc +++ b/engine/src/flutter/impeller/entity/render_target_cache_unittests.cc @@ -112,5 +112,23 @@ TEST_P(RenderTargetCacheTest, CachedTextureGetsNewAttachmentConfig) { 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