[{"id":4995,"web_url":"https://patchwork.libcamera.org/comment/4995/","msgid":"<20200604004807.GH27695@pendragon.ideasonboard.com>","date":"2020-06-04T00:48:07","subject":"Re: [libcamera-devel] [PATCH 3/8] android: hal_manager: Do not\n\thardcode properties","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Tue, May 26, 2020 at 04:22:32PM +0200, Jacopo Mondi wrote:\n> The CameraHalManager::getCameraInfo() method hardcodes the camera facing\n> side and orientation (which corresponds, confusingly, to libcamera's\n> location and rotation properties).\n> \n> Instead of hard-coding the values based on the camera id, cache the\n> libcamera Camera properties in a new initialize() method, and use them\n> both to report camera info and to populate the static metadata buffer.\n\nTechnically speaking, you're not caching the libcamera properties, but\nthe facing and orientation values.\n\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n> ---\n>  src/android/camera_device.cpp      | 80 ++++++++++++++++++++----------\n>  src/android/camera_device.h        |  8 +++\n>  src/android/camera_hal_manager.cpp | 10 ++--\n>  3 files changed, 68 insertions(+), 30 deletions(-)\n> \n> diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\n> index ad277cb059ca..69b25ed2f11f 100644\n> --- a/src/android/camera_device.cpp\n> +++ b/src/android/camera_device.cpp\n> @@ -53,7 +53,8 @@ CameraDevice::Camera3RequestDescriptor::~Camera3RequestDescriptor()\n>   */\n>  \n>  CameraDevice::CameraDevice(unsigned int id, const std::shared_ptr<Camera> &camera)\n> -\t: running_(false), camera_(camera), staticMetadata_(nullptr)\n> +\t: running_(false), camera_(camera), staticMetadata_(nullptr),\n> +\t  facing_(CAMERA_FACING_FRONT), orientation_(0)\n>  {\n>  \tcamera_->requestCompleted.connect(this, &CameraDevice::requestComplete);\n>  }\n> @@ -67,6 +68,45 @@ CameraDevice::~CameraDevice()\n>  \t\tdelete it.second;\n>  }\n>  \n> +/*\n> + * Initialize the camera static information.\n> + * This method is called before the camera device is open.\n\ns/open/opened/ ?\n\n> + */\n> +int CameraDevice::initialize()\n> +{\n> +\t/* Initialize orientation and facing side of the camera. */\n> +\tconst ControlList &properties = camera_->properties();\n> +\n> +\tif (properties.contains(properties::Location)) {\n> +\t\tint32_t location = properties.get(properties::Location);\n> +\t\tswitch (location) {\n> +\t\tcase properties::CameraLocationFront:\n> +\t\t\tfacing_ = CAMERA_FACING_FRONT;\n> +\t\t\tbreak;\n> +\t\tcase properties::CameraLocationBack:\n> +\t\t\tfacing_ = CAMERA_FACING_BACK;\n> +\t\t\tbreak;\n> +\t\tcase properties::CameraLocationExternal:\n> +\t\t\tfacing_ = CAMERA_FACING_EXTERNAL;\n> +\t\t\tbreak;\n> +\t\t}\n> +\t}\n> +\n> +\t/*\n> +\t * The Android orientation metadata and libcamera rotation property are\n> +\t * defined differently but have identical numerical values for Android\n> +\t * devices such as phones and tablets.\n> +\t */\n> +\tif (properties.contains(properties::Rotation))\n> +\t\torientation_ = properties.get(properties::Rotation);\n> +\n> +\treturn 0;\n> +}\n> +\n> +/*\n> + * Open a camera device. The static information on the camera shall have been\n> + * initialized with a call to CameraDevice::init();\n\ns/init();/initialize()./\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> + */\n>  int CameraDevice::open(const hw_module_t *hardwareModule)\n>  {\n>  \tint ret = camera_->acquire();\n> @@ -112,8 +152,6 @@ const camera_metadata_t *CameraDevice::getStaticMetadata()\n>  \tif (staticMetadata_)\n>  \t\treturn staticMetadata_->get();\n>  \n> -\tconst ControlList &properties = camera_->properties();\n> -\n>  \t/*\n>  \t * The here reported metadata are enough to implement a basic capture\n>  \t * example application, but a real camera implementation will require\n> @@ -278,15 +316,7 @@ const camera_metadata_t *CameraDevice::getStaticMetadata()\n>  \tstaticMetadata_->addEntry(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE,\n>  \t\t\t\t  &exposureTimeRange, 2);\n>  \n> -\t/*\n> -\t * The Android orientation metadata and libcamera rotation property are\n> -\t * defined differently but have identical numerical values for Android\n> -\t * devices such as phones and tablets.\n> -\t */\n> -\tint32_t orientation = 0;\n> -\tif (properties.contains(properties::Rotation))\n> -\t\torientation = properties.get(properties::Rotation);\n> -\tstaticMetadata_->addEntry(ANDROID_SENSOR_ORIENTATION, &orientation, 1);\n> +\tstaticMetadata_->addEntry(ANDROID_SENSOR_ORIENTATION, &orientation_, 1);\n>  \n>  \tstd::vector<int32_t> testPatterModes = {\n>  \t\tANDROID_SENSOR_TEST_PATTERN_MODE_OFF,\n> @@ -332,20 +362,18 @@ const camera_metadata_t *CameraDevice::getStaticMetadata()\n>  \t\t\t\t  lensApertures.data(),\n>  \t\t\t\t  lensApertures.size());\n>  \n> -\tuint8_t lensFacing = ANDROID_LENS_FACING_FRONT;\n> -\tif (properties.contains(properties::Location)) {\n> -\t\tint32_t location = properties.get(properties::Location);\n> -\t\tswitch (location) {\n> -\t\tcase properties::CameraLocationFront:\n> -\t\t\tlensFacing = ANDROID_LENS_FACING_FRONT;\n> -\t\t\tbreak;\n> -\t\tcase properties::CameraLocationBack:\n> -\t\t\tlensFacing = ANDROID_LENS_FACING_BACK;\n> -\t\t\tbreak;\n> -\t\tcase properties::CameraLocationExternal:\n> -\t\t\tlensFacing = ANDROID_LENS_FACING_EXTERNAL;\n> -\t\t\tbreak;\n> -\t\t}\n> +\tuint8_t lensFacing;\n> +\tswitch (facing_) {\n> +\tdefault:\n> +\tcase CAMERA_FACING_FRONT:\n> +\t\tlensFacing = ANDROID_LENS_FACING_FRONT;\n> +\t\tbreak;\n> +\tcase CAMERA_FACING_BACK:\n> +\t\tlensFacing = ANDROID_LENS_FACING_BACK;\n> +\t\tbreak;\n> +\tcase CAMERA_FACING_EXTERNAL:\n> +\t\tlensFacing = ANDROID_LENS_FACING_EXTERNAL;\n> +\t\tbreak;\n>  \t}\n>  \tstaticMetadata_->addEntry(ANDROID_LENS_FACING, &lensFacing, 1);\n>  \n> diff --git a/src/android/camera_device.h b/src/android/camera_device.h\n> index 350408c1a3e4..ace9c1b7c929 100644\n> --- a/src/android/camera_device.h\n> +++ b/src/android/camera_device.h\n> @@ -27,12 +27,17 @@ public:\n>  \tCameraDevice(unsigned int id, const std::shared_ptr<libcamera::Camera> &camera);\n>  \t~CameraDevice();\n>  \n> +\tint initialize();\n> +\n>  \tint open(const hw_module_t *hardwareModule);\n>  \tvoid close();\n>  \n>  \tunsigned int id() const { return id_; }\n>  \tcamera3_device_t *camera3Device() { return &camera3Device_; }\n>  \n> +\tint facing() const { return facing_; }\n> +\tint orientation() const { return orientation_; }\n> +\n>  \tvoid setCallbacks(const camera3_callback_ops_t *callbacks);\n>  \tconst camera_metadata_t *getStaticMetadata();\n>  \tconst camera_metadata_t *constructDefaultRequestSettings(int type);\n> @@ -69,6 +74,9 @@ private:\n>  \tCameraMetadata *staticMetadata_;\n>  \tstd::map<unsigned int, CameraMetadata *> requestTemplates_;\n>  \tconst camera3_callback_ops_t *callbacks_;\n> +\n> +\tint facing_;\n> +\tint orientation_;\n>  };\n>  \n>  #endif /* __ANDROID_CAMERA_DEVICE_H__ */\n> diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp\n> index b02d8d1a8362..02b6418fb36d 100644\n> --- a/src/android/camera_hal_manager.cpp\n> +++ b/src/android/camera_hal_manager.cpp\n> @@ -65,8 +65,11 @@ int CameraHalManager::init()\n>  \tunsigned int index = 0;\n>  \tfor (auto &cam : cameraManager_->cameras()) {\n>  \t\tCameraDevice *camera = new CameraDevice(index, cam);\n> -\t\tcameras_.emplace_back(camera);\n> +\t\tret = camera->initialize();\n> +\t\tif (ret)\n> +\t\t\tcontinue;\n>  \n> +\t\tcameras_.emplace_back(camera);\n>  \t\t++index;\n>  \t}\n>  \n> @@ -107,9 +110,8 @@ int CameraHalManager::getCameraInfo(unsigned int id, struct camera_info *info)\n>  \n>  \tCameraDevice *camera = cameras_[id].get();\n>  \n> -\t/* \\todo Get these info dynamically inspecting the camera module. */\n> -\tinfo->facing = id ? CAMERA_FACING_FRONT : CAMERA_FACING_BACK;\n> -\tinfo->orientation = 0;\n> +\tinfo->facing = camera->facing();\n> +\tinfo->orientation = camera->orientation();\n>  \tinfo->device_version = CAMERA_DEVICE_API_VERSION_3_3;\n>  \tinfo->resource_cost = 0;\n>  \tinfo->static_camera_characteristics = camera->getStaticMetadata();","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 72C8E61012\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  4 Jun 2020 02:48:24 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id DFEC929B;\n\tThu,  4 Jun 2020 02:48:23 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Zhm19z3p\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1591231704;\n\tbh=mQXH/DQSmOIZ1QZEfEG/QsSuEil26ks4yI4fyR3Lt2w=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Zhm19z3pEswq02n/qnRLDnrdbNd7/opGoG9jievQVdl10hNF3e+VelDTb4Q4XZrYS\n\tgoqath4K1ZIZ1GHN526w5wKKrJwOsOuc34rrXnIK+JdSZGEOhokNC5lBI1nt2pHN+I\n\tshklSlZU20n/0RYFblUFkHov22nF6VN0ZIlOGIjM=","Date":"Thu, 4 Jun 2020 03:48:07 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20200604004807.GH27695@pendragon.ideasonboard.com>","References":"<20200526142237.407557-1-jacopo@jmondi.org>\n\t<20200526142237.407557-4-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20200526142237.407557-4-jacopo@jmondi.org>","Subject":"Re: [libcamera-devel] [PATCH 3/8] android: hal_manager: Do not\n\thardcode properties","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":"Thu, 04 Jun 2020 00:48:24 -0000"}}]