From a4fe3eb0941e9571bbc4ade95147c4959f8aa67f Mon Sep 17 00:00:00 2001 From: Camillo Lugaresi Date: Tue, 15 Nov 2022 15:59:01 -0800 Subject: [PATCH] Add CreateBufferWithoutPool method to base pools This may not fit exactly in a pool class, but it makes it easy for the multi-pool to find the appropriate method by depending only on the type of the base pool. For the CVPixelBuffer case, the buffer type is CFHolder, and it seems even less appropriate to specialize that template to add such a method there. An alternative would be to allow defining a creation function separately. PiperOrigin-RevId: 488782054 --- mediapipe/gpu/BUILD | 3 ++- mediapipe/gpu/cv_pixel_buffer_pool_wrapper.cc | 17 ++++++++++++-- mediapipe/gpu/cv_pixel_buffer_pool_wrapper.h | 7 ++++-- mediapipe/gpu/gl_texture_buffer_pool.h | 5 +++++ mediapipe/gpu/gpu_buffer_multi_pool.cc | 22 +++++-------------- 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/mediapipe/gpu/BUILD b/mediapipe/gpu/BUILD index 26df167c4..2f06fe1d5 100644 --- a/mediapipe/gpu/BUILD +++ b/mediapipe/gpu/BUILD @@ -368,10 +368,11 @@ cc_library( ], }), deps = [ - ":gpu_buffer", + ":gpu_buffer_format", ":pixel_buffer_pool_util", "//mediapipe/framework/port:logging", "//mediapipe/objc:CFHolder", + "//mediapipe/objc:util", "@com_google_absl//absl/synchronization", ], ) diff --git a/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.cc b/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.cc index 3293b0238..c97268307 100644 --- a/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.cc +++ b/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.cc @@ -19,6 +19,7 @@ #include "CoreFoundation/CFBase.h" #include "mediapipe/framework/port/logging.h" #include "mediapipe/objc/CFHolder.h" +#include "mediapipe/objc/util.h" namespace mediapipe { @@ -32,7 +33,8 @@ CvPixelBufferPoolWrapper::CvPixelBufferPoolWrapper(int width, int height, CreateCVPixelBufferPool(width, height, cv_format, 0, maxAge)); } -GpuBuffer CvPixelBufferPoolWrapper::GetBuffer(std::function flush) { +CFHolder CvPixelBufferPoolWrapper::GetBuffer( + std::function flush) { CVPixelBufferRef buffer; int threshold = 1; NSMutableDictionary* auxAttributes = @@ -58,7 +60,7 @@ GpuBuffer CvPixelBufferPoolWrapper::GetBuffer(std::function flush) { } CHECK(!err) << "Error creating pixel buffer: " << err; count_ = threshold; - return GpuBuffer(MakeCFHolderAdopting(buffer)); + return MakeCFHolderAdopting(buffer); } std::string CvPixelBufferPoolWrapper::GetDebugString() const { @@ -68,4 +70,15 @@ std::string CvPixelBufferPoolWrapper::GetDebugString() const { void CvPixelBufferPoolWrapper::Flush() { CVPixelBufferPoolFlush(*pool_, 0); } +CFHolder CvPixelBufferPoolWrapper::CreateBufferWithoutPool( + int width, int height, GpuBufferFormat format) { + OSType cv_format = CVPixelFormatForGpuBufferFormat(format); + CHECK_NE(cv_format, -1) << "unsupported pixel format"; + CVPixelBufferRef buffer; + CVReturn err = + CreateCVPixelBufferWithoutPool(width, height, cv_format, &buffer); + CHECK(!err) << "Error creating pixel buffer: " << err; + return MakeCFHolderAdopting(buffer); +} + } // namespace mediapipe diff --git a/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.h b/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.h index 081df4676..7412b776f 100644 --- a/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.h +++ b/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.h @@ -23,7 +23,7 @@ #define MEDIAPIPE_GPU_CV_PIXEL_BUFFER_POOL_WRAPPER_H_ #include "CoreFoundation/CFBase.h" -#include "mediapipe/gpu/gpu_buffer.h" +#include "mediapipe/gpu/gpu_buffer_format.h" #include "mediapipe/gpu/pixel_buffer_pool_util.h" #include "mediapipe/objc/CFHolder.h" @@ -33,13 +33,16 @@ class CvPixelBufferPoolWrapper { public: CvPixelBufferPoolWrapper(int width, int height, GpuBufferFormat format, CFTimeInterval maxAge); - GpuBuffer GetBuffer(std::function flush); + CFHolder GetBuffer(std::function flush); int GetBufferCount() const { return count_; } std::string GetDebugString() const; void Flush(); + static CFHolder CreateBufferWithoutPool( + int width, int height, GpuBufferFormat format); + private: CFHolder pool_; int count_ = 0; diff --git a/mediapipe/gpu/gl_texture_buffer_pool.h b/mediapipe/gpu/gl_texture_buffer_pool.h index 4dcad305e..cd755b4aa 100644 --- a/mediapipe/gpu/gl_texture_buffer_pool.h +++ b/mediapipe/gpu/gl_texture_buffer_pool.h @@ -51,6 +51,11 @@ class GlTextureBufferPool // This method is meant for testing. std::pair GetInUseAndAvailableCounts(); + static GlTextureBufferSharedPtr CreateBufferWithoutPool( + int width, int height, GpuBufferFormat format) { + return GlTextureBuffer::Create(width, height, format); + } + private: GlTextureBufferPool(int width, int height, GpuBufferFormat format, int keep_count); diff --git a/mediapipe/gpu/gpu_buffer_multi_pool.cc b/mediapipe/gpu/gpu_buffer_multi_pool.cc index 1909d116e..fdff3e692 100644 --- a/mediapipe/gpu/gpu_buffer_multi_pool.cc +++ b/mediapipe/gpu/gpu_buffer_multi_pool.cc @@ -51,19 +51,9 @@ GpuBufferMultiPool::MakeSimplePool(const GpuBufferMultiPool::BufferSpec& spec) { spec.width, spec.height, spec.format, kMaxInactiveBufferAge); } -GpuBuffer GpuBufferMultiPool::GetBufferWithoutPool(const BufferSpec& spec) { - OSType cv_format = CVPixelFormatForGpuBufferFormat(spec.format); - CHECK_NE(cv_format, -1) << "unsupported pixel format"; - CVPixelBufferRef buffer; - CVReturn err = CreateCVPixelBufferWithoutPool(spec.width, spec.height, - cv_format, &buffer); - CHECK(!err) << "Error creating pixel buffer: " << err; - return GpuBuffer(MakeCFHolderAdopting(buffer)); -} - GpuBuffer GpuBufferMultiPool::GetBufferFromSimplePool( BufferSpec spec, GpuBufferMultiPool::SimplePool& pool) { - return pool.GetBuffer(flush_platform_caches_); + return GpuBuffer(pool.GetBuffer(flush_platform_caches_)); } #else @@ -74,11 +64,6 @@ GpuBufferMultiPool::MakeSimplePool(const BufferSpec& spec) { kKeepCount); } -GpuBuffer GpuBufferMultiPool::GetBufferWithoutPool(const BufferSpec& spec) { - return GpuBuffer( - GlTextureBuffer::Create(spec.width, spec.height, spec.format)); -} - GpuBuffer GpuBufferMultiPool::GetBufferFromSimplePool( BufferSpec spec, GpuBufferMultiPool::SimplePool& pool) { return GpuBuffer(pool.GetBuffer()); @@ -117,4 +102,9 @@ GpuBuffer GpuBufferMultiPool::GetBuffer(int width, int height, } } +GpuBuffer GpuBufferMultiPool::GetBufferWithoutPool(const BufferSpec& spec) { + return GpuBuffer(SimplePool::CreateBufferWithoutPool(spec.width, spec.height, + spec.format)); +} + } // namespace mediapipe