{"id":23226,"url":"https://patchwork.libcamera.org/api/patches/23226/?format=json","web_url":"https://patchwork.libcamera.org/patch/23226/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20250422215920.4297-14-bryan.odonoghue@linaro.org>","date":"2025-04-22T21:59:06","name":"[13/27] libcamera: software_isp: gbm: Add in a GBM helper class for GPU surface access","commit_ref":null,"pull_url":null,"state":"rfc","archived":false,"hash":"9a95936fc63423f7b241f1f066b11f5f174e1b31","submitter":{"id":175,"url":"https://patchwork.libcamera.org/api/people/175/?format=json","name":"Bryan O'Donoghue","email":"bryan.odonoghue@linaro.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/23226/mbox/","series":[{"id":5142,"url":"https://patchwork.libcamera.org/api/series/5142/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5142","date":"2025-04-22T21:58:53","name":"RFC: Add in a eGL based GPUISP in libcamera","version":1,"mbox":"https://patchwork.libcamera.org/series/5142/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/23226/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/23226/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id DA707C327D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 22 Apr 2025 21:59:48 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7285B68B44;\n\tTue, 22 Apr 2025 23:59:48 +0200 (CEST)","from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com\n\t[IPv6:2a00:1450:4864:20::42e])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5C84D68B30\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 22 Apr 2025 23:59:36 +0200 (CEST)","by mail-wr1-x42e.google.com with SMTP id\n\tffacd0b85a97d-39c1ef4acf2so3680184f8f.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 22 Apr 2025 14:59:36 -0700 (PDT)","from inspiron14p-linux.ht.home (188-141-3-146.dynamic.upc.ie.\n\t[188.141.3.146]) by smtp.gmail.com with ESMTPSA id\n\t5b1f17b1804b1-44092d2eccesm2726615e9.20.2025.04.22.14.59.34\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 22 Apr 2025 14:59:34 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=linaro.org header.i=@linaro.org\n\theader.b=\"wDRew07J\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=linaro.org; s=google; t=1745359175; x=1745963975;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=NoLLyeMcYw0WZHw+CBw8JHWe/oH++8f3vXYrkMExzgM=;\n\tb=wDRew07JdnZV2xaQZXYm0IINk4gR3Sz1PWlp6CjEEl8CSC7bd+vF95bb8LNKOOY3SH\n\tVADRN4/gQy5uhPZrhC6AyMsBl370fwvnWw4ZCAt+OCEqHqJwYL60YpAQi9Br/2/rx5A6\n\t7syZnu3Ysz/8vthHwLHX1gyKo/hTYaEqGgstX9oTOjbx6LtxIqqPWzz/Lcs8ad4I4OJd\n\tXOSRgGDaIx5lbiKB4w3UXHl0zy937RP5qRcO6PFXg1hpBGYqPHLyWu1XBRunTkcdrwBg\n\tbPiV05eghYPrH7nkyTQPjYSR3sqjedPDop36cHq1n87Xn8d3gFe3fcbVLG01kUdvQ15I\n\tR9tg==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1745359175; x=1745963975;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=NoLLyeMcYw0WZHw+CBw8JHWe/oH++8f3vXYrkMExzgM=;\n\tb=FE8MFmKKfy2oNE6eGfql9BagvtIfiSuSlwArhiSoAkXO4eoWJB6J5InQm40pG2TREI\n\tSnFaxMlJaWacPpuHs4mzONmDKpgmPHo95MTjZ+gVgs/zRlf3TrH/evKnCqLXuxFI+tO4\n\tVdcQ9zBCMz5lLpdJbNUpMBhRDpv+m3a+uyThZdT6oacBzWfTboj2P6mOBjfn9mXGIa1q\n\tCpEmTzGab02ebM6FxC7fieQ153nyRkUxfxdjjudgxs1EEfbkCZQxz2WfLux9T2q2AaZY\n\tpkH2RW6DhobHL5PmKcMaO8/gScU+HW/qq3SBI4xehjPRuaFgRq72c+ljCI3w3HDHCSPY\n\tHdqQ==","X-Gm-Message-State":"AOJu0Yy0IUu3OXEt0k5VcxAAt4Mv2rRyZZpJTWnXzoxPTTKPfCp+qQWI\n\tEvHEOK/liUeE3FqUl5/fVqYSyfb49XkaCVMttETSGGpHKXqJs8WonsLWb+/kD9p4uKLyexjtv2W\n\tMW1E=","X-Gm-Gg":"ASbGncvbL2/FopSa74uqz0t2jh1YATf6kuo6Xa13Ahc5Xd7fJ+Uik5gmojWB7H07XPo\n\tg2YwtcgOJKKmtGn4ULK+BLJPP20+MjERDasXSkVeWSo4w0TVUYyFp9/vhmP8LNW3IzsY1F701iZ\n\tmSR/TnjhrFZ6JhAlxlUOwATqN+UjVsq6vetg2mSPANn5e75I2mCMVMH+Y1F8jF1/NnPI852xzNu\n\t35q5F8Z9HLh9rzG0zQM81LMxJpoveEDW6V/lDFTE+2/Tt7AFhs2K9w6QI9rkwmx1WsBVhCgmCtd\n\tOxp4MZuPhnTUVaPWB+hqEnXtmxeuvXJ4hiUMUrhtLoWWg71Cwk1ZfCGH6pDKv/4DMUfC4pIryTW\n\tjT9TNINsUjOTKxics7Y6WTRXs+Cm2V2Y=","X-Google-Smtp-Source":"AGHT+IG8XChv8dt+ee21hrbNGl8xt2CF5TIozgTXTi/ysEcKfhiuZjU6Tm79O9RVY8LPTY3l7PQoZQ==","X-Received":"by 2002:a5d:598b:0:b0:390:e8d4:6517 with SMTP id\n\tffacd0b85a97d-39efba3d0e2mr14041727f8f.21.1745359175462; \n\tTue, 22 Apr 2025 14:59:35 -0700 (PDT)","From":"Bryan O'Donoghue <bryan.odonoghue@linaro.org>","To":"libcamera-devel@lists.libcamera.org","Cc":"hdegoede@redhat.com, mzamazal@redhat.com, bryan.odonoghue@linaro.org,\n\tbod.linux@nxsw.ie","Subject":"[PATCH 13/27] libcamera: software_isp: gbm: Add in a GBM helper\n\tclass for GPU surface access","Date":"Tue, 22 Apr 2025 22:59:06 +0100","Message-ID":"<20250422215920.4297-14-bryan.odonoghue@linaro.org>","X-Mailer":"git-send-email 2.49.0","In-Reply-To":"<20250422215920.4297-1-bryan.odonoghue@linaro.org>","References":"<20250422215920.4297-1-bryan.odonoghue@linaro.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"A helper class to interact with GBM. This will allow us to specify the\ninternal storage format of the CPU when making a texture for the Debayer\nvertext/fragment shaders and thus ensure we receive an uncompressed and\nuntiled output buffer.\n\nSigned-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>\n---\n src/libcamera/software_isp/gbm.cpp | 136 +++++++++++++++++++++++++++++\n src/libcamera/software_isp/gbm.h   |  52 +++++++++++\n 2 files changed, 188 insertions(+)\n create mode 100644 src/libcamera/software_isp/gbm.cpp\n create mode 100644 src/libcamera/software_isp/gbm.h","diff":"diff --git a/src/libcamera/software_isp/gbm.cpp b/src/libcamera/software_isp/gbm.cpp\nnew file mode 100644\nindex 00000000..1be00f1f\n--- /dev/null\n+++ b/src/libcamera/software_isp/gbm.cpp\n@@ -0,0 +1,136 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Linaro Ltd.\n+ *\n+ * Authors:\n+ * Bryan O'Donoghue <bryan.odonoghue@linaro.org>\n+ *\n+ * egl.cpp - Helper class for managing GBM interactions.\n+ */\n+\n+#include <fcntl.h>\n+#include <unistd.h>\n+\n+#include <linux/dma-buf.h>\n+#include <linux/dma-heap.h>\n+#include <sys/ioctl.h>\n+#include <sys/mman.h>\n+\n+#include \"gbm.h\"\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(GBM)\n+\n+GBM::GBM() {\n+\tfd_ = 0;\n+};\n+\n+GBM::~GBM() {\n+\tif (gbm_surface_)\n+\t\tgbm_surface_destroy(gbm_surface_);\n+\n+\tif (gbm_device_)\n+\t\tgbm_device_destroy(gbm_device_);\n+\n+\tif (fd_ >= 0)\n+\t\tclose(fd_);\n+};\n+\n+// this should probably go into its own class to deal with the\n+// allocation and deletion of frambuffers attached to GBM devices/objects\n+int GBM::initSurface(uint32_t width, uint32_t height)\n+{\n+\tconst char *dri_node = \"/dev/dri/renderD128\"; //TODO: get from an env or config setting\n+\n+\tfd_ = open(dri_node, O_RDWR | O_CLOEXEC);\t//TODO: CLOEXEC ?\n+\tif (fd_ < 0) {\n+\t\tLOG(GBM, Error) << \"Open \" << dri_node<< \" fail \" << fd_;\n+\t\treturn fd_;\n+\t}\n+\n+\tgbm_device_ = gbm_create_device(fd_);\n+\tif (!gbm_device_) {\n+\t\tLOG(GBM, Error) << \"gbm_crate_device fail\";\n+\t\tgoto fail;\n+\t}\n+\n+\t// GBM_FORMAT_RGBA8888 is not supported mesa::src/gbm/dri/gbm_dri.c::gbm_dri_visuals_table[]\n+\t// This means we need to choose XRGB8888 or ARGB8888 as the raw buffer format\n+\tgbm_surface_ = gbm_surface_create(gbm_device_, width, height, GBM_FORMAT_ARGB8888,\n+\t\t\t\t\t  GBM_BO_USE_RENDERING | GBM_BO_USE_LINEAR);\n+\tif (!gbm_surface_) {\n+\t\tLOG(GBM, Error) << \"Unable to create linear gbm surface\";\n+\t\tgoto fail;\n+\t}\n+\n+\tformat = libcamera::formats::ARGB8888;\n+\n+\treturn 0;\n+fail:\n+\treturn -ENODEV;\n+\n+}\n+\n+int GBM::mapSurface()\n+{\n+\tgbm_bo_ = gbm_surface_lock_front_buffer(gbm_surface_);\n+\tif (!gbm_bo_) {\n+\t\tLOG(GBM, Error) << \"GBM input buffer object create fail\";\n+\t\treturn -ENODEV;\n+\t}\n+\tgbm_surface_release_buffer(gbm_surface_, gbm_bo_);\n+\n+\tbo_fd_ = gbm_bo_get_fd(gbm_bo_);\n+\n+\tif (!bo_fd_) {\n+\t\tgbm_surface_release_buffer(gbm_surface_, gbm_bo_);\n+\t\tLOG(GBM, Error) << \"Unable to get fd for bo: \" << bo_fd_;\n+\t\treturn -ENODEV;\n+\t}\n+\n+\tstride_ = gbm_bo_get_stride(gbm_bo_);\n+\twidth_ = gbm_bo_get_width(gbm_bo_);\n+\theight_ = gbm_bo_get_height(gbm_bo_);\n+\toffset_ = gbm_bo_get_offset(gbm_bo_, 0);\n+\tframesize_ = height_ * stride_;\n+\n+\tmap_ = mmap(NULL, height_ * stride_, PROT_READ, MAP_SHARED, bo_fd_, 0);\n+\tif (map_ == MAP_FAILED) {\n+\t\tLOG(GBM, Error) << \"mmap gbm_bo_ fail\";\n+\t\treturn -ENODEV;\n+\t}\n+\n+\tLOG(GBM, Debug) << \" stride \" << stride_\n+\t\t\t<< \" width \" << width_\n+\t\t\t<< \" height \" << height_\n+\t\t\t<< \" offset \" << offset_\n+\t\t\t<< \" framesize \" << framesize_;\n+\n+\treturn 0;\n+}\n+\n+int GBM::getFrameBufferData(uint8_t *data, size_t data_len)\n+{\n+\tstruct dma_buf_sync sync;\n+\n+\tgbm_surface_lock_front_buffer(gbm_surface_);\n+\n+\tsync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ;\n+\tioctl(bo_fd_, DMA_BUF_IOCTL_SYNC, &sync);\n+\n+\tif (data_len > framesize_) {\n+\t\tLOG(GBM, Error) << \"Invalid read size \" << data_len << \" max is \" << framesize_;\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tmemcpy(data, map_, data_len);\n+\n+\tsync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ;\n+\tioctl(bo_fd_, DMA_BUF_IOCTL_SYNC, &sync);\n+\n+\tgbm_surface_release_buffer(gbm_surface_, gbm_bo_);\n+\n+\treturn 0;\n+}\n+};\t//namespace libcamera\ndiff --git a/src/libcamera/software_isp/gbm.h b/src/libcamera/software_isp/gbm.h\nnew file mode 100644\nindex 00000000..9ccfb1ba\n--- /dev/null\n+++ b/src/libcamera/software_isp/gbm.h\n@@ -0,0 +1,52 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Linaro Ltd.\n+ *\n+ * Authors:\n+ * Bryan O'Donoghue <bryan.odonoghue@linaro.org>\n+ *\n+ * gbm.h - Helper class for managing GBM interactions.\n+ */\n+\n+#pragma once\n+\n+#include <gbm.h>\n+\n+#include <libcamera/base/log.h>\n+#include <libcamera/formats.h>\n+\n+namespace libcamera {\n+\n+LOG_DECLARE_CATEGORY(GBM)\n+\n+class GBM {\n+public:\n+\tGBM();\n+\t~GBM();\n+\n+\tint initSurface(uint32_t width, uint32_t height);\n+\tint mapSurface();\n+\tint getFrameBufferData(uint8_t *data_out, size_t data_len);\n+\tstruct gbm_device *getDevice() { return gbm_device_; };\n+\tstruct gbm_surface *getSurface() { return gbm_surface_; };\n+\tuint32_t getFrameSize() { return framesize_; };\n+\tuint32_t getStride() { return stride_; };\n+\n+\tPixelFormat format;\n+\n+private:\n+\tint fd_;\n+\tstruct gbm_device *gbm_device_;\n+\tstruct gbm_surface *gbm_surface_;\n+\n+\tstruct gbm_bo *gbm_bo_;\n+\tuint32_t width_;\n+\tuint32_t height_;\n+\tuint32_t stride_;\n+\tuint32_t offset_;\n+\tuint32_t framesize_;\n+\tvoid *map_;\n+\tint bo_fd_;\n+};\n+\n+};\n","prefixes":["13/27"]}