From 8c5e88c38c6ef5c9864016b90dfc3ae49cc11ddb Mon Sep 17 00:00:00 2001 From: Hou Qi Date: Fri, 2 Sep 2022 17:10:12 +0800 Subject: [PATCH 05/17] V4L2Buffer: Allocate correct v4l2 buffers for queues For single plane queue, need to fill v4l2_planes_[0] with correct size quried from v4l2 driver. Upstream-Status: Inappropriate [NXP specific] --- media/gpu/v4l2/v4l2_device.cc | 45 ++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/media/gpu/v4l2/v4l2_device.cc b/media/gpu/v4l2/v4l2_device.cc index 8194d8646637c..9614eb8e71668 100644 --- a/media/gpu/v4l2/v4l2_device.cc +++ b/media/gpu/v4l2/v4l2_device.cc @@ -210,24 +210,30 @@ V4L2Buffer::V4L2Buffer(scoped_refptr device, DCHECK(V4L2_TYPE_IS_MULTIPLANAR(type)); DCHECK_LE(format.fmt.pix_mp.num_planes, std::size(v4l2_planes_)); - memset(&v4l2_buffer_, 0, sizeof(v4l2_buffer_)); - memset(v4l2_planes_, 0, sizeof(v4l2_planes_)); - v4l2_buffer_.m.planes = v4l2_planes_; - // Just in case we got more planes than we want. - v4l2_buffer_.length = - std::min(static_cast(format.fmt.pix_mp.num_planes), - std::size(v4l2_planes_)); + if (V4L2_TYPE_IS_MULTIPLANAR(type)) { + memset(&v4l2_buffer_, 0, sizeof(v4l2_buffer_)); + memset(v4l2_planes_, 0, sizeof(v4l2_planes_)); + v4l2_buffer_.m.planes = v4l2_planes_; + // Just in case we got more planes than we want. + v4l2_buffer_.length = + std::min(static_cast(format.fmt.pix_mp.num_planes), + std::size(v4l2_planes_)); + } v4l2_buffer_.index = buffer_id; v4l2_buffer_.type = type; v4l2_buffer_.memory = memory; - plane_mappings_.resize(v4l2_buffer_.length); + plane_mappings_.resize(V4L2_TYPE_IS_MULTIPLANAR(type) ? v4l2_buffer_.length : 1); } V4L2Buffer::~V4L2Buffer() { if (v4l2_buffer_.memory == V4L2_MEMORY_MMAP) { - for (size_t i = 0; i < plane_mappings_.size(); i++) - if (plane_mappings_[i] != nullptr) - device_->Munmap(plane_mappings_[i], v4l2_buffer_.m.planes[i].length); + for (size_t i = 0; i < plane_mappings_.size(); i++) { + if (plane_mappings_[i] != nullptr) { + unsigned int length = V4L2_TYPE_IS_MULTIPLANAR(v4l2_buffer_.type) ? + v4l2_buffer_.m.planes[i].length : v4l2_buffer_.length; + device_->Munmap(plane_mappings_[i], length); + } + } } } @@ -238,6 +244,13 @@ bool V4L2Buffer::Query() { return false; } + if (!V4L2_TYPE_IS_MULTIPLANAR(v4l2_buffer_.type)) { + v4l2_planes_[0].bytesused = v4l2_buffer_.bytesused; + v4l2_planes_[0].length = v4l2_buffer_.length; + v4l2_planes_[0].data_offset = 0; + memcpy (&v4l2_planes_[0].m, &v4l2_buffer_.m, sizeof (v4l2_buffer_.m)); + } + DCHECK(plane_mappings_.size() == v4l2_buffer_.length); return true; @@ -260,9 +273,13 @@ void* V4L2Buffer::GetPlaneMapping(const size_t plane) { return nullptr; } - p = device_->Mmap(nullptr, v4l2_buffer_.m.planes[plane].length, + unsigned int length = V4L2_TYPE_IS_MULTIPLANAR(v4l2_buffer_.type) ? + v4l2_buffer_.m.planes[plane].length : v4l2_planes_[plane].length; + unsigned int mem_offset = V4L2_TYPE_IS_MULTIPLANAR(v4l2_buffer_.type) ? + v4l2_buffer_.m.planes[plane].m.mem_offset : v4l2_planes_[plane].m.mem_offset; + p = device_->Mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, - v4l2_buffer_.m.planes[plane].m.mem_offset); + mem_offset); if (p == MAP_FAILED) { VPLOGF(1) << "mmap() failed: "; return nullptr; @@ -1131,7 +1148,7 @@ size_t V4L2Queue::AllocateBuffers(size_t count, enum v4l2_memory memory) { VQLOGF(1) << "Cannot get format."; return 0; } - planes_count_ = format->fmt.pix_mp.num_planes; + planes_count_ = V4L2_TYPE_IS_MULTIPLANAR(format->type) ? format->fmt.pix_mp.num_planes : 1; DCHECK_LE(planes_count_, static_cast(VIDEO_MAX_PLANES)); struct v4l2_requestbuffers reqbufs; -- 2.17.1