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