{"id":14405,"url":"https://patchwork.libcamera.org/api/1.1/patches/14405/?format=json","web_url":"https://patchwork.libcamera.org/patch/14405/","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":"<20211028111520.256612-11-jacopo@jmondi.org>","date":"2021-10-28T11:15:20","name":"[libcamera-devel,10/10] android: Remove CameraWorker","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"3b602ad0068442e70b3ba064da7aba4b3def5a8a","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/14405/mbox/","series":[{"id":2674,"url":"https://patchwork.libcamera.org/api/1.1/series/2674/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2674","date":"2021-10-28T11:15:10","name":"libcamera: Introduce Fence support","version":1,"mbox":"https://patchwork.libcamera.org/series/2674/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14405/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14405/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 99EE3BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 28 Oct 2021 11:14:51 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 495E660157;\n\tThu, 28 Oct 2021 13:14:51 +0200 (CEST)","from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net\n\t[217.70.183.199])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 44749600C0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 28 Oct 2021 13:14:43 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby relay9-d.mail.gandi.net (Postfix) with ESMTPSA id BD5A5FF802;\n\tThu, 28 Oct 2021 11:14:42 +0000 (UTC)"],"From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 28 Oct 2021 13:15:20 +0200","Message-Id":"<20211028111520.256612-11-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.33.1","In-Reply-To":"<20211028111520.256612-1-jacopo@jmondi.org>","References":"<20211028111520.256612-1-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 10/10] android: Remove CameraWorker","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The CameraWorker class purpose was to handle acquire fences for incoming\ncapture requests directed to libcamera.\n\nNow that fences are handled by the core library, it is not required to\nhandle them in the HAL and the CameraWorker and CaptureRequest classes\ncan be dropped.\n\nUpdate the core in CameraDevice class accordingly to queue Requests\ndirectly to the libcamera::Camera and set the release_fence to the\nvalue of the FrameBuffer::fence() for streams of type ::Direct.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/android/camera_device.cpp  |  39 ++++------\n src/android/camera_device.h    |   5 +-\n src/android/camera_request.cpp |   3 +-\n src/android/camera_request.h   |   3 +-\n src/android/camera_worker.cpp  | 129 ---------------------------------\n src/android/camera_worker.h    |  72 ------------------\n src/android/meson.build        |   1 -\n 7 files changed, 19 insertions(+), 233 deletions(-)\n delete mode 100644 src/android/camera_worker.cpp\n delete mode 100644 src/android/camera_worker.h","diff":"diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\nindex f2e0bdbdbbf6..16237674ea24 100644\n--- a/src/android/camera_device.cpp\n+++ b/src/android/camera_device.cpp\n@@ -406,7 +406,6 @@ void CameraDevice::flush()\n \t\tstate_ = State::Flushing;\n \t}\n \n-\tworker_.stop();\n \tcamera_->stop();\n \n \tMutexLocker stateLock(stateMutex_);\n@@ -419,7 +418,6 @@ void CameraDevice::stop()\n \tif (state_ == State::Stopped)\n \t\treturn;\n \n-\tworker_.stop();\n \tcamera_->stop();\n \n \tdescriptors_ = {};\n@@ -714,9 +712,10 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list)\n }\n \n std::unique_ptr<FrameBuffer>\n-CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer,\n+CameraDevice::createFrameBuffer(const Camera3RequestDescriptor::StreamBuffer &streamBuffer,\n \t\t\t\tPixelFormat pixelFormat, const Size &size)\n {\n+\tconst buffer_handle_t camera3buffer = *streamBuffer.camera3Buffer;\n \tCameraBuffer buf(camera3buffer, pixelFormat, size, PROT_READ);\n \tif (!buf.isValid()) {\n \t\tLOG(HAL, Fatal) << \"Failed to create CameraBuffer\";\n@@ -736,7 +735,8 @@ CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer,\n \t\tplanes[i].length = buf.size(i);\n \t}\n \n-\treturn std::make_unique<FrameBuffer>(planes);\n+\treturn std::make_unique<FrameBuffer>(planes, 0,\n+\t\t\t\t\t     streamBuffer.fence);\n }\n \n int CameraDevice::processControls(Camera3RequestDescriptor *descriptor)\n@@ -912,13 +912,9 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \n \t\t/*\n \t\t * Inspect the camera stream type, create buffers opportunely\n-\t\t * and add them to the Request if required. Only acquire fences\n-\t\t * for streams of type Direct are handled by the CameraWorker,\n-\t\t * while fences for streams of type Internal and Mapped are\n-\t\t * handled at post-processing time.\n+\t\t * and add them to the Request if required.\n \t\t */\n \t\tFrameBuffer *frameBuffer = nullptr;\n-\t\tint acquireFence = -1;\n \t\tswitch (cameraStream->type()) {\n \t\tcase CameraStream::Type::Mapped:\n \t\t\t/*\n@@ -939,11 +935,10 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \t\t\t * lifetime management only.\n \t\t\t */\n \t\t\tbuffer.frameBuffer =\n-\t\t\t\tcreateFrameBuffer(*buffer.camera3Buffer,\n+\t\t\t\tcreateFrameBuffer(buffer,\n \t\t\t\t\t\t  cameraStream->configuration().pixelFormat,\n \t\t\t\t\t\t  cameraStream->configuration().size);\n \t\t\tframeBuffer = buffer.frameBuffer.get();\n-\t\t\tacquireFence = buffer.fence;\n \t\t\tLOG(HAL, Debug) << ss.str() << \" (direct)\";\n \t\t\tbreak;\n \n@@ -970,12 +965,12 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \t\t}\n \n \t\tdescriptor->request_->addBuffer(cameraStream->stream(),\n-\t\t\t\t\t\tframeBuffer, acquireFence);\n+\t\t\t\t\t\tframeBuffer);\n \t}\n \n \t/*\n \t * Translate controls from Android to libcamera and queue the request\n-\t * to the CameraWorker thread.\n+\t * to the camera.\n \t */\n \tint ret = processControls(descriptor.get());\n \tif (ret)\n@@ -1001,26 +996,23 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n \t}\n \n \tif (state_ == State::Stopped) {\n-\t\tworker_.start();\n-\n \t\tret = camera_->start();\n \t\tif (ret) {\n \t\t\tLOG(HAL, Error) << \"Failed to start camera\";\n-\t\t\tworker_.stop();\n \t\t\treturn ret;\n \t\t}\n \n \t\tstate_ = State::Running;\n \t}\n \n-\tCaptureRequest *request = descriptor->request_.get();\n+\tRequest *request = descriptor->request_.get();\n \n \t{\n \t\tMutexLocker descriptorsLock(descriptorsMutex_);\n \t\tdescriptors_.push(std::move(descriptor));\n \t}\n \n-\tworker_.queueRequest(request);\n+\tcamera_->queueRequest(request);\n \n \treturn 0;\n }\n@@ -1042,16 +1034,17 @@ void CameraDevice::requestComplete(Request *request)\n \t\t/*\n \t\t * Streams of type Direct have been queued to the\n \t\t * libcamera::Camera and their acquire fences have\n-\t\t * already been waited on by the CameraWorker.\n+\t\t * been handled by the library.\n+\t\t *\n+\t\t * If the fence has been signalled correctly it will be -1 and\n+\t\t * closed, otherwise it is set to the 'acquire_fence' value and\n+\t\t * the framework will have to close it.\n \t\t *\n \t\t * Acquire fences of streams of type Internal and Mapped\n \t\t * will be handled during post-processing.\n-\t\t *\n-\t\t * \\todo Instrument the CameraWorker to set the acquire\n-\t\t * fence to -1 once it has handled it and remove this check.\n \t\t */\n \t\tif (stream->type() == CameraStream::Type::Direct)\n-\t\t\tbuffer.fence = -1;\n+\t\t\tbuffer.fence = buffer.frameBuffer->fence();\n \t\tbuffer.status = Camera3RequestDescriptor::Status::Success;\n \t}\n \ndiff --git a/src/android/camera_device.h b/src/android/camera_device.h\nindex 2a414020f1ad..d83f77a8cf22 100644\n--- a/src/android/camera_device.h\n+++ b/src/android/camera_device.h\n@@ -30,7 +30,6 @@\n #include \"camera_capabilities.h\"\n #include \"camera_metadata.h\"\n #include \"camera_stream.h\"\n-#include \"camera_worker.h\"\n #include \"jpeg/encoder.h\"\n \n class Camera3RequestDescriptor;\n@@ -86,7 +85,7 @@ private:\n \tvoid stop();\n \n \tstd::unique_ptr<libcamera::FrameBuffer>\n-\tcreateFrameBuffer(const buffer_handle_t camera3buffer,\n+\tcreateFrameBuffer(const Camera3RequestDescriptor::StreamBuffer &streamBuffer,\n \t\t\t  libcamera::PixelFormat pixelFormat,\n \t\t\t  const libcamera::Size &size);\n \tvoid abortRequest(Camera3RequestDescriptor *descriptor) const;\n@@ -105,8 +104,6 @@ private:\n \tunsigned int id_;\n \tcamera3_device_t camera3Device_;\n \n-\tCameraWorker worker_;\n-\n \tlibcamera::Mutex stateMutex_; /* Protects access to the camera state. */\n \tState state_;\n \ndiff --git a/src/android/camera_request.cpp b/src/android/camera_request.cpp\nindex 5bac1b8f7a5f..f656c3ebe448 100644\n--- a/src/android/camera_request.cpp\n+++ b/src/android/camera_request.cpp\n@@ -49,8 +49,7 @@ Camera3RequestDescriptor::Camera3RequestDescriptor(\n \t * Create the CaptureRequest, stored as a unique_ptr<> to tie its\n \t * lifetime to the descriptor.\n \t */\n-\trequest_ = std::make_unique<CaptureRequest>(camera,\n-\t\t\t\t\t\t    reinterpret_cast<uint64_t>(this));\n+\trequest_ = camera->createRequest(reinterpret_cast<uint64_t>(this));\n }\n \n Camera3RequestDescriptor::~Camera3RequestDescriptor() = default;\ndiff --git a/src/android/camera_request.h b/src/android/camera_request.h\nindex cfafa4450d71..e019c38784ba 100644\n--- a/src/android/camera_request.h\n+++ b/src/android/camera_request.h\n@@ -20,7 +20,6 @@\n #include <hardware/camera3.h>\n \n #include \"camera_metadata.h\"\n-#include \"camera_worker.h\"\n \n class CameraBuffer;\n class CameraStream;\n@@ -60,7 +59,7 @@ public:\n \tstd::vector<StreamBuffer> buffers_;\n \n \tCameraMetadata settings_;\n-\tstd::unique_ptr<CaptureRequest> request_;\n+\tstd::unique_ptr<libcamera::Request> request_;\n \tstd::unique_ptr<CameraMetadata> resultMetadata_;\n \n \tbool complete_ = false;\ndiff --git a/src/android/camera_worker.cpp b/src/android/camera_worker.cpp\ndeleted file mode 100644\nindex dabb305407a2..000000000000\n--- a/src/android/camera_worker.cpp\n+++ /dev/null\n@@ -1,129 +0,0 @@\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-#include <unistd.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(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(this);\n-}\n-\n-void CameraWorker::start()\n-{\n-\tThread::start();\n-}\n-\n-void CameraWorker::stop()\n-{\n-\texit();\n-\twait();\n-}\n-\n-void CameraWorker::run()\n-{\n-\texec();\n-\tdispatchMessages(Message::Type::InvokeMessage);\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 waiting for 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\ndeleted file mode 100644\nindex c94f16325925..000000000000\n--- a/src/android/camera_worker.h\n+++ /dev/null\n@@ -1,72 +0,0 @@\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-#include <stdint.h>\n-\n-#include <libcamera/base/object.h>\n-#include <libcamera/base/thread.h>\n-\n-#include <libcamera/camera.h>\n-#include <libcamera/framebuffer.h>\n-#include <libcamera/request.h>\n-#include <libcamera/stream.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-\tlibcamera::ControlList &controls() { return request_->controls(); }\n-\tconst libcamera::ControlList &metadata() const\n-\t{\n-\t\treturn request_->metadata();\n-\t}\n-\tunsigned long cookie() const { return request_->cookie(); }\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 : private libcamera::Thread\n-{\n-public:\n-\tCameraWorker();\n-\n-\tvoid start();\n-\tvoid stop();\n-\n-\tvoid queueRequest(CaptureRequest *request);\n-\n-protected:\n-\tvoid run() override;\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-};\n-\n-#endif /* __ANDROID_CAMERA_WORKER_H__ */\ndiff --git a/src/android/meson.build b/src/android/meson.build\nindex 332b177ca805..75b4bf207085 100644\n--- a/src/android/meson.build\n+++ b/src/android/meson.build\n@@ -47,7 +47,6 @@ android_hal_sources = files([\n     'camera_ops.cpp',\n     'camera_request.cpp',\n     'camera_stream.cpp',\n-    'camera_worker.cpp',\n     'jpeg/encoder_libjpeg.cpp',\n     'jpeg/exif.cpp',\n     'jpeg/post_processor_jpeg.cpp',\n","prefixes":["libcamera-devel","10/10"]}