Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-adaptor.git] / adaptors / base / environment-options.cpp
1 /*
2  * Copyright (c) 2017 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 "environment-options.h"
20
21 // EXTERNAL INCLUDES
22 #include <cstdlib>
23 #include <dali/integration-api/render-controller.h>
24 #include <dali/public-api/math/math-utils.h>
25
26 // INTERNAL INCLUDES
27 #include <base/environment-variables.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 namespace
39 {
40 const unsigned int DEFAULT_STATISTICS_LOG_FREQUENCY = 2;
41
42 unsigned int GetIntegerEnvironmentVariable( const char* variable, unsigned int defaultValue )
43 {
44   const char* variableParameter = std::getenv(variable);
45
46   // if the parameter exists convert it to an integer, else return the default value
47   unsigned int intValue = variableParameter ? std::atoi(variableParameter) : defaultValue;
48   return intValue;
49 }
50
51 bool GetIntegerEnvironmentVariable( const char* variable, int& intValue )
52 {
53   const char* variableParameter = std::getenv(variable);
54
55   if( !variableParameter )
56   {
57     return false;
58   }
59   // if the parameter exists convert it to an integer, else return the default value
60   intValue = std::atoi(variableParameter);
61   return true;
62 }
63
64 bool GetFloatEnvironmentVariable( const char* variable, float& floatValue )
65 {
66   const char* variableParameter = std::getenv(variable);
67
68   if( !variableParameter )
69   {
70     return false;
71   }
72   // if the parameter exists convert it to an integer, else return the default value
73   floatValue = std::atof(variableParameter);
74   return true;
75 }
76
77 const char * GetCharEnvironmentVariable( const char * variable )
78 {
79   return std::getenv( variable );
80 }
81
82 } // unnamed namespace
83
84 EnvironmentOptions::EnvironmentOptions()
85 : mWindowName(),
86   mWindowClassName(),
87   mNetworkControl(0),
88   mFpsFrequency(0),
89   mUpdateStatusFrequency(0),
90   mObjectProfilerInterval( 0 ),
91   mPerformanceStatsLevel(0),
92   mPerformanceStatsFrequency( DEFAULT_STATISTICS_LOG_FREQUENCY ),
93   mPerformanceTimeStampOutput(0),
94   mPanGestureLoggingLevel(0),
95   mPanGesturePredictionMode(-1),
96   mPanGesturePredictionAmount(-1), ///< only sets value in pan gesture if greater than 0
97   mPanGestureMaxPredictionAmount(-1),
98   mPanGestureMinPredictionAmount(-1),
99   mPanGesturePredictionAmountAdjustment(-1),
100   mPanGestureSmoothingMode(-1),
101   mPanGestureSmoothingAmount(-1.0f),
102   mPanMinimumDistance(-1),
103   mPanMinimumEvents(-1),
104   mGlesCallTime( 0 ),
105   mWindowWidth( 0 ),
106   mWindowHeight( 0 ),
107   mThreadingMode( ThreadingMode::COMBINED_UPDATE_RENDER ),
108   mRenderRefreshRate( 1 ),
109   mGlesCallAccumulate( false ),
110   mMultiSamplingLevel( 0 ),
111   mMaxTextureSize( 0 ),
112   mLogFunction( NULL )
113 {
114   ParseEnvironmentOptions();
115 }
116
117 EnvironmentOptions::~EnvironmentOptions()
118 {
119 }
120
121 void EnvironmentOptions::SetLogFunction( const Dali::Integration::Log::LogFunction& logFunction )
122 {
123   mLogFunction = logFunction;
124 }
125
126 void EnvironmentOptions::InstallLogFunction() const
127 {
128   Dali::Integration::Log::InstallLogFunction( mLogFunction );
129 }
130
131 void EnvironmentOptions::UnInstallLogFunction() const
132 {
133   Dali::Integration::Log::UninstallLogFunction();
134 }
135
136 unsigned int EnvironmentOptions::GetNetworkControlMode() const
137 {
138   return mNetworkControl;
139 }
140 unsigned int EnvironmentOptions::GetFrameRateLoggingFrequency() const
141 {
142   return mFpsFrequency;
143 }
144
145 unsigned int EnvironmentOptions::GetUpdateStatusLoggingFrequency() const
146 {
147   return mUpdateStatusFrequency;
148 }
149
150 unsigned int EnvironmentOptions::GetObjectProfilerInterval() const
151 {
152   return mObjectProfilerInterval;
153 }
154
155 unsigned int EnvironmentOptions::GetPerformanceStatsLoggingOptions() const
156 {
157   return mPerformanceStatsLevel;
158 }
159 unsigned int EnvironmentOptions::GetPerformanceStatsLoggingFrequency() const
160 {
161   return mPerformanceStatsFrequency;
162 }
163 unsigned int EnvironmentOptions::GetPerformanceTimeStampOutput() const
164 {
165   return mPerformanceTimeStampOutput;
166 }
167
168 unsigned int EnvironmentOptions::GetPanGestureLoggingLevel() const
169 {
170   return mPanGestureLoggingLevel;
171 }
172
173 int EnvironmentOptions::GetPanGesturePredictionMode() const
174 {
175   return mPanGesturePredictionMode;
176 }
177
178 int EnvironmentOptions::GetPanGesturePredictionAmount() const
179 {
180   return mPanGesturePredictionAmount;
181 }
182
183 int EnvironmentOptions::GetPanGestureMaximumPredictionAmount() const
184 {
185   return mPanGestureMaxPredictionAmount;
186 }
187
188 int EnvironmentOptions::GetPanGestureMinimumPredictionAmount() const
189 {
190   return mPanGestureMinPredictionAmount;
191 }
192
193 int EnvironmentOptions::GetPanGesturePredictionAmountAdjustment() const
194 {
195   return mPanGesturePredictionAmountAdjustment;
196 }
197
198 int EnvironmentOptions::GetPanGestureSmoothingMode() const
199 {
200   return mPanGestureSmoothingMode;
201 }
202
203 float EnvironmentOptions::GetPanGestureSmoothingAmount() const
204 {
205   return mPanGestureSmoothingAmount;
206 }
207
208 int EnvironmentOptions::GetMinimumPanDistance() const
209 {
210   return mPanMinimumDistance;
211 }
212
213 int EnvironmentOptions::GetMinimumPanEvents() const
214 {
215   return mPanMinimumEvents;
216 }
217
218 unsigned int EnvironmentOptions::GetWindowWidth() const
219 {
220   return mWindowWidth;
221 }
222
223 unsigned int EnvironmentOptions::GetWindowHeight() const
224 {
225   return mWindowHeight;
226 }
227
228 int EnvironmentOptions::GetGlesCallTime() const
229 {
230   return mGlesCallTime;
231 }
232
233 bool EnvironmentOptions::GetGlesCallAccumulate() const
234 {
235   return mGlesCallAccumulate;
236 }
237
238 const std::string& EnvironmentOptions::GetWindowName() const
239 {
240   return mWindowName;
241 }
242
243 const std::string& EnvironmentOptions::GetWindowClassName() const
244 {
245   return mWindowClassName;
246 }
247
248 ThreadingMode::Type EnvironmentOptions::GetThreadingMode() const
249 {
250   return mThreadingMode;
251 }
252
253 unsigned int EnvironmentOptions::GetRenderRefreshRate() const
254 {
255   return mRenderRefreshRate;
256 }
257
258 unsigned int EnvironmentOptions::GetMultiSamplingLevel() const
259 {
260   return mMultiSamplingLevel;
261 }
262
263 unsigned int EnvironmentOptions::GetMaxTextureSize() const
264 {
265   return mMaxTextureSize;
266 }
267
268 bool EnvironmentOptions::PerformanceServerRequired() const
269 {
270   return ( ( GetPerformanceStatsLoggingOptions() > 0) ||
271            ( GetPerformanceTimeStampOutput() > 0 ) ||
272            ( GetNetworkControlMode() > 0) );
273 }
274
275 void EnvironmentOptions::ParseEnvironmentOptions()
276 {
277   // get logging options
278   mFpsFrequency = GetIntegerEnvironmentVariable( DALI_ENV_FPS_TRACKING, 0 );
279   mUpdateStatusFrequency = GetIntegerEnvironmentVariable( DALI_ENV_UPDATE_STATUS_INTERVAL, 0 );
280   mObjectProfilerInterval = GetIntegerEnvironmentVariable( DALI_ENV_OBJECT_PROFILER_INTERVAL, 0 );
281   mPerformanceStatsLevel = GetIntegerEnvironmentVariable( DALI_ENV_LOG_PERFORMANCE_STATS, 0 );
282   mPerformanceStatsFrequency = GetIntegerEnvironmentVariable( DALI_ENV_LOG_PERFORMANCE_STATS_FREQUENCY, 0 );
283   mPerformanceTimeStampOutput = GetIntegerEnvironmentVariable( DALI_ENV_PERFORMANCE_TIMESTAMP_OUTPUT, 0 );
284   mNetworkControl = GetIntegerEnvironmentVariable( DALI_ENV_NETWORK_CONTROL, 0 );
285   mPanGestureLoggingLevel = GetIntegerEnvironmentVariable( DALI_ENV_LOG_PAN_GESTURE, 0 );
286
287   int predictionMode;
288   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_PREDICTION_MODE, predictionMode) )
289   {
290     mPanGesturePredictionMode = predictionMode;
291   }
292   int predictionAmount(-1);
293   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_PREDICTION_AMOUNT, predictionAmount) )
294   {
295     if( predictionAmount < 0 )
296     {
297       // do not support times in the past
298       predictionAmount = 0;
299     }
300     mPanGesturePredictionAmount = predictionAmount;
301   }
302   int minPredictionAmount(-1);
303   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MIN_PREDICTION_AMOUNT, minPredictionAmount) )
304   {
305     if( minPredictionAmount < 0 )
306     {
307       // do not support times in the past
308       minPredictionAmount = 0;
309     }
310     mPanGestureMinPredictionAmount = minPredictionAmount;
311   }
312   int maxPredictionAmount(-1);
313   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MAX_PREDICTION_AMOUNT, maxPredictionAmount) )
314   {
315     if( minPredictionAmount > -1 && maxPredictionAmount < minPredictionAmount )
316     {
317       // maximum amount should not be smaller than minimum amount
318       maxPredictionAmount = minPredictionAmount;
319     }
320     mPanGestureMaxPredictionAmount = maxPredictionAmount;
321   }
322   int predictionAmountAdjustment(-1);
323   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_PREDICTION_AMOUNT_ADJUSTMENT, predictionAmountAdjustment) )
324   {
325     if( predictionAmountAdjustment < 0 )
326     {
327       // negative amount doesn't make sense
328       predictionAmountAdjustment = 0;
329     }
330     mPanGesturePredictionAmountAdjustment = predictionAmountAdjustment;
331   }
332   int smoothingMode;
333   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_SMOOTHING_MODE, smoothingMode) )
334   {
335     mPanGestureSmoothingMode = smoothingMode;
336   }
337   float smoothingAmount = 1.0f;
338   if( GetFloatEnvironmentVariable(DALI_ENV_PAN_SMOOTHING_AMOUNT, smoothingAmount) )
339   {
340     smoothingAmount = Clamp(smoothingAmount, 0.0f, 1.0f);
341     mPanGestureSmoothingAmount = smoothingAmount;
342   }
343
344   int minimumDistance(-1);
345   if ( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MINIMUM_DISTANCE, minimumDistance ))
346   {
347     mPanMinimumDistance = minimumDistance;
348   }
349
350   int minimumEvents(-1);
351   if ( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MINIMUM_EVENTS, minimumEvents ))
352   {
353     mPanMinimumEvents = minimumEvents;
354   }
355
356   int glesCallTime(0);
357   if ( GetIntegerEnvironmentVariable(DALI_GLES_CALL_TIME, glesCallTime ))
358   {
359     mGlesCallTime = glesCallTime;
360   }
361
362   int glesCallAccumulate( 0 );
363   if ( GetIntegerEnvironmentVariable( DALI_GLES_CALL_ACCUMULATE, glesCallAccumulate ) )
364   {
365     mGlesCallAccumulate = glesCallAccumulate != 0;
366   }
367
368   int windowWidth(0), windowHeight(0);
369   if ( GetIntegerEnvironmentVariable( DALI_WINDOW_WIDTH, windowWidth ) && GetIntegerEnvironmentVariable( DALI_WINDOW_HEIGHT, windowHeight ) )
370   {
371     mWindowWidth = windowWidth;
372     mWindowHeight = windowHeight;
373   }
374
375   const char * windowName = GetCharEnvironmentVariable( DALI_WINDOW_NAME );
376   if ( windowName )
377   {
378     mWindowName = windowName;
379   }
380
381   const char * windowClassName = GetCharEnvironmentVariable( DALI_WINDOW_CLASS_NAME );
382   if ( windowClassName )
383   {
384     mWindowClassName = windowClassName;
385   }
386
387   int threadingMode(0);
388   if ( GetIntegerEnvironmentVariable( DALI_THREADING_MODE, threadingMode ) )
389   {
390     switch( threadingMode )
391     {
392       case ThreadingMode::SEPARATE_UPDATE_RENDER:
393       case ThreadingMode::COMBINED_UPDATE_RENDER:
394       case ThreadingMode::SINGLE_THREADED:
395       {
396         mThreadingMode = static_cast< ThreadingMode::Type >( threadingMode );
397         break;
398       }
399     }
400   }
401
402   int renderRefreshRate(0);
403   if ( GetIntegerEnvironmentVariable( DALI_REFRESH_RATE, renderRefreshRate ) )
404   {
405     // Only change it if it's valid
406     if( renderRefreshRate > 1 )
407     {
408       mRenderRefreshRate = renderRefreshRate;
409     }
410   }
411
412   int multiSamplingLevel( 0 );
413   if( GetIntegerEnvironmentVariable( DALI_ENV_MULTI_SAMPLING_LEVEL, multiSamplingLevel ) )
414   {
415     if( multiSamplingLevel > 0 )
416     {
417       mMultiSamplingLevel = multiSamplingLevel;
418     }
419   }
420
421   int maxTextureSize( 0 );
422   if( GetIntegerEnvironmentVariable( DALI_ENV_MAX_TEXTURE_SIZE, maxTextureSize ) )
423   {
424     if( maxTextureSize > 0 )
425     {
426       mMaxTextureSize = maxTextureSize;
427     }
428   }
429 }
430
431 } // Adaptor
432
433 } // Internal
434
435 } // Dali