(Environment Options) Reduce cyclomatic complexity of ParseEnvironmentOptions
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / environment-options.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/environment-options.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstdlib>
23 #include <functional>
24 #include <dali/integration-api/render-controller.h>
25 #include <dali/public-api/math/math-utils.h>
26
27 // INTERNAL INCLUDES
28 #include <dali/internal/trace/common/trace-factory.h>
29 #include <dali/internal/system/common/environment-variables.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 namespace
41 {
42 const unsigned int DEFAULT_STATISTICS_LOG_FREQUENCY = 2;
43 const int DEFAULT_MULTI_SAMPLING_LEVEL = -1;
44 const bool DEFAULT_DEPTH_BUFFER_REQUIRED_SETTING = true;
45 const bool DEFAULT_STENCIL_BUFFER_REQUIRED_SETTING = true;
46 const bool DEFAULT_PARTIAL_UPDATE_REQUIRED_SETTING = true;
47
48 unsigned int GetEnvironmentVariable( const char* variable, unsigned int defaultValue )
49 {
50   const char* variableParameter = std::getenv(variable);
51
52   // if the parameter exists convert it to an integer, else return the default value
53   unsigned int intValue = variableParameter ? std::atoi(variableParameter) : defaultValue;
54   return intValue;
55 }
56
57 bool GetEnvironmentVariable( const char* variable, int& intValue )
58 {
59   const char* variableParameter = std::getenv(variable);
60
61   if( !variableParameter )
62   {
63     return false;
64   }
65   // if the parameter exists convert it to an integer, else return the default value
66   intValue = std::atoi(variableParameter);
67   return true;
68 }
69
70 bool GetEnvironmentVariable( const char* variable, float& floatValue )
71 {
72   const char* variableParameter = std::getenv(variable);
73
74   if( !variableParameter )
75   {
76     return false;
77   }
78   // if the parameter exists convert it to an integer, else return the default value
79   floatValue = std::atof(variableParameter);
80   return true;
81 }
82
83 void SetFromEnvironmentVariable(const char* variable, std::string& stringValue)
84 {
85   const char * charValue = std::getenv( variable );
86   if(charValue)
87   {
88     stringValue = charValue;
89   }
90 }
91
92 template<typename Type>
93 void SetFromEnvironmentVariable(const char* variable, Type& memberVariable)
94 {
95   Type envVarValue = -1;
96   if(GetEnvironmentVariable(variable, envVarValue))
97   {
98     memberVariable = envVarValue;
99   }
100 }
101
102 template<typename Type>
103 void SetFromEnvironmentVariable(const char* variable, std::function<void(Type)> function)
104 {
105   Type envVarValue = -1;
106   if(GetEnvironmentVariable(variable, envVarValue))
107   {
108     function(envVarValue);
109   }
110 }
111
112 /// Provides a functor which ensures a non-negative number is set for the given member variable
113 struct MinimumZero
114 {
115   MinimumZero(int& memberValue)
116   : mMemberValue(memberValue)
117   {
118   }
119
120   void operator()(int value)
121   {
122     // Negative Amounts do not make sense
123     mMemberValue = std::max(0, value);
124   }
125
126   int& mMemberValue;
127 };
128
129 /// Provides a functor which clamps the environment variable between 0.0f and 1.0f
130 struct ClampBetweenZeroAndOne
131 {
132   ClampBetweenZeroAndOne(float& memberValue)
133   : mMemberValue(memberValue)
134   {
135   }
136
137   void operator()(float value)
138   {
139     value = Clamp(value, 0.0f, 1.0f);
140     mMemberValue = value;
141   }
142
143   float& mMemberValue;
144 };
145
146 /// Provides a functor which only sets the member variable if the environment variable value is greater than the specified constant
147 struct GreaterThan
148 {
149   GreaterThan(unsigned int& memberValue, int greaterThanValue)
150   : mMemberValue(memberValue),
151     mGreaterThanValue(greaterThanValue)
152   {
153   }
154
155   void operator()(int value)
156   {
157     if(value > mGreaterThanValue)
158     {
159       mMemberValue = value;
160     }
161   }
162
163   unsigned int& mMemberValue;
164   const int mGreaterThanValue;
165 };
166
167 /// Provides a functor which sets the member to 1 if if the environment variable value is not zero
168 struct EnableIfNonZero
169 {
170   EnableIfNonZero(int& memberValue) : mMemberValue(memberValue) {}
171
172   void operator()(int value)
173   {
174     mMemberValue = ( value == 0 ) ? 0 : 1;
175   }
176
177   int& mMemberValue;
178 };
179
180 /// Provides a functor which sets the member to false if the environment variable value is not zero
181 struct DisableIfNonZero
182 {
183   DisableIfNonZero(bool& memberValue) : mMemberValue(memberValue) {}
184
185   void operator()(int value)
186   {
187     if( value > 0 )
188     {
189       mMemberValue = false;
190     }
191   }
192
193   bool& mMemberValue;
194 };
195
196
197 } // unnamed namespace
198
199 EnvironmentOptions::EnvironmentOptions()
200 : mLogFunction( NULL ),
201   mWindowName(),
202   mWindowClassName(),
203   mNetworkControl( 0 ),
204   mFpsFrequency( 0 ),
205   mUpdateStatusFrequency( 0 ),
206   mObjectProfilerInterval( 0 ),
207   mPerformanceStatsLevel( 0 ),
208   mPerformanceStatsFrequency( DEFAULT_STATISTICS_LOG_FREQUENCY ),
209   mPerformanceTimeStampOutput( 0 ),
210   mPanGestureLoggingLevel( 0 ),
211   mWindowWidth( 0u ),
212   mWindowHeight( 0u ),
213   mRenderRefreshRate( 1u ),
214   mMaxTextureSize( 0 ),
215   mRenderToFboInterval( 0u ),
216   mPanGesturePredictionMode( -1 ),
217   mPanGesturePredictionAmount( -1 ), ///< only sets value in pan gesture if greater than 0
218   mPanGestureMaxPredictionAmount( -1 ),
219   mPanGestureMinPredictionAmount( -1 ),
220   mPanGesturePredictionAmountAdjustment( -1 ),
221   mPanGestureSmoothingMode( -1 ),
222   mPanGestureSmoothingAmount( -1.0f ),
223   mPanGestureUseActualTimes( -1 ),
224   mPanGestureInterpolationTimeRange( -1 ),
225   mPanGestureScalarOnlyPredictionEnabled( -1 ),
226   mPanGestureTwoPointPredictionEnabled( -1 ),
227   mPanGestureTwoPointInterpolatePastTime( -1 ),
228   mPanGestureTwoPointVelocityBias( -1.0f ),
229   mPanGestureTwoPointAccelerationBias( -1.0f ),
230   mPanGestureMultitapSmoothingRange( -1 ),
231   mPanMinimumDistance( -1 ),
232   mPanMinimumEvents( -1 ),
233   mPinchMinimumDistance( -1.0f ),
234   mPinchMinimumTouchEvents( -1 ),
235   mPinchMinimumTouchEventsAfterStart( -1 ),
236   mRotationMinimumTouchEvents( -1 ),
237   mRotationMinimumTouchEventsAfterStart( -1 ),
238   mLongPressMinimumHoldingTime( -1 ),
239   mGlesCallTime( 0 ),
240   mMultiSamplingLevel( DEFAULT_MULTI_SAMPLING_LEVEL ),
241   mThreadingMode( ThreadingMode::COMBINED_UPDATE_RENDER ),
242   mGlesCallAccumulate( false ),
243   mDepthBufferRequired( DEFAULT_DEPTH_BUFFER_REQUIRED_SETTING ),
244   mStencilBufferRequired( DEFAULT_STENCIL_BUFFER_REQUIRED_SETTING ),
245   mPartialUpdateRequired( DEFAULT_PARTIAL_UPDATE_REQUIRED_SETTING )
246 {
247   ParseEnvironmentOptions();
248 }
249
250 EnvironmentOptions::~EnvironmentOptions()
251 {
252 }
253
254 void EnvironmentOptions::CreateTraceManager( PerformanceInterface* performanceInterface )
255 {
256   mTraceManager = TraceManagerFactory::CreateTraceFactory( performanceInterface );
257 }
258
259 void EnvironmentOptions::InstallTraceFunction() const
260 {
261   if( mTraceManager )
262   {
263     mTraceManager->Initialise();
264   }
265 }
266
267 void EnvironmentOptions::SetLogFunction( const Dali::Integration::Log::LogFunction& logFunction )
268 {
269   mLogFunction = logFunction;
270 }
271
272 void EnvironmentOptions::InstallLogFunction() const
273 {
274   Dali::Integration::Log::InstallLogFunction( mLogFunction );
275 }
276
277 void EnvironmentOptions::UnInstallLogFunction() const
278 {
279   Dali::Integration::Log::UninstallLogFunction();
280 }
281
282 unsigned int EnvironmentOptions::GetNetworkControlMode() const
283 {
284   return mNetworkControl;
285 }
286 unsigned int EnvironmentOptions::GetFrameRateLoggingFrequency() const
287 {
288   return mFpsFrequency;
289 }
290
291 unsigned int EnvironmentOptions::GetUpdateStatusLoggingFrequency() const
292 {
293   return mUpdateStatusFrequency;
294 }
295
296 unsigned int EnvironmentOptions::GetObjectProfilerInterval() const
297 {
298   return mObjectProfilerInterval;
299 }
300
301 unsigned int EnvironmentOptions::GetPerformanceStatsLoggingOptions() const
302 {
303   return mPerformanceStatsLevel;
304 }
305 unsigned int EnvironmentOptions::GetPerformanceStatsLoggingFrequency() const
306 {
307   return mPerformanceStatsFrequency;
308 }
309 unsigned int EnvironmentOptions::GetPerformanceTimeStampOutput() const
310 {
311   return mPerformanceTimeStampOutput;
312 }
313
314 unsigned int EnvironmentOptions::GetPanGestureLoggingLevel() const
315 {
316   return mPanGestureLoggingLevel;
317 }
318
319 int EnvironmentOptions::GetPanGesturePredictionMode() const
320 {
321   return mPanGesturePredictionMode;
322 }
323
324 int EnvironmentOptions::GetPanGesturePredictionAmount() const
325 {
326   return mPanGesturePredictionAmount;
327 }
328
329 int EnvironmentOptions::GetPanGestureMaximumPredictionAmount() const
330 {
331   return mPanGestureMaxPredictionAmount;
332 }
333
334 int EnvironmentOptions::GetPanGestureMinimumPredictionAmount() const
335 {
336   return mPanGestureMinPredictionAmount;
337 }
338
339 int EnvironmentOptions::GetPanGesturePredictionAmountAdjustment() const
340 {
341   return mPanGesturePredictionAmountAdjustment;
342 }
343
344 int EnvironmentOptions::GetPanGestureSmoothingMode() const
345 {
346   return mPanGestureSmoothingMode;
347 }
348
349 float EnvironmentOptions::GetPanGestureSmoothingAmount() const
350 {
351   return mPanGestureSmoothingAmount;
352 }
353
354 int EnvironmentOptions::GetPanGestureUseActualTimes() const
355 {
356   return mPanGestureUseActualTimes;
357 }
358
359 int EnvironmentOptions::GetPanGestureInterpolationTimeRange() const
360 {
361   return mPanGestureInterpolationTimeRange;
362 }
363
364 int EnvironmentOptions::GetPanGestureScalarOnlyPredictionEnabled() const
365 {
366   return mPanGestureScalarOnlyPredictionEnabled;
367 }
368
369 int EnvironmentOptions::GetPanGestureTwoPointPredictionEnabled() const
370 {
371   return mPanGestureTwoPointPredictionEnabled;
372 }
373
374 int EnvironmentOptions::GetPanGestureTwoPointInterpolatePastTime() const
375 {
376   return mPanGestureTwoPointInterpolatePastTime;
377 }
378
379 float EnvironmentOptions::GetPanGestureTwoPointVelocityBias() const
380 {
381   return mPanGestureTwoPointVelocityBias;
382 }
383
384 float EnvironmentOptions::GetPanGestureTwoPointAccelerationBias() const
385 {
386   return mPanGestureTwoPointAccelerationBias;
387 }
388
389 int EnvironmentOptions::GetPanGestureMultitapSmoothingRange() const
390 {
391   return mPanGestureMultitapSmoothingRange;
392 }
393
394 int EnvironmentOptions::GetMinimumPanDistance() const
395 {
396   return mPanMinimumDistance;
397 }
398
399 int EnvironmentOptions::GetMinimumPanEvents() const
400 {
401   return mPanMinimumEvents;
402 }
403
404 float EnvironmentOptions::GetMinimumPinchDistance() const
405 {
406   return mPinchMinimumDistance;
407 }
408
409 int EnvironmentOptions::GetMinimumPinchTouchEvents() const
410 {
411   return mPinchMinimumTouchEvents;
412 }
413
414 int EnvironmentOptions::GetMinimumPinchTouchEventsAfterStart() const
415 {
416   return mPinchMinimumTouchEventsAfterStart;
417 }
418
419 int EnvironmentOptions::GetMinimumRotationTouchEvents() const
420 {
421   return mRotationMinimumTouchEvents;
422 }
423
424 int EnvironmentOptions::GetMinimumRotationTouchEventsAfterStart() const
425 {
426   return mRotationMinimumTouchEventsAfterStart;
427 }
428
429 int EnvironmentOptions::GetLongPressMinimumHoldingTime() const
430 {
431   return mLongPressMinimumHoldingTime;
432 }
433
434 unsigned int EnvironmentOptions::GetWindowWidth() const
435 {
436   return mWindowWidth;
437 }
438
439 unsigned int EnvironmentOptions::GetWindowHeight() const
440 {
441   return mWindowHeight;
442 }
443
444 int EnvironmentOptions::GetGlesCallTime() const
445 {
446   return mGlesCallTime;
447 }
448
449 bool EnvironmentOptions::GetGlesCallAccumulate() const
450 {
451   return mGlesCallAccumulate;
452 }
453
454 const std::string& EnvironmentOptions::GetWindowName() const
455 {
456   return mWindowName;
457 }
458
459 const std::string& EnvironmentOptions::GetWindowClassName() const
460 {
461   return mWindowClassName;
462 }
463
464 ThreadingMode::Type EnvironmentOptions::GetThreadingMode() const
465 {
466   return mThreadingMode;
467 }
468
469 unsigned int EnvironmentOptions::GetRenderRefreshRate() const
470 {
471   return mRenderRefreshRate;
472 }
473
474 int EnvironmentOptions::GetMultiSamplingLevel() const
475 {
476   return mMultiSamplingLevel;
477 }
478
479 unsigned int EnvironmentOptions::GetMaxTextureSize() const
480 {
481   return mMaxTextureSize;
482 }
483
484 unsigned int EnvironmentOptions::GetRenderToFboInterval() const
485 {
486   return mRenderToFboInterval;
487 }
488
489 bool EnvironmentOptions::PerformanceServerRequired() const
490 {
491   return ( ( GetPerformanceStatsLoggingOptions() > 0) ||
492            ( GetPerformanceTimeStampOutput() > 0 ) ||
493            ( GetNetworkControlMode() > 0) );
494 }
495
496 bool EnvironmentOptions::DepthBufferRequired() const
497 {
498   return mDepthBufferRequired;
499 }
500
501 bool EnvironmentOptions::StencilBufferRequired() const
502 {
503   return mStencilBufferRequired;
504 }
505
506 bool EnvironmentOptions::PartialUpdateRequired() const
507 {
508   return mPartialUpdateRequired;
509 }
510
511 void EnvironmentOptions::ParseEnvironmentOptions()
512 {
513   // get logging options
514   mFpsFrequency = GetEnvironmentVariable( DALI_ENV_FPS_TRACKING, 0 );
515   mUpdateStatusFrequency = GetEnvironmentVariable( DALI_ENV_UPDATE_STATUS_INTERVAL, 0 );
516   mObjectProfilerInterval = GetEnvironmentVariable( DALI_ENV_OBJECT_PROFILER_INTERVAL, 0 );
517   mPerformanceStatsLevel = GetEnvironmentVariable( DALI_ENV_LOG_PERFORMANCE_STATS, 0 );
518   mPerformanceStatsFrequency = GetEnvironmentVariable( DALI_ENV_LOG_PERFORMANCE_STATS_FREQUENCY, 0 );
519   mPerformanceTimeStampOutput = GetEnvironmentVariable( DALI_ENV_PERFORMANCE_TIMESTAMP_OUTPUT, 0 );
520   mNetworkControl = GetEnvironmentVariable( DALI_ENV_NETWORK_CONTROL, 0 );
521   mPanGestureLoggingLevel = GetEnvironmentVariable( DALI_ENV_LOG_PAN_GESTURE, 0 );
522
523   SetFromEnvironmentVariable(DALI_ENV_PAN_PREDICTION_MODE, mPanGesturePredictionMode);
524   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_PREDICTION_AMOUNT, MinimumZero(mPanGesturePredictionAmount));
525   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_MIN_PREDICTION_AMOUNT, MinimumZero(mPanGestureMinPredictionAmount));
526   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_MAX_PREDICTION_AMOUNT,
527                                   [&](int maxPredictionAmount)
528                                   {
529                                     if( mPanGestureMinPredictionAmount > -1 && maxPredictionAmount < mPanGestureMinPredictionAmount )
530                                     {
531                                       // maximum amount should not be smaller than minimum amount
532                                       maxPredictionAmount = mPanGestureMinPredictionAmount;
533                                     }
534                                     mPanGestureMaxPredictionAmount = maxPredictionAmount;
535                                   });
536   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_PREDICTION_AMOUNT_ADJUSTMENT, MinimumZero(mPanGesturePredictionAmountAdjustment));
537   SetFromEnvironmentVariable(DALI_ENV_PAN_SMOOTHING_MODE, mPanGestureSmoothingMode);
538   SetFromEnvironmentVariable<float>(DALI_ENV_PAN_SMOOTHING_AMOUNT, ClampBetweenZeroAndOne(mPanGestureSmoothingAmount));
539   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_USE_ACTUAL_TIMES, EnableIfNonZero(mPanGestureUseActualTimes));
540   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_INTERPOLATION_TIME_RANGE, MinimumZero(mPanGestureInterpolationTimeRange));
541   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_SCALAR_ONLY_PREDICTION_ENABLED, EnableIfNonZero(mPanGestureScalarOnlyPredictionEnabled));
542   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_TWO_POINT_PREDICTION_ENABLED, EnableIfNonZero(mPanGestureTwoPointPredictionEnabled));
543   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_TWO_POINT_PAST_INTERPOLATE_TIME, MinimumZero(mPanGestureTwoPointInterpolatePastTime));
544   SetFromEnvironmentVariable<float>(DALI_ENV_PAN_TWO_POINT_VELOCITY_BIAS, ClampBetweenZeroAndOne(mPanGestureTwoPointVelocityBias));
545   SetFromEnvironmentVariable<float>(DALI_ENV_PAN_TWO_POINT_ACCELERATION_BIAS, ClampBetweenZeroAndOne(mPanGestureTwoPointAccelerationBias));
546   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_MULTITAP_SMOOTHING_RANGE, MinimumZero(mPanGestureMultitapSmoothingRange));
547   SetFromEnvironmentVariable(DALI_ENV_PAN_MINIMUM_DISTANCE, mPanMinimumDistance);
548   SetFromEnvironmentVariable(DALI_ENV_PAN_MINIMUM_EVENTS, mPanMinimumEvents);
549
550   SetFromEnvironmentVariable(DALI_ENV_PINCH_MINIMUM_DISTANCE, mPinchMinimumDistance);
551   SetFromEnvironmentVariable(DALI_ENV_PINCH_MINIMUM_TOUCH_EVENTS, mPinchMinimumTouchEvents);
552   SetFromEnvironmentVariable(DALI_ENV_PINCH_MINIMUM_TOUCH_EVENTS_AFTER_START, mPinchMinimumTouchEventsAfterStart);
553
554   SetFromEnvironmentVariable(DALI_ENV_ROTATION_MINIMUM_TOUCH_EVENTS, mRotationMinimumTouchEvents);
555   SetFromEnvironmentVariable(DALI_ENV_ROTATION_MINIMUM_TOUCH_EVENTS_AFTER_START, mRotationMinimumTouchEventsAfterStart);
556
557   SetFromEnvironmentVariable(DALI_ENV_LONG_PRESS_MINIMUM_HOLDING_TIME, mLongPressMinimumHoldingTime);
558
559   SetFromEnvironmentVariable(DALI_GLES_CALL_TIME, mGlesCallTime);
560   SetFromEnvironmentVariable<int>(DALI_GLES_CALL_ACCUMULATE, [&](int glesCallAccumulate) { mGlesCallAccumulate = glesCallAccumulate != 0; });
561
562   int windowWidth(0), windowHeight(0);
563   if ( GetEnvironmentVariable( DALI_WINDOW_WIDTH, windowWidth ) && GetEnvironmentVariable( DALI_WINDOW_HEIGHT, windowHeight ) )
564   {
565     mWindowWidth = windowWidth;
566     mWindowHeight = windowHeight;
567   }
568   SetFromEnvironmentVariable(DALI_WINDOW_NAME, mWindowName );
569   SetFromEnvironmentVariable(DALI_WINDOW_CLASS_NAME, mWindowClassName);
570
571   SetFromEnvironmentVariable<int>(DALI_THREADING_MODE,
572                                   [&](int threadingMode)
573                                   {
574                                     switch( threadingMode )
575                                     {
576                                       case ThreadingMode::COMBINED_UPDATE_RENDER:
577                                       {
578                                         mThreadingMode = static_cast< ThreadingMode::Type >( threadingMode );
579                                         break;
580                                       }
581                                     }
582                                   });
583
584   SetFromEnvironmentVariable<int>(DALI_REFRESH_RATE, GreaterThan(mRenderRefreshRate, 1));
585
586   SetFromEnvironmentVariable(DALI_ENV_MULTI_SAMPLING_LEVEL, mMultiSamplingLevel);
587
588   SetFromEnvironmentVariable<int>(DALI_ENV_MAX_TEXTURE_SIZE, GreaterThan(mMaxTextureSize, 0));
589
590   mRenderToFboInterval = GetEnvironmentVariable( DALI_RENDER_TO_FBO, 0u );
591
592   SetFromEnvironmentVariable<int>(DALI_ENV_DISABLE_DEPTH_BUFFER,
593                                   [&](int depthBufferRequired)
594                                   {
595                                     if( depthBufferRequired > 0 )
596                                     {
597                                       mDepthBufferRequired = false;
598                                       mStencilBufferRequired = false; // Disable stencil buffer as well
599                                     }
600                                   });
601   SetFromEnvironmentVariable<int>(DALI_ENV_DISABLE_STENCIL_BUFFER, DisableIfNonZero(mStencilBufferRequired));
602
603   SetFromEnvironmentVariable<int>(DALI_ENV_DISABLE_PARTIAL_UPDATE, DisableIfNonZero(mPartialUpdateRequired));
604 }
605
606 } // Adaptor
607
608 } // Internal
609
610 } // Dali