[macos] Release the copied pixel buffer after texture creation (flutter/engine#25548)

This commit is contained in:
Kaushik Iska
2021-04-12 13:54:14 -07:00
committed by GitHub
parent 0cf406f7e9
commit 76f3f23faf
4 changed files with 21 additions and 8 deletions

View File

@@ -39,6 +39,12 @@ NS_ASSUME_NONNULL_BEGIN
createExternalTextureWithIdentifier:(int64_t)textureID createExternalTextureWithIdentifier:(int64_t)textureID
texture:(NSObject<FlutterTexture>*)texture; texture:(NSObject<FlutterTexture>*)texture;
/**
* Creates a GrDirectContext with the provided `MTLDevice` and `MTLCommandQueue`.
*/
+ (sk_sp<GrDirectContext>)createGrContext:(id<MTLDevice>)device
commandQueue:(id<MTLCommandQueue>)commandQueue;
/** /**
* MTLDevice that is backing this context.s * MTLDevice that is backing this context.s
*/ */

View File

@@ -76,6 +76,12 @@ static GrContextOptions CreateMetalGrContextOptions() {
auto contextOptions = CreateMetalGrContextOptions(); auto contextOptions = CreateMetalGrContextOptions();
id<MTLDevice> device = _device; id<MTLDevice> device = _device;
id<MTLCommandQueue> commandQueue = _commandQueue; id<MTLCommandQueue> commandQueue = _commandQueue;
return [FlutterDarwinContextMetal createGrContext:device commandQueue:commandQueue];
}
+ (sk_sp<GrDirectContext>)createGrContext:(id<MTLDevice>)device
commandQueue:(id<MTLCommandQueue>)commandQueue {
auto contextOptions = CreateMetalGrContextOptions();
// Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later // Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later
// when the GrDirectContext is collected. // when the GrDirectContext is collected.
return GrDirectContext::MakeMetal((__bridge_retained void*)device, return GrDirectContext::MakeMetal((__bridge_retained void*)device,

View File

@@ -4,6 +4,8 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureMetal.h" #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureMetal.h"
#include "flutter/fml/platform/darwin/cf_utils.h"
@implementation FlutterExternalTextureMetal { @implementation FlutterExternalTextureMetal {
FlutterDarwinContextMetal* _darwinMetalContext; FlutterDarwinContextMetal* _darwinMetalContext;
@@ -31,7 +33,7 @@
- (BOOL)populateTexture:(FlutterMetalExternalTexture*)textureOut { - (BOOL)populateTexture:(FlutterMetalExternalTexture*)textureOut {
// Copy the pixel buffer from the FlutterTexture instance implemented on the user side. // Copy the pixel buffer from the FlutterTexture instance implemented on the user side.
CVPixelBufferRef pixelBuffer = [_texture copyPixelBuffer]; fml::CFRef<CVPixelBufferRef> pixelBuffer([_texture copyPixelBuffer]);
if (!pixelBuffer) { if (!pixelBuffer) {
return NO; return NO;

View File

@@ -5,11 +5,11 @@
#include "flutter/shell/platform/embedder/embedder_surface_metal.h" #include "flutter/shell/platform/embedder/embedder_surface_metal.h"
#include "flutter/fml/logging.h" #include "flutter/fml/logging.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "flutter/shell/gpu/gpu_surface_metal_delegate.h" #include "flutter/shell/gpu/gpu_surface_metal_delegate.h"
#import "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h" #import "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h"
#include "third_party/skia/include/gpu/GrDirectContext.h" #include "third_party/skia/include/gpu/GrDirectContext.h"
FLUTTER_ASSERT_NOT_ARC
namespace flutter { namespace flutter {
EmbedderSurfaceMetal::EmbedderSurfaceMetal( EmbedderSurfaceMetal::EmbedderSurfaceMetal(
@@ -20,12 +20,11 @@ EmbedderSurfaceMetal::EmbedderSurfaceMetal(
: GPUSurfaceMetalDelegate(MTLRenderTargetType::kMTLTexture), : GPUSurfaceMetalDelegate(MTLRenderTargetType::kMTLTexture),
metal_dispatch_table_(metal_dispatch_table), metal_dispatch_table_(metal_dispatch_table),
external_view_embedder_(external_view_embedder) { external_view_embedder_(external_view_embedder) {
auto darwin_metal_context = main_context_ = [FlutterDarwinContextMetal createGrContext:(id<MTLDevice>)device
fml::scoped_nsobject<FlutterDarwinContextMetal>{[[[FlutterDarwinContextMetal alloc] commandQueue:(id<MTLCommandQueue>)command_queue];
initWithMTLDevice:(id<MTLDevice>)device resource_context_ =
commandQueue:(id<MTLCommandQueue>)command_queue] retain]}; [FlutterDarwinContextMetal createGrContext:(id<MTLDevice>)device
main_context_ = darwin_metal_context.get().mainContext; commandQueue:(id<MTLCommandQueue>)command_queue];
resource_context_ = darwin_metal_context.get().resourceContext;
valid_ = main_context_ && resource_context_; valid_ = main_context_ && resource_context_;
} }