[dali_2.3.37] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-program.h
1 #ifndef DALI_GRAPHICS_GLES_PROGRAM_H
2 #define DALI_GRAPHICS_GLES_PROGRAM_H
3
4 /*
5  * Copyright (c) 2024 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 // EXTERNAL INCLUDES
21 #include <dali/graphics-api/graphics-program-create-info.h>
22 #include <dali/graphics-api/graphics-program.h>
23
24 // INTERNAL INCLUDES
25 #include "gles-graphics-resource.h"
26
27 namespace Dali::Graphics::GLES
28 {
29 class Reflection;
30
31 /**
32  * @brief Program implementation
33  *
34  * Program implementation is owned only by the PipelineCache
35  *
36  * Like pipeline, it's created and managed by the PipelineCache
37  */
38 class ProgramImpl
39 {
40 public:
41   /**
42    * @brief Constructor
43    *
44    * @param[in] createInfo  Valid create info structure
45    * @param[in] controller Valid reference to the controller object
46    */
47   ProgramImpl(const Graphics::ProgramCreateInfo& createInfo, Graphics::EglGraphicsController& controller);
48
49   /**
50    * @brief Destructor
51    */
52   ~ProgramImpl();
53
54   /**
55    * @brief Destroys GL resources associated with the Program
56    *
57    * @return True on success
58    */
59   bool Destroy();
60
61   /**
62    * @brief Creates GL resource for this Program
63    *
64    * @return True on success
65    */
66   bool Create();
67
68   /**
69    * @brief Preprocesses shaders
70    */
71   void Preprocess();
72
73   /**
74    * @brief Returns GL program id
75    *
76    * @return GL program id
77    */
78   [[nodiscard]] uint32_t GetGlProgram() const;
79
80   /**
81    * @brief Increases ref count
82    *
83    * @return new refcount
84    */
85   uint32_t Retain();
86
87   /**
88    * @brief Decreases ref count
89    *
90    * @return New refcount
91    */
92   uint32_t Release();
93
94   /**
95    * @brief Retrieves ref count
96    * @return Refcount value
97    */
98   [[nodiscard]] uint32_t GetRefCount() const;
99
100   /**
101    * @brief Returns reflection
102    *
103    * @return Valid reflection associated with the Program
104    */
105   [[nodiscard]] const GLES::Reflection& GetReflection() const;
106
107   /**
108    * @brief Returns controller
109    *
110    * @return Valid Controller object
111    */
112   [[nodiscard]] EglGraphicsController& GetController() const;
113
114   /**
115    * @brief Returns create info structure
116    *
117    * @return Reference to valid create info structure
118    */
119   [[nodiscard]] const ProgramCreateInfo& GetCreateInfo() const;
120
121   /**
122    * @brief Returns parameter value specified by parameterId
123    *
124    * This function can be used as a backdoor into the implementation
125    * used to retrieve internal data.
126    *
127    * @param[in] parameterId Integer parameter id
128    * @param[out] out Pointer to write to
129    *
130    * @return True on success
131    */
132   bool GetParameter(uint32_t parameterId, void* out);
133
134   /**
135    * @brief Updates standalone uniforms
136    *
137    * Updates standalone uniforms (issues the GL calls) and
138    * updates internal uniform cache
139    *
140    * @param[in] ptr Valid pointer to the uniform block memory
141    */
142   void UpdateStandaloneUniformBlock(const char* ptr);
143
144   /**
145    * @brief Builds standalone uniform cache
146    *
147    * This function allocates cache memory and
148    * gathers a list of GL functions per uniform type.
149    */
150   void BuildStandaloneUniformCache();
151
152 private:
153   friend class Program;
154
155   struct Impl;
156   std::unique_ptr<Impl> mImpl;
157 };
158
159 ///////////////////////////////////////////////////////////////
160
161 /**
162  * @brief Wrapper for the program implementation
163  *
164  * This object is returned back to the client-side
165  */
166 class Program : public Graphics::Program
167 {
168 public:
169   /**
170    * @brief Constructor
171    *
172    * @param[in] impl Pointer to valid implementation
173    */
174   explicit Program(ProgramImpl* impl)
175   : mProgram(impl)
176   {
177     mProgram->Retain();
178   }
179
180   /**
181    * @brief Destructor
182    */
183   ~Program() override;
184
185   /**
186    * @brief Returns reference to the Reflection object
187
188    * @return Reflection
189    */
190   [[nodiscard]] const GLES::Reflection& GetReflection() const;
191
192   /**
193    * @brief Retrieves internal program implementation
194    *
195    * @return Valid pointer to the ProgramImpl object
196    */
197   [[nodiscard]] ProgramImpl* GetImplementation() const
198   {
199     return mProgram;
200   }
201
202   /**
203    * @brief Returns controller
204    *
205    * @return controller
206    */
207   [[nodiscard]] EglGraphicsController& GetController() const;
208
209   /**
210    * @brief Returns create info structure
211    *
212    * @return create info structure
213    */
214   [[nodiscard]] const ProgramCreateInfo& GetCreateInfo() const;
215
216   bool operator==(const GLES::Program& program) const
217   {
218     return (program.mProgram == mProgram);
219   }
220
221   bool operator==(const GLES::ProgramImpl* programImpl) const
222   {
223     return (programImpl == mProgram);
224   }
225
226   bool operator!=(const GLES::Program& program) const
227   {
228     return (program.mProgram != mProgram);
229   }
230
231   /**
232    * @brief Run by UniquePtr to discard resource
233    */
234   void DiscardResource();
235
236   /**
237    * @brief Destroying GL resources
238    *
239    * This function is kept for compatibility with Resource<> class
240    * so can the object can be use with templated functions.
241    */
242   void DestroyResource()
243   {
244     // nothing to do here
245   }
246
247 private:
248   ProgramImpl* mProgram{nullptr};
249 };
250 } // namespace Dali::Graphics::GLES
251
252 #endif //DALI_GRAPHICS_GLES_PROGRAM_H