[darwin] Update pixel format handling in FlutterTexture (flutter/engine#52326)

CVPixelBuffers from the application can have arbitrary pixel formats. However, current implementation doesn't support all pixel formats. To address this, add a comment to the FlutterTexture class to clarify which pixel formats are supported. Also, reject unsupported pixel formats so that the application can handle them properly.

Fixes [#147242](https://github.com/flutter/flutter/issues/147242).

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Byoungchan Lee
2024-04-25 03:10:17 +09:00
committed by GitHub
parent 15e063537f
commit 011c50a4c5
4 changed files with 63 additions and 4 deletions

View File

@@ -19,7 +19,14 @@ FLUTTER_DARWIN_EXPORT
* See also: https://github.com/flutter/plugins/tree/master/packages/camera
*/
@protocol FlutterTexture <NSObject>
/** Copy the contents of the texture into a `CVPixelBuffer`. */
/**
* Copy the contents of the texture into a `CVPixelBuffer`.
*
* The type of the pixel buffer is one of the following:
* - `kCVPixelFormatType_32BGRA`
* - `kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange`
* - `kCVPixelFormatType_420YpCbCr8BiPlanarFullRange`
*/
- (CVPixelBufferRef _Nullable)copyPixelBuffer;
/**

View File

@@ -133,8 +133,11 @@ FLUTTER_ASSERT_ARC
if (_pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange ||
_pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) {
image = [self wrapNV12ExternalPixelBuffer:pixelBuffer context:context];
} else if (_pixelFormat == kCVPixelFormatType_32BGRA) {
image = [self wrapBGRAExternalPixelBuffer:pixelBuffer context:context];
} else {
image = [self wrapRGBAExternalPixelBuffer:pixelBuffer context:context];
FML_LOG(ERROR) << "Unsupported pixel format: " << _pixelFormat;
return nullptr;
}
if (!image) {
@@ -235,7 +238,7 @@ FLUTTER_ASSERT_ARC
return flutter::DlImage::Make(skImage);
}
- (sk_sp<flutter::DlImage>)wrapRGBAExternalPixelBuffer:(CVPixelBufferRef)pixelBuffer
- (sk_sp<flutter::DlImage>)wrapBGRAExternalPixelBuffer:(CVPixelBufferRef)pixelBuffer
context:(flutter::Texture::PaintContext&)context {
SkISize textureSize =
SkISize::Make(CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer));

View File

@@ -288,4 +288,50 @@ TEST_F(FlutterEmbedderExternalTextureTest, TestPopulateExternalTextureYUVA2) {
gpuSurface->makeImageSnapshot();
}
TEST_F(FlutterEmbedderExternalTextureTest, TestPopulateUnsupportedExternalTexture) {
// Constants.
const size_t width = 100;
const size_t height = 100;
const int64_t texture_id = 1;
// Set up the surface.
FlutterDarwinContextMetalSkia* darwinContextMetal =
[[FlutterDarwinContextMetalSkia alloc] initWithDefaultMTLDevice];
SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
GrDirectContext* grContext = darwinContextMetal.mainContext.get();
sk_sp<SkSurface> gpuSurface(SkSurfaces::RenderTarget(grContext, skgpu::Budgeted::kNo, info));
// Create a texture.
TestExternalTexture* testExternalTexture =
[[TestExternalTexture alloc] initWidth:width
height:height
pixelFormatType:kCVPixelFormatType_420YpCbCr8PlanarFullRange];
FlutterExternalTexture* textureHolder =
[[FlutterExternalTexture 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);
auto texture = std::make_unique<FlutterMetalExternalTexture>();
EXPECT_FALSE([textureHolder populateTexture:texture.get()]);
return nullptr;
};
// Render the texture.
std::unique_ptr<flutter::Texture> texture =
std::make_unique<EmbedderExternalTextureMetal>(texture_id, callback);
SkRect bounds = SkRect::MakeWH(info.width(), info.height());
DlImageSampling sampling = DlImageSampling::kNearestNeighbor;
DlSkCanvasAdapter canvas(gpuSurface->getCanvas());
flutter::Texture::PaintContext context{
.canvas = &canvas,
.gr_context = grContext,
};
texture->Paint(context, bounds, /*freeze=*/false, sampling);
}
} // namespace flutter::testing

View File

@@ -43,8 +43,11 @@
if (pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange ||
pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) {
return [self populateTextureFromYUVAPixelBuffer:pixelBuffer textureOut:textureOut];
} else {
} else if (pixel_format == kCVPixelFormatType_32BGRA) {
return [self populateTextureFromRGBAPixelBuffer:pixelBuffer textureOut:textureOut];
} else {
NSLog(@"Unsupported pixel format: %d", pixel_format);
return NO;
}
}