[Tizen] Not execute the remove callback
[platform/core/uifw/dali-core.git] / dali / internal / common / shader-data.h
1 #ifndef DALI_INTERNAL_SHADER_DATA_H
2 #define DALI_INTERNAL_SHADER_DATA_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 <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/graphics-api/graphics-types.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/object/ref-object.h>
28 #include <dali/public-api/rendering/shader.h> // ShaderHints
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 class ShaderData;
35 using ShaderDataPtr = IntrusivePtr<ShaderData>;
36
37 namespace
38 {
39 static const std::vector<char> emptyShader;
40
41 inline std::vector<char> StringToVector(const std::string& str)
42 {
43   auto retval = std::vector<char>{};
44   retval.insert(retval.begin(), str.begin(), str.end());
45   retval.push_back('\0');
46   return retval;
47 }
48
49 } // namespace
50
51 /**
52  * ShaderData class.
53  * A container for shader source code and compiled binary byte code.
54  */
55 class ShaderData : public Dali::RefObject
56 {
57 public:
58   /**
59    * Constructor
60    * @param[in] vertexSource   Source code for vertex program
61    * @param[in] fragmentSource Source code for fragment program
62    * @param[in] hints          Hints for rendering
63    */
64   ShaderData(std::string vertexSource, std::string fragmentSource, const Dali::Shader::Hint::Value hints)
65   : mShaderHash(-1),
66     mVertexShader(StringToVector(vertexSource)),
67     mFragmentShader(StringToVector(fragmentSource)),
68     mHints(hints),
69     mSourceMode(Graphics::ShaderSourceMode::TEXT)
70   {
71   }
72
73   /**
74    * Creates a shader data containing binary content
75    * @param[in] vertexSource   Source code for vertex program
76    * @param[in] fragmentSource Source code for fragment program
77    * @param[in] hints          Hints for rendering
78    */
79   ShaderData(std::vector<char>& vertexSource, std::vector<char>& fragmentSource, const Dali::Shader::Hint::Value hints)
80   : mShaderHash(-1),
81     mVertexShader(vertexSource),
82     mFragmentShader(fragmentSource),
83     mHints(hints),
84     mSourceMode(Graphics::ShaderSourceMode::BINARY)
85   {
86   }
87
88 protected:
89   /**
90    * Protected Destructor
91    * A reference counted object may only be deleted by calling Unreference()
92    */
93   ~ShaderData() override
94   {
95     // vector releases its data
96   }
97
98 public: // API
99   /**
100    * Set hash value which is created with vertex and fragment shader code
101    * @param [in] shaderHash  hash key created with vertex and fragment shader code
102    */
103   void SetHashValue(size_t shaderHash)
104   {
105     DALI_ASSERT_DEBUG(shaderHash != size_t(-1));
106     mShaderHash = shaderHash;
107   }
108
109   /**
110    * Get hash value which is created with vertex and fragment shader code
111    * @return shaderHash  hash key created with vertex and fragment shader code
112    */
113   size_t GetHashValue() const
114   {
115     DALI_ASSERT_DEBUG(mShaderHash != size_t(-1));
116     return mShaderHash;
117   }
118
119   /**
120    * @return the vertex shader
121    */
122   const char* GetVertexShader() const
123   {
124     return &mVertexShader[0];
125   }
126
127   /**
128    * @return the vertex shader
129    */
130   const char* GetFragmentShader() const
131   {
132     return &mFragmentShader[0];
133   }
134
135   /**
136    * Returns a std::vector containing the shader code associated with particular pipeline stage
137    * @param[in] the graphics pipeline stage
138    * @return the shader code
139    */
140   const std::vector<char>& GetShaderForPipelineStage(Graphics::PipelineStage stage) const
141   {
142     if(stage == Graphics::PipelineStage::VERTEX_SHADER)
143     {
144       return mVertexShader;
145     }
146     else if(stage == Graphics::PipelineStage::FRAGMENT_SHADER)
147     {
148       return mFragmentShader;
149     }
150     else
151     {
152       //      DALI_LOG_ERROR("Unsupported shader stage\n");
153       return emptyShader;
154     }
155   }
156
157   /**
158    * @return the hints
159    */
160   Dali::Shader::Hint::Value GetHints() const
161   {
162     return mHints;
163   }
164   /**
165    * Check whether there is a compiled binary available
166    * @return true if this objects contains a compiled binary
167    */
168   bool HasBinary() const
169   {
170     return 0 != mBuffer.Size();
171   }
172
173   /**
174    * Allocate a buffer for the compiled binary bytecode
175    * @param[in] size  The size of the buffer in bytes
176    */
177   void AllocateBuffer(std::size_t size)
178   {
179     mBuffer.Resize(size);
180   }
181
182   /**
183    * Get the program buffer
184    * @return reference to the buffer
185    */
186   std::size_t GetBufferSize() const
187   {
188     return mBuffer.Size();
189   }
190
191   /**
192    * Get the data that the buffer points to
193    * @return raw pointer to the buffer data
194    */
195   uint8_t* GetBufferData()
196   {
197     DALI_ASSERT_DEBUG(mBuffer.Size() > 0);
198     return &mBuffer[0];
199   }
200
201   /**
202    * Get the data that the buffer points to
203    * @return raw pointer to the buffer data
204    */
205   Dali::Vector<uint8_t>& GetBuffer()
206   {
207     return mBuffer;
208   }
209
210   /**
211    * Get the source mode of shader data
212    * @return the source mode of shader data ( text or binary )
213    */
214   Graphics::ShaderSourceMode GetSourceMode() const
215   {
216     return mSourceMode;
217   }
218
219 private:                                        // Not implemented
220   ShaderData(const ShaderData& other);          ///< no copying of this object
221   ShaderData& operator=(const ShaderData& rhs); ///< no copying of this object
222
223 private:                                      // Data
224   std::size_t                mShaderHash;     ///< hash key created with vertex and fragment shader code
225   std::vector<char>          mVertexShader;   ///< source code for vertex program
226   std::vector<char>          mFragmentShader; ///< source code for fragment program
227   Dali::Shader::Hint::Value  mHints;          ///< take a hint
228   Dali::Vector<uint8_t>      mBuffer;         ///< buffer containing compiled binary bytecode
229   Graphics::ShaderSourceMode mSourceMode;     ///< Source mode of shader data ( text or binary )
230 };
231
232 } // namespace Internal
233
234 } // namespace Dali
235
236 #endif // DALI_INTERNAL_SHADER_DATA_H