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