Refactor and add Wayland support.
[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   mClearSampler("Clear calls"),
129   mBindBufferSampler( "Bind buffers"),
130   mBindTextureSampler( "Bind textures"),
131   mDrawSampler("Draw calls"),
132   mUniformSampler("Uniform sets"),
133   mUseProgramSampler("Used programs"),
134   mDrawCount(0),
135   mUniformCount(0),
136   mFrameCount(0)
137 {
138 }
139
140 GlProxyImplementation::~GlProxyImplementation()
141 {
142 }
143
144 void GlProxyImplementation::PreRender()
145 {
146 }
147
148 void GlProxyImplementation::PostRender( unsigned int timeDelta )
149 {
150   // Accumulate counts in each sampler
151   AccumulateSamples();
152
153   // When we reach the desired frame count, output the averages from the samples
154   mFrameCount++;
155   if( mFrameCount >= mEnvironmentOptions.GetGlesCallTime() * NUM_FRAMES_PER_SECOND )
156   {
157     LogResults();
158     ResetSamplers();
159   }
160 }
161
162 void GlProxyImplementation::Clear( GLbitfield mask )
163 {
164   mClearSampler.Increment();
165   GlImplementation::Clear(mask);
166 }
167
168 void GlProxyImplementation::BindBuffer( GLenum target, GLuint buffer )
169 {
170   mBindBufferSampler.Increment();
171   GlImplementation::BindBuffer(target,buffer);
172 }
173
174 void GlProxyImplementation::BindTexture( GLenum target, GLuint texture )
175 {
176   mBindTextureSampler.Increment();
177   GlImplementation::BindTexture(target,texture);
178 }
179
180 void GlProxyImplementation::DrawArrays( GLenum mode, GLint first, GLsizei count )
181 {
182   mDrawSampler.Increment();
183   GlImplementation::DrawArrays(mode,first,count);
184 }
185
186 void GlProxyImplementation::DrawElements( GLenum mode, GLsizei count, GLenum type, const void* indices )
187 {
188   mDrawSampler.Increment();
189   GlImplementation::DrawElements(mode,count,type,indices);
190 }
191
192 void GlProxyImplementation::Uniform1f( GLint location, GLfloat x )
193 {
194   mUniformSampler.Increment();
195   GlImplementation::Uniform1f(location,x);
196 }
197
198 void GlProxyImplementation::Uniform1fv( GLint location, GLsizei count, const GLfloat* v )
199 {
200   mUniformSampler.Increment();
201   GlImplementation::Uniform1fv(location,count,v);
202 }
203
204 void GlProxyImplementation::Uniform1i( GLint location, GLint x )
205 {
206   mUniformSampler.Increment();
207   GlImplementation::Uniform1i(location,x);
208 }
209
210 void GlProxyImplementation::Uniform1iv( GLint location, GLsizei count, const GLint* v )
211 {
212   mUniformSampler.Increment();
213   GlImplementation::Uniform1iv(location,count,v);
214 }
215
216 void GlProxyImplementation::Uniform2f( GLint location, GLfloat x, GLfloat y)
217 {
218   mUniformSampler.Increment();
219   GlImplementation::Uniform2f(location,x,y);
220 }
221
222 void GlProxyImplementation::Uniform2fv( GLint location, GLsizei count, const GLfloat* v )
223 {
224   mUniformSampler.Increment();
225   GlImplementation::Uniform2fv(location,count,v);
226 }
227
228 void GlProxyImplementation::Uniform2i( GLint location, GLint x, GLint y )
229 {
230   mUniformSampler.Increment();
231   GlImplementation::Uniform2i(location,x,y);
232 }
233
234 void GlProxyImplementation::Uniform2iv( GLint location, GLsizei count, const GLint* v )
235 {
236   mUniformSampler.Increment();
237   GlImplementation::Uniform2iv(location,count,v);
238 }
239
240 void GlProxyImplementation::Uniform3f( GLint location, GLfloat x, GLfloat y, GLfloat z)
241 {
242   mUniformSampler.Increment();
243   GlImplementation::Uniform3f(location,x,y,z);
244 }
245
246 void GlProxyImplementation::Uniform3fv( GLint location, GLsizei count, const GLfloat* v )
247 {
248   mUniformSampler.Increment();
249   GlImplementation::Uniform3fv(location,count,v);
250 }
251
252 void GlProxyImplementation::Uniform3i( GLint location, GLint x, GLint y, GLint z )
253 {
254   mUniformSampler.Increment();
255   GlImplementation::Uniform3i(location,x,y,z);
256 }
257
258 void GlProxyImplementation::Uniform3iv( GLint location, GLsizei count, const GLint* v )
259 {
260   mUniformSampler.Increment();
261   GlImplementation::Uniform3iv(location,count,v);
262 }
263
264 void GlProxyImplementation::Uniform4f( GLint location, GLfloat x, GLfloat y, GLfloat z, GLfloat w )
265 {
266   mUniformSampler.Increment();
267   GlImplementation::Uniform4f(location,x,y,z,w);
268 }
269
270 void GlProxyImplementation::Uniform4fv( GLint location, GLsizei count, const GLfloat* v )
271 {
272   mUniformSampler.Increment();
273   GlImplementation::Uniform4fv(location,count,v);
274 }
275
276 void GlProxyImplementation::Uniform4i( GLint location, GLint x, GLint y, GLint z, GLint w )
277 {
278   mUniformSampler.Increment();
279   GlImplementation::Uniform4i(location,x,y,z,w);
280 }
281
282 void GlProxyImplementation::Uniform4iv( GLint location, GLsizei count, const GLint* v )
283 {
284   mUniformSampler.Increment();
285   GlImplementation::Uniform4iv(location,count,v);
286 }
287
288 void GlProxyImplementation::UniformMatrix2fv( GLint location, GLsizei count, GLboolean transpose, const GLfloat* value )
289 {
290   mUniformSampler.Increment();
291   GlImplementation::UniformMatrix2fv(location,count,transpose,value);
292 }
293
294 void GlProxyImplementation::UniformMatrix3fv( GLint location, GLsizei count, GLboolean transpose, const GLfloat* value )
295 {
296   mUniformSampler.Increment();
297   GlImplementation::UniformMatrix3fv(location,count,transpose,value);
298 }
299
300 void GlProxyImplementation::UniformMatrix4fv( GLint location, GLsizei count, GLboolean transpose, const GLfloat* value )
301 {
302   mUniformSampler.Increment();
303   GlImplementation::UniformMatrix4fv(location,count,transpose,value);
304 }
305
306 void GlProxyImplementation::UseProgram( GLuint program )
307 {
308   mUseProgramSampler.Increment();
309   GlImplementation::UseProgram(program);
310 }
311
312 void GlProxyImplementation::AccumulateSamples()
313 {
314   // Accumulate counts in each sampler
315   mClearSampler.Accumulate();
316   mBindBufferSampler.Accumulate();
317   mBindTextureSampler.Accumulate();
318   mDrawSampler.Accumulate();
319   mUniformSampler.Accumulate();
320   mUseProgramSampler.Accumulate();
321 }
322
323 void GlProxyImplementation::LogResults()
324 {
325   Debug::LogMessage( Debug::DebugInfo, "OpenGL ES statistics sampled over %d frames) operations per frame:\n", mFrameCount );
326   LogCalls( mClearSampler );
327   LogCalls( mBindBufferSampler );
328   LogCalls( mBindTextureSampler );
329   LogCalls( mDrawSampler );
330   LogCalls( mUniformSampler );
331   LogCalls( mUseProgramSampler );
332 }
333
334 void GlProxyImplementation::LogCalls( const Sampler& sampler )
335 {
336   Debug::LogMessage( Debug::DebugInfo, "  %s : Mean %5.2f  (Min:%5.2f, Max:%5.2f, StdDev:%5.2f)\n",
337                      sampler.GetDescription(),
338                      sampler.GetMeanValue(), sampler.GetMin(), sampler.GetMax(),
339                      sampler.GetStandardDeviation() );
340 }
341
342 void GlProxyImplementation::ResetSamplers()
343 {
344   mClearSampler.Reset();
345   mBindBufferSampler.Reset();
346   mBindTextureSampler.Reset();
347   mDrawSampler.Reset();
348   mUniformSampler.Reset();
349   mUseProgramSampler.Reset();
350   mFrameCount = 0;
351 }
352
353 } // namespace Adaptor
354
355 } // namespace Internal
356
357 } // namespace Dali