From patchwork Wed Oct 9 07:41:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21554 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 77108C32E7 for ; Wed, 9 Oct 2024 07:46:56 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 52EAE6536F; Wed, 9 Oct 2024 09:46:55 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="GXEC7gYw"; dkim-atps=neutral Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id F17D56536F for ; Wed, 9 Oct 2024 09:46:51 +0200 (CEST) Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-2e199b1d157so4773870a91.2 for ; Wed, 09 Oct 2024 00:46:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1728460010; x=1729064810; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KTpmD1L4y0v1O2KJieH/lIMmNmwARc2tg5l3xqlLN5k=; b=GXEC7gYwNyE0EdxSlxG8V+hoLMuJAIz3f0ggY8IY770BnZ+8u1WCWu93oQIVzvMvyG eGd9K7xdD5YKWw85hqI/8ibvolXm2uHpVDKcGBU4yNu/1NA/l4rLENRBCbCxDryRhTe6 VmLKEqksoAuFAjdhGcWrRKewjmvK2EOWkV8Rc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728460010; x=1729064810; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KTpmD1L4y0v1O2KJieH/lIMmNmwARc2tg5l3xqlLN5k=; b=ss7mk/Edg2Z6Sr2gtfEQKt/5xZ6JyMjcnHKQvgd0NOHkUcvH0wZja0XI8HwNS0LTSu 2oSOxt4ZsEsZ6/oG17TVbNdQgbks1Hy8l7GgjV+vqzQg4t0J4ibqUYGymTsWYS/0zEqN 2xqr8lYY0mooK1He+2ckq7r1SSFvK5TCz601m7FZ1molQdy78/X/T6//L1y+rr3hXrlK o4JOFqvk7kn0ucXK0OeYPeX9k1h8sXCl6YR+Dq0HHoNG+hxEt8pEcxFvGgHNrGqIgBaj 90bv+aQQKwvQlo5gkiwamptGfs1/q4z/oRfdrAfzTbv213R7LMzPpdn88O96DBDIQ4z8 5puw== X-Gm-Message-State: AOJu0YwzH/cF6b4zDF5rBUWzxaSlsLQsHBrH2/bzldQTr+zKj7NNJLS2 6raa2Uw3P7OQlNfUCwGczoaqaIlss3LFfouBTT5UP/zLjmYt3RnsPYUvhjIqRKkL9x5DMCAqCtI = X-Google-Smtp-Source: AGHT+IEJ0Tjw06HXoNrNo4fgOVUUvOOBohGqPp62OkvEdQ4WaOAkT8/2erOwp3jlgvr66Eb+rsqIKg== X-Received: by 2002:a17:90b:fc8:b0:2d8:f7c6:e1dd with SMTP id 98e67ed59e1d1-2e2a25a04e1mr1904759a91.37.1728460010206; Wed, 09 Oct 2024 00:46:50 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (199.211.81.34.bc.googleusercontent.com. [34.81.211.199]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e2a57077c2sm944414a91.17.2024.10.09.00.46.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Oct 2024 00:46:49 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Han-Lin Chen Subject: [PATCH 2/5] libcamera: Add template for pool implementation Date: Wed, 9 Oct 2024 07:41:20 +0000 Message-ID: <20241009074642.2965791-3-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog In-Reply-To: <20241009074642.2965791-1-chenghaoyang@chromium.org> References: <20241009074642.2965791-1-chenghaoyang@chromium.org> MIME-Version: 1.0 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a template helper class as a Pool, which owns a list of objects that allows requesting and recycling objects. Mostly used as buffer pools. Signed-off-by: Han-Lin Chen Co-developed-by: Harvey Yang Signed-off-by: Harvey Yang --- include/libcamera/internal/meson.build | 1 + include/libcamera/internal/pool.h | 119 +++++++++++++++++++++++++ src/libcamera/meson.build | 1 + src/libcamera/pool.cpp | 70 +++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 include/libcamera/internal/pool.h create mode 100644 src/libcamera/pool.cpp diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index 4c1228b95..b78e51d0b 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -32,6 +32,7 @@ libcamera_internal_headers = files([ 'media_device.h', 'media_object.h', 'pipeline_handler.h', + 'pool.h', 'process.h', 'pub_key.h', 'request.h', diff --git a/include/libcamera/internal/pool.h b/include/libcamera/internal/pool.h new file mode 100644 index 000000000..578c6dd12 --- /dev/null +++ b/include/libcamera/internal/pool.h @@ -0,0 +1,119 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Google Inc. + * + * pool.h - Template class for generic pool + * + * Common usage: + * + * Unique tokens: + * Pool>; + * + * Auto closed Fds: + * Pool; + * + * Auto destructed FrameBuffers: + * Pool>; + */ + +#pragma once + +#include +#include +#include +#include +#include + +#include +#include + +namespace libcamera { + +LOG_DEFINE_CATEGORY(Pool) + +template +class Pool : public Loggable +{ +public: + Pool() = default; + ~Pool() = default; + + /* Loggable */ + std::string logPrefix() const override { return "Pool"; } + + void setData(std::vector &pool) + { + pool_.swap(pool); + free_.clear(); + inUse_.clear(); + + for (auto &fd : pool_) { + free_.emplace_back(fd.get()); + } + } + + void release() + { + pool_.clear(); + free_.clear(); + inUse_.clear(); + } + + std::vector &content() + { + return pool_; + } + + size_t size() + { + return pool_.size(); + } + + T get() + { + std::scoped_lock lock(mutex_); + + if (free_.empty()) + LOG(Pool, Fatal) << "not enough data in the Pool"; + + T fd = free_.front(); + free_.pop_front(); + inUse_.emplace(fd); + + return fd; + } + + void put(T data) + { + std::scoped_lock lock(mutex_); + + auto iter = std::find(inUse_.begin(), inUse_.end(), data); + if (iter == inUse_.end()) + LOG(Pool, Fatal) << "return data not belonging to the pool"; + + inUse_.erase(iter); + free_.emplace_front(data); + } + +private: + LIBCAMERA_DISABLE_COPY_AND_MOVE(Pool) + + std::vector pool_; + std::deque free_; + std::set inUse_; + Mutex mutex_; +}; + +template +class BasicContainer +{ +public: + BasicContainer(T &value) + : value_(value) {} + T get() { return value_; } + +private: + T value_; +}; + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 6c3ef5377..f4403687a 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -43,6 +43,7 @@ libcamera_internal_sources = files([ 'media_device.cpp', 'media_object.cpp', 'pipeline_handler.cpp', + 'pool.cpp', 'process.cpp', 'pub_key.cpp', 'shared_mem_object.cpp', diff --git a/src/libcamera/pool.cpp b/src/libcamera/pool.cpp new file mode 100644 index 000000000..122b025d1 --- /dev/null +++ b/src/libcamera/pool.cpp @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Google Inc. + * + * pool.cpp - Template class for generic pool + */ + +/** + * \class libcamera::Pool + * \brief Pool that owns a list of objects + * + * A Pool that owns a list of objects, and allows users to request objects and + * recycle them. It's mostly used as a buffer pool, holding a list of buffers + * as std::unique_ptr. + */ + +/** + * \fn Pool::setData(std::vector &pool) + * \brief Set and claim ownership of objects. Swap the previously owned objects + * not empty. + * \param[inout] pool The objects to be set and the swapped out ones + */ + +/** + * \fn Pool::release() + * \brief Release the ownership of objects and reset states + */ + +/** + * \fn Pool::content() + * \return The reference of objects + */ + +/** + * \fn Pool::size() + * \brief The size of objects + */ + +/** + * \fn Pool::get() + * \brief Get one object. Fatal if there's none available + * + * \return An available object + */ + +/** + * \fn Pool::put(T data) + * \brief Recycle one object that was retrieved by get() + * \param[in] data The object to be recycled + */ + +/** + * \class libcamera::BasicContainer + * \brief A container for basic types + * + * When using Pool with a basic type (e.g. `int`), it'd be easier to get an + * object by value instead of by pointer. BasicContainer makes it possible as a + * helper class. + */ + +/** + * \fn BasicContainer::BasicContainer(T &value) + * \brief Create a BasicContainer with a value + * \param[in] value The value to be contained + */ + +/** + * \fn BasicContainer::get() + * \return Return the object by value + */