From patchwork Sat Dec 12 00:41:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 10651 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 C619CBD80A for ; Sat, 12 Dec 2020 01:12:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 41B0F746F0; Sat, 12 Dec 2020 02:12:40 +0100 (CET) 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="fW7p1zEo"; dkim-atps=neutral Received: from mail-vs1-xe2c.google.com (mail-vs1-xe2c.google.com [IPv6:2607:f8b0:4864:20::e2c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BAAE468053 for ; Sat, 12 Dec 2020 02:12:36 +0100 (CET) Received: by mail-vs1-xe2c.google.com with SMTP id w18so5801252vsk.12 for ; Fri, 11 Dec 2020 17:12:36 -0800 (PST) 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=mrhlPWp8sVUtRnP/iC6DtmwkaemYBaUVHJT6rsBtREE=; b=fW7p1zEobdCx8rRDZ+pdnKnEEC1Tbj8RnrUBFmvUuSBo04jY9K61AucMVeKbqlLpsC ETBDCL9VEpcDkVuoOPQ7BQ3n4LovpahVZgkxI8w/eDZhu05ZjOBYkFjPMWHldf+6aUE5 Js8LystEXR4nN9MHQ7eiGu5YmROa3rvJMXSfY= 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=mrhlPWp8sVUtRnP/iC6DtmwkaemYBaUVHJT6rsBtREE=; b=jY0xHqz1Z2G3FSsACgXDwT40+yZBaMxEkuiiUTcEcmAGB/7DJmhO1W4UFU2/Z4XHtg dtXfMQdENhAbKNo63cyuImDbHqm8RB94eQ5xBc91BUY7oVX6fUQOPwAZXfAc34qM8611 Bu3/nXlw+kqIG9/1ZaN7DSIdyH9aNijj0in88j3eyt21V5RRjd5HDNkHGii+PTQw7hJP 7I0tmG1UfQz7gzTwikk7CsnRoIFMPF1IyZLRsFzDFoKNL7fiebsO6rgg5QFFXQh5lAoo 7V+cqmfOkFofKEBJMer1WMYnFkQ+AGMOw7+96ye+XN7r6FyCobAncu3BvzT/epBcoa+2 DALA== X-Gm-Message-State: AOAM531GSbiLCvmhpwqTd1D/DtwGS78mOJsRgHKRWvH+mSfvD1cDO1/3 78eZbk859iQR6+gQ0CFZGG230dVonz0tN+Tz X-Google-Smtp-Source: ABdhPJxk5GIvcZomG002r6aNwl/NnkL4VwFzt5OjnscswBzNnFQBKAAX3w7Ad4Up7Ah5gf+idD4B5A== X-Received: by 2002:a62:e213:0:b029:19e:59d3:a76a with SMTP id a19-20020a62e2130000b029019e59d3a76amr5526097pfi.53.1607733718943; Fri, 11 Dec 2020 16:41:58 -0800 (PST) Received: from hiroh.c.googlers.com.com (128.141.236.35.bc.googleusercontent.com. [35.236.141.128]) by smtp.gmail.com with ESMTPSA id x188sm11558534pfd.43.2020.12.11.16.41.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Dec 2020 16:41:58 -0800 (PST) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Sat, 12 Dec 2020 00:41:49 +0000 Message-Id: <20201212004149.158801-3-hiroh@chromium.org> X-Mailer: git-send-email 2.29.2.576.ga3fc446d84-goog In-Reply-To: <20201212004149.158801-1-hiroh@chromium.org> References: <20201212004149.158801-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v8 3/3] android: camera_device: Reorder configurations before requesting 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" This reorders Camera3Configs before executing CameraConfiguration::validate() to make it easier for the Camera to satisfy the Android framework request. Signed-off-by: Hirokazu Honda Reviewed-by: Jacopo Mondi Reviewed-by: Kieran Bingham Reviewed-by: Umang Jain Reviewed-by: Laurent Pinchart --- src/android/camera_device.cpp | 113 +++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 2 deletions(-) -- 2.29.2.576.ga3fc446d84-goog diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 7e8b2818..0b68a927 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -27,6 +27,8 @@ using namespace libcamera; +LOG_DECLARE_CATEGORY(HAL) + namespace { /* @@ -144,9 +146,115 @@ struct Camera3StreamConfig { std::vector streams; StreamConfiguration config; }; -} /* namespace */ -LOG_DECLARE_CATEGORY(HAL) +/* + * Reorder the configurations so that libcamera::Camera can accept them as much + * as possible. The sort rule is as follows. + * 1.) The configuration for NV12 request whose resolution is the largest. + * 2.) The configuration for JPEG request. + * 3.) Others. Larger resolutions and different formats are put earlier. + */ +void sortCamera3StreamConfigs(std::vector &unsortedConfigs, + const camera3_stream_t *jpegStream) +{ + const Camera3StreamConfig *jpegConfig = nullptr; + + std::map> formatToConfigs; + for (const auto &streamConfig : unsortedConfigs) { + if (jpegStream && !jpegConfig) { + const auto &streams = streamConfig.streams; + if (std::find_if(streams.begin(), streams.end(), + [jpegStream](const auto &stream) { + return stream.stream == jpegStream; + }) != streams.end()) { + jpegConfig = &streamConfig; + continue; + } + } + formatToConfigs[streamConfig.config.pixelFormat].push_back(&streamConfig); + } + if (jpegStream && !jpegConfig) + LOG(HAL, Fatal) << "No Camera3StreamConfig is found for JPEG"; + + for (auto &fmt : formatToConfigs) { + auto &streamConfigs = fmt.second; + + /* Sorted by resolution. Smaller is put first. */ + std::sort(streamConfigs.begin(), streamConfigs.end(), + [](const auto *streamConfigA, const auto *streamConfigB) { + const Size &sizeA = streamConfigA->config.size; + const Size &sizeB = streamConfigB->config.size; + return sizeA < sizeB; + }); + } + + std::vector sortedConfigs; + sortedConfigs.reserve(unsortedConfigs.size()); + + /* + * NV12 is the most prioritized format. Put the configuration with NV12 + * and the largest resolution first. + */ + const auto nv12It = formatToConfigs.find(formats::NV12); + if (nv12It != formatToConfigs.end()) { + auto &nv12Configs = nv12It->second; + const Camera3StreamConfig *nv12Largest = nv12Configs.back(); + + /* + * If JPEG will be created from NV12 and the size is larger than + * the largest NV12 configurations, then put the NV12 + * configuration for JPEG first. + */ + if (jpegConfig && jpegConfig->config.pixelFormat == formats::NV12) { + const Size &nv12SizeForJpeg = jpegConfig->config.size; + const Size &nv12LargestSize = nv12Largest->config.size; + + if (nv12LargestSize < nv12SizeForJpeg) { + LOG(HAL, Debug) << "Insert " << jpegConfig->config.toString(); + sortedConfigs.push_back(std::move(*jpegConfig)); + jpegConfig = nullptr; + } + } + + LOG(HAL, Debug) << "Insert " << nv12Largest->config.toString(); + sortedConfigs.push_back(*nv12Largest); + nv12Configs.pop_back(); + + if (nv12Configs.empty()) + formatToConfigs.erase(nv12It); + } + + /* If the configuration for JPEG is there, then put it. */ + if (jpegConfig) { + LOG(HAL, Debug) << "Insert " << jpegConfig->config.toString(); + sortedConfigs.push_back(std::move(*jpegConfig)); + jpegConfig = nullptr; + } + + /* + * Put configurations with different formats and larger resolutions + * earlier. + */ + while (!formatToConfigs.empty()) { + for (auto it = formatToConfigs.begin(); it != formatToConfigs.end();) { + auto &configs = it->second; + LOG(HAL, Debug) << "Insert " << configs.back()->config.toString(); + sortedConfigs.push_back(*configs.back()); + configs.pop_back(); + + if (configs.empty()) + it = formatToConfigs.erase(it); + else + it++; + } + } + + ASSERT(sortedConfigs.size() == unsortedConfigs.size()); + + unsortedConfigs = sortedConfigs; +} + +} /* namespace */ MappedCamera3Buffer::MappedCamera3Buffer(const buffer_handle_t camera3buffer, int flags) @@ -1356,6 +1464,7 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) streamConfigs[index].streams.push_back({ jpegStream, type }); } + sortCamera3StreamConfigs(streamConfigs, jpegStream); for (const auto &streamConfig : streamConfigs) { config_->addConfiguration(streamConfig.config);