Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SimpleConverter::DmabufImage SimpleConverter::importDmabuf(int fdesc, Size pixelSize, PixelFormat format)
- {
- LOG(SimplePipeline, Debug) << "IMPORT DMABUF CALLED";
- int bytes_per_pixel = 0;
- if (format == libcamera::formats::ARGB8888) {
- bytes_per_pixel = 4;
- }
- if (format == libcamera::formats::R8) {
- bytes_per_pixel = 1;
- }
- if (bytes_per_pixel == 0) {
- LOG(SimplePipeline, Error) << "Bytes per pixel not modified ";
- }
- EGLint const attrs[] = {
- EGL_WIDTH,
- (int)pixelSize.width,
- EGL_HEIGHT,
- (int)pixelSize.height,
- EGL_LINUX_DRM_FOURCC_EXT,
- (int)format.fourcc(),
- EGL_DMA_BUF_PLANE0_FD_EXT,
- fdesc,
- EGL_DMA_BUF_PLANE0_OFFSET_EXT,
- 0,
- EGL_DMA_BUF_PLANE0_PITCH_EXT,
- (int)pixelSize.width * bytes_per_pixel,
- EGL_NONE,
- };
- EGLImageKHR image = eglCreateImageKHR(
- display_,
- EGL_NO_CONTEXT,
- EGL_LINUX_DMA_BUF_EXT,
- NULL,
- attrs);
- int e = glGetError();
- if (e != GL_NO_ERROR)
- LOG(SimplePipeline, Error) << "GL_ERROR: " << e;
- GLuint texture;
- glGenTextures(1, &texture);
- struct DmabufImage img = {
- .texture = texture,
- .image = image,
- };
- return img;
- }
- int SimpleConverter::start()
- {
- LOG(SimplePipeline, Debug) << "START CALLED";
- /* get an EGL display connection */
- auto eglGetPlatformDisplayEXT = (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT");
- display_ = eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA, gbm_, NULL);
- /* initialize the EGL display connection */
- eglInitialize(display_, NULL, NULL);
- EGLConfig configs[32];
- EGLint num_config;
- EGLint const attribute_list_config[] = {
- EGL_BUFFER_SIZE,
- 32,
- EGL_DEPTH_SIZE,
- EGL_DONT_CARE,
- EGL_STENCIL_SIZE,
- EGL_DONT_CARE,
- EGL_RENDERABLE_TYPE,
- EGL_OPENGL_ES2_BIT,
- EGL_SURFACE_TYPE,
- EGL_WINDOW_BIT,
- EGL_NONE,
- };
- auto c = eglChooseConfig(display_, attribute_list_config, configs, 32, &num_config);
- if (c != EGL_TRUE) {
- EGLint err = eglGetError();
- LOG(SimplePipeline, Error) << "<<< config failed: " << err;
- return -1;
- }
- if (num_config == 0) {
- LOG(SimplePipeline, Error) << "<<< found no configs " << std::endl;
- return -1;
- }
- /* Find a config whose native visual ID is the desired GBM format */
- EGLConfig config = nullptr;
- for (int i = 0; i < num_config; ++i) {
- EGLint gbm_format;
- if (!eglGetConfigAttrib(display_, configs[i],
- EGL_NATIVE_VISUAL_ID, &gbm_format)) {
- continue;
- }
- if (gbm_format == GBM_FORMAT_ARGB8888) {
- config = configs[i];
- break;
- }
- }
- if (config == nullptr) {
- return -1;
- }
- /* create an EGL rendering context */
- EGLint const attrib_list[] = {
- EGL_CONTEXT_MAJOR_VERSION, 1,
- EGL_NONE
- };
- context_ = eglCreateContext(display_, config, EGL_NO_CONTEXT, attrib_list);
- if (context_ == EGL_NO_CONTEXT) {
- EGLint err = eglGetError();
- LOG(SimplePipeline, Error) << " Context creation failed: " << err;
- return -1;
- }
- /* connect the context to the surface */
- eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, context_);
- int e = glGetError();
- if (e != GL_NO_ERROR)
- LOG(SimplePipeline, Error) << "GL_ERROR: " << e;
- framebufferProgram_.callShader("identity.vert", "fixed-color.frag");
- framebufferProgram_.activate();
- /* Prepare framebuffer rectangle VBO and VAO */
- glGenVertexArrays(1, &rectVAO);
- glGenBuffers(1, &rectVBO);
- glBindVertexArray(rectVAO);
- glBindBuffer(GL_ARRAY_BUFFER, rectVBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(rectangleVertices), &rectangleVertices, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void *)0);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void *)(2 * sizeof(float)));
- /* set values for all uniforms */
- glBindAttribLocation(framebufferProgram_.id(), 0, "vertexIn");
- /* create FrameBuffer object */
- glGenFramebuffers(1, &fbo_);
- glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
- return 0;
- }
- int SimpleConverter::queueBufferGL(FrameBuffer *input, FrameBuffer *output)
- {
- LOG(SimplePipeline, Debug) << "QUEUEBUFFERS GL CALLED";
- /* Generate texture from input buffer (with raw data) and bind it to GL_TEXTURE_2D */
- DmabufImage rend_texIn = importDmabuf(input->planes()[0].fd.get(), informat_.size, libcamera::formats::R8);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, rend_texIn.texture);
- auto glEGLImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");
- glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, rend_texIn.image);
- /* Generate texture from output buffer for rendering and bind it to GL_TEXTURE_2D */
- DmabufImage rend_texOut = importDmabuf(output->planes()[0].fd.get(), outformat_.size, libcamera::formats::ARGB8888);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, rend_texOut.texture);
- glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, rend_texOut.image);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- /* Prevents edge bleeding */
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rend_texOut.texture, 0);
- /* Bind the custom framebuffer */
- glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
- glBindVertexArray(rectVAO);
- glDrawArrays(GL_TRIANGLES, 0, 6);
- int e = glGetError();
- if (e != GL_NO_ERROR)
- LOG(SimplePipeline, Error) << "GL_ERROR: " << e;
- glFinish();
- /* Emit input and output bufferready signals */
- inputBufferReady.emit(input);
- outputBufferReady.emit(output);
- /* Specify the color of the background */
- glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement