[dali_2.3.27] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / fps-tracker.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 <dali/internal/system/common/fps-tracker.h>
20
21 // EXTERNAL INCLUDES
22 #include <sys/stat.h>
23 #include <cmath>
24 #include <cstdio>
25
26 // INTERNAL INCLUDES
27 #include <dali/internal/system/common/environment-options.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 namespace Adaptor
34 {
35 namespace
36 {
37 const char* DALI_TEMP_UPDATE_FPS_FILE("/tmp/dalifps.txt");
38 } // unnamed namespace
39
40 FpsTracker::FpsTracker(const EnvironmentOptions& environmentOptions)
41 : mFpsTrackingSeconds(fabsf(environmentOptions.GetFrameRateLoggingFrequency())),
42   mFrameCount(0.0f),
43   mElapsedTime(0.0f)
44 {
45 }
46
47 FpsTracker::~FpsTracker()
48 {
49   if(mFpsTrackingSeconds > 0.f)
50   {
51     OutputFPSRecord();
52   }
53 }
54
55 void FpsTracker::Track(float secondsFromLastFrame)
56 {
57   if(mFpsTrackingSeconds > 0.f)
58   {
59     if(mElapsedTime < mFpsTrackingSeconds)
60     {
61       mElapsedTime += secondsFromLastFrame;
62       mFrameCount += 1.f;
63     }
64     else
65     {
66       OutputFPSRecord();
67       mFrameCount  = 0.f;
68       mElapsedTime = 0.f;
69     }
70   }
71 }
72
73 bool FpsTracker::Enabled() const
74 {
75   return mFpsTrackingSeconds > 0.0f;
76 }
77
78 void FpsTracker::OutputFPSRecord()
79 {
80   float fps = mFrameCount / mElapsedTime;
81   DALI_LOG_FPS("Frame count %.0f, elapsed time %.1fs, FPS: %.2f\n", mFrameCount, mElapsedTime, fps);
82
83   struct stat fileStat;
84
85   // Check file path
86   if(lstat(DALI_TEMP_UPDATE_FPS_FILE, &fileStat) != 0)
87   {
88     return;
89   }
90
91   if(!S_ISREG(fileStat.st_mode))
92   {
93     return;
94   }
95
96   // Dumps out the frame rate.
97   FILE* outfile = fopen(DALI_TEMP_UPDATE_FPS_FILE, "w");
98   if(outfile)
99   {
100     char fpsString[10];
101     snprintf(fpsString, sizeof(fpsString), "%.2f \n", fps);
102     fputs(fpsString, outfile); // ignore the error on purpose
103     fclose(outfile);
104   }
105 }
106
107 } // namespace Adaptor
108
109 } // namespace Internal
110
111 } // namespace Dali