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