Show a patch.

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

{
    "id": 15535,
    "url": "https://patchwork.libcamera.org/api/patches/15535/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/15535/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/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": "<20220324135802.35230-1-ecurtin@redhat.com>",
    "date": "2022-03-24T13:58:02",
    "name": "[libcamera-devel,v3] cam: sdl_sink: Add SDL sink",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "d8d72a15c2122e998b1caee070b6d72db9ab6b5d",
    "submitter": {
        "id": 101,
        "url": "https://patchwork.libcamera.org/api/people/101/?format=api",
        "name": "Eric Curtin",
        "email": "ecurtin@redhat.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/15535/mbox/",
    "series": [
        {
            "id": 2996,
            "url": "https://patchwork.libcamera.org/api/series/2996/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2996",
            "date": "2022-03-24T13:58:02",
            "name": "[libcamera-devel,v3] cam: sdl_sink: Add SDL sink",
            "version": 3,
            "mbox": "https://patchwork.libcamera.org/series/2996/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/15535/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/15535/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 86E7FBD80A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 24 Mar 2022 13:58:12 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AE912604D5;\n\tThu, 24 Mar 2022 14:58:11 +0100 (CET)",
            "from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2289860397\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 24 Mar 2022 14:58:09 +0100 (CET)",
            "from mail-wm1-f72.google.com (mail-wm1-f72.google.com\n\t[209.85.128.72]) 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-220-TRr2rHoLMseSviHgUjY1EQ-1; Thu, 24 Mar 2022 09:58:07 -0400",
            "by mail-wm1-f72.google.com with SMTP id\n\tv67-20020a1cac46000000b00383e71bb26fso1596291wme.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 24 Mar 2022 06:58:07 -0700 (PDT)",
            "from p1.Home ([2001:8a0:6724:4500:a69c:e66f:828e:b340])\n\tby smtp.gmail.com with ESMTPSA id\n\to11-20020adf9d4b000000b001f0077ea337sm2764054wre.22.2022.03.24.06.58.05\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 24 Mar 2022 06:58:05 -0700 (PDT)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1648130291;\n\tbh=SG8MnUOMj3tVtciFc4EC8/3agrNf6VmtIpovFHaAAyk=;\n\th=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post:\n\tList-Help:List-Subscribe:From:Reply-To:From;\n\tb=dvC7CTOPPGnF5NDj75ld4dM7UDg93sC8MyqDuTbFnRBtam/Xt8/Hwe3niycMphEwR\n\tkQhVI7IXhvqZ5DyoweItXfaU5PU1VobtL0uS2rhAyEDcF9SPRh9++mXb9KhKpA3Z8A\n\toiUexR03nZ71KS6gVxqdzscw5F9r5NcAJLBhg3O2J/jYRtJSbblZcKKLPuAsQ9hfNf\n\t3/hF+GoKEt1mSWCKlrpHbzE0G0FUwDJpHCj4qW9gDujlI/DxKseE/ozYQZnSPRYvsd\n\teQvgpP9uRieoP6COmiUh9u8ag1nR6Mm94SodcjOS32Na3dFzGWvfQ8l/+vlyhahyAg\n\tY5cZI7dPXRKwQ==",
            "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1648130288;\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=BLmxiauL1NtN884DxqKPUud+ma+fTdy75GL58X/VFf8=;\n\tb=cFR7dcsnFvLxUT941H6Ea2XjJyNQL+7LwWdO7gkyA2PM77jVkL/O/lHuBUISoZHVqu2NW0\n\t1Vh+X6EUG04MI7B30cb2q1bZMPaXPz6X7FofzUVmdX4fsPS2h8Ye8RWEKQWrywDGClSYPC\n\tG40nwFEL3cukgTB3lu1qnQQBYng4qHc="
        ],
        "Authentication-Results": [
            "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=redhat.com\n\theader.i=@redhat.com header.b=\"cFR7dcsn\"; \n\tdkim-atps=neutral",
            "relay.mimecast.com;\n\tauth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ecurtin@redhat.com"
        ],
        "X-MC-Unique": "TRr2rHoLMseSviHgUjY1EQ-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=BLmxiauL1NtN884DxqKPUud+ma+fTdy75GL58X/VFf8=;\n\tb=bePMyBjDv/YYi7HqUdm5jFBaYQbMO2kIcZ8GDDcX3HZ3MftXiia4YfgN39VAxFgmm3\n\tNh7jsBGhf0dvjEIE4txyfzuuYTNaV3L901iz+/vvp1/CLw2CFzRHJGkT2+gO1dvGJifK\n\tczMHk9p/qGcyIx6i+/jdX5z5E+9Ud19082J0EygEN3lywSy03/aF3tmUW2Htc2bLPKh4\n\th8331yClhZzAbsKpEGhslST8XLv+K9rdMNkTsFBr6RwzxRo0OYayoVtQzlpRKWMR4WIb\n\tbA/WnzMrvaSgaYcb3HdyABuIujCqmROHPM9Xh2jHX5I6UWNPKzoenExFmgKZHVZLbgfG\n\t5lsA==",
        "X-Gm-Message-State": "AOAM533y4y3CB4r6h0HYJMeyoQgb296DLwNneeuvzSCI11NdjopsJFLD\n\tkwBRhS3n70Cvv8C8sl39auYAMZhg62tmYWjE4T5G4kc4SeFxijax92lFnLN+CGCrfBnqrMtgqAv\n\trmsfp9qH92DJyEzq2ugt7RTjOMGC188hJeTnj5k6vVTnryh2DBHTUA85sDninX5E0ZMFa5mxwS5\n\tZ+xl6t0nsP",
        "X-Received": [
            "by 2002:a05:600c:1e02:b0:38c:a5c2:243 with SMTP id\n\tay2-20020a05600c1e0200b0038ca5c20243mr14471917wmb.150.1648130286090; \n\tThu, 24 Mar 2022 06:58:06 -0700 (PDT)",
            "by 2002:a05:600c:1e02:b0:38c:a5c2:243 with SMTP id\n\tay2-20020a05600c1e0200b0038ca5c20243mr14471897wmb.150.1648130285743; \n\tThu, 24 Mar 2022 06:58:05 -0700 (PDT)"
        ],
        "X-Google-Smtp-Source": "ABdhPJw4mHDDYsu4Mp9vl3EDA12tABGqe9s96wO1FRtSuejhNftjGOJQbvV7WMq/i/G0VOCT4KKBqw==",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Thu, 24 Mar 2022 13:58:02 +0000",
        "Message-Id": "<20220324135802.35230-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\"; x-default=true",
        "Subject": "[libcamera-devel] [PATCH v3] 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. Can also be used as kmsdrm sink.\n\nSigned-off-by: Eric Curtin <ecurtin@redhat.com>\n---\nChanges in v2:\n - Remove hardcoded pixel format from SDL_CreateTexture call\n\nChanges in v3:\n - Drop blank line\n - The contents of the if..endif are indented\n - Split configure function into start and configure\n - Add SDL_DestroyRenderer\n - Remove assign and test in the same statement\n---\n src/cam/camera_session.cpp |   8 +++\n src/cam/main.cpp           |   5 ++\n src/cam/main.h             |   1 +\n src/cam/meson.build        |  10 +++\n src/cam/sdl_sink.cpp       | 133 +++++++++++++++++++++++++++++++++++++\n src/cam/sdl_sink.h         |  41 ++++++++++++\n 6 files changed, 198 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..1d62a64a 100644\n--- a/src/cam/main.cpp\n+++ b/src/cam/main.cpp\n@@ -137,6 +137,11 @@ int CamApp::parseOptions(int argc, char *argv[])\n \t\t\t \"Display viewfinder through DRM/KMS on specified connector\",\n \t\t\t \"display\", ArgumentOptional, \"connector\", false,\n \t\t\t OptCamera);\n+#endif\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\"\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..bd536c5b 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..97f601b1\n--- /dev/null\n+++ b/src/cam/sdl_sink.cpp\n@@ -0,0 +1,133 @@\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+\t: sdlRenderer_(0)\n+{\n+\tmemset(&sdlRect_, 0, sizeof(sdlRect_));\n+}\n+\n+SDLSink::~SDLSink()\n+{\n+\tif (sdlRenderer_)\n+\t\tSDL_DestroyRenderer(sdlRenderer_);\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+\tconst libcamera::StreamConfiguration &sCfg = cfg.at(0);\n+\tpf = (SDL_PixelFormatEnum)sCfg.pixelFormat.fourcc();\n+\tif (pf == SDL_DEFINE_PIXELFOURCC('Y', 'U', 'Y', 'V')) {\n+\t\tpf = SDL_PIXELFORMAT_YUY2;\n+\t} else if (int ne = strcmp(SDL_GetPixelFormatName(pf), \"SDL_PIXELFORMAT_UNKNOWN\"); !ne) {\n+\t\tstd::cerr << \"SDL_GetPixelFormatName error - exiting: SDL_PIXELFORMAT_UNKNOWN, no \" << sCfg.pixelFormat.toString() << \" support\\n\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tsdlRect_.w = sCfg.size.width;\n+\tsdlRect_.h = sCfg.size.height;\n+\n+\treturn 0;\n+}\n+\n+int SDLSink::start()\n+{\n+\tint ret = SDL_Init(SDL_INIT_VIDEO);\n+\tif (ret) {\n+\t\tstd::cerr << \"SDL_Init error - exiting: \" << SDL_GetError() << std::endl;\n+\t\treturn ret;\n+\t}\n+\n+\tsdlScreen_ = SDL_CreateWindow(\"\", SDL_WINDOWPOS_UNDEFINED,\n+\t\t\t\t      SDL_WINDOWPOS_UNDEFINED, sdlRect_.w,\n+\t\t\t\t      sdlRect_.h, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);\n+\tif (!sdlScreen_) {\n+\t\tstd::cerr << \"SDL_CreateWindow error - exiting: \" << SDL_GetError() << std::endl;\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tsdlRenderer_ = SDL_CreateRenderer(\n+\t\tsdlScreen_, -1, 0);\n+\tif (!sdlRenderer_) {\n+\t\tstd::cerr << \"SDL_CreateRenderer error - exiting: \" << SDL_GetError() << std::endl;\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tSDL_RenderSetLogicalSize(sdlRenderer_, sdlRect_.w,\n+\t\t\t\t sdlRect_.h);\n+\n+\tsdlTexture_ =\n+\t\tSDL_CreateTexture(sdlRenderer_, pf,\n+\t\t\t\t  SDL_TEXTUREACCESS_STREAMING, sdlRect_.w,\n+\t\t\t\t  sdlRect_.h);\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+\tfor (SDL_Event e; SDL_PollEvent(&e);) {\n+\t\tif (e.type == SDL_QUIT) { // click close icon then quit\n+\t\t\tkill(getpid(), SIGINT);\n+\t\t}\n+\t}\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}\n+}\ndiff --git a/src/cam/sdl_sink.h b/src/cam/sdl_sink.h\nnew file mode 100644\nindex 00000000..f5f8982a\n--- /dev/null\n+++ b/src/cam/sdl_sink.h\n@@ -0,0 +1,41 @@\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+\tint start() override;\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+\tSDL_PixelFormatEnum pf;\n+};\n",
    "prefixes": [
        "libcamera-devel",
        "v3"
    ]
}