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 "pipeline/rs_uni_render_judgement.h"
17 
18 #include <fstream>
19 
20 #include "platform/common/rs_log.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25 const std::string CONFIG_PATH = "/etc/";
26 const std::string UNIRENDER_CONFIG_FILE_NAME = "unirender.config";
27 const std::string UNI_RENDER_DISABLED_TAG = "DISABLED";
28 const std::string UNI_RENDER_ENABLED_FOR_ALL_TAG = "ENABLED_FOR_ALL";
29 }
30 
31 // used by render server
GetUniRenderEnabledType()32 UniRenderEnabledType RSUniRenderJudgement::GetUniRenderEnabledType()
33 {
34     return uniRenderEnabledType_;
35 }
36 
IsUniRender()37 bool RSUniRenderJudgement::IsUniRender()
38 {
39     return RSUniRenderJudgement::GetUniRenderEnabledType() != UniRenderEnabledType::UNI_RENDER_DISABLED;
40 }
41 
InitUniRenderConfig()42 void RSUniRenderJudgement::InitUniRenderConfig()
43 {
44     InitUniRenderWithConfigFile();
45     RS_LOGD("Init RenderService UniRender Type:%{public}d", uniRenderEnabledType_);
46 }
47 
SafeGetLine(std::ifstream & configFile,std::string & line)48 std::ifstream& RSUniRenderJudgement::SafeGetLine(std::ifstream &configFile, std::string &line)
49 {
50     std::string myline;
51     std::getline(configFile, myline);
52     if (myline.size() && myline[myline.size() - 1] == '\r') {
53         line = myline.substr(0, myline.size() - 1);
54     } else {
55         line = myline;
56     }
57     return configFile;
58 }
59 
InitUniRenderWithConfigFile()60 void RSUniRenderJudgement::InitUniRenderWithConfigFile()
61 {
62     // open config file
63     std::string configFilePath = CONFIG_PATH + UNIRENDER_CONFIG_FILE_NAME;
64     std::ifstream configFile = std::ifstream(configFilePath.c_str());
65     std::string line;
66     // first line, init uniRenderEnabledType_
67     if (!configFile.is_open() || !SafeGetLine(configFile, line) || line.empty()) { // default case
68 #ifdef RS_ENABLE_UNI_RENDER
69         uniRenderEnabledType_ = UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL;
70 #else
71         uniRenderEnabledType_ = UniRenderEnabledType::UNI_RENDER_DISABLED;
72 #endif
73     } else if (line == UNI_RENDER_DISABLED_TAG) {
74         uniRenderEnabledType_ = UniRenderEnabledType::UNI_RENDER_DISABLED;
75     } else if (line == UNI_RENDER_ENABLED_FOR_ALL_TAG) {
76         uniRenderEnabledType_ = UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL;
77     }
78     configFile.close();
79 }
80 } // namespace Rosen
81 } // namespace OHOS
82