• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_JS_COLOR_SPACE_UTILS_H
17 #define OHOS_JS_COLOR_SPACE_UTILS_H
18 
19 #include <map>
20 #include <hilog/log.h>
21 
22 #include "color_space.h"
23 #include "color_manager_common.h"
24 #include "native_engine/native_engine.h"
25 #include "native_engine/native_value.h"
26 
27 namespace OHOS {
28 namespace ColorManager {
29 #ifndef TITLE
30 #define TITLE __func__
31 #endif
32 
33 constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, 0xD001400, "JsColorSpace"};
34 #define CMLOGE(fmt, args...) \
35     (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}s: " fmt, TITLE, ##args)
36 #define CMLOGI(fmt, args...) \
37     (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}s: " fmt, TITLE, ##args)
38 
39 enum class ApiColorSpaceType : uint32_t {
40     UNKNOWN = 0,
41     ADOBE_RGB_1998 = 1,
42     DCI_P3 = 2,
43     DISPLAY_P3 = 3,
44     SRGB = 4,
45     CUSTOM = 5,
46     TYPE_END
47 };
48 
49 const std::map<ColorSpaceName, ApiColorSpaceType> NATIVE_TO_JS_COLOR_SPACE_TYPE_MAP {
50     { ColorSpaceName::NONE, ApiColorSpaceType::UNKNOWN },
51     { ColorSpaceName::ADOBE_RGB, ApiColorSpaceType::ADOBE_RGB_1998 },
52     { ColorSpaceName::DCI_P3, ApiColorSpaceType::DCI_P3 },
53     { ColorSpaceName::DISPLAY_P3, ApiColorSpaceType::DISPLAY_P3 },
54     { ColorSpaceName::SRGB, ApiColorSpaceType::SRGB },
55     { ColorSpaceName::CUSTOM, ApiColorSpaceType::CUSTOM },
56 };
57 
58 const std::map<ApiColorSpaceType, ColorSpaceName> JS_TO_NATIVE_COLOR_SPACE_NAME_MAP {
59     { ApiColorSpaceType::UNKNOWN, ColorSpaceName::NONE },
60     { ApiColorSpaceType::ADOBE_RGB_1998, ColorSpaceName::ADOBE_RGB },
61     { ApiColorSpaceType::DCI_P3, ColorSpaceName::DCI_P3 },
62     { ApiColorSpaceType::DISPLAY_P3, ColorSpaceName::DISPLAY_P3 },
63     { ApiColorSpaceType::SRGB, ColorSpaceName::SRGB },
64     { ApiColorSpaceType::CUSTOM, ColorSpaceName::CUSTOM },
65 };
66 
67 template<class T>
ConvertNativeValueTo(NativeValue * value)68 inline T* ConvertNativeValueTo(NativeValue* value)
69 {
70     return (value != nullptr) ? static_cast<T*>(value->GetInterface(T::INTERFACE_ID)) : nullptr;
71 }
72 
73 template<class T>
74 inline T* CheckParamsAndGetThis(const NativeEngine* engine, NativeCallbackInfo* info, const char* name = nullptr)
75 {
76     if (engine == nullptr || info == nullptr) {
77         return nullptr;
78     }
79     NativeObject* object = ConvertNativeValueTo<NativeObject>(info->thisVar);
80     if (object != nullptr && name != nullptr) {
81         object = ConvertNativeValueTo<NativeObject>(object->GetProperty(name));
82     }
83     return (object != nullptr) ? static_cast<T*>(object->GetNativePointer()) : nullptr;
84 }
85 
86 template<class T>
CreateJsValue(NativeEngine & engine,const T & value)87 NativeValue* CreateJsValue(NativeEngine& engine, const T& value)
88 {
89     using ValueType = std::remove_cv_t<std::remove_reference_t<T>>;
90     if constexpr (std::is_same_v<ValueType, bool>) {
91         return engine.CreateBoolean(value);
92     } else if constexpr (std::is_arithmetic_v<ValueType>) {
93         return engine.CreateNumber(value);
94     } else if constexpr (std::is_same_v<ValueType, std::string>) {
95         return engine.CreateString(value.c_str(), value.length());
96     } else if constexpr (std::is_enum_v<ValueType>) {
97         return engine.CreateNumber(static_cast<std::make_signed_t<ValueType>>(value));
98     } else if constexpr (std::is_same_v<ValueType, const char*>) {
99         return (value != nullptr) ? engine.CreateString(value, strlen(value)) : engine.CreateUndefined();
100     }
101     return engine.CreateUndefined();
102 }
103 
104 template<class T>
ConvertFromJsValue(NativeEngine & engine,NativeValue * jsValue,T & value)105 bool ConvertFromJsValue(NativeEngine& engine, NativeValue* jsValue, T& value)
106 {
107     if (jsValue == nullptr) {
108         return false;
109     }
110 
111     using ValueType = std::remove_cv_t<std::remove_reference_t<T>>;
112     if constexpr (std::is_same_v<ValueType, bool>) {
113         auto boolValue = ConvertNativeValueTo<NativeBoolean>(jsValue);
114         if (boolValue == nullptr) {
115             return false;
116         }
117         value = *boolValue;
118         return true;
119     } else if constexpr (std::is_arithmetic_v<ValueType>) {
120         auto numberValue = ConvertNativeValueTo<NativeNumber>(jsValue);
121         if (numberValue == nullptr) {
122             return false;
123         }
124         value = *numberValue;
125         return true;
126     } else if constexpr (std::is_same_v<ValueType, std::string>) {
127         auto stringValue = ConvertNativeValueTo<NativeString>(jsValue);
128         if (stringValue == nullptr) {
129             return false;
130         }
131         size_t len = stringValue->GetLength() + 1;
132         auto buffer = std::make_unique<char[]>(len);
133         size_t strLength = 0;
134         stringValue->GetCString(buffer.get(), len, &strLength);
135         value = buffer.get();
136         return true;
137     } else if constexpr (std::is_enum_v<ValueType>) {
138         auto numberValue = ConvertNativeValueTo<NativeNumber>(jsValue);
139         if (numberValue == nullptr) {
140             return false;
141         }
142         value = static_cast<ValueType>(static_cast<std::make_signed_t<ValueType>>(*numberValue));
143         return true;
144     }
145     CMLOGE("[NAPI]Failed to ConvertFromJsValue");
146     return false;
147 }
148 
149 NativeValue* CreateJsError(NativeEngine& engine, int32_t errCode, const std::string& message = std::string());
150 void BindNativeFunction(NativeEngine& engine, NativeObject& object, const char* name,
151     const char* moduleName, NativeCallback func);
152 bool CheckParamMinimumValid(NativeEngine& engine, const size_t paramNum, const size_t minNum);
153 
154 NativeValue* ColorSpaceTypeInit(NativeEngine* engine);
155 NativeValue* CMErrorInit(NativeEngine* engine);
156 NativeValue* CMErrorCodeInit(NativeEngine* engine);
157 bool ParseJsDoubleValue(NativeObject* jsObject, NativeEngine& engine, const std::string& name, double& data);
158 }  // namespace ColorManager
159 }  // namespace OHOS
160 #endif // OHOS_JS_COLOR_SPACE_UTILS_H
161