From patchwork Mon May 25 17:35:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kaaira Gupta X-Patchwork-Id: 3853 Return-Path: Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A2240603D4 for ; Mon, 25 May 2020 19:35:48 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=es-iitr-ac-in.20150623.gappssmtp.com header.i=@es-iitr-ac-in.20150623.gappssmtp.com header.b="FMZ3RJWi"; dkim-atps=neutral Received: by mail-pf1-x42d.google.com with SMTP id x13so9033517pfn.11 for ; Mon, 25 May 2020 10:35:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=es-iitr-ac-in.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=7XroT1pD2HdjQKyPTxfnK8yjAt7J6AFqtTZSKwOcPL4=; b=FMZ3RJWiIy/p8OhpBgjRHrWCa8ZrDrssa7nTcSA4e+tpPEARX7cfYLzWwIglfGO7J4 V5qL1tKZU5tTXOoc3sXXl+PSgLVaKXIFySDTuiK3Zn/2QK8HKl+eXPEDgowXkAQjZgDk kkqCk5IMn8C68JbfpKch6ArfoIGWvRDZCaVSiIJBID/ASMOD+hBCWthpMs/NiSLp81m1 FHF4YUi4R0mxM/ACvndNLjQlCKuHMGQMzYjlDOYtdFtmNtikMjzUFakCQSr1GCD5wJul HWZASloVvfNXBjRRXhwQA88MnCRjvggeA2fdvhwzl5jN5fzptCeSiOYllygG+oFmqEQG I55w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=7XroT1pD2HdjQKyPTxfnK8yjAt7J6AFqtTZSKwOcPL4=; b=cXmBV+PVuNh7AApkKzRnu8+sD1o75WpJ3pGu2BVpe5NvcG/SBaywlY5xpnFWTFUW8W Ahu9UDedRJ1NwpFeHCFqXHg/jiQJV6atvgSZa1wFYjFlK06CFV6eCEZH9aD+gKV35Rvq GCN6po9Fznyw73DR+P4cNVZm6acvqPldHYE7oak+hhbUNus6YXrX6RPDB5XnewzvtAg7 UCotfyj/RDflTEECCeJIxJRqCzEhvs0FFN9sRJsYYLwzF3J8uM8dTc3mDxchR37AphQk KTgqzVxYH+5ZXSCCW+Ot2OpzZfW0NzfxCzPLl5TqUU8+FkuPU0aYi3S1QNeP2ZG4ruPj O0Kg== X-Gm-Message-State: AOAM530siD0q6ZRkWFzBJuATYaoUkKv8jDqUKKWuvbj/U0iWG7NtwM6a 9sDXDF4hpuNa7dgv+MHwsdDGN3gu6Zngyg== X-Google-Smtp-Source: ABdhPJyuQ8SxExAX/KqznDbvbEWDlzaKD8tQUxMHWXDq/tFctZkSC/QkB12Aq/VS3JCh7Sa/wLZNbg== X-Received: by 2002:a62:6404:: with SMTP id y4mr17311901pfb.64.1590428146433; Mon, 25 May 2020 10:35:46 -0700 (PDT) Received: from kaaira-HP-Pavilion-Notebook ([103.113.213.155]) by smtp.gmail.com with ESMTPSA id mn19sm13179907pjb.8.2020.05.25.10.35.44 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 25 May 2020 10:35:45 -0700 (PDT) Date: Mon, 25 May 2020 23:05:40 +0530 From: Kaaira Gupta To: libcamera-devel@lists.libcamera.org, Kieran Bingham Message-ID: <20200525173540.GA21069@kaaira-HP-Pavilion-Notebook> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) Subject: [libcamera-devel] [PATCH v3] libcamera: PixelFormat: Replace hex with fourcc and modifiers 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-List-Received-Date: Mon, 25 May 2020 17:35:49 -0000 Print fourCC characters instead of the hex values in toString() as they are easier to comprehend. Also, print the corresponding modifiers of the DRM formats so that it can be more specific. Describe the modifiers for MIPI vendor as it is mostly used in libcamera. Print digits for the rest. Signed-off-by: Kaaira Gupta --- Changes since v2: - Remove description for all vendors except for MIPI - Change commit message to reflect this change. - Change tests accordingly. Changes since v1: - Replaced magic numbers with expressive values. - Re-wrote ARM vendor's modifiers - Re-wrote the vendors' map with a macro. - Changed the copyrights in test file. - Changed the tests. src/libcamera/pixelformats.cpp | 65 ++++++++++++++++++++++++++++++++-- test/meson.build | 1 + test/pixel-format.cpp | 47 ++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 test/pixel-format.cpp diff --git a/src/libcamera/pixelformats.cpp b/src/libcamera/pixelformats.cpp index 1330dc5..e16f5ba 100644 --- a/src/libcamera/pixelformats.cpp +++ b/src/libcamera/pixelformats.cpp @@ -6,6 +6,11 @@ */ #include +#include +#include + +#define PIXELFORMAT_VENDOR(vendor) \ + { DRM_FORMAT_MOD_VENDOR_## vendor, #vendor } /** * \file pixelformats.h @@ -108,9 +113,63 @@ bool PixelFormat::operator<(const PixelFormat &other) const */ std::string PixelFormat::toString() const { - char str[11]; - snprintf(str, 11, "0x%08x", fourcc_); - return str; + if (fourcc_ == DRM_FORMAT_INVALID) + return ""; + + char ss[8] = { static_cast(fourcc_ & 0x7f), + static_cast((fourcc_ >> 8) & 0x7f), + static_cast((fourcc_ >> 16) & 0x7f), + static_cast((fourcc_ >> 24) & 0x7f) }; + + for (unsigned int i = 0; i < 4; i++) { + if (!isprint(ss[i])) + ss[i] = '.'; + } + + if (fourcc_ & (1 << 31)) + strcat(ss, "-BE"); + + std::string modifier; + + if (modifier_ == DRM_FORMAT_MOD_INVALID) { + modifier = " - modifier"; + return ss + modifier; + } + + /* Map vendors with their Ids: */ + std::map vendors = { + PIXELFORMAT_VENDOR(NONE), + PIXELFORMAT_VENDOR(MIPI) + }; + + /* Get the vendor name using its Id */ + long int vendorCode = (modifier_ >> 56) & 0xff; + std::string vendor; + std::string vendorSpecification; + + switch (vendorCode) { + case DRM_FORMAT_MOD_VENDOR_NONE: { + vendor = vendors[vendorCode]; + if (modifier_ == DRM_FORMAT_MOD_LINEAR) + vendorSpecification = "Linear Layout"; + break; + } + case DRM_FORMAT_MOD_VENDOR_MIPI: { + vendor = vendors[vendorCode]; + if (modifier_ == MIPI_FORMAT_MOD_CSI2_PACKED) + vendorSpecification = "CSI-2 packed"; + break; + } + default: { + vendor = std::to_string(vendorCode); + vendorSpecification = std::to_string(modifier_ & 0xff); + } + } + + modifier = vendor + " : " + vendorSpecification; + std::string formatString(ss); + + return formatString + " - " + modifier; } } /* namespace libcamera */ diff --git a/test/meson.build b/test/meson.build index bd7da14..591d848 100644 --- a/test/meson.build +++ b/test/meson.build @@ -33,6 +33,7 @@ internal_tests = [ ['message', 'message.cpp'], ['object', 'object.cpp'], ['object-invoke', 'object-invoke.cpp'], + ['pixel-format', 'pixel-format.cpp'], ['signal-threads', 'signal-threads.cpp'], ['threads', 'threads.cpp'], ['timer', 'timer.cpp'], diff --git a/test/pixel-format.cpp b/test/pixel-format.cpp new file mode 100644 index 0000000..edada56 --- /dev/null +++ b/test/pixel-format.cpp @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Kaaira Gupta + * libcamera pixel format handling test + */ + +#include +#include + +#include "libcamera/pixelformats.h" + +#include "test.h" + +using namespace std; +using namespace libcamera; + +class PixelFormatTest : public Test +{ +protected: + int run() + { + std::vector> formats{ + { PixelFormat(DRM_FORMAT_SRGGB8, DRM_FORMAT_MOD_INVALID), "RGGB - modifier" }, + { PixelFormat(DRM_FORMAT_SRGGB8, DRM_FORMAT_MOD_LINEAR), "RGGB - NONE : Linear Layout" }, + { PixelFormat(DRM_FORMAT_C8, DRM_FORMAT_MOD_SAMSUNG_64_32_TILE ), "C8 - 4 : 1" }, + { PixelFormat(DRM_FORMAT_BIG_ENDIAN,MIPI_FORMAT_MOD_CSI2_PACKED),"....-BE - MIPI : CSI-2 packed"} + }; + for (const auto &format : formats) { + if ((format.first).toString() != format.second) { + cerr << "Failed to convert PixelFormat " + << format.first.fourcc() << " to string" + << endl; + return TestFail; + } + } + + if (PixelFormat().toString() != "") { + cerr << "Failed to convert default PixelFormat to string" + << endl; + return TestFail; + } + + return TestPass; + } +}; + +TEST_REGISTER(PixelFormatTest)