Implement CVPixelBufferRef access as a view.

PiperOrigin-RevId: 488798216
This commit is contained in:
Camillo Lugaresi 2022-11-15 17:08:37 -08:00 committed by Copybara-Service
parent 4bda012bba
commit b308c0dd5e
3 changed files with 43 additions and 3 deletions

View File

@ -93,8 +93,11 @@ internal::GpuBufferStorage& GpuBuffer::GetStorageForViewOrDie(
#if !MEDIAPIPE_DISABLE_GPU #if !MEDIAPIPE_DISABLE_GPU
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER #if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
CVPixelBufferRef GetCVPixelBufferRef(const GpuBuffer& buffer) { CVPixelBufferRef GetCVPixelBufferRef(const GpuBuffer& buffer) {
auto p = buffer.internal_storage<GpuBufferStorageCvPixelBuffer>(); if (buffer.GetStorageForView(
if (p) return **p; kTypeId<internal::ViewProvider<CVPixelBufferRef>>,
/*for_writing=*/false) != nullptr) {
return *buffer.GetReadView<CVPixelBufferRef>();
}
return nullptr; return nullptr;
} }
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER #endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER

View File

@ -179,6 +179,10 @@ class GpuBuffer {
// This is mutable because view methods that do not change the contents may // This is mutable because view methods that do not change the contents may
// still need to allocate new storages. // still need to allocate new storages.
mutable std::vector<std::shared_ptr<internal::GpuBufferStorage>> storages_; mutable std::vector<std::shared_ptr<internal::GpuBufferStorage>> storages_;
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
friend CVPixelBufferRef GetCVPixelBufferRef(const GpuBuffer& buffer);
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
}; };
inline bool GpuBuffer::operator==(std::nullptr_t other) const { inline bool GpuBuffer::operator==(std::nullptr_t other) const {

View File

@ -12,10 +12,27 @@ namespace mediapipe {
class GlContext; class GlContext;
namespace internal {
template <>
class ViewProvider<CVPixelBufferRef> {
public:
virtual ~ViewProvider() = default;
virtual CFHolder<CVPixelBufferRef> GetReadView(
internal::types<CVPixelBufferRef>,
std::shared_ptr<GpuBuffer> gpu_buffer) const = 0;
virtual CFHolder<CVPixelBufferRef> GetWriteView(
internal::types<CVPixelBufferRef>,
std::shared_ptr<GpuBuffer> gpu_buffer) = 0;
};
} // namespace internal
class GpuBufferStorageCvPixelBuffer class GpuBufferStorageCvPixelBuffer
: public internal::GpuBufferStorageImpl< : public internal::GpuBufferStorageImpl<
GpuBufferStorageCvPixelBuffer, internal::ViewProvider<GlTextureView>, GpuBufferStorageCvPixelBuffer, internal::ViewProvider<GlTextureView>,
internal::ViewProvider<ImageFrame>>, internal::ViewProvider<ImageFrame>,
internal::ViewProvider<CVPixelBufferRef>>,
public CFHolder<CVPixelBufferRef> { public CFHolder<CVPixelBufferRef> {
public: public:
using CFHolder<CVPixelBufferRef>::CFHolder; using CFHolder<CVPixelBufferRef>::CFHolder;
@ -44,6 +61,12 @@ class GpuBufferStorageCvPixelBuffer
std::shared_ptr<ImageFrame> GetWriteView( std::shared_ptr<ImageFrame> GetWriteView(
internal::types<ImageFrame>, internal::types<ImageFrame>,
std::shared_ptr<GpuBuffer> gpu_buffer) override; std::shared_ptr<GpuBuffer> gpu_buffer) override;
CFHolder<CVPixelBufferRef> GetReadView(
internal::types<CVPixelBufferRef>,
std::shared_ptr<GpuBuffer> gpu_buffer) const override;
CFHolder<CVPixelBufferRef> GetWriteView(
internal::types<CVPixelBufferRef>,
std::shared_ptr<GpuBuffer> gpu_buffer) override;
private: private:
GlTextureView GetTexture(std::shared_ptr<GpuBuffer> gpu_buffer, int plane, GlTextureView GetTexture(std::shared_ptr<GpuBuffer> gpu_buffer, int plane,
@ -51,6 +74,16 @@ class GpuBufferStorageCvPixelBuffer
void ViewDoneWriting(const GlTextureView& view); void ViewDoneWriting(const GlTextureView& view);
}; };
inline CFHolder<CVPixelBufferRef> GpuBufferStorageCvPixelBuffer::GetReadView(
internal::types<CVPixelBufferRef>,
std::shared_ptr<GpuBuffer> gpu_buffer) const {
return *this;
}
inline CFHolder<CVPixelBufferRef> GpuBufferStorageCvPixelBuffer::GetWriteView(
internal::types<CVPixelBufferRef>, std::shared_ptr<GpuBuffer> gpu_buffer) {
return *this;
}
namespace internal { namespace internal {
// These functions enable backward-compatible construction of a GpuBuffer from // These functions enable backward-compatible construction of a GpuBuffer from
// CVPixelBufferRef without having to expose that type in the main GpuBuffer // CVPixelBufferRef without having to expose that type in the main GpuBuffer