Remove RenderSurface from Core
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / context.h
index 28afbfd..be2db89 100644 (file)
@@ -1,8 +1,8 @@
-#ifndef __DALI_INTERNAL_CONTEXT_H__
-#define __DALI_INTERNAL_CONTEXT_H__
+#ifndef DALI_INTERNAL_CONTEXT_H
+#define DALI_INTERNAL_CONTEXT_H
 
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
 #include <dali/public-api/math/rect.h>
 #include <dali/public-api/math/vector4.h>
 #include <dali/public-api/rendering/renderer.h>
+#include <dali/devel-api/common/owner-container.h>
 #include <dali/integration-api/debug.h>
 #include <dali/integration-api/gl-abstraction.h>
 #include <dali/integration-api/gl-defines.h>
@@ -64,7 +65,7 @@ public:
   static const unsigned int MAX_TEXTURE_UNITS = 8; // for GLES 2.0 8 is guaranteed, which is more than DALi uses anyways
 
   /**
-   * Creates the Dali Context object.
+   * Creates the Dali Context object for surface rendering only.
    * This method does not create an OpenGL context i.e. that is done from outside dali-core.
    * @pre Context has not been created.
    * @exception Context already created.
@@ -73,6 +74,16 @@ public:
   Context( Integration::GlAbstraction& glAbstraction );
 
   /**
+   * Creates the Dali Context object for texture (and surface rendering if required).
+   * This method does not create an OpenGL context i.e. that is done from outside dali-core.
+   * @pre Context has not been created.
+   * @exception Context already created.
+   * @param glAbstraction the gl abstraction.
+   * @param contexts The list of scene contexts (for surface rendering)
+   */
+  Context( Integration::GlAbstraction& glAbstraction, std::vector< Context* >* contexts );
+
+  /**
    * Destructor
    */
   ~Context();
@@ -119,7 +130,27 @@ public:
    */
   void PrintGlString(const char* stringName, GLenum stringId)
   {
-    DALI_LOG_INFO(Debug::Filter::gRender, Debug::General, "GL %s = %s\n", stringName, (const char *)GetString( stringId ) );
+    DALI_LOG_INFO(Debug::Filter::gRender, Debug::General, "GL %s = %s\n", stringName, reinterpret_cast< const char * >( GetString( stringId ) ) );
+  }
+
+  void ResetBufferCache()
+  {
+    // reset the cached buffer id's
+    // fixes problem where some drivers will a generate a buffer with the
+    // same id, as the last deleted buffer id.
+    mBoundArrayBufferId = 0;
+    mBoundElementArrayBufferId = 0;
+    mBoundTransformFeedbackBufferId = 0;
+  }
+
+  void ResetTextureCache()
+  {
+    // reset the cached texture id's in case the driver re-uses them
+    // when creating new textures
+    for( unsigned int i=0; i < MAX_TEXTURE_UNITS; ++i )
+    {
+       mBoundTextureId[ i ] = 0;
+    }
   }
 
   /****************************************************************************************
@@ -130,6 +161,22 @@ public:
    ****************************************************************************************/
 
   /**
+   * Wrapper for IsSurfacelessContextSupported of Dali::Integration::GlAbstraction
+   */
+  bool IsSurfacelessContextSupported() const
+  {
+    return mGlAbstraction.IsSurfacelessContextSupported();
+  }
+
+  /**
+   * Wrapper for TextureRequiresConverting of Dali::Integration::GlAbstraction
+   */
+  bool TextureRequiresConverting( const GLenum imageGlFormat, const GLenum textureGlFormat, const bool isSubImage ) const
+  {
+    return mGlAbstraction.TextureRequiresConverting( imageGlFormat, textureGlFormat, isSubImage );
+  }
+
+  /**
    * Wrapper for OpenGL ES 2.0 glActiveTexture()
    */
   void ActiveTexture( TextureUnit textureUnit )
@@ -262,26 +309,12 @@ public:
    * @param textureunit to bind to
    * @param texture to bind
    */
