[v7,02/18] libcamera: internal: Move dma_heaps.[h, cpp] to common directories
diff mbox series

Message ID 20240404084657.353464-3-mzamazal@redhat.com
State Superseded
Headers show
Series
  • libcamera: introduce Software ISP and Software IPA
Related show

Commit Message

Milan Zamazal April 4, 2024, 8:46 a.m. UTC
From: Andrey Konovalov <andrey.konovalov@linaro.org>

DmaHeap class is useful outside the RPi pipeline handler too.

Move dma_heaps.h and dma_heaps.cpp to common directories. Update
the build files and RPi vc4 pipeline handler accordingly.

Tested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org> # sc8280xp Lenovo x13s
Tested-by: Pavel Machek <pavel@ucw.cz>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrey Konovalov <andrey.konovalov@linaro.org>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
---
 .../libcamera/internal}/dma_heaps.h           |  4 --
 include/libcamera/internal/meson.build        |  1 +
 .../{pipeline/rpi/vc4 => }/dma_heaps.cpp      | 63 ++++++++++++++-----
 src/libcamera/meson.build                     |  1 +
 src/libcamera/pipeline/rpi/vc4/meson.build    |  1 -
 src/libcamera/pipeline/rpi/vc4/vc4.cpp        |  5 +-
 6 files changed, 52 insertions(+), 23 deletions(-)
 rename {src/libcamera/pipeline/rpi/vc4 => include/libcamera/internal}/dma_heaps.h (92%)
 rename src/libcamera/{pipeline/rpi/vc4 => }/dma_heaps.cpp (51%)

Comments

Laurent Pinchart April 13, 2024, 12:59 p.m. UTC | #1
Hi Milan and Andrey,

Thank you for the patch.

