avoid sending view and projection matrices multiple times per frame to gl programs
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / context.h
index a151c8c..b5d17d3 100644 (file)
@@ -1,25 +1,25 @@
 #ifndef __DALI_INTERNAL_CONTEXT_H__
 #define __DALI_INTERNAL_CONTEXT_H__
 
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2014 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
 // INTERNAL INCLUDES
 #include <dali/public-api/common/map-wrapper.h>
-#include <dali/public-api/common/set-wrapper.h>
 #include <dali/public-api/common/vector-wrapper.h>
 #include <dali/public-api/actors/renderable-actor.h>
 #include <dali/integration-api/debug.h>
@@ -35,7 +35,6 @@ namespace Dali
 namespace Internal
 {
 
-class ContextObserver;
 class Program; // to be able to cache programs
 
 // wrap gl calls with CHECK_GL eg "CHECK_GL( *this, glBindTexture(textureId) );"
@@ -53,13 +52,11 @@ class Program; // to be able to cache programs
 
 /**
  * Context records the current GL state, and provides access to the OpenGL ES 2.0 API.
- * Observers will be notified when the GL context is created/destroyed.
  * Context avoids duplicate GL calls, if the same setting etc. is requested repeatedly.
  */
 class Context
 {
 public:
-
   /**
    * Size of the VertexAttributeArray enables
    * GLES specification states that there's minimum of 8
@@ -82,15 +79,13 @@ public:
 
   /**
    * Called when the GL context has been created.
-   * @post Context observers will be notified.
    */
   void GlContextCreated();
 
   /**
-   * Called when the GL context is about to be destroyed.
-   * @post Context observers will be notified, and should free any GL resources held.
+   * Called when the GL context has been destroyed.
    */
-  void GlContextToBeDestroyed();
+  void GlContextDestroyed();
 
   /**
    * Query whether the OpenGL context has been created.
@@ -99,20 +94,6 @@ public:
   bool IsGlContextCreated() { return mGlContextCreated; }
 
   /**
-   * Adds an observer to the Context object.
-   * The observer is responsible for calling RemoveObserver() before destruction.
-   * @param[in] observer The observer to add.
-   */
-  void AddObserver(ContextObserver& observer);
-
-  /**
-   * Removes an observer from the Context object.
-   * The observer must call this method before it is destroyed.
-   * @param[in] observer The observer to remove.
-   */
-  void RemoveObserver(ContextObserver& observer);
-
-  /**
    * @return the GLAbstraction
    */
   Integration::GlAbstraction& GetAbstraction() { return mGlAbstraction; }
@@ -602,9 +583,13 @@ public:
    */
   void DeleteBuffers(GLsizei n, const GLuint* buffers)
   {
-    LOG_GL("DeleteBuffers %d %p\n", n, buffers);
-    CHECK_GL( *this, mGlAbstraction.DeleteBuffers(n, 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( *this, 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.
@@ -1662,6 +1647,11 @@ public:
   }
 
   /**
+   * Reset the program matrices
+   */
+  void ResetProgramMatrices();
+
+  /**
    * Get a cached program
    * @param [in] hash value
    * @return pointer to the program
@@ -1688,6 +1678,72 @@ public:
 
 #endif // DEBUG_ENABLED
 
+
+  /**
+   * Set the frame count of render thread
+   */
+  inline void SetFrameCount(unsigned int frameCount)
+  {
+    mFrameCount = frameCount;
+  }
+
+  /**
+   * Get the frame count
+   */
+  inline unsigned int GetFrameCount()
+  {
+    return mFrameCount;
+  }
+
+  /**
+   * Increment the count of culled renderers
+   */
+  inline void IncrementCulledCount()
+  {
+    mCulledCount++;
+  }
+
+  /**
+   * Clear the count of culled renderers
+   */
+  inline void ClearCulledCount()
+  {
+    mCulledCount = 0;
+  }
+
+  /**
+   * Get the count of culled renderers in this frame
+   */
+  inline unsigned int GetCulledCount()
+  {
+    return mCulledCount;
+  }
+
+  /**
+   * Increment the count of culled renderers
+   */
+  inline void IncrementRendererCount()
+  {
+    mRendererCount++;
+  }
+
+  /**
+   * Clear the count of image renderers
+   */
+  inline void ClearRendererCount()
+  {
+    mRendererCount = 0;
+  }
+
+  /**
+   * Get the count of image renderers in this frame
+   */
+  inline unsigned int GetRendererCount()
+  {
+    return mRendererCount;
+  }
+
+
 private: // Implementation
 
   /**
@@ -1771,11 +1827,12 @@ private: // Data
   bool mVertexAttributeCachedState[ MAX_ATTRIBUTE_CACHE_SIZE ];    ///< Value cache for Enable Vertex Attribute
   bool mVertexAttributeCurrentState[ MAX_ATTRIBUTE_CACHE_SIZE ];   ///< Current state on the driver for Enable Vertex Attribute
 
-  std::set<ContextObserver*> mObservers;
-
   Program* mCurrentProgram;
-  std::map< std::size_t, Program* > mProgramCache; /// program cache
-
+  typedef std::map< std::size_t, Program* > ProgramContainer;
+  ProgramContainer mProgramCache; ///< Cache of shader programs
+  unsigned int mFrameCount;       ///< Number of render frames
+  unsigned int mCulledCount;      ///< Number of culled renderers per frame
+  unsigned int mRendererCount;    ///< Number of image renderers per frame
 };
 
 } // namespace Internal