[dali_2.3.27] 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   ~ShaderImpl();
40
41   uint32_t Retain();
42
43   uint32_t Release();
44
45   [[nodiscard]] uint32_t GetRefCount() const;
46
47   /**
48    * Whilst unreferenced, increase the flush count and return it
49    *
50    * @return The new flush count
51    */
52   [[nodiscard]] uint32_t IncreaseFlushCount();
53
54   /**
55    * Get the flush count whilst unreferenced
56    *
57    * @return the flush count
58    */
59   [[nodiscard]] uint32_t GetFlushCount() const;
60
61   /**
62    * @brief Compiles shader
63    *
64    * @return True on success
65    */
66   [[nodiscard]] bool Compile() const;
67
68   /**
69    * @brief Destroys GL shader
70    */
71   void Destroy();
72
73   uint32_t GetGLShader() const;
74
75   [[nodiscard]] const ShaderCreateInfo& GetCreateInfo() const;
76
77   [[nodiscard]] EglGraphicsController& GetController() const;
78
79   /**
80    * Strips legacy prefix fromt he GLSL source code if necessary
81    * @param info valid ShaderCreateInfo strucutre
82    * @param[out] startIndex Start index of the source code
83    * @param[out] finalDataSize Size of trimmed data
84    */
85   static void StripLegacyCodeIfNeeded(const ShaderCreateInfo& info, size_t& startIndex, size_t& finalDataSize);
86
87 private:
88   friend class Shader;
89   struct Impl;
90   std::unique_ptr<Impl> mImpl{nullptr};
91 };
92
93 class Shader : public Graphics::Shader
94 {
95 public:
96   /**
97    * @brief Constructor
98    *
99    * @param[in] impl Pointer to valid implementation
100    */
101   explicit Shader(ShaderImpl* impl)
102   : mShader(impl)
103   {
104     mShader->Retain();
105   }
106
107   /**
108    * @brief Destructor
109    */
110   ~Shader() override;
111
112   [[nodiscard]] ShaderImpl* GetImplementation() const
113   {
114     return mShader;
115   }
116
117   [[nodiscard]] const ShaderCreateInfo& GetCreateInfo() const;
118
119   bool operator==(const GLES::Shader& shader) const
120   {
121     return (shader.mShader == mShader);
122   }
123
124   bool operator==(const GLES::ShaderImpl* shaderImpl) const
125   {
126     return (shaderImpl == mShader);
127   }
128
129   bool operator!=(const GLES::Shader& shader) const
130   {
131     return (shader.mShader != mShader);
132   }
133
134   /**
135    * @brief Called when UniquePtr<> on client-side dies.
136    */
137   void DiscardResource();
138
139   /**
140    * @brief Destroying GL resources
141    *
142    * This function is kept for compatibility with Resource<> class
143    * so can the object can be use with templated functions.
144    */
145   void DestroyResource()
146   {
147     // nothing to do here
148   }
149
150 private:
151   ShaderImpl* mShader{nullptr};
152 };
153
154 } // namespace Dali::Graphics::GLES
155
156 #endif