Skip to content

Commit 2edc85c

Browse files
author
Jonah Williams
authored
[Impeller] make sure binding nullptr texture does not crash. (flutter/engine#56381)
Fixes flutter#158074 binding a nullptr texture should fail but not crash.
1 parent d0bb78c commit 2edc85c

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

engine/src/flutter/impeller/renderer/backend/metal/render_pass_mtl.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ static bool Bind(PassBindingsCacheMTL& pass,
408408
const ShaderMetadata& metadata,
409409
std::shared_ptr<const Texture> texture,
410410
const std::unique_ptr<const Sampler>& sampler) {
411+
if (!texture) {
412+
return false;
413+
}
411414
return Bind(pass_bindings_, stage, slot.texture_index, sampler, *texture);
412415
}
413416

engine/src/flutter/impeller/renderer/backend/vulkan/render_pass_vk.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ bool RenderPassVK::BindResource(ShaderStage stage,
602602
if (bound_buffer_offset_ >= kMaxBindings) {
603603
return false;
604604
}
605-
if (!texture->IsValid() || !sampler) {
605+
if (!texture || !texture->IsValid() || !sampler) {
606606
return false;
607607
}
608608
const TextureVK& texture_vk = TextureVK::Cast(*texture);

engine/src/flutter/impeller/renderer/renderer_unittests.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "impeller/fixtures/swizzle.frag.h"
3232
#include "impeller/fixtures/texture.frag.h"
3333
#include "impeller/fixtures/texture.vert.h"
34-
#include "impeller/geometry/path_builder.h"
3534
#include "impeller/playground/playground.h"
3635
#include "impeller/playground/playground_test.h"
3736
#include "impeller/renderer/command_buffer.h"
@@ -1626,6 +1625,21 @@ TEST_P(RendererTest, CanSepiaToneThenSwizzleWithSubpasses) {
16261625
OpenPlaygroundHere(callback);
16271626
}
16281627

1628+
TEST_P(RendererTest, BindingNullTexturesDoesNotCrash) {
1629+
using FS = BoxFadeFragmentShader;
1630+
1631+
auto context = GetContext();
1632+
const std::unique_ptr<const Sampler>& sampler =
1633+
context->GetSamplerLibrary()->GetSampler({});
1634+
auto command_buffer = context->CreateCommandBuffer();
1635+
1636+
RenderTargetAllocator allocator(context->GetResourceAllocator());
1637+
RenderTarget target = allocator.CreateOffscreen(*context, {1, 1}, 1);
1638+
1639+
auto pass = command_buffer->CreateRenderPass(target);
1640+
EXPECT_FALSE(FS::BindContents2(*pass, nullptr, sampler));
1641+
}
1642+
16291643
} // namespace testing
16301644
} // namespace impeller
16311645

0 commit comments

Comments
 (0)