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