Merge "DALi Version 1.1.10" into devel/master
[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) 2015 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
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 class ShaderData;
35 typedef IntrusivePtr<ShaderData> ShaderDataPtr;
36
37 /**
38  * ShaderData class.
39  * A container for shader source code and compiled binary byte code.
40  */
41 class ShaderData : public Dali::RefObject
42 {
43 public:
44
45   /**
46    * Constructor
47    * @param[in] vertexSource   Source code for vertex program
48    * @param[in] fragmentSource Source code for fragment program
49    */
50   ShaderData(const std::string& vertexSource, const std::string& fragmentSource)
51   : mShaderHash( -1 ),
52     mVertexShader(vertexSource),
53     mFragmentShader(fragmentSource)
54   { }
55
56 protected:
57   /**
58    * Protected Destructor
59    * A reference counted object may only be deleted by calling Unreference()
60    */
61   virtual ~ShaderData()
62   {
63     // vector releases its data
64   }
65
66 public: // API
67
68   /**
69    * Set hash value which is created with vertex and fragment shader code
70    * @param [in] shaderHash  hash key created with vertex and fragment shader code
71    */
72   void SetHashValue(size_t shaderHash)
73   {
74     DALI_ASSERT_DEBUG( shaderHash != size_t(-1) );
75     mShaderHash = shaderHash;
76   }
77
78   /**
79    * Get hash value which is created with vertex and fragment shader code
80    * @return shaderHash  hash key created with vertex and fragment shader code
81    */
82   size_t GetHashValue() const
83   {
84     DALI_ASSERT_DEBUG( mShaderHash != size_t(-1) );
85     return mShaderHash;
86   }
87
88   /**
89    * @return the vertex shader
90    */
91   const char* GetVertexShader() const
92   {
93     return mVertexShader.c_str();
94   }
95
96   /**
97    * @return the vertex shader
98    */
99   const char* GetFragmentShader() const
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     mBuffer.Resize( size );
120   }
121
122   /**
123    * Get the program buffer
124    * @return reference to the buffer
125    */
126   size_t GetBufferSize() const
127   {
128     return mBuffer.Size();
129   }
130
131   /**
132    * Get the data that the buffer points to
133    * @return raw pointer to the buffer data
134    */
135   unsigned char* GetBufferData()
136   {
137     DALI_ASSERT_DEBUG( mBuffer.Size() > 0 );
138     return &mBuffer[0];
139   }
140
141   /**
142    * Get the data that the buffer points to
143    * @return raw pointer to the buffer data
144    */
145   Dali::Vector<unsigned char>& GetBuffer()
146   {
147     return mBuffer;
148   }
149
150 private: // Not implemented
151
152   ShaderData(const ShaderData& other);            ///< no copying of this object
153   ShaderData& operator= (const ShaderData& rhs);  ///< no copying of this object
154
155 private: // Data
156
157   size_t                      mShaderHash;     ///< hash key created with vertex and fragment shader code
158   std::string                 mVertexShader;   ///< source code for vertex program
159   std::string                 mFragmentShader; ///< source code for fragment program
160   Dali::Vector<unsigned char> mBuffer;         ///< buffer containing compiled binary bytecode
161 };
162
163 } // namespace Integration
164
165 } // namespace Dali
166
167 #endif // __DALI_INTERNAL_SHADER_DATA_H__