[dali_2.3.32] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-shader.h
1 #ifndef DALI_GRAPHICS_GLES_SHADER_H
2 #define DALI_GRAPHICS_GLES_SHADER_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
21 // EXTERNAL INCLUDES
22 #include <dali/graphics-api/graphics-shader-create-info.h>
23 #include <dali/graphics-api/graphics-shader.h>
24
25 // INTERNAL INCLUDES
26 #include "gles-graphics-resource.h"
27
28 namespace Dali::Graphics::GLES
29 {
30 class ShaderImpl
31 {
32 public:
33   /**
34    * @brief Constructor
35    * @param[in] createInfo Valid createInfo structure
36    * @param[in] controller Reference to the controller
37    */
38   ShaderImpl(const Graphics::ShaderCreateInfo& createInfo, Graphics::EglGraphicsController& controller);
39
40   /**
41    * @brief destructor
42    */
43   ~ShaderImpl();
44
45   /**
46    * @brief Increases ref count
47    * @return ref count after increment
48    */
49   uint32_t Retain();
50
51   /**
52    * @brief Decreases refcount
53    * @return ref count after decrement
54    */
55   uint32_t Release();
56
57   /**
58    * @brief returns current ref count
59    * @return current ref count
60    */
61   [[nodiscard]] uint32_t GetRefCount() const;
62
63   /**
64    * Whilst unreferenced, increase the flush count and return it
65    *
66    * @return The new flush count
67    */
68   [[nodiscard]] uint32_t IncreaseFlushCount();
69
70   /**
71    * Get the flush count whilst unreferenced
72    *
73    * @return the flush count
74    */
75   [[nodiscard]] uint32_t GetFlushCount() const;
76
77   /**
78    * @brief Compiles shader
79    *
80    * @return True on success
81    */
82   [[nodiscard]] bool Compile() const;
83
84   /**
85    * @brief Destroys GL shader
86    */
87   void Destroy();
88
89   /**
90    * @brief Returns GL resource
91    * @return Valid GL shader resource
92    */
93   uint32_t GetGLShader() const;
94
95   /**
96    * @brief Returns create info structure
97    * @return Returns valid create info structure
98    */
99   [[nodiscard]] const ShaderCreateInfo& GetCreateInfo() const;
100
101   /**
102    * @brief Returns reference to the graphics controller
103    * @return Valid reference to the graphics controller
104    */
105   [[nodiscard]] EglGraphicsController& GetController() const;
106
107   /**
108    * Strips legacy prefix fromt he GLSL source code if necessary
109    * @param info valid ShaderCreateInfo strucutre
110    * @param[out] startIndex Start index of the source code
111    * @param[out] glslVersion Detected GLSL version of legacy shader
112    * @param[out] finalDataSize Size of trimmed data
113    */
114   static void StripLegacyCodeIfNeeded(const ShaderCreateInfo& info, size_t& startIndex, uint32_t& glslVersion, size_t& finalDataSize);
115
116   /**
117    * @brief Sets preprocess code
118    * @param[in] data Valid pointer to the new source code
119    * @param[in] size Size of the source code
120    */
121   void SetPreprocessedCode(void* data, uint32_t size);
122
123   /**
124    * @brief Returns GLSL version
125    * @return Returns valid GLSL version or 0 if undefined
126    */
127   [[nodiscard]] uint32_t GetGLSLVersion() const;
128
129 private:
130   friend class Shader;
131   struct Impl;
132   std::unique_ptr<Impl> mImpl{nullptr};
133 };
134
135 class Shader : public Graphics::Shader
136 {
137 public:
138   /**
139    * @brief Constructor
140    *
141    * @param[in] impl Pointer to valid implementation
142    */
143   explicit Shader(ShaderImpl* impl)
144   : mShader(impl)
145   {
146     mShader->Retain();
147   }
148
149   /**
150    * @brief Destructor
151    */
152   ~Shader() override;
153
154   [[nodiscard]] ShaderImpl* GetImplementation() const
155   {
156     return mShader;
157   }
158
159   [[nodiscard]] const ShaderCreateInfo& GetCreateInfo() const;
160
161   bool operator==(const GLES::Shader& shader) const
162   {
163     return (shader.mShader == mShader);
164   }
165
166   bool operator==(const GLES::ShaderImpl* shaderImpl) const
167   {
168     return (shaderImpl == mShader);
169   }
170
171   bool operator!=(const GLES::Shader& shader) const
172   {
173     return (shader.mShader != mShader);
174   }
175
176   /**
177    * @brief Called when UniquePtr<> on client-side dies.
178    */
179   void DiscardResource();
180
181   /**
182    * @brief Destroying GL resources
183    *
184    * This function is kept for compatibility with Resource<> class
185    * so can the object can be use with templated functions.
186    */
187   void DestroyResource()
188   {
189     // nothing to do here
190   }
191
192   [[nodiscard]] uint32_t GetGLSLVersion() const;
193
194 private:
195   ShaderImpl* mShader{nullptr};
196 };
197
198 } // namespace Dali::Graphics::GLES
199
200 #endif