1 /*
2 * Copyright (c) 2021 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 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "ipp_algo_parser.h"
18 #include "hcs_dm_parser.h"
19 #include <cstdlib>
20
21 #define ENUM_TO_STRING(e) #e
22
23 namespace OHOS::Camera {
24 std::map<std::string, int> IppAlgoParser::algoModeMap_ = {
25 {ENUM_TO_STRING(IPP_ALGO_MODE_NORMAL), IPP_ALGO_MODE_NORMAL},
26 {ENUM_TO_STRING(IPP_ALGO_MODE_BEAUTY), IPP_ALGO_MODE_BEAUTY},
27 {ENUM_TO_STRING(IPP_ALGO_MODE_HDR), IPP_ALGO_MODE_HDR},
28 };
29
IppAlgoParser(const std::string & pathName)30 IppAlgoParser::IppAlgoParser(const std::string& pathName)
31 {
32 pathName_ = pathName;
33 }
34
~IppAlgoParser()35 IppAlgoParser::~IppAlgoParser()
36 {
37 ReleaseHcsTree();
38 devResInstance_ = nullptr;
39 rootNode_ = nullptr;
40 }
41
Init()42 RetCode IppAlgoParser::Init()
43 {
44 ReleaseHcsTree();
45 devResInstance_ = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
46 if (devResInstance_ == nullptr) {
47 CAMERA_LOGE("get hcs interface failed.");
48 return RC_ERROR;
49 }
50
51 CAMERA_LOGI("pathname = %{public}s", pathName_.c_str());
52 SetHcsBlobPath(pathName_.c_str());
53 rootNode_ = devResInstance_->GetRootNode();
54 if (rootNode_ == nullptr) {
55 CAMERA_LOGE("GetRootNode failed");
56 return RC_ERROR;
57 }
58 if (rootNode_->name != nullptr) {
59 CAMERA_LOGI("rootNode = %{public}s", rootNode_->name);
60 }
61
62 return RC_OK;
63 }
64
ConstructPluginByHcsData()65 AlgoPluginList IppAlgoParser::ConstructPluginByHcsData()
66 {
67 const DeviceResourceNode* ippAlgoConfig = devResInstance_->GetChildNode(rootNode_, "ipp_algo_config");
68 if (ippAlgoConfig == nullptr) {
69 return {};
70 }
71 if (ippAlgoConfig->name != nullptr) {
72 CAMERA_LOGI("config name = %{public}s", ippAlgoConfig->name);
73 }
74
75 AlgoPluginList pluginList = {};
76 const struct DeviceResourceNode* childNodeTmp = nullptr;
77 DEV_RES_NODE_FOR_EACH_CHILD_NODE(ippAlgoConfig, childNodeTmp)
78 {
79 if (childNodeTmp == nullptr || childNodeTmp->name == nullptr) {
80 CAMERA_LOGD("param is null");
81 return {};
82 }
83 std::string nodeName = std::string(childNodeTmp->name);
84 CAMERA_LOGI("subnode name = %{public}s", nodeName.c_str());
85 std::shared_ptr<AlgoPlugin> plugin = ConstructPlugin(childNodeTmp);
86 if (plugin != nullptr) {
87 pluginList.emplace_back(plugin);
88 }
89 }
90
91 return pluginList;
92 }
93
ConstructPlugin(const DeviceResourceNode * node)94 std::shared_ptr<AlgoPlugin> IppAlgoParser::ConstructPlugin(const DeviceResourceNode* node)
95 {
96 const char* name = nullptr;
97 int32_t ret = devResInstance_->GetString(node, "name", &name, nullptr);
98 if (ret != 0) {
99 CAMERA_LOGE("get algo name failed");
100 return nullptr;
101 }
102 const char* description = nullptr;
103 ret = devResInstance_->GetString(node, "description", &description, nullptr);
104 if (ret != 0) {
105 CAMERA_LOGE("get algo description failed");
106 return nullptr;
107 }
108 const char* path = nullptr;
109 ret = devResInstance_->GetString(node, "path", &path, nullptr);
110 if (ret != 0) {
111 CAMERA_LOGE("get algo path failed");
112 return nullptr;
113 }
114
115 const char* mode = nullptr;
116 ret = devResInstance_->GetString(node, "mode", &mode, nullptr);
117 if (ret != 0) {
118 CAMERA_LOGE("get algo mode failed");
119 return nullptr;
120 }
121 auto it = algoModeMap_.find(std::string(mode));
122 if (it == algoModeMap_.end()) {
123 CAMERA_LOGE("invalid mode %{public}s", mode);
124 return nullptr;
125 }
126
127 std::string nodeName = std::string(path);
128 if (path[0] != '/') {
129 nodeName = nodeName.insert(0, HDF_LIBRARY_DIR "/");
130 }
131 std::shared_ptr<AlgoPlugin> plugin = std::make_shared<AlgoPlugin>(name, description, algoModeMap_[mode], nodeName);
132 if (plugin == nullptr) {
133 CAMERA_LOGE("create plugin failed");
134 return nullptr;
135 }
136
137 return plugin;
138 }
139 } // namespace OHOS::Camera
140