Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / context.h
1 #ifndef __DALI_INTERNAL_CONTEXT_H__
2 #define __DALI_INTERNAL_CONTEXT_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.0 (the License);
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //     http://floralicense.org/license/
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an AS IS BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19
20 // INTERNAL INCLUDES
21 #include <dali/public-api/common/map-wrapper.h>
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/actors/renderable-actor.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/gl-abstraction.h>
26 #include <dali/internal/render/common/performance-monitor.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/math/rect.h>
29 #include <dali/public-api/math/vector4.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 class Program; // to be able to cache programs
38
39 // wrap gl calls with CHECK_GL eg "CHECK_GL( *this, glBindTexture(textureId) );"
40 // will LOG any glErrors eg "glError (0x0501) GL_INVALID_VALUE - glBindTexture(textureId)"
41 // only enable if specifically enabled as it slows down GL a lot!
42 #ifdef DALI_GL_ERROR_CHECK
43 #define CHECK_GL(c,a)  (a); (c).CheckGlError(#a)
44 #else
45 #define CHECK_GL(c,a)  (a)
46 #endif
47
48 // Don't put guards around here (LOG_INFO has it's own guards)
49 #define LOG_GL(format, args...) \
50   DALI_LOG_INFO(Context::gGlLogFilter, Debug::General, format, ## args)
51
52 /**
53  * Context records the current GL state, and provides access to the OpenGL ES 2.0 API.
54  * Context avoids duplicate GL calls, if the same setting etc. is requested repeatedly.
55  */
56 class Context
57 {
58 public:
59
60   /**
61    * Size of the VertexAttributeArray enables
62    * GLES specification states that there's minimum of 8
63    */
64   static const unsigned int MAX_ATTRIBUTE_CACHE_SIZE = 8;
65
66   /**
67    * Creates the Dali Context object.
68    * This method does not create an OpenGL context i.e. that is done from outside dali-core.
69    * @pre Context has not been created.
70    * @exception Context already created.
71    * @param glAbstraction the gl abstraction.
72    */
73   Context( Integration::GlAbstraction& glAbstraction );
74
75   /**
76    * Destructor
77    */
78   ~Context();
79
80   /**
81    * Called when the GL context has been created.
82    */
83   void GlContextCreated();
84
85   /**
86    * Called when the GL context is about to be destroyed.
87    */
88   void GlContextDestroyed();
89
90   /**
91    * Query whether the OpenGL context has been created.
92    * @return True if the OpenGL context has been created.
93    */
94   bool IsGlContextCreated() { return mGlContextCreated; }
95
96   /**
97    * @return the GLAbstraction
98    */
99   Integration::GlAbstraction& GetAbstraction() { return mGlAbstraction; }
100
101 #ifdef DALI_CONTEXT_LOGGING
102
103   /**
104    * Debug helper which prints the currently cached GL state.
105    */
106   void PrintCurrentState();
107
108 #endif
109
110   /**
111    * Helper to convert GL error code to string
112    * @param errorCode to convert
113    * @return C string
114    */
115   const char* ErrorToString( GLenum errorCode );
116
117   /**
118    * Helper to check GL error during an operation
119    * @param operation that was performed
120    */
121   void CheckGlError( const char* operation )
122   {
123     for( GLint error = GetError(); error; error = GetError() )
124     {
125       DALI_LOG_ERROR( "glError (0x%x) %s - after %s\n",  error, ErrorToString(error), operation );
126       DALI_ASSERT_ALWAYS( error && "GL ERROR"); // if errors are being checked we should assert
127     }
128   }
129
130   /**
131    * Helper to print GL string to debug log
132    */
133   void PrintGlString(const char* stringName, GLenum stringId)
134   {
135     DALI_LOG_INFO(Debug::Filter::gRender, Debug::General, "GL %s = %s\n", stringName, (const char *)GetString( stringId ) );
136   }
137
138   /****************************************************************************************
139    * The following methods are forwarded to Dali::Integration::GlAbstraction.
140    * In some cases the GL state is recorded, to avoid duplicate calls with the same state.
141    * All Shader, Program, Uniform and Attribute related calls are not here, Program class
142    * handles them and optimizes any program related state changes
143    ****************************************************************************************/
144
145   /**
146    * Wrapper for OpenGL ES 2.0 glActiveTexture()
147    */
148   void ActiveTexture(GLenum textureUnit)
149   {
150     // GL texture units are #defines in growing order to converting that to index
151     unsigned int unit = textureUnit - GL_TEXTURE0;
152
153     if ( unit != mActiveTextureUnit )
154     {
155       mActiveTextureUnit = unit;
156       LOG_GL("ActiveTexture %x\n", textureUnit);
157       CHECK_GL( *this, mGlAbstraction.ActiveTexture(textureUnit) );
158     }
159   }
160
161   /**
162    * Wrapper for OpenGL ES 3.0 glBeginQuery()
163    */
164   void BeginQuery(GLenum target, GLuint id)
165   {
166     LOG_GL("BeginQuery %d %d\n", target, id);
167     CHECK_GL( *this, mGlAbstraction.BeginQuery(target, id) );
168   }
169
170   /**
171    * Wrapper for OpenGL ES 3.0 glBeginTransformFeedback()
172    */
173   void BeginTransformFeedback(GLenum primitiveMode)
174   {
175     LOG_GL("BeginTransformFeedback %x\n", primitiveMode);
176     CHECK_GL( *this, mGlAbstraction.BeginTransformFeedback(primitiveMode) );
177   }
178
179   /**
180    * The wrapper for OpenGL ES 2.0 glBindBuffer() has been replaced by BindArrayBuffer & BindElementArrayBuffer & BindTransformFeedbackBuffer.
181    */
182
183   /**
184    * Wrapper for OpenGL ES 2.0 glBindBuffer(GL_ARRAY_BUFFER, ...)
185    */
186   void BindArrayBuffer(GLuint buffer)
187   {
188     // Avoid unecessary calls to BindBuffer
189     if (mBoundArrayBufferId != buffer)
190     {
191       mBoundArrayBufferId = buffer;
192
193       LOG_GL("BindBuffer GL_ARRAY_BUFFER %d\n", buffer);
194       CHECK_GL( *this, mGlAbstraction.BindBuffer(GL_ARRAY_BUFFER, buffer) );
195     }
196   }
197
198   /**
199    * Wrapper for OpenGL ES 2.0 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ...)
200    */
201   void BindElementArrayBuffer(GLuint buffer)
202   {
203     // Avoid unecessary calls to BindBuffer
204     if (mBoundElementArrayBufferId!= buffer)
205     {
206       mBoundElementArrayBufferId = buffer;
207
208       LOG_GL("BindBuffer GL_ELEMENT_ARRAY_BUFFER %d\n", buffer);
209       CHECK_GL( *this, mGlAbstraction.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer) );
210     }
211   }
212
213   /**
214    * Wrapper for OpenGL ES 3.0 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, ...)
215    */
216   void BindTransformFeedbackBuffer(GLuint buffer)
217   {
218     // Avoid unecessary calls to BindBuffer
219     if (mBoundTransformFeedbackBufferId != buffer)
220     {
221       mBoundTransformFeedbackBufferId = buffer;
222
223       LOG_GL("BindBuffer GL_TRANSFORM_FEEDBACK_BUFFER %d\n", buffer);
224       CHECK_GL( *this, mGlAbstraction.BindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER , buffer) );
225     }
226   }
227
228   /**
229    * Wrapper for OpenGL ES 3.0 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, ...)
230    */
231   void BindTransformFeedbackBufferBase(GLuint index, GLuint buffer)
232   {
233     // Avoid unecessary calls to BindBufferBase
234     if (mBoundTransformFeedbackBufferId != buffer)
235     {
236       mBoundTransformFeedbackBufferId = buffer;
237
238       LOG_GL("BindBufferBase GL_TRANSFORM_FEEDBACK_BUFFER %d %d\n", index, buffer);
239       CHECK_GL( *this, mGlAbstraction.BindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, index, buffer) );
240     }
241   }
242
243   /**
244    * Wrapper for OpenGL ES 2.0 glBindFramebuffer()
245    */
246   void BindFramebuffer(GLenum target, GLuint framebuffer)
247   {
248     LOG_GL("BindFramebuffer %d %d\n", target, framebuffer);
249     CHECK_GL( *this, mGlAbstraction.BindFramebuffer(target, framebuffer) );
250   }
251
252   /**
253    * Wrapper for OpenGL ES 2.0 glBindRenderbuffer()
254    */
255   void BindRenderbuffer(GLenum target, GLuint renderbuffer)
256   {
257     LOG_GL("BindRenderbuffer %d %d\n", target, renderbuffer);
258     CHECK_GL( *this, mGlAbstraction.BindRenderbuffer(target, renderbuffer) );
259   }
260
261   /**
262    * Wrapper for OpenGL ES 3.0 glBindTransformFeedback()
263    */
264   void BindTransformFeedback(GLenum target, GLuint id)
265   {
266     LOG_GL("BindTransformFeedback %d %d\n", target, id);
267     CHECK_GL( *this, mGlAbstraction.BindTransformFeedback(target, id) );
268   }
269
270   /**
271    * The wrapper for OpenGL ES 2.0 glBindTexture() has been replaced by Bind2dTexture and BindCubeMapTexture.
272    */
273
274   /**
275    * Wrapper for OpenGL ES 2.0 glBindTexture(GL_TEXTURE_2D)
276    */
277   void Bind2dTexture( GLuint texture )
278   {
279     if (mBound2dTextureId[ mActiveTextureUnit ] != texture)
280     {
281       mBound2dTextureId[ mActiveTextureUnit ] = texture;
282
283       LOG_GL("BindTexture GL_TEXTURE_2D %d\n", texture);
284       CHECK_GL( *this, mGlAbstraction.BindTexture(GL_TEXTURE_2D, texture) );
285
286       INCREASE_COUNTER(PerformanceMonitor::TEXTURE_STATE_CHANGES);
287     }
288   }
289
290   /**
291    * Wrapper for OpenGL ES 2.0 glBlendColor()
292    */
293   void SetDefaultBlendColor()
294   {
295     if( !mUsingDefaultBlendColor )
296     {
297       LOG_GL( "BlendColor %f %f %f %f\n", 0.0f, 0.0f, 0.0f, 0.0f );
298       CHECK_GL( *this, mGlAbstraction.BlendColor( 0.0f, 0.0f, 0.0f, 0.0f ) );
299       mUsingDefaultBlendColor = true;
300     }
301   }
302
303   /**
304    * Wrapper for OpenGL ES 2.0 glBlendColor()
305    */
306   void SetCustomBlendColor( const Vector4& color )
307   {
308     LOG_GL( "BlendColor %f %f %f %f\n", color.r, color.g, color.b, color.a );
309     CHECK_GL( *this, mGlAbstraction.BlendColor(color.r, color.g, color.b, color.a) );
310     mUsingDefaultBlendColor = false;
311   }
312
313   /**
314    * Wrapper for OpenGL ES 2.0 glBlendEquation()
315    */
316   void BlendEquation(GLenum mode)
317   {
318     // use BlendEquationSeparate to set the rgb and alpha modes the same
319     BlendEquationSeparate( mode, mode );
320   }
321
322   /**
323    * Wrapper for OpenGL ES 2.0 glBlendEquationSeparate()
324    */
325   void BlendEquationSeparate(GLenum modeRGB, GLenum modeAlpha)
326   {
327     if( ( modeRGB != mBlendEquationSeparateModeRGB ) ||
328         ( modeAlpha != mBlendEquationSeparateModeAlpha ) )
329     {
330       mBlendEquationSeparateModeRGB = modeRGB;
331       mBlendEquationSeparateModeAlpha = modeAlpha;
332       LOG_GL("BlendEquationSeparate %d %d\n", modeRGB, modeAlpha);
333       CHECK_GL( *this, mGlAbstraction.BlendEquationSeparate(modeRGB, modeAlpha) );
334     }
335   }
336
337   /**
338    * Wrapper for OpenGL ES 2.0 glBlendFunc()
339    */
340   void BlendFunc(GLenum sfactor, GLenum dfactor)
341   {
342     // reuse the BlendFuncSeparate as thats what the DDK does anyways
343     BlendFuncSeparate( sfactor, dfactor, sfactor, dfactor );
344   }
345
346   /**
347    * Wrapper for OpenGL ES 2.0 glBlendFuncSeparate()
348    */
349   void BlendFuncSeparate( GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha )
350   {
351     if( ( mBlendFuncSeparateSrcRGB != srcRGB )||( mBlendFuncSeparateDstRGB != dstRGB )||
352         ( mBlendFuncSeparateSrcAlpha != srcAlpha )||( mBlendFuncSeparateDstAlpha != dstAlpha ) )
353     {
354       mBlendFuncSeparateSrcRGB = srcRGB;
355       mBlendFuncSeparateDstRGB = dstRGB;
356       mBlendFuncSeparateSrcAlpha = srcAlpha;
357       mBlendFuncSeparateDstAlpha = dstAlpha;
358
359       LOG_GL( "BlendFuncSeparate %d %d %d %d\n", srcRGB, dstRGB, srcAlpha, dstAlpha );
360       CHECK_GL( *this, mGlAbstraction.BlendFuncSeparate( srcRGB, dstRGB, srcAlpha, dstAlpha ) );
361     }
362   }
363
364   /**
365    * Wrapper for OpenGL ES 3.0 glBlitFramebuffer()
366    */
367   void BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter)
368   {
369     LOG_GL( "BlitFramebuffer %d %d %d %d %d %d %d %d %x %d\n", srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter );
370     CHECK_GL( *this, mGlAbstraction.BlitFramebuffer( srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter ) );
371   }
372
373   /**
374    * Wrapper for OpenGL ES 2.0 glBufferData()
375    */
376   void BufferData(GLenum target, GLsizeiptr size, const void* data, GLenum usage)
377   {
378     LOG_GL("BufferData %d %d %p %d\n", target, size, data, usage);
379     CHECK_GL( *this, mGlAbstraction.BufferData(target, size, data, usage) );
380   }
381
382   /**
383    * Wrapper for OpenGL ES 2.0 glBufferSubData()
384    */
385   void BufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const void* data)
386   {
387     LOG_GL("BufferSubData %d %d %d %p\n", target, offset, size, data);
388     CHECK_GL( *this, mGlAbstraction.BufferSubData(target, offset, size, data) );
389   }
390
391   /**
392    * Wrapper for OpenGL ES 2.0  glCheckFramebufferStatus()
393    */
394   GLenum CheckFramebufferStatus(GLenum target)
395   {
396     LOG_GL("CheckFramebufferStatus %d\n", target);
397     GLenum value = CHECK_GL( *this, mGlAbstraction.CheckFramebufferStatus(target) );
398     return value;
399   }
400
401   /**
402    * Wrapper for OpenGL ES 2.0 glClear()
403    */
404   void Clear(GLbitfield mask)
405   {
406     LOG_GL("Clear %d\n", mask);
407     CHECK_GL( *this, mGlAbstraction.Clear(mask) );
408   }
409
410   /**
411    * Wrapper for OpenGL ES 2.0 glClearColor()
412    */
413   void ClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
414   {
415     Vector4 newCol(red,green,blue,alpha);
416
417     if (!mClearColorSet || mClearColor !=newCol )
418     {
419       LOG_GL("ClearColor %f %f %f %f\n", red, green, blue, alpha);
420       CHECK_GL( *this, mGlAbstraction.ClearColor(red, green, blue, alpha) );
421
422       mClearColorSet = true;
423       mClearColor = newCol;
424     }
425   }
426
427   /**
428    * Wrapper for OpenGL ES 2.0 glClearDepthf()
429    */
430   void ClearDepthf(GLclampf depth)
431   {
432     LOG_GL("ClearDepthf %f\n", depth);
433     CHECK_GL( *this, mGlAbstraction.ClearDepthf(depth) );
434   }
435
436   /**
437    * Wrapper for OpenGL ES 2.0 glClearStencil()
438    */
439   void ClearStencil(GLint s)
440   {
441     LOG_GL("ClearStencil %d\n", s);
442     CHECK_GL( *this, mGlAbstraction.ClearStencil(s) );
443   }
444
445   /**
446    * Wrapper for OpenGL ES 2.0 glColorMask()
447    * @note This has been optimized to a single boolean value (masking individual channels is not required)
448    */
449   void ColorMask( bool flag )
450   {
451     // only change state if needed
452     if( flag != mColorMask )
453     {
454       mColorMask = flag;
455       LOG_GL("ColorMask %s %s %s %s\n", flag ? "True" : "False", flag ? "True" : "False", flag ? "True" : "False", flag ? "True" : "False");
456       CHECK_GL( *this, mGlAbstraction.ColorMask(flag, flag, flag, flag) );
457     }
458   }
459
460   /**
461    * Wrapper for OpenGL ES 2.0 glCompressedTexImage2D()
462    */
463   void CompressedTexImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height,
464                             GLint border, GLsizei imageSize, const void* data)
465   {
466     LOG_GL("CompressedTexImage2D %d %d %x %d %d %d %d %p\n", target, level, internalformat, width, height, border, imageSize, data);
467     CHECK_GL( *this, mGlAbstraction.CompressedTexImage2D(target, level, internalformat, width, height, border, imageSize, data) );
468   }
469
470   /**
471    * Wrapper for OpenGL ES 3.0 glCompressedTexImage3D()
472    */
473   void CompressedTexImage3D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth,
474                             GLint border, GLsizei imageSize, const void* data)
475   {
476     LOG_GL("CompressedTexImage3D %d %d %x %d %d %d %d %d %p\n", target, level, internalformat, width, height, depth, border, imageSize, data);
477     CHECK_GL( *this, mGlAbstraction.CompressedTexImage3D(target, level, internalformat, width, height, depth, border, imageSize, data) );
478   }
479
480   /**
481    * Wrapper for OpenGL ES 2.0 glCompressedTexSubImage2D()
482    */
483   void CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
484                                GLenum format, GLsizei imageSize, const void* data)
485   {
486     LOG_GL("CompressedTexSubImage2D %x %d %d %d %d %d %x %d %p\n", target, level, xoffset, yoffset, width, height, format, imageSize, data);
487     CHECK_GL( *this, mGlAbstraction.CompressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, imageSize, data) );
488   }
489
490   /**
491    * Wrapper for OpenGL ES 3.0 glCompressedTexSubImage3D()
492    */
493   void CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset,
494                                GLsizei width, GLsizei height, GLsizei depth,
495                                GLenum format, GLsizei imageSize, const void* data)
496   {
497     LOG_GL("CompressedTexSubImage3D %x %d %d %d %d %d %d %d %x %d %p\n", target, level, xoffset, yoffset, xoffset, width, height, depth, format, imageSize, data);
498     CHECK_GL( *this, mGlAbstraction.CompressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, data) );
499   }
500
501   /**
502    * Wrapper for OpenGL ES 2.0 glCopyTexImage2D()
503    */
504   void CopyTexImage2D(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
505   {
506     LOG_GL("CopyTexImage2D %x %d %x %d %d %d %d %d\n", target, level, internalformat, x, y, width, height, border);
507     CHECK_GL( *this, mGlAbstraction.CopyTexImage2D(target, level, internalformat, x, y, width, height, border) );
508   }
509
510   /**
511    * Wrapper for OpenGL ES 2.0 glCopyTexSubImage2D()
512    */
513   void CopyTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
514   {
515     LOG_GL("CopyTexSubImage2D %x %d %d %d %d %d %d %d\n", target, level, xoffset, yoffset, x, y, width, height);
516     CHECK_GL( *this, mGlAbstraction.CopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height) );
517   }
518
519   /**
520    * Wrapper for OpenGL ES 3.0 glCopyTexSubImage3D()
521    */
522   void CopyTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height)
523   {
524     LOG_GL("CopyTexSubImage3D %x %d %d %d %d %d %d %d %d\n", target, level, xoffset, yoffset, zoffset, x, y, width, height);
525     CHECK_GL( *this, mGlAbstraction.CopyTexSubImage3D(target, level, xoffset, yoffset, zoffset, x, y, width, height) );
526   }
527
528   /**
529    * Wrapper for OpenGL ES 2.0 glCullFace()
530    * enables GL_CULL_FACE if in any of the face culling modes
531    * otherwise disables GL_CULL_FACE
532    */
533   void CullFace(CullFaceMode mode)
534   {
535     // Avoid unnecessary calls to gl
536     if(mCullFaceMode != mode)
537     {
538       mCullFaceMode = mode;
539       switch(mode)
540       {
541         case CullNone:
542         {
543           LOG_GL("Disable GL_CULL_FACE\n");
544           CHECK_GL( *this, mGlAbstraction.Disable(GL_CULL_FACE) );
545           break;
546         }
547
548         case CullFront:
549         {
550           LOG_GL("Enable GL_CULL_FACE\n");
551           CHECK_GL( *this, mGlAbstraction.Enable(GL_CULL_FACE) );
552           LOG_GL("Enable GL_FRONT\n");
553           CHECK_GL( *this, mGlAbstraction.CullFace(GL_FRONT) );
554           break;
555         }
556
557         case CullBack:
558         {
559           LOG_GL("Enable GL_CULL_FACE\n");
560           CHECK_GL( *this, mGlAbstraction.Enable(GL_CULL_FACE) );
561           LOG_GL("Enable GL_BACK\n");
562           CHECK_GL( *this, mGlAbstraction.CullFace(GL_BACK) );
563           break;
564         }
565
566         case CullFrontAndBack:
567         {
568           LOG_GL("Enable GL_CULL_FACE\n");
569           CHECK_GL( *this, mGlAbstraction.Enable(GL_CULL_FACE) );
570           LOG_GL("Enable GL_FRONT_AND_BACK\n");
571           CHECK_GL( *this, mGlAbstraction.CullFace(GL_FRONT_AND_BACK) );
572           break;
573         }
574
575         default:
576           break;
577       }
578     }
579   }
580
581   /**
582    * Wrapper for OpenGL ES 2.0 glDeleteBuffers()
583    */
584   void DeleteBuffers(GLsizei n, const GLuint* buffers)
585   {
586     // TODO: this is to prevent mesh destructor from doing GL calls when DALi core is being deleted
587     // can be taken out once render manages either knows about meshes or gpubuffers and can tell them directly that context is lost
588     if( this->IsGlContextCreated() )
589     {
590       LOG_GL("DeleteBuffers %d %p\n", n, buffers);
591       CHECK_GL( *this, mGlAbstraction.DeleteBuffers(n, buffers) );
592     }
593     // reset the cached buffer id's
594     // fixes problem where some drivers will a generate a buffer with the
595     // same id, as the last deleted buffer id.
596     mBoundArrayBufferId = 0;
597     mBoundElementArrayBufferId = 0;
598     mBoundTransformFeedbackBufferId = 0;
599   }
600
601   /**
602    * Wrapper for OpenGL ES 2.0 glDeleteFramebuffers()
603    */
604   void DeleteFramebuffers(GLsizei n, const GLuint* framebuffers)
605   {
606     LOG_GL("DeleteFramebuffers %d %p\n", n, framebuffers);
607     CHECK_GL( *this, mGlAbstraction.DeleteFramebuffers(n, framebuffers) );
608   }
609
610   /**
611    * Wrapper for OpenGL ES 3.0 glDeleteQueries()
612    */
613   void DeleteQueries(GLsizei n, GLuint* ids)
614   {
615     LOG_GL("DeleteQueries %d %p\n", n, ids);
616     CHECK_GL( *this, mGlAbstraction.DeleteQueries(n, ids) );
617   }
618
619   /**
620    * Wrapper for OpenGL ES 2.0 glDeleteRenderbuffers()
621    */
622   void DeleteRenderbuffers(GLsizei n, const GLuint* renderbuffers)
623   {
624     LOG_GL("DeleteRenderbuffers %d %p\n", n, renderbuffers);
625     CHECK_GL( *this, mGlAbstraction.DeleteRenderbuffers(n, renderbuffers) );
626   }
627
628   /**
629    * Wrapper for OpenGL ES 2.0 glDeleteTextures()
630    */
631   void DeleteTextures(GLsizei n, const GLuint* textures)
632   {
633     LOG_GL("DeleteTextures %d %p\n", n, textures);
634     CHECK_GL( *this, mGlAbstraction.DeleteTextures(n, textures) );
635
636     // reset the cached texture id's incase the driver re-uses them
637     // when creating new textures
638     for( int i=0; i < mMaxTextureUnits; ++i )
639     {
640        mBound2dTextureId[ i ] = 0;
641     }
642   }
643
644   /**
645    * Wrapper for OpenGL ES 3.0 glDeleteTransformFeedbacks()
646    */
647   void DeleteTransformFeedbacks(GLsizei n, GLuint* ids)
648   {
649     LOG_GL("DeleteTransformFeedbacks %d %p\n", n, ids);
650     CHECK_GL( *this, mGlAbstraction.DeleteTransformFeedbacks(n, ids) );
651   }
652
653   /**
654    * Wrapper for OpenGL ES 2.0 glDepthFunc()
655    */
656   void DepthFunc(GLenum func)
657   {
658     LOG_GL("DepthFunc %x\n", func);
659     CHECK_GL( *this, mGlAbstraction.DepthFunc(func) );
660   }
661
662   /**
663    * Wrapper for OpenGL ES 2.0 glDepthMask()
664    */
665   void DepthMask(GLboolean flag)
666   {
667     // only change state if needed
668     if( flag != mDepthMaskEnabled )
669     {
670       mDepthMaskEnabled = flag;
671       LOG_GL("DepthMask %s\n", flag ? "True" : "False");
672       CHECK_GL( *this, mGlAbstraction.DepthMask( mDepthMaskEnabled ) );
673     }
674   }
675
676   /**
677    * Wrapper for OpenGL ES 2.0 glDepthRangef()
678    */
679   void DepthRangef(GLclampf zNear, GLclampf zFar)
680   {
681     LOG_GL("DepthRangef %f %f\n", zNear, zFar);
682     CHECK_GL( *this, mGlAbstraction.DepthRangef(zNear, zFar) );
683   }
684
685   /**
686    * The wrapper for OpenGL ES 2.0 glDisable() has been replaced by SetBlend, SetCullFace, SetDepthTest,
687    * SetDither, SetPolygonOffsetFill, SetSampleAlphaToCoverage, SetSampleCoverage, SetScissorTest & SetStencilTest.
688    */
689
690   /**
691    * Wrapper for OpenGL ES 2.0 glDrawArrays()
692    */
693   void DrawArrays(GLenum mode, GLint first, GLsizei count)
694   {
695     FlushVertexAttributeLocations();
696
697     LOG_GL("DrawArrays %x %d %d\n", mode, first, count);
698     CHECK_GL( *this, mGlAbstraction.DrawArrays(mode, first, count) );
699   }
700
701   /**
702    * Wrapper for OpenGL ES 3.0 glDrawArraysInstanced()
703    */
704   void DrawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount)
705   {
706     FlushVertexAttributeLocations();
707
708     LOG_GL("DrawArraysInstanced %x %d %d %d\n", mode, first, count, instanceCount);
709     CHECK_GL( *this, mGlAbstraction.DrawArraysInstanced(mode, first, count,instanceCount) );
710   }
711
712   /**
713    * Wrapper for OpenGL ES 3.0 glDrawBuffers()
714    */
715   void DrawBuffers(GLsizei n, const GLenum* bufs)
716   {
717     LOG_GL("DrawBuffers %d %p\n", n, bufs);
718     CHECK_GL( *this, mGlAbstraction.DrawBuffers(n, bufs) );
719   }
720
721   /**
722    * Wrapper for OpenGL ES 2.0 glDrawElements()
723    */
724   void DrawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
725   {
726     FlushVertexAttributeLocations();
727
728     LOG_GL("DrawElements %x %d %d %p\n", mode, count, type, indices);
729     CHECK_GL( *this, mGlAbstraction.DrawElements(mode, count, type, indices) );
730   }
731
732   /**
733    * Wrapper for OpenGL ES 3.0 glDrawElementsInstanced()
734    */
735   void DrawElementsInstanced(GLenum mode, GLsizei count, GLenum type, const void* indices, GLsizei instanceCount)
736   {
737     FlushVertexAttributeLocations();
738
739     LOG_GL("DrawElementsInstanced %x %d %d %p %d\n", mode, count, type, indices, instanceCount);
740     CHECK_GL( *this, mGlAbstraction.DrawElementsInstanced(mode, count, type, indices, instanceCount) );
741   }
742
743   /**
744    * Wrapper for OpenGL ES 3.0 glDrawRangeElements()
745    */
746   void DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void* indices)
747   {
748     FlushVertexAttributeLocations();
749
750     LOG_GL("DrawRangeElements %x %u %u %d %d %p\n", mode, start, end, count, type, indices);
751     CHECK_GL( *this, mGlAbstraction.DrawRangeElements(mode, start, end, count, type, indices) );
752   }
753
754   /**
755    * Wrapper for OpenGL ES 3.0 glGenQuerieS()
756    */
757   void GenQueries(GLsizei n, GLuint* ids)
758   {
759     LOG_GL("GenQueries %d %p\n", n, ids);
760     CHECK_GL( *this, mGlAbstraction.GenQueries(n, ids) );
761   }
762
763   /**
764    * Wrapper for OpenGL ES 3.0 glGenTransformFeedbacks()
765    */
766   void GenTransformFeedbacks(GLsizei n, GLuint* ids)
767   {
768     LOG_GL("GenTransformFeedbacks %d %p\n", n, ids);
769     CHECK_GL( *this, mGlAbstraction.GenTransformFeedbacks(n, ids) );
770   }
771
772   /**
773    * @return the current buffer bound for a given target
774    */
775   GLuint GetCurrentBoundArrayBuffer(GLenum target)
776   {
777     GLuint result(0);
778     switch(target)
779     {
780       case GL_ARRAY_BUFFER:
781       {
782         result = mBoundArrayBufferId;
783         break;
784       }
785       case GL_ELEMENT_ARRAY_BUFFER:
786       {
787         result = mBoundElementArrayBufferId;
788         break;
789       }
790       case GL_TRANSFORM_FEEDBACK_BUFFER:
791       {
792         result = mBoundTransformFeedbackBufferId;
793         break;
794       }
795       default:
796       {
797         DALI_ASSERT_DEBUG(0 && "target buffer type not supported");
798       }
799     }
800     return result;
801   }
802
803   void EnableVertexAttributeArray( GLuint location )
804   {
805     SetVertexAttributeLocation( location, true);
806   }
807
808   void DisableVertexAttributeArray( GLuint location )
809   {
810     SetVertexAttributeLocation( location, false);
811   }
812
813   /**
814    * Wrapper for OpenGL ES 3.0 glVertexAttribDivisor()
815    */
816   void VertexAttribDivisor ( GLuint index, GLuint divisor )
817   {
818     LOG_GL("VertexAttribPointer(%d, %d)\n", index, divisor );
819     CHECK_GL( *this, mGlAbstraction.VertexAttribDivisor( index, divisor ) );
820   }
821
822   /**
823    * Wrapper for OpenGL ES 2.0 glVertexAttribPointer()
824    */
825   void VertexAttribPointer( GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void* ptr )
826   {
827     LOG_GL("VertexAttribPointer(%d, %d, %d, %d, %d, %x)\n", index, size, type, normalized, stride, ptr );
828     CHECK_GL( *this, mGlAbstraction.VertexAttribPointer( index, size, type, normalized, stride, ptr ) );
829   }
830
831   /**
832    * Wrapper for OpenGL ES 3.0 glInvalidateFramebuffer()
833    */
834   void InvalidateFramebuffer(GLenum target, GLsizei numAttachments, const GLenum *attachments)
835   {
836     LOG_GL("InvalidateFramebuffer\n");
837     CHECK_GL( *this, mGlAbstraction.InvalidateFramebuffer(target, numAttachments, attachments) );
838   }
839
840   /**
841    * The wrapper for OpenGL ES 2.0 glEnable() has been replaced by SetBlend, SetCullFace, SetDepthTest,
842    * SetDither, SetPolygonOffsetFill, SetSampleAlphaToCoverage, SetSampleCoverage, SetScissorTest & SetStencilTest.
843    */
844
845   /**
846    * This method replaces glEnable(GL_BLEND) and glDisable(GL_BLEND).
847    * @param[in] enable True if GL_BLEND should be enabled.
848    */
849   void SetBlend(bool enable)
850   {
851     // Avoid unecessary calls to glEnable/glDisable
852     if (enable != mBlendEnabled)
853     {
854       mBlendEnabled = enable;
855
856       if (enable)
857       {
858         LOG_GL("Enable GL_BLEND\n");
859         CHECK_GL( *this, mGlAbstraction.Enable(GL_BLEND) );
860       }
861       else
862       {
863         LOG_GL("Disable GL_BLEND\n");
864         CHECK_GL( *this, mGlAbstraction.Disable(GL_BLEND) );
865       }
866     }
867   }
868
869   /**
870    * This method replaces glEnable(GL_DEPTH_TEST) and glDisable(GL_DEPTH_TEST).
871    * @param[in] enable True if GL_DEPTH_TEST should be enabled.
872    */
873   void SetDepthTest(bool enable)
874   {
875     // Avoid unecessary calls to glEnable/glDisable
876     if (enable != mDepthTestEnabled)
877     {
878       mDepthTestEnabled = enable;
879
880       if (enable)
881       {
882         LOG_GL("Enable GL_DEPTH_TEST\n");
883         CHECK_GL( *this, mGlAbstraction.Enable(GL_DEPTH_TEST) );
884       }
885       else
886       {
887         LOG_GL("Disable GL_DEPTH_TEST\n");
888         CHECK_GL( *this, mGlAbstraction.Disable(GL_DEPTH_TEST) );
889       }
890     }
891   }
892
893   /**
894    * This method replaces glEnable(GL_DITHER) and glDisable(GL_DITHER).
895    * @param[in] enable True if GL_DITHER should be enabled.
896    */
897   void SetDither(bool enable)
898   {
899     // Avoid unecessary calls to glEnable/glDisable
900     if (enable != mDitherEnabled)
901     {
902       mDitherEnabled = enable;
903
904       if (enable)
905       {
906         LOG_GL("Enable GL_DITHER\n");
907         CHECK_GL( *this, mGlAbstraction.Enable(GL_DITHER) );
908       }
909       else
910       {
911         LOG_GL("Disable GL_DITHER\n");
912         CHECK_GL( *this, mGlAbstraction.Disable(GL_DITHER) );
913       }
914     }
915   }
916
917   /**
918    * This method replaces glEnable(GL_POLYGON_OFFSET_FILL) and glDisable(GL_POLYGON_OFFSET_FILL).
919    * @param[in] enable True if GL_POLYGON_OFFSET_FILL should be enabled.
920    */
921   void SetPolygonOffsetFill(bool enable)
922   {
923     // Avoid unecessary calls to glEnable/glDisable
924     if (enable != mPolygonOffsetFillEnabled)
925     {
926       mPolygonOffsetFillEnabled = enable;
927
928       if (enable)
929       {
930         LOG_GL("Enable GL_POLYGON_OFFSET_FILL\n");
931         CHECK_GL( *this, mGlAbstraction.Enable(GL_POLYGON_OFFSET_FILL) );
932       }
933       else
934       {
935         LOG_GL("Disable GL_POLYGON_OFFSET_FILL\n");
936         CHECK_GL( *this, mGlAbstraction.Disable(GL_POLYGON_OFFSET_FILL) );
937       }
938     }
939   }
940
941   /**
942    * This method replaces glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE) and glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE).
943    * @param[in] enable True if GL_SAMPLE_ALPHA_TO_COVERAGE should be enabled.
944    */
945   void SetSampleAlphaToCoverage(bool enable)
946   {
947     // Avoid unecessary calls to glEnable/glDisable
948     if (enable != mSampleAlphaToCoverageEnabled)
949     {
950       mSampleAlphaToCoverageEnabled = enable;
951
952       if (enable)
953       {
954         LOG_GL("Enable GL_SAMPLE_ALPHA_TO_COVERAGE\n");
955         CHECK_GL( *this, mGlAbstraction.Enable(GL_SAMPLE_ALPHA_TO_COVERAGE) );
956       }
957       else
958       {
959         LOG_GL("Disable GL_SAMPLE_ALPHA_TO_COVERAGE\n");
960         CHECK_GL( *this, mGlAbstraction.Disable(GL_SAMPLE_ALPHA_TO_COVERAGE) );
961       }
962     }
963   }
964
965   /**
966    * This method replaces glEnable(GL_SAMPLE_COVERAGE) and glDisable(GL_SAMPLE_COVERAGE).
967    * @param[in] enable True if GL_SAMPLE_COVERAGE should be enabled.
968    */
969   void SetSampleCoverage(bool enable)
970   {
971     // Avoid unecessary calls to glEnable/glDisable
972     if (enable != mSampleCoverageEnabled)
973     {
974       mSampleCoverageEnabled = enable;
975
976       if (enable)
977       {
978         LOG_GL("Enable GL_SAMPLE_COVERAGE\n");
979         CHECK_GL( *this, mGlAbstraction.Enable(GL_SAMPLE_COVERAGE) );
980       }
981       else
982       {
983         LOG_GL("Disable GL_SAMPLE_COVERAGE\n");
984         CHECK_GL( *this, mGlAbstraction.Disable(GL_SAMPLE_COVERAGE) );
985       }
986     }
987   }
988
989   /**
990    * This method replaces glEnable(GL_SCISSOR_TEST) and glDisable(GL_SCISSOR_TEST).
991    * @param[in] enable True if GL_SCISSOR_TEST should be enabled.
992    */
993   void SetScissorTest(bool enable)
994   {
995     // Avoid unecessary calls to glEnable/glDisable
996     if (enable != mScissorTestEnabled)
997     {
998       mScissorTestEnabled = enable;
999
1000       if (enable)
1001       {
1002         LOG_GL("Enable GL_SCISSOR_TEST\n");
1003         CHECK_GL( *this, mGlAbstraction.Enable(GL_SCISSOR_TEST) );
1004       }
1005       else
1006       {
1007         LOG_GL("Disable GL_SCISSOR_TEST\n");
1008         CHECK_GL( *this, mGlAbstraction.Disable(GL_SCISSOR_TEST) );
1009       }
1010     }
1011   }
1012
1013   /**
1014    * This method replaces glEnable(GL_STENCIL_TEST) and glDisable(GL_STENCIL_TEST).
1015    * @param[in] enable True if GL_STENCIL_TEST should be enabled.
1016    */
1017   void SetStencilTest(bool enable)
1018   {
1019     // Avoid unecessary calls to glEnable/glDisable
1020     if (enable != mStencilTestEnabled)
1021     {
1022       mStencilTestEnabled = enable;
1023
1024       if (enable)
1025       {
1026         LOG_GL("Enable GL_STENCIL_TEST\n");
1027         CHECK_GL( *this, mGlAbstraction.Enable(GL_STENCIL_TEST) );
1028       }
1029       else
1030       {
1031         LOG_GL("Disable GL_STENCIL_TEST\n");
1032         CHECK_GL( *this, mGlAbstraction.Disable(GL_STENCIL_TEST) );
1033       }
1034     }
1035   }
1036
1037   /**
1038    * Wrapper for OpenGL ES 3.0 glEndQuery()
1039    */
1040   void EndQuery(GLenum target)
1041   {
1042     LOG_GL("EndQuery %d\n", target);
1043     CHECK_GL( *this, mGlAbstraction.EndQuery(target) );
1044   }
1045
1046   /**
1047    * Wrapper for OpenGL ES 3.0 glEndTransformFeedback()
1048    */
1049   void EndTransformFeedback()
1050   {
1051     LOG_GL("EndTransformFeedback\n");
1052     CHECK_GL( *this, mGlAbstraction.EndTransformFeedback() );
1053   }
1054
1055   /**
1056    * Wrapper for OpenGL ES 2.0 glFinish()
1057    */
1058   void Finish(void)
1059   {
1060     LOG_GL("Finish\n");
1061     CHECK_GL( *this, mGlAbstraction.Finish() );
1062   }
1063
1064   /**
1065    * Wrapper for OpenGL ES 2.0 glFlush()
1066    */
1067   void Flush(void)
1068   {
1069     LOG_GL("Flush\n");
1070     CHECK_GL( *this, mGlAbstraction.Flush() );
1071   }
1072
1073   /**
1074    * Wrapper for OpenGL ES 2.0 glFramebufferRenderbuffer()
1075    */
1076   void FramebufferRenderbuffer(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)
1077   {
1078     LOG_GL("FramebufferRenderbuffer %x %x %x %d\n", target, attachment, renderbuffertarget, renderbuffer);
1079     CHECK_GL( *this, mGlAbstraction.FramebufferRenderbuffer(target, attachment, renderbuffertarget, renderbuffer) );
1080   }
1081
1082   /**
1083    * Wrapper for OpenGL ES 2.0 glFramebufferTexture2D()
1084    */
1085   void FramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
1086   {
1087     LOG_GL("FramebufferTexture2D %x %x %x %d %d\n", target, attachment, textarget, texture, level);
1088     CHECK_GL( *this, mGlAbstraction.FramebufferTexture2D(target, attachment, textarget, texture, level) );
1089   }
1090
1091   /**
1092    * Wrapper for OpenGL ES 3.0 glFramebufferTextureLayer()
1093    */
1094   void FramebufferTextureLayer(GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer)
1095   {
1096     LOG_GL("FramebufferTextureLayer %x %x %d %d %d\n", target, attachment, texture, level, layer);
1097     CHECK_GL( *this, mGlAbstraction.FramebufferTextureLayer(target, attachment, texture, level, layer) );
1098   }
1099
1100   /**
1101    * Wrapper for OpenGL ES 2.0 glFrontFace()
1102    */
1103   void FrontFace(GLenum mode)
1104   {
1105     LOG_GL("FrontFace %x\n", mode);
1106     CHECK_GL( *this, mGlAbstraction.FrontFace(mode) );
1107   }
1108
1109   /**
1110    * Wrapper for OpenGL ES 2.0 glGenBuffers()
1111    */
1112   void GenBuffers(GLsizei n, GLuint* buffers)
1113   {
1114     LOG_GL("GenBuffers %d\n", n, buffers);
1115     CHECK_GL( *this, mGlAbstraction.GenBuffers(n, buffers) );
1116   }
1117
1118   /**
1119    * Wrapper for OpenGL ES 2.0 glGenerateMipmap()
1120    */
1121   void GenerateMipmap(GLenum target)
1122   {
1123     LOG_GL("GenerateMipmap %x\n", target);
1124     CHECK_GL( *this, mGlAbstraction.GenerateMipmap(target) );
1125   }
1126
1127   /**
1128    * Wrapper for OpenGL ES 2.0 glGenFramebuffers()
1129    */
1130   void GenFramebuffers(GLsizei n, GLuint* framebuffers)
1131   {
1132     LOG_GL("GenFramebuffers %d %p\n", n, framebuffers);
1133     CHECK_GL( *this, mGlAbstraction.GenFramebuffers(n, framebuffers) );
1134   }
1135
1136   /**
1137    * Wrapper for OpenGL ES 2.0 glGenRenderbuffers()
1138    */
1139   void GenRenderbuffers(GLsizei n, GLuint* renderbuffers)
1140   {
1141     LOG_GL("GenRenderbuffers %d %p\n", n, renderbuffers);
1142     CHECK_GL( *this, mGlAbstraction.GenRenderbuffers(n, renderbuffers) );
1143   }
1144
1145   /**
1146    * Wrapper for OpenGL ES 2.0 glGenTextures()
1147    */
1148   void GenTextures(GLsizei n, GLuint* textures)
1149   {
1150     LOG_GL("GenTextures %d %p\n", n, textures);
1151     CHECK_GL( *this, mGlAbstraction.GenTextures(n, textures) );
1152   }
1153
1154   /**
1155    * Wrapper for OpenGL ES 2.0 glGetBooleanv()
1156    */
1157   void GetBooleanv(GLenum pname, GLboolean* params)
1158   {
1159     LOG_GL("GetBooleanv %x\n", pname);
1160     CHECK_GL( *this, mGlAbstraction.GetBooleanv(pname, params) );
1161   }
1162
1163   /**
1164    * Wrapper for OpenGL ES 2.0 glGetBufferParameteriv()
1165    */
1166   void GetBufferParameteriv(GLenum target, GLenum pname, GLint* params)
1167   {
1168     LOG_GL("GetBufferParameteriv %x %x %p\n", target, pname, params);
1169     CHECK_GL( *this, mGlAbstraction.GetBufferParameteriv(target, pname, params) );
1170   }
1171
1172   /**
1173    * Wrapper for OpenGL ES 3.0 glGetBufferPointer()
1174    */
1175   void GetBufferPointerv(GLenum target, GLenum pname, GLvoid** params)
1176   {
1177     LOG_GL("GetBufferPointerv %x %x %p\n", target, pname, params);
1178     CHECK_GL( *this, mGlAbstraction.GetBufferPointerv(target, pname, params) );
1179   }
1180
1181   /**
1182    * Wrapper for OpenGL ES 2.0 glGetError()
1183    */
1184   GLenum GetError(void)
1185   {
1186     // Not worth logging here
1187     return mGlAbstraction.GetError();
1188   }
1189
1190   /**
1191    * Wrapper for OpenGL ES 2.0 glGetFloatv()
1192    */
1193   void GetFloatv(GLenum pname, GLfloat* params)
1194   {
1195     LOG_GL("GetFloatv %x\n", pname);
1196     CHECK_GL( *this, mGlAbstraction.GetFloatv(pname, params) );
1197   }
1198
1199   /**
1200    * Wrapper for OpenGL ES 2.0 glGetFramebufferAttachmentParameteriv()
1201    */
1202   void GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment, GLenum pname, GLint* params)
1203   {
1204     LOG_GL("GetFramebufferAttachmentParameteriv %x %x %x\n", target, attachment, pname);
1205     CHECK_GL( *this, mGlAbstraction.GetFramebufferAttachmentParameteriv(target, attachment, pname, params) );
1206   }
1207
1208   /**
1209    * Wrapper for OpenGL ES 2.0 glGetIntegerv()
1210    */
1211   void GetIntegerv(GLenum pname, GLint* params)
1212   {
1213     LOG_GL("GetIntegerv %x\n", pname);
1214     CHECK_GL( *this, mGlAbstraction.GetIntegerv(pname, params) );
1215   }
1216
1217   /**
1218    * Wrapper for OpenGL ES 3.0 glGetQueryiv()
1219    */
1220   void GetQueryiv(GLenum target, GLenum pname, GLint* params)
1221   {
1222     LOG_GL("GetQueryiv %x %x\n", target, pname);
1223     CHECK_GL( *this, mGlAbstraction.GetQueryiv(target, pname, params) );
1224   }
1225
1226   /**
1227    * Wrapper for OpenGL ES 3.0 glGetQueryObjectuiv()
1228    */
1229   void GetQueryObjectuiv(GLuint id, GLenum pname, GLuint* params)
1230   {
1231     LOG_GL("GetQueryObjectuiv %u %x %p\n", id, pname, params);
1232     CHECK_GL( *this, mGlAbstraction.GetQueryObjectuiv(id, pname, params) );
1233   }
1234
1235   /**
1236    * Wrapper for OpenGL ES 2.0 glGetRenderbufferParameteriv()
1237    */
1238   void GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint* params)
1239   {
1240     LOG_GL("GetRenderbufferParameteriv %x %x\n", target, pname);
1241     CHECK_GL( *this, mGlAbstraction.GetRenderbufferParameteriv(target, pname, params) );
1242   }
1243
1244   /**
1245    * Wrapper for OpenGL ES 2.0 glGetString()
1246    */
1247   const GLubyte* GetString(GLenum name)
1248   {
1249     LOG_GL("GetString %x\n", name);
1250     const GLubyte* str = CHECK_GL( *this, mGlAbstraction.GetString(name) );
1251     return str;
1252   }
1253
1254   /**
1255    * Wrapper for OpenGL ES 2.0 glGetTexParameterfv()
1256    */
1257   void GetTexParameterfv(GLenum target, GLenum pname, GLfloat* params)
1258   {
1259     LOG_GL("GetTexParameterfv %x %x\n", target, pname);
1260     CHECK_GL( *this, mGlAbstraction.GetTexParameterfv(target, pname, params) );
1261   }
1262
1263   /**
1264    * Wrapper for OpenGL ES 2.0 glGetTexParameteriv()
1265    */
1266   void GetTexParameteriv(GLenum target, GLenum pname, GLint* params)
1267   {
1268     LOG_GL("GetTexParameteriv %x %x\n", target, pname);
1269     CHECK_GL( *this, mGlAbstraction.GetTexParameteriv(target, pname, params) );
1270   }
1271
1272   /**
1273    * Wrapper for OpenGL ES 2.0 glHint()
1274    */
1275   void Hint(GLenum target, GLenum mode)
1276   {
1277     LOG_GL("Hint %x %x\n", target, mode);
1278     CHECK_GL( *this, mGlAbstraction.Hint(target, mode) );
1279   }
1280
1281   /**
1282    * Wrapper for OpenGL ES 2.0 glIsBuffer()
1283    */
1284   GLboolean IsBuffer(GLuint buffer)
1285   {
1286     LOG_GL("IsBuffer %d\n", buffer);
1287     GLboolean val = CHECK_GL( *this, mGlAbstraction.IsBuffer(buffer) );
1288     return val;
1289   }
1290
1291   /**
1292    * Wrapper for OpenGL ES 2.0 glIsEnabled()
1293    */
1294   GLboolean IsEnabled(GLenum cap)
1295   {
1296     LOG_GL("IsEnabled %x\n", cap);
1297     GLboolean val = CHECK_GL( *this, mGlAbstraction.IsEnabled(cap) );
1298     return val;
1299   }
1300
1301   /**
1302    * Wrapper for OpenGL ES 2.0 glIsFramebuffer()
1303    */
1304   GLboolean IsFramebuffer(GLuint framebuffer)
1305   {
1306     LOG_GL("IsFramebuffer %d\n", framebuffer);
1307     GLboolean val = CHECK_GL( *this, mGlAbstraction.IsFramebuffer(framebuffer) );
1308     return val;
1309   }
1310
1311   /**
1312    * Wrapper for OpenGL ES 3.0 glIsQuery()
1313    */
1314   GLboolean IsQuery(GLuint id)
1315   {
1316     LOG_GL("IsQuery %u\n", id);
1317     GLboolean val = CHECK_GL( *this, mGlAbstraction.IsQuery(id) );
1318     return val;
1319   }
1320
1321   /**
1322    * Wrapper for OpenGL ES 2.0 glIsRenderbuffer()
1323    */
1324   GLboolean IsRenderbuffer(GLuint renderbuffer)
1325   {
1326     LOG_GL("IsRenderbuffer %d\n", renderbuffer);
1327     GLboolean val = CHECK_GL( *this, mGlAbstraction.IsRenderbuffer(renderbuffer) );
1328     return val;
1329   }
1330
1331   /**
1332    * Wrapper for OpenGL ES 2.0 glIsTexture()
1333    */
1334   GLboolean IsTexture(GLuint texture)
1335   {
1336     LOG_GL("IsTexture %d\n", texture);
1337     GLboolean val = CHECK_GL( *this, mGlAbstraction.IsTexture(texture) );
1338     return val;
1339   }
1340
1341   /**
1342    * Wrapper for OpenGL ES 3.0 glIsTransformFeedback()
1343    */
1344   GLboolean IsTransformFeedback(GLuint id)
1345   {
1346     LOG_GL("IsTransformFeedback %u\n", id);
1347     GLboolean val = CHECK_GL( *this, mGlAbstraction.IsTransformFeedback(id) );
1348     return val;
1349   }
1350
1351   /**
1352    * Wrapper for OpenGL ES 2.0 glLineWidth()
1353    */
1354   void LineWidth(GLfloat width)
1355   {
1356     LOG_GL("LineWidth %f\n", width);
1357     CHECK_GL( *this, mGlAbstraction.LineWidth(width) );
1358   }
1359
1360   /**
1361    * Wrapper for OpenGL ES 3.0 glPauseTransformFeedback()
1362    */
1363   void PauseTransformFeedback()
1364   {
1365     LOG_GL("PauseTransformFeedback\n");
1366     CHECK_GL( *this, mGlAbstraction.PauseTransformFeedback() );
1367   }
1368
1369   /**
1370    * Wrapper for OpenGL ES 2.0 glPixelStorei()
1371    */
1372   void PixelStorei(GLenum pname, GLint param)
1373   {
1374     LOG_GL("PixelStorei %x %d\n", pname, param);
1375     CHECK_GL( *this, mGlAbstraction.PixelStorei(pname, param) );
1376   }
1377
1378   /**
1379    * Wrapper for OpenGL ES 2.0 glPolygonOffset()
1380    */
1381   void PolygonOffset(GLfloat factor, GLfloat units)
1382   {
1383     LOG_GL("PolygonOffset %f %f\n", factor, units);
1384     CHECK_GL( *this, mGlAbstraction.PolygonOffset(factor, units) );
1385   }
1386
1387   /**
1388    * Wrapper for OpenGL ES 2.0 glReadPixels()
1389    */
1390   void ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
1391   {
1392     LOG_GL("ReadPixels %d %d %d %d %x %x\n", x, y, width, height, format, type);
1393     CHECK_GL( *this, mGlAbstraction.ReadPixels(x, y, width, height, format, type, pixels) );
1394   }
1395
1396   /**
1397    * Wrapper for OpenGL ES 2.0 glRenderbufferStorage()
1398    */
1399   void RenderbufferStorage(GLenum target, GLenum internalformat, GLsizei width, GLsizei height)
1400   {
1401     LOG_GL("RenderbufferStorage %x %x %d %d\n", target, internalformat, width, height);
1402     CHECK_GL( *this, mGlAbstraction.RenderbufferStorage(target, internalformat, width, height) );
1403   }
1404
1405   /**
1406    * Wrapper for OpenGL ES 3.0 glRenderbufferStorageMultisample()
1407    */
1408   void RenderbufferStorageMultisample(GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height)
1409   {
1410     LOG_GL("RenderbufferStorageMultisample %x %u %x %d %d\n", target, samples, internalformat, width, height);
1411     CHECK_GL( *this, mGlAbstraction.RenderbufferStorageMultisample(target, samples, internalformat, width, height) );
1412   }
1413
1414   /**
1415    * Wrapper for OpenGL ES 3.0 glResumeTransformFeedback()
1416    */
1417   void ResumeTransformFeedback()
1418   {
1419     LOG_GL("ResumeTransformFeedback\n");
1420     CHECK_GL( *this, mGlAbstraction.ResumeTransformFeedback() );
1421   }
1422
1423   /**
1424    * Wrapper for OpenGL ES 2.0 glSampleCoverage()
1425    */
1426   void SampleCoverage(GLclampf value, GLboolean invert)
1427   {
1428     LOG_GL("SampleCoverage %f %s\n", value, invert ? "True" : "False");
1429     CHECK_GL( *this, mGlAbstraction.SampleCoverage(value, invert) );
1430   }
1431
1432   /**
1433    * Wrapper for OpenGL ES 2.0 glScissor()
1434    */
1435   void Scissor(GLint x, GLint y, GLsizei width, GLsizei height)
1436   {
1437     LOG_GL("Scissor %d %d %d %d\n", x, y, width, height);
1438     CHECK_GL( *this, mGlAbstraction.Scissor(x, y, width, height) );
1439   }
1440
1441   /**
1442    * Wrapper for OpenGL ES 2.0 glStencilFunc()
1443    */
1444   void StencilFunc(GLenum func, GLint ref, GLuint mask)
1445   {
1446     LOG_GL("StencilFunc %x %d %d\n", func, ref, mask);
1447     CHECK_GL( *this, mGlAbstraction.StencilFunc(func, ref, mask) );
1448   }
1449
1450   /**
1451    * Wrapper for OpenGL ES 2.0 glStencilFuncSeparate()
1452    */
1453   void StencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask)
1454   {
1455     LOG_GL("StencilFuncSeparate %x %x %d %d\n", face, func, ref, mask);
1456     CHECK_GL( *this, mGlAbstraction.StencilFuncSeparate(face, func, ref, mask) );
1457   }
1458
1459   /**
1460    * Wrapper for OpenGL ES 2.0 glStencilMask()
1461    */
1462   void StencilMask(GLuint mask)
1463   {
1464     if( mask != mStencilMask )
1465     {
1466       mStencilMask = mask;
1467
1468       LOG_GL("StencilMask %d\n", mask);
1469       CHECK_GL( *this, mGlAbstraction.StencilMask(mask) );
1470     }
1471   }
1472
1473   /**
1474    * Wrapper for OpenGL ES 2.0 glStencilMaskSeparate()
1475    */
1476   void StencilMaskSeparate(GLenum face, GLuint mask)
1477   {
1478     LOG_GL("StencilMaskSeparate %x %d\n", face, mask);
1479     CHECK_GL( *this, mGlAbstraction.StencilMaskSeparate(face, mask) );
1480   }
1481
1482   /**
1483    * Wrapper for OpenGL ES 2.0 glStencilOp()
1484    */
1485   void StencilOp(GLenum fail, GLenum zfail, GLenum zpass)
1486   {
1487     LOG_GL("StencilOp %x %x %x\n", fail, zfail, zpass);
1488     CHECK_GL( *this, mGlAbstraction.StencilOp(fail, zfail, zpass) );
1489   }
1490
1491   /**
1492    * Wrapper for OpenGL ES 2.0 glStencilOpSeparate()
1493    */
1494   void StencilOpSeparate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
1495   {
1496     LOG_GL("StencilOpSeparate %x %x %x %x\n", face, fail, zfail, zpass);
1497     CHECK_GL( *this, mGlAbstraction.StencilOpSeparate(face, fail, zfail, zpass) );
1498   }
1499
1500   /**
1501    * Wrapper for OpenGL ES 2.0 glTexImage2D()
1502    */
1503   void TexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height,
1504                   GLint border, GLenum format, GLenum type, const void* pixels)
1505   {
1506     LOG_GL("TexImage2D %x %d %d %dx%d %d %x %x %p\n", target, level, internalformat, width, height, border, format, type, pixels);
1507     CHECK_GL( *this, mGlAbstraction.TexImage2D(target, level, internalformat, width, height, border, format, type, pixels) );
1508   }
1509
1510   /**
1511    * Wrapper for OpenGL ES 3.0 glTexImage3D()
1512    */
1513   void TexImage3D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth,
1514                   GLint border, GLenum format, GLenum type, const void* pixels)
1515   {
1516     LOG_GL("TexImage3D %x %d %d %dx%dx%d %d %x %x %p\n", target, level, internalformat, width, height, depth, border, format, type, pixels);
1517     CHECK_GL( *this, mGlAbstraction.TexImage3D(target, level, internalformat, width, height, depth, border, format, type, pixels) );
1518   }
1519
1520   /**
1521    * Wrapper for OpenGL ES 2.0 glTexParameterf()
1522    */
1523   void TexParameterf(GLenum target, GLenum pname, GLfloat param)
1524   {
1525     LOG_GL("TexParameterf %x %x %f\n", target, pname, param);
1526     CHECK_GL( *this, mGlAbstraction.TexParameterf(target, pname, param) );
1527   }
1528
1529   /**
1530    * Wrapper for OpenGL ES 2.0 glTexParameterfv()
1531    */
1532   void TexParameterfv(GLenum target, GLenum pname, const GLfloat* params)
1533   {
1534     LOG_GL("TexParameterfv %x %x\n", target, pname);
1535     CHECK_GL( *this, mGlAbstraction.TexParameterfv(target, pname, params) );
1536   }
1537
1538   /**
1539    * Wrapper for OpenGL ES 2.0 glTexParameteri()
1540    */
1541   void TexParameteri(GLenum target, GLenum pname, GLint param)
1542   {
1543     LOG_GL("TexParameteri %x %x %d\n", target, pname, param);
1544     CHECK_GL( *this, mGlAbstraction.TexParameteri(target, pname, param) );
1545   }
1546
1547   /**
1548    * Wrapper for OpenGL ES 2.0 glTexParameteriv()
1549    */
1550   void TexParameteriv(GLenum target, GLenum pname, const GLint* params)
1551   {
1552     LOG_GL("TexParameteriv %x %x\n", target, pname);
1553     CHECK_GL( *this, mGlAbstraction.TexParameteriv(target, pname, params) );
1554   }
1555
1556   /**
1557    * Wrapper for OpenGL ES 2.0 glTexSubImage2D()
1558    */
1559   void TexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
1560                      GLenum format, GLenum type, const void* pixels)
1561   {
1562     LOG_GL("TexSubImage2D %x %d %d %d %d %d %x %x %p\n", target, level, xoffset, yoffset, width, height, format, type, pixels);
1563     CHECK_GL( *this, mGlAbstraction.TexSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels) );
1564   }
1565
1566   /**
1567    * Wrapper for OpenGL ES 3.0 glTexSubImage3D()
1568    */
1569   void TexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset,
1570                      GLsizei width, GLsizei height, GLsizei depth,
1571                      GLenum format, GLenum type, const void* pixels)
1572   {
1573     LOG_GL("TexSubImage3D %x %d %d %d %d %d %d %d %x %x %p\n", target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels);
1574     CHECK_GL( *this, mGlAbstraction.TexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels) );
1575   }
1576
1577   /**
1578    * Wrapper for OpenGL ES 3.0 glUnmapBubffer()
1579    */
1580   GLboolean UnmapBuffer(GLenum target)
1581   {
1582     LOG_GL("UnmapBuffer %x \n", target);
1583     GLboolean val = CHECK_GL( *this, mGlAbstraction.UnmapBuffer(target) );
1584     return val;
1585   }
1586   /**
1587    * Wrapper for OpenGL ES 2.0 glViewport()
1588    */
1589   void Viewport(GLint x, GLint y, GLsizei width, GLsizei height)
1590   {
1591     // check if its same as already set
1592     Rect<int> newViewport( x, y, width, height );
1593     if( mViewPort != newViewport )
1594     {
1595       // set new one
1596       LOG_GL("Viewport %d %d %d %d\n", x, y, width, height);
1597       CHECK_GL( *this, mGlAbstraction.Viewport(x, y, width, height) );
1598       mViewPort = newViewport; // remember new one
1599     }
1600   }
1601
1602   /**
1603    * Get the implementation defined MAX_TEXTURE_SIZE. This values is cached when the context is created
1604    * @return The implementation defined MAX_TEXTURE_SIZE
1605    */
1606   GLint CachedMaxTextureSize() const
1607   {
1608     return mMaxTextureSize;
1609   }
1610
1611   /**
1612    * Get the number of supported program binary formats
1613    * @return The number of supported program binary formats
1614    */
1615   GLint CachedNumberOfProgramBinaryFormats() const
1616   {
1617     return mProgramBinaryFormats.size();
1618   }
1619
1620   /**
1621    * Get a supported program binary format
1622    * @param[in] formatIndex The index of the format to return (default: 0)
1623    * @return A supported program binary format
1624    */
1625   GLint CachedProgramBinaryFormat( const unsigned int formatIndex = 0 ) const
1626   {
1627     DALI_ASSERT_ALWAYS( formatIndex < mProgramBinaryFormats.size() && "formatIndex out of bounds");
1628
1629     return mProgramBinaryFormats[ formatIndex ];
1630   }
1631
1632   /**
1633    * @return current program
1634    */
1635   const Program* GetCurrentProgram() const
1636   {
1637     return mCurrentProgram;
1638   }
1639
1640   /**
1641    * Set current program
1642    * @param [in] program that is in use
1643    */
1644   void SetCurrentProgram( Program* program )
1645   {
1646     mCurrentProgram = program;
1647   }
1648
1649   /**
1650    * Get a cached program
1651    * @param [in] hash value
1652    * @return pointer to the program
1653    */
1654   Program* GetCachedProgram( std::size_t hash ) const;
1655
1656   /**
1657    * Cache a program
1658    * @param [in] hash value
1659    * @param [in] pointer to the program
1660    */
1661   void CacheProgram( std::size_t hash, Program* pointer );
1662
1663   /**
1664    * Get the current viewport.
1665    * @return Viewport rectangle.
1666    */
1667   const Rect< int >& GetViewport();
1668
1669 #ifdef DEBUG_ENABLED
1670
1671   /// Switch debug level to Concise to disable, General to enable. Note, enabling snapshot logging will do this on the fly.
1672   static Debug::Filter *gGlLogFilter;
1673
1674 #endif // DEBUG_ENABLED
1675
1676 private: // Implementation
1677
1678   /**
1679    * Flushes vertex attribute location changes to the driver
1680    */
1681   void FlushVertexAttributeLocations();
1682
1683   /**
1684    * Reset the cached internal vertex attribute state
1685    */
1686   void ResetVertexAttributeState();
1687
1688   /**
1689    * Either enables or disables a vertex attribute location in the cache
1690    * The cahnges won't take affect until FlushVertexAttributeLocations is called
1691    * @param location attribute location
1692    * @param state attribute state
1693    */
1694   void SetVertexAttributeLocation(unsigned int location, bool state);
1695
1696   /**
1697    * Sets the initial GL state.
1698    */
1699   void ResetGlState();
1700
1701 private: // Data
1702
1703   Integration::GlAbstraction& mGlAbstraction;
1704
1705   bool mGlContextCreated; ///< True if the OpenGL context has been created
1706
1707   // glEnable/glDisable states
1708   bool mColorMask;
1709   GLuint mStencilMask;
1710   bool mBlendEnabled;
1711   bool mDepthTestEnabled;
1712   bool mDepthMaskEnabled;
1713   bool mDitherEnabled;
1714   bool mPolygonOffsetFillEnabled;
1715   bool mSampleAlphaToCoverageEnabled;
1716   bool mSampleCoverageEnabled;
1717   bool mScissorTestEnabled;
1718   bool mStencilTestEnabled;
1719   bool mClearColorSet;
1720
1721   // glBindBuffer() state
1722   GLuint mBoundArrayBufferId;        ///< The ID passed to glBindBuffer(GL_ARRAY_BUFFER)
1723   GLuint mBoundElementArrayBufferId; ///< The ID passed to glBindBuffer(GL_ELEMENT_ARRAY_BUFFER)
1724   GLuint mBoundTransformFeedbackBufferId; ///< The ID passed to glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER)
1725
1726   // glBindTexture() state
1727   unsigned int mActiveTextureUnit;
1728   std::vector<GLuint> mBound2dTextureId;  ///< The ID passed to glBindTexture(GL_TEXTURE_2D)
1729
1730   // glBlendColor() state
1731   bool mUsingDefaultBlendColor;
1732
1733   // glBlendFuncSeparate() state
1734   GLenum mBlendFuncSeparateSrcRGB;   ///< The srcRGB parameter passed to glBlendFuncSeparate()
1735   GLenum mBlendFuncSeparateDstRGB;   ///< The dstRGB parameter passed to glBlendFuncSeparate()
1736   GLenum mBlendFuncSeparateSrcAlpha; ///< The srcAlpha parameter passed to glBlendFuncSeparate()
1737   GLenum mBlendFuncSeparateDstAlpha; ///< The dstAlpha parameter passed to glBlendFuncSeparate()
1738
1739   // glBlendEquationSeparate state
1740   GLenum mBlendEquationSeparateModeRGB;    ///< Controls RGB blend mode
1741   GLenum mBlendEquationSeparateModeAlpha;  ///< Controls Alpha blend mode
1742
1743   GLint mMaxTextureSize;      ///< return value from GetIntegerv(GL_MAX_TEXTURE_SIZE)
1744   GLint mMaxTextureUnits;     ///< return value from GetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS)
1745   Vector4 mClearColor;        ///< clear color
1746
1747   std::vector<GLint>  mProgramBinaryFormats; ///< array of supported program binary formats
1748
1749   // Face culling mode
1750   CullFaceMode mCullFaceMode;
1751
1752   // cached viewport size
1753   Rect< int > mViewPort;
1754
1755   // Vertex Attribute Buffer enable caching
1756   bool mVertexAttributeCachedState[ MAX_ATTRIBUTE_CACHE_SIZE ];    ///< Value cache for Enable Vertex Attribute
1757   bool mVertexAttributeCurrentState[ MAX_ATTRIBUTE_CACHE_SIZE ];   ///< Current state on the driver for Enable Vertex Attribute
1758
1759   Program* mCurrentProgram;
1760   typedef std::map< std::size_t, Program* > ProgramContainer;
1761   ProgramContainer mProgramCache; /// program cache
1762
1763 };
1764
1765 } // namespace Internal
1766
1767 } // namespace Dali
1768
1769 #endif // __DALI_INTERNAL_CONTEXT_H__