Remove scene graph property buffer
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / gpu-buffer.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/render/gl-resources/gpu-buffer.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-common.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 namespace
31 {
32 /**
33  * Helper to get our type enum as GL enum
34  * @param type to convert
35  * @return the corresponding GL enum or -1
36  */
37 inline GLenum TypeAsGlEnum( GpuBuffer::Target type )
38 {
39   GLenum retval( -1 );
40   switch( type )
41   {
42     case GpuBuffer::ARRAY_BUFFER :
43     {
44       retval = GL_ARRAY_BUFFER;
45       break;
46     }
47     case GpuBuffer::ELEMENT_ARRAY_BUFFER :
48     {
49       retval = GL_ELEMENT_ARRAY_BUFFER;
50       break;
51     }
52     case GpuBuffer::TRANSFORM_FEEDBACK_BUFFER :
53     {
54       retval = GL_TRANSFORM_FEEDBACK_BUFFER;
55       break;
56     }
57   }
58   return retval;
59 }
60
61 /**
62  * Helper to get our drawmode enum as GL enum
63  * @param type to convert
64  * @return the corresponding GL enum or -1
65  */
66 inline GLenum ModeAsGlEnum( GpuBuffer::Usage type )
67 {
68   GLenum retval( -1 );
69   switch( type )
70   {
71     case GpuBuffer::STREAM_DRAW :
72     {
73       retval = GL_STREAM_DRAW;
74       break;
75     }
76     case GpuBuffer::STATIC_DRAW :
77     {
78       retval = GL_STATIC_DRAW;
79       break;
80     }
81     case GpuBuffer::DYNAMIC_DRAW :
82     {
83       retval = GL_DYNAMIC_DRAW;
84       break;
85     }
86   }
87   return retval;
88 }
89
90 }
91
92 GpuBuffer::GpuBuffer( Context& context )
93 : mContext( context ),
94   mCapacity( 0 ),
95   mSize( 0 ),
96   mBufferId( 0 ),
97   mBufferCreated( false )
98 {
99 }
100
101 GpuBuffer::~GpuBuffer()
102 {
103   // If we have a buffer then delete it.
104   if (mBufferId)
105   {
106     // If a buffer object that is currently bound is deleted, the binding reverts to 0
107     // (the absence of any buffer object, which reverts to client memory usage)
108     mContext.DeleteBuffers(1,&mBufferId);
109   }
110 }
111
112 /*
113  * Creates or updates the buffer data depending on whether it
114  * already exists or not.
115  */
116 void GpuBuffer::UpdateDataBuffer(GLsizeiptr size,const GLvoid *data, Usage usage)
117 {
118   DALI_ASSERT_DEBUG( size > 0 );
119   mSize = size;
120   // make sure we have a buffer name/id before uploading
121   if (mBufferId == 0)
122   {
123     mContext.GenBuffers(1,&mBufferId);
124     DALI_ASSERT_DEBUG(mBufferId);
125   }
126
127   // make sure the buffer is bound, don't perform any checks because size may be zero
128   mContext.BindArrayBuffer( mBufferId );
129
130   // if the buffer has already been created, just update the data providing it fits
131   if (mBufferCreated )
132   {
133     // if the data will fit in the existing buffer, just update it
134     if (size <= mCapacity )
135     {
136       mContext.BufferSubData( GL_ARRAY_BUFFER, 0, size, data );
137     }
138     else
139     {
140       // create a new buffer of the larger size,
141       // gl should automatically deallocate the old buffer
142       mContext.BufferData( GL_ARRAY_BUFFER, size, data, ModeAsGlEnum( usage ) );
143       mCapacity = size;
144     }
145   }
146   else
147   {
148     // create the buffer
149     mContext.BufferData( GL_ARRAY_BUFFER, size, data, ModeAsGlEnum( usage ) );
150     mBufferCreated = true;
151     mCapacity = size;
152   }
153
154   mContext.BindArrayBuffer(0);
155 }
156
157 void GpuBuffer::Bind(Target target) const
158 {
159   DALI_ASSERT_DEBUG(mCapacity);
160
161   if (target == ARRAY_BUFFER)
162   {
163     mContext.BindArrayBuffer(mBufferId);
164   }
165   else
166   {
167     mContext.BindElementArrayBuffer(mBufferId);
168   }
169 }
170
171 bool GpuBuffer::BufferIsValid() const
172 {
173   return mBufferCreated && (0 != mCapacity );
174 }
175
176 void GpuBuffer::GlContextDestroyed()
177 {
178   // If the context is destroyed, GL would have released the buffer.
179   mCapacity = 0;
180   mSize = 0;
181   mBufferId = 0;
182   mBufferCreated = false;
183 }
184
185 } // namespace Internal
186
187 } //namespace Dali