reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 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 |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | |
| 18 | #include "GrGLVertexBuffer.h" |
| 19 | #include "GrGpuGL.h" |
| 20 | |
| 21 | GrGLVertexBuffer::GrGLVertexBuffer(GLuint id, GrGpuGL* gl, uint32_t sizeInBytes, |
| 22 | bool dynamic) : |
| 23 | INHERITED(sizeInBytes, dynamic), |
| 24 | fGL(gl), |
| 25 | fBufferID(id), |
| 26 | fLockPtr(NULL) { |
| 27 | } |
| 28 | |
| 29 | GrGLVertexBuffer::~GrGLVertexBuffer() { |
| 30 | // make sure we've not been abandoned |
| 31 | if (fBufferID) { |
| 32 | fGL->notifyVertexBufferDelete(this); |
| 33 | GR_GL(DeleteBuffers(1, &fBufferID)); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | GLuint GrGLVertexBuffer::bufferID() const { |
| 38 | return fBufferID; |
| 39 | } |
| 40 | |
| 41 | void GrGLVertexBuffer::abandon() { |
| 42 | fBufferID = 0; |
| 43 | fGL = NULL; |
| 44 | fLockPtr = NULL; |
| 45 | } |
| 46 | |
| 47 | void* GrGLVertexBuffer::lock() { |
| 48 | GrAssert(fBufferID); |
| 49 | GrAssert(!isLocked()); |
| 50 | if (fGL->supportsBufferLocking()) { |
| 51 | GR_GL(BindBuffer(GL_ARRAY_BUFFER, fBufferID)); |
| 52 | fGL->notifyVertexBufferBind(this); |
| 53 | // call bufferData with null ptr to allow driver to perform renaming |
| 54 | // If this call is removed revisit updateData to be sure it doesn't |
| 55 | // leave buffer undersized (as it currently does). |
| 56 | GR_GL(BufferData(GL_ARRAY_BUFFER, size(), NULL, |
| 57 | dynamic() ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW)); |
| 58 | fLockPtr = GR_GLEXT(fGL->extensions(), |
| 59 | MapBuffer(GL_ARRAY_BUFFER, GR_WRITE_ONLY)); |
| 60 | return fLockPtr; |
| 61 | } |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | void GrGLVertexBuffer::unlock() { |
| 66 | GrAssert(fBufferID); |
| 67 | GrAssert(isLocked()); |
| 68 | if (fGL->supportsBufferLocking()) { |
| 69 | GR_GL(BindBuffer(GL_ARRAY_BUFFER, fBufferID)); |
| 70 | fGL->notifyVertexBufferBind(this); |
| 71 | GR_GLEXT(fGL->extensions(), |
| 72 | UnmapBuffer(GL_ARRAY_BUFFER)); |
| 73 | fLockPtr = NULL; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | bool GrGLVertexBuffer::isLocked() const { |
| 78 | GrAssert(fBufferID); |
| 79 | #if GR_DEBUG |
| 80 | if (fGL->supportsBufferLocking()) { |
| 81 | GLint mapped; |
| 82 | GR_GL(BindBuffer(GL_ARRAY_BUFFER, fBufferID)); |
| 83 | fGL->notifyVertexBufferBind(this); |
| 84 | GR_GL(GetBufferParameteriv(GL_ARRAY_BUFFER, GR_BUFFER_MAPPED, &mapped)); |
| 85 | GrAssert(!!mapped == !!fLockPtr); |
| 86 | } |
| 87 | #endif |
| 88 | return NULL != fLockPtr; |
| 89 | } |
| 90 | |
| 91 | bool GrGLVertexBuffer::updateData(const void* src, uint32_t srcSizeInBytes) { |
| 92 | GrAssert(fBufferID); |
| 93 | GrAssert(!isLocked()); |
| 94 | if (srcSizeInBytes > size()) { |
| 95 | return false; |
| 96 | } |
| 97 | GR_GL(BindBuffer(GL_ARRAY_BUFFER, fBufferID)); |
| 98 | fGL->notifyVertexBufferBind(this); |
| 99 | GR_GL(BufferData(GL_ARRAY_BUFFER, srcSizeInBytes, src, |
| 100 | dynamic() ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW)); |
| 101 | return true; |
| 102 | } |
| 103 | |