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