CanvasRenderer: Add Gradient, LinearGradient, RadialGradient classes
[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   mGlesCallTime(0),
242   mMultiSamplingLevel(DEFAULT_MULTI_SAMPLING_LEVEL),
243   mThreadingMode(ThreadingMode::COMBINED_UPDATE_RENDER),
244   mGlesCallAccumulate(false),
245   mDepthBufferRequired(DEFAULT_DEPTH_BUFFER_REQUIRED_SETTING),
246   mStencilBufferRequired(DEFAULT_STENCIL_BUFFER_REQUIRED_SETTING),
247   mPartialUpdateRequired(DEFAULT_PARTIAL_UPDATE_REQUIRED_SETTING)
248 {
249   ParseEnvironmentOptions();
250 }
251
252 EnvironmentOptions::~EnvironmentOptions()
253 {
254 }
255
256 void EnvironmentOptions::CreateTraceManager(PerformanceInterface* performanceInterface)
257 {
258   mTraceManager = TraceManagerFactory::CreateTraceFactory(performanceInterface);
259 }
260
261 void EnvironmentOptions::InstallTraceFunction() const
262 {
263   if(mTraceManager)
264   {
265     mTraceManager->Initialise();
266   }
267 }
268
269 void EnvironmentOptions::SetLogFunction(const Dali::Integration::Log::LogFunction& logFunction)
270 {
271   mLogFunction = logFunction;
272 }
273
274 void EnvironmentOptions::InstallLogFunction() const
275 {
276   Dali::Integration::Log::InstallLogFunction(mLogFunction);
277 }
278
279 void EnvironmentOptions::UnInstallLogFunction() const
280 {
281   Dali::Integration::Log::UninstallLogFunction();
282 }
283
284 unsigned int EnvironmentOptions::GetNetworkControlMode() const
285 {
286   return mNetworkControl;
287 }
288 unsigned int EnvironmentOptions::GetFrameRateLoggingFrequency() const
289 {
290   return mFpsFrequency;
291 }
292
293 unsigned int EnvironmentOptions::GetUpdateStatusLoggingFrequency() const
294 {
295   return mUpdateStatusFrequency;
296 }
297
298 unsigned int EnvironmentOptions::GetObjectProfilerInterval() const
299 {
300   return mObjectProfilerInterval;
301 }
302
303 unsigned int EnvironmentOptions::GetPerformanceStatsLoggingOptions() const
304 {
305   return mPerformanceStatsLevel;
306 }
307 unsigned int EnvironmentOptions::GetPerformanceStatsLoggingFrequency() const
308 {
309   return mPerformanceStatsFrequency;
310 }
311 unsigned int EnvironmentOptions::GetPerformanceTimeStampOutput() const
312 {
313   return mPerformanceTimeStampOutput;
314 }
315
316 unsigned int EnvironmentOptions::GetPanGestureLoggingLevel() const
317 {
318   return mPanGestureLoggingLevel;
319 }
320
321 int EnvironmentOptions::GetPanGesturePredictionMode() const
322 {
323   return mPanGesturePredictionMode;
324 }
325
326 int EnvironmentOptions::GetPanGesturePredictionAmount() const
327 {
328   return mPanGesturePredictionAmount;
329 }
330
331 int EnvironmentOptions::GetPanGestureMaximumPredictionAmount() const
332 {
333   return mPanGestureMaxPredictionAmount;
334 }
335
336 int EnvironmentOptions::GetPanGestureMinimumPredictionAmount() const
337 {
338   return mPanGestureMinPredictionAmount;
339 }
340
341 int EnvironmentOptions::GetPanGesturePredictionAmountAdjustment() const
342 {
343   return mPanGesturePredictionAmountAdjustment;
344 }
345
346 int EnvironmentOptions::GetPanGestureSmoothingMode() const
347 {
348   return mPanGestureSmoothingMode;
349 }
350
351 float EnvironmentOptions::GetPanGestureSmoothingAmount() const
352 {
353   return mPanGestureSmoothingAmount;
354 }
355
356 int EnvironmentOptions::GetPanGestureUseActualTimes() const
357 {
358   return mPanGestureUseActualTimes;
359 }
360
361 int EnvironmentOptions::GetPanGestureInterpolationTimeRange() const
362 {
363   return mPanGestureInterpolationTimeRange;
364 }
365
366 int EnvironmentOptions::GetPanGestureScalarOnlyPredictionEnabled() const
367 {
368   return mPanGestureScalarOnlyPredictionEnabled;
369 }
370
371 int EnvironmentOptions::GetPanGestureTwoPointPredictionEnabled() const
372 {
373   return mPanGestureTwoPointPredictionEnabled;
374 }
375
376 int EnvironmentOptions::GetPanGestureTwoPointInterpolatePastTime() const
377 {
378   return mPanGestureTwoPointInterpolatePastTime;
379 }
380
381 float EnvironmentOptions::GetPanGestureTwoPointVelocityBias() const
382 {
383   return mPanGestureTwoPointVelocityBias;
384 }
385
386 float EnvironmentOptions::GetPanGestureTwoPointAccelerationBias() const
387 {
388   return mPanGestureTwoPointAccelerationBias;
389 }
390
391 int EnvironmentOptions::GetPanGestureMultitapSmoothingRange() const
392 {
393   return mPanGestureMultitapSmoothingRange;
394 }
395
396 int EnvironmentOptions::GetMinimumPanDistance() const
397 {
398   return mPanMinimumDistance;
399 }
400
401 int EnvironmentOptions::GetMinimumPanEvents() const
402 {
403   return mPanMinimumEvents;
404 }
405
406 float EnvironmentOptions::GetMinimumPinchDistance() const
407 {
408   return mPinchMinimumDistance;
409 }
410
411 int EnvironmentOptions::GetMinimumPinchTouchEvents() const
412 {
413   return mPinchMinimumTouchEvents;
414 }
415
416 int EnvironmentOptions::GetMinimumPinchTouchEventsAfterStart() const
417 {
418   return mPinchMinimumTouchEventsAfterStart;
419 }
420
421 int EnvironmentOptions::GetMinimumRotationTouchEvents() const
422 {
423   return mRotationMinimumTouchEvents;
424 }
425
426 int EnvironmentOptions::GetMinimumRotationTouchEventsAfterStart() const
427 {
428   return mRotationMinimumTouchEventsAfterStart;
429 }
430
431 int EnvironmentOptions::GetLongPressMinimumHoldingTime() const
432 {
433   return mLongPressMinimumHoldingTime;
434 }
435
436 unsigned int EnvironmentOptions::GetWindowWidth() const
437 {
438   return mWindowWidth;
439 }
440
441 unsigned int EnvironmentOptions::GetWindowHeight() const
442 {
443   return mWindowHeight;
444 }
445
446 int EnvironmentOptions::GetGlesCallTime() const
447 {
448   return mGlesCallTime;
449 }
450
451 bool EnvironmentOptions::GetGlesCallAccumulate() const
452 {
453   return mGlesCallAccumulate;
454 }
455
456 const std::string& EnvironmentOptions::GetWindowName() const
457 {
458   return mWindowName;
459 }
460
461 const std::string& EnvironmentOptions::GetWindowClassName() const
462 {
463   return mWindowClassName;
464 }
465
466 ThreadingMode::Type EnvironmentOptions::GetThreadingMode() const
467 {
468   return mThreadingMode;
469 }
470
471 unsigned int EnvironmentOptions::GetRenderRefreshRate() const
472 {
473   return mRenderRefreshRate;
474 }
475
476 int EnvironmentOptions::GetMultiSamplingLevel() const
477 {
478   return mMultiSamplingLevel;
479 }
480
481 unsigned int EnvironmentOptions::GetMaxTextureSize() const
482 {
483   return mMaxTextureSize;
484 }
485
486 unsigned int EnvironmentOptions::GetRenderToFboInterval() const
487 {
488   return mRenderToFboInterval;
489 }
490
491 bool EnvironmentOptions::PerformanceServerRequired() const
492 {
493   return ((GetPerformanceStatsLoggingOptions() > 0) ||
494           (GetPerformanceTimeStampOutput() > 0) ||
495           (GetNetworkControlMode() > 0));
496 }
497
498 bool EnvironmentOptions::DepthBufferRequired() const
499 {
500   return mDepthBufferRequired;
501 }
502
503 bool EnvironmentOptions::StencilBufferRequired() const
504 {
505   return mStencilBufferRequired;
506 }
507
508 bool EnvironmentOptions::PartialUpdateRequired() const
509 {
510   return mPartialUpdateRequired;
511 }
512
513 void EnvironmentOptions::ParseEnvironmentOptions()
514 {
515   // get logging options
516   mFpsFrequency               = GetEnvironmentVariable(DALI_ENV_FPS_TRACKING, 0);
517   mUpdateStatusFrequency      = GetEnvironmentVariable(DALI_ENV_UPDATE_STATUS_INTERVAL, 0);
518   mObjectProfilerInterval     = GetEnvironmentVariable(DALI_ENV_OBJECT_PROFILER_INTERVAL, 0);
519   mPerformanceStatsLevel      = GetEnvironmentVariable(DALI_ENV_LOG_PERFORMANCE_STATS, 0);
520   mPerformanceStatsFrequency  = GetEnvironmentVariable(DALI_ENV_LOG_PERFORMANCE_STATS_FREQUENCY, 0);
521   mPerformanceTimeStampOutput = GetEnvironmentVariable(DALI_ENV_PERFORMANCE_TIMESTAMP_OUTPUT, 0);
522   mNetworkControl             = GetEnvironmentVariable(DALI_ENV_NETWORK_CONTROL, 0);
523   mPanGestureLoggingLevel     = GetEnvironmentVariable(DALI_ENV_LOG_PAN_GESTURE, 0);
524
525   SetFromEnvironmentVariable(DALI_ENV_PAN_PREDICTION_MODE, mPanGesturePredictionMode);
526   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_PREDICTION_AMOUNT, MinimumZero(mPanGesturePredictionAmount));
527   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_MIN_PREDICTION_AMOUNT, MinimumZero(mPanGestureMinPredictionAmount));
528   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_MAX_PREDICTION_AMOUNT,
529                                   [&](int maxPredictionAmount) {
530                                     if(mPanGestureMinPredictionAmount > -1 && maxPredictionAmount < mPanGestureMinPredictionAmount)
531                                     {
532                                       // maximum amount should not be smaller than minimum amount
533                                       maxPredictionAmount = mPanGestureMinPredictionAmount;
534                                     }
535                                     mPanGestureMaxPredictionAmount = maxPredictionAmount;
536                                   });
537   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_PREDICTION_AMOUNT_ADJUSTMENT, MinimumZero(mPanGesturePredictionAmountAdjustment));
538   SetFromEnvironmentVariable(DALI_ENV_PAN_SMOOTHING_MODE, mPanGestureSmoothingMode);
539   SetFromEnvironmentVariable<float>(DALI_ENV_PAN_SMOOTHING_AMOUNT, ClampBetweenZeroAndOne(mPanGestureSmoothingAmount));
540   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_USE_ACTUAL_TIMES, EnableIfNonZero(mPanGestureUseActualTimes));
541   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_INTERPOLATION_TIME_RANGE, MinimumZero(mPanGestureInterpolationTimeRange));
542   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_SCALAR_ONLY_PREDICTION_ENABLED, EnableIfNonZero(mPanGestureScalarOnlyPredictionEnabled));
543   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_TWO_POINT_PREDICTION_ENABLED, EnableIfNonZero(mPanGestureTwoPointPredictionEnabled));
544   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_TWO_POINT_PAST_INTERPOLATE_TIME, MinimumZero(mPanGestureTwoPointInterpolatePastTime));
545   SetFromEnvironmentVariable<float>(DALI_ENV_PAN_TWO_POINT_VELOCITY_BIAS, ClampBetweenZeroAndOne(mPanGestureTwoPointVelocityBias));
546   SetFromEnvironmentVariable<float>(DALI_ENV_PAN_TWO_POINT_ACCELERATION_BIAS, ClampBetweenZeroAndOne(mPanGestureTwoPointAccelerationBias));
547   SetFromEnvironmentVariable<int>(DALI_ENV_PAN_MULTITAP_SMOOTHING_RANGE, MinimumZero(mPanGestureMultitapSmoothingRange));
548   SetFromEnvironmentVariable(DALI_ENV_PAN_MINIMUM_DISTANCE, mPanMinimumDistance);
549   SetFromEnvironmentVariable(DALI_ENV_PAN_MINIMUM_EVENTS, mPanMinimumEvents);
550
551   SetFromEnvironmentVariable(DALI_ENV_PINCH_MINIMUM_DISTANCE, mPinchMinimumDistance);
552   SetFromEnvironmentVariable(DALI_ENV_PINCH_MINIMUM_TOUCH_EVENTS, mPinchMinimumTouchEvents);
553   SetFromEnvironmentVariable(DALI_ENV_PINCH_MINIMUM_TOUCH_EVENTS_AFTER_START, mPinchMinimumTouchEventsAfterStart);
554
555   SetFromEnvironmentVariable(DALI_ENV_ROTATION_MINIMUM_TOUCH_EVENTS, mRotationMinimumTouchEvents);
556   SetFromEnvironmentVariable(DALI_ENV_ROTATION_MINIMUM_TOUCH_EVENTS_AFTER_START, mRotationMinimumTouchEventsAfterStart);
557
558   SetFromEnvironmentVariable(DALI_ENV_LONG_PRESS_MINIMUM_HOLDING_TIME, mLongPressMinimumHoldingTime);
559
560   SetFromEnvironmentVariable(DALI_GLES_CALL_TIME, mGlesCallTime);
561   SetFromEnvironmentVariable<int>(DALI_GLES_CALL_ACCUMULATE, [&](int glesCallAccumulate) { mGlesCallAccumulate = glesCallAccumulate != 0; });
562
563   int windowWidth(0), windowHeight(0);
564   if(GetEnvironmentVariable(DALI_WINDOW_WIDTH, windowWidth) && GetEnvironmentVariable(DALI_WINDOW_HEIGHT, windowHeight))
565   {
566     mWindowWidth  = windowWidth;
567     mWindowHeight = windowHeight;
568   }
569   SetFromEnvironmentVariable(DALI_WINDOW_NAME, mWindowName);
570   SetFromEnvironmentVariable(DALI_WINDOW_CLASS_NAME, mWindowClassName);
571
572   SetFromEnvironmentVariable<int>(DALI_THREADING_MODE,
573                                   [&](int threadingMode) {
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                                     if(depthBufferRequired > 0)
595                                     {
596                                       mDepthBufferRequired   = false;
597                                       mStencilBufferRequired = false; // Disable stencil buffer as well
598                                     }
599                                   });
600   SetFromEnvironmentVariable<int>(DALI_ENV_DISABLE_STENCIL_BUFFER, DisableIfNonZero(mStencilBufferRequired));
601
602   SetFromEnvironmentVariable<int>(DALI_ENV_DISABLE_PARTIAL_UPDATE, DisableIfNonZero(mPartialUpdateRequired));
603 }
604
605 } // namespace Adaptor
606
607 } // namespace Internal
608
609 } // namespace Dali