• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #define ENUM_TO_STRING(e) #e
21 
22 namespace OHOS::Camera {
23 std::map<std::string, int> IppAlgoParser::algoModeMap_ = {
24     {ENUM_TO_STRING(IPP_ALGO_MODE_NORMAL), IPP_ALGO_MODE_NORMAL},
25     {ENUM_TO_STRING(IPP_ALGO_MODE_BEAUTY), IPP_ALGO_MODE_BEAUTY},
26     {ENUM_TO_STRING(IPP_ALGO_MODE_HDR), IPP_ALGO_MODE_HDR},
27 };
28 
IppAlgoParser(const std::string & pathName)29 IppAlgoParser::IppAlgoParser(const std::string& pathName)
30 {
31     pathName_ = pathName;
32 }
33 
~IppAlgoParser()34 IppAlgoParser::~IppAlgoParser()
35 {
36     ReleaseHcsTree();
37     devResInstance_ = nullptr;
38     rootNode_ = nullptr;
39 }
40 
Init()41 RetCode IppAlgoParser::Init()
42 {
43     ReleaseHcsTree();
44     devResInstance_ = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
45     if (devResInstance_ == nullptr) {
46         CAMERA_LOGE("get hcs interface failed.");
47         return RC_ERROR;
48     }
49 
50     CAMERA_LOGI("pathname = %{public}s", pathName_.c_str());
51     SetHcsBlobPath(pathName_.c_str());
52     rootNode_ = devResInstance_->GetRootNode();
53     if (rootNode_ == nullptr) {
54         CAMERA_LOGE("GetRootNode failed");
55         return RC_ERROR;
56     }
57     if (rootNode_->name != nullptr) {
58         CAMERA_LOGI("rootNode = %{public}s", rootNode_->name);
59     }
60 
61     return RC_OK;
62 }
63 
ConstructPluginByHcsData()64 AlgoPluginList IppAlgoParser::ConstructPluginByHcsData()
65 {
66     const DeviceResourceNode* ippAlgoConfig = devResInstance_->GetChildNode(rootNode_, "ipp_algo_config");
67     if (ippAlgoConfig == nullptr) {
68         return {};
69     }
70     if (ippAlgoConfig->name != nullptr) {
71         CAMERA_LOGI("config name = %{public}s", ippAlgoConfig->name);
72     }
73 
74     AlgoPluginList pluginList = {};
75     const struct DeviceResourceNode* childNodeTmp = nullptr;
76     DEV_RES_NODE_FOR_EACH_CHILD_NODE(ippAlgoConfig, childNodeTmp)
77     {
78         if (childNodeTmp == nullptr || childNodeTmp->name == nullptr) {
79             CAMERA_LOGD("param is null");
80             return {};
81         }
82         std::string nodeName = std::string(childNodeTmp->name);
83         CAMERA_LOGI("subnode name = %{public}s", nodeName.c_str());
84         std::shared_ptr<AlgoPlugin> plugin = ConstructPlugin(childNodeTmp);
85         if (plugin != nullptr) {
86             pluginList.emplace_back(plugin);
87         }
88     }
89 
90     return pluginList;
91 }
92 
ConstructPlugin(const DeviceResourceNode * node)93 std::shared_ptr<AlgoPlugin> IppAlgoParser::ConstructPlugin(const DeviceResourceNode* node)
94 {
95     const char* name = nullptr;
96     int32_t ret = devResInstance_->GetString(node, "name", &name, nullptr);
97     if (ret != 0) {
98         CAMERA_LOGE("get algo name failed");
99         return nullptr;
100     }
101     const char* description = nullptr;
102     ret = devResInstance_->GetString(node, "description", &description, nullptr);
103     if (ret != 0) {
104         CAMERA_LOGE("get algo description failed");
105         return nullptr;
106     }
107     const char* path = nullptr;
108     ret = devResInstance_->GetString(node, "path", &path, nullptr);
109     if (ret != 0) {
110         CAMERA_LOGE("get algo path failed");
111         return nullptr;
112     }
113 
114     const char* mode = nullptr;
115     ret = devResInstance_->GetString(node, "mode", &mode, nullptr);
116     if (ret != 0) {
117         CAMERA_LOGE("get algo mode failed");
118         return nullptr;
119     }
120     auto it = algoModeMap_.find(std::string(mode));
121     if (it == algoModeMap_.end()) {
122         CAMERA_LOGE("invalid mode %{public}s", mode);
123         return nullptr;
124     }
125 
126     std::string nodeName = std::string(path);
127     if (path[0] != '/') {
128         nodeName = nodeName.insert(0, HDF_LIBRARY_DIR "/");
129     }
130     std::shared_ptr<AlgoPlugin> plugin = std::make_shared<AlgoPlugin>(name, description, algoModeMap_[mode], nodeName);
131     if (plugin == nullptr) {
132         CAMERA_LOGE("create plugin failed");
133         return nullptr;
134     }
135 
136     return plugin;
137 }
138 } // namespace OHOS::Camera
139