diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderExternalTextureUnittests.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderExternalTextureUnittests.mm index 813a8c0eab..144d09e22a 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderExternalTextureUnittests.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderExternalTextureUnittests.mm @@ -21,19 +21,25 @@ @interface TestExternalTexture : NSObject -- (nonnull instancetype)initWidth:(size_t)width height:(size_t)height; +- (nonnull instancetype)initWidth:(size_t)width + height:(size_t)height + pixelFormatType:(OSType)pixelFormatType; @end @implementation TestExternalTexture { size_t _width; size_t _height; + OSType _pixelFormatType; } -- (nonnull instancetype)initWidth:(size_t)width height:(size_t)height { +- (nonnull instancetype)initWidth:(size_t)width + height:(size_t)height + pixelFormatType:(OSType)pixelFormatType { if (self = [super init]) { _width = width; _height = height; + _pixelFormatType = pixelFormatType; } return self; } @@ -48,9 +54,8 @@ (NSString*)kCVPixelBufferMetalCompatibilityKey : @YES }; CVPixelBufferRef pxbuffer = NULL; - CVReturn status = - CVPixelBufferCreate(kCFAllocatorDefault, _width, _width, kCVPixelFormatType_32BGRA, - (__bridge CFDictionaryRef)options, &pxbuffer); + CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, _width, _width, _pixelFormatType, + (__bridge CFDictionaryRef)options, &pxbuffer); NSAssert(status == kCVReturnSuccess && pxbuffer != NULL, @"Failed to create pixel buffer."); return pxbuffer; } @@ -127,8 +132,10 @@ TEST(FlutterEmbedderExternalTextureUnittests, TestPopulateExternalTexture) { sk_sp gpuSurface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info)); // Create a texture. - TestExternalTexture* testExternalTexture = [[TestExternalTexture alloc] initWidth:width - height:height]; + TestExternalTexture* testExternalTexture = + [[TestExternalTexture alloc] initWidth:width + height:height + pixelFormatType:kCVPixelFormatType_32BGRA]; FlutterExternalTextureMetal* textureHolder = [[FlutterExternalTextureMetal alloc] initWithFlutterTexture:testExternalTexture darwinMetalContext:darwinContextMetal]; @@ -159,4 +166,100 @@ TEST(FlutterEmbedderExternalTextureUnittests, TestPopulateExternalTexture) { gpuSurface->makeImageSnapshot(); } +TEST(FlutterEmbedderExternalTextureUnittests, TestPopulateExternalTextureYUVA) { + // Constants. + const size_t width = 100; + const size_t height = 100; + const int64_t texture_id = 1; + + // Set up the surface. + FlutterDarwinContextMetal* darwinContextMetal = + [[FlutterDarwinContextMetal alloc] initWithDefaultMTLDevice]; + SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); + GrDirectContext* grContext = darwinContextMetal.mainContext.get(); + sk_sp gpuSurface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info)); + + // Create a texture. + TestExternalTexture* testExternalTexture = + [[TestExternalTexture alloc] initWidth:width + height:height + pixelFormatType:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange]; + FlutterExternalTextureMetal* textureHolder = + [[FlutterExternalTextureMetal alloc] initWithFlutterTexture:testExternalTexture + darwinMetalContext:darwinContextMetal]; + + // Callback to resolve the texture. + EmbedderExternalTextureMetal::ExternalTextureCallback callback = [&](int64_t texture_id, size_t w, + size_t h) { + EXPECT_TRUE(w == width); + EXPECT_TRUE(h == height); + + FlutterMetalExternalTexture* texture = new FlutterMetalExternalTexture(); + [textureHolder populateTexture:texture]; + + EXPECT_TRUE(texture->num_textures == 2); + EXPECT_TRUE(texture->textures != nullptr); + EXPECT_TRUE(texture->pixel_format == FlutterMetalExternalTexturePixelFormat::kYUVA); + + return std::unique_ptr(texture); + }; + + // Render the texture. + std::unique_ptr texture = + std::make_unique(texture_id, callback); + SkRect bounds = SkRect::MakeWH(info.width(), info.height()); + SkSamplingOptions sampling = SkSamplingOptions(SkFilterMode::kNearest); + texture->Paint(*gpuSurface->getCanvas(), bounds, /*freeze=*/false, grContext, sampling); + + gpuSurface->makeImageSnapshot(); +} + +TEST(FlutterEmbedderExternalTextureUnittests, TestPopulateExternalTextureYUVA2) { + // Constants. + const size_t width = 100; + const size_t height = 100; + const int64_t texture_id = 1; + + // Set up the surface. + FlutterDarwinContextMetal* darwinContextMetal = + [[FlutterDarwinContextMetal alloc] initWithDefaultMTLDevice]; + SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); + GrDirectContext* grContext = darwinContextMetal.mainContext.get(); + sk_sp gpuSurface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info)); + + // Create a texture. + TestExternalTexture* testExternalTexture = + [[TestExternalTexture alloc] initWidth:width + height:height + pixelFormatType:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange]; + FlutterExternalTextureMetal* textureHolder = + [[FlutterExternalTextureMetal alloc] initWithFlutterTexture:testExternalTexture + darwinMetalContext:darwinContextMetal]; + + // Callback to resolve the texture. + EmbedderExternalTextureMetal::ExternalTextureCallback callback = [&](int64_t texture_id, size_t w, + size_t h) { + EXPECT_TRUE(w == width); + EXPECT_TRUE(h == height); + + FlutterMetalExternalTexture* texture = new FlutterMetalExternalTexture(); + [textureHolder populateTexture:texture]; + + EXPECT_TRUE(texture->num_textures == 2); + EXPECT_TRUE(texture->textures != nullptr); + EXPECT_TRUE(texture->pixel_format == FlutterMetalExternalTexturePixelFormat::kYUVA); + + return std::unique_ptr(texture); + }; + + // Render the texture. + std::unique_ptr texture = + std::make_unique(texture_id, callback); + SkRect bounds = SkRect::MakeWH(info.width(), info.height()); + SkSamplingOptions sampling = SkSamplingOptions(SkFilterMode::kNearest); + texture->Paint(*gpuSurface->getCanvas(), bounds, /*freeze=*/false, grContext, sampling); + + gpuSurface->makeImageSnapshot(); +} + } // namespace flutter::testing diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureMetal.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureMetal.mm index a2d315137c..1ece011425 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureMetal.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureMetal.mm @@ -39,6 +39,71 @@ return NO; } + OSType pixel_format = CVPixelBufferGetPixelFormatType(pixelBuffer); + if (pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange || + pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) { + return [self populateTextureFromYUVAPixelBuffer:pixelBuffer textureOut:textureOut]; + } else { + return [self populateTextureFromRGBAPixelBuffer:pixelBuffer textureOut:textureOut]; + } +} + +- (BOOL)populateTextureFromYUVAPixelBuffer:(nonnull CVPixelBufferRef)pixelBuffer + textureOut:(nonnull FlutterMetalExternalTexture*)textureOut { + CVMetalTextureRef yCVMetalTexture = nullptr; + CVMetalTextureRef uvCVMetalTextureRef = nullptr; + SkISize textureSize = + SkISize::Make(CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer)); + + CVReturn yCVReturn = CVMetalTextureCacheCreateTextureFromImage( + /*allocator=*/kCFAllocatorDefault, + /*textureCache=*/_darwinMetalContext.textureCache, + /*sourceImage=*/pixelBuffer, + /*textureAttributes=*/nullptr, + /*pixelFormat=*/MTLPixelFormatR8Unorm, + /*width=*/CVPixelBufferGetWidthOfPlane(pixelBuffer, 0u), + /*height=*/CVPixelBufferGetHeightOfPlane(pixelBuffer, 0u), + /*planeIndex=*/0u, + /*texture=*/&yCVMetalTexture); + + if (yCVReturn != kCVReturnSuccess) { + NSLog(@"Could not create Metal texture from pixel buffer: CVReturn %d", yCVReturn); + return NO; + } + + CVReturn uvCVReturn = CVMetalTextureCacheCreateTextureFromImage( + /*allocator=*/kCFAllocatorDefault, + /*textureCache=*/_darwinMetalContext.textureCache, + /*sourceImage=*/pixelBuffer, + /*textureAttributes=*/nullptr, + /*pixelFormat=*/MTLPixelFormatRG8Unorm, + /*width=*/CVPixelBufferGetWidthOfPlane(pixelBuffer, 1u), + /*height=*/CVPixelBufferGetHeightOfPlane(pixelBuffer, 1u), + /*planeIndex=*/1u, + /*texture=*/&uvCVMetalTextureRef); + + if (uvCVReturn != kCVReturnSuccess) { + CVBufferRelease(yCVMetalTexture); + NSLog(@"Could not create Metal texture from pixel buffer: CVReturn %d", uvCVReturn); + return NO; + } + + _textures = {(__bridge FlutterMetalTextureHandle)CVMetalTextureGetTexture(yCVMetalTexture), + (__bridge FlutterMetalTextureHandle)CVMetalTextureGetTexture(uvCVMetalTextureRef)}; + CVBufferRelease(yCVMetalTexture); + CVBufferRelease(uvCVMetalTextureRef); + + textureOut->num_textures = 2; + textureOut->height = textureSize.height(); + textureOut->width = textureSize.width(); + textureOut->pixel_format = FlutterMetalExternalTexturePixelFormat::kYUVA; + textureOut->textures = _textures.data(); + + return YES; +} + +- (BOOL)populateTextureFromRGBAPixelBuffer:(nonnull CVPixelBufferRef)pixelBuffer + textureOut:(nonnull FlutterMetalExternalTexture*)textureOut { SkISize textureSize = SkISize::Make(CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer));