Merge "Set proper locale to harfbuzz" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / base / performance-logging / statistics / stat-context-manager.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 "stat-context-manager.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 namespace Adaptor
31 {
32
33 namespace
34 {
35 const char* const UPDATE_CONTEXT_NAME = "Update";
36 const char* const RENDER_CONTEXT_NAME = "Render";
37 const char* const EVENT_CONTEXT_NAME = "Event";
38 const unsigned int DEFAULT_LOG_FREQUENCY = 2;
39 }
40
41 StatContextManager::StatContextManager( StatContextLogInterface& logInterface )
42 : mLogInterface( logInterface ),
43   mNextContextId( 0 ),
44   mStatisticsLogBitmask(0),
45   mLogFrequency( DEFAULT_LOG_FREQUENCY )
46 {
47
48   mStatContexts.Reserve(4); // intially reserve enough for 3 internal + 1 custom
49
50   // Add defaults
51   mUpdateStats = AddContext( UPDATE_CONTEXT_NAME, PerformanceMarker::UPDATE );
52   mRenderStats = AddContext( RENDER_CONTEXT_NAME, PerformanceMarker::RENDER );
53   mEventStats = AddContext( EVENT_CONTEXT_NAME,   PerformanceMarker::EVENT_PROCESS );
54
55 }
56
57 StatContextManager::~StatContextManager()
58 {
59   for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
60   {
61     StatContext* context = *it;
62     delete context;
63   }
64   mStatContexts.Clear();
65 }
66 PerformanceInterface::ContextId StatContextManager::AddContext( const char* const name,
67                                                                 PerformanceMarker::MarkerFilter type  )
68 {
69   unsigned int contextId = mNextContextId++;
70
71   DALI_ASSERT_DEBUG( NULL == GetContext( contextId ) );
72
73   // logging enabled by default
74   StatContext* statContext = new StatContext( contextId, name, type,  mLogFrequency , mLogInterface );
75
76   // check to see if custom markers are enabled
77   if( ! ( mStatisticsLogBitmask & PerformanceInterface::LOG_CUSTOM_MARKERS ) )
78   {
79     statContext->EnableLogging( false );
80   }
81
82   mStatContexts.PushBack( statContext );
83
84   return contextId;
85 }
86
87 void StatContextManager::AddInternalMarker( const PerformanceMarker& marker )
88 {
89   // log to the stat contexts, can be called from multiple threads so we
90   // protect the data
91   Mutex::ScopedLock lock( mDataMutex );
92   for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
93   {
94     StatContext* context = *it;
95     context->ProcessInternalMarker( marker );
96   }
97 }
98
99 void StatContextManager::AddCustomMarker( const PerformanceMarker& marker, PerformanceInterface::ContextId contextId )
100 {
101   // log to the stat contexts, can be called from multiple threads so we
102   // protect the data
103   Mutex::ScopedLock lock( mDataMutex );
104   StatContext* context = GetContext( contextId );
105   if( context )
106   {
107     context->ProcessCustomMarker( marker );
108   }
109 }
110
111 void StatContextManager::RemoveContext(PerformanceInterface::ContextId contextId )
112 {
113   for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
114   {
115     StatContext* context = *it;
116
117     if( context->GetId() == contextId )
118     {
119       delete context;
120       mStatContexts.Erase( it );
121       return;
122     }
123   }
124 }
125
126
127 void StatContextManager::EnableLogging( bool enable, PerformanceInterface::ContextId contextId )
128 {
129   StatContext* context = GetContext( contextId );
130   if( context )
131   {
132     context->EnableLogging( enable );
133   }
134 }
135
136 void StatContextManager::SetLoggingLevel(  unsigned int statisticsLogOptions, unsigned int logFrequency)
137 {
138   mStatisticsLogBitmask = statisticsLogOptions;
139
140   if( mStatisticsLogBitmask == PerformanceInterface::LOG_EVERYTHING )
141   {
142     mStatisticsLogBitmask = 0xFFFFFFFF; // enable everything
143   }
144
145   mLogFrequency = logFrequency;
146
147   // currently uses DALI_LOG_PERFORMANCE_STATS_FREQ environment variable to determine to log frequency
148   // if it's not set it will be zero
149   if( mLogFrequency == 0 )
150   {
151     mLogFrequency = DEFAULT_LOG_FREQUENCY;
152   }
153   EnableLogging( mStatisticsLogBitmask & PerformanceInterface::LOG_UPDATE_RENDER, mUpdateStats );
154   EnableLogging( mStatisticsLogBitmask & PerformanceInterface::LOG_UPDATE_RENDER, mRenderStats );
155   EnableLogging( mStatisticsLogBitmask & PerformanceInterface::LOG_EVENT_PROCESS, mEventStats );
156
157   for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
158   {
159      StatContext* context = *it;
160      context->SetLogFrequency( mLogFrequency );
161   }
162 }
163
164 void StatContextManager::SetLoggingFrequency( unsigned int logFrequency,
165                                               PerformanceInterface::ContextId contextId  )
166 {
167   StatContext* context = GetContext( contextId );
168   if( context )
169   {
170     if( logFrequency == 0 )
171     {
172       logFrequency = DEFAULT_LOG_FREQUENCY;
173     }
174     context->SetLogFrequency( logFrequency );
175   }
176 }
177 const char* StatContextManager::GetContextName(PerformanceInterface::ContextId contextId) const
178 {
179   StatContext* context = GetContext(contextId);
180   if( context )
181   {
182     return context->GetName();
183   }
184   return "context not found";
185 }
186
187 const char* StatContextManager::GetMarkerDescription( PerformanceInterface::MarkerType type, PerformanceInterface::ContextId contextId ) const
188 {
189   StatContext* context = GetContext(contextId);
190   if( context )
191   {
192     return context->GetMarkerDescription( type );
193   }
194   return "context not found";
195 }
196
197
198 StatContext* StatContextManager::GetContext( PerformanceInterface::ContextId contextId ) const
199 {
200   for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
201   {
202     StatContext* context = *it;
203
204     if( context->GetId() == contextId )
205     {
206       return context;
207     }
208   }
209
210   return NULL;
211 }
212
213
214 } // namespace Internal
215
216 } // namespace Adaptor
217
218 } // namespace Dali
219
220