From patchwork Wed May 26 08:46:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12424 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 E1F54C3200 for ; Wed, 26 May 2021 08:46:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9A3E068927; Wed, 26 May 2021 10:46:39 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="fX4YyQ/T"; dkim-atps=neutral Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 07E546891F for ; Wed, 26 May 2021 10:46:38 +0200 (CEST) Received: by mail-pg1-x536.google.com with SMTP id 29so353015pgu.11 for ; Wed, 26 May 2021 01:46:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=R4HPHdHDV0NMZnrMmtHYY2z028FmfsUBJ/+t9xM/q/s=; b=fX4YyQ/Tyi94z9f9QbljJcolYDlD/oTcJRqaKbnyWQ/QQW+kIGGDHBGgoMJ5A3ka25 wVcUH4VBAVL0K15igAdyr1zQ5HKlI5ZG69PrEVHceJZyZpb+EvljRO4/1p0j0+qznxMS giSNtG5adKi2U2wZ80Mvnxb28ACFNrEPX2M/4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=R4HPHdHDV0NMZnrMmtHYY2z028FmfsUBJ/+t9xM/q/s=; b=U1msDDQElraEXWC1qX6ySQrhTIUNUkk7IAG1YIOdkzijiPVDiHsFE8tm6AlAGzuGio j1xD0SseH3Dd9yG3pg+PG3F5YWBe8PrTxRPGQ+B456bViSpEv4TmWoZRCsBiLleFe201 802sc0jS3RAOI4x1RB95HOKNt8fIon4A3/GtddRUs+l9p00DT9ANU4mq3v5ekr6oR0ze lrQrYqTH4ZtlcZprOxZlJYRzrJZ6G88nkn8El1v/yrmxoB5OJE0pJwlXswykTIm9HUTT hy1WAL3rTOiVEJ9oARFrbT5QF/sQC7OBVeDNWlrTBacNFTG/boTkmqkNCXDsu03Mp4BC kXVA== X-Gm-Message-State: AOAM532FkID4E8A5sUs8PY4JIT68Oy5bqeNRtgA9IzudYOAw9VCz+wSw pKxA6scvoUWA76xuUg/yR/owGj7QsJrhuA== X-Google-Smtp-Source: ABdhPJxfOcpiox2gJh7P2NzPGKDheDrKQuUafOIN93IiL2bEEhQ07/7TxC/fICveEmefLxWQ21+8Rw== X-Received: by 2002:a05:6a00:23c6:b029:28f:d463:17ba with SMTP id g6-20020a056a0023c6b029028fd46317bamr34800823pfc.65.1622018796054; Wed, 26 May 2021 01:46:36 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:e36c:ae58:48e5:2f8e]) by smtp.gmail.com with ESMTPSA id w125sm15346886pfw.214.2021.05.26.01.46.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 May 2021 01:46:35 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Wed, 26 May 2021 17:46:28 +0900 Message-Id: <20210526084628.3893266-2-hiroh@chromium.org> X-Mailer: git-send-email 2.31.1.818.g46aad6cb9e-goog In-Reply-To: <20210526084628.3893266-1-hiroh@chromium.org> References: <20210526084628.3893266-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/1] android: CameraHalManager: Create a static object dynamically 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" Originally CameraHalManager is created in the libcamera start up and destroyed in the libcamera termination. However, CameraHalManager destructor can access other static objects that has been destroyed. Avoid this issue by destroying CameraHalManager when tear_down() is called in ChromeOS or leaking it in other platforms. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart Reviewed-by: Jacopo Mondi --- src/android/camera3_hal.cpp | 13 ++++++------- src/android/camera_hal_manager.cpp | 7 +++++++ src/android/camera_hal_manager.h | 5 ++++- src/android/cros/camera3_hal.cpp | 3 +++ src/android/cros/meson.build | 3 ++- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/android/camera3_hal.cpp b/src/android/camera3_hal.cpp index 08773d33..e6d435e0 100644 --- a/src/android/camera3_hal.cpp +++ b/src/android/camera3_hal.cpp @@ -16,25 +16,23 @@ using namespace libcamera; LOG_DEFINE_CATEGORY(HAL) -static CameraHalManager cameraManager; - /*------------------------------------------------------------------------------ * Android Camera HAL callbacks */ static int hal_get_number_of_cameras() { - return cameraManager.numCameras(); + return CameraHalManager::instance()->numCameras(); } static int hal_get_camera_info(int id, struct camera_info *info) { - return cameraManager.getCameraInfo(id, info); + return CameraHalManager::instance()->getCameraInfo(id, info); } static int hal_set_callbacks(const camera_module_callbacks_t *callbacks) { - cameraManager.setCallbacks(callbacks); + CameraHalManager::instance()->setCallbacks(callbacks); return 0; } @@ -62,7 +60,7 @@ static int hal_init() { LOG(HAL, Info) << "Initialising Android camera HAL"; - cameraManager.init(); + CameraHalManager::instance()->init(); return 0; } @@ -77,7 +75,8 @@ static int hal_dev_open(const hw_module_t *module, const char *name, LOG(HAL, Debug) << "Open camera " << name; int id = atoi(name); - auto [camera, ret] = cameraManager.open(id, module); + + auto [camera, ret] = CameraHalManager::instance()->open(id, module); if (!camera) { LOG(HAL, Error) << "Failed to open camera module '" << id << "'"; diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp index f5b86974..54087d3a 100644 --- a/src/android/camera_hal_manager.cpp +++ b/src/android/camera_hal_manager.cpp @@ -37,6 +37,13 @@ CameraHalManager::CameraHalManager() /* CameraManager calls stop() in the destructor. */ CameraHalManager::~CameraHalManager() = default; +/* static */ +CameraHalManager *CameraHalManager::instance() +{ + static CameraHalManager *cameraHalManager = new CameraHalManager; + return cameraHalManager; +} + int CameraHalManager::init() { cameraManager_ = std::make_unique(); diff --git a/src/android/camera_hal_manager.h b/src/android/camera_hal_manager.h index af1581da..db9354a7 100644 --- a/src/android/camera_hal_manager.h +++ b/src/android/camera_hal_manager.h @@ -26,9 +26,10 @@ class CameraDevice; class CameraHalManager { public: - CameraHalManager(); ~CameraHalManager(); + static CameraHalManager *instance(); + int init(); std::tuple @@ -44,6 +45,8 @@ private: static constexpr unsigned int firstExternalCameraId_ = 1000; + CameraHalManager(); + static int32_t cameraLocation(const libcamera::Camera *cam); void cameraAdded(std::shared_ptr cam); diff --git a/src/android/cros/camera3_hal.cpp b/src/android/cros/camera3_hal.cpp index 31ad36ac..d6fc1d0f 100644 --- a/src/android/cros/camera3_hal.cpp +++ b/src/android/cros/camera3_hal.cpp @@ -7,12 +7,15 @@ #include +#include "../camera_hal_manager.h" + static void set_up(cros::CameraMojoChannelManagerToken *token) { } static void tear_down() { + delete CameraHalManager::instance(); } cros::cros_camera_hal_t CROS_CAMERA_EXPORT CROS_CAMERA_HAL_INFO_SYM = { diff --git a/src/android/cros/meson.build b/src/android/cros/meson.build index 4aab0f20..13ec8f0a 100644 --- a/src/android/cros/meson.build +++ b/src/android/cros/meson.build @@ -12,6 +12,7 @@ cros_hal_info = static_library('cros_hal_info', cros_hal_info_sources, dependencies : dependency('libcros_camera'), c_args : '-Wno-shadow', - include_directories : android_includes) + include_directories : [android_includes, + libcamera_includes]) libcamera_objects += cros_hal_info.extract_objects('camera3_hal.cpp')