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