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 )
93 : mContext( context ),
97 mBufferCreated( false )
101 GpuBuffer::~GpuBuffer()
103 // If we have a buffer then delete it.
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);
113 * Creates or updates the buffer data depending on whether it
114 * already exists or not.
116 void GpuBuffer::UpdateDataBuffer(GLsizeiptr size,const GLvoid *data, Usage usage)
118 DALI_ASSERT_DEBUG( size > 0 );
120 // make sure we have a buffer name/id before uploading
123 mContext.GenBuffers(1,&mBufferId);
124 DALI_ASSERT_DEBUG(mBufferId);
127 // make sure the buffer is bound, don't perform any checks because size may be zero
128 mContext.BindArrayBuffer( mBufferId );
130 // if the buffer has already been created, just update the data providing it fits
133 // if the data will fit in the existing buffer, just update it
134 if (size <= mCapacity )
136 mContext.BufferSubData( GL_ARRAY_BUFFER, 0, size, data );
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 ) );
149 mContext.BufferData( GL_ARRAY_BUFFER, size, data, ModeAsGlEnum( usage ) );
150 mBufferCreated = true;
154 mContext.BindArrayBuffer(0);
157 void GpuBuffer::Bind(Target target) const
159 DALI_ASSERT_DEBUG(mCapacity);
161 if (target == ARRAY_BUFFER)
163 mContext.BindArrayBuffer(mBufferId);
167 mContext.BindElementArrayBuffer(mBufferId);
171 bool GpuBuffer::BufferIsValid() const
173 return mBufferCreated && (0 != mCapacity );
176 void GpuBuffer::GlContextDestroyed()
178 // If the context is destroyed, GL would have released the buffer.
182 mBufferCreated = false;
185 } // namespace Internal