[{"id":21661,"web_url":"https://patchwork.libcamera.org/comment/21661/","msgid":"<YbB5HZS4KanNvfhB@pendragon.ideasonboard.com>","date":"2021-12-08T09:21:33","subject":"Re: [libcamera-devel] [PATCH v4 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 Wed, Dec 01, 2021 at 03:29:28PM +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> ---\n>  include/libcamera/fence.h     |  31 ++++++++++\n>  include/libcamera/meson.build |   1 +\n>  src/libcamera/fence.cpp       | 113 ++++++++++++++++++++++++++++++++++\n>  src/libcamera/meson.build     |   1 +\n>  4 files changed, 146 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..bd737e9848e1\n> --- /dev/null\n> +++ b/src/libcamera/fence.cpp\n> @@ -0,0 +1,113 @@\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 Synchronization fence\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> + * Synchronization fences are most commonly used in association with frame\n> + * buffers. A FrameBuffer can be associated with a Fence so that the library can\n> + * wait for the Fence to be signalled before allowing the camera device to\n> + * actually access the memory area described by the FrameBuffer.\n> + *\n> + * \\sa Request::addBuffer()\n> + *\n> + * By using synchronization fences, application can then synchronize between\n\ns/synchronization fences/fences/ (as explained in the review of v3)\n\n> + * frame buffer consumers and producers, as for example a display device and a\n> + * camera, to guarantee that new data transfers only happen once the existing\n> + * frames have 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> + * kernel sync file. This is currently the only mechanism supported by\n\nYou can move the\n\n * <a href=\"https://www.kernel.org/doc/html/latest/driver-api/sync_file.html\">kernel sync file</a>\n\nfrom below here, inline in the text.\n\n> + * libcamera, but others can be implemented by extending or subclassing this\n> + * class and implementing opportune handling in the core library.\n> + *\n> + * <a href=\"https://www.kernel.org/doc/html/latest/driver-api/sync_file.html\">kernel sync file</a>\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 synchronization fence\n> + * \\param[in] fd The synchronization fence file descriptor\n\ns/synchronization fence/fence/ here too.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\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 b763110e74a6..b4882a2577f5 100644\n> --- a/src/libcamera/meson.build\n> +++ b/src/libcamera/meson.build\n> @@ -14,6 +14,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 7A158BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  8 Dec 2021 09:22:05 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 95EB860868;\n\tWed,  8 Dec 2021 10:22:04 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 9092760225\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  8 Dec 2021 10:22:03 +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 B8D838BB;\n\tWed,  8 Dec 2021 10:22:02 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"ol2hxTlG\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638955323;\n\tbh=o9PFto/VzgdaIcEconE2M+efEeFkX0797afnsMQcJG4=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=ol2hxTlGJ4SsQdmJkuw3NDho2gSGcOKltAp823iDa0XJ2DEWJB86XXucnhyttujd4\n\tXHTsicB7LxN2+Ah5bESNpHPm8JWknJNFdLh43+7GBgPDBTiLe0bCCQ5nG6Ilu3BVTG\n\tetibthvi4bE55+nMTz9c2tyaNh6hTtmQbBIFaBZQ=","Date":"Wed, 8 Dec 2021 11:21:33 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Message-ID":"<YbB5HZS4KanNvfhB@pendragon.ideasonboard.com>","References":"<20211201142936.107405-1-jacopo@jmondi.org>\n\t<20211201142936.107405-4-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211201142936.107405-4-jacopo@jmondi.org>","Subject":"Re: [libcamera-devel] [PATCH v4 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>"}}]