libcamera v0.7.0+1-4ceceb68
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 <sys/types.h>
13#include <unistd.h>
14
15#include <libcamera/base/log.h>
16#include <libcamera/base/span.h>
18
19/*
20 * Workaround for build issues on Mesa <= 22.2, see
21 * https://github.com/KhronosGroup/EGL-Registry/pull/130
22 */
23#define EGL_NO_X11
24
25#define EGL_EGLEXT_PROTOTYPES
26#include <EGL/egl.h>
27#include <EGL/eglext.h>
28#define GL_GLEXT_PROTOTYPES
29#include <GLES2/gl2.h>
30#include <GLES2/gl2ext.h>
31
32namespace libcamera {
33
35
36
50{
51public:
60 eGLImage(uint32_t width, uint32_t height, uint32_t stride, GLenum texture_unit, uint32_t texture_unit_uniform_id)
61 : width_(width), height_(height), stride_(stride),
62 framesize_(stride * height),
63 texture_unit_uniform_id_(texture_unit_uniform_id),
64 texture_unit_(texture_unit)
65 {
66 glGenTextures(1, &texture_);
67 glGenFramebuffers(1, &fbo_);
68 }
69
77 {
78 glDeleteFramebuffers(1, &fbo_);
79 glDeleteTextures(1, &texture_);
80 }
81
82 uint32_t width_;
83 uint32_t height_;
84 uint32_t stride_;
85 uint32_t offset_;
86 uint32_t framesize_;
89 GLuint texture_;
90 GLuint fbo_;
91 EGLImageKHR image_;
93private:
95};
96
97class eGL
98{
99public:
100 eGL();
101 ~eGL();
102
103 int initEGLContext();
104
105 int createInputDMABufTexture2D(eGLImage &eglImage, int fd);
106 int createOutputDMABufTexture2D(eGLImage &eglImage, int fd);
107 void destroyDMABufTexture(eGLImage &eglImage);
108 void createTexture2D(eGLImage &eglImage, GLint format, uint32_t width, uint32_t height, void *data);
109
110 void pushEnv(std::vector<std::string> &shaderEnv, const char *str);
111 void makeCurrent();
112
113 int compileVertexShader(GLuint &shaderId, const unsigned char *shaderData,
114 unsigned int shaderDataLen,
115 Span<const std::string> shaderEnv);
116 int compileFragmentShader(GLuint &shaderId, const unsigned char *shaderData,
117 unsigned int shaderDataLen,
118 Span<const std::string> shaderEnv);
119 int linkProgram(GLuint &programId, GLuint fragmentshaderId, GLuint vertexshaderId);
120 void dumpShaderSource(GLuint shaderId);
121 void useProgram(GLuint programId);
122 void deleteProgram(GLuint programId);
123 void syncOutput();
124
125private:
127
128 pid_t tid_;
129
130 EGLDisplay display_ = EGL_NO_DISPLAY;
131 EGLContext context_ = EGL_NO_CONTEXT;
132 EGLSurface surface_ = EGL_NO_SURFACE;
133
134 int compileShader(int shaderType, GLuint &shaderId, const unsigned char *shaderData,
135 unsigned int shaderDataLen,
136 Span<const std::string> shaderEnv);
137
138 int createDMABufTexture2D(eGLImage &eglImage, int fd, bool output);
139
140 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
141 PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
142 PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
143 PFNGLGETSTRINGPROC glGetString;
144};
145} //namespace libcamera
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
Definition class.h:29
Helper class for managing EGL image resources.
Definition egl.h:50
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
uint32_t offset_
Definition egl.h:85
~eGLImage()
Destroy the eGLImage.
Definition egl.h:76
GLuint texture_
Definition egl.h:89
GLuint fbo_
Definition egl.h:90
uint32_t stride_
Definition egl.h:84
uint32_t height_
Definition egl.h:83
GLenum texture_unit_
Definition egl.h:88
EGLImageKHR image_
Definition egl.h:91
uint32_t framesize_
Definition egl.h:86
uint32_t texture_unit_uniform_id_
Definition egl.h:87
uint32_t width_
Definition egl.h:82
Helper class for managing OpenGL ES operations.
Definition egl.h:98
eGL()
Construct an EGL helper.
Definition egl.cpp:70
int compileFragmentShader(GLuint &shaderId, const unsigned char *shaderData, unsigned int shaderDataLen, Span< const std::string > shaderEnv)
Compile a fragment shader.
Definition egl.cpp:448
void destroyDMABufTexture(eGLImage &eglImage)
Destroy a DMA-BUF texture's EGL image.
Definition egl.cpp:220
int createInputDMABufTexture2D(eGLImage &eglImage, int fd)
Create an input DMA-BUF backed texture.
Definition egl.cpp:186
int linkProgram(GLuint &programId, GLuint fragmentshaderId, GLuint vertexshaderId)
Link a shader program.
Definition egl.cpp:549
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
int initEGLContext()
Initialise the EGL context.
Definition egl.cpp:267
int createOutputDMABufTexture2D(eGLImage &eglImage, int fd)
Create an output DMA-BUF backed texture.
Definition egl.cpp:205
void pushEnv(std::vector< std::string > &shaderEnv, const char *str)
Add a preprocessor definition to shader environment.
Definition egl.cpp:409
void makeCurrent()
Make the EGL context current for the calling thread.
Definition egl.cpp:364
void syncOutput()
Synchronise rendering output.
Definition egl.cpp:94
void useProgram(GLuint programId)
Activate a shader program for rendering.
Definition egl.cpp:380
~eGL()
Destroy the EGL helper.
Definition egl.cpp:79
int compileVertexShader(GLuint &shaderId, const unsigned char *shaderData, unsigned int shaderDataLen, Span< const std::string > shaderEnv)
Compile a vertex shader.
Definition egl.cpp:429
void dumpShaderSource(GLuint shaderId)
Dump shader source code to the log.
Definition egl.cpp:518
void deleteProgram(GLuint programId)
Delete a shader program.
Definition egl.cpp:394
Logging infrastructure.
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Definition log.h:51
Top-level libcamera namespace.
Definition backtrace.h:17
Miscellaneous utility functions.