Merge "Use existing callback ID for recurring callbacks" into devel/master
[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   mIsMultipleWindowSupported( true ),
86   mIsAdvancedBlendEquationSupported( true ),
87   mMaxTextureSizeCached( false ),
88   mIsMultipleWindowSupportedCached( false ),
89   mIsAdvancedBlendEquationSupportedCached( false ),
90   mGlslVersionCached( false )
91 {
92 }
93
94 ConfigurationManager::~ConfigurationManager()
95 {
96 }
97
98 void ConfigurationManager::RetrieveKeysFromConfigFile( const std::string& configFilePath )
99 {
100   Dali::FileStream configFile( configFilePath, Dali::FileStream::READ | Dali::FileStream::TEXT );
101   std::iostream& stream = configFile.GetStream();
102   if( stream.rdbuf()->in_avail() )
103   {
104     std::string value;
105     if( !mMaxTextureSizeCached &&
106         RetrieveKeyFromConfigFile( stream, DALI_ENV_MAX_TEXTURE_SIZE, value ) )
107     {
108       mMaxTextureSize = std::atoi( value.c_str() );
109       mMaxTextureSizeCached = true;
110     }
111
112     if( !mGlslVersionCached &&
113         RetrieveKeyFromConfigFile( stream, DALI_GLSL_VERSION, value ) )
114     {
115       mGlslVersion = std::atoi( value.c_str() );
116       mGlslVersionCached = true;
117     }
118
119     if( !mIsMultipleWindowSupportedCached &&
120         RetrieveKeyFromConfigFile( stream, DALI_ENV_MULTIPLE_WINDOW_SUPPORT, value ) )
121     {
122       mIsMultipleWindowSupported = std::atoi( value.c_str() );
123       mIsMultipleWindowSupportedCached = true;
124     }
125
126     if( !mIsAdvancedBlendEquationSupportedCached &&
127         RetrieveKeyFromConfigFile( stream, DALI_BLEND_EQUATION_ADVANCED_SUPPORT, value ) )
128     {
129       mIsAdvancedBlendEquationSupported = std::atoi( value.c_str() );
130       mIsAdvancedBlendEquationSupportedCached = true;
131     }
132   }
133 }
134
135 uint32_t ConfigurationManager::GetMaxTextureSize()
136 {
137   if( !mMaxTextureSizeCached )
138   {
139     RetrieveKeysFromConfigFile( mSystemCacheFilePath );
140
141     if( !mMaxTextureSizeCached )
142     {
143       GlImplementation& mGLES = mEglGraphics->GetGlesInterface();
144       mMaxTextureSize = mGLES.GetMaxTextureSize();
145       mMaxTextureSizeCached = true;
146
147       Dali::FileStream configFile( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT );
148       std::fstream& stream = dynamic_cast<std::fstream&>( configFile.GetStream() );
149       if( stream.is_open() )
150       {
151         stream << DALI_ENV_MAX_TEXTURE_SIZE << " " << mMaxTextureSize << std::endl;
152       }
153       else
154       {
155         DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
156       }
157     }
158   }
159
160   return mMaxTextureSize;
161 }
162
163 uint32_t ConfigurationManager::GetShadingLanguageVersion()
164 {
165   if ( !mGlslVersionCached )
166   {
167     RetrieveKeysFromConfigFile( mSystemCacheFilePath );
168
169     if ( !mGlslVersionCached )
170     {
171       EglImplementation& eglImpl = mEglGraphics->GetEglImplementation();
172       if ( !eglImpl.IsGlesInitialized() )
173       {
174         // Wait until GLES is initialised, but this will happen once.
175         // This method blocks until the render thread has initialised the graphics.
176         mThreadController->WaitForGraphicsInitialization();
177       }
178
179       // Query from GLES and save the cache
180       mGlslVersion = mEglGraphics->GetGlesInterface().GetShadingLanguageVersion();
181       DALI_LOG_ERROR("mGlslVersion : %d\n", mGlslVersion);
182       mGlslVersionCached = true;
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_GLSL_VERSION << " " << mGlslVersion << 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 mGlslVersion;
198 }
199
200 bool ConfigurationManager::IsMultipleWindowSupported()
201 {
202   if ( !mIsMultipleWindowSupportedCached )
203   {
204     RetrieveKeysFromConfigFile( mSystemCacheFilePath );
205
206     if ( !mIsMultipleWindowSupportedCached )
207     {
208       EglImplementation& eglImpl = mEglGraphics->GetEglImplementation();
209       if ( !eglImpl.IsGlesInitialized() )
210       {
211         // Wait until GLES is initialised, but this will happen once.
212         // This method blocks until the render thread has initialised the graphics.
213         mThreadController->WaitForGraphicsInitialization();
214       }
215
216       // Query from GLES and save the cache
217       mIsMultipleWindowSupported = eglImpl.IsSurfacelessContextSupported();
218       mIsMultipleWindowSupportedCached = true;
219
220       Dali::FileStream configFile( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT );
221       std::fstream& stream = dynamic_cast<std::fstream&>( configFile.GetStream() );
222       if ( stream.is_open() )
223       {
224         stream << DALI_ENV_MULTIPLE_WINDOW_SUPPORT << " " << mIsMultipleWindowSupported << std::endl;
225       }
226       else
227       {
228         DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
229       }
230     }
231   }
232
233   return mIsMultipleWindowSupported;
234 }
235
236 bool ConfigurationManager::IsAdvancedBlendEquationSupported()
237 {
238   if ( !mIsAdvancedBlendEquationSupportedCached )
239   {
240     RetrieveKeysFromConfigFile( mSystemCacheFilePath );
241
242     if ( !mIsAdvancedBlendEquationSupportedCached )
243     {
244       EglImplementation& eglImpl = mEglGraphics->GetEglImplementation();
245       if ( !eglImpl.IsGlesInitialized() )
246       {
247         // Wait until GLES is initialised, but this will happen once.
248         // This method blocks until the render thread has initialised the graphics.
249         mThreadController->WaitForGraphicsInitialization();
250       }
251
252       // Query from GLES and save the cache
253       mIsAdvancedBlendEquationSupported = mEglGraphics->GetGlesInterface().IsAdvancedBlendEquationSupported();
254       mIsAdvancedBlendEquationSupportedCached = true;
255
256       Dali::FileStream configFile( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT );
257       std::fstream& stream = dynamic_cast<std::fstream&>( configFile.GetStream() );
258       if ( stream.is_open() )
259       {
260         stream << DALI_BLEND_EQUATION_ADVANCED_SUPPORT << " " << mIsAdvancedBlendEquationSupported << std::endl;
261       }
262       else
263       {
264         DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
265       }
266     }
267   }
268
269   return mIsAdvancedBlendEquationSupported;
270 }
271
272 } // Adaptor
273
274 } // Internal
275
276 } // Dali