64dad5a41801ed096a3e5b3a8fe2ef739ef2fdc5
[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-resource.h"
28
29 namespace Dali::Graphics::GLES
30 {
31 using PipelineResource = Resource<Graphics::Pipeline, Graphics::PipelineCreateInfo>;
32
33 /**
34  * @brief PipelineWrapper is the object
35  * returned to the client-side
36  */
37 class PipelineImpl
38 {
39 public:
40   /**
41    * @brief Constructor
42    * @param[in] createInfo valid TextureCreateInfo structure
43    * @param[in] controller Reference to the Controller
44    */
45   PipelineImpl(const Graphics::PipelineCreateInfo& createInfo, Graphics::EglGraphicsController& controller);
46
47   /**
48    * @brief Destructor
49    */
50   ~PipelineImpl();
51
52   /**
53    * @brief Destroys all the low-level resources used by the class
54    */
55   void DestroyResource();
56
57   /**
58    * @brief Initializes low-level resources
59    *
60    * @return Tron success
61    */
62   bool InitializeResource();
63
64   /**
65    * @brief Discards object
66    */
67   void DiscardResource();
68
69   /**
70    * @brief returns GL program id
71    * @return GL program id
72    */
73   [[nodiscard]] uint32_t GetGLProgram() const;
74
75   /**
76    * @brief Binds pipeline
77    *
78    * Binds Pipeline by binding GL program and flushing state.
79    *
80    * If previous pipeline specified, it will be used in order to
81    * avoid redundant state swiches.
82    *
83    * @param[in] prevPipeline previous pipeline
84    */
85   void Bind(GLES::PipelineImpl* prevPipeline);
86
87   /**
88    * Executes state change function if condition met
89    */
90   template<typename FUNC, typename STATE>
91   void ExecuteStateChange(FUNC& func, const STATE* prevPipelineState, const STATE* thisPipelineState)
92   {
93     if(!prevPipelineState)
94     {
95       func();
96     }
97     else
98     {
99       // binary test and execute when different
100       if(memcmp(prevPipelineState, thisPipelineState, sizeof(STATE)) != 0)
101       {
102         func();
103       }
104     }
105   }
106
107   void Retain();
108
109   void Release();
110
111   [[nodiscard]] uint32_t GetRefCount() const;
112
113   [[nodiscard]] const PipelineCreateInfo& GetCreateInfo() const;
114
115   [[nodiscard]] auto& GetController() const;
116
117 private:
118   /**
119    * @brief Helper function. Copies state if pointer is set
120    */
121   template<class T>
122   void CopyStateIfSet(const T* sourceState, T& copyState, T** destState)
123   {
124     *destState = nullptr;
125     if(sourceState)
126     {
127       copyState  = *sourceState;
128       *destState = &copyState;
129     }
130   }
131
132   /**
133    * @brief Helper function. Copies const state if pointer is set
134    */
135   template<class T>
136   void CopyStateIfSet(const T* sourceState, T& copyState, const T** destState)
137   {
138     *destState = nullptr;
139     if(sourceState)
140     {
141       copyState  = *sourceState;
142       *destState = &copyState;
143     }
144   }
145
146   // Pipeline state is stored as a copy of create info
147   // data.
148   struct PipelineState;
149   std::unique_ptr<PipelineState> mPipelineState;
150
151   EglGraphicsController& mController;
152   PipelineCreateInfo     mCreateInfo;
153
154   uint32_t mGlProgram{0u};
155
156   uint32_t mRefCount{0u};
157 };
158
159 /**
160  * @brief Pipeline class wraps a unique pipeline object
161  *
162  */
163 class Pipeline : public Graphics::Pipeline
164 {
165 public:
166   Pipeline() = delete;
167
168   explicit Pipeline(GLES::PipelineImpl& pipeline)
169   : mPipeline(pipeline)
170   {
171     // increase refcount
172     mPipeline.Retain();
173   }
174
175   ~Pipeline() override
176   {
177     // decrease refcount
178     mPipeline.Release();
179   }
180
181   [[nodiscard]] auto& GetPipeline() const
182   {
183     return mPipeline;
184   }
185
186   [[nodiscard]] const PipelineCreateInfo& GetCreateInfo() const;
187
188   [[nodiscard]] EglGraphicsController& GetController() const;
189
190 private:
191   GLES::PipelineImpl& mPipeline;
192 };
193
194 } // namespace Dali::Graphics::GLES
195 #endif