Merge "Track ActiveTexture calls" into tizen
[platform/core/uifw/dali-adaptor.git] / adaptors / common / gl / gl-proxy-implementation.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include "gl-proxy-implementation.h"
19
20 // EXTERNAL INCLUDES
21 #include <math.h>
22
23 // INTERNAL INCLUDES
24 #include <base/environment-options.h>
25 #include <dali/integration-api/debug.h>
26
27 namespace
28 {
29 const int NUM_FRAMES_PER_SECOND(60);
30 }
31
32
33 namespace Dali
34 {
35 namespace Internal
36 {
37 namespace Adaptor
38 {
39
40 Sampler::Sampler( const char* description )
41 : mDescription( description ),
42   mAccumulated(0.0f),
43   mAccumulatedSquare(0.0f),
44   mMin(0.0f),
45   mMax(0.0f),
46   mNumSamples(0),
47   mCurrentFrameCount(0)
48 {
49 }
50
51 void Sampler::Increment()
52 {
53   mCurrentFrameCount++;
54 }
55
56 void Sampler::Reset()
57 {
58   mAccumulated = 0.0f;
59   mAccumulatedSquare= 0.0f;
60   mMin = 0.0f;
61   mMax = 0.0f;
62   mNumSamples = 0;
63   mCurrentFrameCount = 0;
64 }
65
66 void Sampler::Accumulate()
67 {
68   if( mNumSamples == 0 )
69   {
70     mMin = mCurrentFrameCount;
71     mMax = mCurrentFrameCount;
72   }
73   else
74   {
75     if(mCurrentFrameCount < mMin)
76     {
77       mMin = mCurrentFrameCount;
78     }
79     if(mCurrentFrameCount > mMax)
80     {
81       mMax = mCurrentFrameCount;
82     }
83   }
84
85   mNumSamples++;
86
87   mAccumulated += mCurrentFrameCount;
88   mAccumulatedSquare += (mCurrentFrameCount * mCurrentFrameCount);
89   mCurrentFrameCount = 0;
90 }
91 const char* Sampler::GetDescription() const
92 {
93   return mDescription;
94 }
95
96 float Sampler::GetMeanValue() const
97 {
98   float meanValue = 0;
99   if( mNumSamples > 0 )
100   {
101     meanValue = mAccumulated / (float)mNumSamples;
102   }
103   return meanValue;
104 }
105
106 float Sampler::GetStandardDeviation() const
107 {
108   float standardDeviation=0.0f;
109   if( mNumSamples > 0 )
110   {
111     standardDeviation = sqrtf( mNumSamples * mAccumulatedSquare - (mAccumulated*mAccumulated)) / mNumSamples;
112   }
113   return standardDeviation;
114 }
115
116 float Sampler::GetMin() const
117 {
118   return mMin;
119 }
120
121 float Sampler::GetMax() const
122 {
123   return mMax;
124 }
125
126 GlProxyImplementation::GlProxyImplementation(EnvironmentOptions& environmentOptions)
127 : mEnvironmentOptions(environmentOptions),
128   mActiveTextureSampler( "ActiveTexture calls"),
129   mClearSampler("Clear calls"),
130   mBindBufferSampler( "Bind buffers"),
131   mBindTextureSampler( "Bind textures"),
132   mDrawSampler("Draw calls"),
133   mUniformSampler("Uniform sets"),
134   mUseProgramSampler("Used programs"),
135   mDrawCount(0),
136   mUniformCount(0),
137   mFrameCount(0)
138 {
139 }
140
141 GlProxyImplementation::~GlProxyImplementation()
142 {
143 }
144
145 void GlProxyImplementation::PreRender()
146 {
147 }
148
149 void GlProxyImplementation::PostRender( unsigned int timeDelta )
150 {
151   // Accumulate counts in each sampler
152   AccumulateSamples();
153
154   // When we reach the desired frame count, output the averages from the samples
155   mFrameCount++;
156   if( mFrameCount >= mEnvironmentOptions.GetGlesCallTime() * NUM_FRAMES_PER_SECOND )
157   {
158     LogResults();
159     ResetSamplers();
160   }
161 }
162
163 void GlProxyImplementation::ActiveTexture( GLenum texture )
164 {
165   mActiveTextureSampler.Increment();
166   GlImplementation::ActiveTexture(texture);
167 }
168
169 void GlProxyImplementation::Clear( GLbitfield mask )
170 {
171   mClearSampler.Increment();
172   GlImplementation::Clear(mask);
173 }
174
175 void GlProxyImplementation::BindBuffer( GLenum target, GLuint buffer )
176 {
177   mBindBufferSampler.Increment();
178   GlImplementation::BindBuffer(target,buffer);
179 }
180
181 void GlProxyImplementation::BindTexture( GLenum target, GLuint texture )
182 {
183   mBindTextureSampler.Increment();
184   GlImplementation::BindTexture(target,texture);
185 }
186
187 void GlProxyImplementation::DrawArrays( GLenum mode, GLint first, GLsizei count )
188 {
189   mDrawSampler.Increment();
190   GlImplementation::DrawArrays(mode,first,count);
191 }
192
193 void GlProxyImplementation::DrawElements( GLenum mode, GLsizei count, GLenum type, const void* indices )
194 {
195   mDrawSampler.Increment();
196   GlImplementation::DrawElements(mode,count,type,indices);
197 }
198
199 void GlProxyImplementation::Uniform1f( GLint location, GLfloat x )
200 {
201   mUniformSampler.Increment();
202   GlImplementation::Uniform1f(location,x);
203 }
204
205 void GlProxyImplementation::Uniform1fv( GLint location, GLsizei count, const GLfloat* v )
206 {
207   mUniformSampler.Increment();
208   GlImplementation::Uniform1fv(location,count,v);
209 }
210
211 void GlProxyImplementation::Uniform1i( GLint location, GLint x )
212 {
213   mUniformSampler.Increment();
214   GlImplementation::Uniform1i(location,x);
215 }
216
217 void GlProxyImplementation::Uniform1iv( GLint location, GLsizei count, const GLint* v )
218 {
219   mUniformSampler.Increment();
220   GlImplementation::Uniform1iv(location,count,v);
221 }
222
223 void GlProxyImplementation::Uniform2f( GLint location, GLfloat x, GLfloat y)
224 {
225   mUniformSampler.Increment();
226   GlImplementation::Uniform2f(location,x,y);
227 }
228
229 void GlProxyImplementation::Uniform2fv( GLint location, GLsizei count, const GLfloat* v )
230 {
231   mUniformSampler.Increment();
232   GlImplementation::Uniform2fv(location,count,v);
233 }
234
235 void GlProxyImplementation::Uniform2i( GLint location, GLint x, GLint y )
236 {
237   mUniformSampler.Increment();
238   GlImplementation::Uniform2i(location,x,y);
239 }
240
241 void GlProxyImplementation::Uniform2iv( GLint location, GLsizei count, const GLint* v )
242 {
243   mUniformSampler.Increment();
244   GlImplementation::Uniform2iv(location,count,v);
245 }
246
247 void GlProxyImplementation::Uniform3f( GLint location, GLfloat x, GLfloat y, GLfloat z)
248 {
249   mUniformSampler.Increment();
250   GlImplementation::Uniform3f(location,x,y,z);
251 }
252
253 void GlProxyImplementation::Uniform3fv( GLint location, GLsizei count, const GLfloat* v )
254 {
255   mUniformSampler.Increment();
256   GlImplementation::Uniform3fv(location,count,v);
257 }
258
259 void GlProxyImplementation::Uniform3i( GLint location, GLint x, GLint y, GLint z )
260 {
261   mUniformSampler.Increment();
262   GlImplementation::Uniform3i(location,x,y,z);
263 }
264
265 void GlProxyImplementation::Uniform3iv( GLint location, GLsizei count, const GLint* v )
266 {
267   mUniformSampler.Increment();
268   GlImplementation::Uniform3iv(location,count,v);
269 }
270
271 void GlProxyImplementation::Uniform4f( GLint location, GLfloat x, GLfloat y, GLfloat z, GLfloat w )
272 {
273   mUniformSampler.Increment();
274   GlImplementation::Uniform4f(location,x,y,z,w);
275 }
276
277 void GlProxyImplementation::Uniform4fv( GLint location, GLsizei count, const GLfloat* v )
278 {
279   mUniformSampler.Increment();
280   GlImplementation::Uniform4fv(location,count,v);
281 }
282
283 void GlProxyImplementation::Uniform4i( GLint location, GLint x, GLint y, GLint z, GLint w )
284 {
285   mUniformSampler.Increment();
286   GlImplementation::Uniform4i(location,x,y,z,w);
287 }
288
289 void GlProxyImplementation::Uniform4iv( GLint location, GLsizei count, const GLint* v )
290 {
291   mUniformSampler.Increment();
292   GlImplementation::Uniform4iv(location,count,v);
293 }
294
295 void GlProxyImplementation::UniformMatrix2fv( GLint location, GLsizei count, GLboolean transpose, const GLfloat* value )
296 {
297   mUniformSampler.Increment();
298   GlImplementation::UniformMatrix2fv(location,count,transpose,value);
299 }
300
301 void GlProxyImplementation::UniformMatrix3fv( GLint location, GLsizei count, GLboolean transpose, const GLfloat* value )
302 {
303   mUniformSampler.Increment();
304   GlImplementation::UniformMatrix3fv(location,count,transpose,value);
305 }
306
307 void GlProxyImplementation::UniformMatrix4fv( GLint location, GLsizei count, GLboolean transpose, const GLfloat* value )
308 {
309   mUniformSampler.Increment();
310   GlImplementation::UniformMatrix4fv(location,count,transpose,value);
311 }
312
313 void GlProxyImplementation::UseProgram( GLuint program )
314 {
315   mUseProgramSampler.Increment();
316   GlImplementation::UseProgram(program);
317 }
318
319 void GlProxyImplementation::AccumulateSamples()
320 {
321   // Accumulate counts in each sampler
322   mActiveTextureSampler.Accumulate();
323   mClearSampler.Accumulate();
324   mBindBufferSampler.Accumulate();
325   mBindTextureSampler.Accumulate();
326   mDrawSampler.Accumulate();
327   mUniformSampler.Accumulate();
328   mUseProgramSampler.Accumulate();
329 }
330
331 void GlProxyImplementation::LogResults()
332 {
333   Debug::LogMessage( Debug::DebugInfo, "OpenGL ES statistics sampled over %d frames) operations per frame:\n", mFrameCount );
334   LogCalls( mActiveTextureSampler );
335   LogCalls( mClearSampler );
336   LogCalls( mBindBufferSampler );
337   LogCalls( mBindTextureSampler );
338   LogCalls( mDrawSampler );
339   LogCalls( mUniformSampler );
340   LogCalls( mUseProgramSampler );
341 }
342
343 void GlProxyImplementation::LogCalls( const Sampler& sampler )
344 {
345   Debug::LogMessage( Debug::DebugInfo, "  %s : Mean %5.2f  (Min:%5.2f, Max:%5.2f, StdDev:%5.2f)\n",
346                      sampler.GetDescription(),
347                      sampler.GetMeanValue(), sampler.GetMin(), sampler.GetMax(),
348                      sampler.GetStandardDeviation() );
349 }
350
351 void GlProxyImplementation::ResetSamplers()
352 {
353   mActiveTextureSampler.Reset();
354   mClearSampler.Reset();
355   mBindBufferSampler.Reset();
356   mBindTextureSampler.Reset();
357   mDrawSampler.Reset();
358   mUniformSampler.Reset();
359   mUseProgramSampler.Reset();
360   mFrameCount = 0;
361 }
362
363 } // namespace Adaptor
364
365 } // namespace Internal
366
367 } // namespace Dali