From patchwork Thu Dec 1 09:27:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 17928 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 AEF93C3284 for ; Thu, 1 Dec 2022 09:27:53 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6A02463336; Thu, 1 Dec 2022 10:27:53 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1669886873; bh=KgBVeC479hx8nwxCUQfH81KCm4FAwzDWbVAlWZXCyUQ=; 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=rno4a/acshpgAqDtFJygLAu0GoLh/OBYL2p5kt3MJWzs0Dp/DkT6J9T2qqADsJzlT Y7rJL0tnBc/ZC3OkOTOCc+fn+Kl+GkVQgwjiNnqYiFE5UalxUVlzH1GpgRkViWaTQv 6AjfOgkp9WDF/xGYGjOhoEx1E7OnPd6tcg0VnTB66lnM4OG+Pd2PRgj4LnfhDjcPaa ctrodHW9MHF6MprRezyDmGdkH9pz6th+FrIoUZsUuSPGMtxw2UWAxOonwT442VGvTY Ov03dAW6txERT1gwbm0bBTRDJYyyvSS/vyDQ34ZMstqc9NYVZGCeul3k5zv5hc2oJr QoJOG827xIpCg== Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8F87D63336 for ; Thu, 1 Dec 2022 10:27:51 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="OT6OmqcR"; dkim-atps=neutral Received: by mail-pj1-x1032.google.com with SMTP id cm20so1330499pjb.1 for ; Thu, 01 Dec 2022 01:27:51 -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=p0n4GYoGdVuD6ag/A8hLDe9e8xGF6rYG6bYzbxH7S6A=; b=OT6OmqcRZiMMvWnglyrhgg1BXr5ah0ouacwN5hwNRMvzTY6bLL8BNP2vv4RMkGGCkW yX1TCLMGWnU49deroqFn432g/r0MFnqu1CLxBI3bb+QIEuY9ALbe4YrFdmwCFfLU7PpV 8fNsB5M1aTeyhyoagO/TIHXoLmXwcML+DHsh4= 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=p0n4GYoGdVuD6ag/A8hLDe9e8xGF6rYG6bYzbxH7S6A=; b=WpPQbmuE+k0cIPWTY5ZUD4Rwnrm37xLi7q5d1U1DIf+YLZhl3z/GBqJ4k9cpCnhYYz +VsY3gMiExOwvQS2YvtUYCMwZw8c2bTXvWy4uixYETfuHqZhUBIABpwHRRG7lOPb7AXY Kxgs4d67LyUK9/rrqRiV+PGJhZKS1QLetUhdKyoqiQx44EdTLnyE72drTr2wpTrUZt9c CKIrGediFRYnOELOFgxrxFASwHpjulcJ05qj4hq2G+539MGMFOlxFeRkZn5netzWEqCC Q1fE6Z7zgh4rZYzHK3kl8eq3KeQo4P8lJjJxGlJV4XSPYEqWtPRFuF9d6Mhkn4LrB2iM OV6Q== X-Gm-Message-State: ANoB5pkI0ntMtRQL8kRCD5CqbJq+zKsvKjmOODI6uYl3fLMccC26nDfj IJMNSGzbk/NlLxktkFpyfxugEnH8MsaVBlUK X-Google-Smtp-Source: AA0mqf4+L6fQZKcllU5mA0Jy3gl4jtCd7MbtQ3v9ubLNtkbrXDS14Q17mLq1WYPibwU9AyBJrvcxhQ== X-Received: by 2002:a17:902:7c82:b0:189:760d:c5f2 with SMTP id y2-20020a1709027c8200b00189760dc5f2mr26980885pll.90.1669886869481; Thu, 01 Dec 2022 01:27:49 -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 z9-20020a1709027e8900b00186b8752a78sm3108050pla.80.2022.12.01.01.27.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Dec 2022 01:27:49 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Thu, 1 Dec 2022 09:27:33 +0000 Message-Id: <20221201092733.2042078-7-chenghaoyang@google.com> X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog In-Reply-To: <20221201092733.2042078-1-chenghaoyang@google.com> References: <20221201092733.2042078-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v7 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: Cheng-Hao 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 Reviewed-by: Laurent Pinchart --- 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 10ac4666..15115424 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); }