libcamera v0.8.0+rpt20261104
Supporting cameras in Linux since 2019
Loading...
Searching...
No Matches
egl.h
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2024, Linaro Ltd.
4 *
5 * Authors:
6 * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
7 *
8 */
9
10#pragma once
11
12#include <span>
13#include <sys/types.h>
14#include <unistd.h>
15
16#include <libcamera/base/log.h>
19
20/*
21 * Workaround for build issues on Mesa <= 22.2, see
22 * https://github.com/KhronosGroup/EGL-Registry/pull/130
23 */
24#ifndef EGL_NO_X11
25#define EGL_NO_X11
26#endif
27
28#define EGL_EGLEXT_PROTOTYPES
29#include <EGL/egl.h>
30#include <EGL/eglext.h>
31#define GL_GLEXT_PROTOTYPES
32#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
34
35namespace libcamera {
36
38
39
53{
54public:
64 eGLImage(GLint format, uint32_t width, uint32_t height, uint32_t stride, GLenum texture_unit, uint32_t texture_unit_uniform_id)
65 : format_(format), width_(width), height_(height), stride_(stride),
66 framesize_(stride * height),
67 texture_unit_uniform_id_(texture_unit_uniform_id),
68 texture_unit_(texture_unit)
69 {
70 glGenTextures(1, &texture_);
71 glGenFramebuffers(1, &fbo_);
72 }
73
81 {
82 glDeleteFramebuffers(1, &fbo_);
83 glDeleteTextures(1, &texture_);
84 }
85
86 GLint format_;
87 uint32_t width_;
88 uint32_t height_;
89 uint32_t stride_;
90 uint32_t offset_;
91 uint32_t framesize_;
94 GLuint texture_;
95 GLuint fbo_;
96 bool dmabuf_import_failed_ = false;
98private:
100};
101
102class eGL
103{
104public:
105 eGL(EGLDisplay display);
106 ~eGL();
107
108 int initEGLContext();
109 void resetEGLContext();
110 static EGLDisplay probeDisplay();
111
112 int createInputDMABufTexture2D(eGLImage &eglImage, int fd);
113 int createOutputDMABufTexture2D(eGLImage &eglImage, int fd);
114 void createTexture2D(eGLImage &eglImage, const void *data, GLint filter);
115 void updateTexture2D(eGLImage &eglImage, const void *data);
116 void createOutputTexture2D(eGLImage &eglImage);
117
118 int attachTextureToFBO(eGLImage &eglImage);
119 void activateBindTexture(eGLImage &eglImage);
120
121 void pushEnv(std::vector<std::string> &shaderEnv, const char *str);
122
123 int compileVertexShader(GLuint &shaderId, std::span<const unsigned char> shaderData,
124 std::span<const std::string> shaderEnv);
125 int compileFragmentShader(GLuint &shaderId, std::span<const unsigned char> shaderData,
126 std::span<const std::string> shaderEnv);
127 int linkProgram(GLuint &programId, GLuint fragmentshaderId, GLuint vertexshaderId);
128 void dumpShaderSource(GLuint shaderId);
129 void syncOutput();
130 void flushOutput();
131
132 void assertThread() const
133 {
134 ASSERT(tid_ == Thread::currentId());
135 }
136
137private:
139
140 pid_t tid_ = -1;
141
142 EGLDisplay display_ = EGL_NO_DISPLAY;
143 EGLContext context_ = EGL_NO_CONTEXT;
144
145 int compileShader(int shaderType, GLuint &shaderId, std::span<const unsigned char> shaderData,
146 std::span<const std::string> shaderEnv);
147
148 int createDMABufTexture2D(eGLImage &eglImage, int fd, bool output);
149
150 void makeCurrent();
151
152 struct VTable {
153 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
154 PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
155 PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
156 } vtable_ = {};
157};
158} //namespace libcamera
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
Definition class.h:29
static pid_t currentId()
Retrieve the ID of the current thread.
Definition thread.cpp:511
Helper class for managing EGL image resources.
Definition egl.h:53
uint32_t offset_
Definition egl.h:90
~eGLImage()
Destroy the eGLImage.
Definition egl.h:80
GLuint texture_
Definition egl.h:94
GLuint fbo_
Definition egl.h:95
uint32_t stride_
Definition egl.h:89
uint32_t height_
Definition egl.h:88
GLenum texture_unit_
Definition egl.h:93
eGLImage(GLint format, 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:64
uint32_t framesize_
Definition egl.h:91
GLint format_
Definition egl.h:86
uint32_t texture_unit_uniform_id_
Definition egl.h:92
uint32_t width_
Definition egl.h:87
Helper class for managing OpenGL ES operations.
Definition egl.h:103
void createTexture2D(eGLImage &eglImage, const void *data, GLint filter)
Create a 2D texture from a memory buffer.
Definition egl.cpp:289
void resetEGLContext()
Destroy the EGL context.
Definition egl.cpp:463
void activateBindTexture(eGLImage &eglImage)
Activate a texture unit and bind a texture to that unit.
Definition egl.cpp:155
void flushOutput()
Flush the rendering pipeline.
Definition egl.cpp:104
int createInputDMABufTexture2D(eGLImage &eglImage, int fd)
Create an input DMA-BUF backed texture.
Definition egl.cpp:252
int linkProgram(GLuint &programId, GLuint fragmentshaderId, GLuint vertexshaderId)
Link a shader program.
Definition egl.cpp:636
int initEGLContext()
Initialise the EGL context.
Definition egl.cpp:380
int createOutputDMABufTexture2D(eGLImage &eglImage, int fd)
Create an output DMA-BUF backed texture.
Definition egl.cpp:271
void pushEnv(std::vector< std::string > &shaderEnv, const char *str)
Add a preprocessor definition to shader environment.
Definition egl.cpp:499
void createOutputTexture2D(eGLImage &eglImage)
Create a 2D texture attached to an FBO for render-to-texture.
Definition egl.cpp:359
void assertThread() const
Assert that the execution is in the right thread.
Definition egl.h:132
int compileFragmentShader(GLuint &shaderId, std::span< const unsigned char > shaderData, std::span< const std::string > shaderEnv)
Compile a fragment shader.
Definition egl.cpp:536
int attachTextureToFBO(eGLImage &eglImage)
Attach a texture to a frame-buffer-object.
Definition egl.cpp:128
void syncOutput()
Synchronise rendering output.
Definition egl.cpp:91
int compileVertexShader(GLuint &shaderId, std::span< const unsigned char > shaderData, std::span< const std::string > shaderEnv)
Compile a vertex shader.
Definition egl.cpp:518
~eGL()
Destroy the EGL helper.
Definition egl.cpp:80
void dumpShaderSource(GLuint shaderId)
Dump shader source code to the log.
Definition egl.cpp:605
void updateTexture2D(eGLImage &eglImage, const void *data)
Update a 2D texture already created.
Definition egl.cpp:342
static EGLDisplay probeDisplay()
Try to create an EGL display for surfaceless rendering.
Definition egl.cpp:314
Logging infrastructure.
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Definition log.h:51
#define ASSERT(condition)
Abort program execution if assertion fails.
Definition log.h:159
Top-level libcamera namespace.
Definition backtrace.h:17
Thread support.
Miscellaneous utility functions.