MultiPool options header refactoring

Passing MultiPool options to the base pool factories means that we don't have to specialize which options we pass to them.

PiperOrigin-RevId: 488782861
This commit is contained in:
Camillo Lugaresi 2022-11-15 16:02:32 -08:00 committed by Copybara-Service
parent 7ef3185ecb
commit 267476657d
6 changed files with 86 additions and 34 deletions

View File

@ -369,6 +369,7 @@ cc_library(
}), }),
deps = [ deps = [
":gpu_buffer_format", ":gpu_buffer_format",
":multi_pool",
":pixel_buffer_pool_util", ":pixel_buffer_pool_util",
"//mediapipe/framework/port:logging", "//mediapipe/framework/port:logging",
"//mediapipe/objc:CFHolder", "//mediapipe/objc:CFHolder",
@ -604,6 +605,7 @@ cc_library(
":gl_texture_buffer", ":gl_texture_buffer",
":gpu_buffer", ":gpu_buffer",
":gpu_shared_data_header", ":gpu_shared_data_header",
":multi_pool",
"//mediapipe/framework:calculator_context", "//mediapipe/framework:calculator_context",
"//mediapipe/framework:calculator_node", "//mediapipe/framework:calculator_node",
"//mediapipe/framework/port:logging", "//mediapipe/framework/port:logging",
@ -612,6 +614,11 @@ cc_library(
], ],
) )
cc_library(
name = "multi_pool",
hdrs = ["multi_pool.h"],
)
cc_library( cc_library(
name = "gpu_buffer_multi_pool", name = "gpu_buffer_multi_pool",
srcs = ["gpu_buffer_multi_pool.cc"], srcs = ["gpu_buffer_multi_pool.cc"],
@ -639,6 +646,7 @@ cc_library(
":gl_base", ":gl_base",
":gpu_buffer", ":gpu_buffer",
":gpu_shared_data_header", ":gpu_shared_data_header",
":multi_pool",
"//mediapipe/framework:calculator_context", "//mediapipe/framework:calculator_context",
"//mediapipe/framework:calculator_node", "//mediapipe/framework:calculator_node",
"//mediapipe/framework/port:logging", "//mediapipe/framework/port:logging",

View File

@ -24,6 +24,7 @@
#include "CoreFoundation/CFBase.h" #include "CoreFoundation/CFBase.h"
#include "mediapipe/gpu/gpu_buffer_format.h" #include "mediapipe/gpu/gpu_buffer_format.h"
#include "mediapipe/gpu/multi_pool.h"
#include "mediapipe/gpu/pixel_buffer_pool_util.h" #include "mediapipe/gpu/pixel_buffer_pool_util.h"
#include "mediapipe/objc/CFHolder.h" #include "mediapipe/objc/CFHolder.h"
@ -34,6 +35,16 @@ class CvPixelBufferPoolWrapper {
CvPixelBufferPoolWrapper(int width, int height, GpuBufferFormat format, CvPixelBufferPoolWrapper(int width, int height, GpuBufferFormat format,
CFTimeInterval maxAge, CFTimeInterval maxAge,
std::function<void(void)> flush_texture_caches); std::function<void(void)> flush_texture_caches);
static std::shared_ptr<CvPixelBufferPoolWrapper> Create(
int width, int height, GpuBufferFormat format,
const MultiPoolOptions& options,
std::function<void(void)> flush_texture_caches = nullptr) {
return std::make_shared<CvPixelBufferPoolWrapper>(
width, height, format, options.max_inactive_buffer_age,
flush_texture_caches);
}
CFHolder<CVPixelBufferRef> GetBuffer(); CFHolder<CVPixelBufferRef> GetBuffer();
int GetBufferCount() const { return count_; } int GetBufferCount() const { return count_; }

View File

@ -23,6 +23,7 @@
#include "absl/synchronization/mutex.h" #include "absl/synchronization/mutex.h"
#include "mediapipe/gpu/gl_texture_buffer.h" #include "mediapipe/gpu/gl_texture_buffer.h"
#include "mediapipe/gpu/multi_pool.h"
namespace mediapipe { namespace mediapipe {
@ -40,6 +41,12 @@ class GlTextureBufferPool
new GlTextureBufferPool(width, height, format, keep_count)); new GlTextureBufferPool(width, height, format, keep_count));
} }
static std::shared_ptr<GlTextureBufferPool> Create(
int width, int height, GpuBufferFormat format,
const MultiPoolOptions& options) {
return Create(width, height, format, options.keep_count);
}
// Obtains a buffers. May either be reused or created anew. // Obtains a buffers. May either be reused or created anew.
// A GlContext must be current when this is called. // A GlContext must be current when this is called.
GlTextureBufferSharedPtr GetBuffer(); GlTextureBufferSharedPtr GetBuffer();

View File

@ -23,24 +23,17 @@
namespace mediapipe { namespace mediapipe {
std::shared_ptr<GpuBufferMultiPool::SimplePool>
GpuBufferMultiPool::MakeSimplePool(const GpuBufferMultiPool::BufferSpec& spec,
const MultiPoolOptions& options) {
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER #if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
return CvPixelBufferPoolWrapper::Create(spec.width, spec.height, spec.format,
std::shared_ptr<GpuBufferMultiPool::SimplePool> options, flush_platform_caches_);
GpuBufferMultiPool::MakeSimplePool(const GpuBufferMultiPool::BufferSpec& spec) {
return std::make_shared<CvPixelBufferPoolWrapper>(
spec.width, spec.height, spec.format, options_.max_inactive_buffer_age,
flush_platform_caches_);
}
#else #else
std::shared_ptr<GpuBufferMultiPool::SimplePool>
GpuBufferMultiPool::MakeSimplePool(const BufferSpec& spec) {
return GlTextureBufferPool::Create(spec.width, spec.height, spec.format, return GlTextureBufferPool::Create(spec.width, spec.height, spec.format,
options_.keep_count); options);
}
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER #endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
}
std::shared_ptr<GpuBufferMultiPool::SimplePool> GpuBufferMultiPool::RequestPool( std::shared_ptr<GpuBufferMultiPool::SimplePool> GpuBufferMultiPool::RequestPool(
const BufferSpec& spec) { const BufferSpec& spec) {
@ -51,7 +44,7 @@ std::shared_ptr<GpuBufferMultiPool::SimplePool> GpuBufferMultiPool::RequestPool(
pool = pool =
cache_.Lookup(spec, [this](const BufferSpec& spec, int request_count) { cache_.Lookup(spec, [this](const BufferSpec& spec, int request_count) {
return (request_count >= options_.min_requests_before_pool) return (request_count >= options_.min_requests_before_pool)
? MakeSimplePool(spec) ? MakeSimplePool(spec, options_)
: nullptr; : nullptr;
}); });
evicted = cache_.Evict(options_.max_pool_count, evicted = cache_.Evict(options_.max_pool_count,

View File

@ -25,6 +25,7 @@
#include "absl/hash/hash.h" #include "absl/hash/hash.h"
#include "absl/synchronization/mutex.h" #include "absl/synchronization/mutex.h"
#include "mediapipe/gpu/gpu_buffer.h" #include "mediapipe/gpu/gpu_buffer.h"
#include "mediapipe/gpu/multi_pool.h"
#include "mediapipe/util/resource_cache.h" #include "mediapipe/util/resource_cache.h"
#ifdef __APPLE__ #ifdef __APPLE__
@ -42,24 +43,6 @@ namespace mediapipe {
struct GpuSharedData; struct GpuSharedData;
class CvPixelBufferPoolWrapper; class CvPixelBufferPoolWrapper;
struct MultiPoolOptions {
// Keep this many buffers allocated for a given frame size.
int keep_count = 2;
// The maximum size of the GpuBufferMultiPool. When the limit is reached, the
// oldest BufferSpec will be dropped.
int max_pool_count = 10;
// Time in seconds after which an inactive buffer can be dropped from the
// pool. Currently only used with CVPixelBufferPool.
float max_inactive_buffer_age = 0.25;
// Skip allocating a buffer pool until at least this many requests have been
// made for a given BufferSpec.
int min_requests_before_pool = 2;
// Do a deeper flush every this many requests.
int request_count_scrub_interval = 50;
};
static constexpr MultiPoolOptions kDefaultMultiPoolOptions;
class GpuBufferMultiPool { class GpuBufferMultiPool {
public: public:
GpuBufferMultiPool(MultiPoolOptions options = kDefaultMultiPoolOptions) GpuBufferMultiPool(MultiPoolOptions options = kDefaultMultiPoolOptions)
@ -98,7 +81,10 @@ class GpuBufferMultiPool {
using SimplePool = GlTextureBufferPool; using SimplePool = GlTextureBufferPool;
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER #endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
std::shared_ptr<SimplePool> MakeSimplePool(const BufferSpec& spec); std::shared_ptr<SimplePool> MakeSimplePool(
const GpuBufferMultiPool::BufferSpec& spec,
const MultiPoolOptions& options);
// 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 CreateBufferWithoutPool. // pool, in which case the caller should invoke CreateBufferWithoutPool.

View File

@ -0,0 +1,47 @@
// Copyright 2019 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This class lets calculators allocate GpuBuffers of various sizes, caching
// and reusing them as needed. It does so by automatically creating and using
// platform-specific buffer pools for the requested sizes.
//
// This class is not meant to be used directly by calculators, but is instead
// used by GlCalculatorHelper to allocate buffers.
#ifndef MEDIAPIPE_GPU_MULTI_POOL_H_
#define MEDIAPIPE_GPU_MULTI_POOL_H_
namespace mediapipe {
struct MultiPoolOptions {
// Keep this many buffers allocated for a given frame size.
int keep_count = 2;
// The maximum size of the GpuBufferMultiPool. When the limit is reached, the
// oldest BufferSpec will be dropped.
int max_pool_count = 10;
// Time in seconds after which an inactive buffer can be dropped from the
// pool. Currently only used with CVPixelBufferPool.
float max_inactive_buffer_age = 0.25;
// Skip allocating a buffer pool until at least this many requests have been
// made for a given BufferSpec.
int min_requests_before_pool = 2;
// Do a deeper flush every this many requests.
int request_count_scrub_interval = 50;
};
static constexpr MultiPoolOptions kDefaultMultiPoolOptions;
} // namespace mediapipe
#endif // MEDIAPIPE_GPU_MULTI_POOL_H_