(SVACE Issues Fix) Initialise uninitialised member variables
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / configuration-manager.cpp
1 /*
2  * Copyright (c) 2020 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 <fstream>
23 #include <dali/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26 #include <dali/devel-api/adaptor-framework/file-stream.h>
27 #include <dali/internal/graphics/gles/egl-graphics.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
35 namespace Internal
36 {
37
38 namespace Adaptor
39 {
40
41 namespace
42 {
43
44 const std::string SYSTEM_CACHE_FILE = "gpu-environment.conf";
45 const std::string DALI_ENV_MULTIPLE_WINDOW_SUPPORT = "DALI_ENV_MULTIPLE_WINDOW_SUPPORT";
46 const std::string DALI_BLEND_EQUATION_ADVANCED_SUPPORT = "DALI_BLEND_EQUATION_ADVANCED_SUPPORT";
47 const std::string DALI_GLSL_VERSION = "DALI_GLSL_VERSION";
48
49 bool RetrieveKeyFromConfigFile( std::iostream& stream, const std::string& key, std::string& value )
50 {
51   bool keyFound = false;
52
53   std::string line;
54   while( std::getline( stream, line ) )
55   {
56     line.erase( line.find_last_not_of( " \t\r\n" ) + 1 );
57     line.erase( 0, line.find_first_not_of( " \t\r\n" ) );
58     if( '#' == *( line.cbegin() ) || line == "" )
59     {
60       continue;
61     }
62
63     std::istringstream stream( line );
64     std::string name;
65     std::getline(stream, name, ' ');
66     if( name == key )
67     {
68       std::getline(stream, value);
69       keyFound = true;
70       break;
71     }
72   }
73
74   return keyFound;
75 }
76
77
78 } // unnamed namespace
79
80 ConfigurationManager::ConfigurationManager( std::string systemCachePath, EglGraphics* eglGraphics, ThreadController* threadController )
81 : mSystemCacheFilePath( systemCachePath + SYSTEM_CACHE_FILE ),
82   mEglGraphics( eglGraphics ),
83   mThreadController( threadController ),
84   mMaxTextureSize( 0u ),
85   mGlslVersion( 0u),
86   mIsMultipleWindowSupported( true ),
87   mIsAdvancedBlendEquationSupported( true ),
88   mMaxTextureSizeCached( false ),
89   mIsMultipleWindowSupportedCached( false ),
90   mIsAdvancedBlendEquationSupportedCached( false ),
91   mGlslVersionCached( false )
92 {
93 }
94
95 ConfigurationManager::~ConfigurationManager()
96 {
97 }
98
99 void ConfigurationManager::RetrieveKeysFromConfigFile( const std::string& configFilePath )
100 {
101   Dali::FileStream configFile( configFilePath, Dali::FileStream::READ | Dali::FileStream::TEXT );
102   std::iostream& stream = configFile.GetStream();
103   if( stream.rdbuf()->in_avail() )
104   {
105     std::string value;
106     if( !mMaxTextureSizeCached &&
107         RetrieveKeyFromConfigFile( stream, DALI_ENV_MAX_TEXTURE_SIZE, value ) )
108     {
109       mMaxTextureSize = std::atoi( value.c_str() );
110       mMaxTextureSizeCached = true;
111     }
112
113     if( !mGlslVersionCached &&
114         RetrieveKeyFromConfigFile( stream, DALI_GLSL_VERSION, value ) )
115     {
116       mGlslVersion = std::atoi( value.c_str() );
117       mGlslVersionCached = true;
118     }
119
120     if( !mIsMultipleWindowSupportedCached &&
121         RetrieveKeyFromConfigFile( stream, DALI_ENV_MULTIPLE_WINDOW_SUPPORT, value ) )
122     {
123       mIsMultipleWindowSupported = std::atoi( value.c_str() );
124       mIsMultipleWindowSupportedCached = true;
125     }
126
127     if( !mIsAdvancedBlendEquationSupportedCached &&
128         RetrieveKeyFromConfigFile( stream, DALI_BLEND_EQUATION_ADVANCED_SUPPORT, value ) )
129     {
130       mIsAdvancedBlendEquationSupported = std::atoi( value.c_str() );
131       mIsAdvancedBlendEquationSupportedCached = true;
132     }
133   }
134 }
135
136 uint32_t ConfigurationManager::GetMaxTextureSize()
137 {
138   if( !mMaxTextureSizeCached )
139   {
140     RetrieveKeysFromConfigFile( mSystemCacheFilePath );
141
142     if( !mMaxTextureSizeCached )
143     {
144       GlImplementation& mGLES = mEglGraphics->GetGlesInterface();
145       mMaxTextureSize = mGLES.GetMaxTextureSize();
146       mMaxTextureSizeCached = true;
147
148       Dali::FileStream configFile( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT );
149       std::fstream& stream = dynamic_cast<std::fstream&>( configFile.GetStream() );
150       if( stream.is_open() )
151       {
152         stream << DALI_ENV_MAX_TEXTURE_SIZE << " " << mMaxTextureSize << std::endl;
153       }
154       else
155       {
156         DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
157       }
158     }
159   }
160
161   return mMaxTextureSize;
162 }
163
164 uint32_t ConfigurationManager::GetShadingLanguageVersion()
165 {
166   if ( !mGlslVersionCached )
167   {
168     RetrieveKeysFromConfigFile( mSystemCacheFilePath );
169
170     if ( !mGlslVersionCached )
171     {
172       EglImplementation& eglImpl = mEglGraphics->GetEglImplementation();
173       if ( !eglImpl.IsGlesInitialized() )
174       {
175         // Wait until GLES 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       // Query from GLES and save the cache
181       mGlslVersion = mEglGraphics->GetGlesInterface().GetShadingLanguageVersion();
182       DALI_LOG_ERROR("mGlslVersion : %d\n", mGlslVersion);
183       mGlslVersionCached = true;
184
185       Dali::FileStream configFile( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT );
186       std::fstream& stream = dynamic_cast<std::fstream&>( configFile.GetStream() );
187       if ( stream.is_open() )
188       {
189         stream << DALI_GLSL_VERSION << " " << mGlslVersion << std::endl;
190       }
191       else
192       {
193         DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
194       }
195     }
196   }
197
198   return mGlslVersion;
199 }
200
201 bool ConfigurationManager::IsMultipleWindowSupported()
202 {
203   if ( !mIsMultipleWindowSupportedCached )
204   {
205     RetrieveKeysFromConfigFile( mSystemCacheFilePath );
206
207     if ( !mIsMultipleWindowSupportedCached )
208     {
209       EglImplementation& eglImpl = mEglGraphics->GetEglImplementation();
210       if ( !eglImpl.IsGlesInitialized() )
211       {
212         // Wait until GLES is initialised, but this will happen once.
213         // This method blocks until the render thread has initialised the graphics.
214         mThreadController->WaitForGraphicsInitialization();
215       }
216
217       // Query from GLES and save the cache
218       mIsMultipleWindowSupported = eglImpl.IsSurfacelessContextSupported();
219       mIsMultipleWindowSupportedCached = true;
220
221       Dali::FileStream configFile( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT );
222       std::fstream& stream = dynamic_cast<std::fstream&>( configFile.GetStream() );
223       if ( stream.is_open() )
224       {
225         stream << DALI_ENV_MULTIPLE_WINDOW_SUPPORT << " " << mIsMultipleWindowSupported << std::endl;
226       }
227       else
228       {
229         DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
230       }
231     }
232   }
233
234   return mIsMultipleWindowSupported;
235 }
236
237 bool ConfigurationManager::IsAdvancedBlendEquationSupported()
238 {
239   if ( !mIsAdvancedBlendEquationSupportedCached )
240   {
241     RetrieveKeysFromConfigFile( mSystemCacheFilePath );
242
243     if ( !mIsAdvancedBlendEquationSupportedCached )
244     {
245       EglImplementation& eglImpl = mEglGraphics->GetEglImplementation();
246       if ( !eglImpl.IsGlesInitialized() )
247       {
248         // Wait until GLES is initialised, but this will happen once.
249         // This method blocks until the render thread has initialised the graphics.
250         mThreadController->WaitForGraphicsInitialization();
251       }
252
253       // Query from GLES and save the cache
254       mIsAdvancedBlendEquationSupported = mEglGraphics->GetGlesInterface().IsAdvancedBlendEquationSupported();
255       mIsAdvancedBlendEquationSupportedCached = true;
256
257       Dali::FileStream configFile( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT );
258       std::fstream& stream = dynamic_cast<std::fstream&>( configFile.GetStream() );
259       if ( stream.is_open() )
260       {
261         stream << DALI_BLEND_EQUATION_ADVANCED_SUPPORT << " " << mIsAdvancedBlendEquationSupported << std::endl;
262       }
263       else
264       {
265         DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
266       }
267     }
268   }
269
270   return mIsAdvancedBlendEquationSupported;
271 }
272
273 } // Adaptor
274
275 } // Internal
276
277 } // Dali