Revert "[Tizen] Do not call gl functions during shutdown"
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / egl-graphics-controller.h
index 673e5a9..6af7383 100644 (file)
@@ -135,6 +135,19 @@ public:
   {
     // Final flush
     Flush();
+
+    if(mContext)
+    {
+      mContext->GlContextDestroyed();
+    }
+
+    for(auto&& context : mSurfaceContexts)
+    {
+      if(context.second)
+      {
+        context.second->GlContextDestroyed();
+      }
+    }
   }
 
   /**
@@ -379,6 +392,30 @@ public:
   }
 
   /**
+   * @brief Pushes RenderPass to the discard queue
+   *
+   * Function is called from the UniquePtr custom deleter.
+   *
+   * @param[in] program Pointer to the RenderPass
+   */
+  void DiscardResource(GLES::RenderPass* renderPass)
+  {
+    mDiscardRenderPassQueue.push(renderPass);
+  }
+
+  /**
+   * @brief Pushes RenderTarget to the discard queue
+   *
+   * Function is called from the UniquePtr custom deleter.
+   *
+   * @param[in] program Pointer to the RenderTarget
+   */
+  void DiscardResource(GLES::RenderTarget* renderTarget)
+  {
+    mDiscardRenderTargetQueue.push(renderTarget);
+  }
+
+  /**
    * @brief Pushes Shader to the discard queue
    *
    * Function is called from the UniquePtr custom deleter.
@@ -434,7 +471,14 @@ public:
    */
   void Flush()
   {
-    mGraphics->ActivateResourceContext();
+    if(!mCreateTextureQueue.empty() ||
+       !mCreateBufferQueue.empty() ||
+       !mCreateFramebufferQueue.empty() ||
+       !mTextureUpdateRequests.empty() ||
+       !mTextureMipmapGenerationRequests.empty())
+    {
+      mGraphics->ActivateResourceContext();
+    }
 
     // Process creations
     ProcessCreateQueues();
@@ -448,6 +492,12 @@ public:
     // Process main command queue
     ProcessCommandQueues();
 
+    // Reset texture cache in the contexts while destroying textures
+    ResetTextureCache();
+
+    // Reset buffer cache in the contexts while destroying buffers
+    ResetBufferCache();
+
     // Process discards
     ProcessDiscardQueues();
 
@@ -483,9 +533,9 @@ public:
   }
 
   /**
-   * @brief Processes a create queue for type specified
+   * @brief Processes a discard queue for type specified
    *
-   * @param[in,out] queue Reference to the create queue
+   * @param[in,out] queue Reference to the discard queue
    */
   template<class U, class T>
   void ProcessDiscardQueue(T& queue)
@@ -516,6 +566,53 @@ public:
   }
 
   /**
+   * @brief Processes a discard queue for pipeline
+   *
+   * @param[in,out] queue Reference to the create queue
+   */
+  void ProcessDiscardQueue(std::queue<GLES::Pipeline*>& queue)
+  {
+    while(!queue.empty())
+    {
+      auto* object = const_cast<GLES::Pipeline*>(queue.front());
+
+      // Inform the contexts to invalidate the pipeline if cached
+      if(mContext)
+      {
+        mContext->InvalidateCachedPipeline(object);
+      }
+
+      for(auto&& context : mSurfaceContexts)
+      {
+        if(context.second)
+        {
+          context.second->InvalidateCachedPipeline(object);
+        }
+      }
+
+      // Destroy
+      object->DestroyResource();
+
+      // Free
+      auto* clbk = object->GetCreateInfo().allocationCallbacks;
+      if(clbk)
+      {
+        // Call destructor
+        using GLESPipeline = GLES::Pipeline;
+        object->~GLESPipeline();
+
+        // Free memory
+        clbk->freeCallback(object, clbk->userData);
+      }
+      else
+      {
+        delete object;
+      }
+      queue.pop();
+    }
+  }
+
+  /**
    * @brief Processes all resource create queues
    */
   void ProcessCreateQueues();
@@ -580,6 +677,44 @@ public:
     return mIsShuttingDown;
   }
 
+  /**
+   * @brief Reset texture cache in the contexts
+   */
+  void ResetTextureCache()
+  {
+    if(mContext)
+    {
+      mContext->GetGLStateCache().ResetTextureCache();
+    }
+
+    for(auto& context : mSurfaceContexts)
+    {
+      if(context.second)
+      {
+        context.second->GetGLStateCache().ResetTextureCache();
+      }
+    }
+  }
+
+  /**
+   * @brief Reset buffer cache in the contexts
+   */
+  void ResetBufferCache()
+  {
+    if(mContext)
+    {
+      mContext->GetGLStateCache().ResetBufferCache();
+    }
+
+    for(auto& context : mSurfaceContexts)
+    {
+      if(context.second)
+      {
+        context.second->GetGLStateCache().ResetBufferCache();
+      }
+    }
+  }
+
   void ProcessCommandBuffer(const GLES::CommandBuffer& commandBuffer);
 
   // Resolves presentation
@@ -611,6 +746,16 @@ public:
    */
   void ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface);
 
+  /**
+   * @brief Returns the current context
+   *
+   * @return the current context
+   */
+  GLES::Context* GetCurrentContext() const
+  {
+    return mCurrentContext;
+  }
+
 private:
   Integration::GlAbstraction*              mGlAbstraction{nullptr};
   Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
@@ -626,6 +771,8 @@ private:
 
   std::queue<GLES::Program*>             mDiscardProgramQueue;       ///< Discard queue for program resource
   std::queue<GLES::Pipeline*>            mDiscardPipelineQueue;      ///< Discard queue of pipelines
+  std::queue<GLES::RenderPass*>          mDiscardRenderPassQueue;    ///< Discard queue for renderpass resource
+  std::queue<GLES::RenderTarget*>        mDiscardRenderTargetQueue;  ///< Discard queue for rendertarget resource
   std::queue<GLES::Shader*>              mDiscardShaderQueue;        ///< Discard queue of shaders
   std::queue<GLES::Sampler*>             mDiscardSamplerQueue;       ///< Discard queue of samplers
   std::queue<const GLES::CommandBuffer*> mDiscardCommandBufferQueue; ///< Discard queue of command buffers
@@ -651,8 +798,7 @@ private:
 
   bool mIsShuttingDown{false}; ///< Indicates whether the controller is shutting down
 
-  // todo: to be removed after renderpass
-  const Graphics::Framebuffer* currentFramebuffer{nullptr};
+  std::queue<const GLES::CommandBuffer*> mPresentationCommandBuffers{}; ///< Queue of reusable command buffers used by presentation engine
 };
 
 } // namespace Graphics