Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-adaptor.git] / adaptors / base / performance-logging / performance-server.h
1 #ifndef __DALI_INTERNAL_ADAPTOR_PERFORMANCE_SERVER_H__
2 #define __DALI_INTERNAL_ADAPTOR_PERFORMANCE_SERVER_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <base/performance-logging/frame-time-stats.h>
23 #include <dali/public-api/common/dali-vector.h>
24 #include <base/interfaces/adaptor-internal-services.h>
25 #include <base/performance-logging/performance-marker.h>
26
27 // EXTERNAL INCLUDES
28 #include <boost/thread/mutex.hpp>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace Adaptor
37 {
38
39 class EnvironmentOptions;
40
41 /**
42  * Concrete implementation of performance interface.
43  * Adaptor classes should never include this file, they
44  * just need to include the abstract class performance-interface.h
45  */
46 class PerformanceServer : public PerformanceInterface
47 {
48 public:
49
50   /**
51    * Constructor
52    * @param adaptorServices adaptor internal services
53    * @param environmentOptions environment options
54    */
55   PerformanceServer( AdaptorInternalServices& adaptorServices,
56                      const EnvironmentOptions& environmentOptions );
57
58   /**
59    * Destructor
60    */
61   virtual ~PerformanceServer();
62
63   /**
64    * @copydoc PerformanceLogger::AddContext()
65    */
66   virtual ContextId AddContext( const char* name );
67
68   /**
69    * @copydoc PerformanceLogger::RemoveContext()
70    */
71   virtual void RemoveContext( ContextId contextId );
72
73   /**
74    * @copydoc PerformanceInterface::AddMarker()
75    */
76   virtual void AddMarker( MarkerType markerType );
77
78   /**
79    * @copydoc PerformanceLogger::AddMarker()
80    */
81   virtual void AddMarker( MarkerType markerType, ContextId contextId );
82
83   /**
84    * @copydoc PerformanceInterface::SetLogging()
85    */
86   virtual void SetLogging( unsigned int level, unsigned int logFrequency );
87
88   /**
89    * @copydoc PerformanceLogger::SetLoggingFrequency()
90    */
91   virtual void SetLoggingFrequency( unsigned int logFrequency, ContextId contextId );
92
93   /**
94    * @copydoc PerformanceLogger::EnableLogging()
95    */
96   virtual void EnableLogging( bool enable, ContextId contextId );
97
98 private:
99
100   /**
101    * Class to hold a stat context
102    */
103   class StatContext
104   {
105   public:
106
107     /**
108      * Default constructor
109      */
110     StatContext();
111
112     /**
113      * Constructor
114      *
115      * @param[in] id The ID to give the context
116      * @param[in] contextName Name of the context to print in console
117      */
118     StatContext( unsigned int id, const char* contextName );
119
120     /**
121      * Return the ID
122      *
123      * @return Return the ID
124      */
125     unsigned int GetId() const;
126
127     /**
128      * Return the context name
129      *
130      * @return Return the context name
131      */
132     const char* GetName() const;
133
134     /**
135      * Log the given marker
136      *
137      * @param[in] marker The marker to log
138      */
139     void LogMarker( const PerformanceMarker& marker );
140
141     /**
142      * Set the frequency for logging
143      *
144      * @param[in] frequencyMicroseconds The frequency to set
145      */
146     void SetLogFrequency( unsigned int frequencyMicroseconds );
147
148     /**
149      * Enable/disable logging
150      *
151      * @param[in] enableLogging Flag to spePerformancecify enabling/disabling
152      */
153     void EnableLogging( bool enableLogging );
154
155   private:
156
157     /**
158      * Helper to print to console
159      */
160     void LogMarker();
161
162     /**
163      * Accumulate timer from markers
164      */
165     void TimeMarkers();
166
167   private:
168
169     typedef Dali::Vector< PerformanceMarker > MarkerVector;
170
171     FrameTimeStats mStats;       ///< Frame time stats to accumulate
172     MarkerVector mMarkers;       ///< vector of markers
173     boost::mutex mDataMutex;     ///< mutex
174
175     const char* mName;           ///< Name of the context
176
177     unsigned int mId;            ///< The ID of the context
178
179     unsigned int mLogFrequencyMicroseconds; ///< if logging is enabled, what frequency to log out at in micro-seconds
180     bool mLog;                              ///< Whether to print the log for this context or not
181   };
182
183   /**
184    * Helper
185    */
186   void AddMarkerToLog( const PerformanceMarker& marker, ContextId contextId );
187
188   /**
189    * Retrieve a stat context by ID
190    *
191    * @param[in] contextId The ID of the context to retrieve
192    * @return Return the stat context if it exists or null
193    */
194   StatContext* GetContext( ContextId contextId );
195
196   /**
197    * Return a string representation of the marker type
198    *
199    * @param[in] type The marker type
200    * @return Return the name of the given marker
201    */
202   const char* GetMarkerName( MarkerType type ) const;
203
204 private:
205
206   typedef Dali::Vector< StatContext* > StatContexts;
207
208   // The list of stat contexts
209   StatContexts mStatContexts;
210
211   Integration::PlatformAbstraction& mPlatformAbstraction; ///< platform abstraction
212   const EnvironmentOptions& mEnvironmentOptions;          ///< environment options
213   KernelTraceInterface& mKernelTrace;                     ///< kernel trace interface
214
215   unsigned int mLogLevel;         ///< log level
216   unsigned int mNextContextId;    ///< The next valid context ID
217
218   // Some defaults
219   ContextId mUpdateStats;    ///< update time statistics
220   ContextId mRenderStats;    ///< render time statistics
221   ContextId mEventStats;     ///< event time statistics
222
223   bool mLoggingEnabled:1;         ///< whether logging update / render to a log is enabled
224   bool mLogFunctionInstalled:1;   ///< whether the log function is installed
225 };
226
227
228 } // namespace Internal
229
230 } // namespace Adaptor
231
232 } // namespace Dali
233
234 #endif