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 #ifndef RENDER_SERVICE_CORE_PIPELINE_RS_RCD_CONFIG_H
17 #define RENDER_SERVICE_CORE_PIPELINE_RS_RCD_CONFIG_H
18 
19 #pragma once
20 #include <libxml/parser.h>
21 #include <libxml/tree.h>
22 #include <cstring>
23 #include <vector>
24 #include <iostream>
25 #include <cstdlib>
26 #include <memory>
27 #include <mutex>
28 #include <optional>
29 #include <thread>
30 #include <unordered_map>
31 
32 #include "image/bitmap.h"
33 #include "securec.h"
34 #include "platform/common/rs_log.h"
35 
36 namespace OHOS {
37 namespace Rosen {
38 namespace rs_rcd {
39 
40 const char PATH_CONFIG_FILE[] = "/sys_prod/etc/display/RoundCornerDisplay/config.xml";
41 const char PATH_CONFIG_DIR[] = "/sys_prod/etc/display/RoundCornerDisplay";
42 
43 const char NODE_ROUNDCORNERDISPLAY[] = "RoundCornerDisplay";
44 const char NODE_LCDMODEL[] = "LCDModel";
45 const char NODE_SURFACECONFIG[] = "SurfaceConfig";
46 const char NODE_TOPSURFACE[] = "TopSurface";
47 const char NODE_BOTTOMSURFACE[] = "BottomSurface";
48 const char NODE_SIDEREGIONCONFIG[] = "SideRegionConfig";
49 const char NODE_SIDEREGION[] = "SideRegion";
50 const char NODE_HARDWARECOMPOSERCONFIG[] = "HardwareComposerConfig";
51 const char NODE_HARDWARECOMPOSER[] = "HardwareComposer";
52 const char NODE_ROG[] = "ROG";
53 const char NODE_PORTRAIT[] = "Portrait";
54 const char NODE_LANDSCAPE[] = "Landscape";
55 const char NODE_PORTRAIT_SIDEREGION[] = "Portrait_sideRegion";
56 const char NODE_LANDSCAPE_SIDEREGION[] = "Landscape_sideRegion";
57 const char NODE_LAYERUP[] = "LayerUp";
58 const char NODE_LAYERDOWN[] = "LayerDown";
59 const char NODE_LAYERHIDE[] = "LayerHide";
60 const char NODE_CAMERAAUO[] = "CameraAuo";
61 
62 const char ATTR_SUPPORT[] = "Support";
63 const char ATTR_DISPLAYMODE[] = "DisplayMode";
64 const char ATTR_REGIONMODE[] = "RegionMode";
65 const char ATTR_NAME[] = "Name";
66 const char ATTR_WIDTH[] = "Width";
67 const char ATTR_HEIGHT[] = "Height";
68 const char ATTR_FILENAME[] = "Filename";
69 const char ATTR_BINFILENAME[] = "BinFilename";
70 const char ATTR_OFFSET_X[] = "OffsetX";
71 const char ATTR_OFFSET_Y[] = "OffsetY";
72 const char ATTR_BUFFERSIZE[] = "BufferSize";
73 const char ATTR_CLDWIDTH[] = "CldWidth";
74 const char ATTR_CLDHEIGHT[] = "CldHeight";
75 const char ATTR_DEFAULT[] = "default";
76 
77 struct XMLReader {
78     static bool RegexMatch(const std::string& str, const std::string& pattern);
79     static bool RegexMatchNum(std::string& str);
80     static xmlNodePtr FindNode(const xmlNodePtr& src, const std::string& index);
81     static std::string ReadAttrStr(const xmlNodePtr& src, const std::string& attr);
82     static int ReadAttrInt(const xmlNodePtr& src, const std::string& attr);
83     static float ReadAttrFloat(const xmlNodePtr& src, const std::string& attr);
84     static bool ReadAttrBool(const xmlNodePtr& src, const std::string& attr);
85 };
86 
87 struct SupportConfig {
88     bool support = false;
89     int mode = 0;
90     bool ReadXmlNode(const xmlNodePtr& ptr, const std::string& supportAttr, const std::string& modeAttr);
91 };
92 
93 struct RoundCornerLayer {
94     std::string fileName;
95     int offsetX = 0;
96     int offsetY = 0;
97     std::string binFileName;
98     int bufferSize = 0;
99     int cldWidth = 0;
100     int cldHeight = 0;
101     uint32_t layerWidth = 0;
102     uint32_t layerHeight = 0;
103     Drawing::Bitmap* curBitmap = nullptr;
104     bool ReadXmlNode(const xmlNodePtr& ptr, const std::vector<std::string>& attrArray);
105 };
106 
107 struct RoundCornerHardware {
108     bool resourceChanged = false;
109     bool resourcePreparing = false;
110     std::shared_ptr<RoundCornerLayer> topLayer = nullptr;
111     std::shared_ptr<RoundCornerLayer> bottomLayer = nullptr;
112 };
113 
114 struct RogPortrait {
115     RoundCornerLayer layerUp;
116     RoundCornerLayer layerDown;
117     RoundCornerLayer layerHide;
118     bool ReadXmlNode(const xmlNodePtr& portraitNodePtr);
119 };
120 
121 struct RogLandscape {
122     RoundCornerLayer layerUp;
123     bool ReadXmlNode(const xmlNodePtr& landNodePtr);
124 };
125 
126 struct ROGSetting {
127     int width = 0;
128     int height = 0;
129     std::unordered_map<std::string, RogPortrait> portraitMap;
130     std::unordered_map<std::string, RogLandscape> landscapeMap;
131     bool ReadXmlNode(const xmlNodePtr& rogNodePtr);
132     bool HavePortrait(const std::string& name) const;
133     bool HaveLandscape(const std::string& name) const;
134     std::optional<RogPortrait> GetPortrait(const std::string& name) const;
135     std::optional<RogLandscape> GetLandscape(const std::string& name) const;
136 };
137 
138 struct SurfaceConfig {
139     SupportConfig topSurface;
140     SupportConfig bottomSurface;
141     bool ReadXmlNode(const xmlNodePtr& surfaceConfigNodePtr);
142 };
143 
144 struct SideRegionConfig {
145     SupportConfig sideRegion;
146     bool ReadXmlNode(const xmlNodePtr& sideRegionNodePtr);
147 };
148 
149 struct HardwareComposer {
150     bool support = false;
151     bool ReadXmlNode(const xmlNodePtr& ptr, const std::string& supportAttr);
152 };
153 
154 struct HardwareComposerConfig {
155     HardwareComposer hardwareComposer;
156     bool ReadXmlNode(const xmlNodePtr& hardwareComposerNodePtr);
157 };
158 
159 struct LCDModel {
LCDModelLCDModel160     LCDModel() {}
161     virtual ~LCDModel();
162     std::string name;
163     SurfaceConfig surfaceConfig;
164     SideRegionConfig sideRegionConfig;
165     HardwareComposerConfig hardwareConfig;
166     std::vector<ROGSetting*> rogs;
167     bool ReadXmlNode(const xmlNodePtr& lcdNodePtr);
168     SurfaceConfig GetSurfaceConfig() const;
169     SideRegionConfig GetSideRegionConfig() const;
170     HardwareComposerConfig GetHardwareComposerConfig() const;
171     ROGSetting* GetRog(const int w, const int h) const;
172 };
173 
174 struct RCDConfig {
RCDConfigRCDConfig175     RCDConfig() {}
176     virtual ~RCDConfig();
177     static void PrintLayer(const std::string& name, const rs_rcd::RoundCornerLayer& layer);
178     static void PrintParseRog(rs_rcd::ROGSetting* rog);
179     LCDModel* GetLcdModel(const std::string& name) const;
180     bool Load(const std::string& configFile);
181 private:
182     void CloseXML();
183     void Clear();
184     xmlDocPtr pDoc = nullptr;
185     xmlNodePtr pRoot = nullptr;
186     std::vector<LCDModel*> lcdModels;
187     std::mutex xmlMut;
188     bool isLoadData = false;
189 };
190 } // namespace rs_rcd
191 } // namespace Rosen
192 } // namespace OHOS
193 
194 #endif // RENDER_SERVICE_CORE_PIPELINE_RS_RCD_CONFIG_H