Remove shared_ptr<GpuBuffer> member from GlTextureView

This only exists to support GlTexture's GetFrame API. It can be moved into GlTexture.

PiperOrigin-RevId: 488812896
This commit is contained in:
Camillo Lugaresi 2022-11-15 18:32:27 -08:00 committed by Copybara-Service
parent 767cc2ee3c
commit 1c0a1d0aab
4 changed files with 9 additions and 19 deletions

View File

@ -201,9 +201,13 @@ class GlTexture {
void Release() { view_ = std::make_shared<GlTextureView>(); }
private:
explicit GlTexture(GlTextureView view)
: view_(std::make_shared<GlTextureView>(std::move(view))) {}
explicit GlTexture(GlTextureView view, GpuBuffer gpu_buffer)
: gpu_buffer_(std::move(gpu_buffer)),
view_(std::make_shared<GlTextureView>(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<GlTextureView> view_;
};

View File

@ -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<ImageFrame> GlTexture::GetFrame<ImageFrame>() const {
view_->DoneWriting();
std::shared_ptr<const ImageFrame> view =
view_->gpu_buffer().GetReadView<ImageFrame>();
gpu_buffer_.GetReadView<ImageFrame>();
auto copy = absl::make_unique<ImageFrame>();
copy->CopyFrom(*view, ImageFrame::kDefaultAlignmentBoundary);
return copy;
@ -151,17 +151,8 @@ std::unique_ptr<ImageFrame> GlTexture::GetFrame<ImageFrame>() const {
template <>
std::unique_ptr<GpuBuffer> GlTexture::GetFrame<GpuBuffer>() 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<GpuBuffer>();
}
#endif // __EMSCRIPTEN__
view_->DoneWriting();
return absl::make_unique<GpuBuffer>(gpu_buffer);
return absl::make_unique<GpuBuffer>(gpu_buffer_);
}
GlTexture GlCalculatorHelperImpl::CreateDestinationTexture(

View File

@ -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;

View File

@ -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<void(GlTextureView&)>;
@ -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<GpuBuffer> gpu_buffer_; // using shared_ptr temporarily
int plane_ = 0;
DetachFn detach_;
mutable DoneWritingFn done_writing_;