From patchwork Thu May 5 15:18:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 15799 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 824E2C3256 for ; Thu, 5 May 2022 15:19:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B235765643; Thu, 5 May 2022 17:19:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1651763954; bh=xcfZ1H7WQhV0yufR4gVBIl3oFguirvVmlkN8iP2vPfw=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=D/II3GpxwzEqGPpD6G5X14K0FZwOz78NocZmaxENsvYtk2bua6HT3gyFPzvmkqXC9 s2SX2985Yr0xXZRcTyhBOYAjEaBkpxUo6xo7yUoU2UY4bntYF1n9sGJYEOcLJqZLQZ QEaIaM10fZrqhpBEGPbJv2lDSStaSqFbtnldg2aRxQOZ+bYisGfP8o+L7NxQ/vsBCJ cAKJTtLw841i5zZSOrdvxB5pJEfwZfs01J1ywlXloIW4vCs256jUrZ0icsa0T290ei BBBSUcUmtRaXrGOICv7/bUQZR4YSUmN3DdT4OVTFtgplozPBYunmEGWbc1HbJ4KoSE xMDilcubHrwbQ== Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.133.74]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 521F6603AB for ; Thu, 5 May 2022 17:19:12 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="JUrmO7XI"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651763950; 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; bh=leP7SagDNmsqLLhIqUJjkwfGzMU02ITMXoicGnFkI44=; b=JUrmO7XIZCqxP6/HxuuLj3hTA6+8VrYa3LIyuBhzbaPUJ01Y4m5YqJvwu389841v9dz8Zu tn/qQLIaPeaUu2NLsUPKYyvkDXO1NXKrC6Un+6kj1XzJVywWM9WfKavYUpsR+fc3Oa92AI zAhFWYvzQKJrRsHUVwKigu8wuTP4Ymc= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-410-H11vJBASOymvY85ALjFCMg-1; Thu, 05 May 2022 11:19:09 -0400 X-MC-Unique: H11vJBASOymvY85ALjFCMg-1 Received: by mail-wm1-f72.google.com with SMTP id s14-20020a05600c29ce00b00394611a8059so1899141wmd.4 for ; Thu, 05 May 2022 08:19:09 -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:mime-version :content-transfer-encoding; bh=leP7SagDNmsqLLhIqUJjkwfGzMU02ITMXoicGnFkI44=; b=Jypav9XH/JhgqOWzy/m1p8CrQdN5r8bVp9cURdR8ddILRoBx/VevQ0mlV9Sbq1R+OX 6/mjbNvHPIsJOkheswfYfSkBYQfbesazHBhPm8ceMaVMygiVzEE2JWSFPTUGCil/PkWE wRJVqq5c0IE4ntVyYc6zBzwbTuFv+NzqtrFRkHV6+965a2AUBZ/LJtQZ6kFwJOJQd7kb URqk6qzDjJ1W5ocAcZuFqmBqXfVmKLiYtGfFg8+30LbuKNfDMGq05HOaH0Fg7eVm8j3Z ItspDfWDKvggngD6gwX5TwW1D/0452GLqeHaNnIvMuBBfBhXKybtH+hZ+cjyhHetp5m3 G2VQ== X-Gm-Message-State: AOAM531CVwQZ7zeYP1HHEAunBauR1aSCShLQHzBcm4grbjUfnOiZ0DNg /OxPefQB6+4EX9s7tWQOZi4R/9gUfcN1I2RaupwFMsB03VagGBSmBTddb8DoeyuV8bXq8IDmzIL Ke2gRPXgzF6skbQa4i2ZSCzR8LG455xK8sWHqWZsFz74U7I0kBSeQdJwkNtp+zb4VPk0hDir8Z8 UmvG/skYt9 X-Received: by 2002:a05:600c:198f:b0:392:9120:c6ee with SMTP id t15-20020a05600c198f00b003929120c6eemr5614470wmq.18.1651763948172; Thu, 05 May 2022 08:19:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxAORZ6i5qRgb2D8pivc34QzuYo6cCFJbub7qD/JSYA3JvM+lWQahN4HvoTOV1dXkGy4swAZg== X-Received: by 2002:a05:600c:198f:b0:392:9120:c6ee with SMTP id t15-20020a05600c198f00b003929120c6eemr5614437wmq.18.1651763947791; Thu, 05 May 2022 08:19:07 -0700 (PDT) Received: from localhost.localdomain ([2001:bb6:9634:7100:33b0:c1b1:8875:4adb]) by smtp.gmail.com with ESMTPSA id z6-20020a056000110600b0020ad05214d5sm1434819wrw.2.2022.05.05.08.19.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 08:19:07 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 5 May 2022 16:18:48 +0100 Message-Id: <20220505151851.2421-1-ecurtin@redhat.com> X-Mailer: git-send-email 2.35.1 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 v8 1/4] cam: event_loop: Add timer events to event loop 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" Extend the EventLoop class to support periodic timer events. This can be used to run tasks periodically, such as handling the event loop of SDL. Also delete all events in the list, before we event_base_loopbreak. Signed-off-by: Eric Curtin Reviewed-by: Kieran Bingham Tested-by: Jacopo Mondi --- src/cam/event_loop.cpp | 33 +++++++++++++++++++++++++++++++++ src/cam/event_loop.h | 6 ++++++ 2 files changed, 39 insertions(+) diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp index e25784c0..181c971c 100644 --- a/src/cam/event_loop.cpp +++ b/src/cam/event_loop.cpp @@ -47,6 +47,10 @@ int EventLoop::exec() void EventLoop::exit(int code) { exitCode_ = code; + for (auto const &e : events_) { + event_del(e->event_); + } + event_base_loopbreak(base_); } @@ -84,6 +88,35 @@ void EventLoop::addEvent(int fd, EventType type, events_.push_back(std::move(event)); } +void EventLoop::toTimeval(const std::chrono::milliseconds d, struct timeval &tv) +{ + tv.tv_sec = std::chrono::duration_cast(d).count(); + tv.tv_usec = std::chrono::duration_cast(d).count(); +} + +void EventLoop::addTimerEvent(const std::chrono::milliseconds d, + const std::function &callback) +{ + std::unique_ptr event = std::make_unique(callback); + event->event_ = event_new(base_, -1, EV_PERSIST, &EventLoop::Event::dispatch, + event.get()); + if (!event->event_) { + std::cerr << "Failed to create timer event" << std::endl; + return; + } + + struct timeval tv; + toTimeval(d, tv); + + int ret = event_add(event->event_, &tv); + if (ret < 0) { + std::cerr << "Failed to add timer event" << std::endl; + return; + } + + events_.push_back(std::move(event)); +} + void EventLoop::dispatchCallback([[maybe_unused]] evutil_socket_t fd, [[maybe_unused]] short flags, void *param) { diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h index a4613eb2..89215dce 100644 --- a/src/cam/event_loop.h +++ b/src/cam/event_loop.h @@ -14,6 +14,8 @@ #include +using namespace std::chrono_literals; + struct event_base; class EventLoop @@ -37,6 +39,9 @@ public: void addEvent(int fd, EventType type, const std::function &handler); + void addTimerEvent(const std::chrono::milliseconds d, + const std::function &handler); + private: struct Event { Event(const std::function &callback); @@ -57,6 +62,7 @@ private: std::list> events_; std::mutex lock_; + void toTimeval(const std::chrono::milliseconds d, struct timeval &tv); static void dispatchCallback(evutil_socket_t fd, short flags, void *param); void dispatchCall(); From patchwork Thu May 5 15:18:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 15800 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 6E2EAC3256 for ; Thu, 5 May 2022 15:19:17 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2FC226563F; Thu, 5 May 2022 17:19:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1651763957; bh=j12gqWSP+QeX2PBTiuHIllAFBnA20FVAmFnqRzibGSU=; 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=O1zNWcafurIIvErlbG5lDo8XysC5bCilZMCnqAxPhDD99Njdbqb0/+aH8hFAaLqxz +WVGuw3e9IbxmZvrUxREnCNfcV6cGMBwi2MQFXb5WYuKxyNFJK2PouiZJ+iE60KjKq uG9TjCl8zFkB05jh2hhFDUKsusajvVbZHUmHi9f1J3Xbylyp6imtJnqg9Luz9YuyUD k3MCTrOG0YWs5z+QVvFAlGNi83a4ilbtlY0fQh0WWEUTkdSNN9O5VoLbIRbYUB7r24 NrTECR641fC+GidOEMlE9awgRWJfO7QmDgFsD+v1Z23yI8Qgu5zpOFB9JBxemg4/sN kDp6x8akpadug== Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.133.74]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CBB9760424 for ; Thu, 5 May 2022 17:19:15 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="RaItfkK4"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651763954; 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=Ju9IjSwBQXYcqiAXugQthBm4b9ZwKNEBO0Yd1CGdgfc=; b=RaItfkK4rCW0eCWJT7wwxmpjQCnFnWZOnYnNPfae5BmsfrpRdJa1sDGtKJvNkRz2Dj5wg0 pOUsAXi7uYnC7j81keTKM5QT9chmiul/CTcJKzeNrXfkBpCCQU5fM4cszUY0rLykc7TLk/ Ph52ljLyF1vPXpN7yN55J1YlNpP/7Ig= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-588-9baQjdThP5u1YwRxoPJUDA-1; Thu, 05 May 2022 11:19:13 -0400 X-MC-Unique: 9baQjdThP5u1YwRxoPJUDA-1 Received: by mail-wm1-f72.google.com with SMTP id t184-20020a1c46c1000000b00394209f54f1so1841630wma.4 for ; Thu, 05 May 2022 08:19:13 -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=Ju9IjSwBQXYcqiAXugQthBm4b9ZwKNEBO0Yd1CGdgfc=; b=ho00zK+vvozCRT6osqdgUj9zMwFeM8MLJ5rF8m1hM5VvGNZNEvYpzNApcccB28s9OS oN2XkEptl0hslwpQAA6+AD0avbq1jg0lHJk6f2zEFNwQeEj6vZyuxUHH5I2TL9+2lnUv jC747ffZ1w6EAJMd6XExjDr0Jq+OvyADSqTdxudnXwBhJWD9h7pOV6AKFeVQOHwpQnNa vZBOMYEybPAWyvwVHyy5Gn621z89LCu2ruk8IxG29YisJg+RVzGRP/MGU1vVYXyvnzeh wMxxnaiAUCPOzIY3I5PFUPnSi8IF7ghC67qSNveiRW92kb6oEEKCR86v2TKZsDmIDpdr f4DQ== X-Gm-Message-State: AOAM531TTBEjtBN0uWe4FBTWgsXLhAPRB9F3Pr2/uRYtX8+Rcv2Lahsy ImnOoMWKiLANxak86AUZ9wO1w0nMFvIbRjyoCEFgt+0Ds3owaDfNGjCcw8Gs4UMpHhAB8VG/9Zb vMVI9Cr5yVReLexeEbHnfG2h3y44PsRCk3nQVObepGEu0oQcyyJ5wJMn3t1DJUNYY/xzhCFH6jc fHx2oxNanY X-Received: by 2002:a7b:c017:0:b0:394:5631:62bc with SMTP id c23-20020a7bc017000000b00394563162bcmr5546153wmb.190.1651763952160; Thu, 05 May 2022 08:19:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzL873gDGidlmw/mvn5ViRfENkhZ5JO8iXCPwxFGfWlgVSJVCtaWIwdcF/ot5yygvT5PanXUA== X-Received: by 2002:a7b:c017:0:b0:394:5631:62bc with SMTP id c23-20020a7bc017000000b00394563162bcmr5546117wmb.190.1651763951878; Thu, 05 May 2022 08:19:11 -0700 (PDT) Received: from localhost.localdomain ([2001:bb6:9634:7100:33b0:c1b1:8875:4adb]) by smtp.gmail.com with ESMTPSA id z6-20020a056000110600b0020ad05214d5sm1434819wrw.2.2022.05.05.08.19.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 08:19:11 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 5 May 2022 16:18:49 +0100 Message-Id: <20220505151851.2421-2-ecurtin@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220505151851.2421-1-ecurtin@redhat.com> References: <20220505151851.2421-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 v8 2/4] cam: event_loop: Rename addEvent to addFDEvent 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" Since we now have two types of events addFDEvent and addTimerEvent. Signed-off-by: Eric Curtin Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/cam/drm.cpp | 4 ++-- src/cam/event_loop.cpp | 4 ++-- src/cam/event_loop.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cam/drm.cpp b/src/cam/drm.cpp index 46e34eb5..421cd61a 100644 --- a/src/cam/drm.cpp +++ b/src/cam/drm.cpp @@ -432,8 +432,8 @@ int Device::init() if (ret < 0) return ret; - EventLoop::instance()->addEvent(fd_, EventLoop::Read, - std::bind(&Device::drmEvent, this)); + EventLoop::instance()->addFDEvent(fd_, EventLoop::Read, + std::bind(&Device::drmEvent, this)); return 0; } diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp index 181c971c..0c2176c7 100644 --- a/src/cam/event_loop.cpp +++ b/src/cam/event_loop.cpp @@ -64,8 +64,8 @@ void EventLoop::callLater(const std::function &func) event_base_once(base_, -1, EV_TIMEOUT, dispatchCallback, this, nullptr); } -void EventLoop::addEvent(int fd, EventType type, - const std::function &callback) +void EventLoop::addFDEvent(int fd, EventType type, + const std::function &callback) { std::unique_ptr event = std::make_unique(callback); short events = (type & Read ? EV_READ : 0) diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h index 89215dce..d921cd8f 100644 --- a/src/cam/event_loop.h +++ b/src/cam/event_loop.h @@ -36,8 +36,8 @@ public: void callLater(const std::function &func); - void addEvent(int fd, EventType type, - const std::function &handler); + void addFDEvent(int fd, EventType type, + const std::function &handler); void addTimerEvent(const std::chrono::milliseconds d, const std::function &handler); From patchwork Thu May 5 15:18:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 15801 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 E76CBC3256 for ; Thu, 5 May 2022 15:19:20 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 943956564C; Thu, 5 May 2022 17:19:20 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1651763960; bh=XDNlXpuJP0v9ny+9VUZFDkUVY2ZMrZkRKjCRG+cakmw=; 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=eUpzUo/5/LWOy/a9YljxOSMGTpETqWT9WN1b5ysdRi0o9AiIPaEPPI5EKbtQNsO/8 mSwpTy1yyvLsJxmTmmFNQvDj+fY+V8nhr+1W0QKDpenIxBkPklTQmRR27Zi1Dg1wsM x8sGfBZREAJBEaxLm3TWEWOF+8wDQ0o3fwHIHppxCgMwUhAFmG9swP3VPrFzF6Blyl lkoZMdDF3MqMXP4RVPrqhHo6cF8NDJSQejIzYCqJUo5/1TY/u1JkAPCUC9KCnweyYI AFpDx5D2WES++rvlAdQOgzIvNQNvzlsSwXwstzbpxPkL7WXmDaCIDFRdMpOVq0gdiP +qDU+1BthSY5w== Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.133.74]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id ADD7860424 for ; Thu, 5 May 2022 17:19:18 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="bjCT/aCb"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651763957; 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=B9Ps2ABkfKYA90JWqdEc93XHx2W/OPs84CLEMF2jatc=; b=bjCT/aCb1F+FDj650Kw0+tqHG+Crl2Gq0qFYL2eqdcJrazexTY40/ZfdzUsKEd3EQ8Zzqr ZcLZdhOf7VjduyaIeQlNOix7LPmNyFqavYwlEdzNMXAOfZ6ZiAWBO8gDcGF5J/if/j1hYH Lac/gDzScnVqJkiLT9og/GmsZS+VQw8= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-656-Y_1DYaG_Mtmai2enAtdn3A-1; Thu, 05 May 2022 11:19:16 -0400 X-MC-Unique: Y_1DYaG_Mtmai2enAtdn3A-1 Received: by mail-wr1-f72.google.com with SMTP id o13-20020adfa10d000000b0020c6fa5a77cso1577313wro.23 for ; Thu, 05 May 2022 08:19:16 -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=B9Ps2ABkfKYA90JWqdEc93XHx2W/OPs84CLEMF2jatc=; b=KwUGE4mLfpi0NCIlx+O83zuK4Dqx5UmUr5d6Jzk6t/xzyZ1+wHtOGzpMTtqQrz1fum hV3zK16TTdxujzbadSrHPY/Bk2FVvAWLFV0P0hLvXo14YfUEtc5tZdnFDsLU/+qQjjR2 ol/JzmIP87+0aD51GJWdTXuEc+z/0z6S01zBsUSuIiT5UZ/L7BB4ecQ9KN2LXuFyCpLs 40YHFIkTEZ5qnL5z/AI2kbNnI/mjhvxBBu9FEIxQ0F4VWTN3ZhyeTGBnLbHvFcvNZ2GG mk5iHojinwGMgt44o9zn78cgfGtNNVVIlXKK9hpjw3iPATfD0DfeTk4GGCFcR1VB3gys DWPQ== X-Gm-Message-State: AOAM533VqlMS3MsVXhi/H8IbQ1bgtDavCrGxosDn278tbtXqocZ1L0wS nd9egSzst9XiuaHhQA8Ww6pWZTqNEpIO8YzQOqoSs9X7H6JwXACthBxyFpuY8VvQGGLMVaiwTuS pYa/92ZRh843NkNvq0hM1WIwFF+qXI9jgxycxeYLNENvPxI9oK/CReEnc91Sugn1WCaoCGoHRI6 alqSaxNVRd X-Received: by 2002:a7b:c74f:0:b0:394:1ce3:cc42 with SMTP id w15-20020a7bc74f000000b003941ce3cc42mr5374624wmk.153.1651763954819; Thu, 05 May 2022 08:19:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyMPHDLg7erjUZAPisQSYmQBbwM08Wf3rDrCHXN5enrDN+5atmF9sYQWK6tYSEcQbEDtgRdYQ== X-Received: by 2002:a7b:c74f:0:b0:394:1ce3:cc42 with SMTP id w15-20020a7bc74f000000b003941ce3cc42mr5374579wmk.153.1651763954381; Thu, 05 May 2022 08:19:14 -0700 (PDT) Received: from localhost.localdomain ([2001:bb6:9634:7100:33b0:c1b1:8875:4adb]) by smtp.gmail.com with ESMTPSA id z6-20020a056000110600b0020ad05214d5sm1434819wrw.2.2022.05.05.08.19.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 08:19:14 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 5 May 2022 16:18:50 +0100 Message-Id: <20220505151851.2421-3-ecurtin@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220505151851.2421-1-ecurtin@redhat.com> References: <20220505151851.2421-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 v8 3/4] cam: sdl_sink: Add SDL sink with initial YUYV support 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 and SDL will handle the pixel buffer conversion to the display. 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 Reviewed-by: Kieran Bingham --- src/cam/camera_session.cpp | 8 ++ src/cam/main.cpp | 4 + src/cam/main.h | 1 + src/cam/meson.build | 12 +++ src/cam/sdl_sink.cpp | 192 +++++++++++++++++++++++++++++++++++ src/cam/sdl_sink.h | 48 +++++++++ src/cam/sdl_texture.cpp | 32 ++++++ src/cam/sdl_texture.h | 23 +++++ src/cam/sdl_texture_yuyv.cpp | 13 +++ src/cam/sdl_texture_yuyv.h | 10 ++ 10 files changed, 343 insertions(+) create mode 100644 src/cam/sdl_sink.cpp create mode 100644 src/cam/sdl_sink.h create mode 100644 src/cam/sdl_texture.cpp create mode 100644 src/cam/sdl_texture.h create mode 100644 src/cam/sdl_texture_yuyv.cpp create mode 100644 src/cam/sdl_texture_yuyv.h diff --git a/src/cam/camera_session.cpp b/src/cam/camera_session.cpp index efffafbf..71e6bd60 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" @@ -186,6 +189,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..fd3108b0 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -137,6 +137,10 @@ 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..3032730b 100644 --- a/src/cam/meson.build +++ b/src/cam/meson.build @@ -32,11 +32,23 @@ if libdrm.found() ]) endif +libsdl2 = dependency('SDL2', required : false) + +if libsdl2.found() + cam_cpp_args += ['-DHAVE_SDL'] + cam_sources += files([ + 'sdl_sink.cpp', + 'sdl_texture.cpp', + 'sdl_texture_yuyv.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..6fbeaf56 --- /dev/null +++ b/src/cam/sdl_sink.cpp @@ -0,0 +1,192 @@ +/* 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 "sdl_texture_yuyv.h" + +#include "event_loop.h" +#include "image.h" + +using namespace libcamera; + +SDLSink::SDLSink() + : sdlWindow_(nullptr), sdlRenderer_(nullptr), sdlRect_({}), + sdlInit_(false) +{ +} + +SDLSink::~SDLSink() +{ + stop(); +} + +int SDLSink::configure(const libcamera::CameraConfiguration &config) +{ + int ret = FrameSink::configure(config); + if (ret < 0) + return ret; + + if (config.size() > 1) { + std::cerr + << "SDL sink only supports one camera stream at present, streaming first camera stream" + << std::endl; + } else if (config.empty()) { + std::cerr << "Require at least one camera stream to process" + << std::endl; + return -EINVAL; + } + + const libcamera::StreamConfiguration &cfg = config.at(0); + sdlRect_.w = cfg.size.width; + sdlRect_.h = cfg.size.height; + + switch (cfg.pixelFormat) { + case libcamera::formats::YUYV: + sdlTexture_ = std::make_unique(sdlRect_); + break; + default: + std::cerr << "Unsupported pixel format " + << cfg.pixelFormat.toString() << 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; + } + + ret = sdlTexture_->create(sdlRenderer_); + if (ret) { + return ret; + } + + EventLoop::instance()->addTimerEvent( + 10ms, std::bind(&SDLSink::processSDLEvents, this)); + + return 0; +} + +int SDLSink::stop() +{ + if (sdlTexture_) { + sdlTexture_->destroy(); + 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; + + sdlTexture_->update(data); + } + + SDL_RenderClear(sdlRenderer_); + SDL_RenderCopy(sdlRenderer_, sdlTexture_->ptr_, 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..37dd9f7e --- /dev/null +++ b/src/cam/sdl_sink.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * sdl_sink.h - SDL Sink + */ + +#pragma once + +#include +#include +#include + +#include + +#include + +#include "frame_sink.h" +#include "sdl_texture.h" + +class Image; + +class SDLSink : public FrameSink +{ +public: + SDLSink(); + ~SDLSink(); + + int configure(const libcamera::CameraConfiguration &config) 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_; + + std::unique_ptr sdlTexture_; + + SDL_Window *sdlWindow_; + SDL_Renderer *sdlRenderer_; + SDL_Rect sdlRect_; + SDL_PixelFormatEnum pixelFormat_; + bool sdlInit_; +}; diff --git a/src/cam/sdl_texture.cpp b/src/cam/sdl_texture.cpp new file mode 100644 index 00000000..b39080c9 --- /dev/null +++ b/src/cam/sdl_texture.cpp @@ -0,0 +1,32 @@ +#include "sdl_texture.h" + +#include + +SDLTexture::SDLTexture(const SDL_Rect &sdlRect, SDL_PixelFormatEnum pixelFormat, + const int pitch) + : sdlRect_(sdlRect), pixelFormat_(pixelFormat), pitch_(pitch) +{ +} + +SDLTexture::~SDLTexture() +{ +} + +int SDLTexture::create(SDL_Renderer *sdlRenderer_) +{ + ptr_ = SDL_CreateTexture(sdlRenderer_, pixelFormat_, + SDL_TEXTUREACCESS_STREAMING, sdlRect_.w, + sdlRect_.h); + if (!ptr_) { + std::cerr << "Failed to create SDL texture: " << SDL_GetError() + << std::endl; + return -ENOMEM; + } + + return 0; +} + +void SDLTexture::destroy() +{ + SDL_DestroyTexture(ptr_); +} diff --git a/src/cam/sdl_texture.h b/src/cam/sdl_texture.h new file mode 100644 index 00000000..f9525f7f --- /dev/null +++ b/src/cam/sdl_texture.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +#include "image.h" + +class SDLTexture +{ +public: + SDLTexture(const SDL_Rect &sdlRect, SDL_PixelFormatEnum pixelFormat, + const int pitch); + virtual ~SDLTexture(); + int create(SDL_Renderer *sdlRenderer_); + void destroy(); + virtual void update(const libcamera::Span &data) = 0; + + SDL_Texture *ptr_; + +protected: + const SDL_Rect &sdlRect_; + SDL_PixelFormatEnum pixelFormat_; + const int pitch_; +}; diff --git a/src/cam/sdl_texture_yuyv.cpp b/src/cam/sdl_texture_yuyv.cpp new file mode 100644 index 00000000..cf519a5d --- /dev/null +++ b/src/cam/sdl_texture_yuyv.cpp @@ -0,0 +1,13 @@ +#include "sdl_texture_yuyv.h" + +using namespace libcamera; + +SDLTextureYUYV::SDLTextureYUYV(const SDL_Rect &sdlRect) + : SDLTexture(sdlRect, SDL_PIXELFORMAT_YUY2, 4 * ((sdlRect.w + 1) / 2)) +{ +} + +void SDLTextureYUYV::update(const Span &data) +{ + SDL_UpdateTexture(ptr_, &sdlRect_, data.data(), pitch_); +} diff --git a/src/cam/sdl_texture_yuyv.h b/src/cam/sdl_texture_yuyv.h new file mode 100644 index 00000000..ad70f14f --- /dev/null +++ b/src/cam/sdl_texture_yuyv.h @@ -0,0 +1,10 @@ +#pragma once + +#include "sdl_texture.h" + +class SDLTextureYUYV : public SDLTexture +{ +public: + SDLTextureYUYV(const SDL_Rect &sdlRect); + void update(const libcamera::Span &data) override; +}; From patchwork Thu May 5 15:18:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 15802 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 AE136C3256 for ; Thu, 5 May 2022 15:19:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5E6E060424; Thu, 5 May 2022 17:19:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1651763964; bh=D+P+IMGuJSzjIrxIpKf7YEsqyDJtPhyn1Yyzkm/vWvw=; 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=uI6ZtdDnROtaYycBS7RQI3gsQMz8mZMyDUvtQIV2pCO82tRqOX1dYDC+w7JPSO23o SA6vWp6IfQDBVGw9iQzUxQvu4haDzDZAZrEiZCFoZJXOOkwk/5ZwRonZLIdyWl7ikk tWm8dWGlGeQxntttYgxEk8VM7AgfaeeTpfelvxqH2sN6nBVx80N+Oxil/JkYUlWL7p aN5HTu65OkuahXAZnwjx+VQs7Q90NO9zTfVpTdY2vRXh5mTAAmADIeIv5fbcT+WbAi UB4wcTJwN5fW2sQ/MnI5EKr6hWtofQ8rMmMqxS6oTp6bz/0/cM9+kE/3z7+3yhdVTQ EqdYdF7HkDqhg== Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.133.74]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 14A2360424 for ; Thu, 5 May 2022 17:19:22 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="E1apAvv+"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651763962; 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=6eHuKn7G9MDNmHaezjG84YR7jDVwdOksX6s62gQFrUc=; b=E1apAvv+RVhtlk38dSUEPWxSISQEHH0hd86NyaEVm3lZnaWLhVQGfTeB12DxWbDHX7dGCS DKcKkn+x3MgUIJbWAxW05wuSb4PQWAFa3emx/fAd6/IRrcbck2pl3aYhOjqBeV4F19ywd2 9pZxijXArniZ5jTDWpJ/cv2HtYxHoKE= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-614-76qiXz_kNHasdTyfK3l-wA-1; Thu, 05 May 2022 11:19:18 -0400 X-MC-Unique: 76qiXz_kNHasdTyfK3l-wA-1 Received: by mail-wm1-f69.google.com with SMTP id v191-20020a1cacc8000000b0038ce818d2efso1848840wme.1 for ; Thu, 05 May 2022 08:19:18 -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=6eHuKn7G9MDNmHaezjG84YR7jDVwdOksX6s62gQFrUc=; b=CAXXc8GfKSBDnccYNG5fxzjIjr63Z9HfTOuk7KgdUIbcMUCQ5gSaojuVI2xZpeldnL pmYELBdO23BxD7Nc65soW8HPSyjWuY65zdH4ckIHsBqOiAWfaGfcYmALulzq9+VpkGgp wSInxXn0/+CPm94CQAl1MstksLtvmeR8QiXiXeAt5tAzHkWifWjeAYXaNlGnrrBRHNcQ TIEFRsA6zacOW8WZo+udpj0LvXnDu/jtzsmHoSrFFr7VjUQhbWBYMI3v/m5Y7s4T2R2b 1l7JK1dWFtWMwwcbnHtqVUADOneGiv5quN/HXP89vbxkXXcMVO90capynt/WTUWe4rWH qaXw== X-Gm-Message-State: AOAM531/pfX9jZN2tdX+XEsjsLPoyWUjyOoZ6E1NtQczd1K353VTl9Pq 240TQS1QGnYPgiFBekhFnYwHgX1rqsmPhUo1I9R1FjAFW3XX9c5T+JqU+x9HUi+Sj4AxzYEXEI6 mRNTA3CW1lVMwk6oTZK8tTFfQZBFm/oevyoy0fNe83y+CPej2sAY9/9zwYAxu3UEB9uWCQC+6vW HkKOOaDdCt X-Received: by 2002:a7b:cc12:0:b0:37c:1ae:100a with SMTP id f18-20020a7bcc12000000b0037c01ae100amr5412228wmh.54.1651763956991; Thu, 05 May 2022 08:19:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJytCO1a/TICuJe/OW6k4ypaM3ZW0pLIRGA+IgHqGImkASG+wn2mhs+u5+fO0zFeALfsQS/Hgw== X-Received: by 2002:a7b:cc12:0:b0:37c:1ae:100a with SMTP id f18-20020a7bcc12000000b0037c01ae100amr5412198wmh.54.1651763956697; Thu, 05 May 2022 08:19:16 -0700 (PDT) Received: from localhost.localdomain ([2001:bb6:9634:7100:33b0:c1b1:8875:4adb]) by smtp.gmail.com with ESMTPSA id z6-20020a056000110600b0020ad05214d5sm1434819wrw.2.2022.05.05.08.19.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 08:19:16 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 5 May 2022 16:18:51 +0100 Message-Id: <20220505151851.2421-4-ecurtin@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220505151851.2421-1-ecurtin@redhat.com> References: <20220505151851.2421-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 v8 4/4] cam: sdl_sink: Add MJPG support to 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" So we have at least two supported capturing pixel formats (although many possible output pixel formats thanks to SDL conversion). MJPG support only built in if SDL2_image is available, provides decompression. Signed-off-by: Eric Curtin Reviewed-by: Kieran Bingham --- src/cam/meson.build | 9 +++++++++ src/cam/sdl_sink.cpp | 9 +++++++++ src/cam/sdl_texture_mjpg.cpp | 18 ++++++++++++++++++ src/cam/sdl_texture_mjpg.h | 10 ++++++++++ 4 files changed, 46 insertions(+) create mode 100644 src/cam/sdl_texture_mjpg.cpp create mode 100644 src/cam/sdl_texture_mjpg.h diff --git a/src/cam/meson.build b/src/cam/meson.build index 3032730b..afc0ea9f 100644 --- a/src/cam/meson.build +++ b/src/cam/meson.build @@ -41,6 +41,14 @@ if libsdl2.found() 'sdl_texture.cpp', 'sdl_texture_yuyv.cpp' ]) + + libsdl2_image = dependency('SDL2_image', required : false) + if libsdl2.found() + cam_cpp_args += ['-DHAVE_SDL_IMAGE'] + cam_sources += files([ + 'sdl_texture_mjpg.cpp' + ]) + endif endif cam = executable('cam', cam_sources, @@ -49,6 +57,7 @@ cam = executable('cam', cam_sources, libcamera_public, libdrm, libsdl2, + libsdl2_image, libevent, ], cpp_args : cam_cpp_args, diff --git a/src/cam/sdl_sink.cpp b/src/cam/sdl_sink.cpp index 6fbeaf56..f15c01cb 100644 --- a/src/cam/sdl_sink.cpp +++ b/src/cam/sdl_sink.cpp @@ -19,6 +19,10 @@ #include "sdl_texture_yuyv.h" +#ifdef HAVE_SDL_IMAGE +#include "sdl_texture_mjpg.h" +#endif + #include "event_loop.h" #include "image.h" @@ -59,6 +63,11 @@ int SDLSink::configure(const libcamera::CameraConfiguration &config) case libcamera::formats::YUYV: sdlTexture_ = std::make_unique(sdlRect_); break; +#ifdef HAVE_SDL_IMAGE + case libcamera::formats::MJPEG: + sdlTexture_ = std::make_unique(sdlRect_); + break; +#endif default: std::cerr << "Unsupported pixel format " << cfg.pixelFormat.toString() << std::endl; diff --git a/src/cam/sdl_texture_mjpg.cpp b/src/cam/sdl_texture_mjpg.cpp new file mode 100644 index 00000000..636fdbea --- /dev/null +++ b/src/cam/sdl_texture_mjpg.cpp @@ -0,0 +1,18 @@ +#include "sdl_texture_mjpg.h" + +#include + +using namespace libcamera; + +SDLTextureMJPG::SDLTextureMJPG(const SDL_Rect &sdlRect) + : SDLTexture(sdlRect, SDL_PIXELFORMAT_RGB24, 0) +{ +} + +void SDLTextureMJPG::update(const Span &data) +{ + SDL_RWops *buffer_stream = SDL_RWFromMem(data.data(), data.size()); + SDL_Surface *frame = IMG_Load_RW(buffer_stream, 0); + SDL_UpdateTexture(ptr_, NULL, frame->pixels, frame->pitch); + SDL_FreeSurface(frame); +} diff --git a/src/cam/sdl_texture_mjpg.h b/src/cam/sdl_texture_mjpg.h new file mode 100644 index 00000000..fefaaeeb --- /dev/null +++ b/src/cam/sdl_texture_mjpg.h @@ -0,0 +1,10 @@ +#pragma once + +#include "sdl_texture.h" + +class SDLTextureMJPG : public SDLTexture +{ +public: + SDLTextureMJPG(const SDL_Rect &sdlRect); + void update(const libcamera::Span &data) override; +};