Merge "(Automated Tests) Synchronised common files with Core" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-gl-abstraction.h
index c17fa22..f910d51 100644 (file)
 // EXTERNAL INCLUDES
 #include <sstream>
 #include <string>
+#include <cstring>
 #include <map>
+#include <cstdio>
+#include <cstring> // for strcmp
 
 // INTERNAL INCLUDES
-#include <dali/public-api/dali-core-capi-internal.h>
 #include <dali/public-api/dali-core.h>
 #include <dali/integration-api/core.h>
 #include <dali/integration-api/gl-abstraction.h>
+#include <dali/integration-api/gl-defines.h>
 #include "test-trace-call-stack.h"
 
 namespace Dali
@@ -52,7 +55,7 @@ public:
   void Initialize();
 
   void PreRender();
-  void PostRender(unsigned int timeDelta);
+  void PostRender();
 
   /* OpenGL ES 2.0 */
 
@@ -83,6 +86,8 @@ public:
 
   inline void BindFramebuffer( GLenum target, GLuint framebuffer )
   {
+    //Add 010 bit;
+    mFramebufferStatus |= 2;
   }
 
   inline void BindRenderbuffer( GLenum target, GLuint renderbuffer )
@@ -133,6 +138,10 @@ public:
         mActiveTextures[ mActiveTextureUnit ].mBoundTextures.push_back( texture );
       }
     }
+
+    std::stringstream out;
+    out << target << ", " << texture;
+    mTextureTrace.PushCall("BindTexture", out.str());
   }
 
   inline void BlendColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
@@ -208,19 +217,45 @@ public:
 
   inline void BufferData(GLenum target, GLsizeiptr size, const void* data, GLenum usage)
   {
+     mBufferDataCalls.push_back(size);
   }
 
   inline void BufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const void* data)
   {
+     mBufferSubDataCalls.push_back(size);
   }
 
   inline GLenum CheckFramebufferStatus(GLenum target)
   {
+    //If it has the three last bits set to 1 - 111, then the three minimum functions to create a
+    //Framebuffer texture have been called
+    if( mFramebufferStatus == 7 )
+    {
+      return GL_FRAMEBUFFER_COMPLETE;
+    }
+
     return mCheckFramebufferStatusResult;
   }
 
+  inline GLenum CheckFramebufferColorAttachment()
+  {
+    return mFramebufferColorAttached;
+  }
+
+  inline GLenum CheckFramebufferDepthAttachment()
+  {
+    return mFramebufferDepthAttached;
+  }
+
+  inline GLenum CheckFramebufferStencilAttachment()
+  {
+    return mFramebufferStencilAttached;
+  }
+
   inline void Clear(GLbitfield mask)
   {
+    mClearCount++;
+    mLastClearBitMask = mask;
   }
 
   inline void ClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
