From patchwork Fri Apr 8 16:00:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 15662 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 93391C3256 for ; Fri, 8 Apr 2022 16:00:28 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5A7F765645; Fri, 8 Apr 2022 18:00:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1649433628; bh=Ng/S6yciohAgIdIwU/in2KfehV/qlxjNl71w3TK6kFk=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=20VYLWhcXruLMZKuHK+7isGVAND8fqfhPUV686XUVqcBhuKHpcWLC+gHfXWb/hmXV 5rDXLqvqdKdkAlETg57IIANJweN3Y2/1e5OPeBnv5RHgHKbmakAi2nCIBKEdduNLBM o4SU11W6qdQwM5emWdtJWg5SsBUWZi0C8EvofXgnKg/dOn+liQ7i/FM7uH0fFmyD8S T5/JwKYzjY3lnYjgfcWn7QPdKx6ubTU/TC83Ez3reD6ECBWmgoYNK2R+N4Wux1sHko FNgAUctusETEjdDrQKdggaapGMxvfwvZZG+yjcTn5nXo44HfCDAGxANmui4i9gr/RL phEtrvv60P1Dw== Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9E60E604BA for ; Fri, 8 Apr 2022 18:00:26 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="VPBa3GSK"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649433625; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hmPJL3UzOocqYomaoA1bip63kjAEIMDVyW75F3umxxY=; b=VPBa3GSKvO2gHuGqCmLplirbj9kDt7U/C0oLLkygAMazVgFr0zI9RPk80X0YyijZuz9YFf bxz0Fkh1Ek+g0RQSZ414yTiUGEHtLxemAK6HW+URTjVraDZ7gHC7QKM9O9VkmtB59Gkvip p1l53ABF6h8nFGHZcgBWKl/op/6che0= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-491-CLuv_uplO3m883r__Kiv-g-1; Fri, 08 Apr 2022 12:00:22 -0400 X-MC-Unique: CLuv_uplO3m883r__Kiv-g-1 Received: by mail-wm1-f70.google.com with SMTP id r64-20020a1c2b43000000b0038b59eb1940so4438464wmr.0 for ; Fri, 08 Apr 2022 09:00:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hmPJL3UzOocqYomaoA1bip63kjAEIMDVyW75F3umxxY=; b=keWO1TtXut8AV3IHHZmm5QW/MiZHeu8Bx02q4X53X0VbmV2F4xrQ4lZ7u7+ZUqVvKL wTR7sCiWMeVfvbUB/6Fpt7BEb8gdhtR14FIgdET8sDylBwR6u9r6/0qc95MhfPOg9yKw gRYqGmiy/ffooyRg8CMtRSz2/yHxNE0V1t2IhnS2JpEE4gpoFLZ+X8EKniQ6iuSP5VI3 q7rHPKe8VDQwAQiIaY3iKNecGAqJ/V7/ZHdts8wn4mS6QEU6Y2WAkutc2NYRn8edX/+c EwuP1I6eX01NgR3xrSxm9wolxDlg4XjGzIfhN6Ti4xNoKXvYABtFTP7nEbZH5g/AhSYv tlWg== X-Gm-Message-State: AOAM530Tb5FOYgFQKWouKVA8nvx+mB+F8/k1eAJ4SJaStMNNTH9Rlk1B qADXQ4ZsGzMREoBIAqVrm/9QQ4Od8gzW2vfYUxsDGeXKRzFAdnEKTs2Vt5PwBwYma1KB/4pBxDb 3Er0X0VEpqTaeDJkEDqpDDehMCyZ/zOBlkeklftom0GWVeN8+Ue4rVwWxIpd0r0VXhX+nVOeZen W27Bh8abm3 X-Received: by 2002:a7b:c041:0:b0:38c:7c21:8ade with SMTP id u1-20020a7bc041000000b0038c7c218ademr17565580wmc.163.1649433620979; Fri, 08 Apr 2022 09:00:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwv1/xBFAv/dNTK0ILG04pKluQYYBzty1va2U5vgtptCulv258ockDgaf5qjPnuMw0+7HCbJg== X-Received: by 2002:a7b:c041:0:b0:38c:7c21:8ade with SMTP id u1-20020a7bc041000000b0038c7c218ademr17565541wmc.163.1649433620509; Fri, 08 Apr 2022 09:00:20 -0700 (PDT) Received: from p1.Home ([2001:8a0:6724:4500:a69c:e66f:828e:b340]) by smtp.gmail.com with ESMTPSA id a12-20020a5d53cc000000b00205a0ee9c74sm18913521wrw.89.2022.04.08.09.00.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Apr 2022 09:00:20 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Fri, 8 Apr 2022 17:00:15 +0100 Message-Id: <20220408160015.33612-2-ecurtin@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220408160015.33612-1-ecurtin@redhat.com> References: <20220408160015.33612-1-ecurtin@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ecurtin@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [libcamera-devel] [PATCH v7 2/2] cam: sdl_sink: Add SDL sink 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: , X-Patchwork-Original-From: Eric Curtin via libcamera-devel From: Eric Curtin Reply-To: Eric Curtin Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This adds more portability to existing cam sinks. You can pass a YUYV camera buffer for example and SDL will handle the pixel buffer conversion, although SDL does not support decompression for pixelformats like MJPEG. This allows cam reference implementation to display images on VMs, Mac M1, Raspberry Pi, etc. This also enables cam reference implementation, to run as a desktop application in wayland or x11. SDL also has support for Android and ChromeOS which has not been tested. Also tested on simpledrm raspberry pi 4 framebuffer successfully where existing kms sink did not work. Can also be used as kmsdrm sink. Only supports one camera stream at present. Signed-off-by: Eric Curtin --- src/cam/camera_session.cpp | 8 ++ src/cam/main.cpp | 5 + src/cam/main.h | 1 + src/cam/meson.build | 10 ++ src/cam/sdl_sink.cpp | 198 +++++++++++++++++++++++++++++++++++++ src/cam/sdl_sink.h | 46 +++++++++ 6 files changed, 268 insertions(+) create mode 100644 src/cam/sdl_sink.cpp create mode 100644 src/cam/sdl_sink.h diff --git a/src/cam/camera_session.cpp b/src/cam/camera_session.cpp index 0428b538..30162dbd 100644 --- a/src/cam/camera_session.cpp +++ b/src/cam/camera_session.cpp @@ -19,6 +19,9 @@ #ifdef HAVE_KMS #include "kms_sink.h" #endif +#ifdef HAVE_SDL +#include "sdl_sink.h" +#endif #include "main.h" #include "stream_options.h" @@ -187,6 +190,11 @@ int CameraSession::start() sink_ = std::make_unique(options_[OptDisplay].toString()); #endif +#ifdef HAVE_SDL + if (options_.isSet(OptSDL)) + sink_ = std::make_unique(); +#endif + if (options_.isSet(OptFile)) { if (!options_[OptFile].toString().empty()) sink_ = std::make_unique(streamNames_, diff --git a/src/cam/main.cpp b/src/cam/main.cpp index c7f664b9..1d62a64a 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -137,6 +137,11 @@ int CamApp::parseOptions(int argc, char *argv[]) "Display viewfinder through DRM/KMS on specified connector", "display", ArgumentOptional, "connector", false, OptCamera); +#endif +#ifdef HAVE_SDL + parser.addOption(OptSDL, OptionNone, + "Display viewfinder through SDL", + "sdl", ArgumentNone, "", false, OptCamera); #endif parser.addOption(OptFile, OptionString, "Write captured frames to disk\n" diff --git a/src/cam/main.h b/src/cam/main.h index 62f7bbc9..2b285808 100644 --- a/src/cam/main.h +++ b/src/cam/main.h @@ -18,6 +18,7 @@ enum { OptListProperties = 'p', OptMonitor = 'm', OptStream = 's', + OptSDL = 'S', OptListControls = 256, OptStrictFormats = 257, OptMetadata = 258, diff --git a/src/cam/meson.build b/src/cam/meson.build index 5bab8c9e..59787741 100644 --- a/src/cam/meson.build +++ b/src/cam/meson.build @@ -32,11 +32,21 @@ if libdrm.found() ]) endif +libsdl2 = dependency('SDL2', required : false) + +if libsdl2.found() + cam_cpp_args += ['-DHAVE_SDL'] + cam_sources += files([ + 'sdl_sink.cpp' + ]) +endif + cam = executable('cam', cam_sources, dependencies : [ libatomic, libcamera_public, libdrm, + libsdl2, libevent, ], cpp_args : cam_cpp_args, diff --git a/src/cam/sdl_sink.cpp b/src/cam/sdl_sink.cpp new file mode 100644 index 00000000..03511974 --- /dev/null +++ b/src/cam/sdl_sink.cpp @@ -0,0 +1,198 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * sdl_sink.cpp - SDL Sink + */ + +#include "sdl_sink.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "event_loop.h" +#include "image.h" + +using namespace libcamera; + +SDLSink::SDLSink() + : sdlWindow_(nullptr), sdlRenderer_(nullptr), sdlTexture_(nullptr), sdlRect_({}), sdlInit_(false) +{ +} + +SDLSink::~SDLSink() +{ + stop(); +} + +int SDLSink::configure(const libcamera::CameraConfiguration &cfg) +{ + int ret = FrameSink::configure(cfg); + if (ret < 0) + return ret; + + if (cfg.size() > 1) { + std::cerr << "SDL sink only supports one camera stream at present, streaming first camera stream" + << std::endl; + } else if (cfg.empty()) { + std::cerr << "Require at least one camera stream to process" << std::endl; + return -EINVAL; + } + + const libcamera::StreamConfiguration &sCfg = cfg.at(0); + sdlRect_.w = sCfg.size.width; + sdlRect_.h = sCfg.size.height; + switch (sCfg.pixelFormat) { + case libcamera::formats::YUYV: + pixelFormat_ = SDL_PIXELFORMAT_YUY2; + pitch_ = 4 * ((sdlRect_.w + 1) / 2); + break; + + /* From here down the fourcc values are identical between SDL, drm, libcamera */ + case libcamera::formats::NV21: + pixelFormat_ = SDL_PIXELFORMAT_NV21; + pitch_ = sdlRect_.w; + break; + case libcamera::formats::NV12: + pixelFormat_ = SDL_PIXELFORMAT_NV12; + pitch_ = sdlRect_.w; + break; + case libcamera::formats::YVU420: + pixelFormat_ = SDL_PIXELFORMAT_YV12; + pitch_ = sdlRect_.w; + break; + case libcamera::formats::YVYU: + pixelFormat_ = SDL_PIXELFORMAT_YVYU; + pitch_ = 4 * ((sdlRect_.w + 1) / 2); + break; + case libcamera::formats::UYVY: + pixelFormat_ = SDL_PIXELFORMAT_UYVY; + pitch_ = 4 * ((sdlRect_.w + 1) / 2); + break; + default: + std::cerr << sCfg.pixelFormat.toString() << " not present in libcamera <-> SDL map" + << std::endl; + return -EINVAL; + }; + + return 0; +} + +int SDLSink::start() +{ + int ret = SDL_Init(SDL_INIT_VIDEO); + if (ret) { + std::cerr << "Failed to initialize SDL: " << SDL_GetError() << std::endl; + return ret; + } + + sdlInit_ = true; + sdlWindow_ = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, sdlRect_.w, sdlRect_.h, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); + if (!sdlWindow_) { + std::cerr << "Failed to create SDL window: " << SDL_GetError() << std::endl; + return -EINVAL; + } + + sdlRenderer_ = SDL_CreateRenderer(sdlWindow_, -1, 0); + if (!sdlRenderer_) { + std::cerr << "Failed to create SDL renderer: " << SDL_GetError() << std::endl; + return -EINVAL; + } + + ret = SDL_RenderSetLogicalSize(sdlRenderer_, sdlRect_.w, sdlRect_.h); + if (ret) { /* Not critical to set, don't return in this case, set for scaling purposes */ + std::cerr << "Failed to set SDL render logical size: " << SDL_GetError() << std::endl; + } + + sdlTexture_ = SDL_CreateTexture(sdlRenderer_, pixelFormat_, SDL_TEXTUREACCESS_STREAMING, sdlRect_.w, sdlRect_.h); + if (!sdlTexture_) { + std::cerr << "Failed to create SDL texture: " << SDL_GetError() << std::endl; + return -EINVAL; + } + + EventLoop::instance()->addTimerEvent(10000, std::bind(&SDLSink::processSDLEvents, this)); + + return 0; +} + +int SDLSink::stop() +{ + if (sdlTexture_) { + SDL_DestroyTexture(sdlTexture_); + sdlTexture_ = nullptr; + } + + if (sdlRenderer_) { + SDL_DestroyRenderer(sdlRenderer_); + sdlRenderer_ = nullptr; + } + + if (sdlWindow_) { + SDL_DestroyWindow(sdlWindow_); + sdlWindow_ = nullptr; + } + + if (sdlInit_) { + SDL_Quit(); + sdlInit_ = false; + } + + return FrameSink::stop(); +} + +void SDLSink::mapBuffer(FrameBuffer *buffer) +{ + std::unique_ptr image = Image::fromFrameBuffer(buffer, Image::MapMode::ReadOnly); + assert(image != nullptr); + + mappedBuffers_[buffer] = std::move(image); +} + +bool SDLSink::processRequest(Request *request) +{ + for (auto [stream, buffer] : request->buffers()) { + renderBuffer(buffer); + break; /* to be expanded to launch SDL window per buffer */ + } + + return true; +} + +/* + * Process SDL events, required for things like window resize and quit button + */ +void SDLSink::processSDLEvents() +{ + for (SDL_Event e; SDL_PollEvent(&e);) { + if (e.type == SDL_QUIT) { /* click close icon then quit */ + EventLoop::instance()->exit(0); + } + } +} + +void SDLSink::renderBuffer(FrameBuffer *buffer) +{ + Image *image = mappedBuffers_[buffer].get(); + + for (unsigned int i = 0; i < buffer->planes().size(); ++i) { + const FrameMetadata::Plane &meta = buffer->metadata().planes()[i]; + + Span data = image->data(i); + if (meta.bytesused > data.size()) + std::cerr << "payload size " << meta.bytesused + << " larger than plane size " << data.size() + << std::endl; + + SDL_UpdateTexture(sdlTexture_, &sdlRect_, data.data(), pitch_); + SDL_RenderClear(sdlRenderer_); + SDL_RenderCopy(sdlRenderer_, sdlTexture_, nullptr, nullptr); + SDL_RenderPresent(sdlRenderer_); + } +} diff --git a/src/cam/sdl_sink.h b/src/cam/sdl_sink.h new file mode 100644 index 00000000..c9b0ab8e --- /dev/null +++ b/src/cam/sdl_sink.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * sdl_sink.h - SDL Sink + */ + +#pragma once + +#include +#include +#include + +#include + +#include + +#include "frame_sink.h" + +class Image; + +class SDLSink : public FrameSink +{ +public: + SDLSink(); + ~SDLSink(); + + int configure(const libcamera::CameraConfiguration &cfg) override; + int start() override; + int stop() override; + void mapBuffer(libcamera::FrameBuffer *buffer) override; + + bool processRequest(libcamera::Request *request) override; + +private: + void renderBuffer(libcamera::FrameBuffer *buffer); + void processSDLEvents(); + + std::map> mappedBuffers_; + + SDL_Window *sdlWindow_; + SDL_Renderer *sdlRenderer_; + SDL_Texture *sdlTexture_; + SDL_Rect sdlRect_; + SDL_PixelFormatEnum pixelFormat_; + bool sdlInit_; + int pitch_; +};