Loading...
Searching...
No Matches
egl.h
/* 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>
/*
* Workaround for build issues on Mesa <= 22.2, see
* https://github.com/KhronosGroup/EGL-Registry/pull/130
*/
#define EGL_NO_X11
#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
{
public:
eGLImage(uint32_t width, uint32_t height, uint32_t stride, GLenum texture_unit, uint32_t texture_unit_uniform_id)
: width_(width), height_(height), stride_(stride),
framesize_(stride * height),
texture_unit_uniform_id_(texture_unit_uniform_id),
texture_unit_(texture_unit)
{
glGenTextures(1, &texture_);
glGenFramebuffers(1, &fbo_);
}
~eGLImage()
{
glDeleteFramebuffers(1, &fbo_);
glDeleteTextures(1, &texture_);
}
uint32_t width_;
uint32_t height_;
uint32_t stride_;
uint32_t offset_;
uint32_t framesize_;
uint32_t texture_unit_uniform_id_;
GLenum texture_unit_;
GLuint texture_;
GLuint fbo_;
EGLImageKHR image_;
private:
LIBCAMERA_DISABLE_COPY_AND_MOVE(eGLImage)
};
class eGL
{
public:
eGL();
~eGL();
int initEGLContext();
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;
PFNGLGETSTRINGPROC glGetString;
};
} //namespace libcamera
LIBCAMERA_DISABLE_COPY_AND_MOVE
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
Definition class.h:29
libcamera::eGLImage
Helper class for managing EGL image resources.
Definition egl.h:50
libcamera::eGLImage::eGLImage
eGLImage(uint32_t width, uint32_t height, uint32_t stride, GLenum texture_unit, uint32_t texture_unit_uniform_id)
Construct an eGLImage with explicit stride.
Definition egl.h:60
libcamera::eGLImage::offset_
uint32_t offset_
Definition egl.h:85
libcamera::eGLImage::~eGLImage
~eGLImage()
Destroy the eGLImage.
Definition egl.h:76
libcamera::eGLImage::texture_
GLuint texture_
Definition egl.h:89
libcamera::eGLImage::fbo_
GLuint fbo_
Definition egl.h:90
libcamera::eGLImage::stride_
uint32_t stride_
Definition egl.h:84
libcamera::eGLImage::height_
uint32_t height_
Definition egl.h:83
libcamera::eGLImage::texture_unit_
GLenum texture_unit_
Definition egl.h:88
libcamera::eGLImage::image_
EGLImageKHR image_
Definition egl.h:91
libcamera::eGLImage::framesize_
uint32_t framesize_
Definition egl.h:86
libcamera::eGLImage::texture_unit_uniform_id_
uint32_t texture_unit_uniform_id_
Definition egl.h:87
libcamera::eGLImage::width_
uint32_t width_
Definition egl.h:82
libcamera::eGL
Helper class for managing OpenGL ES operations.
Definition egl.h:98
libcamera::eGL::eGL
eGL()
Construct an EGL helper.
Definition egl.cpp:70
libcamera::eGL::compileFragmentShader
int compileFragmentShader(GLuint &shaderId, const unsigned char *shaderData, unsigned int shaderDataLen, Span< const std::string > shaderEnv)
Compile a fragment shader.
Definition egl.cpp:448
libcamera::eGL::destroyDMABufTexture
void destroyDMABufTexture(eGLImage &eglImage)
Destroy a DMA-BUF texture's EGL image.
Definition egl.cpp:220
libcamera::eGL::createInputDMABufTexture2D
int createInputDMABufTexture2D(eGLImage &eglImage, int fd)
Create an input DMA-BUF backed texture.
Definition egl.cpp:186
libcamera::eGL::linkProgram
int linkProgram(GLuint &programId, GLuint fragmentshaderId, GLuint vertexshaderId)
Link a shader program.
Definition egl.cpp:549
libcamera::eGL::createTexture2D
void createTexture2D(eGLImage &eglImage, GLint format, uint32_t width, uint32_t height, void *data)
Create a 2D texture from a memory buffer.
Definition egl.cpp:238
libcamera::eGL::initEGLContext
int initEGLContext()
Initialise the EGL context.
Definition egl.cpp:267
libcamera::eGL::createOutputDMABufTexture2D
int createOutputDMABufTexture2D(eGLImage &eglImage, int fd)
Create an output DMA-BUF backed texture.
Definition egl.cpp:205
libcamera::eGL::pushEnv
void pushEnv(std::vector< std::string > &shaderEnv, const char *str)
Add a preprocessor definition to shader environment.
Definition egl.cpp:409
libcamera::eGL::makeCurrent
void makeCurrent()
Make the EGL context current for the calling thread.
Definition egl.cpp:364
libcamera::eGL::syncOutput
void syncOutput()
Synchronise rendering output.
Definition egl.cpp:94
libcamera::eGL::useProgram
void useProgram(GLuint programId)
Activate a shader program for rendering.
Definition egl.cpp:380
libcamera::eGL::~eGL
~eGL()
Destroy the EGL helper.
Definition egl.cpp:79
libcamera::eGL::compileVertexShader
int compileVertexShader(GLuint &shaderId, const unsigned char *shaderData, unsigned int shaderDataLen, Span< const std::string > shaderEnv)
Compile a vertex shader.
Definition egl.cpp:429
libcamera::eGL::dumpShaderSource
void dumpShaderSource(GLuint shaderId)
Dump shader source code to the log.
Definition egl.cpp:518
libcamera::eGL::deleteProgram
void deleteProgram(GLuint programId)
Delete a shader program.
Definition egl.cpp:394
log.h
Logging infrastructure.
LOG_DECLARE_CATEGORY
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Definition log.h:51
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
utils.h
Miscellaneous utility functions.