-
Notifications
You must be signed in to change notification settings - Fork 6k
[metal] Darwin unified external metal textures #24157
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,12 @@ | |
#ifndef SHELL_PLATFORM_DARWIN_GRAPHICS_DARWIN_CONTEXT_METAL_H_ | ||
#define SHELL_PLATFORM_DARWIN_GRAPHICS_DARWIN_CONTEXT_METAL_H_ | ||
|
||
#import <CoreVideo/CVMetalTextureCache.h> | ||
#import <Foundation/Foundation.h> | ||
#import <Metal/Metal.h> | ||
|
||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h" | ||
#import "flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h" | ||
#include "third_party/skia/include/gpu/GrDirectContext.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
@@ -29,6 +32,12 @@ NS_ASSUME_NONNULL_BEGIN | |
- (instancetype)initWithMTLDevice:(id<MTLDevice>)device | ||
commandQueue:(id<MTLCommandQueue>)commandQueue; | ||
|
||
/** | ||
* Creates an external texture with the specified ID and contents. | ||
*/ | ||
- (FlutterDarwinExternalTextureMetal*)externalTextureWithID:(int64_t)textureID | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
texture:(NSObject<FlutterTexture>*)texture; | ||
|
||
/** | ||
* MTLDevice that is backing this context.s | ||
*/ | ||
|
@@ -50,6 +59,11 @@ NS_ASSUME_NONNULL_BEGIN | |
*/ | ||
@property(nonatomic, readonly) sk_sp<GrDirectContext> resourceContext; | ||
|
||
/* | ||
* Texture cache for external textures. | ||
*/ | ||
@property(nonatomic, readonly) CVMetalTextureCacheRef textureCache; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,32 +33,39 @@ - (instancetype)initWithMTLDevice:(id<MTLDevice>)device | |
|
||
if (!_device) { | ||
FML_DLOG(ERROR) << "Could not acquire Metal device."; | ||
[self release]; | ||
return nil; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we decorate translation units with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added it to the ARC targets that i've touched. |
||
} | ||
|
||
_commandQueue = commandQueue; | ||
|
||
if (!_commandQueue) { | ||
FML_DLOG(ERROR) << "Could not create Metal command queue."; | ||
[self release]; | ||
return nil; | ||
} | ||
|
||
[_commandQueue setLabel:@"Flutter Main Queue"]; | ||
|
||
CVReturn cvReturn = CVMetalTextureCacheCreate(kCFAllocatorDefault, // allocator | ||
nil, // cache attributes (nil default) | ||
_device, // metal device | ||
nil, // texture attributes (nil default) | ||
&_textureCache // [out] cache | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CoreFoundation objects need to be explicitly released. IIRC, this would leak the texture cache in the dealloc unless There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was worried about the same. I will try to add a test to see that everything that was allocated gets collected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't able to come up with a way to test de-alloc in a nice way. I audited the code and made sure that are CoreFoundation objects are released and retained as necessary. |
||
); | ||
if (cvReturn != kCVReturnSuccess) { | ||
FML_DLOG(ERROR) << "Could not create Metal texture cache."; | ||
return nil; | ||
} | ||
|
||
auto contextOptions = CreateMetalGrContextOptions(); | ||
|
||
// Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later | ||
// when the GrDirectContext is collected. | ||
_mainContext = | ||
GrDirectContext::MakeMetal([_device retain], [_commandQueue retain], contextOptions); | ||
_resourceContext = | ||
GrDirectContext::MakeMetal([_device retain], [_commandQueue retain], contextOptions); | ||
_mainContext = GrDirectContext::MakeMetal( | ||
(__bridge_retained void*)_device, (__bridge_retained void*)_commandQueue, contextOptions); | ||
_resourceContext = _mainContext; | ||
|
||
if (!_mainContext || !_resourceContext) { | ||
FML_DLOG(ERROR) << "Could not create Skia Metal contexts."; | ||
[self release]; | ||
return nil; | ||
} | ||
|
||
|
@@ -67,4 +74,11 @@ - (instancetype)initWithMTLDevice:(id<MTLDevice>)device | |
return self; | ||
} | ||
|
||
- (FlutterDarwinExternalTextureMetal*)externalTextureWithID:(int64_t)textureID | ||
texture:(NSObject<FlutterTexture>*)texture { | ||
return [[FlutterDarwinExternalTextureMetal alloc] initWithTextureCache:_textureCache | ||
textureID:textureID | ||
texture:texture]; | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <Metal/Metal.h> | ||
|
||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h" | ||
#include "third_party/skia/include/core/SkCanvas.h" | ||
#include "third_party/skia/include/gpu/GrDirectContext.h" | ||
|
||
@interface FlutterDarwinExternalTextureMetal : NSObject | ||
|
||
- (nullable instancetype)initWithTextureCache:(nonnull CVMetalTextureCacheRef)textureCache | ||
textureID:(int64_t)textureID | ||
texture:(nonnull NSObject<FlutterTexture>*)texture; | ||
|
||
- (void)paint:(SkCanvas&)canvas | ||
bounds:(const SkRect&)bounds | ||
freeze:(BOOL)freeze | ||
grContext:(nonnull GrDirectContext*)grContext | ||
sampling:(const SkSamplingOptions&)sampling; | ||
|
||
- (void)onGrContextCreated; | ||
|
||
- (void)onGrContextDestroyed; | ||
|
||
- (void)markNewFrameAvailable; | ||
|
||
- (void)onTextureUnregistered; | ||
|
||
@property(nonatomic, readonly) int64_t textureID; | ||
|
||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be in addition to the
flutter_cflags_objc
? I am not sure what those flags currently are but we are no longer applying them to this target.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that the other targets that enable arc, only had this will verify.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a new high level param for arc targets and unified it.