Removed legacy resource tracking / logging
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / gpu-buffer.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 // CLASS HEADER
18 #include <dali/internal/render/gl-resources/gpu-buffer.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/public-api/common/dali-common.h>
22
23 namespace Dali
24 {
25
26 namespace Internal
27 {
28
29 GpuBuffer::GpuBuffer(Context& context,GLenum target,GLenum usage)
30 :mCapacity(0),
31  mSize(0),
32  mContext(context),
33  mBufferId(0),
34  mBufferCreated(false),
35  mTarget(target),
36  mUsage(usage)
37 {
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);
41 }
42 GpuBuffer::~GpuBuffer()
43 {
44   mContext.RemoveObserver(*this);
45
46   GlContextToBeDestroyed();
47 }
48
49 /*
50  * Creates or updates the buffer data depending on whether it
51  * already exists or not.
52  */
53 void GpuBuffer::UpdateDataBuffer(GLsizeiptr size,const GLvoid *data)
54 {
55   DALI_ASSERT_DEBUG( size > 0 );
56   mSize = size;
57   // make sure we have a buffer name/id before uploading
58   if (mBufferId == 0)
59   {
60     mContext.GenBuffers(1,&mBufferId);
61     DALI_ASSERT_DEBUG(mBufferId);
62   }
63
64   // make sure the buffer is bound, don't perform any checks because size may be zero
65   BindNoChecks(mBufferId);
66
67   // if the buffer has already been created, just update the data providing it fits
68   if (mBufferCreated )
69   {
70     // if the data will fit in the existing buffer, just update it
71     if (size <= mCapacity )
72     {
73       mContext.BufferSubData(mTarget,0, size, data);
74     }
75     else
76     {
77       // create a new buffer of the larger size,
78       // gl should automatically deallocate the old buffer
79       mContext.BufferData(mTarget, size, data, mUsage);
80       mCapacity = size;
81     }
82   }
83   else
84   {
85     // create the buffer
86     mContext.BufferData(mTarget, size, data, mUsage);
87     mBufferCreated = true;
88     mCapacity = size;
89   }
90
91   switch (mTarget)
92   {
93     case GL_ARRAY_BUFFER:
94     {
95       mContext.BindArrayBuffer(0);
96       break;
97     }
98     case GL_ELEMENT_ARRAY_BUFFER:
99     {
100       mContext.BindElementArrayBuffer(0);
101       break;
102     }
103     case GL_TRANSFORM_FEEDBACK_BUFFER:
104     {
105       mContext.BindTransformFeedbackBuffer(0);
106       break;
107     }
108     default:
109       DALI_ASSERT_DEBUG(false && "Unsupported type");
110   }
111 }
112
113 void GpuBuffer::Bind() const
114 {
115   DALI_ASSERT_DEBUG(mCapacity);
116
117   BindNoChecks(mBufferId);
118 }
119
120 bool GpuBuffer::BufferIsValid() const
121 {
122   return mBufferCreated && (0 != mCapacity );
123 }
124
125 /*
126  * If the context is about to go, and we have a buffer then delete it.
127  */
128 void GpuBuffer::GlContextToBeDestroyed()
129 {
130   if (mBufferId)
131   {
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)
135     {
136       BindNoChecks(0);
137     }
138
139     mCapacity = 0;
140     mSize = 0;
141     mContext.DeleteBuffers(1,&mBufferId);
142     mBufferId = 0;
143     mBufferCreated = false;
144   }
145 }
146
147 void GpuBuffer::GlContextCreated()
148 {
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).
152   mCapacity = 0;
153   mSize = 0;
154   mBufferId = 0;
155   mBufferCreated = false;
156 }
157
158 void GpuBuffer::BindNoChecks(GLuint bufferId) const
159 {
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).
163
164   if (mTarget == GL_ARRAY_BUFFER)
165   {
166     mContext.BindArrayBuffer(bufferId);
167   }
168   else
169   {
170     mContext.BindElementArrayBuffer(bufferId);
171   }
172 }
173
174
175 } // namespace Internal
176
177 } //namespace Dali