libcamera: libcamera::eGL Class Reference

libcamera v0.7.0 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
libcamera::eGL Class Reference

Helper class for managing OpenGL ES operations. More...

## Public Member Functions
| eGL ()
| Construct an EGL helper.
| ~eGL ()
| Destroy the EGL helper.
int | initEGLContext ()
| Initialise the EGL context.
int | createInputDMABufTexture2D (eGLImage &eglImage, int fd)
| Create an input DMA-BUF backed texture.
int | createOutputDMABufTexture2D (eGLImage &eglImage, int fd)
| Create an output DMA-BUF backed texture.
void | destroyDMABufTexture (eGLImage &eglImage)
| Destroy a DMA-BUF texture's EGL image.
void | createTexture2D (eGLImage &eglImage, GLint format, uint32_t width, uint32_t height, void *data)
| Create a 2D texture from a memory buffer.
void | pushEnv (std::vector< std::string > &shaderEnv, const char *str)
| Add a preprocessor definition to shader environment.
void | makeCurrent ()
| Make the EGL context current for the calling thread.
int | compileVertexShader (GLuint &shaderId, const unsigned char *shaderData, unsigned int shaderDataLen, Span< const std::string > shaderEnv)
| Compile a vertex shader.
int | compileFragmentShader (GLuint &shaderId, const unsigned char *shaderData, unsigned int shaderDataLen, Span< const std::string > shaderEnv)
| Compile a fragment shader.
int | linkProgram (GLuint &programId, GLuint fragmentshaderId, GLuint vertexshaderId)
| Link a shader program.
void | dumpShaderSource (GLuint shaderId)
| Dump shader source code to the log.
void | useProgram (GLuint programId)
| Activate a shader program for rendering.
void | deleteProgram (GLuint programId)
| Delete a shader program.
void | syncOutput ()
| Synchronise rendering output.

## Detailed Description

Helper class for managing OpenGL ES operations.

It provides:

- EGL context setup and management
- Extension function pointer retrieval
- Shader compilation and program linking
- DMA-BUF texture creation and management
- Synchronisation primitives

This class is designed to work with zero-copy buffers via DMA-BUF file descriptors.

## Constructor & Destructor Documentation

## ◆ eGL()

libcamera::eGL::eGL | ( |  | ) |

Construct an EGL helper.

Creates an eGL instance with uninitialised context. Call initEGLContext() to set up the EGL display, context, and load extension functions.

## ◆ ~eGL()

libcamera::eGL::~eGL | ( |  | ) |

Destroy the EGL helper.

Destroys the EGL context and surface if they were successfully created.

## Member Function Documentation

## ◆ compileFragmentShader()

int libcamera::eGL::compileFragmentShader | ( | GLuint & | shaderId,
|  | const unsigned char * | shaderData,
|  | unsigned int | shaderDataLen,
|  | Span< const std::string > | shaderEnv )

Compile a fragment shader.

Parameters

  [out] | shaderId | OpenGL shader object ID
  [in] | shaderData | Pointer to shader source code
  [in] | shaderDataLen | Length of shader source in bytes
  [in] | shaderEnv | Span of preprocessor definitions to prepend

Compiles a fragment shader from source code with optional preprocessor definitions. On compilation failure, logs the shader info log.

Returns
  0 on success, or -EINVAL on compilation failure

## ◆ compileVertexShader()

int libcamera::eGL::compileVertexShader | ( | GLuint & | shaderId,
|  | const unsigned char * | shaderData,
|  | unsigned int | shaderDataLen,
|  | Span< const std::string > | shaderEnv )

Compile a vertex shader.

Parameters

  [out] | shaderId | OpenGL shader object ID
  [in] | shaderData | Pointer to shader source code
  [in] | shaderDataLen | Length of shader source in bytes
  [in] | shaderEnv | Span of preprocessor definitions to prepend

Compiles a vertex shader from source code with optional preprocessor definitions. On compilation failure, logs the shader info log.

Returns
  0 on success, or -EINVAL on compilation failure

## ◆ createInputDMABufTexture2D()

int libcamera::eGL::createInputDMABufTexture2D | ( | eGLImage & | eglImage,
|  | int | fd )

Create an input DMA-BUF backed texture.

Parameters

  [in,out] | eglImage | EGL image to associate with the DMA-BUF
  [in] | fd | DMA-BUF file descriptor

Creates an EGL image from a DMA-BUF file descriptor and binds it to a 2D texture for use as an input texture in shaders. The texture is configured with nearest filtering and clamp-to-edge wrapping.

Returns
  0 on success, or -ENODEV on failure

