• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <chrono>
17 #include "rs_round_corner_config.h"
18 
19 using std::chrono::high_resolution_clock;
20 using std::chrono::microseconds;
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace rs_rcd {
FindNode(const xmlNodePtr & src,const std::string & index)25 xmlNodePtr XMLReader::FindNode(const xmlNodePtr& src, const std::string& index)
26 {
27     xmlNodePtr startPtr = src->children;
28     while (startPtr != nullptr) {
29         auto name = startPtr->name;
30         if (xmlStrEqual(name, BAD_CAST(index.c_str())) == 1) {
31             return startPtr;
32         }
33         startPtr = startPtr->next;
34     }
35     RS_LOGE("[%{public}s] can not found node %{public}s! \n", __func__, index.c_str());
36     return nullptr;
37 }
38 
ReadAttrStr(const xmlNodePtr & src,std::string attr)39 char* XMLReader::ReadAttrStr(const xmlNodePtr& src, std::string attr)
40 {
41     return reinterpret_cast<char*>((xmlGetProp(src, BAD_CAST(attr.c_str()))));
42 }
43 
ReadAttrInt(const xmlNodePtr & src,std::string attr)44 int XMLReader::ReadAttrInt(const xmlNodePtr& src, std::string attr)
45 {
46     auto result = reinterpret_cast<char*>((xmlGetProp(src, BAD_CAST(attr.c_str()))));
47     if (result == nullptr) {
48         RS_LOGE("[%{public}s] can not found attribute %{public}s! \n", __func__, attr.c_str());
49         return 0;
50     }
51     return std::atoi(result);
52 }
53 
ReadAttrFloat(const xmlNodePtr & src,std::string attr)54 float XMLReader::ReadAttrFloat(const xmlNodePtr& src, std::string attr)
55 {
56     auto result = reinterpret_cast<char*>((xmlGetProp(src, BAD_CAST(attr.c_str()))));
57     if (result == nullptr) {
58         RS_LOGE("[%{public}s] can not found attribute %{public}s! \n", __func__, attr.c_str());
59         return 0.0f;
60     }
61     return std::atof(result);
62 }
63 
ReadAttrBool(const xmlNodePtr & src,std::string attr)64 bool XMLReader::ReadAttrBool(const xmlNodePtr& src, std::string attr)
65 {
66     auto result = reinterpret_cast<char*>((xmlGetProp(src, BAD_CAST(attr.c_str()))));
67     if (result == nullptr) {
68         RS_LOGE("[%{public}s] can not found attribute %{public}s! \n", __func__, attr.c_str());
69         return false;
70     }
71     auto istrue = strcmp(result, "true") == 0 ? true : false;
72     return istrue;
73 }
74 
Init(std::string file)75 bool XMLReader::Init(std::string file)
76 {
77     xmlKeepBlanksDefault(0);
78     pdoc = xmlReadFile(file.c_str(), "", XML_PARSE_RECOVER);
79     if (pdoc == nullptr) {
80         RS_LOGE("[%{public}s] can not found the file %{public}s! \n", __func__, file.c_str());
81         return false;
82     }
83     proot = xmlDocGetRootElement(pdoc);
84     if (proot == nullptr) {
85         RS_LOGE("[%{public}s] can not found root node in file %{public}s! \n", __func__, file.c_str());
86         return false;
87     }
88     return true;
89 }
90 
ReadNode(std::vector<std::string> attribute)91 xmlNodePtr XMLReader::ReadNode(std::vector<std::string> attribute)
92 {
93     xmlNodePtr ptr = proot;
94     auto size = static_cast<int>(attribute.size());
95     for (int i = 0; i < size; i++) {
96         ptr = FindNode(ptr, attribute[i]);
97         if (i == static_cast<int>(attribute.size()) - 1) {
98             return ptr;
99         }
100         if (ptr == nullptr) {
101             RS_LOGE("[%{public}s] can not found attribute %{public}s! \n", __func__, attribute[i].c_str());
102             return nullptr;
103         } else {
104             ptr = ptr->children;
105         }
106     }
107     return ptr;
108 }
109 
Read(std::vector<std::string> attribute)110 xmlChar* XMLReader::Read(std::vector<std::string> attribute)
111 {
112     const int attrSizeMin = 2;
113     if (attribute.size() < attrSizeMin) {
114         RS_LOGE("[%{public}s] attribute size less than two! \n", __func__);
115         return nullptr;
116     }
117     std::vector<std::string> nodeIndex(attribute.begin(), attribute.end() - 1);
118     auto lastNode = ReadNode(nodeIndex);
119     if (lastNode != nullptr) {
120         return xmlGetProp(lastNode, BAD_CAST(attribute[attribute.size() - 1].c_str()));
121     }
122     RS_LOGE("[%{public}s] can not found attribute %{public}s! \n", __func__, attribute[attribute.size() - 1].c_str());
123     return nullptr;
124 }
125 
ReadXmlNode(const xmlNodePtr & ptr,std::string supportAttr,std::string modeAttr)126 bool SupportConfig::ReadXmlNode(const xmlNodePtr& ptr, std::string supportAttr, std::string modeAttr)
127 {
128     auto a1 = xmlGetProp(ptr, BAD_CAST(supportAttr.c_str()));
129     if (a1 == nullptr) {
130         RS_LOGE("[%{public}s] can not found supportAttr %{public}s! \n", __func__, supportAttr.c_str());
131         return false;
132     }
133     support = XMLReader::ReadAttrBool(ptr, supportAttr);
134     mode = XMLReader::ReadAttrInt(ptr, modeAttr);
135     return true;
136 }
137 
ReadXmlNode(const xmlNodePtr & ptr,std::vector<std::string> attrArray)138 bool RoundCornerLayer::ReadXmlNode(const xmlNodePtr& ptr, std::vector<std::string> attrArray)
139 {
140     const int layerAttrSize = 7;
141     if (attrArray.size() < layerAttrSize || ptr == nullptr) {
142         RS_LOGE("[%{public}s] attribute size less than 7! \n", __func__);
143         return false;
144     }
145     const int fileIndex = 0;
146     const int offsetXIndex = 1;
147     const int offsetYIndex = 2;
148     const int binFileIndex = 3;
149     const int bufferSizeIndex = 4;
150     const int cldWidthIndex = 5;
151     const int cldHeightIndex = 6;
152     fileName = XMLReader::ReadAttrStr(ptr, attrArray[fileIndex].c_str());
153     offsetX = XMLReader::ReadAttrInt(ptr, attrArray[offsetXIndex].c_str());
154     offsetY = XMLReader::ReadAttrInt(ptr, attrArray[offsetYIndex].c_str());
155     binFileName = XMLReader::ReadAttrStr(ptr, attrArray[binFileIndex].c_str());
156     bufferSize = XMLReader::ReadAttrInt(ptr, attrArray[bufferSizeIndex].c_str());
157     cldWidth = XMLReader::ReadAttrInt(ptr, attrArray[cldWidthIndex].c_str());
158     cldHeight = XMLReader::ReadAttrInt(ptr, attrArray[cldHeightIndex].c_str());
159     return true;
160 }
161 
ReadXmlNode(const xmlNodePtr & portraitNodePtr)162 bool RogPortrait::ReadXmlNode(const xmlNodePtr& portraitNodePtr)
163 {
164     if (portraitNodePtr == nullptr) {
165         RS_LOGE("[%{public}s] input node is null! \n", __func__);
166         return false;
167     }
168     std::vector<std::string> attrList = { ATTR_FILENAME, ATTR_OFFSET_X, ATTR_OFFSET_Y,
169         ATTR_BINFILENAME, ATTR_BUFFERSIZE, ATTR_CLDWIDTH, ATTR_CLDHEIGHT };
170 
171     auto layerUpPtr = XMLReader::FindNode(portraitNodePtr, std::string(NODE_LAYERUP));
172     if (layerUpPtr == nullptr) {
173         RS_LOGE("[%{public}s] layerUpPtr node is null! \n", __func__);
174         return false;
175     }
176     layerUp.ReadXmlNode(layerUpPtr, attrList);
177 
178     auto layerDownPtr = XMLReader::FindNode(portraitNodePtr, std::string(NODE_LAYERDOWN));
179     if (layerDownPtr == nullptr) {
180         RS_LOGE("[%{public}s] layerDownPtr node is null! \n", __func__);
181         return false;
182     }
183     layerDown.ReadXmlNode(layerDownPtr, attrList);
184 
185     auto layerHidePtr = XMLReader::FindNode(portraitNodePtr, std::string(NODE_LAYERHIDE));
186     if (layerHidePtr == nullptr) {
187         RS_LOGE("[%{public}s] layerHidePtr node is null! \n", __func__);
188         return false;
189     }
190     layerHide.ReadXmlNode(layerHidePtr, attrList);
191     return true;
192 }
193 
ReadXmlNode(const xmlNodePtr & landNodePtr)194 bool RogLandscape::ReadXmlNode(const xmlNodePtr& landNodePtr)
195 {
196     if (landNodePtr == nullptr) {
197         RS_LOGE("[%{public}s] landNodePtr node is null! \n", __func__);
198         return false;
199     }
200     std::vector<std::string> attrList = { ATTR_FILENAME, ATTR_OFFSET_X, ATTR_OFFSET_Y,
201         ATTR_BINFILENAME, ATTR_BUFFERSIZE, ATTR_CLDWIDTH, ATTR_CLDHEIGHT };
202     auto layerUpPtr = XMLReader::FindNode(landNodePtr, std::string(NODE_LAYERUP));
203     if (layerUpPtr == nullptr) {
204         RS_LOGE("[%{public}s] layerUpPtr node is null! \n", __func__);
205         return false;
206     }
207     layerUp.ReadXmlNode(layerUpPtr, attrList);
208     return true;
209 }
210 
ReadXmlNode(const xmlNodePtr & rogNodePtr)211 bool ROGSetting::ReadXmlNode(const xmlNodePtr& rogNodePtr)
212 {
213     if (rogNodePtr == nullptr) {
214         RS_LOGE("[%{public}s] rogNodePtr node is null! \n", __func__);
215         return false;
216     }
217     width = XMLReader::ReadAttrInt(rogNodePtr, ATTR_WIDTH);
218     height = XMLReader::ReadAttrInt(rogNodePtr, ATTR_HEIGHT);
219 
220     auto portPtr = XMLReader::FindNode(rogNodePtr, NODE_PORTRAIT);
221     if (portPtr != nullptr) {
222         RogPortrait p;
223         p.ReadXmlNode(portPtr);
224         portraitMap[NODE_PORTRAIT] = p;
225     }
226 
227     auto portSidePtr = XMLReader::FindNode(rogNodePtr, NODE_PORTRAIT_SIDEREGION);
228     if (portSidePtr != nullptr) {
229         RogPortrait p;
230         p.ReadXmlNode(portSidePtr);
231         portraitMap[NODE_PORTRAIT_SIDEREGION] = p;
232     }
233 
234     auto landPtr = XMLReader::FindNode(rogNodePtr, NODE_LANDSCAPE);
235     if (landPtr != nullptr) {
236         RogLandscape p;
237         p.ReadXmlNode(landPtr);
238         landscapeMap[NODE_LANDSCAPE] = p;
239     }
240 
241     auto landSidePtr = XMLReader::FindNode(rogNodePtr, NODE_LANDSCAPE_SIDEREGION);
242     if (landSidePtr != nullptr) {
243         RogLandscape p;
244         p.ReadXmlNode(landSidePtr);
245         landscapeMap[NODE_LANDSCAPE_SIDEREGION] = p;
246     }
247     return true;
248 }
249 
ReadXmlNode(const xmlNodePtr & surfaceConfigNodePtr)250 bool SurfaceConfig::ReadXmlNode(const xmlNodePtr& surfaceConfigNodePtr)
251 {
252     if (surfaceConfigNodePtr == nullptr) {
253         RS_LOGE("[%{public}s] surfaceConfigNodePtr node is null! \n", __func__);
254         return false;
255     }
256 
257     auto topSurfacePtr = XMLReader::FindNode(surfaceConfigNodePtr, std::string(NODE_TOPSURFACE));
258     if (topSurfacePtr == nullptr) {
259         RS_LOGE("[%{public}s] topSurfacePtr node is null! \n", __func__);
260         return false;
261     }
262     topSurface.ReadXmlNode(topSurfacePtr, ATTR_SUPPORT, ATTR_DISPLAYMODE);
263 
264     auto bottomSurfacePtr = XMLReader::FindNode(surfaceConfigNodePtr, std::string(NODE_BOTTOMSURFACE));
265     if (bottomSurfacePtr == nullptr) {
266         RS_LOGE("[%{public}s] bottomSurfacePtr node is null! \n", __func__);
267         return false;
268     }
269     bottomSurface.ReadXmlNode(bottomSurfacePtr, ATTR_SUPPORT, ATTR_DISPLAYMODE);
270     return true;
271 }
272 
ReadXmlNode(const xmlNodePtr & sideRegionNodePtr)273 bool SideRegionConfig::ReadXmlNode(const xmlNodePtr& sideRegionNodePtr)
274 {
275     if (sideRegionNodePtr == nullptr) {
276         RS_LOGE("[%{public}s] sideRegionNodePtr node is null! \n", __func__);
277         return false;
278     }
279     auto sideRegionPtr = XMLReader::FindNode(sideRegionNodePtr, std::string(NODE_SIDEREGION));
280     if (sideRegionPtr == nullptr) {
281         RS_LOGE("[%{public}s] sideRegionPtr node is null! \n", __func__);
282         return false;
283     }
284     sideRegion.ReadXmlNode(sideRegionPtr, ATTR_SUPPORT, ATTR_DISPLAYMODE);
285     return true;
286 }
287 
ReadXmlNode(const xmlNodePtr & ptr,std::string supportAttr)288 bool HardwareComposer::ReadXmlNode(const xmlNodePtr& ptr, std::string supportAttr)
289 {
290     auto a1 = xmlGetProp(ptr, BAD_CAST(supportAttr.c_str()));
291     if (a1 == nullptr) {
292         RS_LOGE("[%{public}s] supportAttr node is null! \n", __func__);
293         return false;
294     }
295     support = XMLReader::ReadAttrBool(ptr, supportAttr);
296     return true;
297 }
298 
ReadXmlNode(const xmlNodePtr & hardwareComposerNodeptr)299 bool HardwareComposerConfig::ReadXmlNode(const xmlNodePtr& hardwareComposerNodeptr)
300 {
301     if (hardwareComposerNodeptr == nullptr) {
302         RS_LOGE("[%{public}s] hardwareComposerNodeptr node is null! \n", __func__);
303         return false;
304     }
305     auto hardwareComposerPtr = XMLReader::FindNode(hardwareComposerNodeptr, std::string(NODE_HARDWARECOMPOSER));
306     if (hardwareComposerPtr == nullptr) {
307         RS_LOGE("[%{public}s] hardwareComposerPtr node is null! \n", __func__);
308         return false;
309     }
310     hardwareComposer.ReadXmlNode(hardwareComposerPtr, ATTR_SUPPORT);
311     return true;
312 }
313 
~LCDModel()314 LCDModel::~LCDModel()
315 {
316     auto size = static_cast<int>(rogs.size());
317     for (int i = 0; i < size; i++) {
318         if (rogs[i] != nullptr) {
319             delete rogs[i];
320             rogs[i] = nullptr;
321         }
322     }
323 }
324 
ReadXmlNode(const xmlNodePtr & lcdNodePtr)325 bool LCDModel::ReadXmlNode(const xmlNodePtr& lcdNodePtr)
326 {
327     if (lcdNodePtr == nullptr) {
328         RS_LOGE("[%{public}s] input lcdNodePtr node is null! \n", __func__);
329         return false;
330     }
331     name = XMLReader::ReadAttrStr(lcdNodePtr, ATTR_NAME);
332     auto surfaceConfigPtr = XMLReader::FindNode(lcdNodePtr, std::string(NODE_SURFACECONFIG));
333     if (surfaceConfigPtr == nullptr) {
334         RS_LOGW("no surfaceConfig found \n");
335     }
336     surfaceConfig.ReadXmlNode(surfaceConfigPtr);
337 
338     auto sideRegionConfigPtr = XMLReader::FindNode(lcdNodePtr, std::string(NODE_SIDEREGIONCONFIG));
339     if (sideRegionConfigPtr == nullptr) {
340         RS_LOGW("no sideRegionConfig found \n");
341     }
342     sideRegionConfig.ReadXmlNode(sideRegionConfigPtr);
343 
344     auto hardwareComposerConfigPtr = XMLReader::FindNode(lcdNodePtr, std::string(NODE_HARDWARECOMPOSERCONFIG));
345     if (hardwareComposerConfigPtr == nullptr) {
346         RS_LOGW("no harewareComposer found \n");
347     }
348     hardwareConfig.ReadXmlNode(hardwareComposerConfigPtr);
349 
350     xmlNodePtr startPtr = lcdNodePtr->children;
351     while (startPtr != nullptr) {
352         auto name = startPtr->name;
353         if (xmlStrEqual(name, BAD_CAST(NODE_ROG)) == 1) {
354             ROGSetting *rog = new ROGSetting();
355             if (rog->ReadXmlNode(startPtr)) {
356                 rogs.push_back(rog);
357             } else {
358                 delete rog;
359                 rog = nullptr;
360             }
361         }
362         startPtr = startPtr->next;
363     }
364     return true;
365 }
366 
GetSurfaceConfig() const367 SurfaceConfig LCDModel::GetSurfaceConfig() const
368 {
369     return surfaceConfig;
370 }
371 
GetSideRegionConfig() const372 SideRegionConfig LCDModel::GetSideRegionConfig() const
373 {
374     return sideRegionConfig;
375 }
376 
GetHardwareComposerConfig() const377 HardwareComposerConfig LCDModel::GetHardwareComposerConfig() const
378 {
379     return hardwareConfig;
380 }
381 
GetRog(const int w,const int h) const382 ROGSetting* LCDModel::GetRog(const int w, const int h) const
383 {
384     for (auto rog : rogs) {
385         if (rog == nullptr) {
386             continue;
387         }
388         if (rog->width == w && rog->height == h) {
389             return rog;
390         }
391     }
392     return nullptr;
393 }
394 
PrintLayer(std::string name,const rs_rcd::RoundCornerLayer & layer)395 void RCDConfig::PrintLayer(std::string name, const rs_rcd::RoundCornerLayer& layer)
396 {
397     RS_LOGD("%{public}s->Filename: %{public}s, Offset: %{public}d, %{public}d \n",
398         name.c_str(), layer.fileName.c_str(), layer.offsetX, layer.offsetY);
399     RS_LOGD("BinFilename: %{public}s, BufferSize: %{public}d, cldSize:%{public}d, %{public}d \n",
400         layer.binFileName.c_str(), layer.bufferSize, layer.cldWidth, layer.cldHeight);
401 }
402 
PrintParseRog(rs_rcd::ROGSetting * rog)403 void RCDConfig::PrintParseRog(rs_rcd::ROGSetting* rog)
404 {
405     if (rog == nullptr) {
406         RS_LOGE("no model input \n");
407         return;
408     }
409     for (auto kv : rog->portraitMap) {
410         auto port = kv.second;
411         RS_LOGD("rog: %{public}d, %{public}d, %{public}s: \n", rog->width, rog->height, kv.first.c_str());
412         PrintLayer("layerUp  ", port.layerUp);
413         PrintLayer("layerDown", port.layerDown);
414         PrintLayer("layerHide", port.layerHide);
415     }
416     for (auto kv : rog->landscapeMap) {
417         auto port = kv.second;
418         RS_LOGD("rog: %{public}d, %{public}d, %{public}s: \n", rog->width, rog->height, kv.first.c_str());
419         PrintLayer("layerUp  ", port.layerUp);
420     }
421 }
422 
PrintParseLcdModel(rs_rcd::LCDModel * model)423 void RCDConfig::PrintParseLcdModel(rs_rcd::LCDModel* model)
424 {
425     if (model == nullptr) {
426         RS_LOGE("no model input \n");
427         return;
428     }
429     RS_LOGD("lcdModel: %{public}s \n", model->name.c_str());
430     auto surfaceConfig = model->GetSurfaceConfig();
431     RS_LOGD("surfaceConfig: \n");
432     RS_LOGD("topSurface: %{public}d, %{public}d \n", surfaceConfig.topSurface.support,
433         surfaceConfig.topSurface.mode);
434     RS_LOGD("bottomSurface: %{public}d, %{public}d \n", surfaceConfig.bottomSurface.support,
435         surfaceConfig.bottomSurface.mode);
436 
437     auto sideRegionConfig = model->GetSideRegionConfig();
438     RS_LOGD("sideRegionConfig: \n");
439     RS_LOGD("sideRegion: %{public}d, %{public}d \n", sideRegionConfig.sideRegion.support,
440         sideRegionConfig.sideRegion.mode);
441 
442     auto hardwareConfig = model->GetHardwareComposerConfig();
443     RS_LOGD("hardwareConfig: \n");
444     RS_LOGD("hardwareComposer: %{public}d \n", hardwareConfig.hardwareComposer.support);
445 }
446 
Load(std::string configFile)447 bool RCDConfig::Load(std::string configFile)
448 {
449     auto begin = high_resolution_clock::now();
450     xmlKeepBlanksDefault(0);
451     pDoc = xmlReadFile(configFile.c_str(), "", XML_PARSE_RECOVER);
452     if (pDoc == nullptr) {
453         RS_LOGE("RoundCornerDisplay read xml failed \n");
454         return false;
455     }
456     RS_LOGD("RoundCornerDisplay read xml ok \n");
457     pRoot = xmlDocGetRootElement(pDoc);
458     if (pRoot == nullptr) {
459         RS_LOGE("RoundCornerDisplay get xml root failed \n");
460         return false;
461     }
462     xmlNodePtr startPtr = pRoot->children;
463     while (startPtr != nullptr) {
464         auto name = startPtr->name;
465         if (xmlStrEqual(name, BAD_CAST(NODE_LCDMODEL)) == 1) {
466             LCDModel* lcdModel = new LCDModel();
467             if (lcdModel->ReadXmlNode(startPtr)) {
468                 lcdModels.push_back(lcdModel);
469             } else {
470                 delete lcdModel;
471                 lcdModel = nullptr;
472             }
473         }
474         startPtr = startPtr->next;
475     }
476     auto interval = std::chrono::duration_cast<microseconds>(high_resolution_clock::now() - begin);
477     RS_LOGD("RoundCornerDisplay read xml time cost %{public}lld us \n", interval.count());
478     return true;
479 }
480 
GetLcdModel(std::string name) const481 LCDModel* RCDConfig::GetLcdModel(std::string name) const
482 {
483     if (name.empty()) {
484         RS_LOGE("[%{public}s] name is null! \n", __func__);
485         return nullptr;
486     }
487     for (auto model : lcdModels) {
488         if (model == nullptr) {
489             continue;
490         }
491         if (model->name.compare(name) == 0) {
492             return model;
493         }
494     }
495     RS_LOGE("[%{public}s] lcd not found in name %{public}s! \n", __func__, name.c_str());
496     return nullptr;
497 }
498 
~RCDConfig()499 RCDConfig::~RCDConfig()
500 {
501     if (pDoc != nullptr) {
502         xmlFreeDoc(pDoc);
503     }
504     xmlCleanupParser();
505     xmlMemoryDump();
506 
507     auto size = static_cast<int>(lcdModels.size());
508     for (int i = 0; i < size; i++) {
509         if (lcdModels[i] != nullptr) {
510             delete lcdModels[i];
511             lcdModels[i] = nullptr;
512         }
513     }
514 }
515 } // namespace rs_rcd
516 } // namespace Rosen
517 } // OHOS