Skip to content

Commit

Permalink
Use cc_library for DrishtiMetalHelper
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 576230898
  • Loading branch information
schmidt-sebastian authored and copybara-github committed Oct 24, 2023
1 parent 905a18c commit c698414
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
6 changes: 4 additions & 2 deletions mediapipe/gpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -863,12 +863,14 @@ cc_library(
}),
)

objc_library(
cc_library(
name = "MPPMetalHelper",
srcs = ["MPPMetalHelper.mm"],
srcs = ["MPPMetalHelper.cc"],
hdrs = ["MPPMetalHelper.h"],
copts = [
"-Wno-shorten-64-to-32",
"-x objective-c++",
"-fobjc-arc",
],
features = ["-layering_check"],
visibility = ["//visibility:public"],
Expand Down
72 changes: 43 additions & 29 deletions mediapipe/gpu/MPPMetalHelper.mm → mediapipe/gpu/MPPMetalHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@

#import "mediapipe/gpu/MPPMetalHelper.h"

#import "GTMDefines.h"
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "mediapipe/framework/port/ret_check.h"
#import "mediapipe/gpu/gpu_buffer.h"
#import "mediapipe/gpu/gpu_service.h"
#import "mediapipe/gpu/graph_support.h"
#import "mediapipe/gpu/metal_shared_resources.h"
#import "GTMDefines.h"

#include "mediapipe/framework/port/ret_check.h"

@interface MPPMetalHelper () {
mediapipe::GpuResources* _gpuResources;
Expand All @@ -31,7 +30,8 @@ @interface MPPMetalHelper () {

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 {
public:
static CalculatorContract* GetCalculatorContract() {
Expand Down Expand Up @@ -61,7 +61,8 @@ - (instancetype)initWithGpuSharedData:(mediapipe::GpuSharedData*)gpuShared {

- (instancetype)initWithCalculatorContext:(mediapipe::CalculatorContext*)cc {
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 {
Expand All @@ -77,37 +78,41 @@ - (instancetype)initWithCalculatorContext:(mediapipe::CalculatorContext*)cc {
}

// Legacy support.
- (instancetype)initWithSidePackets:(const mediapipe::PacketSet&)inputSidePackets {
- (instancetype)initWithSidePackets:
(const mediapipe::PacketSet&)inputSidePackets {
auto cc = mediapipe::MetalHelperLegacySupport::GetCalculatorContext();
if (cc) {
ABSL_CHECK_EQ(&inputSidePackets, &cc->InputSidePackets());
return [self initWithCalculatorContext:cc];
}

// TODO: remove when we can.
ABSL_LOG(WARNING) << "CalculatorContext not available. If this calculator uses "
"CalculatorBase, call initWithCalculatorContext instead.";
ABSL_LOG(WARNING)
<< "CalculatorContext not available. If this calculator uses "
"CalculatorBase, call initWithCalculatorContext instead.";
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()];
}

// Legacy support.
+ (absl::Status)setupInputSidePackets:(mediapipe::PacketTypeSet*)inputSidePackets {
+ (absl::Status)setupInputSidePackets:
(mediapipe::PacketTypeSet*)inputSidePackets {
auto cc = mediapipe::MetalHelperLegacySupport::GetCalculatorContract();
if (cc) {
ABSL_CHECK_EQ(inputSidePackets, &cc->InputSidePackets());
return [self updateContract:cc];
}

// TODO: remove when we can.
ABSL_LOG(WARNING) << "CalculatorContract not available. If you're calling this "
"from a GetContract method, call updateContract instead.";
ABSL_LOG(WARNING)
<< "CalculatorContract not available. If you're calling this "
"from a GetContract method, call updateContract instead.";
auto id = inputSidePackets->GetId(mediapipe::kGpuSharedTagName, 0);
RET_CHECK(id.IsValid())
<< "A " << mediapipe::kGpuSharedTagName
<< " input side packet is required here.";
RET_CHECK(id.IsValid()) << "A " << mediapipe::kGpuSharedTagName
<< " input side packet is required here.";
inputSidePackets->Get(id).Set<mediapipe::GpuSharedData*>();
return absl::OkStatus();
}
Expand All @@ -125,10 +130,12 @@ - (CVMetalTextureCacheRef)mtlTextureCache {
}

- (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 {
CVPixelBufferRef pixel_buffer = mediapipe::GetCVPixelBufferRef(gpuBuffer);
OSType pixel_format = CVPixelBufferGetPixelFormatType(pixel_buffer);
Expand Down Expand Up @@ -178,41 +185,48 @@ - (CVMetalTextureRef)copyCVMetalTextureWithGpuBuffer:(const mediapipe::GpuBuffer
CVMetalTextureRef texture;
CVReturn err = CVMetalTextureCacheCreateTextureFromImage(
NULL, _gpuResources->metal_shared().resources().mtlTextureCache,
mediapipe::GetCVPixelBufferRef(gpuBuffer), NULL, metalPixelFormat, width, height, plane,
&texture);
mediapipe::GetCVPixelBufferRef(gpuBuffer), NULL, metalPixelFormat, width,
height, plane, &texture);
ABSL_CHECK_EQ(err, kCVReturnSuccess);
return texture;
}

- (CVMetalTextureRef)copyCVMetalTextureWithGpuBuffer:(const mediapipe::GpuBuffer&)gpuBuffer {
- (CVMetalTextureRef)copyCVMetalTextureWithGpuBuffer:
(const mediapipe::GpuBuffer&)gpuBuffer {
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];
}

- (id<MTLTexture>)metalTextureWithGpuBuffer:(const mediapipe::GpuBuffer&)gpuBuffer
plane:(size_t)plane {
- (id<MTLTexture>)metalTextureWithGpuBuffer:
(const mediapipe::GpuBuffer&)gpuBuffer
plane:(size_t)plane {
CFHolder<CVMetalTextureRef> cvTexture;
cvTexture.adopt([self copyCVMetalTextureWithGpuBuffer:gpuBuffer plane:plane]);
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);
}

- (mediapipe::GpuBuffer)mediapipeGpuBufferWithWidth:(int)width
height:(int)height
format:(mediapipe::GpuBufferFormat)format {
height:(int)height
format:(mediapipe::GpuBufferFormat)
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
newLibraryWithFile:[[NSBundle bundleForClass:[self class]] pathForResource:name
ofType:@"metallib"]
newLibraryWithFile:[[NSBundle bundleForClass:[self class]]
pathForResource:name
ofType:@"metallib"]
error:error];
}

Expand Down

0 comments on commit c698414

Please sign in to comment.