{"id":3097,"url":"https://patchwork.libcamera.org/api/patches/3097/?format=json","web_url":"https://patchwork.libcamera.org/patch/3097/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200314235728.15495-5-laurent.pinchart@ideasonboard.com>","date":"2020-03-14T23:57:23","name":"[libcamera-devel,4/9] libcamera: v4l2_videodevice: Refactor allocateBuffers()","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"144ab29baad7c2750d05c9c361d8ab6ed15000f5","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/3097/mbox/","series":[{"id":719,"url":"https://patchwork.libcamera.org/api/series/719/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=719","date":"2020-03-14T23:57:19","name":"Simplify buffer management with V4L2 buffer orphaning","version":1,"mbox":"https://patchwork.libcamera.org/series/719/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/3097/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3097/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BC5D962940\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 15 Mar 2020 00:57:41 +0100 (CET)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 4BD8F10CA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 15 Mar 2020 00:57:41 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1584230261;\n\tbh=ZKmchvz4kyDX2o0XrP57at1LLXhlVJEcZoIsoDsPCVk=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=mCp1LaFILALfTP5vvFygx6oaUKa39tx67Q95xmgJlTQr3xUasEhIjMxatZ+H3zfSy\n\t6PX/zODZ5+VSjUD2mH2TIsc7xyrJtVRYlrdoqPt3xg2JyLminVT0clk6o/eUsxySmM\n\tC7Mt7edFmB0BoAIuaW4IFHE2ovLRsfz13LcLEOlU=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 4/9] libcamera: v4l2_videodevice: Refactor\n\tallocateBuffers()","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Sat, 14 Mar 2020 23:57:42 -0000"},"content":"Move the buffer creation code out of allocateBuffers() to a\ncreateBuffers() function. This prepare for the rework of buffer export\nand will avoid code duplication.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/include/v4l2_videodevice.h |  4 +-\n src/libcamera/v4l2_videodevice.cpp       | 68 +++++++++++++-----------\n 2 files changed, 39 insertions(+), 33 deletions(-)","diff":"diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h\nindex 26f5e5917716..358d89e57414 100644\n--- a/src/libcamera/include/v4l2_videodevice.h\n+++ b/src/libcamera/include/v4l2_videodevice.h\n@@ -228,7 +228,9 @@ private:\n \tint setSelection(unsigned int target, Rectangle *rect);\n \n \tint requestBuffers(unsigned int count, enum v4l2_memory memoryType);\n-\tstd::unique_ptr<FrameBuffer> createBuffer(const struct v4l2_buffer &buf);\n+\tint createBuffers(unsigned int count,\n+\t\t\t  std::vector<std::unique_ptr<FrameBuffer>> *buffers);\n+\tstd::unique_ptr<FrameBuffer> createBuffer(unsigned int index);\n \tFileDescriptor exportDmabufFd(unsigned int index, unsigned int plane);\n \n \tvoid bufferAvailable(EventNotifier *notifier);\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 6911ab024fd7..d02b02ef77d6 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -1052,61 +1052,65 @@ int V4L2VideoDevice::requestBuffers(unsigned int count,\n  */\n int V4L2VideoDevice::allocateBuffers(unsigned int count,\n \t\t\t\t     std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n+{\n+\tint ret = createBuffers(count, buffers);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\tcache_ = new V4L2BufferCache(*buffers);\n+\tmemoryType_ = V4L2_MEMORY_MMAP;\n+\n+\treturn ret;\n+}\n+\n+int V4L2VideoDevice::createBuffers(unsigned int count,\n+\t\t\t\t   std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n {\n \tif (cache_) {\n \t\tLOG(V4L2, Error) << \"Buffers already allocated\";\n \t\treturn -EINVAL;\n \t}\n \n-\tmemoryType_ = V4L2_MEMORY_MMAP;\n-\n \tint ret = requestBuffers(count, V4L2_MEMORY_MMAP);\n \tif (ret < 0)\n \t\treturn ret;\n \n \tfor (unsigned i = 0; i < count; ++i) {\n-\t\tstruct v4l2_buffer buf = {};\n-\t\tstruct v4l2_plane planes[VIDEO_MAX_PLANES] = {};\n-\n-\t\tbuf.index = i;\n-\t\tbuf.type = bufferType_;\n-\t\tbuf.memory = memoryType_;\n-\t\tbuf.length = ARRAY_SIZE(planes);\n-\t\tbuf.m.planes = planes;\n-\n-\t\tret = ioctl(VIDIOC_QUERYBUF, &buf);\n-\t\tif (ret < 0) {\n-\t\t\tLOG(V4L2, Error)\n-\t\t\t\t<< \"Unable to query buffer \" << i << \": \"\n-\t\t\t\t<< strerror(-ret);\n-\t\t\tgoto err_buf;\n-\t\t}\n-\n-\t\tstd::unique_ptr<FrameBuffer> buffer = createBuffer(buf);\n+\t\tstd::unique_ptr<FrameBuffer> buffer = createBuffer(i);\n \t\tif (!buffer) {\n \t\t\tLOG(V4L2, Error) << \"Unable to create buffer\";\n-\t\t\tret = -EINVAL;\n-\t\t\tgoto err_buf;\n+\n+\t\t\trequestBuffers(0, V4L2_MEMORY_MMAP);\n+\t\t\tbuffers->clear();\n+\n+\t\t\treturn -EINVAL;\n \t\t}\n \n \t\tbuffers->push_back(std::move(buffer));\n \t}\n \n-\tcache_ = new V4L2BufferCache(*buffers);\n-\n \treturn count;\n+}\n \n-err_buf:\n-\trequestBuffers(0, V4L2_MEMORY_MMAP);\n+std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index)\n+{\n+\tstruct v4l2_plane v4l2Planes[VIDEO_MAX_PLANES] = {};\n+\tstruct v4l2_buffer buf = {};\n \n-\tbuffers->clear();\n+\tbuf.index = index;\n+\tbuf.type = bufferType_;\n+\tbuf.memory = V4L2_MEMORY_MMAP;\n+\tbuf.length = ARRAY_SIZE(v4l2Planes);\n+\tbuf.m.planes = v4l2Planes;\n \n-\treturn ret;\n-}\n+\tint ret = ioctl(VIDIOC_QUERYBUF, &buf);\n+\tif (ret < 0) {\n+\t\tLOG(V4L2, Error)\n+\t\t\t<< \"Unable to query buffer \" << index << \": \"\n+\t\t\t<< strerror(-ret);\n+\t\treturn nullptr;\n+\t}\n \n-std::unique_ptr<FrameBuffer>\n-V4L2VideoDevice::createBuffer(const struct v4l2_buffer &buf)\n-{\n \tconst bool multiPlanar = V4L2_TYPE_IS_MULTIPLANAR(buf.type);\n \tconst unsigned int numPlanes = multiPlanar ? buf.length : 1;\n \n","prefixes":["libcamera-devel","4/9"]}