[v2,22/37] libcamera: software_isp: gbm: Add in a GBM helper class for GPU surface access
diff mbox series

Message ID 20250824-b4-v0-5-2-gpuisp-v2-a-v2-22-96f4576c814e@linaro.org
State New
Headers show
Series
  • Add GLES 2.0 GPUISP to libcamera
Related show

Commit Message

Bryan O'Donoghue Aug. 24, 2025, 12:48 a.m. UTC
A helper class to interact with GBM. This will allow us to specify the
internal storage format of the CPU when making a texture for the Debayer
vertext/fragment shaders and thus ensure we receive an uncompressed and
untiled output buffer.

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
---
 include/libcamera/internal/gbm.h       | 39 ++++++++++++++++++++++
 include/libcamera/internal/meson.build |  1 +
 src/libcamera/gbm.cpp                  | 61 ++++++++++++++++++++++++++++++++++
 src/libcamera/meson.build              | 11 ++++++
 4 files changed, 112 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/internal/gbm.h b/include/libcamera/internal/gbm.h
new file mode 100644
index 0000000000000000000000000000000000000000..ccf7b2da54eebb1b69a5b7c050eed778c199ac8f
--- /dev/null
+++ b/include/libcamera/internal/gbm.h
@@ -0,0 +1,39 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2024, Linaro Ltd.
+ *
+ * Authors:
+ * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
+ *
+ * gbm.h - Helper class for managing GBM interactions.
+ */
+
+#pragma once
+
+#include <gbm.h>
+
+#include <libcamera/base/log.h>
+
+#include <libcamera/formats.h>
+
+namespace libcamera {
+
+LOG_DECLARE_CATEGORY(GBM)
+
+class GBM
+{
+public:
+	GBM();
+	~GBM();
+
+	int createDevice();
+	struct gbm_device *getDevice() { return gbm_device_; }
+	PixelFormat getPixelFormat() { return format_; }
+
+private:
+	int fd_;
+	struct gbm_device *gbm_device_;
+	PixelFormat format_;
+};
+
+} // namespace libcamera
diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build
index 90155857e44fc64803df5b5a054ad62e48b8e2f5..6c65254ddd95fbe9cbb6dadafc0826226b895a7f 100644
--- a/include/libcamera/internal/meson.build
+++ b/include/libcamera/internal/meson.build
@@ -24,6 +24,7 @@  libcamera_internal_headers = files([
     'dma_buf_allocator.h',
     'formats.h',
     'framebuffer.h',
+    'gbm.h',
     'ipa_data_serializer.h',
     'ipa_manager.h',
     'ipa_module.h',
diff --git a/src/libcamera/gbm.cpp b/src/libcamera/gbm.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..caf9ca1c742ce3269ec10f0b6394f9821dabd932
--- /dev/null
+++ b/src/libcamera/gbm.cpp
@@ -0,0 +1,61 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2024, Linaro Ltd.
+ *
+ * Authors:
+ * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
+ *
+ * egl.cpp - Helper class for managing GBM interactions.
+ */
+
+#include "libcamera/internal/gbm.h"
+
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <linux/dma-buf.h>
+#include <linux/dma-heap.h>
+
+namespace libcamera {
+
+LOG_DEFINE_CATEGORY(GBM)
+
+GBM::GBM()
+{
+	fd_ = 0;
+}
+
+GBM::~GBM()
+{
+	if (gbm_device_)
+		gbm_device_destroy(gbm_device_);
+
+	if (fd_ >= 0)
+		close(fd_);
+}
+
+int GBM::createDevice()
+{
+	const char *dri_node = "/dev/dri/renderD128"; //TODO: get from an env or config setting
+
+	fd_ = open(dri_node, O_RDWR | O_CLOEXEC);
+	if (fd_ < 0) {
+		LOG(GBM, Error) << "Open " << dri_node << " fail " << fd_;
+		return fd_;
+	}
+
+	gbm_device_ = gbm_create_device(fd_);
+	if (!gbm_device_) {
+		LOG(GBM, Error) << "gbm_crate_device fail";
+		goto fail;
+	}
+
+	format_ = libcamera::formats::ARGB8888;
+
+	return 0;
+fail:
+	return -ENODEV;
+}
+} //namespace libcamera
diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build
index de1eb99b28fdb07b78b529ed21d9d584675d50ae..64270b571cea2ece8d4dfea8a23b8edf60de70d3 100644
--- a/src/libcamera/meson.build
+++ b/src/libcamera/meson.build
@@ -68,6 +68,16 @@  libcamera_deps = []
 libatomic = cc.find_library('atomic', required : false)
 libthreads = dependency('threads')
 
+libgbm = cc.find_library('gbm', required: false)
+gbm_works = cc.check_header('gbm.h', required: false) 
+
+if libgbm.found() and gbm_works
+    config_h.set('HAVE_GBM', 1)
+    libcamera_internal_sources += files([
+        'gbm.cpp',
+    ])
+endif
+
 subdir('base')
 subdir('converter')
 subdir('ipa')
@@ -181,6 +191,7 @@  libcamera_deps += [
     libcamera_base_private,
     libcrypto,
     libdl,
+    libgbm,
     liblttng,
     libudev,
     libyaml,