## ◆ createOutputDMABufTexture2D()

int libcamera::eGL::createOutputDMABufTexture2D | ( | eGLImage & | eglImage,
|  | int | fd )

Create an output DMA-BUF backed texture.

Parameters

  [in,out] | eglImage | EGL image to associate with the DMA-BUF
  [in] | fd | DMA-BUF file descriptor

Creates an EGL image from a DMA-BUF file descriptor and binds it to a 2D texture, then attaches it to a framebuffer object for use as a render target. This enables zero-copy rendering directly to the DMA-BUF.

Returns
  0 on success, or -ENODEV on failure

## ◆ createTexture2D()

void libcamera::eGL::createTexture2D | ( | eGLImage & | eglImage,
|  | GLint | format,
|  | uint32_t | width,
|  | uint32_t | height,
|  | void * | data )

Create a 2D texture from a memory buffer.

Parameters

  [in,out] | eglImage | EGL image to associate with the texture
  [in] | format | OpenGL internal format (e.g., GL_RGB, GL_RGBA)
  [in] | width | Texture width in pixels
  [in] | height | Texture height in pixels
  [in] | data | Pointer to pixel data, or nullptr for uninitialised texture

Creates a 2D texture from a CPU-accessible memory buffer. The texture is configured with nearest filtering and clamp-to-edge wrapping. This is useful for uploading static data like lookup tables or uniform color matrices to the GPU.

## ◆ deleteProgram()

void libcamera::eGL::deleteProgram | ( | GLuint | programId | ) |

Delete a shader program.

Parameters

  [in] | programId | OpenGL program object ID

Deletes a shader program and frees associated resources. The program must not be currently in use.

## ◆ destroyDMABufTexture()

void libcamera::eGL::destroyDMABufTexture | ( | eGLImage & | eglImage | ) |

Destroy a DMA-BUF texture's EGL image.

Parameters

  [in,out] | eglImage | EGL image to destroy

Destroys the EGL image associated with a DMA-BUF texture. The OpenGL texture and framebuffer objects are destroyed separately in the eGLImage destructor.

## ◆ dumpShaderSource()

void libcamera::eGL::dumpShaderSource | ( | GLuint | shaderId | ) |

Dump shader source code to the log.

Parameters

  [in] | shaderId | OpenGL shader object ID

Retrieves and logs the complete source code of a compiled shader. Useful for debugging shader compilation issues.

## ◆ initEGLContext()

int libcamera::eGL::initEGLContext | ( |  | ) |

Initialise the EGL context.

Sets up the EGL display, creates an OpenGL ES 2.0 context, and retrieves function pointers for required extensions including:

- eglCreateImageKHR / eglDestroyImageKHR
- glEGLImageTargetTexture2DOES

Returns
  0 on success, or -ENODEV on failure

## ◆ linkProgram()

int libcamera::eGL::linkProgram | ( | GLuint & | programId,
|  | GLuint | vertexshaderId,
|  | GLuint | fragmentshaderId )

Link a shader program.

Parameters

  [out] | programId | OpenGL program object ID
  [in] | fragmentshaderId | Compiled fragment shader ID
  [in] | vertexshaderId | Compiled vertex shader ID

Links vertex and fragment shaders into an executable shader program. On link failure, logs the program info log and deletes the program.

Returns
  0 on success, or -ENODEV on link failure

## ◆ makeCurrent()

void libcamera::eGL::makeCurrent | ( |  | ) |

Make the EGL context current for the calling thread.

Binds the EGL context to the current thread, allowing OpenGL ES operations to be performed. Must be called from the thread that will perform rendering operations.

## ◆ pushEnv()

void libcamera::eGL::pushEnv | ( | std::vector< std::string > & | shaderEnv,
|  | const char * | str )

Add a preprocessor definition to shader environment.

Parameters

  [in,out] | shaderEnv | Vector of shader environment strings
  [in] | str | Preprocessor definition string (e.g., "#define APPLY_RGB_PARAMETERS")

Appends a preprocessor definition to the shader environment vector. These definitions are prepended to shader source code during compilation.

## ◆ syncOutput()

void libcamera::eGL::syncOutput | ( |  | ) |

Synchronise rendering output.

Sychronise here. Calls glFinish() right now.

## ◆ useProgram()

void libcamera::eGL::useProgram | ( | GLuint | programId | ) |

Activate a shader program for rendering.

Parameters

  [in] | programId | OpenGL program object ID

Sets the specified program as the current rendering program. All subsequent draw calls will use this program's shaders.

The documentation for this class was generated from the following files:

- include/libcamera/internal/egl.h
- src/libcamera/egl.cpp