Merge remote-tracking branch 'origin/tizen' into devel/new_mesh
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / gpu-buffer.h
1 #ifndef __DALI_INTERNAL_GPU_BUFFER_H__
2 #define __DALI_INTERNAL_GPU_BUFFER_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 // INTERNAL INCLUDES
22 #include <dali/internal/render/gl-resources/context.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 /**
31  * Used to create and update a GPU memory buffer.
32  *
33  * The buffer can be used for storing vertex data, index arrays (indices)
34  * or pixel data (PBO).
35  *
36  * The buffer allows data to be stored in high-performance
37  * graphics memory on the server side and
38  * promotes efficient data transfer.
39  */
40 class GpuBuffer
41 {
42 public:
43
44   /**
45    * Enum to encapsulate the GL buffer type. This is to avoid having to store a whole int for type
46    */
47   enum Target
48   {
49     ARRAY_BUFFER,             ///< GL_ARRAY_BUFFER
50     ELEMENT_ARRAY_BUFFER,     ///< GL_ELEMENT_ARRAY_BUFFER
51     TRANSFORM_FEEDBACK_BUFFER ///< GL_TRANSFORM_FEEDBACK_BUFFER
52   };
53
54   /**
55    * Enum to encapsulate the GL draw mode. This is to avoid having to store a whole int for mode
56    */
57   enum Usage
58   {
59     STREAM_DRAW,  ///< GL_STREAM_DRAW
60     STATIC_DRAW,  ///< GL_STATIC_DRAW
61     DYNAMIC_DRAW, ///< GL_DYNAMIC_DRAW
62   };
63
64 public:
65
66   /**
67    * constructor
68    * @param context drawing context
69    * @param target the type of buffer to create @see Type
70    * @param usage how the buffer will be used @see DrawMode
71    */
72   GpuBuffer( Context& context, Target target, Usage usage );
73
74   /**
75    * Destructor, non virtual as no virtual methods or inheritance
76    */
77   ~GpuBuffer();
78
79   /**
80    *
81    * Creates or updates a buffer object and binds it to the target.
82    * @param size Specifies the size in bytes of the buffer object's new data store.
83    * @param data pointer to the data to load
84    */
85   void UpdateDataBuffer(GLsizeiptr size, const GLvoid *data);
86
87   /**
88    * Bind the buffer object to the target
89    * Will assert if the buffer size is zero
90    */
91   void Bind() const;
92
93   /**
94    * @return true if the GPU buffer is valid, i.e. its created and not empty
95    */
96   bool BufferIsValid() const;
97
98   /**
99    * Get the size of the buffer
100    * @return size
101    */
102   GLsizeiptr GetBufferSize() const
103   {
104     return mSize;
105   }
106
107   void SetStride( GLuint stride )
108   {
109     mStride = stride;
110   }
111
112   GLuint GetStride()
113   {
114     return mStride;
115   }
116
117   /**
118    * Needs to be called when GL context is destroyed
119    */
120   void GlContextDestroyed();
121
122 private:
123
124   /**
125    * Perfoms a bind without checking the size of the buffer
126    * @param bufferId to bind
127    */
128   void BindNoChecks(GLuint bufferId) const;
129
130 private: // Data
131
132   Context&           mContext;             ///< dali drawing context
133   GLsizeiptr         mCapacity;            ///< buffer capacity
134   GLsizeiptr         mSize;                ///< buffer size
135   GLuint             mBufferId;            ///< buffer object name(id)
136   GLuint             mStride;              ///< stride of data in buffer object
137
138   Target             mTarget:2;            ///< type of buffer (array/element), 2 bits are enough
139   Usage              mUsage:2;             ///< how the buffer is used (read, read/write etc), 2 bits are enough
140   bool               mBufferCreated:1;     ///< whether buffer has been created
141
142 };
143
144 } // namespace Internal
145
146 } // namespace Dali
147
148 #endif // __DALI_INTERNAL_GPU_BUFFER_H__