Remove shared_ptr from SimplePool definition

This makes the types more explicit and will help with factoring out platform-specific code.

PiperOrigin-RevId: 488775470
This commit is contained in:
Camillo Lugaresi 2022-11-15 15:30:59 -08:00 committed by Copybara-Service
parent a67069156e
commit 3c71c64be1
2 changed files with 20 additions and 19 deletions

View File

@ -90,8 +90,8 @@ std::string CvPixelBufferPoolWrapper::GetDebugString() const {
void CvPixelBufferPoolWrapper::Flush() { CVPixelBufferPoolFlush(*pool_, 0); } void CvPixelBufferPoolWrapper::Flush() { CVPixelBufferPoolFlush(*pool_, 0); }
GpuBufferMultiPool::SimplePool GpuBufferMultiPool::MakeSimplePool( std::shared_ptr<GpuBufferMultiPool::SimplePool>
const GpuBufferMultiPool::BufferSpec& spec) { GpuBufferMultiPool::MakeSimplePool(const GpuBufferMultiPool::BufferSpec& spec) {
return std::make_shared<CvPixelBufferPoolWrapper>(spec, return std::make_shared<CvPixelBufferPoolWrapper>(spec,
kMaxInactiveBufferAge); kMaxInactiveBufferAge);
} }
@ -123,7 +123,7 @@ void GpuBufferMultiPool::FlushTextureCaches() {
#define FORCE_CONTIGUOUS_PIXEL_BUFFER_ON_IPHONE_SIMULATOR 0 #define FORCE_CONTIGUOUS_PIXEL_BUFFER_ON_IPHONE_SIMULATOR 0
GpuBuffer GpuBufferMultiPool::GetBufferFromSimplePool( GpuBuffer GpuBufferMultiPool::GetBufferFromSimplePool(
BufferSpec spec, const GpuBufferMultiPool::SimplePool& pool) { BufferSpec spec, GpuBufferMultiPool::SimplePool& pool) {
#if TARGET_IPHONE_SIMULATOR && FORCE_CONTIGUOUS_PIXEL_BUFFER_ON_IPHONE_SIMULATOR #if TARGET_IPHONE_SIMULATOR && FORCE_CONTIGUOUS_PIXEL_BUFFER_ON_IPHONE_SIMULATOR
// On the simulator, syncing the texture with the pixelbuffer does not work, // On the simulator, syncing the texture with the pixelbuffer does not work,
// and we have to use glReadPixels. Since GL_UNPACK_ROW_LENGTH is not // and we have to use glReadPixels. Since GL_UNPACK_ROW_LENGTH is not
@ -134,14 +134,14 @@ GpuBuffer GpuBufferMultiPool::GetBufferFromSimplePool(
// pool to give us contiguous data. // pool to give us contiguous data.
return GetBufferWithoutPool(spec); return GetBufferWithoutPool(spec);
#else #else
return pool->GetBuffer([this]() { FlushTextureCaches(); }); return pool.GetBuffer([this]() { FlushTextureCaches(); });
#endif // TARGET_IPHONE_SIMULATOR #endif // TARGET_IPHONE_SIMULATOR
} }
#else #else
GpuBufferMultiPool::SimplePool GpuBufferMultiPool::MakeSimplePool( std::shared_ptr<GpuBufferMultiPool::SimplePool>
const BufferSpec& spec) { GpuBufferMultiPool::MakeSimplePool(const BufferSpec& spec) {
return GlTextureBufferPool::Create(spec.width, spec.height, spec.format, return GlTextureBufferPool::Create(spec.width, spec.height, spec.format,
kKeepCount); kKeepCount);
} }
@ -152,16 +152,16 @@ GpuBuffer GpuBufferMultiPool::GetBufferWithoutPool(const BufferSpec& spec) {
} }
GpuBuffer GpuBufferMultiPool::GetBufferFromSimplePool( GpuBuffer GpuBufferMultiPool::GetBufferFromSimplePool(
BufferSpec spec, const GpuBufferMultiPool::SimplePool& pool) { BufferSpec spec, GpuBufferMultiPool::SimplePool& pool) {
return GpuBuffer(pool->GetBuffer()); return GpuBuffer(pool.GetBuffer());
} }
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER #endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
GpuBufferMultiPool::SimplePool GpuBufferMultiPool::RequestPool( std::shared_ptr<GpuBufferMultiPool::SimplePool> GpuBufferMultiPool::RequestPool(
const BufferSpec& spec) { const BufferSpec& spec) {
SimplePool pool; std::shared_ptr<SimplePool> pool;
std::vector<SimplePool> evicted; std::vector<std::shared_ptr<SimplePool>> evicted;
{ {
absl::MutexLock lock(&mutex_); absl::MutexLock lock(&mutex_);
pool = pool =
@ -180,10 +180,10 @@ GpuBufferMultiPool::SimplePool GpuBufferMultiPool::RequestPool(
GpuBuffer GpuBufferMultiPool::GetBuffer(int width, int height, GpuBuffer GpuBufferMultiPool::GetBuffer(int width, int height,
GpuBufferFormat format) { GpuBufferFormat format) {
BufferSpec key(width, height, format); BufferSpec key(width, height, format);
SimplePool pool = RequestPool(key); std::shared_ptr<SimplePool> pool = RequestPool(key);
if (pool) { if (pool) {
// Note: we release our multipool lock before accessing the simple pool. // Note: we release our multipool lock before accessing the simple pool.
return GetBufferFromSimplePool(key, pool); return GetBufferFromSimplePool(key, *pool);
} else { } else {
return GetBufferWithoutPool(key); return GetBufferWithoutPool(key);
} }

View File

@ -83,22 +83,23 @@ class GpuBufferMultiPool {
private: private:
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER #if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
using SimplePool = std::shared_ptr<CvPixelBufferPoolWrapper>; using SimplePool = CvPixelBufferPoolWrapper;
#else #else
using SimplePool = std::shared_ptr<GlTextureBufferPool>; using SimplePool = GlTextureBufferPool;
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER #endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
SimplePool MakeSimplePool(const BufferSpec& spec); std::shared_ptr<SimplePool> MakeSimplePool(const BufferSpec& spec);
// Requests a simple buffer pool for the given spec. This may return nullptr // Requests a simple buffer pool for the given spec. This may return nullptr
// if we have not yet reached a sufficient number of requests to allocate a // if we have not yet reached a sufficient number of requests to allocate a
// pool, in which case the caller should invoke GetBufferWithoutPool instead // pool, in which case the caller should invoke GetBufferWithoutPool instead
// of GetBufferFromSimplePool. // of GetBufferFromSimplePool.
SimplePool RequestPool(const BufferSpec& spec); std::shared_ptr<SimplePool> RequestPool(const BufferSpec& spec);
GpuBuffer GetBufferFromSimplePool(BufferSpec spec, const SimplePool& pool); GpuBuffer GetBufferFromSimplePool(BufferSpec spec, SimplePool& pool);
GpuBuffer GetBufferWithoutPool(const BufferSpec& spec); GpuBuffer GetBufferWithoutPool(const BufferSpec& spec);
absl::Mutex mutex_; absl::Mutex mutex_;
mediapipe::ResourceCache<BufferSpec, SimplePool, absl::Hash<BufferSpec>> mediapipe::ResourceCache<BufferSpec, std::shared_ptr<SimplePool>,
absl::Hash<BufferSpec>>
cache_ ABSL_GUARDED_BY(mutex_); cache_ ABSL_GUARDED_BY(mutex_);
#ifdef __APPLE__ #ifdef __APPLE__