blob: 035bfaccce49497a144d367cf949507eaf6ca204 [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,
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) {
25
reed@google.comac10a2d2010-12-22 21:39:39 +000026}
27
bsalomon@google.com8fe72472011-03-30 21:26:44 +000028void GrGLIndexBuffer::onRelease() {
reed@google.comac10a2d2010-12-22 21:39:39 +000029 // make sure we've not been abandoned
bsalomon@google.com72830222013-01-23 20:25:22 +000030 if (fBufferID && !this->isWrapped()) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000031 GPUGL->notifyIndexBufferDelete(this);
bsalomon@google.com0b77d682011-08-19 13:28:54 +000032 GL_CALL(DeleteBuffers(1, &fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000033 fBufferID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000034 }
robertphillips@google.comd3645542012-09-05 18:37:39 +000035
36 INHERITED::onRelease();
reed@google.comac10a2d2010-12-22 21:39:39 +000037}
38
bsalomon@google.com8fe72472011-03-30 21:26:44 +000039void GrGLIndexBuffer::onAbandon() {
40 fBufferID = 0;
41 fLockPtr = NULL;
robertphillips@google.comd3645542012-09-05 18:37:39 +000042
43 INHERITED::onAbandon();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000044}
45
bsalomon@google.com1c13c962011-02-14 16:51:21 +000046void GrGLIndexBuffer::bind() const {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000047 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, fBufferID));
bsalomon@google.com8fe72472011-03-30 21:26:44 +000048 GPUGL->notifyIndexBufferBind(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000049}
50
reed@google.comac10a2d2010-12-22 21:39:39 +000051void* GrGLIndexBuffer::lock() {
52 GrAssert(fBufferID);
53 GrAssert(!isLocked());
bsalomon@google.comf6601872012-08-28 21:11:35 +000054 if (this->getGpu()->getCaps().bufferLockSupport()) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000055 this->bind();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000056 // Let driver know it can discard the old data
bsalomon@google.com0b77d682011-08-19 13:28:54 +000057 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
58 this->sizeInBytes(),
59 NULL,
60 this->dynamic() ? GR_GL_DYNAMIC_DRAW :
61 GR_GL_STATIC_DRAW));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000062 GR_GL_CALL_RET(GPUGL->glInterface(),
63 fLockPtr,
64 MapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER,
65 GR_GL_WRITE_ONLY));
reed@google.comac10a2d2010-12-22 21:39:39 +000066
67 return fLockPtr;
68 }
69 return NULL;
70}
71
bsalomon@google.com1c13c962011-02-14 16:51:21 +000072void* GrGLIndexBuffer::lockPtr() const {
73 return fLockPtr;
74}
75
reed@google.comac10a2d2010-12-22 21:39:39 +000076void GrGLIndexBuffer::unlock() {
77 GrAssert(fBufferID);
78 GrAssert(isLocked());
bsalomon@google.comf6601872012-08-28 21:11:35 +000079 GrAssert(this->getGpu()->getCaps().bufferLockSupport());
reed@google.comac10a2d2010-12-22 21:39:39 +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_ELEMENT_ARRAY_BUFFER));
bsalomon@google.com1c13c962011-02-14 16:51:21 +000083 fLockPtr = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000084}
85
86bool GrGLIndexBuffer::isLocked() const {
bsalomon@google.comd850c182012-03-08 16:45:22 +000087 // this check causes a lot of noise in the gl log
88#if 0
bsalomon@google.com18c9c192011-09-22 21:01:31 +000089 if (this->isValid() && this->getGpu()->getCaps().fBufferLockSupport) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000090 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +000091 GrGLint mapped;
bsalomon@google.com0b77d682011-08-19 13:28:54 +000092 GL_CALL(GetBufferParameteriv(GR_GL_ELEMENT_ARRAY_BUFFER,
93 GR_GL_BUFFER_MAPPED, &mapped));
reed@google.comac10a2d2010-12-22 21:39:39 +000094 GrAssert(!!mapped == !!fLockPtr);
95 }
96#endif
97 return NULL != fLockPtr;
98}
99
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000100bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 GrAssert(fBufferID);
102 GrAssert(!isLocked());
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000103 if (srcSizeInBytes > this->sizeInBytes()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 return false;
105 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000106 this->bind();
twiz@google.com0f31ca72011-03-18 17:38:11 +0000107 GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW;
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000108
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000109#if GR_GL_USE_BUFFER_DATA_NULL_HINT
110 if (this->sizeInBytes() == srcSizeInBytes) {
111 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
112 srcSizeInBytes, src, usage));
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000113 } else {
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000114 // 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.
120 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
121 this->sizeInBytes(), NULL, usage));
122 GL_CALL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER,
123 0, srcSizeInBytes, src));
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000124 }
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000125#else
126 // Note that we're cheating on the size here. Currently no methods
127 // allow a partial update that preserves contents of non-updated
128 // portions of the buffer (lock() does a glBufferData(..size, NULL..))
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000129 GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
bsalomon@google.comc1dd8882012-03-02 20:36:18 +0000130 srcSizeInBytes, src, usage));
131#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000132 return true;
133}