libcamera: software_isp: gbm: Add a GBM helper class for GPU surface access

A helper class to interact with GBM. This will allow us to specify the
internal storage format of the GPU when making a texture for the Debayer
vertex/fragment shaders and thus ensure we receive an uncompressed and
untiled output buffer.

Acked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
Reviewed-by: Robert Mader <robert.mader@collabora.com>
Tested-by: Robert Mader <robert.mader@collabora.com>
Tested-by: Hans de Goede <johannes.goede@oss.qualcomm.com> # ThinkPad T14s gen 6 (arm64) ov02c10 + X1c gen 12 ov08x40
Tested-by: Kieran Bingham <kieran.bingham@ideasonboard.com> # Lenovo X13s
Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Bryan O'Donoghue
2026-01-06 17:00:37 +00:00
committed by Kieran Bingham
parent a1a6253ff9
commit c60b1ce819
4 changed files with 173 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2024, Linaro Ltd.
*
* Authors:
* Bryan O'Donoghue <bryan.odonoghue@linaro.org>
*
* Helper class for managing GBM interactions
*/
#pragma once
#include <gbm.h>
#include <libcamera/base/log.h>
#include <libcamera/base/unique_fd.h>
#include <libcamera/formats.h>
namespace libcamera {
LOG_DECLARE_CATEGORY(GBM)
class GBM
{
public:
GBM();
~GBM();
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 *device() const { return gbmDevice_; }
/**
* \brief Retrieve the pixel format
*
* \return The PixelFormat used by this GBM instance (ARGB8888)
*/
PixelFormat format() const { return format_; }
private:
LIBCAMERA_DISABLE_COPY_AND_MOVE(GBM)
UniqueFD fd_;
struct gbm_device *gbmDevice_ = nullptr;
PixelFormat format_;
};
} /* namespace libcamera */

View File

@@ -23,6 +23,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',

107
src/libcamera/gbm.cpp Normal file
View File

@@ -0,0 +1,107 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2024, Linaro Ltd.
*
* Authors:
* Bryan O'Donoghue <bryan.odonoghue@linaro.org>
*
* 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)
/**
* \class 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.
*/
/**
*\var GBM::fd_
*\brief file descriptor to DRI device
*/
/**
*\var GBM::gbmDevice_
*\brief Pointer to GBM device structure derived from fd_
*/
/**
*\var GBM::format_
*\brief Pixel format the GBM surface was created in
*/
/**
*\brief GBM constructor.
*
* Creates a GBM instance with uninitialised state.
*/
GBM::GBM()
{
}
/**
*\brief GBM destructor
*
* Cleans up the GBM device if it was successfully created, and closes
* the associated file descriptor.
*/
GBM::~GBM()
{
if (gbmDevice_)
gbm_device_destroy(gbmDevice_);
}
/**
* \brief Create and initialize a GBM device
*
* \todo Get dri device name from envOption setting
*
* 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 GBM::createDevice()
{
const char dri_node[] = "/dev/dri/renderD128";
int fd = open(dri_node, O_RDWR | O_CLOEXEC | O_NOCTTY);
if (fd < 0) {
LOG(GBM, Error) << "Open " << dri_node << " fail " << strerror(errno);
return -errno;
}
fd_ = UniqueFD(fd);
gbmDevice_ = gbm_create_device(fd_.get());
if (!gbmDevice_) {
LOG(GBM, Error) << "gbm_create_device fail" << strerror(errno);
return -errno;
}
format_ = libcamera::formats::ARGB8888;
return 0;
}
} /* namespace libcamera */

View File

@@ -70,6 +70,15 @@ libcamera_deps = []
libatomic = cc.find_library('atomic', required : false)
libthreads = dependency('threads')
libgbm = dependency('gbm', required : false)
gbm_works = cc.check_header('gbm.h', required: false)
if libgbm.found() and gbm_works
libcamera_internal_sources += files([
'gbm.cpp',
])
endif
subdir('base')
subdir('converter')
subdir('ipa')
@@ -178,6 +187,7 @@ libcamera_deps += [
libcamera_base_private,
libcrypto,
libdl,
libgbm,
liblttng,
libudev,
libyaml,