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