Fix the crash when it assumes the platform does not support multiple windows as GL...
[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
47 bool RetrieveKeyFromFile( std::fstream& stream, std::string key, std::string& value )
48 {
49   bool keyFound = false;
50
51   std::string line;
52   while( std::getline( stream, line ) )
53   {
54     line.erase( line.find_last_not_of( " \t\r\n" ) + 1 );
55     line.erase( 0, line.find_first_not_of( " \t\r\n" ) );
56     if( '#' == *( line.cbegin() ) || line == "" )
57     {
58       continue;
59     }
60
61     std::istringstream stream( line );
62     std::string name;
63     std::getline(stream, name, ' ');
64     if( name == key )
65     {
66       std::getline(stream, value);
67       keyFound = true;
68       break;
69     }
70   }
71
72   return keyFound;
73 }
74
75
76 } // unnamed namespace
77
78 ConfigurationManager::ConfigurationManager( std::string systemCachePath, EglGraphics* eglGraphics, ThreadController* threadController )
79 : mSystemCacheFilePath( systemCachePath + SYSTEM_CACHE_FILE ),
80   mFileStream( new Dali::FileStream( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT ) ),
81   mEglGraphics( eglGraphics ),
82   mThreadController( threadController ),
83   mMaxTextureSize( 0u ),
84   mIsMultipleWindowSupported( true ),
85   mMaxTextureSizeCached( false ) ,
86   mIsMultipleWindowSupportedCached( false )
87 {
88 }
89
90 ConfigurationManager::~ConfigurationManager()
91 {
92 }
93
94 unsigned int ConfigurationManager::GetMaxTextureSize()
95 {
96   if ( !mMaxTextureSizeCached )
97   {
98     std::fstream& configFile = dynamic_cast<std::fstream&>( mFileStream->GetStream() );
99     if( configFile.is_open() )
100     {
101       std::string environmentVariableValue;
102       if( RetrieveKeyFromFile( configFile, DALI_ENV_MAX_TEXTURE_SIZE, environmentVariableValue ) )
103       {
104         mMaxTextureSize = std::atoi( environmentVariableValue.c_str() );
105       }
106       else
107       {
108         GlImplementation& mGLES = mEglGraphics->GetGlesInterface();
109         mMaxTextureSize = mGLES.GetMaxTextureSize();
110
111         configFile.clear();
112         configFile << DALI_ENV_MAX_TEXTURE_SIZE << " " << mMaxTextureSize << std::endl;
113       }
114
115       mMaxTextureSizeCached = true;
116
117       if ( mIsMultipleWindowSupportedCached )
118       {
119         configFile.close();
120       }
121     }
122     else
123     {
124       DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
125     }
126   }
127
128   return mMaxTextureSize;
129 }
130
131 bool ConfigurationManager::IsMultipleWindowSupported()
132 {
133   if ( !mIsMultipleWindowSupportedCached )
134   {
135     std::fstream& configFile = dynamic_cast<std::fstream&>( mFileStream->GetStream() );
136     if( configFile.is_open() )
137     {
138       std::string environmentVariableValue;
139       if( RetrieveKeyFromFile( configFile, DALI_ENV_MULTIPLE_WINDOW_SUPPORT, environmentVariableValue ) )
140       {
141         mIsMultipleWindowSupported = std::atoi( environmentVariableValue.c_str() );
142       }
143       else
144       {
145         EglImplementation& eglImpl = mEglGraphics->GetEglImplementation();
146         if ( !eglImpl.IsGlesInitialized() )
147         {
148           // Wait until GLES is initialised, but this will happen once.
149           // This method blocks until the render thread has initialised the graphics.
150           mThreadController->WaitForGraphicsInitialization();
151         }
152
153         // Query from GLES and save the cache
154         mIsMultipleWindowSupported = eglImpl.IsSurfacelessContextSupported();
155
156         configFile.clear();
157         configFile << DALI_ENV_MULTIPLE_WINDOW_SUPPORT << " " << mIsMultipleWindowSupported << std::endl;
158       }
159
160       mIsMultipleWindowSupportedCached = true;
161
162       if ( mMaxTextureSizeCached )
163       {
164         configFile.close();
165       }
166     }
167     else
168     {
169       DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
170     }
171   }
172
173   return mIsMultipleWindowSupported;
174 }
175
176 } // Adaptor
177
178 } // Internal
179
180 } // Dali