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