From b69f93fca60a06d7117670530eb6f526bb1df9be Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Fri, 2 Jun 2023 12:29:29 -0700 Subject: [PATCH] Internal change PiperOrigin-RevId: 537381138 --- mediapipe/gpu/gl_calculator_helper.cc | 8 ++++++++ mediapipe/gpu/gl_calculator_helper.h | 16 +++++++++++++++- .../gpu/image_frame_to_gpu_buffer_calculator.cc | 7 +++---- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/mediapipe/gpu/gl_calculator_helper.cc b/mediapipe/gpu/gl_calculator_helper.cc index 9b217ddfd..974525a91 100644 --- a/mediapipe/gpu/gl_calculator_helper.cc +++ b/mediapipe/gpu/gl_calculator_helper.cc @@ -211,6 +211,14 @@ GlTexture GlCalculatorHelper::CreateDestinationTexture(int width, int height, return MapGpuBuffer(gpu_buffer, gpu_buffer.GetWriteView(0)); } +GlTexture GlCalculatorHelper::CreateDestinationTexture( + const ImageFrame& image_frame) { + // TODO: ensure buffer pool is used when creating textures out of + // ImageFrame. + GpuBuffer gpu_buffer = GpuBufferCopyingImageFrame(image_frame); + return MapGpuBuffer(gpu_buffer, gpu_buffer.GetWriteView(0)); +} + GlTexture GlCalculatorHelper::CreateSourceTexture( const mediapipe::Image& image) { return CreateSourceTexture(image.GetGpuBuffer()); diff --git a/mediapipe/gpu/gl_calculator_helper.h b/mediapipe/gpu/gl_calculator_helper.h index af897bbe9..c1b94fa82 100644 --- a/mediapipe/gpu/gl_calculator_helper.h +++ b/mediapipe/gpu/gl_calculator_helper.h @@ -135,6 +135,12 @@ class GlCalculatorHelper { // This is deprecated because: 1) it encourages the use of GlTexture as a // long-lived object; 2) it requires copying the ImageFrame's contents, // which may not always be necessary. + // + // WARNING: do NOT use as a destination texture which will be sent to + // downstream calculators as it may lead to synchronization issues. The result + // is meant to be a short-lived object, local to a single calculator and + // single GL thread. Use `CreateDestinationTexture` instead, if you need a + // destination texture. ABSL_DEPRECATED("Use `GpuBufferWithImageFrame`.") GlTexture CreateSourceTexture(const ImageFrame& image_frame); @@ -156,6 +162,14 @@ class GlCalculatorHelper { int output_width, int output_height, GpuBufferFormat format = GpuBufferFormat::kBGRA32); + // Creates a destination texture copying and uploading passed image frame. + // + // WARNING: mind that this functions creates a new texture every time and + // doesn't use MediaPipe's gpu buffer pool. + // TODO: ensure buffer pool is used when creating textures out of + // ImageFrame. + GlTexture CreateDestinationTexture(const ImageFrame& image_frame); + // The OpenGL name of the output framebuffer. GLuint framebuffer() const; @@ -196,7 +210,7 @@ class GlCalculatorHelper { // This class should be the main way to interface with GL memory within a single // calculator. This is the preferred way to utilize the memory pool inside of // the helper, because GlTexture manages efficiently releasing memory back into -// the pool. A GPU backed Image can be extracted from the unerlying +// the pool. A GPU backed Image can be extracted from the underlying // memory. class GlTexture { public: diff --git a/mediapipe/gpu/image_frame_to_gpu_buffer_calculator.cc b/mediapipe/gpu/image_frame_to_gpu_buffer_calculator.cc index 2a8331db8..a741e42a7 100644 --- a/mediapipe/gpu/image_frame_to_gpu_buffer_calculator.cc +++ b/mediapipe/gpu/image_frame_to_gpu_buffer_calculator.cc @@ -71,11 +71,10 @@ absl::Status ImageFrameToGpuBufferCalculator::Process(CalculatorContext* cc) { #else const auto& input = cc->Inputs().Index(0).Get(); helper_.RunInGlContext([this, &input, &cc]() { - auto src = helper_.CreateSourceTexture(input); - auto output = src.GetFrame(); - glFlush(); + GlTexture dst = helper_.CreateDestinationTexture(input); + std::unique_ptr output = dst.GetFrame(); cc->Outputs().Index(0).Add(output.release(), cc->InputTimestamp()); - src.Release(); + dst.Release(); }); #endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER return absl::OkStatus();