{"id":10042,"url":"https://patchwork.libcamera.org/api/1.1/patches/10042/?format=json","web_url":"https://patchwork.libcamera.org/patch/10042/","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":"<20201010095830.134084-2-jacopo@jmondi.org>","date":"2020-10-10T09:58:28","name":"[libcamera-devel,v2,1/3] android: camera_worker: Introduce CameraWorker","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"990a8c577e2482de12b536af8a8671e707880913","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/1.1/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/10042/mbox/","series":[{"id":1372,"url":"https://patchwork.libcamera.org/api/1.1/series/1372/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1372","date":"2020-10-10T09:58:27","name":"android: Introduce CameraWorker","version":2,"mbox":"https://patchwork.libcamera.org/series/1372/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/10042/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/10042/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 89B9FBEEDF\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat, 10 Oct 2020 09:54:39 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C675F609C4;\n\tSat, 10 Oct 2020 11:54:38 +0200 (CEST)","from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net\n\t[217.70.183.194])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D4CD1603C1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 10 Oct 2020 11:54:36 +0200 (CEST)","from uno.lan (93-34-118-233.ip49.fastwebnet.it [93.34.118.233])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay2-d.mail.gandi.net (Postfix) with ESMTPSA id 789EA40002;\n\tSat, 10 Oct 2020 09:54:36 +0000 (UTC)"],"X-Originating-IP":"93.34.118.233","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Sat, 10 Oct 2020 11:58:28 +0200","Message-Id":"<20201010095830.134084-2-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.28.0","In-Reply-To":"<20201010095830.134084-1-jacopo@jmondi.org>","References":"<20201010095830.134084-1-jacopo@jmondi.org>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v2 1/3] android: camera_worker: Introduce\n\tCameraWorker","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The Android camera framework provides for each buffer part of a capture\nrequest an acquisition fence the camera HAL is supposed to wait on\nbefore using the buffer. As the libcamera HAL runs in the camera service\nthread, it is not possible to perform a synchronous wait there.\n\nIntroduce a CameraWorker class that runs an internal thread to wait\non a set of fences before queueing a capture request to the\nlibcamera::Camera.\n\nFences completion is handled through a simple poll, similar in\nimplementation to the sync_wait() function provided by libdrm.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/android/camera_worker.cpp | 122 ++++++++++++++++++++++++++++++++++\n src/android/camera_worker.h   |  63 ++++++++++++++++++\n src/android/meson.build       |   1 +\n 3 files changed, 186 insertions(+)\n create mode 100644 src/android/camera_worker.cpp\n create mode 100644 src/android/camera_worker.h","diff":"diff --git a/src/android/camera_worker.cpp b/src/android/camera_worker.cpp\nnew file mode 100644\nindex 000000000000..682ad1a82386\n--- /dev/null\n+++ b/src/android/camera_worker.cpp\n@@ -0,0 +1,122 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * camera_worker.cpp - Process capture requests on behalf of the Camera HAL\n+ */\n+\n+#include \"camera_worker.h\"\n+\n+#include <errno.h>\n+#include <string.h>\n+#include <sys/poll.h>\n+\n+#include \"camera_device.h\"\n+\n+using namespace libcamera;\n+\n+LOG_DECLARE_CATEGORY(HAL);\n+\n+/*\n+ * \\class CaptureRequest\n+ * \\brief Wrap a libcamera::Request associated with buffers and fences\n+ *\n+ * A CaptureRequest is constructed by the CameraDevice, filled with\n+ * buffers and fences provided by the camera3 framework and then processed\n+ * by the CameraWorker which queues it to the libcamera::Camera after handling\n+ * fences.\n+ */\n+CaptureRequest::CaptureRequest(libcamera::Camera *camera, uint64_t cookie)\n+\t: camera_(camera)\n+{\n+\trequest_ = camera_->createRequest(cookie);\n+}\n+\n+void CaptureRequest::addBuffer(Stream *stream, FrameBuffer *buffer, int fence)\n+{\n+\trequest_->addBuffer(stream, buffer);\n+\tacquireFences_.push_back(fence);\n+}\n+\n+void CaptureRequest::queue()\n+{\n+\tcamera_->queueRequest(request_.get());\n+}\n+\n+/*\n+ * \\class CameraWorker\n+ * \\brief Process a CaptureRequest on an internal thread\n+ *\n+ * The CameraWorker class wraps a Worker that runs on an internal thread\n+ * and schedules processing of CaptureRequest through it.\n+ */\n+CameraWorker::CameraWorker()\n+{\n+\tworker_.moveToThread(&thread_);\n+}\n+\n+void CameraWorker::start()\n+{\n+\tthread_.start();\n+}\n+\n+void CameraWorker::stop()\n+{\n+\tthread_.exit();\n+\tthread_.wait();\n+}\n+\n+void CameraWorker::queueRequest(CaptureRequest *request)\n+{\n+\t/* Async process the request on the worker which runs its own thread. */\n+\tworker_.invokeMethod(&Worker::processRequest, ConnectionTypeQueued,\n+\t\t\t     request);\n+}\n+\n+/*\n+ * \\class CameraWorker::Worker\n+ * \\brief Process a CaptureRequest handling acquisition fences\n+ */\n+int CameraWorker::Worker::waitFence(int fence)\n+{\n+\t/*\n+\t * \\todo Better characterize the timeout. Currently equal to the one\n+\t * used by the Rockchip Camera HAL on ChromeOS.\n+\t */\n+\tconstexpr unsigned int timeoutMs = 300;\n+\tstruct pollfd fds = { fence, POLLIN, 0 };\n+\n+\tdo {\n+\t\tint ret = poll(&fds, 1, timeoutMs);\n+\t\tif (ret == 0)\n+\t\t\treturn -ETIME;\n+\n+\t\tif (ret > 0) {\n+\t\t\tif (fds.revents & (POLLERR | POLLNVAL))\n+\t\t\t\treturn -EINVAL;\n+\n+\t\t\treturn 0;\n+\t\t}\n+\t} while (errno == EINTR || errno == EAGAIN);\n+\n+\treturn -errno;\n+}\n+\n+void CameraWorker::Worker::processRequest(CaptureRequest *request)\n+{\n+\t/* Wait on all fences before queuing the Request. */\n+\tfor (int fence : request->fences()) {\n+\t\tif (fence == -1)\n+\t\t\tcontinue;\n+\n+\t\tint ret = waitFence(fence);\n+\t\tclose(fence);\n+\t\tif (ret < 0) {\n+\t\t\tLOG(HAL, Error) << \"Failed handling fence: \"\n+\t\t\t\t\t<< fence << \": \" << strerror(-ret);\n+\t\t\treturn;\n+\t\t}\n+\t}\n+\n+\trequest->queue();\n+}\ndiff --git a/src/android/camera_worker.h b/src/android/camera_worker.h\nnew file mode 100644\nindex 000000000000..fff5021708d7\n--- /dev/null\n+++ b/src/android/camera_worker.h\n@@ -0,0 +1,63 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * camera_worker.h - Process capture requests on behalf of the Camera HAL\n+ */\n+#ifndef __ANDROID_CAMERA_WORKER_H__\n+#define __ANDROID_CAMERA_WORKER_H__\n+\n+#include <memory>\n+\n+#include <libcamera/buffer.h>\n+#include <libcamera/camera.h>\n+#include <libcamera/object.h>\n+#include <libcamera/request.h>\n+#include <libcamera/stream.h>\n+\n+#include \"libcamera/internal/thread.h\"\n+\n+class CameraDevice;\n+\n+class CaptureRequest\n+{\n+public:\n+\tCaptureRequest(libcamera::Camera *camera, uint64_t cookie);\n+\n+\tconst std::vector<int> &fences() const { return acquireFences_; }\n+\n+\tvoid addBuffer(libcamera::Stream *stream,\n+\t\t       libcamera::FrameBuffer *buffer, int fence);\n+\tvoid queue();\n+\n+private:\n+\tlibcamera::Camera *camera_;\n+\tstd::vector<int> acquireFences_;\n+\tstd::unique_ptr<libcamera::Request> request_;\n+};\n+\n+class CameraWorker\n+{\n+public:\n+\tCameraWorker();\n+\n+\tvoid start();\n+\tvoid stop();\n+\n+\tvoid queueRequest(CaptureRequest *request);\n+\n+private:\n+\tclass Worker : public libcamera::Object\n+\t{\n+\tpublic:\n+\t\tvoid processRequest(CaptureRequest *request);\n+\n+\tprivate:\n+\t\tint waitFence(int fence);\n+\t};\n+\n+\tWorker worker_;\n+\tlibcamera::Thread thread_;\n+};\n+\n+#endif /* __ANDROID_CAMERA_WORKER_H__ */\ndiff --git a/src/android/meson.build b/src/android/meson.build\nindex 802bb89afe57..b2b2293cf62d 100644\n--- a/src/android/meson.build\n+++ b/src/android/meson.build\n@@ -21,6 +21,7 @@ android_hal_sources = files([\n     'camera_metadata.cpp',\n     'camera_ops.cpp',\n     'camera_stream.cpp',\n+    'camera_worker.cpp',\n     'jpeg/encoder_libjpeg.cpp',\n     'jpeg/exif.cpp',\n ])\n","prefixes":["libcamera-devel","v2","1/3"]}