Skip to content

Commit 3dee902

Browse files
ramisbahimeta-codesync[bot]
authored andcommitted
igl | metal | Prevent C50 HDR import texture-plane crashes
Reviewed By: prashanthsadasivan Differential Revision: D114933597 fbshipit-source-id: 23603fda42e6ebf6a956455eca57083ead342687
1 parent 75f77d3 commit 3dee902

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/igl/metal/PlatformDevice.mm

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,22 +238,40 @@ TextureFormat convertToTextureFormat(OSType pixelFormat, size_t planeIndex) {
238238
planeIndex,
239239
&cvMetalTexture);
240240
IGL_DEBUG_ASSERT(result == kCVReturnSuccess,
241-
"Failed to created Metal texture from PixelBuffer");
241+
"Failed to create Metal texture from pixel buffer");
242242

243243
if (result != kCVReturnSuccess) {
244-
NSLog(@"Failed to created Metal texture from PixelBuffer");
244+
Result::setResult(
245+
outResult,
246+
Result::Code::RuntimeError,
247+
"Failed to create Metal texture from pixel buffer: " + std::to_string(result));
245248
return nullptr;
246249
}
247250

248251
id<MTLTexture> metalTexture = CVMetalTextureGetTexture(cvMetalTexture);
249252
CVBufferRelease(cvMetalTexture);
250253
cvMetalTexture = nullptr;
251254

255+
if (metalTexture == nil) {
256+
Result::setResult(
257+
outResult, Result::Code::RuntimeError, "CoreVideo returned an empty Metal texture");
258+
return nullptr;
259+
}
260+
252261
resultTexture = std::make_unique<Texture>(metalTexture, device_);
253262
if (auto resourceTracker = device_.getResourceTracker()) {
254263
resultTexture->initResourceTracker(resourceTracker);
255264
}
265+
} else {
266+
Result::setResult(
267+
outResult, Result::Code::RuntimeError, "Failed to get the Metal texture cache");
268+
return nullptr;
256269
}
270+
#else
271+
Result::setResult(outResult,
272+
Result::Code::Unsupported,
273+
"Metal pixel-buffer textures are not supported on this target");
274+
return nullptr;
257275
#endif
258276

259277
Result::setOk(outResult);

src/igl/tests/metal/Texture.mm

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,31 @@ void TearDown() override {}
381381
ASSERT_EQ(result.isOk(), false) << result.message.c_str();
382382
}
383383

384+
TEST_F(TextureMTLTest, createTextureFromNativePixelBufferWithInvalidPlaneSetsError) {
385+
CVPixelBufferRef pixelBuffer = nullptr;
386+
NSDictionary* bufferAttributes = @{
387+
(NSString*)kCVPixelBufferIOSurfacePropertiesKey : @{},
388+
(NSString*)kCVPixelBufferMetalCompatibilityKey : @YES,
389+
};
390+
const CVReturn pixelBufferResult =
391+
CVPixelBufferCreate(kCFAllocatorDefault,
392+
100,
393+
100,
394+
kCVPixelFormatType_32BGRA,
395+
(__bridge CFDictionaryRef)(bufferAttributes),
396+
&pixelBuffer);
397+
ASSERT_EQ(pixelBufferResult, kCVReturnSuccess);
398+
399+
auto* platformDevice = device_->getPlatformDevice<igl::metal::PlatformDevice>();
400+
Result result;
401+
auto texture = platformDevice->createTextureFromNativePixelBuffer(
402+
pixelBuffer, TextureFormat::BGRA_UNorm8, 1, &result);
403+
CVPixelBufferRelease(pixelBuffer);
404+
405+
EXPECT_EQ(texture, nullptr);
406+
EXPECT_FALSE(result.isOk());
407+
}
408+
384409
TEST_F(TextureMTLTest, ConvertTextureFormats) {
385410
const std::vector<TextureFormat> inputFormats = {
386411
TextureFormat::A_UNorm8,

0 commit comments

Comments
 (0)