[Tizen] Revert "[Tizen] Fix RenderTarget and RenderPass doesn't destroy issue"
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-pipeline.h
index 0f202e1..57c490e 100644 (file)
 #include <string.h>
 
 // INTERNAL INCLUDES
+#include "gles-graphics-reflection.h"
 #include "gles-graphics-resource.h"
 
 namespace Dali::Graphics::GLES
 {
-using PipelineResource = Resource<Graphics::Pipeline, Graphics::PipelineCreateInfo>;
+class PipelineCache;
+class Program;
 
-class Pipeline : public PipelineResource
+/**
+ * @brief PipelineImpl is the implementation of Pipeline
+ *
+ * PipelineImpl is owned by the pipeline cache. The client-side
+ * will receive Graphics::Pipeline objects which are only
+ * wrappers for this implementation. The lifecycle of
+ * PipelineImpl is managed by the PipelineCache.
+ */
+class PipelineImpl
 {
 public:
   /**
    * @brief Constructor
    * @param[in] createInfo valid TextureCreateInfo structure
    * @param[in] controller Reference to the Controller
+   * @param[in] pipelineCache Reference to valid pipeline cache
    */
-  Pipeline(const Graphics::PipelineCreateInfo& createInfo, Graphics::EglGraphicsController& controller);
+  PipelineImpl(const Graphics::PipelineCreateInfo& createInfo, Graphics::EglGraphicsController& controller, PipelineCache& pipelineCache);
 
   /**
-   * @brief Destroys all the low-level resources used by the class
+   * @brief Destructor
    */
-  void DestroyResource() override;
+  ~PipelineImpl();
 
   /**
-   * @brief Initializes low-level resources
+   * @brief Binds pipeline
+   *
+   * Binds Pipeline by binding GL program and flushing state.
    *
-   * @return Tron success
+   * @param[in] glProgram The GL program to be bound
    */
-  bool InitializeResource() override;
+  void Bind(const uint32_t glProgram) const;
 
   /**
-   * @brief Discards object
+   * @brief Increases ref count
    */
-  void DiscardResource() override;
+  void Retain();
 
   /**
-   * @brief returns GL program id
-   * @return GL program id
+   * @brief Decreases ref count
    */
-  [[nodiscard]] uint32_t GetGLProgram() const;
+  void Release();
 
   /**
-   * @brief Binds pipeline
-   *
-   * Binds Pipeline by binding GL program and flushing state.
-   *
-   * If previous pipeline specified, it will be used in order to
-   * avoid redundant state swiches.
+   * @brief Retrieves ref count
+   * @return Refcount value
+   */
+  [[nodiscard]] uint32_t GetRefCount() const;
+
+  /**
+   * @brief Returns PipelineCreateInfo structure
    *
-   * @param[in] prevPipeline previous pipeline
+   * @return PipelineCreateInfo structure
    */
-  void Bind(GLES::Pipeline* prevPipeline);
+  [[nodiscard]] const PipelineCreateInfo& GetCreateInfo() const;
 
   /**
-   * Executes state change function if condition met
+   * @brief Returns controller
+   *
+   * @return Reference to the Controller
    */
-  template<typename FUNC, typename STATE>
-  void ExecuteStateChange(FUNC& func, const STATE* prevPipelineState, const STATE* thisPipelineState)
-  {
-    if(!prevPipelineState)
-    {
-      func();
-    }
-    else
-    {
-      // binary test and execute when different
-      if(memcmp(prevPipelineState, thisPipelineState, sizeof(STATE)) != 0)
-      {
-        func();
-      }
-    }
-  }
+  [[nodiscard]] auto& GetController() const;
 
 private:
   /**
@@ -124,12 +124,89 @@ private:
     }
   }
 
-  // Pipeline state is stored as a copy of create info
-  // data.
+  // Pipeline state is store locally so any assigned pointers on a
+  // client-side may go safely out of scope.
   struct PipelineState;
-  std::unique_ptr<PipelineState> mPipelineState{};
+  std::unique_ptr<PipelineState> mPipelineState;
+
+  EglGraphicsController& mController;
+  PipelineCreateInfo     mCreateInfo;
+
+  uint32_t mRefCount{0u};
+};
+
+/**
+ * @brief Pipeline class wraps the PipelineImpl
+ */
+class Pipeline : public Graphics::Pipeline
+{
+public:
+  Pipeline() = delete;
+
+  /**
+   * @brief Constructor
+   * @param pipeline Pipeline implementation
+   */
+  explicit Pipeline(GLES::PipelineImpl& pipeline)
+  : mPipeline(pipeline)
+  {
+    // increase refcount
+    mPipeline.Retain();
+  }
+
+  /**
+   * @brief Destructor
+   */
+  ~Pipeline() override;
+
+  /**
+   * @brief Returns pipeline implementation
+   *
+   * @return Valid pipeline implementation
+   */
+  [[nodiscard]] auto& GetPipeline() const
+  {
+    return mPipeline;
+  }
+
+  /**
+   * @brief Returns create info structure
+   *
+   * @return Valid create info structure
+   */
+  [[nodiscard]] const PipelineCreateInfo& GetCreateInfo() const;
 
-  uint32_t mGlProgram{0u};
+  /**
+   * @brief Returns controller
+   *
+   * @return reference to Controller
+   */
+  [[nodiscard]] EglGraphicsController& GetController() const;
+
+  bool operator==(const PipelineImpl* impl) const
+  {
+    return &mPipeline == impl;
+  }
+
+  /**
+   * @brief Run by UniquePtr to discard resource
+   */
+  void DiscardResource();
+
+  /**
+   * @brief Destroy resource
+   *
+   * Despite this class doesn't inherit Resource it must provide
+   * (so it won't duplicate same data) same set of functions
+   * so it can work with resource management functions of Controller.
+   */
+  void DestroyResource()
+  {
+    // Nothing to do here
+  }
+
+private:
+  GLES::PipelineImpl& mPipeline;
 };
 
 } // namespace Dali::Graphics::GLES