[3.0] pan-gesture code refactor and environment variables
[platform/core/uifw/dali-adaptor.git] / adaptors / base / environment-options.cpp
1 /*
2  * Copyright (c) 2015 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   mPanGestureUseActualTimes( -1 ),
103   mPanGestureInterpolationTimeRange( -1 ),
104   mPanGestureScalarOnlyPredictionEnabled( -1 ),
105   mPanGestureTwoPointPredictionEnabled( -1 ),
106   mPanGestureTwoPointInterpolatePastTime( -1 ),
107   mPanGestureTwoPointVelocityBias( -1.0f ),
108   mPanGestureTwoPointAccelerationBias( -1.0f ),
109   mPanGestureMultitapSmoothingRange( -1 ),
110   mPanMinimumDistance( -1 ),
111   mPanMinimumEvents( -1 ),
112   mGlesCallTime( 0 ),
113   mWindowWidth( 0 ),
114   mWindowHeight( 0 ),
115   mThreadingMode( ThreadingMode::COMBINED_UPDATE_RENDER ),
116   mRenderRefreshRate( 1 ),
117   mGlesCallAccumulate( false ),
118   mLogFunction( NULL )
119 {
120   ParseEnvironmentOptions();
121 }
122
123 EnvironmentOptions::~EnvironmentOptions()
124 {
125 }
126
127 void EnvironmentOptions::SetLogFunction( const Dali::Integration::Log::LogFunction& logFunction )
128 {
129   mLogFunction = logFunction;
130 }
131
132 void EnvironmentOptions::InstallLogFunction() const
133 {
134   Dali::Integration::Log::InstallLogFunction( mLogFunction );
135 }
136
137 void EnvironmentOptions::UnInstallLogFunction() const
138 {
139   Dali::Integration::Log::UninstallLogFunction();
140 }
141
142 unsigned int EnvironmentOptions::GetNetworkControlMode() const
143 {
144   return mNetworkControl;
145 }
146 unsigned int EnvironmentOptions::GetFrameRateLoggingFrequency() const
147 {
148   return mFpsFrequency;
149 }
150
151 unsigned int EnvironmentOptions::GetUpdateStatusLoggingFrequency() const
152 {
153   return mUpdateStatusFrequency;
154 }
155
156 unsigned int EnvironmentOptions::GetObjectProfilerInterval() const
157 {
158   return mObjectProfilerInterval;
159 }
160
161 unsigned int EnvironmentOptions::GetPerformanceStatsLoggingOptions() const
162 {
163   return mPerformanceStatsLevel;
164 }
165 unsigned int EnvironmentOptions::GetPerformanceStatsLoggingFrequency() const
166 {
167   return mPerformanceStatsFrequency;
168 }
169 unsigned int EnvironmentOptions::GetPerformanceTimeStampOutput() const
170 {
171   return mPerformanceTimeStampOutput;
172 }
173
174 unsigned int EnvironmentOptions::GetPanGestureLoggingLevel() const
175 {
176   return mPanGestureLoggingLevel;
177 }
178
179 int EnvironmentOptions::GetPanGesturePredictionMode() const
180 {
181   return mPanGesturePredictionMode;
182 }
183
184 int EnvironmentOptions::GetPanGesturePredictionAmount() const
185 {
186   return mPanGesturePredictionAmount;
187 }
188
189 int EnvironmentOptions::GetPanGestureMaximumPredictionAmount() const
190 {
191   return mPanGestureMaxPredictionAmount;
192 }
193
194 int EnvironmentOptions::GetPanGestureMinimumPredictionAmount() const
195 {
196   return mPanGestureMinPredictionAmount;
197 }
198
199 int EnvironmentOptions::GetPanGesturePredictionAmountAdjustment() const
200 {
201   return mPanGesturePredictionAmountAdjustment;
202 }
203
204 int EnvironmentOptions::GetPanGestureSmoothingMode() const
205 {
206   return mPanGestureSmoothingMode;
207 }
208
209 float EnvironmentOptions::GetPanGestureSmoothingAmount() const
210 {
211   return mPanGestureSmoothingAmount;
212 }
213
214 int EnvironmentOptions::GetPanGestureUseActualTimes() const
215 {
216   return mPanGestureUseActualTimes;
217 }
218
219 int EnvironmentOptions::GetPanGestureInterpolationTimeRange() const
220 {
221   return mPanGestureInterpolationTimeRange;
222 }
223
224 int EnvironmentOptions::GetPanGestureScalarOnlyPredictionEnabled() const
225 {
226   return mPanGestureScalarOnlyPredictionEnabled;
227 }
228
229 int EnvironmentOptions::GetPanGestureTwoPointPredictionEnabled() const
230 {
231   return mPanGestureTwoPointPredictionEnabled;
232 }
233
234 int EnvironmentOptions::GetPanGestureTwoPointInterpolatePastTime() const
235 {
236   return mPanGestureTwoPointInterpolatePastTime;
237 }
238
239 float EnvironmentOptions::GetPanGestureTwoPointVelocityBias() const
240 {
241   return mPanGestureTwoPointVelocityBias;
242 }
243
244 float EnvironmentOptions::GetPanGestureTwoPointAccelerationBias() const
245 {
246   return mPanGestureTwoPointAccelerationBias;
247 }
248
249 int EnvironmentOptions::GetPanGestureMultitapSmoothingRange() const
250 {
251   return mPanGestureMultitapSmoothingRange;
252 }
253
254 int EnvironmentOptions::GetMinimumPanDistance() const
255 {
256   return mPanMinimumDistance;
257 }
258
259 int EnvironmentOptions::GetMinimumPanEvents() const
260 {
261   return mPanMinimumEvents;
262 }
263
264 unsigned int EnvironmentOptions::GetWindowWidth() const
265 {
266   return mWindowWidth;
267 }
268
269 unsigned int EnvironmentOptions::GetWindowHeight() const
270 {
271   return mWindowHeight;
272 }
273
274 int EnvironmentOptions::GetGlesCallTime() const
275 {
276   return mGlesCallTime;
277 }
278
279 bool EnvironmentOptions::GetGlesCallAccumulate() const
280 {
281   return mGlesCallAccumulate;
282 }
283
284 const std::string& EnvironmentOptions::GetWindowName() const
285 {
286   return mWindowName;
287 }
288
289 const std::string& EnvironmentOptions::GetWindowClassName() const
290 {
291   return mWindowClassName;
292 }
293
294 ThreadingMode::Type EnvironmentOptions::GetThreadingMode() const
295 {
296   return mThreadingMode;
297 }
298
299 unsigned int EnvironmentOptions::GetRenderRefreshRate() const
300 {
301   return mRenderRefreshRate;
302 }
303
304 bool EnvironmentOptions::PerformanceServerRequired() const
305 {
306   return ( ( GetPerformanceStatsLoggingOptions() > 0) ||
307            ( GetPerformanceTimeStampOutput() > 0 ) ||
308            ( GetNetworkControlMode() > 0) );
309 }
310
311 void EnvironmentOptions::ParseEnvironmentOptions()
312 {
313   // get logging options
314   mFpsFrequency = GetIntegerEnvironmentVariable( DALI_ENV_FPS_TRACKING, 0 );
315   mUpdateStatusFrequency = GetIntegerEnvironmentVariable( DALI_ENV_UPDATE_STATUS_INTERVAL, 0 );
316   mObjectProfilerInterval = GetIntegerEnvironmentVariable( DALI_ENV_OBJECT_PROFILER_INTERVAL, 0 );
317   mPerformanceStatsLevel = GetIntegerEnvironmentVariable( DALI_ENV_LOG_PERFORMANCE_STATS, 0 );
318   mPerformanceStatsFrequency = GetIntegerEnvironmentVariable( DALI_ENV_LOG_PERFORMANCE_STATS_FREQUENCY, 0 );
319   mPerformanceTimeStampOutput = GetIntegerEnvironmentVariable( DALI_ENV_PERFORMANCE_TIMESTAMP_OUTPUT, 0 );
320   mNetworkControl = GetIntegerEnvironmentVariable( DALI_ENV_NETWORK_CONTROL, 0 );
321   mPanGestureLoggingLevel = GetIntegerEnvironmentVariable( DALI_ENV_LOG_PAN_GESTURE, 0 );
322
323   int predictionMode;
324   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_PREDICTION_MODE, predictionMode) )
325   {
326     mPanGesturePredictionMode = predictionMode;
327   }
328   int predictionAmount(-1);
329   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_PREDICTION_AMOUNT, predictionAmount) )
330   {
331     if( predictionAmount < 0 )
332     {
333       // do not support times in the past
334       predictionAmount = 0;
335     }
336     mPanGesturePredictionAmount = predictionAmount;
337   }
338   int minPredictionAmount(-1);
339   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MIN_PREDICTION_AMOUNT, minPredictionAmount) )
340   {
341     if( minPredictionAmount < 0 )
342     {
343       // do not support times in the past
344       minPredictionAmount = 0;
345     }
346     mPanGestureMinPredictionAmount = minPredictionAmount;
347   }
348   int maxPredictionAmount(-1);
349   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MAX_PREDICTION_AMOUNT, maxPredictionAmount) )
350   {
351     if( minPredictionAmount > -1 && maxPredictionAmount < minPredictionAmount )
352     {
353       // maximum amount should not be smaller than minimum amount
354       maxPredictionAmount = minPredictionAmount;
355     }
356     mPanGestureMaxPredictionAmount = maxPredictionAmount;
357   }
358   int predictionAmountAdjustment(-1);
359   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_PREDICTION_AMOUNT_ADJUSTMENT, predictionAmountAdjustment) )
360   {
361     if( predictionAmountAdjustment < 0 )
362     {
363       // negative amount doesn't make sense
364       predictionAmountAdjustment = 0;
365     }
366     mPanGesturePredictionAmountAdjustment = predictionAmountAdjustment;
367   }
368   int smoothingMode;
369   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_SMOOTHING_MODE, smoothingMode) )
370   {
371     mPanGestureSmoothingMode = smoothingMode;
372   }
373   float smoothingAmount = 1.0f;
374   if( GetFloatEnvironmentVariable(DALI_ENV_PAN_SMOOTHING_AMOUNT, smoothingAmount) )
375   {
376     smoothingAmount = Clamp(smoothingAmount, 0.0f, 1.0f);
377     mPanGestureSmoothingAmount = smoothingAmount;
378   }
379
380   int useActualTimes( -1 );
381   if( GetIntegerEnvironmentVariable( DALI_ENV_PAN_USE_ACTUAL_TIMES, useActualTimes ) )
382   {
383     mPanGestureUseActualTimes = useActualTimes == 0 ? 0 : 1;
384   }
385
386   int interpolationTimeRange( -1 );
387   if( GetIntegerEnvironmentVariable( DALI_ENV_PAN_INTERPOLATION_TIME_RANGE, interpolationTimeRange ) )
388   {
389     if( interpolationTimeRange < 0 )
390     {
391       interpolationTimeRange = 0;
392     }
393     mPanGestureInterpolationTimeRange = interpolationTimeRange;
394   }
395
396   int scalarOnlyPredictionEnabled( -1 );
397   if( GetIntegerEnvironmentVariable( DALI_ENV_PAN_SCALAR_ONLY_PREDICTION_ENABLED, scalarOnlyPredictionEnabled ) )
398   {
399     mPanGestureScalarOnlyPredictionEnabled = scalarOnlyPredictionEnabled == 0 ? 0 : 1;
400   }
401
402   int twoPointPredictionEnabled( -1 );
403   if( GetIntegerEnvironmentVariable( DALI_ENV_PAN_TWO_POINT_PREDICTION_ENABLED, twoPointPredictionEnabled ) )
404   {
405     mPanGestureTwoPointPredictionEnabled = twoPointPredictionEnabled == 0 ? 0 : 1;
406   }
407
408   int twoPointPastInterpolateTime( -1 );
409   if( GetIntegerEnvironmentVariable( DALI_ENV_PAN_TWO_POINT_PAST_INTERPOLATE_TIME, twoPointPastInterpolateTime ) )
410   {
411     if( twoPointPastInterpolateTime < 0 )
412     {
413       twoPointPastInterpolateTime = 0;
414     }
415     mPanGestureTwoPointInterpolatePastTime = twoPointPastInterpolateTime;
416   }
417
418   float twoPointVelocityBias = -1.0f;
419   if( GetFloatEnvironmentVariable( DALI_ENV_PAN_TWO_POINT_VELOCITY_BIAS, twoPointVelocityBias ) )
420   {
421     twoPointVelocityBias = Clamp( twoPointVelocityBias, 0.0f, 1.0f );
422     mPanGestureTwoPointVelocityBias = twoPointVelocityBias;
423   }
424
425   float twoPointAccelerationBias = -1.0f;
426   if( GetFloatEnvironmentVariable( DALI_ENV_PAN_TWO_POINT_ACCELERATION_BIAS, twoPointAccelerationBias ) )
427   {
428     twoPointAccelerationBias = Clamp( twoPointAccelerationBias, 0.0f, 1.0f );
429     mPanGestureTwoPointAccelerationBias = twoPointAccelerationBias;
430   }
431
432   int multitapSmoothingRange( -1 );
433   if( GetIntegerEnvironmentVariable( DALI_ENV_PAN_MULTITAP_SMOOTHING_RANGE, multitapSmoothingRange ) )
434   {
435     if( multitapSmoothingRange < 0 )
436     {
437       multitapSmoothingRange = 0;
438     }
439     mPanGestureMultitapSmoothingRange = multitapSmoothingRange;
440   }
441
442   int minimumDistance(-1);
443   if ( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MINIMUM_DISTANCE, minimumDistance ))
444   {
445     mPanMinimumDistance = minimumDistance;
446   }
447
448   int minimumEvents(-1);
449   if ( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MINIMUM_EVENTS, minimumEvents ))
450   {
451     mPanMinimumEvents = minimumEvents;
452   }
453
454   int glesCallTime(0);
455   if ( GetIntegerEnvironmentVariable(DALI_GLES_CALL_TIME, glesCallTime ))
456   {
457     mGlesCallTime = glesCallTime;
458   }
459
460   int glesCallAccumulate( 0 );
461   if ( GetIntegerEnvironmentVariable( DALI_GLES_CALL_ACCUMULATE, glesCallAccumulate ) )
462   {
463     mGlesCallAccumulate = glesCallAccumulate != 0;
464   }
465
466   int windowWidth(0), windowHeight(0);
467   if ( GetIntegerEnvironmentVariable( DALI_WINDOW_WIDTH, windowWidth ) && GetIntegerEnvironmentVariable( DALI_WINDOW_HEIGHT, windowHeight ) )
468   {
469     mWindowWidth = windowWidth;
470     mWindowHeight = windowHeight;
471   }
472
473   const char * windowName = GetCharEnvironmentVariable( DALI_WINDOW_NAME );
474   if ( windowName )
475   {
476     mWindowName = windowName;
477   }
478
479   const char * windowClassName = GetCharEnvironmentVariable( DALI_WINDOW_CLASS_NAME );
480   if ( windowClassName )
481   {
482     mWindowClassName = windowClassName;
483   }
484
485   int threadingMode(0);
486   if ( GetIntegerEnvironmentVariable( DALI_THREADING_MODE, threadingMode ) )
487   {
488     switch( threadingMode )
489     {
490       case ThreadingMode::SEPARATE_UPDATE_RENDER:
491       case ThreadingMode::COMBINED_UPDATE_RENDER:
492       case ThreadingMode::SINGLE_THREADED:
493       {
494         mThreadingMode = static_cast< ThreadingMode::Type >( threadingMode );
495         break;
496       }
497     }
498   }
499
500   int renderRefreshRate(0);
501   if ( GetIntegerEnvironmentVariable( DALI_REFRESH_RATE, renderRefreshRate ) )
502   {
503     // Only change it if it's valid
504     if( renderRefreshRate > 1 )
505     {
506       mRenderRefreshRate = renderRefreshRate;
507     }
508   }
509 }
510
511 } // Adaptor
512
513 } // Internal
514
515 } // Dali