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 "GrGLIndexBuffer.h" |
| 19 | #include "GrGpuGL.h" |
| 20 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 21 | GrGLIndexBuffer::GrGLIndexBuffer(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 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | GrGLIndexBuffer::~GrGLIndexBuffer() { |
| 30 | // make sure we've not been abandoned |
| 31 | if (fBufferID) { |
| 32 | fGL->notifyIndexBufferDelete(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 GrGLIndexBuffer::bind() const { |
| 38 | GR_GL(BindBuffer(GL_ELEMENT_ARRAY_BUFFER, fBufferID)); |
| 39 | fGL->notifyIndexBufferBind(this); |
| 40 | } |
| 41 | |
| 42 | GLuint GrGLIndexBuffer::bufferID() const { |
| 43 | return fBufferID; |
| 44 | } |
| 45 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | void GrGLIndexBuffer::abandon() { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 47 | fBufferID = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | fGL = NULL; |
| 49 | fLockPtr = NULL; |
| 50 | } |
| 51 | |
| 52 | void* GrGLIndexBuffer::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_ELEMENT_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(), |
| 61 | MapBuffer(GL_ELEMENT_ARRAY_BUFFER, GR_WRITE_ONLY)); |
| 62 | |
| 63 | return fLockPtr; |
| 64 | } |
| 65 | return NULL; |
| 66 | } |
| 67 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 68 | void* GrGLIndexBuffer::lockPtr() const { |
| 69 | return fLockPtr; |
| 70 | } |
| 71 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 72 | void GrGLIndexBuffer::unlock() { |
| 73 | GrAssert(fBufferID); |
| 74 | GrAssert(isLocked()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 75 | GrAssert(fGL->supportsBufferLocking()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 76 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 77 | bind(); |
| 78 | GR_GLEXT(fGL->extensions(), UnmapBuffer(GL_ELEMENT_ARRAY_BUFFER)); |
| 79 | fLockPtr = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | bool GrGLIndexBuffer::isLocked() const { |
| 83 | GrAssert(fBufferID); |
| 84 | #if GR_DEBUG |
| 85 | if (fGL->supportsBufferLocking()) { |
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 | GLint mapped; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 88 | GR_GL(GetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | GR_BUFFER_MAPPED, &mapped)); |
| 90 | GrAssert(!!mapped == !!fLockPtr); |
| 91 | } |
| 92 | #endif |
| 93 | return NULL != fLockPtr; |
| 94 | } |
| 95 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 96 | bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | GrAssert(fBufferID); |
| 98 | GrAssert(!isLocked()); |
| 99 | if (srcSizeInBytes > size()) { |
| 100 | return false; |
| 101 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 102 | bind(); |
| 103 | GLenum usage = dynamic() ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW; |
| 104 | if (size() == srcSizeInBytes) { |
| 105 | GR_GL(BufferData(GL_ELEMENT_ARRAY_BUFFER, srcSizeInBytes, src, usage)); |
| 106 | } else { |
| 107 | GR_GL(BufferData(GL_ELEMENT_ARRAY_BUFFER, size(), NULL, usage)); |
| 108 | GR_GL(BufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, srcSizeInBytes, src)); |
| 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | bool GrGLIndexBuffer::updateSubData(const void* src, |
| 114 | size_t srcSizeInBytes, |
| 115 | size_t offset) { |
| 116 | GrAssert(fBufferID); |
| 117 | GrAssert(!isLocked()); |
| 118 | if (srcSizeInBytes + offset > size()) { |
| 119 | return false; |
| 120 | } |
| 121 | bind(); |
| 122 | GR_GL(BufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, srcSizeInBytes, src)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |