diff --git a/mediapipe/gpu/gpu_buffer_storage.h b/mediapipe/gpu/gpu_buffer_storage.h index 3d872eb6..214f506c 100644 --- a/mediapipe/gpu/gpu_buffer_storage.h +++ b/mediapipe/gpu/gpu_buffer_storage.h @@ -74,13 +74,17 @@ class GpuBufferStorageRegistry { template RegistryToken Register() { - return Register( + return RegisterFactory( [](int width, int height, GpuBufferFormat format) -> std::shared_ptr { return CreateStorage(overload_priority<10>{}, width, height, format); - }, - Storage::GetProviderTypes()); + }); + } + + template + RegistryToken RegisterFactory(F&& factory) { + return Register(factory, Storage::GetProviderTypes()); } template @@ -148,6 +152,13 @@ class GpuBufferStorageImpl : public GpuBufferStorage, public U... { return kHashes; } + // Exposing this as a function allows dependent initializers to call this to + // ensure proper ordering. + static GpuBufferStorageRegistry::RegistryToken RegisterOnce() { + static auto registration = GpuBufferStorageRegistry::Get().Register(); + return registration; + } + private: virtual const void* down_cast(TypeId to) const override { return down_cast_impl(to, types{}); @@ -161,8 +172,7 @@ class GpuBufferStorageImpl : public GpuBufferStorage, public U... { return down_cast_impl(to, types{}); } - inline static auto registration = - GpuBufferStorageRegistry::Get().Register(); + inline static auto registration = RegisterOnce(); using RequireStatics = ForceStaticInstantiation<®istration>; }; diff --git a/mediapipe/gpu/gpu_shared_data_internal.cc b/mediapipe/gpu/gpu_shared_data_internal.cc index 52db8863..91723a7d 100644 --- a/mediapipe/gpu/gpu_shared_data_internal.cc +++ b/mediapipe/gpu/gpu_shared_data_internal.cc @@ -200,4 +200,34 @@ GpuSharedData::GpuSharedData() : GpuSharedData(kPlatformGlContextNone) {} MPPGraphGPUData* GpuResources::ios_gpu_data() { return ios_gpu_data_; } #endif // __APPLE__ +extern const GraphService kGpuService; + +#if !MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER +static std::shared_ptr GetGlTextureBufferFromPool( + int width, int height, GpuBufferFormat format) { + std::shared_ptr texture_buffer; + const auto cc = LegacyCalculatorSupport::Scoped::current(); + + if (cc && cc->Service(kGpuService).IsAvailable()) { + GpuBufferMultiPool* pool = + &cc->Service(kGpuService).GetObject().gpu_buffer_pool(); + // Note that the "gpu_buffer_pool" serves GlTextureBuffers on non-Apple + // platforms. TODO: refactor into storage pools. + texture_buffer = pool->GetBuffer(width, height, format) + .internal_storage(); + } else { + texture_buffer = GlTextureBuffer::Create(width, height, format); + } + return texture_buffer; +} + +static auto kGlTextureBufferPoolRegistration = [] { + // Ensure that the GlTextureBuffer's own factory is already registered, so we + // can override it. + GlTextureBuffer::RegisterOnce(); + return internal::GpuBufferStorageRegistry::Get() + .RegisterFactory(GetGlTextureBufferFromPool); +}(); +#endif // !MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER + } // namespace mediapipe