From patchwork Mon May 24 11:56:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12388 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 D0564C3201 for ; Mon, 24 May 2021 11:56:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8EFC368919; Mon, 24 May 2021 13:56:52 +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="ClDA3GBt"; dkim-atps=neutral Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6596668923 for ; Mon, 24 May 2021 13:56:50 +0200 (CEST) Received: by mail-pj1-x1034.google.com with SMTP id gb21-20020a17090b0615b029015d1a863a91so11113156pjb.2 for ; Mon, 24 May 2021 04:56:50 -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=0s2Nd4Ix/jMmEzfVpR+RpQh2atUP4LM4KsrXuCpddx4=; b=ClDA3GBt6uY5U8J8Y4z7uLJ0Pbq41rWHtNQfD0JygsS8+ECc+SqB7J6hvt4kyX+iEu s1fLTW4eBNP+HynyNH8vfpPQitvSqK038LEbNG1T1jszpjTF9hKr7eXi0+Gwc7EmlkAO wWrHKE5udmlC/ZcBZ6si+9HqfFriOsEUC689I= 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=0s2Nd4Ix/jMmEzfVpR+RpQh2atUP4LM4KsrXuCpddx4=; b=AKf2CiLXtWyysPXHggWR29vGKJrNr/USd7hzbhw05UwSQdc6bryPDTnEw+kVaLUZXh 0sTQYyK1AU2dEDRGWvVP4OgNPE1gLFPyTRH3v0o8OEkDEoPtJE3LTv0n5OWULNv/fXTe xgca7+IUs9aQ1NdsT+D5tlL75llNNAKa+5oOadTmpAwvWWbPudSRTW4mY0uWb5tNiLoS JpQlm0qIQHvAN0SDHH7GD1+biDTCEIgFKWfZIGKVKQuZvpPD+qkfp7msGEL0OASLGzxa lA43x4NE4oKOea5pcmq1qDiQL7fIOsENgMUb1XL3A9PKnhxYbMutSEvM63qX4Ybt6Yv2 mZTw== X-Gm-Message-State: AOAM531LVAncMI1x/yGSgmvHS3W+UBY8JiI5uG4Hh19P/L/c9jZybdsc nLBOe4UH7ZmdSWdai+IXdWCfzrfKMcB8rA== X-Google-Smtp-Source: ABdhPJyhSTCw6V8erjvA5ciEXQys7IFBbEe5qAKXDKpVxr051RN1LX/6qjwgE0jjrnFPhwufcHxgjA== X-Received: by 2002:a17:90b:3b92:: with SMTP id pc18mr24335478pjb.218.1621857408594; Mon, 24 May 2021 04:56:48 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:7208:465c:c4e:2dc3]) by smtp.gmail.com with ESMTPSA id t19sm11149980pfg.70.2021.05.24.04.56.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 May 2021 04:56:48 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Mon, 24 May 2021 20:56:40 +0900 Message-Id: <20210524115640.2334778-3-hiroh@chromium.org> X-Mailer: git-send-email 2.31.1.818.g46aad6cb9e-goog In-Reply-To: <20210524115640.2334778-1-hiroh@chromium.org> References: <20210524115640.2334778-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH RFC 2/2] 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 --- src/android/camera3_hal.cpp | 14 +++++++------- src/android/camera_hal_manager.cpp | 7 +++++++ src/android/camera_hal_manager.h | 5 ++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/android/camera3_hal.cpp b/src/android/camera3_hal.cpp index f2d4799f..59d51ed5 100644 --- a/src/android/camera3_hal.cpp +++ b/src/android/camera3_hal.cpp @@ -24,25 +24,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; } @@ -70,7 +68,7 @@ static int hal_init() { LOG(HAL, Info) << "Initialising Android camera HAL"; - cameraManager.init(); + CameraHalManager::instance()->init(); return 0; } @@ -85,7 +83,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 << "'"; @@ -135,6 +134,7 @@ 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/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp index bf3fcda7..387b600d 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 d9bf2798..9b43afc4 100644 --- a/src/android/camera_hal_manager.h +++ b/src/android/camera_hal_manager.h @@ -24,9 +24,10 @@ class CameraDevice; class CameraHalManager { public: - CameraHalManager(); ~CameraHalManager(); + static CameraHalManager *instance(); + int init(); std::tuple @@ -42,6 +43,8 @@ private: static constexpr unsigned int firstExternalCameraId_ = 1000; + CameraHalManager(); + static int32_t cameraLocation(const libcamera::Camera *cam); void cameraAdded(std::shared_ptr cam);