Fix various Klocwork errors
[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   mPanMinimumDistance(-1),
103   mPanMinimumEvents(-1),
104   mGlesCallTime(0),
105   mWindowWidth( 0 ),
106   mWindowHeight( 0 ),
107   mThreadingMode( ThreadingMode::SEPARATE_UPDATE_RENDER ),
108   mLogFunction( NULL )
109 {
110   ParseEnvironmentOptions();
111 }
112
113 EnvironmentOptions::~EnvironmentOptions()
114 {
115 }
116
117 void EnvironmentOptions::SetLogFunction( const Dali::Integration::Log::LogFunction& logFunction )
118 {
119   mLogFunction = logFunction;
120 }
121
122 void EnvironmentOptions::InstallLogFunction() const
123 {
124   Dali::Integration::Log::InstallLogFunction( mLogFunction );
125 }
126
127 void EnvironmentOptions::UnInstallLogFunction() const
128 {
129   Dali::Integration::Log::UninstallLogFunction();
130 }
131
132 unsigned int EnvironmentOptions::GetNetworkControlMode() const
133 {
134   return mNetworkControl;
135 }
136 unsigned int EnvironmentOptions::GetFrameRateLoggingFrequency() const
137 {
138   return mFpsFrequency;
139 }
140
141 unsigned int EnvironmentOptions::GetUpdateStatusLoggingFrequency() const
142 {
143   return mUpdateStatusFrequency;
144 }
145
146 unsigned int EnvironmentOptions::GetObjectProfilerInterval() const
147 {
148   return mObjectProfilerInterval;
149 }
150
151 unsigned int EnvironmentOptions::GetPerformanceStatsLoggingOptions() const
152 {
153   return mPerformanceStatsLevel;
154 }
155 unsigned int EnvironmentOptions::GetPerformanceStatsLoggingFrequency() const
156 {
157   return mPerformanceStatsFrequency;
158 }
159 unsigned int EnvironmentOptions::GetPerformanceTimeStampOutput() const
160 {
161   return mPerformanceTimeStampOutput;
162 }
163
164 unsigned int EnvironmentOptions::GetPanGestureLoggingLevel() const
165 {
166   return mPanGestureLoggingLevel;
167 }
168
169 int EnvironmentOptions::GetPanGesturePredictionMode() const
170 {
171   return mPanGesturePredictionMode;
172 }
173
174 int EnvironmentOptions::GetPanGesturePredictionAmount() const
175 {
176   return mPanGesturePredictionAmount;
177 }
178
179 int EnvironmentOptions::GetPanGestureMaximumPredictionAmount() const
180 {
181   return mPanGestureMaxPredictionAmount;
182 }
183
184 int EnvironmentOptions::GetPanGestureMinimumPredictionAmount() const
185 {
186   return mPanGestureMinPredictionAmount;
187 }
188
189 int EnvironmentOptions::GetPanGesturePredictionAmountAdjustment() const
190 {
191   return mPanGesturePredictionAmountAdjustment;
192 }
193
194 int EnvironmentOptions::GetPanGestureSmoothingMode() const
195 {
196   return mPanGestureSmoothingMode;
197 }
198
199 float EnvironmentOptions::GetPanGestureSmoothingAmount() const
200 {
201   return mPanGestureSmoothingAmount;
202 }
203
204 int EnvironmentOptions::GetMinimumPanDistance() const
205 {
206   return mPanMinimumDistance;
207 }
208
209 int EnvironmentOptions::GetMinimumPanEvents() const
210 {
211   return mPanMinimumEvents;
212 }
213
214 unsigned int EnvironmentOptions::GetWindowWidth() const
215 {
216   return mWindowWidth;
217 }
218
219 unsigned int EnvironmentOptions::GetWindowHeight() const
220 {
221   return mWindowHeight;
222 }
223
224 int EnvironmentOptions::GetGlesCallTime() const
225 {
226   return mGlesCallTime;
227 }
228
229 const std::string& EnvironmentOptions::GetWindowName() const
230 {
231   return mWindowName;
232 }
233
234 const std::string& EnvironmentOptions::GetWindowClassName() const
235 {
236   return mWindowClassName;
237 }
238
239 ThreadingMode::Type EnvironmentOptions::GetThreadingMode() const
240 {
241   return mThreadingMode;
242 }
243
244 bool EnvironmentOptions::PerformanceServerRequired() const
245 {
246   return ( ( GetPerformanceStatsLoggingOptions() > 0) ||
247            ( GetPerformanceTimeStampOutput() > 0 ) ||
248            ( GetNetworkControlMode() > 0) );
249 }
250
251 void EnvironmentOptions::ParseEnvironmentOptions()
252 {
253   // get logging options
254   mFpsFrequency = GetIntegerEnvironmentVariable( DALI_ENV_FPS_TRACKING, 0 );
255   mUpdateStatusFrequency = GetIntegerEnvironmentVariable( DALI_ENV_UPDATE_STATUS_INTERVAL, 0 );
256   mObjectProfilerInterval = GetIntegerEnvironmentVariable( DALI_ENV_OBJECT_PROFILER_INTERVAL, 0 );
257   mPerformanceStatsLevel = GetIntegerEnvironmentVariable( DALI_ENV_LOG_PERFORMANCE_STATS, 0 );
258   mPerformanceStatsFrequency = GetIntegerEnvironmentVariable( DALI_ENV_LOG_PERFORMANCE_STATS_FREQUENCY, 0 );
259   mPerformanceTimeStampOutput = GetIntegerEnvironmentVariable( DALI_ENV_PERFORMANCE_TIMESTAMP_OUTPUT, 0 );
260   mNetworkControl = GetIntegerEnvironmentVariable( DALI_ENV_NETWORK_CONTROL, 0 );
261   mPanGestureLoggingLevel = GetIntegerEnvironmentVariable( DALI_ENV_LOG_PAN_GESTURE, 0 );
262
263   int predictionMode;
264   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_PREDICTION_MODE, predictionMode) )
265   {
266     mPanGesturePredictionMode = predictionMode;
267   }
268   int predictionAmount(-1);
269   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_PREDICTION_AMOUNT, predictionAmount) )
270   {
271     if( predictionAmount < 0 )
272     {
273       // do not support times in the past
274       predictionAmount = 0;
275     }
276     mPanGesturePredictionAmount = predictionAmount;
277   }
278   int minPredictionAmount(-1);
279   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MIN_PREDICTION_AMOUNT, minPredictionAmount) )
280   {
281     if( minPredictionAmount < 0 )
282     {
283       // do not support times in the past
284       minPredictionAmount = 0;
285     }
286     mPanGestureMinPredictionAmount = minPredictionAmount;
287   }
288   int maxPredictionAmount(-1);
289   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MAX_PREDICTION_AMOUNT, maxPredictionAmount) )
290   {
291     if( minPredictionAmount > -1 && maxPredictionAmount < minPredictionAmount )
292     {
293       // maximum amount should not be smaller than minimum amount
294       maxPredictionAmount = minPredictionAmount;
295     }
296     mPanGestureMaxPredictionAmount = maxPredictionAmount;
297   }
298   int predictionAmountAdjustment(-1);
299   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_PREDICTION_AMOUNT_ADJUSTMENT, predictionAmountAdjustment) )
300   {
301     if( predictionAmountAdjustment < 0 )
302     {
303       // negative amount doesn't make sense
304       predictionAmountAdjustment = 0;
305     }
306     mPanGesturePredictionAmountAdjustment = predictionAmountAdjustment;
307   }
308   int smoothingMode;
309   if( GetIntegerEnvironmentVariable(DALI_ENV_PAN_SMOOTHING_MODE, smoothingMode) )
310   {
311     mPanGestureSmoothingMode = smoothingMode;
312   }
313   float smoothingAmount = 1.0f;
314   if( GetFloatEnvironmentVariable(DALI_ENV_PAN_SMOOTHING_AMOUNT, smoothingAmount) )
315   {
316     smoothingAmount = Clamp(smoothingAmount, 0.0f, 1.0f);
317     mPanGestureSmoothingAmount = smoothingAmount;
318   }
319
320   int minimumDistance(-1);
321   if ( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MINIMUM_DISTANCE, minimumDistance ))
322   {
323     mPanMinimumDistance = minimumDistance;
324   }
325
326   int minimumEvents(-1);
327   if ( GetIntegerEnvironmentVariable(DALI_ENV_PAN_MINIMUM_EVENTS, minimumEvents ))
328   {
329     mPanMinimumEvents = minimumEvents;
330   }
331
332   int glesCallTime(0);
333   if ( GetIntegerEnvironmentVariable(DALI_GLES_CALL_TIME, glesCallTime ))
334   {
335     mGlesCallTime = glesCallTime;
336   }
337
338   int windowWidth(0), windowHeight(0);
339   if ( GetIntegerEnvironmentVariable( DALI_WINDOW_WIDTH, windowWidth ) && GetIntegerEnvironmentVariable( DALI_WINDOW_HEIGHT, windowHeight ) )
340   {
341     mWindowWidth = windowWidth;
342     mWindowHeight = windowHeight;
343   }
344
345   const char * windowName = GetCharEnvironmentVariable( DALI_WINDOW_NAME );
346   if ( windowName )
347   {
348     mWindowName = windowName;
349   }
350
351   const char * windowClassName = GetCharEnvironmentVariable( DALI_WINDOW_CLASS_NAME );
352   if ( windowClassName )
353   {
354     mWindowClassName = windowClassName;
355   }
356
357   int threadingMode(0);
358   if ( GetIntegerEnvironmentVariable( DALI_THREADING_MODE, threadingMode ) )
359   {
360     switch( threadingMode )
361     {
362       case ThreadingMode::SEPARATE_UPDATE_RENDER:
363       case ThreadingMode::COMBINED_UPDATE_RENDER:
364       case ThreadingMode::SINGLE_THREADED:
365       {
366         mThreadingMode = static_cast< ThreadingMode::Type >( threadingMode );
367         break;
368       }
369     }
370   }
371 }
372
373 } // Adaptor
374
375 } // Internal
376
377 } // Dali