Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-core.git] / dali / integration-api / shader-data.h
1 #ifndef __DALI_INTEGRATION_SHADER_DATA_H__
2 #define __DALI_INTEGRATION_SHADER_DATA_H__
3
4 /*
5  * Copyright (c) 2014 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/vector-wrapper.h>
27 #include <dali/integration-api/resource-declarations.h>
28
29 namespace Dali
30 {
31
32 namespace Integration
33 {
34
35 class ShaderData;
36
37 typedef IntrusivePtr<ShaderData> ShaderDataPtr;
38
39 /**
40  * ShaderData class.
41  * A container for shader source code and compiled binary byte code
42  */
43 class ShaderData : public Dali::RefObject
44 {
45 public:
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)
52   : mShaderHash( 0 ),
53     mVertexShader(vertexSource),
54     mFragmentShader(fragmentSource),
55     mResourceId( 0 )
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     mShaderHash = shaderHash;
77   }
78
79   /**
80    * Get hash value which is created with vertex and fragment shader code
81    * @return shaderHash  hash key created with vertex and fragment shader code
82    */
83   size_t GetHashValue()
84   {
85     return mShaderHash;
86   }
87
88   /**
89    * @return the vertex shader
90    */
91   const char* GetVertexShader()
92   {
93     return mVertexShader.c_str();
94   }
95
96   /**
97    * @return the vertex shader
98    */
99   const char* GetFragmentShader()
100   {
101     return mFragmentShader.c_str();
102   }
103
104   /**
105    * Check whether there is a compiled binary available
106    * @return true if this objects contains a compiled binary
107    */
108   bool HasBinary() const
109   {
110     return 0 != mBuffer.size();
111   }
112
113   /**
114    * Allocate a buffer for the compiled binary bytecode
115    * @param[in] size  The size of the buffer in bytes
116    */
117   void AllocateBuffer( size_t size )
118   {
119     if( size > mBuffer.size() )
120     {
121       mBuffer.resize( size );
122     }
123   }
124
125   /**
126    * Get the program buffer
127    * @return reference to the buffer
128    */
129   size_t GetBufferSize()
130   {
131     return mBuffer.size();
132   }
133
134   /**
135    * Get the data that the buffer points to
136    * @return raw pointer to the buffer data
137    */
138   unsigned char* GetBufferData()
139   {
140     DALI_ASSERT_DEBUG( mBuffer.size() > 0 );
141     return &mBuffer[0];
142   }
143
144   /**
145    * Get the data that the buffer points to
146    * @TODO TO BE REMOVED WHEN PLATFORM ABSTRACTION FOR SHADER LOAD/STORE IS FIXED
147    * @return raw pointer to the buffer data
148    */
149   std::vector<unsigned char>& GetBuffer()
150   {
151     return mBuffer;
152   }
153
154   /**
155    * Set the resource id
156    * @param resourceId
157    */
158   void SetResourceId( ResourceId resourceId )
159   {
160     mResourceId = resourceId;
161   }
162
163   /**
164    * Get the resource id
165    * @return resourceId
166    */
167   ResourceId GetResourceId()
168   {
169     return mResourceId;
170   }
171
172 private: // Not implemented
173
174   ShaderData(const ShaderData& other);            ///< no copying of this object
175   ShaderData& operator= (const ShaderData& rhs);  ///< no copying of this object
176
177 private: // Data
178
179   size_t                      mShaderHash;     ///< hash key created with vertex and fragment shader code
180   std::string                 mVertexShader;   ///< source code for vertex program
181   std::string                 mFragmentShader; ///< source code for fragment program
182   std::vector<unsigned char>  mBuffer;         ///< buffer containing compiled binary bytecode
183   ResourceId                  mResourceId;     ///< resource id
184
185 };
186
187 } // namespace Integration
188
189 } // namespace Dali
190
191 #endif // __DALI_INTEGRATION_SHADER_DATA_H__