From 943faad06c5b29d3e7c209b0cb2b38c1896c4682 Mon Sep 17 00:00:00 2001 From: LYQ_YES Date: Fri, 19 Apr 2024 15:06:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ffi=E6=A1=A5=E6=8E=A5?= =?UTF-8?q?=E5=B1=82=E4=BB=A3=E7=A0=81=EF=BC=8C=E4=BB=85=E4=BE=9B=E4=BB=93?= =?UTF-8?q?=E9=A2=89=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: LYQ_YES Change-Id: I5c38afcaa446a3505ece05731dde5dc9d01f0a7c Signed-off-by: LYQ_YES --- bundle.json | 1 + interfaces/kits/cj/BUILD.gn | 18 ++ interfaces/kits/cj/color_manager/BUILD.gn | 62 +++++++ .../cj/color_manager/cj_color_manager.cpp | 100 ++++++++++ .../kits/cj/color_manager/cj_color_manager.h | 53 ++++++ .../color_manager/cj_color_manager_mock.cpp | 22 +++ .../cj/color_manager/cj_color_mgr_utils.cpp | 29 +++ .../cj/color_manager/cj_color_mgr_utils.h | 171 ++++++++++++++++++ .../kits/cj/color_manager/color_mgr_ffi.cpp | 89 +++++++++ .../kits/cj/color_manager/color_mgr_ffi.h | 32 ++++ 10 files changed, 577 insertions(+) create mode 100644 interfaces/kits/cj/BUILD.gn create mode 100644 interfaces/kits/cj/color_manager/BUILD.gn create mode 100644 interfaces/kits/cj/color_manager/cj_color_manager.cpp create mode 100644 interfaces/kits/cj/color_manager/cj_color_manager.h create mode 100644 interfaces/kits/cj/color_manager/cj_color_manager_mock.cpp create mode 100644 interfaces/kits/cj/color_manager/cj_color_mgr_utils.cpp create mode 100644 interfaces/kits/cj/color_manager/cj_color_mgr_utils.h create mode 100644 interfaces/kits/cj/color_manager/color_mgr_ffi.cpp create mode 100644 interfaces/kits/cj/color_manager/color_mgr_ffi.h diff --git a/bundle.json b/bundle.json index 0926d958f4..347eabb265 100644 --- a/bundle.json +++ b/bundle.json @@ -115,6 +115,7 @@ "//third_party/libpng:libpng", "//third_party/vulkan-loader:vulkan_loader", "//foundation/graphic/graphic_2d/interfaces/kits/napi:napi_packages", + "//foundation/graphic/graphic_2d/interfaces/kits/cj:ffi_packages", "//foundation/graphic/graphic_2d/rosen/modules/composer:libcomposer", "//foundation/graphic/graphic_2d/rosen/modules/composer/native_vsync:libnative_vsync", "//foundation/graphic/graphic_2d/rosen/modules/2d_graphics:2d_graphics_packages", diff --git a/interfaces/kits/cj/BUILD.gn b/interfaces/kits/cj/BUILD.gn new file mode 100644 index 0000000000..1071830358 --- /dev/null +++ b/interfaces/kits/cj/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +group("ffi_packages") { + deps = [ "color_manager:cj_color_manager_ffi" ] +} diff --git a/interfaces/kits/cj/color_manager/BUILD.gn b/interfaces/kits/cj/color_manager/BUILD.gn new file mode 100644 index 0000000000..0fae7ea66f --- /dev/null +++ b/interfaces/kits/cj/color_manager/BUILD.gn @@ -0,0 +1,62 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//foundation/graphic/graphic_2d/graphic_config.gni") + +ohos_shared_library("cj_color_manager_ffi") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + include_dirs = [ + "$graphic_2d_root/interfaces/inner_api/color_manager", + "$graphic_2d_root/interfaces/kits/napi/graphic/color_manager/color_space_object_convertor", + "$graphic_2d_root/utils/color_manager/export", + ] + + if (!defined(defines)) { + defines = [] + } + + if (product_name != "ohos-sdk") { + deps = [ "$graphic_2d_root/utils/color_manager:color_manager" ] + external_deps = [ + "hilog:libhilog", + "napi:ace_napi", + "napi:cj_bind_ffi", + "napi:cj_bind_native", + ] + sources = [ + "cj_color_manager.cpp", + "cj_color_mgr_utils.cpp", + "color_mgr_ffi.cpp", + ] + } else { + defines += [ "PREVIEWER" ] + sources = [ "cj_color_manager_mock.cpp" ] + } + + if (current_os == "ohos") { + defines += [ "OHOS_PLATFORM" ] + } + + if (current_os == "mingw") { + defines += [ "WINDOWS_PLATFORM" ] + } + + subsystem_name = "graphic" + part_name = "graphic_2d" +} diff --git a/interfaces/kits/cj/color_manager/cj_color_manager.cpp b/interfaces/kits/cj/color_manager/cj_color_manager.cpp new file mode 100644 index 0000000000..2334db8239 --- /dev/null +++ b/interfaces/kits/cj/color_manager/cj_color_manager.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cj_color_manager.h" + +namespace OHOS { +namespace ColorManager { +CjColorManager::CjColorManager(std::shared_ptr ptr) +{ + colorSpaceToken_ = ptr; +} + +std::tuple> CjColorManager::create(ApiColorSpaceType csType) +{ + if (csType == ApiColorSpaceType::UNKNOWN || csType == ApiColorSpaceType::CUSTOM) { + CMLOGE("[CJ]ColorSpaceType is invalid: %{public}u", csType); + return std::make_tuple(static_cast(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_INVALID_ENUM_USAGE)), + "Parameter value is abnormal. Cannot create color manager object using ApiColorSpaceType 5", + nullptr); + } + std::shared_ptr colorSpace = + std::make_shared(CJ_TO_NATIVE_COLOR_SPACE_NAME_MAP.at(csType)); + if (colorSpace == nullptr) { + return std::make_tuple(static_cast(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)), + "Parameter value is abnormal. Fail to create JsColorSpaceObject with input parameter(s).", + nullptr); + } + CMLOGI("[CJ]OnCreateColorSpace CreateJsColorSpaceObject is called"); + return std::make_tuple(0, "", colorSpace); +} + +std::shared_ptr CjColorManager::create(ColorSpacePrimaries primaries, float gamma, int32_t* errCode) +{ + std::shared_ptr colorSpace = std::make_shared(primaries, gamma); + if (colorSpace == nullptr) { + *errCode = static_cast(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)); + return nullptr; + } + CMLOGI("[CJ]OnCreateColorSpace CreateJsColorSpaceObject is called"); + *errCode = 0; + return colorSpace; +} + +uint32_t CjColorManager::GetColorSpaceName(int32_t* errCode) +{ + if (colorSpaceToken_ == nullptr) { + CMLOGE("[CJ]colorSpaceToken_ is nullptr"); + *errCode = static_cast(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)); + return 0; + } + ColorSpaceName csName = colorSpaceToken_->GetColorSpaceName(); + if (NATIVE_TO_CJ_COLOR_SPACE_TYPE_MAP.count(csName) != 0) { + CMLOGI("[CJ]get color space name %{public}u, api type %{public}u", + csName, NATIVE_TO_CJ_COLOR_SPACE_TYPE_MAP.at(csName)); + *errCode = 0; + return static_cast(NATIVE_TO_CJ_COLOR_SPACE_TYPE_MAP.at(csName)); + } + CMLOGE("[CJ]get color space name %{public}u, but not in api type", csName); + *errCode = static_cast(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_INVALID_PARAM)); + return 0; +} + +std::array CjColorManager::GetWhitePoint(int32_t* errCode) +{ + std::array wp; + if (colorSpaceToken_ == nullptr) { + CMLOGE("[CJ]colorSpaceToken_ is nullptr"); + *errCode = static_cast(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)); + return wp; + } + wp = colorSpaceToken_->GetWhitePoint(); + *errCode = 0; + return wp; +} + +float CjColorManager::GetGamma(int32_t* errCode) +{ + if (colorSpaceToken_ == nullptr) { + CMLOGE("[CJ]colorSpaceToken_ is nullptr"); + *errCode = static_cast(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)); + return 0; + } + float gamma = colorSpaceToken_->GetGamma(); + *errCode = 0; + return gamma; +} +} +} \ No newline at end of file diff --git a/interfaces/kits/cj/color_manager/cj_color_manager.h b/interfaces/kits/cj/color_manager/cj_color_manager.h new file mode 100644 index 0000000000..0fe27eaa4c --- /dev/null +++ b/interfaces/kits/cj/color_manager/cj_color_manager.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_CJ_COLOR_MANAGER_H +#define OHOS_CJ_COLOR_MANAGER_H + +#include "color_space.h" +#include "ffi_remote_data.h" +#include "cj_color_mgr_utils.h" + +namespace OHOS { +namespace ColorManager { +class CjColorManager : public OHOS::FFI::FFIData { +public: + explicit CjColorManager(std::shared_ptr ptr); + static std::tuple> create(ApiColorSpaceType colorSpaceName); + static std::shared_ptr create(ColorSpacePrimaries primaries, float gamma, int32_t* errCode); + inline const std::shared_ptr& GetColorSpaceToken() const + { + return colorSpaceToken_; + } + + uint32_t GetColorSpaceName(int32_t* errCode); + std::array GetWhitePoint(int32_t* errCode); + float GetGamma(int32_t* errCode); + OHOS::FFI::RuntimeType *GetRuntimeType() override { return GetClassType(); } +private: + friend class OHOS::FFI::RuntimeType; + friend class OHOS::FFI::TypeBase; + static OHOS::FFI::RuntimeType *GetClassType() + { + static OHOS::FFI::RuntimeType runtimeType = + OHOS::FFI::RuntimeType::Create("CjColorManager"); + return &runtimeType; + } + std::shared_ptr colorSpaceToken_; +}; +} // namespace ColorManager +} // namespace OHOS + +#endif // OHOS_CJ_COLOR_MANAGER_H \ No newline at end of file diff --git a/interfaces/kits/cj/color_manager/cj_color_manager_mock.cpp b/interfaces/kits/cj/color_manager/cj_color_manager_mock.cpp new file mode 100644 index 0000000000..4e7aa7dbea --- /dev/null +++ b/interfaces/kits/cj/color_manager/cj_color_manager_mock.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +extern "C" { +int FFiOHOSColorMgrCreateByColorSpace = 0; +int FFiOHOSColorMgrCreate = 0; +int FFiOHOSColorMgrGetColorSpaceName = 0; +int FFiOHOSColorMgrGetWhitePoint = 0; +int FFiOHOSColorMgrGetGamma = 0; +} \ No newline at end of file diff --git a/interfaces/kits/cj/color_manager/cj_color_mgr_utils.cpp b/interfaces/kits/cj/color_manager/cj_color_mgr_utils.cpp new file mode 100644 index 0000000000..aa208239c1 --- /dev/null +++ b/interfaces/kits/cj/color_manager/cj_color_mgr_utils.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cj_color_mgr_utils.h" + +char* Utils::MallocCString(const std::string& origin) +{ + if (origin.empty()) { + return nullptr; + } + auto len = origin.length() + 1; + char* res = (char*)malloc(sizeof(char) * len); + if (res == nullptr) { + return nullptr; + } + return std::char_traits::copy(res, origin.c_str(), len); +} \ No newline at end of file diff --git a/interfaces/kits/cj/color_manager/cj_color_mgr_utils.h b/interfaces/kits/cj/color_manager/cj_color_mgr_utils.h new file mode 100644 index 0000000000..be1d8e5715 --- /dev/null +++ b/interfaces/kits/cj/color_manager/cj_color_mgr_utils.h @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_CJ_COLOR_MGR_UTILS_H +#define OHOS_CJ_COLOR_MGR_UTILS_H + +#include +#include +#include +#include +#include +#include "color_space.h" + +#define FFI_EXPORT __attribute__((visibility("default"))) + +namespace OHOS { +namespace ColorManager { +#ifndef TITLE +#define TITLE __func__ +#endif + +constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, 0xD001400, "CjColorSpace"}; +#define CMLOGE(fmt, args...) \ + (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}s: " fmt, TITLE, ##args) +#define CMLOGI(fmt, args...) \ + (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}s: " fmt, TITLE, ##args) + +enum class ApiColorSpaceType : uint32_t { + UNKNOWN = 0, + ADOBE_RGB_1998 = 1, + DCI_P3 = 2, + DISPLAY_P3 = 3, + SRGB = 4, + BT709 = 6, + BT601_EBU = 7, + BT601_SMPTE_C = 8, + BT2020_HLG = 9, + BT2020_PQ = 10, + P3_HLG = 11, + P3_PQ = 12, + ADOBE_RGB_1998_LIMIT = 13, + DISPLAY_P3_LIMIT = 14, + SRGB_LIMIT = 15, + BT709_LIMIT = 16, + BT601_EBU_LIMIT = 17, + BT601_SMPTE_C_LIMIT = 18, + BT2020_HLG_LIMIT = 19, + BT2020_PQ_LIMIT = 20, + P3_HLG_LIMIT = 21, + P3_PQ_LIMIT = 22, + LINEAR_P3 = 23, + LINEAR_SRGB = 24, + LINEAR_BT709 = LINEAR_SRGB, + LINEAR_BT2020 = 25, + DISPLAY_SRGB = SRGB, + DISPLAY_P3_SRGB = DISPLAY_P3, + DISPLAY_P3_HLG = P3_HLG, + DISPLAY_P3_PQ = P3_PQ, + CUSTOM = 5, + TYPE_END = 26 +}; + +const std::map NATIVE_TO_CJ_COLOR_SPACE_TYPE_MAP { + { ColorSpaceName::NONE, ApiColorSpaceType::UNKNOWN }, + { ColorSpaceName::ADOBE_RGB, ApiColorSpaceType::ADOBE_RGB_1998 }, + { ColorSpaceName::DCI_P3, ApiColorSpaceType::DCI_P3 }, + { ColorSpaceName::DISPLAY_P3, ApiColorSpaceType::DISPLAY_P3 }, + { ColorSpaceName::SRGB, ApiColorSpaceType::SRGB }, + { ColorSpaceName::BT709, ApiColorSpaceType::BT709 }, + { ColorSpaceName::BT601_EBU, ApiColorSpaceType::BT601_EBU }, + { ColorSpaceName::BT601_SMPTE_C, ApiColorSpaceType::BT601_SMPTE_C }, + { ColorSpaceName::BT2020_HLG, ApiColorSpaceType::BT2020_HLG }, + { ColorSpaceName::BT2020_PQ, ApiColorSpaceType::BT2020_PQ }, + { ColorSpaceName::P3_HLG, ApiColorSpaceType::P3_HLG }, + { ColorSpaceName::P3_PQ, ApiColorSpaceType::P3_PQ }, + { ColorSpaceName::ADOBE_RGB_LIMIT, ApiColorSpaceType::ADOBE_RGB_1998_LIMIT }, + { ColorSpaceName::DISPLAY_P3_LIMIT, ApiColorSpaceType::DISPLAY_P3_LIMIT }, + { ColorSpaceName::SRGB_LIMIT, ApiColorSpaceType::SRGB_LIMIT }, + { ColorSpaceName::BT709_LIMIT, ApiColorSpaceType::BT709_LIMIT }, + { ColorSpaceName::BT601_EBU_LIMIT, ApiColorSpaceType::BT601_EBU_LIMIT }, + { ColorSpaceName::BT601_SMPTE_C_LIMIT, ApiColorSpaceType::BT601_SMPTE_C_LIMIT }, + { ColorSpaceName::BT2020_HLG_LIMIT, ApiColorSpaceType::BT2020_HLG_LIMIT }, + { ColorSpaceName::BT2020_PQ_LIMIT, ApiColorSpaceType::BT2020_PQ_LIMIT }, + { ColorSpaceName::P3_HLG_LIMIT, ApiColorSpaceType::P3_HLG_LIMIT }, + { ColorSpaceName::P3_PQ_LIMIT, ApiColorSpaceType::P3_PQ_LIMIT }, + { ColorSpaceName::LINEAR_P3, ApiColorSpaceType::LINEAR_P3 }, + { ColorSpaceName::LINEAR_SRGB, ApiColorSpaceType::LINEAR_SRGB }, + { ColorSpaceName::LINEAR_BT709, ApiColorSpaceType::LINEAR_BT709 }, + { ColorSpaceName::LINEAR_BT2020, ApiColorSpaceType::LINEAR_BT2020 }, + { ColorSpaceName::DISPLAY_SRGB, ApiColorSpaceType::DISPLAY_SRGB }, + { ColorSpaceName::DISPLAY_P3_SRGB, ApiColorSpaceType::DISPLAY_P3_SRGB }, + { ColorSpaceName::DISPLAY_P3_HLG, ApiColorSpaceType::DISPLAY_P3_HLG }, + { ColorSpaceName::DISPLAY_P3_PQ, ApiColorSpaceType::DISPLAY_P3_PQ }, + { ColorSpaceName::CUSTOM, ApiColorSpaceType::CUSTOM }, +}; + +const std::map CJ_TO_NATIVE_COLOR_SPACE_NAME_MAP { + { ApiColorSpaceType::UNKNOWN, ColorSpaceName::NONE }, + { ApiColorSpaceType::ADOBE_RGB_1998, ColorSpaceName::ADOBE_RGB }, + { ApiColorSpaceType::DCI_P3, ColorSpaceName::DCI_P3 }, + { ApiColorSpaceType::DISPLAY_P3, ColorSpaceName::DISPLAY_P3 }, + { ApiColorSpaceType::SRGB, ColorSpaceName::SRGB }, + { ApiColorSpaceType::BT709, ColorSpaceName::BT709 }, + { ApiColorSpaceType::BT601_EBU, ColorSpaceName::BT601_EBU }, + { ApiColorSpaceType::BT601_SMPTE_C, ColorSpaceName::BT601_SMPTE_C }, + { ApiColorSpaceType::BT2020_HLG, ColorSpaceName::BT2020_HLG }, + { ApiColorSpaceType::BT2020_PQ, ColorSpaceName::BT2020_PQ }, + { ApiColorSpaceType::P3_HLG, ColorSpaceName::P3_HLG }, + { ApiColorSpaceType::P3_PQ, ColorSpaceName::P3_PQ }, + { ApiColorSpaceType::ADOBE_RGB_1998_LIMIT, ColorSpaceName::ADOBE_RGB_LIMIT }, + { ApiColorSpaceType::DISPLAY_P3_LIMIT, ColorSpaceName::DISPLAY_P3_LIMIT }, + { ApiColorSpaceType::SRGB_LIMIT, ColorSpaceName::SRGB_LIMIT }, + { ApiColorSpaceType::BT709_LIMIT, ColorSpaceName::BT709_LIMIT }, + { ApiColorSpaceType::BT601_EBU_LIMIT, ColorSpaceName::BT601_EBU_LIMIT }, + { ApiColorSpaceType::BT601_SMPTE_C_LIMIT, ColorSpaceName::BT601_SMPTE_C_LIMIT }, + { ApiColorSpaceType::BT2020_HLG_LIMIT, ColorSpaceName::BT2020_HLG_LIMIT }, + { ApiColorSpaceType::BT2020_PQ_LIMIT, ColorSpaceName::BT2020_PQ_LIMIT }, + { ApiColorSpaceType::P3_HLG_LIMIT, ColorSpaceName::P3_HLG_LIMIT }, + { ApiColorSpaceType::P3_PQ_LIMIT, ColorSpaceName::P3_PQ_LIMIT }, + { ApiColorSpaceType::LINEAR_P3, ColorSpaceName::LINEAR_P3 }, + { ApiColorSpaceType::LINEAR_SRGB, ColorSpaceName::LINEAR_SRGB }, + { ApiColorSpaceType::LINEAR_BT709, ColorSpaceName::LINEAR_BT709 }, + { ApiColorSpaceType::LINEAR_BT2020, ColorSpaceName::LINEAR_BT2020 }, + { ApiColorSpaceType::DISPLAY_SRGB, ColorSpaceName::DISPLAY_SRGB }, + { ApiColorSpaceType::DISPLAY_P3_SRGB, ColorSpaceName::DISPLAY_P3_SRGB }, + { ApiColorSpaceType::DISPLAY_P3_HLG, ColorSpaceName::DISPLAY_P3_HLG }, + { ApiColorSpaceType::DISPLAY_P3_PQ, ColorSpaceName::DISPLAY_P3_PQ }, + { ApiColorSpaceType::CUSTOM, ColorSpaceName::CUSTOM }, +}; + +enum class CMError : int32_t { + CM_OK = 0, + CM_ERROR_NULLPTR, + CM_ERROR_INVALID_PARAM, + CM_ERROR_INVALID_ENUM_USAGE, +}; + +enum class CMErrorCode : int32_t { + CM_OK = 0, + CM_ERROR_NO_PERMISSION = 201, // the value do not change. It is defined on all system + CM_ERROR_INVALID_PARAM = 401, // the value do not change. It is defined on all system + CM_ERROR_DEVICE_NOT_SUPPORT = 801, // the value do not change. It is defined on all system + CM_ERROR_ABNORMAL_PARAM_VALUE = 18600001, // the value do not change. It is defined on color manager system +}; + +const std::map CJ_TO_ERROR_CODE_MAP { + { CMError::CM_OK, CMErrorCode::CM_OK }, + { CMError::CM_ERROR_NULLPTR, CMErrorCode::CM_ERROR_INVALID_PARAM }, + { CMError::CM_ERROR_INVALID_PARAM, CMErrorCode::CM_ERROR_INVALID_PARAM }, + { CMError::CM_ERROR_INVALID_ENUM_USAGE, CMErrorCode::CM_ERROR_ABNORMAL_PARAM_VALUE }, +}; +} +} + +class FFI_EXPORT Utils { +public: + static char* MallocCString(const std::string& origin); +}; +#endif \ No newline at end of file diff --git a/interfaces/kits/cj/color_manager/color_mgr_ffi.cpp b/interfaces/kits/cj/color_manager/color_mgr_ffi.cpp new file mode 100644 index 0000000000..f3e820003b --- /dev/null +++ b/interfaces/kits/cj/color_manager/color_mgr_ffi.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "color_mgr_ffi.h" +#include "cj_color_manager.h" + +using namespace OHOS::FFI; + +namespace OHOS { +namespace ColorManager { +extern "C" { +const int32_t OK = 0; +RetDataCString CJ_ColorMgrCreateByColorSpace(uint32_t colorSpaceName, int64_t* id) +{ + CMLOGI("[ColorMgr] CJ_ColorMgrCreateByColorSpace start"); + RetDataCString ret = { .code = OK, .data = nullptr }; + auto&& [errCode, errMsg, ptr]= CjColorManager::create(ApiColorSpaceType(colorSpaceName)); + ret.code = errCode; + ret.data = ::Utils::MallocCString(errMsg); + if (errCode != OK) { + *id = 0; + return ret; + } + + auto native = FFIData::Create(ptr); + CMLOGI("[ColorMgr] CJ_ColorMgrCreateByColorSpace success"); + *id = native->GetID(); + return ret; +} + +int64_t CJ_ColorMgrCreate(ColorSpacePrimaries primaries, float gamma, int32_t* errCode) +{ + CMLOGI("[ColorMgr] CJ_ColorMgrCreate start"); + std::shared_ptr ptr = CjColorManager::create(primaries, gamma, errCode); + if (*errCode != OK) { + return 0; + } + auto native = FFIData::Create(ptr); + CMLOGI("[ColorMgr] CJ_ColorMgrCreate success"); + return native->GetID(); +} + +uint32_t CJ_ColorMgrGetColorSpaceName(int64_t id, int32_t* errCode) +{ + CMLOGI("[ColorMgr] CJ_ColorMgrGetColorSpaceName start"); + auto native = FFIData::GetData(id); + CMLOGI("[ColorMgr] CJ_ColorMgrGetColorSpaceName success"); + return native->GetColorSpaceName(errCode); +} + +float* CJ_ColorMgrGetWhitePoint(int64_t id, int32_t* errCode) +{ + CMLOGI("[ColorMgr] CJ_ColorMgrGetWhitePoint start"); + auto native = FFIData::GetData(id); + CMLOGI("[ColorMgr] CJ_ColorMgrGetWhitePoint success"); + std::array arr = native->GetWhitePoint(errCode); + float* res = static_cast(malloc(2 * sizeof(float))); + if (res == nullptr) { + return res; + } + for (size_t i = 0; i < DIMES_2; i++) { + CMLOGI("[ColorMgr] CJ_ColorMgrGetWhitePoint process %{public}f", arr[i]); + *(res + i) = arr[i]; + } + return res; +} + +float CJ_ColorMgrGetGamma(int64_t id, int32_t* errCode) +{ + CMLOGI("[ColorMgr] CJ_ColorMgrGetGamma start"); + auto native = FFIData::GetData(id); + CMLOGI("[ColorMgr] CJ_ColorMgrGetGamma success"); + return native->GetGamma(errCode); +} +} +} +} \ No newline at end of file diff --git a/interfaces/kits/cj/color_manager/color_mgr_ffi.h b/interfaces/kits/cj/color_manager/color_mgr_ffi.h new file mode 100644 index 0000000000..b67b76088b --- /dev/null +++ b/interfaces/kits/cj/color_manager/color_mgr_ffi.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CJ_COLOR_MGR_FFI_H +#define CJ_COLOR_MGR_FFI_H + +#include "cj_common_ffi.h" +#include "color_space.h" +#include "cj_color_mgr_utils.h" + +extern "C" { + FFI_EXPORT RetDataCString CJ_ColorMgrCreateByColorSpace(uint32_t colorSpaceName, int64_t* id); + FFI_EXPORT int64_t CJ_ColorMgrCreate(OHOS::ColorManager::ColorSpacePrimaries primaries, + float gamma, int32_t* errCode); + FFI_EXPORT uint32_t CJ_ColorMgrGetColorSpaceName(int64_t id, int32_t* errCode); + FFI_EXPORT float* CJ_ColorMgrGetWhitePoint(int64_t id, int32_t* errCode); + FFI_EXPORT float CJ_ColorMgrGetGamma(int64_t id, int32_t* errCode); +} + +#endif \ No newline at end of file -- Gitee