1 /*
2 * Copyright (c) 2024 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 #include "common_utils.h"
17
18 #include <map>
19
20 #include "logging.h"
21
22 const std::map<ImageEffect_Format, const char *> formatToStr_ = {
23 {ImageEffect_Format::EFFECT_PIXEL_FORMAT_RGBA8888, "RGBA8888"},
24 {ImageEffect_Format::EFFECT_PIXEL_FORMAT_NV12, "YUVNV12"},
25 {ImageEffect_Format::EFFECT_PIXEL_FORMAT_NV21, "YUVNV21"},
26 };
27
28 const std::map<ImageEffect_BufferType, const char *> bufferTypeToStr_ = {
29 {ImageEffect_BufferType::EFFECT_BUFFER_TYPE_PIXEL, "Pixel"},
30 {ImageEffect_BufferType::EFFECT_BUFFER_TYPE_TEXTURE, "Texture"},
31 };
32
GetStringArgument(napi_env env,napi_value value)33 const char *CommonUtils::GetStringArgument(napi_env env, napi_value value)
34 {
35 char *buffer = nullptr;
36 size_t bufferLength = 0;
37 napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufferLength);
38 if (status == napi_ok && bufferLength > 0) {
39 buffer = reinterpret_cast<char *>(malloc((bufferLength + 1) * sizeof(char)));
40 if (buffer == nullptr) {
41 LOG_E("No memory");
42 return nullptr;
43 }
44
45 status = napi_get_value_string_utf8(env, value, buffer, bufferLength + 1, &bufferLength);
46 if (status != napi_ok) {
47 LOG_E("napi_get_value_string_utf8 fail");
48 free(buffer);
49 buffer = nullptr;
50 }
51 }
52 return buffer;
53 }
54
GetBufferType(ImageEffect_BufferType & bufferType)55 const char *GetBufferType(ImageEffect_BufferType &bufferType)
56 {
57 auto it = bufferTypeToStr_.find(bufferType);
58 if (it == bufferTypeToStr_.end()) {
59 return "unknown";
60 }
61
62 return it->second;
63 }
64
GetFormat(ImageEffect_Format & ohFormat)65 const char *GetFormat(ImageEffect_Format &ohFormat)
66 {
67 auto it = formatToStr_.find(ohFormat);
68 if (it == formatToStr_.end()) {
69 return "unknown";
70 }
71
72 return it->second;
73 }
74
EffectInfoToString(OH_EffectFilterInfo * info)75 std::string CommonUtils::EffectInfoToString(OH_EffectFilterInfo *info)
76 {
77 std::string result = "";
78
79 char *name = nullptr;
80 OH_EffectFilterInfo_GetFilterName(info, &name);
81 result += "name:" + std::string(name) + ", ";
82
83 uint32_t supportedBufferTypesSize = 0;
84 ImageEffect_BufferType *bufferTypeArray = nullptr;
85 OH_EffectFilterInfo_GetSupportedBufferTypes(info, &supportedBufferTypesSize, &bufferTypeArray);
86 result += "supportedBufferType: {";
87 for (uint32_t i = 0;i < supportedBufferTypesSize; ++i) {
88 ImageEffect_BufferType bufferType = bufferTypeArray[i];
89 result += GetBufferType(bufferType) + std::string(" ");
90 }
91 result += "}";
92
93 uint32_t supportedFormatsSize = 0;
94 ImageEffect_Format *formatArray = nullptr;
95 OH_EffectFilterInfo_GetSupportedFormats(info, &supportedFormatsSize, &formatArray);
96 result += "supportedFormat: {";
97 for (uint32_t i = 0;i < supportedFormatsSize; ++i) {
98 ImageEffect_Format ohFormat = formatArray[i];
99 result += GetFormat(ohFormat) + std::string(" ");
100 }
101 result += "}";
102
103 return result;
104 }