[{"id":12595,"web_url":"https://patchwork.libcamera.org/comment/12595/","msgid":"<20200920004407.GC2692107@oden.dyn.berto.se>","date":"2020-09-20T00:44:07","subject":"Re: [libcamera-devel] [PATCH v2 6/8] android: camera_device: Add\n\tmethods to get and return buffers","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Jacopo,\n\nThanks for your work.\n\nOn 2020-09-18 15:50:39 +0200, Jacopo Mondi wrote:\n> Add two methods to the CameraDevice class to retrieve and return\n> frame buffers associated to a stream from the memory pool reserved\n> in libcamera.\n> \n> Protect accessing the vector of FrameBufer pointers with a\n> per-pool mutex in the get and return buffer methods.\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\n> ---\n>  src/android/camera_device.cpp | 34 +++++++++++++++++++++++++++++++++-\n>  src/android/camera_device.h   | 11 +++++++++--\n>  2 files changed, 42 insertions(+), 3 deletions(-)\n> \n> diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\n> index f96ea7321a67..6ed56ff57dab 100644\n> --- a/src/android/camera_device.cpp\n> +++ b/src/android/camera_device.cpp\n> @@ -8,6 +8,7 @@\n>  #include \"camera_device.h\"\n>  #include \"camera_ops.h\"\n>  \n> +#include <mutex>\n>  #include <sys/mman.h>\n>  #include <tuple>\n>  #include <vector>\n> @@ -1400,11 +1401,42 @@ int CameraDevice::allocateBuffersPool(Stream *stream)\n>  \t * the HAL.\n>  \t */\n>  \tfor (const auto &frameBuffer : allocator_.buffers(stream))\n> -\t\tbufferPool_[stream].push_back(frameBuffer.get());\n> +\t\tbufferPool_[stream].buffers.push_back(frameBuffer.get());\n>  \n>  \treturn 0;\n>  }\n>  \n> +libcamera::FrameBuffer *CameraDevice::getBuffer(libcamera::Stream *stream)\n> +{\n> +\tif (bufferPool_.find(stream) == bufferPool_.end())\n> +\t\treturn nullptr;\n> +\n> +\tBufferPool *pool = &bufferPool_[stream];\n> +\tstd::lock_guard<std::mutex> locker(pool->mutex);\n> +\n> +\tif (pool->buffers.empty()) {\n> +\t\tLOG(HAL, Error) << \"Buffer underrun\";\n> +\t\treturn nullptr;\n> +\t}\n> +\n> +\tFrameBuffer *buffer = pool->buffers.front();\n> +\tpool->buffers.erase(pool->buffers.begin());\n> +\n> +\treturn buffer;\n> +}\n> +\n> +void CameraDevice::returnBuffer(libcamera::Stream *stream,\n> +\t\t\t\tlibcamera::FrameBuffer *buffer)\n> +{\n> +\tif (bufferPool_.find(stream) == bufferPool_.end())\n> +\t\treturn;\n> +\n> +\tBufferPool *pool = &bufferPool_[stream];\n> +\tstd::lock_guard<std::mutex> locker(pool->mutex);\n> +\n> +\tpool->buffers.push_back(buffer);\n> +}\n> +\n>  int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Request)\n>  {\n>  \tif (!camera3Request->num_output_buffers) {\n> diff --git a/src/android/camera_device.h b/src/android/camera_device.h\n> index 4cef34c01a49..5addffdc070a 100644\n> --- a/src/android/camera_device.h\n> +++ b/src/android/camera_device.h\n> @@ -9,6 +9,7 @@\n>  \n>  #include <map>\n>  #include <memory>\n> +#include <mutex>\n>  #include <tuple>\n>  #include <vector>\n>  \n> @@ -166,8 +167,11 @@ protected:\n>  \tstd::string logPrefix() const override;\n>  \n>  private:\n> -\tusing FrameBufferPool = std::map<libcamera::Stream *,\n> -\t\t\t\t\t std::vector<libcamera::FrameBuffer *>>;\n> +\tstruct BufferPool {\n> +\t\tstd::mutex mutex;\n> +\t\tstd::vector<libcamera::FrameBuffer *> buffers;\n> +\t};\n> +\tusing FrameBufferPool = std::map<libcamera::Stream *, BufferPool>;\n>  \n>  \tCameraDevice(unsigned int id, const std::shared_ptr<libcamera::Camera> &camera);\n>  \n> @@ -198,6 +202,9 @@ private:\n>  \tstd::tuple<uint32_t, uint32_t> calculateStaticMetadataSize();\n>  \tlibcamera::FrameBuffer *createFrameBuffer(const buffer_handle_t camera3buffer);\n>  \tint allocateBuffersPool(libcamera::Stream *stream);\n> +\tlibcamera::FrameBuffer *getBuffer(libcamera::Stream *stream);\n> +\tvoid returnBuffer(libcamera::Stream *stream,\n> +\t\t\t  libcamera::FrameBuffer *buffer);\n>  \n>  \tvoid notifyShutter(uint32_t frameNumber, uint64_t timestamp);\n>  \tvoid notifyError(uint32_t frameNumber, camera3_stream_t *stream);\n> -- \n> 2.28.0\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","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 BDC8AC3B5B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 20 Sep 2020 00:44:10 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5509E62FBC;\n\tSun, 20 Sep 2020 02:44:10 +0200 (CEST)","from mail-lf1-x143.google.com (mail-lf1-x143.google.com\n\t[IPv6:2a00:1450:4864:20::143])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A836C62FAB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 20 Sep 2020 02:44:08 +0200 (CEST)","by mail-lf1-x143.google.com with SMTP id z17so10134621lfi.12\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 19 Sep 2020 17:44:08 -0700 (PDT)","from localhost (h-209-203.A463.priv.bahnhof.se. [155.4.209.203])\n\tby smtp.gmail.com with ESMTPSA id\n\tg63sm1523777lfd.28.2020.09.19.17.44.07\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tSat, 19 Sep 2020 17:44:07 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=ragnatech-se.20150623.gappssmtp.com\n\theader.i=@ragnatech-se.20150623.gappssmtp.com\n\theader.b=\"a955qFJt\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to;\n\tbh=Tc0/4AmlPyOKSAeAgnqiXwZPjv+CHoUF2OWGHYeWuBo=;\n\tb=a955qFJtyTtD/p3fiP/CHlr2/g+ke70FdKOG+Q7tTJdKf+DCuQeNFhSoWEpxHzF3Xu\n\t/HGT5jDgmTaSmmsK3XCHRLBYlmMTqUGTKesk5VImznlYonwnIXHSHQ1SumbkIxHpOMNZ\n\tNALdOB2cY6uiMPr93GePKqZEJ8ItsPdXP0ih8M6nZoaaw9C80h3sA0dGqIsS2318GNMV\n\t0MGxMl1PWZfJiTqHUqt4S8FeF9CeOlOzdct7oy9Odo3/1TEwCxKhHYZmVMNHzdP78mGI\n\t4B8hTWc2m0oq/88KUbRUgLmRZ1c7NGm16hGyqzCwkHo5W8VAhPfFnqQtw74KRCEC6wzf\n\tdSVg==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to;\n\tbh=Tc0/4AmlPyOKSAeAgnqiXwZPjv+CHoUF2OWGHYeWuBo=;\n\tb=Lqx0gLY0Wxg19a/8snbVr0lGK1bqKqR6NAx898VSlg015MdfSNJqQcMiI8Up6uz/oL\n\tKhz1wXE8S8ei/4qNzUzFNXRr6rhWdDFECaQxrjmA8lcDqOvQgjEpTiiwiQWwz+xMlAH4\n\tg8cEtx15TwAM4O7BWVT+vB3aYWlO1km1hDMv+oZ2lW0qQnZDRmm+suWSo+vZiHUTTGiO\n\txcXlLp3SVE+M0BVJV4mUloB+QhvFr0h35Gdfqpyl7LQHxlo55jkj+Y9cadREO9jxcW8D\n\tR5tWj0a74nOS/3SxJmRfJx9Mll7/agCQnB9QnrJ2Al231D50uIGVtX3x8IwdCbdDaNeq\n\t86Rw==","X-Gm-Message-State":"AOAM5309UwifhEsz8ObR+u7CjGwmbksps1PRLTp3fGMxY6s/vKA4tFOx\n\t6s0f4MJoHGf2gFhxJA/h7yDO9A==","X-Google-Smtp-Source":"ABdhPJzB/pbTaUmbXrdkt00HF1cxpa3JKE7dBseTBP4tKN2KdryCfJNIVucThNVJNcJJ8n5tBWZ7oA==","X-Received":"by 2002:a19:64b:: with SMTP id 72mr12985988lfg.106.1600562648044;\n\tSat, 19 Sep 2020 17:44:08 -0700 (PDT)","Date":"Sun, 20 Sep 2020 02:44:07 +0200","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Jacopo Mondi <jacopo@jmondi.org>","Message-ID":"<20200920004407.GC2692107@oden.dyn.berto.se>","References":"<20200918135041.91492-1-jacopo@jmondi.org>\n\t<20200918135041.91492-7-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20200918135041.91492-7-jacopo@jmondi.org>","Subject":"Re: [libcamera-devel] [PATCH v2 6/8] android: camera_device: Add\n\tmethods to get and return buffers","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>","Cc":"hanlinchen@chromium.org, libcamera-devel@lists.libcamera.org","Content-Type":"text/plain; charset=\"iso-8859-1\"","Content-Transfer-Encoding":"quoted-printable","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]