From 267476657d18598dc993dc6bb7f5f084a951d8ff Mon Sep 17 00:00:00 2001 From: Camillo Lugaresi Date: Tue, 15 Nov 2022 16:02:32 -0800 Subject: [PATCH] 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 --- mediapipe/gpu/BUILD | 8 ++++ mediapipe/gpu/cv_pixel_buffer_pool_wrapper.h | 11 +++++ mediapipe/gpu/gl_texture_buffer_pool.h | 7 +++ mediapipe/gpu/gpu_buffer_multi_pool.cc | 23 ++++------ mediapipe/gpu/gpu_buffer_multi_pool.h | 24 +++------- mediapipe/gpu/multi_pool.h | 47 ++++++++++++++++++++ 6 files changed, 86 insertions(+), 34 deletions(-) create mode 100644 mediapipe/gpu/multi_pool.h diff --git a/mediapipe/gpu/BUILD b/mediapipe/gpu/BUILD index 2f06fe1d5..b94623ca5 100644 --- a/mediapipe/gpu/BUILD +++ b/mediapipe/gpu/BUILD @@ -369,6 +369,7 @@ cc_library( }), deps = [ ":gpu_buffer_format", + ":multi_pool", ":pixel_buffer_pool_util", "//mediapipe/framework/port:logging", "//mediapipe/objc:CFHolder", @@ -604,6 +605,7 @@ cc_library( ":gl_texture_buffer", ":gpu_buffer", ":gpu_shared_data_header", + ":multi_pool", "//mediapipe/framework:calculator_context", "//mediapipe/framework:calculator_node", "//mediapipe/framework/port:logging", @@ -612,6 +614,11 @@ cc_library( ], ) +cc_library( + name = "multi_pool", + hdrs = ["multi_pool.h"], +) + cc_library( name = "gpu_buffer_multi_pool", srcs = ["gpu_buffer_multi_pool.cc"], @@ -639,6 +646,7 @@ cc_library( ":gl_base", ":gpu_buffer", ":gpu_shared_data_header", + ":multi_pool", "//mediapipe/framework:calculator_context", "//mediapipe/framework:calculator_node", "//mediapipe/framework/port:logging", diff --git a/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.h b/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.h index 9d9328ca1..185ba37c6 100644 --- a/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.h +++ b/mediapipe/gpu/cv_pixel_buffer_pool_wrapper.h @@ -24,6 +24,7 @@ #include "CoreFoundation/CFBase.h" #include "mediapipe/gpu/gpu_buffer_format.h" +#include "mediapipe/gpu/multi_pool.h" #include "mediapipe/gpu/pixel_buffer_pool_util.h" #include "mediapipe/objc/CFHolder.h" @@ -34,6 +35,16 @@ class CvPixelBufferPoolWrapper { CvPixelBufferPoolWrapper(int width, int height, GpuBufferFormat format, CFTimeInterval maxAge, std::function flush_texture_caches); + + static std::shared_ptr Create( + int width, int height, GpuBufferFormat format, + const MultiPoolOptions& options, + std::function flush_texture_caches = nullptr) { + return std::make_shared( + width, height, format, options.max_inactive_buffer_age, + flush_texture_caches); + } + CFHolder GetBuffer(); int GetBufferCount() const { return count_; } diff --git a/mediapipe/gpu/gl_texture_buffer_pool.h b/mediapipe/gpu/gl_texture_buffer_pool.h index cd755b4aa..fee46915e 100644 --- a/mediapipe/gpu/gl_texture_buffer_pool.h +++ b/mediapipe/gpu/gl_texture_buffer_pool.h @@ -23,6 +23,7 @@ #include "absl/synchronization/mutex.h" #include "mediapipe/gpu/gl_texture_buffer.h" +#include "mediapipe/gpu/multi_pool.h" namespace mediapipe { @@ -40,6 +41,12 @@ class GlTextureBufferPool new GlTextureBufferPool(width, height, format, keep_count)); } + static std::shared_ptr 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. // A GlContext must be current when this is called. GlTextureBufferSharedPtr GetBuffer(); diff --git a/mediapipe/gpu/gpu_buffer_multi_pool.cc b/mediapipe/gpu/gpu_buffer_multi_pool.cc index 44f1d40df..df228b7dd 100644 --- a/mediapipe/gpu/gpu_buffer_multi_pool.cc +++ b/mediapipe/gpu/gpu_buffer_multi_pool.cc @@ -23,24 +23,17 @@ namespace mediapipe { +std::shared_ptr +GpuBufferMultiPool::MakeSimplePool(const GpuBufferMultiPool::BufferSpec& spec, + const MultiPoolOptions& options) { #if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER - -std::shared_ptr -GpuBufferMultiPool::MakeSimplePool(const GpuBufferMultiPool::BufferSpec& spec) { - return std::make_shared( - spec.width, spec.height, spec.format, options_.max_inactive_buffer_age, - flush_platform_caches_); -} - + return CvPixelBufferPoolWrapper::Create(spec.width, spec.height, spec.format, + options, flush_platform_caches_); #else - -std::shared_ptr -GpuBufferMultiPool::MakeSimplePool(const BufferSpec& spec) { return GlTextureBufferPool::Create(spec.width, spec.height, spec.format, - options_.keep_count); -} - + options); #endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER +} std::shared_ptr GpuBufferMultiPool::RequestPool( const BufferSpec& spec) { @@ -51,7 +44,7 @@ std::shared_ptr GpuBufferMultiPool::RequestPool( pool = cache_.Lookup(spec, [this](const BufferSpec& spec, int request_count) { return (request_count >= options_.min_requests_before_pool) - ? MakeSimplePool(spec) + ? MakeSimplePool(spec, options_) : nullptr; }); evicted = cache_.Evict(options_.max_pool_count, diff --git a/mediapipe/gpu/gpu_buffer_multi_pool.h b/mediapipe/gpu/gpu_buffer_multi_pool.h index 1396bcdb3..3ea299f78 100644 --- a/mediapipe/gpu/gpu_buffer_multi_pool.h +++ b/mediapipe/gpu/gpu_buffer_multi_pool.h @@ -25,6 +25,7 @@ #include "absl/hash/hash.h" #include "absl/synchronization/mutex.h" #include "mediapipe/gpu/gpu_buffer.h" +#include "mediapipe/gpu/multi_pool.h" #include "mediapipe/util/resource_cache.h" #ifdef __APPLE__ @@ -42,24 +43,6 @@ namespace mediapipe { struct GpuSharedData; 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 { public: GpuBufferMultiPool(MultiPoolOptions options = kDefaultMultiPoolOptions) @@ -98,7 +81,10 @@ class GpuBufferMultiPool { using SimplePool = GlTextureBufferPool; #endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER - std::shared_ptr MakeSimplePool(const BufferSpec& spec); + std::shared_ptr MakeSimplePool( + const GpuBufferMultiPool::BufferSpec& spec, + const MultiPoolOptions& options); + // 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 // pool, in which case the caller should invoke CreateBufferWithoutPool. diff --git a/mediapipe/gpu/multi_pool.h b/mediapipe/gpu/multi_pool.h new file mode 100644 index 000000000..e504fc820 --- /dev/null +++ b/mediapipe/gpu/multi_pool.h @@ -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_