Advertisement
KunalA18

OpenGL

Nov 19th, 2022
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.81 KB | Source Code | 0 0
  1.  
  2. SimpleConverter::DmabufImage SimpleConverter::importDmabuf(int fdesc, Size pixelSize, PixelFormat format)
  3. {
  4.     LOG(SimplePipeline, Debug) << "IMPORT DMABUF CALLED";
  5.     int bytes_per_pixel = 0;
  6.     if (format == libcamera::formats::ARGB8888) {
  7.         bytes_per_pixel = 4;
  8.     }
  9.     if (format == libcamera::formats::R8) {
  10.         bytes_per_pixel = 1;
  11.     }
  12.     if (bytes_per_pixel == 0) {
  13.         LOG(SimplePipeline, Error) << "Bytes per pixel not modified ";
  14.     }
  15.  
  16.     EGLint const attrs[] = {
  17.         EGL_WIDTH,
  18.         (int)pixelSize.width,
  19.         EGL_HEIGHT,
  20.         (int)pixelSize.height,
  21.         EGL_LINUX_DRM_FOURCC_EXT,
  22.         (int)format.fourcc(),
  23.         EGL_DMA_BUF_PLANE0_FD_EXT,
  24.         fdesc,
  25.         EGL_DMA_BUF_PLANE0_OFFSET_EXT,
  26.         0,
  27.         EGL_DMA_BUF_PLANE0_PITCH_EXT,
  28.         (int)pixelSize.width * bytes_per_pixel,
  29.         EGL_NONE,
  30.     };
  31.  
  32.     EGLImageKHR image = eglCreateImageKHR(
  33.         display_,
  34.         EGL_NO_CONTEXT,
  35.         EGL_LINUX_DMA_BUF_EXT,
  36.         NULL,
  37.         attrs);
  38.  
  39.     int e = glGetError();
  40.  
  41.     if (e != GL_NO_ERROR)
  42.         LOG(SimplePipeline, Error) << "GL_ERROR: " << e;
  43.  
  44.     GLuint texture;
  45.     glGenTextures(1, &texture);
  46.     struct DmabufImage img = {
  47.         .texture = texture,
  48.         .image = image,
  49.     };
  50.  
  51.     return img;
  52. }
  53.  
  54. int SimpleConverter::start()
  55. {
  56.     LOG(SimplePipeline, Debug) << "START CALLED";
  57.  
  58.     /* get an EGL display connection */
  59.     auto eglGetPlatformDisplayEXT = (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT");
  60.     display_ = eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA, gbm_, NULL);
  61.  
  62.     /* initialize the EGL display connection */
  63.     eglInitialize(display_, NULL, NULL);
  64.  
  65.     EGLConfig configs[32];
  66.     EGLint num_config;
  67.     EGLint const attribute_list_config[] = {
  68.         EGL_BUFFER_SIZE,
  69.         32,
  70.         EGL_DEPTH_SIZE,
  71.         EGL_DONT_CARE,
  72.         EGL_STENCIL_SIZE,
  73.         EGL_DONT_CARE,
  74.         EGL_RENDERABLE_TYPE,
  75.         EGL_OPENGL_ES2_BIT,
  76.         EGL_SURFACE_TYPE,
  77.         EGL_WINDOW_BIT,
  78.         EGL_NONE,
  79.     };
  80.     auto c = eglChooseConfig(display_, attribute_list_config, configs, 32, &num_config);
  81.     if (c != EGL_TRUE) {
  82.         EGLint err = eglGetError();
  83.         LOG(SimplePipeline, Error) << "<<< config failed: " << err;
  84.         return -1;
  85.     }
  86.     if (num_config == 0) {
  87.         LOG(SimplePipeline, Error) << "<<< found no configs " << std::endl;
  88.         return -1;
  89.     }
  90.  
  91.     /* Find a config whose native visual ID is the desired GBM format */
  92.     EGLConfig config = nullptr;
  93.     for (int i = 0; i < num_config; ++i) {
  94.         EGLint gbm_format;
  95.  
  96.         if (!eglGetConfigAttrib(display_, configs[i],
  97.                     EGL_NATIVE_VISUAL_ID, &gbm_format)) {
  98.             continue;
  99.         }
  100.  
  101.         if (gbm_format == GBM_FORMAT_ARGB8888) {
  102.             config = configs[i];
  103.             break;
  104.         }
  105.     }
  106.  
  107.     if (config == nullptr) {
  108.         return -1;
  109.     }
  110.  
  111.     /* create an EGL rendering context */
  112.     EGLint const attrib_list[] = {
  113.         EGL_CONTEXT_MAJOR_VERSION, 1,
  114.         EGL_NONE
  115.     };
  116.     context_ = eglCreateContext(display_, config, EGL_NO_CONTEXT, attrib_list);
  117.     if (context_ == EGL_NO_CONTEXT) {
  118.         EGLint err = eglGetError();
  119.         LOG(SimplePipeline, Error) << " Context creation failed: " << err;
  120.         return -1;
  121.     }
  122.  
  123.     /* connect the context to the surface */
  124.     eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, context_);
  125.  
  126.     int e = glGetError();
  127.     if (e != GL_NO_ERROR)
  128.         LOG(SimplePipeline, Error) << "GL_ERROR: " << e;
  129.  
  130.     framebufferProgram_.callShader("identity.vert", "fixed-color.frag");
  131.     framebufferProgram_.activate();
  132.  
  133.     /* Prepare framebuffer rectangle VBO and VAO */
  134.     glGenVertexArrays(1, &rectVAO);
  135.     glGenBuffers(1, &rectVBO);
  136.     glBindVertexArray(rectVAO);
  137.     glBindBuffer(GL_ARRAY_BUFFER, rectVBO);
  138.     glBufferData(GL_ARRAY_BUFFER, sizeof(rectangleVertices), &rectangleVertices, GL_STATIC_DRAW);
  139.     glEnableVertexAttribArray(0);
  140.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void *)0);
  141.     glEnableVertexAttribArray(1);
  142.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void *)(2 * sizeof(float)));
  143.  
  144.     /* set values for all uniforms */
  145.     glBindAttribLocation(framebufferProgram_.id(), 0, "vertexIn");
  146.     /* create FrameBuffer object */
  147.     glGenFramebuffers(1, &fbo_);
  148.     glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
  149.  
  150.     return 0;
  151. }
  152. int SimpleConverter::queueBufferGL(FrameBuffer *input, FrameBuffer *output)
  153. {
  154.     LOG(SimplePipeline, Debug) << "QUEUEBUFFERS GL CALLED";
  155.  
  156.     /* Generate texture from input buffer (with raw data) and bind it to GL_TEXTURE_2D */
  157.  
  158.     DmabufImage rend_texIn = importDmabuf(input->planes()[0].fd.get(), informat_.size, libcamera::formats::R8);
  159.     glActiveTexture(GL_TEXTURE0);
  160.     glBindTexture(GL_TEXTURE_2D, rend_texIn.texture);
  161.     auto glEGLImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");
  162.     glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, rend_texIn.image);
  163.  
  164.     /* Generate texture from output buffer for rendering and bind it to GL_TEXTURE_2D */
  165.  
  166.     DmabufImage rend_texOut = importDmabuf(output->planes()[0].fd.get(), outformat_.size, libcamera::formats::ARGB8888);
  167.     glActiveTexture(GL_TEXTURE1);
  168.     glBindTexture(GL_TEXTURE_2D, rend_texOut.texture);
  169.     glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, rend_texOut.image);
  170.  
  171.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  172.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  173.  
  174.     /* Prevents edge bleeding */
  175.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  176.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  177.  
  178.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rend_texOut.texture, 0);
  179.  
  180.     /* Bind the custom framebuffer */
  181.     glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
  182.  
  183.     glBindVertexArray(rectVAO);
  184.     glDrawArrays(GL_TRIANGLES, 0, 6);
  185.     int e = glGetError();
  186.     if (e != GL_NO_ERROR)
  187.         LOG(SimplePipeline, Error) << "GL_ERROR: " << e;
  188.     glFinish();
  189.  
  190.     /* Emit input and output bufferready signals */
  191.     inputBufferReady.emit(input);
  192.     outputBufferReady.emit(output);
  193.  
  194.     /* Specify the color of the background */
  195.     glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
  196.     glClear(GL_COLOR_BUFFER_BIT);
  197.  
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement