Use cc_library for DrishtiMetalHelper

PiperOrigin-RevId: 576230898
This commit is contained in:
Sebastian Schmidt 2023-10-24 12:37:28 -07:00 committed by Copybara-Service
parent 905a18c88c
commit c698414c71
2 changed files with 47 additions and 31 deletions

View File

@ -863,12 +863,14 @@ cc_library(
}), }),
) )
objc_library( cc_library(
name = "MPPMetalHelper", name = "MPPMetalHelper",
srcs = ["MPPMetalHelper.mm"], srcs = ["MPPMetalHelper.cc"],
hdrs = ["MPPMetalHelper.h"], hdrs = ["MPPMetalHelper.h"],
copts = [ copts = [
"-Wno-shorten-64-to-32", "-Wno-shorten-64-to-32",
"-x objective-c++",
"-fobjc-arc",
], ],
features = ["-layering_check"], features = ["-layering_check"],
visibility = ["//visibility:public"], visibility = ["//visibility:public"],

View File

@ -14,15 +14,14 @@
#import "mediapipe/gpu/MPPMetalHelper.h" #import "mediapipe/gpu/MPPMetalHelper.h"
#import "GTMDefines.h"
#include "absl/log/absl_check.h" #include "absl/log/absl_check.h"
#include "absl/log/absl_log.h" #include "absl/log/absl_log.h"
#include "mediapipe/framework/port/ret_check.h"
#import "mediapipe/gpu/gpu_buffer.h" #import "mediapipe/gpu/gpu_buffer.h"
#import "mediapipe/gpu/gpu_service.h" #import "mediapipe/gpu/gpu_service.h"
#import "mediapipe/gpu/graph_support.h" #import "mediapipe/gpu/graph_support.h"
#import "mediapipe/gpu/metal_shared_resources.h" #import "mediapipe/gpu/metal_shared_resources.h"
#import "GTMDefines.h"
#include "mediapipe/framework/port/ret_check.h"
@interface MPPMetalHelper () { @interface MPPMetalHelper () {
mediapipe::GpuResources* _gpuResources; mediapipe::GpuResources* _gpuResources;
@ -31,7 +30,8 @@
namespace mediapipe { namespace mediapipe {
// Using a C++ class so it can be declared as a friend of LegacyCalculatorSupport. // Using a C++ class so it can be declared as a friend of
// LegacyCalculatorSupport.
class MetalHelperLegacySupport { class MetalHelperLegacySupport {
public: public:
static CalculatorContract* GetCalculatorContract() { static CalculatorContract* GetCalculatorContract() {
@ -61,7 +61,8 @@ class MetalHelperLegacySupport {
- (instancetype)initWithCalculatorContext:(mediapipe::CalculatorContext*)cc { - (instancetype)initWithCalculatorContext:(mediapipe::CalculatorContext*)cc {
if (!cc) return nil; if (!cc) return nil;
return [self initWithGpuResources:&cc->Service(mediapipe::kGpuService).GetObject()]; return [self
initWithGpuResources:&cc->Service(mediapipe::kGpuService).GetObject()];
} }
+ (absl::Status)updateContract:(mediapipe::CalculatorContract*)cc { + (absl::Status)updateContract:(mediapipe::CalculatorContract*)cc {
@ -77,7 +78,8 @@ class MetalHelperLegacySupport {
} }
// Legacy support. // Legacy support.
- (instancetype)initWithSidePackets:(const mediapipe::PacketSet&)inputSidePackets { - (instancetype)initWithSidePackets:
(const mediapipe::PacketSet&)inputSidePackets {
auto cc = mediapipe::MetalHelperLegacySupport::GetCalculatorContext(); auto cc = mediapipe::MetalHelperLegacySupport::GetCalculatorContext();
if (cc) { if (cc) {
ABSL_CHECK_EQ(&inputSidePackets, &cc->InputSidePackets()); ABSL_CHECK_EQ(&inputSidePackets, &cc->InputSidePackets());
@ -85,16 +87,19 @@ class MetalHelperLegacySupport {
} }
// TODO: remove when we can. // TODO: remove when we can.
ABSL_LOG(WARNING) << "CalculatorContext not available. If this calculator uses " ABSL_LOG(WARNING)
"CalculatorBase, call initWithCalculatorContext instead."; << "CalculatorContext not available. If this calculator uses "
"CalculatorBase, call initWithCalculatorContext instead.";
mediapipe::GpuSharedData* gpu_shared = mediapipe::GpuSharedData* gpu_shared =
inputSidePackets.Tag(mediapipe::kGpuSharedTagName).Get<mediapipe::GpuSharedData*>(); inputSidePackets.Tag(mediapipe::kGpuSharedTagName)
.Get<mediapipe::GpuSharedData*>();
return [self initWithGpuResources:gpu_shared->gpu_resources.get()]; return [self initWithGpuResources:gpu_shared->gpu_resources.get()];
} }
// Legacy support. // Legacy support.
+ (absl::Status)setupInputSidePackets:(mediapipe::PacketTypeSet*)inputSidePackets { + (absl::Status)setupInputSidePackets:
(mediapipe::PacketTypeSet*)inputSidePackets {
auto cc = mediapipe::MetalHelperLegacySupport::GetCalculatorContract(); auto cc = mediapipe::MetalHelperLegacySupport::GetCalculatorContract();
if (cc) { if (cc) {
ABSL_CHECK_EQ(inputSidePackets, &cc->InputSidePackets()); ABSL_CHECK_EQ(inputSidePackets, &cc->InputSidePackets());
@ -102,12 +107,12 @@ class MetalHelperLegacySupport {
} }
// TODO: remove when we can. // TODO: remove when we can.
ABSL_LOG(WARNING) << "CalculatorContract not available. If you're calling this " ABSL_LOG(WARNING)
"from a GetContract method, call updateContract instead."; << "CalculatorContract not available. If you're calling this "
"from a GetContract method, call updateContract instead.";
auto id = inputSidePackets->GetId(mediapipe::kGpuSharedTagName, 0); auto id = inputSidePackets->GetId(mediapipe::kGpuSharedTagName, 0);
RET_CHECK(id.IsValid()) RET_CHECK(id.IsValid()) << "A " << mediapipe::kGpuSharedTagName
<< "A " << mediapipe::kGpuSharedTagName << " input side packet is required here.";
<< " input side packet is required here.";
inputSidePackets->Get(id).Set<mediapipe::GpuSharedData*>(); inputSidePackets->Get(id).Set<mediapipe::GpuSharedData*>();
return absl::OkStatus(); return absl::OkStatus();
} }
@ -125,10 +130,12 @@ class MetalHelperLegacySupport {
} }
- (id<MTLCommandBuffer>)commandBuffer { - (id<MTLCommandBuffer>)commandBuffer {
return [_gpuResources->metal_shared().resources().mtlCommandQueue commandBuffer]; return
[_gpuResources->metal_shared().resources().mtlCommandQueue commandBuffer];
} }
- (CVMetalTextureRef)copyCVMetalTextureWithGpuBuffer:(const mediapipe::GpuBuffer&)gpuBuffer - (CVMetalTextureRef)copyCVMetalTextureWithGpuBuffer:
(const mediapipe::GpuBuffer&)gpuBuffer
plane:(size_t)plane { plane:(size_t)plane {
CVPixelBufferRef pixel_buffer = mediapipe::GetCVPixelBufferRef(gpuBuffer); CVPixelBufferRef pixel_buffer = mediapipe::GetCVPixelBufferRef(gpuBuffer);
OSType pixel_format = CVPixelBufferGetPixelFormatType(pixel_buffer); OSType pixel_format = CVPixelBufferGetPixelFormatType(pixel_buffer);
@ -178,41 +185,48 @@ class MetalHelperLegacySupport {
CVMetalTextureRef texture; CVMetalTextureRef texture;
CVReturn err = CVMetalTextureCacheCreateTextureFromImage( CVReturn err = CVMetalTextureCacheCreateTextureFromImage(
NULL, _gpuResources->metal_shared().resources().mtlTextureCache, NULL, _gpuResources->metal_shared().resources().mtlTextureCache,
mediapipe::GetCVPixelBufferRef(gpuBuffer), NULL, metalPixelFormat, width, height, plane, mediapipe::GetCVPixelBufferRef(gpuBuffer), NULL, metalPixelFormat, width,
&texture); height, plane, &texture);
ABSL_CHECK_EQ(err, kCVReturnSuccess); ABSL_CHECK_EQ(err, kCVReturnSuccess);
return texture; return texture;
} }
- (CVMetalTextureRef)copyCVMetalTextureWithGpuBuffer:(const mediapipe::GpuBuffer&)gpuBuffer { - (CVMetalTextureRef)copyCVMetalTextureWithGpuBuffer:
(const mediapipe::GpuBuffer&)gpuBuffer {
return [self copyCVMetalTextureWithGpuBuffer:gpuBuffer plane:0]; return [self copyCVMetalTextureWithGpuBuffer:gpuBuffer plane:0];
} }
- (id<MTLTexture>)metalTextureWithGpuBuffer:(const mediapipe::GpuBuffer&)gpuBuffer { - (id<MTLTexture>)metalTextureWithGpuBuffer:
(const mediapipe::GpuBuffer&)gpuBuffer {
return [self metalTextureWithGpuBuffer:gpuBuffer plane:0]; return [self metalTextureWithGpuBuffer:gpuBuffer plane:0];
} }
- (id<MTLTexture>)metalTextureWithGpuBuffer:(const mediapipe::GpuBuffer&)gpuBuffer - (id<MTLTexture>)metalTextureWithGpuBuffer:
plane:(size_t)plane { (const mediapipe::GpuBuffer&)gpuBuffer
plane:(size_t)plane {
CFHolder<CVMetalTextureRef> cvTexture; CFHolder<CVMetalTextureRef> cvTexture;
cvTexture.adopt([self copyCVMetalTextureWithGpuBuffer:gpuBuffer plane:plane]); cvTexture.adopt([self copyCVMetalTextureWithGpuBuffer:gpuBuffer plane:plane]);
return CVMetalTextureGetTexture(*cvTexture); return CVMetalTextureGetTexture(*cvTexture);
} }
- (mediapipe::GpuBuffer)mediapipeGpuBufferWithWidth:(int)width height:(int)height { - (mediapipe::GpuBuffer)mediapipeGpuBufferWithWidth:(int)width
height:(int)height {
return _gpuResources->gpu_buffer_pool().GetBuffer(width, height); return _gpuResources->gpu_buffer_pool().GetBuffer(width, height);
} }
- (mediapipe::GpuBuffer)mediapipeGpuBufferWithWidth:(int)width - (mediapipe::GpuBuffer)mediapipeGpuBufferWithWidth:(int)width
height:(int)height height:(int)height
format:(mediapipe::GpuBufferFormat)format { format:(mediapipe::GpuBufferFormat)
format {
return _gpuResources->gpu_buffer_pool().GetBuffer(width, height, format); return _gpuResources->gpu_buffer_pool().GetBuffer(width, height, format);
} }
- (id<MTLLibrary>)newLibraryWithResourceName:(NSString*)name error:(NSError * _Nullable *)error { - (id<MTLLibrary>)newLibraryWithResourceName:(NSString*)name
error:(NSError* _Nullable*)error {
return [_gpuResources->metal_shared().resources().mtlDevice return [_gpuResources->metal_shared().resources().mtlDevice
newLibraryWithFile:[[NSBundle bundleForClass:[self class]] pathForResource:name newLibraryWithFile:[[NSBundle bundleForClass:[self class]]
ofType:@"metallib"] pathForResource:name
ofType:@"metallib"]
error:error]; error:error];
} }