{"id":2449,"url":"https://patchwork.libcamera.org/api/1.1/patches/2449/?format=json","web_url":"https://patchwork.libcamera.org/patch/2449/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20191223072620.13022-7-paul.elder@ideasonboard.com>","date":"2019-12-23T07:26:20","name":"[libcamera-devel,v3,6/6] v4l2: v4l2-compat: add buffer state tracking to V4L2CameraProxy","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"07261bdd0d57a6981b0a055f80681283592e35f2","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2449/mbox/","series":[{"id":592,"url":"https://patchwork.libcamera.org/api/1.1/series/592/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=592","date":"2019-12-23T07:26:14","name":"V4L2 compatibility layer","version":3,"mbox":"https://patchwork.libcamera.org/series/592/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2449/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2449/checks/","tags":{},"headers":{"Return-Path":"<paul.elder@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2C03F605D0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 23 Dec 2019 08:26:54 +0100 (CET)","from neptunite.amanokami.net (173-16-160-11.client.mchsi.com\n\t[173.16.160.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 1C0DE330;\n\tMon, 23 Dec 2019 08:26:52 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1577086013;\n\tbh=IXYGWPXGfQwEnLjlRtnoBfAV1KHfvNGI+ukb5UFk0EU=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=s8Ul/Kv62iEDGNjpd58l2yckf60TLARF4VnsKRYSqDxN6hsrSXNZZnDqUurT/F28i\n\tcw14NUkZ9REv4YsjWLiy95GSeynhX7ER3PbXBYoPVYISCvb8FwPDoLSwkEflli14pL\n\tNgulvUjey2qT9Fib4Sn0jdN0i+VEBEYW1Tr3cjmI=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 23 Dec 2019 01:26:20 -0600","Message-Id":"<20191223072620.13022-7-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.23.0","In-Reply-To":"<20191223072620.13022-1-paul.elder@ideasonboard.com>","References":"<20191223072620.13022-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 6/6] v4l2: v4l2-compat: add buffer\n\tstate tracking to V4L2CameraProxy","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":"Mon, 23 Dec 2019 07:26:54 -0000"},"content":"Add a way for V4L2CameraProxy to cache the state of all the completed\nbuffers as v4l2_buffers. This reduces the number of cross-thread calls,\nsince the newly added V4L2CameraProxy::updateBuffers(), which goes\nthrough V4L2Camera::completedBuffers(), does not need to be called\nacross the thread boundary.\n\nAlso move the v4l2_buffer flag-setting logic to V4L2CameraProxy.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n\n---\nNew in v3\n---\n src/v4l2/v4l2_camera.cpp       | 36 ++++++---------\n src/v4l2/v4l2_camera.h         |  7 ++-\n src/v4l2/v4l2_camera_proxy.cpp | 80 +++++++++++++++++++++++++++-------\n src/v4l2/v4l2_camera_proxy.h   |  3 ++\n 4 files changed, 84 insertions(+), 42 deletions(-)","diff":"diff --git a/src/v4l2/v4l2_camera.cpp b/src/v4l2/v4l2_camera.cpp\nindex 2d33be9f..403e24f6 100644\n--- a/src/v4l2/v4l2_camera.cpp\n+++ b/src/v4l2/v4l2_camera.cpp\n@@ -70,6 +70,19 @@ void V4L2Camera::getStreamConfig(StreamConfiguration *streamConfig)\n \t*streamConfig = config_->at(0);\n }\n \n+std::vector<FrameMetadata> V4L2Camera::completedBuffers()\n+{\n+\tstd::vector<FrameMetadata> v;\n+\n+\tbufferLock_.lock();\n+\tfor (std::unique_ptr<FrameMetadata> &fmd : completedBuffers_)\n+\t\tv.push_back(*fmd.get());\n+\tcompletedBuffers_.clear();\n+\tbufferLock_.unlock();\n+\n+\treturn v;\n+}\n+\n void V4L2Camera::requestComplete(Request *request)\n {\n \tif (request->status() == Request::RequestCancelled)\n@@ -80,7 +93,7 @@ void V4L2Camera::requestComplete(Request *request)\n \tBuffer *buffer = request->buffers().begin()->second;\n \tstd::unique_ptr<FrameMetadata> fmd =\n \t\tutils::make_unique<FrameMetadata>(buffer);\n-\tcompletedBuffers_.push(std::move(fmd));\n+\tcompletedBuffers_.push_back(std::move(fmd));\n \tbufferLock_.unlock();\n \n \tbufferSema_.release();\n@@ -225,24 +238,3 @@ void V4L2Camera::qbuf(int *ret, unsigned int index)\n \n \t*ret = 0;\n }\n-\n-int V4L2Camera::dqbuf(struct v4l2_buffer *arg, bool nonblock)\n-{\n-\tif (nonblock && !bufferSema_.tryAcquire())\n-\t\treturn -EAGAIN;\n-\telse\n-\t\tbufferSema_.acquire();\n-\n-\tbufferLock_.lock();\n-\tFrameMetadata *fmd = completedBuffers_.front().get();\n-\tcompletedBuffers_.pop();\n-\tbufferLock_.unlock();\n-\n-\targ->bytesused = fmd->bytesused();\n-\targ->field = V4L2_FIELD_NONE;\n-\targ->timestamp.tv_sec = fmd->timestamp() / 1000000000;\n-\targ->timestamp.tv_usec = fmd->timestamp() % 1000000;\n-\targ->sequence = fmd->sequence();\n-\n-\treturn 0;\n-}\ndiff --git a/src/v4l2/v4l2_camera.h b/src/v4l2/v4l2_camera.h\nindex 13418b6b..43ab8d02 100644\n--- a/src/v4l2/v4l2_camera.h\n+++ b/src/v4l2/v4l2_camera.h\n@@ -11,7 +11,6 @@\n #include <deque>\n #include <linux/videodev2.h>\n #include <mutex>\n-#include <queue>\n \n #include <libcamera/buffer.h>\n #include <libcamera/camera.h>\n@@ -51,6 +50,7 @@ public:\n \tvoid open(int *ret);\n \tvoid close(int *ret);\n \tvoid getStreamConfig(StreamConfiguration *streamConfig);\n+\tstd::vector<FrameMetadata> completedBuffers();\n \n \tvoid mmap(void **ret, unsigned int index);\n \n@@ -63,8 +63,8 @@ public:\n \tvoid streamOff(int *ret);\n \n \tvoid qbuf(int *ret, unsigned int index);\n-\tint dqbuf(struct v4l2_buffer *arg, bool nonblock);\n \n+\tSemaphore bufferSema_;\n private:\n \tvoid requestComplete(Request *request);\n \n@@ -74,11 +74,10 @@ private:\n \tunsigned int bufferCount_;\n \tbool isRunning_;\n \n-\tSemaphore bufferSema_;\n \tstd::mutex bufferLock_;\n \n \tstd::deque<std::unique_ptr<Request>> pendingRequests_;\n-\tstd::queue<std::unique_ptr<FrameMetadata>> completedBuffers_;\n+\tstd::deque<std::unique_ptr<FrameMetadata>> completedBuffers_;\n };\n \n #endif /* __V4L2_CAMERA_H__ */\ndiff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp\nindex b0acd477..4e303500 100644\n--- a/src/v4l2/v4l2_camera_proxy.cpp\n+++ b/src/v4l2/v4l2_camera_proxy.cpp\n@@ -101,6 +101,9 @@ void *V4L2CameraProxy::mmap(void *addr, size_t length, int prot, int flags,\n \tvoid *val;\n \tvcam_->invokeMethod(&V4L2Camera::mmap, ConnectionTypeBlocking,\n \t\t\t    &val, index);\n+\n+\tbuffers_[index].flags |= V4L2_BUF_FLAG_MAPPED;\n+\n \treturn val;\n }\n \n@@ -173,6 +176,35 @@ void V4L2CameraProxy::querycap(std::shared_ptr<Camera> camera)\n \tmemset(capabilities_.reserved, 0, sizeof(capabilities_.reserved));\n }\n \n+void V4L2CameraProxy::updateBuffers()\n+{\n+\tstd::vector<FrameMetadata> completedBuffers = vcam_->completedBuffers();\n+\tfor (FrameMetadata &fmd : completedBuffers) {\n+\t\t/* \\todo is this index valid if the buffer status != success? */\n+\t\tstruct v4l2_buffer &buf = buffers_[fmd.index()];\n+\n+\t\tswitch (fmd.status()) {\n+\t\tcase Buffer::Status::BufferSuccess:\n+\t\t\tbuf.index = fmd.index();\n+\t\t\tbuf.bytesused = fmd.bytesused();\n+\t\t\tbuf.field = V4L2_FIELD_NONE;\n+\t\t\tbuf.timestamp.tv_sec = fmd.timestamp() / 1000000000;\n+\t\t\tbuf.timestamp.tv_usec = fmd.timestamp() % 1000000;\n+\t\t\tbuf.sequence = fmd.sequence();\n+\n+\t\t\tbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;\n+\t\t\tbuf.length = curV4L2Format_.fmt.pix.sizeimage;\n+\t\t\tbuf.memory = V4L2_MEMORY_MMAP;\n+\t\t\tbuf.m.offset = buf.index * curV4L2Format_.fmt.pix.sizeimage;\n+\t\t\tbreak;\n+\t\tcase Buffer::Status::BufferError:\n+\t\t\tbuf.flags |= V4L2_BUF_FLAG_ERROR;\n+\t\tdefault:\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+}\n+\n int V4L2CameraProxy::vidioc_querycap(struct v4l2_capability *arg)\n {\n \tLOG(V4L2Compat, Debug) << \"Servicing vidioc_querycap\";\n@@ -344,13 +376,21 @@ int V4L2CameraProxy::vidioc_querybuf(struct v4l2_buffer *arg)\n \t    arg->index >= stream->buffers().size())\n \t\treturn -EINVAL;\n \n-\tunsigned int index = arg->index;\n-\tmemset(arg, 0, sizeof(*arg));\n-\targ->index = index;\n-\targ->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;\n-\targ->length = curV4L2Format_.fmt.pix.sizeimage;\n-\targ->memory = V4L2_MEMORY_MMAP;\n-\targ->m.offset = arg->index * curV4L2Format_.fmt.pix.sizeimage;\n+\t/* \\todo make updateBuffers() get only one buffer? */\n+\tupdateBuffers();\n+\n+\tif (buffers_.size() <= arg->index) {\n+\t\tstruct v4l2_buffer buf;\n+\t\tbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;\n+\t\tbuf.length = curV4L2Format_.fmt.pix.sizeimage;\n+\t\tbuf.memory = V4L2_MEMORY_MMAP;\n+\t\tbuf.m.offset = arg->index * curV4L2Format_.fmt.pix.sizeimage;\n+\n+\t\tbuffers_.resize(arg->index + 1);\n+\t\tbuffers_[arg->index] = buf;\n+\t}\n+\n+\tmemcpy(arg, &buffers_[arg->index], sizeof(*arg));\n \n \treturn 0;\n }\n@@ -388,19 +428,23 @@ int V4L2CameraProxy::vidioc_dqbuf(struct v4l2_buffer *arg)\n \t    !validateMemoryType(arg->memory))\n \t\treturn -EINVAL;\n \n-\targ->index = currentBuf_;\n-\tcurrentBuf_ = (currentBuf_ + 1) % bufferCount_;\n+\tif (nonBlocking_ && !vcam_->bufferSema_.tryAcquire())\n+\t\treturn -EAGAIN;\n+\telse\n+\t\tvcam_->bufferSema_.acquire();\n \n-\tint ret = vcam_->dqbuf(arg, nonBlocking_);\n-\tif (ret < 0)\n-\t\treturn ret;\n+\tupdateBuffers();\n \n-\targ->flags &= ~V4L2_BUF_FLAG_QUEUED;\n-\targ->flags |= V4L2_BUF_FLAG_DONE;\n+\tmemcpy(arg, &buffers_[arg->index], sizeof(*arg));\n \n-\targ->length = sizeimage_;\n+\tstruct v4l2_buffer &buf = buffers_[arg->index];\n+\targ->index = currentBuf_;\n+\tcurrentBuf_ = (currentBuf_ + 1) % bufferCount_;\n+\tbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;\n+\tbuf.flags |= V4L2_BUF_FLAG_DONE;\n+\tbuf.length = sizeimage_;\n \n-\treturn ret;\n+\treturn 0;\n }\n \n int V4L2CameraProxy::vidioc_streamon(int *arg)\n@@ -426,6 +470,10 @@ int V4L2CameraProxy::vidioc_streamoff(int *arg)\n \tint ret;\n \tvcam_->invokeMethod(&V4L2Camera::streamOff,\n \t\t\t    ConnectionTypeBlocking, &ret);\n+\n+\tfor (struct v4l2_buffer &buf : buffers_)\n+\t\tbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;\n+\n \treturn ret;\n }\n \ndiff --git a/src/v4l2/v4l2_camera_proxy.h b/src/v4l2/v4l2_camera_proxy.h\nindex 51fdbe19..19688717 100644\n--- a/src/v4l2/v4l2_camera_proxy.h\n+++ b/src/v4l2/v4l2_camera_proxy.h\n@@ -40,6 +40,7 @@ private:\n \tvoid setFmtFromConfig(StreamConfiguration &streamConfig);\n \tunsigned int calculateSizeImage(StreamConfiguration &streamConfig);\n \tvoid querycap(std::shared_ptr<Camera> camera);\n+\tvoid updateBuffers();\n \n \tint vidioc_querycap(struct v4l2_capability *arg);\n \tint vidioc_enum_fmt(struct v4l2_fmtdesc *arg);\n@@ -64,6 +65,8 @@ private:\n \tunsigned int currentBuf_;\n \tunsigned int sizeimage_;\n \n+\tstd::vector<struct v4l2_buffer> buffers_;\n+\n \tstd::unique_ptr<V4L2Camera> vcam_;\n };\n \n","prefixes":["libcamera-devel","v3","6/6"]}