Remove std::shared_ptr<GpuBuffer> argument from GetRead/WriteView

PiperOrigin-RevId: 488813004
This commit is contained in:
Camillo Lugaresi 2022-11-15 18:33:04 -08:00 committed by Copybara-Service
parent 1c0a1d0aab
commit 13b4b825d7
8 changed files with 38 additions and 67 deletions

View File

@ -255,9 +255,8 @@ void GlTextureBuffer::WaitForConsumersOnGpu() {
// precisely, on only one GL context.
}
GlTextureView GlTextureBuffer::GetReadView(
internal::types<GlTextureView>, std::shared_ptr<GpuBuffer> gpu_buffer,
int plane) const {
GlTextureView GlTextureBuffer::GetReadView(internal::types<GlTextureView>,
int plane) const {
auto gl_context = GlContext::GetCurrent();
CHECK(gl_context);
CHECK_EQ(plane, 0);
@ -269,13 +268,11 @@ GlTextureView GlTextureBuffer::GetReadView(
DidRead(texture.gl_context()->CreateSyncToken());
};
return GlTextureView(gl_context.get(), target(), name(), width(), height(),
std::move(gpu_buffer), plane, std::move(detach),
nullptr);
plane, std::move(detach), nullptr);
}
GlTextureView GlTextureBuffer::GetWriteView(
internal::types<GlTextureView>, std::shared_ptr<GpuBuffer> gpu_buffer,
int plane) {
GlTextureView GlTextureBuffer::GetWriteView(internal::types<GlTextureView>,
int plane) {
auto gl_context = GlContext::GetCurrent();
CHECK(gl_context);
CHECK_EQ(plane, 0);
@ -286,8 +283,7 @@ GlTextureView GlTextureBuffer::GetWriteView(
GlTextureView::DoneWritingFn done_writing =
[this](const GlTextureView& texture) { ViewDoneWriting(texture); };
return GlTextureView(gl_context.get(), target(), name(), width(), height(),
std::move(gpu_buffer), plane, nullptr,
std::move(done_writing));
plane, nullptr, std::move(done_writing));
}
void GlTextureBuffer::ViewDoneWriting(const GlTextureView& view) {
@ -364,7 +360,7 @@ static std::shared_ptr<GpuBufferStorageImageFrame> ConvertToImageFrame(
absl::make_unique<ImageFrame>(image_format, buf->width(), buf->height(),
ImageFrame::kGlDefaultAlignmentBoundary);
buf->GetProducerContext()->Run([buf, &output] {
auto view = buf->GetReadView(internal::types<GlTextureView>{}, nullptr, 0);
auto view = buf->GetReadView(internal::types<GlTextureView>{}, 0);
ReadTexture(view, buf->format(), output->MutablePixelData(),
output->PixelDataSize());
});
@ -393,9 +389,8 @@ static std::shared_ptr<GpuBufferStorageCvPixelBuffer> ConvertToCvPixelBuffer(
buf->width(), buf->height(), buf->format());
buf->GetProducerContext()->Run([buf, &output] {
TempGlFramebuffer framebuffer;
auto src = buf->GetReadView(internal::types<GlTextureView>{}, nullptr, 0);
auto dst =
output->GetWriteView(internal::types<GlTextureView>{}, nullptr, 0);
auto src = buf->GetReadView(internal::types<GlTextureView>{}, 0);
auto dst = output->GetWriteView(internal::types<GlTextureView>{}, 0);
CopyGlTexture(src, dst);
glFlush();
});

View File

@ -95,10 +95,8 @@ class GlTextureBuffer
GpuBufferFormat format() const { return format_; }
GlTextureView GetReadView(internal::types<GlTextureView>,
std::shared_ptr<GpuBuffer> gpu_buffer,
int plane) const override;
GlTextureView GetWriteView(internal::types<GlTextureView>,
std::shared_ptr<GpuBuffer> gpu_buffer,
int plane) override;
// If this texture is going to be used outside of the context that produced

View File

@ -65,8 +65,8 @@ class GlTextureView {
friend class GpuBufferStorageCvPixelBuffer;
friend class GpuBufferStorageAhwb;
GlTextureView(GlContext* context, GLenum target, GLuint name, int width,
int height, std::shared_ptr<GpuBuffer> gpu_buffer, int plane,
DetachFn detach, DoneWritingFn done_writing)
int height, int plane, DetachFn detach,
DoneWritingFn done_writing)
: gl_context_(context),
target_(target),
name_(name),
@ -108,12 +108,8 @@ class ViewProvider<GlTextureView> {
// the same view implement the same signature.
// Note that we allow different views to have custom signatures, providing
// additional view-specific arguments that may be needed.
virtual GlTextureView GetReadView(types<GlTextureView>,
std::shared_ptr<GpuBuffer> gpu_buffer,
int plane) const = 0;
virtual GlTextureView GetWriteView(types<GlTextureView>,
std::shared_ptr<GpuBuffer> gpu_buffer,
int plane) = 0;
virtual GlTextureView GetReadView(types<GlTextureView>, int plane) const = 0;
virtual GlTextureView GetWriteView(types<GlTextureView>, int plane) = 0;
};
} // namespace internal

View File

@ -106,8 +106,7 @@ class GpuBuffer {
template <class View, class... Args>
decltype(auto) GetReadView(Args... args) const {
return GetViewProviderOrDie<View>(false).GetReadView(
internal::types<View>{}, std::make_shared<GpuBuffer>(*this),
std::forward<Args>(args)...);
internal::types<View>{}, std::forward<Args>(args)...);
}
// Gets a write view of the specified type. The arguments depend on the
@ -115,8 +114,7 @@ class GpuBuffer {
template <class View, class... Args>
decltype(auto) GetWriteView(Args... args) {
return GetViewProviderOrDie<View>(true).GetWriteView(
internal::types<View>{}, std::make_shared<GpuBuffer>(*this),
std::forward<Args>(args)...);
internal::types<View>{}, std::forward<Args>(args)...);
}
// Attempts to access an underlying storage object of the specified type.

View File

@ -26,8 +26,7 @@ GpuBufferStorageCvPixelBuffer::GpuBufferStorageCvPixelBuffer(
}
GlTextureView GpuBufferStorageCvPixelBuffer::GetTexture(
std::shared_ptr<GpuBuffer> gpu_buffer, int plane,
GlTextureView::DoneWritingFn done_writing) const {
int plane, GlTextureView::DoneWritingFn done_writing) const {
CVReturn err;
auto gl_context = GlContext::GetCurrent();
CHECK(gl_context);
@ -60,33 +59,30 @@ GlTextureView GpuBufferStorageCvPixelBuffer::GetTexture(
cv_texture.adopt(cv_texture_temp);
return GlTextureView(
gl_context.get(), CVOpenGLESTextureGetTarget(*cv_texture),
CVOpenGLESTextureGetName(*cv_texture), width(), height(),
std::move(gpu_buffer), plane,
CVOpenGLESTextureGetName(*cv_texture), width(), height(), plane,
[cv_texture](mediapipe::GlTextureView&) { /* only retains cv_texture */ },
done_writing);
#endif // TARGET_OS_OSX
}
GlTextureView GpuBufferStorageCvPixelBuffer::GetReadView(
internal::types<GlTextureView>, std::shared_ptr<GpuBuffer> gpu_buffer,
int plane) const {
return GetTexture(std::move(gpu_buffer), plane, nullptr);
internal::types<GlTextureView>, int plane) const {
return GetTexture(plane, nullptr);
}
GlTextureView GpuBufferStorageCvPixelBuffer::GetWriteView(
internal::types<GlTextureView>, std::shared_ptr<GpuBuffer> gpu_buffer,
int plane) {
return GetTexture(
std::move(gpu_buffer), plane,
[this](const mediapipe::GlTextureView& view) { ViewDoneWriting(view); });
internal::types<GlTextureView>, int plane) {
return GetTexture(plane, [this](const mediapipe::GlTextureView& view) {
ViewDoneWriting(view);
});
}
std::shared_ptr<const ImageFrame> GpuBufferStorageCvPixelBuffer::GetReadView(
internal::types<ImageFrame>, std::shared_ptr<GpuBuffer> gpu_buffer) const {
internal::types<ImageFrame>) const {
return CreateImageFrameForCVPixelBuffer(**this);
}
std::shared_ptr<ImageFrame> GpuBufferStorageCvPixelBuffer::GetWriteView(
internal::types<ImageFrame>, std::shared_ptr<GpuBuffer> gpu_buffer) {
internal::types<ImageFrame>) {
return CreateImageFrameForCVPixelBuffer(**this);
}

View File

@ -19,11 +19,9 @@ class ViewProvider<CVPixelBufferRef> {
public:
virtual ~ViewProvider() = default;
virtual CFHolder<CVPixelBufferRef> GetReadView(
internal::types<CVPixelBufferRef>,
std::shared_ptr<GpuBuffer> gpu_buffer) const = 0;
internal::types<CVPixelBufferRef>) const = 0;
virtual CFHolder<CVPixelBufferRef> GetWriteView(
internal::types<CVPixelBufferRef>,
std::shared_ptr<GpuBuffer> gpu_buffer) = 0;
internal::types<CVPixelBufferRef>) = 0;
};
} // namespace internal
@ -50,37 +48,30 @@ class GpuBufferStorageCvPixelBuffer
CVPixelBufferGetPixelFormatType(**this));
}
GlTextureView GetReadView(internal::types<GlTextureView>,
std::shared_ptr<GpuBuffer> gpu_buffer,
int plane) const override;
GlTextureView GetWriteView(internal::types<GlTextureView>,
std::shared_ptr<GpuBuffer> gpu_buffer,
int plane) override;
std::shared_ptr<const ImageFrame> GetReadView(
internal::types<ImageFrame>,
std::shared_ptr<GpuBuffer> gpu_buffer) const override;
internal::types<ImageFrame>) const override;
std::shared_ptr<ImageFrame> GetWriteView(
internal::types<ImageFrame>,
std::shared_ptr<GpuBuffer> gpu_buffer) override;
internal::types<ImageFrame>) override;
CFHolder<CVPixelBufferRef> GetReadView(
internal::types<CVPixelBufferRef>,
std::shared_ptr<GpuBuffer> gpu_buffer) const override;
internal::types<CVPixelBufferRef>) const override;
CFHolder<CVPixelBufferRef> GetWriteView(
internal::types<CVPixelBufferRef>,
std::shared_ptr<GpuBuffer> gpu_buffer) override;
internal::types<CVPixelBufferRef>) override;
private:
GlTextureView GetTexture(std::shared_ptr<GpuBuffer> gpu_buffer, int plane,
GlTextureView GetTexture(int plane,
GlTextureView::DoneWritingFn done_writing) const;
void ViewDoneWriting(const GlTextureView& view);
};
inline CFHolder<CVPixelBufferRef> GpuBufferStorageCvPixelBuffer::GetReadView(
internal::types<CVPixelBufferRef>,
std::shared_ptr<GpuBuffer> gpu_buffer) const {
internal::types<CVPixelBufferRef>) const {
return *this;
}
inline CFHolder<CVPixelBufferRef> GpuBufferStorageCvPixelBuffer::GetWriteView(
internal::types<CVPixelBufferRef>, std::shared_ptr<GpuBuffer> gpu_buffer) {
internal::types<CVPixelBufferRef>) {
return *this;
}

