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