This CL fixes compilation errors when MEDIAPIPE_USE_WEBGPU is defined on MAC. The errors were caused by the fact that metal was assumed to be always enabled on MAC.

PiperOrigin-RevId: 665018987
This commit is contained in:
MediaPipe Team 2024-08-19 15:51:53 -07:00 committed by Copybara-Service
parent e33940de13
commit bfd933c9a0
6 changed files with 33 additions and 20 deletions

View File

@ -80,7 +80,11 @@
#define MEDIAPIPE_METAL_ENABLED 1
#elif defined(MEDIAPIPE_OSX)
#define MEDIAPIPE_OPENGL_ES_VERSION 0
#if MEDIAPIPE_USE_WEBGPU
#define MEDIAPIPE_METAL_ENABLED 0
#else
#define MEDIAPIPE_METAL_ENABLED 1
#endif // MEDIAPIPE_USE_WEBGPU
#elif defined(__EMSCRIPTEN__)
// WebGL config.
#define MEDIAPIPE_OPENGL_ES_VERSION MEDIAPIPE_OPENGL_ES_30

View File

@ -380,7 +380,7 @@ absl::Status GlContext::FinishInitialization(bool create_thread) {
});
}
GlContext::GlContext() {}
GlContext::GlContext() = default;
GlContext::~GlContext() {
destructing_ = true;

View File

@ -36,9 +36,9 @@
#include "mediapipe/gpu/graph_support.h"
#include "mediapipe/gpu/multi_pool.h"
#if __APPLE__
#if MEDIAPIPE_METAL_ENABLED
#include "mediapipe/gpu/metal_shared_resources.h"
#endif // __APPLE__
#endif // MEDIAPIPE_METAL_ENABLED
namespace mediapipe {
@ -136,7 +136,9 @@ GpuResources::GpuResources(std::shared_ptr<GlContext> gl_context,
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
texture_caches_->RegisterTextureCache(gl_context->cv_texture_cache());
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
#if MEDIAPIPE_METAL_ENABLED
metal_shared_ = std::make_unique<MetalSharedResources>();
#endif // MEDIAPIPE_METAL_ENABLED
#endif // __APPLE__
}

View File

@ -41,9 +41,9 @@
namespace mediapipe {
#ifdef __APPLE__
#if MEDIAPIPE_METAL_ENABLED
class MetalSharedResources;
#endif // defined(__APPLE__)
#endif // MEDIAPIPE_METAL_ENABLED
// TODO: rename to GpuService or GpuManager or something.
class GpuResources {
@ -72,9 +72,9 @@ class GpuResources {
// Shared buffer pool.
GpuBufferMultiPool& gpu_buffer_pool() { return gpu_buffer_pool_; }
#ifdef __APPLE__
#if MEDIAPIPE_METAL_ENABLED
MetalSharedResources& metal_shared() { return *metal_shared_; }
#endif // defined(__APPLE__)§
#endif // MEDIAPIPE_METAL_ENABLED
absl::Status PrepareGpuNode(CalculatorNode* node);
@ -108,9 +108,9 @@ class GpuResources {
// ios_gpu_data, so the declaration order is important.
GpuBufferMultiPool gpu_buffer_pool_;
#ifdef __APPLE__
#if MEDIAPIPE_METAL_ENABLED
std::unique_ptr<MetalSharedResources> metal_shared_;
#endif // defined(__APPLE__)
#endif // MEDIAPIPE_METAL_ENABLED
std::map<std::string, std::shared_ptr<Executor>> named_executors_;
};

View File

@ -14,6 +14,8 @@
#include "mediapipe/objc/util.h"
#import <CoreGraphics/CGImage.h>
#include <cstdint>
#include "absl/base/macros.h"
@ -39,7 +41,8 @@ vImage_CGImageFormat vImageFormatForCVPixelFormat(OSType pixel_format) {
.bitsPerComponent = 8,
.bitsPerPixel = 8,
.colorSpace = CGColorSpaceCreateDeviceGray(),
.bitmapInfo = kCGImageAlphaNone | kCGBitmapByteOrderDefault,
.bitmapInfo = kCGImageAlphaNone |
static_cast<CGBitmapInfo>(kCGBitmapByteOrderDefault),
};
case kCVPixelFormatType_32BGRA:
@ -47,7 +50,8 @@ vImage_CGImageFormat vImageFormatForCVPixelFormat(OSType pixel_format) {
.bitsPerComponent = 8,
.bitsPerPixel = 32,
.colorSpace = NULL,
.bitmapInfo = kCGImageAlphaFirst | kCGBitmapByteOrder32Little,
.bitmapInfo = kCGImageAlphaFirst |
static_cast<CGBitmapInfo>(kCGBitmapByteOrder32Little),
};
case kCVPixelFormatType_32RGBA:
@ -55,7 +59,8 @@ vImage_CGImageFormat vImageFormatForCVPixelFormat(OSType pixel_format) {
.bitsPerComponent = 8,
.bitsPerPixel = 32,
.colorSpace = NULL,
.bitmapInfo = kCGImageAlphaLast | kCGBitmapByteOrderDefault,
.bitmapInfo = kCGImageAlphaLast |
static_cast<CGBitmapInfo>(kCGBitmapByteOrderDefault),
};
default:
@ -498,8 +503,8 @@ absl::Status CreateCGImageFromCVPixelBuffer(CVPixelBufferRef image_buffer,
switch (pixel_format) {
case kCVPixelFormatType_32BGRA:
color_space = CGColorSpaceCreateDeviceRGB();
bitmap_info =
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst;
bitmap_info = static_cast<CGBitmapInfo>(kCGBitmapByteOrder32Little) |
kCGImageAlphaPremultipliedFirst;
break;
case kCVPixelFormatType_OneComponent8:
@ -551,7 +556,8 @@ absl::Status CreateCVPixelBufferFromCGImage(
size_t bytes_per_row = CVPixelBufferGetBytesPerRow(*pixel_buffer);
CGContextRef context = CGBitmapContextCreate(
base_address, width, height, 8, bytes_per_row, color_space,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
static_cast<CGBitmapInfo>(kCGBitmapByteOrder32Little) |
kCGImageAlphaPremultipliedFirst);
CGRect rect = CGRectMake(0, 0, width, height);
CGContextClearRect(context, rect);
CGContextDrawImage(context, rect, image);

View File

@ -32,17 +32,18 @@
/// call, and it must stay locked as long as the vImage_Buffer is in use.
inline vImage_Buffer vImageForCVPixelBuffer(CVPixelBufferRef pixel_buffer) {
return {.data = CVPixelBufferGetBaseAddress(pixel_buffer),
.width = CVPixelBufferGetWidth(pixel_buffer),
.height = CVPixelBufferGetHeight(pixel_buffer),
.width = CVPixelBufferGetWidth(pixel_buffer),
.rowBytes = CVPixelBufferGetBytesPerRow(pixel_buffer)};
}
/// Returns a vImage_Buffer describing the data inside the ImageFrame.
inline vImage_Buffer vImageForImageFrame(const mediapipe::ImageFrame& frame) {
return {.data = (void*)frame.PixelData(),
.width = static_cast<vImagePixelCount>(frame.Width()),
.height = static_cast<vImagePixelCount>(frame.Height()),
.rowBytes = static_cast<size_t>(frame.WidthStep())};
return {
.data = reinterpret_cast<void*>(const_cast<uint8_t*>(frame.PixelData())),
.height = static_cast<vImagePixelCount>(frame.Height()),
.width = static_cast<vImagePixelCount>(frame.Width()),
.rowBytes = static_cast<size_t>(frame.WidthStep())};
}
/// Converts a grayscale image without alpha to BGRA format.