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 64f9058c1c..813a8c0eab 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 @@ -10,6 +10,7 @@ #import "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h" #import "flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h" +#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureMetal.h" #include "flutter/shell/platform/embedder/embedder.h" #include "flutter/shell/platform/embedder/embedder_external_texture_metal.h" #import "flutter/testing/testing.h" @@ -18,6 +19,44 @@ #include "third_party/skia/include/core/SkSamplingOptions.h" #include "third_party/skia/include/core/SkSurface.h" +@interface TestExternalTexture : NSObject + +- (nonnull instancetype)initWidth:(size_t)width height:(size_t)height; + +@end + +@implementation TestExternalTexture { + size_t _width; + size_t _height; +} + +- (nonnull instancetype)initWidth:(size_t)width height:(size_t)height { + if (self = [super init]) { + _width = width; + _height = height; + } + return self; +} + +- (CVPixelBufferRef)copyPixelBuffer { + return [self pixelBuffer]; +} + +- (CVPixelBufferRef)pixelBuffer { + NSDictionary* options = @{ + // This key is required to generate SKPicture with CVPixelBufferRef in metal. + (NSString*)kCVPixelBufferMetalCompatibilityKey : @YES + }; + CVPixelBufferRef pxbuffer = NULL; + CVReturn status = + CVPixelBufferCreate(kCFAllocatorDefault, _width, _width, kCVPixelFormatType_32BGRA, + (__bridge CFDictionaryRef)options, &pxbuffer); + NSAssert(status == kCVReturnSuccess && pxbuffer != NULL, @"Failed to create pixel buffer."); + return pxbuffer; +} + +@end + namespace flutter::testing { TEST(FlutterEmbedderExternalTextureUnittests, TestTextureResolution) { @@ -74,4 +113,50 @@ TEST(FlutterEmbedderExternalTextureUnittests, TestTextureResolution) { gpuSurface->makeImageSnapshot(); } +TEST(FlutterEmbedderExternalTextureUnittests, TestPopulateExternalTexture) { + // 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]; + 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 == 1); + EXPECT_TRUE(texture->textures != nullptr); + EXPECT_TRUE(texture->pixel_format == FlutterMetalExternalTexturePixelFormat::kRGBA); + + 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 672642531c..fae7a42a02 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 @@ -11,7 +11,7 @@ id _texture; - id _mtlTexture; + std::vector _textures; } - (instancetype)initWithFlutterTexture:(id)texture @@ -57,17 +57,14 @@ return NO; } - _mtlTexture = CVMetalTextureGetTexture(cvMetalTexture); + _textures = {(__bridge FlutterMetalTextureHandle)CVMetalTextureGetTexture(cvMetalTexture)}; CVBufferRelease(cvMetalTexture); - std::vector textures = { - (__bridge FlutterMetalTextureHandle)_mtlTexture}; - textureOut->num_textures = 1; textureOut->height = textureSize.height(); textureOut->width = textureSize.width(); textureOut->pixel_format = FlutterMetalExternalTexturePixelFormat::kRGBA; - textureOut->textures = textures.data(); + textureOut->textures = _textures.data(); return YES; } diff --git a/engine/src/flutter/testing/scenario_app/ios/Scenarios/Scenarios/ContinuousTexture.m b/engine/src/flutter/testing/scenario_app/ios/Scenarios/Scenarios/ContinuousTexture.m index 0211062a28..037d9531cc 100644 --- a/engine/src/flutter/testing/scenario_app/ios/Scenarios/Scenarios/ContinuousTexture.m +++ b/engine/src/flutter/testing/scenario_app/ios/Scenarios/Scenarios/ContinuousTexture.m @@ -34,9 +34,7 @@ CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, 200, 200, kCVPixelFormatType_32BGRA, (__bridge CFDictionaryRef)options, &pxbuffer); - NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL); - - CVPixelBufferLockBaseAddress(pxbuffer, 0); + NSAssert(status == kCVReturnSuccess && pxbuffer != NULL, @"Failed to create pixel buffer."); return pxbuffer; }