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>
29 GpuBuffer::GpuBuffer(Context& context,GLenum target,GLenum usage)
34 mBufferCreated(false),
38 // the buffer is owned by the GPU so if we lose context, we lose the buffer
39 // so we need to know when we lose context
40 mContext.AddObserver(*this);
42 GpuBuffer::~GpuBuffer()
44 mContext.RemoveObserver(*this);
46 GlContextToBeDestroyed();
50 * Creates or updates the buffer data depending on whether it
51 * already exists or not.
53 void GpuBuffer::UpdateDataBuffer(GLsizeiptr size,const GLvoid *data)
55 DALI_ASSERT_DEBUG( size > 0 );
57 // make sure we have a buffer name/id before uploading
60 mContext.GenBuffers(1,&mBufferId);
61 DALI_ASSERT_DEBUG(mBufferId);
64 // make sure the buffer is bound, don't perform any checks because size may be zero
65 BindNoChecks(mBufferId);
67 // if the buffer has already been created, just update the data providing it fits
70 // if the data will fit in the existing buffer, just update it
71 if (size <= mCapacity )
73 mContext.BufferSubData(mTarget,0, size, data);
77 // create a new buffer of the larger size,
78 // gl should automatically deallocate the old buffer
79 mContext.BufferData(mTarget, size, data, mUsage);
86 mContext.BufferData(mTarget, size, data, mUsage);
87 mBufferCreated = true;
95 mContext.BindArrayBuffer(0);
98 case GL_ELEMENT_ARRAY_BUFFER:
100 mContext.BindElementArrayBuffer(0);
103 case GL_TRANSFORM_FEEDBACK_BUFFER:
105 mContext.BindTransformFeedbackBuffer(0);
109 DALI_ASSERT_DEBUG(false && "Unsupported type");
113 void GpuBuffer::Bind() const
115 DALI_ASSERT_DEBUG(mCapacity);
117 BindNoChecks(mBufferId);
120 bool GpuBuffer::BufferIsValid() const
122 return mBufferCreated && (0 != mCapacity );
126 * If the context is about to go, and we have a buffer then delete it.
128 void GpuBuffer::GlContextToBeDestroyed()
132 // If the buffer is currently bound, then unbind it by setting the
133 // currently bound buffer to zero.
134 if (mContext.GetCurrentBoundArrayBuffer(mTarget) == mBufferId)
141 mContext.DeleteBuffers(1,&mBufferId);
143 mBufferCreated = false;
147 void GpuBuffer::GlContextCreated()
149 // set some default values, just incase we don't get a
150 // GlContextToBeDestroyed when the context is lost and then
151 // is recreated (should never happen).
155 mBufferCreated = false;
158 void GpuBuffer::BindNoChecks(GLuint bufferId) const
160 // context currently only supports two targets, element and array
161 // and it caches both of them (as in it won't bind them if the
162 // buffer id is already bound).
164 if (mTarget == GL_ARRAY_BUFFER)
166 mContext.BindArrayBuffer(bufferId);
170 mContext.BindElementArrayBuffer(bufferId);
175 } // namespace Internal