blob: 9c019c149b07e4d9a8b2df1bff9affc18c152e53 [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 "GrGLIndexBuffer.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 +000017GrGLIndexBuffer::GrGLIndexBuffer(GrGpuGL* gpu,
18 GrGLuint id,
19 size_t sizeInBytes,
20 bool dynamic)
21 : INHERITED(gpu, sizeInBytes, dynamic)
22 , fBufferID(id)
23 , fLockPtr(NULL) {
24
reed@google.comac10a2d2010-12-22 21:39:39 +000025}
26
bsalomon@google.com8fe72472011-03-30 21:26:44 +000027void GrGLIndexBuffer::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->notifyIndexBufferDelete(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 GrGLIndexBuffer::onAbandon() {
37 fBufferID = 0;
38 fLockPtr = NULL;
39}
40
bsalomon@google.com1c13c962011-02-14 16:51:21 +000041void GrGLIndexBuffer::bind() const {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000042 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000043 GPUGL->notifyIndexBufferBind(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000044}
45
twiz@google.com0f31ca72011-03-18 17:38:11 +000046GrGLuint GrGLIndexBuffer::bufferID() const {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000047 return fBufferID;
48}
49
reed@google.comac10a2d2010-12-22 21:39:39 +000050void* GrGLIndexBuffer::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_ELEMENT_ARRAY_BUFFER,
57 this->sizeInBytes(),
58 NULL,
59 this->dynamic() ? GR_GL_DYNAMIC_DRAW :
60 GR_GL_STATIC_DRAW));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000061 GR_GL_CALL_RET(GPUGL->glInterface(),
62 fLockPtr,
63 MapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER,
64 GR_GL_WRITE_ONLY));
reed@google.comac10a2d2010-12-22 21:39:39 +000065
66 return fLockPtr;
67 }
68 return NULL;
69}
70
bsalomon@google.com1c13c962011-02-14 16:51:21 +000071void* GrGLIndexBuffer::lockPtr() const {
72 return fLockPtr;
73}
74
reed@google.comac10a2d2010-12-22 21:39:39 +000075void GrGLIndexBuffer::unlock() {
76 GrAssert(fBufferID);
77 GrAssert(isLocked());
bsalomon@google.com18c9c192011-09-22 21:01:31 +000078 GrAssert(this->getGpu()->getCaps().fBufferLockSupport);
reed@google.comac10a2d2010-12-22 21:39:39 +000079
bsalomon@google.com8fe72472011-03-30 21:26:44 +000080 this->bind();
bsalomon@google.com0b77d682011-08-19 13:28:54 +000081 GL_CALL(UnmapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER));
bsalomon@google.com1c13c962011-02-14 16:51:21 +000082 fLockPtr = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000083}
84
85bool GrGLIndexBuffer::isLocked() const {
bsalomon@google.comd850c182012-03-08 16:45:22 +000086 // this check causes a lot of noise in the gl log
87#if 0
bsalomon@google.com18c9c192011-09-22 21:01:31 +000088 if (this->isValid() && this->getGpu()->getCaps().fBufferLockSupport) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000089 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +000090 GrGLint mapped;
bsalomon@google.com0b77d682011-08-19 13:28:54 +000091 GL_CALL(GetBufferParameteriv(GR_GL_ELEMENT_ARRAY_BUFFER,
92 GR_GL_BUFFER_MAPPED, &mapped));
reed@google.comac10a2d2010-12-22 21:39:39 +000093 GrAssert(!!mapped == !!fLockPtr);
94 }
95#endif
96 return NULL != fLockPtr;
97}
98
bsalomon@google.com1c13c962011-02-14 16:51:21 +000099bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 GrAssert(fBufferID);
101 GrAssert(!isLocked());
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000102 if (srcSizeInBytes > this->sizeInBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 return false;
104 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000105 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +0000106 GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW;
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000107
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000108#if GR_GL_USE_BUFFER_DATA_NULL_HINT
109 if (this->sizeInBytes() == srcSizeInBytes) {
110 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
111 srcSizeInBytes, src, usage));
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000112 } else {
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000113 // 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.
119 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
120 this->sizeInBytes(), NULL, usage));
121 GL_CALL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER,
122 0, srcSizeInBytes, src));
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000123 }
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000124#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..))
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000128 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000129 srcSizeInBytes, src, usage));
130#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000131 return true;
132}
133