@@ -316,11 +351,11 @@ public:
   inline void DeleteTextures(GLsizei n, const GLuint* textures)
   {
     std::stringstream out;
-    out << n << ", " << textures << " = [" ;
+    out << n << ", " << textures << " = [";
 
     for(GLsizei i=0; i<n; i++)
     {
-      out << textures[i] << ", " ;
+      out << textures[i] << ", ";
       mDeletedTextureIds.push_back(textures[i]);
     }
     out << "]";
@@ -354,6 +389,9 @@ public:
 
   inline void DepthFunc(GLenum func)
   {
+    std::stringstream out;
+    out << func;
+    mDepthFunctionTrace.PushCall("DepthFunc", out.str());
   }
 
   inline void DepthMask(GLboolean flag)
@@ -375,7 +413,7 @@ public:
   {
     std::stringstream out;
     out << cap;
-    mCullFaceTrace.PushCall("Disable", out.str());
+    mEnableDisableTrace.PushCall("Disable", out.str());
   }
 
   inline void DisableVertexAttribArray(GLuint index)
@@ -401,7 +439,7 @@ public:
   {
     std::stringstream out;
     out << cap;
-    mCullFaceTrace.PushCall("Enable", out.str());
+    mEnableDisableTrace.PushCall("Enable", out.str());
   }
 
   inline void EnableVertexAttribArray(GLuint index)
@@ -419,10 +457,26 @@ public:
 
   inline void FramebufferRenderbuffer(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)
   {
+    if (attachment == GL_DEPTH_ATTACHMENT)
+    {
+      mFramebufferDepthAttached = true;
+    }
+    else if (attachment == GL_STENCIL_ATTACHMENT)
+    {
+      mFramebufferStencilAttached = true;
+    }
   }
 
   inline void FramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
   {
+    //Add 100 bit;
+    mFramebufferStatus |= 4;
+
+    //We check 4 attachment colors
+    if ((attachment == GL_COLOR_ATTACHMENT0) || (attachment == GL_COLOR_ATTACHMENT1) || (attachment == GL_COLOR_ATTACHMENT2)  || (attachment == GL_COLOR_ATTACHMENT4))
+    {
+      mFramebufferColorAttached = true;
+    }
   }
 
   inline void FrontFace(GLenum mode)
@@ -437,14 +491,28 @@ public:
 
   inline void GenerateMipmap(GLenum target)
   {
+    std::stringstream out;
+    out<<target;
+    mTextureTrace.PushCall("GenerateMipmap", out.str());
   }
 
   inline void GenFramebuffers(GLsizei n, GLuint* framebuffers)
   {
+    for( int i = 0; i < n; i++ )
+    {
+      framebuffers[i] = i + 1;
+    }
+
+    //Add 001 bit, this function needs to be called the first one in the chain
+    mFramebufferStatus = 1;
   }
 
   inline void GenRenderbuffers(GLsizei n, GLuint* renderbuffers)
   {
+    for( int i = 0; i < n; i++ )
+    {
+      renderbuffers[i] = i + 1;
+    }
   }
 
   /**
@@ -475,6 +543,17 @@ public:
         *(textures+i) = ++mLastAutoTextureIdUsed;
       }
     }
+
+    std::stringstream out;
+    for(int i=0; i<n; i++)
+    {
+      out << textures[i];
+      if(i<n-1)
+      {
+        out << ", ";
+      }
+    }
+    mTextureTrace.PushCall("GenTextures", out.str());
   }
 
   inline void GetActiveAttrib(GLuint program, GLuint index, GLsizei bufsize, GLsizei* length, GLint* size, GLenum* type, char* name)
@@ -483,6 +562,21 @@ public:
 
   inline void GetActiveUniform(GLuint program, GLuint index, GLsizei bufsize, GLsizei* length, GLint* size, GLenum* type, char* name)
   {
+    switch(index)
+    {
+      case 0:
+        *length = snprintf(name, bufsize, "sTexture");
+        *type = GL_SAMPLER_2D;
+        *size = 1;
+        break;
+      case 1:
+        *length = snprintf(name, bufsize, "sEffect");
+        *type = GL_SAMPLER_2D;
+        *size = 1;
+        break;
+      default:
+        break;
+    }
   }
 
   inline void GetAttachedShaders(GLuint program, GLsizei maxcount, GLsizei* count, GLuint* shaders)
@@ -547,13 +641,20 @@ public:
 
   inline void GetProgramiv(GLuint program, GLenum pname, GLint* params)
   {
-    switch( pname ) {
+    switch( pname )
+    {
       case GL_LINK_STATUS:
         *params = mLinkStatus;
         break;
       case GL_PROGRAM_BINARY_LENGTH_OES:
         *params = mProgramBinaryLength;
         break;
+      case GL_ACTIVE_UNIFORMS:
+        *params = mNumberOfActiveUniforms;
+        break;
+      case GL_ACTIVE_UNIFORM_MAX_LENGTH:
+        *params = 100;
+        break;
     }
   }
 
@@ -561,7 +662,6 @@ public:
   {
   }
 
-
   inline void GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint* params)
   {
   }
@@ -686,6 +786,10 @@ public:
     std::stringstream out;
     out << program;
     mShaderTrace.PushCall("LinkProgram", out.str());
+
+    mNumberOfActiveUniforms=2;
+    GetUniformLocation(program, "sTexture");
+    GetUniformLocation(program, "sEffect");
   }
 
   inline void PixelStorei(GLenum pname, GLint param)
@@ -714,6 +818,10 @@ public:
 
   inline void Scissor(GLint x, GLint y, GLsizei width, GLsizei height)
   {
+    mScissorParams.x = x;
+    mScissorParams.y = y;
+    mScissorParams.width = width;
+    mScissorParams.height = height;
   }
 
   inline void ShaderBinary(GLsizei n, const GLuint* shaders, GLenum binaryformat, const void* binary, GLsizei length)
@@ -747,6 +855,11 @@ public:
     }
   }
 
+  inline std::string GetShaderSource(GLuint shader)
+  {
+    return mShaderSources[shader];
+  }
+
   inline void StencilFunc(GLenum func, GLint ref, GLuint mask)
   {
   }
@@ -774,30 +887,42 @@ public:
   inline void TexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void* pixels)
   {
     std::stringstream out;
-    out << width << ", " << height;
+    out << target<<", "<<level<<", "<<width << ", " << height;
     mTextureTrace.PushCall("TexImage2D", out.str());
   }
 
   inline void TexParameterf(GLenum target, GLenum pname, GLfloat param)
   {
+    std::stringstream out;
+    out << target << ", " << pname << ", " << param;
+    mTexParamaterTrace.PushCall("TexParameterf", out.str());
   }
 
   inline void TexParameterfv(GLenum target, GLenum pname, const GLfloat* params)
   {
+    std::stringstream out;
+    out << target << ", " << pname << ", " << params[0];
+    mTexParamaterTrace.PushCall("TexParameterfv", out.str());
   }
 
   inline void TexParameteri(GLenum target, GLenum pname, GLint param)
   {
+    std::stringstream out;
+    out << target << ", " << pname << ", " << param;
+    mTexParamaterTrace.PushCall("TexParameteri", out.str());
   }
 
   inline void TexParameteriv(GLenum target, GLenum pname, const GLint* params)
   {
+    std::stringstream out;
+    out << target << ", " << pname << ", " << params[0];
+    mTexParamaterTrace.PushCall("TexParameteriv", out.str());
   }
 
   inline void TexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void* pixels)
   {
     std::stringstream out;
-    out << xoffset << ", " << yoffset << ", " << width << ", " << height;
+    out << target << ", "<<level <<", " << xoffset << ", " << yoffset << ", " << width << ", " << height;
     mTextureTrace.PushCall("TexSubImage2D", out.str());
   }
 
@@ -1424,6 +1549,7 @@ public:
 
   inline void GetProgramBinary(GLuint program, GLsizei bufSize, GLsizei* length, GLenum* binaryFormat, GLvoid* binary)
   {
+    mGetProgramBinaryCalled = true;
   }
 
   inline void ProgramBinary(GLuint program, GLenum binaryFormat, const GLvoid* binary, GLsizei length)
@@ -1489,6 +1615,11 @@ public: // TEST FUNCTIONS
   inline void ResetCullFaceCallStack() { mCullFaceTrace.Reset(); }
   inline TraceCallStack& GetCullFaceTrace() { return mCullFaceTrace; }
 
+  //Methods for Enable/Disable call verification
+  inline void EnableEnableDisableCallTrace(bool enable) { mEnableDisableTrace.Enable(enable); }
+  inline void ResetEnableDisableCallStack() { mEnableDisableTrace.Reset(); }
+  inline TraceCallStack& GetEnableDisableTrace() { return mEnableDisableTrace; }
+
   //Methods for Shader verification
   inline void EnableShaderCallTrace(bool enable) { mShaderTrace.Enable(enable); }
   inline void ResetShaderCallStack() { mShaderTrace.Reset(); }
@@ -1499,11 +1630,45 @@ public: // TEST FUNCTIONS
   inline void ResetTextureCallStack() { mTextureTrace.Reset(); }
   inline TraceCallStack& GetTextureTrace() { return mTextureTrace; }
 
+  //Methods for Texture verification
+  inline void EnableTexParameterCallTrace(bool enable) { mTexParamaterTrace.Enable(enable); }
+  inline void ResetTexParameterCallStack() { mTexParamaterTrace.Reset(); }
+  inline TraceCallStack& GetTexParameterTrace() { return mTexParamaterTrace; }
+
   //Methods for Draw verification
   inline void EnableDrawCallTrace(bool enable) { mDrawTrace.Enable(enable); }
   inline void ResetDrawCallStack() { mDrawTrace.Reset(); }
   inline TraceCallStack& GetDrawTrace() { return mDrawTrace; }
 
+  //Methods for Depth function verification
+  inline void EnableDepthFunctionCallTrace(bool enable) { mDepthFunctionTrace.Enable(enable); }
+  inline void ResetDepthFunctionCallStack() { mDepthFunctionTrace.Reset(); }
+  inline TraceCallStack& GetDepthFunctionTrace() { return mDepthFunctionTrace; }
+
+  template <typename T>
+  inline bool GetUniformValue( const char* name, T& value ) const
+  {
+    for( ProgramUniformMap::const_iterator program_it = mUniforms.begin();
+          program_it != mUniforms.end();
+          ++program_it )
+    {
+      const UniformIDMap &uniformIDs = program_it->second;
+
+      UniformIDMap::const_iterator uniform_it = uniformIDs.find( name );
+      if( uniform_it != uniformIDs.end() )
+      {
+        // found one matching uniform name, lets check the value...
+        GLuint programId = program_it->first;
+        GLint uniformId = uniform_it->second;
+
+        const ProgramUniformValue<T> &mProgramUniforms = GetProgramUniformsForType( value );
+        return mProgramUniforms.GetUniformValue( programId, uniformId, value );
+      }
+    }
+    return false;
+  }
+
+
   template <typename T>
   inline bool CheckUniformValue( const char* name, const T& value ) const
   {
@@ -1529,7 +1694,7 @@ public: // TEST FUNCTIONS
       }
     }
 
-    fprintf(stderr, "Not found, printing possible values:" );
+    fprintf(stderr, "Not found, printing possible values:\n" );
     for( ProgramUniformMap::const_iterator program_it = mUniforms.begin();
           program_it != mUniforms.end();
           ++program_it )
@@ -1549,7 +1714,7 @@ public: // TEST FUNCTIONS
         {
           std::stringstream out;
           out << uniform_it->first << ": " << origValue;
-          fprintf(stderr, "%s", out.str().c_str() );
+          fprintf(stderr, "%s\n", out.str().c_str() );
         }
       }
     }
@@ -1582,8 +1747,6 @@ public: // TEST FUNCTIONS
     return false;
   }
 
-
-
   inline GLuint GetLastShaderCompiled() const
   {
     return mLastShaderCompiled;
@@ -1594,6 +1757,11 @@ public: // TEST FUNCTIONS
     return mLastProgramIdUsed;
   }
 
+  inline GLbitfield GetLastClearMask() const
+  {
+    return mLastClearBitMask;
+  }
+
   enum AttribType
   {
     ATTRIB_UNKNOWN = -1,
@@ -1606,11 +1774,38 @@ public: // TEST FUNCTIONS
     ATTRIB_TYPE_LAST
   };
 
+  struct ScissorParams
+  {
+    GLint x;
+    GLint y;
+    GLsizei width;
+    GLsizei height;
+
+    ScissorParams() : x( 0 ), y( 0 ), width( 0 ), height( 0 ) { }
+  };
+
+  // Methods to check scissor tests
+  inline const ScissorParams& GetScissorParams() const { return mScissorParams; }
+
+  inline bool GetProgramBinaryCalled() const { return mGetProgramBinaryCalled; }
+
+  inline unsigned int GetClearCountCalled() const { return mClearCount; }
+
+  typedef std::vector<size_t> BufferDataCalls;
+  inline const BufferDataCalls& GetBufferDataCalls() const { return mBufferDataCalls; }
+  inline void ResetBufferDataCalls() { mBufferDataCalls.clear(); }
+
+  typedef std::vector<size_t> BufferSubDataCalls;
+  inline const BufferSubDataCalls& GetBufferSubDataCalls() const { return mBufferSubDataCalls; }
+  inline void ResetBufferSubDataCalls() { mBufferSubDataCalls.clear(); }
 
 private:
   GLuint     mCurrentProgram;
   GLuint     mCompileStatus;
+  BufferDataCalls mBufferDataCalls;
+  BufferSubDataCalls mBufferSubDataCalls;
   GLuint     mLinkStatus;
+  GLint      mNumberOfActiveUniforms;
   GLint      mGetAttribLocationResult;
   GLenum     mGetErrorResult;
   GLubyte*   mGetStringResult;
@@ -1623,14 +1818,21 @@ private:
   GLboolean  mIsTextureResult;
   GLenum     mActiveTextureUnit;
   GLenum     mCheckFramebufferStatusResult;
+  GLint      mFramebufferStatus;
+  GLenum     mFramebufferColorAttached;
+  GLenum     mFramebufferDepthAttached;
+  GLenum     mFramebufferStencilAttached;
   GLint      mNumBinaryFormats;
   GLint      mBinaryFormats;
   GLint      mProgramBinaryLength;
   bool       mVertexAttribArrayState[MAX_ATTRIBUTE_CACHE_SIZE];
   bool       mVertexAttribArrayChanged;                            // whether the vertex attrib array has been changed
+  bool       mGetProgramBinaryCalled;
   typedef std::map< GLuint, std::string> ShaderSourceMap;
   ShaderSourceMap mShaderSources;
   GLuint     mLastShaderCompiled;
+  GLbitfield mLastClearBitMask;
+  unsigned int mClearCount;
 
   Vector4 mLastBlendColor;
   GLenum  mLastBlendEquationRgb;
@@ -1654,9 +1856,12 @@ private:
   ActiveTextureType mActiveTextures[ MIN_TEXTURE_UNIT_LIMIT ];
 
   TraceCallStack mCullFaceTrace;
+  TraceCallStack mEnableDisableTrace;
   TraceCallStack mShaderTrace;
   TraceCallStack mTextureTrace;
+  TraceCallStack mTexParamaterTrace;
   TraceCallStack mDrawTrace;
+  TraceCallStack mDepthFunctionTrace;
 
   // Shaders & Uniforms
   GLuint mLastShaderIdUsed;
@@ -1782,6 +1987,8 @@ private:
     mVertexAttribArrayState[ index ] = state;
     mVertexAttribArrayChanged = true;
   }
+
+  ScissorParams mScissorParams;
 };
 
 template <>