reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 2 | Copyright 2011 Google Inc. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 21 | #define GPUGL static_cast<GrGpuGL*>(getGpu()) |
| 22 | |
| 23 | GrGLIndexBuffer::GrGLIndexBuffer(GrGpuGL* gpu, |
| 24 | GrGLuint id, |
| 25 | size_t sizeInBytes, |
| 26 | bool dynamic) |
| 27 | : INHERITED(gpu, sizeInBytes, dynamic) |
| 28 | , fBufferID(id) |
| 29 | , fLockPtr(NULL) { |
| 30 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | } |
| 32 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 33 | void GrGLIndexBuffer::onRelease() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | // make sure we've not been abandoned |
| 35 | if (fBufferID) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 36 | GPUGL->notifyIndexBufferDelete(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 37 | GR_GL(DeleteBuffers(1, &fBufferID)); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 38 | fBufferID = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 42 | void GrGLIndexBuffer::onAbandon() { |
| 43 | fBufferID = 0; |
| 44 | fLockPtr = NULL; |
| 45 | } |
| 46 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 47 | void GrGLIndexBuffer::bind() const { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 48 | GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, fBufferID)); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 49 | GPUGL->notifyIndexBufferBind(this); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 50 | } |
| 51 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 52 | GrGLuint GrGLIndexBuffer::bufferID() const { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 53 | return fBufferID; |
| 54 | } |
| 55 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | void* GrGLIndexBuffer::lock() { |
| 57 | GrAssert(fBufferID); |
| 58 | GrAssert(!isLocked()); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 59 | if (GPUGL->supportsBufferLocking()) { |
| 60 | this->bind(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 61 | // Let driver know it can discard the old data |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame^] | 62 | GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, this->sizeInBytes(), NULL, |
| 63 | this->dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW)); |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 64 | fLockPtr = GR_GL(MapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, GR_GL_WRITE_ONLY)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 65 | |
| 66 | return fLockPtr; |
| 67 | } |
| 68 | return NULL; |
| 69 | } |
| 70 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 71 | void* GrGLIndexBuffer::lockPtr() const { |
| 72 | return fLockPtr; |
| 73 | } |
| 74 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 75 | void GrGLIndexBuffer::unlock() { |
| 76 | GrAssert(fBufferID); |
| 77 | GrAssert(isLocked()); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 78 | GrAssert(GPUGL->supportsBufferLocking()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 80 | this->bind(); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 81 | GR_GL(UnmapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 82 | fLockPtr = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool GrGLIndexBuffer::isLocked() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | #if GR_DEBUG |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 87 | if (this->isValid() && GPUGL->supportsBufferLocking()) { |
| 88 | this->bind(); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 89 | GrGLint mapped; |
| 90 | GR_GL(GetBufferParameteriv(GR_GL_ELEMENT_ARRAY_BUFFER, |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 91 | GR_GL_BUFFER_MAPPED, &mapped)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 92 | GrAssert(!!mapped == !!fLockPtr); |
| 93 | } |
| 94 | #endif |
| 95 | return NULL != fLockPtr; |
| 96 | } |
| 97 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 98 | bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | GrAssert(fBufferID); |
| 100 | GrAssert(!isLocked()); |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame^] | 101 | if (srcSizeInBytes > this->sizeInBytes()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 102 | return false; |
| 103 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 104 | this->bind(); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 105 | GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW; |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame^] | 106 | if (this->sizeInBytes() == srcSizeInBytes) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 107 | GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, srcSizeInBytes, src, usage)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 108 | } else { |
bsalomon@google.com | 9ae4429 | 2011-07-01 15:21:59 +0000 | [diff] [blame] | 109 | #if GR_GL_USE_BUFFER_DATA_NULL_HINT |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame^] | 110 | GR_GL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, this->sizeInBytes(), NULL, usage)); |
bsalomon@google.com | 9ae4429 | 2011-07-01 15:21:59 +0000 | [diff] [blame] | 111 | #endif |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 112 | GR_GL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER, 0, srcSizeInBytes, src)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool GrGLIndexBuffer::updateSubData(const void* src, |
| 118 | size_t srcSizeInBytes, |
| 119 | size_t offset) { |
| 120 | GrAssert(fBufferID); |
| 121 | GrAssert(!isLocked()); |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame^] | 122 | if (srcSizeInBytes + offset > this->sizeInBytes()) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 123 | return false; |
| 124 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 125 | this->bind(); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 126 | GR_GL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER, offset, srcSizeInBytes, src)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |