blob: 1512fef1fa3d57c2a2b649438c37ba98b85c6da2 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comac10a2d2010-12-22 21:39:39 +00006 */
7
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGLVertexBuffer.h"
11#include "GrGpuGL.h"
12
bsalomon@google.com8fe72472011-03-30 21:26:44 +000013#define GPUGL static_cast<GrGpuGL*>(getGpu())
14
bsalomon@google.com0b77d682011-08-19 13:28:54 +000015#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
16
bsalomon@google.com8fe72472011-03-30 21:26:44 +000017GrGLVertexBuffer::GrGLVertexBuffer(GrGpuGL* gpu,
bsalomon@google.com72830222013-01-23 20:25:22 +000018 bool isWrapped,
bsalomon@google.com8fe72472011-03-30 21:26:44 +000019 GrGLuint id,
20 size_t sizeInBytes,
21 bool dynamic)
bsalomon@google.com72830222013-01-23 20:25:22 +000022 : INHERITED(gpu, isWrapped, sizeInBytes, dynamic)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000023 , 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
bsalomon@google.com72830222013-01-23 20:25:22 +000029 if (fBufferID && !this->isWrapped()) {
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 }
robertphillips@google.comd3645542012-09-05 18:37:39 +000034
35 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +000036}
37
bsalomon@google.com8fe72472011-03-30 21:26:44 +000038void GrGLVertexBuffer::onAbandon() {
39 fBufferID = 0;
40 fLockPtr = NULL;
robertphillips@google.comd3645542012-09-05 18:37:39 +000041
42 INHERITED::onAbandon();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000043}
44
bsalomon@google.com1c13c962011-02-14 16:51:21 +000045void GrGLVertexBuffer::bind() const {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000046 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000047 GPUGL->notifyVertexBufferBind(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000048}
49
twiz@google.com0f31ca72011-03-18 17:38:11 +000050GrGLuint GrGLVertexBuffer::bufferID() const {
reed@google.comac10a2d2010-12-22 21:39:39 +000051 return fBufferID;
52}
53
reed@google.comac10a2d2010-12-22 21:39:39 +000054void* GrGLVertexBuffer::lock() {
55 GrAssert(fBufferID);
56 GrAssert(!isLocked());
bsalomon@google.comf6601872012-08-28 21:11:35 +000057 if (this->getGpu()->getCaps().bufferLockSupport()) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000058 this->bind();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000059 // Let driver know it can discard the old data
bsalomon@google.com0b77d682011-08-19 13:28:54 +000060 GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, this->sizeInBytes(), NULL,
61 this->dynamic() ? GR_GL_DYNAMIC_DRAW :
62 GR_GL_STATIC_DRAW));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000063 GR_GL_CALL_RET(GPUGL->glInterface(),
64 fLockPtr,
65 MapBuffer(GR_GL_ARRAY_BUFFER, GR_GL_WRITE_ONLY));
reed@google.comac10a2d2010-12-22 21:39:39 +000066 return fLockPtr;
67 }
68 return NULL;
69}
70
bsalomon@google.com1c13c962011-02-14 16:51:21 +000071void* GrGLVertexBuffer::lockPtr() const {
72 return fLockPtr;
73}
74
reed@google.comac10a2d2010-12-22 21:39:39 +000075void GrGLVertexBuffer::unlock() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000076
reed@google.comac10a2d2010-12-22 21:39:39 +000077 GrAssert(fBufferID);
78 GrAssert(isLocked());
bsalomon@google.comf6601872012-08-28 21:11:35 +000079 GrAssert(this->getGpu()->getCaps().bufferLockSupport());
bsalomon@google.com1c13c962011-02-14 16:51:21 +000080
bsalomon@google.com8fe72472011-03-30 21:26:44 +000081 this->bind();
bsalomon@google.com0b77d682011-08-19 13:28:54 +000082 GL_CALL(UnmapBuffer(GR_GL_ARRAY_BUFFER));
bsalomon@google.com1c13c962011-02-14 16:51:21 +000083 fLockPtr = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000084}
85
86bool GrGLVertexBuffer::isLocked() const {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000087 GrAssert(!this->isValid() || fBufferID);
bsalomon@google.comd850c182012-03-08 16:45:22 +000088 // this check causes a lot of noise in the gl log
89#if 0
bsalomon@google.com18c9c192011-09-22 21:01:31 +000090 if (this->isValid() && this->getGpu()->getCaps().fBufferLockSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +000091 GrGLint mapped;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000092 this->bind();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000093 GL_CALL(GetBufferParameteriv(GR_GL_ARRAY_BUFFER,
bsalomon@google.com0b77d682011-08-19 13:28:54 +000094 GR_GL_BUFFER_MAPPED, &mapped));
reed@google.comac10a2d2010-12-22 21:39:39 +000095 GrAssert(!!mapped == !!fLockPtr);
96 }
97#endif
98 return NULL != fLockPtr;
99}
100
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000101bool GrGLVertexBuffer::updateData(const void* src, size_t srcSizeInBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 GrAssert(fBufferID);
103 GrAssert(!isLocked());
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000104 if (srcSizeInBytes > this->sizeInBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 return false;
106 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000107 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +0000108 GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW;
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000109
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000110#if GR_GL_USE_BUFFER_DATA_NULL_HINT
111 if (this->sizeInBytes() == srcSizeInBytes) {
112 GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, srcSizeInBytes, src, usage));
113 } else {
114 // Before we call glBufferSubData we give the driver a hint using
115 // glBufferData with NULL. This makes the old buffer contents
116 // inaccessible to future draws. The GPU may still be processing
117 // draws that reference the old contents. With this hint it can
118 // assign a different allocation for the new contents to avoid
119 // flushing the gpu past draws consuming the old contents.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000120 GL_CALL(BufferData(GR_GL_ARRAY_BUFFER,
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000121 this->sizeInBytes(), NULL, usage));
122 GL_CALL(BufferSubData(GR_GL_ARRAY_BUFFER, 0, srcSizeInBytes, src));
123 }
124#else
125 // Note that we're cheating on the size here. Currently no methods
126 // allow a partial update that preserves contents of non-updated
127 // portions of the buffer (lock() does a glBufferData(..size, NULL..))
128 bool doSubData = false;
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000129#if GR_GL_MAC_BUFFER_OBJECT_PERFOMANCE_WORKAROUND
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000130 static int N = 0;
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000131 // 128 was chosen experimentally. At 256 a slight hitchiness was noticed
132 // when dragging a Chromium window around with a canvas tab backgrounded.
133 doSubData = 0 == (N % 128);
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000134 ++N;
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000135#endif
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000136 if (doSubData) {
137 // The workaround is to do a glBufferData followed by glBufferSubData.
138 // Chromium's command buffer may turn a glBufferSubData where the size
139 // exactly matches the buffer size into a glBufferData. So we tack 1
140 // extra byte onto the glBufferData.
141 GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, srcSizeInBytes + 1,
142 NULL, usage));
143 GL_CALL(BufferSubData(GR_GL_ARRAY_BUFFER, 0, srcSizeInBytes, src));
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000144 } else {
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000145 GL_CALL(BufferData(GR_GL_ARRAY_BUFFER, srcSizeInBytes, src, usage));
146 }
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000147#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 return true;
149}