Show a patch.

GET /api/1.1/patches/15510/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 15510,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/15510/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/15510/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20220322183514.65073-1-ecurtin@redhat.com>",
    "date": "2022-03-22T18:35:14",
    "name": "[libcamera-devel] cam: sdl_sink: Add SDL sink",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "bd6296dee2ffb7839984c50a76d20faece3cf6d9",
    "submitter": {
        "id": 101,
        "url": "https://patchwork.libcamera.org/api/1.1/people/101/?format=api",
        "name": "Eric Curtin",
        "email": "ecurtin@redhat.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/15510/mbox/",
    "series": [
        {
            "id": 2984,
            "url": "https://patchwork.libcamera.org/api/1.1/series/2984/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2984",
            "date": "2022-03-22T18:35:14",
            "name": "[libcamera-devel] cam: sdl_sink: Add SDL sink",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/2984/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/15510/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/15510/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 5D57CC0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 22 Mar 2022 18:35:25 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7D4CB604E6;\n\tTue, 22 Mar 2022 19:35:24 +0100 (CET)",
            "from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id CF743604C5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 22 Mar 2022 19:35:21 +0100 (CET)",
            "from mail-wm1-f71.google.com (mail-wm1-f71.google.com\n\t[209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id\n\tus-mta-259-aI50VjDWMCi_jWJCJm0dhA-1; Tue, 22 Mar 2022 14:35:19 -0400",
            "by mail-wm1-f71.google.com with SMTP id\n\tz16-20020a05600c0a1000b0038bebbd8548so1784921wmp.3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 22 Mar 2022 11:35:19 -0700 (PDT)",
            "from p1.Home ([2001:8a0:6724:4500:a69c:e66f:828e:b340])\n\tby smtp.gmail.com with ESMTPSA id\n\ti15-20020a5d584f000000b00204171ba528sm5021688wrf.109.2022.03.22.11.35.16\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 22 Mar 2022 11:35:17 -0700 (PDT)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1647974124;\n\tbh=61+S3tsx6cjuVLkB9XT26S5jRFB/IwKVYcZdYzv9R6U=;\n\th=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post:\n\tList-Help:List-Subscribe:From:Reply-To:From;\n\tb=PXmmWsyOQ3CJkkPInHNup9GynGL9yFHdkGmi1Qa9WqPynL7s598BhgnCcdYbQjj2C\n\ts5KdjvBwrQ8ADHfYE0K5C8IXOkqbRiBurwTGrOrNhFJCynXTz3sdDVQwRnftTcrKNd\n\tbCI+gjsDjaaNSxByPtk2hEe+XCxzm4Qe0RrJcHIWC2HmgB9HLcuy3GzlCB/7l1t1uq\n\tl2gd/X3ntMN2jctZVqWN4xv7DGlss5SlNhQ1z+xgzw7NeJ37i40dtmuBE2Lep78ZuQ\n\tmbYkqOOt8wrHm0pewjMvMo9mRdCbDNxlnLBpZJLwblZcOeB/7QjxB0LhVAxod1GFsE\n\taUiHx7VyZWM+w==",
            "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1647974120;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding;\n\tbh=+ny5YsFme7mV8wUuvtXAEmz/uJ3BUgDbvqP/Z3fR/Eo=;\n\tb=dY2PC8lksK64VPi2krCpHvCiQWoybk7mSOWbybLHd+RQ+6pOAM/PN8hUI5VoNr7lxS8gcI\n\t08FqgCcbYpJUhyax30VgP1gyS4d787X16VAq9uKLxT61BQ+6Ug6zesESmAgS+4rw4xOWgt\n\tBRMnRif5hzROm0eRaCIgyla5t98U8c8="
        ],
        "Authentication-Results": [
            "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=redhat.com\n\theader.i=@redhat.com header.b=\"dY2PC8lk\"; \n\tdkim-atps=neutral",
            "relay.mimecast.com;\n\tauth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ecurtin@redhat.com"
        ],
        "X-MC-Unique": "aI50VjDWMCi_jWJCJm0dhA-1",
        "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version\n\t:content-transfer-encoding;\n\tbh=+ny5YsFme7mV8wUuvtXAEmz/uJ3BUgDbvqP/Z3fR/Eo=;\n\tb=e6JCVkKD0rM4UeS/94bazM58JGFy5uRa8GfpG+pxhJDIvTPoSoqlTAHC/uaRIXnrQu\n\tbhvs9VXkjUjraJv2TxO4l6CwBwXcGOhGHUBaBUzq+BMlJPZh7fXTuOIAVfsf1EaE0kND\n\thIgllo5MsbTMApmm0YxdZzNbWglc6kHhKvCoXZ/+klJpH6a/TKvBL6HyfOQeMYAIZwYF\n\t4hTG1w6oRIqAmOAWRfnw+vzjAYZuE52rvurPx/qh67sDofjA2uSVklH+5gIt8uepNzxX\n\tFDrqRu13KnjTKS6WRDBuUxYdh+iUDTLmpDpRYjVmFmMVY2YsN7q8bFepBIx1IqpcSxNg\n\tOksw==",
        "X-Gm-Message-State": "AOAM531jcz+zt+03ndjq1R8E5IDYF89JboMGEUiKFBIQ+21oi17McRK4\n\t+ZgZQk2ahlN/ksn9dxsBisiZK5TF5t7tSIZ1f1pOVr5mT5pxa7DHCCkPy2+7mI+Afg4V8HUyxlO\n\thU0uFxdoipaqNi4mDOpPBSmbCpFNHya1a8IjMxRtn0WzvkWIPsr2r+5tSr2K9kDU/EkioyAZDDE\n\tAjbjO2y/rz",
        "X-Received": [
            "by 2002:adf:fcce:0:b0:203:dcdb:dd80 with SMTP id\n\tf14-20020adffcce000000b00203dcdbdd80mr23846568wrs.455.1647974117903; \n\tTue, 22 Mar 2022 11:35:17 -0700 (PDT)",
            "by 2002:adf:fcce:0:b0:203:dcdb:dd80 with SMTP id\n\tf14-20020adffcce000000b00203dcdbdd80mr23846544wrs.455.1647974117600; \n\tTue, 22 Mar 2022 11:35:17 -0700 (PDT)"
        ],
        "X-Google-Smtp-Source": "ABdhPJw59CggTNl3kUF4S+jxVYxVrk8Eo9DpOJ4AVAWOZn/SK8GbTOAtmai8/74kDtrc5r4gQVR6zg==",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Tue, 22 Mar 2022 18:35:14 +0000",
        "Message-Id": "<20220322183514.65073-1-ecurtin@redhat.com>",
        "X-Mailer": "git-send-email 2.35.1",
        "MIME-Version": "1.0",
        "X-Mimecast-Spam-Score": "0",
        "X-Mimecast-Originator": "redhat.com",
        "Content-Transfer-Encoding": "8bit",
        "Content-Type": "text/plain; charset=\"US-ASCII\"",
        "Subject": "[libcamera-devel] [PATCH] cam: sdl_sink: Add SDL sink",
        "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>",
        "From": "Eric Curtin via libcamera-devel <libcamera-devel@lists.libcamera.org>",
        "Reply-To": "Eric Curtin <ecurtin@redhat.com>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "This adds more portability to existing cam sinks. You can pass a\nYUYV camera buffer for example and SDL will handle the pixel buffer\nconversion, although SDL does not support decompression for pixelformats\nlike MJPEG. This allows cam reference implementation to display images\non VMs, Mac M1, Raspberry Pi, etc. This also enables cam reference\nimplementation, to run as a desktop application in wayland or x11.\nSDL also has support for Android and ChromeOS which I have not tested.\nAlso tested on simpledrm raspberry pi 4 framebuffer successfully where\nexisting kms sink did not work.\n\nSigned-off-by: Eric Curtin <ecurtin@redhat.com>\n---\n src/cam/camera_session.cpp |   8 +++\n src/cam/main.cpp           |   6 ++\n src/cam/main.h             |   1 +\n src/cam/meson.build        |  10 +++\n src/cam/sdl_sink.cpp       | 125 +++++++++++++++++++++++++++++++++++++\n src/cam/sdl_sink.h         |  40 ++++++++++++\n 6 files changed, 190 insertions(+)\n create mode 100644 src/cam/sdl_sink.cpp\n create mode 100644 src/cam/sdl_sink.h",
    "diff": "diff --git a/src/cam/camera_session.cpp b/src/cam/camera_session.cpp\nindex 0428b538..30162dbd 100644\n--- a/src/cam/camera_session.cpp\n+++ b/src/cam/camera_session.cpp\n@@ -19,6 +19,9 @@\n #ifdef HAVE_KMS\n #include \"kms_sink.h\"\n #endif\n+#ifdef HAVE_SDL\n+#include \"sdl_sink.h\"\n+#endif\n #include \"main.h\"\n #include \"stream_options.h\"\n \n@@ -187,6 +190,11 @@ int CameraSession::start()\n \t\tsink_ = std::make_unique<KMSSink>(options_[OptDisplay].toString());\n #endif\n \n+#ifdef HAVE_SDL\n+\tif (options_.isSet(OptSDL))\n+\t\tsink_ = std::make_unique<SDLSink>();\n+#endif\n+\n \tif (options_.isSet(OptFile)) {\n \t\tif (!options_[OptFile].toString().empty())\n \t\t\tsink_ = std::make_unique<FileSink>(streamNames_,\ndiff --git a/src/cam/main.cpp b/src/cam/main.cpp\nindex c7f664b9..406d61bc 100644\n--- a/src/cam/main.cpp\n+++ b/src/cam/main.cpp\n@@ -138,6 +138,12 @@ int CamApp::parseOptions(int argc, char *argv[])\n \t\t\t \"display\", ArgumentOptional, \"connector\", false,\n \t\t\t OptCamera);\n #endif\n+\n+#ifdef HAVE_SDL\n+\tparser.addOption(OptSDL, OptionNone,\n+\t\t\t \"Display viewfinder through SDL\",\n+\t\t\t \"sdl\", ArgumentNone, \"\", false, OptCamera);\n+#endif\n \tparser.addOption(OptFile, OptionString,\n \t\t\t \"Write captured frames to disk\\n\"\n \t\t\t \"If the file name ends with a '/', it sets the directory in which\\n\"\ndiff --git a/src/cam/main.h b/src/cam/main.h\nindex 62f7bbc9..a64f95a0 100644\n--- a/src/cam/main.h\n+++ b/src/cam/main.h\n@@ -11,6 +11,7 @@ enum {\n \tOptCamera = 'c',\n \tOptCapture = 'C',\n \tOptDisplay = 'D',\n+\tOptSDL = 'S',\n \tOptFile = 'F',\n \tOptHelp = 'h',\n \tOptInfo = 'I',\ndiff --git a/src/cam/meson.build b/src/cam/meson.build\nindex e8e2ae57..44202ef0 100644\n--- a/src/cam/meson.build\n+++ b/src/cam/meson.build\n@@ -32,11 +32,21 @@ cam_sources += files([\n ])\n endif\n \n+libsdl2 = dependency('SDL2', required : false)\n+\n+if libsdl2.found()\n+cam_cpp_args += [ '-DHAVE_SDL' ]\n+cam_sources += files([\n+    'sdl_sink.cpp'\n+])\n+endif\n+\n cam  = executable('cam', cam_sources,\n                   dependencies : [\n                       libatomic,\n                       libcamera_public,\n                       libdrm,\n+                      libsdl2,\n                       libevent,\n                   ],\n                   cpp_args : cam_cpp_args,\ndiff --git a/src/cam/sdl_sink.cpp b/src/cam/sdl_sink.cpp\nnew file mode 100644\nindex 00000000..6de1493b\n--- /dev/null\n+++ b/src/cam/sdl_sink.cpp\n@@ -0,0 +1,125 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * sdl_sink.cpp - SDL Sink\n+ */\n+\n+#include \"sdl_sink.h\"\n+\n+#include <assert.h>\n+#include <fcntl.h>\n+#include <iomanip>\n+#include <iostream>\n+#include <signal.h>\n+#include <sstream>\n+#include <string.h>\n+#include <unistd.h>\n+\n+#include <libcamera/camera.h>\n+#include <libcamera/formats.h>\n+\n+#include \"image.h\"\n+\n+using namespace libcamera;\n+\n+SDLSink::SDLSink()\n+{\n+\tmemset(&sdlRect_, 0, sizeof(sdlRect_));\n+}\n+\n+SDLSink::~SDLSink()\n+{\n+\tSDL_Quit();\n+}\n+\n+int SDLSink::configure(const libcamera::CameraConfiguration &cfg)\n+{\n+\tint ret = FrameSink::configure(cfg);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\tif ((ret = SDL_Init(SDL_INIT_VIDEO))) {\n+\t\tstd::cout << \"Could not initialize SDL - \" << SDL_GetError() << \"\\n\";\n+\t\treturn ret;\n+\t}\n+\n+\tconst libcamera::StreamConfiguration &sCfg = cfg.at(0);\n+\tsdlScreen_ = SDL_CreateWindow(\"\", SDL_WINDOWPOS_UNDEFINED,\n+\t\t\t\t      SDL_WINDOWPOS_UNDEFINED, sCfg.size.width,\n+\t\t\t\t      sCfg.size.height, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);\n+\tif (!sdlScreen_) {\n+\t\tstd::cerr << \"SDL: could not create window - exiting: \" << SDL_GetError() << \"\\n\";\n+\t\treturn -1;\n+\t}\n+\n+\tsdlRenderer_ = SDL_CreateRenderer(\n+\t\tsdlScreen_, -1, 0);\n+\tif (!sdlRenderer_) {\n+\t\tstd::cerr << \"SDL_CreateRenderer Error\\n\";\n+\t\treturn -2;\n+\t}\n+\n+\tSDL_RenderSetLogicalSize(sdlRenderer_, sCfg.size.width,\n+\t\t\t\t sCfg.size.height);\n+\n+\tSDL_PixelFormatEnum pf = (SDL_PixelFormatEnum)sCfg.pixelFormat.fourcc();\n+\tif (pf == SDL_DEFINE_PIXELFOURCC('Y', 'U', 'Y', 'V')) {\n+\t\tpf = SDL_PIXELFORMAT_YUY2;\n+\t}\n+\n+\tif (!(ret = strcmp(SDL_GetPixelFormatName(pf), \"SDL_PIXELFORMAT_UNKNOWN\"))) {\n+\t\tstd::cerr << \"error: SDL_PIXELFORMAT_UNKNOWN, no \" << sCfg.pixelFormat.toString() << \" support\\n\";\n+\t\treturn -3;\n+\t}\n+\n+\tsdlTexture_ =\n+\t\tSDL_CreateTexture(sdlRenderer_, SDL_PIXELFORMAT_YUY2,\n+\t\t\t\t  SDL_TEXTUREACCESS_STREAMING, sCfg.size.width,\n+\t\t\t\t  sCfg.size.height);\n+\tsdlRect_.w = sCfg.size.width;\n+\tsdlRect_.h = sCfg.size.height;\n+\n+\treturn 0;\n+}\n+\n+void SDLSink::mapBuffer(FrameBuffer *buffer)\n+{\n+\tstd::unique_ptr<Image> image =\n+\t\tImage::fromFrameBuffer(buffer, Image::MapMode::ReadOnly);\n+\tassert(image != nullptr);\n+\n+\tmappedBuffers_[buffer] = std::move(image);\n+}\n+\n+bool SDLSink::processRequest(Request *request)\n+{\n+\tfor (auto [stream, buffer] : request->buffers())\n+\t\twriteBuffer(buffer);\n+\n+\treturn true;\n+}\n+\n+void SDLSink::writeBuffer(FrameBuffer *buffer)\n+{\n+\tImage *image = mappedBuffers_[buffer].get();\n+\n+\tfor (unsigned int i = 0; i < buffer->planes().size(); ++i) {\n+\t\tconst FrameMetadata::Plane &meta = buffer->metadata().planes()[i];\n+\n+\t\tSpan<uint8_t> data = image->data(i);\n+\t\tif (meta.bytesused > data.size())\n+\t\t\tstd::cerr << \"payload size \" << meta.bytesused\n+\t\t\t\t  << \" larger than plane size \" << data.size()\n+\t\t\t\t  << std::endl;\n+\n+\t\tSDL_UpdateTexture(sdlTexture_, &sdlRect_, data.data(), sdlRect_.w * 2);\n+\t\tSDL_RenderClear(sdlRenderer_);\n+\t\tSDL_RenderCopy(sdlRenderer_, sdlTexture_, NULL, NULL);\n+\t\tSDL_RenderPresent(sdlRenderer_);\n+\t\tSDL_Event e;\n+\t\twhile (SDL_PollEvent(&e)) {\n+\t\t\tif (e.type == SDL_QUIT) { // click close icon then quit\n+\t\t\t\tkill(getpid(), SIGINT);\n+\t\t\t}\n+\t\t};\n+\t}\n+}\ndiff --git a/src/cam/sdl_sink.h b/src/cam/sdl_sink.h\nnew file mode 100644\nindex 00000000..f4f843fa\n--- /dev/null\n+++ b/src/cam/sdl_sink.h\n@@ -0,0 +1,40 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * sdl_sink.h - SDL Sink\n+ */\n+\n+#pragma once\n+\n+#include <map>\n+#include <memory>\n+#include <string>\n+\n+#include <libcamera/stream.h>\n+\n+#include <SDL2/SDL.h>\n+\n+#include \"frame_sink.h\"\n+\n+class Image;\n+\n+class SDLSink : public FrameSink\n+{\n+public:\n+\tSDLSink();\n+\t~SDLSink();\n+\n+\tint configure(const libcamera::CameraConfiguration &cfg) override;\n+\n+\tvoid mapBuffer(libcamera::FrameBuffer *buffer) override;\n+\n+\tbool processRequest(libcamera::Request *request) override;\n+\n+private:\n+\tvoid writeBuffer(libcamera::FrameBuffer *buffer);\n+\n+\tstd::map<libcamera::FrameBuffer *, std::unique_ptr<Image>> mappedBuffers_;\n+\tSDL_Window *sdlScreen_;\n+\tSDL_Renderer *sdlRenderer_;\n+\tSDL_Texture *sdlTexture_;\n+\tSDL_Rect sdlRect_;\n+};\n",
    "prefixes": [
        "libcamera-devel"
    ]
}