From patchwork Sat Jan 10 21:09:30 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Mader X-Patchwork-Id: 25711 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 94E1ABE08B for ; Sat, 10 Jan 2026 21:09:56 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C457061FA0; Sat, 10 Jan 2026 22:09:55 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=collabora.com header.i=robert.mader@collabora.com header.b="YTRKD5lH"; dkim-atps=neutral Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9BE0161A35 for ; Sat, 10 Jan 2026 22:09:53 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; t=1768079390; cv=none; d=zohomail.com; s=zohoarc; b=hPJLqyivoDeoe/gpet85nwlFZH4Y6TruMuudgUCWHx/GVCkkmVYuXWhy6sbUFj2H/7mlhY/1puYiC2Ot1u2gsH/onXoC6gF8+ELJqlFGHC8YDk45+/ggoPIuhCkGvGzEWBdgWgC2l8h405S1Qe4XjD2OYI6jdbTOvwJsdzk5pXI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1768079390; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=HAlaZ9YrlOiWZQaawaiKN9sNV7/yLS4yNislAg+ZmG0=; b=VVItXbp4tFAeoLH8lNcftZ8gbq68jue/B9xpRemoldW3fmMTdJxW9YJ+ZLx24ZEytnjvfnsjwuHhuF9u/J3cY5ND0bEfWdCHxUQ409MBmCaiUsLxSHFm8SDlH2XFNgPPhgB+jgodUr7jmtNCx3PjIre3VW6GvSE1fAJ1rR3lUII= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=robert.mader@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1768079390; s=zohomail; d=collabora.com; i=robert.mader@collabora.com; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To; bh=HAlaZ9YrlOiWZQaawaiKN9sNV7/yLS4yNislAg+ZmG0=; b=YTRKD5lHBmZHeiaBvV9nzxlhidJQL+emWIEHJOT1CYT2A+jHrwG6Q+7FEO++xTlb JpwQX5r+ipYid75ZWykKobOrOKyEYBVPi5UlSi1ay+qE0NEescM0z74TzK2MTxpxSmc 13SULdxWaMq1EeUcdQZffmwJ6h07TMA61OF9hSbU= Received: by mx.zohomail.com with SMTPS id 1768079388831202.18040813299058; Sat, 10 Jan 2026 13:09:48 -0800 (PST) From: Robert Mader To: libcamera-devel@lists.libcamera.org Cc: Robert Mader Subject: [PATCH] RFC: egl: Implement DMABuf import for input buffers Date: Sat, 10 Jan 2026 22:09:30 +0100 Message-ID: <20260110210930.123920-1-robert.mader@collabora.com> X-Mailer: git-send-email 2.52.0 MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" In many cases we can import the GPU-ISP input buffer, a dmabuf from v4l2, directly into EGL instead of mapping and uploading - i.e. copying - it. This reduces memory bandwith usage and can even slightly improve latency. This main reason this doesn't work in many cases is the stride alignment, as GPUs often have stricter requirements (often 128 or even 256 bytes) than hardware ISPs. Thus try to import buffer directly and - if that fails - fall back to the previous upload path. To do so, adjust some function parameters and turn down error messages. Failing imports should come at low cost as drivers know the limitations and can bail out early, without causing additional IO or context switches. In the future we might be able to request buffers with a matching stride from v4l2 drivers in many cases, making direct import the norm instead of a hit-or-miss. An optional kernel API for that exists, but doesn't seem to be implemented by any driver tested so far. Signed-off-by: Robert Mader --- This patch should be applied on top of https://patchwork.libcamera.org/cover/25706/ --- include/libcamera/internal/egl.h | 4 +-- src/libcamera/egl.cpp | 37 +++++++++++++++++----- src/libcamera/software_isp/debayer_egl.cpp | 21 ++++++------ src/libcamera/software_isp/debayer_egl.h | 2 +- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/include/libcamera/internal/egl.h b/include/libcamera/internal/egl.h index f007f448a..b61769542 100644 --- a/include/libcamera/internal/egl.h +++ b/include/libcamera/internal/egl.h @@ -98,7 +98,7 @@ public: int initEGLContext(GBM *gbmContext); - int createInputDMABufTexture2D(eGLImage &eglImage, int fd); + int createInputDMABufTexture2D(eGLImage &eglImage, int fd, GLint format, uint32_t width, uint32_t height); int createOutputDMABufTexture2D(eGLImage &eglImage, int fd); void destroyDMABufTexture(eGLImage &eglImage); void createTexture2D(eGLImage &eglImage, GLint format, uint32_t width, uint32_t height, void *data); @@ -131,7 +131,7 @@ private: unsigned int shaderDataLen, Span shaderEnv); - int createDMABufTexture2D(eGLImage &eglImage, int fd, bool output); + int createDMABufTexture2D(eGLImage &eglImage, int fd, uint32_t drm_format, uint32_t width, uint32_t height, bool output); PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES; PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR; diff --git a/src/libcamera/egl.cpp b/src/libcamera/egl.cpp index 0ffd008c7..2853be7c0 100644 --- a/src/libcamera/egl.cpp +++ b/src/libcamera/egl.cpp @@ -19,6 +19,7 @@ #include +#include #include namespace libcamera { @@ -102,6 +103,9 @@ void eGL::syncOutput() * \brief Create a DMA-BUF backed 2D texture * \param[in,out] eglImage EGL image to associate with the DMA-BUF * \param[in] fd DMA-BUF file descriptor + * \param[in] drm_format the DRM fourcc + * \param[in] width the buffer width + * \param[in] height the buffer height * \param[in] output If true, create framebuffer for render target * * Internal implementation for creating DMA-BUF textures. Creates an EGL @@ -110,7 +114,7 @@ void eGL::syncOutput() * * \return 0 on success, or -ENODEV on failure */ -int eGL::createDMABufTexture2D(eGLImage &eglImage, int fd, bool output) +int eGL::createDMABufTexture2D(eGLImage &eglImage, int fd, uint32_t drm_format, uint32_t width, uint32_t height, bool output) { int ret = 0; @@ -118,9 +122,9 @@ int eGL::createDMABufTexture2D(eGLImage &eglImage, int fd, bool output) // clang-format off EGLint image_attrs[] = { - EGL_WIDTH, (EGLint)eglImage.width_, - EGL_HEIGHT, (EGLint)eglImage.height_, - EGL_LINUX_DRM_FOURCC_EXT, DRM_FORMAT_ARGB8888, + EGL_WIDTH, (EGLint)width, + EGL_HEIGHT, (EGLint)height, + EGL_LINUX_DRM_FOURCC_EXT, (EGLint)drm_format, EGL_DMA_BUF_PLANE0_FD_EXT, fd, EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0, EGL_DMA_BUF_PLANE0_PITCH_EXT, (EGLint)eglImage.stride_, @@ -135,7 +139,7 @@ int eGL::createDMABufTexture2D(eGLImage &eglImage, int fd, bool output) NULL, image_attrs); if (eglImage.image_ == EGL_NO_IMAGE_KHR) { - LOG(eGL, Error) << "eglCreateImageKHR fail"; + LOG(eGL, Debug) << "eglCreateImageKHR fail"; ret = -ENODEV; goto done; } @@ -176,6 +180,9 @@ done: * \brief Create an input DMA-BUF backed texture * \param[in,out] eglImage EGL image to associate with the DMA-BUF * \param[in] fd DMA-BUF file descriptor + * \param[in] format the GL format + * \param[in] width the buffer width + * \param[in] height the buffer height * * Creates an EGL image from a DMA-BUF file descriptor and binds it to * a 2D texture for use as an input texture in shaders. The texture is @@ -183,11 +190,25 @@ done: * * \return 0 on success, or -ENODEV on failure */ -int eGL::createInputDMABufTexture2D(eGLImage &eglImage, int fd) +int eGL::createInputDMABufTexture2D(eGLImage &eglImage, int fd, GLint format, uint32_t width, uint32_t height) { + EGLint drm_format; + ASSERT(tid_ == Thread::currentId()); - return createDMABufTexture2D(eglImage, fd, false); + switch (format) { + case GL_LUMINANCE: + drm_format = DRM_FORMAT_R8; + break; + case GL_RG: + drm_format = DRM_FORMAT_RG88; + break; + default: + LOG(eGL, Error) << "unhandled GL format"; + return -ENODEV; + } + + return createDMABufTexture2D(eglImage, fd, drm_format, width, height, false); } /** @@ -206,7 +227,7 @@ int eGL::createOutputDMABufTexture2D(eGLImage &eglImage, int fd) { ASSERT(tid_ == Thread::currentId()); - return createDMABufTexture2D(eglImage, fd, true); + return createDMABufTexture2D(eglImage, fd, DRM_FORMAT_ARGB8888, eglImage.width_, eglImage.height_, true); } /** diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp index 9693d7252..e6387c851 100644 --- a/src/libcamera/software_isp/debayer_egl.cpp +++ b/src/libcamera/software_isp/debayer_egl.cpp @@ -509,13 +509,22 @@ void DebayerEGL::setShaderVariableValues(DebayerParams ¶ms) return; } -int DebayerEGL::debayerGPU(MappedFrameBuffer &in, int out_fd, DebayerParams ¶ms) +int DebayerEGL::debayerGPU(FrameBuffer *input, int out_fd, DebayerParams ¶ms) { /* eGL context switch */ egl_.makeCurrent(); /* Create a standard texture input */ - egl_.createTexture2D(*eglImageBayerIn_, glFormat_, inputConfig_.stride / bytesPerPixel_, height_, in.planes()[0].data()); + if (egl_.createInputDMABufTexture2D(*eglImageBayerIn_, input->planes()[0].fd.get(), glFormat_, inputConfig_.stride / bytesPerPixel_, height_) != 0) { + MappedFrameBuffer in(input, MappedFrameBuffer::MapFlag::Read); + if (!in.isValid()) { + LOG(Debayer, Error) << "mmap-ing buffer(s) failed"; + return -ENODEV; + } + + LOG(Debayer, Debug) << "Importing input buffer with DMABuf import failed, falling back to upload"; + egl_.createTexture2D(*eglImageBayerIn_, glFormat_, inputConfig_.stride / bytesPerPixel_, height_, in.planes()[0].data()); + } /* Generate the output render framebuffer as render to texture */ egl_.createOutputDMABufTexture2D(*eglImageBayerOut_, out_fd); @@ -552,13 +561,7 @@ void DebayerEGL::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output metadata.sequence = input->metadata().sequence; metadata.timestamp = input->metadata().timestamp; - MappedFrameBuffer in(input, MappedFrameBuffer::MapFlag::Read); - if (!in.isValid()) { - LOG(Debayer, Error) << "mmap-ing buffer(s) failed"; - goto error; - } - - if (debayerGPU(in, output->planes()[0].fd.get(), params)) { + if (debayerGPU(input, output->planes()[0].fd.get(), params)) { LOG(Debayer, Error) << "debayerGPU failed"; goto error; } diff --git a/src/libcamera/software_isp/debayer_egl.h b/src/libcamera/software_isp/debayer_egl.h index a5033bc63..4b2cf448f 100644 --- a/src/libcamera/software_isp/debayer_egl.h +++ b/src/libcamera/software_isp/debayer_egl.h @@ -74,7 +74,7 @@ private: int getShaderVariableLocations(); void setShaderVariableValues(DebayerParams ¶ms); void configureTexture(GLuint &texture); - int debayerGPU(MappedFrameBuffer &in, int out_fd, DebayerParams ¶ms); + int debayerGPU(FrameBuffer *input, int out_fd, DebayerParams ¶ms); /* Shader program identifiers */ GLuint vertexShaderId_ = 0;