[{"id":12046,"web_url":"https://patchwork.libcamera.org/comment/12046/","msgid":"<20200819144038.GM6049@pendragon.ideasonboard.com>","date":"2020-08-19T14:40:38","subject":"Re: [libcamera-devel] [PATCH v3 3/5] android: camera_device: Make\n\tCameraDevice a shared object","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Umang,\n\nThank you for the patch.\n\nOn Mon, Aug 17, 2020 at 08:26:39PM +0000, Umang Jain wrote:\n> CameraDevice needs to be wrapper into the std::shared_ptr instead\n> of std::unique_ptr to enable refcounting. The refcounting will help\n> us to support hotplug and hot-unplug CameraHalManager operations\n> in the subsequent commit.\n> \n> Signed-off-by: Umang Jain <email@uajain.com>\n> ---\n>  src/android/camera_device.cpp      | 15 +++++++++++++++\n>  src/android/camera_device.h        |  7 +++++--\n>  src/android/camera_hal_manager.cpp |  4 ++--\n>  src/android/camera_hal_manager.h   |  2 +-\n>  4 files changed, 23 insertions(+), 5 deletions(-)\n> \n> diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\n> index d918350..a79bb69 100644\n> --- a/src/android/camera_device.cpp\n> +++ b/src/android/camera_device.cpp\n> @@ -233,6 +233,21 @@ CameraDevice::~CameraDevice()\n>  \t\tdelete it.second;\n>  }\n>  \n> +std::shared_ptr<CameraDevice> CameraDevice::create(unsigned int id,\n> +\t\t\t\t\t\t    const std::shared_ptr<Camera> &cam)\n> +{\n> +\tstruct Deleter : std::default_delete<CameraDevice> {\n> +\t\tvoid operator()(CameraDevice *camera)\n> +\t\t{\n> +\t\t\tdelete camera;\n> +\t\t}\n> +\t};\n\nThe reason we have a custom deleter for the Camera class is to allow\nmaking the destructor private. The private destructor can't be accessed\nfrom the default deleter, so we create a custom version inside this\nmember function of CameraDevice, which gives it access to the\ndestructor.\n\nAs this is internal to the HAL, making the destructor private is a bit\noverkill in my opinion. You could simplify this function to\n\n\tCameraDevice *camera = new CameraDevice(id, cam);\n\treturn std::shared_ptr<CameraDevice>(camera);\n\nif you made the destructor public. The constructor can stay private.\n\nWith this changed,\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\n> +\tCameraDevice *camera = new CameraDevice(id, cam);\n> +\n> +\treturn std::shared_ptr<CameraDevice>(camera, Deleter());\n> +}\n> +\n>  /*\n>   * Initialize the camera static information.\n>   * This method is called before the camera device is opened.\n> diff --git a/src/android/camera_device.h b/src/android/camera_device.h\n> index 7be9e11..b149043 100644\n> --- a/src/android/camera_device.h\n> +++ b/src/android/camera_device.h\n> @@ -47,8 +47,8 @@ struct CameraStream {\n>  class CameraDevice : protected libcamera::Loggable\n>  {\n>  public:\n> -\tCameraDevice(unsigned int id, const std::shared_ptr<libcamera::Camera> &camera);\n> -\t~CameraDevice();\n> +\tstatic std::shared_ptr<CameraDevice> create(unsigned int id,\n> +\t\t\t\t\t\t    const std::shared_ptr<libcamera::Camera> &cam);\n>  \n>  \tint initialize();\n>  \n> @@ -72,6 +72,9 @@ protected:\n>  \tstd::string logPrefix() const override;\n>  \n>  private:\n> +\tCameraDevice(unsigned int id, const std::shared_ptr<libcamera::Camera> &camera);\n> +\t~CameraDevice();\n> +\n>  \tstruct Camera3RequestDescriptor {\n>  \t\tCamera3RequestDescriptor(unsigned int frameNumber,\n>  \t\t\t\t\t unsigned int numBuffers);\n> diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp\n> index 3d6d2b4..3a744af 100644\n> --- a/src/android/camera_hal_manager.cpp\n> +++ b/src/android/camera_hal_manager.cpp\n> @@ -64,12 +64,12 @@ int CameraHalManager::init()\n>  \t */\n>  \tunsigned int index = 0;\n>  \tfor (auto &cam : cameraManager_->cameras()) {\n> -\t\tCameraDevice *camera = new CameraDevice(index, cam);\n> +\t\tstd::shared_ptr<CameraDevice> camera = CameraDevice::create(index, cam);\n>  \t\tret = camera->initialize();\n>  \t\tif (ret)\n>  \t\t\tcontinue;\n>  \n> -\t\tcameras_.emplace_back(camera);\n> +\t\tcameras_.emplace_back(std::move(camera));\n>  \t\t++index;\n>  \t}\n>  \n> diff --git a/src/android/camera_hal_manager.h b/src/android/camera_hal_manager.h\n> index a582f04..3e34d63 100644\n> --- a/src/android/camera_hal_manager.h\n> +++ b/src/android/camera_hal_manager.h\n> @@ -36,7 +36,7 @@ private:\n>  \tlibcamera::CameraManager *cameraManager_;\n>  \n>  \tconst camera_module_callbacks_t *callbacks_;\n> -\tstd::vector<std::unique_ptr<CameraDevice>> cameras_;\n> +\tstd::vector<std::shared_ptr<CameraDevice>> cameras_;\n>  };\n>  \n>  #endif /* __ANDROID_CAMERA_MANAGER_H__ */","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 08BBBBE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 19 Aug 2020 14:40:59 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7537861EEB;\n\tWed, 19 Aug 2020 16:40:58 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 714436038C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 19 Aug 2020 16:40:56 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id F055E29E;\n\tWed, 19 Aug 2020 16:40:55 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"C1pwB/q+\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1597848056;\n\tbh=8h7LB++yRy7ziCvGb+BR7SbB5Ta80OfKXXuziEW/BLg=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=C1pwB/q+vuu/0zoLBD6DZOZ4TPmuimNSLSFGvqzNuoOAzBTvYFJaM6VzaeTH9G9Ey\n\tW3qpFmfzs88UZrW0eBZRcH69sFVtV7kRZBCf5Gx2m3ICWJVxUhgjy7xlSknNMHgK5V\n\t1TL5lNyLt1XQkgZAYvnziRmIgp4AFNAOodU65AXM=","Date":"Wed, 19 Aug 2020 17:40:38 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Umang Jain <email@uajain.com>","Message-ID":"<20200819144038.GM6049@pendragon.ideasonboard.com>","References":"<20200817202629.4277-1-email@uajain.com>\n\t<20200817202629.4277-4-email@uajain.com>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20200817202629.4277-4-email@uajain.com>","Subject":"Re: [libcamera-devel] [PATCH v3 3/5] android: camera_device: Make\n\tCameraDevice a shared object","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":"libcamera-devel@lists.libcamera.org","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":12054,"web_url":"https://patchwork.libcamera.org/comment/12054/","msgid":"<20200820072847.GE39265@wyvern>","date":"2020-08-20T07:28:47","subject":"Re: [libcamera-devel] [PATCH v3 3/5] android: camera_device: Make\n\tCameraDevice a shared object","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Umang,\n\nThanks for your patch.\n\nOn 2020-08-17 20:26:39 +0000, Umang Jain wrote:\n> CameraDevice needs to be wrapper into the std::shared_ptr instead\n> of std::unique_ptr to enable refcounting. The refcounting will help\n> us to support hotplug and hot-unplug CameraHalManager operations\n> in the subsequent commit.\n> \n> Signed-off-by: Umang Jain <email@uajain.com>\n\nWith the comment about making the destructor public pointed out by \nLaurent fixed,\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\n> ---\n>  src/android/camera_device.cpp      | 15 +++++++++++++++\n>  src/android/camera_device.h        |  7 +++++--\n>  src/android/camera_hal_manager.cpp |  4 ++--\n>  src/android/camera_hal_manager.h   |  2 +-\n>  4 files changed, 23 insertions(+), 5 deletions(-)\n> \n> diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\n> index d918350..a79bb69 100644\n> --- a/src/android/camera_device.cpp\n> +++ b/src/android/camera_device.cpp\n> @@ -233,6 +233,21 @@ CameraDevice::~CameraDevice()\n>  \t\tdelete it.second;\n>  }\n>  \n> +std::shared_ptr<CameraDevice> CameraDevice::create(unsigned int id,\n> +\t\t\t\t\t\t    const std::shared_ptr<Camera> &cam)\n> +{\n> +\tstruct Deleter : std::default_delete<CameraDevice> {\n> +\t\tvoid operator()(CameraDevice *camera)\n> +\t\t{\n> +\t\t\tdelete camera;\n> +\t\t}\n> +\t};\n> +\n> +\tCameraDevice *camera = new CameraDevice(id, cam);\n> +\n> +\treturn std::shared_ptr<CameraDevice>(camera, Deleter());\n> +}\n> +\n>  /*\n>   * Initialize the camera static information.\n>   * This method is called before the camera device is opened.\n> diff --git a/src/android/camera_device.h b/src/android/camera_device.h\n> index 7be9e11..b149043 100644\n> --- a/src/android/camera_device.h\n> +++ b/src/android/camera_device.h\n> @@ -47,8 +47,8 @@ struct CameraStream {\n>  class CameraDevice : protected libcamera::Loggable\n>  {\n>  public:\n> -\tCameraDevice(unsigned int id, const std::shared_ptr<libcamera::Camera> &camera);\n> -\t~CameraDevice();\n> +\tstatic std::shared_ptr<CameraDevice> create(unsigned int id,\n> +\t\t\t\t\t\t    const std::shared_ptr<libcamera::Camera> &cam);\n>  \n>  \tint initialize();\n>  \n> @@ -72,6 +72,9 @@ protected:\n>  \tstd::string logPrefix() const override;\n>  \n>  private:\n> +\tCameraDevice(unsigned int id, const std::shared_ptr<libcamera::Camera> &camera);\n> +\t~CameraDevice();\n> +\n>  \tstruct Camera3RequestDescriptor {\n>  \t\tCamera3RequestDescriptor(unsigned int frameNumber,\n>  \t\t\t\t\t unsigned int numBuffers);\n> diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp\n> index 3d6d2b4..3a744af 100644\n> --- a/src/android/camera_hal_manager.cpp\n> +++ b/src/android/camera_hal_manager.cpp\n> @@ -64,12 +64,12 @@ int CameraHalManager::init()\n>  \t */\n>  \tunsigned int index = 0;\n>  \tfor (auto &cam : cameraManager_->cameras()) {\n> -\t\tCameraDevice *camera = new CameraDevice(index, cam);\n> +\t\tstd::shared_ptr<CameraDevice> camera = CameraDevice::create(index, cam);\n>  \t\tret = camera->initialize();\n>  \t\tif (ret)\n>  \t\t\tcontinue;\n>  \n> -\t\tcameras_.emplace_back(camera);\n> +\t\tcameras_.emplace_back(std::move(camera));\n>  \t\t++index;\n>  \t}\n>  \n> diff --git a/src/android/camera_hal_manager.h b/src/android/camera_hal_manager.h\n> index a582f04..3e34d63 100644\n> --- a/src/android/camera_hal_manager.h\n> +++ b/src/android/camera_hal_manager.h\n> @@ -36,7 +36,7 @@ private:\n>  \tlibcamera::CameraManager *cameraManager_;\n>  \n>  \tconst camera_module_callbacks_t *callbacks_;\n> -\tstd::vector<std::unique_ptr<CameraDevice>> cameras_;\n> +\tstd::vector<std::shared_ptr<CameraDevice>> cameras_;\n>  };\n>  \n>  #endif /* __ANDROID_CAMERA_MANAGER_H__ */\n> -- \n> 2.26.2\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 84618BD87C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 20 Aug 2020 07:28:52 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1AB3860380;\n\tThu, 20 Aug 2020 09:28:52 +0200 (CEST)","from mail-lj1-x243.google.com (mail-lj1-x243.google.com\n\t[IPv6:2a00:1450:4864:20::243])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5A43160380\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 20 Aug 2020 09:28:50 +0200 (CEST)","by mail-lj1-x243.google.com with SMTP id t6so970723ljk.9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 20 Aug 2020 00:28:50 -0700 (PDT)","from localhost (host-78-79-224-122.mobileonline.telia.com.\n\t[78.79.224.122]) by smtp.gmail.com with ESMTPSA id\n\tq14sm304576lfp.93.2020.08.20.00.28.48\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 20 Aug 2020 00:28:48 -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=\"f3FDWeRx\"; 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=SOiUR9ls+0V/zcmNe7j5Q9ZSad9GM6bg5FxrFuNhUIY=;\n\tb=f3FDWeRxP9W1a4+i0EMnlNB498u1ylM13goZ4ggfYwrYjTeJU7+TBrhegbKn1/uptf\n\tunwXRDZ6gBdD44ZAZ8gGc/GfapZudbA8hcBcYoiIrH4lQ7+aG5+syyykZJH3YDUoF2uE\n\t8QRTSmHQtT7oxilro9o4bESiw8B92LduNIHlG/C/dhBJNoQcifFqaE2Ti0ikQlAMGWIf\n\tdHQMwOM7WM8PhRd98Pu0kv4CGpoFoRD8At6WTs/C2UbYKBSt5HA2HdO21EF4ernlZJRG\n\tu/WLL4g5iKjVHhFcwN+woR0DaizKY63MWZnoBYmfB0mPTqJOdyXkDtAlTjVlnZusb0YO\n\tgWiQ==","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=SOiUR9ls+0V/zcmNe7j5Q9ZSad9GM6bg5FxrFuNhUIY=;\n\tb=Ha4Rfwt+Vwj0sbcCvknLL2o2HqglceGz0UZ/PtftaJ44ToQSlR0krjaxnGNKwpLVSl\n\t2JmIewHQfOPMofquegDgk3UjTH6kItb3vr8PdkmJaxinaNwKwfS9R5aFV2o8rY09CxsA\n\tUgN1SZ+2yCcBhWIDGIIM7aDWLT3wpnkcLxwl6J3DhwXV5zg5W4+Bf5G7zZenRkLD6Hco\n\tX7l4qGzDUrqic696NvCcTVKQH5Z2v5hC9nU947Torn8SgrilSC+URU8IwHIYcU/WO+Np\n\tOcspCpH6Y2SOkM5xubz+DmJ4c199xFtKGcEi8X3jvQiDFh5uWvH4mPb/ElmDf+D7FTn3\n\tk7Qw==","X-Gm-Message-State":"AOAM532YJITbmpIFPhCI1Lu8GWzHRA0Lp3ZKwUDIAaP1gKMba/90l1zy\n\tWEmYPdS6x50Wg2iWt6NkZWbgzvqaKFhpbtmSWlQ=","X-Google-Smtp-Source":"ABdhPJxePSxK82aa/8t8ze3tHfi8P+DxPiMbw4/8aOs8C1BmbuHDWKgs1AYv3dY1jZZseJcWusOEcw==","X-Received":"by 2002:a2e:9196:: with SMTP id f22mr971512ljg.435.1597908529641;\n\tThu, 20 Aug 2020 00:28:49 -0700 (PDT)","Date":"Thu, 20 Aug 2020 09:28:47 +0200","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Umang Jain <email@uajain.com>","Message-ID":"<20200820072847.GE39265@wyvern>","References":"<20200817202629.4277-1-email@uajain.com>\n\t<20200817202629.4277-4-email@uajain.com>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20200817202629.4277-4-email@uajain.com>","Subject":"Re: [libcamera-devel] [PATCH v3 3/5] android: camera_device: Make\n\tCameraDevice a shared object","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":"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>"}}]