5002b3ed66a43f51ef0f5cb174d091a3829c7cb4
[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) 2019 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/public-api/object/ref-object.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/rendering/shader.h> // ShaderHints
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 class ShaderData;
36 typedef IntrusivePtr<ShaderData> ShaderDataPtr;
37
38 /**
39  * ShaderData class.
40  * A container for shader source code and compiled binary byte code.
41  */
42 class ShaderData : public Dali::RefObject
43 {
44 public:
45
46   /**
47    * Constructor
48    * @param[in] vertexSource   Source code for vertex program
49    * @param[in] fragmentSource Source code for fragment program
50    */
51   ShaderData(const std::string& vertexSource, const std::string& fragmentSource, const Dali::Shader::Hint::Value hints)
52   : mShaderHash( -1 ),
53     mVertexShader(vertexSource),
54     mFragmentShader(fragmentSource),
55     mHints(hints)
56   { }
57
58 protected:
59   /**
60    * Protected Destructor
61    * A reference counted object may only be deleted by calling Unreference()
62    */
63   virtual ~ShaderData()
64   {
65     // vector releases its data
66   }
67
68 public: // API
69
70   /**
71    * Set hash value which is created with vertex and fragment shader code
72    * @param [in] shaderHash  hash key created with vertex and fragment shader code
73    */
74   void SetHashValue(size_t shaderHash)
75   {
76     DALI_ASSERT_DEBUG( shaderHash != size_t(-1) );
77     mShaderHash = shaderHash;
78   }
79
80   /**
81    * Get hash value which is created with vertex and fragment shader code
82    * @return shaderHash  hash key created with vertex and fragment shader code
83    */
84   size_t GetHashValue() const
85   {
86     DALI_ASSERT_DEBUG( mShaderHash != size_t(-1) );
87     return mShaderHash;
88   }
89
90   /**
91    * @return the vertex shader
92    */
93   const char* GetVertexShader() const
94   {
95     return mVertexShader.c_str();
96   }
97
98   /**
99    * @return the vertex shader
100    */
101   const char* GetFragmentShader() const
102   {
103     return mFragmentShader.c_str();
104   }
105
106   /**
107    * @return the hints
108    */
109   Dali::Shader::Hint::Value GetHints() const
110   {
111     return mHints;
112   }
113   /**
114    * Check whether there is a compiled binary available
115    * @return true if this objects contains a compiled binary
116    */
117   bool HasBinary() const
118   {
119     return 0 != mBuffer.Size();
120   }
121
122   /**
123    * Allocate a buffer for the compiled binary bytecode
124    * @param[in] size  The size of the buffer in bytes
125    */
126   void AllocateBuffer( std::size_t size )
127   {
128     mBuffer.Resize( size );
129   }
130
131   /**
132    * Get the program buffer
133    * @return reference to the buffer
134    */
135   std::size_t GetBufferSize() const
136   {
137     return mBuffer.Size();
138   }
139
140   /**
141    * Get the data that the buffer points to
142    * @return raw pointer to the buffer data
143    */
144   uint8_t* GetBufferData()
145   {
146     DALI_ASSERT_DEBUG( mBuffer.Size() > 0 );
147     return &mBuffer[0];
148   }
149
150   /**
151    * Get the data that the buffer points to
152    * @return raw pointer to the buffer data
153    */
154   Dali::Vector<uint8_t>& GetBuffer()
155   {
156     return mBuffer;
157   }
158
159 private: // Not implemented
160
161   ShaderData(const ShaderData& other);            ///< no copying of this object
162   ShaderData& operator= (const ShaderData& rhs);  ///< no copying of this object
163
164 private: // Data
165
166   std::size_t               mShaderHash;     ///< hash key created with vertex and fragment shader code
167   std::string               mVertexShader;   ///< source code for vertex program
168   std::string               mFragmentShader; ///< source code for fragment program
169   Dali::Shader::Hint::Value mHints;          ///< take a hint
170   Dali::Vector<uint8_t>     mBuffer;         ///< buffer containing compiled binary bytecode
171
172 };
173
174 } // namespace Integration
175
176 } // namespace Dali
177
178 #endif // DALI_INTERNAL_SHADER_DATA_H