blob: 5c94c36309540365e37866390355b128c3e53bf9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrGLVertexBuffer.h"
12#include "GrGpuGL.h"
13
bsalomon@google.com8fe72472011-03-30 21:26:44 +000014#define GPUGL static_cast<GrGpuGL*>(getGpu())
15
bsalomon@google.com0b77d682011-08-19 13:28:54 +000016#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
17
bsalomon@google.com8fe72472011-03-30 21:26:44 +000018GrGLVertexBuffer::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.comac10a2d2010-12-22 21:39:39 +000025}
26
bsalomon@google.com8fe72472011-03-30 21:26:44 +000027void GrGLVertexBuffer::onRelease() {
reed@google.comac10a2d2010-12-22 21:39:39 +000028 // make sure we've not been abandoned
29 if (fBufferID) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000030 GPUGL->notifyVertexBufferDelete(this);
bsalomon@google.com0b77d682011-08-19 13:28:54 +000031 GL_CALL(DeleteBuffers(1, &fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000032 fBufferID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000033 }
34}
35
bsalomon@google.com8fe72472011-03-30 21:26:44 +000036void GrGLVertexBuffer::onAbandon() {
37 fBufferID = 0;
38 fLockPtr = NULL;
39}
40
bsalomon@google.com1c13c962011-02-14 16:51:21 +000041void GrGLVertexBuffer::bind() const {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000042 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000043 GPUGL->notifyVertexBufferBind(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000044}
45
twiz@google.com0f31ca72011-03-18 17:38:11 +000046GrGLuint GrGLVertexBuffer::bufferID() const {
reed@google.comac10a2d2010-12-22 21:39:39 +000047 return fBufferID;
48}
49
reed@google.comac10a2d2010-12-22 21:39:39 +000050void* GrGLVertexBuffer::lock() {
51 GrAssert(fBufferID);
52 GrAssert(!isLocked());
bsalomon@google.com18c9c192011-09-22 21:01:31 +000053 if (this->getGpu()->getCaps().fBufferLockSupport) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000054 this->bind();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000055 // Let driver know it can discard the old data
bsalomon@google.com0b77d682011-08-19 13:28:54 +000056 GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, this->sizeInBytes(), NULL,
57 this->dynamic() ? GR_GL_DYNAMIC_DRAW :
58 GR_GL_STATIC_DRAW));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000059 GR_GL_CALL_RET(GPUGL->glInterface(),
60 fLockPtr,
61 MapBuffer(GR_GL_ARRAY_BUFFER, GR_GL_WRITE_ONLY));
reed@google.comac10a2d2010-12-22 21:39:39 +000062 return fLockPtr;
63 }
64 return NULL;
65}
66
bsalomon@google.com1c13c962011-02-14 16:51:21 +000067void* GrGLVertexBuffer::lockPtr() const {
68 return fLockPtr;
69}
70
reed@google.comac10a2d2010-12-22 21:39:39 +000071void GrGLVertexBuffer::unlock() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000072
reed@google.comac10a2d2010-12-22 21:39:39 +000073 GrAssert(fBufferID);
74 GrAssert(isLocked());
bsalomon@google.com18c9c192011-09-22 21:01:31 +000075 GrAssert(this->getGpu()->getCaps().fBufferLockSupport);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000076
bsalomon@google.com8fe72472011-03-30 21:26:44 +000077 this->bind();
bsalomon@google.com0b77d682011-08-19 13:28:54 +000078 GL_CALL(UnmapBuffer(GR_GL_ARRAY_BUFFER));
bsalomon@google.com1c13c962011-02-14 16:51:21 +000079 fLockPtr = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000080}
81
82bool GrGLVertexBuffer::isLocked() const {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000083 GrAssert(!this->isValid() || fBufferID);
bsalomon@google.comd850c182012-03-08 16:45:22 +000084 // this check causes a lot of noise in the gl log
85#if 0
bsalomon@google.com18c9c192011-09-22 21:01:31 +000086 if (this->isValid() && this->getGpu()->getCaps().fBufferLockSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +000087 GrGLint mapped;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000088 this->bind();
bsalomon@google.com0b77d682011-08-19 13:28:54 +000089 GL_CALL(GetBufferParameteriv(GR_GL_ARRAY_BUFFER,
90 GR_GL_BUFFER_MAPPED, &mapped));
reed@google.comac10a2d2010-12-22 21:39:39 +000091 GrAssert(!!mapped == !!fLockPtr);
92 }
93#endif
94 return NULL != fLockPtr;
95}
96
bsalomon@google.com1c13c962011-02-14 16:51:21 +000097bool GrGLVertexBuffer::updateData(const void* src, size_t srcSizeInBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +000098 GrAssert(fBufferID);
99 GrAssert(!isLocked());
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000100 if (srcSizeInBytes > this->sizeInBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 return false;
102 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000103 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +0000104 GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW;
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000105
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000106#if GR_GL_USE_BUFFER_DATA_NULL_HINT
107 if (this->sizeInBytes() == srcSizeInBytes) {
108 GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, srcSizeInBytes, src, usage));
109 } else {
110 // Before we call glBufferSubData we give the driver a hint using
111 // glBufferData with NULL. This makes the old buffer contents
112 // inaccessible to future draws. The GPU may still be processing
113 // draws that reference the old contents. With this hint it can
114 // assign a different allocation for the new contents to avoid
115 // flushing the gpu past draws consuming the old contents.
116 GL_CALL(BufferData(GR_GL_ARRAY_BUFFER,
117 this->sizeInBytes(), NULL, usage));
118 GL_CALL(BufferSubData(GR_GL_ARRAY_BUFFER, 0, srcSizeInBytes, src));
119 }
120#else
121 // Note that we're cheating on the size here. Currently no methods
122 // allow a partial update that preserves contents of non-updated
123 // portions of the buffer (lock() does a glBufferData(..size, NULL..))
124 bool doSubData = false;
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000125#if GR_GL_MAC_BUFFER_OBJECT_PERFOMANCE_WORKAROUND
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000126 static int N = 0;
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000127 // 128 was chosen experimentally. At 256 a slight hitchiness was noticed
128 // when dragging a Chromium window around with a canvas tab backgrounded.
129 doSubData = 0 == (N % 128);
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000130 ++N;
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000131#endif
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000132 if (doSubData) {
133 // The workaround is to do a glBufferData followed by glBufferSubData.
134 // Chromium's command buffer may turn a glBufferSubData where the size
135 // exactly matches the buffer size into a glBufferData. So we tack 1
136 // extra byte onto the glBufferData.
137 GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, srcSizeInBytes + 1,
138 NULL, usage));
139 GL_CALL(BufferSubData(GR_GL_ARRAY_BUFFER, 0, srcSizeInBytes, src));
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000140 } else {
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000141 GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, srcSizeInBytes, src, usage));
142 }
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000143#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 return true;
145}
146