2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/render/gl-resources/gpu-buffer.h>
22 #include <dali/public-api/common/dali-common.h>
33 * Helper to get our type enum as GL enum
34 * @param type to convert
35 * @return the corresponding GL enum or -1
37 inline GLenum TypeAsGlEnum( GpuBuffer::Target type )
42 case GpuBuffer::ARRAY_BUFFER :
44 retval = GL_ARRAY_BUFFER;
47 case GpuBuffer::ELEMENT_ARRAY_BUFFER :
49 retval = GL_ELEMENT_ARRAY_BUFFER;
52 case GpuBuffer::TRANSFORM_FEEDBACK_BUFFER :
54 retval = GL_TRANSFORM_FEEDBACK_BUFFER;
62 * Helper to get our drawmode enum as GL enum
63 * @param type to convert
64 * @return the corresponding GL enum or -1
66 inline GLenum ModeAsGlEnum( GpuBuffer::Usage type )
71 case GpuBuffer::STREAM_DRAW :
73 retval = GL_STREAM_DRAW;
76 case GpuBuffer::STATIC_DRAW :
78 retval = GL_STATIC_DRAW;
81 case GpuBuffer::DYNAMIC_DRAW :
83 retval = GL_DYNAMIC_DRAW;
92 GpuBuffer::GpuBuffer( Context& context, Target target, Usage usage )
93 : mContext( context ),
99 mBufferCreated( false )
103 GpuBuffer::~GpuBuffer()
105 // If we have a buffer then delete it.
108 // If a buffer object that is currently bound is deleted, the binding reverts to 0
109 // (the absence of any buffer object, which reverts to client memory usage)
110 mContext.DeleteBuffers(1,&mBufferId);
115 * Creates or updates the buffer data depending on whether it
116 * already exists or not.
118 void GpuBuffer::UpdateDataBuffer(GLsizeiptr size,const GLvoid *data)
120 DALI_ASSERT_DEBUG( size > 0 );
122 // make sure we have a buffer name/id before uploading
125 mContext.GenBuffers(1,&mBufferId);
126 DALI_ASSERT_DEBUG(mBufferId);
129 // make sure the buffer is bound, don't perform any checks because size may be zero
130 BindNoChecks(mBufferId);
132 // if the buffer has already been created, just update the data providing it fits
135 // if the data will fit in the existing buffer, just update it
136 if (size <= mCapacity )
138 mContext.BufferSubData( TypeAsGlEnum(mTarget), 0, size, data );
142 // create a new buffer of the larger size,
143 // gl should automatically deallocate the old buffer
144 mContext.BufferData( TypeAsGlEnum(mTarget), size, data, ModeAsGlEnum( mUsage ) );
151 mContext.BufferData( TypeAsGlEnum(mTarget), size, data, ModeAsGlEnum( mUsage ) );
152 mBufferCreated = true;
160 mContext.BindArrayBuffer(0);
163 case ELEMENT_ARRAY_BUFFER:
165 mContext.BindElementArrayBuffer(0);
168 case TRANSFORM_FEEDBACK_BUFFER:
170 mContext.BindTransformFeedbackBuffer(0);
176 void GpuBuffer::Bind() const
178 DALI_ASSERT_DEBUG(mCapacity);
180 BindNoChecks(mBufferId);
183 bool GpuBuffer::BufferIsValid() const
185 return mBufferCreated && (0 != mCapacity );
188 void GpuBuffer::GlContextDestroyed()
190 // If the context is destroyed, GL would have released the buffer.
194 mBufferCreated = false;
197 void GpuBuffer::BindNoChecks(GLuint bufferId) const
199 // context currently only supports two targets, element and array
200 // and it caches both of them (as in it won't bind them if the
201 // buffer id is already bound).
203 if (mTarget == ARRAY_BUFFER)
205 mContext.BindArrayBuffer(bufferId);
209 mContext.BindElementArrayBuffer(bufferId);
214 } // namespace Internal