Merge branch 'devel/master' into devel/graphics
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-pipeline.h
1 #ifndef DALI_GRAPHICS_GLES_PIPELINE_H
2 #define DALI_GRAPHICS_GLES_PIPELINE_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/graphics-api/graphics-pipeline-create-info.h>
23 #include <dali/graphics-api/graphics-pipeline.h>
24 #include <string.h>
25
26 // INTERNAL INCLUDES
27 #include "gles-graphics-reflection.h"
28 #include "gles-graphics-resource.h"
29
30 namespace Dali::Graphics::GLES
31 {
32 class PipelineCache;
33
34 /**
35  * @brief PipelineImpl is the implementation of Pipeline
36  *
37  * PipelineImpl is owned by the pipeline cache. The client-side
38  * will receive Graphics::Pipeline objects which are only
39  * wrappers for this implementation. The lifecycle of
40  * PipelineImpl is managed by the PipelineCache.
41  */
42 class PipelineImpl
43 {
44 public:
45   /**
46    * @brief Constructor
47    * @param[in] createInfo valid TextureCreateInfo structure
48    * @param[in] controller Reference to the Controller
49    * @param[in] pipelineCache Reference to valid pipeline cache
50    */
51   PipelineImpl(const Graphics::PipelineCreateInfo& createInfo, Graphics::EglGraphicsController& controller, PipelineCache& pipelineCache);
52
53   /**
54    * @brief Destructor
55    */
56   ~PipelineImpl();
57
58   /**
59    * @brief Binds pipeline
60    *
61    * Binds Pipeline by binding GL program and flushing state.
62    *
63    * If previous pipeline specified, it will be used in order to
64    * avoid redundant state swiches.
65    *
66    * @param[in] prevPipeline previous pipeline
67    */
68   void Bind(GLES::PipelineImpl* prevPipeline);
69
70   /**
71    * @brief Increases ref count
72    */
73   void Retain();
74
75   /**
76    * @brief Decreases ref count
77    */
78   void Release();
79
80   /**
81    * @brief Retrieves ref count
82    * @return Refcount value
83    */
84   [[nodiscard]] uint32_t GetRefCount() const;
85
86   /**
87    * @brief Returns PipelineCreateInfo structure
88    *
89    * @return PipelineCreateInfo structure
90    */
91   [[nodiscard]] const PipelineCreateInfo& GetCreateInfo() const;
92
93   /**
94    * @brief Returns controller
95    *
96    * @return Reference to the Controller
97    */
98   [[nodiscard]] auto& GetController() const;
99
100 private:
101   /**
102    * @brief Helper function. Copies state if pointer is set
103    */
104   template<class T>
105   void CopyStateIfSet(const T* sourceState, T& copyState, T** destState)
106   {
107     *destState = nullptr;
108     if(sourceState)
109     {
110       copyState  = *sourceState;
111       *destState = &copyState;
112     }
113   }
114
115   /**
116    * @brief Helper function. Copies const state if pointer is set
117    */
118   template<class T>
119   void CopyStateIfSet(const T* sourceState, T& copyState, const T** destState)
120   {
121     *destState = nullptr;
122     if(sourceState)
123     {
124       copyState  = *sourceState;
125       *destState = &copyState;
126     }
127   }
128
129   // Pipeline state is store locally so any assigned pointers on a
130   // client-side may go safely out of scope.
131   struct PipelineState;
132   std::unique_ptr<PipelineState> mPipelineState;
133
134   EglGraphicsController& mController;
135   PipelineCreateInfo     mCreateInfo;
136
137   uint32_t mRefCount{0u};
138 };
139
140 /**
141  * @brief Pipeline class wraps the PipelineImpl
142  */
143 class Pipeline : public Graphics::Pipeline
144 {
145 public:
146   Pipeline() = delete;
147
148   /**
149    * @brief Constructor
150    * @param pipeline Pipeline implementation
151    */
152   explicit Pipeline(GLES::PipelineImpl& pipeline)
153   : mPipeline(pipeline)
154   {
155     // increase refcount
156     mPipeline.Retain();
157   }
158
159   /**
160    * @brief Destructor
161    */
162   ~Pipeline() override;
163
164   /**
165    * @brief Returns pipeline implementation
166    *
167    * @return Valid pipeline implementation
168    */
169   [[nodiscard]] auto& GetPipeline() const
170   {
171     return mPipeline;
172   }
173
174   /**
175    * @brief Returns create info structure
176    *
177    * @return Valid create info structure
178    */
179   [[nodiscard]] const PipelineCreateInfo& GetCreateInfo() const;
180
181   /**
182    * @brief Returns controller
183    *
184    * @return reference to Controller
185    */
186   [[nodiscard]] EglGraphicsController& GetController() const;
187
188   bool operator==(const PipelineImpl* impl) const
189   {
190     return &mPipeline == impl;
191   }
192
193   /**
194    * @brief Run by UniquePtr to discard resource
195    */
196   void DiscardResource();
197
198   /**
199    * @brief Destroy resource
200    *
201    * Despite this class doesn't inherit Resource it must provide
202    * (so it won't duplicate same data) same set of functions
203    * so it can work with resource management functions of Controller.
204    */
205   void DestroyResource()
206   {
207     // Nothing to do here
208   }
209
210 private:
211   GLES::PipelineImpl& mPipeline;
212 };
213
214 } // namespace Dali::Graphics::GLES
215 #endif