View File

@ -29,13 +29,11 @@ class GpuBufferStorageImageFrame
std::shared_ptr<const ImageFrame> image_frame() const { return image_frame_; }
std::shared_ptr<ImageFrame> image_frame() { return image_frame_; }
std::shared_ptr<const ImageFrame> GetReadView(
internal::types<ImageFrame>,
std::shared_ptr<GpuBuffer> gpu_buffer) const override {
internal::types<ImageFrame>) const override {
return image_frame_;
}
std::shared_ptr<ImageFrame> GetWriteView(
internal::types<ImageFrame>,
std::shared_ptr<GpuBuffer> gpu_buffer) override {
internal::types<ImageFrame>) override {
return image_frame_;
}

View File

@ -12,9 +12,8 @@ class ViewProvider<ImageFrame> {
public:
virtual ~ViewProvider() = default;
virtual std::shared_ptr<const ImageFrame> GetReadView(
types<ImageFrame>, std::shared_ptr<GpuBuffer> gpu_buffer) const = 0;
virtual std::shared_ptr<ImageFrame> GetWriteView(
types<ImageFrame>, std::shared_ptr<GpuBuffer> gpu_buffer) = 0;
types<ImageFrame>) const = 0;
virtual std::shared_ptr<ImageFrame> GetWriteView(types<ImageFrame>) = 0;
};
} // namespace internal