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>
34 * Helper to get our drawmode enum as GL enum
35 * @param type to convert
36 * @return the corresponding GL enum or -1
38 inline GLenum ModeAsGlEnum( GpuBuffer::Usage type )
43 case GpuBuffer::STREAM_DRAW :
45 retval = GL_STREAM_DRAW;
48 case GpuBuffer::STATIC_DRAW :
50 retval = GL_STATIC_DRAW;
53 case GpuBuffer::DYNAMIC_DRAW :
55 retval = GL_DYNAMIC_DRAW;
64 GpuBuffer::GpuBuffer( Context& context )
65 : mContext( context ),
69 mBufferCreated( false )
73 GpuBuffer::~GpuBuffer()
75 // If we have a buffer then delete it.
78 // If a buffer object that is currently bound is deleted, the binding reverts to 0
79 // (the absence of any buffer object, which reverts to client memory usage)
80 mContext.DeleteBuffers(1,&mBufferId);
85 * Creates or updates the buffer data depending on whether it
86 * already exists or not.
88 void GpuBuffer::UpdateDataBuffer(GLsizeiptr size, const GLvoid *data, Usage usage, Target target)
90 DALI_ASSERT_DEBUG( size > 0 );
92 // make sure we have a buffer name/id before uploading
95 mContext.GenBuffers(1,&mBufferId);
96 DALI_ASSERT_DEBUG(mBufferId);
99 GLenum glTargetEnum = GL_ARRAY_BUFFER;
101 if(ELEMENT_ARRAY_BUFFER == target)
103 glTargetEnum = GL_ELEMENT_ARRAY_BUFFER;
105 else if(TRANSFORM_FEEDBACK_BUFFER == target)
107 glTargetEnum = GL_TRANSFORM_FEEDBACK_BUFFER;
110 // make sure the buffer is bound, don't perform any checks because size may be zero
111 if(ARRAY_BUFFER == target)
113 mContext.BindArrayBuffer( mBufferId );
115 else if(ELEMENT_ARRAY_BUFFER == target)
117 mContext.BindElementArrayBuffer( mBufferId );
119 else if(TRANSFORM_FEEDBACK_BUFFER == target)
121 mContext.BindTransformFeedbackBuffer( mBufferId );
124 // if the buffer has already been created, just update the data providing it fits
127 // if the data will fit in the existing buffer, just update it
128 if (size <= mCapacity )
130 mContext.BufferSubData( glTargetEnum, 0, size, data );
134 // create a new buffer of the larger size,
135 // gl should automatically deallocate the old buffer
136 mContext.BufferData( glTargetEnum, size, data, ModeAsGlEnum( usage ) );
143 mContext.BufferData( glTargetEnum, size, data, ModeAsGlEnum( usage ) );
144 mBufferCreated = true;
148 if(ARRAY_BUFFER == target)
150 mContext.BindArrayBuffer( 0 );
152 else if(ELEMENT_ARRAY_BUFFER == target)
154 mContext.BindElementArrayBuffer( 0 );
156 else if(TRANSFORM_FEEDBACK_BUFFER == target)
158 mContext.BindTransformFeedbackBuffer( 0 );
162 void GpuBuffer::Bind(Target target) const
164 DALI_ASSERT_DEBUG(mCapacity);
166 if (target == ARRAY_BUFFER)
168 mContext.BindArrayBuffer(mBufferId);
170 else if (target == ELEMENT_ARRAY_BUFFER)
172 mContext.BindElementArrayBuffer(mBufferId);
174 else if (target == TRANSFORM_FEEDBACK_BUFFER)
176 mContext.BindTransformFeedbackBuffer(mBufferId);
180 bool GpuBuffer::BufferIsValid() const
182 return mBufferCreated && (0 != mCapacity );
185 void GpuBuffer::GlContextDestroyed()
187 // If the context is destroyed, GL would have released the buffer.
191 mBufferCreated = false;
194 } // namespace Internal