new file mode 100644
@@ -0,0 +1,84 @@
+/* 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)
+
+/**
+ * \brief Helper class for managing GBM interactions
+ *
+ * The GBM class provides a simplified interface for creating and managing
+ * GBM devices. It handles the initialization and teardown of GBM devices
+ * used for buffer allocation in graphics and camera pipelines.
+ *
+ * This class is responsible for opening a DRI render node, creating a GBM
+ * device, and providing access to the device and its associated pixel format.
+ */
+class GBM
+{
+public:
+ /**
+ *\brief GBM constructor.
+ *
+ * Creates a GBM instance with unitialised state.
+ */
+ GBM();
+
+ /**
+ *\brief GBM destructor
+ *
+ * Cleans up the GBM device if it was successfully created, and closes
+ * the associated file descriptor.
+ */
+ ~GBM();
+
+ /**
+ * \brief Create and initialize a GBM device
+ *
+ * Opens the DRI render node (/dev/dri/renderD128) and creates a GBM
+ * device using the libgbm library. Sets the default pixel format to
+ * ARGB8888.
+ *
+ * \return 0 on success, or a negative error code on failure
+ */
+ int createDevice();
+
+
+ /**
+ * \brief Retrieve the GBM device handle
+ *
+ * \return Pointer to the gbm_device structure, or nullptr if the device
+ * has not been created
+ */
+ struct gbm_device *getDevice() { return gbm_device_; }
+
+ /**
+ * \brief Retrieve the pixel format
+ *
+ * \return The PixelFormat used by this GBM instance (ARGB8888)
+ */
+ PixelFormat getPixelFormat() { return format_; }
+
+private:
+ int fd_;
+ struct gbm_device *gbm_device_;
+ PixelFormat format_;
+};
+
+} // namespace libcamera
@@ -24,6 +24,7 @@ libcamera_internal_headers = files([
'dma_buf_allocator.h',
'formats.h',
'framebuffer.h',
+ 'gbm.h',
'global_configuration.h',
'ipa_data_serializer.h',
'ipa_manager.h',
new file mode 100644
@@ -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 <errno.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_device_ = NULL;
+}
+
+GBM::~GBM()
+{
+ if (gbm_device_)
+ gbm_device_destroy(gbm_device_);
+}
+
+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:
+ close(fd_);
+ return -errno;
+}
+} //namespace libcamera
@@ -69,6 +69,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')
@@ -176,6 +186,7 @@ libcamera_deps += [
libcamera_base_private,
libcrypto,
libdl,
+ libgbm,
liblttng,
libudev,
libyaml,