0515b46a4013e2e7a668dd07ccf1bf9433d495b1
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / configuration-manager.cpp
1 /*
2  * Copyright (c) 2022 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_MULTISAMPLED_RENDER_TO_TEXTURE_SUPPORT = "DALI_MULTISAMPLED_RENDER_TO_TEXTURE_SUPPORT";
44 const std::string DALI_GLSL_VERSION                           = "DALI_GLSL_VERSION";
45
46 } // unnamed namespace
47
48 ConfigurationManager::ConfigurationManager(std::string systemCachePath, GraphicsInterface* graphics, ThreadController* threadController)
49 : mSystemCacheFilePath(systemCachePath + SYSTEM_CACHE_FILE),
50   mGraphics(graphics),
51   mThreadController(threadController),
52   mMaxTextureSize(0u),
53   mShaderLanguageVersion(0u),
54   mIsMultipleWindowSupported(true),
55   mIsAdvancedBlendEquationSupported(true),
56   mIsMultisampledRenderToTextureSupported(true),
57   mMaxTextureSizeCached(false),
58   mIsMultipleWindowSupportedCached(false),
59   mIsAdvancedBlendEquationSupportedCached(false),
60   mIsMultisampledRenderToTextureSupportedCached(false),
61   mShaderLanguageVersionCached(false)
62 {
63 }
64
65 ConfigurationManager::~ConfigurationManager()
66 {
67 }
68
69 void ConfigurationManager::RetrieveKeysFromConfigFile(const std::string& configFilePath)
70 {
71   Dali::FileStream configFile(configFilePath, Dali::FileStream::READ | Dali::FileStream::TEXT);
72   std::iostream&   stream = configFile.GetStream();
73   if(stream.rdbuf()->in_avail())
74   {
75     std::string line;
76     while(std::getline(stream, line))
77     {
78       line.erase(line.find_last_not_of(" \t\r\n") + 1);
79       line.erase(0, line.find_first_not_of(" \t\r\n"));
80       if('#' == *(line.cbegin()) || line == "")
81       {
82         continue;
83       }
84
85       std::istringstream subStream(line);
86       std::string        name;
87       std::string        value;
88       std::getline(subStream, name, ' ');
89       if(!mMaxTextureSizeCached && name == DALI_ENV_MAX_TEXTURE_SIZE)
90       {
91         std::getline(subStream, value);
92         mMaxTextureSize       = std::atoi(value.c_str());
93         mMaxTextureSizeCached = true;
94       }
95       else if(!mIsAdvancedBlendEquationSupportedCached && name == DALI_BLEND_EQUATION_ADVANCED_SUPPORT)
96       {
97         std::getline(subStream, value);
98         mIsAdvancedBlendEquationSupported       = std::atoi(value.c_str());
99         mIsAdvancedBlendEquationSupportedCached = true;
100       }
101       else if(!mIsMultisampledRenderToTextureSupportedCached && name == DALI_MULTISAMPLED_RENDER_TO_TEXTURE_SUPPORT)
102       {
103         std::getline(subStream, value);
104         mIsMultisampledRenderToTextureSupported       = std::atoi(value.c_str());
105         mIsMultisampledRenderToTextureSupportedCached = true;
106       }
107       else if(!mShaderLanguageVersionCached && name == DALI_GLSL_VERSION)
108       {
109         std::getline(subStream, value);
110         mShaderLanguageVersion       = std::atoi(value.c_str());
111         mShaderLanguageVersionCached = true;
112       }
113       else if(!mIsMultipleWindowSupportedCached && name == DALI_ENV_MULTIPLE_WINDOW_SUPPORT)
114       {
115         std::getline(subStream, value);
116         mIsMultipleWindowSupported       = std::atoi(value.c_str());
117         mIsMultipleWindowSupportedCached = true;
118       }
119     }
120   }
121 }
122
123 uint32_t ConfigurationManager::GetMaxTextureSize()
124 {
125   if(!mMaxTextureSizeCached)
126   {
127     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
128
129     if(!mMaxTextureSizeCached)
130     {
131       if(!mGraphics->IsInitialized())
132       {
133         // Wait until Graphics Subsystem is initialised, but this will happen once.
134         // This method blocks until the render thread has initialised the graphics.
135         mThreadController->WaitForGraphicsInitialization();
136       }
137
138       mMaxTextureSize       = mGraphics->GetMaxTextureSize();
139       mMaxTextureSizeCached = true;
140
141       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
142       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
143       if(stream.is_open())
144       {
145         stream << DALI_ENV_MAX_TEXTURE_SIZE << " " << mMaxTextureSize << std::endl;
146       }
147       else
148       {
149         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
150       }
151     }
152   }
153
154   return mMaxTextureSize;
155 }
156
157 uint32_t ConfigurationManager::GetShadingLanguageVersion()
158 {
159   if(!mShaderLanguageVersionCached)
160   {
161     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
162
163     if(!mShaderLanguageVersionCached)
164     {
165       if(!mGraphics->IsInitialized())
166       {
167         // Wait until Graphics Subsystem is initialised, but this will happen once.
168         // This method blocks until the render thread has initialised the graphics.
169         mThreadController->WaitForGraphicsInitialization();
170       }
171
172       // Query from graphics and save the cache
173       mShaderLanguageVersion       = mGraphics->GetShaderLanguageVersion();
174       mShaderLanguageVersionCached = true;
175
176       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
177       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
178       if(stream.is_open())
179       {
180         stream << DALI_GLSL_VERSION << " " << mShaderLanguageVersion << std::endl;
181       }
182       else
183       {
184         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
185       }
186     }
187   }
188
189   return mShaderLanguageVersion;
190 }
191
192 bool ConfigurationManager::IsMultipleWindowSupported()
193 {
194   if(!mIsMultipleWindowSupportedCached)
195   {
196     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
197
198     if(!mIsMultipleWindowSupportedCached)
199     {
200       if(!mGraphics->IsInitialized())
201       {
202         // Wait until Graphics Subsystem is initialised, but this will happen once.
203         // This method blocks until the render thread has initialised the graphics.
204         mThreadController->WaitForGraphicsInitialization();
205       }
206
207       // Query from Graphics Subsystem and save the cache
208       mIsMultipleWindowSupported       = mGraphics->IsResourceContextSupported();
209       mIsMultipleWindowSupportedCached = true;
210
211       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
212       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
213       if(stream.is_open())
214       {
215         stream << DALI_ENV_MULTIPLE_WINDOW_SUPPORT << " " << mIsMultipleWindowSupported << std::endl;
216       }
217       else
218       {
219         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
220       }
221     }
222   }
223
224   return mIsMultipleWindowSupported;
225 }
226
227 bool ConfigurationManager::IsAdvancedBlendEquationSupported()
228 {
229   if(!mIsAdvancedBlendEquationSupportedCached)
230   {
231     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
232
233     if(!mIsAdvancedBlendEquationSupportedCached)
234     {
235       if(!mGraphics->IsInitialized())
236       {
237         // Wait until graphics subsystem is initialised, but this will happen once per factory reset.
238         // This method blocks until the render thread has initialised the graphics.
239         mThreadController->WaitForGraphicsInitialization();
240       }
241
242       // Query from Graphics Subsystem and save the cache
243       mIsAdvancedBlendEquationSupported       = mGraphics->IsAdvancedBlendEquationSupported();
244       mIsAdvancedBlendEquationSupportedCached = true;
245
246       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
247       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
248       if(stream.is_open())
249       {
250         stream << DALI_BLEND_EQUATION_ADVANCED_SUPPORT << " " << mIsAdvancedBlendEquationSupported << std::endl;
251       }
252       else
253       {
254         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
255       }
256     }
257   }
258
259   return mIsAdvancedBlendEquationSupported;
260 }
261
262 bool ConfigurationManager::IsMultisampledRenderToTextureSupported()
263 {
264   if(!mIsMultisampledRenderToTextureSupportedCached)
265   {
266     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
267
268     if(!mIsMultisampledRenderToTextureSupportedCached)
269     {
270       if(!mGraphics->IsInitialized())
271       {
272         // Wait until graphics subsystem is initialised, but this will happen once per factory reset.
273         // This method blocks until the render thread has initialised the graphics.
274         mThreadController->WaitForGraphicsInitialization();
275       }
276
277       // Query from Graphics Subsystem and save the cache
278       mIsMultisampledRenderToTextureSupported       = mGraphics->IsMultisampledRenderToTextureSupported();
279       mIsMultisampledRenderToTextureSupportedCached = true;
280
281       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
282       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
283       if(stream.is_open())
284       {
285         stream << DALI_MULTISAMPLED_RENDER_TO_TEXTURE_SUPPORT << " " << mIsMultisampledRenderToTextureSupported << std::endl;
286       }
287       else
288       {
289         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
290       }
291     }
292   }
293
294   return mIsMultisampledRenderToTextureSupported;
295 }
296
297 } // namespace Adaptor
298
299 } // namespace Internal
300
301 } // namespace Dali