From patchwork Wed Oct 28 10:01:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 10286 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 9CCA7C3B5C for ; Wed, 28 Oct 2020 10:01:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0AEB662390; Wed, 28 Oct 2020 11:01:48 +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="VPnBk5u3"; dkim-atps=neutral Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 03A3462067 for ; Wed, 28 Oct 2020 11:01:47 +0100 (CET) Received: by mail-pj1-x1042.google.com with SMTP id gi3so2214572pjb.3 for ; Wed, 28 Oct 2020 03:01:46 -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:mime-version :content-transfer-encoding; bh=osQIJntr8WSN03DZiuYvHMC87nWvx0zAzTYas9a7b9c=; b=VPnBk5u309rV4YmbRzQbyFPwyOeO6Xz0JR8Y/BmUJJyYa+SwKdhRXRckWgz8R0jek2 SgSA/0Du9z5r6VGL+ziTXt6UIP/fd1vBsdyvOy6swfWl3KdwVfQ9nkHu2ttJKgZ00ktp mqcvnQn7BAVoftikoysdODt7s4ONVQHTW8RlE= 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:mime-version :content-transfer-encoding; bh=osQIJntr8WSN03DZiuYvHMC87nWvx0zAzTYas9a7b9c=; b=AHgpZSTyhfSntR4TdfCCtWJ1UntGw5j4kpsdZT6/Namy4Xg/XhpXf+lkJqZf/x+b1E al2QPbKAA2JwCrxe8Q4Bs3Cfga27A0zuah26OOGx3RPL+PAQi6Uv/AF6majJWoOADEqP OO7L0EJSg7Ws0xNsl/PPZgHTNmPObuu/u6B7KM64TXRgIX26gUvywc1foFR9hqG5mm0G hZgd8VJpIDFFrzbqHYnRBb5gbqGZJ69gHCL5PL8gnwqNsUCkx/ToygTQLtUCeEcH1Eg+ VCyFCBNIXRSHjU4TuMLxmddYWlLtXuc8390IxLYgqt89hrJT4PVM24QVsnijI4tZZV8v nlpw== X-Gm-Message-State: AOAM532Puok/lRcOgEnvG/idaFdaAEYlHXNUql57ELMqGC6o3lrv8+ey KjTJbLTFuOr4iDy0cM8WQi3Qa7pcRaa5Xw== X-Google-Smtp-Source: ABdhPJzwHVZyFJWav73sElnuuKVuUQ3BuiWK/ontCCSVNCFxVBv6YYvbCtK0xj+JxnbvA8FjaVingA== X-Received: by 2002:a17:90b:1085:: with SMTP id gj5mr6135216pjb.30.1603879305177; Wed, 28 Oct 2020 03:01:45 -0700 (PDT) Received: from hiroh.tok.corp.google.com ([2401:fa00:8f:2:de4a:3eff:fe7d:f78f]) by smtp.gmail.com with ESMTPSA id t129sm5462526pfc.140.2020.10.28.03.01.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Oct 2020 03:01:44 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Wed, 28 Oct 2020 19:01:34 +0900 Message-Id: <20201028100134.3007829-1-hiroh@chromium.org> X-Mailer: git-send-email 2.29.0.rc2.309.g374f81d7ae-goog MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH] android: camera_device: Reorder configurations given by a HAL client 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" Reorder configurations given by a HAL client in configureStreams() so that CameraDevice and pipeline handlers can accept them as much as possible. Signed-off-by: Hirokazu Honda --- src/android/camera_device.cpp | 108 +++++++++++++++++++++++++++++++++- src/android/camera_device.h | 3 +- 2 files changed, 109 insertions(+), 2 deletions(-) -- 2.29.0.rc2.309.g374f81d7ae-goog diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index ca60f51..eee7248 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -9,6 +9,7 @@ #include "camera_ops.h" #include "post_processor.h" +#include #include #include #include @@ -132,6 +133,16 @@ const std::map camera3FormatsMap = { LOG_DECLARE_CATEGORY(HAL) +void printStreamList(const camera3_stream_configuration_t& streamList) { + LOG(HAL, Error) << "num_streams=" << streamList.num_streams; + for (unsigned int i = 0; i < streamList.num_streams; i++) { + const auto* stream = streamList.streams[i]; + LOG(HAL, Error) << i << ": width=" << stream->width + << ", height=" << stream->height + << ", format=" << stream->format; + } +} + MappedCamera3Buffer::MappedCamera3Buffer(const buffer_handle_t camera3buffer, int flags) { @@ -1188,7 +1199,7 @@ const camera_metadata_t *CameraDevice::constructDefaultRequestSettings(int type) return requestTemplate->get(); } -PixelFormat CameraDevice::toPixelFormat(int format) +PixelFormat CameraDevice::toPixelFormat(int format) const { /* Translate Android format code to libcamera pixel format. */ auto it = formatsMap_.find(format); @@ -1201,6 +1212,97 @@ PixelFormat CameraDevice::toPixelFormat(int format) return it->second; } +int CameraDevice::sortStreamConfig(camera3_stream_configuration_t* streamList) const +{ + std::map> streamMap; + for (unsigned int i = 0; i < streamList->num_streams; ++i) { + const camera3_stream_t* stream = streamList->streams[i]; + PixelFormat format = toPixelFormat(stream->format); + if (!format.isValid()) + return -EINVAL; + streamMap[format.fourcc()].push_back(stream); + } + std::vector formats; + for (auto& s: streamMap) { + formats.push_back(s.first); + auto& streams = s.second; + /* Sorted by resolution. Smaller is put first. */ + std::sort(streams.begin(), streams.end(), + [](const camera3_stream_t* streamA, + const camera3_stream_t* streamB) { + if (streamA->width != streamB->width) + return streamA->width < streamB->width; + return streamA->height < streamB->height; + }); + } + + if (streamMap.find(formats::MJPEG) != streamMap.end() && + streamMap[formats::MJPEG].size() > 1) { + LOG(HAL, Error) << "Multiple JPEG streams are not supported"; + return -EINVAL; + } + + std::vector sortedStreams; + /* + * NV12 is the most prioritized format. Put the configuration with NV12 + * and the largest resolution first. + */ + auto nv12StreamsIt = streamMap.find(formats::NV12); + if (nv12StreamsIt != streamMap.end()) { + auto& nv12Streams = nv12StreamsIt->second; + const camera3_stream_t* nv12Stream = nv12Streams.back(); + sortedStreams.push_back(nv12Stream); + nv12Streams.pop_back(); + } + + /* + * If JPEG is there, then put the JPEG configuration second and a + * configuration whose specified stream the JPEG is encoded from third + * if there is. + */ + auto jpegStreamsIt = streamMap.find(formats::MJPEG); + if (jpegStreamsIt != streamMap.end()) { + auto& jpegStreams = jpegStreamsIt->second; + const camera3_stream_t* jpegStream = jpegStreams[0]; + sortedStreams.push_back(jpegStream); + streamMap.erase(jpegStreamsIt); + if (nv12StreamsIt != streamMap.end()) { + auto& nv12Streams = nv12StreamsIt->second; + auto nv12StreamForJpegIt = + std::find_if(nv12Streams.begin(), + nv12Streams.end(), + [&jpegStream](const auto* nv12Stream) { + return nv12Stream->width == jpegStream->width && nv12Stream->height == jpegStream->height; + }); + if (nv12StreamForJpegIt != nv12Streams.end()) { + sortedStreams.push_back(*nv12StreamForJpegIt); + nv12Streams.erase(nv12StreamForJpegIt); + } + } + } + + /* + * Put configurations with different formats and larger resolutions + * earlier. + */ + while (!streamMap.empty()) { + for (auto it = streamMap.begin(); it != streamMap.end();) { + auto& streams = it->second; + if (streams.empty()) { + it = streamMap.erase(it); + continue; + } + sortedStreams.push_back(streams.back()); + streams.pop_back(); + } + } + + assert(sortedStreams.size() == streamList->num_streams); + memcpy(streamList->streams, sortedStreams.data(), + sizeof(camera3_stream_t*) * streamList->num_streams); + return 0; +} + /* * Inspect the stream_list to produce a list of StreamConfiguration to * be use to configure the Camera. @@ -1225,6 +1327,10 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) streams_.clear(); streams_.reserve(stream_list->num_streams); + printStreamList(*stream_list); + sortStreamConfig(stream_list); + printStreamList(*stream_list); + /* First handle all non-MJPEG streams. */ camera3_stream_t *jpegStream = nullptr; for (unsigned int i = 0; i < stream_list->num_streams; ++i) { diff --git a/src/android/camera_device.h b/src/android/camera_device.h index fd08738..f8dc28b 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -104,7 +104,8 @@ private: void notifyShutter(uint32_t frameNumber, uint64_t timestamp); void notifyError(uint32_t frameNumber, camera3_stream_t *stream); CameraMetadata *requestTemplatePreview(); - libcamera::PixelFormat toPixelFormat(int format); + libcamera::PixelFormat toPixelFormat(int format) const; + int sortStreamConfig(camera3_stream_configuration_t* streamList) const; std::unique_ptr getResultMetadata(int frame_number, int64_t timestamp);