From 1c0a1d0aab81bdea369ef912f3f0739cfe84ad81 Mon Sep 17 00:00:00 2001 From: Camillo Lugaresi Date: Tue, 15 Nov 2022 18:32:27 -0800 Subject: [PATCH] Remove shared_ptr member from GlTextureView This only exists to support GlTexture's GetFrame API. It can be moved into GlTexture. PiperOrigin-RevId: 488812896 --- mediapipe/gpu/gl_calculator_helper.h | 8 ++++++-- mediapipe/gpu/gl_calculator_helper_impl_common.cc | 15 +++------------ mediapipe/gpu/gl_texture_view.cc | 1 - mediapipe/gpu/gl_texture_view.h | 4 ---- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/mediapipe/gpu/gl_calculator_helper.h b/mediapipe/gpu/gl_calculator_helper.h index e44523202..0a0cc16cb 100644 --- a/mediapipe/gpu/gl_calculator_helper.h +++ b/mediapipe/gpu/gl_calculator_helper.h @@ -201,9 +201,13 @@ class GlTexture { void Release() { view_ = std::make_shared(); } private: - explicit GlTexture(GlTextureView view) - : view_(std::make_shared(std::move(view))) {} + explicit GlTexture(GlTextureView view, GpuBuffer gpu_buffer) + : gpu_buffer_(std::move(gpu_buffer)), + view_(std::make_shared(std::move(view))) {} friend class GlCalculatorHelperImpl; + // We store the GpuBuffer to support GetFrame, and to ensure that the storage + // outlives the view. + GpuBuffer gpu_buffer_; std::shared_ptr view_; }; diff --git a/mediapipe/gpu/gl_calculator_helper_impl_common.cc b/mediapipe/gpu/gl_calculator_helper_impl_common.cc index c5c028d4f..6311d8905 100644 --- a/mediapipe/gpu/gl_calculator_helper_impl_common.cc +++ b/mediapipe/gpu/gl_calculator_helper_impl_common.cc @@ -101,7 +101,7 @@ GlTexture GlCalculatorHelperImpl::MapGpuBuffer(const GpuBuffer& gpu_buffer, glBindTexture(view.target(), 0); } - return GlTexture(std::move(view)); + return GlTexture(std::move(view), gpu_buffer); } GlTexture GlCalculatorHelperImpl::CreateSourceTexture( @@ -143,7 +143,7 @@ template <> std::unique_ptr GlTexture::GetFrame() const { view_->DoneWriting(); std::shared_ptr view = - view_->gpu_buffer().GetReadView(); + gpu_buffer_.GetReadView(); auto copy = absl::make_unique(); copy->CopyFrom(*view, ImageFrame::kDefaultAlignmentBoundary); return copy; @@ -151,17 +151,8 @@ std::unique_ptr GlTexture::GetFrame() const { template <> std::unique_ptr GlTexture::GetFrame() const { - auto gpu_buffer = view_->gpu_buffer(); -#ifdef __EMSCRIPTEN__ - // When WebGL is used, the GL context may be spontaneously lost which can - // cause GpuBuffer allocations to fail. In that case, return a dummy buffer - // to allow processing of the current frame complete. - if (!gpu_buffer) { - return std::make_unique(); - } -#endif // __EMSCRIPTEN__ view_->DoneWriting(); - return absl::make_unique(gpu_buffer); + return absl::make_unique(gpu_buffer_); } GlTexture GlCalculatorHelperImpl::CreateDestinationTexture( diff --git a/mediapipe/gpu/gl_texture_view.cc b/mediapipe/gpu/gl_texture_view.cc index 5d1862ddc..cae4039a4 100644 --- a/mediapipe/gpu/gl_texture_view.cc +++ b/mediapipe/gpu/gl_texture_view.cc @@ -7,7 +7,6 @@ void GlTextureView::Release() { if (detach_) detach_(*this); detach_ = nullptr; gl_context_ = nullptr; - gpu_buffer_ = nullptr; plane_ = 0; name_ = 0; width_ = 0; diff --git a/mediapipe/gpu/gl_texture_view.h b/mediapipe/gpu/gl_texture_view.h index 8b47d620b..d6734ed71 100644 --- a/mediapipe/gpu/gl_texture_view.h +++ b/mediapipe/gpu/gl_texture_view.h @@ -43,7 +43,6 @@ class GlTextureView { name_ = other.name_; width_ = other.width_; height_ = other.height_; - gpu_buffer_ = std::move(other.gpu_buffer_); plane_ = other.plane_; detach_ = std::exchange(other.detach_, nullptr); done_writing_ = std::exchange(other.done_writing_, nullptr); @@ -55,7 +54,6 @@ class GlTextureView { int height() const { return height_; } GLenum target() const { return target_; } GLuint name() const { return name_; } - const GpuBuffer& gpu_buffer() const { return *gpu_buffer_; } int plane() const { return plane_; } using DetachFn = std::function; @@ -74,7 +72,6 @@ class GlTextureView { name_(name), width_(width), height_(height), - gpu_buffer_(std::move(gpu_buffer)), plane_(plane), detach_(std::move(detach)), done_writing_(std::move(done_writing)) {} @@ -93,7 +90,6 @@ class GlTextureView { // Note: when scale is not 1, we still give the nominal size of the image. int width_ = 0; int height_ = 0; - std::shared_ptr gpu_buffer_; // using shared_ptr temporarily int plane_ = 0; DetachFn detach_; mutable DoneWritingFn done_writing_;