Merge "Added shader support to pipeline cache" into 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) 2023 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 Returns GL program id
70    *
71    * @return GL program id
72    */
73   [[nodiscard]] uint32_t GetGlProgram() const;
74
75   /**
76    * @brief Increases ref count
77    *
78    * @return new refcount
79    */
80   uint32_t Retain();
81
82   /**
83    * @brief Decreases ref count
84    *
85    * @return New refcount
86    */
87   uint32_t Release();
88
89   /**
90    * @brief Retrieves ref count
91    * @return Refcount value
92    */
93   [[nodiscard]] uint32_t GetRefCount() const;
94
95   /**
96    * @brief Returns reflection
97    *
98    * @return Valid reflection associated with the Program
99    */
100   [[nodiscard]] const GLES::Reflection& GetReflection() const;
101
102   /**
103    * @brief Returns controller
104    *
105    * @return Valid Controller object
106    */
107   [[nodiscard]] EglGraphicsController& GetController() const;
108
109   /**
110    * @brief Returns create info structure
111    *
112    * @return Reference to valid create info structure
113    */
114   [[nodiscard]] const ProgramCreateInfo& GetCreateInfo() const;
115
116   /**
117    * @brief Returns parameter value specified by parameterId
118    *
119    * This function can be used as a backdoor into the implementation
120    * used to retrieve internal data.
121    *
122    * @param[in] parameterId Integer parameter id
123    * @param[out] out Pointer to write to
124    *
125    * @return True on success
126    */
127   bool GetParameter(uint32_t parameterId, void* out);
128
129   /**
130    * @brief Updates standalone uniforms
131    *
132    * Updates standalone uniforms (issues the GL calls) and
133    * updates internal uniform cache
134    *
135    * @param[in] ptr Valid pointer to the uniform block memory
136    */
137   void UpdateStandaloneUniformBlock(const char* ptr);
138
139   /**
140    * @brief Builds standalone uniform cache
141    *
142    * This function allocates cache memory and
143    * gathers a list of GL functions per uniform type.
144    */
145   void BuildStandaloneUniformCache();
146
147 private:
148   friend class Program;
149
150   struct Impl;
151   std::unique_ptr<Impl> mImpl;
152 };
153
154 ///////////////////////////////////////////////////////////////
155
156 /**
157  * @brief Wrapper for the program implementation
158  *
159  * This object is returned back to the client-side
160  */
161 class Program : public Graphics::Program
162 {
163 public:
164   /**
165    * @brief Constructor
166    *
167    * @param[in] impl Pointer to valid implementation
168    */
169   explicit Program(ProgramImpl* impl)
170   : mProgram(impl)
171   {
172     mProgram->Retain();
173   }
174
175   /**
176    * @brief Destructor
177    */
178   ~Program() override;
179
180   /**
181    * @brief Returns reference to the Reflection object
182
183    * @return Reflection
184    */
185   [[nodiscard]] const GLES::Reflection& GetReflection() const;
186
187   /**
188    * @brief Retrieves internal program implementation
189    *
190    * @return Valid pointer to the ProgramImpl object
191    */
192   [[nodiscard]] ProgramImpl* GetImplementation() const
193   {
194     return mProgram;
195   }
196
197   /**
198    * @brief Returns controller
199    *
200    * @return controller
201    */
202   [[nodiscard]] EglGraphicsController& GetController() const;
203
204   /**
205    * @brief Returns create info structure
206    *
207    * @return create info structure
208    */
209   [[nodiscard]] const ProgramCreateInfo& GetCreateInfo() const;
210
211   bool operator==(const GLES::Program& program) const
212   {
213     return (program.mProgram == mProgram);
214   }
215
216   bool operator==(const GLES::ProgramImpl* programImpl) const
217   {
218     return (programImpl == mProgram);
219   }
220
221   bool operator!=(const GLES::Program& program) const
222   {
223     return (program.mProgram != mProgram);
224   }
225
226   /**
227    * @brief Run by UniquePtr to discard resource
228    */
229   void DiscardResource();
230
231   /**
232    * @brief Destroying GL resources
233    *
234    * This function is kept for compatibility with Resource<> class
235    * so can the object can be use with templated functions.
236    */
237   void DestroyResource()
238   {
239     // nothing to do here
240   }
241
242 private:
243   ProgramImpl* mProgram{nullptr};
244 };
245 } // namespace Dali::Graphics::GLES
246
247 #endif //DALI_GRAPHICS_GLES_PROGRAM_H