2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
18 #include <dali/internal/render/gl-resources/gpu-buffer.h>
21 #include <dali/public-api/common/dali-common.h>
32 * Helper to get our type enum as GL enum
33 * @param type to convert
34 * @return the corresponding GL enum or -1
36 inline GLenum TypeAsGlEnum( GpuBuffer::Target type )
41 case GpuBuffer::ARRAY_BUFFER :
43 retval = GL_ARRAY_BUFFER;
46 case GpuBuffer::ELEMENT_ARRAY_BUFFER :
48 retval = GL_ELEMENT_ARRAY_BUFFER;
51 case GpuBuffer::TRANSFORM_FEEDBACK_BUFFER :
53 retval = GL_TRANSFORM_FEEDBACK_BUFFER;
61 * Helper to get our drawmode enum as GL enum
62 * @param type to convert
63 * @return the corresponding GL enum or -1
65 inline GLenum ModeAsGlEnum( GpuBuffer::Usage type )
70 case GpuBuffer::STREAM_DRAW :
72 retval = GL_STREAM_DRAW;
75 case GpuBuffer::STATIC_DRAW :
77 retval = GL_STATIC_DRAW;
80 case GpuBuffer::DYNAMIC_DRAW :
82 retval = GL_DYNAMIC_DRAW;
91 GpuBuffer::GpuBuffer( Context& context, Target target, Usage usage )
92 : mContext( context ),
98 mBufferCreated( false )
102 GpuBuffer::~GpuBuffer()
104 // If we have a buffer then delete it.
107 // If a buffer object that is currently bound is deleted, the binding reverts to 0
108 // (the absence of any buffer object, which reverts to client memory usage)
109 mContext.DeleteBuffers(1,&mBufferId);
114 * Creates or updates the buffer data depending on whether it
115 * already exists or not.
117 void GpuBuffer::UpdateDataBuffer(GLsizeiptr size,const GLvoid *data)
119 DALI_ASSERT_DEBUG( size > 0 );
121 // make sure we have a buffer name/id before uploading
124 mContext.GenBuffers(1,&mBufferId);
125 DALI_ASSERT_DEBUG(mBufferId);
128 // make sure the buffer is bound, don't perform any checks because size may be zero
129 BindNoChecks(mBufferId);
131 // if the buffer has already been created, just update the data providing it fits
134 // if the data will fit in the existing buffer, just update it
135 if (size <= mCapacity )
137 mContext.BufferSubData( TypeAsGlEnum(mTarget), 0, size, data );
141 // create a new buffer of the larger size,
142 // gl should automatically deallocate the old buffer
143 mContext.BufferData( TypeAsGlEnum(mTarget), size, data, ModeAsGlEnum( mUsage ) );
150 mContext.BufferData( TypeAsGlEnum(mTarget), size, data, ModeAsGlEnum( mUsage ) );
151 mBufferCreated = true;
159 mContext.BindArrayBuffer(0);
162 case ELEMENT_ARRAY_BUFFER:
164 mContext.BindElementArrayBuffer(0);
167 case TRANSFORM_FEEDBACK_BUFFER:
169 mContext.BindTransformFeedbackBuffer(0);
175 void GpuBuffer::Bind() const
177 DALI_ASSERT_DEBUG(mCapacity);
179 BindNoChecks(mBufferId);
182 bool GpuBuffer::BufferIsValid() const
184 return mBufferCreated && (0 != mCapacity );
187 void GpuBuffer::GlContextDestroyed()
189 // If the context is destroyed, GL would have released the buffer.
193 mBufferCreated = false;
196 void GpuBuffer::BindNoChecks(GLuint bufferId) const
198 // context currently only supports two targets, element and array
199 // and it caches both of them (as in it won't bind them if the
200 // buffer id is already bound).
202 if (mTarget == ARRAY_BUFFER)
204 mContext.BindArrayBuffer(bufferId);
208 mContext.BindElementArrayBuffer(bufferId);
213 } // namespace Internal