From patchwork Wed Dec 14 09:33:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18008 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 8CECFC328D for ; Wed, 14 Dec 2022 09:33:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4E95963374; Wed, 14 Dec 2022 10:33:47 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1671010427; bh=kmpDd7yixG54jaDd+YBHVfCce/k6l4WE6/wbifByPug=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=UnTYk88El8z2z3S9WzbJknHW8cTAa0NwbygMJHGYbS6fn0yubJCZdIPTe7J908zVQ +2kIph9IwIN1NajOkR8EdLGGvbR99fQ+CFGeeMvNd16Gy8vYYonYuFKWbg2rtJBpP/ c3PiFc3wBqOe+ozAK/35NQpHAZy0+rgP6eVFFEzOF7JbgkJXR8+aJyq+ncRy3qDKJ7 hOquBebYR6AhUMV+GJ2A23bSQxhBb2AyGf3KZ/ANnU15HIU06w90Qw/xdYX5exvW28 HL6ODjWXfsDYrkYahCl8stfzrL0mVrzV3YVIq63kXPCKHB4Y7aPFPe/jhy9cbg9sZN nC9atx9T1H2EA== Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D7A4563365 for ; Wed, 14 Dec 2022 10:33:44 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="dgJja3lc"; dkim-atps=neutral Received: by mail-pj1-x102f.google.com with SMTP id gt4so6330021pjb.1 for ; Wed, 14 Dec 2022 01:33:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GUhT+SY+bU9vAVRynk22I9wvkA9Aii8PDWqKJ6JeH/Y=; b=dgJja3lcVtwdr1+vHyZST3khNcIjYvz03+uS641Cv8z5xiw8Tz9XWhgghuJYiT0eTj 3W2O0RfkwISmOLV+xCFD5KDWVb/e4RNj8Hm0zNgY73z0m8+8XaGv5tvxEJ/Yn3uPMg5V ALeS3ZEs9iu8mABrPUB+KhLh6d/km9/v2UpwY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GUhT+SY+bU9vAVRynk22I9wvkA9Aii8PDWqKJ6JeH/Y=; b=tFVSxPbNRA6scKECH0zgtgcDUGmN37LzflAC+skuL4vnROjbD83dNdKAgQt1aP+8BT fKoQ4sv44EbSh1CJdtHuaASHj4QokMMJFFbhz6Jg6GrbtPn4siEs9BDF0auP0BRtBwcV kL+66Yya1qOJkM/nPlXnvvImBDdqQNNgM8NbjfWcffB9Ke7r1bu1KIziDL6llCgCzO3T pxK6YqSAEMtO42wzjtrA5Vo0z7CpZ/cSe1Zx5PlIrHKiIK3SWwLFup+B5BUYqfhOXwZg oW8WMGFUe+pZkwdGjAp5n/3B8JrEbleBwrMjK+2iS2eX6WB1oc0gpENLLA0fB0SdW0fn dTnQ== X-Gm-Message-State: ANoB5pkJ9dhhRC+Q1mMySutheqNTUfCxDivp9es3+23ilvmyl3cwRYw6 OCqsWjJ0dTXZE47EydGdsh1HM+9VnmatSafG X-Google-Smtp-Source: AA0mqf7/A05LNAoH6F4+snlTyWO4VUOCLYVqhy5TqLHbb/fE0E79Ghu4rLhCCEC0Ncq20VmpBn2DbQ== X-Received: by 2002:a05:6a20:12d6:b0:a7:8ce5:6fc6 with SMTP id v22-20020a056a2012d600b000a78ce56fc6mr33668841pzg.13.1671010423103; Wed, 14 Dec 2022 01:33:43 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (46.165.189.35.bc.googleusercontent.com. [35.189.165.46]) by smtp.gmail.com with ESMTPSA id d206-20020a621dd7000000b0056bc742d21esm9331190pfd.176.2022.12.14.01.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 01:33:42 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Wed, 14 Dec 2022 09:33:30 +0000 Message-Id: <20221214093330.3345421-8-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.0.rc1.256.g54fd8350bd-goog In-Reply-To: <20221214093330.3345421-1-chenghaoyang@google.com> References: <20221214093330.3345421-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v8 7/7] Add JEA implementation 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: , X-Patchwork-Original-From: Harvey Yang via libcamera-devel From: Harvey Yang Reply-To: Harvey Yang Cc: Harvey Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Harvey Yang This patch adds JEA implementation to replace LibJpeg in CrOS platform, where hardware accelerator is available. Signed-off-by: Harvey Yang --- src/android/cros/camera3_hal.cpp | 4 +- src/android/cros_mojo_token.h | 12 +++ src/android/jpeg/encoder_jea.cpp | 105 +++++++++++++++++++++++ src/android/jpeg/encoder_jea.h | 35 ++++++++ src/android/jpeg/meson.build | 13 ++- src/android/jpeg/post_processor_jpeg.cpp | 8 ++ 6 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 src/android/cros_mojo_token.h create mode 100644 src/android/jpeg/encoder_jea.cpp create mode 100644 src/android/jpeg/encoder_jea.h diff --git a/src/android/cros/camera3_hal.cpp b/src/android/cros/camera3_hal.cpp index fb863b5f..71acb441 100644 --- a/src/android/cros/camera3_hal.cpp +++ b/src/android/cros/camera3_hal.cpp @@ -8,9 +8,11 @@ #include #include "../camera_hal_manager.h" +#include "../cros_mojo_token.h" -static void set_up([[maybe_unused]] cros::CameraMojoChannelManagerToken *token) +static void set_up(cros::CameraMojoChannelManagerToken *token) { + gCrosMojoToken = token; } static void tear_down() diff --git a/src/android/cros_mojo_token.h b/src/android/cros_mojo_token.h new file mode 100644 index 00000000..043c752a --- /dev/null +++ b/src/android/cros_mojo_token.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2022, Google Inc. + * + * cros_mojo_token.h - cros-specific mojo token + */ + +#pragma once + +#include + +inline cros::CameraMojoChannelManagerToken *gCrosMojoToken = nullptr; diff --git a/src/android/jpeg/encoder_jea.cpp b/src/android/jpeg/encoder_jea.cpp new file mode 100644 index 00000000..139bde93 --- /dev/null +++ b/src/android/jpeg/encoder_jea.cpp @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2022, Google Inc. + * + * encoder_jea.cpp - JPEG encoding using CrOS JEA + */ + +#include "encoder_jea.h" + +#include "libcamera/internal/mapped_framebuffer.h" + +#include + +#include "../cros_mojo_token.h" +#include "../hal_framebuffer.h" + +EncoderJea::EncoderJea() = default; + +EncoderJea::~EncoderJea() = default; + +int EncoderJea::configure(const libcamera::StreamConfiguration &cfg) +{ + size_ = cfg.size; + + if (jpegCompressor_) + return 0; + + if (gCrosMojoToken == nullptr) + return -ENOTSUP; + + jpegCompressor_ = cros::JpegCompressor::GetInstance(gCrosMojoToken); + + return 0; +} + +int EncoderJea::encode(Camera3RequestDescriptor::StreamBuffer *streamBuffer, + libcamera::Span exifData, + unsigned int quality) +{ + if (!jpegCompressor_) + return -ENOTSUP; + + uint32_t outDataSize = 0; + const HALFrameBuffer *fb = dynamic_cast( + streamBuffer->srcBuffer); + + if (!jpegCompressor_->CompressImageFromHandle(fb->handle(), + *streamBuffer->camera3Buffer, + size_.width, size_.height, + quality, exifData.data(), + exifData.size(), + &outDataSize)) + return -EBUSY; + + return outDataSize; +} + +void EncoderJea::generateThumbnail(const libcamera::FrameBuffer &source, + const libcamera::Size &targetSize, + unsigned int quality, + std::vector *thumbnail) +{ + if (!jpegCompressor_) + return; + + libcamera::MappedFrameBuffer frame(&source, + libcamera::MappedFrameBuffer::MapFlag::Read); + + if (frame.planes().empty()) + return; + + // JEA needs consecutive memory. + unsigned long size = 0, index = 0; + for (const auto& plane : frame.planes()) + size += plane.size(); + + std::vector data(size); + for (const auto& plane : frame.planes()) { + memcpy(&data[index], plane.data(), plane.size()); + index += plane.size(); + } + + uint32_t outDataSize = 0; + + /* + * Since the structure of the App1 segment is like: + * 0xFF [1 byte marker] [2 bytes size] [data] + * And it should not be larger than 64K. + */ + constexpr int kApp1MaxDataSize = 65532; + thumbnail->resize(kApp1MaxDataSize); + + if (!jpegCompressor_->GenerateThumbnail(data.data(), + size_.width, size_.height, + targetSize.width, + targetSize.height, quality, + thumbnail->size(), + thumbnail->data(), + &outDataSize)) { + thumbnail->clear(); + return; + } + + thumbnail->resize(outDataSize); +} diff --git a/src/android/jpeg/encoder_jea.h b/src/android/jpeg/encoder_jea.h new file mode 100644 index 00000000..817b3202 --- /dev/null +++ b/src/android/jpeg/encoder_jea.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2022, Google Inc. + * + * encoder_jea.h - JPEG encoding using CrOS JEA + */ + +#pragma once + +#include + +#include + +#include "encoder.h" + +class EncoderJea : public Encoder +{ +public: + EncoderJea(); + ~EncoderJea(); + + int configure(const libcamera::StreamConfiguration &cfg) override; + int encode(Camera3RequestDescriptor::StreamBuffer *streamBuffer, + libcamera::Span exifData, + unsigned int quality) override; + void generateThumbnail(const libcamera::FrameBuffer &source, + const libcamera::Size &targetSize, + unsigned int quality, + std::vector *thumbnail) override; + +private: + libcamera::Size size_; + + std::unique_ptr jpegCompressor_; +}; diff --git a/src/android/jpeg/meson.build b/src/android/jpeg/meson.build index 08397a87..2b68f54c 100644 --- a/src/android/jpeg/meson.build +++ b/src/android/jpeg/meson.build @@ -1,8 +1,17 @@ # SPDX-License-Identifier: CC0-1.0 android_hal_sources += files([ - 'encoder_libjpeg.cpp', 'exif.cpp', 'post_processor_jpeg.cpp', - 'thumbnailer.cpp' ]) + +platform = get_option('android_platform') +if platform == 'generic' + android_hal_sources += files([ + 'encoder_libjpeg.cpp', + 'thumbnailer.cpp' + ]) +elif platform == 'cros' + android_hal_sources += files(['encoder_jea.cpp']) + android_deps += [dependency('libcros_camera')] +endif diff --git a/src/android/jpeg/post_processor_jpeg.cpp b/src/android/jpeg/post_processor_jpeg.cpp index 918ab492..3424f8e7 100644 --- a/src/android/jpeg/post_processor_jpeg.cpp +++ b/src/android/jpeg/post_processor_jpeg.cpp @@ -12,7 +12,11 @@ #include "../camera_device.h" #include "../camera_metadata.h" #include "../camera_request.h" +#if defined(OS_CHROMEOS) +#include "encoder_jea.h" +#else // !defined(OS_CHROMEOS) #include "encoder_libjpeg.h" +#endif #include "exif.h" #include @@ -44,7 +48,11 @@ int PostProcessorJpeg::configure(const StreamConfiguration &inCfg, streamSize_ = outCfg.size; +#if defined(OS_CHROMEOS) + encoder_ = std::make_unique(); +#else // !defined(OS_CHROMEOS) encoder_ = std::make_unique(); +#endif return encoder_->configure(inCfg); }