-  void BindTextureForUnit( TextureUnit textureunit, GLuint texture )
+  void BindTextureForUnit( TextureUnit textureunit, int target, GLuint texture )
   {
-    if( mBound2dTextureId[ textureunit ] != texture )
+    if( mBoundTextureId[ textureunit ] != texture )
     {
       ActiveTexture( textureunit );
-      Bind2dTexture( texture );
-    }
-  }
-
-  /**
-   * Wrapper for OpenGL ES 2.0 glBindTexture(GL_TEXTURE_2D)
-   */
-  void Bind2dTexture( GLuint texture )
-  {
-    if (mBound2dTextureId[ mActiveTextureUnit ] != texture)
-    {
-      mBound2dTextureId[ mActiveTextureUnit ] = texture;
-
-      LOG_GL("BindTexture GL_TEXTURE_2D %d\n", texture);
-      CHECK_GL( mGlAbstraction, mGlAbstraction.BindTexture(GL_TEXTURE_2D, texture) );
+      BindTexture( target, texture );
     }
   }
 
@@ -290,9 +323,9 @@ public:
    */
   void BindTexture( int target, GLuint texture )
   {
-    if (mBound2dTextureId[ mActiveTextureUnit ] != texture)
+    if (mBoundTextureId[ mActiveTextureUnit ] != texture)
     {
-      mBound2dTextureId[ mActiveTextureUnit ] = texture;
+      mBoundTextureId[ mActiveTextureUnit ] = texture;
 
       LOG_GL("BindTexture target(%d) %d\n", target, texture);
       CHECK_GL( mGlAbstraction, mGlAbstraction.BindTexture(target, texture) );
@@ -300,15 +333,6 @@ public:
   }
 
   /**
-   * Wrapper for OpenGL ES 2.0 glBindTexture(GL_TEXTURE_CUBE_MAP)
-   */
-  void BindCubeMapTexture( GLuint texture )
-  {
-    LOG_GL("BindTexture GL_TEXTURE_CUBE_MAP %d\n", texture);
-    CHECK_GL( mGlAbstraction, mGlAbstraction.BindTexture(GL_TEXTURE_CUBE_MAP, texture) );
-  }
-
-  /**
    * Wrapper for OpenGL ES 2.0 glBlendColor()
    */
   void SetDefaultBlendColor()
@@ -613,19 +637,26 @@ public:
    */
   void DeleteBuffers(GLsizei n, const GLuint* buffers)
   {
-    // @todo: this is to prevent mesh destructor from doing GL calls when DALi core is being deleted
-    // can be taken out once render manages either knows about meshes or gpubuffers and can tell them directly that context is lost
     if( this->IsGlContextCreated() )
     {
       LOG_GL("DeleteBuffers %d %p\n", n, buffers);
       CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteBuffers(n, buffers) );
     }
-    // reset the cached buffer id's
-    // fixes problem where some drivers will a generate a buffer with the
-    // same id, as the last deleted buffer id.
-    mBoundArrayBufferId = 0;
-    mBoundElementArrayBufferId = 0;
-    mBoundTransformFeedbackBufferId = 0;
+
+    ResetBufferCache();
+
+    // Need to reset the buffer cache in the surface contexts
+    // This will only be executed by the surfaceless context when there are contexts for surface rendering
+    if ( mSceneContexts )
+    {
+      for ( auto&& context : *mSceneContexts )
+      {
+        if ( context )
+        {
+          context->ResetBufferCache();
+        }
+      }
+    }
   }
 
   /**
@@ -665,11 +696,19 @@ public:
     LOG_GL("DeleteTextures %d %p\n", n, textures);
     CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteTextures(n, textures) );
 
-    // reset the cached texture id's incase the driver re-uses them
-    // when creating new textures
-    for( unsigned int i=0; i < MAX_TEXTURE_UNITS; ++i )
+    ResetTextureCache();
+
+    // Need to reset the texture cache in the scene contexts
+    // This will only be executed by the surfaceless context when there are contexts for surface rendering
+    if ( mSceneContexts )
     {
-       mBound2dTextureId[ i ] = 0;
+      for ( auto&& context : *mSceneContexts )
+      {
+        if ( context )
+        {
+          context->ResetTextureCache();
+        }
+      }
     }
   }
 
@@ -1497,10 +1536,15 @@ public:
    */
   void StencilFunc(GLenum func, GLint ref, GLuint mask)
   {
+    if( ( func != mStencilFunc ) || ( ref != mStencilFuncRef ) || ( mask != mStencilFuncMask ) )
+    {
+      mStencilFunc = func;
+      mStencilFuncRef = ref;
+      mStencilFuncMask = mask;
 
-
-    LOG_GL("StencilFunc %x %d %d\n", func, ref, mask);
-    CHECK_GL( mGlAbstraction, mGlAbstraction.StencilFunc(func, ref, mask) );
+      LOG_GL("StencilFunc %x %d %d\n", func, ref, mask);
+      CHECK_GL( mGlAbstraction, mGlAbstraction.StencilFunc(func, ref, mask) );
+    }
   }
 
   /**
@@ -1540,8 +1584,15 @@ public:
    */
   void StencilOp(GLenum fail, GLenum zfail, GLenum zpass)
   {
-    LOG_GL("StencilOp %x %x %x\n", fail, zfail, zpass);
-    CHECK_GL( mGlAbstraction, mGlAbstraction.StencilOp(fail, zfail, zpass) );
+    if( ( fail != mStencilOpFail ) || ( zfail != mStencilOpDepthFail ) || ( zpass != mStencilOpDepthPass ) )
+    {
+      mStencilOpFail = fail;
+      mStencilOpDepthFail = zfail;
+      mStencilOpDepthPass = zpass;
+
+      LOG_GL("StencilOp %x %x %x\n", fail, zfail, zpass);
+      CHECK_GL( mGlAbstraction, mGlAbstraction.StencilOp(fail, zfail, zpass) );
+    }
   }
 
   /**
@@ -1646,7 +1697,11 @@ public:
   {
     // check if its same as already set
     Rect<int> newViewport( x, y, width, height );
-    if( mViewPort != newViewport )
+
+    // Temporarily disable the viewport caching, as the implementation of GLES driver in Tizen platform
+    // share a global viewport between multiple contexts, therefore glViewport has to be called every
+    // time after glBindFramebuffer regardless of the same vewport size in the same context.
+//    if( mViewPort != newViewport )
     {
       // set new one
       LOG_GL("Viewport %d %d %d %d\n", x, y, width, height);
@@ -1734,7 +1789,7 @@ private: // Data
 
   // glBindTexture() state
   TextureUnit mActiveTextureUnit;
-  GLuint mBound2dTextureId[ MAX_TEXTURE_UNITS ];  ///< The ID passed to glBindTexture(GL_TEXTURE_2D)
+  GLuint mBoundTextureId[ MAX_TEXTURE_UNITS ];  ///< The ID passed to glBindTexture()
 
   // glBlendColor() state
   Vector4 mBlendColor; ///< Blend color
@@ -1749,6 +1804,14 @@ private: // Data
   GLenum mBlendEquationSeparateModeRGB;    ///< Controls RGB blend mode
   GLenum mBlendEquationSeparateModeAlpha;  ///< Controls Alpha blend mode
 
+  // glStencilFunc() and glStencilOp() state.
+  GLenum mStencilFunc;
+  GLint mStencilFuncRef;
+  GLuint mStencilFuncMask;
+  GLenum mStencilOpFail;
+  GLenum mStencilOpDepthFail;
+  GLenum mStencilOpDepthPass;
+
   GLenum mDepthFunction;  ///The depth function
 
   GLint mMaxTextureSize;      ///< return value from GetIntegerv(GL_MAX_TEXTURE_SIZE)
@@ -1765,10 +1828,12 @@ private: // Data
   bool mVertexAttributeCurrentState[ MAX_ATTRIBUTE_CACHE_SIZE ];   ///< Current state on the driver for Enable Vertex Attribute
 
   FrameBufferStateCache mFrameBufferStateCache;   ///< frame buffer state cache
+
+  std::vector< Context* >* mSceneContexts;      ///< The pointer of the container of contexts for surface rendering
 };
 
 } // namespace Internal
 
 } // namespace Dali
 
-#endif // __DALI_INTERNAL_CONTEXT_H__
+#endif // DALI_INTERNAL_CONTEXT_H