From patchwork Fri Jul 15 14:38:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 16653 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 A8A29BE173 for ; Fri, 15 Jul 2022 14:38:41 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 64E5863325; Fri, 15 Jul 2022 16:38:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1657895921; bh=+tjB4LTzWCfPPM67vf/WerPRthW/aELUZae3TY7QBkE=; 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=bfC0TfLnPbUUFNBdBTeRfv63S6Xh5vERf1fTvKoteMhPOBcK1n+P7sG21wmIYn1+S 59CKzlz7aEmuK9Vm0o2RuDLjWPFjH9MRK6tgRvtmmdOo3mJ1+SmoQU+eRD0iKY6GBT QyjwFmUCDV3yEgAi3P3z5QLEI9HRdsuDK4uS2GvGoQdPoCXiyKfKzNmcVMwtYFSwgN 6H4sihedzoeB1oEukMvObK95KKe62WDjLQ1ledyt0xGGVxIDHmN/w3s3lTfK16FPuk tkN+GqmeiyViXd7YFlwY7NU4F1jBSZpsXPikvBUOglSigmlZf2gWVDvI0hNpOGZfKH tbOZ+yzR5cbbw== Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5EA176331F for ; Fri, 15 Jul 2022 16:38:39 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="O6S22fFg"; dkim-atps=neutral Received: by mail-pl1-x636.google.com with SMTP id x21so3383974plb.3 for ; Fri, 15 Jul 2022 07:38:39 -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=mfZvt5bd/xUhy+7wLC7VGXH1E/p8r11kFkp7vFXsRe8=; b=O6S22fFga8bxl3quSyx0YEk2IB5U7dFYujyvGPyim99jcxl3yZ6VFCkvNm9eg+yEdq zrChfPoYCnjE5jnSs3TpkN+YMMCLMoRn11kwdE3KpyheocJqac3X0vmexJ8Tyq1Z2MF/ BI7EBVvDx+2mG1TGEgUB1RX21MwFDTPbbSi2Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mfZvt5bd/xUhy+7wLC7VGXH1E/p8r11kFkp7vFXsRe8=; b=6hm0WJENqI2snXKtdxroft3hiX07wmAbYh79VAFyHL3wCyBCWKgxZHXxvOkFoFCezy ItTl1t6eBA9uu/jNXQErNHGLRgQGV9dtuC0c1TdWXTensFrWfw017rqPm56H/D48wuYu WXY10F/tYEH4HpXsG5eivmoE7CtOngO3S5l7n6TNXEbS677N9rXiiMuObFtk5LOlT2oT iZlWmBbsNaD19fEf+3RN/b2Xr99te57rWMs9HvLjcNHyIA9kHDQp1IrSKQLbcnXRNiJ/ 00rvhrsZKkRf8LYB+qxJDI63b/yYM6iUKgJgnptq1OSfQtoJwp9jTqJOez8OAJbgRKwZ BqKw== X-Gm-Message-State: AJIora+hJtBJpCt4wXxT47hW4PgcDzgsqG61y3YL59wcSYuYzL/OWx72 anp9UzQVoOWiIyXkHDdFSWvRBwsylPba3A== X-Google-Smtp-Source: AGRyM1vDlW1sBQjUvljaUPe8o/mksBMAODMmr0m1Vu5AoR8o1zyQuyI+5LX/iy8QMo4baYX3Zg/5Hg== X-Received: by 2002:a17:90b:4a88:b0:1ef:e6a1:f2e5 with SMTP id lp8-20020a17090b4a8800b001efe6a1f2e5mr16357053pjb.1.1657895917415; Fri, 15 Jul 2022 07:38:37 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (208.158.221.35.bc.googleusercontent.com. [35.221.158.208]) by smtp.gmail.com with ESMTPSA id f15-20020aa7968f000000b00528c22fbb45sm3972782pfk.141.2022.07.15.07.38.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jul 2022 07:38:36 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Fri, 15 Jul 2022 14:38:22 +0000 Message-Id: <20220715143822.672321-7-chenghaoyang@google.com> X-Mailer: git-send-email 2.37.0.170.g444d1eabd0-goog In-Reply-To: <20220715143822.672321-1-chenghaoyang@google.com> References: <20220715143822.672321-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v6 6/6] 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 Lib Jpeg in cros platform, where HW accelerator is available. Signed-off-by: Harvey Yang --- src/android/cros/camera3_hal.cpp | 2 + src/android/cros_mojo_token.h | 12 +++ src/android/jpeg/encoder_jea.cpp | 93 ++++++++++++++++++++++++ src/android/jpeg/encoder_jea.h | 35 +++++++++ src/android/jpeg/meson.build | 12 ++- src/android/jpeg/post_processor_jpeg.cpp | 8 ++ 6 files changed, 160 insertions(+), 2 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..d75afccb 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) { + 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..66a854c4 --- /dev/null +++ b/src/android/jpeg/encoder_jea.cpp @@ -0,0 +1,93 @@ +/* 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; +} + +int EncoderJea::generateThumbnail(const libcamera::FrameBuffer &source, + const libcamera::Size &targetSize, + unsigned int quality, + std::vector *thumbnail) +{ + if (!jpegCompressor_) + return -ENOTSUP; + + libcamera::MappedFrameBuffer frame(&source, + libcamera::MappedFrameBuffer::MapFlag::Read); + + if (frame.planes().empty()) + return -ENODATA; + + 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. + const int kApp1MaxDataSize = 65532; + thumbnail->resize(kApp1MaxDataSize); + + if (!jpegCompressor_->GenerateThumbnail(frame.planes()[0].data(), + size_.width, size_.height, + targetSize.width, + targetSize.height, quality, + thumbnail->size(), + thumbnail->data(), + &outDataSize)) { + thumbnail->clear(); + return -EBUSY; + } + + thumbnail->resize(outDataSize); + return outDataSize; +} diff --git a/src/android/jpeg/encoder_jea.h b/src/android/jpeg/encoder_jea.h new file mode 100644 index 00000000..2eba31c2 --- /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; + int 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..d25016b1 100644 --- a/src/android/jpeg/meson.build +++ b/src/android/jpeg/meson.build @@ -1,8 +1,16 @@ # 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 a1555048..4486ff08 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); }