03eec987f359d8f7260d17cadd2650e5ec1ba9da
[platform/core/uifw/dali-adaptor.git] / adaptors / common / gl / gl-proxy-implementation.cpp
1 /*
2  * Copyright (c) 2016 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 "gl-proxy-implementation.h"
20
21 // EXTERNAL INCLUDES
22 #include <math.h>
23
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
27 #include <base/environment-options.h>
28
29 namespace
30 {
31 const int NUM_FRAMES_PER_SECOND( 60 );
32 }
33
34
35 namespace Dali
36 {
37 namespace Internal
38 {
39 namespace Adaptor
40 {
41
42 Sampler::Sampler( const char* description )
43 : mDescription( description ),
44   mAccumulatedSquare( 0 ),
45   mAccumulated( 0 ),
46   mNumSamples( 0 ),
47   mMin( 0.0f ),
48   mMax( 0.0f ),
49   mCurrentFrameCount( 0 )
50 {
51 }
52
53 void Sampler::Increment()
54 {
55   mCurrentFrameCount++;
56 }
57
58 void Sampler::Reset()
59 {
60   mAccumulatedSquare = 0;
61   mAccumulated = 0;
62   mNumSamples = 0;
63   mMin = 0.0f;
64   mMax = 0.0f;
65   mCurrentFrameCount = 0;
66 }
67
68 void Sampler::Accumulate()
69 {
70   if( mNumSamples == 0 )
71   {
72     mMin = mCurrentFrameCount;
73     mMax = mCurrentFrameCount;
74   }
75   else
76   {
77     if( mCurrentFrameCount < mMin )
78     {
79       mMin = mCurrentFrameCount;
80     }
81     if( mCurrentFrameCount > mMax )
82     {
83       mMax = mCurrentFrameCount;
84     }
85   }
86
87   mNumSamples++;
88
89   mAccumulated += mCurrentFrameCount;
90   mAccumulatedSquare += ( mCurrentFrameCount * mCurrentFrameCount );
91   mCurrentFrameCount = 0;
92 }
93 const char* Sampler::GetDescription() const
94 {
95   return mDescription;
96 }
97
98 float Sampler::GetMeanValue() const
99 {
100   float meanValue = 0;
101   if( mNumSamples > 0 )
102   {
103     meanValue = static_cast<double>( mAccumulated ) / static_cast<double>( mNumSamples );
104   }
105   return meanValue;
106 }
107
108 float Sampler::GetStandardDeviation() const
109 {
110   float standardDeviation = 0.0f;
111   if( mNumSamples > 0 )
112   {
113     standardDeviation = sqrtf( mNumSamples * mAccumulatedSquare - ( mAccumulated * mAccumulated ) ) / mNumSamples;
114   }
115   return standardDeviation;
116 }
117
118 float Sampler::GetMin() const
119 {
120   return mMin;
121 }
122
123 float Sampler::GetMax() const
124 {
125   return mMax;
126 }
127
128 uint64_t Sampler::GetCount() const
129 {
130   return mAccumulated;
131 }
132
133 ObjectCounter::ObjectCounter( const char* description )
134 : mDescription( description ),
135   mCount( 0 ),
136   mPeak( 0 )
137 {}
138
139 void ObjectCounter::Increment()
140 {
141   ++mCount;
142   if( mCount > mPeak )
143   {
144     mPeak = mCount;
145   }
146 }
147
148 void ObjectCounter::Decrement()
149 {
150   --mCount;
151 }
152
153 unsigned int ObjectCounter::GetCount() const
154 {
155   return mCount;
156 }
157 unsigned int ObjectCounter::GetPeak() const
158 {
159   return mPeak;
160 }
161
162 const char* ObjectCounter::GetDescription() const
163 {
164   return mDescription;
165 }
166
167 GlProxyImplementation::GlProxyImplementation( EnvironmentOptions& environmentOptions )
168 : mEnvironmentOptions( environmentOptions ),
169   mActiveTextureSampler( "ActiveTexture calls" ),
170   mClearSampler( "Clear calls" ),
171   mBindBufferSampler( "Bind buffers" ),
172   mBindTextureSampler( "Bind textures" ),
173   mDrawSampler( "Draw calls" ),
174   mUniformSampler( "Uniform sets" ),
175   mUseProgramSampler( "Used programs" ),
176   mBufferCount( "Buffer Count" ),
177   mTextureCount( "Texture Count" ),
178   mProgramCount( "Program Count" ),
179   mCurrentFrameCount( 0 ),
180   mTotalFrameCount( 0 )
181 {
182 }
183
184 GlProxyImplementation::~GlProxyImplementation()
185 {
186 }
187
188 void GlProxyImplementation::PreRender()
189 {
190 }
191
192 void GlProxyImplementation::PostRender()
193 {
194   // Accumulate counts in each sampler
195   AccumulateSamples();
196
197   // When we reach the desired frame count, output the averages from the samples
198   mTotalFrameCount++;
199   mCurrentFrameCount++;
200
201   if( mCurrentFrameCount >= mEnvironmentOptions.GetGlesCallTime() * NUM_FRAMES_PER_SECOND )
202   {
203     mCurrentFrameCount = 0;
204     LogResults();
205
206     if( !mEnvironmentOptions.GetGlesCallAccumulate() )
207     {
208       ResetSamplers();
209     }
210   }
211 }
212
213 void GlProxyImplementation::Clear( GLbitfield mask )
214 {
215   mClearSampler.Increment();
216   GlImplementation::Clear(mask);
217 }
218
219 void GlProxyImplementation::GenBuffers(GLsizei n, GLuint* buffers)
220 {
221   mBufferCount.Increment();
222   GlImplementation::GenBuffers( n, buffers );
223 }
224
225 void GlProxyImplementation::DeleteBuffers( GLsizei n, const GLuint* buffers )
226 {
227   mBufferCount.Decrement();
228   GlImplementation::DeleteBuffers( n, buffers );
229 }
230
231 void GlProxyImplementation::BindBuffer( GLenum target, GLuint buffer )
232 {
233   mBindBufferSampler.Increment();
234   GlImplementation::BindBuffer( target, buffer );
235 }
236
237 void GlProxyImplementation::GenTextures( GLsizei n, GLuint* textures )
238 {
239   mTextureCount.Increment();
240   GlImplementation::GenTextures( n, textures );
241 }
242
243 void GlProxyImplementation::DeleteTextures( GLsizei n, const GLuint* textures )
244 {
245   mTextureCount.Decrement();
246   GlImplementation::DeleteTextures( n, textures );
247 }
248
249 void GlProxyImplementation::ActiveTexture( GLenum texture )
250 {
251   mActiveTextureSampler.Increment();
252   GlImplementation::ActiveTexture( texture );
253 }
254
255 void GlProxyImplementation::BindTexture( GLenum target, GLuint texture )
256 {
257   mBindTextureSampler.Increment();
258   GlImplementation::BindTexture(target,texture);
259 }
260
261 void GlProxyImplementation::DrawArrays( GLenum mode, GLint first, GLsizei count )
262 {
263   mDrawSampler.Increment();
264   GlImplementation::DrawArrays( mode, first, count );
265 }
266
267 void GlProxyImplementation::DrawElements( GLenum mode, GLsizei count, GLenum type, const void* indices )
268 {
269   mDrawSampler.Increment();
270   GlImplementation::DrawElements( mode, count, type, indices );
271 }
272
273 void GlProxyImplementation::Uniform1f( GLint location, GLfloat x )
274 {
275   mUniformSampler.Increment();
276   GlImplementation::Uniform1f( location, x );
277 }
278
279 void GlProxyImplementation::Uniform1fv( GLint location, GLsizei count, const GLfloat* v )
280 {
281   mUniformSampler.Increment();
282   GlImplementation::Uniform1fv( location, count, v );
283 }
284
285 void GlProxyImplementation::Uniform1i( GLint location, GLint x )
286 {
287   mUniformSampler.Increment();
288   GlImplementation::Uniform1i( location, x );
289 }
290
291 void GlProxyImplementation::Uniform1iv( GLint location, GLsizei count, const GLint* v )
292 {
293   mUniformSampler.Increment();
294   GlImplementation::Uniform1iv( location, count, v );
295 }
296
297 void GlProxyImplementation::Uniform2f( GLint location, GLfloat x, GLfloat y)
298 {
299   mUniformSampler.Increment();
300   GlImplementation::Uniform2f( location, x, y );
301 }
302
303 void GlProxyImplementation::Uniform2fv( GLint location, GLsizei count, const GLfloat* v )
304 {
305   mUniformSampler.Increment();
306   GlImplementation::Uniform2fv( location, count, v );
307 }
308
309 void GlProxyImplementation::Uniform2i( GLint location, GLint x, GLint y )
310 {
311   mUniformSampler.Increment();
312   GlImplementation::Uniform2i( location, x, y );
313 }
314
315 void GlProxyImplementation::Uniform2iv( GLint location, GLsizei count, const GLint* v )
316 {
317   mUniformSampler.Increment();
318   GlImplementation::Uniform2iv( location, count, v );
319 }
320
321 void GlProxyImplementation::Uniform3f( GLint location, GLfloat x, GLfloat y, GLfloat z )
322 {
323   mUniformSampler.Increment();
324   GlImplementation::Uniform3f( location, x, y, z );
325 }
326
327 void GlProxyImplementation::Uniform3fv( GLint location, GLsizei count, const GLfloat* v )
328 {
329   mUniformSampler.Increment();
330   GlImplementation::Uniform3fv( location, count, v );
331 }
332
333 void GlProxyImplementation::Uniform3i( GLint location, GLint x, GLint y, GLint z )
334 {
335   mUniformSampler.Increment();
336   GlImplementation::Uniform3i( location, x, y, z );
337 }
338
339 void GlProxyImplementation::Uniform3iv( GLint location, GLsizei count, const GLint* v )
340 {
341   mUniformSampler.Increment();
342   GlImplementation::Uniform3iv( location, count, v );
343 }
344
345 void GlProxyImplementation::Uniform4f( GLint location, GLfloat x, GLfloat y, GLfloat z, GLfloat w )
346 {
347   mUniformSampler.Increment();
348   GlImplementation::Uniform4f( location, x, y, z, w );
349 }
350
351 void GlProxyImplementation::Uniform4fv( GLint location, GLsizei count, const GLfloat* v )
352 {
353   mUniformSampler.Increment();
354   GlImplementation::Uniform4fv( location, count, v );
355 }
356
357 void GlProxyImplementation::Uniform4i( GLint location, GLint x, GLint y, GLint z, GLint w )
358 {
359   mUniformSampler.Increment();
360   GlImplementation::Uniform4i( location, x, y, z, w );
361 }
362
363 void GlProxyImplementation::Uniform4iv( GLint location, GLsizei count, const GLint* v )
364 {
365   mUniformSampler.Increment();
366   GlImplementation::Uniform4iv( location, count, v );
367 }
368
369 void GlProxyImplementation::UniformMatrix2fv( GLint location, GLsizei count, GLboolean transpose, const GLfloat* value )
370 {
371   mUniformSampler.Increment();
372   GlImplementation::UniformMatrix2fv( location, count, transpose, value );
373 }
374
375 void GlProxyImplementation::UniformMatrix3fv( GLint location, GLsizei count, GLboolean transpose, const GLfloat* value )
376 {
377   mUniformSampler.Increment();
378   GlImplementation::UniformMatrix3fv( location, count, transpose, value );
379 }
380
381 void GlProxyImplementation::UniformMatrix4fv( GLint location, GLsizei count, GLboolean transpose, const GLfloat* value )
382 {
383   mUniformSampler.Increment();
384   GlImplementation::UniformMatrix4fv( location, count, transpose, value);
385 }
386
387 GLuint GlProxyImplementation::CreateProgram( void )
388 {
389   mProgramCount.Increment();
390   return GlImplementation::CreateProgram();
391 }
392
393 void GlProxyImplementation::DeleteProgram( GLuint program )
394 {
395   mProgramCount.Decrement();
396   GlImplementation::DeleteProgram( program );
397 }
398
399 void GlProxyImplementation::UseProgram( GLuint program )
400 {
401   mUseProgramSampler.Increment();
402   GlImplementation::UseProgram( program );
403 }
404
405 void GlProxyImplementation::AccumulateSamples()
406 {
407   // Accumulate counts in each sampler
408   mActiveTextureSampler.Accumulate();
409   mClearSampler.Accumulate();
410   mBindBufferSampler.Accumulate();
411   mBindTextureSampler.Accumulate();
412   mDrawSampler.Accumulate();
413   mUniformSampler.Accumulate();
414   mUseProgramSampler.Accumulate();
415 }
416
417 void GlProxyImplementation::LogResults()
418 {
419   Debug::LogMessage( Debug::DebugInfo, "OpenGL ES statistics sampled over %d frames) operations per frame:\n", mTotalFrameCount );
420   LogCalls( mActiveTextureSampler );
421   LogCalls( mClearSampler );
422   LogCalls( mBindBufferSampler );
423   LogCalls( mBindTextureSampler );
424   LogCalls( mDrawSampler );
425   LogCalls( mUniformSampler );
426   LogCalls( mUseProgramSampler );
427   Debug::LogMessage( Debug::DebugInfo, "OpenGL ES Object Count:\n" );
428   LogObjectCounter( mBufferCount );
429   LogObjectCounter( mTextureCount );
430   LogObjectCounter( mProgramCount );
431 }
432
433 void GlProxyImplementation::LogCalls( const Sampler& sampler )
434 {
435   Debug::LogMessage( Debug::DebugInfo, "  %s : Mean %5.2f  (Min:%5.2f, Max:%5.2f, StdDev:%5.2f, Actual:%d)\n",
436                      sampler.GetDescription(),
437                      sampler.GetMeanValue(), sampler.GetMin(), sampler.GetMax(),
438                      sampler.GetStandardDeviation(),
439                      sampler.GetCount() );
440 }
441
442 void GlProxyImplementation::LogObjectCounter( const ObjectCounter& sampler )
443 {
444   Debug::LogMessage( Debug::DebugInfo, "  %s : %u  (Peak:%u)\n",
445                      sampler.GetDescription(),
446                      sampler.GetCount(),
447                      sampler.GetPeak() );
448 }
449
450 void GlProxyImplementation::ResetSamplers()
451 {
452   mActiveTextureSampler.Reset();
453   mClearSampler.Reset();
454   mBindBufferSampler.Reset();
455   mBindTextureSampler.Reset();
456   mDrawSampler.Reset();
457   mUniformSampler.Reset();
458   mUseProgramSampler.Reset();
459   mTotalFrameCount = 0;
460 }
461
462 } // namespace Adaptor
463
464 } // namespace Internal
465
466 } // namespace Dali