Files
external_libcamera/include/libcamera/internal/egl.h
Bryan O'Donoghue 99fd2e669c libcamera: software_isp: egl: Add a eGL base helper class
Introduce an eGL base helper class which provides an eGL context based on a
passed width and height.

The initGLContext function could be overloaded to provide an interface to a
real display.

A set of helper functions is provided to compile and link GLSL shaders.
linkShaderProgram currently compiles vertex/fragment pairs but could be
overloaded or passed a parameter to link a compute shader instead.

Breaking the eGL interface away from debayering - allows to use the eGL
context inside of a dma-buf heap cleanly, reuse that context inside of a
debayer layer and conceivably reuse the context in a multi-stage shader
pass.

Small note the image_attrs[] array doesn't pass checkstyle.py however the
elements of the array are in pairs.

Acked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
[bod: Takes fix from Hans for constructor stride bpp]
[bod: Drops eglClientWaitSync in favour of glFinish Robert/Milan]
Co-developed-by: Hans de Goede <johannes.goede@oss.qualcomm.com>
Signed-off-by: Hans de Goede <johannes.goede@oss.qualcomm.com>
Co-developed-by: Milan Zamazal <mzamazal@redhat.com>
Signed-off-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>
2026-01-07 17:02:57 +00:00

153 lines
4.6 KiB
C++

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2024, Linaro Ltd.
*
* Authors:
* Bryan O'Donoghue <bryan.odonoghue@linaro.org>
*
*/
#pragma once
#include <sys/types.h>
#include <unistd.h>
#include <libcamera/base/log.h>
#include <libcamera/base/span.h>
#include <libcamera/base/utils.h>
#include "libcamera/internal/gbm.h"
#define EGL_EGLEXT_PROTOTYPES
#include <EGL/egl.h>
#include <EGL/eglext.h>
#define GL_GLEXT_PROTOTYPES
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
namespace libcamera {
LOG_DECLARE_CATEGORY(eGL)
/**
* \class eGLImage
* \brief Helper class for managing EGL image resources
*
* The eGLImage class encapsulates OpenGL ES texture and framebuffer objects
* along with their associated EGL image. It aggregates handles, descriptors,
* and routines for managing textures that can be associated with shader
* uniform IDs.
*
* This class is particularly useful for managing DMA-BUF backed textures
* in zero-copy rendering pipelines, where textures are bound to specific
* texture units and can be used as both input textures and render targets.
*/
class eGLImage
{
public:
/**
* \brief Construct an eGLImage with explicit stride
* \param[in] width Image width in pixels
* \param[in] height Image height in pixels
* \param[in] bpp Bytes per pixel
* \param[in] stride Row stride in bytes
* \param[in] texture_unit OpenGL texture unit
* \param[in] texture_unit_uniform_id Shader uniform ID
*/
eGLImage(uint32_t width, uint32_t height, uint32_t bpp, uint32_t stride, GLenum texture_unit, uint32_t texture_unit_uniform_id)
: width_(width), height_(height), stride_(stride),
framesize_(stride * height), bpp_(bpp),
texture_unit_uniform_id_(texture_unit_uniform_id),
texture_unit_(texture_unit)
{
glGenTextures(1, &texture_);
glGenFramebuffers(1, &fbo_);
}
/**
* \brief Construct an eGLImage with automatic stride calculation
*/
eGLImage(uint32_t width, uint32_t height, uint32_t bpp, GLenum texture_unit, uint32_t texture_unit_uniform_id)
: eGLImage(width, height, bpp, utils::alignUp(width * bpp / 8, 256),
texture_unit, texture_unit_uniform_id)
{
}
/**
* \brief Destroy the eGLImage
*
* Cleans up OpenGL resources by deleting the framebuffer object and
* texture.
*/
~eGLImage()
{
glDeleteFramebuffers(1, &fbo_);
glDeleteTextures(1, &texture_);
}
uint32_t width_; /**< Image width in pixels */
uint32_t height_; /**< Image height in pixels */
uint32_t stride_; /**< Row stride in bytes */
uint32_t offset_; /**< Buffer offset (reserved for future use) */
uint32_t framesize_; /**< Total frame size in bytes (stride * height) */
uint32_t bpp_; /**< Bytes per pixel */
uint32_t texture_unit_uniform_id_; /**< Shader uniform id for texture unit */
GLenum texture_unit_; /**< Texture unit associated with this image eg (GL_TEXTURE0) */
GLuint texture_; /**< OpenGL texture object ID */
GLuint fbo_; /**< OpenGL frame buffer object ID */
EGLImageKHR image_; /**< EGL Image handle */
private:
LIBCAMERA_DISABLE_COPY_AND_MOVE(eGLImage)
};
class eGL
{
public:
eGL();
~eGL();
int initEGLContext(GBM *gbmContext);
void cleanUp();
int createInputDMABufTexture2D(eGLImage &eglImage, int fd);
int createOutputDMABufTexture2D(eGLImage &eglImage, int fd);
void destroyDMABufTexture(eGLImage &eglImage);
void createTexture2D(eGLImage &eglImage, GLint format, uint32_t width, uint32_t height, void *data);
void pushEnv(std::vector<std::string> &shaderEnv, const char *str);
void makeCurrent();
int compileVertexShader(GLuint &shaderId, const unsigned char *shaderData,
unsigned int shaderDataLen,
Span<const std::string> shaderEnv);
int compileFragmentShader(GLuint &shaderId, const unsigned char *shaderData,
unsigned int shaderDataLen,
Span<const std::string> shaderEnv);
int linkProgram(GLuint &programId, GLuint fragmentshaderId, GLuint vertexshaderId);
void dumpShaderSource(GLuint shaderId);
void useProgram(GLuint programId);
void deleteProgram(GLuint programId);
void syncOutput();
private:
LIBCAMERA_DISABLE_COPY_AND_MOVE(eGL)
pid_t tid_;
EGLDisplay display_ = EGL_NO_DISPLAY;
EGLContext context_ = EGL_NO_CONTEXT;
EGLSurface surface_ = EGL_NO_SURFACE;
int compileShader(int shaderType, GLuint &shaderId, const unsigned char *shaderData,
unsigned int shaderDataLen,
Span<const std::string> shaderEnv);
int createDMABufTexture2D(eGLImage &eglImage, int fd, bool output);
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
};
} //namespace libcamera