From patchwork Sat Mar 14 23:57:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 3097 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BC5D962940 for ; Sun, 15 Mar 2020 00:57:41 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 4BD8F10CA for ; Sun, 15 Mar 2020 00:57:41 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1584230261; bh=ZKmchvz4kyDX2o0XrP57at1LLXhlVJEcZoIsoDsPCVk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=mCp1LaFILALfTP5vvFygx6oaUKa39tx67Q95xmgJlTQr3xUasEhIjMxatZ+H3zfSy 6PX/zODZ5+VSjUD2mH2TIsc7xyrJtVRYlrdoqPt3xg2JyLminVT0clk6o/eUsxySmM C7Mt7edFmB0BoAIuaW4IFHE2ovLRsfz13LcLEOlU= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sun, 15 Mar 2020 01:57:23 +0200 Message-Id: <20200314235728.15495-5-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200314235728.15495-1-laurent.pinchart@ideasonboard.com> References: <20200314235728.15495-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 4/9] libcamera: v4l2_videodevice: Refactor allocateBuffers() X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 14 Mar 2020 23:57:42 -0000 Move the buffer creation code out of allocateBuffers() to a createBuffers() function. This prepare for the rework of buffer export and will avoid code duplication. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- src/libcamera/include/v4l2_videodevice.h | 4 +- src/libcamera/v4l2_videodevice.cpp | 68 +++++++++++++----------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h index 26f5e5917716..358d89e57414 100644 --- a/src/libcamera/include/v4l2_videodevice.h +++ b/src/libcamera/include/v4l2_videodevice.h @@ -228,7 +228,9 @@ private: int setSelection(unsigned int target, Rectangle *rect); int requestBuffers(unsigned int count, enum v4l2_memory memoryType); - std::unique_ptr createBuffer(const struct v4l2_buffer &buf); + int createBuffers(unsigned int count, + std::vector> *buffers); + std::unique_ptr createBuffer(unsigned int index); FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane); void bufferAvailable(EventNotifier *notifier); diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 6911ab024fd7..d02b02ef77d6 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -1052,61 +1052,65 @@ int V4L2VideoDevice::requestBuffers(unsigned int count, */ int V4L2VideoDevice::allocateBuffers(unsigned int count, std::vector> *buffers) +{ + int ret = createBuffers(count, buffers); + if (ret < 0) + return ret; + + cache_ = new V4L2BufferCache(*buffers); + memoryType_ = V4L2_MEMORY_MMAP; + + return ret; +} + +int V4L2VideoDevice::createBuffers(unsigned int count, + std::vector> *buffers) { if (cache_) { LOG(V4L2, Error) << "Buffers already allocated"; return -EINVAL; } - memoryType_ = V4L2_MEMORY_MMAP; - int ret = requestBuffers(count, V4L2_MEMORY_MMAP); if (ret < 0) return ret; for (unsigned i = 0; i < count; ++i) { - struct v4l2_buffer buf = {}; - struct v4l2_plane planes[VIDEO_MAX_PLANES] = {}; - - buf.index = i; - buf.type = bufferType_; - buf.memory = memoryType_; - buf.length = ARRAY_SIZE(planes); - buf.m.planes = planes; - - ret = ioctl(VIDIOC_QUERYBUF, &buf); - if (ret < 0) { - LOG(V4L2, Error) - << "Unable to query buffer " << i << ": " - << strerror(-ret); - goto err_buf; - } - - std::unique_ptr buffer = createBuffer(buf); + std::unique_ptr buffer = createBuffer(i); if (!buffer) { LOG(V4L2, Error) << "Unable to create buffer"; - ret = -EINVAL; - goto err_buf; + + requestBuffers(0, V4L2_MEMORY_MMAP); + buffers->clear(); + + return -EINVAL; } buffers->push_back(std::move(buffer)); } - cache_ = new V4L2BufferCache(*buffers); - return count; +} -err_buf: - requestBuffers(0, V4L2_MEMORY_MMAP); +std::unique_ptr V4L2VideoDevice::createBuffer(unsigned int index) +{ + struct v4l2_plane v4l2Planes[VIDEO_MAX_PLANES] = {}; + struct v4l2_buffer buf = {}; - buffers->clear(); + buf.index = index; + buf.type = bufferType_; + buf.memory = V4L2_MEMORY_MMAP; + buf.length = ARRAY_SIZE(v4l2Planes); + buf.m.planes = v4l2Planes; - return ret; -} + int ret = ioctl(VIDIOC_QUERYBUF, &buf); + if (ret < 0) { + LOG(V4L2, Error) + << "Unable to query buffer " << index << ": " + << strerror(-ret); + return nullptr; + } -std::unique_ptr -V4L2VideoDevice::createBuffer(const struct v4l2_buffer &buf) -{ const bool multiPlanar = V4L2_TYPE_IS_MULTIPLANAR(buf.type); const unsigned int numPlanes = multiPlanar ? buf.length : 1;