blob: 66ee09537a78b172133d24f914f7ff97a225402d [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 }
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 GrGLIndexBuffer::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 GrGLIndexBuffer::bind() const {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000046 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000047 GPUGL->notifyIndexBufferBind(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000048}
49
twiz@google.com0f31ca72011-03-18 17:38:11 +000050GrGLuint GrGLIndexBuffer::bufferID() const {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000051 return fBufferID;
52}
53
reed@google.comac10a2d2010-12-22 21:39:39 +000054void* GrGLIndexBuffer::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_ELEMENT_ARRAY_BUFFER,
61 this->sizeInBytes(),
62 NULL,
63 this->dynamic() ? GR_GL_DYNAMIC_DRAW :
64 GR_GL_STATIC_DRAW));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000065 GR_GL_CALL_RET(GPUGL->glInterface(),
66 fLockPtr,
67 MapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER,
68 GR_GL_WRITE_ONLY));
reed@google.comac10a2d2010-12-22 21:39:39 +000069
70 return fLockPtr;
71 }
72 return NULL;
73}
74
bsalomon@google.com1c13c962011-02-14 16:51:21 +000075void* GrGLIndexBuffer::lockPtr() const {
76 return fLockPtr;
77}
78
reed@google.comac10a2d2010-12-22 21:39:39 +000079void GrGLIndexBuffer::unlock() {
80 GrAssert(fBufferID);
81 GrAssert(isLocked());
bsalomon@google.comf6601872012-08-28 21:11:35 +000082 GrAssert(this->getGpu()->getCaps().bufferLockSupport());
reed@google.comac10a2d2010-12-22 21:39:39 +000083
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084 this->bind();
bsalomon@google.com0b77d682011-08-19 13:28:54 +000085 GL_CALL(UnmapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER));
bsalomon@google.com1c13c962011-02-14 16:51:21 +000086 fLockPtr = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000087}
88
89bool GrGLIndexBuffer::isLocked() const {
bsalomon@google.comd850c182012-03-08 16:45:22 +000090 // this check causes a lot of noise in the gl log
91#if 0
bsalomon@google.com18c9c192011-09-22 21:01:31 +000092 if (this->isValid() && this->getGpu()->getCaps().fBufferLockSupport) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +000094 GrGLint mapped;
bsalomon@google.com0b77d682011-08-19 13:28:54 +000095 GL_CALL(GetBufferParameteriv(GR_GL_ELEMENT_ARRAY_BUFFER,
96 GR_GL_BUFFER_MAPPED, &mapped));
reed@google.comac10a2d2010-12-22 21:39:39 +000097 GrAssert(!!mapped == !!fLockPtr);
98 }
99#endif
100 return NULL != fLockPtr;
101}
102
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000103bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 GrAssert(fBufferID);
105 GrAssert(!isLocked());
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000106 if (srcSizeInBytes > this->sizeInBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 return false;
108 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000109 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +0000110 GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW;
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000111
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000112#if GR_GL_USE_BUFFER_DATA_NULL_HINT
113 if (this->sizeInBytes() == srcSizeInBytes) {
114 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
115 srcSizeInBytes, src, usage));
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000116 } else {
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000117 // Before we call glBufferSubData we give the driver a hint using
118 // glBufferData with NULL. This makes the old buffer contents
119 // inaccessible to future draws. The GPU may still be processing
120 // draws that reference the old contents. With this hint it can
121 // assign a different allocation for the new contents to avoid
122 // flushing the gpu past draws consuming the old contents.
123 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
124 this->sizeInBytes(), NULL, usage));
125 GL_CALL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER,
126 0, srcSizeInBytes, src));
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000127 }
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000128#else
129 // Note that we're cheating on the size here. Currently no methods
130 // allow a partial update that preserves contents of non-updated
131 // portions of the buffer (lock() does a glBufferData(..size, NULL..))
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000132 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000133 srcSizeInBytes, src, usage));
134#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000135 return true;
136}
137