1 /* 2 Copyright 2011 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 #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) { 30 } 31 32 void GrGLVertexBuffer::onRelease() { 33 // make sure we've not been abandoned 34 if (fBufferID) { 35 GPUGL->notifyVertexBufferDelete(this); 36 GR_GL(DeleteBuffers(1, &fBufferID)); 37 fBufferID = 0; 38 } 39 } 40 41 void GrGLVertexBuffer::onAbandon() { 42 fBufferID = 0; 43 fLockPtr = NULL; 44 } 45 46 void GrGLVertexBuffer::bind() const { 47 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, fBufferID)); 48 GPUGL->notifyVertexBufferBind(this); 49 } 50 51 GrGLuint GrGLVertexBuffer::bufferID() const { 52 return fBufferID; 53 } 54 55 void* GrGLVertexBuffer::lock() { 56 GrAssert(fBufferID); 57 GrAssert(!isLocked()); 58 if (GPUGL->supportsBufferLocking()) { 59 this->bind(); 60 // Let driver know it can discard the old data 61 GR_GL(BufferData(GR_GL_ARRAY_BUFFER, size(), NULL, 62 dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW)); 63 fLockPtr = GR_GL(MapBuffer(GR_GL_ARRAY_BUFFER, GR_GL_WRITE_ONLY)); 64 return fLockPtr; 65 } 66 return NULL; 67 } 68 69 void* GrGLVertexBuffer::lockPtr() const { 70 return fLockPtr; 71 } 72 73 void GrGLVertexBuffer::unlock() { 74 75 GrAssert(fBufferID); 76 GrAssert(isLocked()); 77 GrAssert(GPUGL->supportsBufferLocking()); 78 79 this->bind(); 80 GR_GL(UnmapBuffer(GR_GL_ARRAY_BUFFER)); 81 fLockPtr = NULL; 82 } 83 84 bool GrGLVertexBuffer::isLocked() const { 85 GrAssert(!this->isValid() || fBufferID); 86 #if GR_DEBUG 87 if (this->isValid() && GPUGL->supportsBufferLocking()) { 88 GrGLint mapped; 89 this->bind(); 90 GR_GL(GetBufferParameteriv(GR_GL_ARRAY_BUFFER, GR_GL_BUFFER_MAPPED, &mapped)); 91 GrAssert(!!mapped == !!fLockPtr); 92 } 93 #endif 94 return NULL != fLockPtr; 95 } 96 97 bool GrGLVertexBuffer::updateData(const void* src, size_t srcSizeInBytes) { 98 GrAssert(fBufferID); 99 GrAssert(!isLocked()); 100 if (srcSizeInBytes > size()) { 101 return false; 102 } 103 this->bind(); 104 GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW; 105 if (size() == srcSizeInBytes) { 106 GR_GL(BufferData(GR_GL_ARRAY_BUFFER, srcSizeInBytes, src, usage)); 107 } else { 108 GR_GL(BufferData(GR_GL_ARRAY_BUFFER, size(), NULL, usage)); 109 GR_GL(BufferSubData(GR_GL_ARRAY_BUFFER, 0, srcSizeInBytes, src)); 110 } 111 return true; 112 } 113 114 bool GrGLVertexBuffer::updateSubData(const void* src, 115 size_t srcSizeInBytes, 116 size_t offset) { 117 GrAssert(fBufferID); 118 GrAssert(!isLocked()); 119 if (srcSizeInBytes + offset > size()) { 120 return false; 121 } 122 this->bind(); 123 GR_GL(BufferSubData(GR_GL_ARRAY_BUFFER, offset, srcSizeInBytes, src)); 124 return true; 125 } 126 127