On Thu, Apr 04, 2024 at 10:46:39AM +0200, Milan Zamazal wrote:
> From: Andrey Konovalov <andrey.konovalov@linaro.org>
> 
> DmaHeap class is useful outside the RPi pipeline handler too.
> 
> Move dma_heaps.h and dma_heaps.cpp to common directories. Update
> the build files and RPi vc4 pipeline handler accordingly.
> 
> Tested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org> # sc8280xp Lenovo x13s
> Tested-by: Pavel Machek <pavel@ucw.cz>
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
> Reviewed-by: Pavel Machek <pavel@ucw.cz>
> Signed-off-by: Andrey Konovalov <andrey.konovalov@linaro.org>
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
> ---
>  .../libcamera/internal}/dma_heaps.h           |  4 --
>  include/libcamera/internal/meson.build        |  1 +
>  .../{pipeline/rpi/vc4 => }/dma_heaps.cpp      | 63 ++++++++++++++-----
>  src/libcamera/meson.build                     |  1 +
>  src/libcamera/pipeline/rpi/vc4/meson.build    |  1 -
>  src/libcamera/pipeline/rpi/vc4/vc4.cpp        |  5 +-
>  6 files changed, 52 insertions(+), 23 deletions(-)
>  rename {src/libcamera/pipeline/rpi/vc4 => include/libcamera/internal}/dma_heaps.h (92%)
>  rename src/libcamera/{pipeline/rpi/vc4 => }/dma_heaps.cpp (51%)
> 
> diff --git a/src/libcamera/pipeline/rpi/vc4/dma_heaps.h b/include/libcamera/internal/dma_heaps.h
> similarity index 92%
> rename from src/libcamera/pipeline/rpi/vc4/dma_heaps.h
> rename to include/libcamera/internal/dma_heaps.h
> index 0a4a8d86..cff8f140 100644
> --- a/src/libcamera/pipeline/rpi/vc4/dma_heaps.h
> +++ b/include/libcamera/internal/dma_heaps.h
> @@ -13,8 +13,6 @@
>  
>  namespace libcamera {
>  
> -namespace RPi {
> -
>  class DmaHeap
>  {
>  public:
> @@ -27,6 +25,4 @@ private:
>  	UniqueFD dmaHeapHandle_;
>  };
>  
> -} /* namespace RPi */
> -
>  } /* namespace libcamera */
> diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build
> index 7f1f3440..33eb0fb3 100644
> --- a/include/libcamera/internal/meson.build
> +++ b/include/libcamera/internal/meson.build
> @@ -25,6 +25,7 @@ libcamera_internal_headers = files([
>      'device_enumerator.h',
>      'device_enumerator_sysfs.h',
>      'device_enumerator_udev.h',
> +    'dma_heaps.h',
>      'formats.h',
>      'framebuffer.h',
>      'ipa_manager.h',
> diff --git a/src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp b/src/libcamera/dma_heaps.cpp
> similarity index 51%
> rename from src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp
> rename to src/libcamera/dma_heaps.cpp
> index 317b1fc1..70da6f3a 100644
> --- a/src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp
> +++ b/src/libcamera/dma_heaps.cpp
> @@ -5,19 +5,25 @@
>   * dma_heaps.h - Helper class for dma-heap allocations.

While at it, s/dma_heaps.h/dma_heaps.cpp/

>   */
>  
> -#include "dma_heaps.h"
> +#include "libcamera/internal/dma_heaps.h"
>  
>  #include <array>
>  #include <fcntl.h>
> -#include <linux/dma-buf.h>
> -#include <linux/dma-heap.h>
>  #include <sys/ioctl.h>
>  #include <unistd.h>
>  
> +#include <linux/dma-buf.h>
> +#include <linux/dma-heap.h>
> +
>  #include <libcamera/base/log.h>
>  
> +/**
> + * \file dma_heaps.cpp
> + * \brief CMA dma-heap allocator
> + */
> +
>  /*
> - * /dev/dma-heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma
> + * /dev/dma_heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma
>   * to only have to worry about importing.
>   *
>   * Annoyingly, should the cma heap size be specified on the kernel command line
> @@ -30,18 +36,30 @@ static constexpr std::array<const char *, 2> heapNames = {
>  
>  namespace libcamera {
>  
> -LOG_DECLARE_CATEGORY(RPI)
> +LOG_DEFINE_CATEGORY(DmaHeap)
>  
> -namespace RPi {
> +/**
> + * \class DmaHeap
> + * \brief Helper class for CMA dma-heap allocations
> + */
>  
> +/**
> + * \brief Construct a DmaHeap that owns a CMA dma-heap file descriptor
> + *
> + * Looks for a CMA dma-heap device to use. If it fails to open any dma-heap
> + * device, an invalid DmaHeap object is constructed.
> + *
> + * Check the new DmaHeap object with DmaHeap::isValid before using it.

s/DmaHeap::isValid/isValid()/

doxygen should do the right thing.

> + */
>  DmaHeap::DmaHeap()
>  {
>  	for (const char *name : heapNames) {
>  		int ret = ::open(name, O_RDWR | O_CLOEXEC, 0);
>  		if (ret < 0) {
>  			ret = errno;
> -			LOG(RPI, Debug) << "Failed to open " << name << ": "
> -					<< strerror(ret);
> +			LOG(DmaHeap, Debug)
> +				<< "Failed to open " << name << ": "
> +				<< strerror(ret);
>  			continue;
>  		}
>  
> @@ -50,11 +68,30 @@ DmaHeap::DmaHeap()
>  	}
>  
>  	if (!dmaHeapHandle_.isValid())
> -		LOG(RPI, Error) << "Could not open any dmaHeap device";
> +		LOG(DmaHeap, Error) << "Could not open any dmaHeap device";
>  }
>  
> +/**
> + * \brief Destroy the DmaHeap instance
> + */
>  DmaHeap::~DmaHeap() = default;
>  
> +/**
> + * \fn DmaHeap::isValid()
> + * \brief Check if the DmaHeap instance is valid
> + * \return True if the DmaHeap is valid, false otherwise
> + */
> +
> +/**
> + * \brief Allocate a dma-buf from the DmaHeap
> + * \param [in] name The name to set for the allocated buffer
> + * \param [in] size The size of the buffer to allocate
> + *
> + * Allocates a dma-buf with read/write access.
> + * If the allocation fails returns invalid UniqueFD.

s/ returns invalid/, return an invalid/

I haven't found an easy way to check this in checkstyle.py yet, so I
have to repeat the comment myself for now: There should be no line break
in the middle of a paragraph. Either you meant this text to be in a
single paragraph, in which case it should be written as

 * Allocates a dma-buf with read/write access. If the allocation fails, return
 * an invalid UniqueFD.

or you meant to have two separate paragraphs:

 * Allocates a dma-buf with read/write access.
 *
 * If the allocation fails, return an invalid UniqueFD.

With these small issues fixed,

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> + *
> + * \return The UniqueFD of the allocated buffer
> + */
>  UniqueFD DmaHeap::alloc(const char *name, std::size_t size)
>  {
>  	int ret;
> @@ -69,22 +106,18 @@ UniqueFD DmaHeap::alloc(const char *name, std::size_t size)
>  
>  	ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);
>  	if (ret < 0) {
> -		LOG(RPI, Error) << "dmaHeap allocation failure for "
> -				<< name;
> +		LOG(DmaHeap, Error) << "dmaHeap allocation failure for " << name;
>  		return {};
>  	}
>  
>  	UniqueFD allocFd(alloc.fd);
>  	ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);
>  	if (ret < 0) {
> -		LOG(RPI, Error) << "dmaHeap naming failure for "
> -				<< name;
> +		LOG(DmaHeap, Error) << "dmaHeap naming failure for " << name;
>  		return {};
>  	}
>  
>  	return allocFd;
>  }
>  
> -} /* namespace RPi */
> -
>  } /* namespace libcamera */
> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build
> index 2e7b0c77..dd8107fa 100644
> --- a/src/libcamera/meson.build
> +++ b/src/libcamera/meson.build
> @@ -15,6 +15,7 @@ libcamera_sources = files([
>      'delayed_controls.cpp',
>      'device_enumerator.cpp',
>      'device_enumerator_sysfs.cpp',
> +    'dma_heaps.cpp',
>      'fence.cpp',
>      'formats.cpp',
>      'framebuffer.cpp',
> diff --git a/src/libcamera/pipeline/rpi/vc4/meson.build b/src/libcamera/pipeline/rpi/vc4/meson.build
> index cdb049c5..386e2296 100644
> --- a/src/libcamera/pipeline/rpi/vc4/meson.build
> +++ b/src/libcamera/pipeline/rpi/vc4/meson.build
> @@ -1,7 +1,6 @@
>  # SPDX-License-Identifier: CC0-1.0
>  
>  libcamera_sources += files([
> -    'dma_heaps.cpp',
>      'vc4.cpp',
>  ])
>  
> diff --git a/src/libcamera/pipeline/rpi/vc4/vc4.cpp b/src/libcamera/pipeline/rpi/vc4/vc4.cpp
> index ad7dddde..947b1e73 100644
> --- a/src/libcamera/pipeline/rpi/vc4/vc4.cpp
> +++ b/src/libcamera/pipeline/rpi/vc4/vc4.cpp
> @@ -12,12 +12,11 @@
>  #include <libcamera/formats.h>
>  
>  #include "libcamera/internal/device_enumerator.h"
> +#include "libcamera/internal/dma_heaps.h"
>  
>  #include "../common/pipeline_base.h"
>  #include "../common/rpi_stream.h"
>  
> -#include "dma_heaps.h"
> -
>  using namespace std::chrono_literals;
>  
>  namespace libcamera {
> @@ -87,7 +86,7 @@ public:
>  	RPi::Device<Isp, 4> isp_;
>  
>  	/* DMAHEAP allocation helper. */
> -	RPi::DmaHeap dmaHeap_;
> +	DmaHeap dmaHeap_;
>  	SharedFD lsTable_;
>  
>  	struct Config {

Patch
diff mbox series

diff --git a/src/libcamera/pipeline/rpi/vc4/dma_heaps.h b/include/libcamera/internal/dma_heaps.h
similarity index 92%
rename from src/libcamera/pipeline/rpi/vc4/dma_heaps.h
rename to include/libcamera/internal/dma_heaps.h
index 0a4a8d86..cff8f140 100644
--- a/src/libcamera/pipeline/rpi/vc4/dma_heaps.h
+++ b/include/libcamera/internal/dma_heaps.h
@@ -13,8 +13,6 @@ 
 
 namespace libcamera {
 
-namespace RPi {
-
 class DmaHeap
 {
 public:
@@ -27,6 +25,4 @@  private:
 	UniqueFD dmaHeapHandle_;
 };
 
-} /* namespace RPi */
-
 } /* namespace libcamera */
diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build
index 7f1f3440..33eb0fb3 100644
--- a/include/libcamera/internal/meson.build
+++ b/include/libcamera/internal/meson.build
@@ -25,6 +25,7 @@  libcamera_internal_headers = files([
     'device_enumerator.h',
     'device_enumerator_sysfs.h',
     'device_enumerator_udev.h',
+    'dma_heaps.h',
     'formats.h',
     'framebuffer.h',
     'ipa_manager.h',
diff --git a/src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp b/src/libcamera/dma_heaps.cpp
similarity index 51%
rename from src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp
rename to src/libcamera/dma_heaps.cpp
index 317b1fc1..70da6f3a 100644
--- a/src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp
+++ b/src/libcamera/dma_heaps.cpp
@@ -5,19 +5,25 @@ 
  * dma_heaps.h - Helper class for dma-heap allocations.
  */
 
-#include "dma_heaps.h"
+#include "libcamera/internal/dma_heaps.h"
 
 #include <array>
 #include <fcntl.h>
-#include <linux/dma-buf.h>
-#include <linux/dma-heap.h>
 #include <sys/ioctl.h>
 #include <unistd.h>
 
+#include <linux/dma-buf.h>
+#include <linux/dma-heap.h>
+
 #include <libcamera/base/log.h>
 
+/**
+ * \file dma_heaps.cpp
+ * \brief CMA dma-heap allocator
+ */
+
 /*
- * /dev/dma-heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma
+ * /dev/dma_heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma
  * to only have to worry about importing.
  *
  * Annoyingly, should the cma heap size be specified on the kernel command line
@@ -30,18 +36,30 @@  static constexpr std::array<const char *, 2> heapNames = {
 
 namespace libcamera {
 
-LOG_DECLARE_CATEGORY(RPI)
+LOG_DEFINE_CATEGORY(DmaHeap)
 
-namespace RPi {
+/**
+ * \class DmaHeap
+ * \brief Helper class for CMA dma-heap allocations
+ */
 
+/**
+ * \brief Construct a DmaHeap that owns a CMA dma-heap file descriptor
+ *
+ * Looks for a CMA dma-heap device to use. If it fails to open any dma-heap
+ * device, an invalid DmaHeap object is constructed.
+ *
+ * Check the new DmaHeap object with DmaHeap::isValid before using it.
+ */
 DmaHeap::DmaHeap()
 {
 	for (const char *name : heapNames) {
 		int ret = ::open(name, O_RDWR | O_CLOEXEC, 0);
 		if (ret < 0) {
 			ret = errno;
-			LOG(RPI, Debug) << "Failed to open " << name << ": "
-					<< strerror(ret);
+			LOG(DmaHeap, Debug)
+				<< "Failed to open " << name << ": "
+				<< strerror(ret);
 			continue;
 		}
 
@@ -50,11 +68,30 @@  DmaHeap::DmaHeap()
 	}
 
 	if (!dmaHeapHandle_.isValid())
-		LOG(RPI, Error) << "Could not open any dmaHeap device";
+		LOG(DmaHeap, Error) << "Could not open any dmaHeap device";
 }
 
+/**
+ * \brief Destroy the DmaHeap instance
+ */
 DmaHeap::~DmaHeap() = default;
 
+/**
+ * \fn DmaHeap::isValid()
+ * \brief Check if the DmaHeap instance is valid
+ * \return True if the DmaHeap is valid, false otherwise
+ */
+
+/**
+ * \brief Allocate a dma-buf from the DmaHeap
+ * \param [in] name The name to set for the allocated buffer
+ * \param [in] size The size of the buffer to allocate
+ *
+ * Allocates a dma-buf with read/write access.
+ * If the allocation fails returns invalid UniqueFD.
+ *
+ * \return The UniqueFD of the allocated buffer
+ */
 UniqueFD DmaHeap::alloc(const char *name, std::size_t size)
 {
 	int ret;
@@ -69,22 +106,18 @@  UniqueFD DmaHeap::alloc(const char *name, std::size_t size)
 
 	ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);
 	if (ret < 0) {
-		LOG(RPI, Error) << "dmaHeap allocation failure for "
-				<< name;
+		LOG(DmaHeap, Error) << "dmaHeap allocation failure for " << name;
 		return {};
 	}
 
 	UniqueFD allocFd(alloc.fd);
 	ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);
 	if (ret < 0) {
-		LOG(RPI, Error) << "dmaHeap naming failure for "
-				<< name;
+		LOG(DmaHeap, Error) << "dmaHeap naming failure for " << name;
 		return {};
 	}
 
 	return allocFd;
 }
 
-} /* namespace RPi */
-
 } /* namespace libcamera */
diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build
index 2e7b0c77..dd8107fa 100644
--- a/src/libcamera/meson.build
+++ b/src/libcamera/meson.build
@@ -15,6 +15,7 @@  libcamera_sources = files([
     'delayed_controls.cpp',
     'device_enumerator.cpp',
     'device_enumerator_sysfs.cpp',
+    'dma_heaps.cpp',
     'fence.cpp',
     'formats.cpp',
     'framebuffer.cpp',
diff --git a/src/libcamera/pipeline/rpi/vc4/meson.build b/src/libcamera/pipeline/rpi/vc4/meson.build
index cdb049c5..386e2296 100644
--- a/src/libcamera/pipeline/rpi/vc4/meson.build
+++ b/src/libcamera/pipeline/rpi/vc4/meson.build
@@ -1,7 +1,6 @@ 
 # SPDX-License-Identifier: CC0-1.0
 
 libcamera_sources += files([
-    'dma_heaps.cpp',
     'vc4.cpp',
 ])
 
diff --git a/src/libcamera/pipeline/rpi/vc4/vc4.cpp b/src/libcamera/pipeline/rpi/vc4/vc4.cpp
index ad7dddde..947b1e73 100644
--- a/src/libcamera/pipeline/rpi/vc4/vc4.cpp
+++ b/src/libcamera/pipeline/rpi/vc4/vc4.cpp
@@ -12,12 +12,11 @@ 
 #include <libcamera/formats.h>
 
 #include "libcamera/internal/device_enumerator.h"
+#include "libcamera/internal/dma_heaps.h"
 
 #include "../common/pipeline_base.h"
 #include "../common/rpi_stream.h"
 
-#include "dma_heaps.h"
-
 using namespace std::chrono_literals;
 
 namespace libcamera {
@@ -87,7 +86,7 @@  public:
 	RPi::Device<Isp, 4> isp_;
 
 	/* DMAHEAP allocation helper. */
-	RPi::DmaHeap dmaHeap_;
+	DmaHeap dmaHeap_;
 	SharedFD lsTable_;
 
 	struct Config {