[{"id":21758,"web_url":"https://patchwork.libcamera.org/comment/21758/","msgid":"<YbPBJg1BQXb7d5it@pendragon.ideasonboard.com>","date":"2021-12-10T21:05:42","subject":"Re: [libcamera-devel] [PATCH v5 03/11] libcamera: fence: Introduce\n\tFence","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Fri, Dec 10, 2021 at 09:52:31PM +0100, Jacopo Mondi wrote:\n> Introduce a Fence class which models a synchronization primitive that\n> allows to notify the availability of a resource.\n> \n> The Fence is modeled as a wrapper of a UniqueFD instance where\n> read events are used to signal the Fence. The class can be later\n> extended to support additional signalling mechanisms.\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  include/libcamera/fence.h     |  31 ++++++++++\n>  include/libcamera/meson.build |   1 +\n>  src/libcamera/fence.cpp       | 112 ++++++++++++++++++++++++++++++++++\n>  src/libcamera/meson.build     |   1 +\n>  4 files changed, 145 insertions(+)\n>  create mode 100644 include/libcamera/fence.h\n>  create mode 100644 src/libcamera/fence.cpp\n> \n> diff --git a/include/libcamera/fence.h b/include/libcamera/fence.h\n> new file mode 100644\n> index 000000000000..c0c916c264b4\n> --- /dev/null\n> +++ b/include/libcamera/fence.h\n> @@ -0,0 +1,31 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2021, Google Inc.\n> + *\n> + * internal/fence.h - Synchronization fence\n> + */\n> +\n> +#pragma once\n> +\n> +#include <libcamera/base/class.h>\n> +#include <libcamera/base/unique_fd.h>\n> +\n> +namespace libcamera {\n> +\n> +class Fence\n> +{\n> +public:\n> +\tFence(UniqueFD fd);\n> +\n> +\tbool isValid() const { return fd_.isValid(); }\n> +\tconst UniqueFD &fd() const { return fd_; }\n> +\n> +\tUniqueFD release() { return std::move(fd_); }\n> +\n> +private:\n> +\tLIBCAMERA_DISABLE_COPY_AND_MOVE(Fence)\n> +\n> +\tUniqueFD fd_;\n> +};\n> +\n> +} /* namespace libcamera */\n> diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build\n> index 5f42977c034b..8c2cae00d877 100644\n> --- a/include/libcamera/meson.build\n> +++ b/include/libcamera/meson.build\n> @@ -6,6 +6,7 @@ libcamera_public_headers = files([\n>      'camera.h',\n>      'camera_manager.h',\n>      'controls.h',\n> +    'fence.h',\n>      'framebuffer.h',\n>      'framebuffer_allocator.h',\n>      'geometry.h',\n> diff --git a/src/libcamera/fence.cpp b/src/libcamera/fence.cpp\n> new file mode 100644\n> index 000000000000..3fb0e9c5281a\n> --- /dev/null\n> +++ b/src/libcamera/fence.cpp\n> @@ -0,0 +1,112 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2021, Google Inc.\n> + *\n> + * fence.cpp - Synchronization fence\n> + */\n> +\n> +#include \"libcamera/fence.h\"\n> +\n> +namespace libcamera {\n> +\n> +/**\n> + *\n> + * \\file libcamera/fence.h\n> + * \\brief Definition of the Fence class\n> + */\n> +\n> +/**\n> + * \\class Fence\n> + * \\brief Synchronization primitive to manage resources\n> + *\n> + * The Fence class models a synchronization primitive that can be used by\n> + * applications to explicitly synchronize resource usage, and can be shared by\n> + * multiple processes.\n> + *\n> + * Fences are most commonly used in association with frame buffers. A\n> + * FrameBuffer can be associated with a Fence so that the library can wait for\n> + * the Fence to be signalled before allowing the camera device to actually\n> + * access the memory area described by the FrameBuffer.\n> + *\n> + * \\sa Request::addBuffer()\n> + *\n> + * By using a fence, applications can then synchronize between frame buffer\n> + * consumers and producers, as for example a display device and a camera, to\n> + * guarantee that a new data transfers only happen once the existing frames have\n> + * been displayed.\n> + *\n> + * A Fence can be realized by different event notification primitives, the most\n> + * common of which is represented by waiting for read events to happen on a\n> + * <a href=\"https://www.kernel.org/doc/html/latest/driver-api/sync_file.html\">kernel sync file</a>\n\nMissing . at the end of the line.\n\n> + * This is currently the only mechanism supported by libcamera, but others can\n> + * be implemented by extending or subclassing this class and implementing\n> + * opportune handling in the core library.\n> + *\n> + * \\internal\n> + *\n> + * The Fence class is a thin abstraction around a UniqueFD which simply allows\n> + * to access it as a const reference or to move its ownership to the caller.\n> + *\n> + * The usage of the Fence class allows to abstract the underlying\n> + * synchronization mechanism in use and implement an interface towards other\n> + * library components that will not change when new synchronization primitives\n> + * will be added as fences.\n> + *\n> + * A Fence is constructed with a UniqueFD whose ownership is moved in the Fence.\n> + * A FrameBuffer can be associated with a Fence by passing it to the\n> + * Request::addBuffer() function, which will move the Fence into the FrameBuffer\n> + * itself. Once a Request is queued to the Camera, a preparation phase\n> + * guarantees that before actually applying the Request to the hardware, all the\n> + * valid fences of the frame buffers in a Request are correctly signalled. Once\n> + * a Fence has completed, the library will release the FrameBuffer fence so that\n> + * application won't be allowed to access it.\n> + *\n> + * An optional timeout can be started while waiting for a fence to complete. If\n> + * waiting on a Fence fails for whatever reason, the FrameBuffer's fence is not\n> + * reset and is made available to application for them to handle it, by\n> + * releasing the Fence to correctly close the underlying UniqueFD.\n> + *\n> + * A failure in waiting for a Fence to complete will result in the Request to\n> + * complete in failed state.\n> + *\n> + * \\sa Request::prepare()\n> + * \\sa PipelineHandler::doQueueRequests()\n> + */\n> +\n> +/**\n> + * \\brief Create a Fence\n> + * \\param[in] fd The fence file descriptor\n> + *\n> + * The file descriptor ownership is moved to the Fence.\n> + */\n> +Fence::Fence(UniqueFD fd)\n> +\t: fd_(std::move(fd))\n> +{\n> +}\n> +\n> +/**\n> + * \\fn Fence::isValid()\n> + * \\brief Check if a Fence is valid\n> + *\n> + * A Fence is valid if the file descriptor it wraps is valid.\n> + *\n> + * \\return True if the Fence is valid, false otherwise\n> + */\n> +\n> +/**\n> + * \\fn Fence::fd()\n> + * \\brief Retrieve a constant reference to the file descriptor\n> + * \\return A const reference to the fence file descriptor\n> + */\n> +\n> +/**\n> + * \\fn Fence::release()\n> + * \\brief Release the ownership of the file descriptor\n> + *\n> + * Release the ownership of the wrapped file descriptor by returning it to the\n> + * caller.\n> + *\n> + * \\return The wrapper UniqueFD\n> + */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> index 2e54cc04edc7..dc1cc52a427a 100644\n> --- a/src/libcamera/meson.build\n> +++ b/src/libcamera/meson.build\n> @@ -15,6 +15,7 @@ libcamera_sources = files([\n>      'delayed_controls.cpp',\n>      'device_enumerator.cpp',\n>      'device_enumerator_sysfs.cpp',\n> +    'fence.cpp',\n>      'formats.cpp',\n>      'framebuffer.cpp',\n>      'framebuffer_allocator.cpp',","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 9B78CBDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 10 Dec 2021 21:06:14 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0EC4160890;\n\tFri, 10 Dec 2021 22:06:14 +0100 (CET)","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 6652A60868\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 10 Dec 2021 22:06:12 +0100 (CET)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id CA30EF84;\n\tFri, 10 Dec 2021 22:06:11 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"QvilO4YE\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1639170372;\n\tbh=+u+2GNoRv/n6ZY/WaEro84KscbBomXvvmwMG5SGo8Fo=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=QvilO4YE32ZMkDqE9/suGGzK/DjLlVzMJq0kidSNLw+tzim78TqRYI9HbhM82TYsF\n\tNqrDoMYPp1WByV72wJ6pwzlwohzm+q16wLANR6rdPWRZs6PFtScrnU4P09AQBhQeUc\n\tJNX5vP5WkJ9gLZ61zxMVNM2buvGRhdCyREGLc8zc=","Date":"Fri, 10 Dec 2021 23:05:42 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Message-ID":"<YbPBJg1BQXb7d5it@pendragon.ideasonboard.com>","References":"<20211210205239.354901-1-jacopo@jmondi.org>\n\t<20211210205239.354901-4-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211210205239.354901-4-jacopo@jmondi.org>","Subject":"Re: [libcamera-devel] [PATCH v5 03/11] libcamera: fence: Introduce\n\tFence","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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]