Use existing callback ID for recurring callbacks
[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 RetrieveKeyFromConfigFile( std::iostream& stream, const 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   mEglGraphics( eglGraphics ),
81   mThreadController( threadController ),
82   mMaxTextureSize( 0u ),
83   mIsMultipleWindowSupported( true ),
84   mMaxTextureSizeCached( false ) ,
85   mIsMultipleWindowSupportedCached( false )
86 {
87 }
88
89 ConfigurationManager::~ConfigurationManager()
90 {
91 }
92
93 void ConfigurationManager::RetrieveKeysFromConfigFile( const std::string& configFilePath )
94 {
95   Dali::FileStream configFile( configFilePath, Dali::FileStream::READ | Dali::FileStream::TEXT );
96   std::iostream& stream = configFile.GetStream();
97   if( stream.rdbuf()->in_avail() )
98   {
99     std::string value;
100     if( !mMaxTextureSizeCached &&
101         RetrieveKeyFromConfigFile( stream, DALI_ENV_MAX_TEXTURE_SIZE, value ) )
102     {
103       mMaxTextureSize = std::atoi( value.c_str() );
104       mMaxTextureSizeCached = true;
105     }
106
107     if( !mIsMultipleWindowSupportedCached &&
108         RetrieveKeyFromConfigFile( stream, DALI_ENV_MULTIPLE_WINDOW_SUPPORT, value ) )
109     {
110       mIsMultipleWindowSupported = std::atoi( value.c_str() );
111       mIsMultipleWindowSupportedCached = true;
112     }
113   }
114 }
115
116 unsigned int ConfigurationManager::GetMaxTextureSize()
117 {
118   if( !mMaxTextureSizeCached )
119   {
120     RetrieveKeysFromConfigFile( mSystemCacheFilePath );
121
122     if( !mMaxTextureSizeCached )
123     {
124       GlImplementation& mGLES = mEglGraphics->GetGlesInterface();
125       mMaxTextureSize = mGLES.GetMaxTextureSize();
126       mMaxTextureSizeCached = true;
127
128       Dali::FileStream configFile( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT );
129       std::fstream& stream = dynamic_cast<std::fstream&>( configFile.GetStream() );
130       if( stream.is_open() )
131       {
132         stream << DALI_ENV_MAX_TEXTURE_SIZE << " " << mMaxTextureSize << std::endl;
133       }
134       else
135       {
136         DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
137       }
138     }
139   }
140
141   return mMaxTextureSize;
142 }
143
144 bool ConfigurationManager::IsMultipleWindowSupported()
145 {
146   if ( !mIsMultipleWindowSupportedCached )
147   {
148     RetrieveKeysFromConfigFile( mSystemCacheFilePath );
149
150     if ( !mIsMultipleWindowSupportedCached )
151     {
152       EglImplementation& eglImpl = mEglGraphics->GetEglImplementation();
153       if ( !eglImpl.IsGlesInitialized() )
154       {
155         // Wait until GLES is initialised, but this will happen once.
156         // This method blocks until the render thread has initialised the graphics.
157         mThreadController->WaitForGraphicsInitialization();
158       }
159
160       // Query from GLES and save the cache
161       mIsMultipleWindowSupported = eglImpl.IsSurfacelessContextSupported();
162       mIsMultipleWindowSupportedCached = true;
163
164       Dali::FileStream configFile( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT );
165       std::fstream& stream = dynamic_cast<std::fstream&>( configFile.GetStream() );
166       if ( stream.is_open() )
167       {
168         stream << DALI_ENV_MULTIPLE_WINDOW_SUPPORT << " " << mIsMultipleWindowSupported << std::endl;
169       }
170       else
171       {
172         DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
173       }
174     }
175   }
176
177   return mIsMultipleWindowSupported;
178 }
179
180 } // Adaptor
181
182 } // Internal
183
184 } // Dali