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