libcamera v0.7.1 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>
17#include <libcamera/base/utils.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
32 namespace libcamera {
33
34 LOG_DECLARE_CATEGORY(eGL)
35
36
49class eGLImage
50{
51 public:
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
76 ~eGLImage()
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_;
87 uint32_t texture_unit_uniform_id_;
88 GLenum texture_unit_;
89 GLuint texture_;
90 GLuint fbo_;
91
92 private:
93 LIBCAMERA_DISABLE_COPY_AND_MOVE(eGLImage)
94};
95
96 class eGL
97{
98 public:
99 eGL();
100 ~eGL();
101
102 int initEGLContext();
103
104 int createInputDMABufTexture2D(eGLImage &eglImage, int fd);
105 int createOutputDMABufTexture2D(eGLImage &eglImage, int fd);
106 void createTexture2D(eGLImage &eglImage, GLint format, uint32_t width, uint32_t height, void *data);
107
108 void pushEnv(std::vector<std::string> &shaderEnv, const char *str);
109 void makeCurrent();
110
111 int compileVertexShader(GLuint &shaderId, const unsigned char *shaderData,
112 unsigned int shaderDataLen,
113 Span<const std::string> shaderEnv);
114 int compileFragmentShader(GLuint &shaderId, const unsigned char *shaderData,
115 unsigned int shaderDataLen,
116 Span<const std::string> shaderEnv);
117 int linkProgram(GLuint &programId, GLuint fragmentshaderId, GLuint vertexshaderId);
118 void dumpShaderSource(GLuint shaderId);
119 void useProgram(GLuint programId);
120 void deleteProgram(GLuint programId);
121 void syncOutput();
122
123 private:
124 LIBCAMERA_DISABLE_COPY_AND_MOVE(eGL)
125
126 pid_t tid_;
127
128 EGLDisplay display_ = EGL_NO_DISPLAY;
129 EGLContext context_ = EGL_NO_CONTEXT;
130 EGLSurface surface_ = EGL_NO_SURFACE;
131
132 int compileShader(int shaderType, GLuint &shaderId, const unsigned char *shaderData,
133 unsigned int shaderDataLen,
134 Span<const std::string> shaderEnv);
135
136 int createDMABufTexture2D(eGLImage &eglImage, int fd, bool output);
137
138 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
139 PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
140 PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
141 PFNGLGETSTRINGPROC glGetString;
142};
143} //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::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:97
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:431
libcamera::eGL::createInputDMABufTexture2D
int createInputDMABufTexture2D(eGLImage &eglImage, int fd)
Create an input DMA-BUF backed texture.
Definition egl.cpp:182
libcamera::eGL::linkProgram
int linkProgram(GLuint &programId, GLuint fragmentshaderId, GLuint vertexshaderId)
Link a shader program.
Definition egl.cpp:532
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:221
libcamera::eGL::initEGLContext
int initEGLContext()
Initialise the EGL context.
Definition egl.cpp:250
libcamera::eGL::createOutputDMABufTexture2D
int createOutputDMABufTexture2D(eGLImage &eglImage, int fd)
Create an output DMA-BUF backed texture.
Definition egl.cpp:201
libcamera::eGL::pushEnv
void pushEnv(std::vector< std::string > &shaderEnv, const char *str)
Add a preprocessor definition to shader environment.
Definition egl.cpp:392
libcamera::eGL::makeCurrent
void makeCurrent()
Make the EGL context current for the calling thread.
Definition egl.cpp:347
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:363
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:412
libcamera::eGL::dumpShaderSource
void dumpShaderSource(GLuint shaderId)
Dump shader source code to the log.
Definition egl.cpp:501
libcamera::eGL::deleteProgram
void deleteProgram(GLuint programId)
Delete a shader program.
Definition egl.cpp:377
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.