Initial refactoring of graphics interface
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / configuration-manager.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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
18 // CLASS HEADER
19 #include <dali/internal/system/common/configuration-manager.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <fstream>
24
25 // INTERNAL INCLUDES
26 #include <dali/devel-api/adaptor-framework/file-stream.h>
27 #include <dali/internal/graphics/common/graphics-interface.h>
28 #include <dali/internal/system/common/environment-options.h>
29 #include <dali/internal/system/common/environment-variables.h>
30 #include <dali/internal/system/common/thread-controller.h>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace Adaptor
37 {
38 namespace
39 {
40 const std::string SYSTEM_CACHE_FILE                    = "gpu-environment.conf";
41 const std::string DALI_ENV_MULTIPLE_WINDOW_SUPPORT     = "DALI_ENV_MULTIPLE_WINDOW_SUPPORT";
42 const std::string DALI_BLEND_EQUATION_ADVANCED_SUPPORT = "DALI_BLEND_EQUATION_ADVANCED_SUPPORT";
43 const std::string DALI_GLSL_VERSION                    = "DALI_GLSL_VERSION";
44
45 bool RetrieveKeyFromConfigFile(std::iostream& stream, const std::string& key, std::string& value)
46 {
47   bool keyFound = false;
48
49   std::string line;
50   while(std::getline(stream, line))
51   {
52     line.erase(line.find_last_not_of(" \t\r\n") + 1);
53     line.erase(0, line.find_first_not_of(" \t\r\n"));
54     if('#' == *(line.cbegin()) || line == "")
55     {
56       continue;
57     }
58
59     std::istringstream stream(line);
60     std::string        name;
61     std::getline(stream, name, ' ');
62     if(name == key)
63     {
64       std::getline(stream, value);
65       keyFound = true;
66       break;
67     }
68   }
69
70   return keyFound;
71 }
72
73 } // unnamed namespace
74
75 ConfigurationManager::ConfigurationManager(std::string systemCachePath, GraphicsInterface* graphics, ThreadController* threadController)
76 : mSystemCacheFilePath(systemCachePath + SYSTEM_CACHE_FILE),
77   mGraphics(graphics),
78   mThreadController(threadController),
79   mMaxTextureSize(0u),
80   mShaderLanguageVersion(0u),
81   mIsMultipleWindowSupported(true),
82   mIsAdvancedBlendEquationSupported(true),
83   mMaxTextureSizeCached(false),
84   mIsMultipleWindowSupportedCached(false),
85   mIsAdvancedBlendEquationSupportedCached(false),
86   mShaderLanguageVersionCached(false)
87 {
88 }
89
90 ConfigurationManager::~ConfigurationManager()
91 {
92 }
93
94 void ConfigurationManager::RetrieveKeysFromConfigFile(const std::string& configFilePath)
95 {
96   Dali::FileStream configFile(configFilePath, Dali::FileStream::READ | Dali::FileStream::TEXT);
97   std::iostream&   stream = configFile.GetStream();
98   if(stream.rdbuf()->in_avail())
99   {
100     std::string value;
101     if(!mMaxTextureSizeCached &&
102        RetrieveKeyFromConfigFile(stream, DALI_ENV_MAX_TEXTURE_SIZE, value))
103     {
104       mMaxTextureSize       = std::atoi(value.c_str());
105       mMaxTextureSizeCached = true;
106     }
107
108     if(!mShaderLanguageVersionCached &&
109        RetrieveKeyFromConfigFile(stream, DALI_GLSL_VERSION, value))
110     {
111       mShaderLanguageVersion       = std::atoi(value.c_str());
112       mShaderLanguageVersionCached = true;
113     }
114
115     if(!mIsMultipleWindowSupportedCached &&
116        RetrieveKeyFromConfigFile(stream, DALI_ENV_MULTIPLE_WINDOW_SUPPORT, value))
117     {
118       mIsMultipleWindowSupported       = std::atoi(value.c_str());
119       mIsMultipleWindowSupportedCached = true;
120     }
121
122     if(!mIsAdvancedBlendEquationSupportedCached &&
123        RetrieveKeyFromConfigFile(stream, DALI_BLEND_EQUATION_ADVANCED_SUPPORT, value))
124     {
125       mIsAdvancedBlendEquationSupported       = std::atoi(value.c_str());
126       mIsAdvancedBlendEquationSupportedCached = true;
127     }
128   }
129 }
130
131 uint32_t ConfigurationManager::GetMaxTextureSize()
132 {
133   if(!mMaxTextureSizeCached)
134   {
135     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
136
137     if(!mMaxTextureSizeCached)
138     {
139       mMaxTextureSize       = mGraphics->GetMaxTextureSize();
140       mMaxTextureSizeCached = true;
141
142       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
143       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
144       if(stream.is_open())
145       {
146         stream << DALI_ENV_MAX_TEXTURE_SIZE << " " << mMaxTextureSize << std::endl;
147       }
148       else
149       {
150         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
151       }
152     }
153   }
154
155   return mMaxTextureSize;
156 }
157
158 uint32_t ConfigurationManager::GetShadingLanguageVersion()
159 {
160   if(!mShaderLanguageVersionCached)
161   {
162     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
163
164     if(!mShaderLanguageVersionCached)
165     {
166       if(!mGraphics->IsInitialized())
167       {
168         // Wait until Graphics Subsystem is initialised, but this will happen once.
169         // This method blocks until the render thread has initialised the graphics.
170         mThreadController->WaitForGraphicsInitialization();
171       }
172
173       // Query from graphics and save the cache
174       mShaderLanguageVersion = mGraphics->GetShaderLanguageVersion();
175       DALI_LOG_ERROR("mShaderLanguageVersion : %d\n", mShaderLanguageVersion);
176       mShaderLanguageVersionCached = true;
177
178       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
179       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
180       if(stream.is_open())
181       {
182         stream << DALI_GLSL_VERSION << " " << mShaderLanguageVersion << std::endl;
183       }
184       else
185       {
186         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
187       }
188     }
189   }
190
191   return mShaderLanguageVersion;
192 }
193
194 bool ConfigurationManager::IsMultipleWindowSupported()
195 {
196   if(!mIsMultipleWindowSupportedCached)
197   {
198     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
199
200     if(!mIsMultipleWindowSupportedCached)
201     {
202       if(!mGraphics->IsInitialized())
203       {
204         // Wait until Graphics Subsystem is initialised, but this will happen once.
205         // This method blocks until the render thread has initialised the graphics.
206         mThreadController->WaitForGraphicsInitialization();
207       }
208
209       // Query from Graphics Subsystem and save the cache
210       mIsMultipleWindowSupported       = mGraphics->IsResourceContextSupported();
211       mIsMultipleWindowSupportedCached = true;
212
213       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
214       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
215       if(stream.is_open())
216       {
217         stream << DALI_ENV_MULTIPLE_WINDOW_SUPPORT << " " << mIsMultipleWindowSupported << std::endl;
218       }
219       else
220       {
221         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
222       }
223     }
224   }
225
226   return mIsMultipleWindowSupported;
227 }
228
229 bool ConfigurationManager::IsAdvancedBlendEquationSupported()
230 {
231   if(!mIsAdvancedBlendEquationSupportedCached)
232   {
233     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
234
235     if(!mIsAdvancedBlendEquationSupportedCached)
236     {
237       if(!mGraphics->IsInitialized())
238       {
239         // Wait until graphics subsystem is initialised, but this will happen once per factory reset.
240         // This method blocks until the render thread has initialised the graphics.
241         mThreadController->WaitForGraphicsInitialization();
242       }
243
244       // Query from Graphics Subsystem and save the cache
245       mIsAdvancedBlendEquationSupported       = mGraphics->IsAdvancedBlendEquationSupported();
246       mIsAdvancedBlendEquationSupportedCached = true;
247
248       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
249       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
250       if(stream.is_open())
251       {
252         stream << DALI_BLEND_EQUATION_ADVANCED_SUPPORT << " " << mIsAdvancedBlendEquationSupported << std::endl;
253       }
254       else
255       {
256         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
257       }
258     }
259   }
260
261   return mIsAdvancedBlendEquationSupported;
262 }
263
264 } // namespace Adaptor
265
266 } // namespace Internal
267
268 } // namespace Dali