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<CVPixelBufferRef>, 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
This commit is contained in:
Camillo Lugaresi 2022-11-15 15:59:01 -08:00 committed by Copybara-Service
parent 0d273dd11a
commit a4fe3eb094
5 changed files with 33 additions and 21 deletions

View File

@ -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",
],
)

View File

@ -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<void(void)> flush) {
CFHolder<CVPixelBufferRef> CvPixelBufferPoolWrapper::GetBuffer(
std::function<void(void)> flush) {
CVPixelBufferRef buffer;
int threshold = 1;
NSMutableDictionary* auxAttributes =
@ -58,7 +60,7 @@ GpuBuffer CvPixelBufferPoolWrapper::GetBuffer(std::function<void(void)> 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<CVPixelBufferRef> 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

View File

@ -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<void(void)> flush);
CFHolder<CVPixelBufferRef> GetBuffer(std::function<void(void)> flush);
int GetBufferCount() const { return count_; }
std::string GetDebugString() const;
void Flush();
static CFHolder<CVPixelBufferRef> CreateBufferWithoutPool(
int width, int height, GpuBufferFormat format);
private:
CFHolder<CVPixelBufferPoolRef> pool_;
int count_ = 0;

View File

@ -51,6 +51,11 @@ class GlTextureBufferPool
// This method is meant for testing.
std::pair<int, int> 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);

View File

@ -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