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 "GrGLVertexBuffer.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 | GrGLVertexBuffer::GrGLVertexBuffer(GrGpuGL* gpu, |
| 19 | GrGLuint id, |
| 20 | size_t sizeInBytes, |
| 21 | bool dynamic) |
| 22 | : INHERITED(gpu, sizeInBytes, dynamic) |
| 23 | , fBufferID(id) |
| 24 | , fLockPtr(NULL) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | } |
| 26 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 27 | void GrGLVertexBuffer::onRelease() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | // make sure we've not been abandoned |
| 29 | if (fBufferID) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 30 | GPUGL->notifyVertexBufferDelete(this); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 31 | GL_CALL(DeleteBuffers(1, &fBufferID)); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 32 | fBufferID = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 36 | void GrGLVertexBuffer::onAbandon() { |
| 37 | fBufferID = 0; |
| 38 | fLockPtr = NULL; |
| 39 | } |
| 40 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 41 | void GrGLVertexBuffer::bind() const { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 42 | GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, fBufferID)); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 43 | GPUGL->notifyVertexBufferBind(this); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 44 | } |
| 45 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 46 | GrGLuint GrGLVertexBuffer::bufferID() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | return fBufferID; |
| 48 | } |
| 49 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | void* GrGLVertexBuffer::lock() { |
| 51 | GrAssert(fBufferID); |
| 52 | GrAssert(!isLocked()); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 53 | if (this->getGpu()->getCaps().fBufferLockSupport) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 54 | this->bind(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 55 | // Let driver know it can discard the old data |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 56 | GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, this->sizeInBytes(), NULL, |
| 57 | this->dynamic() ? GR_GL_DYNAMIC_DRAW : |
| 58 | GR_GL_STATIC_DRAW)); |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 59 | GR_GL_CALL_RET(GPUGL->glInterface(), |
| 60 | fLockPtr, |
| 61 | MapBuffer(GR_GL_ARRAY_BUFFER, GR_GL_WRITE_ONLY)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | return fLockPtr; |
| 63 | } |
| 64 | return NULL; |
| 65 | } |
| 66 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 67 | void* GrGLVertexBuffer::lockPtr() const { |
| 68 | return fLockPtr; |
| 69 | } |
| 70 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | void GrGLVertexBuffer::unlock() { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 72 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 73 | GrAssert(fBufferID); |
| 74 | GrAssert(isLocked()); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 75 | GrAssert(this->getGpu()->getCaps().fBufferLockSupport); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 76 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 77 | this->bind(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 78 | GL_CALL(UnmapBuffer(GR_GL_ARRAY_BUFFER)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 79 | fLockPtr = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | bool GrGLVertexBuffer::isLocked() const { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 83 | GrAssert(!this->isValid() || fBufferID); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | #if GR_DEBUG |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 85 | if (this->isValid() && this->getGpu()->getCaps().fBufferLockSupport) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 86 | GrGLint mapped; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 87 | this->bind(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 88 | GL_CALL(GetBufferParameteriv(GR_GL_ARRAY_BUFFER, |
| 89 | GR_GL_BUFFER_MAPPED, &mapped)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 90 | GrAssert(!!mapped == !!fLockPtr); |
| 91 | } |
| 92 | #endif |
| 93 | return NULL != fLockPtr; |
| 94 | } |
| 95 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 96 | bool GrGLVertexBuffer::updateData(const void* src, size_t srcSizeInBytes) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | GrAssert(fBufferID); |
| 98 | GrAssert(!isLocked()); |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 99 | if (srcSizeInBytes > this->sizeInBytes()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 102 | this->bind(); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 103 | GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW; |
bsalomon@google.com | 5ffd5ba | 2012-03-01 15:29:07 +0000 | [diff] [blame] | 104 | |
bsalomon@google.com | c1dd888 | 2012-03-02 20:36:18 +0000 | [diff] [blame] | 105 | #if GR_GL_USE_BUFFER_DATA_NULL_HINT |
| 106 | if (this->sizeInBytes() == srcSizeInBytes) { |
| 107 | GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, srcSizeInBytes, src, usage)); |
| 108 | } else { |
| 109 | // Before we call glBufferSubData we give the driver a hint using |
| 110 | // glBufferData with NULL. This makes the old buffer contents |
| 111 | // inaccessible to future draws. The GPU may still be processing |
| 112 | // draws that reference the old contents. With this hint it can |
| 113 | // assign a different allocation for the new contents to avoid |
| 114 | // flushing the gpu past draws consuming the old contents. |
| 115 | GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, |
| 116 | this->sizeInBytes(), NULL, usage)); |
| 117 | GL_CALL(BufferSubData(GR_GL_ARRAY_BUFFER, 0, srcSizeInBytes, src)); |
| 118 | } |
| 119 | #else |
| 120 | // Note that we're cheating on the size here. Currently no methods |
| 121 | // allow a partial update that preserves contents of non-updated |
| 122 | // portions of the buffer (lock() does a glBufferData(..size, NULL..)) |
| 123 | bool doSubData = false; |
bsalomon@google.com | 5ffd5ba | 2012-03-01 15:29:07 +0000 | [diff] [blame] | 124 | #if GR_GL_MAC_BUFFER_OBJECT_PERFOMANCE_WORKAROUND |
bsalomon@google.com | 5ffd5ba | 2012-03-01 15:29:07 +0000 | [diff] [blame] | 125 | static int N = 0; |
bsalomon@google.com | c1dd888 | 2012-03-02 20:36:18 +0000 | [diff] [blame] | 126 | // 128 was chosen experimentally. At 256 a slight hitchiness was noticed |
| 127 | // when dragging a Chromium window around with a canvas tab backgrounded. |
| 128 | doSubData = 0 == (N % 128); |
bsalomon@google.com | 5ffd5ba | 2012-03-01 15:29:07 +0000 | [diff] [blame] | 129 | ++N; |
bsalomon@google.com | 96e96df | 2011-10-10 14:49:29 +0000 | [diff] [blame] | 130 | #endif |
bsalomon@google.com | c1dd888 | 2012-03-02 20:36:18 +0000 | [diff] [blame] | 131 | if (doSubData) { |
| 132 | // The workaround is to do a glBufferData followed by glBufferSubData. |
| 133 | // Chromium's command buffer may turn a glBufferSubData where the size |
| 134 | // exactly matches the buffer size into a glBufferData. So we tack 1 |
| 135 | // extra byte onto the glBufferData. |
| 136 | GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, srcSizeInBytes + 1, |
| 137 | NULL, usage)); |
| 138 | GL_CALL(BufferSubData(GR_GL_ARRAY_BUFFER, 0, srcSizeInBytes, src)); |
bsalomon@google.com | 5ffd5ba | 2012-03-01 15:29:07 +0000 | [diff] [blame] | 139 | } else { |
bsalomon@google.com | 5ffd5ba | 2012-03-01 15:29:07 +0000 | [diff] [blame] | 140 | GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, srcSizeInBytes, src, usage)); |
| 141 | } |
bsalomon@google.com | c1dd888 | 2012-03-02 20:36:18 +0000 | [diff] [blame] | 142 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 143 | return true; |
| 144 | } |
| 145 | |