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