From patchwork Fri Oct 14 10:36:12 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: 17595 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 C1C37C327C for ; Fri, 14 Oct 2022 10:36:23 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8376C62DBD; Fri, 14 Oct 2022 12:36:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1665743783; bh=BQdZdP5h3dEKLVbksBt8YSucoVWeUyzm+F8vO82kE8I=; 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=uEAtd2E3UB5g4+b6FwE6pPefJ+rFPh6/QJ+ARzSQy2+oKDgzun3zrPxqbphoZe1cg BdHoj2DrdnOlDvK4BIliOjTsA5iRd/eYJ8S0s6yGC9GCtBoAqh5Q3EpCxlAe3hVBLD /DtGk3bY4En8LJN6bW3XRSwBQTxNmWsCp+mjfcLUnxgSJV2jL2lVCYIbfGikm8SZOE pPtPhBwP3Gl8NeoKsBu9ZU4gq9UevYf8ea0mO+OYfaTgMseLhIHFT0EUulyJQJsCpW vCDLhReBb/532cSH6KT9rv0SWay7EPV1gmsqBztWYycDU+BL2XYCo3WkeWELN/FFr5 g8vmwwt8l8wPw== Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 436C862DB2 for ; Fri, 14 Oct 2022 12:36:20 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="K7K1UbD1"; dkim-atps=neutral Received: by mail-pj1-x1030.google.com with SMTP id 70so4552011pjo.4 for ; Fri, 14 Oct 2022 03:36:20 -0700 (PDT) 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=u00/zmljhCbFyVJOd8MnOWM5DsTO44YCqHj/Umyxvi8=; b=K7K1UbD1pAWXrN2BQDj1SXqRbX3Mpr/PvuE8kXMEPWrN3S3Kquckl9jjKNR3txHVPo jwSexoRWEGHs46QgkP5CuFUi3eopj9iSEIl7H65odv5dr9YqcrkJJGBJ/CIddcOXuV0Z cHBuwAHg0Q94I0+LxGqZz9lXVqnb0833IcqKg= 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=u00/zmljhCbFyVJOd8MnOWM5DsTO44YCqHj/Umyxvi8=; b=m9AkP2dZpFnIeklvCuY4V5GNOevQqzisIG+NgP7Zx8GSVHsCkMuyhzFRhqAydNQgDn cehVag1MeVYqAskIK/ChJ8eT1EwjJfKPQoDO2iZbJplPfRuJ5GESYUxq9MQEWhiZdaUV WnSPop1WpOwiNzl4+rzv3Vla9V8DUxb0VqWdttSgUz5hBJlT4w24asizJUs1ENXlaGaf pSIyJRU2QCDdyylJf8LEal1pO5IkNv/y2xEbJso3c4Fce7iivI/XssNT+UqQxd2Ovt+U PfBAZ+VE2ZHKq12QVdKzji5h29bFfwMqNxROiqISesD+4oRtv6Pf3h+5xmEDhBQk6gi9 kq1g== X-Gm-Message-State: ACrzQf25r3DJMioBvymWp7f/hib7cgWeO3UfloYi56ChC76yg2kKEMd7 YEzuVAZ3byxS4dqMlLo022CRCDkUdU8Cmg== X-Google-Smtp-Source: AMsMyM4qlrruLVbOZC95HU4cIXBbkpgavfHhPK+TZ3HlhXKij16En0WTKwHX7hEpjvCn+BWg6NwklQ== X-Received: by 2002:a17:902:c405:b0:181:83e4:490e with SMTP id k5-20020a170902c40500b0018183e4490emr4385886plk.4.1665743778320; Fri, 14 Oct 2022 03:36:18 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (231.137.80.34.bc.googleusercontent.com. [34.80.137.231]) by smtp.gmail.com with ESMTPSA id e15-20020a170902784f00b00178b9c997e5sm1405704pln.138.2022.10.14.03.36.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Oct 2022 03:36:17 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Fri, 14 Oct 2022 10:36:12 +0000 Message-Id: <20221014103612.241629-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog In-Reply-To: <20221014103612.241629-1-chenghaoyang@google.com> References: <20221014103612.241629-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/1] Add fake pipeline handler 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" --- meson_options.txt | 2 +- src/libcamera/pipeline/fake/fake.cpp | 441 ++++++++++++++++++++++++ src/libcamera/pipeline/fake/meson.build | 3 + test/camera/camera_reconfigure.cpp | 2 +- 4 files changed, 446 insertions(+), 2 deletions(-) create mode 100644 src/libcamera/pipeline/fake/fake.cpp create mode 100644 src/libcamera/pipeline/fake/meson.build diff --git a/meson_options.txt b/meson_options.txt index f1d67808..f08dfc5f 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -37,7 +37,7 @@ option('lc-compliance', option('pipelines', type : 'array', - choices : ['ipu3', 'raspberrypi', 'rkisp1', 'simple', 'uvcvideo', 'vimc'], + choices : ['ipu3', 'raspberrypi', 'rkisp1', 'simple', 'uvcvideo', 'vimc', 'fake'], description : 'Select which pipeline handlers to include') option('qcam', diff --git a/src/libcamera/pipeline/fake/fake.cpp b/src/libcamera/pipeline/fake/fake.cpp new file mode 100644 index 00000000..1ee24e3b --- /dev/null +++ b/src/libcamera/pipeline/fake/fake.cpp @@ -0,0 +1,441 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * fake.cpp - Pipeline handler for fake cameras + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "libcamera/internal/camera.h" +#include "libcamera/internal/device_enumerator.h" +#include "libcamera/internal/formats.h" +#include "libcamera/internal/framebuffer.h" +#include "libcamera/internal/media_device.h" +#include "libcamera/internal/pipeline_handler.h" + +namespace libcamera { + +LOG_DEFINE_CATEGORY(Fake) + +uint64_t CurrentTimestamp() +{ + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) { + LOG(Fake, Error) << "Get clock time fails"; + return 0; + } + + return ts.tv_sec * 1'000'000'000LL + ts.tv_nsec; +} + +static const ControlInfoMap::Map FakeControls = { + { &controls::draft::PipelineDepth, ControlInfo(2, 3) }, +}; + +class FakeCameraData : public Camera::Private +{ +public: + struct Resolution { + Size size; + std::vector frame_rates; + std::vector formats; + }; + + FakeCameraData(PipelineHandler *pipe) + : Camera::Private(pipe) + { + } + + std::vector supportedResolutions_; + + Stream outStream_; + Stream vfStream_; + Stream rawStream_; + + bool started_ = false; +}; + +class FakeCameraConfiguration : public CameraConfiguration +{ +public: + static constexpr unsigned int kBufferCount = 4; // 4~6 + static constexpr unsigned int kMaxStreams = 3; + + FakeCameraConfiguration(FakeCameraData *data); + + Status validate() override; + +private: + /* + * The FakeCameraData instance is guaranteed to be valid as long as the + * corresponding Camera instance is valid. In order to borrow a + * reference to the camera data, store a new reference to the camera. + */ + const FakeCameraData *data_; +}; + +class PipelineHandlerFake : public PipelineHandler +{ +public: + PipelineHandlerFake(CameraManager *manager); + + CameraConfiguration *generateConfiguration(Camera *camera, + const StreamRoles &roles) override; + int configure(Camera *camera, CameraConfiguration *config) override; + + int exportFrameBuffers(Camera *camera, Stream *stream, + std::vector> *buffers) override; + + int start(Camera *camera, const ControlList *controls) override; + void stopDevice(Camera *camera) override; + + int queueRequestDevice(Camera *camera, Request *request) override; + + bool match(DeviceEnumerator *enumerator) override; + +private: + FakeCameraData *cameraData(Camera *camera) + { + return static_cast(camera->_d()); + } + + int registerCameras(); + + static bool registered_; +}; + +bool PipelineHandlerFake::registered_ = false; + +FakeCameraConfiguration::FakeCameraConfiguration(FakeCameraData *data) + : CameraConfiguration() +{ + data_ = data; +} + +CameraConfiguration::Status FakeCameraConfiguration::validate() +{ + Status status = Valid; + + if (config_.empty()) + return Invalid; + + /* Cap the number of entries to the available streams. */ + if (config_.size() > kMaxStreams) { + config_.resize(kMaxStreams); + status = Adjusted; + } + + /* + * Validate the requested stream configuration and select the sensor + * format by collecting the maximum RAW stream width and height and + * picking the closest larger match. + * + * If no RAW stream is requested use the one of the largest YUV stream, + * plus margin pixels for the IF and BDS rectangle to downscale. + * + * \todo Clarify the IF and BDS margins requirements. + */ + unsigned int rawCount = 0; + unsigned int yuvCount = 0; + Size rawRequirement; + Size maxYuvSize; + Size rawSize; + + for (const StreamConfiguration &cfg : config_) { + const PixelFormatInfo &info = PixelFormatInfo::info(cfg.pixelFormat); + + if (info.colourEncoding == PixelFormatInfo::ColourEncodingRAW) { + rawCount++; + rawSize = std::max(rawSize, cfg.size); + } else { + yuvCount++; + maxYuvSize = std::max(maxYuvSize, cfg.size); + rawRequirement.expandTo(cfg.size); + } + } + + // TODO: Check the configuration file. + if (rawCount > 1 || yuvCount > 2) { + LOG(Fake, Debug) << "Camera configuration not supported"; + return Invalid; + } + + /* + * Adjust the configurations if needed and assign streams while + * iterating them. + */ + bool mainOutputAvailable = true; + for (unsigned int i = 0; i < config_.size(); ++i) { + const PixelFormatInfo &info = PixelFormatInfo::info(config_[i].pixelFormat); + const StreamConfiguration originalCfg = config_[i]; + StreamConfiguration *cfg = &config_[i]; + + LOG(Fake, Debug) << "Validating stream: " << config_[i].toString(); + + if (info.colourEncoding == PixelFormatInfo::ColourEncodingRAW) { + /* Initialize the RAW stream with the CIO2 configuration. */ + cfg->size = rawSize; + // TODO: check + cfg->pixelFormat = formats::SBGGR10; + cfg->bufferCount = FakeCameraConfiguration::kBufferCount; + cfg->stride = info.stride(cfg->size.width, 0, 64); + cfg->frameSize = info.frameSize(cfg->size, 64); + cfg->setStream(const_cast(&data_->rawStream_)); + + LOG(Fake, Debug) << "Assigned " << cfg->toString() + << " to the raw stream"; + } else { + /* Assign and configure the main and viewfinder outputs. */ + + cfg->pixelFormat = formats::NV12; + cfg->bufferCount = kBufferCount; + cfg->stride = info.stride(cfg->size.width, 0, 1); + cfg->frameSize = info.frameSize(cfg->size, 1); + + /* + * Use the main output stream in case only one stream is + * requested or if the current configuration is the one + * with the maximum YUV output size. + */ + if (mainOutputAvailable && + (originalCfg.size == maxYuvSize || yuvCount == 1)) { + cfg->setStream(const_cast(&data_->outStream_)); + mainOutputAvailable = false; + + LOG(Fake, Debug) << "Assigned " << cfg->toString() + << " to the main output"; + } else { + cfg->setStream(const_cast(&data_->vfStream_)); + + LOG(Fake, Debug) << "Assigned " << cfg->toString() + << " to the viewfinder output"; + } + } + + if (cfg->pixelFormat != originalCfg.pixelFormat || + cfg->size != originalCfg.size) { + LOG(Fake, Debug) + << "Stream " << i << " configuration adjusted to " + << cfg->toString(); + status = Adjusted; + } + } + + return status; +} + +PipelineHandlerFake::PipelineHandlerFake(CameraManager *manager) + : PipelineHandler(manager) +{ + // TODO: read the fake hal spec file. +} + +CameraConfiguration *PipelineHandlerFake::generateConfiguration(Camera *camera, + const StreamRoles &roles) +{ + FakeCameraData *data = cameraData(camera); + FakeCameraConfiguration *config = new FakeCameraConfiguration(data); + + if (roles.empty()) + return config; + + Size minSize, sensorResolution; + for (const auto& resolution : data->supportedResolutions_) { + if (minSize.isNull() || minSize > resolution.size) + minSize = resolution.size; + + sensorResolution = std::max(sensorResolution, resolution.size); + } + + for (const StreamRole role : roles) { + std::map> streamFormats; + unsigned int bufferCount; + PixelFormat pixelFormat; + Size size; + + switch (role) { + case StreamRole::StillCapture: + size = sensorResolution; + pixelFormat = formats::NV12; + bufferCount = FakeCameraConfiguration::kBufferCount; + streamFormats[pixelFormat] = { { minSize, size } }; + + break; + + case StreamRole::Raw: { + // TODO: check + pixelFormat = formats::SBGGR10; + size = sensorResolution; + bufferCount = FakeCameraConfiguration::kBufferCount; + streamFormats[pixelFormat] = { { minSize, size } }; + + break; + } + + case StreamRole::Viewfinder: + case StreamRole::VideoRecording: { + /* + * Default viewfinder and videorecording to 1280x720, + * capped to the maximum sensor resolution and aligned + * to the ImgU output constraints. + */ + size = sensorResolution; + pixelFormat = formats::NV12; + bufferCount = FakeCameraConfiguration::kBufferCount; + streamFormats[pixelFormat] = { { minSize, size } }; + + break; + } + + default: + LOG(Fake, Error) + << "Requested stream role not supported: " << role; + delete config; + return nullptr; + } + + StreamFormats formats(streamFormats); + StreamConfiguration cfg(formats); + cfg.size = size; + cfg.pixelFormat = pixelFormat; + cfg.bufferCount = bufferCount; + config->addConfiguration(cfg); + } + + if (config->validate() == CameraConfiguration::Invalid) + return {}; + + return config; +} + +int PipelineHandlerFake::configure(Camera *camera, CameraConfiguration *c) +{ + if (camera || c) + return 0; + return 0; +} + +int PipelineHandlerFake::exportFrameBuffers(Camera *camera, Stream *stream, + std::vector> *buffers) +{ + // Assume it's never called. + LOG(Fake, Fatal) << "exportFrameBuffers should never be called"; + if (camera || stream || buffers) + return -EINVAL; + return -EINVAL; +} + +int PipelineHandlerFake::start(Camera *camera, [[maybe_unused]] const ControlList *controls) +{ + FakeCameraData *data = cameraData(camera); + data->started_ = true; + + return 0; +} + +void PipelineHandlerFake::stopDevice(Camera *camera) +{ + FakeCameraData *data = cameraData(camera); + + data->started_ = false; +} + +int PipelineHandlerFake::queueRequestDevice(Camera *camera, Request *request) +{ + if (!camera) + return -EINVAL; + + for (auto it : request->buffers()) + completeBuffer(request, it.second); + + // TODO: request.metadata() + request->metadata().set(controls::SensorTimestamp, CurrentTimestamp()); + completeRequest(request); + + return 0; +} + +bool PipelineHandlerFake::match(DeviceEnumerator *enumerator) +{ + // TODO: exhaust all devices in |enumerator|. + if (!enumerator) + LOG(Fake, Info) << "Invalid enumerator"; + + if (registered_) + return false; + + registered_ = true; + return registerCameras() == 0; +} + +/** + * \brief Initialise ImgU and CIO2 devices associated with cameras + * + * Initialise the two ImgU instances and create cameras with an associated + * CIO2 device instance. + * + * \return 0 on success or a negative error code for error or if no camera + * has been created + * \retval -ENODEV no camera has been created + */ +int PipelineHandlerFake::registerCameras() +{ + std::unique_ptr data = + std::make_unique(this); + std::set streams = { + &data->outStream_, + &data->vfStream_, + &data->rawStream_, + }; + + // TODO: Read from config or from IPC. + // TODO: Check with Han-lin: Can this function be called more than once? + data->supportedResolutions_.resize(2); + data->supportedResolutions_[0].size = Size(1920, 1080); + data->supportedResolutions_[0].frame_rates.push_back(30); + data->supportedResolutions_[0].formats.push_back(formats::NV12); + data->supportedResolutions_[0].formats.push_back(formats::MJPEG); + data->supportedResolutions_[1].size = Size(1280, 720); + data->supportedResolutions_[1].frame_rates.push_back(30); + data->supportedResolutions_[1].frame_rates.push_back(60); + data->supportedResolutions_[1].formats.push_back(formats::NV12); + data->supportedResolutions_[1].formats.push_back(formats::MJPEG); + + // TODO: Assign different locations for different cameras based on config. + data->properties_.set(properties::Location, properties::CameraLocationFront); + data->properties_.set(properties::PixelArrayActiveAreas, { Rectangle(Size(1920, 1080)) }); + + // TODO: Set FrameDurationLimits based on config. + ControlInfoMap::Map controls = FakeControls; + int64_t min_frame_duration = 30, max_frame_duration = 60; + controls[&controls::FrameDurationLimits] = ControlInfo(min_frame_duration, max_frame_duration); + data->controlInfo_ = ControlInfoMap(std::move(controls), controls::controls); + + std::shared_ptr camera = + Camera::create(std::move(data), "\\_SB_.PCI0.I2C4.CAM1" /* cameraId */, streams); + + manager_->addCamera(std::move(camera), {}); + + return 0; +} + +REGISTER_PIPELINE_HANDLER(PipelineHandlerFake) + +} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/fake/meson.build b/src/libcamera/pipeline/fake/meson.build new file mode 100644 index 00000000..13980b4a --- /dev/null +++ b/src/libcamera/pipeline/fake/meson.build @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: CC0-1.0 + +libcamera_sources += files([ 'fake.cpp' ]) diff --git a/test/camera/camera_reconfigure.cpp b/test/camera/camera_reconfigure.cpp index d12e2413..06c87730 100644 --- a/test/camera/camera_reconfigure.cpp +++ b/test/camera/camera_reconfigure.cpp @@ -98,7 +98,7 @@ private: return TestFail; } - requests_.push_back(move(request)); + requests_.push_back(std::move(request)); } camera_->requestCompleted.connect(this, &CameraReconfigure::requestComplete);