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 #include "filter_factory.h"
17 #include "ec_log.h"
18 #include "builder.h"
19
20 namespace OHOS {
21 namespace Rosen {
CreateFromConfig(std::string path)22 ImageChain* Builder::CreateFromConfig(std::string path)
23 {
24 char newpath[PATH_MAX + 1] = { 0x00 };
25 if (strlen(path.c_str()) > PATH_MAX || realpath(path.c_str(), newpath) == NULL) {
26 return nullptr;
27 }
28
29 std::ifstream configFile;
30 configFile.open(newpath);
31 std::stringstream JFilterParamsStream;
32 JFilterParamsStream << configFile.rdbuf();
33 configFile.close();
34 std::string JFilterParamsString = JFilterParamsStream.str();
35 cJSON* overallData = cJSON_Parse(JFilterParamsString.c_str());
36 if (overallData != nullptr) {
37 cJSON* filters_ = cJSON_GetObjectItem(overallData, "filters");
38 if (filters_ != nullptr) {
39 AnalyseFilters(filters_);
40 }
41 cJSON* connections_ = cJSON_GetObjectItem(overallData, "connections");
42 if (connections_ != nullptr) {
43 ConnectPipeline(connections_);
44 }
45 cJSON_Delete(overallData);
46 } else {
47 LOGE("The json file fails to compile.");
48 return nullptr;
49 }
50 if (inputs_.size() != 0) {
51 return new ImageChain(inputs_);
52 } else {
53 LOGE("No input.");
54 return nullptr;
55 }
56 }
57
AnalyseFilters(cJSON * filters)58 void Builder::AnalyseFilters(cJSON* filters)
59 {
60 int size = cJSON_GetArraySize(filters);
61 for (int i = 0; i < size; i++) {
62 cJSON* item = cJSON_GetArrayItem(filters, i);
63 cJSON* type = cJSON_GetObjectItem(item, "type");
64 cJSON* name = cJSON_GetObjectItem(item, "name");
65 cJSON* params = cJSON_GetObjectItem(item, "params");
66 if (type != nullptr && name != nullptr && type->valuestring != nullptr && name->valuestring != nullptr) {
67 nameType_[name->valuestring] = type->valuestring;
68 auto tempFilter = filterFactory->GetFilter(type->valuestring);
69 if (tempFilter != nullptr) {
70 ParseParams(tempFilter, params);
71 nameFilter_[name->valuestring] = tempFilter;
72 }
73 }
74 }
75 }
76
ParseParams(std::shared_ptr<Filter> filter,cJSON * params)77 void Builder::ParseParams(std::shared_ptr<Filter> filter, cJSON* params)
78 {
79 if (params == nullptr) {
80 return;
81 }
82 auto childParam = params->child;
83 while (childParam != nullptr) {
84 if (cJSON_IsArray(childParam)) {
85 ParseArray(filter, childParam);
86 } else if (cJSON_IsNumber(childParam)) {
87 std::shared_ptr<float> tempValue = std::make_shared<float>(childParam->valuedouble);
88 filter->SetValue(childParam->string, tempValue, 1);
89 } else if (cJSON_IsString(childParam)) {
90 std::string tempString = childParam->valuestring;
91 std::shared_ptr<std::string> tempValue = std::make_shared<std::string>(tempString);
92 filter->SetValue(childParam->string, tempValue, 1);
93 } else {
94 LOGE("Invalid input parameters!");
95 }
96 childParam = childParam->next;
97 }
98 }
99
ParseArray(std::shared_ptr<Filter> filter,cJSON * childParam)100 void Builder::ParseArray(std::shared_ptr<Filter> filter, cJSON* childParam)
101 {
102 if (filter == nullptr || childParam == nullptr) {
103 return;
104 }
105 int arrayLength = cJSON_GetArraySize(childParam);
106 std::shared_ptr<std::vector<float>> tempArray = std::make_shared<std::vector<float>>(arrayLength, 0);
107 for (int i = 0; i < arrayLength; i++) {
108 cJSON* arrayItem = cJSON_GetArrayItem(childParam, i);
109 if (cJSON_IsNumber(arrayItem)) {
110 (*tempArray.get())[i] = arrayItem->valuedouble;
111 }
112 }
113 filter->SetValue(childParam->string, tempArray, arrayLength);
114 }
115
ConnectPipeline(cJSON * connections)116 void Builder::ConnectPipeline(cJSON* connections)
117 {
118 int size = cJSON_GetArraySize(connections);
119 for (int i = 0; i < size; i++) {
120 cJSON* item = cJSON_GetArrayItem(connections, i);
121 cJSON* from = cJSON_GetObjectItem(item, "from");
122 cJSON* to = cJSON_GetObjectItem(item, "to");
123 std::shared_ptr<Filter> fFilter = nullptr;
124 std::shared_ptr<Filter> tFilter = nullptr;
125 if (from != nullptr && to != nullptr && from->valuestring != nullptr && to->valuestring != nullptr) {
126 auto itFrom = nameFilter_.find(from->valuestring);
127 if (itFrom != nameFilter_.end()) {
128 fFilter = itFrom->second;
129 if (fFilter->GetFilterType() == FILTER_TYPE::INPUT) {
130 inputs_.push_back(std::static_pointer_cast<Input>(fFilter));
131 }
132 } else {
133 LOGE("The from filter %{public}s fails to be connected", from->valuestring);
134 }
135 auto itTo = nameFilter_.find(to->valuestring);
136 if (itTo != nameFilter_.end()) {
137 tFilter = itTo->second;
138 } else {
139 LOGE("The to filter %{public}s fails to be connected", to->valuestring);
140 }
141 if (fFilter != nullptr && tFilter != nullptr) {
142 fFilter->AddNextFilter(tFilter);
143 }
144 }
145 }
146 }
147 } // namespcae Rosen
148 } // namespace OHOS
149