{"id":2268,"url":"https://patchwork.libcamera.org/api/patches/2268/?format=json","web_url":"https://patchwork.libcamera.org/patch/2268/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20191028022525.796995-9-niklas.soderlund@ragnatech.se>","date":"2019-10-28T02:25:21","name":"[libcamera-devel,RFC,08/12] libcamera: buffer: Add a buffer allocator","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"52aa8b56de7acb73722dcc6c5158b7b1b786bf8e","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2268/mbox/","series":[{"id":561,"url":"https://patchwork.libcamera.org/api/series/561/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=561","date":"2019-10-28T02:25:13","name":"libcamera: Rework buffer API","version":1,"mbox":"https://patchwork.libcamera.org/series/561/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2268/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2268/checks/","tags":{},"headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net\n\t[195.74.38.227])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 298816017C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 28 Oct 2019 03:25:55 +0100 (CET)","from localhost.localdomain (unknown [93.2.121.143])\n\tby bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA\n\tid 42bdd136-f92a-11e9-903a-005056917f90;\n\tMon, 28 Oct 2019 03:25:51 +0100 (CET)"],"X-Halon-ID":"42bdd136-f92a-11e9-903a-005056917f90","Authorized-sender":"niklas@soderlund.pp.se","From":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 28 Oct 2019 03:25:21 +0100","Message-Id":"<20191028022525.796995-9-niklas.soderlund@ragnatech.se>","X-Mailer":"git-send-email 2.23.0","In-Reply-To":"<20191028022525.796995-1-niklas.soderlund@ragnatech.se>","References":"<20191028022525.796995-1-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC 08/12] libcamera: buffer: Add a buffer\n\tallocator","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>","X-List-Received-Date":"Mon, 28 Oct 2019 02:25:55 -0000"},"content":">From an applications point of view buffers shall not be allocated\ndirectly by a camera or stream. Instead they shall be allocated\nexternally and used by a camera.\n\nTo model this behavior add a buffer allocator that is exposed to\napplications. How the allocator creates buffers is pipeline specific and\nhandled by sub-classing the stream class.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n include/libcamera/buffer.h | 17 ++++++++++++++\n include/libcamera/stream.h |  1 +\n src/libcamera/buffer.cpp   | 48 ++++++++++++++++++++++++++++++++++++++\n 3 files changed, 66 insertions(+)","diff":"diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h\nindex c626f669040b3c04..adb642ad5da072d2 100644\n--- a/include/libcamera/buffer.h\n+++ b/include/libcamera/buffer.h\n@@ -8,11 +8,14 @@\n #define __LIBCAMERA_BUFFER_H__\n \n #include <array>\n+#include <map>\n+#include <memory>\n #include <stdint.h>\n #include <vector>\n \n namespace libcamera {\n \n+class Camera;\n class Request;\n class Stream;\n \n@@ -139,6 +142,20 @@ private:\n \tstd::vector<PlaneInfo> planes_;\n };\n \n+class BufferAllocator\n+{\n+public:\n+\tBufferAllocator(std::shared_ptr<Camera> camera);\n+\t~BufferAllocator();\n+\n+\tint allocate(Stream *stream);\n+\tstd::vector<Buffer *> get(Stream *stream);\n+\n+private:\n+\tstd::shared_ptr<Camera> camera_;\n+\tstd::map<Stream *, std::vector<Buffer *>> allocated_;\n+};\n+\n } /* namespace libcamera */\n \n #endif /* __LIBCAMERA_BUFFER_H__ */\ndiff --git a/include/libcamera/stream.h b/include/libcamera/stream.h\nindex dac4831cfa1a9b1d..b051341511a7ab7c 100644\n--- a/include/libcamera/stream.h\n+++ b/include/libcamera/stream.h\n@@ -84,6 +84,7 @@ public:\n \tMemoryType memoryType() const { return memoryType_; }\n \n protected:\n+\tfriend class BufferAllocator;\n \tfriend class Camera;\n \n \tvirtual int allocateBuffers(std::vector<Buffer *> *buffers) { return -EINVAL; }\ndiff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp\nindex 10b16a862393b536..fce1ce5e49cbbf42 100644\n--- a/src/libcamera/buffer.cpp\n+++ b/src/libcamera/buffer.cpp\n@@ -12,6 +12,9 @@\n #include <sys/mman.h>\n #include <unistd.h>\n \n+#include <libcamera/camera.h>\n+#include <libcamera/stream.h>\n+\n #include \"log.h\"\n \n /**\n@@ -393,4 +396,49 @@ void Buffer::cancel()\n  * The intended callers are Request::prepare() and Request::completeBuffer().\n  */\n \n+BufferAllocator::BufferAllocator(std::shared_ptr<Camera> camera)\n+\t: camera_(camera)\n+{\n+}\n+\n+BufferAllocator::~BufferAllocator()\n+{\n+\tfor (auto &it : allocated_) {\n+\t\tStream *stream = it.first;\n+\t\tstd::vector<Buffer *> &buffers = it.second;\n+\n+\t\tfor (Buffer *buffer : buffers)\n+\t\t\tdelete buffer;\n+\n+\t\tbuffers.clear();\n+\n+\t\tstream->allocateBuffers(nullptr);\n+\t}\n+}\n+\n+int BufferAllocator::allocate(Stream *stream)\n+{\n+\tbool found = false;\n+\n+\tfor (const Stream *s : camera_->streams()) {\n+\t\tif (stream == s) {\n+\t\t\tfound = true;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (!found)\n+\t\treturn -EINVAL;\n+\n+\treturn stream->allocateBuffers(&allocated_[stream]);\n+}\n+\n+std::vector<Buffer *> BufferAllocator::get(Stream *stream)\n+{\n+\tif (allocated_.find(stream) == allocated_.end())\n+\t\treturn {};\n+\n+\treturn allocated_[stream];\n+}\n+\n } /* namespace libcamera */\n","prefixes":["libcamera-devel","RFC","08/12"]}