libcamera v0.7.2 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:
61 eGLImage(GLint format, uint32_t width, uint32_t height, uint32_t stride, GLenum texture_unit, uint32_t texture_unit_uniform_id)
62 : format_(format), width_(width), height_(height), stride_(stride),
63 framesize_(stride * height),
64 texture_unit_uniform_id_(texture_unit_uniform_id),
65 texture_unit_(texture_unit)
66 {
67 glGenTextures(1, &texture_);
68 glGenFramebuffers(1, &fbo_);
69 }
70
77 ~eGLImage()
78 {
79 glDeleteFramebuffers(1, &fbo_);
80 glDeleteTextures(1, &texture_);
81 }
82
83 GLint format_;
84 uint32_t width_;
85 uint32_t height_;
86 uint32_t stride_;
87 uint32_t offset_;
88 uint32_t framesize_;
89 uint32_t texture_unit_uniform_id_;
90 GLenum texture_unit_;
91 GLuint texture_;
92 GLuint fbo_;
93 bool dmabuf_import_failed_ = false;
94
95 private:
96 LIBCAMERA_DISABLE_COPY_AND_MOVE(eGLImage)
97};
98
99 class eGL
100{
101 public:
102 eGL();
103 ~eGL();
104
105 int initEGLContext();
106 static bool isAvailable();
107
108 int createInputDMABufTexture2D(eGLImage &eglImage, int fd);
109 int createOutputDMABufTexture2D(eGLImage &eglImage, int fd);
110 void createTexture2D(eGLImage &eglImage, void *data);
111 void updateTexture2D(eGLImage &eglImage, void *data);
112 void createOutputTexture2D(eGLImage &eglImage);
113
114 int attachTextureToFBO(eGLImage &eglImage);
115 void activateBindTexture(eGLImage &eglImage);
116
117 void pushEnv(std::vector<std::string> &shaderEnv, const char *str);
118 void makeCurrent();
119
120 int compileVertexShader(GLuint &shaderId, Span<const unsigned char> shaderData,
121 Span<const std::string> shaderEnv);
122 int compileFragmentShader(GLuint &shaderId, Span<const unsigned char> shaderData,
123 Span<const std::string> shaderEnv);
124 int linkProgram(GLuint &programId, GLuint fragmentshaderId, GLuint vertexshaderId);
125 void dumpShaderSource(GLuint shaderId);
126 void useProgram(GLuint programId);
127 void deleteProgram(GLuint programId);
128 void syncOutput();
129 void flushOutput();
130
131 private:
132 LIBCAMERA_DISABLE_COPY_AND_MOVE(eGL)
133
134 pid_t tid_;
135
136 EGLDisplay display_ = EGL_NO_DISPLAY;
137 EGLContext context_ = EGL_NO_CONTEXT;
138 EGLSurface surface_ = EGL_NO_SURFACE;
139
140 static EGLDisplay probeDisplay();
141 int compileShader(int shaderType, GLuint &shaderId, Span<const unsigned char> shaderData,
142 Span<const std::string> shaderEnv);
143
144 int createDMABufTexture2D(eGLImage &eglImage, int fd, bool output);
145
146 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
147 PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
148 PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
149 PFNGLGETSTRINGPROC glGetString;
150};
151} //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::offset_
uint32_t offset_
Definition egl.h:87
libcamera::eGLImage::~eGLImage
~eGLImage()
Destroy the eGLImage.
Definition egl.h:77
libcamera::eGLImage::texture_
GLuint texture_
Definition egl.h:91
libcamera::eGLImage::fbo_
GLuint fbo_
Definition egl.h:92
libcamera::eGLImage::stride_
uint32_t stride_
Definition egl.h:86
libcamera::eGLImage::height_
uint32_t height_
Definition egl.h:85
libcamera::eGLImage::dmabuf_import_failed_
bool dmabuf_import_failed_
Definition egl.h:93
libcamera::eGLImage::texture_unit_
GLenum texture_unit_
Definition egl.h:90
libcamera::eGLImage::eGLImage
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:61
libcamera::eGLImage::framesize_
uint32_t framesize_
Definition egl.h:88
libcamera::eGLImage::format_
GLint format_
Definition egl.h:83
libcamera::eGLImage::texture_unit_uniform_id_
uint32_t texture_unit_uniform_id_
Definition egl.h:89
libcamera::eGLImage::width_
uint32_t width_
Definition egl.h:84
libcamera::eGL
Helper class for managing OpenGL ES operations.
Definition egl.h:100
libcamera::eGL::isAvailable
static bool isAvailable()
Probe whether EGL surfaceless rendering is available.
Definition egl.cpp:336
libcamera::eGL::activateBindTexture
void activateBindTexture(eGLImage &eglImage)
Activate a texture unit and bind a texture to that unit.
Definition egl.cpp:151
libcamera::eGL::eGL
eGL()
Construct an EGL helper.
Definition egl.cpp:71
libcamera::eGL::flushOutput
void flushOutput()
Flush the rendering pipeline.
Definition egl.cpp:108
libcamera::eGL::createInputDMABufTexture2D
int createInputDMABufTexture2D(eGLImage &eglImage, int fd)
Create an input DMA-BUF backed texture.
Definition egl.cpp:248
libcamera::eGL::linkProgram
int linkProgram(GLuint &programId, GLuint fragmentshaderId, GLuint vertexshaderId)
Link a shader program.
Definition egl.cpp:654
libcamera::eGL::initEGLContext
int initEGLContext()
Initialise the EGL context.
Definition egl.cpp:386
libcamera::eGL::createOutputDMABufTexture2D
int createOutputDMABufTexture2D(eGLImage &eglImage, int fd)
Create an output DMA-BUF backed texture.
Definition egl.cpp:267
libcamera::eGL::pushEnv
void pushEnv(std::vector< std::string > &shaderEnv, const char *str)
Add a preprocessor definition to shader environment.
Definition egl.cpp:517
libcamera::eGL::createOutputTexture2D
void createOutputTexture2D(eGLImage &eglImage)
Create a 2D texture attached to an FBO for render-to-texture.
Definition egl.cpp:370
libcamera::eGL::makeCurrent
void makeCurrent()
Make the EGL context current for the calling thread.
Definition egl.cpp:472
libcamera::eGL::compileVertexShader
int compileVertexShader(GLuint &shaderId, Span< const unsigned char > shaderData, Span< const std::string > shaderEnv)
Compile a vertex shader.
Definition egl.cpp:536
libcamera::eGL::attachTextureToFBO
int attachTextureToFBO(eGLImage &eglImage)
Attach a texture to a frame-buffer-object.
Definition egl.cpp:124
libcamera::eGL::syncOutput
void syncOutput()
Synchronise rendering output.
Definition egl.cpp:95
libcamera::eGL::useProgram
void useProgram(GLuint programId)
Activate a shader program for rendering.
Definition egl.cpp:488
libcamera::eGL::updateTexture2D
void updateTexture2D(eGLImage &eglImage, void *data)
Update a 2D texture already created.
Definition egl.cpp:353
libcamera::eGL::dumpShaderSource
void dumpShaderSource(GLuint shaderId)
Dump shader source code to the log.
Definition egl.cpp:623
libcamera::eGL::createTexture2D
void createTexture2D(eGLImage &eglImage, void *data)
Create a 2D texture from a memory buffer.
Definition egl.cpp:284
libcamera::eGL::deleteProgram
void deleteProgram(GLuint programId)
Delete a shader program.
Definition egl.cpp:502
libcamera::eGL::compileFragmentShader
int compileFragmentShader(GLuint &shaderId, Span< const unsigned char > shaderData, Span< const std::string > shaderEnv)
Compile a fragment shader.
Definition egl.cpp:554
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.