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 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 21 | GrGLVertexBuffer::GrGLVertexBuffer(GLuint id, GrGpuGL* gl, size_t sizeInBytes, |
| 22 | bool dynamic) : |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 37 | void GrGLVertexBuffer::bind() const { |
| 38 | GR_GL(BindBuffer(GL_ARRAY_BUFFER, fBufferID)); |
| 39 | fGL->notifyVertexBufferBind(this); |
| 40 | } |
| 41 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 42 | GLuint GrGLVertexBuffer::bufferID() const { |
| 43 | return fBufferID; |
| 44 | } |
| 45 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 46 | void GrGLVertexBuffer::abandon() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | fBufferID = 0; |
| 48 | fGL = NULL; |
| 49 | fLockPtr = NULL; |
| 50 | } |
| 51 | |
| 52 | void* GrGLVertexBuffer::lock() { |
| 53 | GrAssert(fBufferID); |
| 54 | GrAssert(!isLocked()); |
| 55 | if (fGL->supportsBufferLocking()) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 56 | bind(); |
| 57 | // Let driver know it can discard the old data |
| 58 | GR_GL(BufferData(GL_ARRAY_BUFFER, size(), NULL, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | dynamic() ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW)); |
| 60 | fLockPtr = GR_GLEXT(fGL->extensions(), |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 61 | MapBuffer(GL_ARRAY_BUFFER, GR_WRITE_ONLY)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | return fLockPtr; |
| 63 | } |
| 64 | return NULL; |
| 65 | } |
| 66 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 67 | void* GrGLVertexBuffer::lockPtr() const { |
| 68 | return fLockPtr; |
| 69 | } |
| 70 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | void GrGLVertexBuffer::unlock() { |
| 72 | GrAssert(fBufferID); |
| 73 | GrAssert(isLocked()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 74 | GrAssert(fGL->supportsBufferLocking()); |
| 75 | |
| 76 | bind(); |
| 77 | GR_GLEXT(fGL->extensions(), UnmapBuffer(GL_ARRAY_BUFFER)); |
| 78 | fLockPtr = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | bool GrGLVertexBuffer::isLocked() const { |
| 82 | GrAssert(fBufferID); |
| 83 | #if GR_DEBUG |
| 84 | if (fGL->supportsBufferLocking()) { |
| 85 | GLint mapped; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 86 | bind(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 87 | GR_GL(GetBufferParameteriv(GL_ARRAY_BUFFER, GR_BUFFER_MAPPED, &mapped)); |
| 88 | GrAssert(!!mapped == !!fLockPtr); |
| 89 | } |
| 90 | #endif |
| 91 | return NULL != fLockPtr; |
| 92 | } |
| 93 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 94 | bool GrGLVertexBuffer::updateData(const void* src, size_t srcSizeInBytes) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | GrAssert(fBufferID); |
| 96 | GrAssert(!isLocked()); |
| 97 | if (srcSizeInBytes > size()) { |
| 98 | return false; |
| 99 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 100 | bind(); |
| 101 | GLenum usage = dynamic() ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW; |
| 102 | if (size() == srcSizeInBytes) { |
| 103 | GR_GL(BufferData(GL_ARRAY_BUFFER, srcSizeInBytes, src, usage)); |
| 104 | } else { |
| 105 | GR_GL(BufferData(GL_ARRAY_BUFFER, size(), NULL, usage)); |
| 106 | GR_GL(BufferSubData(GL_ARRAY_BUFFER, 0, srcSizeInBytes, src)); |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | bool GrGLVertexBuffer::updateSubData(const void* src, |
| 112 | size_t srcSizeInBytes, |
| 113 | size_t offset) { |
| 114 | GrAssert(fBufferID); |
| 115 | GrAssert(!isLocked()); |
| 116 | if (srcSizeInBytes + offset > size()) { |
| 117 | return false; |
| 118 | } |
| 119 | bind(); |
| 120 | GR_GL(BufferSubData(GL_ARRAY_BUFFER, offset, srcSizeInBytes, src)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |