Wait gles initialize when we try to get MaxTextureSize
[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_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       if(!mGraphics->IsInitialized())
123       {
124         // Wait until Graphics Subsystem is initialised, but this will happen once.
125         // This method blocks until the render thread has initialised the graphics.
126         mThreadController->WaitForGraphicsInitialization();
127       }
128
129       mMaxTextureSize       = mGraphics->GetMaxTextureSize();
130       mMaxTextureSizeCached = true;
131
132       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
133       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
134       if(stream.is_open())
135       {
136         stream << DALI_ENV_MAX_TEXTURE_SIZE << " " << mMaxTextureSize << std::endl;
137       }
138       else
139       {
140         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
141       }
142     }
143   }
144
145   return mMaxTextureSize;
146 }
147
148 uint32_t ConfigurationManager::GetShadingLanguageVersion()
149 {
150   if(!mShaderLanguageVersionCached)
151   {
152     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
153
154     if(!mShaderLanguageVersionCached)
155     {
156       if(!mGraphics->IsInitialized())
157       {
158         // Wait until Graphics Subsystem is initialised, but this will happen once.
159         // This method blocks until the render thread has initialised the graphics.
160         mThreadController->WaitForGraphicsInitialization();
161       }
162
163       // Query from graphics and save the cache
164       mShaderLanguageVersion       = mGraphics->GetShaderLanguageVersion();
165       mShaderLanguageVersionCached = true;
166
167       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
168       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
169       if(stream.is_open())
170       {
171         stream << DALI_GLSL_VERSION << " " << mShaderLanguageVersion << std::endl;
172       }
173       else
174       {
175         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
176       }
177     }
178   }
179
180   return mShaderLanguageVersion;
181 }
182
183 bool ConfigurationManager::IsMultipleWindowSupported()
184 {
185   if(!mIsMultipleWindowSupportedCached)
186   {
187     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
188
189     if(!mIsMultipleWindowSupportedCached)
190     {
191       if(!mGraphics->IsInitialized())
192       {
193         // Wait until Graphics Subsystem is initialised, but this will happen once.
194         // This method blocks until the render thread has initialised the graphics.
195         mThreadController->WaitForGraphicsInitialization();
196       }
197
198       // Query from Graphics Subsystem and save the cache
199       mIsMultipleWindowSupported       = mGraphics->IsResourceContextSupported();
200       mIsMultipleWindowSupportedCached = true;
201
202       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
203       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
204       if(stream.is_open())
205       {
206         stream << DALI_ENV_MULTIPLE_WINDOW_SUPPORT << " " << mIsMultipleWindowSupported << std::endl;
207       }
208       else
209       {
210         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
211       }
212     }
213   }
214
215   return mIsMultipleWindowSupported;
216 }
217
218 bool ConfigurationManager::IsAdvancedBlendEquationSupported()
219 {
220   if(!mIsAdvancedBlendEquationSupportedCached)
221   {
222     RetrieveKeysFromConfigFile(mSystemCacheFilePath);
223
224     if(!mIsAdvancedBlendEquationSupportedCached)
225     {
226       if(!mGraphics->IsInitialized())
227       {
228         // Wait until graphics subsystem is initialised, but this will happen once per factory reset.
229         // This method blocks until the render thread has initialised the graphics.
230         mThreadController->WaitForGraphicsInitialization();
231       }
232
233       // Query from Graphics Subsystem and save the cache
234       mIsAdvancedBlendEquationSupported       = mGraphics->IsAdvancedBlendEquationSupported();
235       mIsAdvancedBlendEquationSupportedCached = true;
236
237       Dali::FileStream configFile(mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT);
238       std::fstream&    stream = dynamic_cast<std::fstream&>(configFile.GetStream());
239       if(stream.is_open())
240       {
241         stream << DALI_BLEND_EQUATION_ADVANCED_SUPPORT << " " << mIsAdvancedBlendEquationSupported << std::endl;
242       }
243       else
244       {
245         DALI_LOG_ERROR("Fail to open file : %s\n", mSystemCacheFilePath.c_str());
246       }
247     }
248   }
249
250   return mIsAdvancedBlendEquationSupported;
251 }
252
253 } // namespace Adaptor
254
255 } // namespace Internal
256
257 } // namespace Dali